GNOME Bugzilla – Bug 390048
Entire word search flag
Last modified: 2013-07-08 21:40:32 UTC
Patch that adds a new flag, GTK_TEXT_SEARCH_TEXT_WHOLE_WORD, to GtkTextSearchFlags. If you add this to the flag argument to gtk_text_iter_forward_search or gtk_text_iter_backward_search then it will only match if the beginning and ending iterators begin and end a word, respectively.
Created attachment 78946 [details] [review] Patch for header file
Created attachment 78947 [details] [review] Patch for source file
Created attachment 246979 [details] [review] GtkTextIter: add GTK_TEXT_SEARCH_ENTIRE_WORD_ONLY flag Create the forward_search() and backward_search() functions, that doesn't take the new flag into account, so it is mainly a copy/paste from the previous code of gtk_text_iter_forward_search() and gtk_text_iter_backward_search(). The parameters are slightly different, to avoid recomputing the same thing at each call. So to check if the match is an entire word, we first do a normal search (with the other flags), and then we check if it's an entire word. There is a loop to search the next normal match if the match was not an entire word. This permits to not complicate further forward_search() and backward_search(). Another thing, part of the documentation has been moved to the enum, which is a better place IMHO. And unit tests have been written for the new flag.
To give a bit context, ENTIRE_WORD_ONLY comes from gedit. We are planning to add a higher-level API for the search and replace in GtkSourceView (bug #701466). By fixing this bug, we avoid the need for another enum in GtkSourceView.
Created attachment 247305 [details] [review] Improve doc of gtk_text_iter_forward_search() While I'm at it. It was not possible to know if the @limit was for @match_start or @match_end. It was documented for backward_search(), but not for forward_search().
Review of attachment 247305 [details] [review]: Looks good to me.
Move GtkTextView -> GtkSourceView. The plan is to have other search flags in gsv.
I agree that gsv should have its own enum as long as it has extra features, but still patch in comment 3 makes sense to me and looks good at a glance and the api addition is minimal (just one enum value), so unless gtk maintainers object I would still like see it go there
Summary of the discussion on IRC: With the regex search (bug #134674), it would be possible to match at word boundaries with a regex like "\bgtk_foo\b". It will match "gtk_foo", but not "gtk_foobar". So with the regex search, it is really easy to implement the "entire word" search flag. And maybe a better name is GTK_TEXT_SEARCH_AT_WORD_BOUNDARIES, because "entire word" can span multiple words, it's confusing. If GTK_TEXT_SEARCH_AT_WORD_BOUNDARIES exists, it is only for convenience. If the regex search is implemented in gtk+, we can just document the \b trick, and add the convenience flag in gsv.
And the link: http://www.regular-expressions.info/wordboundaries.html
Created attachment 247716 [details] [review] Document GtkTextSearchFlags Extract from the previous patch the move of the documentation. Move the doc about the flags from gtk_text_iter_forward_search() to the enum.
Review of attachment 247716 [details] [review]: Looks good.
Implemented in GtkSourceView, with the "search-at-word-boundaries" property in GtkSourceBuffer.