Rename js.c to jscript.c to match universal-ctags
[geany-mirror.git] / plugins / htmlchars.c
blob05f069553a228bffd1fc2a489573ae3678b5489a
1 /*
2 * htmlchars.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6 * Copyright 2007-2012 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 along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* HTML Characters plugin (Inserts HTML character entities like '&amp;') */
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "geanyplugin.h"
30 #include <string.h>
31 #include "SciLexer.h"
34 GeanyPlugin *geany_plugin;
35 GeanyData *geany_data;
38 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
40 PLUGIN_SET_INFO(_("HTML Characters"), _("Inserts HTML character entities like '&amp;'."), VERSION,
41 _("The Geany developer team"))
44 /* Keybinding(s) */
45 enum
47 KB_INSERT_HTML_CHARS,
48 KB_REPLACE_HTML_ENTITIES,
49 KB_HTMLTOGGLE_ACTIVE,
50 KB_COUNT
54 enum
56 COLUMN_CHARACTER,
57 COLUMN_HTML_NAME,
58 N_COLUMNS
61 static GtkWidget *main_menu_item = NULL;
62 static GtkWidget *main_menu = NULL;
63 static GtkWidget *main_menu_submenu = NULL;
64 static GtkWidget *menu_bulk_replace = NULL;
65 static GtkWidget *sc_dialog = NULL;
66 static GtkTreeStore *sc_store = NULL;
67 static GtkTreeView *sc_tree = NULL;
68 static GtkWidget *menu_htmltoggle = NULL;
69 static gboolean plugin_active = FALSE;
71 /* Configuration file */
72 static gchar *config_file = NULL;
74 const gchar *chars[][2] ={
75 { N_("HTML characters"), NULL },
76 { "\"", "&quot;" },
77 { "&", "&amp;" },
78 { "<", "&lt;" },
79 { ">", "&gt;" },
81 { N_("ISO 8859-1 characters"), NULL },
82 { " ", "&nbsp;" },
83 { "¡", "&iexcl;" },
84 { "¢", "&cent;" },
85 { "£", "&pound;" },
86 { "¤", "&curren;" },
87 { "¥", "&yen;" },
88 { "¦", "&brvbar;" },
89 { "§", "&sect;" },
90 { "¨", "&uml;" },
91 { "©", "&copy;" },
92 { "®", "&reg;" },
93 { "«", "&laquo;" },
94 { "»", "&raquo;" },
95 { "¬", "&not;" },
96 { " ", "&shy;" },
97 { "¯", "&macr;" },
98 { "°", "&deg;" },
99 { "±", "&plusmn;" },
100 { "¹", "&sup1;" },
101 { "²", "&sup2;" },
102 { "³", "&sup3;" },
103 { "¼", "&frac14;" },
104 { "½", "&frac12;" },
105 { "¾", "&frac34;" },
106 { "×", "&times;" },
107 { "÷", "&divide;" },
108 { "´", "&acute;" },
109 { "µ", "&micro;" },
110 { "¶", "&para;" },
111 { "·", "&middot;" },
112 { "¸", "&cedil;" },
113 { "ª", "&ordf;" },
114 { "º", "&ordm;" },
115 { "¿", "&iquest;" },
116 { "À", "&Agrave;" },
117 { "Á", "&Aacute;" },
118 { "Â", "&Acirc;" },
119 { "Ã", "&Atilde;" },
120 { "Ä", "&Auml;" },
121 { "Å", "&Aring;" },
122 { "Æ", "&AElig;" },
123 { "Ç", "&Ccedil;" },
124 { "È", "&Egrave;" },
125 { "É", "&Eacute;" },
126 { "Ê", "&Ecirc;" },
127 { "Ë", "&Euml;" },
128 { "Ì", "&Igrave;" },
129 { "Í", "&Iacute;" },
130 { "Î", "&Icirc;" },
131 { "Ï", "&Iuml;" },
132 { "Ð", "&ETH;" },
133 { "Ñ", "&Ntilde;" },
134 { "Ò", "&Ograve;" },
135 { "Ó", "&Oacute;" },
136 { "Ô", "&Ocirc;" },
137 { "Õ", "&Otilde;" },
138 { "Ö", "&Ouml;" },
139 { "Ø", "&Oslash;" },
140 { "Ù", "&Ugrave;" },
141 { "Ú", "&Uacute;" },
142 { "Û", "&Ucirc;" },
143 { "Ü", "&Uuml;" },
144 { "Ý", "&Yacute;" },
145 { "Þ", "&THORN;" },
146 { "ß", "&szlig;" },
147 { "à", "&agrave;" },
148 { "á", "&aacute;" },
149 { "â", "&acirc;" },
150 { "ã", "&atilde;" },
151 { "ä", "&auml;" },
152 { "å", "&aring;" },
153 { "æ", "&aelig;" },
154 { "ç", "&ccedil;" },
155 { "è", "&egrave;" },
156 { "é", "&eacute;" },
157 { "ê", "&ecirc;" },
158 { "ë", "&euml;" },
159 { "ì", "&igrave;" },
160 { "í", "&iacute;" },
161 { "î", "&icirc;" },
162 { "ï", "&iuml;" },
163 { "ð", "&eth;" },
164 { "ñ", "&ntilde;" },
165 { "ò", "&ograve;" },
166 { "ó", "&oacute;" },
167 { "ô", "&ocirc;" },
168 { "õ", "&otilde;" },
169 { "ö", "&ouml;" },
170 { "ø", "&oslash;" },
171 { "ù", "&ugrave;" },
172 { "ú", "&uacute;" },
173 { "û", "&ucirc;" },
174 { "ü", "&uuml;" },
175 { "ý", "&yacute;" },
176 { "þ", "&thorn;" },
177 { "ÿ", "&yuml;" },
179 { N_("Greek characters"), NULL },
180 { "Α", "&Alpha;" },
181 { "α", "&alpha;" },
182 { "Β", "&Beta;" },
183 { "β", "&beta;" },
184 { "Γ", "&Gamma;" },
185 { "γ", "&gamma;" },
186 { "Δ", "&Delta;" },
187 { "δ", "&Delta;" },
188 { "δ", "&delta;" },
189 { "Ε", "&Epsilon;" },
190 { "ε", "&epsilon;" },
191 { "Ζ", "&Zeta;" },
192 { "ζ", "&zeta;" },
193 { "Η", "&Eta;" },
194 { "η", "&eta;" },
195 { "Θ", "&Theta;" },
196 { "θ", "&theta;" },
197 { "Ι", "&Iota;" },
198 { "ι", "&iota;" },
199 { "Κ", "&Kappa;" },
200 { "κ", "&kappa;" },
201 { "Λ", "&Lambda;" },
202 { "λ", "&lambda;" },
203 { "Μ", "&Mu;" },
204 { "μ", "&mu;" },
205 { "Ν", "&Nu;" },
206 { "ν", "&nu;" },
207 { "Ξ", "&Xi;" },
208 { "ξ", "&xi;" },
209 { "Ο", "&Omicron;" },
210 { "ο", "&omicron;" },
211 { "Π", "&Pi;" },
212 { "π", "&pi;" },
213 { "Ρ", "&Rho;" },
214 { "ρ", "&rho;" },
215 { "Σ", "&Sigma;" },
216 { "ς", "&sigmaf;" },
217 { "σ", "&sigma;" },
218 { "Τ", "&Tau;" },
219 { "τ", "&tau;" },
220 { "Υ", "&Upsilon;" },
221 { "υ", "&upsilon;" },
222 { "Φ", "&Phi;" },
223 { "φ", "&phi;" },
224 { "Χ", "&Chi;" },
225 { "χ", "&chi;" },
226 { "Ψ", "&Psi;" },
227 { "ψ", "&psi;" },
228 { "Ω", "&Omega;" },
229 { "ω", "&omega;" },
230 { "ϑ", "&thetasym;" },
231 { "ϒ", "&upsih;" },
232 { "ϖ", "&piv;" },
234 { N_("Mathematical characters"), NULL },
235 { "∀", "&forall;" },
236 { "∂", "&part;" },
237 { "∃", "&exist;" },
238 { "∅", "&empty;" },
239 { "∇", "&nabla;" },
240 { "∈", "&isin;" },
241 { "∉", "&notin;" },
242 { "∋", "&ni;" },
243 { "∏", "&prod;" },
244 { "∑", "&sum;" },
245 { "−", "&minus;" },
246 { "∗", "&lowast;" },
247 { "√", "&radic;" },
248 { "∝", "&prop;" },
249 { "∞", "&infin;" },
250 { "∠", "&ang;" },
251 { "∧", "&and;" },
252 { "∨", "&or;" },
253 { "∩", "&cap;" },
254 { "∪", "&cup;" },
255 { "∫", "&int;" },
256 { "∴", "&there4;" },
257 { "∼", "&sim;" },
258 { "≅", "&cong;" },
259 { "≈", "&asymp;" },
260 { "≠", "&ne;" },
261 { "≡", "&equiv;" },
262 { "≤", "&le;" },
263 { "≥", "&ge;" },
264 { "⊂", "&sub;" },
265 { "⊃", "&sup;" },
266 { "⊄", "&nsub;" },
267 { "⊆", "&sube;" },
268 { "⊇", "&supe;" },
269 { "⊕", "&oplus;" },
270 { "⊗", "&otimes;" },
271 { "⊥", "&perp;" },
272 { "⋅", "&sdot;" },
273 { "◊", "&loz;" },
275 { N_("Technical characters"), NULL },
276 { "⌈", "&lceil;" },
277 { "⌉", "&rceil;" },
278 { "⌊", "&lfloor;" },
279 { "⌋", "&rfloor;" },
280 { "〈", "&lang;" },
281 { "〉", "&rang;" },
283 { N_("Arrow characters"), NULL },
284 { "←", "&larr;" },
285 { "↑", "&uarr;" },
286 { "→", "&rarr;" },
287 { "↓", "&darr;" },
288 { "↔", "&harr;" },
289 { "↵", "&crarr;" },
290 { "⇐", "&lArr;" },
291 { "⇑", "&uArr;" },
292 { "⇒", "&rArr;" },
293 { "⇓", "&dArr;" },
294 { "⇔", "&hArr;" },
296 { N_("Punctuation characters"), NULL },
297 { "–", "&ndash;" },
298 { "—", "&mdash;" },
299 { "‘", "&lsquo;" },
300 { "’", "&rsquo;" },
301 { "‚", "&sbquo;" },
302 { "“", "&ldquo;" },
303 { "”", "&rdquo;" },
304 { "„", "&bdquo;" },
305 { "†", "&dagger;" },
306 { "‡", "&Dagger;" },
307 { "…", "&hellip;" },
308 { "‰", "&permil;" },
309 { "‹", "&lsaquo;" },
310 { "›", "&rsaquo;" },
312 { N_("Miscellaneous characters"), NULL },
313 { "•", "&bull;" },
314 { "′", "&prime;" },
315 { "″", "&Prime;" },
316 { "‾", "&oline;" },
317 { "⁄", "&frasl;" },
318 { "℘", "&weierp;" },
319 { "ℑ", "&image;" },
320 { "ℜ", "&real;" },
321 { "™", "&trade;" },
322 { "€", "&euro;" },
323 { "ℵ", "&alefsym;" },
324 { "♠", "&spades;" },
325 { "♣", "&clubs;" },
326 { "♥", "&hearts;" },
327 { "♦", "&diams;" },
328 { "Œ", "&OElig;" },
329 { "œ", "&oelig;" },
330 { "Š", "&Scaron;" },
331 { "š", "&scaron;" },
332 { "Ÿ", "&Yuml;" },
333 { "ƒ", "&fnof;" },
336 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
337 SCNotification *nt, gpointer data);
340 PluginCallback plugin_callbacks[] =
342 { "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
343 { NULL, NULL, FALSE, NULL }
347 /* Functions to toggle the status of plugin */
348 static void set_status(gboolean new_status)
350 if (plugin_active != new_status)
352 GKeyFile *config = g_key_file_new();
353 gchar *data;
354 gchar *config_dir = g_path_get_dirname(config_file);
356 plugin_active = new_status;
358 /* Now we save the new status into configuration file to
359 * remember next time */
360 g_key_file_set_boolean(config, "general", "replacement_on_typing_active",
361 plugin_active);
363 if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
364 && utils_mkdir(config_dir, TRUE) != 0)
366 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
367 _("Plugin configuration directory could not be created."));
369 else
371 /* write config to file */
372 data = g_key_file_to_data(config, NULL, NULL);
373 utils_write_file(config_file, data);
374 g_free(data);
376 g_free(config_dir);
377 g_key_file_free(config);
382 static void toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
384 if (plugin_active == TRUE)
385 set_status(FALSE);
386 else
387 set_status(TRUE);
391 static void sc_on_tools_show_dialog_insert_special_chars_response
392 (GtkDialog *dialog, gint response, gpointer user_data);
393 static void sc_on_tree_row_activated
394 (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
395 static void sc_fill_store(GtkTreeStore *store);
396 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter);
399 /* Function takes over value of key which was pressed and returns
400 * HTML/SGML entity if any */
401 static const gchar *get_entity(gchar *letter)
403 guint i, len;
405 len = G_N_ELEMENTS(chars);
407 /* Ignore tags marking caracters as well as spaces*/
408 for (i = 7; i < len; i++)
410 if (utils_str_equal(chars[i][0], letter) &&
411 !utils_str_equal(" ", letter))
413 return chars[i][1];
417 /* if the char is not in the list */
418 return NULL;
422 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
423 SCNotification *nt, gpointer data)
425 gint lexer;
427 g_return_val_if_fail(editor != NULL, FALSE);
429 if (!plugin_active)
430 return FALSE;
432 lexer = sci_get_lexer(editor->sci);
433 if (lexer != SCLEX_HTML && lexer != SCLEX_XML)
434 return FALSE;
436 if (nt->nmhdr.code == SCN_CHARADDED)
438 gchar buf[7];
439 gint len;
441 len = g_unichar_to_utf8(nt->ch, buf);
442 if (len > 0)
444 const gchar *entity;
445 buf[len] = '\0';
446 entity = get_entity(buf);
448 if (entity != NULL)
450 gint pos = sci_get_current_position(editor->sci);
452 sci_set_selection_start(editor->sci, pos - len);
453 sci_set_selection_end(editor->sci, pos);
455 sci_replace_sel(editor->sci, entity);
460 return FALSE;
464 /* Called when keys were pressed */
465 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)
467 if (plugin_active == TRUE)
469 set_status(FALSE);
471 else
473 set_status(TRUE);
478 static void tools_show_dialog_insert_special_chars(void)
480 if (sc_dialog == NULL)
482 gint height;
483 GtkCellRenderer *renderer;
484 GtkTreeViewColumn *column;
485 GtkWidget *swin, *vbox, *label;
487 sc_dialog = gtk_dialog_new_with_buttons(
488 _("Special Characters"), GTK_WINDOW(geany->main_widgets->window),
489 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
490 _("_Insert"), GTK_RESPONSE_OK, NULL);
491 vbox = ui_dialog_vbox_new(GTK_DIALOG(sc_dialog));
492 gtk_box_set_spacing(GTK_BOX(vbox), 6);
493 gtk_widget_set_name(sc_dialog, "GeanyDialog");
495 height = GEANY_DEFAULT_DIALOG_HEIGHT;
496 gtk_window_set_default_size(GTK_WINDOW(sc_dialog), height * 8 / 10, height);
497 gtk_dialog_set_default_response(GTK_DIALOG(sc_dialog), GTK_RESPONSE_CANCEL);
499 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."));
500 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
501 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
502 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
504 sc_tree = GTK_TREE_VIEW(gtk_tree_view_new());
506 sc_store = gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
507 gtk_tree_view_set_model(GTK_TREE_VIEW(sc_tree),
508 GTK_TREE_MODEL(sc_store));
509 g_object_unref(sc_store);
511 renderer = gtk_cell_renderer_text_new();
512 column = gtk_tree_view_column_new_with_attributes(
513 _("Character"), renderer, "text", COLUMN_CHARACTER, NULL);
514 gtk_tree_view_column_set_resizable(column, TRUE);
515 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
517 renderer = gtk_cell_renderer_text_new();
518 column = gtk_tree_view_column_new_with_attributes(
519 _("HTML (name)"), renderer, "text", COLUMN_HTML_NAME, NULL);
520 gtk_tree_view_column_set_resizable(column, TRUE);
521 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
523 swin = gtk_scrolled_window_new(NULL, NULL);
524 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_AUTOMATIC,
525 GTK_POLICY_AUTOMATIC);
526 gtk_container_add(GTK_CONTAINER(swin), GTK_WIDGET(sc_tree));
527 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
529 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
531 g_signal_connect(sc_tree, "row-activated", G_CALLBACK(sc_on_tree_row_activated), NULL);
533 g_signal_connect(sc_dialog, "response",
534 G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response), NULL);
536 g_signal_connect(sc_dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
538 sc_fill_store(sc_store);
540 /*gtk_tree_view_expand_all(special_characters_tree);*/
541 gtk_tree_view_set_search_column(sc_tree, COLUMN_HTML_NAME);
543 gtk_widget_show_all(sc_dialog);
547 /* fill the tree model with data
548 ** TODO move this in a file and make it extendable for more data types */
549 static void sc_fill_store(GtkTreeStore *store)
551 GtkTreeIter iter;
552 GtkTreeIter *parent_iter = NULL;
553 guint i, len;
555 len = G_N_ELEMENTS(chars);
556 for (i = 0; i < len; i++)
558 if (chars[i][1] == NULL)
559 { /* add a category */
560 gtk_tree_store_append(store, &iter, NULL);
561 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, _(chars[i][0]), -1);
562 if (parent_iter != NULL) gtk_tree_iter_free(parent_iter);
563 parent_iter = gtk_tree_iter_copy(&iter);
565 else
566 { /* add child to parent_iter */
567 gtk_tree_store_append(store, &iter, parent_iter);
568 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0],
569 COLUMN_HTML_NAME, chars[i][1], -1);
575 /* just inserts the HTML_NAME coloumn of the selected row at current position
576 * returns only TRUE if a valid selection(i.e. no category) could be found */
577 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter)
579 GeanyDocument *doc = document_get_current();
580 gboolean result = FALSE;
582 if (doc != NULL)
584 gchar *str;
585 gint pos = sci_get_current_position(doc->editor->sci);
587 gtk_tree_model_get(model, iter, COLUMN_HTML_NAME, &str, -1);
588 if (!EMPTY(str))
590 sci_insert_text(doc->editor->sci, pos, str);
591 g_free(str);
592 result = TRUE;
595 return result;
599 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog *dialog, gint response,
600 gpointer user_data)
602 if (response == GTK_RESPONSE_OK)
604 GtkTreeSelection *selection;
605 GtkTreeModel *model;
606 GtkTreeIter iter;
608 selection = gtk_tree_view_get_selection(sc_tree);
610 if (gtk_tree_selection_get_selected(selection, &model, &iter))
612 /* only hide dialog if selection was not a category */
613 if (sc_insert(model, &iter))
614 gtk_widget_hide(GTK_WIDGET(dialog));
617 else
618 gtk_widget_hide(GTK_WIDGET(dialog));
622 static void sc_on_tree_row_activated(GtkTreeView *treeview, GtkTreePath *path,
623 GtkTreeViewColumn *col, gpointer user_data)
625 GtkTreeIter iter;
626 GtkTreeModel *model = GTK_TREE_MODEL(sc_store);
628 if (gtk_tree_model_get_iter(model, &iter, path))
630 /* only hide dialog if selection was not a category */
631 if (sc_insert(model, &iter))
632 gtk_widget_hide(sc_dialog);
633 else
634 { /* double click on a category to toggle the expand or collapse it */
635 if (gtk_tree_view_row_expanded(sc_tree, path))
636 gtk_tree_view_collapse_row(sc_tree, path);
637 else
638 gtk_tree_view_expand_row(sc_tree, path, FALSE);
644 static void replace_special_character(void)
646 GeanyDocument *doc = NULL;
647 doc = document_get_current();
649 if (doc != NULL && sci_has_selection(doc->editor->sci))
651 gsize selection_len;
652 gchar *selection;
653 GString *replacement = g_string_new(NULL);
654 gsize i;
655 gchar *new;
656 const gchar *entity = NULL;
657 gchar buf[7];
658 gint len;
660 selection = sci_get_selection_contents(doc->editor->sci);
662 selection_len = strlen(selection);
663 for (i = 0; i < selection_len; i++)
665 len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
666 i = (guint)len - 1 + i;
668 buf[len] = '\0';
669 entity = get_entity(buf);
671 if (entity != NULL)
673 replacement = g_string_append(replacement, entity);
675 else
677 replacement = g_string_append(replacement, buf);
680 new = g_string_free(replacement, FALSE);
681 sci_replace_sel(doc->editor->sci, new);
682 g_free(selection);
683 g_free(new);
688 /* Callback for special chars menu */
689 static void
690 item_activate(GtkMenuItem *menuitem, gpointer gdata)
692 tools_show_dialog_insert_special_chars();
696 static void kb_activate(G_GNUC_UNUSED guint key_id)
698 item_activate(NULL, NULL);
702 /* Callback for bulk replacement of selected text */
703 static void
704 replace_special_character_activated(GtkMenuItem *menuitem, gpointer gdata)
706 replace_special_character();
710 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)
712 replace_special_character();
716 static void init_configuration(void)
718 GKeyFile *config = g_key_file_new();
720 /* loading configurations from file ...*/
721 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S,
722 "plugins", G_DIR_SEPARATOR_S,
723 "htmchars", G_DIR_SEPARATOR_S, "general.conf", NULL);
725 /* ... and initialising options from config file */
726 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
728 plugin_active = utils_get_setting_boolean(config, "general",
729 "replacement_on_typing_active", FALSE);
733 /* Called by Geany to initialise the plugin */
734 void plugin_init(GeanyData *data)
736 GeanyKeyGroup *key_group;
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(_("_Auto-replace Special Characters"));
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 _("_Replace Characters in Selection"));
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 key_group = plugin_set_key_group(geany_plugin, "html_chars", KB_COUNT, NULL);
783 keybindings_set_item(key_group, KB_INSERT_HTML_CHARS,
784 kb_activate, 0, 0, "insert_html_chars",
785 _("Insert Special HTML Characters"), menu_item);
786 keybindings_set_item(key_group, KB_REPLACE_HTML_ENTITIES,
787 kb_special_chars_replacement, 0, 0, "replace_special_characters",
788 _("Replace special characters"), NULL);
789 keybindings_set_item(key_group, KB_HTMLTOGGLE_ACTIVE,
790 kbhtmltoggle_toggle, 0, 0, "htmltoogle_toggle_plugin_status",
791 _("Toggle plugin status"), menu_htmltoggle);
795 /* Destroy widgets */
796 void plugin_cleanup(void)
798 gtk_widget_destroy(main_menu_item);
799 gtk_widget_destroy(main_menu);
801 if (sc_dialog != NULL)
802 gtk_widget_destroy(sc_dialog);
804 if (config_file != NULL)
805 g_free(config_file);