Force selection to change when Default or Alt colourscheme chosen
[geany-mirror.git] / src / symbols.c
blobaa0cc05616170b25b3c5b1f1c0c35ba9dd861e2f
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"
56 #include "filetypesprivate.h"
59 const guint TM_GLOBAL_TYPE_MASK =
60 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
61 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
64 static gchar **html_entities = NULL;
66 typedef struct
68 gboolean tags_loaded;
69 const gchar *tag_file;
70 } TagFileInfo;
72 /* Check before adding any more tags files, usually they should be downloaded separately. */
73 enum /* Geany tag files */
75 GTF_C,
76 GTF_PASCAL,
77 GTF_PHP,
78 GTF_HTML_ENTITIES,
79 GTF_LATEX,
80 GTF_PYTHON,
81 GTF_MAX
84 static TagFileInfo tag_file_info[GTF_MAX] =
86 {FALSE, "c99.tags"},
87 {FALSE, "pascal.tags"},
88 {FALSE, "php.tags"},
89 {FALSE, "html_entities.tags"},
90 {FALSE, "latex.tags"},
91 {FALSE, "python.tags"}
94 static GPtrArray *top_level_iter_names = NULL;
96 static struct
98 GtkWidget *expand_all;
99 GtkWidget *collapse_all;
100 GtkWidget *sort_by_name;
101 GtkWidget *sort_by_appearance;
103 symbol_menu = {NULL, NULL, NULL, NULL};
106 static void html_tags_loaded(void);
107 static void load_user_tags(filetype_id ft_id);
109 /* get the tags_ignore list, exported by tagmanager's options.c */
110 extern gchar **c_tags_ignore;
112 /* ignore certain tokens when parsing C-like syntax.
113 * Also works for reloading. */
114 static void load_c_ignore_tags(void)
116 gchar *path = g_strconcat(app->configdir, G_DIR_SEPARATOR_S "ignore.tags", NULL);
117 gchar *content;
119 if (g_file_get_contents(path, &content, NULL, NULL))
121 /* historically we ignore the glib _DECLS for tag generation */
122 SETPTR(content, g_strconcat("G_BEGIN_DECLS G_END_DECLS\n", content, NULL));
124 g_strfreev(c_tags_ignore);
125 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
126 g_free(content);
128 g_free(path);
132 void symbols_reload_config_files(void)
134 load_c_ignore_tags();
138 static gsize get_tag_count(void)
140 GPtrArray *tags = tm_get_workspace()->global_tags;
141 gsize count = tags ? tags->len : 0;
143 return count;
147 /* wrapper for tm_workspace_load_global_tags().
148 * note that the tag count only counts new global tags added - if a tag has the same name,
149 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
150 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
152 gboolean result;
153 gsize old_tag_count = get_tag_count();
155 result = tm_workspace_load_global_tags(tags_file, ft->lang);
156 if (result)
158 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
159 (guint) (get_tag_count() - old_tag_count));
161 return result;
165 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
166 * This provides autocompletion, calltips, etc. */
167 void symbols_global_tags_loaded(guint file_type_idx)
169 TagFileInfo *tfi;
170 gint tag_type;
172 /* load ignore list for C/C++ parser */
173 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
174 c_tags_ignore == NULL)
176 load_c_ignore_tags();
179 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
180 return;
182 /* load config in case of custom filetypes */
183 filetypes_load_config(file_type_idx, FALSE);
185 load_user_tags(file_type_idx);
187 switch (file_type_idx)
189 case GEANY_FILETYPES_PHP:
190 case GEANY_FILETYPES_HTML:
191 html_tags_loaded();
193 switch (file_type_idx)
195 case GEANY_FILETYPES_CPP:
196 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
197 /* no C++ tagfile yet */
198 return;
199 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
200 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
201 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
202 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
203 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
204 default:
205 return;
207 tfi = &tag_file_info[tag_type];
209 if (! tfi->tags_loaded)
211 gchar *fname = g_strconcat(app->datadir, G_DIR_SEPARATOR_S, tfi->tag_file, NULL);
213 symbols_load_global_tags(fname, filetypes[file_type_idx]);
214 tfi->tags_loaded = TRUE;
215 g_free(fname);
220 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
221 static void html_tags_loaded(void)
223 TagFileInfo *tfi;
225 if (cl_options.ignore_global_tags)
226 return;
228 tfi = &tag_file_info[GTF_HTML_ENTITIES];
229 if (! tfi->tags_loaded)
231 gchar *file = g_strconcat(app->datadir, G_DIR_SEPARATOR_S, tfi->tag_file, NULL);
233 html_entities = utils_read_file_in_array(file);
234 tfi->tags_loaded = TRUE;
235 g_free(file);
240 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
242 guint j;
243 TMTag *tag;
244 GString *s = NULL;
245 GPtrArray *typedefs;
246 gint tag_lang;
248 g_return_val_if_fail(tags_array != NULL, NULL);
250 typedefs = tm_tags_extract(tags_array, tag_types);
252 if ((typedefs) && (typedefs->len > 0))
254 s = g_string_sized_new(typedefs->len * 10);
255 for (j = 0; j < typedefs->len; ++j)
257 tag = TM_TAG(typedefs->pdata[j]);
258 /* tag->atts.file.lang contains (for some reason) the line of the tag if
259 * tag->atts.entry.file is not NULL */
260 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
262 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
263 * e.g. PHP classes in C++ files
264 * lang = -2 disables the check */
265 if (tag->name && (tag_lang == lang || lang == -2))
267 if (j != 0)
268 g_string_append_c(s, ' ');
269 g_string_append(s, tag->name);
273 if (typedefs)
274 g_ptr_array_free(typedefs, TRUE);
275 return s;
279 /** Gets the context separator used by the tag manager for a particular file
280 * type.
281 * @param ft_id File type identifier.
282 * @return The context separator string.
284 * @since 0.19
286 const gchar *symbols_get_context_separator(gint ft_id)
288 switch (ft_id)
290 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
291 case GEANY_FILETYPES_CPP:
292 case GEANY_FILETYPES_GLSL: /* for structs */
293 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
294 return "::";
296 /* avoid confusion with other possible separators in group/section name */
297 case GEANY_FILETYPES_CONF:
298 case GEANY_FILETYPES_REST:
299 return ":::";
301 default:
302 return ".";
307 GString *symbols_get_macro_list(gint lang)
309 guint j, i;
310 GPtrArray *ftags;
311 GString *words;
312 gint tag_lang;
313 TMTag *tag;
315 if (app->tm_workspace->work_objects == NULL)
316 return NULL;
318 ftags = g_ptr_array_sized_new(50);
319 words = g_string_sized_new(200);
321 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
323 GPtrArray *tags;
325 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
326 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
327 if (NULL != tags)
329 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
331 tag = TM_TAG(tags->pdata[i]);
332 tag_lang = (tag->atts.entry.file) ?
333 tag->atts.entry.file->lang : tag->atts.file.lang;
335 if (tag_lang == lang)
336 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
338 g_ptr_array_free(tags, TRUE);
342 if (ftags->len == 0)
344 g_ptr_array_free(ftags, TRUE);
345 g_string_free(words, TRUE);
346 return NULL;
349 tm_tags_sort(ftags, NULL, FALSE);
350 for (j = 0; j < ftags->len; j++)
352 if (j > 0)
353 g_string_append_c(words, '\n');
354 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
356 g_ptr_array_free(ftags, TRUE);
357 return words;
361 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
362 static TMTag *
363 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
365 guint i;
366 g_return_val_if_fail(tags != NULL, NULL);
368 for (i = 0; i < tags->len; ++i)
370 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
371 return TM_TAG(tags->pdata[i]);
373 return NULL;
377 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
378 const gchar *tag_name, guint type)
380 GPtrArray *tags;
381 TMTag *tmtag;
383 if (G_LIKELY(workobj != NULL))
385 tags = tm_tags_extract(workobj->tags_array, type);
386 if (tags != NULL)
388 tmtag = symbols_find_tm_tag(tags, tag_name);
390 g_ptr_array_free(tags, TRUE);
392 if (tmtag != NULL)
393 return tmtag;
396 return NULL; /* not found */
400 static TMTag *find_workspace_tag(const gchar *tag_name, guint type)
402 guint j;
403 const GPtrArray *work_objects = NULL;
405 if (app->tm_workspace != NULL)
406 work_objects = app->tm_workspace->work_objects;
408 if (work_objects != NULL)
410 for (j = 0; j < work_objects->len; j++)
412 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
413 TMTag *tmtag;
415 tmtag = find_work_object_tag(workobj, tag_name, type);
416 if (tmtag != NULL)
417 return tmtag;
420 return NULL; /* not found */
424 const gchar **symbols_get_html_entities(void)
426 if (html_entities == NULL)
427 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
429 return (const gchar **) html_entities;
433 /* sort by name, then line */
434 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
436 gint ret;
438 if (tag_a == NULL || tag_b == NULL)
439 return 0;
441 if (tag_a->name == NULL)
442 return -(tag_a->name != tag_b->name);
444 if (tag_b->name == NULL)
445 return tag_a->name != tag_b->name;
447 ret = strcmp(tag_a->name, tag_b->name);
448 if (ret == 0)
450 return tag_a->atts.entry.line - tag_b->atts.entry.line;
452 return ret;
456 /* sort by line, then scope */
457 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
459 const TMTag *tag_a = TM_TAG(a);
460 const TMTag *tag_b = TM_TAG(b);
461 gint ret;
463 if (a == NULL || b == NULL)
464 return 0;
466 ret = tag_a->atts.entry.line - tag_b->atts.entry.line;
467 if (ret == 0)
469 if (tag_a->atts.entry.scope == NULL)
470 return -(tag_a->atts.entry.scope != tag_b->atts.entry.scope);
471 if (tag_b->atts.entry.scope == NULL)
472 return tag_a->atts.entry.scope != tag_b->atts.entry.scope;
473 else
474 return strcmp(tag_a->atts.entry.scope, tag_b->atts.entry.scope);
476 return ret;
480 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
482 GList *tag_names = NULL;
483 TMTag *tag;
484 guint i;
486 g_return_val_if_fail(doc, NULL);
488 if (! doc->tm_file || ! doc->tm_file->tags_array)
489 return NULL;
491 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
493 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
494 if (G_UNLIKELY(tag == NULL))
495 return NULL;
497 if (tag->type & tag_types)
499 tag_names = g_list_prepend(tag_names, tag);
502 tag_names = g_list_sort(tag_names, compare_symbol_lines);
503 return tag_names;
507 /* amount of types in the symbol list (currently max. 8 are used) */
508 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
510 struct TreeviewSymbols
512 GtkTreeIter tag_function;
513 GtkTreeIter tag_class;
514 GtkTreeIter tag_macro;
515 GtkTreeIter tag_member;
516 GtkTreeIter tag_variable;
517 GtkTreeIter tag_namespace;
518 GtkTreeIter tag_struct;
519 GtkTreeIter tag_interface;
520 GtkTreeIter tag_type;
521 GtkTreeIter tag_other;
522 } tv_iters;
525 static void init_tag_iters(void)
527 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
528 * filetypes(e.g. config file to Python crashes Geany without this) */
529 tv_iters.tag_function.stamp = -1;
530 tv_iters.tag_class.stamp = -1;
531 tv_iters.tag_member.stamp = -1;
532 tv_iters.tag_macro.stamp = -1;
533 tv_iters.tag_variable.stamp = -1;
534 tv_iters.tag_namespace.stamp = -1;
535 tv_iters.tag_struct.stamp = -1;
536 tv_iters.tag_interface.stamp = -1;
537 tv_iters.tag_type.stamp = -1;
538 tv_iters.tag_other.stamp = -1;
542 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
544 static GtkIconTheme *icon_theme = NULL;
545 static gint x, y;
547 if (G_UNLIKELY(icon_theme == NULL))
549 #ifndef G_OS_WIN32
550 gchar *path = g_strconcat(GEANY_DATADIR, "/icons", NULL);
551 #endif
552 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &y);
553 icon_theme = gtk_icon_theme_get_default();
554 #ifdef G_OS_WIN32
555 gtk_icon_theme_append_search_path(icon_theme, "share\\icons");
556 #else
557 gtk_icon_theme_append_search_path(icon_theme, path);
558 g_free(path);
559 #endif
561 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
565 /* finds the next iter at any level
566 * @param iter in/out, the current iter, will be changed to the next one
567 * @param down whether to try the child iter
568 * @return TRUE if there @p iter was set, or FALSE if there is no next iter */
569 static gboolean next_iter(GtkTreeModel *model, GtkTreeIter *iter, gboolean down)
571 GtkTreeIter guess;
572 GtkTreeIter copy = *iter;
574 /* go down if the item has children */
575 if (down && gtk_tree_model_iter_children(model, &guess, iter))
576 *iter = guess;
577 /* or to the next item at the same level */
578 else if (gtk_tree_model_iter_next(model, &copy))
579 *iter = copy;
580 /* or to the next item at a parent level */
581 else if (gtk_tree_model_iter_parent(model, &guess, iter))
583 copy = guess;
584 while(TRUE)
586 if (gtk_tree_model_iter_next(model, &copy))
588 *iter = copy;
589 return TRUE;
591 else if (gtk_tree_model_iter_parent(model, &copy, &guess))
592 guess = copy;
593 else
594 return FALSE;
597 else
598 return FALSE;
600 return TRUE;
604 static gboolean find_toplevel_iter(GtkTreeStore *store, GtkTreeIter *iter, const gchar *title)
606 GtkTreeModel *model = GTK_TREE_MODEL(store);
608 if (!gtk_tree_model_get_iter_first(model, iter))
609 return FALSE;
612 gchar *candidate;
614 gtk_tree_model_get(model, iter, SYMBOLS_COLUMN_NAME, &candidate, -1);
615 /* FIXME: what if 2 different items have the same name?
616 * this should never happen, but might be caused by a typo in a translation */
617 if (utils_str_equal(candidate, title))
619 g_free(candidate);
620 return TRUE;
622 else
623 g_free(candidate);
625 while (gtk_tree_model_iter_next(model, iter));
627 return FALSE;
631 /* Adds symbol list groups in (iter*, title) pairs.
632 * The list must be ended with NULL. */
633 static void G_GNUC_NULL_TERMINATED
634 tag_list_add_groups(GtkTreeStore *tree_store, ...)
636 va_list args;
637 GtkTreeIter *iter;
639 g_return_if_fail(top_level_iter_names);
641 va_start(args, tree_store);
642 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
644 gchar *title = va_arg(args, gchar*);
645 gchar *icon_name = va_arg(args, gchar *);
646 GdkPixbuf *icon = NULL;
648 if (icon_name)
650 icon = get_tag_icon(icon_name);
653 g_assert(title != NULL);
654 g_ptr_array_add(top_level_iter_names, title);
656 if (!find_toplevel_iter(tree_store, iter, title))
657 gtk_tree_store_append(tree_store, iter, NULL);
659 if (G_IS_OBJECT(icon))
661 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
662 g_object_unref(icon);
664 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
666 va_end(args);
670 static void add_top_level_items(GeanyDocument *doc)
672 filetype_id ft_id = doc->file_type->id;
673 GtkTreeStore *tag_store = doc->priv->tag_store;
675 if (top_level_iter_names == NULL)
676 top_level_iter_names = g_ptr_array_new();
677 else
678 g_ptr_array_set_size(top_level_iter_names, 0);
680 init_tag_iters();
682 switch (ft_id)
684 case GEANY_FILETYPES_DIFF:
686 tag_list_add_groups(tag_store,
687 &(tv_iters.tag_function), _("Files"), NULL, NULL);
688 break;
690 case GEANY_FILETYPES_DOCBOOK:
692 tag_list_add_groups(tag_store,
693 &(tv_iters.tag_function), _("Chapter"), NULL,
694 &(tv_iters.tag_class), _("Section"), NULL,
695 &(tv_iters.tag_member), _("Sect1"), NULL,
696 &(tv_iters.tag_macro), _("Sect2"), NULL,
697 &(tv_iters.tag_variable), _("Sect3"), NULL,
698 &(tv_iters.tag_struct), _("Appendix"), NULL,
699 &(tv_iters.tag_other), _("Other"), NULL,
700 NULL);
701 break;
703 case GEANY_FILETYPES_HASKELL:
704 tag_list_add_groups(tag_store,
705 &tv_iters.tag_namespace, _("Module"), NULL,
706 &tv_iters.tag_type, _("Types"), NULL,
707 &tv_iters.tag_macro, _("Type constructors"), NULL,
708 &tv_iters.tag_function, _("Functions"), "classviewer-method",
709 NULL);
710 break;
711 case GEANY_FILETYPES_COBOL:
712 tag_list_add_groups(tag_store,
713 &tv_iters.tag_class, _("Program"), "classviewer-class",
714 &tv_iters.tag_function, _("File"), "classviewer-method",
715 &tv_iters.tag_namespace, _("Sections"), "classviewer-namespace",
716 &tv_iters.tag_macro, _("Paragraph"), "classviewer-other",
717 &tv_iters.tag_struct, _("Group"), "classviewer-struct",
718 &tv_iters.tag_variable, _("Data"), "classviewer-var",
719 NULL);
720 break;
721 case GEANY_FILETYPES_CONF:
722 tag_list_add_groups(tag_store,
723 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
724 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
725 NULL);
726 break;
727 case GEANY_FILETYPES_NSIS:
728 tag_list_add_groups(tag_store,
729 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
730 &tv_iters.tag_function, _("Functions"), "classviewer-method",
731 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
732 NULL);
733 break;
734 case GEANY_FILETYPES_LATEX:
736 tag_list_add_groups(tag_store,
737 &(tv_iters.tag_function), _("Command"), NULL,
738 &(tv_iters.tag_class), _("Environment"), NULL,
739 &(tv_iters.tag_member), _("Section"), NULL,
740 &(tv_iters.tag_macro), _("Subsection"), NULL,
741 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
742 &(tv_iters.tag_struct), _("Label"), NULL,
743 &(tv_iters.tag_namespace), _("Chapter"), NULL,
744 &(tv_iters.tag_other), _("Other"), NULL,
745 NULL);
746 break;
748 case GEANY_FILETYPES_MATLAB:
750 tag_list_add_groups(tag_store,
751 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
752 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
753 NULL);
754 break;
756 case GEANY_FILETYPES_R:
758 tag_list_add_groups(tag_store,
759 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
760 &(tv_iters.tag_struct), _("Other"), NULL,
761 NULL);
762 break;
764 case GEANY_FILETYPES_PERL:
766 tag_list_add_groups(tag_store,
767 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
768 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
769 &(tv_iters.tag_macro), _("Labels"), NULL,
770 &(tv_iters.tag_type), _("Constants"), NULL,
771 &(tv_iters.tag_other), _("Other"), "classviewer-other",
772 NULL);
773 break;
775 case GEANY_FILETYPES_PHP:
777 tag_list_add_groups(tag_store,
778 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
779 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
780 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
781 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
782 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
783 NULL);
784 break;
786 case GEANY_FILETYPES_HTML:
788 tag_list_add_groups(tag_store,
789 &(tv_iters.tag_function), _("Functions"), NULL,
790 &(tv_iters.tag_member), _("Anchors"), NULL,
791 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
792 &(tv_iters.tag_class), _("H2 Headings"), NULL,
793 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
794 NULL);
795 break;
797 case GEANY_FILETYPES_CSS:
799 tag_list_add_groups(tag_store,
800 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
801 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
802 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
803 break;
805 case GEANY_FILETYPES_REST:
806 case GEANY_FILETYPES_TXT2TAGS:
807 case GEANY_FILETYPES_ABC:
809 tag_list_add_groups(tag_store,
810 &(tv_iters.tag_namespace), _("Chapter"), NULL,
811 &(tv_iters.tag_member), _("Section"), NULL,
812 &(tv_iters.tag_macro), _("Subsection"), NULL,
813 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
814 NULL);
815 break;
817 case GEANY_FILETYPES_RUBY:
819 tag_list_add_groups(tag_store,
820 &(tv_iters.tag_namespace), _("Modules"), NULL,
821 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
822 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
823 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
824 NULL);
825 break;
827 case GEANY_FILETYPES_TCL:
829 tag_list_add_groups(tag_store,
830 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
831 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
832 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
833 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
834 NULL);
835 break;
837 case GEANY_FILETYPES_PYTHON:
839 tag_list_add_groups(tag_store,
840 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
841 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
842 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
843 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
844 &(tv_iters.tag_namespace), _("Imports"), "classviewer-namespace",
845 NULL);
846 break;
848 case GEANY_FILETYPES_VHDL:
850 tag_list_add_groups(tag_store,
851 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
852 &(tv_iters.tag_class), _("Entities"), "classviewer-class",
853 &(tv_iters.tag_struct), _("Architectures"), "classviewer-struct",
854 &(tv_iters.tag_type), _("Types"), "classviewer-other",
855 &(tv_iters.tag_function), _("Functions / Procedures"), "classviewer-method",
856 &(tv_iters.tag_variable), _("Variables / Signals"), "classviewer-var",
857 &(tv_iters.tag_member), _("Processes / Components"), "classviewer-member",
858 &(tv_iters.tag_other), _("Other"), "classviewer-other",
859 NULL);
860 break;
862 case GEANY_FILETYPES_VERILOG:
864 tag_list_add_groups(tag_store,
865 &(tv_iters.tag_type), _("Events"), "classviewer-macro",
866 &(tv_iters.tag_class), _("Modules"), "classviewer-class",
867 &(tv_iters.tag_function), _("Functions / Tasks"), "classviewer-method",
868 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
869 &(tv_iters.tag_other), _("Other"), "classviewer-other",
870 NULL);
871 break;
873 case GEANY_FILETYPES_JAVA:
875 tag_list_add_groups(tag_store,
876 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
877 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
878 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
879 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
880 &(tv_iters.tag_member), _("Members"), "classviewer-member",
881 &(tv_iters.tag_other), _("Other"), "classviewer-other",
882 NULL);
883 break;
885 case GEANY_FILETYPES_AS:
887 tag_list_add_groups(tag_store,
888 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
889 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
890 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
891 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
892 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
893 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
894 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
895 &(tv_iters.tag_other), _("Other"), "classviewer-other",
896 NULL);
897 break;
899 case GEANY_FILETYPES_HAXE:
901 tag_list_add_groups(tag_store,
902 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
903 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
904 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
905 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
906 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
907 &(tv_iters.tag_other), _("Other"), "classviewer-other",
908 NULL);
909 break;
911 case GEANY_FILETYPES_BASIC:
913 tag_list_add_groups(tag_store,
914 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
915 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
916 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
917 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
918 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
919 &(tv_iters.tag_other), _("Other"), "classviewer-other",
920 NULL);
921 break;
923 case GEANY_FILETYPES_F77:
924 case GEANY_FILETYPES_FORTRAN:
926 tag_list_add_groups(tag_store,
927 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
928 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
929 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
930 &(tv_iters.tag_member), _("Subroutines"), "classviewer-method",
931 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
932 &(tv_iters.tag_type), _("Types"), "classviewer-namespace",
933 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
934 &(tv_iters.tag_other), _("Other"), "classviewer-other",
935 NULL);
936 break;
938 case GEANY_FILETYPES_ASM:
940 tag_list_add_groups(tag_store,
941 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
942 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
943 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
944 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
945 NULL);
946 break;
948 case GEANY_FILETYPES_MAKE:
949 tag_list_add_groups(tag_store,
950 &tv_iters.tag_function, _("Targets"), "classviewer-method",
951 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
952 NULL);
953 break;
954 case GEANY_FILETYPES_SQL:
956 tag_list_add_groups(tag_store,
957 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
958 &(tv_iters.tag_namespace), _("Procedures"), "classviewer-namespace",
959 &(tv_iters.tag_struct), _("Indexes"), "classviewer-struct",
960 &(tv_iters.tag_class), _("Tables"), "classviewer-class",
961 &(tv_iters.tag_macro), _("Triggers"), "classviewer-macro",
962 &(tv_iters.tag_member), _("Views"), "classviewer-var",
963 &(tv_iters.tag_other), _("Other"), "classviewer-other",
964 NULL);
965 break;
967 case GEANY_FILETYPES_D:
968 default:
970 if (ft_id == GEANY_FILETYPES_D)
971 tag_list_add_groups(tag_store,
972 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
973 else
974 tag_list_add_groups(tag_store,
975 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
977 tag_list_add_groups(tag_store,
978 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
979 &(tv_iters.tag_interface), _("Interfaces"), "classviewer-struct",
980 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
981 &(tv_iters.tag_member), _("Members"), "classviewer-member",
982 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
983 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
984 NULL);
986 if (ft_id != GEANY_FILETYPES_D)
988 tag_list_add_groups(tag_store,
989 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
991 tag_list_add_groups(tag_store,
992 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
993 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
999 /* removes toplevel items that have no children */
1000 static void hide_empty_rows(GtkTreeStore *store)
1002 GtkTreeIter iter;
1003 gboolean cont = TRUE;
1005 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
1006 return; /* stop when first iter is invalid, i.e. no elements */
1008 while (cont)
1010 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
1011 cont = gtk_tree_store_remove(store, &iter);
1012 else
1013 cont = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
1018 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
1020 gchar *utf8_name;
1021 const gchar *scope = tag->atts.entry.scope;
1022 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
1023 gboolean doc_is_utf8 = FALSE;
1025 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1026 * for None at this point completely */
1027 if (utils_str_equal(doc->encoding, "UTF-8") ||
1028 utils_str_equal(doc->encoding, "None"))
1029 doc_is_utf8 = TRUE;
1031 if (! doc_is_utf8)
1032 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
1033 -1, doc->encoding, TRUE);
1034 else
1035 utf8_name = tag->name;
1037 if (utf8_name == NULL)
1038 return NULL;
1040 if (! buffer)
1041 buffer = g_string_new(NULL);
1042 else
1043 g_string_truncate(buffer, 0);
1045 /* check first char of scope is a wordchar */
1046 if (!found_parent && scope &&
1047 strpbrk(scope, GEANY_WORDCHARS) == scope)
1049 const gchar *sep = symbols_get_context_separator(doc->file_type->id);
1051 g_string_append(buffer, scope);
1052 g_string_append(buffer, sep);
1054 g_string_append(buffer, utf8_name);
1056 if (! doc_is_utf8)
1057 g_free(utf8_name);
1059 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
1061 return buffer->str;
1065 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
1067 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
1069 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
1070 * for None at this point completely */
1071 if (utf8_name != NULL &&
1072 ! utils_str_equal(doc->encoding, "UTF-8") &&
1073 ! utils_str_equal(doc->encoding, "None"))
1075 SETPTR(utf8_name,
1076 encodings_convert_to_utf8_from_charset(utf8_name, -1, doc->encoding, TRUE));
1079 if (utf8_name != NULL)
1080 SETPTR(utf8_name, g_markup_escape_text(utf8_name, -1));
1082 return utf8_name;
1086 /* find the last word in "foo::bar::blah", e.g. "blah" */
1087 static const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
1089 const gchar *scope = tag->atts.entry.scope;
1090 const gchar *separator = symbols_get_context_separator(ft_id);
1091 const gchar *str, *ptr;
1093 if (!scope)
1094 return NULL;
1096 str = scope;
1098 while (1)
1100 ptr = strstr(str, separator);
1101 if (ptr)
1103 str = ptr + strlen(separator);
1105 else
1106 break;
1109 return NZV(str) ? str : NULL;
1113 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1115 GtkTreeIter *iter = NULL;
1117 switch (tag_type)
1119 case tm_tag_prototype_t:
1120 case tm_tag_method_t:
1121 case tm_tag_function_t:
1123 iter = &tv_iters.tag_function;
1124 break;
1126 case tm_tag_macro_t:
1127 case tm_tag_macro_with_arg_t:
1129 iter = &tv_iters.tag_macro;
1130 break;
1132 case tm_tag_class_t:
1134 iter = &tv_iters.tag_class;
1135 break;
1137 case tm_tag_member_t:
1138 case tm_tag_field_t:
1140 iter = &tv_iters.tag_member;
1141 break;
1143 case tm_tag_typedef_t:
1144 case tm_tag_enum_t:
1146 iter = &tv_iters.tag_type;
1147 break;
1149 case tm_tag_union_t:
1150 case tm_tag_struct_t:
1152 iter = &tv_iters.tag_struct;
1153 break;
1155 case tm_tag_interface_t:
1156 iter = &tv_iters.tag_interface;
1157 break;
1158 case tm_tag_variable_t:
1160 iter = &tv_iters.tag_variable;
1161 break;
1163 case tm_tag_namespace_t:
1164 case tm_tag_package_t:
1166 iter = &tv_iters.tag_namespace;
1167 break;
1169 default:
1171 iter = &tv_iters.tag_other;
1174 if (G_LIKELY(iter->stamp != -1))
1175 return iter;
1176 else
1177 return NULL;
1181 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1183 GdkPixbuf *icon = NULL;
1185 if (parent == &tv_iters.tag_other)
1187 return get_tag_icon("classviewer-var");
1189 /* copy parent icon */
1190 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1191 SYMBOLS_COLUMN_ICON, &icon, -1);
1192 return icon;
1196 static gboolean tag_equal(gconstpointer v1, gconstpointer v2)
1198 const TMTag *t1 = v1;
1199 const TMTag *t2 = v2;
1201 return (t1->type == t2->type && strcmp(t1->name, t2->name) == 0 &&
1202 utils_str_equal(t1->atts.entry.scope, t2->atts.entry.scope) &&
1203 /* include arglist in match to support e.g. C++ overloading */
1204 utils_str_equal(t1->atts.entry.arglist, t2->atts.entry.arglist));
1208 /* inspired from g_str_hash() */
1209 static guint tag_hash(gconstpointer v)
1211 const TMTag *tag = v;
1212 const gchar *p;
1213 guint32 h = 5381;
1215 h = (h << 5) + h + tag->type;
1216 for (p = tag->name; *p != '\0'; p++)
1217 h = (h << 5) + h + *p;
1218 if (tag->atts.entry.scope)
1220 for (p = tag->atts.entry.scope; *p != '\0'; p++)
1221 h = (h << 5) + h + *p;
1223 /* for e.g. C++ overloading */
1224 if (tag->atts.entry.arglist)
1226 for (p = tag->atts.entry.arglist; *p != '\0'; p++)
1227 h = (h << 5) + h + *p;
1230 return h;
1234 /* like gtk_tree_view_expand_to_path() but with an iter */
1235 static void tree_view_expand_to_iter(GtkTreeView *view, GtkTreeIter *iter)
1237 GtkTreeModel *model = gtk_tree_view_get_model(view);
1238 GtkTreePath *path = gtk_tree_model_get_path(model, iter);
1240 gtk_tree_view_expand_to_path(view, path);
1241 gtk_tree_path_free(path);
1245 /* like gtk_tree_store_remove() but finds the next iter at any level */
1246 static gboolean tree_store_remove_row(GtkTreeStore *store, GtkTreeIter *iter)
1248 GtkTreeIter parent;
1249 gboolean has_parent;
1250 gboolean cont;
1252 has_parent = gtk_tree_model_iter_parent(GTK_TREE_MODEL(store), &parent, iter);
1253 cont = gtk_tree_store_remove(store, iter);
1254 /* if there is no next at this level but there is a parent iter, continue from it */
1255 if (! cont && has_parent)
1257 *iter = parent;
1258 cont = next_iter(GTK_TREE_MODEL(store), iter, FALSE);
1261 return cont;
1265 /* adds a new element in the parent table if it's key is known.
1266 * duplicates are kept */
1267 static void update_parents_table(GHashTable *table, const TMTag *tag, const gchar *parent_name,
1268 const GtkTreeIter *iter)
1270 GList **list;
1271 if (g_hash_table_lookup_extended(table, tag->name, NULL, (gpointer *) &list) &&
1272 ! utils_str_equal(parent_name, tag->name) /* prevent Foo::Foo from making parent = child */)
1274 if (! list)
1276 list = g_slice_alloc(sizeof *list);
1277 *list = NULL;
1278 g_hash_table_insert(table, tag->name, list);
1280 *list = g_list_prepend(*list, g_slice_dup(GtkTreeIter, iter));
1285 static void free_iter_slice_list(gpointer data)
1287 GList **list = data;
1289 if (list)
1291 GList *node;
1292 foreach_list(node, *list)
1293 g_slice_free(GtkTreeIter, node->data);
1294 g_list_free(*list);
1295 g_slice_free1(sizeof *list, list);
1300 /* inserts a @data in @table on key @tag.
1301 * previous data is not overwritten if the key is duplicated, but rather the
1302 * two values are kept in a list
1304 * table is: GHashTable<TMTag, GList<GList<TMTag>>> */
1305 static void tags_table_insert(GHashTable *table, TMTag *tag, GList *data)
1307 GList *list = g_hash_table_lookup(table, tag);
1308 list = g_list_prepend(list, data);
1309 g_hash_table_insert(table, tag, list);
1313 /* looks up the entry in @table that better matches @tag.
1314 * if there are more than one candidate, the one that has closest line position to @tag is chosen */
1315 static GList *tags_table_lookup(GHashTable *table, TMTag *tag)
1317 GList *data = NULL;
1318 GList *node = g_hash_table_lookup(table, tag);
1319 if (node)
1321 glong delta;
1322 data = node->data;
1324 #define TAG_DELTA(a, b) ABS((glong) TM_TAG(a)->atts.entry.line - (glong) TM_TAG(b)->atts.entry.line)
1326 delta = TAG_DELTA(((GList *) node->data)->data, tag);
1327 for (node = node->next; node; node = node->next)
1329 glong d = TAG_DELTA(((GList *) node->data)->data, tag);
1331 if (d < delta)
1333 data = node->data;
1334 delta = d;
1338 #undef TAG_DELTA
1341 return data;
1345 /* removes the element at @tag from @table.
1346 * @tag must be the exact pointer used at insertion time */
1347 static void tags_table_remove(GHashTable *table, TMTag *tag)
1349 GList *list = g_hash_table_lookup(table, tag);
1350 if (list)
1352 GList *node;
1353 foreach_list(node, list)
1355 if (((GList *) node->data)->data == tag)
1356 break;
1358 list = g_list_delete_link(list, node);
1359 if (list)
1360 g_hash_table_insert(table, tag, list);
1361 else
1362 g_hash_table_remove(table, tag);
1368 * Updates the tag tree for a document with the tags in *list.
1369 * @param doc a document
1370 * @param tags a pointer to a GList* holding the tags to add/update. This
1371 * list may be updated, removing updated elements.
1373 * The update is done in two passes:
1374 * 1) walking the current tree, update tags that still exist and remove the
1375 * obsolescent ones;
1376 * 2) walking the remaining (non updated) tags, adds them in the list.
1378 * For better performances, we use 2 hash tables:
1379 * - one containing all the tags for lookup in the first pass (actually stores a
1380 * reference in the tags list for removing it efficiently), avoiding list search
1381 * on each tag;
1382 * - the other holding "tag-name":row references for tags having children, used to
1383 * lookup for a parent in both passes, avoiding tree traversal.
1385 static void update_tree_tags(GeanyDocument *doc, GList **tags)
1387 GtkTreeStore *store = doc->priv->tag_store;
1388 GtkTreeModel *model = GTK_TREE_MODEL(store);
1389 GHashTable *parents_table;
1390 GHashTable *tags_table;
1391 GtkTreeIter iter;
1392 gboolean cont;
1393 GList *item;
1395 /* Build hash tables holding tags and parents */
1396 /* parent table holds "tag-name":GtkTreeIter */
1397 parents_table = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, free_iter_slice_list);
1398 /* tags table is another representation of the @tags list, TMTag:GList<TMTag> */
1399 tags_table = g_hash_table_new_full(tag_hash, tag_equal, NULL, NULL);
1400 foreach_list(item, *tags)
1402 TMTag *tag = item->data;
1403 const gchar *name;
1405 tags_table_insert(tags_table, tag, item);
1407 name = get_parent_name(tag, doc->file_type->id);
1408 if (name)
1409 g_hash_table_insert(parents_table, (gpointer) name, NULL);
1412 /* First pass, update existing rows or delete them.
1413 * It is OK to delete them since we walk top down so we would remove
1414 * parents before checking for their children, thus never implicitly
1415 * deleting an updated child */
1416 cont = gtk_tree_model_get_iter_first(model, &iter);
1417 while (cont)
1419 TMTag *tag;
1421 gtk_tree_model_get(model, &iter, SYMBOLS_COLUMN_TAG, &tag, -1);
1422 if (! tag) /* most probably a toplevel, skip it */
1423 cont = next_iter(model, &iter, TRUE);
1424 else
1426 GList *found_item;
1428 found_item = tags_table_lookup(tags_table, tag);
1429 if (! found_item) /* tag doesn't exist, remove it */
1430 cont = tree_store_remove_row(store, &iter);
1431 else /* tag still exist, update it */
1433 const gchar *name;
1434 const gchar *parent_name;
1435 TMTag *found = found_item->data;
1437 parent_name = get_parent_name(found, doc->file_type->id);
1438 /* if parent is unknown, ignore it */
1439 if (parent_name && ! g_hash_table_lookup(parents_table, parent_name))
1440 parent_name = NULL;
1442 /* only update fields that (can) have changed (name that holds line
1443 * number, and the tag itself) */
1444 name = get_symbol_name(doc, found, parent_name != NULL);
1445 gtk_tree_store_set(store, &iter,
1446 SYMBOLS_COLUMN_NAME, name,
1447 SYMBOLS_COLUMN_TAG, found,
1448 -1);
1450 update_parents_table(parents_table, found, parent_name, &iter);
1452 /* remove the updated tag from the table and list */
1453 tags_table_remove(tags_table, found);
1454 *tags = g_list_delete_link(*tags, found_item);
1456 cont = next_iter(model, &iter, TRUE);
1459 tm_tag_unref(tag);
1463 /* Second pass, now we have a tree cleaned up from invalid rows,
1464 * we simply add new ones */
1465 foreach_list (item, *tags)
1467 TMTag *tag = item->data;
1468 GtkTreeIter *parent;
1470 parent = get_tag_type_iter(tag->type, doc->file_type->id);
1471 if (G_UNLIKELY(! parent))
1472 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1473 else
1475 gboolean expand;
1476 const gchar *name;
1477 const gchar *parent_name;
1478 gchar *tooltip;
1479 GdkPixbuf *icon = get_child_icon(store, parent);
1481 parent_name = get_parent_name(tag, doc->file_type->id);
1482 if (parent_name)
1484 GList **candidates;
1485 GtkTreeIter *parent_search = NULL;
1487 /* walk parent candidates to find the better one.
1488 * if there are more than one, take the one that has the closest line number
1489 * after the tag we're searching the parent for */
1490 candidates = g_hash_table_lookup(parents_table, parent_name);
1491 if (candidates)
1493 GList *node;
1494 glong delta = G_MAXLONG;
1495 foreach_list(node, *candidates)
1497 TMTag *parent_tag;
1498 glong d;
1500 gtk_tree_model_get(GTK_TREE_MODEL(store), node->data,
1501 SYMBOLS_COLUMN_TAG, &parent_tag, -1);
1503 d = tag->atts.entry.line - parent_tag->atts.entry.line;
1504 if (! parent_search || (d >= 0 && d < delta))
1506 delta = d;
1507 parent_search = node->data;
1512 if (parent_search)
1513 parent = parent_search;
1514 else
1515 parent_name = NULL;
1518 /* only expand to the iter if the parent was empty, otherwise we let the
1519 * folding as it was before (already expanded, or closed by the user) */
1520 expand = ! gtk_tree_model_iter_has_child(model, parent);
1522 /* insert the new element */
1523 gtk_tree_store_append(store, &iter, parent);
1524 name = get_symbol_name(doc, tag, parent_name != NULL);
1525 tooltip = get_symbol_tooltip(doc, tag);
1526 gtk_tree_store_set(store, &iter,
1527 SYMBOLS_COLUMN_NAME, name,
1528 SYMBOLS_COLUMN_TOOLTIP, tooltip,
1529 SYMBOLS_COLUMN_ICON, icon,
1530 SYMBOLS_COLUMN_TAG, tag,
1531 -1);
1532 g_free(tooltip);
1533 if (G_LIKELY(icon))
1534 g_object_unref(icon);
1536 update_parents_table(parents_table, tag, parent_name, &iter);
1538 if (expand)
1539 tree_view_expand_to_iter(GTK_TREE_VIEW(doc->priv->tag_tree), &iter);
1543 g_hash_table_destroy(parents_table);
1544 g_hash_table_destroy(tags_table);
1548 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1549 * is not stable, so the order is already lost. */
1550 static gint compare_top_level_names(const gchar *a, const gchar *b)
1552 guint i;
1553 const gchar *name;
1555 /* This should never happen as it would mean that two or more top
1556 * level items have the same name but it can happen by typos in the translations. */
1557 if (utils_str_equal(a, b))
1558 return 1;
1560 foreach_ptr_array(name, i, top_level_iter_names)
1562 if (utils_str_equal(name, a))
1563 return -1;
1564 if (utils_str_equal(name, b))
1565 return 1;
1567 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1568 return 0;
1572 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1573 GtkTreeIter *iter)
1575 /* if the tag has a parent tag, it should be at depth >= 2 */
1576 return NZV(tag->atts.entry.scope) &&
1577 gtk_tree_store_iter_depth(store, iter) == 1;
1581 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1582 gpointer user_data)
1584 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1585 TMTag *tag_a, *tag_b;
1586 gint cmp;
1588 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1589 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1591 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1592 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1593 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1594 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1596 cmp = sort_by_name ? compare_symbol(tag_a, tag_b) :
1597 compare_symbol_lines(tag_a, tag_b);
1599 else
1601 gchar *astr, *bstr;
1603 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1604 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1606 /* if a is toplevel, b must be also */
1607 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1609 cmp = compare_top_level_names(astr, bstr);
1611 else
1613 /* this is what g_strcmp0() does */
1614 if (! astr)
1615 cmp = -(astr != bstr);
1616 else if (! bstr)
1617 cmp = astr != bstr;
1618 else
1620 cmp = strcmp(astr, bstr);
1622 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1623 if (tag_a && tag_b)
1624 if (!sort_by_name ||
1625 (utils_str_equal(tag_a->name, tag_b->name) &&
1626 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1627 cmp = compare_symbol_lines(tag_a, tag_b);
1630 g_free(astr);
1631 g_free(bstr);
1633 tm_tag_unref(tag_a);
1634 tm_tag_unref(tag_b);
1636 return cmp;
1640 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1642 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1643 GINT_TO_POINTER(sort_by_name), NULL);
1645 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1649 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1651 GList *tags;
1653 g_return_val_if_fail(doc != NULL, FALSE);
1655 tags = get_tag_list(doc, tm_tag_max_t);
1656 if (tags == NULL)
1657 return FALSE;
1659 /* FIXME: Not sure why we detached the model here? */
1661 /* disable sorting during update because the code doesn't support correctly
1662 * models that are currently being built */
1663 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(doc->priv->tag_store), GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID, 0);
1665 /* add grandparent type iters */
1666 add_top_level_items(doc);
1668 update_tree_tags(doc, &tags);
1669 g_list_free(tags);
1671 hide_empty_rows(doc->priv->tag_store);
1673 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1674 sort_mode = doc->priv->symbol_list_sort_mode;
1676 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1677 doc->priv->symbol_list_sort_mode = sort_mode;
1679 return TRUE;
1683 /* Detects a global tags filetype from the *.lang.* language extension.
1684 * Returns NULL if there was no matching TM language. */
1685 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1687 gchar *tags_ext;
1688 gchar *shortname = utils_strdupa(utf8_filename);
1689 GeanyFiletype *ft = NULL;
1691 tags_ext = g_strrstr(shortname, ".tags");
1692 if (tags_ext)
1694 *tags_ext = '\0'; /* remove .tags extension */
1695 ft = filetypes_detect_from_extension(shortname);
1696 if (ft->id != GEANY_FILETYPES_NONE)
1697 return ft;
1699 return NULL;
1703 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1704 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1705 * the relevant path.
1706 * Example:
1707 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1708 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1710 /* -E pre-process, -dD output user macros, -p prof info (?) */
1711 const char pre_process[] = "gcc -E -dD -p -I.";
1713 if (argc > 2)
1715 /* Create global taglist */
1716 int status;
1717 char *command;
1718 const char *tags_file = argv[1];
1719 char *utf8_fname;
1720 GeanyFiletype *ft;
1722 utf8_fname = utils_get_utf8_from_locale(tags_file);
1723 ft = detect_global_tags_filetype(utf8_fname);
1724 g_free(utf8_fname);
1726 if (ft == NULL)
1728 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1729 return 1;
1731 /* load config in case of custom filetypes */
1732 filetypes_load_config(ft->id, FALSE);
1734 /* load ignore list for C/C++ parser */
1735 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1736 load_c_ignore_tags();
1738 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1739 command = g_strdup_printf("%s %s", pre_process, NVL(getenv("CFLAGS"), ""));
1740 else
1741 command = NULL; /* don't preprocess */
1743 geany_debug("Generating %s tags file.", ft->name);
1744 tm_get_workspace();
1745 status = tm_workspace_create_global_tags(command, (const char **) (argv + 2),
1746 argc - 2, tags_file, ft->lang);
1747 g_free(command);
1748 symbols_finalize(); /* free c_tags_ignore data */
1749 if (! status)
1751 g_printerr(_("Failed to create tags file, perhaps because no tags "
1752 "were found.\n"));
1753 return 1;
1756 else
1758 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1759 g_printerr(_("Example:\n"
1760 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1761 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1762 return 1;
1764 return 0;
1768 void symbols_show_load_tags_dialog(void)
1770 GtkWidget *dialog;
1771 GtkFileFilter *filter;
1773 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1774 GTK_FILE_CHOOSER_ACTION_OPEN,
1775 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1776 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1777 NULL);
1778 gtk_widget_set_name(dialog, "GeanyDialog");
1779 filter = gtk_file_filter_new();
1780 gtk_file_filter_set_name(filter, _("Geany tag files (*.*.tags)"));
1781 gtk_file_filter_add_pattern(filter, "*.*.tags");
1782 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1784 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1786 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1787 GSList *item;
1789 for (item = flist; item != NULL; item = g_slist_next(item))
1791 gchar *fname = item->data;
1792 gchar *utf8_fname;
1793 GeanyFiletype *ft;
1795 utf8_fname = utils_get_utf8_from_locale(fname);
1796 ft = detect_global_tags_filetype(utf8_fname);
1798 if (ft != NULL && symbols_load_global_tags(fname, ft))
1799 /* For translators: the first wildcard is the filetype, the second the filename */
1800 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."),
1801 filetypes_get_display_name(ft), utf8_fname);
1802 else
1803 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1805 g_free(utf8_fname);
1806 g_free(fname);
1808 g_slist_free(flist);
1810 gtk_widget_destroy(dialog);
1814 static void detect_tag_files(const GSList *file_list)
1816 const GSList *node;
1818 for (node = file_list; node != NULL; node = g_slist_next(node))
1820 gchar *fname = node->data;
1821 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1822 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1824 g_free(utf8_fname);
1826 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1827 ft->priv->tag_files = g_slist_prepend(ft->priv->tag_files, fname);
1828 else
1829 geany_debug("Unknown filetype for file '%s'.", fname);
1834 static void init_user_tags(void)
1836 GSList *file_list = NULL, *list = NULL;
1837 gchar *dir;
1839 dir = g_build_filename(app->configdir, "tags", NULL);
1840 /* create the user tags dir for next time if it doesn't exist */
1841 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1843 utils_mkdir(dir, FALSE);
1845 file_list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1847 SETPTR(dir, g_build_filename(app->datadir, "tags", NULL));
1848 list = utils_get_file_list_full(dir, TRUE, FALSE, NULL);
1849 g_free(dir);
1851 file_list = g_slist_concat(file_list, list);
1852 detect_tag_files(file_list);
1853 /* don't need to delete list contents because they are stored in ft->priv->tag_files */
1854 g_slist_free(file_list);
1858 static void load_user_tags(filetype_id ft_id)
1860 static guchar *tags_loaded = NULL;
1861 static gboolean init_tags = FALSE;
1862 const GSList *node;
1863 GeanyFiletype *ft = filetypes[ft_id];
1865 g_return_if_fail(ft_id > 0);
1867 if (!tags_loaded)
1868 tags_loaded = g_new0(guchar, filetypes_array->len);
1869 if (tags_loaded[ft_id])
1870 return;
1871 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1873 if (!init_tags)
1875 init_user_tags();
1876 init_tags = TRUE;
1879 for (node = ft->priv->tag_files; node != NULL; node = g_slist_next(node))
1881 const gchar *fname = node->data;
1883 symbols_load_global_tags(fname, ft);
1888 static gboolean goto_tag(const gchar *name, gboolean definition)
1890 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1891 guint type;
1892 TMTag *tmtag = NULL;
1893 GeanyDocument *old_doc = document_get_current();
1895 /* goto tag definition: all except prototypes / forward declarations / externs */
1896 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1898 /* first look in the current document */
1899 if (old_doc != NULL && old_doc->tm_file)
1900 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1902 /* if not found, look in the workspace */
1903 if (tmtag == NULL)
1904 tmtag = find_workspace_tag(name, type);
1906 if (tmtag != NULL)
1908 GeanyDocument *new_doc = document_find_by_real_path(
1909 tmtag->atts.entry.file->work_object.file_name);
1911 if (new_doc)
1913 /* If we are already on the tag line, swap definition/declaration */
1914 if (new_doc == old_doc &&
1915 tmtag->atts.entry.line == (guint)sci_get_current_line(old_doc->editor->sci) + 1)
1917 if (goto_tag(name, !definition))
1918 return TRUE;
1921 else
1923 /* not found in opened document, should open */
1924 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1927 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1928 return TRUE;
1930 return FALSE;
1934 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1936 if (goto_tag(name, definition))
1937 return TRUE;
1939 /* if we are here, there was no match and we are beeping ;-) */
1940 utils_beep();
1942 if (!definition)
1943 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1944 else
1945 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1946 return FALSE;
1950 /* This could perhaps be improved to check for #if, class etc. */
1951 static gint get_function_fold_number(GeanyDocument *doc)
1953 /* for Java the functions are always one fold level above the class scope */
1954 if (doc->file_type->id == GEANY_FILETYPES_JAVA)
1955 return SC_FOLDLEVELBASE + 1;
1956 else
1957 return SC_FOLDLEVELBASE;
1961 /* Should be used only with symbols_get_current_function. */
1962 static gboolean current_function_changed(GeanyDocument *doc, gint cur_line, gint fold_level)
1964 static gint old_line = -2;
1965 static GeanyDocument *old_doc = NULL;
1966 static gint old_fold_num = -1;
1967 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1968 gboolean ret;
1970 /* check if the cached line and file index have changed since last time: */
1971 if (doc == NULL || doc != old_doc)
1972 ret = TRUE;
1973 else
1974 if (cur_line == old_line)
1975 ret = FALSE;
1976 else
1978 /* if the line has only changed by 1 */
1979 if (abs(cur_line - old_line) == 1)
1981 const gint fn_fold =
1982 get_function_fold_number(doc);
1983 /* It's the same function if the fold number hasn't changed, or both the new
1984 * and old fold numbers are above the function fold number. */
1985 gboolean same =
1986 fold_num == old_fold_num ||
1987 (old_fold_num > fn_fold && fold_num > fn_fold);
1989 ret = ! same;
1991 else ret = TRUE;
1994 /* record current line and file index for next time */
1995 old_line = cur_line;
1996 old_doc = doc;
1997 old_fold_num = fold_num;
1998 return ret;
2002 /* Parse the function name up to 2 lines before tag_line.
2003 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
2004 * type or argument names can be confused with the function name. */
2005 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
2007 gint start, end, max_pos;
2008 gchar *cur_tag;
2009 gint fn_style;
2011 switch (sci_get_lexer(sci))
2013 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
2014 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
2015 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
2017 start = sci_get_position_from_line(sci, tag_line - 2);
2018 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2019 while (sci_get_style_at(sci, start) != fn_style
2020 && start < max_pos) start++;
2022 end = start;
2023 while (sci_get_style_at(sci, end) == fn_style
2024 && end < max_pos) end++;
2026 if (start == end)
2027 return NULL;
2028 cur_tag = g_malloc(end - start + 1);
2029 sci_get_text_range(sci, start, end, cur_tag);
2030 return cur_tag;
2034 /* Parse the function name */
2035 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
2037 gint start, end, first_pos, max_pos;
2038 gint tmp;
2039 gchar c;
2040 gchar *cur_tag;
2042 first_pos = end = sci_get_position_from_line(sci, tag_line);
2043 max_pos = sci_get_position_from_line(sci, tag_line + 1);
2044 tmp = 0;
2045 /* goto the begin of function body */
2046 while (end < max_pos &&
2047 (tmp = sci_get_char_at(sci, end)) != '{' &&
2048 tmp != 0) end++;
2049 if (tmp == 0) end --;
2051 /* go back to the end of function identifier */
2052 while (end > 0 && end > first_pos - 500 &&
2053 (tmp = sci_get_char_at(sci, end)) != '(' &&
2054 tmp != 0) end--;
2055 end--;
2056 if (end < 0) end = 0;
2058 /* skip whitespaces between identifier and ( */
2059 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
2061 start = end;
2062 c = 0;
2063 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
2064 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
2065 || tmp == SCE_C_GLOBALCLASS
2066 || (c = sci_get_char_at(sci, start)) == '~'
2067 || c == ':'))
2068 start--;
2069 if (start != 0 && start < end) start++; /* correct for last non-matching char */
2071 if (start == end) return NULL;
2072 cur_tag = g_malloc(end - start + 2);
2073 sci_get_text_range(sci, start, end + 1, cur_tag);
2074 return cur_tag;
2078 /* Sets *tagname to point at the current function or tag name.
2079 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
2080 * call to this function.
2081 * Returns: line number of the current tag, or -1 if unknown. */
2082 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
2084 static gint tag_line = -1;
2085 static gchar *cur_tag = NULL;
2086 gint line;
2087 gint fold_level;
2088 TMWorkObject *tm_file;
2090 if (doc == NULL) /* reset current function */
2092 current_function_changed(NULL, -1, -1);
2093 g_free(cur_tag);
2094 cur_tag = g_strdup(_("unknown"));
2095 if (tagname != NULL)
2096 *tagname = cur_tag;
2097 tag_line = -1;
2098 return tag_line;
2101 line = sci_get_current_line(doc->editor->sci);
2102 fold_level = sci_get_fold_level(doc->editor->sci, line);
2103 /* check if the cached line and file index have changed since last time: */
2104 if (! current_function_changed(doc, line, fold_level))
2106 /* we can assume same current function as before */
2107 *tagname = cur_tag;
2108 return tag_line;
2110 g_free(cur_tag); /* free the old tag, it will be replaced. */
2112 /* if line is at base fold level, we're not in a function */
2113 if ((fold_level & SC_FOLDLEVELNUMBERMASK) == SC_FOLDLEVELBASE)
2115 cur_tag = g_strdup(_("unknown"));
2116 *tagname = cur_tag;
2117 tag_line = -1;
2118 return tag_line;
2120 tm_file = doc->tm_file;
2122 /* if the document has no changes, get the previous function name from TM */
2123 if (! doc->changed && tm_file != NULL && tm_file->tags_array != NULL)
2125 const TMTag *tag = (const TMTag*) tm_get_current_function(tm_file->tags_array, line);
2127 if (tag != NULL)
2129 gchar *tmp;
2130 tmp = tag->atts.entry.scope;
2131 cur_tag = tmp ? g_strconcat(tmp, "::", tag->name, NULL) : g_strdup(tag->name);
2132 *tagname = cur_tag;
2133 tag_line = tag->atts.entry.line;
2134 return tag_line;
2138 /* parse the current function name here because TM line numbers may have changed,
2139 * and it would take too long to reparse the whole file. */
2140 if (doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
2142 const gint fn_fold = get_function_fold_number(doc);
2144 tag_line = line;
2145 do /* find the top level fold point */
2147 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
2148 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
2149 } while (tag_line >= 0 &&
2150 (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold);
2152 if (tag_line >= 0)
2154 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
2155 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
2156 else
2157 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
2159 if (cur_tag != NULL)
2161 *tagname = cur_tag;
2162 return tag_line;
2167 cur_tag = g_strdup(_("unknown"));
2168 *tagname = cur_tag;
2169 tag_line = -1;
2170 return tag_line;
2174 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
2176 gint sort_mode = GPOINTER_TO_INT(user_data);
2177 GeanyDocument *doc = document_get_current();
2179 if (ignore_callback)
2180 return;
2182 if (doc != NULL)
2183 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
2187 static void on_symbol_tree_menu_show(GtkWidget *widget,
2188 gpointer user_data)
2190 GeanyDocument *doc = document_get_current();
2191 gboolean enable;
2193 enable = doc && doc->has_tags;
2194 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
2195 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
2196 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
2197 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
2199 if (! doc)
2200 return;
2202 ignore_callback = TRUE;
2204 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
2205 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
2206 else
2207 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
2209 ignore_callback = FALSE;
2213 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
2215 gboolean expand = GPOINTER_TO_INT(user_data);
2216 GeanyDocument *doc = document_get_current();
2218 if (! doc)
2219 return;
2221 g_return_if_fail(doc->priv->tag_tree);
2223 if (expand)
2224 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2225 else
2226 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
2230 static void create_taglist_popup_menu(void)
2232 GtkWidget *item, *menu;
2234 tv.popup_taglist = menu = gtk_menu_new();
2236 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
2237 gtk_widget_show(item);
2238 gtk_container_add(GTK_CONTAINER(menu), item);
2239 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(TRUE));
2241 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
2242 gtk_widget_show(item);
2243 gtk_container_add(GTK_CONTAINER(menu), item);
2244 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GINT_TO_POINTER(FALSE));
2246 item = gtk_separator_menu_item_new();
2247 gtk_widget_show(item);
2248 gtk_container_add(GTK_CONTAINER(menu), item);
2250 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
2251 _("Sort by _Name"));
2252 gtk_widget_show(item);
2253 gtk_container_add(GTK_CONTAINER(menu), item);
2254 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2255 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
2257 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
2258 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
2259 gtk_widget_show(item);
2260 gtk_container_add(GTK_CONTAINER(menu), item);
2261 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
2262 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
2264 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
2266 sidebar_add_common_menu_items(GTK_MENU(menu));
2270 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
2272 gchar *f = g_build_filename(app->configdir, "ignore.tags", NULL);
2274 g_return_if_fail(NZV(doc->real_path));
2276 if (utils_str_equal(doc->real_path, f))
2277 load_c_ignore_tags();
2279 g_free(f);
2283 void symbols_init(void)
2285 gchar *f;
2287 create_taglist_popup_menu();
2289 f = g_build_filename(app->configdir, "ignore.tags", NULL);
2290 ui_add_config_file_menu_item(f, NULL, NULL);
2291 g_free(f);
2293 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
2297 void symbols_finalize(void)
2299 g_strfreev(html_entities);
2300 g_strfreev(c_tags_ignore);