Fix anonymous tag renaming when interleaved by scopeless macros
[geany-mirror.git] / src / symbols.c
blobf6e61adb7eb7ce8e50b9d89e0bfc751ed35334fe
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 "documentprivate.h"
42 #include "editor.h"
43 #include "encodings.h"
44 #include "filetypesprivate.h"
45 #include "geanyobject.h"
46 #include "highlighting.h"
47 #include "main.h"
48 #include "navqueue.h"
49 #include "sciwrappers.h"
50 #include "sidebar.h"
51 #include "support.h"
52 #include "tm_parser.h"
53 #include "tm_tag.h"
54 #include "tm_ctags.h"
55 #include "ui_utils.h"
56 #include "utils.h"
58 #include "SciLexer.h"
60 #include <ctype.h>
61 #include <string.h>
62 #include <stdlib.h>
63 #include <gtk/gtk.h>
66 typedef struct
68 gint found_line; /* return: the nearest line found */
69 gint line; /* input: the line to look for */
70 gboolean lower /* input: search only for lines with lower number than @line */;
71 } TreeSearchData;
74 static GPtrArray *top_level_iter_names = NULL;
77 static struct
79 const gchar *icon_name;
80 GdkPixbuf *pixbuf;
82 /* keep in sync with enum in tm_parser.h */
83 symbols_icons[TM_N_ICONS] = {
84 [TM_ICON_CLASS] = { "classviewer-class", NULL },
85 [TM_ICON_MACRO] = { "classviewer-macro", NULL },
86 [TM_ICON_MEMBER] = { "classviewer-member", NULL },
87 [TM_ICON_METHOD] = { "classviewer-method", NULL },
88 [TM_ICON_NAMESPACE] = { "classviewer-namespace", NULL },
89 [TM_ICON_OTHER] = { "classviewer-other", NULL },
90 [TM_ICON_STRUCT] = { "classviewer-struct", NULL },
91 [TM_ICON_VAR] = { "classviewer-var", NULL },
94 static struct
96 GtkWidget *expand_all;
97 GtkWidget *collapse_all;
98 GtkWidget *sort_by_name;
99 GtkWidget *sort_by_appearance;
100 GtkWidget *find_usage;
101 GtkWidget *find_doc_usage;
102 GtkWidget *find_in_files;
104 symbol_menu;
106 static void load_user_tags(GeanyFiletypeID ft_id);
108 /* get the tags_ignore list, exported by geany_lcpp.c */
109 extern gchar **c_tags_ignore;
111 /* ignore certain tokens when parsing C-like syntax.
112 * Also works for reloading. */
113 static void load_c_ignore_tags(void)
115 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
116 gchar *content;
118 if (g_file_get_contents(path, &content, NULL, NULL))
120 gchar **line;
122 /* historically we ignore the glib _DECLS for tag generation */
123 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
125 g_strfreev(c_tags_ignore);
126 tm_ctags_clear_ignore_symbols();
128 /* for old c.c parser */
129 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
130 /* for new cxx parser */
131 foreach_strv(line, c_tags_ignore)
133 tm_ctags_add_ignore_symbol(*line);
136 g_free(content);
138 g_free(path);
142 void symbols_reload_config_files(void)
144 load_c_ignore_tags();
148 static gsize get_tag_count(void)
150 GPtrArray *tags = tm_get_workspace()->global_tags;
151 gsize count = tags ? tags->len : 0;
153 return count;
157 /* wrapper for tm_workspace_load_global_tags().
158 * note that the tag count only counts new global tags added - if a tag has the same name,
159 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
160 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
162 gboolean result;
163 gsize old_tag_count = get_tag_count();
165 result = tm_workspace_load_global_tags(tags_file, ft->lang);
166 if (result)
168 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file, ft->name,
169 (guint) (get_tag_count() - old_tag_count));
171 return result;
175 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
176 * This provides autocompletion, calltips, etc. */
177 void symbols_global_tags_loaded(guint file_type_idx)
179 /* load ignore list for C/C++ parser */
180 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
181 c_tags_ignore == NULL)
183 load_c_ignore_tags();
186 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
187 return;
189 /* load config in case of custom filetypes */
190 filetypes_load_config(file_type_idx, FALSE);
192 load_user_tags(file_type_idx);
194 switch (file_type_idx)
196 case GEANY_FILETYPES_CPP:
197 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
198 break;
199 case GEANY_FILETYPES_PHP:
200 symbols_global_tags_loaded(GEANY_FILETYPES_HTML); /* load HTML global tags */
201 break;
206 GString *symbols_find_typenames_as_string(TMParserType lang, gboolean global)
208 guint j;
209 TMTag *tag;
210 GString *s = NULL;
211 GPtrArray *typedefs;
212 TMParserType tag_lang;
214 if (global)
215 typedefs = app->tm_workspace->global_typename_array;
216 else
217 typedefs = app->tm_workspace->typename_array;
219 if ((typedefs) && (typedefs->len > 0))
221 const gchar *last_name = "";
223 s = g_string_sized_new(typedefs->len * 10);
224 for (j = 0; j < typedefs->len; ++j)
226 tag = TM_TAG(typedefs->pdata[j]);
227 tag_lang = tag->lang;
229 if (tag->name && tm_parser_langs_compatible(lang, tag_lang) &&
230 strcmp(tag->name, last_name) != 0)
232 if (j != 0)
233 g_string_append_c(s, ' ');
234 g_string_append(s, tag->name);
235 last_name = tag->name;
239 return s;
243 /** Gets the context separator used by the tag manager for a particular file
244 * type.
245 * @param ft_id File type identifier.
246 * @return The context separator string.
248 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
249 * without a context separator.
251 * @since 0.19
253 GEANY_API_SYMBOL
254 const gchar *symbols_get_context_separator(gint ft_id)
256 return tm_parser_scope_separator(filetypes[ft_id]->lang);
260 /* sort by name, then line */
261 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
263 gint ret;
265 if (tag_a == NULL || tag_b == NULL)
266 return 0;
268 if (tag_a->name == NULL)
269 return -(tag_a->name != tag_b->name);
271 if (tag_b->name == NULL)
272 return tag_a->name != tag_b->name;
274 ret = strcmp(tag_a->name, tag_b->name);
275 if (ret == 0)
277 return tag_a->line - tag_b->line;
279 return ret;
283 /* sort by line, then scope */
284 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
286 const TMTag *tag_a = TM_TAG(a);
287 const TMTag *tag_b = TM_TAG(b);
288 gint ret;
290 if (a == NULL || b == NULL)
291 return 0;
293 ret = tag_a->line - tag_b->line;
294 if (ret == 0)
296 if (tag_a->scope == NULL)
297 return -(tag_a->scope != tag_b->scope);
298 if (tag_b->scope == NULL)
299 return tag_a->scope != tag_b->scope;
300 else
301 return strcmp(tag_a->scope, tag_b->scope);
303 return ret;
307 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
309 GList *tag_names = NULL;
310 guint i;
311 gchar **tf_strv;
313 g_return_val_if_fail(doc, NULL);
315 if (! doc->tm_file || ! doc->tm_file->tags_array)
316 return NULL;
318 tf_strv = g_strsplit_set(doc->priv->tag_filter, " ", -1);
320 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
322 TMTag *tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
324 if (tag->type & tag_types)
326 gboolean filtered = FALSE;
327 gchar **val;
328 gchar *full_tagname = g_strconcat(tag->scope ? tag->scope : "",
329 tag->scope ? tm_parser_scope_separator_printable(tag->lang) : "",
330 tag->name, NULL);
331 gchar *normalized_tagname = g_utf8_normalize(full_tagname, -1, G_NORMALIZE_ALL);
333 foreach_strv(val, tf_strv)
335 gchar *normalized_val = g_utf8_normalize(*val, -1, G_NORMALIZE_ALL);
337 if (normalized_tagname != NULL && normalized_val != NULL)
339 gchar *case_normalized_tagname = g_utf8_casefold(normalized_tagname, -1);
340 gchar *case_normalized_val = g_utf8_casefold(normalized_val, -1);
342 filtered = strstr(case_normalized_tagname, case_normalized_val) == NULL;
343 g_free(case_normalized_tagname);
344 g_free(case_normalized_val);
346 g_free(normalized_val);
348 if (filtered)
349 break;
351 if (!filtered)
352 tag_names = g_list_prepend(tag_names, tag);
354 g_free(normalized_tagname);
355 g_free(full_tagname);
358 tag_names = g_list_sort(tag_names, compare_symbol_lines);
360 g_strfreev(tf_strv);
362 return tag_names;
366 /* amount of types in the symbol list - can be increased if needed */
367 #define MAX_SYMBOL_TYPES 15
369 GtkTreeIter tv_iters[MAX_SYMBOL_TYPES];
372 static void init_tag_iters(void)
374 guint i;
375 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
376 * filetypes(e.g. config file to Python crashes Geany without this) */
377 for (i = 0; i < MAX_SYMBOL_TYPES; i++)
378 tv_iters[i].stamp = -1;
382 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
384 static GtkIconTheme *icon_theme = NULL;
385 static gint x = -1;
387 if (G_UNLIKELY(x < 0))
389 gint dummy;
390 icon_theme = gtk_icon_theme_get_default();
391 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
393 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
397 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
399 GtkTreeModel *model = GTK_TREE_MODEL(store);
401 if (!gtk_tree_model_get_iter_first(model, iter))
402 return FALSE;
405 gchar *candidate;
407 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
408 /* FIXME: what if 2 different items have the same name?
409 * this should never happen, but might be caused by a typo in a translation */
410 if (utils_str_equal(candidate, title))
412 g_free(candidate);
413 return TRUE;
415 else
416 g_free(candidate);
418 while (gtk_tree_model_iter_next(model, iter));
420 return FALSE;
424 static void tag_list_add_groups(GtkTreeStore *tree_store, TMParserType lang)
426 const gchar *title;
427 guint i;
428 guint icon_id;
430 g_return_if_fail(top_level_iter_names);
432 for (i = 0; title = tm_parser_get_sidebar_info(lang, i, &icon_id); i++)
434 GtkTreeIter *iter = &tv_iters[i];
435 GdkPixbuf *icon = NULL;
437 if (icon_id < TM_N_ICONS)
438 icon = symbols_icons[icon_id].pixbuf;
440 g_assert(title != NULL);
441 g_ptr_array_add(top_level_iter_names, (gchar *)title);
443 if (!find_toplevel_iter(tree_store, iter, title))
444 gtk_tree_store_append(tree_store, iter, NULL);
446 if (icon)
447 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
448 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
453 static void add_top_level_items(GeanyDocument *doc)
455 TMParserType lang = doc->file_type->lang;
456 GtkTreeStore *tag_store = doc->priv->tag_store;
458 if (top_level_iter_names == NULL)
459 top_level_iter_names = g_ptr_array_new();
460 else
461 g_ptr_array_set_size(top_level_iter_names, 0);
463 init_tag_iters();
465 tag_list_add_groups(tag_store, lang);
469 /* removes toplevel items that have no children */
470 static void hide_empty_rows(GtkTreeStore *store)
472 GtkTreeIter iter;
473 gboolean cont = TRUE;
475 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
476 return; /* stop when first iter is invalid, i.e. no elements */
478 while (cont)
480 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
481 cont = gtk_tree_store_remove(store, &iter);
482 else
483 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
488 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
490 gchar *utf8_name;
491 const gchar *scope = tag->scope;
492 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
493 gboolean doc_is_utf8 = FALSE;
495 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
496 * for None at this point completely */
497 if (utils_str_equal(doc->encoding, "UTF-8") ||
498 utils_str_equal(doc->encoding, "None"))
499 doc_is_utf8 = TRUE;
500 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
501 * plugin might have called tm_source_file_update(), so check to be sure */
502 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
504 if (! doc_is_utf8)
505 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
506 -1, doc->encoding, TRUE);
507 else
508 utf8_name = tag->name;
510 if (utf8_name == NULL)
511 return NULL;
513 if (! buffer)
514 buffer = g_string_new(NULL);
515 else
516 g_string_truncate(buffer, 0);
518 /* check first char of scope is a wordchar */
519 if (!found_parent && scope &&
520 strpbrk(scope, GEANY_WORDCHARS) == scope)
522 const gchar *sep = tm_parser_scope_separator_printable(tag->lang);
524 g_string_append(buffer, scope);
525 g_string_append(buffer, sep);
527 g_string_append(buffer, utf8_name);
529 if (! doc_is_utf8)
530 g_free(utf8_name);
532 g_string_append_printf(buffer, " [%lu]", tag->line);
534 return buffer->str;
538 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
540 gchar *utf8_name = tm_parser_format_function(tag->lang, tag->name,
541 tag->arglist, tag->var_type, tag->scope);
543 if (!utf8_name && tag->var_type &&
544 tag->type & (tm_tag_field_t | tm_tag_member_t | tm_tag_variable_t | tm_tag_externvar_t))
546 utf8_name = tm_parser_format_variable(tag->lang, tag->name, tag->var_type);
549 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
550 * for None at this point completely */
551 if (utf8_name != NULL &&
552 ! utils_str_equal(doc->encoding, "UTF-8") &&
553 ! utils_str_equal(doc->encoding, "None"))
555 SETPTR(utf8_name,
556 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
559 return utf8_name;
563 static const gchar *get_parent_name(const TMTag *tag)
565 return !EMPTY(tag->scope) ? tag->scope : NULL;
569 static GtkTreeIter *get_tag_type_iter(TMParserType lang, TMTagType tag_type)
571 /* TODO: tm_parser_get_sidebar_group() goes through groups one by one.
572 * If this happens to be slow for tree construction, create a lookup
573 * table for them. */
574 gint group = tm_parser_get_sidebar_group(lang, tag_type);
576 if (group < 0)
577 return NULL;
579 return &tv_iters[group];
583 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
585 GdkPixbuf *icon = NULL;
587 /* copy parent icon */
588 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
589 SYMBOLS_COLUMN_ICON, &icon, -1);
590 return icon;
594 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
596 const TMTag *t1 = v1;
597 const TMTag *t2 = v2;
599 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
600 utils_str_equal(t1->scope, t2->scope) &&
601 /* include arglist in match to support e.g. C++ overloading */
602 utils_str_equal(t1->arglist, t2->arglist));
606 /* inspired from g_str_hash() */
607 static guint tag_hash(gconstpointer v)
609 const TMTag *tag = v;
610 const gchar *p;
611 guint32 h = 5381;
613 h = (h << 5) + h + tag->type;
614 for (p = tag->name; *p != '\0'; p++)
615 h = (h << 5) + h + *p;
616 if (tag->scope)
618 for (p = tag->scope; *p != '\0'; p++)
619 h = (h << 5) + h + *p;
621 /* for e.g. C++ overloading */
622 if (tag->arglist)
624 for (p = tag->arglist; *p != '\0'; p++)
625 h = (h << 5) + h + *p;
628 return h;
632 /* like gtk_tree_view_expand_to_path() but with an iter */
633 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
635 GtkTreeModel *model = gtk_tree_view_get_model(view);
636 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
638 gtk_tree_view_expand_to_path(view, path);
639 gtk_tree_path_free(path);
643 /* like gtk_tree_store_remove() but finds the next iter at any level */
644 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
646 GtkTreeIter parent;
647 gboolean has_parent;
648 gboolean cont;
650 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
651 cont = gtk_tree_store_remove(store, iter);
652 /* if there is no next at this level but there is a parent iter, continue from it */
653 if (! cont && has_parent)
655 *iter = parent;
656 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
659 return cont;
663 static gint tree_search_func(gconstpointer key, gpointer user_data)
665 TreeSearchData *data = user_data;
666 gint parent_line = GPOINTER_TO_INT(key);
667 gboolean new_nearest;
669 if (data->found_line == -1)
670 data->found_line = parent_line; /* initial value */
672 new_nearest = ABS(data->line - parent_line) < ABS(data->line - data->found_line);
674 if (parent_line > data->line)
676 if (new_nearest && !data->lower)
677 data->found_line = parent_line;
678 return -1;
681 if (new_nearest)
682 data->found_line = parent_line;
684 if (parent_line < data->line)
685 return 1;
687 return 0;
691 static gint tree_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
693 return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
697 static void parents_table_tree_value_free(gpointer data)
699 g_slice_free(GtkTreeIter, data);
703 /* adds a new element in the parent table if its key is known. */
704 static void update_parents_table(GHashTable *table, const TMTag *tag, const GtkTreeIter *iter)
706 const gchar *name;
707 gchar *name_free = NULL;
708 GTree *tree;
710 if (EMPTY(tag->scope))
712 /* simple case, just use the tag name */
713 name = tag->name;
715 else if (! tm_parser_has_full_scope(tag->lang))
717 /* if the parser doesn't use fully qualified scope, use the name alone but
718 * prevent Foo::Foo from making parent = child */
719 if (utils_str_equal(tag->scope, tag->name))
720 name = NULL;
721 else
722 name = tag->name;
724 else
726 /* build the fully qualified scope as get_parent_name() would return it for a child tag */
727 name_free = g_strconcat(tag->scope, tm_parser_scope_separator(tag->lang), tag->name, NULL);
728 name = name_free;
731 if (name && g_hash_table_lookup_extended(table, name, NULL, (gpointer *) &tree))
733 if (!tree)
735 tree = g_tree_new_full(tree_cmp, NULL, NULL, parents_table_tree_value_free);
736 g_hash_table_insert(table, name_free ? name_free : g_strdup(name), tree);
737 name_free = NULL;
740 g_tree_insert(tree, GINT_TO_POINTER(tag->line), g_slice_dup(GtkTreeIter, iter));
743 g_free(name_free);
747 static GtkTreeIter *parents_table_lookup(GHashTable *table, const gchar *name, guint line)
749 GtkTreeIter *parent_search = NULL;
750 GTree *tree;
752 tree = g_hash_table_lookup(table, name);
753 if (tree)
755 TreeSearchData user_data = {-1, line, TRUE};
757 /* search parent candidates for the one with the nearest
758 * line number which is lower than the tag's line number */
759 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
760 parent_search = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
763 return parent_search;
767 static void parents_table_value_free(gpointer data)
769 GTree *tree = data;
770 if (tree)
771 g_tree_destroy(tree);
775 /* inserts a @data in @table on key @tag.
776 * previous data is not overwritten if the key is duplicated, but rather the
777 * two values are kept in a list
779 * table is: GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
780 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
782 GTree *tree = g_hash_table_lookup(table, tag);
783 if (!tree)
785 tree = g_tree_new_full(tree_cmp, NULL, NULL, NULL);
786 g_hash_table_insert(table, tag, tree);
788 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
789 list = g_list_prepend(list, data);
790 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
794 /* looks up the entry in @table that best matches @tag.
795 * if there is more than one candidate, the one that has closest line position to @tag is chosen */
796 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
798 TreeSearchData user_data = {-1, tag->line, FALSE};
799 GTree *tree = g_hash_table_lookup(table, tag);
801 if (tree)
803 GList *list;
805 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
806 list = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
807 /* return the first value in the list - we don't care which of the
808 * tags with identical names defined on the same line we get */
809 if (list)
810 return list->data;
812 return NULL;
816 /* removes the element at @tag from @table.
817 * @tag must be the exact pointer used at insertion time */
818 static void tags_table_remove(GHashTable *table, TMTag *tag)
820 GTree *tree = g_hash_table_lookup(table, tag);
821 if (tree)
823 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
824 if (list)
826 GList *node;
827 /* should always be the first element as we returned the first one in
828 * tags_table_lookup() */
829 foreach_list(node, list)
831 if (((GList *) node->data)->data == tag)
832 break;
834 list = g_list_delete_link(list, node);
835 if (!list)
836 g_tree_remove(tree, GINT_TO_POINTER(tag->line));
837 else
838 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
844 static gboolean tags_table_tree_value_free(gpointer key, gpointer value, gpointer data)
846 GList *list = value;
847 g_list_free(list);
848 return FALSE;
852 static void tags_table_value_free(gpointer data)
854 GTree *tree = data;
855 if (tree)
857 /* free any leftover elements. note that we can't register a value_free_func when
858 * creating the tree because we only want to free it when destroying the tree,
859 * not when inserting a duplicate (we handle this manually) */
860 g_tree_foreach(tree, tags_table_tree_value_free, NULL);
861 g_tree_destroy(tree);
867 * Updates the tag tree for a document with the tags in *list.
868 * @param doc a document
869 * @param tags a pointer to a GList* holding the tags to add/update. This
870 * list may be updated, removing updated elements.
872 * The update is done in two passes:
873 * 1) walking the current tree, update tags that still exist and remove the
874 * obsolescent ones;
875 * 2) walking the remaining (non updated) tags, adds them in the list.
877 * For better performances, we use 2 hash tables:
878 * - one containing all the tags for lookup in the first pass (actually stores a
879 * reference in the tags list for removing it efficiently), avoiding list search
880 * on each tag;
881 * - the other holding "tag-name":row references for tags having children, used to
882 * lookup for a parent in both passes, avoiding tree traversal.
884 static void update_tree_tags(GeanyDocument *doc, GList **tags)
886 GtkTreeStore *store = doc->priv->tag_store;
887 GtkTreeModel *model = GTK_TREE_MODEL(store);
888 GHashTable *parents_table;
889 GHashTable *tags_table;
890 GtkTreeIter iter;
891 gboolean cont;
892 GList *item;
894 /* Build hash tables holding tags and parents */
895 /* parent table is GHashTable<tag_name, GTree<line_num, GtkTreeIter>>
896 * where tag_name might be a fully qualified name (with scope) if the language
897 * parser reports scope properly (see tm_parser_has_full_scope()). */
898 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, parents_table_value_free);
899 /* tags table is another representation of the @tags list,
900 * GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
901 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, tags_table_value_free);
902 foreach_list(item, *tags)
904 TMTag *tag = item->data;
905 const gchar *parent_name;
907 tags_table_insert(tags_table, tag, item);
909 parent_name = get_parent_name(tag);
910 if (parent_name)
911 g_hash_table_insert(parents_table, g_strdup(parent_name), NULL);
914 /* First pass, update existing rows or delete them.
915 * It is OK to delete them since we walk top down so we would remove
916 * parents before checking for their children, thus never implicitly
917 * deleting an updated child */
918 cont = gtk_tree_model_get_iter_first(model, &iter);
919 while (cont)
921 TMTag *tag;
923 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
924 if (! tag) /* most probably a toplevel, skip it */
925 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
926 else
928 GList *found_item;
930 found_item = tags_table_lookup(tags_table, tag);
931 if (! found_item) /* tag doesn't exist, remove it */
932 cont = tree_store_remove_row(store, &iter);
933 else /* tag still exist, update it */
935 const gchar *parent_name;
936 TMTag *found = found_item->data;
938 parent_name = get_parent_name(found);
939 /* if parent is unknown, ignore it */
940 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
941 parent_name = NULL;
943 if (!tm_tags_equal(tag, found))
945 const gchar *name;
946 gchar *tooltip;
948 /* only update fields that (can) have changed (name that holds line
949 * number, tooltip, and the tag itself) */
950 name = get_symbol_name(doc, found, parent_name != NULL);
951 tooltip = get_symbol_tooltip(doc, found);
952 gtk_tree_store_set(store, &iter,
953 SYMBOLS_COLUMN_NAME, name,
954 SYMBOLS_COLUMN_TOOLTIP, tooltip,
955 SYMBOLS_COLUMN_TAG, found,
956 -1);
957 g_free(tooltip);
960 update_parents_table(parents_table, found, &iter);
962 /* remove the updated tag from the table and list */
963 tags_table_remove(tags_table, found);
964 *tags = g_list_delete_link(*tags, found_item);
966 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
969 tm_tag_unref(tag);
973 /* Second pass, now we have a tree cleaned up from invalid rows,
974 * we simply add new ones */
975 foreach_list (item, *tags)
977 TMTag *tag = item->data;
978 GtkTreeIter *parent;
980 parent = get_tag_type_iter(tag->lang, tag->type);
981 if (parent)
983 gboolean expand;
984 const gchar *name;
985 const gchar *parent_name;
986 gchar *tooltip;
987 GdkPixbuf *icon = get_child_icon(store, parent);
989 parent_name = get_parent_name(tag);
990 if (parent_name)
992 GtkTreeIter *parent_search = parents_table_lookup(parents_table, parent_name, tag->line);
994 if (parent_search)
995 parent = parent_search;
996 else
997 parent_name = NULL;
1000 /* only expand to the iter if the parent was empty, otherwise we let the
1001 * folding as it was before (already expanded, or closed by the user) */
1002 expand = ! gtk_tree_model_iter_has_child(model, parent);
1004 /* insert the new element */
1005 name = get_symbol_name(doc, tag, parent_name != NULL);
1006 tooltip = get_symbol_tooltip(doc, tag);
1007 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1008 SYMBOLS_COLUMN_NAME, name,
1009 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1010 SYMBOLS_COLUMN_ICON, icon,
1011 SYMBOLS_COLUMN_TAG, tag,
1012 -1);
1013 g_free(tooltip);
1014 if (G_LIKELY(icon))
1015 g_object_unref(icon);
1017 update_parents_table(parents_table, tag, &iter);
1019 if (expand)
1020 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1024 g_hash_table_destroy(parents_table);
1025 g_hash_table_destroy(tags_table);
1029 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1030 * is not stable, so the order is already lost. */
1031 static gint compare_top_level_names(const gchar *a, const gchar *b)
1033 guint i;
1034 const gchar *name;
1036 /* This should never happen as it would mean that two or more top
1037 * level items have the same name but it can happen by typos in the translations. */
1038 if (utils_str_equal(a, b))
1039 return 1;
1041 foreach_ptr_array(name, i, top_level_iter_names)
1043 if (utils_str_equal(name, a))
1044 return -1;
1045 if (utils_str_equal(name, b))
1046 return 1;
1048 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1049 return 0;
1053 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1054 GtkTreeIter *iter)
1056 /* if the tag has a parent tag, it should be at depth >= 2 */
1057 return !EMPTY(tag->scope) &&
1058 gtk_tree_store_iter_depth(store, iter) == 1;
1062 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1063 gpointer user_data)
1065 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1066 TMTag *tag_a, *tag_b;
1067 gint cmp;
1069 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1070 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1072 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1073 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1074 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1075 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1077 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1078 compare_symbol_lines(tag_a, tag_b);
1080 else
1082 gchar *astr, *bstr;
1084 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1085 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1087 /* if a is toplevel, b must be also */
1088 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1090 cmp = compare_top_level_names(astr, bstr);
1092 else
1094 /* this is what g_strcmp0() does */
1095 if (! astr)
1096 cmp = -(astr != bstr);
1097 else if (! bstr)
1098 cmp = astr != bstr;
1099 else
1101 cmp = strcmp(astr, bstr);
1103 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1104 if (tag_a && tag_b)
1105 if (!sort_by_name ||
1106 (utils_str_equal(tag_a->name, tag_b->name) &&
1107 utils_str_equal(tag_a->scope, tag_b->scope)))
1108 cmp = compare_symbol_lines(tag_a, tag_b);
1111 g_free(astr);
1112 g_free(bstr);
1114 tm_tag_unref(tag_a);
1115 tm_tag_unref(tag_b);
1117 return cmp;
1121 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1123 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1124 GINT_TO_POINTER(sort_by_name), NULL);
1126 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1130 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1132 GList *tags;
1134 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1136 tags = get_tag_list(doc, tm_tag_max_t);
1137 if (tags == NULL)
1138 return FALSE;
1140 /* FIXME: Not sure why we detached the model here? */
1142 /* disable sorting during update because the code doesn't support correctly
1143 * models that are currently being built */
1144 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1146 /* add grandparent type iters */
1147 add_top_level_items(doc);
1149 update_tree_tags(doc, &tags);
1150 g_list_free(tags);
1152 hide_empty_rows(doc->priv->tag_store);
1154 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1155 sort_mode = doc->priv->symbol_list_sort_mode;
1157 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1158 doc->priv->symbol_list_sort_mode = sort_mode;
1160 return TRUE;
1164 /* Detects a global tags filetype from the *.lang.* language extension.
1165 * Returns NULL if there was no matching TM language. */
1166 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1168 gchar *tags_ext;
1169 gchar *shortname = utils_strdupa(utf8_filename);
1170 GeanyFiletype *ft = NULL;
1172 tags_ext = g_strrstr(shortname, ".tags");
1173 if (tags_ext)
1175 *tags_ext = '\0'; /* remove .tags extension */
1176 ft = filetypes_detect_from_extension(shortname);
1177 if (ft->id != GEANY_FILETYPES_NONE)
1178 return ft;
1180 return NULL;
1184 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1185 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1186 * the relevant path.
1187 * Example:
1188 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1189 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1191 /* -E pre-process, -dD output user macros, -p prof info (?) */
1192 const char pre_process[] = "gcc -E -dD -p -I.";
1194 if (argc > 2)
1196 /* Create global taglist */
1197 int status;
1198 char *command;
1199 const char *tags_file = argv[1];
1200 char *utf8_fname;
1201 GeanyFiletype *ft;
1203 utf8_fname = utils_get_utf8_from_locale(tags_file);
1204 ft = detect_global_tags_filetype(utf8_fname);
1205 g_free(utf8_fname);
1207 if (ft == NULL)
1209 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1210 return 1;
1212 /* load config in case of custom filetypes */
1213 filetypes_load_config(ft->id, FALSE);
1215 /* load ignore list for C/C++ parser */
1216 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1217 load_c_ignore_tags();
1219 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1221 const gchar *cflags = getenv("CFLAGS");
1222 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1224 else
1225 command = NULL; /* don't preprocess */
1227 geany_debug("Generating %s tags file.", ft->name);
1228 tm_get_workspace();
1229 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1230 argc - 2, tags_file, ft->lang);
1231 g_free(command);
1232 symbols_finalize(); /* free c_tags_ignore data */
1233 if (! status)
1235 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1236 "were found.\n"));
1237 return 1;
1240 else
1242 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv[0]);
1243 g_printerr(_("Example:\n"
1244 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1245 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1246 return 1;
1248 return 0;
1252 void symbols_show_load_tags_dialog(void)
1254 GtkWidget *dialog;
1255 GtkFileFilter *filter;
1257 dialog = gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets.window),
1258 GTK_FILE_CHOOSER_ACTION_OPEN,
1259 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1260 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1261 NULL);
1262 gtk_widget_set_name(dialog, "GeanyDialog");
1263 filter = gtk_file_filter_new();
1264 gtk_file_filter_set_name(filter, _("Geany tags file (*.*.tags)"));
1265 gtk_file_filter_add_pattern(filter, "*.*.tags");
1266 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1268 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1270 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1271 GSList *item;
1273 for (item = flist; item != NULL; item = g_slist_next(item))
1275 gchar *fname = item->data;
1276 gchar *utf8_fname;
1277 GeanyFiletype *ft;
1279 utf8_fname = utils_get_utf8_from_locale(fname);
1280 ft = detect_global_tags_filetype(utf8_fname);
1282 if (ft != NULL && symbols_load_global_tags(fname, ft))
1283 /* For translators: the first wildcard is the filetype, the second the filename */
1284 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1285 filetypes_get_display_name(ft), utf8_fname);
1286 else
1287 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1289 g_free(utf8_fname);
1290 g_free(fname);
1292 g_slist_free(flist);
1294 gtk_widget_destroy(dialog);
1298 static void init_user_tags(void)
1300 GSList *file_list = NULL, *list = NULL;
1301 const GSList *node;
1302 gchar *dir;
1304 dir = g_build_filename(app->configdir, GEANY_TAGS_SUBDIR, NULL);
1305 /* create the user tags dir for next time if it doesn't exist */
1306 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1307 utils_mkdir(dir, FALSE);
1308 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1310 SETPTR(dir, g_build_filename(app->datadir, GEANY_TAGS_SUBDIR, NULL));
1311 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1312 g_free(dir);
1314 file_list = g_slist_concat(file_list, list);
1316 /* populate the filetype-specific tag files lists */
1317 for (node = file_list; node != NULL; node = node->next)
1319 gchar *fname = node->data;
1320 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1321 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1323 g_free(utf8_fname);
1325 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1326 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1327 else
1329 geany_debug("Unknown filetype for file '%s'.", fname);
1330 g_free(fname);
1334 /* don't need to delete list contents because they are now stored in
1335 * ft->priv->tag_files */
1336 g_slist_free(file_list);
1340 static void load_user_tags(GeanyFiletypeID ft_id)
1342 static guchar *tags_loaded = NULL;
1343 static gboolean init_tags = FALSE;
1344 const GSList *node;
1345 GeanyFiletype *ft = filetypes[ft_id];
1347 g_return_if_fail(ft_id > 0);
1349 if (!tags_loaded)
1350 tags_loaded = g_new0(guchar, filetypes_array->len);
1351 if (tags_loaded[ft_id])
1352 return;
1353 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1355 if (!init_tags)
1357 init_user_tags();
1358 init_tags = TRUE;
1361 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1363 const gchar *fname = node->data;
1365 symbols_load_global_tags(fname, ft);
1370 static void on_goto_popup_item_activate(GtkMenuItem *item, TMTag *tag)
1372 GeanyDocument *new_doc, *old_doc;
1374 g_return_if_fail(tag);
1376 old_doc = document_get_current();
1377 new_doc = document_open_file(tag->file->file_name, FALSE, NULL, NULL);
1379 if (new_doc)
1380 navqueue_goto_line(old_doc, new_doc, tag->line);
1384 static guint get_tag_class(const TMTag *tag)
1386 gint group = tm_parser_get_sidebar_group(tag->lang, tag->type);
1388 if (group >= 0)
1390 guint icon_id;
1391 if (tm_parser_get_sidebar_info(tag->lang, group, &icon_id))
1392 return icon_id;
1395 return TM_ICON_STRUCT;
1399 /* positions a popup at the caret from the ScintillaObject in @p data */
1400 static void goto_popup_position_func(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1402 gint line_height;
1403 GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(menu));
1404 gint monitor_num;
1405 GdkRectangle monitor;
1406 GtkRequisition req;
1407 GdkEventButton *event_button = g_object_get_data(G_OBJECT(menu), "geany-button-event");
1409 if (event_button)
1411 /* if we got a mouse click, popup at that position */
1412 *x = (gint) event_button->x_root;
1413 *y = (gint) event_button->y_root;
1414 line_height = 0; /* we don't want to offset below the line or anything */
1416 else /* keyboard positioning */
1418 ScintillaObject *sci = data;
1419 GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(sci));
1420 gint pos = sci_get_current_position(sci);
1421 gint line = sci_get_line_from_position(sci, pos);
1422 gint pos_x = SSM(sci, SCI_POINTXFROMPOSITION, 0, pos);
1423 gint pos_y = SSM(sci, SCI_POINTYFROMPOSITION, 0, pos);
1425 line_height = SSM(sci, SCI_TEXTHEIGHT, line, 0);
1427 gdk_window_get_origin(window, x, y);
1428 *x += pos_x;
1429 *y += pos_y;
1432 monitor_num = gdk_screen_get_monitor_at_point(screen, *x, *y);
1434 gtk_widget_get_preferred_size(GTK_WIDGET(menu), NULL, &req);
1436 #if GTK_CHECK_VERSION(3, 4, 0)
1437 gdk_screen_get_monitor_workarea(screen, monitor_num, &monitor);
1438 #else
1439 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
1440 #endif
1442 /* put on one size of the X position, but within the monitor */
1443 if (gtk_widget_get_direction(GTK_WIDGET(menu)) == GTK_TEXT_DIR_RTL)
1445 if (*x - req.width - 1 >= monitor.x)
1446 *x -= req.width + 1;
1447 else if (*x + req.width > monitor.x + monitor.width)
1448 *x = monitor.x;
1449 else
1450 *x += 1;
1452 else
1454 if (*x + req.width + 1 <= monitor.x + monitor.width)
1455 *x = MAX(monitor.x, *x + 1);
1456 else if (*x - req.width - 1 >= monitor.x)
1457 *x -= req.width + 1;
1458 else
1459 *x = monitor.x + MAX(0, monitor.width - req.width);
1462 /* try to put, in order:
1463 * 1. below the Y position, under the line
1464 * 2. above the Y position
1465 * 3. within the monitor */
1466 if (*y + line_height + req.height <= monitor.y + monitor.height)
1467 *y = MAX(monitor.y, *y + line_height);
1468 else if (*y - req.height >= monitor.y)
1469 *y = *y - req.height;
1470 else
1471 *y = monitor.y + MAX(0, monitor.height - req.height);
1473 *push_in = FALSE;
1477 static void show_goto_popup(GeanyDocument *doc, GPtrArray *tags, gboolean have_best)
1479 GtkWidget *first = NULL;
1480 GtkWidget *menu;
1481 GdkEvent *event;
1482 GdkEventButton *button_event = NULL;
1483 TMTag *tmtag;
1484 guint i;
1485 gchar **short_names, **file_names;
1486 menu = gtk_menu_new();
1488 /* If popup would show multiple files present a smart file list that allows
1489 * to easily distinguish the files while avoiding the file paths in their entirety */
1490 file_names = g_new(gchar *, tags->len);
1491 foreach_ptr_array(tmtag, i, tags)
1492 file_names[i] = tmtag->file->file_name;
1493 short_names = utils_strv_shorten_file_list(file_names, tags->len);
1494 g_free(file_names);
1496 foreach_ptr_array(tmtag, i, tags)
1498 GtkWidget *item;
1499 GtkWidget *label;
1500 GtkWidget *image;
1501 gchar *fname = short_names[i];
1502 gchar *text;
1504 if (! first && have_best)
1505 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1506 text = g_markup_printf_escaped(_("<b>%s: %lu</b>"), fname, tmtag->line);
1507 else
1508 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1509 text = g_markup_printf_escaped(_("%s: %lu"), fname, tmtag->line);
1511 image = gtk_image_new_from_pixbuf(symbols_icons[get_tag_class(tmtag)].pixbuf);
1512 label = g_object_new(GTK_TYPE_LABEL, "label", text, "use-markup", TRUE, "xalign", 0.0, NULL);
1513 item = g_object_new(GTK_TYPE_IMAGE_MENU_ITEM, "image", image, "child", label, "always-show-image", TRUE, NULL);
1514 g_signal_connect_data(item, "activate", G_CALLBACK(on_goto_popup_item_activate),
1515 tm_tag_ref(tmtag), (GClosureNotify) tm_tag_unref, 0);
1516 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1518 if (! first)
1519 first = item;
1521 g_free(text);
1522 g_free(fname);
1524 g_free(short_names);
1526 gtk_widget_show_all(menu);
1528 if (first) /* always select the first item for better keyboard navigation */
1529 g_signal_connect(menu, "realize", G_CALLBACK(gtk_menu_shell_select_item), first);
1531 event = gtk_get_current_event();
1532 if (event && event->type == GDK_BUTTON_PRESS)
1533 button_event = (GdkEventButton *) event;
1534 else
1535 gdk_event_free(event);
1537 g_object_set_data_full(G_OBJECT(menu), "geany-button-event", button_event,
1538 button_event ? (GDestroyNotify) gdk_event_free : NULL);
1539 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, goto_popup_position_func, doc->editor->sci,
1540 button_event ? button_event->button : 0, gtk_get_current_event_time ());
1544 static gint compare_tags_by_name_line(gconstpointer ptr1, gconstpointer ptr2)
1546 gint res;
1547 TMTag *t1 = *((TMTag **) ptr1);
1548 TMTag *t2 = *((TMTag **) ptr2);
1550 res = g_strcmp0(t1->file->short_name, t2->file->short_name);
1551 if (res != 0)
1552 return res;
1553 return t1->line - t2->line;
1557 static TMTag *find_best_goto_tag(GeanyDocument *doc, GPtrArray *tags)
1559 TMTag *tag;
1560 guint i;
1562 /* first check if we have a tag in the current file */
1563 foreach_ptr_array(tag, i, tags)
1565 if (g_strcmp0(doc->real_path, tag->file->file_name) == 0)
1566 return tag;
1569 /* next check if we have a tag for some of the open documents */
1570 foreach_ptr_array(tag, i, tags)
1572 guint j;
1574 foreach_document(j)
1576 if (g_strcmp0(documents[j]->real_path, tag->file->file_name) == 0)
1577 return tag;
1581 /* next check if we have a tag for a file inside the current document's directory */
1582 foreach_ptr_array(tag, i, tags)
1584 gchar *dir = g_path_get_dirname(doc->real_path);
1586 if (g_str_has_prefix(tag->file->file_name, dir))
1588 g_free(dir);
1589 return tag;
1591 g_free(dir);
1594 return NULL;
1598 static GPtrArray *filter_tags(GPtrArray *tags, TMTag *current_tag, gboolean definition)
1600 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1601 TMTag *tmtag, *last_tag = NULL;
1602 GPtrArray *filtered_tags = g_ptr_array_new();
1603 guint i;
1605 foreach_ptr_array(tmtag, i, tags)
1607 if ((definition && !(tmtag->type & forward_types)) ||
1608 (!definition && (tmtag->type & forward_types)))
1610 /* If there are typedefs of e.g. a struct such as
1611 * "typedef struct Foo {} Foo;", filter out the typedef unless
1612 * cursor is at the struct name. */
1613 if (last_tag != NULL && last_tag->file == tmtag->file &&
1614 last_tag->type != tm_tag_typedef_t && tmtag->type == tm_tag_typedef_t)
1616 if (last_tag == current_tag)
1617 g_ptr_array_add(filtered_tags, tmtag);
1619 else if (tmtag != current_tag)
1620 g_ptr_array_add(filtered_tags, tmtag);
1622 last_tag = tmtag;
1626 return filtered_tags;
1630 static gboolean goto_tag(const gchar *name, gboolean definition)
1632 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1633 TMTag *tmtag, *current_tag = NULL;
1634 GeanyDocument *old_doc = document_get_current();
1635 gboolean found = FALSE;
1636 const GPtrArray *all_tags;
1637 GPtrArray *tags, *filtered_tags;
1638 guint i;
1639 guint current_line = sci_get_current_line(old_doc->editor->sci) + 1;
1641 all_tags = tm_workspace_find(name, NULL, tm_tag_max_t, NULL, old_doc->file_type->lang);
1643 /* get rid of global tags and find tag at current line */
1644 tags = g_ptr_array_new();
1645 foreach_ptr_array(tmtag, i, all_tags)
1647 if (tmtag->file)
1649 g_ptr_array_add(tags, tmtag);
1650 if (tmtag->file == old_doc->tm_file && tmtag->line == current_line)
1651 current_tag = tmtag;
1655 if (current_tag)
1656 /* swap definition/declaration search */
1657 definition = current_tag->type & forward_types;
1659 filtered_tags = filter_tags(tags, current_tag, definition);
1660 if (filtered_tags->len == 0)
1662 /* if we didn't find anything, try again with the opposite type */
1663 g_ptr_array_free(filtered_tags, TRUE);
1664 filtered_tags = filter_tags(tags, current_tag, !definition);
1666 g_ptr_array_free(tags, TRUE);
1667 tags = filtered_tags;
1669 if (tags->len == 1)
1671 GeanyDocument *new_doc;
1673 tmtag = tags->pdata[0];
1674 new_doc = document_find_by_real_path(tmtag->file->file_name);
1676 if (!new_doc)
1677 /* not found in opened document, should open */
1678 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1680 navqueue_goto_line(old_doc, new_doc, tmtag->line);
1682 else if (tags->len > 1)
1684 GPtrArray *tag_list;
1685 TMTag *tag, *best_tag;
1687 g_ptr_array_sort(tags, compare_tags_by_name_line);
1688 best_tag = find_best_goto_tag(old_doc, tags);
1690 tag_list = g_ptr_array_new();
1691 if (best_tag)
1692 g_ptr_array_add(tag_list, best_tag);
1693 foreach_ptr_array(tag, i, tags)
1695 if (tag != best_tag)
1696 g_ptr_array_add(tag_list, tag);
1698 show_goto_popup(old_doc, tag_list, best_tag != NULL);
1700 g_ptr_array_free(tag_list, TRUE);
1703 found = tags->len > 0;
1704 g_ptr_array_free(tags, TRUE);
1706 return found;
1710 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1712 if (goto_tag(name, definition))
1713 return TRUE;
1715 /* if we are here, there was no match and we are beeping ;-) */
1716 utils_beep();
1718 if (!definition)
1719 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1720 else
1721 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1722 return FALSE;
1726 /* This could perhaps be improved to check for #if, class etc. */
1727 static gint get_function_fold_number(GeanyDocument *doc)
1729 /* for Java the functions are always one fold level above the class scope */
1730 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1731 return SC_FOLDLEVELBASE + 1;
1732 else
1733 return SC_FOLDLEVELBASE;
1737 /* Should be used only with get_current_tag_cached.
1738 * tag_types caching might trigger recomputation too often but this isn't used differently often
1739 * enough to be an issue for now */
1740 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1742 static gint old_line = -2;
1743 static GeanyDocument *old_doc = NULL;
1744 static gint old_fold_num = -1;
1745 static guint old_tag_types = 0;
1746 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1747 gboolean ret;
1749 /* check if the cached line and file index have changed since last time: */
1750 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1751 ret = TRUE;
1752 else if (cur_line == old_line)
1753 ret = FALSE;
1754 else
1756 /* if the line has only changed by 1 */
1757 if (abs(cur_line - old_line) == 1)
1759 /* It's the same function if the fold number hasn't changed */
1760 ret = (fold_num != old_fold_num);
1762 else ret = TRUE;
1765 /* record current line and file index for next time */
1766 old_line = cur_line;
1767 old_doc = doc;
1768 old_fold_num = fold_num;
1769 old_tag_types = tag_types;
1770 return ret;
1774 /* Parse the function name up to 2 lines before tag_line.
1775 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1776 * type or argument names can be confused with the function name. */
1777 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1779 gint start, end, max_pos;
1780 gint fn_style;
1782 switch (sci_get_lexer(sci))
1784 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
1785 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
1786 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
1788 start = sci_get_position_from_line(sci, tag_line - 2);
1789 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1790 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
1791 start++;
1793 end = start;
1794 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
1795 end++;
1797 if (start == end)
1798 return NULL;
1799 return sci_get_contents_range(sci, start, end);
1803 /* Parse the function name */
1804 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
1806 gint start, end, first_pos, max_pos;
1807 gint tmp;
1808 gchar c;
1810 first_pos = end = sci_get_position_from_line(sci, tag_line);
1811 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1812 tmp = 0;
1813 /* goto the begin of function body */
1814 while (end < max_pos &&
1815 (tmp = sci_get_char_at(sci, end)) != '{' &&
1816 tmp != 0) end++;
1817 if (tmp == 0) end --;
1819 /* go back to the end of function identifier */
1820 while (end > 0 && end > first_pos - 500 &&
1821 (tmp = sci_get_char_at(sci, end)) != '(' &&
1822 tmp != 0) end--;
1823 end--;
1824 if (end < 0) end = 0;
1826 /* skip whitespaces between identifier and ( */
1827 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
1829 start = end;
1830 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1831 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
1832 || tmp == SCE_C_GLOBALCLASS
1833 || (c = sci_get_char_at(sci, start)) == '~'
1834 || c == ':'))
1835 start--;
1836 if (start != 0 && start < end) start++; /* correct for last non-matching char */
1838 if (start == end) return NULL;
1839 return sci_get_contents_range(sci, start, end + 1);
1843 /* gets the fold header after or on @line, but skipping folds created because of parentheses */
1844 static gint get_fold_header_after(ScintillaObject *sci, gint line)
1846 const gint line_count = sci_get_line_count(sci);
1848 for (; line < line_count; line++)
1850 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
1852 const gint last_child = SSM(sci, SCI_GETLASTCHILD, line, -1);
1853 const gint line_end = sci_get_line_end_position(sci, line);
1854 const gint lexer = sci_get_lexer(sci);
1855 gint parenthesis_match_line = -1;
1857 /* now find any unbalanced open parenthesis on the line and see where the matching
1858 * brace would be, mimicking what folding on () does */
1859 for (gint pos = sci_get_position_from_line(sci, line); pos < line_end; pos++)
1861 if (highlighting_is_code_style(lexer, sci_get_style_at(sci, pos)) &&
1862 sci_get_char_at(sci, pos) == '(')
1864 const gint matching = sci_find_matching_brace(sci, pos);
1866 if (matching >= 0)
1868 parenthesis_match_line = sci_get_line_from_position(sci, matching);
1869 if (parenthesis_match_line != line)
1870 break; /* match is on a different line, we found a possible fold */
1871 else
1872 pos = matching; /* just skip the range and continue searching */
1877 /* if the matching parenthesis matches the fold level, skip it and continue.
1878 * it matches if it either spans the same lines, or spans one more but the next one is
1879 * a fold header (in which case the last child of the fold is one less to let the
1880 * header be at the parent level) */
1881 if ((parenthesis_match_line == last_child) ||
1882 (parenthesis_match_line == last_child + 1 &&
1883 sci_get_fold_level(sci, parenthesis_match_line) & SC_FOLDLEVELHEADERFLAG))
1884 line = last_child;
1885 else
1886 return line;
1890 return -1;
1894 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
1896 gint line;
1897 gint parent;
1899 line = sci_get_current_line(doc->editor->sci);
1900 parent = sci_get_fold_parent(doc->editor->sci, line);
1901 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
1902 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
1903 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
1905 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
1907 if (tag)
1909 gint tag_line = tag->line - 1;
1910 gint last_child = line + 1;
1912 /* if it may be a false positive because we're inside a fold level not inside anything
1913 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
1914 * right after the tag we got from TM.
1915 * Additionally, we perform parentheses matching on the initial line not to get confused
1916 * by folding on () in case the parameter list spans multiple lines */
1917 if (abs(tag_line - parent) > 1)
1919 const gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
1920 if (tag_fold >= 0)
1921 last_child = SSM(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
1924 if (line <= last_child)
1926 if (tag->scope)
1927 *tagname = g_strconcat(tag->scope,
1928 tm_parser_scope_separator(tag->lang), tag->name, NULL);
1929 else
1930 *tagname = g_strdup(tag->name);
1932 return tag_line;
1936 /* for the poor guy with a modified document and without real time tag parsing, we fallback
1937 * to dirty and inaccurate hand-parsing */
1938 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
1940 const gint fn_fold = get_function_fold_number(doc);
1941 gint tag_line = parent;
1942 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1944 /* find the top level fold point */
1945 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
1947 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
1948 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1951 if (tag_line >= 0)
1953 gchar *cur_tag;
1955 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
1956 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
1957 else
1958 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
1960 if (cur_tag != NULL)
1962 *tagname = cur_tag;
1963 return tag_line;
1968 *tagname = g_strdup(_("unknown"));
1969 return -1;
1973 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
1975 static gint tag_line = -1;
1976 static gchar *cur_tag = NULL;
1978 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
1980 if (doc == NULL) /* reset current function */
1982 current_tag_changed(NULL, -1, -1, 0);
1983 g_free(cur_tag);
1984 cur_tag = g_strdup(_("unknown"));
1985 if (tagname != NULL)
1986 *tagname = cur_tag;
1987 tag_line = -1;
1989 else
1991 gint line = sci_get_current_line(doc->editor->sci);
1992 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
1994 if (current_tag_changed(doc, line, fold_level, tag_types))
1996 g_free(cur_tag);
1997 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
1999 *tagname = cur_tag;
2002 return tag_line;
2006 /* Sets *tagname to point at the current function or tag name.
2007 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2008 * call to this function.
2009 * Returns: line number of the current tag, or -1 if unknown. */
2010 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2012 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2016 /* same as symbols_get_current_function() but finds class, namespaces and more */
2017 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2019 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2020 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t | tm_tag_namespace_t);
2022 return get_current_tag_name_cached(doc, tagname, tag_types);
2026 const gchar *symbols_get_icon_name(guint icon_id)
2028 if (icon_id < TM_N_ICONS)
2029 return symbols_icons[icon_id].icon_name;
2030 return NULL;
2034 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2036 gint sort_mode = GPOINTER_TO_INT(user_data);
2037 GeanyDocument *doc = document_get_current();
2039 if (ignore_callback)
2040 return;
2042 if (doc != NULL)
2043 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2047 static void on_symbol_tree_menu_show(GtkWidget *widget,
2048 gpointer user_data)
2050 GeanyDocument *doc = document_get_current();
2051 gboolean enable;
2053 enable = doc && doc->has_tags;
2054 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2055 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2056 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2057 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2058 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2059 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2061 if (! doc)
2062 return;
2064 ignore_callback = TRUE;
2066 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2067 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2068 else
2069 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2071 ignore_callback = FALSE;
2075 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2077 gboolean expand = GPOINTER_TO_INT(user_data);
2078 GeanyDocument *doc = document_get_current();
2080 if (! doc)
2081 return;
2083 g_return_if_fail(doc->priv->tag_tree);
2085 if (expand)
2086 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2087 else
2088 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2092 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2094 GtkTreeIter iter;
2095 GtkTreeSelection *selection;
2096 GtkTreeModel *model;
2097 GeanyDocument *doc;
2098 TMTag *tag = NULL;
2100 doc = document_get_current();
2101 if (!doc)
2102 return;
2104 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2105 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2106 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2107 if (tag)
2109 if (widget == symbol_menu.find_in_files)
2110 search_show_find_in_files_dialog_full(tag->name, NULL);
2111 else
2112 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2113 widget == symbol_menu.find_usage);
2115 tm_tag_unref(tag);
2120 static void create_taglist_popup_menu(void)
2122 GtkWidget *item, *menu;
2124 tv.popup_taglist = menu = gtk_menu_new();
2126 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2127 gtk_widget_show(item);
2128 gtk_container_add(GTK_CONTAINER(menu), item);
2129 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2131 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2132 gtk_widget_show(item);
2133 gtk_container_add(GTK_CONTAINER(menu), item);
2134 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2136 item = gtk_separator_menu_item_new();
2137 gtk_widget_show(item);
2138 gtk_container_add(GTK_CONTAINER(menu), item);
2140 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2141 _("Sort by _Name"));
2142 gtk_widget_show(item);
2143 gtk_container_add(GTK_CONTAINER(menu), item);
2144 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2145 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2147 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2148 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2149 gtk_widget_show(item);
2150 gtk_container_add(GTK_CONTAINER(menu), item);
2151 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2152 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2154 item = gtk_separator_menu_item_new();
2155 gtk_widget_show(item);
2156 gtk_container_add(GTK_CONTAINER(menu), item);
2158 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2159 gtk_widget_show(item);
2160 gtk_container_add(GTK_CONTAINER(menu), item);
2161 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2163 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2164 gtk_widget_show(item);
2165 gtk_container_add(GTK_CONTAINER(menu), item);
2166 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2168 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2169 gtk_widget_show(item);
2170 gtk_container_add(GTK_CONTAINER(menu), item);
2171 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2173 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2175 sidebar_add_common_menu_items(GTK_MENU(menu));
2179 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2181 gchar *f;
2183 g_return_if_fail(!EMPTY(doc->real_path));
2185 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2186 if (utils_str_equal(doc->real_path, f))
2187 load_c_ignore_tags();
2189 g_free(f);
2193 void symbols_init(void)
2195 gchar *f;
2196 guint i;
2198 create_taglist_popup_menu();
2200 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2201 ui_add_config_file_menu_item(f, NULL, NULL);
2202 g_free(f);
2204 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2206 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2207 symbols_icons[i].pixbuf = get_tag_icon(symbols_icons[i].icon_name);
2211 void symbols_finalize(void)
2213 guint i;
2215 g_strfreev(c_tags_ignore);
2217 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2219 if (symbols_icons[i].pixbuf)
2220 g_object_unref(symbols_icons[i].pixbuf);