Fix escaping of session filepaths
[geany-mirror.git] / plugins / htmlchars.c
blobe0ad4bac3ca9ec9b1e82d778077e68e0ffb1e99f
1 /*
2 * htmlchars.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2011 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 * Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6 * Copyright 2007-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301, USA.
24 /* HTML Characters plugin (Inserts HTML character entities like '&amp;') */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include "geanyplugin.h"
31 #include <string.h>
32 #include "SciLexer.h"
35 GeanyData *geany_data;
36 GeanyFunctions *geany_functions;
39 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
41 PLUGIN_SET_INFO(_("HTML Characters"), _("Inserts HTML character entities like '&amp;'."), VERSION,
42 _("The Geany developer team"))
45 /* Keybinding(s) */
46 enum
48 KB_INSERT_HTML_CHARS,
49 KB_REPLACE_HTML_ENTITIES,
50 KB_HTMLTOGGLE_ACTIVE,
51 KB_COUNT
54 PLUGIN_KEY_GROUP(html_chars, KB_COUNT)
57 enum
59 COLUMN_CHARACTER,
60 COLUMN_HTML_NAME,
61 N_COLUMNS
64 static GtkWidget *main_menu_item = NULL;
65 static GtkWidget *main_menu = NULL;
66 static GtkWidget *main_menu_submenu = NULL;
67 static GtkWidget *menu_bulk_replace = NULL;
68 static GtkWidget *sc_dialog = NULL;
69 static GtkTreeStore *sc_store = NULL;
70 static GtkTreeView *sc_tree = NULL;
71 static GtkWidget *menu_htmltoggle = NULL;
72 static gboolean plugin_active = FALSE;
74 /* Configuration file */
75 static gchar *config_file = NULL;
77 const gchar *chars[][2] ={
78 { N_("HTML characters"), NULL },
79 { "\"", "&quot;" },
80 { "&", "&amp;" },
81 { "<", "&lt;" },
82 { ">", "&gt;" },
84 { N_("ISO 8859-1 characters"), NULL },
85 { " ", "&nbsp;" },
86 { "¡", "&iexcl;" },
87 { "¢", "&cent;" },
88 { "£", "&pound;" },
89 { "¤", "&curren;" },
90 { "¥", "&yen;" },
91 { "¦", "&brvbar;" },
92 { "§", "&sect;" },
93 { "¨", "&uml;" },
94 { "©", "&copy;" },
95 { "®", "&reg;" },
96 { "«", "&laquo;" },
97 { "»", "&raquo;" },
98 { "¬", "&not;" },
99 { " ", "&shy;" },
100 { "¯", "&macr;" },
101 { "°", "&deg;" },
102 { "±", "&plusmn;" },
103 { "¹", "&sup1;" },
104 { "²", "&sup2;" },
105 { "³", "&sup3;" },
106 { "¼", "&frac14;" },
107 { "½", "&frac12;" },
108 { "¾", "&frac34;" },
109 { "×", "&times;" },
110 { "÷", "&divide;" },
111 { "´", "&acute;" },
112 { "µ", "&micro;" },
113 { "¶", "&para;" },
114 { "·", "&middot;" },
115 { "¸", "&cedil;" },
116 { "ª", "&ordf;" },
117 { "º", "&ordm;" },
118 { "¿", "&iquest;" },
119 { "À", "&Agrave;" },
120 { "Á", "&Aacute;" },
121 { "Â", "&Acirc;" },
122 { "Ã", "&Atilde;" },
123 { "Ä", "&Auml;" },
124 { "Å", "&Aring;" },
125 { "Æ", "&AElig;" },
126 { "Ç", "&Ccedil;" },
127 { "È", "&Egrave;" },
128 { "É", "&Eacute;" },
129 { "Ê", "&Ecirc;" },
130 { "Ë", "&Euml;" },
131 { "Ì", "&Igrave;" },
132 { "Í", "&Iacute;" },
133 { "Î", "&Icirc;" },
134 { "Ï", "&Iuml;" },
135 { "Ð", "&ETH;" },
136 { "Ñ", "&Ntilde;" },
137 { "Ò", "&Ograve;" },
138 { "Ó", "&Oacute;" },
139 { "Ô", "&Ocirc;" },
140 { "Õ", "&Otilde;" },
141 { "Ö", "&Ouml;" },
142 { "Ø", "&Oslash;" },
143 { "Ù", "&Ugrave;" },
144 { "Ú", "&Uacute;" },
145 { "Û", "&Ucirc;" },
146 { "Ü", "&Uuml;" },
147 { "Ý", "&Yacute;" },
148 { "Þ", "&THORN;" },
149 { "ß", "&szlig;" },
150 { "à", "&agrave;" },
151 { "á", "&aacute;" },
152 { "â", "&acirc;" },
153 { "ã", "&atilde;" },
154 { "ä", "&auml;" },
155 { "å", "&aring;" },
156 { "æ", "&aelig;" },
157 { "ç", "&ccedil;" },
158 { "è", "&egrave;" },
159 { "é", "&eacute;" },
160 { "ê", "&ecirc;" },
161 { "ë", "&euml;" },
162 { "ì", "&igrave;" },
163 { "í", "&iacute;" },
164 { "î", "&icirc;" },
165 { "ï", "&iuml;" },
166 { "ð", "&eth;" },
167 { "ñ", "&ntilde;" },
168 { "ò", "&ograve;" },
169 { "ó", "&oacute;" },
170 { "ô", "&ocirc;" },
171 { "õ", "&otilde;" },
172 { "ö", "&ouml;" },
173 { "ø", "&oslash;" },
174 { "ù", "&ugrave;" },
175 { "ú", "&uacute;" },
176 { "û", "&ucirc;" },
177 { "ü", "&uuml;" },
178 { "ý", "&yacute;" },
179 { "þ", "&thorn;" },
180 { "ÿ", "&yuml;" },
182 { N_("Greek characters"), NULL },
183 { "Α", "&Alpha;" },
184 { "α", "&alpha;" },
185 { "Β", "&Beta;" },
186 { "β", "&beta;" },
187 { "Γ", "&Gamma;" },
188 { "γ", "&gamma;" },
189 { "Δ", "&Delta;" },
190 { "δ", "&Delta;" },
191 { "δ", "&delta;" },
192 { "Ε", "&Epsilon;" },
193 { "ε", "&epsilon;" },
194 { "Ζ", "&Zeta;" },
195 { "ζ", "&zeta;" },
196 { "Η", "&Eta;" },
197 { "η", "&eta;" },
198 { "Θ", "&Theta;" },
199 { "θ", "&theta;" },
200 { "Ι", "&Iota;" },
201 { "ι", "&iota;" },
202 { "Κ", "&Kappa;" },
203 { "κ", "&kappa;" },
204 { "Λ", "&Lambda;" },
205 { "λ", "&lambda;" },
206 { "Μ", "&Mu;" },
207 { "μ", "&mu;" },
208 { "Ν", "&Nu;" },
209 { "ν", "&nu;" },
210 { "Ξ", "&Xi;" },
211 { "ξ", "&xi;" },
212 { "Ο", "&Omicron;" },
213 { "ο", "&omicron;" },
214 { "Π", "&Pi;" },
215 { "π", "&pi;" },
216 { "Ρ", "&Rho;" },
217 { "ρ", "&rho;" },
218 { "Σ", "&Sigma;" },
219 { "ς", "&sigmaf;" },
220 { "σ", "&sigma;" },
221 { "Τ", "&Tau;" },
222 { "τ", "&tau;" },
223 { "Υ", "&Upsilon;" },
224 { "υ", "&upsilon;" },
225 { "Φ", "&Phi;" },
226 { "φ", "&phi;" },
227 { "Χ", "&Chi;" },
228 { "χ", "&chi;" },
229 { "Ψ", "&Psi;" },
230 { "ψ", "&psi;" },
231 { "Ω", "&Omega;" },
232 { "ω", "&omega;" },
233 { "ϑ", "&thetasym;" },
234 { "ϒ", "&upsih;" },
235 { "ϖ", "&piv;" },
237 { N_("Mathematical characters"), NULL },
238 { "∀", "&forall;" },
239 { "∂", "&part;" },
240 { "∃", "&exist;" },
241 { "∅", "&empty;" },
242 { "∇", "&nabla;" },
243 { "∈", "&isin;" },
244 { "∉", "&notin;" },
245 { "∋", "&ni;" },
246 { "∏", "&prod;" },
247 { "∑", "&sum;" },
248 { "−", "&minus;" },
249 { "∗", "&lowast;" },
250 { "√", "&radic;" },
251 { "∝", "&prop;" },
252 { "∞", "&infin;" },
253 { "∠", "&ang;" },
254 { "∧", "&and;" },
255 { "∨", "&or;" },
256 { "∩", "&cap;" },
257 { "∪", "&cup;" },
258 { "∫", "&int;" },
259 { "∴", "&there4;" },
260 { "∼", "&sim;" },
261 { "≅", "&cong;" },
262 { "≈", "&asymp;" },
263 { "≠", "&ne;" },
264 { "≡", "&equiv;" },
265 { "≤", "&le;" },
266 { "≥", "&ge;" },
267 { "⊂", "&sub;" },
268 { "⊃", "&sup;" },
269 { "⊄", "&nsub;" },
270 { "⊆", "&sube;" },
271 { "⊇", "&supe;" },
272 { "⊕", "&oplus;" },
273 { "⊗", "&otimes;" },
274 { "⊥", "&perp;" },
275 { "⋅", "&sdot;" },
276 { "◊", "&loz;" },
278 { N_("Technical characters"), NULL },
279 { "⌈", "&lceil;" },
280 { "⌉", "&rceil;" },
281 { "⌊", "&lfloor;" },
282 { "⌋", "&rfloor;" },
283 { "〈", "&lang;" },
284 { "〉", "&rang;" },
286 { N_("Arrow characters"), NULL },
287 { "←", "&larr;" },
288 { "↑", "&uarr;" },
289 { "→", "&rarr;" },
290 { "↓", "&darr;" },
291 { "↔", "&harr;" },
292 { "↵", "&crarr;" },
293 { "⇐", "&lArr;" },
294 { "⇑", "&uArr;" },
295 { "⇒", "&rArr;" },
296 { "⇓", "&dArr;" },
297 { "⇔", "&hArr;" },
299 { N_("Punctuation characters"), NULL },
300 { "–", "&ndash;" },
301 { "—", "&mdash;" },
302 { "‘", "&lsquo;" },
303 { "’", "&rsquo;" },
304 { "‚", "&sbquo;" },
305 { "“", "&ldquo;" },
306 { "”", "&rdquo;" },
307 { "„", "&bdquo;" },
308 { "†", "&dagger;" },
309 { "‡", "&Dagger;" },
310 { "…", "&hellip;" },
311 { "‰", "&permil;" },
312 { "‹", "&lsaquo;" },
313 { "›", "&rsaquo;" },
315 { N_("Miscellaneous characters"), NULL },
316 { "•", "&bull;" },
317 { "′", "&prime;" },
318 { "″", "&Prime;" },
319 { "‾", "&oline;" },
320 { "⁄", "&frasl;" },
321 { "℘", "&weierp;" },
322 { "ℑ", "&image;" },
323 { "ℜ", "&real;" },
324 { "™", "&trade;" },
325 { "€", "&euro;" },
326 { "ℵ", "&alefsym;" },
327 { "♠", "&spades;" },
328 { "♣", "&clubs;" },
329 { "♥", "&hearts;" },
330 { "♦", "&diams;" },
331 { "Œ", "&OElig;" },
332 { "œ", "&oelig;" },
333 { "Š", "&Scaron;" },
334 { "š", "&scaron;" },
335 { "Ÿ", "&Yuml;" },
336 { "ƒ", "&fnof;" },
339 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
340 SCNotification *nt, gpointer data);
343 PluginCallback plugin_callbacks[] =
345 { "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
346 { NULL, NULL, FALSE, NULL }
350 /* Functions to toggle the status of plugin */
351 static void set_status(gboolean new_status)
353 if (plugin_active != new_status)
355 GKeyFile *config = g_key_file_new();
356 gchar *data;
357 gchar *config_dir = g_path_get_dirname(config_file);
359 plugin_active = new_status;
361 /* Now we save the new status into configuration file to
362 * remember next time */
363 g_key_file_set_boolean(config, "general", "replacement_on_typing_active",
364 plugin_active);
366 if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
367 && utils_mkdir(config_dir, TRUE) != 0)
369 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
370 _("Plugin configuration directory could not be created."));
372 else
374 /* write config to file */
375 data = g_key_file_to_data(config, NULL, NULL);
376 utils_write_file(config_file, data);
377 g_free(data);
379 g_free(config_dir);
380 g_key_file_free(config);
385 static void toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
387 if (plugin_active == TRUE)
388 set_status(FALSE);
389 else
390 set_status(TRUE);
394 static void sc_on_tools_show_dialog_insert_special_chars_response
395 (GtkDialog *dialog, gint response, gpointer user_data);
396 static void sc_on_tree_row_activated
397 (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
398 static void sc_fill_store(GtkTreeStore *store);
399 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter);
402 /* Function takes over value of key which was pressed and returns
403 * HTML/SGML entity if any */
404 static const gchar *get_entity(gchar *letter)
406 guint i, len;
408 len = G_N_ELEMENTS(chars);
410 /* Ignore tags marking caracters as well as spaces*/
411 for (i = 7; i < len; i++)
413 if (utils_str_equal(chars[i][0], letter) &&
414 !utils_str_equal(" ", letter))
416 return chars[i][1];
420 /* if the char is not in the list */
421 return NULL;
425 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
426 SCNotification *nt, gpointer data)
428 gint lexer;
430 g_return_val_if_fail(editor != NULL, FALSE);
432 if (!plugin_active)
433 return FALSE;
435 lexer = sci_get_lexer(editor->sci);
436 if (lexer != SCLEX_HTML && lexer != SCLEX_XML)
437 return FALSE;
439 if (nt->nmhdr.code == SCN_CHARADDED)
441 gchar buf[7];
442 gint len;
444 len = g_unichar_to_utf8(nt->ch, buf);
445 if (len > 0)
447 const gchar *entity;
448 buf[len] = '\0';
449 entity = get_entity(buf);
451 if (entity != NULL)
453 gint pos = sci_get_current_position(editor->sci);
455 sci_set_selection_start(editor->sci, pos - len);
456 sci_set_selection_end(editor->sci, pos);
458 sci_replace_sel(editor->sci, entity);
463 return FALSE;
467 /* Called when keys were pressed */
468 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)
470 if (plugin_active == TRUE)
472 set_status(FALSE);
474 else
476 set_status(TRUE);
481 static void tools_show_dialog_insert_special_chars(void)
483 if (sc_dialog == NULL)
485 gint height;
486 GtkCellRenderer *renderer;
487 GtkTreeViewColumn *column;
488 GtkWidget *swin, *vbox, *label;
490 sc_dialog = gtk_dialog_new_with_buttons(
491 _("Special Characters"), GTK_WINDOW(geany->main_widgets->window),
492 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
493 _("_Insert"), GTK_RESPONSE_OK, NULL);
494 vbox = ui_dialog_vbox_new(GTK_DIALOG(sc_dialog));
495 gtk_box_set_spacing(GTK_BOX(vbox), 6);
496 gtk_widget_set_name(sc_dialog, "GeanyDialog");
498 height = GEANY_DEFAULT_DIALOG_HEIGHT;
499 gtk_window_set_default_size(GTK_WINDOW(sc_dialog), height * 8 / 10, height);
500 gtk_dialog_set_default_response(GTK_DIALOG(sc_dialog), GTK_RESPONSE_CANCEL);
502 label = gtk_label_new(_("Choose a special character from the list below and double click on it or use the button to insert it at the current cursor position."));
503 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
504 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
505 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
507 sc_tree = GTK_TREE_VIEW(gtk_tree_view_new());
509 sc_store = gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
510 gtk_tree_view_set_model(GTK_TREE_VIEW(sc_tree),
511 GTK_TREE_MODEL(sc_store));
512 g_object_unref(sc_store);
514 renderer = gtk_cell_renderer_text_new();
515 column = gtk_tree_view_column_new_with_attributes(
516 _("Character"), renderer, "text", COLUMN_CHARACTER, NULL);
517 gtk_tree_view_column_set_resizable(column, TRUE);
518 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
520 renderer = gtk_cell_renderer_text_new();
521 column = gtk_tree_view_column_new_with_attributes(
522 _("HTML (name)"), renderer, "text", COLUMN_HTML_NAME, NULL);
523 gtk_tree_view_column_set_resizable(column, TRUE);
524 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
526 swin = gtk_scrolled_window_new(NULL, NULL);
527 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_AUTOMATIC,
528 GTK_POLICY_AUTOMATIC);
529 gtk_scrolled_window_add_with_viewport(
530 GTK_SCROLLED_WINDOW(swin), GTK_WIDGET(sc_tree));
532 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
534 g_signal_connect(sc_tree, "row-activated", G_CALLBACK(sc_on_tree_row_activated), NULL);
536 g_signal_connect(sc_dialog, "response",
537 G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response), NULL);
539 g_signal_connect(sc_dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
541 sc_fill_store(sc_store);
543 /*gtk_tree_view_expand_all(special_characters_tree);*/
544 gtk_tree_view_set_search_column(sc_tree, COLUMN_HTML_NAME);
546 gtk_widget_show_all(sc_dialog);
550 /* fill the tree model with data
551 ** TODO move this in a file and make it extendable for more data types */
552 static void sc_fill_store(GtkTreeStore *store)
554 GtkTreeIter iter;
555 GtkTreeIter *parent_iter = NULL;
556 guint i, len;
558 len = G_N_ELEMENTS(chars);
559 for (i = 0; i < len; i++)
561 if (chars[i][1] == NULL)
562 { /* add a category */
563 gtk_tree_store_append(store, &iter, NULL);
564 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0], -1);
565 if (parent_iter != NULL) gtk_tree_iter_free(parent_iter);
566 parent_iter = gtk_tree_iter_copy(&iter);
568 else
569 { /* add child to parent_iter */
570 gtk_tree_store_append(store, &iter, parent_iter);
571 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0],
572 COLUMN_HTML_NAME, chars[i][1], -1);
578 /* just inserts the HTML_NAME coloumn of the selected row at current position
579 * returns only TRUE if a valid selection(i.e. no category) could be found */
580 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter)
582 GeanyDocument *doc = document_get_current();
583 gboolean result = FALSE;
585 if (doc != NULL)
587 gchar *str;
588 gint pos = sci_get_current_position(doc->editor->sci);
590 gtk_tree_model_get(model, iter, COLUMN_HTML_NAME, &str, -1);
591 if (NZV(str))
593 sci_insert_text(doc->editor->sci, pos, str);
594 g_free(str);
595 result = TRUE;
598 return result;
602 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog *dialog, gint response,
603 gpointer user_data)
605 if (response == GTK_RESPONSE_OK)
607 GtkTreeSelection *selection;
608 GtkTreeModel *model;
609 GtkTreeIter iter;
611 selection = gtk_tree_view_get_selection(sc_tree);
613 if (gtk_tree_selection_get_selected(selection, &model, &iter))
615 /* only hide dialog if selection was not a category */
616 if (sc_insert(model, &iter))
617 gtk_widget_hide(GTK_WIDGET(dialog));
620 else
621 gtk_widget_hide(GTK_WIDGET(dialog));
625 static void sc_on_tree_row_activated(GtkTreeView *treeview, GtkTreePath *path,
626 GtkTreeViewColumn *col, gpointer user_data)
628 GtkTreeIter iter;
629 GtkTreeModel *model = GTK_TREE_MODEL(sc_store);
631 if (gtk_tree_model_get_iter(model, &iter, path))
633 /* only hide dialog if selection was not a category */
634 if (sc_insert(model, &iter))
635 gtk_widget_hide(sc_dialog);
636 else
637 { /* double click on a category to toggle the expand or collapse it */
638 if (gtk_tree_view_row_expanded(sc_tree, path))
639 gtk_tree_view_collapse_row(sc_tree, path);
640 else
641 gtk_tree_view_expand_row(sc_tree, path, FALSE);
647 static void replace_special_character(void)
649 GeanyDocument *doc = NULL;
650 doc = document_get_current();
652 if (doc != NULL && sci_has_selection(doc->editor->sci))
654 gsize selection_len;
655 gchar *selection;
656 GString *replacement = g_string_new(NULL);
657 gsize i;
658 gchar *new;
659 const gchar *entity = NULL;
660 gchar buf[7];
661 gint len;
663 selection = sci_get_selection_contents(doc->editor->sci);
665 selection_len = strlen(selection);
666 for (i = 0; i < selection_len; i++)
668 len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
669 i = (guint)len - 1 + i;
671 buf[len] = '\0';
672 entity = get_entity(buf);
674 if (entity != NULL)
676 replacement = g_string_append(replacement, entity);
678 else
680 replacement = g_string_append(replacement, buf);
683 new = g_string_free(replacement, FALSE);
684 sci_replace_sel(doc->editor->sci, new);
685 g_free(selection);
686 g_free(new);
691 /* Callback for special chars menu */
692 static void
693 item_activate(GtkMenuItem *menuitem, gpointer gdata)
695 tools_show_dialog_insert_special_chars();
699 static void kb_activate(G_GNUC_UNUSED guint key_id)
701 item_activate(NULL, NULL);
705 /* Callback for bulk replacement of selected text */
706 static void
707 replace_special_character_activated(GtkMenuItem *menuitem, gpointer gdata)
709 replace_special_character();
713 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)
715 replace_special_character();
719 static void init_configuration(void)
721 GKeyFile *config = g_key_file_new();
723 /* loading configurations from file ...*/
724 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S,
725 "plugins", G_DIR_SEPARATOR_S,
726 "htmchars", G_DIR_SEPARATOR_S, "general.conf", NULL);
728 /* ... and initialising options from config file */
729 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
731 plugin_active = utils_get_setting_boolean(config, "general",
732 "replacement_on_typing_active", FALSE);
736 /* Called by Geany to initialise the plugin */
737 void plugin_init(GeanyData *data)
739 GtkWidget *menu_item;
740 const gchar *menu_text = _("_Insert Special HTML Characters");
742 /* First we catch the configuration and initialize them */
743 init_configuration();
745 /* Add an item to the Tools menu for html chars dialog*/
746 menu_item = gtk_menu_item_new_with_mnemonic(menu_text);
747 gtk_widget_show(menu_item);
748 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_item);
749 g_signal_connect(menu_item, "activate", G_CALLBACK(item_activate), NULL);
751 /* disable menu_item when there are no documents open */
752 ui_add_document_sensitive(menu_item);
754 /* Add menuitem for html replacement functions*/
755 main_menu = gtk_menu_item_new_with_mnemonic(_("_HTML Replacement"));
756 gtk_widget_show_all(main_menu);
757 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu);
759 main_menu_submenu = gtk_menu_new();
760 gtk_menu_item_set_submenu(GTK_MENU_ITEM(main_menu), main_menu_submenu);
762 menu_htmltoggle = gtk_check_menu_item_new_with_mnemonic(_("_Auto-replace Special Characters"));
763 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menu_htmltoggle),
764 plugin_active);
765 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_htmltoggle);
767 g_signal_connect((gpointer) menu_htmltoggle, "activate",
768 G_CALLBACK(toggle_status), NULL);
770 menu_bulk_replace = gtk_menu_item_new_with_mnemonic(
771 _("_Replace Characters in Selection"));
772 g_signal_connect((gpointer) menu_bulk_replace, "activate",
773 G_CALLBACK(replace_special_character_activated), NULL);
775 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_bulk_replace);
777 ui_add_document_sensitive(main_menu);
778 gtk_widget_show(menu_bulk_replace);
779 gtk_widget_show(menu_htmltoggle);
781 main_menu_item = menu_item;
783 /* setup keybindings */
784 keybindings_set_item(plugin_key_group, KB_INSERT_HTML_CHARS,
785 kb_activate, 0, 0, "insert_html_chars",
786 _("Insert Special HTML Characters"), menu_item);
787 keybindings_set_item(plugin_key_group, KB_REPLACE_HTML_ENTITIES,
788 kb_special_chars_replacement, 0, 0, "replace_special_characters",
789 _("Replace special characters"), NULL);
790 keybindings_set_item(plugin_key_group, KB_HTMLTOGGLE_ACTIVE,
791 kbhtmltoggle_toggle, 0, 0, "htmltoogle_toggle_plugin_status",
792 _("Toggle plugin status"), menu_htmltoggle);
796 /* Destroy widgets */
797 void plugin_cleanup(void)
799 gtk_widget_destroy(main_menu_item);
800 gtk_widget_destroy(main_menu);
802 if (sc_dialog != NULL)
803 gtk_widget_destroy(sc_dialog);
805 if (config_file != NULL)
806 g_free(config_file);