So, after this mailing list made me aware of the super great snipped
feature, I decided to get a bit into Geany hacking to expand the snipped
featureset.
What my patch does is basically allow more than 1 cursor position and
move to the next one using a hotkey. Little example:
for(i = 0; i < [cursor1]; i++)
{
[cursor2]
}
[cursor3]
After doing for<tab>, the cursor ends up at cursor1, now you insert your
text (e.g. MAX_COUNT), and hit ctrl+tab and move to cursor2, doing your
stuff.
That saves a lot of movement with the arrow keys!
The snippet.conf syntax doesn't change, you're just going to add more
%cursor%.
However, the current patch is far from intelligent and nice. It's a bit
hackish (limited to 10 cursors, global struct in editor.c ...), and the
next cursor movement is apparently broken once you hit tab normally
between "jumps". To be investigated! Also, I changed the hotkey of "go
to last used tab" to nothing, so that ctrl+tab is free for this (this is
the best hotkey I can find for), feel free to discuss!
This being my first code contribution to Geany, I'd really like to get
some feedback! Don't hit me if it's bad though ;)
PS: Geany is great :D
Index: src/utils.c
===================================================================
--- src/utils.c (Revision 3435)
+++ src/utils.c (Arbeitskopie)
@@ -1355,20 +1355,25 @@
/**
- * Replaces all occurrences of @c needle in @c haystack with @c replace.
+ * Replaces occurrences of @c needle in @c haystack with @c replace.
* As of Geany 0.16, @a replace can match @a needle, so the following will work:
* @code utils_string_replace_all(text, "\n", "\r\n"); @endcode
*
* @param haystack The input string to operate on. This string is modified in place.
* @param needle The string which should be replaced.
* @param replace The replacement for @c needle.
+ * @param max_replaces Limits the amount of replaces to be done, 0 is unlimited.
*
* @return @a TRUE if @c needle was found, else @a FALSE.
**/
-gboolean utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
+static gboolean utils_string_replace__helper(GString *haystack,
+ const gchar *needle,
+ const gchar *replace,
+ const guint max_replaces)
{
const gchar *stack, *match;
gssize pos = -1;
+ guint replace_count = 0;
if (haystack->len == 0)
return FALSE;
@@ -1378,7 +1383,7 @@
while (1)
{
match = strstr(stack, needle);
- if (match == NULL)
+ if (match == NULL || (max_replaces && replace_count++ >= max_replaces))
break;
else
{
@@ -1399,7 +1404,16 @@
return (pos != -1);
}
+gboolean utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace)
+{
+ utils_string_replace__helper(haystack, needle, replace, 0);
+}
+gboolean utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace)
+{
+ utils_string_replace__helper(haystack, needle, replace, 1);
+}
+
/* Get project or default startup directory (if set), or NULL. */
const gchar *utils_get_default_dir_utf8(void)
{
Index: src/utils.h
===================================================================
--- src/utils.h (Revision 3435)
+++ src/utils.h (Arbeitskopie)
@@ -82,8 +82,10 @@
gchar *utils_get_hostname(void);
-gboolean utils_string_replace_all(GString *str, const gchar *needle, const gchar *replace);
+gboolean utils_string_replace_all(GString *haystack, const gchar *needle, const gchar *replace);
+gboolean utils_string_replace_first(GString *haystack, const gchar *needle, const gchar *replace);
+
gchar *utils_str_replace(gchar *haystack, const gchar *needle, const gchar *replacement);
gint utils_strpos(const gchar* haystack, const gchar *needle);
Index: src/keybindings.c
===================================================================
--- src/keybindings.c (Revision 3435)
+++ src/keybindings.c (Arbeitskopie)
@@ -233,6 +233,8 @@
GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
keybindings_set_item(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL, /* handled specially in check_snippet_completion() */
GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
+ keybindings_set_item(group, GEANY_KEYS_EDITOR_SNIPPET_NEXT_CURSOR, cb_func_editor_action,
+ GDK_Tab, GDK_CONTROL_MASK, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
keybindings_set_item(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, cb_func_editor_action,
0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
keybindings_set_item(group, GEANY_KEYS_EDITOR_CONTEXTACTION, cb_func_editor_action,
@@ -412,7 +414,7 @@
keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
- GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
+ 0, 0, "switch_tablastused", _("Switch to last used document"), NULL);
keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
GDK_Page_Up, GDK_MOD1_MASK, "move_tableft", _("Move document left"), NULL);
keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
@@ -852,7 +854,6 @@
return FALSE;
}
-
#ifdef HAVE_VTE
static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
gpointer user_data)
@@ -1547,6 +1548,9 @@
case GEANY_KEYS_EDITOR_DUPLICATELINE:
duplicate_lines(doc->editor);
break;
+ case GEANY_KEYS_EDITOR_SNIPPET_NEXT_CURSOR:
+ editor_snippet_next_cursor(doc->editor->sci, sci_get_current_position(doc->editor->sci));
+ break;
case GEANY_KEYS_EDITOR_DELETELINE:
delete_lines(doc->editor);
break;
Index: src/keybindings.h
===================================================================
--- src/keybindings.h (Revision 3435)
+++ src/keybindings.h (Arbeitskopie)
@@ -134,6 +134,7 @@
GEANY_KEYS_EDITOR_SCROLLLINEUP,
GEANY_KEYS_EDITOR_SCROLLLINEDOWN,
GEANY_KEYS_EDITOR_COMPLETESNIPPET,
+ GEANY_KEYS_EDITOR_SNIPPET_NEXT_CURSOR,
GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION,
GEANY_KEYS_EDITOR_CONTEXTACTION,
GEANY_KEYS_EDITOR_AUTOCOMPLETE,
Index: src/editor.c
===================================================================
--- src/editor.c (Revision 3435)
+++ src/editor.c (Arbeitskopie)
@@ -61,6 +61,15 @@
#include "projectprivate.h"
+#define MAX_CURSOR_IN_SNIPPET 10
+struct snippet_cursor_info {
+ gint cur_idx;
+ gint current;
+ gint positions[MAX_CURSOR_IN_SNIPPET];
+ gboolean enabled;
+};
+
+static struct snippet_cursor_info snippet_cursor;
/* Note: Avoid using SSM in files not related to scintilla, use sciwrappers.h instead. */
#define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
@@ -1835,6 +1844,8 @@
g_free(whitespace);
}
+ /* the next 2 replacements have no effect to snippets, since they replace
+ * seperately (because those must be done before %cursor% replacement */
/* transform line endings */
utils_string_replace_all(buf, "\n", editor_get_eol_char(editor));
@@ -1862,19 +1873,33 @@
g_string_free(buf, TRUE);
}
+/* Move the cursor to the next specified cursor position in a inserted snipped */
+void editor_snippet_next_cursor(ScintillaObject *sci, gint current_pos)
+{
+ snippet_cursor.cur_idx++;
+ if (!snippet_cursor.enabled)
+ return;
+ if (snippet_cursor.cur_idx > 0)
+ snippet_cursor.current = snippet_cursor.positions[snippet_cursor.cur_idx]
+ - snippet_cursor.positions[snippet_cursor.cur_idx-1];
+ if (snippet_cursor.current > 0)
+ sci_set_current_position(sci, snippet_cursor.current+current_pos, FALSE);
+ else
+ snippet_cursor.enabled = FALSE;
+}
static gboolean snippets_complete_constructs(GeanyEditor *editor, gint pos, const gchar *word)
{
gchar *str, *whitespace;
GString *pattern;
- gint step, str_len;
+ gint str_len, i;
gsize cur_index;
gint ft_id = FILETYPE_ID(editor->document->file_type);
GHashTable *specials;
ScintillaObject *sci = editor->sci;
-
str = g_strdup(word);
g_strstrip(str);
+ snippet_cursor.enabled = FALSE;
pattern = g_string_new(snippets_find_completion_by_name(filetypes[ft_id]->name, str));
if (pattern == NULL || pattern->len == 0)
@@ -1906,24 +1931,45 @@
snippets_replace_wildcards(editor, pattern);
/* transform other wildcards */
+ /* transform line endings */
utils_string_replace_all(pattern, "%newline%", editor_get_eol_char(editor));
+ utils_string_replace_all(pattern, "\n", editor_get_eol_char(editor));
- /* use spaces for indentation, will be fixed up */
+ /* transform tabs into indent widths (in spaces) */
whitespace = g_strnfill(editor_get_indent_prefs(editor)->width, ' ');
utils_string_replace_all(pattern, "%ws%", whitespace);
+ utils_string_replace_all(pattern, "\t", whitespace);
g_free(whitespace);
- /* find the %cursor% pos (has to be done after all other operations) */
- step = utils_strpos(pattern->str, "%cursor%");
- if (step != -1)
- utils_string_replace_all(pattern, "%cursor%", "");
-
- /* finally insert the text and set the cursor */
- if (step != -1)
- cur_index = step;
+ /* find the %cursor% positions
+ * (has to be done after all other operations) */
+ for (i = 0; i < MAX_CURSOR_IN_SNIPPET; i++)
+ {
+ gint temp;
+ temp = utils_strpos(pattern->str, "%cursor%");
+ if (temp != -1)
+ utils_string_replace_first(pattern, "%cursor%", "");
+ else /* no more found */
+ break;
+ snippet_cursor.positions[i] = temp;
+ }
+ snippet_cursor.current = snippet_cursor.positions[0];
+ snippet_cursor.cur_idx = 0;
+ if (snippet_cursor.current < 1)
+ {
+ cur_index = pattern->len;
+ snippet_cursor.enabled = FALSE;
+ }
else
- cur_index = pattern->len;
-
+ {
+ cur_index = snippet_cursor.current;
+ snippet_cursor.enabled = TRUE;
+ }
+ for (i = 0; i < MAX_CURSOR_IN_SNIPPET; i++)
+ {
+ printf("cursor: %d, val: %d\n", i, snippet_cursor.positions[i]);
+ }
+
editor_insert_text_block(editor, pattern->str, pos, cur_index, -1);
sci_scroll_caret(sci);
_______________________________________________
Geany mailing list
[email protected]
http://lists.uvena.de/cgi-bin/mailman/listinfo/geany