Add option to remove duplicate command history entries

Message ID 1433434918-30948-1-git-send-email-patrick@parcs.ath.cx
State New, archived
Headers

Commit Message

Patrick Palka June 4, 2015, 4:21 p.m. UTC
  This patch implements the new option "history remove-duplicates", which
controls whether GDB should remove duplicate command-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.

[ I decided to go with a basic boolean option instead of a numeric
  lookbehind_threshold option because it is simpler, and the maximum
  lookbehind is already restricted by the number of unique commands
  invoked during the session, a number which will rarely exceed 100.  ]

gdb/ChangeLog:

	* NEWS: Mention the new option "history remove-duplicates".
	* top.c (history_remove_duplicates_p): New static variable.
	(show_history_remove_duplicates_p): New static function.
	(gdb_add_history): Conditionally remove duplicate history
	entries.
	(init_main): Add "history remove-duplicates" option.

gdb/doc/ChangeLog:

	* gdb.texinfo: Document the new option
	"history remove-duplicates".

gdb/testsuite/ChangeLog:

	* gdb.base/history-duplicates.exp: New test.
---
 gdb/NEWS                                      |  4 ++
 gdb/doc/gdb.texinfo                           | 13 ++++++
 gdb/testsuite/gdb.base/history-duplicates.exp | 57 +++++++++++++++++++++++++++
 gdb/top.c                                     | 51 ++++++++++++++++++++++++
 4 files changed, 125 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/history-duplicates.exp
  

Comments

Eli Zaretskii June 4, 2015, 4:47 p.m. UTC | #1
> From: Patrick Palka <patrick@parcs.ath.cx>
> Cc: Patrick Palka <patrick@parcs.ath.cx>
> Date: Thu,  4 Jun 2015 12:21:58 -0400
> 
> This patch implements the new option "history remove-duplicates", which
> controls whether GDB should remove duplicate command-history entries
> (off by default).

Thanks.

> gdb/doc/ChangeLog:
> 
> 	* gdb.texinfo: Document the new option
> 	"history remove-duplicates".

This ChangeLog entry should name the node in which you made the
changes, as if it were a function (i.e., in parentheses).

> diff --git a/gdb/NEWS b/gdb/NEWS
> index bbfb55d..411be32 100644
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -123,6 +123,10 @@ show max-completions
>    to avoid generating large completion lists, the computation of
>    which can cause the debugger to become temporarily unresponsive.
>  
> +set history remove-duplicates
> +show history remove-duplicates
> +  Control the removal of duplicate history entries.

This part is OK.

> +@cindex remove duplicate history
> +@kindex set history remove-duplicates
> +@item set history remove-duplicates
> +@itemx set history remove-duplicates on
> +Remove duplicate history entries added during the current session.  Before a

Given the description below, this summary is slightly misleading,
IMO.  Why not simply

  Keep in history of CLI commands only one copy of each command.

> +entry it finds. This option is off by default.
                 ^^
Two spaces between sentences, please.

The documentation parts are okay with these fixed.
  
Patrick Palka June 4, 2015, 6:53 p.m. UTC | #2
On Thu, Jun 4, 2015 at 12:47 PM, Eli Zaretskii <eliz@gnu.org> wrote:
>> From: Patrick Palka <patrick@parcs.ath.cx>
>> Cc: Patrick Palka <patrick@parcs.ath.cx>
>> Date: Thu,  4 Jun 2015 12:21:58 -0400
>>
>> This patch implements the new option "history remove-duplicates", which
>> controls whether GDB should remove duplicate command-history entries
>> (off by default).
>
> Thanks.
>
>> gdb/doc/ChangeLog:
>>
>>       * gdb.texinfo: Document the new option
>>       "history remove-duplicates".
>
> This ChangeLog entry should name the node in which you made the
> changes, as if it were a function (i.e., in parentheses).
>
>> diff --git a/gdb/NEWS b/gdb/NEWS
>> index bbfb55d..411be32 100644
>> --- a/gdb/NEWS
>> +++ b/gdb/NEWS
>> @@ -123,6 +123,10 @@ show max-completions
>>    to avoid generating large completion lists, the computation of
>>    which can cause the debugger to become temporarily unresponsive.
>>
>> +set history remove-duplicates
>> +show history remove-duplicates
>> +  Control the removal of duplicate history entries.
>
> This part is OK.
>
>> +@cindex remove duplicate history
>> +@kindex set history remove-duplicates
>> +@item set history remove-duplicates
>> +@itemx set history remove-duplicates on
>> +Remove duplicate history entries added during the current session.  Before a
>
> Given the description below, this summary is slightly misleading,
> IMO.  Why not simply
>
>   Keep in history of CLI commands only one copy of each command.

How about I rewrite this section into:

 Keep in the command history list only one copy of each command.  If a new
 command being added to the history list is a duplicate of an older one, the
 older entry is removed from the list.  Only history entries added during the
 current session are considered for removal.  This option is off by default.

>
>> +entry it finds. This option is off by default.
>                  ^^
> Two spaces between sentences, please.
>
> The documentation parts are okay with these fixed.

Everything else fixed.  Thanks for reviewing.
  
Eli Zaretskii June 4, 2015, 7:25 p.m. UTC | #3
> From: Patrick Palka <patrick@parcs.ath.cx>
> Date: Thu, 4 Jun 2015 14:53:40 -0400
> Cc: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
> 
> >> +@cindex remove duplicate history
> >> +@kindex set history remove-duplicates
> >> +@item set history remove-duplicates
> >> +@itemx set history remove-duplicates on
> >> +Remove duplicate history entries added during the current session.  Before a
> >
> > Given the description below, this summary is slightly misleading,
> > IMO.  Why not simply
> >
> >   Keep in history of CLI commands only one copy of each command.
> 
> How about I rewrite this section into:
> 
>  Keep in the command history list only one copy of each command.  If a new
>  command being added to the history list is a duplicate of an older one, the
>  older entry is removed from the list.  Only history entries added during the
>  current session are considered for removal.  This option is off by default.

Perfect, thanks.
  
Pedro Alves June 9, 2015, 6:10 p.m. UTC | #4
On 06/04/2015 05:21 PM, Patrick Palka wrote:
> This patch implements the new option "history remove-duplicates", which
> controls whether GDB should remove duplicate command-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.
> 

Did you consider bash's erasedups and ignoredups?  Specifically,
this seems to implement something like erasedups, and I'm wondering
how you'd fit in ignoredups in this option's UI.  Might be good to
prepare for it with an enum instead, something like:

  "set history duplicates ignore|erase|leave"

WDYT?

(haven't looked at the patch yet)

Thanks,
Pedro Alves
  
Patrick Palka June 9, 2015, 6:40 p.m. UTC | #5
On Tue, Jun 9, 2015 at 2:10 PM, Pedro Alves <palves@redhat.com> wrote:
> On 06/04/2015 05:21 PM, Patrick Palka wrote:
>> This patch implements the new option "history remove-duplicates", which
>> controls whether GDB should remove duplicate command-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.
>>
>
> Did you consider bash's erasedups and ignoredups?  Specifically,
> this seems to implement something like erasedups, and I'm wondering
> how you'd fit in ignoredups in this option's UI.  Might be good to
> prepare for it with an enum instead, something like:
>
>   "set history duplicates ignore|erase|leave"
>
> WDYT?

An "ignoredups" option currently seems not useful in GDB since we
already have the empty-command shorthand for running the previous
command again, which does not add to the history.  But if we ever make
the empty-command shorthand toggle-able then an "ignoredups"
equivalent could be useful when the shorthand is turned off.  I am
actually thinking about implementing that too, since I do not like the
shorthand very much and would like to be able to turn it off.  So I
might as well implement "ignoredups" too.

>
> (haven't looked at the patch yet)
>
> Thanks,
> Pedro Alves
>
  
Pedro Alves June 10, 2015, 3:11 p.m. UTC | #6
On 06/09/2015 07:40 PM, Patrick Palka wrote:
> On Tue, Jun 9, 2015 at 2:10 PM, Pedro Alves <palves@redhat.com> wrote:
>> On 06/04/2015 05:21 PM, Patrick Palka wrote:
>>> This patch implements the new option "history remove-duplicates", which
>>> controls whether GDB should remove duplicate command-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.
>>>
>>
>> Did you consider bash's erasedups and ignoredups?  Specifically,
>> this seems to implement something like erasedups, and I'm wondering
>> how you'd fit in ignoredups in this option's UI.  Might be good to
>> prepare for it with an enum instead, something like:
>>
>>   "set history duplicates ignore|erase|leave"
>>
>> WDYT?
> 
> An "ignoredups" option currently seems not useful in GDB since we
> already have the empty-command shorthand for running the previous
> command again, which does not add to the history.  But if we ever make
> the empty-command shorthand toggle-able then an "ignoredups"
> equivalent could be useful when the shorthand is turned off.  I am
> actually thinking about implementing that too, since I do not like the
> shorthand very much and would like to be able to turn it off.  So I
> might as well implement "ignoredups" too.

Alright, sounds good.  I like the shorthand myself, but then
that's why these things have knobs.  :-)

Thanks,
Pedro Alves
  

Patch

diff --git a/gdb/NEWS b/gdb/NEWS
index bbfb55d..411be32 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -123,6 +123,10 @@  show max-completions
   to avoid generating large completion lists, the computation of
   which can cause the debugger to become temporarily unresponsive.
 
+set history remove-duplicates
+show history remove-duplicates
+  Control the removal of duplicate history entries.
+
 maint set symbol-cache-size
 maint show symbol-cache-size
   Control the size of the symbol cache.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 9ea846a..722186c 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -22607,6 +22607,19 @@  This defaults to the value of the environment variable
 @code{HISTSIZE}, or to 256 if this variable is not set.  If @var{size}
 is @code{unlimited}, the number of commands @value{GDBN} keeps in the
 history list is unlimited.
+
+@cindex remove duplicate history
+@kindex set history remove-duplicates
+@item set history remove-duplicates
+@itemx set history remove-duplicates on
+Remove duplicate history entries added during the current session.  Before a
+history entry is added while this option is on, @value{GDBN} scans the command
+history list in reverse-chronological order and removes the first duplicate
+entry it finds. This option is off by default.
+
+@item set history remove-duplicates off
+Do not remove duplicate history entries.
+
 @end table
 
 History expansion assigns special meaning to the character @kbd{!}.
diff --git a/gdb/testsuite/gdb.base/history-duplicates.exp b/gdb/testsuite/gdb.base/history-duplicates.exp
new file mode 100644
index 0000000..ef6f6a3
--- /dev/null
+++ b/gdb/testsuite/gdb.base/history-duplicates.exp
@@ -0,0 +1,57 @@ 
+# Copyright 2015 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# This file is part of the gdb testsuite.
+
+# Test the operation of the "history remove-duplicates" option.
+
+
+# Check that the previous history entry is ENTRY.
+
+proc check_prev_history_entry { entry } {
+    set test "history entry is $entry"
+
+    # Send ^P followed by ^L.
+    send_gdb "\x10\x0c"
+
+    gdb_expect {
+        -re $entry {
+            pass $test
+        }
+        timeout {
+            fail $test
+        }
+    }
+}
+
+gdb_start
+
+gdb_test "set history remove-duplicates" ""
+
+gdb_test "print 0" ""
+gdb_test "print 1" ""
+gdb_test "print 2" ""
+gdb_test "print 1" ""
+gdb_test "print 1" ""
+gdb_test "print 2" ""
+gdb_test "print 3" ""
+gdb_test "print 3" ""
+gdb_test "print 4" ""
+
+check_prev_history_entry "print 4"
+check_prev_history_entry "print 3"
+check_prev_history_entry "print 2"
+check_prev_history_entry "print 1"
+check_prev_history_entry "print 0"
diff --git a/gdb/top.c b/gdb/top.c
index f8f926b..f5a0819 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -698,6 +698,18 @@  show_history_size (struct ui_file *file, int from_tty,
 		    value);
 }
 
+/* Variable which controls whether duplicate history entries added during this
+   session are scanned for and removed.  */
+static int history_remove_duplicates_p = 0;
+
+static void
+show_history_remove_duplicates_p (struct ui_file *file, int from_tty,
+				  struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Removal of duplicate history entries is %s.\n"),
+		    value);
+}
+
 static char *history_filename;
 static void
 show_history_filename (struct ui_file *file, int from_tty,
@@ -897,6 +909,34 @@  static int command_count = 0;
 void
 gdb_add_history (const char *command)
 {
+  if (history_remove_duplicates_p)
+    {
+      int lookbehind;
+
+      /* Check to see whether we have an existing command history entry for
+	 this command -- and remove it if so -- but only among history entries
+	 added during this session.  Since we update the history file by
+	 appending to it, history entries that are already stored in the
+	 history file can't be meaningfully deleted.  */
+      using_history ();
+      for (lookbehind = 0; lookbehind < command_count; 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);
   command_count++;
 }
@@ -1862,6 +1902,17 @@  variable \"HISTSIZE\", or to 256 if this variable is not set."),
 			    show_history_size,
 			    &sethistlist, &showhistlist);
 
+  add_setshow_boolean_cmd ("remove-duplicates", no_class,
+			   &history_remove_duplicates_p, _("\
+Set removal of duplicate history entries added during this session."), _("\
+Show removal of duplicate history entries added during this session."), _("\
+Use \"on\" to enable removal of duplicate entries, and \"off\" to disable it.\n\
+Only history entries added during this session are considered for removal.\n\
+Without an argument, removal of duplicate entries is enabled."),
+			   NULL,
+			   show_history_remove_duplicates_p,
+			   &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"), _("\