Created
January 18, 2014 20:56
-
-
Save codebrainz/8496313 to your computer and use it in GitHub Desktop.
Geany plugin to toggle long line marker between off and line mode using keybinding or menu item.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env make | |
| llt_cflags = $(CFLAGS) -g -Wall -Werror $(shell pkg-config --cflags geany) | |
| llt_ldflags = $(LDFLAGS) $(shell pkg-config --libs geany) | |
| all: llt.so | |
| llt.so: plugin.o | |
| $(CC) -shared $(llt_cflags) -o $@ $^ $(llt_ldflags) | |
| plugin.o: plugin.c | |
| $(CC) -c -fPIC $(llt_cflags) -o $@ $< | |
| install: | |
| cp llt.so ~/.config/geany/plugins/ | |
| clean: | |
| $(RM) *.o *.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <geanyplugin.h> | |
| GeanyPlugin *geany_plugin; | |
| GeanyFunctions *geany_functions; | |
| GeanyData *geany_data; | |
| PLUGIN_VERSION_CHECK(211) | |
| PLUGIN_SET_INFO(_("LongLineToggler"), | |
| _("Adds a keybinding and menu item to toggle long-line marker"), "0.01", | |
| "Matthew Brush <matt@geany.org>") | |
| static const char *menu_view_name = "menu_view1_menu"; | |
| static const char *menu_previous_sibling = "menu_show_indentation_guides1"; | |
| static GtkWidget *long_line_toggler_widget = NULL; | |
| static gboolean edge_active_toggle_kb(guint key_id) | |
| { | |
| gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(long_line_toggler_widget), | |
| !gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(long_line_toggler_widget))); | |
| return FALSE; // let others handle it too | |
| } | |
| static void edge_mode_menu_toggled(GtkCheckMenuItem *menuitem, gpointer user_data) | |
| { | |
| GeanyDocument *doc = document_get_current(); | |
| if (!DOC_VALID(doc)) return; | |
| ScintillaObject *sci = doc->editor->sci; | |
| gint mode = scintilla_send_message(sci, SCI_GETEDGEMODE, 0, 0); | |
| guint i; | |
| foreach_document(i) { | |
| doc = document_index(i); | |
| // Note: only disabled or line edge mode supported, not background mode | |
| scintilla_send_message(doc->editor->sci, SCI_SETEDGEMODE, | |
| (mode == EDGE_NONE) ? EDGE_LINE : EDGE_NONE, 0); | |
| } | |
| } | |
| static inline gint where_to_put_item(GtkContainer *container) | |
| { | |
| gint index = 0; | |
| GtkWidget *after_wid = ui_lookup_widget(geany_data->main_widgets->window, | |
| menu_previous_sibling); | |
| GList *iter, *children = gtk_container_get_children(GTK_CONTAINER(container)); | |
| for (iter = children; iter != NULL; iter = g_list_next(iter)) { | |
| if (GTK_WIDGET(iter->data) == after_wid) { | |
| g_list_free(children); | |
| return index; | |
| } | |
| index++; | |
| } | |
| g_list_free(children); | |
| return -1; | |
| } | |
| static gboolean is_line_currently_shown() | |
| { | |
| GeanyDocument *doc = document_get_current(); | |
| if (!DOC_VALID(doc)) return FALSE; | |
| ScintillaObject *sci = doc->editor->sci; | |
| return (scintilla_send_message(sci, SCI_GETEDGEMODE, 0, 0) != EDGE_NONE); | |
| } | |
| // handle case of no documents open when the keybinding is toggled, to | |
| // synchronize the new/opened document setting with the menu item's | |
| static void when_a_document_is_opened(GObject *unused, GeanyDocument *doc, | |
| gpointer user_data) | |
| { | |
| scintilla_send_message(doc->editor->sci, SCI_SETEDGEMODE, | |
| gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(long_line_toggler_widget)) ? | |
| EDGE_LINE : EDGE_NONE, 0); | |
| } | |
| static void add_menu_item_on_startup(GObject *unused, gpointer user_data) | |
| { | |
| GtkWidget *menu = ui_lookup_widget(geany_data->main_widgets->window, menu_view_name); | |
| long_line_toggler_widget = gtk_check_menu_item_new_with_label(_("Show Long Line Marker")); | |
| gtk_menu_shell_append(GTK_MENU_SHELL(menu), long_line_toggler_widget); | |
| gint item_pos = where_to_put_item(GTK_CONTAINER(menu)); | |
| if (item_pos > -1) | |
| gtk_menu_reorder_child(GTK_MENU(menu), long_line_toggler_widget, item_pos + 1); | |
| gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(long_line_toggler_widget), | |
| is_line_currently_shown()); | |
| g_signal_connect(long_line_toggler_widget, "toggled", | |
| G_CALLBACK(edge_mode_menu_toggled), NULL); | |
| plugin_signal_connect(geany_plugin, NULL, "document-open", TRUE, | |
| G_CALLBACK(when_a_document_is_opened), NULL); | |
| plugin_signal_connect(geany_plugin, NULL, "document-new", TRUE, | |
| G_CALLBACK(when_a_document_is_opened), NULL); | |
| gtk_widget_show(long_line_toggler_widget); | |
| } | |
| void plugin_init(void) | |
| { | |
| plugin_signal_connect(geany_plugin, NULL, "geany-startup-complete", | |
| TRUE, G_CALLBACK(add_menu_item_on_startup), NULL); | |
| keybindings_set_item( | |
| plugin_set_key_group(geany_plugin, "LongLineToggler", 1, NULL), | |
| 0, (GeanyKeyCallback) edge_active_toggle_kb, 0, 0, "long-line-toggler", | |
| "Toggle long line marker", NULL); | |
| } | |
| void plugin_cleanup(void) | |
| { | |
| if (GTK_IS_WIDGET(long_line_toggler_widget)) | |
| gtk_widget_destroy(long_line_toggler_widget); | |
| long_line_toggler_widget = NULL; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment