[gnome-builder/wip/vim] vim: add forward search with *



commit 9c8c183d0cc83d13e18a4cf9bcb96eefa847c256
Author: Christian Hergert <christian hergert me>
Date:   Tue Sep 30 16:22:40 2014 -0700

    vim: add forward search with *
    
    Also refactor a bit so we can use clear_selection eventually.

 src/editor/gb-editor-vim.c |   95 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 77 insertions(+), 18 deletions(-)
---
diff --git a/src/editor/gb-editor-vim.c b/src/editor/gb-editor-vim.c
index 4d2d859..7b2a67f 100644
--- a/src/editor/gb-editor-vim.c
+++ b/src/editor/gb-editor-vim.c
@@ -1154,6 +1154,25 @@ gb_editor_vim_select_current_word (GbEditorVim *vim,
 }
 
 static void
+gb_editor_vim_clear_selection (GbEditorVim *vim)
+{
+  GtkTextBuffer *buffer;
+  GtkTextMark *insert;
+  GtkTextIter iter;
+
+  g_assert (GB_IS_EDITOR_VIM (vim));
+
+  buffer = gtk_text_view_get_buffer (vim->priv->text_view);
+  insert = gtk_text_buffer_get_insert (buffer);
+  gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
+  gtk_text_buffer_select_range (buffer, &iter, &iter);
+
+  vim->priv->target_line_offset = gb_editor_vim_get_line_offset (vim);
+
+  gtk_text_view_move_mark_onscreen (vim->priv->text_view, insert);
+}
+
+static void
 gb_editor_vim_reverse_search (GbEditorVim *vim)
 {
   GbSourceView *source_view;
@@ -1185,37 +1204,69 @@ gb_editor_vim_reverse_search (GbEditorVim *vim)
        * feature.
        */
       gtk_widget_grab_focus (GTK_WIDGET (vim->priv->text_view));
+
+      /*
+       * And it also selects the word, and VIM does not (it just leaves us
+       * on the word). So let's clear the selection too.
+       */
+#if 0
+      gb_editor_vim_clear_selection (vim);
+#endif
     }
 }
 
-static gboolean
-gb_editor_vim_get_has_selection (GbEditorVim *vim)
+static void
+gb_editor_vim_search (GbEditorVim *vim)
 {
-  GtkTextBuffer *buffer;
+  GbSourceView *source_view;
 
-  g_assert (GB_IS_EDITOR_VIM (vim));
+  GtkTextIter begin;
+  GtkTextIter end;
 
-  buffer = gtk_text_view_get_buffer (vim->priv->text_view);
-  return gtk_text_buffer_get_has_selection (buffer);
+  g_return_if_fail (GB_IS_EDITOR_VIM (vim));
+
+  if (!GB_IS_SOURCE_VIEW (vim->priv->text_view))
+    return;
+
+  source_view = GB_SOURCE_VIEW (vim->priv->text_view);
+
+  if (gb_editor_vim_select_current_word (vim, &begin, &end))
+    {
+      gchar *text;
+
+      /*
+       * Set the search text and begin jumping back to the previous match.
+       */
+      text = gtk_text_iter_get_slice (&begin, &end);
+      gb_source_view_begin_search (source_view, GTK_DIR_DOWN, text);
+      g_free (text);
+
+      /*
+       * But don't let the search entry focus. VIM let's us just keep hitting
+       * '#' over and over without any intervention, and that's a useful
+       * feature.
+       */
+      gtk_widget_grab_focus (GTK_WIDGET (vim->priv->text_view));
+
+      /*
+       * And it also selects the word, and VIM does not (it just leaves us
+       * on the word). So let's clear the selection too.
+       */
+#if 0
+      gb_editor_vim_clear_selection (vim);
+#endif
+    }
 }
 
-static void
-gb_editor_vim_clear_selection (GbEditorVim *vim)
+static gboolean
+gb_editor_vim_get_has_selection (GbEditorVim *vim)
 {
   GtkTextBuffer *buffer;
-  GtkTextMark *insert;
-  GtkTextIter iter;
 
   g_assert (GB_IS_EDITOR_VIM (vim));
 
   buffer = gtk_text_view_get_buffer (vim->priv->text_view);
-  insert = gtk_text_buffer_get_insert (buffer);
-  gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
-  gtk_text_buffer_select_range (buffer, &iter, &iter);
-
-  vim->priv->target_line_offset = gb_editor_vim_get_line_offset (vim);
-
-  gtk_text_view_move_mark_onscreen (vim->priv->text_view, insert);
+  return gtk_text_buffer_get_has_selection (buffer);
 }
 
 static gboolean
@@ -1493,9 +1544,17 @@ gb_editor_vim_handle_normal (GbEditorVim *vim,
       gb_editor_vim_move_line_start (vim);
       return TRUE;
 
+    case GDK_KEY_asterisk:
+      /*
+       * Start search in the forward direction for the word that is under
+       * the cursor. If we are over a space, we move ot the next word.
+       */
+      gb_editor_vim_search (vim);
+      return TRUE;
+
     case GDK_KEY_numbersign:
       /*
-       * Start searching in the reverse direction for the world that is
+       * Start searching in the reverse direction for the word that is
        * under the cursor. If we are over a space, we move to the next
        * word.
        */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]