Merge pull request #3572 from techee/document_before_save_as
[geany-mirror.git] / src / symbols.c
blob591159dc6b7774d0680a5b2b5c07f4da55de968f
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006 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 /**
22 * @file symbols.h
23 * Tag-related functions.
24 **/
27 * Symbol Tree and TagManager-related convenience functions.
28 * TagManager parses tags for each document, and also adds them to the workspace (session).
29 * Global tags are lists of tags for each filetype, loaded when a document with a
30 * matching filetype is first loaded.
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
37 #include "symbols.h"
39 #include "app.h"
40 #include "callbacks.h" /* FIXME: for ignore_callback */
41 #include "dialogs.h"
42 #include "documentprivate.h"
43 #include "editor.h"
44 #include "encodings.h"
45 #include "filetypesprivate.h"
46 #include "geanyobject.h"
47 #include "highlighting.h"
48 #include "main.h"
49 #include "navqueue.h"
50 #include "sciwrappers.h"
51 #include "sidebar.h"
52 #include "support.h"
53 #include "tm_parser.h"
54 #include "tm_tag.h"
55 #include "tm_ctags.h"
56 #include "ui_utils.h"
57 #include "utils.h"
59 #include "SciLexer.h"
61 #include <ctype.h>
62 #include <string.h>
63 #include <stdlib.h>
64 #include <gtk/gtk.h>
67 typedef struct
69 gint found_line; /* return: the nearest line found */
70 gint line; /* input: the line to look for */
71 gboolean lower /* input: search only for lines with lower number than @line */;
72 } TreeSearchData;
75 static GPtrArray *top_level_iter_names = NULL;
78 static struct
80 const gchar *icon_name;
81 GdkPixbuf *pixbuf;
83 /* keep in sync with enum in tm_parser.h */
84 symbols_icons[TM_N_ICONS] = {
85 [TM_ICON_CLASS] = { "classviewer-class", NULL },
86 [TM_ICON_MACRO] = { "classviewer-macro", NULL },
87 [TM_ICON_MEMBER] = { "classviewer-member", NULL },
88 [TM_ICON_METHOD] = { "classviewer-method", NULL },
89 [TM_ICON_NAMESPACE] = { "classviewer-namespace", NULL },
90 [TM_ICON_OTHER] = { "classviewer-other", NULL },
91 [TM_ICON_STRUCT] = { "classviewer-struct", NULL },
92 [TM_ICON_VAR] = { "classviewer-var", NULL },
95 static struct
97 GtkWidget *expand_all;
98 GtkWidget *collapse_all;
99 GtkWidget *sort_by_name;
100 GtkWidget *sort_by_appearance;
101 GtkWidget *find_usage;
102 GtkWidget *find_doc_usage;
103 GtkWidget *find_in_files;
104 GtkWidget *group_by_type;
106 symbol_menu;
108 static void load_user_tags(GeanyFiletypeID ft_id);
110 /* get the tags_ignore list, exported by geany_lcpp.c */
111 extern gchar **c_tags_ignore;
113 /* ignore certain tokens when parsing C-like syntax.
114 * Also works for reloading. */
115 static void load_c_ignore_tags(void)
117 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
118 gchar *content;
120 if (g_file_get_contents(path, &content, NULL, NULL))
122 gchar **line;
124 /* historically we ignore the glib _DECLS for tag generation */
125 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
127 g_strfreev(c_tags_ignore);
128 tm_ctags_clear_ignore_symbols();
130 /* for old c.c parser */
131 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
132 /* for new cxx parser */
133 foreach_strv(line, c_tags_ignore)
135 tm_ctags_add_ignore_symbol(*line);
138 g_free(content);
140 g_free(path);
144 void symbols_reload_config_files(void)
146 load_c_ignore_tags();
150 static gsize get_tag_count(void)
152 GPtrArray *tags = tm_get_workspace()->global_tags;
153 gsize count = tags ? tags->len : 0;
155 return count;
159 /* wrapper for tm_workspace_load_global_tags().
160 * note that the tag count only counts new global tags added - if a tag has the same name,
161 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
162 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
164 gboolean result;
165 gsize old_tag_count = get_tag_count();
167 result = tm_workspace_load_global_tags(tags_file, ft->lang);
168 if (result)
170 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file, ft->name,
171 (guint) (get_tag_count() - old_tag_count));
173 return result;
177 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
178 * This provides autocompletion, calltips, etc. */
179 void symbols_global_tags_loaded(guint file_type_idx)
181 /* load ignore list for C/C++ parser */
182 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
183 c_tags_ignore == NULL)
185 load_c_ignore_tags();
188 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
189 return;
191 /* load config in case of custom filetypes */
192 filetypes_load_config(file_type_idx, FALSE);
194 load_user_tags(file_type_idx);
196 switch (file_type_idx)
198 case GEANY_FILETYPES_CPP:
199 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
200 break;
201 case GEANY_FILETYPES_PHP:
202 symbols_global_tags_loaded(GEANY_FILETYPES_HTML); /* load HTML global tags */
203 break;
208 GString *symbols_find_typenames_as_string(TMParserType lang, gboolean global)
210 guint j;
211 TMTag *tag;
212 GString *s = NULL;
213 GPtrArray *typedefs;
214 TMParserType tag_lang;
216 if (global)
217 typedefs = app->tm_workspace->global_typename_array;
218 else
219 typedefs = app->tm_workspace->typename_array;
221 if ((typedefs) && (typedefs->len > 0))
223 const gchar *last_name = "";
225 s = g_string_sized_new(typedefs->len * 10);
226 for (j = 0; j < typedefs->len; ++j)
228 tag = TM_TAG(typedefs->pdata[j]);
229 tag_lang = tag->lang;
231 if (tag->name && tm_parser_langs_compatible(lang, tag_lang) &&
232 strcmp(tag->name, last_name) != 0)
234 if (j != 0)
235 g_string_append_c(s, ' ');
236 g_string_append(s, tag->name);
237 last_name = tag->name;
241 return s;
245 /** Gets the context separator used by the tag manager for a particular file
246 * type.
247 * @param ft_id File type identifier.
248 * @return The context separator string.
250 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
251 * without a context separator.
253 * @since 0.19
255 GEANY_API_SYMBOL
256 const gchar *symbols_get_context_separator(gint ft_id)
258 return tm_parser_scope_separator(filetypes[ft_id]->lang);
262 /* sort by name, then line */
263 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
265 gint ret;
267 if (tag_a == NULL || tag_b == NULL)
268 return 0;
270 if (tag_a->name == NULL)
271 return -(tag_a->name != tag_b->name);
273 if (tag_b->name == NULL)
274 return tag_a->name != tag_b->name;
276 ret = strcmp(tag_a->name, tag_b->name);
277 if (ret == 0)
279 return tag_a->line - tag_b->line;
281 return ret;
285 /* sort by line, then scope */
286 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
288 const TMTag *tag_a = TM_TAG(a);
289 const TMTag *tag_b = TM_TAG(b);
290 gint ret;
292 if (a == NULL || b == NULL)
293 return 0;
295 ret = tag_a->line - tag_b->line;
296 if (ret == 0)
298 if (tag_a->scope == NULL)
299 return -(tag_a->scope != tag_b->scope);
300 if (tag_b->scope == NULL)
301 return tag_a->scope != tag_b->scope;
302 else
303 return strcmp(tag_a->scope, tag_b->scope);
305 return ret;
309 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
311 GList *tag_names = NULL;
312 guint i;
313 gchar **tf_strv;
315 g_return_val_if_fail(doc, NULL);
317 if (! doc->tm_file || ! doc->tm_file->tags_array)
318 return NULL;
320 tf_strv = g_strsplit_set(doc->priv->tag_filter, " ", -1);
322 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
324 TMTag *tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
326 if (tag->type & tag_types)
328 gboolean filtered = FALSE;
329 gchar **val;
330 gchar *full_tagname = g_strconcat(tag->scope ? tag->scope : "",
331 tag->scope ? tm_parser_scope_separator_printable(tag->lang) : "",
332 tag->name, NULL);
333 gchar *normalized_tagname = g_utf8_normalize(full_tagname, -1, G_NORMALIZE_ALL);
335 foreach_strv(val, tf_strv)
337 gchar *normalized_val = g_utf8_normalize(*val, -1, G_NORMALIZE_ALL);
339 if (normalized_tagname != NULL && normalized_val != NULL)
341 gchar *case_normalized_tagname = g_utf8_casefold(normalized_tagname, -1);
342 gchar *case_normalized_val = g_utf8_casefold(normalized_val, -1);
344 filtered = strstr(case_normalized_tagname, case_normalized_val) == NULL;
345 g_free(case_normalized_tagname);
346 g_free(case_normalized_val);
348 g_free(normalized_val);
350 if (filtered)
351 break;
353 if (!filtered)
354 tag_names = g_list_prepend(tag_names, tag);
356 g_free(normalized_tagname);
357 g_free(full_tagname);
360 tag_names = g_list_sort(tag_names, compare_symbol_lines);
362 g_strfreev(tf_strv);
364 return tag_names;
368 /* amount of types in the symbol list - can be increased if needed */
369 #define MAX_SYMBOL_TYPES 15
371 GtkTreeIter tv_iters[MAX_SYMBOL_TYPES];
374 static void init_tag_iters(void)
376 guint i;
377 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
378 * filetypes(e.g. config file to Python crashes Geany without this) */
379 for (i = 0; i < MAX_SYMBOL_TYPES; i++)
380 tv_iters[i].stamp = -1;
384 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
386 static GtkIconTheme *icon_theme = NULL;
387 static gint x = -1;
389 if (G_UNLIKELY(x < 0))
391 gint dummy;
392 icon_theme = gtk_icon_theme_get_default();
393 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
395 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
399 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
401 GtkTreeModel *model = GTK_TREE_MODEL(store);
403 if (!gtk_tree_model_get_iter_first(model, iter))
404 return FALSE;
407 gchar *candidate;
409 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
410 /* FIXME: what if 2 different items have the same name?
411 * this should never happen, but might be caused by a typo in a translation */
412 if (utils_str_equal(candidate, title))
414 g_free(candidate);
415 return TRUE;
417 else
418 g_free(candidate);
420 while (gtk_tree_model_iter_next(model, iter));
422 return FALSE;
426 static void tag_list_add_groups(GtkTreeStore *tree_store, TMParserType lang)
428 const gchar *title;
429 guint i;
430 guint icon_id;
432 g_return_if_fail(top_level_iter_names);
434 for (i = 0; (title = tm_parser_get_sidebar_info(lang, i, &icon_id)) != NULL; i++)
436 GtkTreeIter *iter = &tv_iters[i];
437 GdkPixbuf *icon = NULL;
439 if (icon_id < TM_N_ICONS)
440 icon = symbols_icons[icon_id].pixbuf;
442 g_assert(title != NULL);
443 g_ptr_array_add(top_level_iter_names, (gchar *)title);
445 if (!find_toplevel_iter(tree_store, iter, title))
446 gtk_tree_store_append(tree_store, iter, NULL);
448 if (icon)
449 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
450 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
455 static void add_top_level_items(GeanyDocument *doc)
457 TMParserType lang = doc->file_type->lang;
458 GtkTreeStore *tag_store = doc->priv->tag_store;
460 if (top_level_iter_names == NULL)
461 top_level_iter_names = g_ptr_array_new();
462 else
463 g_ptr_array_set_size(top_level_iter_names, 0);
465 init_tag_iters();
467 tag_list_add_groups(tag_store, lang);
471 /* removes toplevel items that have no children */
472 static void hide_empty_rows(GtkTreeStore *store)
474 GtkTreeIter iter;
475 gboolean cont = TRUE;
477 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
478 return; /* stop when first iter is invalid, i.e. no elements */
480 while (cont)
482 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
483 cont = gtk_tree_store_remove(store, &iter);
484 else
485 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
490 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean include_scope,
491 gboolean include_line)
493 gchar *utf8_name;
494 const gchar *scope = tag->scope;
495 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
496 gboolean doc_is_utf8 = FALSE;
498 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
499 * for None at this point completely */
500 if (utils_str_equal(doc->encoding, "UTF-8") ||
501 utils_str_equal(doc->encoding, "None"))
502 doc_is_utf8 = TRUE;
503 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
504 * plugin might have called tm_source_file_update(), so check to be sure */
505 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
507 if (! doc_is_utf8)
508 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
509 -1, doc->encoding, TRUE);
510 else
511 utf8_name = tag->name;
513 if (utf8_name == NULL)
514 return NULL;
516 if (! buffer)
517 buffer = g_string_new(NULL);
518 else
519 g_string_truncate(buffer, 0);
521 /* check first char of scope is a wordchar */
522 if (include_scope && scope &&
523 strpbrk(scope, GEANY_WORDCHARS) == scope)
525 const gchar *sep = tm_parser_scope_separator_printable(tag->lang);
527 g_string_append(buffer, scope);
528 g_string_append(buffer, sep);
530 g_string_append(buffer, utf8_name);
532 if (! doc_is_utf8)
533 g_free(utf8_name);
535 if (include_line)
536 g_string_append_printf(buffer, " [%lu]", tag->line);
538 return buffer->str;
542 // Returns NULL if the tag is not a variable or callable
543 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag, gboolean include_scope)
545 gchar *utf8_name = tm_parser_format_function(tag->lang, tag->name,
546 tag->arglist, tag->var_type, tag->scope);
548 if (!utf8_name && tag->var_type &&
549 tag->type & (tm_tag_field_t | tm_tag_member_t | tm_tag_variable_t | tm_tag_externvar_t))
551 gchar *scope = include_scope ? tag->scope : NULL;
552 utf8_name = tm_parser_format_variable(tag->lang, tag->name, tag->var_type, scope);
555 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
556 * for None at this point completely */
557 if (utf8_name != NULL &&
558 ! utils_str_equal(doc->encoding, "UTF-8") &&
559 ! utils_str_equal(doc->encoding, "None"))
561 SETPTR(utf8_name,
562 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
565 return utf8_name;
569 static const gchar *get_parent_name(const TMTag *tag)
571 return !EMPTY(tag->scope) ? tag->scope : NULL;
575 static GtkTreeIter *get_tag_type_iter(TMParserType lang, TMTagType tag_type)
577 /* TODO: tm_parser_get_sidebar_group() goes through groups one by one.
578 * If this happens to be slow for tree construction, create a lookup
579 * table for them. */
580 gint group = tm_parser_get_sidebar_group(lang, tag_type);
582 if (group < 0)
583 return NULL;
585 return &tv_iters[group];
589 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
591 GdkPixbuf *icon = NULL;
593 /* copy parent icon */
594 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
595 SYMBOLS_COLUMN_ICON, &icon, -1);
596 return icon;
600 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
602 const TMTag *t1 = v1;
603 const TMTag *t2 = v2;
605 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
606 utils_str_equal(t1->scope, t2->scope) &&
607 /* include arglist in match to support e.g. C++ overloading */
608 utils_str_equal(t1->arglist, t2->arglist));
612 /* inspired from g_str_hash() */
613 static guint tag_hash(gconstpointer v)
615 const TMTag *tag = v;
616 const gchar *p;
617 guint32 h = 5381;
619 h = (h << 5) + h + tag->type;
620 for (p = tag->name; *p != '\0'; p++)
621 h = (h << 5) + h + *p;
622 if (tag->scope)
624 for (p = tag->scope; *p != '\0'; p++)
625 h = (h << 5) + h + *p;
627 /* for e.g. C++ overloading */
628 if (tag->arglist)
630 for (p = tag->arglist; *p != '\0'; p++)
631 h = (h << 5) + h + *p;
634 return h;
638 /* like gtk_tree_view_expand_to_path() but with an iter */
639 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
641 GtkTreeModel *model = gtk_tree_view_get_model(view);
642 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
644 gtk_tree_view_expand_to_path(view, path);
645 gtk_tree_path_free(path);
649 /* like gtk_tree_store_remove() but finds the next iter at any level */
650 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
652 GtkTreeIter parent;
653 gboolean has_parent;
654 gboolean cont;
656 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
657 cont = gtk_tree_store_remove(store, iter);
658 /* if there is no next at this level but there is a parent iter, continue from it */
659 if (! cont && has_parent)
661 *iter = parent;
662 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
665 return cont;
669 static gint tree_search_func(gconstpointer key, gpointer user_data)
671 TreeSearchData *data = user_data;
672 gint parent_line = GPOINTER_TO_INT(key);
673 gboolean new_nearest;
675 if (data->found_line == -1)
676 data->found_line = parent_line; /* initial value */
678 new_nearest = ABS(data->line - parent_line) < ABS(data->line - data->found_line);
680 if (parent_line > data->line)
682 if (new_nearest && !data->lower)
683 data->found_line = parent_line;
684 return -1;
687 if (new_nearest)
688 data->found_line = parent_line;
690 if (parent_line < data->line)
691 return 1;
693 return 0;
697 static gint tree_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
699 return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
703 static void parents_table_tree_value_free(gpointer data)
705 g_slice_free(GtkTreeIter, data);
709 /* adds a new element in the parent table if its key is known. */
710 static void update_parents_table(GHashTable *table, const TMTag *tag, const GtkTreeIter *iter)
712 const gchar *name;
713 gchar *name_free = NULL;
714 GTree *tree;
716 if (EMPTY(tag->scope))
718 /* simple case, just use the tag name */
719 name = tag->name;
721 else if (! tm_parser_has_full_scope(tag->lang))
723 /* if the parser doesn't use fully qualified scope, use the name alone but
724 * prevent Foo::Foo from making parent = child */
725 if (utils_str_equal(tag->scope, tag->name))
726 name = NULL;
727 else
728 name = tag->name;
730 else
732 /* build the fully qualified scope as get_parent_name() would return it for a child tag */
733 name_free = g_strconcat(tag->scope, tm_parser_scope_separator(tag->lang), tag->name, NULL);
734 name = name_free;
737 if (name && g_hash_table_lookup_extended(table, name, NULL, (gpointer *) &tree))
739 if (!tree)
741 tree = g_tree_new_full(tree_cmp, NULL, NULL, parents_table_tree_value_free);
742 g_hash_table_insert(table, name_free ? name_free : g_strdup(name), tree);
743 name_free = NULL;
746 g_tree_insert(tree, GINT_TO_POINTER(tag->line), g_slice_dup(GtkTreeIter, iter));
749 g_free(name_free);
753 static GtkTreeIter *parents_table_lookup(GHashTable *table, const gchar *name, guint line)
755 GtkTreeIter *parent_search = NULL;
756 GTree *tree;
758 tree = g_hash_table_lookup(table, name);
759 if (tree)
761 TreeSearchData user_data = {-1, line, TRUE};
763 /* search parent candidates for the one with the nearest
764 * line number which is lower than the tag's line number */
765 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
766 parent_search = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
769 return parent_search;
773 static void parents_table_value_free(gpointer data)
775 GTree *tree = data;
776 if (tree)
777 g_tree_destroy(tree);
781 /* inserts a @data in @table on key @tag.
782 * previous data is not overwritten if the key is duplicated, but rather the
783 * two values are kept in a list
785 * table is: GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
786 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
788 GTree *tree = g_hash_table_lookup(table, tag);
789 if (!tree)
791 tree = g_tree_new_full(tree_cmp, NULL, NULL, NULL);
792 g_hash_table_insert(table, tag, tree);
794 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
795 list = g_list_prepend(list, data);
796 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
800 /* looks up the entry in @table that best matches @tag.
801 * if there is more than one candidate, the one that has closest line position to @tag is chosen */
802 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
804 TreeSearchData user_data = {-1, tag->line, FALSE};
805 GTree *tree = g_hash_table_lookup(table, tag);
807 if (tree)
809 GList *list;
811 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
812 list = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
813 /* return the first value in the list - we don't care which of the
814 * tags with identical names defined on the same line we get */
815 if (list)
816 return list->data;
818 return NULL;
822 /* removes the element at @tag from @table.
823 * @tag must be the exact pointer used at insertion time */
824 static void tags_table_remove(GHashTable *table, TMTag *tag)
826 GTree *tree = g_hash_table_lookup(table, tag);
827 if (tree)
829 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
830 if (list)
832 GList *node;
833 /* should always be the first element as we returned the first one in
834 * tags_table_lookup() */
835 foreach_list(node, list)
837 if (((GList *) node->data)->data == tag)
838 break;
840 list = g_list_delete_link(list, node);
841 if (!list)
842 g_tree_remove(tree, GINT_TO_POINTER(tag->line));
843 else
844 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
850 static gboolean tags_table_tree_value_free(gpointer key, gpointer value, gpointer data)
852 GList *list = value;
853 g_list_free(list);
854 return FALSE;
858 static void tags_table_value_free(gpointer data)
860 GTree *tree = data;
861 if (tree)
863 /* free any leftover elements. note that we can't register a value_free_func when
864 * creating the tree because we only want to free it when destroying the tree,
865 * not when inserting a duplicate (we handle this manually) */
866 g_tree_foreach(tree, tags_table_tree_value_free, NULL);
867 g_tree_destroy(tree);
873 * Updates the tag tree for a document with the tags in *list.
874 * @param doc a document
875 * @param tags a pointer to a GList* holding the tags to add/update. This
876 * list may be updated, removing updated elements.
878 * The update is done in two passes:
879 * 1) walking the current tree, update tags that still exist and remove the
880 * obsolescent ones;
881 * 2) walking the remaining (non updated) tags, adds them in the list.
883 * For better performances, we use 2 hash tables:
884 * - one containing all the tags for lookup in the first pass (actually stores a
885 * reference in the tags list for removing it efficiently), avoiding list search
886 * on each tag;
887 * - the other holding "tag-name":row references for tags having children, used to
888 * lookup for a parent in both passes, avoiding tree traversal.
890 static void update_tree_tags(GeanyDocument *doc, GList **tags)
892 GtkTreeStore *store = doc->priv->tag_store;
893 GtkTreeModel *model = GTK_TREE_MODEL(store);
894 GHashTable *parents_table;
895 GHashTable *tags_table;
896 GtkTreeIter iter;
897 gboolean cont;
898 GList *item;
900 /* Build hash tables holding tags and parents */
901 /* parent table is GHashTable<tag_name, GTree<line_num, GtkTreeIter>>
902 * where tag_name might be a fully qualified name (with scope) if the language
903 * parser reports scope properly (see tm_parser_has_full_scope()). */
904 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, parents_table_value_free);
905 /* tags table is another representation of the @tags list,
906 * GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
907 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, tags_table_value_free);
908 foreach_list(item, *tags)
910 TMTag *tag = item->data;
911 const gchar *parent_name;
913 tags_table_insert(tags_table, tag, item);
915 parent_name = get_parent_name(tag);
916 if (parent_name)
917 g_hash_table_insert(parents_table, g_strdup(parent_name), NULL);
920 /* First pass, update existing rows or delete them.
921 * It is OK to delete them since we walk top down so we would remove
922 * parents before checking for their children, thus never implicitly
923 * deleting an updated child */
924 cont = gtk_tree_model_get_iter_first(model, &iter);
925 while (cont)
927 TMTag *tag;
929 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
930 if (! tag) /* most probably a toplevel, skip it */
931 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
932 else
934 GList *found_item;
936 found_item = tags_table_lookup(tags_table, tag);
937 if (! found_item) /* tag doesn't exist, remove it */
938 cont = tree_store_remove_row(store, &iter);
939 else /* tag still exist, update it */
941 const gchar *parent_name;
942 TMTag *found = found_item->data;
944 parent_name = get_parent_name(found);
945 /* if parent is unknown, ignore it */
946 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
947 parent_name = NULL;
949 if (!tm_tags_equal(tag, found))
951 const gchar *name;
952 gchar *tooltip;
954 /* only update fields that (can) have changed (name that holds line
955 * number, tooltip, and the tag itself) */
956 name = get_symbol_name(doc, found, parent_name == NULL, TRUE);
957 tooltip = get_symbol_tooltip(doc, found, FALSE);
958 gtk_tree_store_set(store, &iter,
959 SYMBOLS_COLUMN_NAME, name,
960 SYMBOLS_COLUMN_TOOLTIP, tooltip,
961 SYMBOLS_COLUMN_TAG, found,
962 -1);
963 g_free(tooltip);
966 update_parents_table(parents_table, found, &iter);
968 /* remove the updated tag from the table and list */
969 tags_table_remove(tags_table, found);
970 *tags = g_list_delete_link(*tags, found_item);
972 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
975 tm_tag_unref(tag);
979 /* Second pass, now we have a tree cleaned up from invalid rows,
980 * we simply add new ones */
981 foreach_list (item, *tags)
983 TMTag *tag = item->data;
984 GtkTreeIter *parent, *parent_group;
986 parent_group = get_tag_type_iter(tag->lang, tag->type);
987 /* tv_iters[0] is reserved for the "Symbols" group */
988 parent = ui_prefs.symbols_group_by_type ? parent_group : &tv_iters[0];
989 if (parent_group)
991 gboolean expand;
992 const gchar *name;
993 const gchar *parent_name;
994 gchar *tooltip;
995 GdkPixbuf *icon = get_child_icon(store, parent_group);
997 parent_name = get_parent_name(tag);
998 if (parent_name)
1000 GtkTreeIter *parent_search = parents_table_lookup(parents_table, parent_name, tag->line);
1002 if (parent_search)
1003 parent = parent_search;
1004 else
1005 parent_name = NULL;
1008 /* only expand to the iter if the parent was empty, otherwise we let the
1009 * folding as it was before (already expanded, or closed by the user) */
1010 expand = ! gtk_tree_model_iter_has_child(model, parent);
1012 /* insert the new element */
1013 name = get_symbol_name(doc, tag, parent_name == NULL, TRUE);
1014 tooltip = get_symbol_tooltip(doc, tag, FALSE);
1015 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1016 SYMBOLS_COLUMN_NAME, name,
1017 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1018 SYMBOLS_COLUMN_ICON, icon,
1019 SYMBOLS_COLUMN_TAG, tag,
1020 -1);
1021 g_free(tooltip);
1022 if (G_LIKELY(icon))
1023 g_object_unref(icon);
1025 update_parents_table(parents_table, tag, &iter);
1027 if (expand)
1028 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1032 g_hash_table_destroy(parents_table);
1033 g_hash_table_destroy(tags_table);
1037 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1038 * is not stable, so the order is already lost. */
1039 static gint compare_top_level_names(const gchar *a, const gchar *b)
1041 guint i;
1042 const gchar *name;
1044 /* This should never happen as it would mean that two or more top
1045 * level items have the same name but it can happen by typos in the translations. */
1046 if (utils_str_equal(a, b))
1047 return 1;
1049 foreach_ptr_array(name, i, top_level_iter_names)
1051 if (utils_str_equal(name, a))
1052 return -1;
1053 if (utils_str_equal(name, b))
1054 return 1;
1056 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1057 return 0;
1061 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1062 GtkTreeIter *iter)
1064 /* if the tag has a parent tag, it should be at depth >= 2 */
1065 return !EMPTY(tag->scope) &&
1066 gtk_tree_store_iter_depth(store, iter) == 1;
1070 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1071 gpointer user_data)
1073 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1074 TMTag *tag_a, *tag_b;
1075 gint cmp;
1077 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1078 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1080 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1081 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1082 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1083 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1085 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1086 compare_symbol_lines(tag_a, tag_b);
1088 else
1090 gchar *astr, *bstr;
1092 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1093 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1095 /* if a is toplevel, b must be also */
1096 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1098 cmp = compare_top_level_names(astr, bstr);
1100 else
1102 /* this is what g_strcmp0() does */
1103 if (! astr)
1104 cmp = -(astr != bstr);
1105 else if (! bstr)
1106 cmp = astr != bstr;
1107 else
1109 cmp = strcmp(astr, bstr);
1111 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1112 if (tag_a && tag_b)
1113 if (!sort_by_name ||
1114 (utils_str_equal(tag_a->name, tag_b->name) &&
1115 utils_str_equal(tag_a->scope, tag_b->scope)))
1116 cmp = compare_symbol_lines(tag_a, tag_b);
1119 g_free(astr);
1120 g_free(bstr);
1122 tm_tag_unref(tag_a);
1123 tm_tag_unref(tag_b);
1125 return cmp;
1129 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1131 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1132 GINT_TO_POINTER(sort_by_name), NULL);
1134 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1138 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1140 GList *tags;
1142 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1144 tags = get_tag_list(doc, ~(tm_tag_local_var_t | tm_tag_include_t));
1145 if (tags == NULL)
1146 return FALSE;
1148 if (doc->priv->symbols_group_by_type != ui_prefs.symbols_group_by_type)
1149 gtk_tree_store_clear(doc->priv->tag_store);
1151 doc->priv->symbols_group_by_type = ui_prefs.symbols_group_by_type;
1153 /* FIXME: Not sure why we detached the model here? */
1155 /* disable sorting during update because the code doesn't support correctly
1156 * models that are currently being built */
1157 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1159 /* add grandparent type iters */
1160 add_top_level_items(doc);
1162 update_tree_tags(doc, &tags);
1163 g_list_free(tags);
1165 hide_empty_rows(doc->priv->tag_store);
1167 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1168 sort_mode = doc->priv->symbol_list_sort_mode;
1170 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1171 doc->priv->symbol_list_sort_mode = sort_mode;
1173 return TRUE;
1177 /* Detects a global tags filetype from the *.lang.* language extension.
1178 * Returns NULL if there was no matching TM language. */
1179 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1181 gchar *tags_ext;
1182 gchar *shortname = utils_strdupa(utf8_filename);
1183 GeanyFiletype *ft = NULL;
1185 tags_ext = g_strrstr(shortname, ".tags");
1186 if (tags_ext)
1188 *tags_ext = '\0'; /* remove .tags extension */
1189 ft = filetypes_detect_from_extension(shortname);
1190 if (ft->id != GEANY_FILETYPES_NONE)
1191 return ft;
1193 return NULL;
1197 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1198 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1199 * the relevant path.
1200 * Example:
1201 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1202 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1204 /* -E pre-process, -dD output user macros, -p prof info (?) */
1205 const char pre_process[] = "gcc -E -dD -p -I.";
1207 if (argc > 2)
1209 /* Create global taglist */
1210 int status;
1211 char *command;
1212 const char *tags_file = argv[1];
1213 char *utf8_fname;
1214 GeanyFiletype *ft;
1216 utf8_fname = utils_get_utf8_from_locale(tags_file);
1217 ft = detect_global_tags_filetype(utf8_fname);
1218 g_free(utf8_fname);
1220 if (ft == NULL)
1222 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1223 return 1;
1225 /* load config in case of custom filetypes */
1226 filetypes_load_config(ft->id, FALSE);
1228 /* load ignore list for C/C++ parser */
1229 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1230 load_c_ignore_tags();
1232 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1234 const gchar *cflags = getenv("CFLAGS");
1235 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1237 else
1238 command = NULL; /* don't preprocess */
1240 geany_debug("Generating %s tags file.", ft->name);
1241 tm_get_workspace();
1242 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1243 argc - 2, tags_file, ft->lang);
1244 g_free(command);
1245 symbols_finalize(); /* free c_tags_ignore data */
1246 if (! status)
1248 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1249 "were found.\n"));
1250 return 1;
1253 else
1255 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv[0]);
1256 g_printerr(_("Example:\n"
1257 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1258 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1259 return 1;
1261 return 0;
1265 void symbols_show_load_tags_dialog(void)
1267 GtkFileChooser *dialog;
1268 GtkFileFilter *filter;
1270 if (interface_prefs.use_native_windows_dialogs)
1271 dialog = GTK_FILE_CHOOSER(gtk_file_chooser_native_new(_("Load Tags File"),
1272 GTK_WINDOW(main_widgets.window), GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL));
1273 else
1275 dialog = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets.window),
1276 GTK_FILE_CHOOSER_ACTION_OPEN,
1277 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1278 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1279 NULL));
1280 gtk_widget_set_name(GTK_WIDGET(dialog), "GeanyDialog");
1282 filter = gtk_file_filter_new();
1283 gtk_file_filter_set_name(filter, _("Geany tags file (*.*.tags)"));
1284 gtk_file_filter_add_pattern(filter, "*.*.tags");
1285 gtk_file_chooser_add_filter(dialog, filter);
1287 if (dialogs_file_chooser_run(dialog) == GTK_RESPONSE_ACCEPT)
1289 GSList *flist = gtk_file_chooser_get_filenames(dialog);
1290 GSList *item;
1292 for (item = flist; item != NULL; item = g_slist_next(item))
1294 gchar *fname = item->data;
1295 gchar *utf8_fname;
1296 GeanyFiletype *ft;
1298 utf8_fname = utils_get_utf8_from_locale(fname);
1299 ft = detect_global_tags_filetype(utf8_fname);
1301 if (ft != NULL && symbols_load_global_tags(fname, ft))
1302 /* For translators: the first wildcard is the filetype, the second the filename */
1303 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1304 filetypes_get_display_name(ft), utf8_fname);
1305 else
1306 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1308 g_free(utf8_fname);
1309 g_free(fname);
1311 g_slist_free(flist);
1313 dialogs_file_chooser_destroy(dialog);
1317 static void init_user_tags(void)
1319 GSList *file_list = NULL, *list = NULL;
1320 const GSList *node;
1321 gchar *dir;
1323 dir = g_build_filename(app->configdir, GEANY_TAGS_SUBDIR, NULL);
1324 /* create the user tags dir for next time if it doesn't exist */
1325 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1326 utils_mkdir(dir, FALSE);
1327 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1329 SETPTR(dir, g_build_filename(app->datadir, GEANY_TAGS_SUBDIR, NULL));
1330 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1331 g_free(dir);
1333 file_list = g_slist_concat(file_list, list);
1335 /* populate the filetype-specific tag files lists */
1336 for (node = file_list; node != NULL; node = node->next)
1338 gchar *fname = node->data;
1339 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1340 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1342 g_free(utf8_fname);
1344 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1345 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1346 else
1348 geany_debug("Unknown filetype for file '%s'.", fname);
1349 g_free(fname);
1353 /* don't need to delete list contents because they are now stored in
1354 * ft->priv->tag_files */
1355 g_slist_free(file_list);
1359 static void load_user_tags(GeanyFiletypeID ft_id)
1361 static guchar *tags_loaded = NULL;
1362 static gboolean init_tags = FALSE;
1363 const GSList *node;
1364 GeanyFiletype *ft = filetypes[ft_id];
1366 g_return_if_fail(ft_id > 0);
1368 if (!tags_loaded)
1369 tags_loaded = g_new0(guchar, filetypes_array->len);
1370 if (tags_loaded[ft_id])
1371 return;
1372 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1374 if (!init_tags)
1376 init_user_tags();
1377 init_tags = TRUE;
1380 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1382 const gchar *fname = node->data;
1384 symbols_load_global_tags(fname, ft);
1389 static void on_goto_popup_item_activate(GtkMenuItem *item, TMTag *tag)
1391 GeanyDocument *new_doc, *old_doc;
1393 g_return_if_fail(tag);
1395 old_doc = document_get_current();
1396 new_doc = document_open_file(tag->file->file_name, FALSE, NULL, NULL);
1398 if (new_doc)
1399 navqueue_goto_line(old_doc, new_doc, tag->line);
1403 static guint get_tag_class(const TMTag *tag)
1405 gint group = tm_parser_get_sidebar_group(tag->lang, tag->type);
1407 if (group >= 0)
1409 guint icon_id;
1410 if (tm_parser_get_sidebar_info(tag->lang, group, &icon_id))
1411 return icon_id;
1414 return TM_ICON_STRUCT;
1418 /* opens menu at caret position */
1419 static void show_menu_at_caret(GtkMenu* menu, ScintillaObject *sci)
1421 GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(sci));
1422 gint pos = sci_get_current_position(sci);
1423 gint line = sci_get_line_from_position(sci, pos);
1424 gint line_height = SSM(sci, SCI_TEXTHEIGHT, line, 0);
1425 gint x = SSM(sci, SCI_POINTXFROMPOSITION, 0, pos);
1426 gint y = SSM(sci, SCI_POINTYFROMPOSITION, 0, pos);
1427 gint pos_next = sci_get_position_after(sci, pos);
1428 gint char_width = 0;
1429 /* if next pos is on the same Y (same line and not after wrapping), diff the X */
1430 if (pos_next > pos && SSM(sci, SCI_POINTYFROMPOSITION, 0, pos_next) == y)
1431 char_width = SSM(sci, SCI_POINTXFROMPOSITION, 0, pos_next) - x;
1432 GdkRectangle rect = {x, y, char_width, line_height};
1433 gtk_menu_popup_at_rect(GTK_MENU(menu), window, &rect, GDK_GRAVITY_SOUTH_WEST, GDK_GRAVITY_NORTH_WEST, NULL);
1437 static void show_goto_popup(GeanyDocument *doc, GPtrArray *tags, gboolean have_best)
1439 GtkWidget *first = NULL;
1440 GtkWidget *menu;
1441 GtkSizeGroup *group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
1442 GdkEvent *event;
1443 TMTag *tmtag;
1444 guint i;
1445 gchar **short_names, **file_names;
1446 menu = gtk_menu_new();
1448 /* If popup would show multiple files present a smart file list that allows
1449 * to easily distinguish the files while avoiding the file paths in their entirety */
1450 file_names = g_new(gchar *, tags->len);
1451 foreach_ptr_array(tmtag, i, tags)
1452 file_names[i] = tmtag->file->file_name;
1453 short_names = utils_strv_shorten_file_list(file_names, tags->len);
1454 g_free(file_names);
1456 foreach_ptr_array(tmtag, i, tags)
1458 GtkWidget *item;
1459 GtkWidget *label;
1460 GtkWidget *box;
1461 GtkWidget *image;
1462 gchar *fname = short_names[i];
1463 gchar *text;
1464 gchar *tooltip;
1465 gchar *sym = get_symbol_tooltip(doc, tmtag, TRUE);
1467 if (!sym)
1468 sym = g_strdup(get_symbol_name(doc, tmtag, TRUE, FALSE));
1469 if (!sym)
1470 sym = g_strdup("");
1472 if (! first && have_best)
1473 text = g_markup_printf_escaped("<b>%s:%lu</b>", fname, tmtag->line);
1474 else
1475 text = g_markup_printf_escaped("%s:%lu", fname, tmtag->line);
1477 tooltip = g_markup_printf_escaped("%s:%lu\n<small><tt>%s</tt></small>", fname, tmtag->line, sym);
1479 image = gtk_image_new_from_pixbuf(symbols_icons[get_tag_class(tmtag)].pixbuf);
1480 box = g_object_new(GTK_TYPE_BOX, "orientation", GTK_ORIENTATION_HORIZONTAL, "spacing", 12, NULL);
1481 label = g_object_new(GTK_TYPE_LABEL, "label", text, "use-markup", TRUE, "xalign", 0.0, NULL);
1482 gtk_size_group_add_widget(group, label);
1483 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
1484 g_free(text);
1485 text = g_markup_printf_escaped("<small><tt>%s</tt></small>", sym);
1486 gtk_box_pack_start(GTK_BOX(box), g_object_new(GTK_TYPE_LABEL, "label", text, "use-markup", TRUE, "xalign", 0.0,
1487 "max-width-chars", 80, "ellipsize", PANGO_ELLIPSIZE_END, NULL), FALSE, FALSE, 0);
1488 item = g_object_new(GTK_TYPE_IMAGE_MENU_ITEM, "image", image, "child", box, "always-show-image", TRUE,
1489 "tooltip-markup", tooltip, NULL);
1490 g_signal_connect_data(item, "activate", G_CALLBACK(on_goto_popup_item_activate),
1491 tm_tag_ref(tmtag), CLOSURE_NOTIFY(tm_tag_unref), 0);
1492 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1494 if (! first)
1495 first = item;
1497 g_free(sym);
1498 g_free(text);
1499 g_free(fname);
1501 g_free(short_names);
1503 gtk_widget_show_all(menu);
1505 if (first) /* always select the first item for better keyboard navigation */
1506 g_signal_connect(menu, "realize", G_CALLBACK(gtk_menu_shell_select_item), first);
1508 event = gtk_get_current_event();
1509 if (event && event->type == GDK_BUTTON_PRESS)
1510 gtk_menu_popup_at_pointer(GTK_MENU(menu), event);
1511 else
1512 show_menu_at_caret(GTK_MENU(menu), doc->editor->sci);
1513 if (event)
1514 gdk_event_free(event);
1516 g_object_unref(group);
1520 static gint compare_tags_by_name_line(gconstpointer ptr1, gconstpointer ptr2)
1522 gint res;
1523 TMTag *t1 = *((TMTag **) ptr1);
1524 TMTag *t2 = *((TMTag **) ptr2);
1526 res = g_strcmp0(t1->file->short_name, t2->file->short_name);
1527 if (res != 0)
1528 return res;
1529 return t1->line - t2->line;
1533 static TMTag *find_best_goto_tag(GeanyDocument *doc, GPtrArray *tags)
1535 TMTag *tag;
1536 guint i;
1538 /* first check if we have a tag in the current file */
1539 foreach_ptr_array(tag, i, tags)
1541 if (g_strcmp0(doc->real_path, tag->file->file_name) == 0)
1542 return tag;
1545 /* next check if we have a tag for some of the open documents */
1546 foreach_ptr_array(tag, i, tags)
1548 guint j;
1550 foreach_document(j)
1552 if (g_strcmp0(documents[j]->real_path, tag->file->file_name) == 0)
1553 return tag;
1557 /* next check if we have a tag for a file inside the current document's directory */
1558 foreach_ptr_array(tag, i, tags)
1560 gchar *dir = g_path_get_dirname(doc->real_path);
1562 if (g_str_has_prefix(tag->file->file_name, dir))
1564 g_free(dir);
1565 return tag;
1567 g_free(dir);
1570 return NULL;
1574 static GPtrArray *filter_tags(GPtrArray *tags, TMTag *current_tag, gboolean definition)
1576 GeanyDocument *doc = document_get_current();
1577 guint current_line = sci_get_current_line(doc->editor->sci) + 1;
1578 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1579 TMTag *tmtag, *last_tag = NULL;
1580 const gchar *current_scope = NULL;
1581 GPtrArray *filtered_tags = g_ptr_array_new();
1582 guint i;
1584 symbols_get_current_function(doc, &current_scope);
1586 foreach_ptr_array(tmtag, i, tags)
1588 /* don't show local variables outside current function or other
1589 * irrelevant tags - same as in the autocomplete case */
1590 if (!tm_workspace_is_autocomplete_tag(tmtag, doc->tm_file, current_line, current_scope))
1591 continue;
1593 if ((definition && !(tmtag->type & forward_types)) ||
1594 (!definition && (tmtag->type & forward_types)))
1596 /* If there are typedefs of e.g. a struct such as
1597 * "typedef struct Foo {} Foo;", filter out the typedef unless
1598 * cursor is at the struct name. */
1599 if (last_tag != NULL && last_tag->file == tmtag->file &&
1600 last_tag->type != tm_tag_typedef_t && tmtag->type == tm_tag_typedef_t)
1602 if (last_tag == current_tag)
1603 g_ptr_array_add(filtered_tags, tmtag);
1605 else if (tmtag != current_tag)
1606 g_ptr_array_add(filtered_tags, tmtag);
1608 last_tag = tmtag;
1612 return filtered_tags;
1616 static gboolean goto_tag(const gchar *name, gboolean definition)
1618 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1619 TMTag *tmtag, *current_tag = NULL;
1620 GeanyDocument *old_doc = document_get_current();
1621 gboolean found = FALSE;
1622 const GPtrArray *all_tags;
1623 GPtrArray *tags, *filtered_tags;
1624 guint i;
1625 guint current_line = sci_get_current_line(old_doc->editor->sci) + 1;
1627 all_tags = tm_workspace_find(name, NULL, tm_tag_max_t, NULL, old_doc->file_type->lang);
1629 /* get rid of global tags and find tag at current line */
1630 tags = g_ptr_array_new();
1631 foreach_ptr_array(tmtag, i, all_tags)
1633 if (tmtag->file)
1635 g_ptr_array_add(tags, tmtag);
1636 if (tmtag->file == old_doc->tm_file && tmtag->line == current_line)
1637 current_tag = tmtag;
1641 if (current_tag)
1642 /* swap definition/declaration search */
1643 definition = current_tag->type & forward_types;
1645 filtered_tags = filter_tags(tags, current_tag, definition);
1646 if (filtered_tags->len == 0)
1648 /* if we didn't find anything, try again with the opposite type */
1649 g_ptr_array_free(filtered_tags, TRUE);
1650 filtered_tags = filter_tags(tags, current_tag, !definition);
1652 g_ptr_array_free(tags, TRUE);
1653 tags = filtered_tags;
1655 if (tags->len == 1)
1657 GeanyDocument *new_doc;
1659 tmtag = tags->pdata[0];
1660 new_doc = document_find_by_real_path(tmtag->file->file_name);
1662 if (!new_doc)
1663 /* not found in opened document, should open */
1664 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1666 navqueue_goto_line(old_doc, new_doc, tmtag->line);
1668 else if (tags->len > 1)
1670 GPtrArray *tag_list;
1671 TMTag *tag, *best_tag;
1673 g_ptr_array_sort(tags, compare_tags_by_name_line);
1674 best_tag = find_best_goto_tag(old_doc, tags);
1676 tag_list = g_ptr_array_new();
1677 if (best_tag)
1678 g_ptr_array_add(tag_list, best_tag);
1679 foreach_ptr_array(tag, i, tags)
1681 if (tag != best_tag)
1682 g_ptr_array_add(tag_list, tag);
1684 show_goto_popup(old_doc, tag_list, best_tag != NULL);
1686 g_ptr_array_free(tag_list, TRUE);
1689 found = tags->len > 0;
1690 g_ptr_array_free(tags, TRUE);
1692 return found;
1696 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1698 if (goto_tag(name, definition))
1699 return TRUE;
1701 /* if we are here, there was no match and we are beeping ;-) */
1702 utils_beep();
1704 if (!definition)
1705 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1706 else
1707 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1708 return FALSE;
1712 /* This could perhaps be improved to check for #if, class etc. */
1713 static gint get_function_fold_number(GeanyDocument *doc)
1715 /* for Java the functions are always one fold level above the class scope */
1716 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1717 return SC_FOLDLEVELBASE + 1;
1718 else
1719 return SC_FOLDLEVELBASE;
1723 /* Should be used only with get_current_tag_cached.
1724 * tag_types caching might trigger recomputation too often but this isn't used differently often
1725 * enough to be an issue for now */
1726 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1728 static gint old_line = -2;
1729 static GeanyDocument *old_doc = NULL;
1730 static gint old_fold_num = -1;
1731 static guint old_tag_types = 0;
1732 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1733 gboolean ret;
1735 /* check if the cached line and file index have changed since last time: */
1736 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1737 ret = TRUE;
1738 else if (cur_line == old_line)
1739 ret = FALSE;
1740 else
1742 /* if the line has only changed by 1 */
1743 if (abs(cur_line - old_line) == 1)
1745 /* It's the same function if the fold number hasn't changed */
1746 ret = (fold_num != old_fold_num);
1748 else ret = TRUE;
1751 /* record current line and file index for next time */
1752 old_line = cur_line;
1753 old_doc = doc;
1754 old_fold_num = fold_num;
1755 old_tag_types = tag_types;
1756 return ret;
1760 /* Parse the function name up to 2 lines before tag_line.
1761 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1762 * type or argument names can be confused with the function name. */
1763 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1765 gint start, end, max_pos;
1766 gint fn_style;
1768 switch (sci_get_lexer(sci))
1770 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
1771 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
1772 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
1774 start = sci_get_position_from_line(sci, tag_line - 2);
1775 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1776 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
1777 start++;
1779 end = start;
1780 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
1781 end++;
1783 if (start == end)
1784 return NULL;
1785 return sci_get_contents_range(sci, start, end);
1789 /* Parse the function name */
1790 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
1792 gint start, end, first_pos, max_pos;
1793 gint tmp;
1794 gchar c;
1796 first_pos = end = sci_get_position_from_line(sci, tag_line);
1797 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1798 tmp = 0;
1799 /* goto the begin of function body */
1800 while (end < max_pos &&
1801 (tmp = sci_get_char_at(sci, end)) != '{' &&
1802 tmp != 0) end++;
1803 if (tmp == 0) end --;
1805 /* go back to the end of function identifier */
1806 while (end > 0 && end > first_pos - 500 &&
1807 (tmp = sci_get_char_at(sci, end)) != '(' &&
1808 tmp != 0) end--;
1809 end--;
1810 if (end < 0) end = 0;
1812 /* skip whitespaces between identifier and ( */
1813 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
1815 start = end;
1816 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1817 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
1818 || tmp == SCE_C_GLOBALCLASS
1819 || (c = sci_get_char_at(sci, start)) == '~'
1820 || c == ':'))
1821 start--;
1822 if (start != 0 && start < end) start++; /* correct for last non-matching char */
1824 if (start == end) return NULL;
1825 return sci_get_contents_range(sci, start, end + 1);
1829 /* gets the fold header after or on @line, but skipping folds created because of parentheses */
1830 static gint get_fold_header_after(ScintillaObject *sci, gint line)
1832 const gint line_count = sci_get_line_count(sci);
1834 for (; line < line_count; line++)
1836 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
1838 const gint last_child = SSM(sci, SCI_GETLASTCHILD, line, -1);
1839 const gint line_end = sci_get_line_end_position(sci, line);
1840 const gint lexer = sci_get_lexer(sci);
1841 gint parenthesis_match_line = -1;
1843 /* now find any unbalanced open parenthesis on the line and see where the matching
1844 * brace would be, mimicking what folding on () does */
1845 for (gint pos = sci_get_position_from_line(sci, line); pos < line_end; pos++)
1847 if (highlighting_is_code_style(lexer, sci_get_style_at(sci, pos)) &&
1848 sci_get_char_at(sci, pos) == '(')
1850 const gint matching = sci_find_matching_brace(sci, pos);
1852 if (matching >= 0)
1854 parenthesis_match_line = sci_get_line_from_position(sci, matching);
1855 if (parenthesis_match_line != line)
1856 break; /* match is on a different line, we found a possible fold */
1857 else
1858 pos = matching; /* just skip the range and continue searching */
1863 /* if the matching parenthesis matches the fold level, skip it and continue.
1864 * it matches if it either spans the same lines, or spans one more but the next one is
1865 * a fold header (in which case the last child of the fold is one less to let the
1866 * header be at the parent level) */
1867 if ((parenthesis_match_line == last_child) ||
1868 (parenthesis_match_line == last_child + 1 &&
1869 sci_get_fold_level(sci, parenthesis_match_line) & SC_FOLDLEVELHEADERFLAG))
1870 line = last_child;
1871 else
1872 return line;
1876 return -1;
1880 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
1882 gint line;
1883 gint parent;
1885 line = sci_get_current_line(doc->editor->sci);
1886 parent = sci_get_fold_parent(doc->editor->sci, line);
1887 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
1888 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
1889 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
1891 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
1893 if (tag)
1895 gint tag_line = tag->line - 1;
1896 gint last_child = line + 1;
1898 /* if it may be a false positive because we're inside a fold level not inside anything
1899 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
1900 * right after the tag we got from TM.
1901 * Additionally, we perform parentheses matching on the initial line not to get confused
1902 * by folding on () in case the parameter list spans multiple lines */
1903 if (abs(tag_line - parent) > 1)
1905 const gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
1906 if (tag_fold >= 0)
1907 last_child = SSM(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
1910 if (line <= last_child)
1912 if (tag->scope)
1913 *tagname = g_strconcat(tag->scope,
1914 tm_parser_scope_separator(tag->lang), tag->name, NULL);
1915 else
1916 *tagname = g_strdup(tag->name);
1918 return tag_line;
1922 /* for the poor guy with a modified document and without real time tag parsing, we fallback
1923 * to dirty and inaccurate hand-parsing */
1924 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
1926 const gint fn_fold = get_function_fold_number(doc);
1927 gint tag_line = parent;
1928 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1930 /* find the top level fold point */
1931 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
1933 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
1934 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1937 if (tag_line >= 0)
1939 gchar *cur_tag;
1941 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
1942 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
1943 else
1944 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
1946 if (cur_tag != NULL)
1948 *tagname = cur_tag;
1949 return tag_line;
1954 *tagname = g_strdup(_("unknown"));
1955 return -1;
1959 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
1961 static gint tag_line = -1;
1962 static gchar *cur_tag = NULL;
1964 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
1966 if (doc == NULL) /* reset current function */
1968 current_tag_changed(NULL, -1, -1, 0);
1969 g_free(cur_tag);
1970 cur_tag = g_strdup(_("unknown"));
1971 if (tagname != NULL)
1972 *tagname = cur_tag;
1973 tag_line = -1;
1975 else
1977 gint line = sci_get_current_line(doc->editor->sci);
1978 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
1980 if (current_tag_changed(doc, line, fold_level, tag_types))
1982 g_free(cur_tag);
1983 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
1985 *tagname = cur_tag;
1988 return tag_line;
1992 /* Sets *tagname to point at the current function or tag name.
1993 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
1994 * call to this function.
1995 * Returns: line number of the current tag, or -1 if unknown. */
1996 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
1998 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2002 /* same as symbols_get_current_function() but finds class, namespaces and more */
2003 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2005 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2006 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t | tm_tag_namespace_t);
2008 return get_current_tag_name_cached(doc, tagname, tag_types);
2012 const gchar *symbols_get_icon_name(guint icon_id)
2014 if (icon_id < TM_N_ICONS)
2015 return symbols_icons[icon_id].icon_name;
2016 return NULL;
2020 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2022 gint sort_mode = GPOINTER_TO_INT(user_data);
2023 GeanyDocument *doc = document_get_current();
2025 if (ignore_callback)
2026 return;
2028 if (doc != NULL)
2029 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2032 static void on_symbol_tree_group_by_type_clicked(GtkMenuItem *menuitem, gpointer user_data)
2034 GeanyDocument *doc = document_get_current();
2036 if (ignore_callback)
2037 return;
2039 ui_prefs.symbols_group_by_type = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
2040 if (doc != NULL)
2041 doc->has_tags = symbols_recreate_tag_list(doc, SYMBOLS_SORT_USE_PREVIOUS);
2044 static void on_symbol_tree_menu_show(GtkWidget *widget,
2045 gpointer user_data)
2047 GeanyDocument *doc = document_get_current();
2048 gboolean enable;
2050 enable = doc && doc->has_tags;
2051 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2052 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2053 gtk_widget_set_sensitive(symbol_menu.group_by_type, enable);
2054 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2055 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2056 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2057 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2059 if (! doc)
2060 return;
2062 ignore_callback = TRUE;
2064 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2065 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2066 else
2067 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2069 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.group_by_type),
2070 ui_prefs.symbols_group_by_type);
2072 ignore_callback = FALSE;
2076 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2078 gboolean expand = GPOINTER_TO_INT(user_data);
2079 GeanyDocument *doc = document_get_current();
2081 if (! doc)
2082 return;
2084 g_return_if_fail(doc->priv->tag_tree);
2086 if (expand)
2087 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2088 else
2089 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2093 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2095 GtkTreeIter iter;
2096 GtkTreeSelection *selection;
2097 GtkTreeModel *model;
2098 GeanyDocument *doc;
2099 TMTag *tag = NULL;
2101 doc = document_get_current();
2102 if (!doc)
2103 return;
2105 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2106 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2107 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2108 if (tag)
2110 if (widget == symbol_menu.find_in_files)
2111 search_show_find_in_files_dialog_full(tag->name, NULL);
2112 else
2113 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2114 widget == symbol_menu.find_usage);
2116 tm_tag_unref(tag);
2121 static void create_taglist_popup_menu(void)
2123 GtkWidget *item, *menu;
2125 tv.popup_taglist = menu = gtk_menu_new();
2127 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2128 gtk_widget_show(item);
2129 gtk_container_add(GTK_CONTAINER(menu), item);
2130 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2132 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2133 gtk_widget_show(item);
2134 gtk_container_add(GTK_CONTAINER(menu), item);
2135 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2137 item = gtk_separator_menu_item_new();
2138 gtk_widget_show(item);
2139 gtk_container_add(GTK_CONTAINER(menu), item);
2141 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2142 _("Sort by _Name"));
2143 gtk_widget_show(item);
2144 gtk_container_add(GTK_CONTAINER(menu), item);
2145 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2146 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2148 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2149 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2150 gtk_widget_show(item);
2151 gtk_container_add(GTK_CONTAINER(menu), item);
2152 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2153 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2155 item = gtk_separator_menu_item_new();
2156 gtk_widget_show(item);
2157 gtk_container_add(GTK_CONTAINER(menu), item);
2159 symbol_menu.group_by_type = item = gtk_check_menu_item_new_with_mnemonic(_("_Group by Type"));
2160 gtk_widget_show(item);
2161 gtk_container_add(GTK_CONTAINER(menu), item);
2162 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_group_by_type_clicked), NULL);
2164 item = gtk_separator_menu_item_new();
2165 gtk_widget_show(item);
2166 gtk_container_add(GTK_CONTAINER(menu), item);
2168 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2169 gtk_widget_show(item);
2170 gtk_container_add(GTK_CONTAINER(menu), item);
2171 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2173 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2174 gtk_widget_show(item);
2175 gtk_container_add(GTK_CONTAINER(menu), item);
2176 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2178 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2179 gtk_widget_show(item);
2180 gtk_container_add(GTK_CONTAINER(menu), item);
2181 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2183 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2185 sidebar_add_common_menu_items(GTK_MENU(menu));
2189 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2191 gchar *f;
2193 g_return_if_fail(!EMPTY(doc->real_path));
2195 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2196 if (utils_str_equal(doc->real_path, f))
2197 load_c_ignore_tags();
2199 g_free(f);
2203 void symbols_init(void)
2205 gchar *f;
2206 guint i;
2208 create_taglist_popup_menu();
2210 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2211 ui_add_config_file_menu_item(f, NULL, NULL);
2212 g_free(f);
2214 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2216 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2217 symbols_icons[i].pixbuf = get_tag_icon(symbols_icons[i].icon_name);
2221 void symbols_finalize(void)
2223 guint i;
2225 g_strfreev(c_tags_ignore);
2227 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2229 if (symbols_icons[i].pixbuf)
2230 g_object_unref(symbols_icons[i].pixbuf);