"Fix" some GCC -Wparentheses warnings
[geany-mirror.git] / src / symbols.c
blobf85df5a596824db3d68a52d0c67b6695d54f2d0d
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;
103 GtkWidget *group_by_type;
105 symbol_menu;
107 static void load_user_tags(GeanyFiletypeID ft_id);
109 /* get the tags_ignore list, exported by geany_lcpp.c */
110 extern gchar **c_tags_ignore;
112 /* ignore certain tokens when parsing C-like syntax.
113 * Also works for reloading. */
114 static void load_c_ignore_tags(void)
116 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
117 gchar *content;
119 if (g_file_get_contents(path, &content, NULL, NULL))
121 gchar **line;
123 /* historically we ignore the glib _DECLS for tag generation */
124 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
126 g_strfreev(c_tags_ignore);
127 tm_ctags_clear_ignore_symbols();
129 /* for old c.c parser */
130 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
131 /* for new cxx parser */
132 foreach_strv(line, c_tags_ignore)
134 tm_ctags_add_ignore_symbol(*line);
137 g_free(content);
139 g_free(path);
143 void symbols_reload_config_files(void)
145 load_c_ignore_tags();
149 static gsize get_tag_count(void)
151 GPtrArray *tags = tm_get_workspace()->global_tags;
152 gsize count = tags ? tags->len : 0;
154 return count;
158 /* wrapper for tm_workspace_load_global_tags().
159 * note that the tag count only counts new global tags added - if a tag has the same name,
160 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
161 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
163 gboolean result;
164 gsize old_tag_count = get_tag_count();
166 result = tm_workspace_load_global_tags(tags_file, ft->lang);
167 if (result)
169 geany_debug("Loaded %s (%s), %u symbol(s).", tags_file, ft->name,
170 (guint) (get_tag_count() - old_tag_count));
172 return result;
176 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
177 * This provides autocompletion, calltips, etc. */
178 void symbols_global_tags_loaded(guint file_type_idx)
180 /* load ignore list for C/C++ parser */
181 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
182 c_tags_ignore == NULL)
184 load_c_ignore_tags();
187 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
188 return;
190 /* load config in case of custom filetypes */
191 filetypes_load_config(file_type_idx, FALSE);
193 load_user_tags(file_type_idx);
195 switch (file_type_idx)
197 case GEANY_FILETYPES_CPP:
198 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
199 break;
200 case GEANY_FILETYPES_PHP:
201 symbols_global_tags_loaded(GEANY_FILETYPES_HTML); /* load HTML global tags */
202 break;
207 GString *symbols_find_typenames_as_string(TMParserType lang, gboolean global)
209 guint j;
210 TMTag *tag;
211 GString *s = NULL;
212 GPtrArray *typedefs;
213 TMParserType tag_lang;
215 if (global)
216 typedefs = app->tm_workspace->global_typename_array;
217 else
218 typedefs = app->tm_workspace->typename_array;
220 if ((typedefs) && (typedefs->len > 0))
222 const gchar *last_name = "";
224 s = g_string_sized_new(typedefs->len * 10);
225 for (j = 0; j < typedefs->len; ++j)
227 tag = TM_TAG(typedefs->pdata[j]);
228 tag_lang = tag->lang;
230 if (tag->name && tm_parser_langs_compatible(lang, tag_lang) &&
231 strcmp(tag->name, last_name) != 0)
233 if (j != 0)
234 g_string_append_c(s, ' ');
235 g_string_append(s, tag->name);
236 last_name = tag->name;
240 return s;
244 /** Gets the context separator used by the tag manager for a particular file
245 * type.
246 * @param ft_id File type identifier.
247 * @return The context separator string.
249 * Returns non-printing sequence "\x03" ie ETX (end of text) for filetypes
250 * without a context separator.
252 * @since 0.19
254 GEANY_API_SYMBOL
255 const gchar *symbols_get_context_separator(gint ft_id)
257 return tm_parser_scope_separator(filetypes[ft_id]->lang);
261 /* sort by name, then line */
262 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
264 gint ret;
266 if (tag_a == NULL || tag_b == NULL)
267 return 0;
269 if (tag_a->name == NULL)
270 return -(tag_a->name != tag_b->name);
272 if (tag_b->name == NULL)
273 return tag_a->name != tag_b->name;
275 ret = strcmp(tag_a->name, tag_b->name);
276 if (ret == 0)
278 return tag_a->line - tag_b->line;
280 return ret;
284 /* sort by line, then scope */
285 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
287 const TMTag *tag_a = TM_TAG(a);
288 const TMTag *tag_b = TM_TAG(b);
289 gint ret;
291 if (a == NULL || b == NULL)
292 return 0;
294 ret = tag_a->line - tag_b->line;
295 if (ret == 0)
297 if (tag_a->scope == NULL)
298 return -(tag_a->scope != tag_b->scope);
299 if (tag_b->scope == NULL)
300 return tag_a->scope != tag_b->scope;
301 else
302 return strcmp(tag_a->scope, tag_b->scope);
304 return ret;
308 static GList *get_tag_list(GeanyDocument *doc, TMTagType tag_types)
310 GList *tag_names = NULL;
311 guint i;
312 gchar **tf_strv;
314 g_return_val_if_fail(doc, NULL);
316 if (! doc->tm_file || ! doc->tm_file->tags_array)
317 return NULL;
319 tf_strv = g_strsplit_set(doc->priv->tag_filter, " ", -1);
321 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
323 TMTag *tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
325 if (tag->type & tag_types)
327 gboolean filtered = FALSE;
328 gchar **val;
329 gchar *full_tagname = g_strconcat(tag->scope ? tag->scope : "",
330 tag->scope ? tm_parser_scope_separator_printable(tag->lang) : "",
331 tag->name, NULL);
332 gchar *normalized_tagname = g_utf8_normalize(full_tagname, -1, G_NORMALIZE_ALL);
334 foreach_strv(val, tf_strv)
336 gchar *normalized_val = g_utf8_normalize(*val, -1, G_NORMALIZE_ALL);
338 if (normalized_tagname != NULL && normalized_val != NULL)
340 gchar *case_normalized_tagname = g_utf8_casefold(normalized_tagname, -1);
341 gchar *case_normalized_val = g_utf8_casefold(normalized_val, -1);
343 filtered = strstr(case_normalized_tagname, case_normalized_val) == NULL;
344 g_free(case_normalized_tagname);
345 g_free(case_normalized_val);
347 g_free(normalized_val);
349 if (filtered)
350 break;
352 if (!filtered)
353 tag_names = g_list_prepend(tag_names, tag);
355 g_free(normalized_tagname);
356 g_free(full_tagname);
359 tag_names = g_list_sort(tag_names, compare_symbol_lines);
361 g_strfreev(tf_strv);
363 return tag_names;
367 /* amount of types in the symbol list - can be increased if needed */
368 #define MAX_SYMBOL_TYPES 15
370 GtkTreeIter tv_iters[MAX_SYMBOL_TYPES];
373 static void init_tag_iters(void)
375 guint i;
376 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
377 * filetypes(e.g. config file to Python crashes Geany without this) */
378 for (i = 0; i < MAX_SYMBOL_TYPES; i++)
379 tv_iters[i].stamp = -1;
383 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
385 static GtkIconTheme *icon_theme = NULL;
386 static gint x = -1;
388 if (G_UNLIKELY(x < 0))
390 gint dummy;
391 icon_theme = gtk_icon_theme_get_default();
392 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &dummy);
394 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
398 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
400 GtkTreeModel *model = GTK_TREE_MODEL(store);
402 if (!gtk_tree_model_get_iter_first(model, iter))
403 return FALSE;
406 gchar *candidate;
408 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
409 /* FIXME: what if 2 different items have the same name?
410 * this should never happen, but might be caused by a typo in a translation */
411 if (utils_str_equal(candidate, title))
413 g_free(candidate);
414 return TRUE;
416 else
417 g_free(candidate);
419 while (gtk_tree_model_iter_next(model, iter));
421 return FALSE;
425 static void tag_list_add_groups(GtkTreeStore *tree_store, TMParserType lang)
427 const gchar *title;
428 guint i;
429 guint icon_id;
431 g_return_if_fail(top_level_iter_names);
433 for (i = 0; (title = tm_parser_get_sidebar_info(lang, i, &icon_id)) != NULL; i++)
435 GtkTreeIter *iter = &tv_iters[i];
436 GdkPixbuf *icon = NULL;
438 if (icon_id < TM_N_ICONS)
439 icon = symbols_icons[icon_id].pixbuf;
441 g_assert(title != NULL);
442 g_ptr_array_add(top_level_iter_names, (gchar *)title);
444 if (!find_toplevel_iter(tree_store, iter, title))
445 gtk_tree_store_append(tree_store, iter, NULL);
447 if (icon)
448 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
449 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
454 static void add_top_level_items(GeanyDocument *doc)
456 TMParserType lang = doc->file_type->lang;
457 GtkTreeStore *tag_store = doc->priv->tag_store;
459 if (top_level_iter_names == NULL)
460 top_level_iter_names = g_ptr_array_new();
461 else
462 g_ptr_array_set_size(top_level_iter_names, 0);
464 init_tag_iters();
466 tag_list_add_groups(tag_store, lang);
470 /* removes toplevel items that have no children */
471 static void hide_empty_rows(GtkTreeStore *store)
473 GtkTreeIter iter;
474 gboolean cont = TRUE;
476 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
477 return; /* stop when first iter is invalid, i.e. no elements */
479 while (cont)
481 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
482 cont = gtk_tree_store_remove(store, &iter);
483 else
484 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
489 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
491 gchar *utf8_name;
492 const gchar *scope = tag->scope;
493 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
494 gboolean doc_is_utf8 = FALSE;
496 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
497 * for None at this point completely */
498 if (utils_str_equal(doc->encoding, "UTF-8") ||
499 utils_str_equal(doc->encoding, "None"))
500 doc_is_utf8 = TRUE;
501 else /* normally the tags will always be in UTF-8 since we parse from our buffer, but a
502 * plugin might have called tm_source_file_update(), so check to be sure */
503 doc_is_utf8 = g_utf8_validate(tag->name, -1, NULL);
505 if (! doc_is_utf8)
506 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
507 -1, doc->encoding, TRUE);
508 else
509 utf8_name = tag->name;
511 if (utf8_name == NULL)
512 return NULL;
514 if (! buffer)
515 buffer = g_string_new(NULL);
516 else
517 g_string_truncate(buffer, 0);
519 /* check first char of scope is a wordchar */
520 if (!found_parent && scope &&
521 strpbrk(scope, GEANY_WORDCHARS) == scope)
523 const gchar *sep = tm_parser_scope_separator_printable(tag->lang);
525 g_string_append(buffer, scope);
526 g_string_append(buffer, sep);
528 g_string_append(buffer, utf8_name);
530 if (! doc_is_utf8)
531 g_free(utf8_name);
533 g_string_append_printf(buffer, " [%lu]", tag->line);
535 return buffer->str;
539 // Returns NULL if the tag is not a variable or callable
540 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
542 gchar *utf8_name = tm_parser_format_function(tag->lang, tag->name,
543 tag->arglist, tag->var_type, tag->scope);
545 if (!utf8_name && tag->var_type &&
546 tag->type & (tm_tag_field_t | tm_tag_member_t | tm_tag_variable_t | tm_tag_externvar_t))
548 utf8_name = tm_parser_format_variable(tag->lang, tag->name, tag->var_type);
551 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
552 * for None at this point completely */
553 if (utf8_name != NULL &&
554 ! utils_str_equal(doc->encoding, "UTF-8") &&
555 ! utils_str_equal(doc->encoding, "None"))
557 SETPTR(utf8_name,
558 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
561 return utf8_name;
565 static const gchar *get_parent_name(const TMTag *tag)
567 return !EMPTY(tag->scope) ? tag->scope : NULL;
571 static GtkTreeIter *get_tag_type_iter(TMParserType lang, TMTagType tag_type)
573 /* TODO: tm_parser_get_sidebar_group() goes through groups one by one.
574 * If this happens to be slow for tree construction, create a lookup
575 * table for them. */
576 gint group = tm_parser_get_sidebar_group(lang, tag_type);
578 if (group < 0)
579 return NULL;
581 return &tv_iters[group];
585 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
587 GdkPixbuf *icon = NULL;
589 /* copy parent icon */
590 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
591 SYMBOLS_COLUMN_ICON, &icon, -1);
592 return icon;
596 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
598 const TMTag *t1 = v1;
599 const TMTag *t2 = v2;
601 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
602 utils_str_equal(t1->scope, t2->scope) &&
603 /* include arglist in match to support e.g. C++ overloading */
604 utils_str_equal(t1->arglist, t2->arglist));
608 /* inspired from g_str_hash() */
609 static guint tag_hash(gconstpointer v)
611 const TMTag *tag = v;
612 const gchar *p;
613 guint32 h = 5381;
615 h = (h << 5) + h + tag->type;
616 for (p = tag->name; *p != '\0'; p++)
617 h = (h << 5) + h + *p;
618 if (tag->scope)
620 for (p = tag->scope; *p != '\0'; p++)
621 h = (h << 5) + h + *p;
623 /* for e.g. C++ overloading */
624 if (tag->arglist)
626 for (p = tag->arglist; *p != '\0'; p++)
627 h = (h << 5) + h + *p;
630 return h;
634 /* like gtk_tree_view_expand_to_path() but with an iter */
635 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
637 GtkTreeModel *model = gtk_tree_view_get_model(view);
638 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
640 gtk_tree_view_expand_to_path(view, path);
641 gtk_tree_path_free(path);
645 /* like gtk_tree_store_remove() but finds the next iter at any level */
646 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
648 GtkTreeIter parent;
649 gboolean has_parent;
650 gboolean cont;
652 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
653 cont = gtk_tree_store_remove(store, iter);
654 /* if there is no next at this level but there is a parent iter, continue from it */
655 if (! cont && has_parent)
657 *iter = parent;
658 cont = ui_tree_model_iter_any_next(GTK_TREE_MODEL(store), iter, FALSE);
661 return cont;
665 static gint tree_search_func(gconstpointer key, gpointer user_data)
667 TreeSearchData *data = user_data;
668 gint parent_line = GPOINTER_TO_INT(key);
669 gboolean new_nearest;
671 if (data->found_line == -1)
672 data->found_line = parent_line; /* initial value */
674 new_nearest = ABS(data->line - parent_line) < ABS(data->line - data->found_line);
676 if (parent_line > data->line)
678 if (new_nearest && !data->lower)
679 data->found_line = parent_line;
680 return -1;
683 if (new_nearest)
684 data->found_line = parent_line;
686 if (parent_line < data->line)
687 return 1;
689 return 0;
693 static gint tree_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
695 return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
699 static void parents_table_tree_value_free(gpointer data)
701 g_slice_free(GtkTreeIter, data);
705 /* adds a new element in the parent table if its key is known. */
706 static void update_parents_table(GHashTable *table, const TMTag *tag, const GtkTreeIter *iter)
708 const gchar *name;
709 gchar *name_free = NULL;
710 GTree *tree;
712 if (EMPTY(tag->scope))
714 /* simple case, just use the tag name */
715 name = tag->name;
717 else if (! tm_parser_has_full_scope(tag->lang))
719 /* if the parser doesn't use fully qualified scope, use the name alone but
720 * prevent Foo::Foo from making parent = child */
721 if (utils_str_equal(tag->scope, tag->name))
722 name = NULL;
723 else
724 name = tag->name;
726 else
728 /* build the fully qualified scope as get_parent_name() would return it for a child tag */
729 name_free = g_strconcat(tag->scope, tm_parser_scope_separator(tag->lang), tag->name, NULL);
730 name = name_free;
733 if (name && g_hash_table_lookup_extended(table, name, NULL, (gpointer *) &tree))
735 if (!tree)
737 tree = g_tree_new_full(tree_cmp, NULL, NULL, parents_table_tree_value_free);
738 g_hash_table_insert(table, name_free ? name_free : g_strdup(name), tree);
739 name_free = NULL;
742 g_tree_insert(tree, GINT_TO_POINTER(tag->line), g_slice_dup(GtkTreeIter, iter));
745 g_free(name_free);
749 static GtkTreeIter *parents_table_lookup(GHashTable *table, const gchar *name, guint line)
751 GtkTreeIter *parent_search = NULL;
752 GTree *tree;
754 tree = g_hash_table_lookup(table, name);
755 if (tree)
757 TreeSearchData user_data = {-1, line, TRUE};
759 /* search parent candidates for the one with the nearest
760 * line number which is lower than the tag's line number */
761 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
762 parent_search = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
765 return parent_search;
769 static void parents_table_value_free(gpointer data)
771 GTree *tree = data;
772 if (tree)
773 g_tree_destroy(tree);
777 /* inserts a @data in @table on key @tag.
778 * previous data is not overwritten if the key is duplicated, but rather the
779 * two values are kept in a list
781 * table is: GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
782 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
784 GTree *tree = g_hash_table_lookup(table, tag);
785 if (!tree)
787 tree = g_tree_new_full(tree_cmp, NULL, NULL, NULL);
788 g_hash_table_insert(table, tag, tree);
790 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
791 list = g_list_prepend(list, data);
792 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
796 /* looks up the entry in @table that best matches @tag.
797 * if there is more than one candidate, the one that has closest line position to @tag is chosen */
798 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
800 TreeSearchData user_data = {-1, tag->line, FALSE};
801 GTree *tree = g_hash_table_lookup(table, tag);
803 if (tree)
805 GList *list;
807 g_tree_search(tree, (GCompareFunc)tree_search_func, &user_data);
808 list = g_tree_lookup(tree, GINT_TO_POINTER(user_data.found_line));
809 /* return the first value in the list - we don't care which of the
810 * tags with identical names defined on the same line we get */
811 if (list)
812 return list->data;
814 return NULL;
818 /* removes the element at @tag from @table.
819 * @tag must be the exact pointer used at insertion time */
820 static void tags_table_remove(GHashTable *table, TMTag *tag)
822 GTree *tree = g_hash_table_lookup(table, tag);
823 if (tree)
825 GList *list = g_tree_lookup(tree, GINT_TO_POINTER(tag->line));
826 if (list)
828 GList *node;
829 /* should always be the first element as we returned the first one in
830 * tags_table_lookup() */
831 foreach_list(node, list)
833 if (((GList *) node->data)->data == tag)
834 break;
836 list = g_list_delete_link(list, node);
837 if (!list)
838 g_tree_remove(tree, GINT_TO_POINTER(tag->line));
839 else
840 g_tree_insert(tree, GINT_TO_POINTER(tag->line), list);
846 static gboolean tags_table_tree_value_free(gpointer key, gpointer value, gpointer data)
848 GList *list = value;
849 g_list_free(list);
850 return FALSE;
854 static void tags_table_value_free(gpointer data)
856 GTree *tree = data;
857 if (tree)
859 /* free any leftover elements. note that we can't register a value_free_func when
860 * creating the tree because we only want to free it when destroying the tree,
861 * not when inserting a duplicate (we handle this manually) */
862 g_tree_foreach(tree, tags_table_tree_value_free, NULL);
863 g_tree_destroy(tree);
869 * Updates the tag tree for a document with the tags in *list.
870 * @param doc a document
871 * @param tags a pointer to a GList* holding the tags to add/update. This
872 * list may be updated, removing updated elements.
874 * The update is done in two passes:
875 * 1) walking the current tree, update tags that still exist and remove the
876 * obsolescent ones;
877 * 2) walking the remaining (non updated) tags, adds them in the list.
879 * For better performances, we use 2 hash tables:
880 * - one containing all the tags for lookup in the first pass (actually stores a
881 * reference in the tags list for removing it efficiently), avoiding list search
882 * on each tag;
883 * - the other holding "tag-name":row references for tags having children, used to
884 * lookup for a parent in both passes, avoiding tree traversal.
886 static void update_tree_tags(GeanyDocument *doc, GList **tags)
888 GtkTreeStore *store = doc->priv->tag_store;
889 GtkTreeModel *model = GTK_TREE_MODEL(store);
890 GHashTable *parents_table;
891 GHashTable *tags_table;
892 GtkTreeIter iter;
893 gboolean cont;
894 GList *item;
896 /* Build hash tables holding tags and parents */
897 /* parent table is GHashTable<tag_name, GTree<line_num, GtkTreeIter>>
898 * where tag_name might be a fully qualified name (with scope) if the language
899 * parser reports scope properly (see tm_parser_has_full_scope()). */
900 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, parents_table_value_free);
901 /* tags table is another representation of the @tags list,
902 * GHashTable<TMTag, GTree<line_num, GList<GList<TMTag>>>> */
903 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, tags_table_value_free);
904 foreach_list(item, *tags)
906 TMTag *tag = item->data;
907 const gchar *parent_name;
909 tags_table_insert(tags_table, tag, item);
911 parent_name = get_parent_name(tag);
912 if (parent_name)
913 g_hash_table_insert(parents_table, g_strdup(parent_name), NULL);
916 /* First pass, update existing rows or delete them.
917 * It is OK to delete them since we walk top down so we would remove
918 * parents before checking for their children, thus never implicitly
919 * deleting an updated child */
920 cont = gtk_tree_model_get_iter_first(model, &iter);
921 while (cont)
923 TMTag *tag;
925 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
926 if (! tag) /* most probably a toplevel, skip it */
927 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
928 else
930 GList *found_item;
932 found_item = tags_table_lookup(tags_table, tag);
933 if (! found_item) /* tag doesn't exist, remove it */
934 cont = tree_store_remove_row(store, &iter);
935 else /* tag still exist, update it */
937 const gchar *parent_name;
938 TMTag *found = found_item->data;
940 parent_name = get_parent_name(found);
941 /* if parent is unknown, ignore it */
942 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
943 parent_name = NULL;
945 if (!tm_tags_equal(tag, found))
947 const gchar *name;
948 gchar *tooltip;
950 /* only update fields that (can) have changed (name that holds line
951 * number, tooltip, and the tag itself) */
952 name = get_symbol_name(doc, found, parent_name != NULL);
953 tooltip = get_symbol_tooltip(doc, found);
954 gtk_tree_store_set(store, &iter,
955 SYMBOLS_COLUMN_NAME, name,
956 SYMBOLS_COLUMN_TOOLTIP, tooltip,
957 SYMBOLS_COLUMN_TAG, found,
958 -1);
959 g_free(tooltip);
962 update_parents_table(parents_table, found, &iter);
964 /* remove the updated tag from the table and list */
965 tags_table_remove(tags_table, found);
966 *tags = g_list_delete_link(*tags, found_item);
968 cont = ui_tree_model_iter_any_next(model, &iter, TRUE);
971 tm_tag_unref(tag);
975 /* Second pass, now we have a tree cleaned up from invalid rows,
976 * we simply add new ones */
977 foreach_list (item, *tags)
979 TMTag *tag = item->data;
980 GtkTreeIter *parent, *parent_group;
982 parent_group = get_tag_type_iter(tag->lang, tag->type);
983 /* tv_iters[0] is reserved for the "Symbols" group */
984 parent = ui_prefs.symbols_group_by_type ? parent_group : &tv_iters[0];
985 if (parent_group)
987 gboolean expand;
988 const gchar *name;
989 const gchar *parent_name;
990 gchar *tooltip;
991 GdkPixbuf *icon = get_child_icon(store, parent_group);
993 parent_name = get_parent_name(tag);
994 if (parent_name)
996 GtkTreeIter *parent_search = parents_table_lookup(parents_table, parent_name, tag->line);
998 if (parent_search)
999 parent = parent_search;
1000 else
1001 parent_name = NULL;
1004 /* only expand to the iter if the parent was empty, otherwise we let the
1005 * folding as it was before (already expanded, or closed by the user) */
1006 expand = ! gtk_tree_model_iter_has_child(model, parent);
1008 /* insert the new element */
1009 name = get_symbol_name(doc, tag, parent_name != NULL);
1010 tooltip = get_symbol_tooltip(doc, tag);
1011 gtk_tree_store_insert_with_values(store, &iter, parent, 0,
1012 SYMBOLS_COLUMN_NAME, name,
1013 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1014 SYMBOLS_COLUMN_ICON, icon,
1015 SYMBOLS_COLUMN_TAG, tag,
1016 -1);
1017 g_free(tooltip);
1018 if (G_LIKELY(icon))
1019 g_object_unref(icon);
1021 update_parents_table(parents_table, tag, &iter);
1023 if (expand)
1024 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1028 g_hash_table_destroy(parents_table);
1029 g_hash_table_destroy(tags_table);
1033 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1034 * is not stable, so the order is already lost. */
1035 static gint compare_top_level_names(const gchar *a, const gchar *b)
1037 guint i;
1038 const gchar *name;
1040 /* This should never happen as it would mean that two or more top
1041 * level items have the same name but it can happen by typos in the translations. */
1042 if (utils_str_equal(a, b))
1043 return 1;
1045 foreach_ptr_array(name, i, top_level_iter_names)
1047 if (utils_str_equal(name, a))
1048 return -1;
1049 if (utils_str_equal(name, b))
1050 return 1;
1052 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1053 return 0;
1057 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1058 GtkTreeIter *iter)
1060 /* if the tag has a parent tag, it should be at depth >= 2 */
1061 return !EMPTY(tag->scope) &&
1062 gtk_tree_store_iter_depth(store, iter) == 1;
1066 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1067 gpointer user_data)
1069 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1070 TMTag *tag_a, *tag_b;
1071 gint cmp;
1073 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1074 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1076 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1077 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1078 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1079 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1081 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1082 compare_symbol_lines(tag_a, tag_b);
1084 else
1086 gchar *astr, *bstr;
1088 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1089 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1091 /* if a is toplevel, b must be also */
1092 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1094 cmp = compare_top_level_names(astr, bstr);
1096 else
1098 /* this is what g_strcmp0() does */
1099 if (! astr)
1100 cmp = -(astr != bstr);
1101 else if (! bstr)
1102 cmp = astr != bstr;
1103 else
1105 cmp = strcmp(astr, bstr);
1107 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1108 if (tag_a && tag_b)
1109 if (!sort_by_name ||
1110 (utils_str_equal(tag_a->name, tag_b->name) &&
1111 utils_str_equal(tag_a->scope, tag_b->scope)))
1112 cmp = compare_symbol_lines(tag_a, tag_b);
1115 g_free(astr);
1116 g_free(bstr);
1118 tm_tag_unref(tag_a);
1119 tm_tag_unref(tag_b);
1121 return cmp;
1125 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1127 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1128 GINT_TO_POINTER(sort_by_name), NULL);
1130 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1134 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1136 GList *tags;
1138 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1140 tags = get_tag_list(doc, ~(tm_tag_local_var_t | tm_tag_include_t));
1141 if (tags == NULL)
1142 return FALSE;
1144 if (doc->priv->symbols_group_by_type != ui_prefs.symbols_group_by_type)
1145 gtk_tree_store_clear(doc->priv->tag_store);
1147 doc->priv->symbols_group_by_type = ui_prefs.symbols_group_by_type;
1149 /* FIXME: Not sure why we detached the model here? */
1151 /* disable sorting during update because the code doesn't support correctly
1152 * models that are currently being built */
1153 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1155 /* add grandparent type iters */
1156 add_top_level_items(doc);
1158 update_tree_tags(doc, &tags);
1159 g_list_free(tags);
1161 hide_empty_rows(doc->priv->tag_store);
1163 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1164 sort_mode = doc->priv->symbol_list_sort_mode;
1166 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1167 doc->priv->symbol_list_sort_mode = sort_mode;
1169 return TRUE;
1173 /* Detects a global tags filetype from the *.lang.* language extension.
1174 * Returns NULL if there was no matching TM language. */
1175 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1177 gchar *tags_ext;
1178 gchar *shortname = utils_strdupa(utf8_filename);
1179 GeanyFiletype *ft = NULL;
1181 tags_ext = g_strrstr(shortname, ".tags");
1182 if (tags_ext)
1184 *tags_ext = '\0'; /* remove .tags extension */
1185 ft = filetypes_detect_from_extension(shortname);
1186 if (ft->id != GEANY_FILETYPES_NONE)
1187 return ft;
1189 return NULL;
1193 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1194 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1195 * the relevant path.
1196 * Example:
1197 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1198 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1200 /* -E pre-process, -dD output user macros, -p prof info (?) */
1201 const char pre_process[] = "gcc -E -dD -p -I.";
1203 if (argc > 2)
1205 /* Create global taglist */
1206 int status;
1207 char *command;
1208 const char *tags_file = argv[1];
1209 char *utf8_fname;
1210 GeanyFiletype *ft;
1212 utf8_fname = utils_get_utf8_from_locale(tags_file);
1213 ft = detect_global_tags_filetype(utf8_fname);
1214 g_free(utf8_fname);
1216 if (ft == NULL)
1218 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1219 return 1;
1221 /* load config in case of custom filetypes */
1222 filetypes_load_config(ft->id, FALSE);
1224 /* load ignore list for C/C++ parser */
1225 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1226 load_c_ignore_tags();
1228 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1230 const gchar *cflags = getenv("CFLAGS");
1231 command = g_strdup_printf("%s %s", pre_process, FALLBACK(cflags, ""));
1233 else
1234 command = NULL; /* don't preprocess */
1236 geany_debug("Generating %s tags file.", ft->name);
1237 tm_get_workspace();
1238 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1239 argc - 2, tags_file, ft->lang);
1240 g_free(command);
1241 symbols_finalize(); /* free c_tags_ignore data */
1242 if (! status)
1244 g_printerr(_("Failed to create tags file, perhaps because no symbols "
1245 "were found.\n"));
1246 return 1;
1249 else
1251 g_printerr(_("Usage: %s -g <Tags File> <File list>\n\n"), argv[0]);
1252 g_printerr(_("Example:\n"
1253 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1254 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1255 return 1;
1257 return 0;
1261 void symbols_show_load_tags_dialog(void)
1263 GtkWidget *dialog;
1264 GtkFileFilter *filter;
1266 dialog = gtk_file_chooser_dialog_new(_("Load Tags File"), GTK_WINDOW(main_widgets.window),
1267 GTK_FILE_CHOOSER_ACTION_OPEN,
1268 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1269 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1270 NULL);
1271 gtk_widget_set_name(dialog, "GeanyDialog");
1272 filter = gtk_file_filter_new();
1273 gtk_file_filter_set_name(filter, _("Geany tags file (*.*.tags)"));
1274 gtk_file_filter_add_pattern(filter, "*.*.tags");
1275 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1277 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1279 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1280 GSList *item;
1282 for (item = flist; item != NULL; item = g_slist_next(item))
1284 gchar *fname = item->data;
1285 gchar *utf8_fname;
1286 GeanyFiletype *ft;
1288 utf8_fname = utils_get_utf8_from_locale(fname);
1289 ft = detect_global_tags_filetype(utf8_fname);
1291 if (ft != NULL && symbols_load_global_tags(fname, ft))
1292 /* For translators: the first wildcard is the filetype, the second the filename */
1293 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1294 filetypes_get_display_name(ft), utf8_fname);
1295 else
1296 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1298 g_free(utf8_fname);
1299 g_free(fname);
1301 g_slist_free(flist);
1303 gtk_widget_destroy(dialog);
1307 static void init_user_tags(void)
1309 GSList *file_list = NULL, *list = NULL;
1310 const GSList *node;
1311 gchar *dir;
1313 dir = g_build_filename(app->configdir, GEANY_TAGS_SUBDIR, NULL);
1314 /* create the user tags dir for next time if it doesn't exist */
1315 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1316 utils_mkdir(dir, FALSE);
1317 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1319 SETPTR(dir, g_build_filename(app->datadir, GEANY_TAGS_SUBDIR, NULL));
1320 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1321 g_free(dir);
1323 file_list = g_slist_concat(file_list, list);
1325 /* populate the filetype-specific tag files lists */
1326 for (node = file_list; node != NULL; node = node->next)
1328 gchar *fname = node->data;
1329 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1330 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1332 g_free(utf8_fname);
1334 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1335 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1336 else
1338 geany_debug("Unknown filetype for file '%s'.", fname);
1339 g_free(fname);
1343 /* don't need to delete list contents because they are now stored in
1344 * ft->priv->tag_files */
1345 g_slist_free(file_list);
1349 static void load_user_tags(GeanyFiletypeID ft_id)
1351 static guchar *tags_loaded = NULL;
1352 static gboolean init_tags = FALSE;
1353 const GSList *node;
1354 GeanyFiletype *ft = filetypes[ft_id];
1356 g_return_if_fail(ft_id > 0);
1358 if (!tags_loaded)
1359 tags_loaded = g_new0(guchar, filetypes_array->len);
1360 if (tags_loaded[ft_id])
1361 return;
1362 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1364 if (!init_tags)
1366 init_user_tags();
1367 init_tags = TRUE;
1370 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1372 const gchar *fname = node->data;
1374 symbols_load_global_tags(fname, ft);
1379 static void on_goto_popup_item_activate(GtkMenuItem *item, TMTag *tag)
1381 GeanyDocument *new_doc, *old_doc;
1383 g_return_if_fail(tag);
1385 old_doc = document_get_current();
1386 new_doc = document_open_file(tag->file->file_name, FALSE, NULL, NULL);
1388 if (new_doc)
1389 navqueue_goto_line(old_doc, new_doc, tag->line);
1393 static guint get_tag_class(const TMTag *tag)
1395 gint group = tm_parser_get_sidebar_group(tag->lang, tag->type);
1397 if (group >= 0)
1399 guint icon_id;
1400 if (tm_parser_get_sidebar_info(tag->lang, group, &icon_id))
1401 return icon_id;
1404 return TM_ICON_STRUCT;
1408 /* positions a popup at the caret from the ScintillaObject in @p data */
1409 static void goto_popup_position_func(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1411 gint line_height;
1412 GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(menu));
1413 gint monitor_num;
1414 GdkRectangle monitor;
1415 GtkRequisition req;
1416 GdkEventButton *event_button = g_object_get_data(G_OBJECT(menu), "geany-button-event");
1418 if (event_button)
1420 /* if we got a mouse click, popup at that position */
1421 *x = (gint) event_button->x_root;
1422 *y = (gint) event_button->y_root;
1423 line_height = 0; /* we don't want to offset below the line or anything */
1425 else /* keyboard positioning */
1427 ScintillaObject *sci = data;
1428 GdkWindow *window = gtk_widget_get_window(GTK_WIDGET(sci));
1429 gint pos = sci_get_current_position(sci);
1430 gint line = sci_get_line_from_position(sci, pos);
1431 gint pos_x = SSM(sci, SCI_POINTXFROMPOSITION, 0, pos);
1432 gint pos_y = SSM(sci, SCI_POINTYFROMPOSITION, 0, pos);
1434 line_height = SSM(sci, SCI_TEXTHEIGHT, line, 0);
1436 gdk_window_get_origin(window, x, y);
1437 *x += pos_x;
1438 *y += pos_y;
1441 monitor_num = gdk_screen_get_monitor_at_point(screen, *x, *y);
1443 gtk_widget_get_preferred_size(GTK_WIDGET(menu), NULL, &req);
1445 #if GTK_CHECK_VERSION(3, 4, 0)
1446 gdk_screen_get_monitor_workarea(screen, monitor_num, &monitor);
1447 #else
1448 gdk_screen_get_monitor_geometry(screen, monitor_num, &monitor);
1449 #endif
1451 /* put on one size of the X position, but within the monitor */
1452 if (gtk_widget_get_direction(GTK_WIDGET(menu)) == GTK_TEXT_DIR_RTL)
1454 if (*x - req.width - 1 >= monitor.x)
1455 *x -= req.width + 1;
1456 else if (*x + req.width > monitor.x + monitor.width)
1457 *x = monitor.x;
1458 else
1459 *x += 1;
1461 else
1463 if (*x + req.width + 1 <= monitor.x + monitor.width)
1464 *x = MAX(monitor.x, *x + 1);
1465 else if (*x - req.width - 1 >= monitor.x)
1466 *x -= req.width + 1;
1467 else
1468 *x = monitor.x + MAX(0, monitor.width - req.width);
1471 /* try to put, in order:
1472 * 1. below the Y position, under the line
1473 * 2. above the Y position
1474 * 3. within the monitor */
1475 if (*y + line_height + req.height <= monitor.y + monitor.height)
1476 *y = MAX(monitor.y, *y + line_height);
1477 else if (*y - req.height >= monitor.y)
1478 *y = *y - req.height;
1479 else
1480 *y = monitor.y + MAX(0, monitor.height - req.height);
1482 *push_in = FALSE;
1486 static void show_goto_popup(GeanyDocument *doc, GPtrArray *tags, gboolean have_best)
1488 GtkWidget *first = NULL;
1489 GtkWidget *menu;
1490 GdkEvent *event;
1491 GdkEventButton *button_event = NULL;
1492 TMTag *tmtag;
1493 guint i;
1494 gchar **short_names, **file_names;
1495 menu = gtk_menu_new();
1497 /* If popup would show multiple files present a smart file list that allows
1498 * to easily distinguish the files while avoiding the file paths in their entirety */
1499 file_names = g_new(gchar *, tags->len);
1500 foreach_ptr_array(tmtag, i, tags)
1501 file_names[i] = tmtag->file->file_name;
1502 short_names = utils_strv_shorten_file_list(file_names, tags->len);
1503 g_free(file_names);
1505 foreach_ptr_array(tmtag, i, tags)
1507 GtkWidget *item;
1508 GtkWidget *label;
1509 GtkWidget *image;
1510 gchar *fname = short_names[i];
1511 gchar *text;
1512 gchar *sym = get_symbol_tooltip(doc, tmtag);
1514 if (!sym)
1515 sym = g_strdup("");
1516 if (! first && have_best)
1517 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1518 text = g_markup_printf_escaped(_("<b>%s:%lu:</b> %s"), fname, tmtag->line, sym);
1519 else
1520 /* For translators: it's the filename and line number of a symbol in the goto-symbol popup menu */
1521 text = g_markup_printf_escaped(_("<i>%s:%lu:</i> %s"), fname, tmtag->line, sym);
1523 g_free(sym);
1524 image = gtk_image_new_from_pixbuf(symbols_icons[get_tag_class(tmtag)].pixbuf);
1525 label = g_object_new(GTK_TYPE_LABEL, "label", text, "use-markup", TRUE, "xalign", 0.0, NULL);
1526 item = g_object_new(GTK_TYPE_IMAGE_MENU_ITEM, "image", image, "child", label, "always-show-image", TRUE, NULL);
1527 g_signal_connect_data(item, "activate", G_CALLBACK(on_goto_popup_item_activate),
1528 tm_tag_ref(tmtag), (GClosureNotify) tm_tag_unref, 0);
1529 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1531 if (! first)
1532 first = item;
1534 g_free(text);
1535 g_free(fname);
1537 g_free(short_names);
1539 gtk_widget_show_all(menu);
1541 if (first) /* always select the first item for better keyboard navigation */
1542 g_signal_connect(menu, "realize", G_CALLBACK(gtk_menu_shell_select_item), first);
1544 event = gtk_get_current_event();
1545 if (event && event->type == GDK_BUTTON_PRESS)
1546 button_event = (GdkEventButton *) event;
1547 else
1548 gdk_event_free(event);
1550 g_object_set_data_full(G_OBJECT(menu), "geany-button-event", button_event,
1551 button_event ? (GDestroyNotify) gdk_event_free : NULL);
1552 ui_menu_popup(GTK_MENU(menu), goto_popup_position_func, doc->editor->sci,
1553 button_event ? button_event->button : 0, gtk_get_current_event_time ());
1557 static gint compare_tags_by_name_line(gconstpointer ptr1, gconstpointer ptr2)
1559 gint res;
1560 TMTag *t1 = *((TMTag **) ptr1);
1561 TMTag *t2 = *((TMTag **) ptr2);
1563 res = g_strcmp0(t1->file->short_name, t2->file->short_name);
1564 if (res != 0)
1565 return res;
1566 return t1->line - t2->line;
1570 static TMTag *find_best_goto_tag(GeanyDocument *doc, GPtrArray *tags)
1572 TMTag *tag;
1573 guint i;
1575 /* first check if we have a tag in the current file */
1576 foreach_ptr_array(tag, i, tags)
1578 if (g_strcmp0(doc->real_path, tag->file->file_name) == 0)
1579 return tag;
1582 /* next check if we have a tag for some of the open documents */
1583 foreach_ptr_array(tag, i, tags)
1585 guint j;
1587 foreach_document(j)
1589 if (g_strcmp0(documents[j]->real_path, tag->file->file_name) == 0)
1590 return tag;
1594 /* next check if we have a tag for a file inside the current document's directory */
1595 foreach_ptr_array(tag, i, tags)
1597 gchar *dir = g_path_get_dirname(doc->real_path);
1599 if (g_str_has_prefix(tag->file->file_name, dir))
1601 g_free(dir);
1602 return tag;
1604 g_free(dir);
1607 return NULL;
1611 static GPtrArray *filter_tags(GPtrArray *tags, TMTag *current_tag, gboolean definition)
1613 GeanyDocument *doc = document_get_current();
1614 guint current_line = sci_get_current_line(doc->editor->sci) + 1;
1615 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1616 TMTag *tmtag, *last_tag = NULL;
1617 const gchar *current_scope = NULL;
1618 GPtrArray *filtered_tags = g_ptr_array_new();
1619 guint i;
1621 symbols_get_current_function(doc, &current_scope);
1623 foreach_ptr_array(tmtag, i, tags)
1625 /* don't show local variables outside current function or other
1626 * irrelevant tags - same as in the autocomplete case */
1627 if (!tm_workspace_is_autocomplete_tag(tmtag, doc->tm_file, current_line, current_scope))
1628 continue;
1630 if ((definition && !(tmtag->type & forward_types)) ||
1631 (!definition && (tmtag->type & forward_types)))
1633 /* If there are typedefs of e.g. a struct such as
1634 * "typedef struct Foo {} Foo;", filter out the typedef unless
1635 * cursor is at the struct name. */
1636 if (last_tag != NULL && last_tag->file == tmtag->file &&
1637 last_tag->type != tm_tag_typedef_t && tmtag->type == tm_tag_typedef_t)
1639 if (last_tag == current_tag)
1640 g_ptr_array_add(filtered_tags, tmtag);
1642 else if (tmtag != current_tag)
1643 g_ptr_array_add(filtered_tags, tmtag);
1645 last_tag = tmtag;
1649 return filtered_tags;
1653 static gboolean goto_tag(const gchar *name, gboolean definition)
1655 const TMTagType forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1656 TMTag *tmtag, *current_tag = NULL;
1657 GeanyDocument *old_doc = document_get_current();
1658 gboolean found = FALSE;
1659 const GPtrArray *all_tags;
1660 GPtrArray *tags, *filtered_tags;
1661 guint i;
1662 guint current_line = sci_get_current_line(old_doc->editor->sci) + 1;
1664 all_tags = tm_workspace_find(name, NULL, tm_tag_max_t, NULL, old_doc->file_type->lang);
1666 /* get rid of global tags and find tag at current line */
1667 tags = g_ptr_array_new();
1668 foreach_ptr_array(tmtag, i, all_tags)
1670 if (tmtag->file)
1672 g_ptr_array_add(tags, tmtag);
1673 if (tmtag->file == old_doc->tm_file && tmtag->line == current_line)
1674 current_tag = tmtag;
1678 if (current_tag)
1679 /* swap definition/declaration search */
1680 definition = current_tag->type & forward_types;
1682 filtered_tags = filter_tags(tags, current_tag, definition);
1683 if (filtered_tags->len == 0)
1685 /* if we didn't find anything, try again with the opposite type */
1686 g_ptr_array_free(filtered_tags, TRUE);
1687 filtered_tags = filter_tags(tags, current_tag, !definition);
1689 g_ptr_array_free(tags, TRUE);
1690 tags = filtered_tags;
1692 if (tags->len == 1)
1694 GeanyDocument *new_doc;
1696 tmtag = tags->pdata[0];
1697 new_doc = document_find_by_real_path(tmtag->file->file_name);
1699 if (!new_doc)
1700 /* not found in opened document, should open */
1701 new_doc = document_open_file(tmtag->file->file_name, FALSE, NULL, NULL);
1703 navqueue_goto_line(old_doc, new_doc, tmtag->line);
1705 else if (tags->len > 1)
1707 GPtrArray *tag_list;
1708 TMTag *tag, *best_tag;
1710 g_ptr_array_sort(tags, compare_tags_by_name_line);
1711 best_tag = find_best_goto_tag(old_doc, tags);
1713 tag_list = g_ptr_array_new();
1714 if (best_tag)
1715 g_ptr_array_add(tag_list, best_tag);
1716 foreach_ptr_array(tag, i, tags)
1718 if (tag != best_tag)
1719 g_ptr_array_add(tag_list, tag);
1721 show_goto_popup(old_doc, tag_list, best_tag != NULL);
1723 g_ptr_array_free(tag_list, TRUE);
1726 found = tags->len > 0;
1727 g_ptr_array_free(tags, TRUE);
1729 return found;
1733 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1735 if (goto_tag(name, definition))
1736 return TRUE;
1738 /* if we are here, there was no match and we are beeping ;-) */
1739 utils_beep();
1741 if (!definition)
1742 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1743 else
1744 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1745 return FALSE;
1749 /* This could perhaps be improved to check for #if, class etc. */
1750 static gint get_function_fold_number(GeanyDocument *doc)
1752 /* for Java the functions are always one fold level above the class scope */
1753 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1754 return SC_FOLDLEVELBASE + 1;
1755 else
1756 return SC_FOLDLEVELBASE;
1760 /* Should be used only with get_current_tag_cached.
1761 * tag_types caching might trigger recomputation too often but this isn't used differently often
1762 * enough to be an issue for now */
1763 static gboolean current_tag_changed(GeanyDocument *doc, gint cur_line, gint fold_level, guint tag_types)
1765 static gint old_line = -2;
1766 static GeanyDocument *old_doc = NULL;
1767 static gint old_fold_num = -1;
1768 static guint old_tag_types = 0;
1769 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1770 gboolean ret;
1772 /* check if the cached line and file index have changed since last time: */
1773 if (doc == NULL || doc != old_doc || old_tag_types != tag_types)
1774 ret = TRUE;
1775 else if (cur_line == old_line)
1776 ret = FALSE;
1777 else
1779 /* if the line has only changed by 1 */
1780 if (abs(cur_line - old_line) == 1)
1782 /* It's the same function if the fold number hasn't changed */
1783 ret = (fold_num != old_fold_num);
1785 else ret = TRUE;
1788 /* record current line and file index for next time */
1789 old_line = cur_line;
1790 old_doc = doc;
1791 old_fold_num = fold_num;
1792 old_tag_types = tag_types;
1793 return ret;
1797 /* Parse the function name up to 2 lines before tag_line.
1798 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1799 * type or argument names can be confused with the function name. */
1800 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1802 gint start, end, max_pos;
1803 gint fn_style;
1805 switch (sci_get_lexer(sci))
1807 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
1808 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
1809 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
1811 start = sci_get_position_from_line(sci, tag_line - 2);
1812 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1813 while (start < max_pos && sci_get_style_at(sci, start) != fn_style)
1814 start++;
1816 end = start;
1817 while (end < max_pos && sci_get_style_at(sci, end) == fn_style)
1818 end++;
1820 if (start == end)
1821 return NULL;
1822 return sci_get_contents_range(sci, start, end);
1826 /* Parse the function name */
1827 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
1829 gint start, end, first_pos, max_pos;
1830 gint tmp;
1831 gchar c;
1833 first_pos = end = sci_get_position_from_line(sci, tag_line);
1834 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1835 tmp = 0;
1836 /* goto the begin of function body */
1837 while (end < max_pos &&
1838 (tmp = sci_get_char_at(sci, end)) != '{' &&
1839 tmp != 0) end++;
1840 if (tmp == 0) end --;
1842 /* go back to the end of function identifier */
1843 while (end > 0 && end > first_pos - 500 &&
1844 (tmp = sci_get_char_at(sci, end)) != '(' &&
1845 tmp != 0) end--;
1846 end--;
1847 if (end < 0) end = 0;
1849 /* skip whitespaces between identifier and ( */
1850 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
1852 start = end;
1853 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1854 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
1855 || tmp == SCE_C_GLOBALCLASS
1856 || (c = sci_get_char_at(sci, start)) == '~'
1857 || c == ':'))
1858 start--;
1859 if (start != 0 && start < end) start++; /* correct for last non-matching char */
1861 if (start == end) return NULL;
1862 return sci_get_contents_range(sci, start, end + 1);
1866 /* gets the fold header after or on @line, but skipping folds created because of parentheses */
1867 static gint get_fold_header_after(ScintillaObject *sci, gint line)
1869 const gint line_count = sci_get_line_count(sci);
1871 for (; line < line_count; line++)
1873 if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
1875 const gint last_child = SSM(sci, SCI_GETLASTCHILD, line, -1);
1876 const gint line_end = sci_get_line_end_position(sci, line);
1877 const gint lexer = sci_get_lexer(sci);
1878 gint parenthesis_match_line = -1;
1880 /* now find any unbalanced open parenthesis on the line and see where the matching
1881 * brace would be, mimicking what folding on () does */
1882 for (gint pos = sci_get_position_from_line(sci, line); pos < line_end; pos++)
1884 if (highlighting_is_code_style(lexer, sci_get_style_at(sci, pos)) &&
1885 sci_get_char_at(sci, pos) == '(')
1887 const gint matching = sci_find_matching_brace(sci, pos);
1889 if (matching >= 0)
1891 parenthesis_match_line = sci_get_line_from_position(sci, matching);
1892 if (parenthesis_match_line != line)
1893 break; /* match is on a different line, we found a possible fold */
1894 else
1895 pos = matching; /* just skip the range and continue searching */
1900 /* if the matching parenthesis matches the fold level, skip it and continue.
1901 * it matches if it either spans the same lines, or spans one more but the next one is
1902 * a fold header (in which case the last child of the fold is one less to let the
1903 * header be at the parent level) */
1904 if ((parenthesis_match_line == last_child) ||
1905 (parenthesis_match_line == last_child + 1 &&
1906 sci_get_fold_level(sci, parenthesis_match_line) & SC_FOLDLEVELHEADERFLAG))
1907 line = last_child;
1908 else
1909 return line;
1913 return -1;
1917 static gint get_current_tag_name(GeanyDocument *doc, gchar **tagname, TMTagType tag_types)
1919 gint line;
1920 gint parent;
1922 line = sci_get_current_line(doc->editor->sci);
1923 parent = sci_get_fold_parent(doc->editor->sci, line);
1924 /* if we're inside a fold level and we have up-to-date tags, get the function from TM */
1925 if (parent >= 0 && doc->tm_file != NULL && doc->tm_file->tags_array != NULL &&
1926 (! doc->changed || editor_prefs.autocompletion_update_freq > 0))
1928 const TMTag *tag = tm_get_current_tag(doc->tm_file->tags_array, parent + 1, tag_types);
1930 if (tag)
1932 gint tag_line = tag->line - 1;
1933 gint last_child = line + 1;
1935 /* if it may be a false positive because we're inside a fold level not inside anything
1936 * we match, e.g. a #if in C or C++, we check we're inside the fold level that start
1937 * right after the tag we got from TM.
1938 * Additionally, we perform parentheses matching on the initial line not to get confused
1939 * by folding on () in case the parameter list spans multiple lines */
1940 if (abs(tag_line - parent) > 1)
1942 const gint tag_fold = get_fold_header_after(doc->editor->sci, tag_line);
1943 if (tag_fold >= 0)
1944 last_child = SSM(doc->editor->sci, SCI_GETLASTCHILD, tag_fold, -1);
1947 if (line <= last_child)
1949 if (tag->scope)
1950 *tagname = g_strconcat(tag->scope,
1951 tm_parser_scope_separator(tag->lang), tag->name, NULL);
1952 else
1953 *tagname = g_strdup(tag->name);
1955 return tag_line;
1959 /* for the poor guy with a modified document and without real time tag parsing, we fallback
1960 * to dirty and inaccurate hand-parsing */
1961 else if (parent >= 0 && doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
1963 const gint fn_fold = get_function_fold_number(doc);
1964 gint tag_line = parent;
1965 gint fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1967 /* find the top level fold point */
1968 while (tag_line >= 0 && (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold)
1970 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
1971 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1974 if (tag_line >= 0)
1976 gchar *cur_tag;
1978 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
1979 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
1980 else
1981 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
1983 if (cur_tag != NULL)
1985 *tagname = cur_tag;
1986 return tag_line;
1991 *tagname = g_strdup(_("unknown"));
1992 return -1;
1996 static gint get_current_tag_name_cached(GeanyDocument *doc, const gchar **tagname, TMTagType tag_types)
1998 static gint tag_line = -1;
1999 static gchar *cur_tag = NULL;
2001 g_return_val_if_fail(doc == NULL || doc->is_valid, -1);
2003 if (doc == NULL) /* reset current function */
2005 current_tag_changed(NULL, -1, -1, 0);
2006 g_free(cur_tag);
2007 cur_tag = g_strdup(_("unknown"));
2008 if (tagname != NULL)
2009 *tagname = cur_tag;
2010 tag_line = -1;
2012 else
2014 gint line = sci_get_current_line(doc->editor->sci);
2015 gint fold_level = sci_get_fold_level(doc->editor->sci, line);
2017 if (current_tag_changed(doc, line, fold_level, tag_types))
2019 g_free(cur_tag);
2020 tag_line = get_current_tag_name(doc, &cur_tag, tag_types);
2022 *tagname = cur_tag;
2025 return tag_line;
2029 /* Sets *tagname to point at the current function or tag name.
2030 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2031 * call to this function.
2032 * Returns: line number of the current tag, or -1 if unknown. */
2033 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2035 return get_current_tag_name_cached(doc, tagname, tm_tag_function_t | tm_tag_method_t);
2039 /* same as symbols_get_current_function() but finds class, namespaces and more */
2040 gint symbols_get_current_scope(GeanyDocument *doc, const gchar **tagname)
2042 TMTagType tag_types = (tm_tag_function_t | tm_tag_method_t | tm_tag_class_t |
2043 tm_tag_struct_t | tm_tag_enum_t | tm_tag_union_t | tm_tag_namespace_t);
2045 return get_current_tag_name_cached(doc, tagname, tag_types);
2049 const gchar *symbols_get_icon_name(guint icon_id)
2051 if (icon_id < TM_N_ICONS)
2052 return symbols_icons[icon_id].icon_name;
2053 return NULL;
2057 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2059 gint sort_mode = GPOINTER_TO_INT(user_data);
2060 GeanyDocument *doc = document_get_current();
2062 if (ignore_callback)
2063 return;
2065 if (doc != NULL)
2066 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2069 static void on_symbol_tree_group_by_type_clicked(GtkMenuItem *menuitem, gpointer user_data)
2071 GeanyDocument *doc = document_get_current();
2073 if (ignore_callback)
2074 return;
2076 ui_prefs.symbols_group_by_type = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem));
2077 if (doc != NULL)
2078 doc->has_tags = symbols_recreate_tag_list(doc, SYMBOLS_SORT_USE_PREVIOUS);
2081 static void on_symbol_tree_menu_show(GtkWidget *widget,
2082 gpointer user_data)
2084 GeanyDocument *doc = document_get_current();
2085 gboolean enable;
2087 enable = doc && doc->has_tags;
2088 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2089 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2090 gtk_widget_set_sensitive(symbol_menu.group_by_type, enable);
2091 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2092 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2093 gtk_widget_set_sensitive(symbol_menu.find_usage, enable);
2094 gtk_widget_set_sensitive(symbol_menu.find_doc_usage, enable);
2096 if (! doc)
2097 return;
2099 ignore_callback = TRUE;
2101 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2102 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2103 else
2104 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2106 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.group_by_type),
2107 ui_prefs.symbols_group_by_type);
2109 ignore_callback = FALSE;
2113 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2115 gboolean expand = GPOINTER_TO_INT(user_data);
2116 GeanyDocument *doc = document_get_current();
2118 if (! doc)
2119 return;
2121 g_return_if_fail(doc->priv->tag_tree);
2123 if (expand)
2124 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2125 else
2126 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2130 static void on_find_usage(GtkWidget *widget, G_GNUC_UNUSED gpointer unused)
2132 GtkTreeIter iter;
2133 GtkTreeSelection *selection;
2134 GtkTreeModel *model;
2135 GeanyDocument *doc;
2136 TMTag *tag = NULL;
2138 doc = document_get_current();
2139 if (!doc)
2140 return;
2142 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(doc->priv->tag_tree));
2143 if (gtk_tree_selection_get_selected(selection, &model, &iter))
2144 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
2145 if (tag)
2147 if (widget == symbol_menu.find_in_files)
2148 search_show_find_in_files_dialog_full(tag->name, NULL);
2149 else
2150 search_find_usage(tag->name, tag->name, GEANY_FIND_WHOLEWORD | GEANY_FIND_MATCHCASE,
2151 widget == symbol_menu.find_usage);
2153 tm_tag_unref(tag);
2158 static void create_taglist_popup_menu(void)
2160 GtkWidget *item, *menu;
2162 tv.popup_taglist = menu = gtk_menu_new();
2164 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2165 gtk_widget_show(item);
2166 gtk_container_add(GTK_CONTAINER(menu), item);
2167 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2169 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2170 gtk_widget_show(item);
2171 gtk_container_add(GTK_CONTAINER(menu), item);
2172 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2174 item = gtk_separator_menu_item_new();
2175 gtk_widget_show(item);
2176 gtk_container_add(GTK_CONTAINER(menu), item);
2178 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2179 _("Sort by _Name"));
2180 gtk_widget_show(item);
2181 gtk_container_add(GTK_CONTAINER(menu), item);
2182 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2183 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2185 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2186 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2187 gtk_widget_show(item);
2188 gtk_container_add(GTK_CONTAINER(menu), item);
2189 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2190 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2192 item = gtk_separator_menu_item_new();
2193 gtk_widget_show(item);
2194 gtk_container_add(GTK_CONTAINER(menu), item);
2196 symbol_menu.group_by_type = item = gtk_check_menu_item_new_with_mnemonic(_("_Group by Type"));
2197 gtk_widget_show(item);
2198 gtk_container_add(GTK_CONTAINER(menu), item);
2199 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_group_by_type_clicked), NULL);
2201 item = gtk_separator_menu_item_new();
2202 gtk_widget_show(item);
2203 gtk_container_add(GTK_CONTAINER(menu), item);
2205 symbol_menu.find_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Usage"));
2206 gtk_widget_show(item);
2207 gtk_container_add(GTK_CONTAINER(menu), item);
2208 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_usage);
2210 symbol_menu.find_doc_usage = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find _Document Usage"));
2211 gtk_widget_show(item);
2212 gtk_container_add(GTK_CONTAINER(menu), item);
2213 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), symbol_menu.find_doc_usage);
2215 symbol_menu.find_in_files = item = ui_image_menu_item_new(GTK_STOCK_FIND, _("Find in F_iles..."));
2216 gtk_widget_show(item);
2217 gtk_container_add(GTK_CONTAINER(menu), item);
2218 g_signal_connect(item, "activate", G_CALLBACK(on_find_usage), NULL);
2220 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2222 sidebar_add_common_menu_items(GTK_MENU(menu));
2226 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2228 gchar *f;
2230 g_return_if_fail(!EMPTY(doc->real_path));
2232 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2233 if (utils_str_equal(doc->real_path, f))
2234 load_c_ignore_tags();
2236 g_free(f);
2240 void symbols_init(void)
2242 gchar *f;
2243 guint i;
2245 create_taglist_popup_menu();
2247 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2248 ui_add_config_file_menu_item(f, NULL, NULL);
2249 g_free(f);
2251 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2253 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2254 symbols_icons[i].pixbuf = get_tag_icon(symbols_icons[i].icon_name);
2258 void symbols_finalize(void)
2260 guint i;
2262 g_strfreev(c_tags_ignore);
2264 for (i = 0; i < G_N_ELEMENTS(symbols_icons); i++)
2266 if (symbols_icons[i].pixbuf)
2267 g_object_unref(symbols_icons[i].pixbuf);