2 * htmlchars.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2009-2010 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
5 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
6 * Copyright 2007-2010 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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
26 /* HTML Characters plugin (Inserts HTML character entities like '&') */
28 #include "geanyplugin.h"
32 GeanyData
*geany_data
;
33 GeanyFunctions
*geany_functions
;
36 PLUGIN_VERSION_CHECK(GEANY_API_VERSION
)
38 PLUGIN_SET_INFO(_("HTML Characters"), _("Inserts HTML character entities like '&'."), VERSION
,
39 _("The Geany developer team"))
46 KB_REPLACE_HTML_ENTITIES
,
51 PLUGIN_KEY_GROUP(html_chars
, KB_COUNT
)
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 const gchar
*chars
[][2] ={
72 { N_("HTML characters"), NULL
},
78 { N_("ISO 8859-1 characters"), NULL
},
176 { N_("Greek characters"), NULL
},
186 { "Ε", "Ε" },
187 { "ε", "ε" },
206 { "Ο", "Ο" },
207 { "ο", "ο" },
217 { "Υ", "Υ" },
218 { "υ", "υ" },
227 { "ϑ", "ϑ" },
231 { N_("Mathematical characters"), NULL
},
272 { N_("Technical characters"), NULL
},
280 { N_("Arrow characters"), NULL
},
293 { N_("Punctuation characters"), NULL
},
309 { N_("Miscellaneous characters"), NULL
},
320 { "ℵ", "ℵ" },
333 static gboolean
ht_editor_notify_cb(GObject
*object
, GeanyEditor
*editor
,
334 SCNotification
*nt
, gpointer data
);
337 PluginCallback plugin_callbacks
[] =
339 { "editor-notify", (GCallback
) &ht_editor_notify_cb
, FALSE
, NULL
},
340 { NULL
, NULL
, FALSE
, NULL
}
344 /* Functions to toggle the status of plugin */
345 void set_status(gboolean new_status
)
347 /* No more function at the moment.*/
348 if (plugin_active
!= new_status
)
349 plugin_active
= new_status
;
353 static void toggle_status(G_GNUC_UNUSED GtkMenuItem
* menuitem
)
355 if (plugin_active
== TRUE
)
362 static void sc_on_tools_show_dialog_insert_special_chars_response
363 (GtkDialog
*dialog
, gint response
, gpointer user_data
);
364 static void sc_on_tree_row_activated
365 (GtkTreeView
*treeview
, GtkTreePath
*path
, GtkTreeViewColumn
*col
, gpointer user_data
);
366 static void sc_fill_store(GtkTreeStore
*store
);
367 static gboolean
sc_insert(GtkTreeModel
*model
, GtkTreeIter
*iter
);
370 /* Function takes over value of key which was pressed and returns
371 * HTML/SGML entity if any */
372 const gchar
*get_entity(gchar
*letter
)
376 len
= G_N_ELEMENTS(chars
);
378 /* Ignore tags marking caracters as well as spaces*/
379 for (i
= 7; i
< len
; i
++)
381 if (utils_str_equal(chars
[i
][0], letter
) &&
382 !utils_str_equal(" ", letter
))
388 /* if the char is not in the list */
393 static gboolean
ht_editor_notify_cb(GObject
*object
, GeanyEditor
*editor
,
394 SCNotification
*nt
, gpointer data
)
396 g_return_val_if_fail(editor
!= NULL
, FALSE
);
398 if (plugin_active
!= TRUE
)
401 if (nt
->nmhdr
.code
== SCN_CHARADDED
)
406 len
= g_unichar_to_utf8(nt
->ch
, buf
);
411 entity
= get_entity(buf
);
415 gint pos
= sci_get_current_position(editor
->sci
);
417 sci_set_selection_start(editor
->sci
, pos
- len
);
418 sci_set_selection_end(editor
->sci
, pos
);
420 sci_replace_sel(editor
->sci
, entity
);
429 /* Called when keys were pressed */
430 static void kbhtmltoggle_toggle(G_GNUC_UNUSED guint key_id
)
432 if (plugin_active
== TRUE
)
439 static void tools_show_dialog_insert_special_chars(void)
441 if (sc_dialog
== NULL
)
444 GtkCellRenderer
*renderer
;
445 GtkTreeViewColumn
*column
;
446 GtkWidget
*swin
, *vbox
, *label
;
448 sc_dialog
= gtk_dialog_new_with_buttons(
449 _("Special Characters"), GTK_WINDOW(geany
->main_widgets
->window
),
450 GTK_DIALOG_DESTROY_WITH_PARENT
, GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
451 _("_Insert"), GTK_RESPONSE_OK
, NULL
);
452 vbox
= ui_dialog_vbox_new(GTK_DIALOG(sc_dialog
));
453 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
454 gtk_widget_set_name(sc_dialog
, "GeanyDialog");
456 height
= GEANY_DEFAULT_DIALOG_HEIGHT
;
457 gtk_window_set_default_size(GTK_WINDOW(sc_dialog
), height
* 8 / 10, height
);
458 gtk_dialog_set_default_response(GTK_DIALOG(sc_dialog
), GTK_RESPONSE_CANCEL
);
460 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."));
461 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
462 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
463 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, FALSE
, 0);
465 sc_tree
= GTK_TREE_VIEW(gtk_tree_view_new());
467 sc_store
= gtk_tree_store_new(N_COLUMNS
, G_TYPE_STRING
, G_TYPE_STRING
);
468 gtk_tree_view_set_model(GTK_TREE_VIEW(sc_tree
),
469 GTK_TREE_MODEL(sc_store
));
470 g_object_unref(sc_store
);
472 renderer
= gtk_cell_renderer_text_new();
473 column
= gtk_tree_view_column_new_with_attributes(
474 _("Character"), renderer
, "text", COLUMN_CHARACTER
, NULL
);
475 gtk_tree_view_column_set_resizable(column
, TRUE
);
476 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree
), column
);
478 renderer
= gtk_cell_renderer_text_new();
479 column
= gtk_tree_view_column_new_with_attributes(
480 _("HTML (name)"), renderer
, "text", COLUMN_HTML_NAME
, NULL
);
481 gtk_tree_view_column_set_resizable(column
, TRUE
);
482 gtk_tree_view_append_column(GTK_TREE_VIEW(sc_tree
), column
);
484 swin
= gtk_scrolled_window_new(NULL
, NULL
);
485 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
), GTK_POLICY_AUTOMATIC
,
486 GTK_POLICY_AUTOMATIC
);
487 gtk_scrolled_window_add_with_viewport(
488 GTK_SCROLLED_WINDOW(swin
), GTK_WIDGET(sc_tree
));
490 gtk_box_pack_start(GTK_BOX(vbox
), swin
, TRUE
, TRUE
, 0);
492 g_signal_connect(sc_tree
, "row-activated", G_CALLBACK(sc_on_tree_row_activated
), NULL
);
494 g_signal_connect(sc_dialog
, "response",
495 G_CALLBACK(sc_on_tools_show_dialog_insert_special_chars_response
), NULL
);
497 g_signal_connect(sc_dialog
, "delete-event", G_CALLBACK(gtk_widget_hide_on_delete
), NULL
);
499 sc_fill_store(sc_store
);
501 /*gtk_tree_view_expand_all(special_characters_tree);*/
502 gtk_tree_view_set_search_column(sc_tree
, COLUMN_HTML_NAME
);
504 gtk_widget_show_all(sc_dialog
);
508 /* fill the tree model with data
509 ** TODO move this in a file and make it extendable for more data types */
510 static void sc_fill_store(GtkTreeStore
*store
)
513 GtkTreeIter
*parent_iter
= NULL
;
516 len
= G_N_ELEMENTS(chars
);
517 for (i
= 0; i
< len
; i
++)
519 if (chars
[i
][1] == NULL
)
520 { /* add a category */
521 gtk_tree_store_append(store
, &iter
, NULL
);
522 gtk_tree_store_set(store
, &iter
, COLUMN_CHARACTER
, chars
[i
][0], -1);
523 if (parent_iter
!= NULL
) gtk_tree_iter_free(parent_iter
);
524 parent_iter
= gtk_tree_iter_copy(&iter
);
527 { /* add child to parent_iter */
528 gtk_tree_store_append(store
, &iter
, parent_iter
);
529 gtk_tree_store_set(store
, &iter
, COLUMN_CHARACTER
, chars
[i
][0],
530 COLUMN_HTML_NAME
, chars
[i
][1], -1);
536 /* just inserts the HTML_NAME coloumn of the selected row at current position
537 * returns only TRUE if a valid selection(i.e. no category) could be found */
538 static gboolean
sc_insert(GtkTreeModel
*model
, GtkTreeIter
*iter
)
540 GeanyDocument
*doc
= document_get_current();
541 gboolean result
= FALSE
;
546 gint pos
= sci_get_current_position(doc
->editor
->sci
);
548 gtk_tree_model_get(model
, iter
, COLUMN_HTML_NAME
, &str
, -1);
551 sci_insert_text(doc
->editor
->sci
, pos
, str
);
560 static void sc_on_tools_show_dialog_insert_special_chars_response(GtkDialog
*dialog
, gint response
,
563 if (response
== GTK_RESPONSE_OK
)
565 GtkTreeSelection
*selection
;
569 selection
= gtk_tree_view_get_selection(sc_tree
);
571 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
573 /* only hide dialog if selection was not a category */
574 if (sc_insert(model
, &iter
))
575 gtk_widget_hide(GTK_WIDGET(dialog
));
579 gtk_widget_hide(GTK_WIDGET(dialog
));
583 static void sc_on_tree_row_activated(GtkTreeView
*treeview
, GtkTreePath
*path
,
584 GtkTreeViewColumn
*col
, gpointer user_data
)
587 GtkTreeModel
*model
= GTK_TREE_MODEL(sc_store
);
589 if (gtk_tree_model_get_iter(model
, &iter
, path
))
591 /* only hide dialog if selection was not a category */
592 if (sc_insert(model
, &iter
))
593 gtk_widget_hide(sc_dialog
);
595 { /* double click on a category to toggle the expand or collapse it */
596 if (gtk_tree_view_row_expanded(sc_tree
, path
))
597 gtk_tree_view_collapse_row(sc_tree
, path
);
599 gtk_tree_view_expand_row(sc_tree
, path
, FALSE
);
605 static void replace_special_character()
607 GeanyDocument
*doc
= NULL
;
608 doc
= document_get_current();
610 if (doc
!= NULL
&& sci_has_selection(doc
->editor
->sci
))
614 GString
*replacement
= g_string_new(NULL
);
617 const gchar
*entity
= NULL
;
621 selection
= sci_get_selection_contents(doc
->editor
->sci
);
623 selection_len
= strlen(selection
);
624 for (i
= 0; i
< selection_len
; i
++)
626 len
= g_unichar_to_utf8(g_utf8_get_char(selection
+ i
), buf
);
630 entity
= get_entity(buf
);
634 replacement
= g_string_append(replacement
, entity
);
638 replacement
= g_string_append(replacement
, buf
);
641 new = g_string_free(replacement
, FALSE
);
642 sci_replace_sel(doc
->editor
->sci
, new);
649 /* Callback for special chars menu */
651 item_activate(GtkMenuItem
*menuitem
, gpointer gdata
)
653 tools_show_dialog_insert_special_chars();
657 static void kb_activate(G_GNUC_UNUSED guint key_id
)
659 item_activate(NULL
, NULL
);
663 /* Callback for bulk replacement of selected text */
665 replace_special_character_activated(GtkMenuItem
*menuitem
, gpointer gdata
)
667 replace_special_character();
671 static void kb_special_chars_replacement(G_GNUC_UNUSED guint key_id
)
673 replace_special_character();
677 /* Called by Geany to initialize the plugin */
678 void plugin_init(GeanyData
*data
)
680 GtkWidget
*menu_item
;
681 const gchar
*menu_text
= _("_Insert Special HTML Characters");
683 /* Add an item to the Tools menu for html chars dialog*/
684 menu_item
= gtk_menu_item_new_with_mnemonic(menu_text
);
685 gtk_widget_show(menu_item
);
686 gtk_container_add(GTK_CONTAINER(geany
->main_widgets
->tools_menu
), menu_item
);
687 g_signal_connect(menu_item
, "activate", G_CALLBACK(item_activate
), NULL
);
689 /* disable menu_item when there are no documents open */
690 ui_add_document_sensitive(menu_item
);
692 /* Add menuitem for html replacement functions*/
693 main_menu
= gtk_menu_item_new_with_mnemonic(_("HTML Replacement"));
694 gtk_widget_show_all(main_menu
);
695 gtk_container_add(GTK_CONTAINER(geany
->main_widgets
->tools_menu
), main_menu
);
697 main_menu_submenu
= gtk_menu_new();
698 gtk_menu_item_set_submenu(GTK_MENU_ITEM(main_menu
), main_menu_submenu
);
700 menu_htmltoggle
= gtk_check_menu_item_new_with_mnemonic(_("_HTMLToggle"));
701 gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(menu_htmltoggle
),
703 gtk_container_add(GTK_CONTAINER(main_menu_submenu
), menu_htmltoggle
);
705 g_signal_connect((gpointer
) menu_htmltoggle
, "activate",
706 G_CALLBACK(toggle_status
), NULL
);
708 menu_bulk_replace
= gtk_menu_item_new_with_mnemonic(
709 _("Bulk replacement of special chars"));
710 g_signal_connect((gpointer
) menu_bulk_replace
, "activate",
711 G_CALLBACK(replace_special_character_activated
), NULL
);
713 gtk_container_add(GTK_CONTAINER(main_menu_submenu
), menu_bulk_replace
);
715 ui_add_document_sensitive(main_menu
);
716 gtk_widget_show(menu_bulk_replace
);
717 gtk_widget_show(menu_htmltoggle
);
719 main_menu_item
= menu_item
;
721 /* setup keybindings */
722 keybindings_set_item(plugin_key_group
, KB_INSERT_HTML_CHARS
,
723 kb_activate
, 0, 0, "insert_html_chars",
724 _("Insert Special HTML Characters"), menu_item
);
725 keybindings_set_item(plugin_key_group
, KB_REPLACE_HTML_ENTITIES
,
726 kb_special_chars_replacement
, 0, 0, "replace_special_characters",
727 _("Replace special characters"), NULL
);
728 keybindings_set_item(plugin_key_group
, KB_HTMLTOGGLE_ACTIVE
,
729 kbhtmltoggle_toggle
, 0, 0, "htmltoogle_toggle_plugin_status",
730 _("Toggle plugin status"), menu_htmltoggle
);
734 /* Destroy widgets */
735 void plugin_cleanup(void)
737 gtk_widget_destroy(main_menu_item
);
738 gtk_widget_destroy(main_menu
);
740 if (sc_dialog
!= NULL
)
741 gtk_widget_destroy(sc_dialog
);