Only automatically replace characters when the current document is
[geany-mirror.git] / plugins / htmlchars.c
blob18751dc2e17f0397f871a0d76b49e76e6bde1035
1 /*
2 * htmlchars.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2010 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6 * Copyright 2007-2010 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.
23 * $Id$
26 /* HTML Characters plugin (Inserts HTML character entities like '&amp;') */
28 #include "geanyplugin.h"
29 #include <string.h>
30 #include "SciLexer.h"
33 GeanyData *geany_data;
34 GeanyFunctions *geany_functions;
37 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
39 PLUGIN_SET_INFO(_("HTML Characters"), _("Inserts HTML character entities like '&amp;'."), VERSION,
40 _("The Geany developer team"))
43 /* Keybinding(s) */
44 enum
46 KB_INSERT_HTML_CHARS,
47 KB_REPLACE_HTML_ENTITIES,
48 KB_HTMLTOGGLE_ACTIVE,
49 KB_COUNT
52 PLUGIN_KEY_GROUP(html_chars, KB_COUNT)
55 enum
57 COLUMN_CHARACTER,
58 COLUMN_HTML_NAME,
59 N_COLUMNS
62 static GtkWidget *main_menu_item = NULL;
63 static GtkWidget *main_menu = NULL;
64 static GtkWidget *main_menu_submenu = NULL;
65 static GtkWidget *menu_bulk_replace = NULL;
66 static GtkWidget *sc_dialog = NULL;
67 static GtkTreeStore *sc_store = NULL;
68 static GtkTreeView *sc_tree = NULL;
69 static GtkWidget *menu_htmltoggle = NULL;
70 static gboolean plugin_active = FALSE;
72 /* Configuration file */
73 static gchar *config_file = NULL;
75 const gchar *chars[][2] ={
76 { N_("HTML characters"), NULL },
77 { "\"", "&quot;" },
78 { "&", "&amp;" },
79 { "<", "&lt;" },
80 { ">", "&gt;" },
82 { N_("ISO 8859-1 characters"), NULL },
83 { " ", "&nbsp;" },
84 { "¡", "&iexcl;" },
85 { "¢", "&cent;" },
86 { "£", "&pound;" },
87 { "¤", "&curren;" },
88 { "¥", "&yen;" },
89 { "¦", "&brvbar;" },
90 { "§", "&sect;" },
91 { "¨", "&uml;" },
92 { "©", "&copy;" },
93 { "®", "&reg;" },
94 { "«", "&laquo;" },
95 { "»", "&raquo;" },
96 { "¬", "&not;" },
97 { " ", "&shy;" },
98 { "¯", "&macr;" },
99 { "°", "&deg;" },
100 { "±", "&plusmn;" },
101 { "¹", "&sup1;" },
102 { "²", "&sup2;" },
103 { "³", "&sup3;" },
104 { "¼", "&frac14;" },
105 { "½", "&frac12;" },
106 { "¾", "&frac34;" },
107 { "×", "&times;" },
108 { "÷", "&divide;" },
109 { "´", "&acute;" },
110 { "µ", "&micro;" },
111 { "¶", "&para;" },
112 { "·", "&middot;" },
113 { "¸", "&cedil;" },
114 { "ª", "&ordf;" },
115 { "º", "&ordm;" },
116 { "¿", "&iquest;" },
117 { "À", "&Agrave;" },
118 { "Á", "&Aacute;" },
119 { "Â", "&Acirc;" },
120 { "Ã", "&Atilde;" },
121 { "Ä", "&Auml;" },
122 { "Å", "&Aring;" },
123 { "Æ", "&AElig;" },
124 { "Ç", "&Ccedil;" },
125 { "È", "&Egrave;" },
126 { "É", "&Eacute;" },
127 { "Ê", "&Ecirc;" },
128 { "Ë", "&Euml;" },
129 { "Ì", "&Igrave;" },
130 { "Í", "&Iacute;" },
131 { "Î", "&Icirc;" },
132 { "Ï", "&Iuml;" },
133 { "Ð", "&ETH;" },
134 { "Ñ", "&Ntilde;" },
135 { "Ò", "&Ograve;" },
136 { "Ó", "&Oacute;" },
137 { "Ô", "&Ocirc;" },
138 { "Õ", "&Otilde;" },
139 { "Ö", "&Ouml;" },
140 { "Ø", "&Oslash;" },
141 { "Ù", "&Ugrave;" },
142 { "Ú", "&Uacute;" },
143 { "Û", "&Ucirc;" },
144 { "Ü", "&Uuml;" },
145 { "Ý", "&Yacute;" },
146 { "Þ", "&THORN;" },
147 { "ß", "&szlig;" },
148 { "à", "&agrave;" },
149 { "á", "&aacute;" },
150 { "â", "&acirc;" },
151 { "ã", "&atilde;" },
152 { "ä", "&auml;" },
153 { "å", "&aring;" },
154 { "æ", "&aelig;" },
155 { "ç", "&ccedil;" },
156 { "è", "&egrave;" },
157 { "é", "&eacute;" },
158 { "ê", "&ecirc;" },
159 { "ë", "&euml;" },
160 { "ì", "&igrave;" },
161 { "í", "&iacute;" },
162 { "î", "&icirc;" },
163 { "ï", "&iuml;" },
164 { "ð", "&eth;" },
165 { "ñ", "&ntilde;" },
166 { "ò", "&ograve;" },
167 { "ó", "&oacute;" },
168 { "ô", "&ocirc;" },
169 { "õ", "&otilde;" },
170 { "ö", "&ouml;" },
171 { "ø", "&oslash;" },
172 { "ù", "&ugrave;" },
173 { "ú", "&uacute;" },
174 { "û", "&ucirc;" },
175 { "ü", "&uuml;" },
176 { "ý", "&yacute;" },
177 { "þ", "&thorn;" },
178 { "ÿ", "&yuml;" },
180 { N_("Greek characters"), NULL },
181 { "Α", "&Alpha;" },
182 { "α", "&alpha;" },
183 { "Β", "&Beta;" },
184 { "β", "&beta;" },
185 { "Γ", "&Gamma;" },
186 { "γ", "&gamma;" },
187 { "Δ", "&Delta;" },
188 { "δ", "&Delta;" },
189 { "δ", "&delta;" },
190 { "Ε", "&Epsilon;" },
191 { "ε", "&epsilon;" },
192 { "Ζ", "&Zeta;" },
193 { "ζ", "&zeta;" },
194 { "Η", "&Eta;" },
195 { "η", "&eta;" },
196 { "Θ", "&Theta;" },
197 { "θ", "&theta;" },
198 { "Ι", "&Iota;" },
199 { "ι", "&iota;" },
200 { "Κ", "&Kappa;" },
201 { "κ", "&kappa;" },
202 { "Λ", "&Lambda;" },
203 { "λ", "&lambda;" },
204 { "Μ", "&Mu;" },
205 { "μ", "&mu;" },
206 { "Ν", "&Nu;" },
207 { "ν", "&nu;" },
208 { "Ξ", "&Xi;" },
209 { "ξ", "&xi;" },
210 { "Ο", "&Omicron;" },
211 { "ο", "&omicron;" },
212 { "Π", "&Pi;" },
213 { "π", "&pi;" },
214 { "Ρ", "&Rho;" },
215 { "ρ", "&rho;" },
216 { "Σ", "&Sigma;" },
217 { "ς", "&sigmaf;" },
218 { "σ", "&sigma;" },
219 { "Τ", "&Tau;" },
220 { "τ", "&tau;" },
221 { "Υ", "&Upsilon;" },
222 { "υ", "&upsilon;" },
223 { "Φ", "&Phi;" },
224 { "φ", "&phi;" },
225 { "Χ", "&Chi;" },
226 { "χ", "&chi;" },
227 { "Ψ", "&Psi;" },
228 { "ψ", "&psi;" },
229 { "Ω", "&Omega;" },
230 { "ω", "&omega;" },
231 { "ϑ", "&thetasym;" },
232 { "ϒ", "&upsih;" },
233 { "ϖ", "&piv;" },
235 { N_("Mathematical characters"), NULL },
236 { "∀", "&forall;" },
237 { "∂", "&part;" },
238 { "∃", "&exist;" },
239 { "∅", "&empty;" },
240 { "∇", "&nabla;" },
241 { "∈", "&isin;" },
242 { "∉", "&notin;" },
243 { "∋", "&ni;" },
244 { "∏", "&prod;" },
245 { "∑", "&sum;" },
246 { "−", "&minus;" },
247 { "∗", "&lowast;" },
248 { "√", "&radic;" },
249 { "∝", "&prop;" },
250 { "∞", "&infin;" },
251 { "∠", "&ang;" },
252 { "∧", "&and;" },
253 { "∨", "&or;" },
254 { "∩", "&cap;" },
255 { "∪", "&cup;" },
256 { "∫", "&int;" },
257 { "∴", "&there4;" },
258 { "∼", "&sim;" },
259 { "≅", "&cong;" },
260 { "≈", "&asymp;" },
261 { "≠", "&ne;" },
262 { "≡", "&equiv;" },
263 { "≤", "&le;" },
264 { "≥", "&ge;" },
265 { "⊂", "&sub;" },
266 { "⊃", "&sup;" },
267 { "⊄", "&nsub;" },
268 { "⊆", "&sube;" },
269 { "⊇", "&supe;" },
270 { "⊕", "&oplus;" },
271 { "⊗", "&otimes;" },
272 { "⊥", "&perp;" },
273 { "⋅", "&sdot;" },
274 { "◊", "&loz;" },
276 { N_("Technical characters"), NULL },
277 { "⌈", "&lceil;" },
278 { "⌉", "&rceil;" },
279 { "⌊", "&lfloor;" },
280 { "⌋", "&rfloor;" },
281 { "〈", "&lang;" },
282 { "〉", "&rang;" },
284 { N_("Arrow characters"), NULL },
285 { "←", "&larr;" },
286 { "↑", "&uarr;" },
287 { "→", "&rarr;" },
288 { "↓", "&darr;" },
289 { "↔", "&harr;" },
290 { "↵", "&crarr;" },
291 { "⇐", "&lArr;" },
292 { "⇑", "&uArr;" },
293 { "⇒", "&rArr;" },
294 { "⇓", "&dArr;" },
295 { "⇔", "&hArr;" },
297 { N_("Punctuation characters"), NULL },
298 { "–", "&ndash;" },
299 { "—", "&mdash;" },
300 { "‘", "&lsquo;" },
301 { "’", "&rsquo;" },
302 { "‚", "&sbquo;" },
303 { "“", "&ldquo;" },
304 { "”", "&rdquo;" },
305 { "„", "&bdquo;" },
306 { "†", "&dagger;" },
307 { "‡", "&Dagger;" },
308 { "…", "&hellip;" },
309 { "‰", "&permil;" },
310 { "‹", "&lsaquo;" },
311 { "›", "&rsaquo;" },
313 { N_("Miscellaneous characters"), NULL },
314 { "•", "&bull;" },
315 { "′", "&prime;" },
316 { "″", "&Prime;" },
317 { "‾", "&oline;" },
318 { "⁄", "&frasl;" },
319 { "℘", "&weierp;" },
320 { "ℑ", "&image;" },
321 { "ℜ", "&real;" },
322 { "™", "&trade;" },
323 { "€", "&euro;" },
324 { "ℵ", "&alefsym;" },
325 { "♠", "&spades;" },
326 { "♣", "&clubs;" },
327 { "♥", "&hearts;" },
328 { "♦", "&diams;" },
329 { "Œ", "&OElig;" },
330 { "œ", "&oelig;" },
331 { "Š", "&Scaron;" },
332 { "š", "&scaron;" },
333 { "Ÿ", "&Yuml;" },
334 { "ƒ", "&fnof;" },
337 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
338 SCNotification *nt, gpointer data);
341 PluginCallback plugin_callbacks[] =
343 { "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
344 { NULL, NULL, FALSE, NULL }
348 /* Functions to toggle the status of plugin */
349 static void set_status(gboolean new_status)
351 if (plugin_active != new_status)
353 GKeyFile *config = g_key_file_new();
354 gchar *data;
355 gchar *config_dir = g_path_get_dirname(config_file);
357 plugin_active = new_status;
359 /* Now we save the new status into configuration file to
360 * remember next time */
361 g_key_file_set_boolean(config, "general", "replacement_on_typing_active",
362 plugin_active);
364 if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
365 && utils_mkdir(config_dir, TRUE) != 0)
367 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
368 _("Plugin configuration directory could not be created."));
370 else
372 /* write config to file */
373 data = g_key_file_to_data(config, NULL, NULL);
374 utils_write_file(config_file, data);
375 g_free(data);
377 g_free(config_dir);
378 g_key_file_free(config);
383 static void toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
385 if (plugin_active == TRUE)
386 set_status(FALSE);
387 else
388 set_status(TRUE);
392 static void sc_on_tools_show_dialog_insert_special_chars_response
393 (GtkDialog *dialog, gint response, gpointer user_data);
394 static void sc_on_tree_row_activated
395 (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
396 static void sc_fill_store(GtkTreeStore *store);
397 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter);
400 /* Function takes over value of key which was pressed and returns
401 * HTML/SGML entity if any */
402 static const gchar *get_entity(gchar *letter)
404 guint i, len;
406 len = G_N_ELEMENTS(chars);
408 /* Ignore tags marking caracters as well as spaces*/
409 for (i = 7; i < len; i++)
411 if (utils_str_equal(chars[i][0], letter) &&
412 !utils_str_equal(" ", letter))
414 return chars[i][1];
418 /* if the char is not in the list */
419 return NULL;
423 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
424 SCNotification *nt, gpointer data)
426 gint lexer;
428 g_return_val_if_fail(editor != NULL, FALSE);
430 if (!plugin_active)
431 return FALSE;
433 lexer = sci_get_lexer(editor->sci);
434 if (lexer != SCLEX_HTML && lexer != SCLEX_XML)
435 return FALSE;
437 if (nt->nmhdr.code == SCN_CHARADDED)
439 gchar buf[7];
440 gint len;
442 len = g_unichar_to_utf8(nt->ch, buf);
443 if (len > 0)
445 const gchar *entity;
446 buf[len] = '\0';
447 entity = get_entity(buf);
449 if (entity != NULL)
451 gint pos = sci_get_current_position(editor->sci);
453 sci_set_selection_start(editor->sci, pos - len);
454 sci_set_selection_end(editor->sci, pos);
456 sci_replace_sel(editor->sci, entity);
461 return FALSE;
465 /* Called when keys were pressed */
466 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)
468 if (plugin_active == TRUE)
470 set_status(FALSE);
472 else
474 set_status(TRUE);
479 static void tools_show_dialog_insert_special_chars(void)
481 if (sc_dialog == NULL)
483 gint height;
484 GtkCellRenderer *renderer;
485 GtkTreeViewColumn *column;
486 GtkWidget *swin, *vbox, *label;
488 sc_dialog = gtk_dialog_new_with_buttons(
489 _("Special Characters"), GTK_WINDOW(geany->main_widgets->window),
490 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
491 _("_Insert"), GTK_RESPONSE_OK, NULL);
492 vbox = ui_dialog_vbox_new(GTK_DIALOG(sc_dialog));
493 gtk_box_set_spacing(GTK_BOX(vbox), 6);
494 gtk_widget_set_name(sc_dialog, "GeanyDialog");
496 height = GEANY_DEFAULT_DIALOG_HEIGHT;
497 gtk_window_set_default_size(GTK_WINDOW(sc_dialog), height * 8 / 10, height);
498 gtk_dialog_set_default_response(GTK_DIALOG(sc_dialog), GTK_RESPONSE_CANCEL);
500 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."));
501 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
502 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
503 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
505 sc_tree = GTK_TREE_VIEW(gtk_tree_view_new());
507 sc_store = gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
508 gtk_tree_view_set_model(GTK_TREE_VIEW(sc_tree),
509 GTK_TREE_MODEL(sc_store));
510 g_object_unref(sc_store);
512 renderer = gtk_cell_renderer_text_new();
513 column = gtk_tree_view_column_new_with_attributes(
514 _("Character"), renderer, "text", COLUMN_CHARACTER, NULL);
515 gtk_tree_view_column_set_resizable(column, TRUE);
516 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
518 renderer = gtk_cell_renderer_text_new();
519 column = gtk_tree_view_column_new_with_attributes(
520 _("HTML (name)"), renderer, "text", COLUMN_HTML_NAME, NULL);
521 gtk_tree_view_column_set_resizable(column, TRUE);
522 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
524 swin = gtk_scrolled_window_new(NULL, NULL);
525 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_AUTOMATIC,
526 GTK_POLICY_AUTOMATIC);
527 gtk_scrolled_window_add_with_viewport(
528 GTK_SCROLLED_WINDOW(swin), GTK_WIDGET(sc_tree));
530 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
532 g_signal_connect(sc_tree, "row-activated", G_CALLBACK(sc_on_tree_row_activated), NULL);
534 g_signal_connect(sc_dialog, "response",
535 G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response), NULL);
537 g_signal_connect(sc_dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
539 sc_fill_store(sc_store);
541 /*gtk_tree_view_expand_all(special_characters_tree);*/
542 gtk_tree_view_set_search_column(sc_tree, COLUMN_HTML_NAME);
544 gtk_widget_show_all(sc_dialog);
548 /* fill the tree model with data
549 ** TODO move this in a file and make it extendable for more data types */
550 static void sc_fill_store(GtkTreeStore *store)
552 GtkTreeIter iter;
553 GtkTreeIter *parent_iter = NULL;
554 guint i, len;
556 len = G_N_ELEMENTS(chars);
557 for (i = 0; i < len; i++)
559 if (chars[i][1] == NULL)
560 { /* add a category */
561 gtk_tree_store_append(store, &iter, NULL);
562 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0], -1);
563 if (parent_iter != NULL) gtk_tree_iter_free(parent_iter);
564 parent_iter = gtk_tree_iter_copy(&iter);
566 else
567 { /* add child to parent_iter */
568 gtk_tree_store_append(store, &iter, parent_iter);
569 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0],
570 COLUMN_HTML_NAME, chars[i][1], -1);
576 /* just inserts the HTML_NAME coloumn of the selected row at current position
577 * returns only TRUE if a valid selection(i.e. no category) could be found */
578 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter)
580 GeanyDocument *doc = document_get_current();
581 gboolean result = FALSE;
583 if (doc != NULL)
585 gchar *str;
586 gint pos = sci_get_current_position(doc->editor->sci);
588 gtk_tree_model_get(model, iter, COLUMN_HTML_NAME, &str, -1);
589 if (NZV(str))
591 sci_insert_text(doc->editor->sci, pos, str);
592 g_free(str);
593 result = TRUE;
596 return result;
600 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog *dialog, gint response,
601 gpointer user_data)
603 if (response == GTK_RESPONSE_OK)
605 GtkTreeSelection *selection;
606 GtkTreeModel *model;
607 GtkTreeIter iter;
609 selection = gtk_tree_view_get_selection(sc_tree);
611 if (gtk_tree_selection_get_selected(selection, &model, &iter))
613 /* only hide dialog if selection was not a category */
614 if (sc_insert(model, &iter))
615 gtk_widget_hide(GTK_WIDGET(dialog));
618 else
619 gtk_widget_hide(GTK_WIDGET(dialog));
623 static void sc_on_tree_row_activated(GtkTreeView *treeview, GtkTreePath *path,
624 GtkTreeViewColumn *col, gpointer user_data)
626 GtkTreeIter iter;
627 GtkTreeModel *model = GTK_TREE_MODEL(sc_store);
629 if (gtk_tree_model_get_iter(model, &iter, path))
631 /* only hide dialog if selection was not a category */
632 if (sc_insert(model, &iter))
633 gtk_widget_hide(sc_dialog);
634 else
635 { /* double click on a category to toggle the expand or collapse it */
636 if (gtk_tree_view_row_expanded(sc_tree, path))
637 gtk_tree_view_collapse_row(sc_tree, path);
638 else
639 gtk_tree_view_expand_row(sc_tree, path, FALSE);
645 static void replace_special_character(void)
647 GeanyDocument *doc = NULL;
648 doc = document_get_current();
650 if (doc != NULL && sci_has_selection(doc->editor->sci))
652 guint selection_len;
653 gchar *selection;
654 GString *replacement = g_string_new(NULL);
655 guint i;
656 gchar *new;
657 const gchar *entity = NULL;
658 gchar buf[7];
659 gint len;
661 selection = sci_get_selection_contents(doc->editor->sci);
663 selection_len = strlen(selection);
664 for (i = 0; i < selection_len; i++)
666 len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
667 i = len - 1 + i;
669 buf[len] = '\0';
670 entity = get_entity(buf);
672 if (entity != NULL)
674 replacement = g_string_append(replacement, entity);
676 else
678 replacement = g_string_append(replacement, buf);
681 new = g_string_free(replacement, FALSE);
682 sci_replace_sel(doc->editor->sci, new);
683 g_free(selection);
684 g_free(new);
689 /* Callback for special chars menu */
690 static void
691 item_activate(GtkMenuItem *menuitem, gpointer gdata)
693 tools_show_dialog_insert_special_chars();
697 static void kb_activate(G_GNUC_UNUSED guint key_id)
699 item_activate(NULL, NULL);
703 /* Callback for bulk replacement of selected text */
704 static void
705 replace_special_character_activated(GtkMenuItem *menuitem, gpointer gdata)
707 replace_special_character();
711 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)
713 replace_special_character();
717 static void init_configuration(void)
719 GKeyFile *config = g_key_file_new();
721 /* loading configurations from file ...*/
722 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S,
723 "plugins", G_DIR_SEPARATOR_S,
724 "htmchars", G_DIR_SEPARATOR_S, "general.conf", NULL);
726 /* ... and initialising options from config file */
727 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
729 plugin_active = utils_get_setting_boolean(config, "general",
730 "replacement_on_typing_active", FALSE);
734 /* Called by Geany to initialise the plugin */
735 void plugin_init(GeanyData *data)
737 GtkWidget *menu_item;
738 const gchar *menu_text = _("_Insert Special HTML Characters");
740 /* First we catch the configuration and initialize them */
741 init_configuration();
743 /* Add an item to the Tools menu for html chars dialog*/
744 menu_item = gtk_menu_item_new_with_mnemonic(menu_text);
745 gtk_widget_show(menu_item);
746 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_item);
747 g_signal_connect(menu_item, "activate", G_CALLBACK(item_activate), NULL);
749 /* disable menu_item when there are no documents open */
750 ui_add_document_sensitive(menu_item);
752 /* Add menuitem for html replacement functions*/
753 main_menu = gtk_menu_item_new_with_mnemonic(_("HTML Replacement"));
754 gtk_widget_show_all(main_menu);
755 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu);
757 main_menu_submenu = gtk_menu_new();
758 gtk_menu_item_set_submenu(GTK_MENU_ITEM(main_menu), main_menu_submenu);
760 menu_htmltoggle = gtk_check_menu_item_new_with_mnemonic(_("_HTMLToggle"));
761 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menu_htmltoggle),
762 plugin_active);
763 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_htmltoggle);
765 g_signal_connect((gpointer) menu_htmltoggle, "activate",
766 G_CALLBACK(toggle_status), NULL);
768 menu_bulk_replace = gtk_menu_item_new_with_mnemonic(
769 _("Bulk replacement of special chars"));
770 g_signal_connect((gpointer) menu_bulk_replace, "activate",
771 G_CALLBACK(replace_special_character_activated), NULL);
773 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_bulk_replace);
775 ui_add_document_sensitive(main_menu);
776 gtk_widget_show(menu_bulk_replace);
777 gtk_widget_show(menu_htmltoggle);
779 main_menu_item = menu_item;
781 /* setup keybindings */
782 keybindings_set_item(plugin_key_group, KB_INSERT_HTML_CHARS,
783 kb_activate, 0, 0, "insert_html_chars",
784 _("Insert Special HTML Characters"), menu_item);
785 keybindings_set_item(plugin_key_group, KB_REPLACE_HTML_ENTITIES,
786 kb_special_chars_replacement, 0, 0, "replace_special_characters",
787 _("Replace special characters"), NULL);
788 keybindings_set_item(plugin_key_group, KB_HTMLTOGGLE_ACTIVE,
789 kbhtmltoggle_toggle, 0, 0, "htmltoogle_toggle_plugin_status",
790 _("Toggle plugin status"), menu_htmltoggle);
794 /* Destroy widgets */
795 void plugin_cleanup(void)
797 gtk_widget_destroy(main_menu_item);
798 gtk_widget_destroy(main_menu);
800 if (sc_dialog != NULL)
801 gtk_widget_destroy(sc_dialog);
803 if (config_file != NULL)
804 g_free(config_file);