Merge pull request #3977 from techee/filetype_groups
[geany-mirror.git] / plugins / htmlchars.c
blob06e8513ee50efd05ab91af41eba6b808ade61d79
1 /*
2 * htmlchars.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 The Geany contributors
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.
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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /* HTML Characters plugin (Inserts HTML character entities like '&') */
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
27 #include "geanyplugin.h"
28 #include <string.h>
29 #include "SciLexer.h"
32 GeanyPlugin *geany_plugin;
33 GeanyData *geany_data;
36 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
38 PLUGIN_SET_INFO(_("HTML Characters"), _("Inserts HTML character entities like '&amp;'."),
39 PACKAGE_VERSION, _("The Geany developer team"))
42 /* Keybinding(s) */
43 enum
45 KB_INSERT_HTML_CHARS,
46 KB_REPLACE_HTML_ENTITIES,
47 KB_HTMLTOGGLE_ACTIVE,
48 KB_COUNT
52 enum
54 COLUMN_CHARACTER,
55 COLUMN_HTML_NAME,
56 N_COLUMNS
59 static GtkWidget *main_menu_item = NULL;
60 static GtkWidget *main_menu = NULL;
61 static GtkWidget *main_menu_submenu = NULL;
62 static GtkWidget *menu_bulk_replace = NULL;
63 static GtkWidget *sc_dialog = NULL;
64 static GtkTreeStore *sc_store = NULL;
65 static GtkTreeView *sc_tree = NULL;
66 static GtkWidget *menu_htmltoggle = NULL;
67 static gboolean plugin_active = FALSE;
69 /* Configuration file */
70 static gchar *config_file = NULL;
72 const gchar *chars[][2] ={
73 { N_("HTML characters"), NULL },
74 { "\"", "&quot;" },
75 { "&", "&amp;" },
76 { "<", "&lt;" },
77 { ">", "&gt;" },
79 { N_("ISO 8859-1 characters"), NULL },
80 { " ", "&nbsp;" },
81 { "¡", "&iexcl;" },
82 { "¢", "&cent;" },
83 { "£", "&pound;" },
84 { "¤", "&curren;" },
85 { "¥", "&yen;" },
86 { "¦", "&brvbar;" },
87 { "§", "&sect;" },
88 { "¨", "&uml;" },
89 { "©", "&copy;" },
90 { "®", "&reg;" },
91 { "«", "&laquo;" },
92 { "»", "&raquo;" },
93 { "¬", "&not;" },
94 { " ", "&shy;" },
95 { "¯", "&macr;" },
96 { "°", "&deg;" },
97 { "±", "&plusmn;" },
98 { "¹", "&sup1;" },
99 { "²", "&sup2;" },
100 { "³", "&sup3;" },
101 { "¼", "&frac14;" },
102 { "½", "&frac12;" },
103 { "¾", "&frac34;" },
104 { "×", "&times;" },
105 { "÷", "&divide;" },
106 { "´", "&acute;" },
107 { "µ", "&micro;" },
108 { "¶", "&para;" },
109 { "·", "&middot;" },
110 { "¸", "&cedil;" },
111 { "ª", "&ordf;" },
112 { "º", "&ordm;" },
113 { "¿", "&iquest;" },
114 { "À", "&Agrave;" },
115 { "Á", "&Aacute;" },
116 { "Â", "&Acirc;" },
117 { "Ã", "&Atilde;" },
118 { "Ä", "&Auml;" },
119 { "Å", "&Aring;" },
120 { "Æ", "&AElig;" },
121 { "Ç", "&Ccedil;" },
122 { "È", "&Egrave;" },
123 { "É", "&Eacute;" },
124 { "Ê", "&Ecirc;" },
125 { "Ë", "&Euml;" },
126 { "Ì", "&Igrave;" },
127 { "Í", "&Iacute;" },
128 { "Î", "&Icirc;" },
129 { "Ï", "&Iuml;" },
130 { "Ð", "&ETH;" },
131 { "Ñ", "&Ntilde;" },
132 { "Ò", "&Ograve;" },
133 { "Ó", "&Oacute;" },
134 { "Ô", "&Ocirc;" },
135 { "Õ", "&Otilde;" },
136 { "Ö", "&Ouml;" },
137 { "Ø", "&Oslash;" },
138 { "Ù", "&Ugrave;" },
139 { "Ú", "&Uacute;" },
140 { "Û", "&Ucirc;" },
141 { "Ü", "&Uuml;" },
142 { "Ý", "&Yacute;" },
143 { "Þ", "&THORN;" },
144 { "ß", "&szlig;" },
145 { "à", "&agrave;" },
146 { "á", "&aacute;" },
147 { "â", "&acirc;" },
148 { "ã", "&atilde;" },
149 { "ä", "&auml;" },
150 { "å", "&aring;" },
151 { "æ", "&aelig;" },
152 { "ç", "&ccedil;" },
153 { "è", "&egrave;" },
154 { "é", "&eacute;" },
155 { "ê", "&ecirc;" },
156 { "ë", "&euml;" },
157 { "ì", "&igrave;" },
158 { "í", "&iacute;" },
159 { "î", "&icirc;" },
160 { "ï", "&iuml;" },
161 { "ð", "&eth;" },
162 { "ñ", "&ntilde;" },
163 { "ò", "&ograve;" },
164 { "ó", "&oacute;" },
165 { "ô", "&ocirc;" },
166 { "õ", "&otilde;" },
167 { "ö", "&ouml;" },
168 { "ø", "&oslash;" },
169 { "ù", "&ugrave;" },
170 { "ú", "&uacute;" },
171 { "û", "&ucirc;" },
172 { "ü", "&uuml;" },
173 { "ý", "&yacute;" },
174 { "þ", "&thorn;" },
175 { "ÿ", "&yuml;" },
177 { N_("Greek characters"), NULL },
178 { "Α", "&Alpha;" },
179 { "α", "&alpha;" },
180 { "Β", "&Beta;" },
181 { "β", "&beta;" },
182 { "Γ", "&Gamma;" },
183 { "γ", "&gamma;" },
184 { "Δ", "&Delta;" },
185 { "δ", "&Delta;" },
186 { "δ", "&delta;" },
187 { "Ε", "&Epsilon;" },
188 { "ε", "&epsilon;" },
189 { "Ζ", "&Zeta;" },
190 { "ζ", "&zeta;" },
191 { "Η", "&Eta;" },
192 { "η", "&eta;" },
193 { "Θ", "&Theta;" },
194 { "θ", "&theta;" },
195 { "Ι", "&Iota;" },
196 { "ι", "&iota;" },
197 { "Κ", "&Kappa;" },
198 { "κ", "&kappa;" },
199 { "Λ", "&Lambda;" },
200 { "λ", "&lambda;" },
201 { "Μ", "&Mu;" },
202 { "μ", "&mu;" },
203 { "Ν", "&Nu;" },
204 { "ν", "&nu;" },
205 { "Ξ", "&Xi;" },
206 { "ξ", "&xi;" },
207 { "Ο", "&Omicron;" },
208 { "ο", "&omicron;" },
209 { "Π", "&Pi;" },
210 { "π", "&pi;" },
211 { "Ρ", "&Rho;" },
212 { "ρ", "&rho;" },
213 { "Σ", "&Sigma;" },
214 { "ς", "&sigmaf;" },
215 { "σ", "&sigma;" },
216 { "Τ", "&Tau;" },
217 { "τ", "&tau;" },
218 { "Υ", "&Upsilon;" },
219 { "υ", "&upsilon;" },
220 { "Φ", "&Phi;" },
221 { "φ", "&phi;" },
222 { "Χ", "&Chi;" },
223 { "χ", "&chi;" },
224 { "Ψ", "&Psi;" },
225 { "ψ", "&psi;" },
226 { "Ω", "&Omega;" },
227 { "ω", "&omega;" },
228 { "ϑ", "&thetasym;" },
229 { "ϒ", "&upsih;" },
230 { "ϖ", "&piv;" },
232 { N_("Mathematical characters"), NULL },
233 { "∀", "&forall;" },
234 { "∂", "&part;" },
235 { "∃", "&exist;" },
236 { "∅", "&empty;" },
237 { "∇", "&nabla;" },
238 { "∈", "&isin;" },
239 { "∉", "&notin;" },
240 { "∋", "&ni;" },
241 { "∏", "&prod;" },
242 { "∑", "&sum;" },
243 { "−", "&minus;" },
244 { "∗", "&lowast;" },
245 { "√", "&radic;" },
246 { "∝", "&prop;" },
247 { "∞", "&infin;" },
248 { "∠", "&ang;" },
249 { "∧", "&and;" },
250 { "∨", "&or;" },
251 { "∩", "&cap;" },
252 { "∪", "&cup;" },
253 { "∫", "&int;" },
254 { "∴", "&there4;" },
255 { "∼", "&sim;" },
256 { "≅", "&cong;" },
257 { "≈", "&asymp;" },
258 { "≠", "&ne;" },
259 { "≡", "&equiv;" },
260 { "≤", "&le;" },
261 { "≥", "&ge;" },
262 { "⊂", "&sub;" },
263 { "⊃", "&sup;" },
264 { "⊄", "&nsub;" },
265 { "⊆", "&sube;" },
266 { "⊇", "&supe;" },
267 { "⊕", "&oplus;" },
268 { "⊗", "&otimes;" },
269 { "⊥", "&perp;" },
270 { "⋅", "&sdot;" },
271 { "◊", "&loz;" },
273 { N_("Technical characters"), NULL },
274 { "⌈", "&lceil;" },
275 { "⌉", "&rceil;" },
276 { "⌊", "&lfloor;" },
277 { "⌋", "&rfloor;" },
278 { "〈", "&lang;" },
279 { "〉", "&rang;" },
281 { N_("Arrow characters"), NULL },
282 { "←", "&larr;" },
283 { "↑", "&uarr;" },
284 { "→", "&rarr;" },
285 { "↓", "&darr;" },
286 { "↔", "&harr;" },
287 { "↵", "&crarr;" },
288 { "⇐", "&lArr;" },
289 { "⇑", "&uArr;" },
290 { "⇒", "&rArr;" },
291 { "⇓", "&dArr;" },
292 { "⇔", "&hArr;" },
294 { N_("Punctuation characters"), NULL },
295 { "–", "&ndash;" },
296 { "—", "&mdash;" },
297 { "‘", "&lsquo;" },
298 { "’", "&rsquo;" },
299 { "‚", "&sbquo;" },
300 { "“", "&ldquo;" },
301 { "”", "&rdquo;" },
302 { "„", "&bdquo;" },
303 { "†", "&dagger;" },
304 { "‡", "&Dagger;" },
305 { "…", "&hellip;" },
306 { "‰", "&permil;" },
307 { "‹", "&lsaquo;" },
308 { "›", "&rsaquo;" },
310 { N_("Miscellaneous characters"), NULL },
311 { "•", "&bull;" },
312 { "′", "&prime;" },
313 { "″", "&Prime;" },
314 { "‾", "&oline;" },
315 { "⁄", "&frasl;" },
316 { "℘", "&weierp;" },
317 { "ℑ", "&image;" },
318 { "ℜ", "&real;" },
319 { "™", "&trade;" },
320 { "€", "&euro;" },
321 { "ℵ", "&alefsym;" },
322 { "♠", "&spades;" },
323 { "♣", "&clubs;" },
324 { "♥", "&hearts;" },
325 { "♦", "&diams;" },
326 { "Œ", "&OElig;" },
327 { "œ", "&oelig;" },
328 { "Š", "&Scaron;" },
329 { "š", "&scaron;" },
330 { "Ÿ", "&Yuml;" },
331 { "ƒ", "&fnof;" },
334 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
335 SCNotification *nt, gpointer data);
338 PluginCallback plugin_callbacks[] =
340 { "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
341 { NULL, NULL, FALSE, NULL }
345 /* Functions to toggle the status of plugin */
346 static void set_status(gboolean new_status)
348 if (plugin_active != new_status)
350 GKeyFile *config = g_key_file_new();
351 gchar *data;
352 gchar *config_dir = g_path_get_dirname(config_file);
354 plugin_active = new_status;
356 /* Now we save the new status into configuration file to
357 * remember next time */
358 g_key_file_set_boolean(config, "general", "replacement_on_typing_active",
359 plugin_active);
361 if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
362 && utils_mkdir(config_dir, TRUE) != 0)
364 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
365 _("Plugin configuration directory could not be created."));
367 else
369 /* write config to file */
370 data = g_key_file_to_data(config, NULL, NULL);
371 utils_write_file(config_file, data);
372 g_free(data);
374 g_free(config_dir);
375 g_key_file_free(config);
380 static void toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
382 if (plugin_active == TRUE)
383 set_status(FALSE);
384 else
385 set_status(TRUE);
389 static void sc_on_tools_show_dialog_insert_special_chars_response
390 (GtkDialog *dialog, gint response, gpointer user_data);
391 static void sc_on_tree_row_activated
392 (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
393 static void sc_fill_store(GtkTreeStore *store);
394 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter);
397 /* Function takes over value of key which was pressed and returns
398 * HTML/SGML entity if any */
399 static const gchar *get_entity(gchar *letter)
401 guint i, len;
403 len = G_N_ELEMENTS(chars);
405 /* Ignore tags marking caracters as well as spaces*/
406 for (i = 7; i < len; i++)
408 if (utils_str_equal(chars[i][0], letter) &&
409 !utils_str_equal(" ", letter))
411 return chars[i][1];
415 /* if the char is not in the list */
416 return NULL;
420 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
421 SCNotification *nt, gpointer data)
423 gint lexer;
425 g_return_val_if_fail(editor != NULL, FALSE);
427 if (!plugin_active)
428 return FALSE;
430 lexer = sci_get_lexer(editor->sci);
431 if (lexer != SCLEX_HTML && lexer != SCLEX_XML)
432 return FALSE;
434 if (nt->nmhdr.code == SCN_CHARADDED)
436 gchar buf[7];
437 gint len;
439 len = g_unichar_to_utf8(nt->ch, buf);
440 if (len > 0)
442 const gchar *entity;
443 buf[len] = '\0';
444 entity = get_entity(buf);
446 if (entity != NULL)
448 gint pos = sci_get_current_position(editor->sci);
450 sci_set_selection_start(editor->sci, pos - len);
451 sci_set_selection_end(editor->sci, pos);
453 sci_replace_sel(editor->sci, entity);
458 return FALSE;
462 /* Called when keys were pressed */
463 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)
465 if (plugin_active == TRUE)
467 set_status(FALSE);
469 else
471 set_status(TRUE);
476 static void tools_show_dialog_insert_special_chars(void)
478 if (sc_dialog == NULL)
480 gint height;
481 GtkCellRenderer *renderer;
482 GtkTreeViewColumn *column;
483 GtkWidget *swin, *vbox, *label;
485 sc_dialog = gtk_dialog_new_with_buttons(
486 _("Special Characters"), GTK_WINDOW(geany->main_widgets->window),
487 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
488 _("_Insert"), GTK_RESPONSE_OK, NULL);
489 vbox = ui_dialog_vbox_new(GTK_DIALOG(sc_dialog));
490 gtk_box_set_spacing(GTK_BOX(vbox), 6);
491 gtk_widget_set_name(sc_dialog, "GeanyDialog");
493 height = GEANY_DEFAULT_DIALOG_HEIGHT;
494 gtk_window_set_default_size(GTK_WINDOW(sc_dialog), height * 8 / 10, height);
495 gtk_dialog_set_default_response(GTK_DIALOG(sc_dialog), GTK_RESPONSE_CANCEL);
497 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."));
498 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
499 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
500 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
502 sc_tree = GTK_TREE_VIEW(gtk_tree_view_new());
504 sc_store = gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
505 gtk_tree_view_set_model(GTK_TREE_VIEW(sc_tree),
506 GTK_TREE_MODEL(sc_store));
507 g_object_unref(sc_store);
509 renderer = gtk_cell_renderer_text_new();
510 column = gtk_tree_view_column_new_with_attributes(
511 _("Character"), renderer, "text", COLUMN_CHARACTER, NULL);
512 gtk_tree_view_column_set_resizable(column, TRUE);
513 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
515 renderer = gtk_cell_renderer_text_new();
516 column = gtk_tree_view_column_new_with_attributes(
517 _("HTML (name)"), renderer, "text", COLUMN_HTML_NAME, NULL);
518 gtk_tree_view_column_set_resizable(column, TRUE);
519 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
521 swin = gtk_scrolled_window_new(NULL, NULL);
522 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_AUTOMATIC,
523 GTK_POLICY_AUTOMATIC);
524 gtk_container_add(GTK_CONTAINER(swin), GTK_WIDGET(sc_tree));
525 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
527 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
529 g_signal_connect(sc_tree, "row-activated", G_CALLBACK(sc_on_tree_row_activated), NULL);
531 g_signal_connect(sc_dialog, "response",
532 G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response), NULL);
534 g_signal_connect(sc_dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
536 sc_fill_store(sc_store);
538 /*gtk_tree_view_expand_all(special_characters_tree);*/
539 gtk_tree_view_set_search_column(sc_tree, COLUMN_HTML_NAME);
541 gtk_widget_show_all(sc_dialog);
545 /* fill the tree model with data
546 ** TODO move this in a file and make it extendable for more data types */
547 static void sc_fill_store(GtkTreeStore *store)
549 GtkTreeIter iter;
550 GtkTreeIter *parent_iter = NULL;
551 guint i, len;
553 len = G_N_ELEMENTS(chars);
554 for (i = 0; i < len; i++)
556 if (chars[i][1] == NULL)
557 { /* add a category */
558 gtk_tree_store_append(store, &iter, NULL);
559 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, _(chars[i][0]), -1);
560 if (parent_iter != NULL) gtk_tree_iter_free(parent_iter);
561 parent_iter = gtk_tree_iter_copy(&iter);
563 else
564 { /* add child to parent_iter */
565 gtk_tree_store_append(store, &iter, parent_iter);
566 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0],
567 COLUMN_HTML_NAME, chars[i][1], -1);
573 /* just inserts the HTML_NAME coloumn of the selected row at current position
574 * returns only TRUE if a valid selection(i.e. no category) could be found */
575 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter)
577 GeanyDocument *doc = document_get_current();
578 gboolean result = FALSE;
580 if (doc != NULL)
582 gchar *str;
583 gint pos = sci_get_current_position(doc->editor->sci);
585 gtk_tree_model_get(model, iter, COLUMN_HTML_NAME, &str, -1);
586 if (!EMPTY(str))
588 sci_insert_text(doc->editor->sci, pos, str);
589 g_free(str);
590 result = TRUE;
593 return result;
597 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog *dialog, gint response,
598 gpointer user_data)
600 if (response == GTK_RESPONSE_OK)
602 GtkTreeSelection *selection;
603 GtkTreeModel *model;
604 GtkTreeIter iter;
606 selection = gtk_tree_view_get_selection(sc_tree);
608 if (gtk_tree_selection_get_selected(selection, &model, &iter))
610 /* only hide dialog if selection was not a category */
611 if (sc_insert(model, &iter))
612 gtk_widget_hide(GTK_WIDGET(dialog));
615 else
616 gtk_widget_hide(GTK_WIDGET(dialog));
620 static void sc_on_tree_row_activated(GtkTreeView *treeview, GtkTreePath *path,
621 GtkTreeViewColumn *col, gpointer user_data)
623 GtkTreeIter iter;
624 GtkTreeModel *model = GTK_TREE_MODEL(sc_store);
626 if (gtk_tree_model_get_iter(model, &iter, path))
628 /* only hide dialog if selection was not a category */
629 if (sc_insert(model, &iter))
630 gtk_widget_hide(sc_dialog);
631 else
632 { /* double click on a category to toggle the expand or collapse it */
633 if (gtk_tree_view_row_expanded(sc_tree, path))
634 gtk_tree_view_collapse_row(sc_tree, path);
635 else
636 gtk_tree_view_expand_row(sc_tree, path, FALSE);
642 static void replace_special_character(void)
644 GeanyDocument *doc = NULL;
645 doc = document_get_current();
647 if (doc != NULL && sci_has_selection(doc->editor->sci))
649 gsize selection_len;
650 gchar *selection;
651 GString *replacement = g_string_new(NULL);
652 gsize i;
653 gchar *new;
654 const gchar *entity = NULL;
655 gchar buf[7];
656 gint len;
658 selection = sci_get_selection_contents(doc->editor->sci);
660 selection_len = strlen(selection);
661 for (i = 0; i < selection_len; i++)
663 len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
664 i = (guint)len - 1 + i;
666 buf[len] = '\0';
667 entity = get_entity(buf);
669 if (entity != NULL)
671 replacement = g_string_append(replacement, entity);
673 else
675 replacement = g_string_append(replacement, buf);
678 new = g_string_free(replacement, FALSE);
679 sci_replace_sel(doc->editor->sci, new);
680 g_free(selection);
681 g_free(new);
686 /* Callback for special chars menu */
687 static void
688 item_activate(GtkMenuItem *menuitem, gpointer gdata)
690 tools_show_dialog_insert_special_chars();
694 static void kb_activate(G_GNUC_UNUSED guint key_id)
696 item_activate(NULL, NULL);
700 /* Callback for bulk replacement of selected text */
701 static void
702 replace_special_character_activated(GtkMenuItem *menuitem, gpointer gdata)
704 replace_special_character();
708 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)
710 replace_special_character();
714 static void init_configuration(void)
716 GKeyFile *config = g_key_file_new();
718 /* loading configurations from file ...*/
719 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S,
720 "plugins", G_DIR_SEPARATOR_S,
721 "htmchars", G_DIR_SEPARATOR_S, "general.conf", NULL);
723 /* ... and initialising options from config file */
724 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
726 plugin_active = utils_get_setting_boolean(config, "general",
727 "replacement_on_typing_active", FALSE);
731 /* Called by Geany to initialise the plugin */
732 void plugin_init(GeanyData *data)
734 GeanyKeyGroup *key_group;
735 GtkWidget *menu_item;
736 const gchar *menu_text = _("_Insert Special HTML Characters...");
738 /* First we catch the configuration and initialize them */
739 init_configuration();
741 /* Add an item to the Tools menu for html chars dialog*/
742 menu_item = gtk_menu_item_new_with_mnemonic(menu_text);
743 gtk_widget_show(menu_item);
744 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_item);
745 g_signal_connect(menu_item, "activate", G_CALLBACK(item_activate), NULL);
747 /* disable menu_item when there are no documents open */
748 ui_add_document_sensitive(menu_item);
750 /* Add menuitem for html replacement functions*/
751 main_menu = gtk_menu_item_new_with_mnemonic(_("_HTML Replacement"));
752 gtk_widget_show_all(main_menu);
753 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu);
755 main_menu_submenu = gtk_menu_new();
756 gtk_menu_item_set_submenu(GTK_MENU_ITEM(main_menu), main_menu_submenu);
758 menu_htmltoggle = gtk_check_menu_item_new_with_mnemonic(_("_Auto-replace Special Characters"));
759 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menu_htmltoggle),
760 plugin_active);
761 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_htmltoggle);
763 g_signal_connect((gpointer) menu_htmltoggle, "activate",
764 G_CALLBACK(toggle_status), NULL);
766 menu_bulk_replace = gtk_menu_item_new_with_mnemonic(
767 _("_Replace Characters in Selection"));
768 g_signal_connect((gpointer) menu_bulk_replace, "activate",
769 G_CALLBACK(replace_special_character_activated), NULL);
771 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_bulk_replace);
773 ui_add_document_sensitive(main_menu);
774 gtk_widget_show(menu_bulk_replace);
775 gtk_widget_show(menu_htmltoggle);
777 main_menu_item = menu_item;
779 /* setup keybindings */
780 key_group = plugin_set_key_group(geany_plugin, "html_chars", KB_COUNT, NULL);
781 keybindings_set_item(key_group, KB_INSERT_HTML_CHARS,
782 kb_activate, 0, 0, "insert_html_chars",
783 _("Insert Special HTML Characters"), menu_item);
784 keybindings_set_item(key_group, KB_REPLACE_HTML_ENTITIES,
785 kb_special_chars_replacement, 0, 0, "replace_special_characters",
786 _("Replace special characters"), NULL);
787 keybindings_set_item(key_group, KB_HTMLTOGGLE_ACTIVE,
788 kbhtmltoggle_toggle, 0, 0, "htmltoogle_toggle_plugin_status",
789 _("Toggle plugin status"), menu_htmltoggle);
793 /* Destroy widgets */
794 void plugin_cleanup(void)
796 gtk_widget_destroy(main_menu_item);
797 gtk_widget_destroy(main_menu);
799 if (sc_dialog != NULL)
800 gtk_widget_destroy(sc_dialog);
802 if (config_file != NULL)
803 g_free(config_file);