>From 1f93cced670e309347ef3a93b537bce34b2903ff Mon Sep 17 00:00:00 2001 From: Bernhard R. Link Date: Mon, 9 Jun 2008 23:50:27 +0200 Subject: [PATCH] define different history types and use them (implementation does not separate them yet, though) --- src/actions.c | 41 ++++++++++++++++++++++------------------- src/data.h | 1 + src/editor.c | 11 ++++++----- src/editor.h | 2 +- src/history.c | 25 ++++++++++++++++++------- src/history.h | 16 ++++++++++++---- src/input.c | 8 ++++---- src/input.h | 4 ++-- src/ratpoison.h | 2 -- 9 files changed, 66 insertions(+), 44 deletions(-) diff --git a/src/actions.c b/src/actions.c index 4365679..c38731f 100644 --- a/src/actions.c +++ b/src/actions.c @@ -1313,7 +1313,8 @@ cmd_select (int interactive, struct cmdarg **args) /* FIXME: This is manually done because of the kinds of things select accepts. */ if (args[0] == NULL) - str = get_input (MESSAGE_PROMPT_SWITCH_TO_WINDOW, window_completions); + str = get_input (MESSAGE_PROMPT_SWITCH_TO_WINDOW, hist_SELECT, + window_completions); else str = xstrdup (ARG_STRING(0)); @@ -1453,14 +1454,14 @@ frame_selector_match (char ch) } static cmdret * -read_string (struct argspec *spec, struct sbuf *s, completion_fn fn, struct cmdarg **arg) +read_string (struct argspec *spec, struct sbuf *s, int history_id, completion_fn fn, struct cmdarg **arg) { char *input; if (s) input = xstrdup (sbuf_get(s)); else - input = get_input (spec->prompt, fn); + input = get_input (spec->prompt, history_id, fn); if (input) { @@ -1481,7 +1482,7 @@ read_keymap (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get (s)); else - input = get_input (spec->prompt, keymap_completions); + input = get_input (spec->prompt, hist_KEYMAP, keymap_completions); if (input) { @@ -1507,7 +1508,7 @@ read_keydesc (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get (s)); else - input = get_input (spec->prompt, trivial_completions); + input = get_input (spec->prompt, hist_KEY, trivial_completions); if (input) { @@ -1602,7 +1603,7 @@ colon_completions (char* str) static cmdret * read_command (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) { - return read_string (spec, s, colon_completions, arg); + return read_string (spec, s, hist_COMMAND, colon_completions, arg); } static struct list_head * @@ -1668,7 +1669,7 @@ exec_completions (char *str) static cmdret * read_shellcmd (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) { - return read_string (spec, s, exec_completions, arg); + return read_string (spec, s, hist_SHELLCMD, exec_completions, arg); } /* Return NULL on abort/failure. */ @@ -1795,7 +1796,7 @@ read_window (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) name = xstrdup (sbuf_get (s)); else - name = get_input (spec->prompt, window_completions); + name = get_input (spec->prompt, hist_WINDOW, window_completions); if (name) { @@ -1868,7 +1869,7 @@ read_gravity (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get(s)); else - input = get_input (spec->prompt , trivial_completions); + input = get_input (spec->prompt, hist_GRAVITY, trivial_completions); if (input) { @@ -1919,7 +1920,7 @@ read_group (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get(s)); else - input = get_input (spec->prompt , group_completions); + input = get_input (spec->prompt, hist_GROUP, group_completions); if (input) { @@ -1975,7 +1976,7 @@ read_hook (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get(s)); else - input = get_input (spec->prompt , hook_completions); + input = get_input (spec->prompt, hist_HOOK, hook_completions); if (input) { @@ -2043,7 +2044,7 @@ read_variable (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get(s)); else - input = get_input (spec->prompt , var_completions); + input = get_input (spec->prompt, hist_VARIABLE, var_completions); if (input) { @@ -2074,7 +2075,8 @@ read_number (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) if (s) input = xstrdup (sbuf_get(s)); else - input = get_input (spec->prompt , trivial_completions); + /* numbers should perhaps be more fine grained, or hist_NONE */ + input = get_input (spec->prompt, hist_OTHER, trivial_completions); if (input) { @@ -2099,7 +2101,7 @@ read_arg (struct argspec *spec, struct sbuf *s, struct cmdarg **arg) case arg_STRING: case arg_REST: case arg_RAW: - ret = read_string (spec, s, trivial_completions, arg); + ret = read_string (spec, s, hist_OTHER, trivial_completions, arg); break; case arg_KEYMAP: ret = read_keymap (spec, s, arg); @@ -2511,9 +2513,10 @@ cmd_colon (int interactive, struct cmdarg **args) char *input; if (args[0] == NULL) - input = get_input (MESSAGE_PROMPT_COMMAND, colon_completions); + input = get_input (MESSAGE_PROMPT_COMMAND, hist_COMMAND, colon_completions); else - input = get_more_input (MESSAGE_PROMPT_COMMAND, ARG_STRING(0), colon_completions); + input = get_more_input (MESSAGE_PROMPT_COMMAND, ARG_STRING(0), hist_COMMAND, + colon_completions); /* User aborted. */ if (input == NULL) @@ -5491,7 +5494,7 @@ cmd_prompt (int interactive, struct cmdarg **args) char *query, *output, *prefix; if (args[0] == NULL) - output = get_input(MESSAGE_PROMPT_COMMAND, trivial_completions); + output = get_input(MESSAGE_PROMPT_COMMAND, hist_PROMPT, trivial_completions); else { prefix = strchr (ARG_STRING(0), ':'); @@ -5501,12 +5504,12 @@ cmd_prompt (int interactive, struct cmdarg **args) query = xmalloc (prefix - ARG_STRING(0) + 1); strncpy (query, ARG_STRING(0), prefix - ARG_STRING(0)); query[prefix - ARG_STRING(0)] = 0; /* null terminate */ - output = get_more_input (query, prefix, trivial_completions); + output = get_more_input (query, prefix, hist_PROMPT, trivial_completions); free (query); } else { - output = get_input (ARG_STRING(0), trivial_completions); + output = get_input (ARG_STRING(0), hist_PROMPT, trivial_completions); } } ret = cmdret_new (RET_SUCCESS, "%s", output); diff --git a/src/data.h b/src/data.h index 1d5a1a8..814e86e 100644 --- a/src/data.h +++ b/src/data.h @@ -354,6 +354,7 @@ struct rp_input_line int size; rp_completions *compl; Atom selection; + int history_id; }; /* The hook dictionary. */ diff --git a/src/editor.c b/src/editor.c index 21fbe63..b2a72f9 100644 --- a/src/editor.c +++ b/src/editor.c @@ -102,13 +102,14 @@ static edit_binding edit_bindings[] = { {0, 0}, 0} }; rp_input_line * -input_line_new (char *prompt, char *preinput, completion_fn fn) +input_line_new (char *prompt, char *preinput, int history_id, completion_fn fn) { rp_input_line *line; line = xmalloc (sizeof (rp_input_line)); line->prompt = prompt; line->compl = completions_new (fn); + line->history_id = history_id; /* Allocate some memory to start with */ line->size = strlen (preinput) + 100; @@ -358,7 +359,7 @@ static edit_status editor_history_previous (rp_input_line *line) { #ifdef HAVE_HISTORY - char *entry = history_previous (); + const char *entry = history_previous (line->history_id); if (entry) { @@ -395,7 +396,7 @@ static edit_status editor_history_next (rp_input_line *line) { #ifdef HAVE_HISTORY - char *entry = history_next (); + const char *entry = history_next (line->history_id); if (entry) { @@ -473,7 +474,7 @@ editor_enter (rp_input_line *line) line->buffer[line->length] = '\0'; #ifdef HAVE_HISTORY - result = history_expand_line (line->buffer, &expansion); + result = history_expand_line (line->history_id, line->buffer, &expansion); PRINT_DEBUG (("History Expansion - result: %d\n", result)); PRINT_DEBUG (("History Expansion - expansion: \'%s\'\n", expansion)); @@ -486,7 +487,7 @@ editor_enter (rp_input_line *line) } else /* result == 0 || result == 1 */ { - history_add (expansion); + history_add (line->history_id, expansion); free (line->buffer); line->buffer = expansion; } diff --git a/src/editor.h b/src/editor.h index b2cf558..f3ffa31 100644 --- a/src/editor.h +++ b/src/editor.h @@ -36,7 +36,7 @@ edit_status }; /* Input line functions */ -rp_input_line *input_line_new (char *prompt, char *preinput, completion_fn fn); +rp_input_line *input_line_new (char *prompt, char *preinput, int history_id, completion_fn fn); void input_line_free (rp_input_line *line); edit_status execute_edit_action (rp_input_line *line, KeySym ch, unsigned int modifier, char *keysym_buf); diff --git a/src/history.c b/src/history.c index 4ec8b2e..28292f6 100644 --- a/src/history.c +++ b/src/history.c @@ -82,9 +82,14 @@ history_reset (void) } void -history_add (char *item) +history_add (int history_id, char *item) { - HIST_ENTRY *h = history_get (history_length); + HIST_ENTRY *h; + + if (history_id == hist_NONE) + return; + + h = history_get (history_length); if (item == NULL || *item == '\0' || isspace (*item) || (h != NULL && !strcmp (h->line, item))) return; @@ -93,27 +98,33 @@ history_add (char *item) add_history (item); } -char * -history_previous (void) +const char * +history_previous (int history_id) { HIST_ENTRY *h = NULL; + if (history_id == hist_NONE) + return NULL; + h = previous_history(); return h ? h->line : NULL; } -char * -history_next (void) +const char * +history_next (int history_id) { HIST_ENTRY *h = NULL; + if (history_id == hist_NONE) + return NULL; + h = next_history(); return h ? h->line : NULL; } -int history_expand_line (char *string, char **output) +int history_expand_line (int history_id, char *string, char **output) { return history_expand (string, output); } diff --git a/src/history.h b/src/history.h index b133df1..f6b6dd9 100644 --- a/src/history.h +++ b/src/history.h @@ -21,13 +21,21 @@ #ifndef _RATPOISON_HISTORY_H #define _RATPOISON_HISTORY_H 1 +enum { hist_NONE=0, hist_COMMAND, hist_SHELLCMD, + hist_SELECT, hist_KEYMAP, hist_KEY, + hist_WINDOW, hist_GRAVITY, hist_GROUP, + hist_HOOK, hist_VARIABLE, hist_PROMPT, + hist_OTHER, + /* must be last, do not use, for length only: */ + hist_COUNT}; + void history_load (void); void history_save (void); void history_resize (int size); void history_reset (void); -void history_add (char *item); -char *history_next (void); -char *history_previous (void); -int history_expand_line (char *string, char **output); +void history_add (int, char *item); +const char *history_next (int); +const char *history_previous (int); +int history_expand_line (int, char *string, char **output); #endif /* ! _RATPOISON_HISTORY_H */ diff --git a/src/input.c b/src/input.c index c92acd8..db36ba7 100644 --- a/src/input.c +++ b/src/input.c @@ -515,13 +515,13 @@ ring_bell (void) } char * -get_input (char *prompt, completion_fn fn) +get_input (char *prompt, int history_id, completion_fn fn) { - return get_more_input (prompt, "", fn); + return get_more_input (prompt, "", history_id, fn); } char * -get_more_input (char *prompt, char *preinput, +get_more_input (char *prompt, char *preinput, int history_id, completion_fn compl_fn) { /* Emacs 21 uses a 513 byte string to store the keysym name. */ @@ -542,7 +542,7 @@ get_more_input (char *prompt, char *preinput, #endif /* HAVE_READLINE_HISTORY_H */ /* Create our line structure */ - line = input_line_new (prompt, preinput, compl_fn); + line = input_line_new (prompt, preinput, history_id, compl_fn); /* We don't want to draw overtop of the program bar. */ hide_bar (s); diff --git a/src/input.h b/src/input.h index 1a421c3..5beb96e 100644 --- a/src/input.h +++ b/src/input.h @@ -24,8 +24,8 @@ char *keysym_to_string (KeySym keysym, unsigned int modifier); int cook_keycode (XKeyEvent *ev, KeySym *keysym, unsigned int *mod, char *keysym_name, int len, int ignore_bad_mods); -char *get_input (char *prompt, completion_fn fn); -char *get_more_input (char *prompt, char *preinput, completion_fn fn); +char *get_input (char *prompt, int history_id, completion_fn fn); +char *get_more_input (char *prompt, char *preinput, int history_id, completion_fn fn); void read_any_key (void); int read_single_key (KeySym *keysym, unsigned int *modifiers, char *keysym_name, int len); int read_key (KeySym *keysym, unsigned int *modifiers, char *keysym_name, int len); diff --git a/src/ratpoison.h b/src/ratpoison.h index b0690f6..8004d36 100644 --- a/src/ratpoison.h +++ b/src/ratpoison.h @@ -82,9 +82,7 @@ extern XGCValues gv; #include "screen.h" #include "group.h" #include "editor.h" -#ifdef HAVE_HISTORY #include "history.h" -#endif /* HAVE_HISTORY */ #include "completions.h" #include "hook.h" #include "xinerama.h" -- 1.4.4.4