From 38286827058dd314c52839399ff33d27640012af Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Mon, 22 Sep 2008 11:57:14 +0000 Subject: [PATCH] Fix HTML table autocompletion when the indent type is 'Tabs & Spaces' (#2118289). Add some useful functions count_indent_size(), string_append_indent_width(). git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@2982 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 5 ++++ src/editor.c | 71 +++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index bc994ae0..2cf3eb20 100644 --- a/ChangeLog +++ b/ChangeLog @@ -5,6 +5,11 @@ auto-indentation for all editors. Don't disable the auto-indent document pref when switching back to a document with auto-indent turned off. + * src/editor.c: + Fix HTML table autocompletion when the indent type is 'Tabs & + Spaces' (#2118289). + Add some useful functions count_indent_size(), + string_append_indent_width(). 2008-09-21 Enrico Tröger diff --git a/src/editor.c b/src/editor.c index 2508c64a..00959b7a 100644 --- a/src/editor.c +++ b/src/editor.c @@ -1897,12 +1897,69 @@ static gboolean handle_xml(GeanyEditor *editor, gchar ch) } +static gsize count_indent_size(GeanyEditor *editor, const gchar *base_indent) +{ + const gchar *ptr; + gsize tab_size = sci_get_tab_width(editor->sci); + gsize count = 0; + + g_return_val_if_fail(base_indent, 0); + + for (ptr = base_indent; *ptr != 0; ptr++) + { + switch (*ptr) + { + case ' ': + count++; + break; + case '\t': + count += tab_size; + break; + } + } + return count; +} + + +static void string_append_indent_width(GString *str, const GeanyIndentPrefs *iprefs, + gsize width) +{ + gchar *ws = get_whitespace(iprefs, width); + + g_string_append(str, ws); + g_free(ws); +} + + +static gchar *get_table_body(GeanyEditor *editor, const gchar *base_indent) +{ + gsize base_size = count_indent_size(editor, base_indent); + const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor); + gsize indent_width = iprefs->width; + GString *str = g_string_new("\n"); + + if (! editor->auto_indent) + indent_width = 0; + + string_append_indent_width(str, iprefs, base_size + indent_width); + g_string_append(str, "\n"); + string_append_indent_width(str, iprefs, base_size + indent_width + indent_width); + g_string_append(str, "\n"); + string_append_indent_width(str, iprefs, base_size + indent_width + indent_width); + g_string_append(str, "\n"); + string_append_indent_width(str, iprefs, base_size + indent_width); + g_string_append(str, "\n"); + string_append_indent_width(str, iprefs, base_size); + + return g_string_free(str, FALSE); +} + + static void auto_table(GeanyEditor *editor, gint pos) { ScintillaObject *sci = editor->sci; gchar *table; gint indent_pos; - gchar *indent_str; if (SSM(sci, SCI_GETLEXER, 0, 0) != SCLEX_HTML) return; @@ -1925,18 +1982,8 @@ static void auto_table(GeanyEditor *editor, gint pos) } /* get indent string for generated code */ - if (! editor->auto_indent) - indent_str = g_strdup(""); - else - indent_str = get_single_indent(editor); - - table = g_strconcat("\n", indent, indent_str, "\n", - indent, indent_str, indent_str, "\n", - indent, indent_str, indent_str, "\n", - indent, indent_str, "\n", - indent, NULL); + table = get_table_body(editor, indent); sci_insert_text(sci, pos, table); - g_free(indent_str); g_free(table); }