[Xfce4-commits] <mousepad:master> * mousepad/mousepad-{preferences, search-bar, document}.c, mousepad/mousepad-types.h: Add match whole word option.

Nick Schermer noreply at xfce.org
Sat May 5 21:30:39 CEST 2012


Updating branch refs/heads/master
         to c0bf115da5b4dd9ca9be101014e41565ffdeae1c (commit)
       from 0f9ccf029632a9bb61f393243c1153fb58dd5793 (commit)

commit c0bf115da5b4dd9ca9be101014e41565ffdeae1c
Author: Nick Schermer <nick at xfce.org>
Date:   Sun May 20 18:44:10 2007 +0000

    	* mousepad/mousepad-{preferences,search-bar,document}.c,
    	  mousepad/mousepad-types.h: Add match whole word option.
    
    (Old svn revision: 25736)

 ChangeLog                       |    5 ++
 mousepad/mousepad-document.c    |   24 ++++++++-
 mousepad/mousepad-preferences.c |   19 +++++++-
 mousepad/mousepad-search-bar.c  |  102 ++++++++++++++++++++++++++++++++-------
 mousepad/mousepad-types.h       |    1 +
 5 files changed, 130 insertions(+), 21 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 66929ca..d1d2fb6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,9 @@
 2007-05-20	Nick Schermer <nick at xfce.org>
+	* mousepad/mousepad-{preferences,search-bar,document}.c,
+	  mousepad/mousepad-types.h: Add match whole word option.
+
+
+2007-05-20	Nick Schermer <nick at xfce.org>
 	* mousepad/Makefile.am: Use the new exo-csource
 	  --strip-comments and --strip-content arguments.
 
diff --git a/mousepad/mousepad-document.c b/mousepad/mousepad-document.c
index 6c92296..b446d29 100644
--- a/mousepad/mousepad-document.c
+++ b/mousepad/mousepad-document.c
@@ -750,11 +750,28 @@ mousepad_document_iter_search (const GtkTextIter   *start,
           /* we've hit the end of the search string, so we had a full match */
           if (G_UNLIKELY (*str == '\0'))
             {
-              /* forward one character */
               if (G_LIKELY (search_forward))
-                gtk_text_iter_forward_char (&iter);
+                {
+                  /* forward one character */
+                  gtk_text_iter_forward_char (&iter);
+
+                  /* check if we match a whole word */
+                  if (flags & MOUSEPAD_SEARCH_WHOLE_WORD
+                      && !(gtk_text_iter_starts_word (&begin)
+                           && gtk_text_iter_ends_word (&iter)))
+                    goto reset_match;
+                }
               else
-                gtk_text_iter_forward_char (&begin);
+                {
+                  /* 'backward' one character */
+                  gtk_text_iter_forward_char (&begin);
+
+                  /* check if we match a whole word */
+                  if (flags & MOUSEPAD_SEARCH_WHOLE_WORD
+                      && !(gtk_text_iter_starts_word (&iter)
+                           && gtk_text_iter_ends_word (&begin)))
+                    goto reset_match;
+                }
 
               /* set the start and end iters */
               *match_start = begin;
@@ -767,6 +784,7 @@ mousepad_document_iter_search (const GtkTextIter   *start,
         }
       else if (G_UNLIKELY (str_offset > 0))
         {
+          reset_match:
           /* go back to the first character in the string */
           for (;str_offset > 0; str_offset--)
             str = g_utf8_prev_char (str);
diff --git a/mousepad/mousepad-preferences.c b/mousepad/mousepad-preferences.c
index a602057..e7070ca 100644
--- a/mousepad/mousepad-preferences.c
+++ b/mousepad/mousepad-preferences.c
@@ -52,6 +52,7 @@ enum
   PROP_LAST_AUTO_INDENT,
   PROP_LAST_LINE_NUMBERS,
   PROP_LAST_MATCH_CASE,
+  PROP_LAST_MATCH_WHOLE_WORD,
   PROP_LAST_STATUSBAR_VISIBLE,
   PROP_LAST_WINDOW_HEIGHT,
   PROP_LAST_WINDOW_WIDTH,
@@ -232,6 +233,19 @@ mousepad_preferences_class_init (MousepadPreferencesClass *klass)
                                                          FALSE,
                                                          MOUSEPAD_PARAM_READWRITE));
 
+    /**
+   * MousepadPreferences:last-match-whole-word
+   *
+   * Whether to enable match case in the search bar.
+   **/
+  g_object_class_install_property (gobject_class,
+                                   PROP_LAST_MATCH_WHOLE_WORD,
+                                   g_param_spec_boolean ("last-match-whole-word",
+                                                         "last-match-whole-word",
+                                                         "last-match-whole-word",
+                                                         FALSE,
+                                                         MOUSEPAD_PARAM_READWRITE));
+
   /**
    * MousepadPreferences:last-statusbar-visible
    *
@@ -432,7 +446,10 @@ mousepad_preferences_set_property (GObject      *object,
 
   dst = preferences->values + prop_id;
   if (G_UNLIKELY (!G_IS_VALUE (dst)))
-    g_value_init (dst, pspec->value_type);
+    {
+      g_value_init (dst, pspec->value_type);
+      g_param_value_set_default (pspec, dst);
+    }
 
   if (g_param_values_cmp (pspec, value, dst) != 0)
     {
diff --git a/mousepad/mousepad-search-bar.c b/mousepad/mousepad-search-bar.c
index 2804684..65948eb 100644
--- a/mousepad/mousepad-search-bar.c
+++ b/mousepad/mousepad-search-bar.c
@@ -53,8 +53,12 @@ static void      mousepad_search_bar_match_case_toggled         (GtkWidget
                                                                  MousepadSearchBar       *search_bar);
 static void      mousepad_search_bar_wrap_around_toggled        (GtkWidget               *button,
                                                                  MousepadSearchBar       *search_bar);
+static void      mousepad_search_bar_match_whole_word_toggled   (GtkWidget               *button,
+                                                                 MousepadSearchBar       *search_bar);
 static void      mousepad_search_bar_menuitem_toggled           (GtkCheckMenuItem        *item,
                                                                  GtkToggleButton         *button);
+static void      mousepad_search_bar_highlight_idle             (MousepadSearchBar       *search_bar,
+                                                                 gboolean                 forced);
 static gboolean  mousepad_search_bar_highlight_timeout          (gpointer                 user_data);
 static void      mousepad_search_bar_highlight_timeout_destroy  (gpointer                 user_data);
 static void      mousepad_search_bar_nothing_found              (MousepadSearchBar       *search_bar,
@@ -87,6 +91,7 @@ struct _MousepadSearchBar
   /* menu entries */
   GtkWidget           *match_case_entry;
   GtkWidget           *wrap_around_entry;
+  GtkWidget           *match_whole_word_entry;
 
   /* if something was found */
   guint                nothing_found : 1;
@@ -95,6 +100,7 @@ struct _MousepadSearchBar
   guint                highlight_all : 1;
   guint                match_case : 1;
   guint                wrap_around : 1;
+  guint                match_whole_word : 1;
 
   /* timeout for highlighting while typing */
   guint                highlight_id;
@@ -203,7 +209,7 @@ mousepad_search_bar_init (MousepadSearchBar *search_bar)
 {
   GtkWidget   *label, *image, *check, *menuitem;
   GtkToolItem *item;
-  gboolean     match_case, wrap_around;
+  gboolean     match_case, wrap_around, match_whole_word;
 
   /* preferences */
   search_bar->preferences = mousepad_preferences_get ();
@@ -212,6 +218,7 @@ mousepad_search_bar_init (MousepadSearchBar *search_bar)
   g_object_get (G_OBJECT (search_bar->preferences),
                 "last-match-case", &match_case,
                 "last-wrap-around", &wrap_around,
+                "last-match-whole-word", &match_whole_word,
                 NULL);
 
   /* init variables */
@@ -219,6 +226,7 @@ mousepad_search_bar_init (MousepadSearchBar *search_bar)
   search_bar->highlight_id = 0;
   search_bar->match_case = match_case;
   search_bar->wrap_around = wrap_around;
+  search_bar->match_whole_word = match_whole_word;
 
   /* the close button */
   item = gtk_tool_button_new_from_stock (GTK_STOCK_CLOSE);
@@ -328,6 +336,28 @@ mousepad_search_bar_init (MousepadSearchBar *search_bar)
   gtk_widget_show (menuitem);
   g_signal_connect (G_OBJECT (menuitem), "toggled",
                     G_CALLBACK (mousepad_search_bar_menuitem_toggled), check);
+
+  /* check button for match whole word, including the proxy menu item */
+  item = gtk_tool_item_new ();
+  g_signal_connect_object (G_OBJECT (search_bar), "destroy", G_CALLBACK (gtk_widget_destroy), item, G_CONNECT_SWAPPED);
+  gtk_toolbar_insert (GTK_TOOLBAR (search_bar), item, -1);
+  gtk_widget_show (GTK_WIDGET (item));
+
+  check = gtk_check_button_new_with_mnemonic (_("Match _Whole Word"));
+  g_signal_connect_object (G_OBJECT (search_bar), "destroy", G_CALLBACK (gtk_widget_destroy), item, G_CONNECT_SWAPPED);
+  gtk_container_add (GTK_CONTAINER (item), check);
+  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), match_whole_word);
+  gtk_widget_show (check);
+  g_signal_connect (G_OBJECT (check), "toggled",
+                    G_CALLBACK (mousepad_search_bar_match_whole_word_toggled), search_bar);
+
+  search_bar->match_whole_word_entry = menuitem = gtk_check_menu_item_new_with_mnemonic (_("Match _Whole Word"));
+  g_signal_connect_object (G_OBJECT (search_bar), "destroy", G_CALLBACK (gtk_widget_destroy), item, G_CONNECT_SWAPPED);
+  gtk_tool_item_set_proxy_menu_item (item, "case-sensitive", menuitem);
+  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem), match_whole_word);
+  gtk_widget_show (menuitem);
+  g_signal_connect (G_OBJECT (menuitem), "toggled",
+                    G_CALLBACK (mousepad_search_bar_menuitem_toggled), check);
 }
 
 
@@ -364,6 +394,10 @@ mousepad_search_bar_find_string (MousepadSearchBar *search_bar,
   if (search_bar->wrap_around)
     flags |= MOUSEPAD_SEARCH_WRAP_AROUND;
 
+  /* wrap around flag */
+  if (search_bar->match_whole_word)
+    flags |= MOUSEPAD_SEARCH_WHOLE_WORD;
+
   /* get the entry string */
   string = gtk_entry_get_text (GTK_ENTRY (search_bar->entry));
 
@@ -400,13 +434,8 @@ mousepad_search_bar_entry_changed (GtkWidget         *entry,
   if (search_bar->highlight_id != 0)
     g_source_remove (search_bar->highlight_id);
 
-  if (search_bar->highlight_all)
-    {
-      /* start a new highlight timeout */
-      search_bar->highlight_id = g_timeout_add_full (G_PRIORITY_LOW, HIGHTLIGHT_TIMEOUT,
-                                                           mousepad_search_bar_highlight_timeout, search_bar,
-                                                           mousepad_search_bar_highlight_timeout_destroy);
-    }
+  /* re-run the highlight */
+  mousepad_search_bar_highlight_idle (search_bar, FALSE);
 
   /* find */
   mousepad_search_bar_find_string (search_bar, 0);
@@ -432,9 +461,8 @@ mousepad_search_bar_highlight_toggled (GtkWidget         *button,
   /* save the state */
   search_bar->highlight_all = active;
 
-  /* invoke the highlight function to update the buffer */
-  search_bar->highlight_id = g_idle_add_full (G_PRIORITY_LOW, mousepad_search_bar_highlight_timeout,
-                                              search_bar, mousepad_search_bar_highlight_timeout_destroy);
+  /* re-run the highlight */
+  mousepad_search_bar_highlight_idle (search_bar, TRUE);
 }
 
 
@@ -459,12 +487,8 @@ mousepad_search_bar_match_case_toggled (GtkWidget         *button,
   /* save the setting */
   g_object_set (G_OBJECT (search_bar->preferences), "last-match-case", active, NULL);
 
-  if (search_bar->highlight_all)
-    {
-      /* invoke the highlight function to update the buffer */
-      search_bar->highlight_id = g_idle_add_full (G_PRIORITY_LOW, mousepad_search_bar_highlight_timeout,
-                                                  search_bar, mousepad_search_bar_highlight_timeout_destroy);
-    }
+  /* re-run the highlight */
+  mousepad_search_bar_highlight_idle (search_bar, FALSE);
 }
 
 
@@ -493,6 +517,32 @@ mousepad_search_bar_wrap_around_toggled (GtkWidget         *button,
 
 
 static void
+mousepad_search_bar_match_whole_word_toggled (GtkWidget         *button,
+                                              MousepadSearchBar *search_bar)
+{
+  gboolean active;
+
+  _mousepad_return_if_fail (MOUSEPAD_IS_SEARCH_BAR (search_bar));
+
+  /* get the state of the toggle button */
+  active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
+
+  /* set the state of the menu item */
+  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (search_bar->match_whole_word_entry), active);
+
+  /* save the state */
+  search_bar->match_whole_word = active;
+
+  /* save the setting */
+  g_object_set (G_OBJECT (search_bar->preferences), "last-match-whole-word", active, NULL);
+
+  /* re-run the highlight */
+  mousepad_search_bar_highlight_idle (search_bar, FALSE);
+}
+
+
+
+static void
 mousepad_search_bar_menuitem_toggled (GtkCheckMenuItem *item,
                                       GtkToggleButton  *button)
 {
@@ -504,6 +554,20 @@ mousepad_search_bar_menuitem_toggled (GtkCheckMenuItem *item,
 
 
 
+static void
+mousepad_search_bar_highlight_idle (MousepadSearchBar *search_bar,
+                                    gboolean           forced)
+{
+  if ((forced || search_bar->highlight_all) && search_bar->highlight_id == 0)
+    {
+      /* invoke the highlight function to update the buffer */
+      search_bar->highlight_id = g_idle_add_full (G_PRIORITY_LOW, mousepad_search_bar_highlight_timeout,
+                                                  search_bar, mousepad_search_bar_highlight_timeout_destroy);
+    }
+}
+
+
+
 static gboolean
 mousepad_search_bar_highlight_timeout (gpointer user_data)
 {
@@ -518,6 +582,10 @@ mousepad_search_bar_highlight_timeout (gpointer user_data)
   if (!search_bar->match_case)
     flags |= MOUSEPAD_SEARCH_CASE_INSENSITIVE;
 
+  /* wrap around flag */
+  if (search_bar->match_whole_word)
+    flags |= MOUSEPAD_SEARCH_WHOLE_WORD;
+
   /* set the entry string or a 0 string to remove the highlight */
   if (search_bar->highlight_all)
     string = gtk_entry_get_text (GTK_ENTRY (search_bar->entry));
diff --git a/mousepad/mousepad-types.h b/mousepad/mousepad-types.h
index f819392..4056afd 100644
--- a/mousepad/mousepad-types.h
+++ b/mousepad/mousepad-types.h
@@ -28,6 +28,7 @@ typedef enum
   MOUSEPAD_SEARCH_WRAP_AROUND      = 1 << 2,
   MOUSEPAD_SEARCH_FORWARDS         = 1 << 3,
   MOUSEPAD_SEARCH_BACKWARDS        = 1 << 4,
+  MOUSEPAD_SEARCH_WHOLE_WORD       = 1 << 5,
 } MousepadSearchFlags;
 
 G_END_DECLS


More information about the Xfce4-commits mailing list