Advertisement
xeritt

Plugin for geany Save copy document

Feb 22nd, 2018
368
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *      savecopy.c
  3.  *
  4.  *      https://www.geany.org/manual/reference/howto.html
  5.  *     
  6.  *      yum install intltool geany-devel
  7.  *
  8.  *      gcc -c savecopy.c -fPIC `pkg-config --cflags geany`
  9.  *      gcc savecopy.o -o savecopy.so -shared `pkg-config --libs geany`
  10.  *
  11.  *      Copyright 2018 Nikolay <xeritt(at)gmail.com>
  12.  *      
  13.  *      This program is free software; you can redistribute it and/or modify
  14.  *      it under the terms of the GNU General Public License as published by
  15.  *      the Free Software Foundation; either version 2 of the License, or
  16.  *      (at your option) any later version.
  17.  *      
  18.  *      This program is distributed in the hope that it will be useful,
  19.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *      GNU General Public License for more details.
  22.  *      
  23.  *      You should have received a copy of the GNU General Public License
  24.  *      along with this program; if not, write to the Free Software
  25.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  26.  *      MA 02110-1301, USA.
  27.  */
  28.  
  29. #include <geanyplugin.h>
  30. static void write_data(const gchar *filename, const gchar *data){
  31.     gint error_nr = utils_write_file(filename, data);
  32.     gchar *utf8_filename = utils_get_utf8_from_locale(filename);
  33.  
  34.     if (error_nr == 0)
  35.         ui_set_statusbar(TRUE, _("Document successfully exported as '%s'."), utf8_filename);
  36.     else
  37.         ui_set_statusbar(TRUE, _("File '%s' could not be written (%s)."),
  38.             utf8_filename, g_strerror(error_nr));
  39.  
  40.     g_free(utf8_filename);
  41. }
  42.  
  43. static void item_activate_cb(GtkMenuItem *menuitem, gpointer user_data){
  44.     GeanyDocument *doc;
  45.     doc = document_get_current();
  46.    
  47.     gint doc_len, i;
  48.     gchar c, c_next;
  49.    
  50.     doc_len = sci_get_length(doc->editor->sci);
  51.     GString *body;
  52.     body = g_string_new("");
  53.    
  54.     char new_name[doc_len];
  55.     sprintf(new_name, "%s_copy", doc->file_name);
  56.     for (i = 0; i < doc_len; i++){
  57.         c = sci_get_char_at(doc->editor->sci, i);
  58.         g_string_append_c(body, c);
  59.     }  
  60.     write_data(new_name, body->str);
  61.     g_string_free(body, TRUE);
  62. }
  63.  
  64. static gboolean save_init(GeanyPlugin *plugin, gpointer pdata){
  65.     GtkWidget *main_menu_item;
  66.     // Create a new menu item and show it
  67.     main_menu_item = gtk_menu_item_new_with_mnemonic("Save copy");
  68.     gtk_widget_show(main_menu_item);
  69.     gtk_container_add(GTK_CONTAINER(plugin->geany_data->main_widgets->tools_menu),
  70.             main_menu_item);
  71.     g_signal_connect(main_menu_item, "activate",
  72.             G_CALLBACK(item_activate_cb), NULL);
  73.     geany_plugin_set_data(plugin, main_menu_item, NULL);
  74.     return TRUE;
  75. }
  76. static void save_cleanup(GeanyPlugin *plugin, gpointer pdata)
  77. {
  78.     GtkWidget *main_menu_item = (GtkWidget *) pdata;
  79.     gtk_widget_destroy(main_menu_item);
  80. }
  81. G_MODULE_EXPORT
  82. void geany_load_module(GeanyPlugin *plugin)
  83. {
  84.     /* Step 1: Set metadata */
  85.     plugin->info->name = "Save Copy";
  86.     plugin->info->description = "Just save copy document";
  87.     plugin->info->version = "1.0";
  88.     plugin->info->author = "Nikolay <xeritt@gmail.com>";
  89.     /* Step 2: Set functions */
  90.     plugin->funcs->init = save_init;
  91.     plugin->funcs->cleanup = save_cleanup;
  92.     /* Step 3: Register! */
  93.     GEANY_PLUGIN_REGISTER(plugin, 225);
  94.     /* alternatively:
  95.     GEANY_PLUGIN_REGISTER_FULL(plugin, 225, data, free_func); */
  96. }
Advertisement
RAW Paste Data Copied
Advertisement