From 89e5cc319ea49d8b982c6b5bf82587bed0995b75 Mon Sep 17 00:00:00 2001 From: Pavel Roskin Date: Tue, 18 Feb 2003 05:25:45 +0000 Subject: [PATCH] * panel.h: Convert directory history to GList. * widget.h: Convert input history to GList. Adjust all dependencies. --- src/ChangeLog | 6 ++ src/cmd.c | 12 ++-- src/main.c | 48 ++++++------- src/panel.h | 2 +- src/screen.c | 16 ++--- src/widget.c | 189 +++++++++++++++++++++++++------------------------- src/widget.h | 14 ++-- 7 files changed, 138 insertions(+), 149 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 8e5f38c8c..1717fefa8 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2003-02-18 Pavel Roskin + + * panel.h: Convert directory history to GList. + * widget.h: Convert input history to GList. + Adjust all dependencies. + 2003-02-11 Pavel Roskin * cmd.c (edit_cmd_new): Call editor with NULL argiment, not with diff --git a/src/cmd.c b/src/cmd.c index a35848a94..7ad35fbfd 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -921,7 +921,7 @@ void history_cmd (void) { Listbox *listbox; - Hist *current; + GList *current; if (cmdline->need_push) { if (push_history (cmdline, cmdline->buffer) == 2) @@ -931,14 +931,12 @@ history_cmd (void) message (1, MSG_ERROR, _(" The command history is empty ")); return; } - current = cmdline->history; - while (current->prev) - current = current->prev; + current = g_list_first (cmdline->history); listbox = create_listbox_window (60, 10, _(" Command history "), "[Command Menu]"); while (current) { - LISTBOX_APPEND_TEXT (listbox, 0, current->text, current); - current = current->next; + LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current); + current = g_list_next(current); } run_dlg (listbox->dlg); if (listbox->dlg->ret_value == B_CANCEL) @@ -951,7 +949,7 @@ history_cmd (void) if (!current) return; cmdline->history = current; - assign_text (cmdline, cmdline->history->text); + assign_text (cmdline, (char *) current->data); update_input (cmdline, 1); } diff --git a/src/main.c b/src/main.c index 3f0c46040..fc18e9012 100644 --- a/src/main.c +++ b/src/main.c @@ -692,26 +692,12 @@ subshell_chdir (char *directory) void directory_history_add (struct WPanel *panel, char *s) { - if (!panel->dir_history) { - panel->dir_history = g_new0 (Hist, 1); - panel->dir_history->text = g_strdup (s); - strip_password (panel->dir_history->text, 1); - return; - } - if (!strcmp (panel->dir_history->text, s)) - return; - if (panel->dir_history->next) { - if (panel->dir_history->next->text) { - g_free (panel->dir_history->next->text); - panel->dir_history->next->text = 0; - } - } else { - panel->dir_history->next = g_new0 (Hist, 1); - panel->dir_history->next->prev = panel->dir_history; - } - panel->dir_history = panel->dir_history->next; - panel->dir_history->text = g_strdup (s); - strip_password (panel->dir_history->text, 1); + char *text; + + text = g_strdup (s); + strip_password (s, 1); + + panel->dir_history = g_list_append (panel->dir_history, text); } /* @@ -818,19 +804,29 @@ do_cd (char *new_dir, enum cd_enum exact) void directory_history_next (WPanel *panel) { - if (!panel->dir_history->next) + GList *nextdir; + + nextdir = g_list_next (panel->dir_history); + + if (!nextdir) return; - if (_do_panel_cd (panel, panel->dir_history->next->text, cd_exact)) - panel->dir_history = panel->dir_history->next; + + if (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)) + panel->dir_history = nextdir; } void directory_history_prev (WPanel *panel) { - if (!panel->dir_history->prev) + GList *prevdir; + + prevdir = g_list_previous (panel->dir_history); + + if (!prevdir) return; - if (_do_panel_cd (panel, panel->dir_history->prev->text, cd_exact)) - panel->dir_history = panel->dir_history->prev; + + if (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)) + panel->dir_history = prevdir; } void diff --git a/src/panel.h b/src/panel.h index cd139487a..a2e4516fb 100644 --- a/src/panel.h +++ b/src/panel.h @@ -41,7 +41,7 @@ typedef struct WPanel { int active; /* If panel is currently selected */ char cwd [MC_MAXPATHLEN];/* Current Working Directory */ char lwd [MC_MAXPATHLEN];/* Last Working Directory */ - struct hist_entry *dir_history; /* directory history */ + GList *dir_history; /* directory history */ char *hist_name; /* directory history name for history file */ int count; /* Number of files in dir structure */ int marked; /* Count of marked files */ diff --git a/src/screen.c b/src/screen.c index 7f9d0ab8d..44f47b294 100644 --- a/src/screen.c +++ b/src/screen.c @@ -895,20 +895,14 @@ panel_destroy (WPanel *p) panel_save_setup (p, name); panel_clean_dir (p); -/* save and clean history */ + /* save and clean history */ if (p->dir_history){ - Hist *current, *old; history_put (p->hist_name, p->dir_history); - current = p->dir_history; - while (current->next) - current = current->next; - while (current){ - old = current; - current = current->prev; - g_free (old->text); - g_free (old); - } + + g_list_foreach (p->dir_history, (GFunc) g_free, NULL); + g_list_free (p->dir_history); } + g_free (p->hist_name); delete_format (p->format); diff --git a/src/widget.c b/src/widget.c index 9efdcee22..845e2d08c 100644 --- a/src/widget.c +++ b/src/widget.c @@ -787,10 +787,12 @@ winput_set_origin (WInput *in, int x, int field_len) /* {{{ history saving and loading */ +int num_history_items_recorded = 60; + /* This loads and saves the history of an input line to and from the widget. It is called with the widgets tk name on creation of the - widget, and returns the Hist list. It stores histories in the file + widget, and returns the GList list. It stores histories in the file ~/.mc/history in using the profile code. If def_text is passed as INPUT_LAST_TEXT (to the input_new() @@ -798,17 +800,15 @@ winput_set_origin (WInput *in, int x, int field_len) entered, or "" if not found. */ -int num_history_items_recorded = 60; - -Hist * +GList * history_get (char *input_name) { int i; - Hist *old, *new; + GList *hist; char *profile; - - old = new = NULL; - + + hist = NULL; + if (!num_history_items_recorded) /* this is how to disable */ return 0; if (!input_name) @@ -820,22 +820,21 @@ history_get (char *input_name) char key_name[BUF_TINY]; char this_entry[BUF_LARGE]; g_snprintf (key_name, sizeof (key_name), "%d", i); - GetPrivateProfileString (input_name, key_name, "", this_entry, sizeof (this_entry), profile); + GetPrivateProfileString (input_name, key_name, "", this_entry, + sizeof (this_entry), profile); if (!*this_entry) break; - new = g_new0 (Hist, 1); - new->text = g_strdup (this_entry); - new->prev = old; /* set up list pointers */ - if (old) - old->next = new; - old = new; + + hist = g_list_append (hist, g_strdup (this_entry)); } g_free (profile); - return new; /* return pointer to last entry in list */ + + /* return pointer to last entry in list */ + return hist; } void -history_put (char *input_name, Hist *h) +history_put (char *input_name, GList *h) { int i; char *profile; @@ -845,7 +844,7 @@ history_put (char *input_name, Hist *h) if (!*input_name) return; - + if (!h) return; @@ -856,34 +855,38 @@ history_put (char *input_name, Hist *h) if ((i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) != -1) close (i); - /* Just in case I forgot to strip passwords somewhere -- Norbert */ - if (chmod (profile, S_IRUSR | S_IWUSR) == -1 && errno != ENOENT){ + + /* Make sure the history is only readable by the user */ + if (chmod (profile, S_IRUSR | S_IWUSR) == -1 && errno != ENOENT) { g_free (profile); return; } - while (h->next) /* go to end of list */ - h = h->next; + /* go to end of list */ + h = g_list_last (h); /* go back 60 places */ - for (i = 0; i < num_history_items_recorded - 1 && h->prev; i++) - h = h->prev; + for (i = 0; i < num_history_items_recorded - 1 && h->prev; i++) + h = g_list_previous (h); if (input_name) profile_clean_section (input_name, profile); /* dump histories into profile */ - for (i = 0; h; h = h->next){ - if (h->text){ + for (i = 0; h; h = g_list_next (h)) { + char *text; - /* probably aren't any null entries, but lets be sure */ - if (*(h->text)){ - char key_name[BUF_TINY]; - g_snprintf (key_name, sizeof(key_name), "%d", i++); - WritePrivateProfileString (input_name, key_name, h->text, profile); - } + text = (char *) h->data; + + /* We shouldn't have null entries, but let's be sure */ + if (text && *text) { + char key_name[BUF_TINY]; + g_snprintf (key_name, sizeof (key_name), "%d", i++); + WritePrivateProfileString (input_name, key_name, text, + profile); } } + g_free (profile); } @@ -905,9 +908,9 @@ i18n_htitle (void) static inline int listbox_fwd (WListbox *l); char * -show_hist (Hist * history, int widget_x, int widget_y) +show_hist (GList *history, int widget_x, int widget_y) { - Hist *hi, *z; + GList *hi, *z; size_t maxlen = strlen (i18n_htitle ()), i, count = 0; int x, y, w, h; char *q, *r = 0; @@ -918,14 +921,13 @@ show_hist (Hist * history, int widget_x, int widget_y) if (!z) return 0; - while (z->prev) /* goto first */ - z = z->prev; + z = g_list_first (history); hi = z; while (hi) { - if ((i = strlen (hi->text)) > maxlen) + if ((i = strlen ((char *) hi->data)) > maxlen) maxlen = i; count++; - hi = hi->next; + hi = g_list_next (hi); } y = widget_y; @@ -954,17 +956,18 @@ show_hist (Hist * history, int widget_x, int widget_y) add_widget (query_dlg, query_list); hi = z; if (y < widget_y) { - while (hi) { /* traverse */ - listbox_add_item (query_list, 0, 0, hi->text, NULL); - hi = hi->next; + /* traverse */ + while (hi) { + listbox_add_item (query_list, 0, 0, (char *) hi->data, NULL); + hi = g_list_next (hi); } while (listbox_fwd (query_list)); } else { - while (hi->next) - hi = hi->next; - while (hi) { /* traverse backwards */ - listbox_add_item (query_list, 0, 0, hi->text, NULL); - hi = hi->prev; + /* traverse backwards */ + hi = g_list_last (history); + while (hi) { + listbox_add_item (query_list, 0, 0, (char *) hi->data, NULL); + hi = g_list_previous (hi); } } run_dlg (query_dlg); @@ -999,22 +1002,15 @@ input_destroy (WInput *in) } new_input (in); - if (in->history){ - Hist *current, *old; + if (in->history){ if (!in->is_password) /* don't save passwords ;-) */ history_put (in->history_name, in->history); - current = in->history; - while (current->next) - current = current->next; - while (current){ - old = current; - current = current->prev; - g_free (old->text); - g_free (old); - } + g_list_foreach (in->history, (GFunc) g_free, NULL); + g_list_free (in->history); } + g_free (in->buffer); free_completions (in); if (in->history_name) @@ -1049,44 +1045,43 @@ push_history (WInput *in, char *text) N_(" FTP to machine "), N_(" SMB link to machine ") }; - Hist *new; + char *t; char *p; int i; if (!i18n) { i18n = 1; - for (i = 0; i < ELEMENTS(password_input_fields); i++) + for (i = 0; i < ELEMENTS (password_input_fields); i++) password_input_fields[i] = _(password_input_fields[i]); } - + for (p = text; *p == ' ' || *p == '\t'; p++); if (!*p) - return 0; - if (in->history){ - while (in->history->next) - in->history = in->history->next; - if (!strcmp (in->history->text, text)) + return 0; + + if (in->history) { + /* Avoid duplicated entries */ + in->history = g_list_last (in->history); + if (!strcmp ((char *) in->history->data, text)) return 1; - new = g_new (Hist, 1); - in->history->next = new; - } else - new = g_new (Hist, 1); - in->need_push = 0; - new->next = 0; - new->prev = in->history; - new->text = g_strdup (text); - if (in->history_name) { - p = in->history_name + 3; - for (i = 0; i < ELEMENTS(password_input_fields); i++) - if (strcmp (p, password_input_fields[i]) == 0) - break; - if (i < ELEMENTS(password_input_fields)) - strip_password (new->text, 0); - else - strip_password (new->text, 1); } - - in->history = new; + + t = g_strdup (text); + + if (in->history_name) { + p = in->history_name + 3; + for (i = 0; i < ELEMENTS (password_input_fields); i++) + if (strcmp (p, password_input_fields[i]) == 0) + break; + if (i < ELEMENTS (password_input_fields)) + strip_password (t, 0); + else + strip_password (t, 1); + } + + in->history = g_list_append (in->history, t); + in->need_push = 0; + return 2; } @@ -1351,15 +1346,21 @@ hist_prev (WInput *in) if (in->need_push) { switch (push_history (in, in->buffer)) { - case 2: in->history = in->history->prev; break; - case 1: if (in->history->prev) in->history = in->history->prev; break; - case 0: break; + case 2: + in->history = g_list_previous (in->history); + break; + case 1: + if (in->history->prev) + in->history = g_list_previous (in->history); + break; + case 0: + break; } } else if (in->history->prev) - in->history = in->history->prev; + in->history = g_list_previous (in->history); else - return; - assign_text (in, in->history->text); + return; + assign_text (in, (char *) in->history->data); in->need_push = 0; } @@ -1384,8 +1385,8 @@ hist_next (WInput *in) return; } - in->history = in->history->next; - assign_text (in, in->history->text); + in->history = g_list_next (in->history); + assign_text (in, (char *) in->history->data); in->need_push = 0; } @@ -1623,8 +1624,8 @@ input_new (int y, int x, int color, int len, const char *def_text, char *tkname) if (def_text == INPUT_LAST_TEXT) { def_text = ""; if (in->history) - if (in->history->text) - def_text = in->history->text; + if (in->history->data) + def_text = (char *) in->history->data; } initial_buffer_len = 1 + max (len, strlen (def_text)); in->widget.options |= W_IS_INPUT; diff --git a/src/widget.h b/src/widget.h index cc94ab6e7..68a1cae5b 100644 --- a/src/widget.h +++ b/src/widget.h @@ -50,15 +50,9 @@ typedef struct WGauge { int current; } WGauge; -typedef struct hist_entry { - struct hist_entry *prev; - struct hist_entry *next; - char *text; -} Hist; - -Hist *history_get (char *input_name); -void history_put (char *input_name, Hist *h); -char *show_hist (Hist *history, int widget_y, int widget_x); +GList *history_get (char *input_name); +void history_put (char *input_name, GList *h); +char *show_hist (GList *history, int widget_y, int widget_x); typedef struct { Widget widget; @@ -72,7 +66,7 @@ typedef struct { int disable_update; /* Do we want to skip updates? */ int is_password; /* Is this a password input line? */ unsigned char *buffer; /* pointer to editing buffer */ - Hist *history; /* The history */ + GList *history; /* The history */ int need_push; /* need to push the current Input on hist? */ char **completions; /* Possible completions array */ int completion_flags; /* INPUT_COMPLETE* bitwise flags(complete.h) */