aboutsummaryrefslogtreecommitdiff
path: root/gdb/top.c
diff options
context:
space:
mode:
authorPatrick Palka <patrick@parcs.ath.cx>2015-06-02 22:49:15 -0400
committerPatrick Palka <patrick@parcs.ath.cx>2015-06-26 11:05:56 -0400
commitfc637f04c741b08726cc1631428bf094235ecb4e (patch)
tree1bb91721617e984a814138fd3fb0401af3541ba4 /gdb/top.c
parent2e52ae68e7cedc3a1f9908c98ee60a8602705835 (diff)
downloadgdb-fc637f04c741b08726cc1631428bf094235ecb4e.zip
gdb-fc637f04c741b08726cc1631428bf094235ecb4e.tar.gz
gdb-fc637f04c741b08726cc1631428bf094235ecb4e.tar.bz2
Add option to remove duplicate command history entries
This patch implements the new option "history remove-duplicates", which controls the removal of duplicate history entries ("off" by default). The motivation for this option is to be able to reduce the prevalence of basic commands such as "up" and "down" in the history file. These common commands crowd out more unique commands in the history file (when the history file has a fixed size), and they make navigation of the history file via ^P, ^N and ^R more inconvenient. The option takes an integer denoting the number of history entries to look back at for a history entry that is a duplicate of the latest one. "history remove-duplicates 1" is equivalent to bash's ignoredups option, and "history remove-duplicates unlimited" is equivalent to bash's erasedups option. [ I decided to go with this integer approach instead of a tri-state enum because it's slightly more flexible and seemingly more intuitive than leave/erase/ignore. ] gdb/ChangeLog: * NEWS: Mention the new option "history remove-duplicates". * top.c (history_remove_duplicates): New static variable. (show_history_remove_duplicates): New static function. (gdb_add_history): Conditionally remove duplicate history entries. (init_main): Add "history remove-duplicates" option. gdb/doc/ChangeLog: * gdb.texinfo (Command History): Document the new option "history remove-duplicates". gdb/testsuite/ChangeLog: * gdb.base/history-duplicates.exp: New test.
Diffstat (limited to 'gdb/top.c')
-rw-r--r--gdb/top.c66
1 files changed, 65 insertions, 1 deletions
diff --git a/gdb/top.c b/gdb/top.c
index 77fe096..01fddd2 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -698,6 +698,20 @@ show_history_size (struct ui_file *file, int from_tty,
value);
}
+/* Variable associated with the "history remove-duplicates" option.
+ The value -1 means unlimited. */
+static int history_remove_duplicates = 0;
+
+static void
+show_history_remove_duplicates (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ fprintf_filtered (file,
+ _("The number of history entries to look back at for "
+ "duplicates is %s.\n"),
+ value);
+}
+
static char *history_filename;
static void
show_history_filename (struct ui_file *file, int from_tty,
@@ -897,8 +911,43 @@ static int command_count = 0;
void
gdb_add_history (const char *command)
{
- add_history (command);
command_count++;
+
+ if (history_remove_duplicates != 0)
+ {
+ int lookbehind;
+ int lookbehind_threshold;
+
+ /* The lookbehind threshold for finding a duplicate history entry is
+ bounded by command_count because we can't meaningfully delete
+ history entries that are already stored in the history file since
+ the history file is appended to. */
+ if (history_remove_duplicates == -1
+ || history_remove_duplicates > command_count)
+ lookbehind_threshold = command_count;
+ else
+ lookbehind_threshold = history_remove_duplicates;
+
+ using_history ();
+ for (lookbehind = 0; lookbehind < lookbehind_threshold; lookbehind++)
+ {
+ HIST_ENTRY *temp = previous_history ();
+
+ if (temp == NULL)
+ break;
+
+ if (strcmp (temp->line, command) == 0)
+ {
+ HIST_ENTRY *prev = remove_history (where_history ());
+ command_count--;
+ free_history_entry (prev);
+ break;
+ }
+ }
+ using_history ();
+ }
+
+ add_history (command);
}
/* Safely append new history entries to the history file in a corruption-free
@@ -1880,6 +1929,21 @@ variable \"GDBHISTSIZE\", or to 256 if this variable is not set."),
show_history_size,
&sethistlist, &showhistlist);
+ add_setshow_zuinteger_unlimited_cmd ("remove-duplicates", no_class,
+ &history_remove_duplicates, _("\
+Set how far back in history to look for and remove duplicate entries."), _("\
+Show how far back in history to look for and remove duplicate entries."), _("\
+If set to a nonzero value N, GDB will look back at the last N history entries\n\
+and remove the first history entry that is a duplicate of the most recent\n\
+entry, each time a new history entry is added.\n\
+If set to \"unlimited\", this lookbehind is unbounded.\n\
+Only history entries added during this session are considered for removal.\n\
+If set to 0, removal of duplicate history entries is disabled.\n\
+By default this option is set to 0."),
+ NULL,
+ show_history_remove_duplicates,
+ &sethistlist, &showhistlist);
+
add_setshow_filename_cmd ("filename", no_class, &history_filename, _("\
Set the filename in which to record the command history"), _("\
Show the filename in which to record the command history"), _("\