1 /*
2 * lipsum.c
3 *
4 * Copyright 2008-2017 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h" /* for the gettext domain */
23 #endif
24
25 #include <string.h>
26 #ifdef HAVE_LOCALE_H
27 # include <locale.h>
28 #endif
29
30 #include <geanyplugin.h>
31 #include <glib.h>
32 #include <glib/gstdio.h>
33 #include <errno.h>
34
35
36 GeanyPlugin *geany_plugin;
37 GeanyData *geany_data;
38
39 PLUGIN_VERSION_CHECK(224)
40 PLUGIN_SET_TRANSLATABLE_INFO(
41 LOCALEDIR,
42 GETTEXT_PACKAGE,
43 _("Lipsum"),
44 _("Creating dummy text with Geany"),
45 VERSION,
46 "Frank Lanitz <frank@frank.uvena.de>")
47
48 static GtkWidget *main_menu_item = NULL;
49 static gchar *lipsum = NULL;
50 static const gchar *default_loremipsum = "\
51 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do\
52 eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim\
53 ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut\
54 aliquip ex ea commodo consequat. Duis aute irure dolor in\
55 reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla\
56 pariatur. Excepteur sint occaecat cupidatat non proident, sunt in\
57 culpa qui officia deserunt mollit anim id est laborum. ";
58
59
60 /* Doing some basic keybinding stuff */
61 enum
62 {
63 LIPSUM_KB_INSERT,
64 COUNT_KB
65 };
66
67
68
69
70
71 static void
insert_string(GeanyDocument * doc,const gchar * string)72 insert_string(GeanyDocument *doc, const gchar *string)
73 {
74 if (doc != NULL)
75 {
76 gint pos = sci_get_current_position(doc->editor->sci);
77 sci_insert_text(doc->editor->sci, pos, string);
78 }
79 }
80
81
82 static void
lipsum_activated(G_GNUC_UNUSED GtkMenuItem * menuitem,G_GNUC_UNUSED gpointer gdata)83 lipsum_activated(G_GNUC_UNUSED GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer gdata)
84 {
85 GeanyDocument *doc = NULL;
86
87 /* Setting default length to 1500 characters */
88 gdouble value = 1500;
89
90 doc = document_get_current();
91
92 if (doc != NULL && dialogs_show_input_numeric(_("Lipsum-Generator"),
93 _("Enter the length of Lipsum text here"), &value, 1, 5000, 1))
94 {
95
96 int tmp = 0;
97 int x = 0;
98 int i = 0;
99 int missing = 0;
100
101 /* Checking what we have */
102 tmp = strlen(lipsum);
103
104 if (tmp > value)
105 {
106 x = 0;
107 missing = value - (x * tmp);
108 }
109 else if (tmp == (int)value)
110 {
111 x = 1;
112 }
113 else if (tmp > 0)
114 {
115 x = value / tmp;
116 missing = value - (x * tmp);
117 }
118
119 sci_start_undo_action(doc->editor->sci);
120
121 /* Insert lipsum snippet as often as needed ... */
122 if (missing > 0)
123 {
124 gchar *missing_text = g_strndup(lipsum, missing);
125 insert_string(doc, missing_text);
126 g_free(missing_text);
127 }
128 for (i = 0; i < x; i++)
129 {
130 insert_string(doc, lipsum);
131 }
132 sci_end_undo_action(doc->editor->sci);
133 }
134 }
135
136
137 /* Called when keystroke were pressed */
kblipsum_insert(G_GNUC_UNUSED guint key_id)138 static void kblipsum_insert(G_GNUC_UNUSED guint key_id)
139 {
140 lipsum_activated(NULL, NULL);
141 }
142
143
144 /* Called by Geany to initialize the plugin */
145 void
plugin_init(G_GNUC_UNUSED GeanyData * data)146 plugin_init(G_GNUC_UNUSED GeanyData *data)
147 {
148 GtkWidget *menu_lipsum = NULL;
149 GKeyFile *config = g_key_file_new();
150 gchar *config_file = NULL;
151 gchar *config_file_old = NULL;
152 gchar *config_dir = NULL;
153 gchar *config_dir_old = NULL;
154 GeanyKeyGroup *key_group;
155
156
157 config_file = g_strconcat(geany->app->configdir,
158 G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
159 "geanylipsum", G_DIR_SEPARATOR_S, "lipsum.conf", NULL);
160
161 #ifndef G_OS_WIN32
162 /* We try only to move if we are on not Windows platform */
163 config_dir_old = g_build_filename(geany->app->configdir,
164 "plugins", "geanylipsum", NULL);
165 config_file_old = g_build_filename(config_dir_old,
166 "lipsum.conf", NULL);
167 config_dir = g_build_filename(geany->app->configdir,
168 "plugins", "lipsum", NULL);
169 if (g_file_test(config_file_old, G_FILE_TEST_EXISTS))
170 {
171 if (dialogs_show_question(
172 _("Renamed plugin detected!\n"
173 "\n"
174 "As you may have already noticed, GeanyLipsum has been "
175 "renamed to just Lipsum. \n"
176 "Geany is able to migrate your old plugin configuration by "
177 "moving the old configuration file to new location.\n"
178 "Warning: This will not include your keybindings.\n"
179 "Move now?")))
180 {
181 if (g_rename(config_dir_old, config_dir) == 0)
182 {
183 dialogs_show_msgbox(GTK_MESSAGE_INFO,
184 _("Your configuration directory has been "
185 "successfully moved from \"%s\" to \"%s\"."),
186 config_dir_old, config_dir);
187 }
188 else
189 {
190 /* If there was an error on migrating we need
191 * to load from original one.
192 * When saving new configuration it will go to
193 * new folder so migration should
194 * be implicit. */
195 g_free(config_file);
196 config_file = g_strdup(config_file_old);
197 dialogs_show_msgbox(
198 GTK_MESSAGE_WARNING,
199 _("Your old configuration directory \"%s\" could "
200 "not be moved to \"%s\" (%s). "
201 "Please manually move the directory to the new location."),
202 config_dir_old,
203 config_dir,
204 g_strerror(errno));
205 }
206 }
207 }
208
209 g_free(config_dir_old);
210 g_free(config_dir);
211 g_free(config_file_old);
212 #endif
213
214 /* Initialising options from config file if there is any*/
215 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
216 lipsum = utils_get_setting_string(config, "snippets", "lipsumtext", default_loremipsum);
217
218 g_key_file_free(config);
219 g_free(config_file);
220
221 /* Building menu entry */
222 #if GTK_CHECK_VERSION(3, 10, 0)
223 menu_lipsum = gtk_menu_item_new_with_mnemonic(_("_Lipsum..."));
224 #else
225 menu_lipsum = gtk_image_menu_item_new_with_mnemonic(_("_Lipsum..."));
226 #endif
227 gtk_widget_set_tooltip_text(menu_lipsum, _("Include Pseudotext to your code"));
228 gtk_widget_show(menu_lipsum);
229 g_signal_connect((gpointer) menu_lipsum, "activate",
230 G_CALLBACK(lipsum_activated), NULL);
231 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_lipsum);
232
233
234 ui_add_document_sensitive(menu_lipsum);
235
236 main_menu_item = menu_lipsum;
237
238 /* init keybindings */
239 key_group = plugin_set_key_group(geany_plugin, "lipsum", COUNT_KB, NULL);
240 keybindings_set_item(key_group, LIPSUM_KB_INSERT, kblipsum_insert,
241 0, 0, "insert_lipsum", _("Insert Lipsum text"), menu_lipsum);
242 }
243
244
245 /* Called by Geany before unloading the plugin. */
plugin_cleanup(void)246 void plugin_cleanup(void)
247 {
248 /* remove the menu item added in plugin_init() */
249 gtk_widget_destroy(main_menu_item);
250
251 /* free lipsum snippet */
252 g_free(lipsum);
253 }
254