Recognize #!/bin/dash as shebang for Shell files (closes #3470986)
[geany-mirror.git] / src / symbols.c
blob4eb416e70a1cfed1d08a3a55d63c18a6861d33cc
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /**
23 * @file symbols.h
24 * Tag-related functions.
25 **/
28 * Symbol Tree and TagManager-related convenience functions.
29 * TagManager parses tags for each document, and also adds them to the workspace (session).
30 * Global tags are lists of tags for each filetype, loaded when a document with a
31 * matching filetype is first loaded.
34 #include "SciLexer.h"
35 #include "geany.h"
37 #include <ctype.h>
38 #include <string.h>
39 #include <stdlib.h>
41 #include "prefix.h"
42 #include "symbols.h"
43 #include "utils.h"
44 #include "filetypes.h"
45 #include "encodings.h"
46 #include "document.h"
47 #include "documentprivate.h"
48 #include "support.h"
49 #include "msgwindow.h"
50 #include "sidebar.h"
51 #include "main.h"
52 #include "navqueue.h"
53 #include "ui_utils.h"
54 #include "editor.h"
55 #include "sciwrappers.h"
58 const guint TM_GLOBAL_TYPE_MASK =
59 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
60 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
63 static gchar **html_entities = NULL;
65 typedef struct
67 gboolean tags_loaded;
68 const gchar *tag_file;
69 } TagFileInfo;
71 /* Check before adding any more tags files, usually they should be downloaded separately. */
72 enum /* Geany tag files */
74 GTF_C,
75 GTF_PASCAL,
76 GTF_PHP,
77 GTF_HTML_ENTITIES,
78 GTF_LATEX,
79 GTF_PYTHON,
80 GTF_MAX
83 static TagFileInfo tag_file_info[GTF_MAX] =
85 {FALSE, "c99.tags"},
86 {FALSE, "pascal.tags"},
87 {FALSE, "php.tags"},
88 {FALSE, "html_entities.tags"},
89 {FALSE, "latex.tags"},
90 {FALSE, "python.tags"}
93 static GPtrArray *top_level_iter_names = NULL;
95 static struct
97 GtkWidget *expand_all;
98 GtkWidget *collapse_all;
99 GtkWidget *sort_by_name;
100 GtkWidget *sort_by_appearance;
102 symbol_menu = {NULL, NULL, NULL, NULL};
105 static void html_tags_loaded(void);
106 static void load_user_tags(filetype_id ft_id);
108 /* get the tags_ignore list, exported by tagmanager's options.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_strconcat(app->configdir, G_DIR_SEPARATOR_S "ignore.tags", NULL);
116 gchar *content;
118 if (g_file_get_contents(path, &content, NULL, NULL))
120 g_strfreev(c_tags_ignore);
121 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
122 g_free(content);
124 g_free(path);
128 void symbols_reload_config_files(void)
130 load_c_ignore_tags();
134 static gsize get_tag_count(void)
136 GPtrArray *tags = tm_get_workspace()->global_tags;
137 gsize count = tags ? tags->len : 0;
139 return count;
143 /* wrapper for tm_workspace_load_global_tags().
144 * note that the tag count only counts new global tags added - if a tag has the same name,
145 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
146 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
148 gboolean result;
149 gsize old_tag_count = get_tag_count();
151 result = tm_workspace_load_global_tags(tags_file, ft->lang);
152 if (result)
154 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
155 (guint) (get_tag_count() - old_tag_count));
157 return result;
161 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
162 * This provides autocompletion, calltips, etc. */
163 void symbols_global_tags_loaded(guint file_type_idx)
165 TagFileInfo *tfi;
166 gint tag_type;
168 /* load ignore list for C/C++ parser */
169 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
170 c_tags_ignore == NULL)
172 load_c_ignore_tags();
175 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
176 return;
178 /* load config in case of custom filetypes */
179 filetypes_load_config(file_type_idx, FALSE);
181 load_user_tags(file_type_idx);
183 switch (file_type_idx)
185 case GEANY_FILETYPES_PHP:
186 case GEANY_FILETYPES_HTML:
187 html_tags_loaded();
189 switch (file_type_idx)
191 case GEANY_FILETYPES_CPP:
192 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
193 /* no C++ tagfile yet */
194 return;
195 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
196 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
197 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
198 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
199 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
200 default:
201 return;
203 tfi = &tag_file_info[tag_type];
205 if (! tfi->tags_loaded)
207 gchar *fname = g_strconcat(app->datadir, G_DIR_SEPARATOR_S, tfi->tag_file, NULL);
209 symbols_load_global_tags(fname, filetypes[file_type_idx]);
210 tfi->tags_loaded = TRUE;
211 g_free(fname);
216 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
217 static void html_tags_loaded(void)
219 TagFileInfo *tfi;
221 if (cl_options.ignore_global_tags)
222 return;
224 tfi = &tag_file_info[GTF_HTML_ENTITIES];
225 if (! tfi->tags_loaded)
227 gchar *file = g_strconcat(app->datadir, G_DIR_SEPARATOR_S, tfi->tag_file, NULL);
229 html_entities = utils_read_file_in_array(file);
230 tfi->tags_loaded = TRUE;
231 g_free(file);
236 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
238 guint j;
239 TMTag *tag;
240 GString *s = NULL;
241 GPtrArray *typedefs;
242 gint tag_lang;
244 g_return_val_if_fail(tags_array != NULL, NULL);
246 typedefs = tm_tags_extract(tags_array, tag_types);
248 if ((typedefs) && (typedefs->len > 0))
250 s = g_string_sized_new(typedefs->len * 10);
251 for (j = 0; j < typedefs->len; ++j)
253 tag = TM_TAG(typedefs->pdata[j]);
254 /* tag->atts.file.lang contains (for some reason) the line of the tag if
255 * tag->atts.entry.file is not NULL */
256 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
258 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
259 * e.g. PHP classes in C++ files
260 * lang = -2 disables the check */
261 if (tag->name && (tag_lang == lang || lang == -2))
263 if (j != 0)
264 g_string_append_c(s, ' ');
265 g_string_append(s, tag->name);
269 if (typedefs)
270 g_ptr_array_free(typedefs, TRUE);
271 return s;
275 /** Gets the context separator used by the tag manager for a particular file
276 * type.
277 * @param ft_id File type identifier.
278 * @return The context separator string.
280 * @since 0.19
282 const gchar *symbols_get_context_separator(gint ft_id)
284 switch (ft_id)
286 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
287 case GEANY_FILETYPES_CPP:
288 case GEANY_FILETYPES_GLSL: /* for structs */
289 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
290 return "::";
292 /* avoid confusion with other possible separators in group/section name */
293 case GEANY_FILETYPES_CONF:
294 case GEANY_FILETYPES_REST:
295 return ":::";
297 default:
298 return ".";
303 GString *symbols_get_macro_list(gint lang)
305 guint j, i;
306 GPtrArray *ftags;
307 GString *words;
308 gint tag_lang;
309 TMTag *tag;
311 if (app->tm_workspace->work_objects == NULL)
312 return NULL;
314 ftags = g_ptr_array_sized_new(50);
315 words = g_string_sized_new(200);
317 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
319 GPtrArray *tags;
321 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
322 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
323 if (NULL != tags)
325 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
327 tag = TM_TAG(tags->pdata[i]);
328 tag_lang = (tag->atts.entry.file) ?
329 tag->atts.entry.file->lang : tag->atts.file.lang;
331 if (tag_lang == lang)
332 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
334 g_ptr_array_free(tags, TRUE);
338 if (ftags->len == 0)
340 g_ptr_array_free(ftags, TRUE);
341 g_string_free(words, TRUE);
342 return NULL;
345 tm_tags_sort(ftags, NULL, FALSE);
346 for (j = 0; j < ftags->len; j++)
348 if (j > 0)
349 g_string_append_c(words, '\n');
350 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
352 g_ptr_array_free(ftags, TRUE);
353 return words;
357 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
358 static TMTag *
359 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
361 guint i;
362 g_return_val_if_fail(tags != NULL, NULL);
364 for (i = 0; i < tags->len; ++i)
366 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
367 return TM_TAG(tags->pdata[i]);
369 return NULL;
373 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
374 const gchar *tag_name, guint type)
376 GPtrArray *tags;
377 TMTag *tmtag;
379 if (G_LIKELY(workobj != NULL))
381 tags = tm_tags_extract(workobj->tags_array, type);
382 if (tags != NULL)
384 tmtag = symbols_find_tm_tag(tags, tag_name);
386 g_ptr_array_free(tags, TRUE);
388 if (tmtag != NULL)
389 return tmtag;
392 return NULL; /* not found */
396 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
398 guint j;
399 const GPtrArray *work_objects = NULL;
401 if (app->tm_workspace != NULL)
402 work_objects = app->tm_workspace->work_objects;
404 if (work_objects != NULL)
406 for (j = 0; j < work_objects->len; j++)
408 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
409 TMTag *tmtag;
411 tmtag = find_work_object_tag(workobj, tag_name, type);
412 if (tmtag != NULL)
413 return tmtag;
416 return NULL; /* not found */
420 const gchar **symbols_get_html_entities(void)
422 if (html_entities == NULL)
423 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
425 return (const gchar **) html_entities;
429 /* sort by name, then line */
430 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
432 gint ret;
434 if (tag_a == NULL || tag_b == NULL)
435 return 0;
437 if (tag_a->name == NULL)
438 return -(tag_a->name != tag_b->name);
440 if (tag_b->name == NULL)
441 return tag_a->name != tag_b->name;
443 ret = strcmp(tag_a->name, tag_b->name);
444 if (ret == 0)
446 return tag_a->atts.entry.line - tag_b->atts.entry.line;
448 return ret;
452 /* sort by line, then scope */
453 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
455 const TMTag *tag_a = TM_TAG(a);
456 const TMTag *tag_b = TM_TAG(b);
457 gint ret;
459 if (a == NULL || b == NULL)
460 return 0;
462 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
463 if (ret == 0)
465 if (tag_a->atts.entry.scope == NULL)
466 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
467 if (tag_b->atts.entry.scope == NULL)
468 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
469 else
470 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
472 return ret;
476 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
478 GList *tag_names = NULL;
479 TMTag *tag;
480 guint i;
482 g_return_val_if_fail(doc, NULL);
484 if (! doc->tm_file || ! doc->tm_file->tags_array)
485 return NULL;
487 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
489 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
490 if (G_UNLIKELY(tag == NULL))
491 return NULL;
493 if (tag->type & tag_types)
495 tag_names = g_list_prepend(tag_names, tag);
498 tag_names = g_list_sort(tag_names, compare_symbol_lines);
499 return tag_names;
503 /* amount of types in the symbol list (currently max. 8 are used) */
504 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
506 struct TreeviewSymbols
508 GtkTreeIter tag_function;
509 GtkTreeIter tag_class;
510 GtkTreeIter tag_macro;
511 GtkTreeIter tag_member;
512 GtkTreeIter tag_variable;
513 GtkTreeIter tag_namespace;
514 GtkTreeIter tag_struct;
515 GtkTreeIter tag_interface;
516 GtkTreeIter tag_type;
517 GtkTreeIter tag_other;
518 } tv_iters;
521 static void init_tag_iters(void)
523 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
524 * filetypes(e.g. config file to Python crashes Geany without this) */
525 tv_iters.tag_function.stamp = -1;
526 tv_iters.tag_class.stamp = -1;
527 tv_iters.tag_member.stamp = -1;
528 tv_iters.tag_macro.stamp = -1;
529 tv_iters.tag_variable.stamp = -1;
530 tv_iters.tag_namespace.stamp = -1;
531 tv_iters.tag_struct.stamp = -1;
532 tv_iters.tag_interface.stamp = -1;
533 tv_iters.tag_type.stamp = -1;
534 tv_iters.tag_other.stamp = -1;
538 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
540 static GtkIconTheme *icon_theme = NULL;
541 static gint x, y;
543 if (G_UNLIKELY(icon_theme == NULL))
545 #ifndef G_OS_WIN32
546 gchar *path = g_strconcat(GEANY_DATADIR, "/icons", NULL);
547 #endif
548 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &y);
549 icon_theme = gtk_icon_theme_get_default();
550 #ifdef G_OS_WIN32
551 gtk_icon_theme_append_search_path(icon_theme, "share\\icons");
552 #else
553 gtk_icon_theme_append_search_path(icon_theme, path);
554 g_free(path);
555 #endif
557 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
561 /* finds the next iter at any level
562 * @param iter in/out, the current iter, will be changed to the next one
563 * @param down whether to try the child iter
564 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
565 static gboolean next_iter(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
567 GtkTreeIter guess;
568 GtkTreeIter copy = *iter;
570 /* go down if the item has children */
571 if (down && gtk_tree_model_iter_children(model, &guess, iter))
572 *iter = guess;
573 /* or to the next item at the same level */
574 else if (gtk_tree_model_iter_next(model, &copy))
575 *iter = copy;
576 /* or to the next item at a parent level */
577 else if (gtk_tree_model_iter_parent(model, &guess, iter))
579 copy = guess;
580 while(TRUE)
582 if (gtk_tree_model_iter_next(model, &copy))
584 *iter = copy;
585 return TRUE;
587 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
588 guess = copy;
589 else
590 return FALSE;
593 else
594 return FALSE;
596 return TRUE;
600 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
602 GtkTreeModel *model = GTK_TREE_MODEL(store);
604 if (!gtk_tree_model_get_iter_first(model, iter))
605 return FALSE;
608 gchar *candidate;
610 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
611 /* FIXME: what if 2 different items have the same name?
612 * this should never happen, but might be caused by a typo in a translation */
613 if (utils_str_equal(candidate, title))
615 g_free(candidate);
616 return TRUE;
618 else
619 g_free(candidate);
621 while (gtk_tree_model_iter_next(model, iter));
623 return FALSE;
627 /* Adds symbol list groups in (iter*, title) pairs.
628 * The list must be ended with NULL. */
629 static void G_GNUC_NULL_TERMINATED
630 tag_list_add_groups(GtkTreeStore *tree_store, ...)
632 va_list args;
633 GtkTreeIter *iter;
635 g_return_if_fail(top_level_iter_names);
637 va_start(args, tree_store);
638 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
640 gchar *title = va_arg(args, gchar*);
641 gchar *icon_name = va_arg(args, gchar *);
642 GdkPixbuf *icon = NULL;
644 if (icon_name)
646 icon = get_tag_icon(icon_name);
649 g_assert(title != NULL);
650 g_ptr_array_add(top_level_iter_names, title);
652 if (!find_toplevel_iter(tree_store, iter, title))
653 gtk_tree_store_append(tree_store, iter, NULL);
655 if (G_IS_OBJECT(icon))
657 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
658 g_object_unref(icon);
660 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
662 va_end(args);
666 static void add_top_level_items(GeanyDocument *doc)
668 filetype_id ft_id = doc->file_type->id;
669 GtkTreeStore *tag_store = doc->priv->tag_store;
671 if (top_level_iter_names == NULL)
672 top_level_iter_names = g_ptr_array_new();
673 else
674 g_ptr_array_set_size(top_level_iter_names, 0);
676 init_tag_iters();
678 switch (ft_id)
680 case GEANY_FILETYPES_DIFF:
682 tag_list_add_groups(tag_store,
683 &(tv_iters.tag_function), _("Files"), NULL, NULL);
684 break;
686 case GEANY_FILETYPES_DOCBOOK:
688 tag_list_add_groups(tag_store,
689 &(tv_iters.tag_function), _("Chapter"), NULL,
690 &(tv_iters.tag_class), _("Section"), NULL,
691 &(tv_iters.tag_member), _("Sect1"), NULL,
692 &(tv_iters.tag_macro), _("Sect2"), NULL,
693 &(tv_iters.tag_variable), _("Sect3"), NULL,
694 &(tv_iters.tag_struct), _("Appendix"), NULL,
695 &(tv_iters.tag_other), _("Other"), NULL,
696 NULL);
697 break;
699 case GEANY_FILETYPES_HASKELL:
700 tag_list_add_groups(tag_store,
701 &tv_iters.tag_namespace, _("Module"), NULL,
702 &tv_iters.tag_type, _("Types"), NULL,
703 &tv_iters.tag_macro, _("Type constructors"), NULL,
704 &tv_iters.tag_function, _("Functions"), "classviewer-method",
705 NULL);
706 break;
707 case GEANY_FILETYPES_COBOL:
708 tag_list_add_groups(tag_store,
709 &tv_iters.tag_class, _("Program"), "classviewer-class",
710 &tv_iters.tag_function, _("File"), "classviewer-method",
711 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
712 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
713 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
714 &tv_iters.tag_variable, _("Data"), "classviewer-var",
715 NULL);
716 break;
717 case GEANY_FILETYPES_CONF:
718 tag_list_add_groups(tag_store,
719 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
720 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
721 NULL);
722 break;
723 case GEANY_FILETYPES_NSIS:
724 tag_list_add_groups(tag_store,
725 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
726 &tv_iters.tag_function, _("Functions"), "classviewer-method",
727 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
728 NULL);
729 break;
730 case GEANY_FILETYPES_LATEX:
732 tag_list_add_groups(tag_store,
733 &(tv_iters.tag_function), _("Command"), NULL,
734 &(tv_iters.tag_class), _("Environment"), NULL,
735 &(tv_iters.tag_member), _("Section"), NULL,
736 &(tv_iters.tag_macro), _("Subsection"), NULL,
737 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
738 &(tv_iters.tag_struct), _("Label"), NULL,
739 &(tv_iters.tag_namespace), _("Chapter"), NULL,
740 &(tv_iters.tag_other), _("Other"), NULL,
741 NULL);
742 break;
744 case GEANY_FILETYPES_MATLAB:
746 tag_list_add_groups(tag_store,
747 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
748 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
749 NULL);
750 break;
752 case GEANY_FILETYPES_R:
754 tag_list_add_groups(tag_store,
755 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
756 &(tv_iters.tag_struct), _("Other"), NULL,
757 NULL);
758 break;
760 case GEANY_FILETYPES_PERL:
762 tag_list_add_groups(tag_store,
763 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
764 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
765 &(tv_iters.tag_macro), _("Labels"), NULL,
766 &(tv_iters.tag_type), _("Constants"), NULL,
767 &(tv_iters.tag_other), _("Other"), "classviewer-other",
768 NULL);
769 break;
771 case GEANY_FILETYPES_PHP:
773 tag_list_add_groups(tag_store,
774 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
775 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
776 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
777 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
778 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
779 NULL);
780 break;
782 case GEANY_FILETYPES_HTML:
784 tag_list_add_groups(tag_store,
785 &(tv_iters.tag_function), _("Functions"), NULL,
786 &(tv_iters.tag_member), _("Anchors"), NULL,
787 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
788 &(tv_iters.tag_class), _("H2 Headings"), NULL,
789 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
790 NULL);
791 break;
793 case GEANY_FILETYPES_CSS:
795 tag_list_add_groups(tag_store,
796 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
797 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
798 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
799 break;
801 case GEANY_FILETYPES_REST:
802 case GEANY_FILETYPES_TXT2TAGS:
803 case GEANY_FILETYPES_ABC:
805 tag_list_add_groups(tag_store,
806 &(tv_iters.tag_namespace), _("Chapter"), NULL,
807 &(tv_iters.tag_member), _("Section"), NULL,
808 &(tv_iters.tag_macro), _("Subsection"), NULL,
809 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
810 NULL);
811 break;
813 case GEANY_FILETYPES_RUBY:
815 tag_list_add_groups(tag_store,
816 &(tv_iters.tag_namespace), _("Modules"), NULL,
817 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
818 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
819 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
820 NULL);
821 break;
823 case GEANY_FILETYPES_TCL:
825 tag_list_add_groups(tag_store,
826 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
827 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
828 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
829 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
830 NULL);
831 break;
833 case GEANY_FILETYPES_PYTHON:
835 tag_list_add_groups(tag_store,
836 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
837 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
838 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
839 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
840 &(tv_iters.tag_namespace), _("Imports"), "classviewer-namespace",
841 NULL);
842 break;
844 case GEANY_FILETYPES_VHDL:
846 tag_list_add_groups(tag_store,
847 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
848 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
849 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
850 &(tv_iters.tag_type), _("Types"), "classviewer-other",
851 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
852 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
853 &(tv_iters.tag_member), _("Processes / Components"), "classviewer-member",
854 &(tv_iters.tag_other), _("Other"), "classviewer-other",
855 NULL);
856 break;
858 case GEANY_FILETYPES_VERILOG:
860 tag_list_add_groups(tag_store,
861 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
862 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
863 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
864 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
865 &(tv_iters.tag_other), _("Other"), "classviewer-other",
866 NULL);
867 break;
869 case GEANY_FILETYPES_JAVA:
871 tag_list_add_groups(tag_store,
872 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
873 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
874 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
875 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
876 &(tv_iters.tag_member), _("Members"), "classviewer-member",
877 &(tv_iters.tag_other), _("Other"), "classviewer-other",
878 NULL);
879 break;
881 case GEANY_FILETYPES_AS:
883 tag_list_add_groups(tag_store,
884 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
885 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
886 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
887 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
888 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
889 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
890 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
891 &(tv_iters.tag_other), _("Other"), "classviewer-other",
892 NULL);
893 break;
895 case GEANY_FILETYPES_HAXE:
897 tag_list_add_groups(tag_store,
898 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
899 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
900 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
901 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
902 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
903 &(tv_iters.tag_other), _("Other"), "classviewer-other",
904 NULL);
905 break;
907 case GEANY_FILETYPES_BASIC:
909 tag_list_add_groups(tag_store,
910 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
911 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
912 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
913 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
914 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
915 &(tv_iters.tag_other), _("Other"), "classviewer-other",
916 NULL);
917 break;
919 case GEANY_FILETYPES_F77:
920 case GEANY_FILETYPES_FORTRAN:
922 tag_list_add_groups(tag_store,
923 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
924 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
925 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
926 &(tv_iters.tag_member), _("Subroutines"), "classviewer-method",
927 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
928 &(tv_iters.tag_type), _("Types"), "classviewer-namespace",
929 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
930 &(tv_iters.tag_other), _("Other"), "classviewer-other",
931 NULL);
932 break;
934 case GEANY_FILETYPES_ASM:
936 tag_list_add_groups(tag_store,
937 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
938 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
939 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
940 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
941 NULL);
942 break;
944 case GEANY_FILETYPES_MAKE:
945 tag_list_add_groups(tag_store,
946 &tv_iters.tag_function, _("Targets"), "classviewer-method",
947 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
948 NULL);
949 break;
950 case GEANY_FILETYPES_SQL:
952 tag_list_add_groups(tag_store,
953 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
954 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
955 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
956 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
957 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
958 &(tv_iters.tag_member), _("Views"), "classviewer-var",
959 &(tv_iters.tag_other), _("Other"), "classviewer-other",
960 NULL);
961 break;
963 case GEANY_FILETYPES_D:
964 default:
966 if (ft_id == GEANY_FILETYPES_D)
967 tag_list_add_groups(tag_store,
968 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
969 else
970 tag_list_add_groups(tag_store,
971 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
973 tag_list_add_groups(tag_store,
974 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
975 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
976 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
977 &(tv_iters.tag_member), _("Members"), "classviewer-member",
978 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
979 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
980 NULL);
982 if (ft_id != GEANY_FILETYPES_D)
984 tag_list_add_groups(tag_store,
985 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
987 tag_list_add_groups(tag_store,
988 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
989 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
995 /* removes toplevel items that have no children */
996 static void hide_empty_rows(GtkTreeStore *store)
998 GtkTreeIter iter;
999 gboolean cont = TRUE;
1001 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1002 return; /* stop when first iter is invalid, i.e. no elements */
1004 while (cont)
1006 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1007 cont = gtk_tree_store_remove(store, &iter);
1008 else
1009 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1014 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1016 gchar *utf8_name;
1017 const gchar *scope = tag->atts.entry.scope;
1018 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1019 gboolean doc_is_utf8 = FALSE;
1021 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1022 * for None at this point completely */
1023 if (utils_str_equal(doc->encoding, "UTF-8") ||
1024 utils_str_equal(doc->encoding, "None"))
1025 doc_is_utf8 = TRUE;
1027 if (! doc_is_utf8)
1028 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1029 -1, doc->encoding, TRUE);
1030 else
1031 utf8_name = tag->name;
1033 if (utf8_name == NULL)
1034 return NULL;
1036 if (! buffer)
1037 buffer = g_string_new(NULL);
1038 else
1039 g_string_truncate(buffer, 0);
1041 /* check first char of scope is a wordchar */
1042 if (!found_parent && scope &&
1043 strpbrk(scope, GEANY_WORDCHARS) == scope)
1045 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1047 g_string_append(buffer, scope);
1048 g_string_append(buffer, sep);
1050 g_string_append(buffer, utf8_name);
1052 if (! doc_is_utf8)
1053 g_free(utf8_name);
1055 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1057 return buffer->str;
1061 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1063 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1065 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1066 * for None at this point completely */
1067 if (utf8_name != NULL &&
1068 ! utils_str_equal(doc->encoding, "UTF-8") &&
1069 ! utils_str_equal(doc->encoding, "None"))
1071 setptr(utf8_name,
1072 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1075 if (utf8_name != NULL)
1076 setptr(utf8_name, g_markup_escape_text(utf8_name, -1));
1078 return utf8_name;
1082 /* find the last word in "foo::bar::blah", e.g. "blah" */
1083 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1085 const gchar *scope = tag->atts.entry.scope;
1086 const gchar *separator = symbols_get_context_separator(ft_id);
1087 const gchar *str, *ptr;
1089 if (!scope)
1090 return NULL;
1092 str = scope;
1094 while (1)
1096 ptr = strstr(str, separator);
1097 if (ptr)
1099 str = ptr + strlen(separator);
1101 else
1102 break;
1105 return NZV(str) ? str : NULL;
1109 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1111 GtkTreeIter *iter = NULL;
1113 switch (tag_type)
1115 case tm_tag_prototype_t:
1116 case tm_tag_method_t:
1117 case tm_tag_function_t:
1119 iter = &tv_iters.tag_function;
1120 break;
1122 case tm_tag_macro_t:
1123 case tm_tag_macro_with_arg_t:
1125 iter = &tv_iters.tag_macro;
1126 break;
1128 case tm_tag_class_t:
1130 iter = &tv_iters.tag_class;
1131 break;
1133 case tm_tag_member_t:
1134 case tm_tag_field_t:
1136 iter = &tv_iters.tag_member;
1137 break;
1139 case tm_tag_typedef_t:
1140 case tm_tag_enum_t:
1142 iter = &tv_iters.tag_type;
1143 break;
1145 case tm_tag_union_t:
1146 case tm_tag_struct_t:
1148 iter = &tv_iters.tag_struct;
1149 break;
1151 case tm_tag_interface_t:
1152 iter = &tv_iters.tag_interface;
1153 break;
1154 case tm_tag_variable_t:
1156 iter = &tv_iters.tag_variable;
1157 break;
1159 case tm_tag_namespace_t:
1160 case tm_tag_package_t:
1162 iter = &tv_iters.tag_namespace;
1163 break;
1165 default:
1167 iter = &tv_iters.tag_other;
1170 if (G_LIKELY(iter->stamp != -1))
1171 return iter;
1172 else
1173 return NULL;
1177 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1179 GdkPixbuf *icon = NULL;
1181 if (parent == &tv_iters.tag_other)
1183 return get_tag_icon("classviewer-var");
1185 /* copy parent icon */
1186 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1187 SYMBOLS_COLUMN_ICON, &icon, -1);
1188 return icon;
1192 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1194 const TMTag *t1 = v1;
1195 const TMTag *t2 = v2;
1197 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1198 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1199 /* include arglist in match to support e.g. C++ overloading */
1200 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1204 /* inspired from g_str_hash() */
1205 static guint tag_hash(gconstpointer v)
1207 const TMTag *tag = v;
1208 const gchar *p;
1209 guint32 h = 5381;
1211 h = (h << 5) + h + tag->type;
1212 for (p = tag->name; *p != '\0'; p++)
1213 h = (h << 5) + h + *p;
1214 if (tag->atts.entry.scope)
1216 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1217 h = (h << 5) + h + *p;
1219 /* for e.g. C++ overloading */
1220 if (tag->atts.entry.arglist)
1222 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1223 h = (h << 5) + h + *p;
1226 return h;
1230 /* like gtk_tree_view_expand_to_path() but with an iter */
1231 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1233 GtkTreeModel *model = gtk_tree_view_get_model(view);
1234 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1236 gtk_tree_view_expand_to_path(view, path);
1237 gtk_tree_path_free(path);
1241 /* like gtk_tree_store_remove() but finds the next iter at any level */
1242 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1244 GtkTreeIter parent;
1245 gboolean has_parent;
1246 gboolean cont;
1248 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1249 cont = gtk_tree_store_remove(store, iter);
1250 /* if there is no next at this level but there is a parent iter, continue from it */
1251 if (! cont && has_parent)
1253 *iter = parent;
1254 cont = next_iter(GTK_TREE_MODEL(store), iter, FALSE);
1257 return cont;
1261 /* updates @table adding @tag->name:@iter if necessary */
1262 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1263 const GtkTreeIter *iter)
1265 if (g_hash_table_lookup_extended(table, tag->name, NULL, NULL) &&
1266 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1268 g_hash_table_insert(table, tag->name, g_slice_dup(GtkTreeIter, iter));
1273 static void free_iter_slice(gpointer iter)
1275 g_slice_free(GtkTreeIter, iter);
1280 * Updates the tag tree for a document with the tags in *list.
1281 * @param doc a document
1282 * @param tags a pointer to a GList* holding the tags to add/update. This
1283 * list may be updated, removing updated elements.
1285 * The update is done in two passes:
1286 * 1) walking the current tree, update tags that still exist and remove the
1287 * obsolescent ones;
1288 * 2) walking the remaining (non updated) tags, adds them in the list.
1290 * For better performances, we use 2 hash tables:
1291 * - one containing all the tags for lookup in the first pass (actually stores a
1292 * reference in the tags list for removing it efficiently), avoiding list search
1293 * on each tag;
1294 * - the other holding "tag-name":row references for tags having children, used to
1295 * lookup for a parent in both passes, avoiding tree traversal.
1297 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1299 GtkTreeStore *store = doc->priv->tag_store;
1300 GtkTreeModel *model = GTK_TREE_MODEL(store);
1301 GHashTable *parents_table;
1302 GHashTable *tags_table;
1303 GtkTreeIter iter;
1304 gboolean cont;
1305 GList *item;
1307 /* Build hash tables holding tags and parents */
1308 /* parent table holds "tag-name":GtkTreeIter */
1309 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice);
1310 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1311 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1312 foreach_list(item, *tags)
1314 TMTag *tag = item->data;
1315 const gchar *name;
1317 g_hash_table_insert(tags_table, tag, item);
1319 name = get_parent_name(tag, doc->file_type->id);
1320 if (name)
1321 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1324 /* First pass, update existing rows or delete them.
1325 * It is OK to delete them since we walk top down so we would remove
1326 * parents before checking for their children, thus never implicitly
1327 * deleting an updated child */
1328 cont = gtk_tree_model_get_iter_first(model, &iter);
1329 while (cont)
1331 TMTag *tag;
1333 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1334 if (! tag) /* most probably a toplevel, skip it */
1335 cont = next_iter(model, &iter, TRUE);
1336 else
1338 GList *found_item;
1340 found_item = g_hash_table_lookup(tags_table, tag);
1341 if (! found_item) /* tag doesn't exist, remove it */
1342 cont = tree_store_remove_row(store, &iter);
1343 else /* tag still exist, update it */
1345 const gchar *name;
1346 const gchar *parent_name;
1347 TMTag *found = found_item->data;
1349 parent_name = get_parent_name(found, doc->file_type->id);
1350 /* if parent is unknown, ignore it */
1351 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1352 parent_name = NULL;
1354 /* only update fields that (can) have changed (name that holds line
1355 * number, and the tag itself) */
1356 name = get_symbol_name(doc, found, parent_name != NULL);
1357 gtk_tree_store_set(store, &iter,
1358 SYMBOLS_COLUMN_NAME, name,
1359 SYMBOLS_COLUMN_TAG, found,
1360 -1);
1362 update_parents_table(parents_table, found, parent_name, &iter);
1364 /* remove the updated tag from the table and list */
1365 g_hash_table_remove(tags_table, found);
1366 *tags = g_list_delete_link(*tags, found_item);
1368 cont = next_iter(model, &iter, TRUE);
1371 tm_tag_unref(tag);
1375 /* Second pass, now we have a tree cleaned up from invalid rows,
1376 * we simply add new ones */
1377 foreach_list (item, *tags)
1379 TMTag *tag = item->data;
1380 GtkTreeIter *parent;
1382 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1383 if (G_UNLIKELY(! parent))
1384 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1385 else
1387 gboolean expand;
1388 const gchar *name;
1389 const gchar *parent_name;
1390 gchar *tooltip;
1391 GdkPixbuf *icon = get_child_icon(store, parent);
1393 parent_name = get_parent_name(tag, doc->file_type->id);
1394 if (parent_name)
1396 GtkTreeIter *parent_search;
1398 parent_search = g_hash_table_lookup(parents_table, parent_name);
1399 if (parent_search)
1400 parent = parent_search;
1401 else
1402 parent_name = NULL;
1405 /* only expand to the iter if the parent was empty, otherwise we let the
1406 * folding as it was before (already expanded, or closed by the user) */
1407 expand = ! gtk_tree_model_iter_has_child(model, parent);
1409 /* insert the new element */
1410 gtk_tree_store_append(store, &iter, parent);
1411 name = get_symbol_name(doc, tag, parent_name != NULL);
1412 tooltip = get_symbol_tooltip(doc, tag);
1413 gtk_tree_store_set(store, &iter,
1414 SYMBOLS_COLUMN_NAME, name,
1415 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1416 SYMBOLS_COLUMN_ICON, icon,
1417 SYMBOLS_COLUMN_TAG, tag,
1418 -1);
1419 g_free(tooltip);
1420 if (G_LIKELY(icon))
1421 g_object_unref(icon);
1423 update_parents_table(parents_table, tag, parent_name, &iter);
1425 if (expand)
1426 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1430 g_hash_table_destroy(parents_table);
1431 g_hash_table_destroy(tags_table);
1435 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1436 * is not stable, so the order is already lost. */
1437 static gint compare_top_level_names(const gchar *a, const gchar *b)
1439 guint i;
1440 const gchar *name;
1442 /* This should never happen as it would mean that two or more top
1443 * level items have the same name but it can happen by typos in the translations. */
1444 if (utils_str_equal(a, b))
1445 return 1;
1447 foreach_ptr_array(name, i, top_level_iter_names)
1449 if (utils_str_equal(name, a))
1450 return -1;
1451 if (utils_str_equal(name, b))
1452 return 1;
1454 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1455 return 0;
1459 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1460 GtkTreeIter *iter)
1462 /* if the tag has a parent tag, it should be at depth >= 2 */
1463 return NZV(tag->atts.entry.scope) &&
1464 gtk_tree_store_iter_depth(store, iter) == 1;
1468 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1469 gpointer user_data)
1471 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1472 TMTag *tag_a, *tag_b;
1473 gint cmp;
1475 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1476 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1478 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1479 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1480 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1481 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1483 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1484 compare_symbol_lines(tag_a, tag_b);
1486 else
1488 gchar *astr, *bstr;
1490 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1491 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1493 /* if a is toplevel, b must be also */
1494 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1496 cmp = compare_top_level_names(astr, bstr);
1498 else
1500 /* this is what g_strcmp0() does */
1501 if (! astr)
1502 cmp = -(astr != bstr);
1503 else if (! bstr)
1504 cmp = astr != bstr;
1505 else
1507 cmp = strcmp(astr, bstr);
1509 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1510 if (tag_a && tag_b)
1511 if (!sort_by_name ||
1512 (utils_str_equal(tag_a->name, tag_b->name) &&
1513 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1514 cmp = compare_symbol_lines(tag_a, tag_b);
1517 g_free(astr);
1518 g_free(bstr);
1520 tm_tag_unref(tag_a);
1521 tm_tag_unref(tag_b);
1523 return cmp;
1527 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1529 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1530 GINT_TO_POINTER(sort_by_name), NULL);
1532 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1536 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1538 GList *tags;
1540 g_return_val_if_fail(doc != NULL, FALSE);
1542 tags = get_tag_list(doc, tm_tag_max_t);
1543 if (tags == NULL)
1544 return FALSE;
1546 /* FIXME: Not sure why we detached the model here? */
1548 /* disable sorting during update because the code doesn't support correctly
1549 * models that are currently being built */
1550 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1552 /* add grandparent type iters */
1553 add_top_level_items(doc);
1555 update_tree_tags(doc, &tags);
1556 g_list_free(tags);
1558 hide_empty_rows(doc->priv->tag_store);
1560 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1561 sort_mode = doc->priv->symbol_list_sort_mode;
1563 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1564 doc->priv->symbol_list_sort_mode = sort_mode;
1566 return TRUE;
1570 /* Detects a global tags filetype from the *.lang.* language extension.
1571 * Returns NULL if there was no matching TM language. */
1572 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1574 gchar *tags_ext;
1575 gchar *shortname = g_strdup(utf8_filename);
1576 GeanyFiletype *ft = NULL;
1578 tags_ext = strstr(shortname, ".tags");
1579 if (tags_ext)
1581 *tags_ext = '\0'; /* remove .tags extension */
1582 ft = filetypes_detect_from_extension(shortname);
1584 g_free(shortname);
1585 return ft;
1589 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1590 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1591 * the relevant path.
1592 * Example:
1593 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1594 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1596 /* -E pre-process, -dD output user macros, -p prof info (?),
1597 * -undef remove builtin macros (seems to be needed with FC5 gcc 4.1.1) */
1598 const char pre_process[] = "gcc -E -dD -p -undef";
1600 if (argc > 2)
1602 /* Create global taglist */
1603 int status;
1604 char *command;
1605 const char *tags_file = argv[1];
1606 char *utf8_fname;
1607 GeanyFiletype *ft;
1609 utf8_fname = utils_get_utf8_from_locale(tags_file);
1610 ft = detect_global_tags_filetype(utf8_fname);
1611 g_free(utf8_fname);
1613 if (ft == NULL)
1615 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1616 return 1;
1618 /* load config in case of custom filetypes */
1619 filetypes_load_config(ft->id, FALSE);
1621 /* load ignore list for C/C++ parser */
1622 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1623 load_c_ignore_tags();
1625 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1626 command = g_strdup_printf("%s %s", pre_process, NVL(getenv("CFLAGS"), ""));
1627 else
1628 command = NULL; /* don't preprocess */
1630 geany_debug("Generating %s tags file.", ft->name);
1631 tm_get_workspace();
1632 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1633 argc - 2, tags_file, ft->lang);
1634 g_free(command);
1635 symbols_finalize(); /* free c_tags_ignore data */
1636 if (! status)
1638 g_printerr(_("Failed to create tags file, perhaps because no tags "
1639 "were found.\n"));
1640 return 1;
1643 else
1645 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1646 g_printerr(_("Example:\n"
1647 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1648 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1649 return 1;
1651 return 0;
1655 void symbols_show_load_tags_dialog(void)
1657 GtkWidget *dialog;
1658 GtkFileFilter *filter;
1660 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1661 GTK_FILE_CHOOSER_ACTION_OPEN,
1662 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1663 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1664 NULL);
1665 gtk_widget_set_name(dialog, "GeanyDialog");
1666 filter = gtk_file_filter_new();
1667 gtk_file_filter_set_name(filter, _("Geany tag files (*.tags)"));
1668 gtk_file_filter_add_pattern(filter, "*.tags");
1669 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1671 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1673 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1674 GSList *item;
1676 for (item = flist; item != NULL; item = g_slist_next(item))
1678 gchar *fname = item->data;
1679 gchar *utf8_fname;
1680 GeanyFiletype *ft;
1682 utf8_fname = utils_get_utf8_from_locale(fname);
1683 ft = detect_global_tags_filetype(utf8_fname);
1685 if (ft != NULL && symbols_load_global_tags(fname, ft))
1686 /* For translators: the first wildcard is the filetype, the second the filename */
1687 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1688 filetypes_get_display_name(ft), utf8_fname);
1689 else
1690 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1692 g_free(utf8_fname);
1693 g_free(fname);
1695 g_slist_free(flist);
1697 gtk_widget_destroy(dialog);
1701 /* Fills a hash table with filetype keys that hold a linked list of filenames. */
1702 static GHashTable *get_tagfile_hash(const GSList *file_list)
1704 const GSList *node;
1705 GHashTable *hash = g_hash_table_new(NULL, NULL);
1707 for (node = file_list; node != NULL; node = g_slist_next(node))
1709 GList *fnames;
1710 gchar *fname = node->data;
1711 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1712 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1714 g_free(utf8_fname);
1716 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1718 fnames = g_hash_table_lookup(hash, ft); /* may be NULL */
1719 fnames = g_list_append(fnames, fname);
1720 g_hash_table_insert(hash, ft, fnames);
1722 else
1723 geany_debug("Unknown filetype for file '%s'.", fname);
1725 return hash;
1729 static GHashTable *init_user_tags(void)
1731 GSList *file_list = NULL, *list = NULL;
1732 GHashTable *lang_hash;
1733 gchar *dir;
1735 dir = utils_build_path(app->configdir, "tags", NULL);
1736 /* create the user tags dir for next time if it doesn't exist */
1737 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1739 utils_mkdir(dir, FALSE);
1741 file_list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
1743 setptr(dir, utils_build_path(app->datadir, "tags", NULL));
1744 list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
1745 g_free(dir);
1747 file_list = g_slist_concat(file_list, list);
1749 lang_hash = get_tagfile_hash(file_list);
1750 /* don't need to delete list contents because they are now used for hash contents */
1751 g_slist_free(file_list);
1753 return lang_hash;
1757 static void load_user_tags(filetype_id ft_id)
1759 static guchar *tags_loaded = NULL;
1760 static GHashTable *lang_hash = NULL;
1761 GList *fnames;
1762 const GList *node;
1763 GeanyFiletype *ft = filetypes[ft_id];
1765 g_return_if_fail(ft_id > 0);
1767 if (!tags_loaded)
1768 tags_loaded = g_new0(guchar, filetypes_array->len);
1769 if (tags_loaded[ft_id])
1770 return;
1771 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1773 if (lang_hash == NULL)
1774 lang_hash = init_user_tags();
1776 fnames = g_hash_table_lookup(lang_hash, ft);
1778 for (node = fnames; node != NULL; node = g_list_next(node))
1780 const gchar *fname = node->data;
1782 symbols_load_global_tags(fname, ft);
1784 g_list_foreach(fnames, (GFunc) g_free, NULL);
1785 g_list_free(fnames);
1786 g_hash_table_remove(lang_hash, (gpointer) ft);
1790 static gboolean goto_tag(const gchar *name, gboolean definition)
1792 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1793 guint type;
1794 TMTag *tmtag = NULL;
1795 GeanyDocument *old_doc = document_get_current();
1797 /* goto tag definition: all except prototypes / forward declarations / externs */
1798 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1800 /* first look in the current document */
1801 if (old_doc != NULL && old_doc->tm_file)
1802 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1804 /* if not found, look in the workspace */
1805 if (tmtag == NULL)
1806 tmtag = find_workspace_tag(name, type);
1808 if (tmtag != NULL)
1810 GeanyDocument *new_doc = document_find_by_real_path(
1811 tmtag->atts.entry.file->work_object.file_name);
1813 if (new_doc)
1815 /* If we are already on the tag line, swap definition/declaration */
1816 if (new_doc == old_doc &&
1817 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1819 if (goto_tag(name, !definition))
1820 return TRUE;
1823 else
1825 /* not found in opened document, should open */
1826 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1829 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1830 return TRUE;
1832 return FALSE;
1836 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1838 if (goto_tag(name, definition))
1839 return TRUE;
1841 /* if we are here, there was no match and we are beeping ;-) */
1842 utils_beep();
1844 if (!definition)
1845 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1846 else
1847 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1848 return FALSE;
1852 /* This could perhaps be improved to check for #if, class etc. */
1853 static gint get_function_fold_number(GeanyDocument *doc)
1855 /* for Java the functions are always one fold level above the class scope */
1856 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1857 return SC_FOLDLEVELBASE + 1;
1858 else
1859 return SC_FOLDLEVELBASE;
1863 /* Should be used only with symbols_get_current_function. */
1864 static gboolean current_function_changed(GeanyDocument *doc, gint cur_line, gint fold_level)
1866 static gint old_line = -2;
1867 static GeanyDocument *old_doc = NULL;
1868 static gint old_fold_num = -1;
1869 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1870 gboolean ret;
1872 /* check if the cached line and file index have changed since last time: */
1873 if (doc == NULL || doc != old_doc)
1874 ret = TRUE;
1875 else
1876 if (cur_line == old_line)
1877 ret = FALSE;
1878 else
1880 /* if the line has only changed by 1 */
1881 if (abs(cur_line - old_line) == 1)
1883 const gint fn_fold =
1884 get_function_fold_number(doc);
1885 /* It's the same function if the fold number hasn't changed, or both the new
1886 * and old fold numbers are above the function fold number. */
1887 gboolean same =
1888 fold_num == old_fold_num ||
1889 (old_fold_num > fn_fold && fold_num > fn_fold);
1891 ret = ! same;
1893 else ret = TRUE;
1896 /* record current line and file index for next time */
1897 old_line = cur_line;
1898 old_doc = doc;
1899 old_fold_num = fold_num;
1900 return ret;
1904 /* Parse the function name up to 2 lines before tag_line.
1905 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1906 * type or argument names can be confused with the function name. */
1907 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1909 gint start, end, max_pos;
1910 gchar *cur_tag;
1911 gint fn_style;
1913 switch (sci_get_lexer(sci))
1915 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
1916 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
1917 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
1919 start = sci_get_position_from_line(sci, tag_line - 2);
1920 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1921 while (sci_get_style_at(sci, start) != fn_style
1922 && start < max_pos) start++;
1924 end = start;
1925 while (sci_get_style_at(sci, end) == fn_style
1926 && end < max_pos) end++;
1928 if (start == end)
1929 return NULL;
1930 cur_tag = g_malloc(end - start + 1);
1931 sci_get_text_range(sci, start, end, cur_tag);
1932 return cur_tag;
1936 /* Parse the function name */
1937 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
1939 gint start, end, first_pos, max_pos;
1940 gint tmp;
1941 gchar c;
1942 gchar *cur_tag;
1944 first_pos = end = sci_get_position_from_line(sci, tag_line);
1945 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1946 tmp = 0;
1947 /* goto the begin of function body */
1948 while (end < max_pos &&
1949 (tmp = sci_get_char_at(sci, end)) != '{' &&
1950 tmp != 0) end++;
1951 if (tmp == 0) end --;
1953 /* go back to the end of function identifier */
1954 while (end > 0 && end > first_pos - 500 &&
1955 (tmp = sci_get_char_at(sci, end)) != '(' &&
1956 tmp != 0) end--;
1957 end--;
1958 if (end < 0) end = 0;
1960 /* skip whitespaces between identifier and ( */
1961 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
1963 start = end;
1964 c = 0;
1965 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1966 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
1967 || tmp == SCE_C_GLOBALCLASS
1968 || (c = sci_get_char_at(sci, start)) == '~'
1969 || c == ':'))
1970 start--;
1971 if (start != 0 && start < end) start++; /* correct for last non-matching char */
1973 if (start == end) return NULL;
1974 cur_tag = g_malloc(end - start + 2);
1975 sci_get_text_range(sci, start, end + 1, cur_tag);
1976 return cur_tag;
1980 /* Sets *tagname to point at the current function or tag name.
1981 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
1982 * call to this function.
1983 * Returns: line number of the current tag, or -1 if unknown. */
1984 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
1986 static gint tag_line = -1;
1987 static gchar *cur_tag = NULL;
1988 gint line;
1989 gint fold_level;
1990 TMWorkObject *tm_file;
1992 if (doc == NULL) /* reset current function */
1994 current_function_changed(NULL, -1, -1);
1995 g_free(cur_tag);
1996 cur_tag = g_strdup(_("unknown"));
1997 if (tagname != NULL)
1998 *tagname = cur_tag;
1999 tag_line = -1;
2000 return tag_line;
2003 line = sci_get_current_line(doc->editor->sci);
2004 fold_level = sci_get_fold_level(doc->editor->sci, line);
2005 /* check if the cached line and file index have changed since last time: */
2006 if (! current_function_changed(doc, line, fold_level))
2008 /* we can assume same current function as before */
2009 *tagname = cur_tag;
2010 return tag_line;
2012 g_free(cur_tag); /* free the old tag, it will be replaced. */
2014 /* if line is at base fold level, we're not in a function */
2015 if ((fold_level & SC_FOLDLEVELNUMBERMASK) == SC_FOLDLEVELBASE)
2017 cur_tag = g_strdup(_("unknown"));
2018 *tagname = cur_tag;
2019 tag_line = -1;
2020 return tag_line;
2022 tm_file = doc->tm_file;
2024 /* if the document has no changes, get the previous function name from TM */
2025 if (! doc->changed && tm_file != NULL && tm_file->tags_array != NULL)
2027 const TMTag *tag = (const TMTag*) tm_get_current_function(tm_file->tags_array, line);
2029 if (tag != NULL)
2031 gchar *tmp;
2032 tmp = tag->atts.entry.scope;
2033 cur_tag = tmp ? g_strconcat(tmp, "::", tag->name, NULL) : g_strdup(tag->name);
2034 *tagname = cur_tag;
2035 tag_line = tag->atts.entry.line;
2036 return tag_line;
2040 /* parse the current function name here because TM line numbers may have changed,
2041 * and it would take too long to reparse the whole file. */
2042 if (doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2044 const gint fn_fold = get_function_fold_number(doc);
2046 tag_line = line;
2047 do /* find the top level fold point */
2049 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2050 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2051 } while (tag_line >= 0 &&
2052 (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold);
2054 if (tag_line >= 0)
2056 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2057 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2058 else
2059 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2061 if (cur_tag != NULL)
2063 *tagname = cur_tag;
2064 return tag_line;
2069 cur_tag = g_strdup(_("unknown"));
2070 *tagname = cur_tag;
2071 tag_line = -1;
2072 return tag_line;
2076 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2078 gint sort_mode = GPOINTER_TO_INT(user_data);
2079 GeanyDocument *doc = document_get_current();
2081 if (ignore_callback)
2082 return;
2084 if (doc != NULL)
2085 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2089 static void on_symbol_tree_menu_show(GtkWidget *widget,
2090 gpointer user_data)
2092 GeanyDocument *doc = document_get_current();
2093 gboolean enable;
2095 enable = doc && doc->has_tags;
2096 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2097 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2098 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2099 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2101 if (! doc)
2102 return;
2104 ignore_callback = TRUE;
2106 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2107 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2108 else
2109 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2111 ignore_callback = FALSE;
2115 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2117 gboolean expand = GPOINTER_TO_INT(user_data);
2118 GeanyDocument *doc = document_get_current();
2120 if (! doc)
2121 return;
2123 g_return_if_fail(doc->priv->tag_tree);
2125 if (expand)
2126 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2127 else
2128 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2132 static void create_taglist_popup_menu(void)
2134 GtkWidget *item, *menu;
2136 tv.popup_taglist = menu = gtk_menu_new();
2138 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2139 gtk_widget_show(item);
2140 gtk_container_add(GTK_CONTAINER(menu), item);
2141 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2143 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2144 gtk_widget_show(item);
2145 gtk_container_add(GTK_CONTAINER(menu), item);
2146 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2148 item = gtk_separator_menu_item_new();
2149 gtk_widget_show(item);
2150 gtk_container_add(GTK_CONTAINER(menu), item);
2152 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2153 _("Sort by _Name"));
2154 gtk_widget_show(item);
2155 gtk_container_add(GTK_CONTAINER(menu), item);
2156 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2157 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2159 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2160 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2161 gtk_widget_show(item);
2162 gtk_container_add(GTK_CONTAINER(menu), item);
2163 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2164 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2166 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2168 sidebar_add_common_menu_items(GTK_MENU(menu));
2172 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2174 gchar *f = utils_build_path(app->configdir, "ignore.tags", NULL);
2176 g_return_if_fail(NZV(doc->real_path));
2178 if (utils_str_equal(doc->real_path, f))
2179 load_c_ignore_tags();
2181 g_free(f);
2185 void symbols_init(void)
2187 gchar *f;
2189 create_taglist_popup_menu();
2191 f = utils_build_path(app->configdir, "ignore.tags", NULL);
2192 ui_add_config_file_menu_item(f, NULL, NULL);
2193 g_free(f);
2195 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2199 void symbols_finalize(void)
2201 g_strfreev(html_entities);
2202 g_strfreev(c_tags_ignore);