[gnome-builder/wip/vim] vim: add basic ctrl-a/ctrl-x support.



commit 37b8139eb9eeb162519c98fce77636bc47f4041a
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 2 03:40:58 2014 -0700

    vim: add basic ctrl-a/ctrl-x support.

 src/editor/gb-editor-vim.c |   69 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)
---
diff --git a/src/editor/gb-editor-vim.c b/src/editor/gb-editor-vim.c
index 9d2fa40..2fef944 100644
--- a/src/editor/gb-editor-vim.c
+++ b/src/editor/gb-editor-vim.c
@@ -18,6 +18,7 @@
 
 #define G_LOG_DOMAIN "vim"
 
+#include <errno.h>
 #include <glib/gi18n.h>
 #include <gtksourceview/gtksource.h>
 #include <stdio.h>
@@ -1830,6 +1831,50 @@ gb_editor_vim_unindent (GbEditorVim *vim)
     gb_source_view_unindent_selection (view);
 }
 
+static void
+gb_editor_vim_add (GbEditorVim *vim,
+                   gint         by_count)
+{
+  GtkTextBuffer *buffer;
+  GtkTextIter iter;
+  GtkTextIter selection;
+  gchar *endptr = NULL;
+  gchar *replace = NULL;
+  gchar *slice;
+  gint64 value = 0;
+
+  g_assert (vim);
+
+  /*
+   * TODO: There are a lot of smarts we can put in here. Guessing the base
+   *       comes to mind (hex, octal, etc).
+   */
+
+  buffer = gtk_text_view_get_buffer (vim->priv->text_view);
+  gtk_text_buffer_get_selection_bounds (buffer, &iter, &selection);
+
+  slice = gtk_text_iter_get_slice (&iter, &selection);
+  value = g_ascii_strtoll (slice, &endptr, 10);
+
+  if (((value == G_MAXINT64) || (value == G_MININT64)) && (errno == ERANGE))
+    goto cleanup;
+
+  if (!endptr || *endptr)
+    goto cleanup;
+
+  value += by_count;
+
+  replace = g_strdup_printf ("%"G_GINT64_FORMAT, value);
+
+  gtk_text_buffer_delete (buffer, &iter, &selection);
+  gtk_text_buffer_insert (buffer, &iter, replace, -1);
+  gtk_text_buffer_select_range (buffer, &iter, &iter);
+
+cleanup:
+  g_free (slice);
+  g_free (replace);
+}
+
 static GbEditorVimPhraseStatus
 gb_editor_vim_parse_phrase (GbEditorVim       *vim,
                             GbEditorVimPhrase *phrase)
@@ -1947,6 +1992,30 @@ gb_editor_vim_handle_normal (GbEditorVim *vim,
         }
       break;
 
+    case GDK_KEY_a:
+    case GDK_KEY_x:
+      if ((event->state & GDK_CONTROL_MASK) != 0)
+        {
+          GtkTextBuffer *buffer;
+          GtkTextIter begin;
+          GtkTextIter end;
+
+          buffer = gtk_text_view_get_buffer (vim->priv->text_view);
+          gb_editor_vim_clear_phrase (vim);
+          gb_editor_vim_clear_selection (vim);
+          if (gb_editor_vim_select_current_word (vim, &begin, &end))
+            {
+              if (gtk_text_iter_backward_char (&begin) &&
+                  ('-' != gtk_text_iter_get_char (&begin)))
+                gtk_text_iter_forward_char (&begin);
+              gtk_text_buffer_select_range (buffer, &begin, &end);
+              gb_editor_vim_add (vim, (event->keyval == GDK_KEY_a) ? 1 : -1);
+              gb_editor_vim_clear_selection (vim);
+            }
+          return TRUE;
+        }
+      break;
+
     case GDK_KEY_b:
       if ((event->state & GDK_CONTROL_MASK) != 0)
         {


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