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 '&') */
27 #include "geanyplugin.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 '&'."),
39 PACKAGE_VERSION
, _("The Geany developer team"))
46 KB_REPLACE_HTML_ENTITIES
,
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
},
79 { N_("ISO 8859-1 characters"), NULL
},
177 { N_("Greek characters"), NULL
},
187 { "Ε", "Ε" },
188 { "ε", "ε" },
207 { "Ο", "Ο" },
208 { "ο", "ο" },
218 { "Υ", "Υ" },
219 { "υ", "υ" },
228 { "ϑ", "ϑ" },
232 { N_("Mathematical characters"), NULL
},
273 { N_("Technical characters"), NULL
},
281 { N_("Arrow characters"), NULL
},
294 { N_("Punctuation characters"), NULL
},
310 { N_("Miscellaneous characters"), NULL
},
321 { "ℵ", "ℵ" },
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();
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",
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."));
369 /* write config to file */
370 data
= g_key_file_to_data(config
, NULL
, NULL
);
371 utils_write_file(config_file
, data
);
375 g_key_file_free(config
);
380 static void toggle_status(G_GNUC_UNUSED GtkMenuItem
* menuitem
)
382 if (plugin_active
== 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
)
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
))
415 /* if the char is not in the list */
420 static gboolean
ht_editor_notify_cb(GObject
*object
, GeanyEditor
*editor
,
421 SCNotification
*nt
, gpointer data
)
425 g_return_val_if_fail(editor
!= NULL
, FALSE
);
430 lexer
= sci_get_lexer(editor
->sci
);
431 if (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
)
434 if (nt
->nmhdr
.code
== SCN_CHARADDED
)
439 len
= g_unichar_to_utf8(nt
->ch
, buf
);
444 entity
= get_entity(buf
);
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
);
462 /* Called when keys were pressed */
463 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id
)
465 if (plugin_active
== TRUE
)
476 static void tools_show_dialog_insert_special_chars(void)
478 if (sc_dialog
== NULL
)
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
)
550 GtkTreeIter
*parent_iter
= NULL
;
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
);
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
;
583 gint pos
= sci_get_current_position(doc
->editor
->sci
);
585 gtk_tree_model_get(model
, iter
, COLUMN_HTML_NAME
, &str
, -1);
588 sci_insert_text(doc
->editor
->sci
, pos
, str
);
597 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog
*dialog
, gint response
,
600 if (response
== GTK_RESPONSE_OK
)
602 GtkTreeSelection
*selection
;
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
));
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
)
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
);
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
);
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
))
651 GString
*replacement
= g_string_new(NULL
);
654 const gchar
*entity
= NULL
;
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
;
667 entity
= get_entity(buf
);
671 replacement
= g_string_append(replacement
, entity
);
675 replacement
= g_string_append(replacement
, buf
);
678 new = g_string_free(replacement
, FALSE
);
679 sci_replace_sel(doc
->editor
->sci
, new);
686 /* Callback for special chars menu */
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 */
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
),
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
)