Branch: refs/heads/master Author: Frank Lanitz frank@frank.uvena.de Committer: Frank Lanitz frank@frank.uvena.de Date: Mon, 08 Sep 2014 18:07:07 UTC Commit: 012769274c4914636f8b5d1cf245116961c6a315 https://github.com/geany/geany-plugins/commit/012769274c4914636f8b5d1cf24511...
Log Message: ----------- Merge pull request #141 from scriptum/autoclose-v0.2.1
Autoclose v0.2.1
Modified Paths: -------------- autoclose/src/autoclose.c
Modified: autoclose/src/autoclose.c 242 lines changed, 126 insertions(+), 116 deletions(-) =================================================================== @@ -43,19 +43,15 @@ GeanyPlugin *geany_plugin; GeanyData *geany_data; GeanyFunctions *geany_functions;
-PLUGIN_VERSION_CHECK(216) +PLUGIN_VERSION_CHECK(218) PLUGIN_SET_TRANSLATABLE_INFO( LOCALEDIR, GETTEXT_PACKAGE, _("Auto-close"), _("Auto-close braces and brackets with lot of features"), - "0.2", + "0.3", "Pavel Roschin <rpg89(at)post(dot)ru>")
-/* avoid aggresive warnings */ -#undef DOC_VALID -#define DOC_VALID(doc_ptr) (((doc_ptr) && (doc_ptr)->is_valid)) - typedef struct { /* close chars */ gboolean parenthesis; @@ -77,6 +73,7 @@ typedef struct { gboolean make_indent_for_cbracket; gboolean move_cursor_to_beginning; gboolean improved_cbracket_indent; + gboolean whitesmiths_style; gboolean close_functions; gboolean bcksp_remove_pair; gboolean jump_on_tab; @@ -87,7 +84,6 @@ typedef struct { static AutocloseInfo *ac_info = NULL;
typedef struct { - gulong notify_handler[2]; /* used to place the caret after autoclosed items on tab (similar to eclipse) */ gint jump_on_tab; /* used to reset jump_on_tab when needed */ @@ -256,7 +252,7 @@ get_end_pos(ScintillaObject *sci, gint line) end = sci_get_line_end_position(sci, line); ch = char_at(sci, end - 1); /* ignore spaces and "}" */ - while(isspace_no_newline(ch) || '}' == ch) + while (isspace_no_newline(ch) || '}' == ch) { end--; ch = char_at(sci, end - 1); @@ -361,8 +357,17 @@ improve_indent( SSM(sci, SCI_ADDTEXT, 2, (sptr_t)"\n\n"); else SSM(sci, SCI_ADDTEXT, 3, (sptr_t)"\n\n}"); - sci_set_line_indentation(sci, line + 1, indent + indent_width); - sci_set_line_indentation(sci, line + 2, indent); + if (ac_info->whitesmiths_style) + { + sci_set_line_indentation(sci, line, indent); + sci_set_line_indentation(sci, line + 1, indent); + sci_set_line_indentation(sci, line + 2, indent); + } + else + { + sci_set_line_indentation(sci, line + 1, indent + indent_width); + sci_set_line_indentation(sci, line + 2, indent); + } /* move to the end of added line */ end_pos = sci_get_line_end_position(sci, line + 1); sci_set_current_position(sci, end_pos, TRUE); @@ -498,11 +503,17 @@ enclose_selection( start_indent = sci_get_line_indentation(sci, start_line); indent_width = editor_get_indent_prefs(editor)->width; sci_set_line_indentation(sci, start_line, start_indent); - sci_set_line_indentation(sci, start_line + 1, start_indent + indent_width); - for(i = start_line + 2; i <= end_line; i++) + if (!ac_info->whitesmiths_style) + sci_set_line_indentation(sci, start_line + 1, start_indent + indent_width); + else + sci_set_line_indentation(sci, start_line + 1, start_indent); + for (i = start_line + 2; i <= end_line; i++) { current_indent = sci_get_line_indentation(sci, i); - sci_set_line_indentation(sci, i, current_indent + indent_width); + if (!ac_info->whitesmiths_style) + sci_set_line_indentation(sci, i, current_indent + indent_width); + else + sci_set_line_indentation(sci, i, current_indent); } text_end_pos = sci_get_line_end_position(sci, i - 1); sci_set_current_position(sci, text_end_pos, FALSE); @@ -529,7 +540,7 @@ enclose_selection( /* looks like a forward loop but actually lines processed in reverse order */ for (i = 0; i < selections; i++) { - if(selection_is_up_down) + if (selection_is_up_down) line = selections - i - 1; else line = i; @@ -589,7 +600,7 @@ check_struct( gchar ch; gint line, len; ch = char_at(sci, pos - 1); - while(g_ascii_isspace(ch)) + while (g_ascii_isspace(ch)) { pos--; ch = char_at(sci, pos - 1); @@ -709,13 +720,15 @@ auto_close_chars( style = sci_get_style_at(sci, pos + lex_offset);
/* add ; after functions */ - if (lexer_cpp_like(lexer, style) && - chars_left[0] == '(' && - !has_sel && - ac_info->close_functions && - pos == get_end_pos(sci, line) && - sci_get_line_indentation(sci, line) != 0 && - !check_define(sci, line)) + if ( + !has_sel && + ac_info->close_functions && + chars_left[0] == '(' && + lexer_cpp_like(lexer, style) && + pos == get_end_pos(sci, line) && + sci_get_line_indentation(sci, line) != 0 && + !check_define(sci, line) + ) chars_right[1] = ';';
style = sci_get_style_at(sci, pos); @@ -800,7 +813,6 @@ on_sci_notify(ScintillaObject *sci, gint scn, SCNotification *nt, gpointer user_ data->last_line = new_line; }
- static void on_document_open(GObject *obj, GeanyDocument *doc, gpointer user_data) { @@ -810,41 +822,15 @@ on_document_open(GObject *obj, GeanyDocument *doc, gpointer user_data)
sci = doc->editor->sci; data = g_new0(AutocloseUserData, 1); - g_return_if_fail(data); data->doc = doc; - data->notify_handler[0] = g_signal_connect(G_OBJECT(sci), "sci-notify", - G_CALLBACK(on_sci_notify), data); - data->notify_handler[1] = g_signal_connect(G_OBJECT(sci), "key-press-event", - G_CALLBACK(on_key_press), data); + plugin_signal_connect(geany_plugin, G_OBJECT(sci), "sci-notify", + FALSE, G_CALLBACK(on_sci_notify), data); + plugin_signal_connect(geany_plugin, G_OBJECT(sci), "key-press-event", + FALSE, G_CALLBACK(on_key_press), data); /* This will free the data when the sci is destroyed */ g_object_set_data_full(G_OBJECT(sci), "autoclose-userdata", data, g_free); }
-static void -autoclose_handlers_cleanup(void) -{ - gint i; - - foreach_document(i) - { - gint j; - gpointer data; - ScintillaObject *sci; - AutocloseUserData *autoclose_data; - - sci = documents[i]->editor->sci; - data = g_object_get_data(G_OBJECT(sci), "autoclose-userdata"); - if(!data) - continue; - autoclose_data = (AutocloseUserData*)data; - for(j = 0; j < 2; j++) - { - gulong handler = autoclose_data->notify_handler[j]; - g_signal_handler_disconnect(sci, handler); - } - } -} - PluginCallback plugin_callbacks[] = { { "document-open", (GCallback) &on_document_open, FALSE, NULL }, @@ -858,16 +844,15 @@ configure_response_cb(GtkDialog *dialog, gint response, gpointer user_data) if (response != GTK_RESPONSE_OK && response != GTK_RESPONSE_APPLY) return; GKeyFile *config = g_key_file_new(); - gchar *data; gchar *config_dir = g_path_get_dirname(ac_info->config_file);
g_key_file_load_from_file(config, ac_info->config_file, G_KEY_FILE_NONE, NULL);
-#define SAVE_CONF_BOOL(name) do { \ +#define SAVE_CONF_BOOL(name) G_STMT_START { \ ac_info->name = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( \ - g_object_get_data(G_OBJECT(dialog), "check_" #name))); \ + g_object_get_data(G_OBJECT(dialog), "check_" #name))); \ g_key_file_set_boolean(config, "autoclose", #name, ac_info->name); \ -} while (0) +} G_STMT_END
SAVE_CONF_BOOL(parenthesis); SAVE_CONF_BOOL(abracket); @@ -887,6 +872,7 @@ configure_response_cb(GtkDialog *dialog, gint response, gpointer user_data) SAVE_CONF_BOOL(make_indent_for_cbracket); SAVE_CONF_BOOL(move_cursor_to_beginning); SAVE_CONF_BOOL(improved_cbracket_indent); + SAVE_CONF_BOOL(whitesmiths_style); SAVE_CONF_BOOL(close_functions); SAVE_CONF_BOOL(bcksp_remove_pair); SAVE_CONF_BOOL(jump_on_tab); @@ -901,6 +887,7 @@ configure_response_cb(GtkDialog *dialog, gint response, gpointer user_data) else { /* write config to file */ + gchar *data; data = g_key_file_to_data(config, NULL, NULL); utils_write_file(ac_info->config_file, data); g_free(data); @@ -914,6 +901,7 @@ void plugin_init(G_GNUC_UNUSED GeanyData *data) { guint i; + foreach_document(i) { on_document_open(NULL, documents[i], NULL); @@ -948,6 +936,7 @@ plugin_init(G_GNUC_UNUSED GeanyData *data) GET_CONF_BOOL(make_indent_for_cbracket, TRUE); GET_CONF_BOOL(move_cursor_to_beginning, TRUE); GET_CONF_BOOL(improved_cbracket_indent, TRUE); + GET_CONF_BOOL(whitesmiths_style, FALSE); GET_CONF_BOOL(close_functions, TRUE); GET_CONF_BOOL(bcksp_remove_pair, FALSE); GET_CONF_BOOL(jump_on_tab, TRUE); @@ -1020,95 +1009,100 @@ plugin_configure(GtkDialog *dialog) GtkWidget *widget, *vbox, *frame, *container, *scrollbox; vbox = gtk_vbox_new(FALSE, 0); scrollbox = gtk_scrolled_window_new(NULL, NULL); + gtk_widget_set_size_request(GTK_WIDGET(scrollbox), 350, 400); gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrollbox), vbox); gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollbox), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
-#define WIDGET_FRAME(description) do { \ +#define WIDGET_FRAME(description) G_STMT_START { \ container = gtk_vbox_new(FALSE, 0); \ frame = gtk_frame_new(NULL); \ - gtk_frame_set_label(GTK_FRAME(frame), _(description)); \ + gtk_frame_set_label(GTK_FRAME(frame), description); \ gtk_container_add(GTK_CONTAINER(frame), container); \ gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 3); \ -} while (0) +} G_STMT_END
-#define WIDGET_CONF_BOOL(name, description, tooltip) do { \ - widget = gtk_check_button_new_with_label(_(description)); \ - if (tooltip) gtk_widget_set_tooltip_text(widget, _(tooltip)); \ +#define WIDGET_CONF_BOOL(name, description, tooltip) G_STMT_START { \ + widget = gtk_check_button_new_with_label(description); \ + if (tooltip) gtk_widget_set_tooltip_text(widget, tooltip); \ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), ac_info->name); \ gtk_box_pack_start(GTK_BOX(container), widget, FALSE, FALSE, 3); \ g_object_set_data(G_OBJECT(dialog), "check_" #name, widget); \ -} while (0) +} G_STMT_END
- WIDGET_FRAME("Auto-close quotes and brackets"); - WIDGET_CONF_BOOL(parenthesis, "Parenthesis ( )", - "Auto-close parenthesis "(" -> "(|)""); + WIDGET_FRAME(_("Auto-close quotes and brackets")); + WIDGET_CONF_BOOL(parenthesis, _("Parenthesis ( )"), + _("Auto-close parenthesis "(" -> "(|)"")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_parenthesis_cb), dialog); - WIDGET_CONF_BOOL(cbracket, "Curly brackets { }", - "Auto-close curly brackets "{" -> "{|}""); + WIDGET_CONF_BOOL(cbracket, _("Curly brackets { }"), + _("Auto-close curly brackets "{" -> "{|}"")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_cbracket_cb), dialog); - WIDGET_CONF_BOOL(sbracket, "Square brackets [ ]", - "Auto-close square brackets "[" -> "[|]""); - WIDGET_CONF_BOOL(abracket, "Angular brackets < >", - "Auto-close angular brackets "<" -> "<|>""); + WIDGET_CONF_BOOL(sbracket, _("Square brackets [ ]"), + _("Auto-close square brackets "[" -> "[|]"")); + WIDGET_CONF_BOOL(abracket, _("Angular brackets < >"), + _("Auto-close angular brackets "<" -> "<|>"")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_abracket_htmlonly_cb), dialog); - WIDGET_CONF_BOOL(abracket_htmlonly, "\tOnly for HTML", - "Auto-close angular brackets only in HTML documents"); - WIDGET_CONF_BOOL(dquote, "Double quotes " "", - "Auto-close double quotes " -> "|""); - WIDGET_CONF_BOOL(squote, "Single quotes ' '", - "Auto-close single quotes ' -> '|'"); - WIDGET_CONF_BOOL(backquote, "Backquote ` `", - "Auto-close backquote ` -> `|`"); + WIDGET_CONF_BOOL(abracket_htmlonly, _("\tOnly for HTML"), + _("Auto-close angular brackets only in HTML documents")); + WIDGET_CONF_BOOL(dquote, _("Double quotes " ""), + _("Auto-close double quotes " -> "|"")); + WIDGET_CONF_BOOL(squote, _("Single quotes ' '"), + _("Auto-close single quotes ' -> '|'")); + WIDGET_CONF_BOOL(backquote, _("Backquote ` `"), + _("Auto-close backquote ` -> `|`")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_backquote_bashonly_cb), dialog); - WIDGET_CONF_BOOL(backquote_bashonly, "\tOnly for Bash", - "Auto-close backquote only in Bash"); + WIDGET_CONF_BOOL(backquote_bashonly, _("\tOnly for Bash"), + _("Auto-close backquote only in Bash"));
- WIDGET_FRAME("Improve curly brackets completion"); - WIDGET_CONF_BOOL(make_indent_for_cbracket, "Indent when enclosing", - "If you select some text and press "{" or "}", plugin " + WIDGET_FRAME(_("Improve curly brackets completion")); + WIDGET_CONF_BOOL(make_indent_for_cbracket, _("Indent when enclosing"), + _("If you select some text and press "{" or "}", plugin " "will auto-close selected lines and make new block with indent." "\nYou do not need to select block precisely - block enclosing " - "takes into account only lines."); + "takes into account only lines.")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_make_indent_for_cbracket_cb), dialog); - WIDGET_CONF_BOOL(move_cursor_to_beginning, "Move cursor to beginning", - "If you checked "Indent when enclosing", moving cursor " + WIDGET_CONF_BOOL(move_cursor_to_beginning, _("Move cursor to beginning"), + _("If you checked "Indent when enclosing", moving cursor " "to beginning may be useful: usually you make new block " - "and need to create new statement before this block."); - WIDGET_CONF_BOOL(improved_cbracket_indent, "Improved auto-indentation", - "Improved auto-indent for curly brackets: type "{" " + "and need to create new statement before this block.")); + WIDGET_CONF_BOOL(improved_cbracket_indent, _("Improved auto-indentation"), + _("Improved auto-indent for curly brackets: type "{" " "and then press Enter - plugin will create full indented block. " - "Works without "auto-close { }" checkbox."); + "Works without "auto-close { }" checkbox.")); + WIDGET_CONF_BOOL(whitesmiths_style, _("\tWhitesmiths style"), + _("This style puts the brace associated with a control statement on " + "the next line, indented. Statements within the braces are indented " + "to the same level as the braces."));
container = vbox; - WIDGET_CONF_BOOL(delete_pairing_brace, "Delete pairing character while backspacing first", - "Check if you want to delete pairing bracket by pressing BackSpace."); + WIDGET_CONF_BOOL(delete_pairing_brace, _("Delete pairing character while backspacing first"), + _("Check if you want to delete pairing bracket by pressing BackSpace.")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_delete_pairing_brace_cb), dialog); - WIDGET_CONF_BOOL(suppress_doubling, "Suppress double-completion", - "Check if you want to allow editor automatically fix mistypes " - "with brackets: if you type "{}" you will get "{}", not "{}}"."); - WIDGET_CONF_BOOL(enclose_selections, "Enclose selections", - "Automatically enclose selected text by pressing just one bracket key."); + WIDGET_CONF_BOOL(suppress_doubling, _("Suppress double-completion"), + _("Check if you want to allow editor automatically fix mistypes " + "with brackets: if you type "{}" you will get "{}", not "{}}".")); + WIDGET_CONF_BOOL(enclose_selections, _("Enclose selections"), + _("Automatically enclose selected text by pressing just one bracket key.")); g_signal_connect(widget, "toggled", G_CALLBACK(ac_enclose_selections_cb), dialog); - WIDGET_CONF_BOOL(keep_selection, "Keep selection when enclosing", - "Keep your previously selected text after enclosing."); + WIDGET_CONF_BOOL(keep_selection, _("Keep selection when enclosing"), + _("Keep your previously selected text after enclosing."));
- WIDGET_FRAME("Behaviour inside comments and strings"); - WIDGET_CONF_BOOL(comments_ac_enable, "Allow auto-closing in strings and comments", - "Check if you wan to keep auto-closing inside strings and comments too."); - WIDGET_CONF_BOOL(comments_enclose, "Enclose selections in strings and comments", - "Check if you wan to enclose selections inside strings and comments too."); + WIDGET_FRAME(_("Behaviour inside comments and strings")); + WIDGET_CONF_BOOL(comments_ac_enable, _("Allow auto-closing in strings and comments"), + _("Check if you wan to keep auto-closing inside strings and comments too.")); + WIDGET_CONF_BOOL(comments_enclose, _("Enclose selections in strings and comments"), + _("Check if you wan to enclose selections inside strings and comments too."));
container = vbox; - WIDGET_CONF_BOOL(close_functions, "Auto-complete ";" for functions", - "Full function auto-closing (works only for C/C++): type "sin(" " - "and you will get "sin(|);"."); - WIDGET_CONF_BOOL(bcksp_remove_pair, "Shift+BackSpace removes pairing brace too", - "Remove left and right brace while pressing Shift+BackSpace.\nTip: " + WIDGET_CONF_BOOL(close_functions, _("Auto-complete ";" for functions"), + _("Full function auto-closing (works only for C/C++): type "sin(" " + "and you will get "sin(|);".")); + WIDGET_CONF_BOOL(bcksp_remove_pair, _("Shift+BackSpace removes pairing brace too"), + _("Remove left and right brace while pressing Shift+BackSpace.\nTip: " "to completely remove indented block just Shift+BackSpace first "{" " - "or last "}"."); - WIDGET_CONF_BOOL(jump_on_tab, "Jump on Tab to enclosed char", - "Jump behind autoclosed items on Tab press."); + "or last "}".")); + WIDGET_CONF_BOOL(jump_on_tab, _("Jump on Tab to enclosed char"), + _("Jump behind autoclosed items on Tab press."));
#undef WIDGET_CONF_BOOL #undef WIDGET_FRAME @@ -1124,11 +1118,27 @@ plugin_configure(GtkDialog *dialog) return scrollbox; }
+static void +autoclose_cleanup(void) +{ + guint i; + + foreach_document(i) + { + gpointer data; + ScintillaObject *sci; + + sci = documents[i]->editor->sci; + data = g_object_steal_data(G_OBJECT(sci), "autoclose-userdata"); + g_free(data); + } +} + /* Called by Geany before unloading the plugin. */ void plugin_cleanup(void) { - autoclose_handlers_cleanup(); + autoclose_cleanup(); g_free(ac_info->config_file); g_free(ac_info); }
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).