[gtksourceview/wip/chergert/vim: 218/363] add rot13 command
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/chergert/vim: 218/363] add rot13 command
- Date: Mon, 8 Nov 2021 19:53:51 +0000 (UTC)
commit 5c22235a96cf4248da9468cfbec04eb9018adae5
Author: Christian Hergert <chergert redhat com>
Date: Tue Nov 2 14:58:33 2021 -0700
add rot13 command
gtksourceview/vim/gtk-source-vim-command.c | 50 ++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
---
diff --git a/gtksourceview/vim/gtk-source-vim-command.c b/gtksourceview/vim/gtk-source-vim-command.c
index 0e126881..3ede431c 100644
--- a/gtksourceview/vim/gtk-source-vim-command.c
+++ b/gtksourceview/vim/gtk-source-vim-command.c
@@ -315,6 +315,55 @@ gtk_source_vim_command_downcase (GtkSourceVimCommand *self)
return gtk_source_vim_command_change_case (self, GTK_SOURCE_CHANGE_CASE_LOWER);
}
+static char *
+rot13 (const char *str)
+{
+ GString *ret = g_string_new (NULL);
+
+ for (const char *c = str; *c; c = g_utf8_next_char (c))
+ {
+ gunichar ch = g_utf8_get_char (c);
+
+ if ((ch >= 65 && ch < 90) || (ch >= 97 && ch < 122))
+ {
+ if (g_ascii_tolower (ch) < 'n')
+ g_string_append_c (ret, ch + 13);
+ else
+ g_string_append_c (ret, ch - 13);
+ }
+ else
+ {
+ g_string_append_unichar (ret, ch);
+ }
+ }
+
+ return g_string_free (ret, FALSE);
+}
+
+static void
+gtk_source_vim_command_rot13 (GtkSourceVimCommand *self)
+{
+ GtkSourceBuffer *buffer;
+ GtkTextIter iter;
+ GtkTextIter selection;
+ char *text;
+ char *new_text;
+
+ buffer = gtk_source_vim_state_get_buffer (GTK_SOURCE_VIM_STATE (self), &iter, &selection);
+ text = gtk_text_iter_get_slice (&iter, &selection);
+ new_text = rot13 (text);
+
+ gtk_source_vim_state_begin_user_action (GTK_SOURCE_VIM_STATE (self));
+ gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &iter, &selection);
+ gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter, new_text, -1);
+ gtk_source_vim_state_end_user_action (GTK_SOURCE_VIM_STATE (self));
+
+ gtk_source_vim_state_set_can_repeat (GTK_SOURCE_VIM_STATE (self), TRUE);
+
+ g_free (text);
+ g_free (new_text);
+}
+
static void
gtk_source_vim_command_undo (GtkSourceVimCommand *self)
{
@@ -556,6 +605,7 @@ gtk_source_vim_command_class_init (GtkSourceVimCommandClass *klass)
ADD_COMMAND ("toggle-case", gtk_source_vim_command_toggle_case);
ADD_COMMAND ("upcase", gtk_source_vim_command_upcase);
ADD_COMMAND ("downcase", gtk_source_vim_command_downcase);
+ ADD_COMMAND ("rot13", gtk_source_vim_command_rot13);
#undef ADD_COMMAND
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]