xeritt

Plugin for geany Format C linux kernel style

Mar 22nd, 2018
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *      format_c.c
  3.  *
  4.  *      https://www.geany.org/manual/reference/howto.html
  5.  *
  6.  *      yum install intltool geany-devel
  7.  *      yum install indent
  8.  *
  9.  *      gcc -c format_c.c -fPIC `pkg-config --cflags geany`
  10.  *      gcc format_c.o -o format_c.so -shared `pkg-config --libs geany`
  11.  *      sudo cp format_c.so /usr/lib/x86_64-linux-gnu/geany
  12.  *
  13.  *      Copyright 2018 Nikolay <xeritt(at)gmail.com>
  14.  *
  15.  *      This program is free software; you can redistribute it and/or modify
  16.  *      it under the terms of the GNU General Public License as published by
  17.  *      the Free Software Foundation; either version 2 of the License, or
  18.  *      (at your option) any later version.
  19.  *
  20.  *      This program is distributed in the hope that it will be useful,
  21.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  *      GNU General Public License for more details.
  24.  *
  25.  *      You should have received a copy of the GNU General Public License
  26.  *      along with this program; if not, write to the Free Software
  27.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  28.  *      MA 02110-1301, USA.
  29.  */
  30.  
  31. #include <geanyplugin.h>
  32. #include <stdlib.h>
  33.  
  34. static void item_activate_cb(GtkMenuItem * menuitem, gpointer user_data)
  35. {
  36.     GeanyDocument *doc;
  37.     doc = document_get_current();
  38.  
  39.     char command[1024];
  40.     sprintf(command, "indent -kr -i8 -ts8 -sob -l80 -ss -bs -linux \"%s\"",
  41.         doc->file_name);
  42.     gint error_nr = system(command);
  43.     if (error_nr == 0) {
  44.         ui_set_statusbar(TRUE,
  45.                  ("Document '%s' successfully format. Reload document!"),
  46.                  doc->file_name);
  47.         gboolean res = document_reload_force(doc, NULL);
  48.     } else
  49.         ui_set_statusbar(TRUE, _("File '%s' could not be format (%s)."),
  50.                  doc->file_name, g_strerror(error_nr));
  51. }
  52.  
  53. static void on_activate_cb(G_GNUC_UNUSED guint key_id)
  54. {
  55.     item_activate_cb(NULL, NULL);
  56. }
  57.  
  58.  
  59. static gboolean save_init(GeanyPlugin * plugin, gpointer pdata)
  60. {
  61.     GtkWidget *main_menu_item;
  62.     // Create a new menu item and show it
  63.     main_menu_item = gtk_menu_item_new_with_mnemonic("Format C/Linux");
  64.     gtk_widget_show(main_menu_item);
  65.     gtk_container_add(GTK_CONTAINER
  66.               (plugin->geany_data->main_widgets->tools_menu),
  67.               main_menu_item);
  68.     g_signal_connect(main_menu_item, "activate",
  69.              G_CALLBACK(item_activate_cb), NULL);
  70.  
  71.     GeanyKeyGroup *group;
  72.  
  73.     group = plugin_set_key_group(plugin, "Format code", 1, NULL);
  74.      
  75.     keybindings_set_item(group, 0, on_activate_cb,
  76.                  0, 0, "format c", _("Format code C linux style"),
  77.                  main_menu_item);
  78.     geany_plugin_set_data(plugin, main_menu_item, NULL);
  79.     return TRUE;
  80. }
  81.  
  82. static void save_cleanup(GeanyPlugin * plugin, gpointer pdata)
  83. {
  84.     GtkWidget *main_menu_item = (GtkWidget *) pdata;
  85.     gtk_widget_destroy(main_menu_item);
  86. }
  87.  
  88. G_MODULE_EXPORT void geany_load_module(GeanyPlugin * plugin)
  89. {
  90.     /* Step 1: Set metadata */
  91.     plugin->info->name = "Format C/Linux";
  92.     plugin->info->description = "Format document by C linux kernel style";
  93.     plugin->info->version = "1.0";
  94.     plugin->info->author = "Nikolay <xeritt@gmail.com>";
  95.     /* Step 2: Set functions */
  96.     plugin->funcs->init = save_init;
  97.     plugin->funcs->cleanup = save_cleanup;
  98.     /* Step 3: Register! */
  99.     GEANY_PLUGIN_REGISTER(plugin, 225);
  100.     /* alternatively:
  101.        GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, free_func); */
  102. }
RAW Paste Data Copied