1 /*
2  *      htmlchars.c - this file is part of Geany, a fast and lightweight IDE
3  *
4  *      Copyright 2007 The Geany contributors
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 /* HTML Characters plugin (Inserts HTML character entities like '&') */
22 
23 #ifdef HAVE_CONFIG_H
24 #	include "config.h"
25 #endif
26 
27 #include "geanyplugin.h"
28 #include <string.h>
29 #include "SciLexer.h"
30 
31 
32 GeanyPlugin		*geany_plugin;
33 GeanyData		*geany_data;
34 
35 
36 PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
37 
38 PLUGIN_SET_INFO(_("HTML Characters"), _("Inserts HTML character entities like '&amp;'."), VERSION,
39 	_("The Geany developer team"))
40 
41 
42 /* Keybinding(s) */
43 enum
44 {
45 	KB_INSERT_HTML_CHARS,
46 	KB_REPLACE_HTML_ENTITIES,
47 	KB_HTMLTOGGLE_ACTIVE,
48 	KB_COUNT
49 };
50 
51 
52 enum
53 {
54 	COLUMN_CHARACTER,
55 	COLUMN_HTML_NAME,
56 	N_COLUMNS
57 };
58 
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;
68 
69 /* Configuration file */
70 static gchar *config_file = NULL;
71 
72 const gchar *chars[][2] ={
73 	{ N_("HTML characters"), NULL },
74 	{ "\"", "&quot;" },
75 	{ "&", "&amp;" },
76 	{ "<", "&lt;" },
77 	{ ">", "&gt;" },
78 
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;" },
176 
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;" },
231 
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;" },
272 
273 	{ N_("Technical characters"), NULL },
274 	{ "⌈", "&lceil;" },
275 	{ "⌉", "&rceil;" },
276 	{ "⌊", "&lfloor;" },
277 	{ "⌋", "&rfloor;" },
278 	{ "〈", "&lang;" },
279 	{ "〉", "&rang;" },
280 
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;" },
293 
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;" },
309 
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;" },
332 };
333 
334 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
335 									SCNotification *nt, gpointer data);
336 
337 
338 PluginCallback plugin_callbacks[] =
339 {
340 	{ "editor-notify", (GCallback) &ht_editor_notify_cb, FALSE, NULL },
341 	{ NULL, NULL, FALSE, NULL }
342 };
343 
344 
345 /* Functions to toggle the status of plugin */
set_status(gboolean new_status)346 static void set_status(gboolean new_status)
347 {
348 	if (plugin_active != new_status)
349 	{
350 		GKeyFile *config = g_key_file_new();
351 		gchar *data;
352 		gchar *config_dir = g_path_get_dirname(config_file);
353 
354 		plugin_active = new_status;
355 
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);
360 
361 		if (!g_file_test(config_dir, G_FILE_TEST_IS_DIR)
362 			&& utils_mkdir(config_dir, TRUE) != 0)
363 		{
364 			dialogs_show_msgbox(GTK_MESSAGE_ERROR,
365 				_("Plugin configuration directory could not be created."));
366 		}
367 		else
368 		{
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);
373 		}
374 		g_free(config_dir);
375 		g_key_file_free(config);
376 	}
377 }
378 
379 
toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)380 static void toggle_status(G_GNUC_UNUSED GtkMenuItem * menuitem)
381 {
382 	if (plugin_active == TRUE)
383 		set_status(FALSE);
384 	else
385 		set_status(TRUE);
386 }
387 
388 
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);
395 
396 
397 /* Function takes over value of key which was pressed and returns
398  * HTML/SGML entity if any */
get_entity(gchar * letter)399 static const gchar *get_entity(gchar *letter)
400 {
401 	guint i, len;
402 
403 	len = G_N_ELEMENTS(chars);
404 
405 	/* Ignore tags marking caracters as well as spaces*/
406 	for (i = 7; i < len; i++)
407 	{
408 		if (utils_str_equal(chars[i][0], letter) &&
409 			!utils_str_equal(" ", letter))
410 		{
411 			return chars[i][1];
412 		}
413 	}
414 
415 	/* if the char is not in the list */
416 	return NULL;
417 }
418 
419 
ht_editor_notify_cb(GObject * object,GeanyEditor * editor,SCNotification * nt,gpointer data)420 static gboolean ht_editor_notify_cb(GObject *object, GeanyEditor *editor,
421 									SCNotification *nt, gpointer data)
422 {
423 	gint lexer;
424 
425 	g_return_val_if_fail(editor != NULL, FALSE);
426 
427 	if (!plugin_active)
428 		return FALSE;
429 
430 	lexer = sci_get_lexer(editor->sci);
431 	if (lexer != SCLEX_HTML && lexer != SCLEX_XML)
432 			return FALSE;
433 
434 	if (nt->nmhdr.code == SCN_CHARADDED)
435 	{
436 		gchar buf[7];
437 		gint len;
438 
439 		len = g_unichar_to_utf8(nt->ch, buf);
440 		if (len > 0)
441 		{
442 			const gchar *entity;
443 			buf[len] = '\0';
444 			entity = get_entity(buf);
445 
446 			if (entity != NULL)
447 			{
448 				gint pos = sci_get_current_position(editor->sci);
449 
450 				sci_set_selection_start(editor->sci, pos - len);
451 				sci_set_selection_end(editor->sci, pos);
452 
453 				sci_replace_sel(editor->sci, entity);
454 			}
455 		}
456 	}
457 
458 	return FALSE;
459 }
460 
461 
462 /* Called when keys were pressed */
kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)463 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id)
464 {
465 	if (plugin_active == TRUE)
466 	{
467 		set_status(FALSE);
468 	}
469 	else
470 	{
471 		set_status(TRUE);
472 	}
473 }
474 
475 
tools_show_dialog_insert_special_chars(void)476 static void tools_show_dialog_insert_special_chars(void)
477 {
478 	if (sc_dialog == NULL)
479 	{
480 		gint height;
481 		GtkCellRenderer *renderer;
482 		GtkTreeViewColumn *column;
483 		GtkWidget *swin, *vbox, *label;
484 
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");
492 
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);
496 
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);
501 
502 		sc_tree = GTK_TREE_VIEW(gtk_tree_view_new());
503 
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);
508 
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);
514 
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);
520 
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);
526 
527 		gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
528 
529 		g_signal_connect(sc_tree, "row-activated", G_CALLBACK(sc_on_tree_row_activated), NULL);
530 
531 		g_signal_connect(sc_dialog, "response",
532 					G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response), NULL);
533 
534 		g_signal_connect(sc_dialog, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete), NULL);
535 
536 		sc_fill_store(sc_store);
537 
538 		/*gtk_tree_view_expand_all(special_characters_tree);*/
539 		gtk_tree_view_set_search_column(sc_tree, COLUMN_HTML_NAME);
540 	}
541 	gtk_widget_show_all(sc_dialog);
542 }
543 
544 
545 /* fill the tree model with data
546  ** TODO move this in a file and make it extendable for more data types */
sc_fill_store(GtkTreeStore * store)547 static void sc_fill_store(GtkTreeStore *store)
548 {
549 	GtkTreeIter iter;
550 	GtkTreeIter *parent_iter = NULL;
551 	guint i, len;
552 
553 	len = G_N_ELEMENTS(chars);
554 	for (i = 0; i < len; i++)
555 	{
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);
562 		}
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);
568 		}
569 	}
570 }
571 
572 
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 */
sc_insert(GtkTreeModel * model,GtkTreeIter * iter)575 static gboolean sc_insert(GtkTreeModel *model, GtkTreeIter *iter)
576 {
577 	GeanyDocument *doc = document_get_current();
578 	gboolean result = FALSE;
579 
580 	if (doc != NULL)
581 	{
582 		gchar *str;
583 		gint pos = sci_get_current_position(doc->editor->sci);
584 
585 		gtk_tree_model_get(model, iter, COLUMN_HTML_NAME, &str, -1);
586 		if (!EMPTY(str))
587 		{
588 			sci_insert_text(doc->editor->sci, pos, str);
589 			g_free(str);
590 			result = TRUE;
591 		}
592 	}
593 	return result;
594 }
595 
596 
sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog * dialog,gint response,gpointer user_data)597 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog *dialog, gint response,
598 														gpointer user_data)
599 {
600 	if (response == GTK_RESPONSE_OK)
601 	{
602 		GtkTreeSelection *selection;
603 		GtkTreeModel *model;
604 		GtkTreeIter iter;
605 
606 		selection = gtk_tree_view_get_selection(sc_tree);
607 
608 		if (gtk_tree_selection_get_selected(selection, &model, &iter))
609 		{
610 			/* only hide dialog if selection was not a category */
611 			if (sc_insert(model, &iter))
612 				gtk_widget_hide(GTK_WIDGET(dialog));
613 		}
614 	}
615 	else
616 		gtk_widget_hide(GTK_WIDGET(dialog));
617 }
618 
619 
sc_on_tree_row_activated(GtkTreeView * treeview,GtkTreePath * path,GtkTreeViewColumn * col,gpointer user_data)620 static void sc_on_tree_row_activated(GtkTreeView *treeview, GtkTreePath *path,
621 											  GtkTreeViewColumn *col, gpointer user_data)
622 {
623 	GtkTreeIter iter;
624 	GtkTreeModel *model = GTK_TREE_MODEL(sc_store);
625 
626 	if (gtk_tree_model_get_iter(model, &iter, path))
627 	{
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);
637 		}
638 	}
639 }
640 
641 
replace_special_character(void)642 static void replace_special_character(void)
643 {
644 	GeanyDocument *doc = NULL;
645 	doc = document_get_current();
646 
647 	if (doc != NULL && sci_has_selection(doc->editor->sci))
648 	{
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;
657 
658 		selection = sci_get_selection_contents(doc->editor->sci);
659 
660 		selection_len = strlen(selection);
661 		for (i = 0; i < selection_len; i++)
662 		{
663 			len = g_unichar_to_utf8(g_utf8_get_char(selection + i), buf);
664 			i = (guint)len - 1 + i;
665 
666 			buf[len] = '\0';
667 			entity = get_entity(buf);
668 
669 			if (entity != NULL)
670 			{
671 				replacement = g_string_append(replacement, entity);
672 			}
673 			else
674 			{
675 				replacement = g_string_append(replacement, buf);
676 			}
677 		}
678 		new = g_string_free(replacement, FALSE);
679 		sci_replace_sel(doc->editor->sci, new);
680 		g_free(selection);
681 		g_free(new);
682 	}
683 }
684 
685 
686 /* Callback for special chars menu */
687 static void
item_activate(GtkMenuItem * menuitem,gpointer gdata)688 item_activate(GtkMenuItem *menuitem, gpointer gdata)
689 {
690 	tools_show_dialog_insert_special_chars();
691 }
692 
693 
kb_activate(G_GNUC_UNUSED guint key_id)694 static void kb_activate(G_GNUC_UNUSED guint key_id)
695 {
696 	item_activate(NULL, NULL);
697 }
698 
699 
700 /* Callback for bulk replacement of selected text */
701 static void
replace_special_character_activated(GtkMenuItem * menuitem,gpointer gdata)702 replace_special_character_activated(GtkMenuItem *menuitem, gpointer gdata)
703 {
704 	replace_special_character();
705 }
706 
707 
kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)708 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id)
709 {
710 	replace_special_character();
711 }
712 
713 
init_configuration(void)714 static void init_configuration(void)
715 {
716 	GKeyFile *config = g_key_file_new();
717 
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);
722 
723 	/* ... and initialising options from config file */
724 	g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
725 
726 	plugin_active = utils_get_setting_boolean(config, "general",
727 		"replacement_on_typing_active", FALSE);
728 }
729 
730 
731 /* Called by Geany to initialise the plugin */
plugin_init(GeanyData * data)732 void plugin_init(GeanyData *data)
733 {
734 	GeanyKeyGroup *key_group;
735 	GtkWidget *menu_item;
736 	const gchar *menu_text = _("_Insert Special HTML Characters...");
737 
738 	/* First we catch the configuration and initialize them */
739 	init_configuration();
740 
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);
746 
747 	/* disable menu_item when there are no documents open */
748 	ui_add_document_sensitive(menu_item);
749 
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);
754 
755 	main_menu_submenu = gtk_menu_new();
756 	gtk_menu_item_set_submenu(GTK_MENU_ITEM(main_menu), main_menu_submenu);
757 
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);
762 
763 	g_signal_connect((gpointer) menu_htmltoggle, "activate",
764 			 G_CALLBACK(toggle_status), NULL);
765 
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);
770 
771 	gtk_container_add(GTK_CONTAINER(main_menu_submenu), menu_bulk_replace);
772 
773 	ui_add_document_sensitive(main_menu);
774 	gtk_widget_show(menu_bulk_replace);
775 	gtk_widget_show(menu_htmltoggle);
776 
777 	main_menu_item = menu_item;
778 
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);
790 }
791 
792 
793 /* Destroy widgets */
plugin_cleanup(void)794 void plugin_cleanup(void)
795 {
796 	gtk_widget_destroy(main_menu_item);
797 	gtk_widget_destroy(main_menu);
798 
799 	if (sc_dialog != NULL)
800 		gtk_widget_destroy(sc_dialog);
801 
802 	if (config_file != NULL)
803 		g_free(config_file);
804 }
805