Remove a suspect ungetc() call in VHDL parser
[geany-mirror.git] / src / symbols.c
blob451f48c88508a626e96b1cd61ab04c0074abee8b
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2011-2012 Colomban Wendling <ban(at)herbesfolles(dot)org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /**
24 * @file symbols.h
25 * Tag-related functions.
26 **/
29 * Symbol Tree and TagManager-related convenience functions.
30 * TagManager parses tags for each document, and also adds them to the workspace (session).
31 * Global tags are lists of tags for each filetype, loaded when a document with a
32 * matching filetype is first loaded.
35 #include "SciLexer.h"
36 #include "geany.h"
38 #include <ctype.h>
39 #include <string.h>
40 #include <stdlib.h>
42 #include "prefix.h"
43 #include "symbols.h"
44 #include "utils.h"
45 #include "filetypes.h"
46 #include "encodings.h"
47 #include "document.h"
48 #include "documentprivate.h"
49 #include "support.h"
50 #include "msgwindow.h"
51 #include "sidebar.h"
52 #include "main.h"
53 #include "navqueue.h"
54 #include "ui_utils.h"
55 #include "editor.h"
56 #include "sciwrappers.h"
57 #include "filetypesprivate.h"
60 const guint TM_GLOBAL_TYPE_MASK =
61 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
62 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
65 static gchar **html_entities = NULL;
67 typedef struct
69 gboolean tags_loaded;
70 const gchar *tag_file;
71 } TagFileInfo;
73 /* Check before adding any more tags files, usually they should be downloaded separately. */
74 enum /* Geany tag files */
76 GTF_C,
77 GTF_PASCAL,
78 GTF_PHP,
79 GTF_HTML_ENTITIES,
80 GTF_LATEX,
81 GTF_PYTHON,
82 GTF_MAX
85 static TagFileInfo tag_file_info[GTF_MAX] =
87 {FALSE, "c99.tags"},
88 {FALSE, "pascal.tags"},
89 {FALSE, "php.tags"},
90 {FALSE, "html_entities.tags"},
91 {FALSE, "latex.tags"},
92 {FALSE, "python.tags"}
95 static GPtrArray *top_level_iter_names = NULL;
97 static struct
99 GtkWidget *expand_all;
100 GtkWidget *collapse_all;
101 GtkWidget *sort_by_name;
102 GtkWidget *sort_by_appearance;
104 symbol_menu = {NULL, NULL, NULL, NULL};
107 static void html_tags_loaded(void);
108 static void load_user_tags(filetype_id ft_id);
110 /* get the tags_ignore list, exported by tagmanager's options.c */
111 extern gchar **c_tags_ignore;
113 /* ignore certain tokens when parsing C-like syntax.
114 * Also works for reloading. */
115 static void load_c_ignore_tags(void)
117 gchar *path = g_build_filename(app->configdir, "ignore.tags", NULL);
118 gchar *content;
120 if (g_file_get_contents(path, &content, NULL, NULL))
122 /* historically we ignore the glib _DECLS for tag generation */
123 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
125 g_strfreev(c_tags_ignore);
126 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
127 g_free(content);
129 g_free(path);
133 void symbols_reload_config_files(void)
135 load_c_ignore_tags();
139 static gsize get_tag_count(void)
141 GPtrArray *tags = tm_get_workspace()->global_tags;
142 gsize count = tags ? tags->len : 0;
144 return count;
148 /* wrapper for tm_workspace_load_global_tags().
149 * note that the tag count only counts new global tags added - if a tag has the same name,
150 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
151 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
153 gboolean result;
154 gsize old_tag_count = get_tag_count();
156 result = tm_workspace_load_global_tags(tags_file, ft->lang);
157 if (result)
159 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
160 (guint) (get_tag_count() - old_tag_count));
162 return result;
166 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
167 * This provides autocompletion, calltips, etc. */
168 void symbols_global_tags_loaded(guint file_type_idx)
170 TagFileInfo *tfi;
171 gint tag_type;
173 /* load ignore list for C/C++ parser */
174 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
175 c_tags_ignore == NULL)
177 load_c_ignore_tags();
180 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
181 return;
183 /* load config in case of custom filetypes */
184 filetypes_load_config(file_type_idx, FALSE);
186 load_user_tags(file_type_idx);
188 switch (file_type_idx)
190 case GEANY_FILETYPES_PHP:
191 case GEANY_FILETYPES_HTML:
192 html_tags_loaded();
194 switch (file_type_idx)
196 case GEANY_FILETYPES_CPP:
197 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
198 /* no C++ tagfile yet */
199 return;
200 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
201 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
202 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
203 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
204 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
205 default:
206 return;
208 tfi = &tag_file_info[tag_type];
210 if (! tfi->tags_loaded)
212 gchar *fname = g_build_filename(app->datadir, tfi->tag_file, NULL);
214 symbols_load_global_tags(fname, filetypes[file_type_idx]);
215 tfi->tags_loaded = TRUE;
216 g_free(fname);
221 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
222 static void html_tags_loaded(void)
224 TagFileInfo *tfi;
226 if (cl_options.ignore_global_tags)
227 return;
229 tfi = &tag_file_info[GTF_HTML_ENTITIES];
230 if (! tfi->tags_loaded)
232 gchar *file = g_build_filename(app->datadir, tfi->tag_file, NULL);
234 html_entities = utils_read_file_in_array(file);
235 tfi->tags_loaded = TRUE;
236 g_free(file);
241 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
243 guint j;
244 TMTag *tag;
245 GString *s = NULL;
246 GPtrArray *typedefs;
247 gint tag_lang;
249 g_return_val_if_fail(tags_array != NULL, NULL);
251 typedefs = tm_tags_extract(tags_array, tag_types);
253 if ((typedefs) && (typedefs->len > 0))
255 s = g_string_sized_new(typedefs->len * 10);
256 for (j = 0; j < typedefs->len; ++j)
258 tag = TM_TAG(typedefs->pdata[j]);
259 /* tag->atts.file.lang contains (for some reason) the line of the tag if
260 * tag->atts.entry.file is not NULL */
261 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
263 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
264 * e.g. PHP classes in C++ files
265 * lang = -2 disables the check */
266 if (tag->name && (tag_lang == lang || lang == -2))
268 if (j != 0)
269 g_string_append_c(s, ' ');
270 g_string_append(s, tag->name);
274 if (typedefs)
275 g_ptr_array_free(typedefs, TRUE);
276 return s;
280 /** Gets the context separator used by the tag manager for a particular file
281 * type.
282 * @param ft_id File type identifier.
283 * @return The context separator string.
285 * @since 0.19
287 const gchar *symbols_get_context_separator(gint ft_id)
289 switch (ft_id)
291 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
292 case GEANY_FILETYPES_CPP:
293 case GEANY_FILETYPES_GLSL: /* for structs */
294 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
295 return "::";
297 /* avoid confusion with other possible separators in group/section name */
298 case GEANY_FILETYPES_CONF:
299 case GEANY_FILETYPES_REST:
300 return ":::";
302 default:
303 return ".";
308 GString *symbols_get_macro_list(gint lang)
310 guint j, i;
311 GPtrArray *ftags;
312 GString *words;
313 gint tag_lang;
314 TMTag *tag;
316 if (app->tm_workspace->work_objects == NULL)
317 return NULL;
319 ftags = g_ptr_array_sized_new(50);
320 words = g_string_sized_new(200);
322 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
324 GPtrArray *tags;
326 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
327 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
328 if (NULL != tags)
330 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
332 tag = TM_TAG(tags->pdata[i]);
333 tag_lang = (tag->atts.entry.file) ?
334 tag->atts.entry.file->lang : tag->atts.file.lang;
336 if (tag_lang == lang)
337 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
339 g_ptr_array_free(tags, TRUE);
343 if (ftags->len == 0)
345 g_ptr_array_free(ftags, TRUE);
346 g_string_free(words, TRUE);
347 return NULL;
350 tm_tags_sort(ftags, NULL, FALSE);
351 for (j = 0; j < ftags->len; j++)
353 if (j > 0)
354 g_string_append_c(words, '\n');
355 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
357 g_ptr_array_free(ftags, TRUE);
358 return words;
362 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
363 static TMTag *
364 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
366 guint i;
367 g_return_val_if_fail(tags != NULL, NULL);
369 for (i = 0; i < tags->len; ++i)
371 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
372 return TM_TAG(tags->pdata[i]);
374 return NULL;
378 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
379 const gchar *tag_name, guint type)
381 GPtrArray *tags;
382 TMTag *tmtag;
384 if (G_LIKELY(workobj != NULL))
386 tags = tm_tags_extract(workobj->tags_array, type);
387 if (tags != NULL)
389 tmtag = symbols_find_tm_tag(tags, tag_name);
391 g_ptr_array_free(tags, TRUE);
393 if (tmtag != NULL)
394 return tmtag;
397 return NULL; /* not found */
401 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
403 guint j;
404 const GPtrArray *work_objects = NULL;
406 if (app->tm_workspace != NULL)
407 work_objects = app->tm_workspace->work_objects;
409 if (work_objects != NULL)
411 for (j = 0; j < work_objects->len; j++)
413 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
414 TMTag *tmtag;
416 tmtag = find_work_object_tag(workobj, tag_name, type);
417 if (tmtag != NULL)
418 return tmtag;
421 return NULL; /* not found */
425 const gchar **symbols_get_html_entities(void)
427 if (html_entities == NULL)
428 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
430 return (const gchar **) html_entities;
434 /* sort by name, then line */
435 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
437 gint ret;
439 if (tag_a == NULL || tag_b == NULL)
440 return 0;
442 if (tag_a->name == NULL)
443 return -(tag_a->name != tag_b->name);
445 if (tag_b->name == NULL)
446 return tag_a->name != tag_b->name;
448 ret = strcmp(tag_a->name, tag_b->name);
449 if (ret == 0)
451 return tag_a->atts.entry.line - tag_b->atts.entry.line;
453 return ret;
457 /* sort by line, then scope */
458 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
460 const TMTag *tag_a = TM_TAG(a);
461 const TMTag *tag_b = TM_TAG(b);
462 gint ret;
464 if (a == NULL || b == NULL)
465 return 0;
467 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
468 if (ret == 0)
470 if (tag_a->atts.entry.scope == NULL)
471 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
472 if (tag_b->atts.entry.scope == NULL)
473 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
474 else
475 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
477 return ret;
481 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
483 GList *tag_names = NULL;
484 TMTag *tag;
485 guint i;
487 g_return_val_if_fail(doc, NULL);
489 if (! doc->tm_file || ! doc->tm_file->tags_array)
490 return NULL;
492 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
494 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
495 if (G_UNLIKELY(tag == NULL))
496 return NULL;
498 if (tag->type & tag_types)
500 tag_names = g_list_prepend(tag_names, tag);
503 tag_names = g_list_sort(tag_names, compare_symbol_lines);
504 return tag_names;
508 /* amount of types in the symbol list (currently max. 8 are used) */
509 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
511 struct TreeviewSymbols
513 GtkTreeIter tag_function;
514 GtkTreeIter tag_class;
515 GtkTreeIter tag_macro;
516 GtkTreeIter tag_member;
517 GtkTreeIter tag_variable;
518 GtkTreeIter tag_namespace;
519 GtkTreeIter tag_struct;
520 GtkTreeIter tag_interface;
521 GtkTreeIter tag_type;
522 GtkTreeIter tag_other;
523 } tv_iters;
526 static void init_tag_iters(void)
528 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
529 * filetypes(e.g. config file to Python crashes Geany without this) */
530 tv_iters.tag_function.stamp = -1;
531 tv_iters.tag_class.stamp = -1;
532 tv_iters.tag_member.stamp = -1;
533 tv_iters.tag_macro.stamp = -1;
534 tv_iters.tag_variable.stamp = -1;
535 tv_iters.tag_namespace.stamp = -1;
536 tv_iters.tag_struct.stamp = -1;
537 tv_iters.tag_interface.stamp = -1;
538 tv_iters.tag_type.stamp = -1;
539 tv_iters.tag_other.stamp = -1;
543 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
545 static GtkIconTheme *icon_theme = NULL;
546 static gint x, y;
548 if (G_UNLIKELY(icon_theme == NULL))
550 #ifndef G_OS_WIN32
551 gchar *path = g_build_filename(GEANY_DATADIR, "icons", NULL);
552 #endif
553 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &y);
554 icon_theme = gtk_icon_theme_get_default();
555 #ifdef G_OS_WIN32
556 gtk_icon_theme_append_search_path(icon_theme, "share\\icons");
557 #else
558 gtk_icon_theme_append_search_path(icon_theme, path);
559 g_free(path);
560 #endif
562 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
566 /* finds the next iter at any level
567 * @param iter in/out, the current iter, will be changed to the next one
568 * @param down whether to try the child iter
569 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
570 static gboolean next_iter(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
572 GtkTreeIter guess;
573 GtkTreeIter copy = *iter;
575 /* go down if the item has children */
576 if (down && gtk_tree_model_iter_children(model, &guess, iter))
577 *iter = guess;
578 /* or to the next item at the same level */
579 else if (gtk_tree_model_iter_next(model, &copy))
580 *iter = copy;
581 /* or to the next item at a parent level */
582 else if (gtk_tree_model_iter_parent(model, &guess, iter))
584 copy = guess;
585 while(TRUE)
587 if (gtk_tree_model_iter_next(model, &copy))
589 *iter = copy;
590 return TRUE;
592 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
593 guess = copy;
594 else
595 return FALSE;
598 else
599 return FALSE;
601 return TRUE;
605 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
607 GtkTreeModel *model = GTK_TREE_MODEL(store);
609 if (!gtk_tree_model_get_iter_first(model, iter))
610 return FALSE;
613 gchar *candidate;
615 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
616 /* FIXME: what if 2 different items have the same name?
617 * this should never happen, but might be caused by a typo in a translation */
618 if (utils_str_equal(candidate, title))
620 g_free(candidate);
621 return TRUE;
623 else
624 g_free(candidate);
626 while (gtk_tree_model_iter_next(model, iter));
628 return FALSE;
632 /* Adds symbol list groups in (iter*, title) pairs.
633 * The list must be ended with NULL. */
634 static void G_GNUC_NULL_TERMINATED
635 tag_list_add_groups(GtkTreeStore *tree_store, ...)
637 va_list args;
638 GtkTreeIter *iter;
640 g_return_if_fail(top_level_iter_names);
642 va_start(args, tree_store);
643 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
645 gchar *title = va_arg(args, gchar*);
646 gchar *icon_name = va_arg(args, gchar *);
647 GdkPixbuf *icon = NULL;
649 if (icon_name)
651 icon = get_tag_icon(icon_name);
654 g_assert(title != NULL);
655 g_ptr_array_add(top_level_iter_names, title);
657 if (!find_toplevel_iter(tree_store, iter, title))
658 gtk_tree_store_append(tree_store, iter, NULL);
660 if (G_IS_OBJECT(icon))
662 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
663 g_object_unref(icon);
665 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
667 va_end(args);
671 static void add_top_level_items(GeanyDocument *doc)
673 filetype_id ft_id = doc->file_type->id;
674 GtkTreeStore *tag_store = doc->priv->tag_store;
676 if (top_level_iter_names == NULL)
677 top_level_iter_names = g_ptr_array_new();
678 else
679 g_ptr_array_set_size(top_level_iter_names, 0);
681 init_tag_iters();
683 switch (ft_id)
685 case GEANY_FILETYPES_DIFF:
687 tag_list_add_groups(tag_store,
688 &(tv_iters.tag_function), _("Files"), NULL, NULL);
689 break;
691 case GEANY_FILETYPES_DOCBOOK:
693 tag_list_add_groups(tag_store,
694 &(tv_iters.tag_function), _("Chapter"), NULL,
695 &(tv_iters.tag_class), _("Section"), NULL,
696 &(tv_iters.tag_member), _("Sect1"), NULL,
697 &(tv_iters.tag_macro), _("Sect2"), NULL,
698 &(tv_iters.tag_variable), _("Sect3"), NULL,
699 &(tv_iters.tag_struct), _("Appendix"), NULL,
700 &(tv_iters.tag_other), _("Other"), NULL,
701 NULL);
702 break;
704 case GEANY_FILETYPES_HASKELL:
705 tag_list_add_groups(tag_store,
706 &tv_iters.tag_namespace, _("Module"), NULL,
707 &tv_iters.tag_type, _("Types"), NULL,
708 &tv_iters.tag_macro, _("Type constructors"), NULL,
709 &tv_iters.tag_function, _("Functions"), "classviewer-method",
710 NULL);
711 break;
712 case GEANY_FILETYPES_COBOL:
713 tag_list_add_groups(tag_store,
714 &tv_iters.tag_class, _("Program"), "classviewer-class",
715 &tv_iters.tag_function, _("File"), "classviewer-method",
716 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
717 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
718 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
719 &tv_iters.tag_variable, _("Data"), "classviewer-var",
720 NULL);
721 break;
722 case GEANY_FILETYPES_CONF:
723 tag_list_add_groups(tag_store,
724 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
725 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
726 NULL);
727 break;
728 case GEANY_FILETYPES_NSIS:
729 tag_list_add_groups(tag_store,
730 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
731 &tv_iters.tag_function, _("Functions"), "classviewer-method",
732 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
733 NULL);
734 break;
735 case GEANY_FILETYPES_LATEX:
737 tag_list_add_groups(tag_store,
738 &(tv_iters.tag_function), _("Command"), NULL,
739 &(tv_iters.tag_class), _("Environment"), NULL,
740 &(tv_iters.tag_member), _("Section"), NULL,
741 &(tv_iters.tag_macro), _("Subsection"), NULL,
742 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
743 &(tv_iters.tag_struct), _("Label"), NULL,
744 &(tv_iters.tag_namespace), _("Chapter"), NULL,
745 &(tv_iters.tag_other), _("Other"), NULL,
746 NULL);
747 break;
749 case GEANY_FILETYPES_MATLAB:
751 tag_list_add_groups(tag_store,
752 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
753 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
754 NULL);
755 break;
757 case GEANY_FILETYPES_R:
759 tag_list_add_groups(tag_store,
760 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
761 &(tv_iters.tag_struct), _("Other"), NULL,
762 NULL);
763 break;
765 case GEANY_FILETYPES_PERL:
767 tag_list_add_groups(tag_store,
768 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
769 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
770 &(tv_iters.tag_macro), _("Labels"), NULL,
771 &(tv_iters.tag_type), _("Constants"), NULL,
772 &(tv_iters.tag_other), _("Other"), "classviewer-other",
773 NULL);
774 break;
776 case GEANY_FILETYPES_PHP:
778 tag_list_add_groups(tag_store,
779 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
780 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
781 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
782 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
783 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
784 NULL);
785 break;
787 case GEANY_FILETYPES_HTML:
789 tag_list_add_groups(tag_store,
790 &(tv_iters.tag_function), _("Functions"), NULL,
791 &(tv_iters.tag_member), _("Anchors"), NULL,
792 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
793 &(tv_iters.tag_class), _("H2 Headings"), NULL,
794 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
795 NULL);
796 break;
798 case GEANY_FILETYPES_CSS:
800 tag_list_add_groups(tag_store,
801 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
802 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
803 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
804 break;
806 case GEANY_FILETYPES_REST:
807 case GEANY_FILETYPES_TXT2TAGS:
808 case GEANY_FILETYPES_ABC:
810 tag_list_add_groups(tag_store,
811 &(tv_iters.tag_namespace), _("Chapter"), NULL,
812 &(tv_iters.tag_member), _("Section"), NULL,
813 &(tv_iters.tag_macro), _("Subsection"), NULL,
814 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
815 NULL);
816 break;
818 case GEANY_FILETYPES_RUBY:
820 tag_list_add_groups(tag_store,
821 &(tv_iters.tag_namespace), _("Modules"), NULL,
822 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
823 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
824 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
825 NULL);
826 break;
828 case GEANY_FILETYPES_TCL:
830 tag_list_add_groups(tag_store,
831 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
832 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
833 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
834 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
835 NULL);
836 break;
838 case GEANY_FILETYPES_PYTHON:
840 tag_list_add_groups(tag_store,
841 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
842 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
843 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
844 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
845 &(tv_iters.tag_namespace), _("Imports"), "classviewer-namespace",
846 NULL);
847 break;
849 case GEANY_FILETYPES_VHDL:
851 tag_list_add_groups(tag_store,
852 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
853 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
854 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
855 &(tv_iters.tag_type), _("Types"), "classviewer-other",
856 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
857 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
858 &(tv_iters.tag_member), _("Processes / Blocks / Components"), "classviewer-member",
859 &(tv_iters.tag_other), _("Other"), "classviewer-other",
860 NULL);
861 break;
863 case GEANY_FILETYPES_VERILOG:
865 tag_list_add_groups(tag_store,
866 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
867 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
868 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
869 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
870 &(tv_iters.tag_other), _("Other"), "classviewer-other",
871 NULL);
872 break;
874 case GEANY_FILETYPES_JAVA:
876 tag_list_add_groups(tag_store,
877 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
878 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
879 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
880 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
881 &(tv_iters.tag_member), _("Members"), "classviewer-member",
882 &(tv_iters.tag_other), _("Other"), "classviewer-other",
883 NULL);
884 break;
886 case GEANY_FILETYPES_AS:
888 tag_list_add_groups(tag_store,
889 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
890 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
891 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
892 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
893 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
894 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
895 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
896 &(tv_iters.tag_other), _("Other"), "classviewer-other",
897 NULL);
898 break;
900 case GEANY_FILETYPES_HAXE:
902 tag_list_add_groups(tag_store,
903 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
904 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
905 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
906 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
907 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
908 &(tv_iters.tag_other), _("Other"), "classviewer-other",
909 NULL);
910 break;
912 case GEANY_FILETYPES_BASIC:
914 tag_list_add_groups(tag_store,
915 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
916 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
917 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
918 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
919 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
920 &(tv_iters.tag_other), _("Other"), "classviewer-other",
921 NULL);
922 break;
924 case GEANY_FILETYPES_F77:
925 case GEANY_FILETYPES_FORTRAN:
927 tag_list_add_groups(tag_store,
928 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
929 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
930 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
931 &(tv_iters.tag_member), _("Subroutines"), "classviewer-method",
932 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
933 &(tv_iters.tag_type), _("Types"), "classviewer-namespace",
934 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
935 &(tv_iters.tag_other), _("Other"), "classviewer-other",
936 NULL);
937 break;
939 case GEANY_FILETYPES_ASM:
941 tag_list_add_groups(tag_store,
942 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
943 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
944 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
945 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
946 NULL);
947 break;
949 case GEANY_FILETYPES_MAKE:
950 tag_list_add_groups(tag_store,
951 &tv_iters.tag_function, _("Targets"), "classviewer-method",
952 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
953 NULL);
954 break;
955 case GEANY_FILETYPES_SQL:
957 tag_list_add_groups(tag_store,
958 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
959 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
960 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
961 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
962 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
963 &(tv_iters.tag_member), _("Views"), "classviewer-var",
964 &(tv_iters.tag_other), _("Other"), "classviewer-other",
965 NULL);
966 break;
968 case GEANY_FILETYPES_D:
969 default:
971 if (ft_id == GEANY_FILETYPES_D)
972 tag_list_add_groups(tag_store,
973 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
974 else
975 tag_list_add_groups(tag_store,
976 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
978 tag_list_add_groups(tag_store,
979 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
980 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
981 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
982 &(tv_iters.tag_member), _("Members"), "classviewer-member",
983 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
984 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
985 NULL);
987 if (ft_id != GEANY_FILETYPES_D)
989 tag_list_add_groups(tag_store,
990 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
992 tag_list_add_groups(tag_store,
993 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
994 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
1000 /* removes toplevel items that have no children */
1001 static void hide_empty_rows(GtkTreeStore *store)
1003 GtkTreeIter iter;
1004 gboolean cont = TRUE;
1006 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1007 return; /* stop when first iter is invalid, i.e. no elements */
1009 while (cont)
1011 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1012 cont = gtk_tree_store_remove(store, &iter);
1013 else
1014 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1019 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1021 gchar *utf8_name;
1022 const gchar *scope = tag->atts.entry.scope;
1023 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1024 gboolean doc_is_utf8 = FALSE;
1026 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1027 * for None at this point completely */
1028 if (utils_str_equal(doc->encoding, "UTF-8") ||
1029 utils_str_equal(doc->encoding, "None"))
1030 doc_is_utf8 = TRUE;
1032 if (! doc_is_utf8)
1033 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1034 -1, doc->encoding, TRUE);
1035 else
1036 utf8_name = tag->name;
1038 if (utf8_name == NULL)
1039 return NULL;
1041 if (! buffer)
1042 buffer = g_string_new(NULL);
1043 else
1044 g_string_truncate(buffer, 0);
1046 /* check first char of scope is a wordchar */
1047 if (!found_parent && scope &&
1048 strpbrk(scope, GEANY_WORDCHARS) == scope)
1050 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1052 g_string_append(buffer, scope);
1053 g_string_append(buffer, sep);
1055 g_string_append(buffer, utf8_name);
1057 if (! doc_is_utf8)
1058 g_free(utf8_name);
1060 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1062 return buffer->str;
1066 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1068 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1070 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1071 * for None at this point completely */
1072 if (utf8_name != NULL &&
1073 ! utils_str_equal(doc->encoding, "UTF-8") &&
1074 ! utils_str_equal(doc->encoding, "None"))
1076 SETPTR(utf8_name,
1077 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1080 if (utf8_name != NULL)
1081 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1083 return utf8_name;
1087 /* find the last word in "foo::bar::blah", e.g. "blah" */
1088 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1090 const gchar *scope = tag->atts.entry.scope;
1091 const gchar *separator = symbols_get_context_separator(ft_id);
1092 const gchar *str, *ptr;
1094 if (!scope)
1095 return NULL;
1097 str = scope;
1099 while (1)
1101 ptr = strstr(str, separator);
1102 if (ptr)
1104 str = ptr + strlen(separator);
1106 else
1107 break;
1110 return NZV(str) ? str : NULL;
1114 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1116 GtkTreeIter *iter = NULL;
1118 switch (tag_type)
1120 case tm_tag_prototype_t:
1121 case tm_tag_method_t:
1122 case tm_tag_function_t:
1124 iter = &tv_iters.tag_function;
1125 break;
1127 case tm_tag_macro_t:
1128 case tm_tag_macro_with_arg_t:
1130 iter = &tv_iters.tag_macro;
1131 break;
1133 case tm_tag_class_t:
1135 iter = &tv_iters.tag_class;
1136 break;
1138 case tm_tag_member_t:
1139 case tm_tag_field_t:
1141 iter = &tv_iters.tag_member;
1142 break;
1144 case tm_tag_typedef_t:
1145 case tm_tag_enum_t:
1147 iter = &tv_iters.tag_type;
1148 break;
1150 case tm_tag_union_t:
1151 case tm_tag_struct_t:
1153 iter = &tv_iters.tag_struct;
1154 break;
1156 case tm_tag_interface_t:
1157 iter = &tv_iters.tag_interface;
1158 break;
1159 case tm_tag_variable_t:
1161 iter = &tv_iters.tag_variable;
1162 break;
1164 case tm_tag_namespace_t:
1165 case tm_tag_package_t:
1167 iter = &tv_iters.tag_namespace;
1168 break;
1170 default:
1172 iter = &tv_iters.tag_other;
1175 if (G_LIKELY(iter->stamp != -1))
1176 return iter;
1177 else
1178 return NULL;
1182 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1184 GdkPixbuf *icon = NULL;
1186 if (parent == &tv_iters.tag_other)
1188 return get_tag_icon("classviewer-var");
1190 /* copy parent icon */
1191 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1192 SYMBOLS_COLUMN_ICON, &icon, -1);
1193 return icon;
1197 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1199 const TMTag *t1 = v1;
1200 const TMTag *t2 = v2;
1202 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1203 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1204 /* include arglist in match to support e.g. C++ overloading */
1205 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1209 /* inspired from g_str_hash() */
1210 static guint tag_hash(gconstpointer v)
1212 const TMTag *tag = v;
1213 const gchar *p;
1214 guint32 h = 5381;
1216 h = (h << 5) + h + tag->type;
1217 for (p = tag->name; *p != '\0'; p++)
1218 h = (h << 5) + h + *p;
1219 if (tag->atts.entry.scope)
1221 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1222 h = (h << 5) + h + *p;
1224 /* for e.g. C++ overloading */
1225 if (tag->atts.entry.arglist)
1227 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1228 h = (h << 5) + h + *p;
1231 return h;
1235 /* like gtk_tree_view_expand_to_path() but with an iter */
1236 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1238 GtkTreeModel *model = gtk_tree_view_get_model(view);
1239 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1241 gtk_tree_view_expand_to_path(view, path);
1242 gtk_tree_path_free(path);
1246 /* like gtk_tree_store_remove() but finds the next iter at any level */
1247 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1249 GtkTreeIter parent;
1250 gboolean has_parent;
1251 gboolean cont;
1253 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1254 cont = gtk_tree_store_remove(store, iter);
1255 /* if there is no next at this level but there is a parent iter, continue from it */
1256 if (! cont && has_parent)
1258 *iter = parent;
1259 cont = next_iter(GTK_TREE_MODEL(store), iter, FALSE);
1262 return cont;
1266 /* adds a new element in the parent table if it's key is known.
1267 * duplicates are kept */
1268 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1269 const GtkTreeIter *iter)
1271 GList **list;
1272 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1273 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1275 if (! list)
1277 list = g_slice_alloc(sizeof *list);
1278 *list = NULL;
1279 g_hash_table_insert(table, tag->name, list);
1281 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1286 static void free_iter_slice_list(gpointer data)
1288 GList **list = data;
1290 if (list)
1292 GList *node;
1293 foreach_list(node, *list)
1294 g_slice_free(GtkTreeIter, node->data);
1295 g_list_free(*list);
1296 g_slice_free1(sizeof *list, list);
1301 /* inserts a @data in @table on key @tag.
1302 * previous data is not overwritten if the key is duplicated, but rather the
1303 * two values are kept in a list
1305 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1306 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1308 GList *list = g_hash_table_lookup(table, tag);
1309 list = g_list_prepend(list, data);
1310 g_hash_table_insert(table, tag, list);
1314 /* looks up the entry in @table that better matches @tag.
1315 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1316 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1318 GList *data = NULL;
1319 GList *node = g_hash_table_lookup(table, tag);
1320 if (node)
1322 glong delta;
1323 data = node->data;
1325 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1327 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1328 for (node = node->next; node; node = node->next)
1330 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1332 if (d < delta)
1334 data = node->data;
1335 delta = d;
1339 #undef TAG_DELTA
1342 return data;
1346 /* removes the element at @tag from @table.
1347 * @tag must be the exact pointer used at insertion time */
1348 static void tags_table_remove(GHashTable *table, TMTag *tag)
1350 GList *list = g_hash_table_lookup(table, tag);
1351 if (list)
1353 GList *node;
1354 foreach_list(node, list)
1356 if (((GList *) node->data)->data == tag)
1357 break;
1359 list = g_list_delete_link(list, node);
1360 if (list)
1361 g_hash_table_insert(table, tag, list);
1362 else
1363 g_hash_table_remove(table, tag);
1369 * Updates the tag tree for a document with the tags in *list.
1370 * @param doc a document
1371 * @param tags a pointer to a GList* holding the tags to add/update. This
1372 * list may be updated, removing updated elements.
1374 * The update is done in two passes:
1375 * 1) walking the current tree, update tags that still exist and remove the
1376 * obsolescent ones;
1377 * 2) walking the remaining (non updated) tags, adds them in the list.
1379 * For better performances, we use 2 hash tables:
1380 * - one containing all the tags for lookup in the first pass (actually stores a
1381 * reference in the tags list for removing it efficiently), avoiding list search
1382 * on each tag;
1383 * - the other holding "tag-name":row references for tags having children, used to
1384 * lookup for a parent in both passes, avoiding tree traversal.
1386 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1388 GtkTreeStore *store = doc->priv->tag_store;
1389 GtkTreeModel *model = GTK_TREE_MODEL(store);
1390 GHashTable *parents_table;
1391 GHashTable *tags_table;
1392 GtkTreeIter iter;
1393 gboolean cont;
1394 GList *item;
1396 /* Build hash tables holding tags and parents */
1397 /* parent table holds "tag-name":GtkTreeIter */
1398 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1399 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1400 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1401 foreach_list(item, *tags)
1403 TMTag *tag = item->data;
1404 const gchar *name;
1406 tags_table_insert(tags_table, tag, item);
1408 name = get_parent_name(tag, doc->file_type->id);
1409 if (name)
1410 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1413 /* First pass, update existing rows or delete them.
1414 * It is OK to delete them since we walk top down so we would remove
1415 * parents before checking for their children, thus never implicitly
1416 * deleting an updated child */
1417 cont = gtk_tree_model_get_iter_first(model, &iter);
1418 while (cont)
1420 TMTag *tag;
1422 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1423 if (! tag) /* most probably a toplevel, skip it */
1424 cont = next_iter(model, &iter, TRUE);
1425 else
1427 GList *found_item;
1429 found_item = tags_table_lookup(tags_table, tag);
1430 if (! found_item) /* tag doesn't exist, remove it */
1431 cont = tree_store_remove_row(store, &iter);
1432 else /* tag still exist, update it */
1434 const gchar *name;
1435 const gchar *parent_name;
1436 TMTag *found = found_item->data;
1438 parent_name = get_parent_name(found, doc->file_type->id);
1439 /* if parent is unknown, ignore it */
1440 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1441 parent_name = NULL;
1443 /* only update fields that (can) have changed (name that holds line
1444 * number, and the tag itself) */
1445 name = get_symbol_name(doc, found, parent_name != NULL);
1446 gtk_tree_store_set(store, &iter,
1447 SYMBOLS_COLUMN_NAME, name,
1448 SYMBOLS_COLUMN_TAG, found,
1449 -1);
1451 update_parents_table(parents_table, found, parent_name, &iter);
1453 /* remove the updated tag from the table and list */
1454 tags_table_remove(tags_table, found);
1455 *tags = g_list_delete_link(*tags, found_item);
1457 cont = next_iter(model, &iter, TRUE);
1460 tm_tag_unref(tag);
1464 /* Second pass, now we have a tree cleaned up from invalid rows,
1465 * we simply add new ones */
1466 foreach_list (item, *tags)
1468 TMTag *tag = item->data;
1469 GtkTreeIter *parent;
1471 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1472 if (G_UNLIKELY(! parent))
1473 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1474 else
1476 gboolean expand;
1477 const gchar *name;
1478 const gchar *parent_name;
1479 gchar *tooltip;
1480 GdkPixbuf *icon = get_child_icon(store, parent);
1482 parent_name = get_parent_name(tag, doc->file_type->id);
1483 if (parent_name)
1485 GList **candidates;
1486 GtkTreeIter *parent_search = NULL;
1488 /* walk parent candidates to find the better one.
1489 * if there are more than one, take the one that has the closest line number
1490 * after the tag we're searching the parent for */
1491 candidates = g_hash_table_lookup(parents_table, parent_name);
1492 if (candidates)
1494 GList *node;
1495 glong delta = G_MAXLONG;
1496 foreach_list(node, *candidates)
1498 TMTag *parent_tag;
1499 glong d;
1501 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1502 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1504 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1505 if (! parent_search || (d >= 0 && d < delta))
1507 delta = d;
1508 parent_search = node->data;
1513 if (parent_search)
1514 parent = parent_search;
1515 else
1516 parent_name = NULL;
1519 /* only expand to the iter if the parent was empty, otherwise we let the
1520 * folding as it was before (already expanded, or closed by the user) */
1521 expand = ! gtk_tree_model_iter_has_child(model, parent);
1523 /* insert the new element */
1524 gtk_tree_store_append(store, &iter, parent);
1525 name = get_symbol_name(doc, tag, parent_name != NULL);
1526 tooltip = get_symbol_tooltip(doc, tag);
1527 gtk_tree_store_set(store, &iter,
1528 SYMBOLS_COLUMN_NAME, name,
1529 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1530 SYMBOLS_COLUMN_ICON, icon,
1531 SYMBOLS_COLUMN_TAG, tag,
1532 -1);
1533 g_free(tooltip);
1534 if (G_LIKELY(icon))
1535 g_object_unref(icon);
1537 update_parents_table(parents_table, tag, parent_name, &iter);
1539 if (expand)
1540 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1544 g_hash_table_destroy(parents_table);
1545 g_hash_table_destroy(tags_table);
1549 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1550 * is not stable, so the order is already lost. */
1551 static gint compare_top_level_names(const gchar *a, const gchar *b)
1553 guint i;
1554 const gchar *name;
1556 /* This should never happen as it would mean that two or more top
1557 * level items have the same name but it can happen by typos in the translations. */
1558 if (utils_str_equal(a, b))
1559 return 1;
1561 foreach_ptr_array(name, i, top_level_iter_names)
1563 if (utils_str_equal(name, a))
1564 return -1;
1565 if (utils_str_equal(name, b))
1566 return 1;
1568 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1569 return 0;
1573 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1574 GtkTreeIter *iter)
1576 /* if the tag has a parent tag, it should be at depth >= 2 */
1577 return NZV(tag->atts.entry.scope) &&
1578 gtk_tree_store_iter_depth(store, iter) == 1;
1582 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1583 gpointer user_data)
1585 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1586 TMTag *tag_a, *tag_b;
1587 gint cmp;
1589 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1590 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1592 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1593 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1594 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1595 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1597 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1598 compare_symbol_lines(tag_a, tag_b);
1600 else
1602 gchar *astr, *bstr;
1604 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1605 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1607 /* if a is toplevel, b must be also */
1608 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1610 cmp = compare_top_level_names(astr, bstr);
1612 else
1614 /* this is what g_strcmp0() does */
1615 if (! astr)
1616 cmp = -(astr != bstr);
1617 else if (! bstr)
1618 cmp = astr != bstr;
1619 else
1621 cmp = strcmp(astr, bstr);
1623 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1624 if (tag_a && tag_b)
1625 if (!sort_by_name ||
1626 (utils_str_equal(tag_a->name, tag_b->name) &&
1627 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1628 cmp = compare_symbol_lines(tag_a, tag_b);
1631 g_free(astr);
1632 g_free(bstr);
1634 tm_tag_unref(tag_a);
1635 tm_tag_unref(tag_b);
1637 return cmp;
1641 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1643 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1644 GINT_TO_POINTER(sort_by_name), NULL);
1646 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1650 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1652 GList *tags;
1654 g_return_val_if_fail(doc != NULL, FALSE);
1656 tags = get_tag_list(doc, tm_tag_max_t);
1657 if (tags == NULL)
1658 return FALSE;
1660 /* FIXME: Not sure why we detached the model here? */
1662 /* disable sorting during update because the code doesn't support correctly
1663 * models that are currently being built */
1664 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1666 /* add grandparent type iters */
1667 add_top_level_items(doc);
1669 update_tree_tags(doc, &tags);
1670 g_list_free(tags);
1672 hide_empty_rows(doc->priv->tag_store);
1674 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1675 sort_mode = doc->priv->symbol_list_sort_mode;
1677 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1678 doc->priv->symbol_list_sort_mode = sort_mode;
1680 return TRUE;
1684 /* Detects a global tags filetype from the *.lang.* language extension.
1685 * Returns NULL if there was no matching TM language. */
1686 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1688 gchar *tags_ext;
1689 gchar *shortname = utils_strdupa(utf8_filename);
1690 GeanyFiletype *ft = NULL;
1692 tags_ext = g_strrstr(shortname, ".tags");
1693 if (tags_ext)
1695 *tags_ext = '\0'; /* remove .tags extension */
1696 ft = filetypes_detect_from_extension(shortname);
1697 if (ft->id != GEANY_FILETYPES_NONE)
1698 return ft;
1700 return NULL;
1704 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1705 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1706 * the relevant path.
1707 * Example:
1708 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1709 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1711 /* -E pre-process, -dD output user macros, -p prof info (?) */
1712 const char pre_process[] = "gcc -E -dD -p -I.";
1714 if (argc > 2)
1716 /* Create global taglist */
1717 int status;
1718 char *command;
1719 const char *tags_file = argv[1];
1720 char *utf8_fname;
1721 GeanyFiletype *ft;
1723 utf8_fname = utils_get_utf8_from_locale(tags_file);
1724 ft = detect_global_tags_filetype(utf8_fname);
1725 g_free(utf8_fname);
1727 if (ft == NULL)
1729 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1730 return 1;
1732 /* load config in case of custom filetypes */
1733 filetypes_load_config(ft->id, FALSE);
1735 /* load ignore list for C/C++ parser */
1736 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1737 load_c_ignore_tags();
1739 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1740 command = g_strdup_printf("%s %s", pre_process, NVL(getenv("CFLAGS"), ""));
1741 else
1742 command = NULL; /* don't preprocess */
1744 geany_debug("Generating %s tags file.", ft->name);
1745 tm_get_workspace();
1746 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1747 argc - 2, tags_file, ft->lang);
1748 g_free(command);
1749 symbols_finalize(); /* free c_tags_ignore data */
1750 if (! status)
1752 g_printerr(_("Failed to create tags file, perhaps because no tags "
1753 "were found.\n"));
1754 return 1;
1757 else
1759 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1760 g_printerr(_("Example:\n"
1761 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1762 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1763 return 1;
1765 return 0;
1769 void symbols_show_load_tags_dialog(void)
1771 GtkWidget *dialog;
1772 GtkFileFilter *filter;
1774 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1775 GTK_FILE_CHOOSER_ACTION_OPEN,
1776 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1777 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1778 NULL);
1779 gtk_widget_set_name(dialog, "GeanyDialog");
1780 filter = gtk_file_filter_new();
1781 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1782 gtk_file_filter_add_pattern(filter, "*.*.tags");
1783 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1785 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1787 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1788 GSList *item;
1790 for (item = flist; item != NULL; item = g_slist_next(item))
1792 gchar *fname = item->data;
1793 gchar *utf8_fname;
1794 GeanyFiletype *ft;
1796 utf8_fname = utils_get_utf8_from_locale(fname);
1797 ft = detect_global_tags_filetype(utf8_fname);
1799 if (ft != NULL && symbols_load_global_tags(fname, ft))
1800 /* For translators: the first wildcard is the filetype, the second the filename */
1801 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1802 filetypes_get_display_name(ft), utf8_fname);
1803 else
1804 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1806 g_free(utf8_fname);
1807 g_free(fname);
1809 g_slist_free(flist);
1811 gtk_widget_destroy(dialog);
1815 static void detect_tag_files(const GSList *file_list)
1817 const GSList *node;
1819 for (node = file_list; node != NULL; node = g_slist_next(node))
1821 gchar *fname = node->data;
1822 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1823 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1825 g_free(utf8_fname);
1827 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1828 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1829 else
1830 geany_debug("Unknown filetype for file '%s'.", fname);
1835 static void init_user_tags(void)
1837 GSList *file_list = NULL, *list = NULL;
1838 gchar *dir;
1840 dir = g_build_filename(app->configdir, "tags", NULL);
1841 /* create the user tags dir for next time if it doesn't exist */
1842 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1844 utils_mkdir(dir, FALSE);
1846 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1848 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1849 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1850 g_free(dir);
1852 file_list = g_slist_concat(file_list, list);
1853 detect_tag_files(file_list);
1854 /* don't need to delete list contents because they are stored in ft->priv->tag_files */
1855 g_slist_free(file_list);
1859 static void load_user_tags(filetype_id ft_id)
1861 static guchar *tags_loaded = NULL;
1862 static gboolean init_tags = FALSE;
1863 const GSList *node;
1864 GeanyFiletype *ft = filetypes[ft_id];
1866 g_return_if_fail(ft_id > 0);
1868 if (!tags_loaded)
1869 tags_loaded = g_new0(guchar, filetypes_array->len);
1870 if (tags_loaded[ft_id])
1871 return;
1872 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1874 if (!init_tags)
1876 init_user_tags();
1877 init_tags = TRUE;
1880 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1882 const gchar *fname = node->data;
1884 symbols_load_global_tags(fname, ft);
1889 static gboolean goto_tag(const gchar *name, gboolean definition)
1891 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1892 guint type;
1893 TMTag *tmtag = NULL;
1894 GeanyDocument *old_doc = document_get_current();
1896 /* goto tag definition: all except prototypes / forward declarations / externs */
1897 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1899 /* first look in the current document */
1900 if (old_doc != NULL && old_doc->tm_file)
1901 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1903 /* if not found, look in the workspace */
1904 if (tmtag == NULL)
1905 tmtag = find_workspace_tag(name, type);
1907 if (tmtag != NULL)
1909 GeanyDocument *new_doc = document_find_by_real_path(
1910 tmtag->atts.entry.file->work_object.file_name);
1912 if (new_doc)
1914 /* If we are already on the tag line, swap definition/declaration */
1915 if (new_doc == old_doc &&
1916 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1918 if (goto_tag(name, !definition))
1919 return TRUE;
1922 else
1924 /* not found in opened document, should open */
1925 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1928 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1929 return TRUE;
1931 return FALSE;
1935 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1937 if (goto_tag(name, definition))
1938 return TRUE;
1940 /* if we are here, there was no match and we are beeping ;-) */
1941 utils_beep();
1943 if (!definition)
1944 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1945 else
1946 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1947 return FALSE;
1951 /* This could perhaps be improved to check for #if, class etc. */
1952 static gint get_function_fold_number(GeanyDocument *doc)
1954 /* for Java the functions are always one fold level above the class scope */
1955 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1956 return SC_FOLDLEVELBASE + 1;
1957 else
1958 return SC_FOLDLEVELBASE;
1962 /* Should be used only with symbols_get_current_function. */
1963 static gboolean current_function_changed(GeanyDocument *doc, gint cur_line, gint fold_level)
1965 static gint old_line = -2;
1966 static GeanyDocument *old_doc = NULL;
1967 static gint old_fold_num = -1;
1968 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1969 gboolean ret;
1971 /* check if the cached line and file index have changed since last time: */
1972 if (doc == NULL || doc != old_doc)
1973 ret = TRUE;
1974 else
1975 if (cur_line == old_line)
1976 ret = FALSE;
1977 else
1979 /* if the line has only changed by 1 */
1980 if (abs(cur_line - old_line) == 1)
1982 const gint fn_fold =
1983 get_function_fold_number(doc);
1984 /* It's the same function if the fold number hasn't changed, or both the new
1985 * and old fold numbers are above the function fold number. */
1986 gboolean same =
1987 fold_num == old_fold_num ||
1988 (old_fold_num > fn_fold && fold_num > fn_fold);
1990 ret = ! same;
1992 else ret = TRUE;
1995 /* record current line and file index for next time */
1996 old_line = cur_line;
1997 old_doc = doc;
1998 old_fold_num = fold_num;
1999 return ret;
2003 /* Parse the function name up to 2 lines before tag_line.
2004 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2005 * type or argument names can be confused with the function name. */
2006 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2008 gint start, end, max_pos;
2009 gchar *cur_tag;
2010 gint fn_style;
2012 switch (sci_get_lexer(sci))
2014 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2015 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2016 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2018 start = sci_get_position_from_line(sci, tag_line - 2);
2019 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2020 while (sci_get_style_at(sci, start) != fn_style
2021 && start < max_pos) start++;
2023 end = start;
2024 while (sci_get_style_at(sci, end) == fn_style
2025 && end < max_pos) end++;
2027 if (start == end)
2028 return NULL;
2029 cur_tag = g_malloc(end - start + 1);
2030 sci_get_text_range(sci, start, end, cur_tag);
2031 return cur_tag;
2035 /* Parse the function name */
2036 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2038 gint start, end, first_pos, max_pos;
2039 gint tmp;
2040 gchar c;
2041 gchar *cur_tag;
2043 first_pos = end = sci_get_position_from_line(sci, tag_line);
2044 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2045 tmp = 0;
2046 /* goto the begin of function body */
2047 while (end < max_pos &&
2048 (tmp = sci_get_char_at(sci, end)) != '{' &&
2049 tmp != 0) end++;
2050 if (tmp == 0) end --;
2052 /* go back to the end of function identifier */
2053 while (end > 0 && end > first_pos - 500 &&
2054 (tmp = sci_get_char_at(sci, end)) != '(' &&
2055 tmp != 0) end--;
2056 end--;
2057 if (end < 0) end = 0;
2059 /* skip whitespaces between identifier and ( */
2060 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2062 start = end;
2063 c = 0;
2064 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2065 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2066 || tmp == SCE_C_GLOBALCLASS
2067 || (c = sci_get_char_at(sci, start)) == '~'
2068 || c == ':'))
2069 start--;
2070 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2072 if (start == end) return NULL;
2073 cur_tag = g_malloc(end - start + 2);
2074 sci_get_text_range(sci, start, end + 1, cur_tag);
2075 return cur_tag;
2079 /* Sets *tagname to point at the current function or tag name.
2080 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2081 * call to this function.
2082 * Returns: line number of the current tag, or -1 if unknown. */
2083 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2085 static gint tag_line = -1;
2086 static gchar *cur_tag = NULL;
2087 gint line;
2088 gint fold_level;
2089 TMWorkObject *tm_file;
2091 if (doc == NULL) /* reset current function */
2093 current_function_changed(NULL, -1, -1);
2094 g_free(cur_tag);
2095 cur_tag = g_strdup(_("unknown"));
2096 if (tagname != NULL)
2097 *tagname = cur_tag;
2098 tag_line = -1;
2099 return tag_line;
2102 line = sci_get_current_line(doc->editor->sci);
2103 fold_level = sci_get_fold_level(doc->editor->sci, line);
2104 /* check if the cached line and file index have changed since last time: */
2105 if (! current_function_changed(doc, line, fold_level))
2107 /* we can assume same current function as before */
2108 *tagname = cur_tag;
2109 return tag_line;
2111 g_free(cur_tag); /* free the old tag, it will be replaced. */
2113 /* if line is at base fold level, we're not in a function */
2114 if ((fold_level & SC_FOLDLEVELNUMBERMASK) == SC_FOLDLEVELBASE)
2116 cur_tag = g_strdup(_("unknown"));
2117 *tagname = cur_tag;
2118 tag_line = -1;
2119 return tag_line;
2121 tm_file = doc->tm_file;
2123 /* if the document has no changes, get the previous function name from TM */
2124 if (! doc->changed && tm_file != NULL && tm_file->tags_array != NULL)
2126 const TMTag *tag = (const TMTag*) tm_get_current_function(tm_file->tags_array, line);
2128 if (tag != NULL)
2130 gchar *tmp;
2131 tmp = tag->atts.entry.scope;
2132 cur_tag = tmp ? g_strconcat(tmp, "::", tag->name, NULL) : g_strdup(tag->name);
2133 *tagname = cur_tag;
2134 tag_line = tag->atts.entry.line;
2135 return tag_line;
2139 /* parse the current function name here because TM line numbers may have changed,
2140 * and it would take too long to reparse the whole file. */
2141 if (doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2143 const gint fn_fold = get_function_fold_number(doc);
2145 tag_line = line;
2146 do /* find the top level fold point */
2148 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2149 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2150 } while (tag_line >= 0 &&
2151 (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold);
2153 if (tag_line >= 0)
2155 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2156 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2157 else
2158 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2160 if (cur_tag != NULL)
2162 *tagname = cur_tag;
2163 return tag_line;
2168 cur_tag = g_strdup(_("unknown"));
2169 *tagname = cur_tag;
2170 tag_line = -1;
2171 return tag_line;
2175 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2177 gint sort_mode = GPOINTER_TO_INT(user_data);
2178 GeanyDocument *doc = document_get_current();
2180 if (ignore_callback)
2181 return;
2183 if (doc != NULL)
2184 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2188 static void on_symbol_tree_menu_show(GtkWidget *widget,
2189 gpointer user_data)
2191 GeanyDocument *doc = document_get_current();
2192 gboolean enable;
2194 enable = doc && doc->has_tags;
2195 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2196 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2197 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2198 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2200 if (! doc)
2201 return;
2203 ignore_callback = TRUE;
2205 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2206 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2207 else
2208 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2210 ignore_callback = FALSE;
2214 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2216 gboolean expand = GPOINTER_TO_INT(user_data);
2217 GeanyDocument *doc = document_get_current();
2219 if (! doc)
2220 return;
2222 g_return_if_fail(doc->priv->tag_tree);
2224 if (expand)
2225 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2226 else
2227 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2231 static void create_taglist_popup_menu(void)
2233 GtkWidget *item, *menu;
2235 tv.popup_taglist = menu = gtk_menu_new();
2237 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2238 gtk_widget_show(item);
2239 gtk_container_add(GTK_CONTAINER(menu), item);
2240 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2242 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2243 gtk_widget_show(item);
2244 gtk_container_add(GTK_CONTAINER(menu), item);
2245 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2247 item = gtk_separator_menu_item_new();
2248 gtk_widget_show(item);
2249 gtk_container_add(GTK_CONTAINER(menu), item);
2251 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2252 _("Sort by _Name"));
2253 gtk_widget_show(item);
2254 gtk_container_add(GTK_CONTAINER(menu), item);
2255 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2256 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2258 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2259 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2260 gtk_widget_show(item);
2261 gtk_container_add(GTK_CONTAINER(menu), item);
2262 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2263 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2265 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2267 sidebar_add_common_menu_items(GTK_MENU(menu));
2271 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2273 gchar *f = g_build_filename(app->configdir, "ignore.tags", NULL);
2275 g_return_if_fail(NZV(doc->real_path));
2277 if (utils_str_equal(doc->real_path, f))
2278 load_c_ignore_tags();
2280 g_free(f);
2284 void symbols_init(void)
2286 gchar *f;
2288 create_taglist_popup_menu();
2290 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2291 ui_add_config_file_menu_item(f, NULL, NULL);
2292 g_free(f);
2294 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2298 void symbols_finalize(void)
2300 g_strfreev(html_entities);
2301 g_strfreev(c_tags_ignore);