public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Prune duplicate command history entries
@ 2015-06-03  3:22 Patrick Palka
  2015-06-03  8:20 ` Andrew Burgess
  0 siblings, 1 reply; 5+ messages in thread
From: Patrick Palka @ 2015-06-03  3:22 UTC (permalink / raw)
  To: gdb-patches; +Cc: Patrick Palka

This patch implements pruning of duplicate command-history entries using
a modest amount of lookbehind.  The motivation for this patch is 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.  By
pruning duplicate command-history entries we can significantly reduce
the occurrence of such entries, leaving the history file filled with
more interesting commands.

The maximum lookbehind is fixed to 50 (an arbitrary number) so that the
operation will be guaranteed to not take too long.

gdb/ChangeLog:

	* top.c (gdb_add_history): Prune duplicate command-history
	entries.
---
 gdb/top.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/gdb/top.c b/gdb/top.c
index 74e1e07..ec88fa3 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -897,6 +897,34 @@ static int command_count = 0;
 void
 gdb_add_history (const char *command)
 {
+  int lookbehind;
+  const int lookbehind_threshold = 50;
+
+  /* To help avoid cluttering the history file with entries for "up", "down",
+     "list", "bt", and so on, perform pruning of duplicate command-history
+     entries, 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 pruned.  */
+  using_history ();
+  for (lookbehind = 0;
+       lookbehind < command_count && 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);
   command_count++;
 }
-- 
2.4.2.387.gf86f31a.dirty

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-06-03 17:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-03  3:22 [PATCH] Prune duplicate command history entries Patrick Palka
2015-06-03  8:20 ` Andrew Burgess
2015-06-03 14:16   ` Patrick Palka
2015-06-03 17:42     ` Andrew Burgess
2015-06-03 17:10   ` Joel Brobecker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).