Fix callbacks to scintilla functions to work with splitwindow
[geany-mirror.git] / plugins / htmlchars.c
blob6c1ec40767ded6809390f0ec0918842deee81aeb
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 GeanyData *geany_data;
35 GeanyFunctions *geany_functions;
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
53 PLUGIN_KEY_GROUP(html_chars, KB_COUNT)
56 enum
58 COLUMN_CHARACTER,
59 COLUMN_HTML_NAME,
60 N_COLUMNS
63 static GtkWidget *main_menu_item = NULL;
64 static GtkWidget *main_menu = NULL;
65 static GtkWidget *main_menu_submenu = NULL;
66 static GtkWidget *menu_bulk_replace = NULL;
67 static GtkWidget *sc_dialog = NULL;
68 static GtkTreeStore *sc_store = NULL;
69 static GtkTreeView *sc_tree = NULL;
70 static GtkWidget *menu_htmltoggle = NULL;
71 static gboolean plugin_active = FALSE;
73 /* Configuration file */
74 static gchar *config_file = NULL;
76 const gchar *chars[][2] ={
77 { N_("HTML characters"), NULL },
78 { "\"", "&quot;" },
79 { "&", "&amp;" },
80 { "<", "&lt;" },
81 { ">", "&gt;" },
83 { N_("ISO 8859-1 characters"), NULL },
84 { " ", "&nbsp;" },
85 { "¡", "&iexcl;" },
86 { "¢", "&cent;" },
87 { "£", "&pound;" },
88 { "¤", "&curren;" },
89 { "¥", "&yen;" },
90 { "¦", "&brvbar;" },
91 { "§", "&sect;" },
92 { "¨", "&uml;" },
93 { "©", "&copy;" },
94 { "®", "&reg;" },
95 { "«", "&laquo;" },
96 { "»", "&raquo;" },
97 { "¬", "&not;" },
98 { " ", "&shy;" },
99 { "¯", "&macr;" },
100 { "°", "&deg;" },
101 { "±", "&plusmn;" },
102 { "¹", "&sup1;" },
103 { "²", "&sup2;" },
104 { "³", "&sup3;" },
105 { "¼", "&frac14;" },
106 { "½", "&frac12;" },
107 { "¾", "&frac34;" },
108 { "×", "&times;" },
109 { "÷", "&divide;" },
110 { "´", "&acute;" },
111 { "µ", "&micro;" },
112 { "¶", "&para;" },
113 { "·", "&middot;" },
114 { "¸", "&cedil;" },
115 { "ª", "&ordf;" },
116 { "º", "&ordm;" },
117 { "¿", "&iquest;" },
118 { "À", "&Agrave;" },
119 { "Á", "&Aacute;" },
120 { "Â", "&Acirc;" },
121 { "Ã", "&Atilde;" },
122 { "Ä", "&Auml;" },
123 { "Å", "&Aring;" },
124 { "Æ", "&AElig;" },
125 { "Ç", "&Ccedil;" },
126 { "È", "&Egrave;" },
127 { "É", "&Eacute;" },
128 { "Ê", "&Ecirc;" },
129 { "Ë", "&Euml;" },
130 { "Ì", "&Igrave;" },
131 { "Í", "&Iacute;" },
132 { "Î", "&Icirc;" },
133 { "Ï", "&Iuml;" },
134 { "Ð", "&ETH;" },
135 { "Ñ", "&Ntilde;" },
136 { "Ò", "&Ograve;" },
137 { "Ó", "&Oacute;" },
138 { "Ô", "&Ocirc;" },
139 { "Õ", "&Otilde;" },
140 { "Ö", "&Ouml;" },
141 { "Ø", "&Oslash;" },
142 { "Ù", "&Ugrave;" },
143 { "Ú", "&Uacute;" },
144 { "Û", "&Ucirc;" },
145 { "Ü", "&Uuml;" },
146 { "Ý", "&Yacute;" },
147 { "Þ", "&THORN;" },
148 { "ß", "&szlig;" },
149 { "à", "&agrave;" },
150 { "á", "&aacute;" },
151 { "â", "&acirc;" },
152 { "ã", "&atilde;" },
153 { "ä", "&auml;" },
154 { "å", "&aring;" },
155 { "æ", "&aelig;" },
156 { "ç", "&ccedil;" },
157 { "è", "&egrave;" },
158 { "é", "&eacute;" },
159 { "ê", "&ecirc;" },
160 { "ë", "&euml;" },
161 { "ì", "&igrave;" },
162 { "í", "&iacute;" },
163 { "î", "&icirc;" },
164 { "ï", "&iuml;" },
165 { "ð", "&eth;" },
166 { "ñ", "&ntilde;" },
167 { "ò", "&ograve;" },
168 { "ó", "&oacute;" },
169 { "ô", "&ocirc;" },
170 { "õ", "&otilde;" },
171 { "ö", "&ouml;" },
172 { "ø", "&oslash;" },
173 { "ù", "&ugrave;" },
174 { "ú", "&uacute;" },
175 { "û", "&ucirc;" },
176 { "ü", "&uuml;" },
177 { "ý", "&yacute;" },
178 { "þ", "&thorn;" },
179 { "ÿ", "&yuml;" },
181 { N_("Greek characters"), NULL },
182 { "Α", "&Alpha;" },
183 { "α", "&alpha;" },
184 { "Β", "&Beta;" },
185 { "β", "&beta;" },
186 { "Γ", "&Gamma;" },
187 { "γ", "&gamma;" },
188 { "Δ", "&Delta;" },
189 { "δ", "&Delta;" },
190 { "δ", "&delta;" },
191 { "Ε", "&Epsilon;" },
192 { "ε", "&epsilon;" },
193 { "Ζ", "&Zeta;" },
194 { "ζ", "&zeta;" },
195 { "Η", "&Eta;" },
196 { "η", "&eta;" },
197 { "Θ", "&Theta;" },
198 { "θ", "&theta;" },
199 { "Ι", "&Iota;" },
200 { "ι", "&iota;" },
201 { "Κ", "&Kappa;" },
202 { "κ", "&kappa;" },
203 { "Λ", "&Lambda;" },
204 { "λ", "&lambda;" },
205 { "Μ", "&Mu;" },
206 { "μ", "&mu;" },
207 { "Ν", "&Nu;" },
208 { "ν", "&nu;" },
209 { "Ξ", "&Xi;" },
210 { "ξ", "&xi;" },
211 { "Ο", "&Omicron;" },
212 { "ο", "&omicron;" },
213 { "Π", "&Pi;" },
214 { "π", "&pi;" },
215 { "Ρ", "&Rho;" },
216 { "ρ", "&rho;" },
217 { "Σ", "&Sigma;" },
218 { "ς", "&sigmaf;" },
219 { "σ", "&sigma;" },
220 { "Τ", "&Tau;" },
221 { "τ", "&tau;" },
222 { "Υ", "&Upsilon;" },
223 { "υ", "&upsilon;" },
224 { "Φ", "&Phi;" },
225 { "φ", "&phi;" },
226 { "Χ", "&Chi;" },
227 { "χ", "&chi;" },
228 { "Ψ", "&Psi;" },
229 { "ψ", "&psi;" },
230 { "Ω", "&Omega;" },
231 { "ω", "&omega;" },
232 { "ϑ", "&thetasym;" },
233 { "ϒ", "&upsih;" },
234 { "ϖ", "&piv;" },
236 { N_("Mathematical characters"), NULL },
237 { "∀", "&forall;" },
238 { "∂", "&part;" },
239 { "∃", "&exist;" },
240 { "∅", "&empty;" },
241 { "∇", "&nabla;" },
242 { "∈", "&isin;" },
243 { "∉", "&notin;" },
244 { "∋", "&ni;" },
245 { "∏", "&prod;" },
246 { "∑", "&sum;" },
247 { "−", "&minus;" },
248 { "∗", "&lowast;" },
249 { "√", "&radic;" },
250 { "∝", "&prop;" },
251 { "∞", "&infin;" },
252 { "∠", "&ang;" },
253 { "∧", "&and;" },
254 { "∨", "&or;" },
255 { "∩", "&cap;" },
256 { "∪", "&cup;" },
257 { "∫", "&int;" },
258 { "∴", "&there4;" },
259 { "∼", "&sim;" },
260 { "≅", "&cong;" },
261 { "≈", "&asymp;" },
262 { "≠", "&ne;" },
263 { "≡", "&equiv;" },
264 { "≤", "&le;" },
265 { "≥", "&ge;" },
266 { "⊂", "&sub;" },
267 { "⊃", "&sup;" },
268 { "⊄", "&nsub;" },
269 { "⊆", "&sube;" },
270 { "⊇", "&supe;" },
271 { "⊕", "&oplus;" },
272 { "⊗", "&otimes;" },
273 { "⊥", "&perp;" },
274 { "⋅", "&sdot;" },
275 { "◊", "&loz;" },
277 { N_("Technical characters"), NULL },
278 { "⌈", "&lceil;" },
279 { "⌉", "&rceil;" },
280 { "⌊", "&lfloor;" },
281 { "⌋", "&rfloor;" },
282 { "〈", "&lang;" },
283 { "〉", "&rang;" },
285 { N_("Arrow characters"), NULL },
286 { "←", "&larr;" },
287 { "↑", "&uarr;" },
288 { "→", "&rarr;" },
289 { "↓", "&darr;" },
290 { "↔", "&harr;" },
291 { "↵", "&crarr;" },
292 { "⇐", "&lArr;" },
293 { "⇑", "&uArr;" },
294 { "⇒", "&rArr;" },
295 { "⇓", "&dArr;" },
296 { "⇔", "&hArr;" },
298 { N_("Punctuation characters"), NULL },
299 { "–", "&ndash;" },
300 { "—", "&mdash;" },
301 { "‘", "&lsquo;" },
302 { "’", "&rsquo;" },
303 { "‚", "&sbquo;" },
304 { "“", "&ldquo;" },
305 { "”", "&rdquo;" },
306 { "„", "&bdquo;" },
307 { "†", "&dagger;" },
308 { "‡", "&Dagger;" },
309 { "…", "&hellip;" },
310 { "‰", "&permil;" },
311 { "‹", "&lsaquo;" },
312 { "›", "&rsaquo;" },
314 { N_("Miscellaneous characters"), NULL },
315 { "•", "&bull;" },
316 { "′", "&prime;" },
317 { "″", "&Prime;" },
318 { "‾", "&oline;" },
319 { "⁄", "&frasl;" },
320 { "℘", "&weierp;" },
321 { "ℑ", "&image;" },
322 { "ℜ", "&real;" },
323 { "™", "&trade;" },
324 { "€", "&euro;" },
325 { "ℵ", "&alefsym;" },
326 { "♠", "&spades;" },
327 { "♣", "&clubs;" },
328 { "♥", "&hearts;" },
329 { "♦", "&diams;" },
330 { "Œ", "&OElig;" },
331 { "œ", "&oelig;" },
332 { "Š", "&Scaron;" },
333 { "š", "&scaron;" },
334 { "Ÿ", "&Yuml;" },
335 { "ƒ", "&fnof;" },
338 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
339 SCNotification *nt, gpointer data);
342 PluginCallback plugin_callbacks[] =
344 { "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
345 { NULL, NULL, FALSE, NULL }
349 /* Functions to toggle the status of plugin */
350 static void set_status(gboolean new_status)
352 if (plugin_active != new_status)
354 GKeyFile *config = g_key_file_new();
355 gchar *data;
356 gchar *config_dir = g_path_get_dirname(config_file);
358 plugin_active = new_status;
360 /* Now we save the new status into configuration file to
361 * remember next time */
362 g_key_file_set_boolean(config, "general", "replacement_on_typing_active",
363 plugin_active);
365 if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
366 && utils_mkdir(config_dir, TRUE) != 0)
368 dialogs_show_msgbox(GTK_MESSAGE_ERROR,
369 _("Plugin configuration directory could not be created."));
371 else
373 /* write config to file */
374 data = g_key_file_to_data(config, NULL, NULL);
375 utils_write_file(config_file, data);
376 g_free(data);
378 g_free(config_dir);
379 g_key_file_free(config);
384 static void toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
386 if (plugin_active == TRUE)
387 set_status(FALSE);
388 else
389 set_status(TRUE);
393 static void sc_on_tools_show_dialog_insert_special_chars_response
394 (GtkDialog *dialog, gint response, gpointer user_data);
395 static void sc_on_tree_row_activated
396 (GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *col, gpointer user_data);
397 static void sc_fill_store(GtkTreeStore *store);
398 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter);
401 /* Function takes over value of key which was pressed and returns
402 * HTML/SGML entity if any */
403 static const gchar *get_entity(gchar *letter)
405 guint i, len;
407 len = G_N_ELEMENTS(chars);
409 /* Ignore tags marking caracters as well as spaces*/
410 for (i = 7; i < len; i++)
412 if (utils_str_equal(chars[i][0], letter) &&
413 !utils_str_equal(" ", letter))
415 return chars[i][1];
419 /* if the char is not in the list */
420 return NULL;
424 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
425 SCNotification *nt, gpointer data)
427 gint lexer;
429 g_return_val_if_fail(editor != NULL, FALSE);
431 if (!plugin_active)
432 return FALSE;
434 lexer = sci_get_lexer(editor->sci);
435 if (lexer != SCLEX_HTML && lexer != SCLEX_XML)
436 return FALSE;
438 if (nt->nmhdr.code == SCN_CHARADDED)
440 gchar buf[7];
441 gint len;
443 len = g_unichar_to_utf8(nt->ch, buf);
444 if (len > 0)
446 const gchar *entity;
447 buf[len] = '\0';
448 entity = get_entity(buf);
450 if (entity != NULL)
452 gint pos = sci_get_current_position(editor->sci);
454 sci_set_selection_start(editor->sci, pos - len);
455 sci_set_selection_end(editor->sci, pos);
457 sci_replace_sel(editor->sci, entity);
462 return FALSE;
466 /* Called when keys were pressed */
467 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)
469 if (plugin_active == TRUE)
471 set_status(FALSE);
473 else
475 set_status(TRUE);
480 static void tools_show_dialog_insert_special_chars(void)
482 if (sc_dialog == NULL)
484 gint height;
485 GtkCellRenderer *renderer;
486 GtkTreeViewColumn *column;
487 GtkWidget *swin, *vbox, *label;
489 sc_dialog = gtk_dialog_new_with_buttons(
490 _("Special Characters"), GTK_WINDOW(geany->main_widgets->window),
491 GTK_DIALOG_DESTROY_WITH_PARENT, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
492 _("_Insert"), GTK_RESPONSE_OK, NULL);
493 vbox = ui_dialog_vbox_new(GTK_DIALOG(sc_dialog));
494 gtk_box_set_spacing(GTK_BOX(vbox), 6);
495 gtk_widget_set_name(sc_dialog, "GeanyDialog");
497 height = GEANY_DEFAULT_DIALOG_HEIGHT;
498 gtk_window_set_default_size(GTK_WINDOW(sc_dialog), height * 8 / 10, height);
499 gtk_dialog_set_default_response(GTK_DIALOG(sc_dialog), GTK_RESPONSE_CANCEL);
501 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."));
502 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
503 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
504 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
506 sc_tree = GTK_TREE_VIEW(gtk_tree_view_new());
508 sc_store = gtk_tree_store_new(N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING);
509 gtk_tree_view_set_model(GTK_TREE_VIEW(sc_tree),
510 GTK_TREE_MODEL(sc_store));
511 g_object_unref(sc_store);
513 renderer = gtk_cell_renderer_text_new();
514 column = gtk_tree_view_column_new_with_attributes(
515 _("Character"), renderer, "text", COLUMN_CHARACTER, NULL);
516 gtk_tree_view_column_set_resizable(column, TRUE);
517 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
519 renderer = gtk_cell_renderer_text_new();
520 column = gtk_tree_view_column_new_with_attributes(
521 _("HTML (name)"), renderer, "text", COLUMN_HTML_NAME, NULL);
522 gtk_tree_view_column_set_resizable(column, TRUE);
523 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree), column);
525 swin = gtk_scrolled_window_new(NULL, NULL);
526 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_AUTOMATIC,
527 GTK_POLICY_AUTOMATIC);
528 gtk_container_add(GTK_CONTAINER(swin), GTK_WIDGET(sc_tree));
529 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
531 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
533 g_signal_connect(sc_tree, "row-activated", G_CALLBACK(sc_on_tree_row_activated), NULL);
535 g_signal_connect(sc_dialog, "response",
536 G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response), NULL);
538 g_signal_connect(sc_dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
540 sc_fill_store(sc_store);
542 /*gtk_tree_view_expand_all(special_characters_tree);*/
543 gtk_tree_view_set_search_column(sc_tree, COLUMN_HTML_NAME);
545 gtk_widget_show_all(sc_dialog);
549 /* fill the tree model with data
550 ** TODO move this in a file and make it extendable for more data types */
551 static void sc_fill_store(GtkTreeStore *store)
553 GtkTreeIter iter;
554 GtkTreeIter *parent_iter = NULL;
555 guint i, len;
557 len = G_N_ELEMENTS(chars);
558 for (i = 0; i < len; i++)
560 if (chars[i][1] == NULL)
561 { /* add a category */
562 gtk_tree_store_append(store, &iter, NULL);
563 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, _(chars[i][0]), -1);
564 if (parent_iter != NULL) gtk_tree_iter_free(parent_iter);
565 parent_iter = gtk_tree_iter_copy(&iter);
567 else
568 { /* add child to parent_iter */
569 gtk_tree_store_append(store, &iter, parent_iter);
570 gtk_tree_store_set(store, &iter, COLUMN_CHARACTER, chars[i][0],
571 COLUMN_HTML_NAME, chars[i][1], -1);
577 /* just inserts the HTML_NAME coloumn of the selected row at current position
578 * returns only TRUE if a valid selection(i.e. no category) could be found */
579 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter)
581 GeanyDocument *doc = document_get_current();
582 gboolean result = FALSE;
584 if (doc != NULL)
586 gchar *str;
587 gint pos = sci_get_current_position(doc->editor->sci);
589 gtk_tree_model_get(model, iter, COLUMN_HTML_NAME, &str, -1);
590 if (!EMPTY(str))
592 sci_insert_text(doc->editor->sci, pos, str);
593 g_free(str);
594 result = TRUE;
597 return result;
601 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog *dialog, gint response,
602 gpointer user_data)
604 if (response == GTK_RESPONSE_OK)
606 GtkTreeSelection *selection;
607 GtkTreeModel *model;
608 GtkTreeIter iter;
610 selection = gtk_tree_view_get_selection(sc_tree);
612 if (gtk_tree_selection_get_selected(selection, &model, &iter))
614 /* only hide dialog if selection was not a category */
615 if (sc_insert(model, &iter))
616 gtk_widget_hide(GTK_WIDGET(dialog));
619 else
620 gtk_widget_hide(GTK_WIDGET(dialog));
624 static void sc_on_tree_row_activated(GtkTreeView *treeview, GtkTreePath *path,
625 GtkTreeViewColumn *col, gpointer user_data)
627 GtkTreeIter iter;
628 GtkTreeModel *model = GTK_TREE_MODEL(sc_store);
630 if (gtk_tree_model_get_iter(model, &iter, path))
632 /* only hide dialog if selection was not a category */
633 if (sc_insert(model, &iter))
634 gtk_widget_hide(sc_dialog);
635 else
636 { /* double click on a category to toggle the expand or collapse it */
637 if (gtk_tree_view_row_expanded(sc_tree, path))
638 gtk_tree_view_collapse_row(sc_tree, path);
639 else
640 gtk_tree_view_expand_row(sc_tree, path, FALSE);
646 static void replace_special_character(void)
648 GeanyDocument *doc = NULL;
649 doc = document_get_current();
651 if (doc != NULL && sci_has_selection(doc->editor->sci))
653 gsize selection_len;
654 gchar *selection;
655 GString *replacement = g_string_new(NULL);
656 gsize i;
657 gchar *new;
658 const gchar *entity = NULL;
659 gchar buf[7];
660 gint len;
662 selection = sci_get_selection_contents(doc->editor->sci);
664 selection_len = strlen(selection);
665 for (i = 0; i < selection_len; i++)
667 len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
668 i = (guint)len - 1 + i;
670 buf[len] = '\0';
671 entity = get_entity(buf);
673 if (entity != NULL)
675 replacement = g_string_append(replacement, entity);
677 else
679 replacement = g_string_append(replacement, buf);
682 new = g_string_free(replacement, FALSE);
683 sci_replace_sel(doc->editor->sci, new);
684 g_free(selection);
685 g_free(new);
690 /* Callback for special chars menu */
691 static void
692 item_activate(GtkMenuItem *menuitem, gpointer gdata)
694 tools_show_dialog_insert_special_chars();
698 static void kb_activate(G_GNUC_UNUSED guint key_id)
700 item_activate(NULL, NULL);
704 /* Callback for bulk replacement of selected text */
705 static void
706 replace_special_character_activated(GtkMenuItem *menuitem, gpointer gdata)
708 replace_special_character();
712 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)
714 replace_special_character();
718 static void init_configuration(void)
720 GKeyFile *config = g_key_file_new();
722 /* loading configurations from file ...*/
723 config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S,
724 "plugins", G_DIR_SEPARATOR_S,
725 "htmchars", G_DIR_SEPARATOR_S, "general.conf", NULL);
727 /* ... and initialising options from config file */
728 g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
730 plugin_active = utils_get_setting_boolean(config, "general",
731 "replacement_on_typing_active", FALSE);
735 /* Called by Geany to initialise the plugin */
736 void plugin_init(GeanyData *data)
738 GtkWidget *menu_item;
739 const gchar *menu_text = _("_Insert Special HTML Characters...");
741 /* First we catch the configuration and initialize them */
742 init_configuration();
744 /* Add an item to the Tools menu for html chars dialog*/
745 menu_item = gtk_menu_item_new_with_mnemonic(menu_text);
746 gtk_widget_show(menu_item);
747 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), menu_item);
748 g_signal_connect(menu_item, "activate", G_CALLBACK(item_activate), NULL);
750 /* disable menu_item when there are no documents open */
751 ui_add_document_sensitive(menu_item);
753 /* Add menuitem for html replacement functions*/
754 main_menu = gtk_menu_item_new_with_mnemonic(_("_HTML Replacement"));
755 gtk_widget_show_all(main_menu);
756 gtk_container_add(GTK_CONTAINER(geany->main_widgets->tools_menu), main_menu);
758 main_menu_submenu = gtk_menu_new();
759 gtk_menu_item_set_submenu(GTK_MENU_ITEM(main_menu), main_menu_submenu);
761 menu_htmltoggle = gtk_check_menu_item_new_with_mnemonic(_("_Auto-replace Special Characters"));
762 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menu_htmltoggle),
763 plugin_active);
764 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_htmltoggle);
766 g_signal_connect((gpointer) menu_htmltoggle, "activate",
767 G_CALLBACK(toggle_status), NULL);
769 menu_bulk_replace = gtk_menu_item_new_with_mnemonic(
770 _("_Replace Characters in Selection"));
771 g_signal_connect((gpointer) menu_bulk_replace, "activate",
772 G_CALLBACK(replace_special_character_activated), NULL);
774 gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_bulk_replace);
776 ui_add_document_sensitive(main_menu);
777 gtk_widget_show(menu_bulk_replace);
778 gtk_widget_show(menu_htmltoggle);
780 main_menu_item = menu_item;
782 /* setup keybindings */
783 keybindings_set_item(plugin_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(plugin_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(plugin_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);