Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * refactoring.c 2021
- * https://www.geany.org/manual/reference/howto.html
- *
- * RedHat: yum install intltool geany-devel
- * Ubuntu: sudo apt-get install libgtk-3-dev
- *
- * gcc -c savecopy.c -fPIC `pkg-config --cflags geany`
- * gcc savecopy.o -o savecopy.so -shared `pkg-config --libs geany`
- * sudo cp format_c.so /usr/lib/x86_64-linux-gnu/geany
- *
- * Copyright 2021 Nikolay <xeritt(at)gmail.com>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- */
- #include <stdio.h>
- #include <geanyplugin.h>
- static void write_data(const gchar *filename, const gchar *data){
- gint error_nr = utils_write_file(filename, data);
- gchar *utf8_filename = utils_get_utf8_from_locale(filename);
- if (error_nr == 0)
- ui_set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
- else
- ui_set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
- utf8_filename, g_strerror(error_nr));
- g_free(utf8_filename);
- }
- static void extractConst(GtkMenuItem *menuitem, gpointer user_data){
- GeanyDocument *doc;
- doc = document_get_current();
- //dialogs_show_msgbox(GTK_MESSAGE_INFO, sci_get_selection_contents(doc->editor->sci));
- gint doc_len, i;
- doc_len = sci_get_length(doc->editor->sci);
- gchar c, c_next;
- int fCom = 0;
- for (i = 0; i < doc_len; ++i){
- c = sci_get_char_at(doc->editor->sci, i);
- if (c == '*') {
- fCom++;
- }
- if (fCom>0){
- if (c == '/'){
- ++i;
- gchar* seltext = sci_get_selection_contents(doc->editor->sci);
- int offset = 13;
- sci_insert_text(doc->editor->sci, i, "\n");
- sci_insert_text(doc->editor->sci, i + 1, ";");
- sci_insert_text(doc->editor->sci, i + 1, "const <type>");
- sci_insert_text(doc->editor->sci, i + offset, seltext);
- sci_set_current_position(doc->editor->sci, i + offset, TRUE);
- ui_set_statusbar(TRUE, _("Extract const at position '%i'."), i);
- break;
- }
- }
- }
- }
- static void extractDefine(GtkMenuItem *menuitem, gpointer user_data){
- GeanyDocument *doc;
- doc = document_get_current();
- //dialogs_show_msgbox(GTK_MESSAGE_INFO, sci_get_selection_contents(doc->editor->sci));
- gint doc_len, i;
- doc_len = sci_get_length(doc->editor->sci);
- gchar c, c_next;
- int fCom = 0;
- for (i = 0; i < doc_len; ++i){
- c = sci_get_char_at(doc->editor->sci, i);
- if (c == '*') {
- fCom++;
- }
- if (fCom>0){
- if (c == '/'){
- ++i;
- gchar* seltext = sci_get_selection_contents(doc->editor->sci);
- int offset = 9;
- sci_insert_text(doc->editor->sci, i, "\n");
- sci_insert_text(doc->editor->sci, i + 1, "#define ");
- sci_insert_text(doc->editor->sci, i + offset, seltext);
- sci_set_current_position(doc->editor->sci, i + offset, TRUE);
- ui_set_statusbar(TRUE, _("Extract define at position '%i'."), i);
- break;
- }
- }
- }
- }
- static gboolean save_init(GeanyPlugin *plugin, gpointer pdata){
- GtkWidget* refactMenu;
- GtkWidget* refactSubItem;
- GtkWidget* menuItem;
- GtkWidget* extractConstItem;
- refactMenu = gtk_menu_new();
- refactSubItem = gtk_menu_item_new_with_label("Refactoring");
- menuItem = gtk_menu_item_new_with_label("Extract #define ...");
- extractConstItem = gtk_menu_item_new_with_label("Extract const ...");
- gtk_menu_item_set_submenu(GTK_MENU_ITEM(refactSubItem), refactMenu);
- gtk_menu_shell_append(GTK_MENU_SHELL(refactMenu), menuItem);
- gtk_menu_shell_append(GTK_MENU_SHELL(refactMenu), extractConstItem);
- gtk_widget_show(menuItem);
- gtk_widget_show(extractConstItem);
- gtk_widget_show(refactSubItem);
- gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->editor_menu), refactSubItem);
- g_signal_connect(menuItem, "activate", G_CALLBACK(extractDefine), NULL);
- g_signal_connect(extractConstItem, "activate", G_CALLBACK(extractConst), NULL);
- geany_plugin_set_data(plugin, refactMenu, NULL);
- return TRUE;
- }
- static void save_cleanup(GeanyPlugin *plugin, gpointer pdata)
- {
- GtkWidget *main_menu_item = (GtkWidget *) pdata;
- gtk_widget_destroy(main_menu_item);
- }
- G_MODULE_EXPORT
- void geany_load_module(GeanyPlugin *plugin)
- {
- /* Step 1: Set metadata */
- plugin->info->name = "Refactoring";
- plugin->info->description = "Simple action code refactoring";
- plugin->info->version = "1.0";
- /* Step 2: Set functions */
- plugin->funcs->init = save_init;
- plugin->funcs->cleanup = save_cleanup;
- /* Step 3: Register! */
- GEANY_PLUGIN_REGISTER(plugin, 225);
- /* alternatively:
- GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, free_func); */
- }
Add Comment
Please, Sign In to add comment