xeritt

Plugin for Geany Refactoring C code

Dec 17th, 2021 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.72 KB | None | 0 0
  1. /*
  2.  *      refactoring.c 2021
  3.  *      https://www.geany.org/manual/reference/howto.html
  4.  *     
  5.  *      RedHat: yum install intltool geany-devel
  6.  *      Ubuntu: sudo apt-get install libgtk-3-dev
  7.  *
  8.  *      gcc -c savecopy.c -fPIC `pkg-config --cflags geany`
  9.  *      gcc savecopy.o -o savecopy.so -shared `pkg-config --libs geany`
  10.  *      sudo cp format_c.so /usr/lib/x86_64-linux-gnu/geany
  11.  *
  12.  *      Copyright 2021 Nikolay <xeritt(at)gmail.com>
  13.  *      
  14.  *      This program is free software; you can redistribute it and/or modify
  15.  *      it under the terms of the GNU General Public License as published by
  16.  *      the Free Software Foundation; either version 2 of the License, or
  17.  *      (at your option) any later version.
  18.  *      
  19.  *      This program is distributed in the hope that it will be useful,
  20.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  *      GNU General Public License for more details.
  23.  *      
  24.  *      You should have received a copy of the GNU General Public License
  25.  *      along with this program; if not, write to the Free Software
  26.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  27.  *      MA 02110-1301, USA.
  28.  */
  29. #include <stdio.h>
  30. #include <geanyplugin.h>
  31. static void write_data(const gchar *filename, const gchar *data){
  32.     gint error_nr = utils_write_file(filename, data);
  33.     gchar *utf8_filename = utils_get_utf8_from_locale(filename);
  34.  
  35.     if (error_nr == 0)
  36.         ui_set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
  37.     else
  38.         ui_set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
  39.             utf8_filename, g_strerror(error_nr));
  40.  
  41.     g_free(utf8_filename);
  42. }
  43.  
  44. static void extractConst(GtkMenuItem *menuitem, gpointer user_data){
  45.     GeanyDocument *doc;
  46.     doc = document_get_current();
  47.    
  48.     //dialogs_show_msgbox(GTK_MESSAGE_INFO, sci_get_selection_contents(doc->editor->sci));
  49.  
  50.     gint doc_len, i;
  51.     doc_len = sci_get_length(doc->editor->sci);
  52.    
  53.     gchar c, c_next;
  54.     int fCom = 0;
  55.     for (i = 0; i < doc_len; ++i){
  56.         c = sci_get_char_at(doc->editor->sci, i);
  57.         if (c == '*') {
  58.             fCom++;
  59.         }
  60.         if (fCom>0){
  61.             if (c == '/'){
  62.                 ++i;
  63.                 gchar* seltext = sci_get_selection_contents(doc->editor->sci);
  64.                 int offset = 13;
  65.                 sci_insert_text(doc->editor->sci, i, "\n");
  66.                 sci_insert_text(doc->editor->sci, i + 1, ";");
  67.                 sci_insert_text(doc->editor->sci, i + 1, "const <type>");
  68.                 sci_insert_text(doc->editor->sci, i + offset, seltext);
  69.                 sci_set_current_position(doc->editor->sci, i + offset, TRUE);
  70.                 ui_set_statusbar(TRUE, _("Extract const at position '%i'."), i);
  71.                 break;
  72.             }
  73.         }
  74.     }    
  75. }
  76.  
  77.  
  78. static void extractDefine(GtkMenuItem *menuitem, gpointer user_data){
  79.     GeanyDocument *doc;
  80.     doc = document_get_current();
  81.    
  82.     //dialogs_show_msgbox(GTK_MESSAGE_INFO, sci_get_selection_contents(doc->editor->sci));
  83.  
  84.     gint doc_len, i;
  85.     doc_len = sci_get_length(doc->editor->sci);
  86.    
  87.     gchar c, c_next;
  88.     int fCom = 0;
  89.     for (i = 0; i < doc_len; ++i){
  90.         c = sci_get_char_at(doc->editor->sci, i);
  91.         if (c == '*') {
  92.             fCom++;
  93.         }
  94.         if (fCom>0){
  95.             if (c == '/'){
  96.                 ++i;
  97.                 gchar* seltext = sci_get_selection_contents(doc->editor->sci);
  98.                 int offset = 9;
  99.                 sci_insert_text(doc->editor->sci, i, "\n");
  100.                 sci_insert_text(doc->editor->sci, i + 1, "#define  ");
  101.                 sci_insert_text(doc->editor->sci, i + offset, seltext);
  102.                 sci_set_current_position(doc->editor->sci, i + offset, TRUE);
  103.                 ui_set_statusbar(TRUE, _("Extract define at position '%i'."), i);
  104.                 break;
  105.             }
  106.         }
  107.     }    
  108. }
  109.  
  110. static gboolean save_init(GeanyPlugin *plugin, gpointer pdata){
  111.  
  112.   GtkWidget* refactMenu;
  113.   GtkWidget* refactSubItem;
  114.   GtkWidget* menuItem;
  115.   GtkWidget* extractConstItem;
  116.   refactMenu = gtk_menu_new();
  117.  
  118.   refactSubItem = gtk_menu_item_new_with_label("Refactoring");
  119.   menuItem = gtk_menu_item_new_with_label("Extract #define ...");
  120.   extractConstItem = gtk_menu_item_new_with_label("Extract const ...");
  121.  
  122.   gtk_menu_item_set_submenu(GTK_MENU_ITEM(refactSubItem), refactMenu);
  123.   gtk_menu_shell_append(GTK_MENU_SHELL(refactMenu), menuItem);
  124.   gtk_menu_shell_append(GTK_MENU_SHELL(refactMenu), extractConstItem);
  125.  
  126.   gtk_widget_show(menuItem);
  127.   gtk_widget_show(extractConstItem);
  128.   gtk_widget_show(refactSubItem);
  129.  
  130.   gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->editor_menu), refactSubItem);  
  131.   g_signal_connect(menuItem, "activate", G_CALLBACK(extractDefine), NULL);
  132.   g_signal_connect(extractConstItem, "activate", G_CALLBACK(extractConst), NULL);
  133.   geany_plugin_set_data(plugin, refactMenu, NULL);
  134.  
  135.   return TRUE;
  136. }
  137. static void save_cleanup(GeanyPlugin *plugin, gpointer pdata)
  138. {
  139.     GtkWidget *main_menu_item = (GtkWidget *) pdata;
  140.     gtk_widget_destroy(main_menu_item);
  141. }
  142. G_MODULE_EXPORT
  143. void geany_load_module(GeanyPlugin *plugin)
  144. {
  145.     /* Step 1: Set metadata */
  146.     plugin->info->name = "Refactoring";
  147.     plugin->info->description = "Simple action code refactoring";
  148.     plugin->info->version = "1.0";
  149.     plugin->info->author = "Nikolay <[email protected]>";
  150.     /* Step 2: Set functions */
  151.     plugin->funcs->init = save_init;
  152.     plugin->funcs->cleanup = save_cleanup;
  153.     /* Step 3: Register! */
  154.     GEANY_PLUGIN_REGISTER(plugin, 225);
  155.     /* alternatively:
  156.     GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, free_func); */
  157. }
  158.  
  159.  
  160.  
Add Comment
Please, Sign In to add comment