[Xfce4-commits] <xfce-utils:completion> Restore the user command after a completion cycle.

Jérôme Guelfucci noreply at xfce.org
Wed Nov 3 23:22:02 CET 2010


Updating branch refs/heads/completion
         to 8448cf33026d38d7a7697fd6494ca22c6c2a5641 (commit)
       from 8433c2053b8bb2e2ac7406eb675462e6e9f136bb (commit)

commit 8448cf33026d38d7a7697fd6494ca22c6c2a5641
Author: Jérôme Guelfucci <jeromeg at xfce.org>
Date:   Wed Nov 3 23:16:55 2010 +0100

    Restore the user command after a completion cycle.
    
    When we are back to the beginning of the list, restore the command
    entered by the user before starting the completion.

 xfrun/xfrun-dialog.c  |   56 +++++++++++++++++++++++++++++++++++++++++++++++-
 xfrun/xfrun-history.c |   21 +++++++++++++++--
 xfrun/xfrun-history.h |    6 +++-
 3 files changed, 76 insertions(+), 7 deletions(-)

diff --git a/xfrun/xfrun-dialog.c b/xfrun/xfrun-dialog.c
index abe8a25..beac70a 100644
--- a/xfrun/xfrun-dialog.c
+++ b/xfrun/xfrun-dialog.c
@@ -55,6 +55,7 @@ struct _XfrunDialogPrivate
     XfrunHistory *history;
     gboolean      in_history_cycle;
     gboolean      in_history_completion;
+    gchar        *before_completion;
 
     gboolean      destroy_on_close;
     gchar        *working_directory;
@@ -143,6 +144,7 @@ xfrun_dialog_init(XfrunDialog *dialog)
     dialog->priv->history = xfrun_history_new();
     dialog->priv->in_history_cycle = FALSE;
     dialog->priv->in_history_completion = FALSE;
+    dialog->priv->before_completion = NULL;
 
     /* Create the UI */
     GTK_WINDOW(dialog)->type = GTK_WINDOW_TOPLEVEL;
@@ -235,6 +237,7 @@ xfrun_dialog_finalize(GObject *object)
     XfrunDialog *dialog = XFRUN_DIALOG(object);
 
     g_free(dialog->priv->working_directory);
+    g_free(dialog->priv->before_completion);
     g_object_unref(dialog->priv->history);
 
     G_OBJECT_CLASS(xfrun_dialog_parent_class)->finalize(object);
@@ -251,8 +254,26 @@ xfrun_dialog_key_press_event(GtkWidget *widget,
         return TRUE;
     } else if(evt->keyval == GDK_Down && dialog->priv->in_history_cycle) {
         XfrunHistoryItem *item;
+        gboolean          back_to_beginning;
 
-        item = xfrun_history_get_previous(dialog->priv->history);
+        item = xfrun_history_get_previous(dialog->priv->history,
+                                          &back_to_beginning);
+
+        if(back_to_beginning) {
+            gtk_entry_set_text(GTK_ENTRY(dialog->priv->entry),
+                               dialog->priv->before_completion);
+            gtk_editable_set_position (GTK_EDITABLE(dialog->priv->entry), -1);
+
+            /* Reset history cycle */
+            dialog->priv->in_history_cycle = FALSE;
+            xfrun_history_reset(dialog->priv->history);
+
+            /* Reset saved user command */
+            g_free(dialog->priv->before_completion);
+            dialog->priv->before_completion = NULL;
+
+            return TRUE;
+        }
 
         if(item) {
             gtk_entry_set_text(GTK_ENTRY(dialog->priv->entry), item->command);
@@ -268,6 +289,13 @@ xfrun_dialog_key_press_event(GtkWidget *widget,
         item = xfrun_history_get_next(dialog->priv->history);
 
         if(item) {
+            if(!dialog->priv->in_history_cycle) {
+                g_free(dialog->priv->before_completion);
+
+                dialog->priv->before_completion =
+                    g_strdup(gtk_entry_get_text(GTK_ENTRY(dialog->priv->entry)));
+            }
+
             gtk_entry_set_text(GTK_ENTRY(dialog->priv->entry), item->command);
             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON (dialog->priv->terminal_chk),
                                          item->in_terminal);
@@ -288,6 +316,12 @@ xfrun_dialog_key_press_event(GtkWidget *widget,
         }
 
         if(item) {
+            if(!dialog->priv->in_history_completion) {
+                g_free(dialog->priv->before_completion);
+                dialog->priv->before_completion =
+                    g_strdup(gtk_entry_get_text(GTK_ENTRY(dialog->priv->entry)));
+            }
+
             gtk_entry_set_text(GTK_ENTRY(dialog->priv->entry), item->command);
             gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->priv->terminal_chk),
                                          item->in_terminal);
@@ -298,8 +332,26 @@ xfrun_dialog_key_press_event(GtkWidget *widget,
         return TRUE;
     } else if(evt->keyval == GDK_Page_Down && dialog->priv->in_history_completion) {
         XfrunHistoryItem *item;
+        gboolean          back_to_beginning;
+
+        item = xfrun_history_completion_previous(dialog->priv->history,
+                                                 &back_to_beginning);
+
+        if(back_to_beginning) {
+            gtk_entry_set_text(GTK_ENTRY(dialog->priv->entry),
+                               dialog->priv->before_completion);
+            gtk_editable_set_position (GTK_EDITABLE(dialog->priv->entry), -1);
+
+            /* Reset history cycle */
+            dialog->priv->in_history_completion = FALSE;
+            xfrun_history_completion_reset(dialog->priv->history);
 
-        item = xfrun_history_completion_previous(dialog->priv->history);
+            /* Reset saved user command */
+            g_free(dialog->priv->before_completion);
+            dialog->priv->before_completion = NULL;
+
+            return TRUE;
+        }
 
         if(item) {
             gtk_entry_set_text(GTK_ENTRY(dialog->priv->entry), item->command);
diff --git a/xfrun/xfrun-history.c b/xfrun/xfrun-history.c
index c8f4a9c..1a572a6 100644
--- a/xfrun/xfrun-history.c
+++ b/xfrun/xfrun-history.c
@@ -281,14 +281,19 @@ XfrunHistoryItem
 /**
  * xfrun_history_get_previous:
  * @history: a #XfrunHistory
+ * @back_to_beginning: a #gboolean
  *
  * Returns the previous item in history, the same item if we are at the
  * beginning of the history list or %NULL if the history is empty.
  *
+ * @back_to_beginning is set to TRUE if @history current item was the first one,
+ * else it is set to FALSE.
+ *
  * Return value: a #XfrunHistoryItem that should not be modified/freed or %NULL.
  **/
 XfrunHistoryItem
-*xfrun_history_get_previous (XfrunHistory *history)
+*xfrun_history_get_previous (XfrunHistory *history,
+                             gboolean     *back_to_beginning)
 {
   g_return_val_if_fail (XFRUN_IS_HISTORY (history), NULL);
 
@@ -296,6 +301,8 @@ XfrunHistoryItem
   if (history->priv->history == NULL)
     return NULL;
 
+  *back_to_beginning = FALSE;
+
   if (history->priv->current_history)
     {
       /* We already started to cycle through history */
@@ -305,6 +312,8 @@ XfrunHistoryItem
       previous = g_list_previous (history->priv->current_history);
       if (previous)
         history->priv->current_history = previous;
+      else
+        *back_to_beginning = TRUE;
 
       return history->priv->current_history->data;
     }
@@ -406,12 +415,13 @@ XfrunHistoryItem
 /**
  * xfrun_history_completion_previous:
  * @history: a #XfrunHistory
+ * @back_to_beginning: a #gboolean
  *
  * Returns the previous completion item for current completion or %NULL
  * if there is no history based completion at the moment.
  *
  * If the beginning of the completion list was reached, the same item is
- * returned.
+ * returned and @back_to_beginning is set to TRUE. Else it is set to FALSE.
  *
  * xfrun_history_completion_start must be called once before using this
  * function.
@@ -419,7 +429,8 @@ XfrunHistoryItem
  * Return value: a #XfrunHistoryItem that should not be modified/freed or %NULL.
  **/
 XfrunHistoryItem
-*xfrun_history_completion_previous (XfrunHistory *history)
+*xfrun_history_completion_previous (XfrunHistory *history,
+                                    gboolean     *back_to_beginning)
 {
   GList *previous;
 
@@ -429,12 +440,16 @@ XfrunHistoryItem
   if (history->priv->current_completion == NULL)
     return NULL;
 
+  *back_to_beginning = FALSE;
+
   /* Get the previous item */
   previous = g_list_previous (history->priv->current_completion);
 
   /* If we are at the beginning of the list, we stay there */
   if (previous)
     history->priv->current_completion = previous;
+  else
+    *back_to_beginning = TRUE;
 
   return history->priv->current_completion->data;
 }
diff --git a/xfrun/xfrun-history.h b/xfrun/xfrun-history.h
index c4bbffb..9bdcf66 100644
--- a/xfrun/xfrun-history.h
+++ b/xfrun/xfrun-history.h
@@ -60,13 +60,15 @@ typedef struct
 GType xfrun_history_get_type (void);
 
 XfrunHistoryItem *xfrun_history_get_next            (XfrunHistory *history);
-XfrunHistoryItem *xfrun_history_get_previous        (XfrunHistory *history);
+XfrunHistoryItem *xfrun_history_get_previous        (XfrunHistory *history,
+                                                     gboolean     *back_to_beginning);
 void              xfrun_history_reset               (XfrunHistory *history);
 
 XfrunHistoryItem *xfrun_history_completion_start    (XfrunHistory *history,
                                                      const gchar  *prefix);
 XfrunHistoryItem *xfrun_history_completion_next     (XfrunHistory *history);
-XfrunHistoryItem *xfrun_history_completion_previous (XfrunHistory *history);
+XfrunHistoryItem *xfrun_history_completion_previous (XfrunHistory *history,
+                                                     gboolean     *back_to_beginning);
 void              xfrun_history_completion_reset    (XfrunHistory *history);
 
 void              xfrun_history_add                 (XfrunHistory *history,



More information about the Xfce4-commits mailing list