Try to fix Gtk warning when using Tools->Reload Configuration.
[geany-mirror.git] / src / symbols.c
blob0685ae542f21a5e12ac0ce3d22afe728e91dde62
1 /*
2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2009 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.
21 * $Id$
25 * Symbol Tree and TagManager-related convenience functions.
26 * TagManager parses tags for each document, and also adds them to the workspace (session).
27 * Global tags are lists of tags for each filetype, loaded when a document with a
28 * matching filetype is first loaded.
31 #include "SciLexer.h"
32 #include "geany.h"
34 #include <ctype.h>
35 #include <string.h>
36 #include <stdlib.h>
38 #include "prefix.h"
39 #include "symbols.h"
40 #include "utils.h"
41 #include "filetypes.h"
42 #include "encodings.h"
43 #include "document.h"
44 #include "documentprivate.h"
45 #include "support.h"
46 #include "msgwindow.h"
47 #include "sidebar.h"
48 #include "main.h"
49 #include "navqueue.h"
50 #include "ui_utils.h"
51 #include "editor.h"
52 #include "sciwrappers.h"
55 const guint TM_GLOBAL_TYPE_MASK =
56 tm_tag_class_t | tm_tag_enum_t | tm_tag_interface_t |
57 tm_tag_struct_t | tm_tag_typedef_t | tm_tag_union_t | tm_tag_namespace_t;
60 static gchar **html_entities = NULL;
62 typedef struct
64 gboolean tags_loaded;
65 const gchar *tag_file;
66 } TagFileInfo;
68 /* Check before adding any more tags files, usually they should be downloaded separately. */
69 enum /* Geany tag files */
71 GTF_C,
72 GTF_PASCAL,
73 GTF_PHP,
74 GTF_HTML_ENTITIES,
75 GTF_LATEX,
76 GTF_PYTHON,
77 GTF_MAX
80 static TagFileInfo tag_file_info[GTF_MAX] =
82 {FALSE, "c99.tags"},
83 {FALSE, "pascal.tags"},
84 {FALSE, "php.tags"},
85 {FALSE, "html_entities.tags"},
86 {FALSE, "latex.tags"},
87 {FALSE, "python.tags"}
90 static GPtrArray *top_level_iter_names = NULL;
92 static struct
94 GtkWidget *expand_all;
95 GtkWidget *collapse_all;
96 GtkWidget *sort_by_name;
97 GtkWidget *sort_by_appearance;
99 symbol_menu = {NULL, NULL, NULL, NULL};
102 static void html_tags_loaded(void);
103 static void load_user_tags(filetype_id ft_id);
105 /* get the tags_ignore list, exported by tagmanager's options.c */
106 extern gchar **c_tags_ignore;
108 /* ignore certain tokens when parsing C-like syntax.
109 * Also works for reloading. */
110 static void load_c_ignore_tags(void)
112 gchar *path = g_strconcat(app->configdir, G_DIR_SEPARATOR_S "ignore.tags", NULL);
113 gchar *content;
115 if (g_file_get_contents(path, &content, NULL, NULL))
117 g_strfreev(c_tags_ignore);
118 c_tags_ignore = g_strsplit_set(content, " \n\r", -1);
119 g_free(content);
121 g_free(path);
125 void symbols_reload_config_files(void)
127 load_c_ignore_tags();
131 static gsize get_tag_count()
133 GPtrArray *tags = tm_get_workspace()->global_tags;
134 gsize count = tags ? tags->len : 0;
136 return count;
140 /* wrapper for tm_workspace_load_global_tags().
141 * note that the tag count only counts new global tags added - if a tag has the same name,
142 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
143 static gboolean symbols_load_global_tags(const gchar *tags_file, GeanyFiletype *ft)
145 gboolean result;
146 gsize old_tag_count = get_tag_count();
148 result = tm_workspace_load_global_tags(tags_file, ft->lang);
149 if (result)
151 geany_debug("Loaded %s (%s), %u tag(s).", tags_file, ft->name,
152 (guint) (get_tag_count() - old_tag_count));
154 return result;
158 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
159 * This provides autocompletion, calltips, etc. */
160 void symbols_global_tags_loaded(gint file_type_idx)
162 TagFileInfo *tfi;
163 gint tag_type;
165 /* load ignore list for C/C++ parser */
166 if ((file_type_idx == GEANY_FILETYPES_C || file_type_idx == GEANY_FILETYPES_CPP) &&
167 c_tags_ignore == NULL)
169 load_c_ignore_tags();
172 if (cl_options.ignore_global_tags || app->tm_workspace == NULL)
173 return;
175 /* load config in case of custom filetypes */
176 filetypes_load_config(file_type_idx, FALSE);
178 load_user_tags(file_type_idx);
180 switch (file_type_idx)
182 case GEANY_FILETYPES_PHP:
183 case GEANY_FILETYPES_HTML:
184 html_tags_loaded();
186 switch (file_type_idx)
188 case GEANY_FILETYPES_CPP:
189 symbols_global_tags_loaded(GEANY_FILETYPES_C); /* load C global tags */
190 /* no C++ tagfile yet */
191 return;
192 case GEANY_FILETYPES_C: tag_type = GTF_C; break;
193 case GEANY_FILETYPES_PASCAL:tag_type = GTF_PASCAL; break;
194 case GEANY_FILETYPES_PHP: tag_type = GTF_PHP; break;
195 case GEANY_FILETYPES_LATEX: tag_type = GTF_LATEX; break;
196 case GEANY_FILETYPES_PYTHON:tag_type = GTF_PYTHON; break;
197 default:
198 return;
200 tfi = &tag_file_info[tag_type];
202 if (! tfi->tags_loaded)
204 gchar *fname = g_strconcat(app->datadir, G_DIR_SEPARATOR_S, tfi->tag_file, NULL);
206 symbols_load_global_tags(fname, filetypes[file_type_idx]);
207 tfi->tags_loaded = TRUE;
208 g_free(fname);
213 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&amp;') */
214 static void html_tags_loaded(void)
216 TagFileInfo *tfi;
218 if (cl_options.ignore_global_tags)
219 return;
221 tfi = &tag_file_info[GTF_HTML_ENTITIES];
222 if (! tfi->tags_loaded)
224 gchar *file = g_strconcat(app->datadir, G_DIR_SEPARATOR_S, tfi->tag_file, NULL);
226 html_entities = utils_read_file_in_array(file);
227 tfi->tags_loaded = TRUE;
228 g_free(file);
233 GString *symbols_find_tags_as_string(GPtrArray *tags_array, guint tag_types, gint lang)
235 guint j;
236 TMTag *tag;
237 GString *s = NULL;
238 GPtrArray *typedefs;
239 gint tag_lang;
241 g_return_val_if_fail(tags_array != NULL, NULL);
243 typedefs = tm_tags_extract(tags_array, tag_types);
245 if ((typedefs) && (typedefs->len > 0))
247 s = g_string_sized_new(typedefs->len * 10);
248 for (j = 0; j < typedefs->len; ++j)
250 tag = TM_TAG(typedefs->pdata[j]);
251 /* tag->atts.file.lang contains (for some reason) the line of the tag if
252 * tag->atts.entry.file is not NULL */
253 tag_lang = (tag->atts.entry.file) ? tag->atts.entry.file->lang : tag->atts.file.lang;
255 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
256 * e.g. PHP classes in C++ files
257 * lang = -2 disables the check */
258 if (tag->name && (tag_lang == lang || lang == -2))
260 if (j != 0)
261 g_string_append_c(s, ' ');
262 g_string_append(s, tag->name);
266 if (typedefs)
267 g_ptr_array_free(typedefs, TRUE);
268 return s;
272 const gchar *symbols_get_context_separator(gint ft_id)
274 switch (ft_id)
276 case GEANY_FILETYPES_C: /* for C++ .h headers or C structs */
277 case GEANY_FILETYPES_CPP:
278 case GEANY_FILETYPES_GLSL: /* for structs */
279 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
280 return "::";
282 /* avoid confusion with other possible separators in group/section name */
283 case GEANY_FILETYPES_CONF:
284 case GEANY_FILETYPES_REST:
285 return ":::";
287 default:
288 return ".";
293 GString *symbols_get_macro_list(gint lang)
295 guint j, i;
296 GPtrArray *ftags;
297 GString *words;
298 gint tag_lang;
299 TMTag *tag;
301 if (app->tm_workspace->work_objects == NULL)
302 return NULL;
304 ftags = g_ptr_array_sized_new(50);
305 words = g_string_sized_new(200);
307 for (j = 0; j < app->tm_workspace->work_objects->len; j++)
309 GPtrArray *tags;
311 tags = tm_tags_extract(TM_WORK_OBJECT(app->tm_workspace->work_objects->pdata[j])->tags_array,
312 tm_tag_enum_t | tm_tag_variable_t | tm_tag_macro_t | tm_tag_macro_with_arg_t);
313 if (NULL != tags)
315 for (i = 0; ((i < tags->len) && (i < editor_prefs.autocompletion_max_entries)); ++i)
317 tag = TM_TAG(tags->pdata[i]);
318 tag_lang = (tag->atts.entry.file) ?
319 tag->atts.entry.file->lang : tag->atts.file.lang;
321 if (tag_lang == lang)
322 g_ptr_array_add(ftags, (gpointer) tags->pdata[i]);
324 g_ptr_array_free(tags, TRUE);
328 if (ftags->len == 0)
330 g_ptr_array_free(ftags, TRUE);
331 g_string_free(words, TRUE);
332 return NULL;
335 tm_tags_sort(ftags, NULL, FALSE);
336 for (j = 0; j < ftags->len; j++)
338 if (j > 0)
339 g_string_append_c(words, '\n');
340 g_string_append(words, TM_TAG(ftags->pdata[j])->name);
342 g_ptr_array_free(ftags, TRUE);
343 return words;
347 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
348 static TMTag *
349 symbols_find_tm_tag(const GPtrArray *tags, const gchar *tag_name)
351 guint i;
352 g_return_val_if_fail(tags != NULL, NULL);
354 for (i = 0; i < tags->len; ++i)
356 if (utils_str_equal(TM_TAG(tags->pdata[i])->name, tag_name))
357 return TM_TAG(tags->pdata[i]);
359 return NULL;
363 static TMTag *find_work_object_tag(const TMWorkObject *workobj,
364 const gchar *tag_name, gint type)
366 GPtrArray *tags;
367 TMTag *tmtag;
369 if (G_LIKELY(workobj != NULL))
371 tags = tm_tags_extract(workobj->tags_array, type);
372 if (tags != NULL)
374 tmtag = symbols_find_tm_tag(tags, tag_name);
376 g_ptr_array_free(tags, TRUE);
378 if (tmtag != NULL)
379 return tmtag;
382 return NULL; /* not found */
386 static TMTag *find_workspace_tag(const gchar *tag_name, gint type)
388 guint j;
389 const GPtrArray *work_objects = NULL;
391 if (app->tm_workspace != NULL)
392 work_objects = app->tm_workspace->work_objects;
394 if (work_objects != NULL)
396 for (j = 0; j < work_objects->len; j++)
398 TMWorkObject *workobj = TM_WORK_OBJECT(work_objects->pdata[j]);
399 TMTag *tmtag;
401 tmtag = find_work_object_tag(workobj, tag_name, type);
402 if (tmtag != NULL)
403 return tmtag;
406 return NULL; /* not found */
410 const gchar **symbols_get_html_entities(void)
412 if (html_entities == NULL)
413 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
415 return (const gchar **) html_entities;
419 /* sort by name, then line */
420 static gint compare_symbol(const TMTag *tag_a, const TMTag *tag_b)
422 gint ret;
424 if (tag_a == NULL || tag_b == NULL)
425 return 0;
427 if (tag_a->name == NULL)
428 return -(tag_a->name != tag_b->name);
430 if (tag_b->name == NULL)
431 return tag_a->name != tag_b->name;
433 ret = strcmp(tag_a->name, tag_b->name);
434 if (ret == 0)
436 return tag_a->atts.entry.line - tag_b->atts.entry.line;
438 return ret;
442 /* sort by line only */
443 static gint compare_symbol_lines(gconstpointer a, gconstpointer b)
445 const TMTag *tag_a = TM_TAG(a);
446 const TMTag *tag_b = TM_TAG(b);
448 if (a == NULL || b == NULL)
449 return 0;
451 return tag_a->atts.entry.line - tag_b->atts.entry.line;
455 static GList *get_tag_list(GeanyDocument *doc, guint tag_types)
457 GList *tag_names = NULL;
458 TMTag *tag;
459 guint i;
461 g_return_val_if_fail(doc, NULL);
463 if (! doc->tm_file || ! doc->tm_file->tags_array)
464 return NULL;
466 for (i = 0; i < doc->tm_file->tags_array->len; ++i)
468 tag = TM_TAG(doc->tm_file->tags_array->pdata[i]);
469 if (G_UNLIKELY(tag == NULL))
470 return NULL;
472 if (tag->type & tag_types)
474 tag_names = g_list_append(tag_names, tag);
477 tag_names = g_list_sort(tag_names, compare_symbol_lines);
478 return tag_names;
482 /* amount of types in the symbol list (currently max. 8 are used) */
483 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
485 struct TreeviewSymbols
487 GtkTreeIter tag_function;
488 GtkTreeIter tag_class;
489 GtkTreeIter tag_macro;
490 GtkTreeIter tag_member;
491 GtkTreeIter tag_variable;
492 GtkTreeIter tag_namespace;
493 GtkTreeIter tag_struct;
494 GtkTreeIter tag_type;
495 GtkTreeIter tag_other;
496 } tv_iters;
499 static void init_tag_iters(void)
501 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
502 * filetypes(e.g. config file to Python crashes Geany without this) */
503 tv_iters.tag_function.stamp = -1;
504 tv_iters.tag_class.stamp = -1;
505 tv_iters.tag_member.stamp = -1;
506 tv_iters.tag_macro.stamp = -1;
507 tv_iters.tag_variable.stamp = -1;
508 tv_iters.tag_namespace.stamp = -1;
509 tv_iters.tag_struct.stamp = -1;
510 tv_iters.tag_type.stamp = -1;
511 tv_iters.tag_other.stamp = -1;
515 static GdkPixbuf *get_tag_icon(const gchar *icon_name)
517 static GtkIconTheme *icon_theme = NULL;
518 static gint x, y;
520 if (G_UNLIKELY(icon_theme == NULL))
522 #ifndef G_OS_WIN32
523 gchar *path = g_strconcat(GEANY_DATADIR, "/icons", NULL);
524 #endif
525 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &x, &y);
526 icon_theme = gtk_icon_theme_get_default();
527 #ifdef G_OS_WIN32
528 gtk_icon_theme_append_search_path(icon_theme, "share\\icons");
529 #else
530 gtk_icon_theme_append_search_path(icon_theme, path);
531 g_free(path);
532 #endif
534 return gtk_icon_theme_load_icon(icon_theme, icon_name, x, 0, NULL);
538 /* Adds symbol list groups in (iter*, title) pairs.
539 * The list must be ended with NULL. */
540 static void G_GNUC_NULL_TERMINATED
541 tag_list_add_groups(GtkTreeStore *tree_store, ...)
543 va_list args;
544 GtkTreeIter *iter;
546 g_return_if_fail(top_level_iter_names);
548 va_start(args, tree_store);
549 for (; iter = va_arg(args, GtkTreeIter*), iter != NULL;)
551 gchar *title = va_arg(args, gchar*);
552 gchar *icon_name = va_arg(args, gchar *);
553 GdkPixbuf *icon = NULL;
555 if (icon_name)
557 icon = get_tag_icon(icon_name);
560 g_assert(title != NULL);
561 g_ptr_array_add(top_level_iter_names, title);
563 gtk_tree_store_append(tree_store, iter, NULL);
565 if (G_IS_OBJECT(icon))
567 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_ICON, icon, -1);
568 g_object_unref(icon);
570 gtk_tree_store_set(tree_store, iter, SYMBOLS_COLUMN_NAME, title, -1);
572 va_end(args);
576 static void add_top_level_items(GeanyDocument *doc)
578 filetype_id ft_id = doc->file_type->id;
579 GtkTreeStore *tag_store = doc->priv->tag_store;
581 if (top_level_iter_names == NULL)
582 top_level_iter_names = g_ptr_array_new();
583 else
584 g_ptr_array_set_size(top_level_iter_names, 0);
586 init_tag_iters();
588 switch (ft_id)
590 case GEANY_FILETYPES_DIFF:
592 tag_list_add_groups(tag_store,
593 &(tv_iters.tag_function), _("Files"), NULL, NULL);
594 break;
596 case GEANY_FILETYPES_DOCBOOK:
598 tag_list_add_groups(tag_store,
599 &(tv_iters.tag_function), _("Chapter"), NULL,
600 &(tv_iters.tag_class), _("Section"), NULL,
601 &(tv_iters.tag_member), _("Sect1"), NULL,
602 &(tv_iters.tag_macro), _("Sect2"), NULL,
603 &(tv_iters.tag_variable), _("Sect3"), NULL,
604 &(tv_iters.tag_struct), _("Appendix"), NULL,
605 &(tv_iters.tag_other), _("Other"), NULL,
606 NULL);
607 /* &(tv_iters.tag_namespace), _("Other"), NULL, NULL); */
608 break;
610 case GEANY_FILETYPES_HASKELL:
611 tag_list_add_groups(tag_store,
612 &tv_iters.tag_namespace, _("Module"), NULL,
613 &tv_iters.tag_type, _("Types"), NULL,
614 &tv_iters.tag_macro, _("Type constructors"), NULL,
615 &tv_iters.tag_function, _("Functions"), "classviewer-method",
616 NULL);
617 break;
618 case GEANY_FILETYPES_CONF:
619 tag_list_add_groups(tag_store,
620 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
621 &tv_iters.tag_macro, _("Keys"), "classviewer-var",
622 NULL);
623 break;
624 case GEANY_FILETYPES_NSIS:
625 tag_list_add_groups(tag_store,
626 &tv_iters.tag_namespace, _("Sections"), "classviewer-other",
627 &tv_iters.tag_function, _("Functions"), "classviewer-method",
628 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
629 NULL);
630 break;
631 case GEANY_FILETYPES_LATEX:
633 tag_list_add_groups(tag_store,
634 &(tv_iters.tag_function), _("Command"), NULL,
635 &(tv_iters.tag_class), _("Environment"), NULL,
636 &(tv_iters.tag_member), _("Section"), NULL,
637 &(tv_iters.tag_macro), _("Subsection"), NULL,
638 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
639 &(tv_iters.tag_struct), _("Label"), NULL,
640 &(tv_iters.tag_namespace), _("Chapter"), NULL,
641 &(tv_iters.tag_other), _("Other"), NULL,
642 NULL);
643 break;
645 case GEANY_FILETYPES_MATLAB:
647 tag_list_add_groups(tag_store,
648 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
649 &(tv_iters.tag_struct), _("Structures"), "classviewer-struct",
650 NULL);
651 break;
653 case GEANY_FILETYPES_PERL:
655 tag_list_add_groups(tag_store,
656 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
657 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
658 &(tv_iters.tag_macro), _("Labels"), NULL,
659 &(tv_iters.tag_type), _("Constants"), NULL,
660 &(tv_iters.tag_other), _("Other"), "classviewer-other",
661 NULL);
662 break;
664 case GEANY_FILETYPES_PHP:
666 tag_list_add_groups(tag_store,
667 &(tv_iters.tag_struct), _("Interfaces"), "classviewer-struct",
668 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
669 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
670 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
671 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
672 NULL);
673 /*&(tv_iters.tag_struct), _("Label"),*/
674 /*&(tv_iters.tag_namespace), _("Begin"),*/
675 /*&(tv_iters.tag_other), _("Other"), NULL);*/
676 break;
678 case GEANY_FILETYPES_HTML:
680 tag_list_add_groups(tag_store,
681 &(tv_iters.tag_function), _("Functions"), NULL,
682 &(tv_iters.tag_member), _("Anchors"), NULL,
683 &(tv_iters.tag_namespace), _("H1 Headings"), NULL,
684 &(tv_iters.tag_class), _("H2 Headings"), NULL,
685 &(tv_iters.tag_variable), _("H3 Headings"), NULL,
686 NULL);
687 break;
689 case GEANY_FILETYPES_CSS:
691 tag_list_add_groups(tag_store,
692 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
693 &(tv_iters.tag_variable), _("ID Selectors"), "classviewer-var",
694 &(tv_iters.tag_struct), _("Type Selectors"), "classviewer-struct", NULL);
695 break;
697 case GEANY_FILETYPES_REST:
698 case GEANY_FILETYPES_TXT2TAGS:
699 case GEANY_FILETYPES_ABC:
701 tag_list_add_groups(tag_store,
702 &(tv_iters.tag_namespace), _("Chapter"), NULL,
703 &(tv_iters.tag_member), _("Section"), NULL,
704 &(tv_iters.tag_macro), _("Subsection"), NULL,
705 &(tv_iters.tag_variable), _("Subsubsection"), NULL,
706 NULL);
707 break;
709 case GEANY_FILETYPES_RUBY:
711 tag_list_add_groups(tag_store,
712 &(tv_iters.tag_namespace), _("Modules"), NULL,
713 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
714 &(tv_iters.tag_member), _("Singletons"), "classviewer-struct",
715 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
716 NULL);
717 /*&(tv_iters.tag_namespace), _("Begin"),*/
718 /*&(tv_iters.tag_other), _("Other"), NULL);*/
719 break;
721 case GEANY_FILETYPES_TCL:
723 tag_list_add_groups(tag_store,
724 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace",
725 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
726 &(tv_iters.tag_member), _("Methods"), "classviewer-method",
727 &(tv_iters.tag_function), _("Procedures"), "classviewer-other",
728 NULL);
729 break;
731 case GEANY_FILETYPES_PYTHON:
733 tag_list_add_groups(tag_store,
734 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
735 &(tv_iters.tag_member), _("Methods"), "classviewer-macro",
736 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
737 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
738 &(tv_iters.tag_namespace), _("Imports"), "classviewer-namespace",
739 NULL);
740 /*&(tv_iters.tag_macro), _("Mixin"),*/
741 /*&(tv_iters.tag_variable), _("Variables"),*/
742 /*&(tv_iters.tag_struct), _("Members"),*/
743 /*&(tv_iters.tag_namespace), _("Begin"),*/
744 /*&(tv_iters.tag_other), _("Other"), NULL);*/
745 break;
747 case GEANY_FILETYPES_VHDL:
749 tag_list_add_groups(tag_store,
750 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
751 /*&(tv_iters.tag_class), _("Constants"),*/
752 /*&(tv_iters.tag_member), _("Members"),*/
753 /*&(tv_iters.tag_macro), _("Macros"),*/
754 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
755 NULL);
756 /*&(tv_iters.tag_namespace), _("Namespaces"),*/
757 /*&(tv_iters.tag_struct), _("Signals"),*/
758 /*&(tv_iters.tag_other), _("Other"), NULL);*/
759 break;
761 case GEANY_FILETYPES_JAVA:
763 tag_list_add_groups(tag_store,
764 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
765 &(tv_iters.tag_struct), _("Interfaces"), "classviewer-struct",
766 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
767 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
768 &(tv_iters.tag_member), _("Members"), "classviewer-member",
769 /*&(tv_iters.tag_macro), _("Macros"),*/
770 /*&(tv_iters.tag_variable), _("Variables"),*/
771 &(tv_iters.tag_other), _("Other"), "classviewer-other",
772 NULL);
773 break;
775 case GEANY_FILETYPES_AS:
777 tag_list_add_groups(tag_store,
778 &(tv_iters.tag_namespace), _("Package"), "classviewer-namespace",
779 &(tv_iters.tag_struct), _("Interfaces"), "classviewer-struct",
780 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
781 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
782 &(tv_iters.tag_member), _("Properties"), "classviewer-member",
783 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
784 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
785 &(tv_iters.tag_other), _("Other"), "classviewer-other",
786 NULL);
787 break;
789 case GEANY_FILETYPES_HAXE:
791 tag_list_add_groups(tag_store,
792 &(tv_iters.tag_struct), _("Interfaces"), "classviewer-struct",
793 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
794 &(tv_iters.tag_function), _("Methods"), "classviewer-method",
795 &(tv_iters.tag_type), _("Types"), "classviewer-macro",
796 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
797 &(tv_iters.tag_other), _("Other"), "classviewer-other",
798 NULL);
799 break;
801 case GEANY_FILETYPES_BASIC:
803 tag_list_add_groups(tag_store,
804 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
805 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
806 &(tv_iters.tag_macro), _("Constants"), "classviewer-macro",
807 &(tv_iters.tag_struct), _("Types"), "classviewer-namespace",
808 &(tv_iters.tag_namespace), _("Labels"), "classviewer-member",
809 &(tv_iters.tag_other), _("Other"), "classviewer-other",
810 NULL);
811 break;
813 case GEANY_FILETYPES_F77:
814 case GEANY_FILETYPES_FORTRAN:
816 tag_list_add_groups(tag_store,
817 &(tv_iters.tag_namespace), _("Module"), "classviewer-class",
818 &(tv_iters.tag_struct), _("Interfaces"), "classviewer-struct",
819 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
820 &(tv_iters.tag_member), _("Subroutines"), "classviewer-method",
821 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
822 &(tv_iters.tag_type), _("Types"), "classviewer-namespace",
823 &(tv_iters.tag_macro), _("Blocks"), "classviewer-member",
824 &(tv_iters.tag_other), _("Other"), "classviewer-other",
825 NULL);
826 break;
828 case GEANY_FILETYPES_ASM:
830 tag_list_add_groups(tag_store,
831 &(tv_iters.tag_namespace), _("Labels"), "classviewer-namespace",
832 &(tv_iters.tag_function), _("Macros"), "classviewer-method",
833 &(tv_iters.tag_macro), _("Defines"), "classviewer-macro",
834 &(tv_iters.tag_struct), _("Types"), "classviewer-struct",
835 NULL);
836 break;
838 case GEANY_FILETYPES_MAKE:
839 tag_list_add_groups(tag_store,
840 &tv_iters.tag_function, _("Targets"), "classviewer-method",
841 &tv_iters.tag_macro, _("Macros"), "classviewer-macro",
842 NULL);
843 break;
844 case GEANY_FILETYPES_D:
845 default:
847 if (ft_id == GEANY_FILETYPES_D)
848 tag_list_add_groups(tag_store,
849 &(tv_iters.tag_namespace), _("Module"), NULL, NULL);
850 else
851 tag_list_add_groups(tag_store,
852 &(tv_iters.tag_namespace), _("Namespaces"), "classviewer-namespace", NULL);
854 tag_list_add_groups(tag_store,
855 &(tv_iters.tag_class), _("Classes"), "classviewer-class",
856 &(tv_iters.tag_function), _("Functions"), "classviewer-method",
857 &(tv_iters.tag_member), _("Members"), "classviewer-member",
858 &(tv_iters.tag_struct), _("Structs"), "classviewer-struct",
859 &(tv_iters.tag_type), _("Typedefs / Enums"), "classviewer-struct",
860 NULL);
862 if (ft_id != GEANY_FILETYPES_D)
864 tag_list_add_groups(tag_store,
865 &(tv_iters.tag_macro), _("Macros"), "classviewer-macro", NULL);
867 tag_list_add_groups(tag_store,
868 &(tv_iters.tag_variable), _("Variables"), "classviewer-var",
869 &(tv_iters.tag_other), _("Other"), "classviewer-other", NULL);
875 /* the following code surely can be improved, at the moment it collects some iters
876 * for removal and after that the actual removal is done. I didn't find a way to find and remove
877 * an empty row in one loop (next iter fails then) */
878 static void hide_empty_rows(GtkTreeStore *store)
880 GtkTreeIter iter, *iters[MAX_SYMBOL_TYPES] = { NULL };
881 guint i = 0;
883 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
884 return; /* stop when first iter is invalid, i.e. no elements */
886 do /* first collect the iters we need to delete empty rows */
888 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store), &iter))
889 iters[i++] = gtk_tree_iter_copy(&iter);
890 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
892 /* now actually delete the collected iters */
893 for (i = 0; i < MAX_SYMBOL_TYPES; i++)
895 if (G_UNLIKELY(iters[i] == NULL))
896 break;
897 gtk_tree_store_remove(store, iters[i]);
898 gtk_tree_iter_free(iters[i]);
903 static const gchar *get_symbol_name(GeanyDocument *doc, const TMTag *tag, gboolean found_parent)
905 gchar *utf8_name;
906 const gchar *scope = tag->atts.entry.scope;
907 static GString *buffer = NULL; /* buffer will be small so we can keep it for reuse */
908 gboolean doc_is_utf8 = FALSE;
910 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
911 * for None at this point completely */
912 if (utils_str_equal(doc->encoding, "UTF-8") ||
913 utils_str_equal(doc->encoding, "None"))
914 doc_is_utf8 = TRUE;
916 if (! doc_is_utf8)
917 utf8_name = encodings_convert_to_utf8_from_charset(tag->name,
918 (gsize) -1, doc->encoding, TRUE);
919 else
920 utf8_name = tag->name;
922 if (utf8_name == NULL)
923 return NULL;
925 if (! buffer)
926 buffer = g_string_new(NULL);
927 else
928 g_string_truncate(buffer, 0);
930 /* check first char of scope is a wordchar */
931 if (!found_parent && scope &&
932 strpbrk(scope, GEANY_WORDCHARS) == scope)
934 const gchar *sep = symbols_get_context_separator(FILETYPE_ID(doc->file_type));
936 g_string_append(buffer, scope);
937 g_string_append(buffer, sep);
939 g_string_append(buffer, utf8_name);
941 if (! doc_is_utf8)
942 g_free(utf8_name);
944 g_string_append_printf(buffer, " [%lu]", tag->atts.entry.line);
946 return buffer->str;
950 static gchar *get_symbol_tooltip(GeanyDocument *doc, const TMTag *tag)
952 gchar *utf8_name = editor_get_calltip_text(doc->editor, tag);
954 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
955 * for None at this point completely */
956 if (utf8_name != NULL &&
957 ! utils_str_equal(doc->encoding, "UTF-8") &&
958 ! utils_str_equal(doc->encoding, "None"))
960 setptr(utf8_name,
961 encodings_convert_to_utf8_from_charset(utf8_name, (gsize) -1, doc->encoding, TRUE));
964 if (utf8_name != NULL)
965 setptr(utf8_name, g_markup_escape_text(utf8_name, -1));
967 return utf8_name;
971 /* find the last word in "foo::bar::blah", e.g. "blah" */
972 const gchar *get_parent_name(const TMTag *tag, filetype_id ft_id)
974 const gchar *scope = tag->atts.entry.scope;
975 const gchar *separator = symbols_get_context_separator(ft_id);
976 const gchar *str, *ptr;
978 if (!scope)
979 return NULL;
981 str = scope;
983 while (1)
985 ptr = strstr(str, separator);
986 if (ptr)
988 str = ptr + strlen(separator);
990 else
991 break;
994 return NZV(str) ? str : NULL;
998 static GtkTreeIter *get_tag_type_iter(TMTagType tag_type, filetype_id ft_id)
1000 GtkTreeIter *iter = NULL;
1002 switch (tag_type)
1004 case tm_tag_prototype_t:
1005 case tm_tag_method_t:
1006 case tm_tag_function_t:
1008 iter = &tv_iters.tag_function;
1009 break;
1011 case tm_tag_macro_t:
1012 case tm_tag_macro_with_arg_t:
1014 iter = &tv_iters.tag_macro;
1015 break;
1017 case tm_tag_class_t:
1019 iter = &tv_iters.tag_class;
1020 break;
1022 case tm_tag_member_t:
1023 case tm_tag_field_t:
1025 iter = &tv_iters.tag_member;
1026 break;
1028 case tm_tag_typedef_t:
1029 case tm_tag_enum_t:
1031 iter = &tv_iters.tag_type;
1032 break;
1034 case tm_tag_union_t:
1035 case tm_tag_struct_t:
1036 case tm_tag_interface_t:
1038 iter = &tv_iters.tag_struct;
1039 break;
1041 case tm_tag_variable_t:
1043 iter = &tv_iters.tag_variable;
1044 break;
1046 case tm_tag_namespace_t:
1047 case tm_tag_package_t:
1049 iter = &tv_iters.tag_namespace;
1050 break;
1052 default:
1054 iter = &tv_iters.tag_other;
1057 if (G_LIKELY(iter->stamp != -1))
1058 return iter;
1059 else
1060 return NULL;
1064 static GdkPixbuf *get_child_icon(GtkTreeStore *tree_store, GtkTreeIter *parent)
1066 GdkPixbuf *icon = NULL;
1068 if (parent == &tv_iters.tag_other)
1070 return get_tag_icon("classviewer-var");
1072 /* copy parent icon */
1073 gtk_tree_model_get(GTK_TREE_MODEL(tree_store), parent,
1074 SYMBOLS_COLUMN_ICON, &icon, -1);
1075 return icon;
1079 static void add_tree_tag(GeanyDocument *doc, const TMTag *tag, GHashTable *parent_hash)
1081 filetype_id ft_id = FILETYPE_ID(doc->file_type);
1082 GtkTreeStore *tree_store = doc->priv->tag_store;
1083 GtkTreeIter *parent = NULL;
1085 parent = get_tag_type_iter(tag->type, ft_id);
1087 if (G_LIKELY(parent))
1089 const gchar *name;
1090 const gchar *parent_name = get_parent_name(tag, ft_id);
1091 GtkTreeIter iter;
1092 GtkTreeIter *child = NULL;
1093 GdkPixbuf *icon = NULL;
1095 child = &iter;
1096 icon = get_child_icon(tree_store, parent);
1098 if (parent_name)
1100 GtkTreeIter *parent_search =
1101 (GtkTreeIter *)g_hash_table_lookup(parent_hash, parent_name);
1103 if (parent_search)
1104 parent = parent_search;
1105 else
1106 parent_name = NULL;
1109 /* check if the current tag is a parent of other tags */
1110 if (g_hash_table_lookup_extended(parent_hash, tag->name, NULL, NULL) &&
1111 !utils_str_equal(tag->name, parent_name)) /* prevent Foo::Foo from making parent = child */
1113 GtkTreeIter *new_iter = g_new0(GtkTreeIter, 1);
1115 /* set an iter value for the hash key */
1116 g_hash_table_insert(parent_hash, tag->name, new_iter);
1117 /* instead of ignoring the appended child iter below, use the one in the hash table */
1118 child = new_iter;
1121 gtk_tree_store_append(tree_store, child, parent);
1123 name = get_symbol_name(doc, tag, (parent_name != NULL));
1124 gtk_tree_store_set(tree_store, child,
1125 SYMBOLS_COLUMN_ICON, icon,
1126 SYMBOLS_COLUMN_NAME, name,
1127 SYMBOLS_COLUMN_TAG, tag,
1128 -1);
1130 if (gtk_check_version(2, 12, 0) == NULL)
1132 gchar *tooltip = get_symbol_tooltip(doc, tag);
1133 gtk_tree_store_set(tree_store, child, SYMBOLS_COLUMN_TOOLTIP, tooltip, -1);
1134 g_free(tooltip);
1137 if (G_LIKELY(G_IS_OBJECT(icon)))
1138 g_object_unref(icon);
1140 else
1141 geany_debug("Missing symbol-tree parent iter for type %d!", tag->type);
1145 static void add_tree_tags(GeanyDocument *doc, const GList *tags)
1147 const GList *item;
1148 GHashTable *parent_hash;
1150 /* Create a hash table "parent_tag_name":(GtkTreeIter*) */
1151 parent_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
1153 /* find and store all parent names in the hash table */
1154 for (item = tags; item; item = g_list_next(item))
1156 const TMTag *tag = item->data;
1157 const gchar *name = get_parent_name(tag, FILETYPE_ID(doc->file_type));
1159 if (name)
1160 g_hash_table_insert(parent_hash, (gpointer)name, NULL);
1162 for (item = tags; item; item = g_list_next(item))
1164 const TMTag *tag = item->data;
1166 add_tree_tag(doc, tag, parent_hash);
1168 g_hash_table_destroy(parent_hash);
1172 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1173 * is not stable, so the order is already lost. */
1174 static gint compare_top_level_names(const gchar *a, const gchar *b)
1176 guint i;
1177 const gchar *name;
1179 foreach_ptr_array(name, i, top_level_iter_names)
1181 if (utils_str_equal(name, a))
1182 return -1;
1183 if (utils_str_equal(name, b))
1184 return 1;
1185 /* This should never happen as it would mean that two or more top
1186 * level items have the same name but it can happen by typos in the translations. */
1187 if (utils_str_equal(a, b))
1188 return 1;
1190 g_warning("Couldn't find top level node '%s' or '%s'!", a, b);
1191 return 0;
1195 static gboolean tag_has_missing_parent(const TMTag *tag, GtkTreeStore *store,
1196 GtkTreeIter *iter)
1198 /* if the tag has a parent tag, it should be at depth >= 2 */
1199 return NZV(tag->atts.entry.scope) &&
1200 gtk_tree_store_iter_depth(store, iter) == 1;
1204 static gint tree_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b,
1205 gpointer user_data)
1207 gboolean sort_by_name = GPOINTER_TO_INT(user_data);
1208 const TMTag *tag_a, *tag_b;
1210 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_TAG, &tag_a, -1);
1211 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_TAG, &tag_b, -1);
1213 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1214 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1215 if (tag_a && !tag_has_missing_parent(tag_a, GTK_TREE_STORE(model), a) &&
1216 tag_b && !tag_has_missing_parent(tag_b, GTK_TREE_STORE(model), b))
1218 return sort_by_name ? compare_symbol(tag_a, tag_b) :
1219 compare_symbol_lines(tag_a, tag_b);
1221 else
1223 gchar *astr, *bstr;
1224 gint cmp;
1226 gtk_tree_model_get(model, a, SYMBOLS_COLUMN_NAME, &astr, -1);
1227 gtk_tree_model_get(model, b, SYMBOLS_COLUMN_NAME, &bstr, -1);
1229 /* if a is toplevel, b must be also */
1230 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model), a) == 0)
1232 cmp = compare_top_level_names(astr, bstr);
1234 else
1236 cmp = strcmp(astr, bstr);
1238 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1239 if (tag_a && tag_b)
1240 if (!sort_by_name ||
1241 (utils_str_equal(tag_a->name, tag_b->name) &&
1242 utils_str_equal(tag_a->atts.entry.scope, tag_b->atts.entry.scope)))
1243 cmp = compare_symbol_lines(tag_a, tag_b);
1245 g_free(astr);
1246 g_free(bstr);
1247 return cmp;
1252 static void sort_tree(GtkTreeStore *store, gboolean sort_by_name)
1254 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, tree_sort_func,
1255 GINT_TO_POINTER(sort_by_name), NULL);
1257 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store), SYMBOLS_COLUMN_NAME, GTK_SORT_ASCENDING);
1261 gboolean symbols_recreate_tag_list(GeanyDocument *doc, gint sort_mode)
1263 GList *tags;
1265 g_return_val_if_fail(doc != NULL, FALSE);
1267 tags = get_tag_list(doc, tm_tag_max_t);
1268 if (tags == NULL)
1269 return FALSE;
1271 /* Make sure the model stays with us after the tree view unrefs it */
1272 g_object_ref(GTK_TREE_MODEL(doc->priv->tag_store));
1273 /* Detach model from view */
1274 gtk_tree_view_set_model(GTK_TREE_VIEW(doc->priv->tag_tree), NULL);
1275 /* Clear all contents */
1276 gtk_tree_store_clear(doc->priv->tag_store);
1278 /* add grandparent type iters */
1279 add_top_level_items(doc);
1281 add_tree_tags(doc, tags);
1282 g_list_free(tags);
1284 hide_empty_rows(doc->priv->tag_store);
1286 if (sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
1287 sort_mode = doc->priv->symbol_list_sort_mode;
1289 sort_tree(doc->priv->tag_store, sort_mode == SYMBOLS_SORT_BY_NAME);
1290 doc->priv->symbol_list_sort_mode = sort_mode;
1292 /* Re-attach model to view */
1293 gtk_tree_view_set_model(GTK_TREE_VIEW(doc->priv->tag_tree),
1294 GTK_TREE_MODEL(doc->priv->tag_store));
1295 g_object_unref(GTK_TREE_MODEL(doc->priv->tag_store));
1296 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
1298 return TRUE;
1302 /* Detects a global tags filetype from the *.lang.* language extension.
1303 * Returns NULL if there was no matching TM language. */
1304 static GeanyFiletype *detect_global_tags_filetype(const gchar *utf8_filename)
1306 gchar *tags_ext;
1307 gchar *shortname = g_strdup(utf8_filename);
1308 GeanyFiletype *ft = NULL;
1310 tags_ext = strstr(shortname, ".tags");
1311 if (tags_ext)
1313 *tags_ext = '\0'; /* remove .tags extension */
1314 ft = filetypes_detect_from_extension(shortname);
1316 g_free(shortname);
1317 return ft;
1321 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1322 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1323 * the relevant path.
1324 * Example:
1325 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1326 int symbols_generate_global_tags(int argc, char **argv, gboolean want_preprocess)
1328 /* -E pre-process, -dD output user macros, -p prof info (?),
1329 * -undef remove builtin macros (seems to be needed with FC5 gcc 4.1.1) */
1330 const char pre_process[] = "gcc -E -dD -p -undef";
1332 if (argc > 2)
1334 /* Create global taglist */
1335 int status;
1336 char *command;
1337 const char *tags_file = argv[1];
1338 char *utf8_fname;
1339 GeanyFiletype *ft;
1341 utf8_fname = utils_get_utf8_from_locale(tags_file);
1342 ft = detect_global_tags_filetype(utf8_fname);
1343 g_free(utf8_fname);
1345 if (ft == NULL)
1347 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file);
1348 return 1;
1350 /* load ignore list for C/C++ parser */
1351 if (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP)
1352 load_c_ignore_tags();
1354 if (want_preprocess && (ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP))
1355 command = g_strdup_printf("%s %s", pre_process, NVL(getenv("CFLAGS"), ""));
1356 else
1357 command = NULL; /* don't preprocess */
1359 geany_debug("Generating %s tags file.", ft->name);
1360 tm_get_workspace();
1361 status = tm_workspace_create_global_tags(app->configdir, command,
1362 (const char **) (argv + 2),
1363 argc - 2, tags_file, ft->lang);
1364 g_free(command);
1365 symbols_finalize(); /* free c_tags_ignore data */
1366 if (! status)
1368 g_printerr(_("Failed to create tags file, perhaps because no tags "
1369 "were found.\n"));
1370 return 1;
1373 else
1375 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv[0]);
1376 g_printerr(_("Example:\n"
1377 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1378 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv[0]);
1379 return 1;
1381 return 0;
1385 void symbols_show_load_tags_dialog(void)
1387 GtkWidget *dialog;
1388 GtkFileFilter *filter;
1390 dialog = gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets.window),
1391 GTK_FILE_CHOOSER_ACTION_OPEN,
1392 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1393 GTK_STOCK_OPEN, GTK_RESPONSE_OK,
1394 NULL);
1395 gtk_widget_set_name(dialog, "GeanyDialog");
1396 filter = gtk_file_filter_new();
1397 gtk_file_filter_set_name(filter, _("Geany tag files (*.tags)"));
1398 gtk_file_filter_add_pattern(filter, "*.tags");
1399 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
1401 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK)
1403 GSList *flist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog));
1404 GSList *item;
1406 for (item = flist; item != NULL; item = g_slist_next(item))
1408 gchar *fname = item->data;
1409 gchar *utf8_fname;
1410 GeanyFiletype *ft;
1412 utf8_fname = utils_get_utf8_from_locale(fname);
1413 ft = detect_global_tags_filetype(utf8_fname);
1415 if (ft != NULL && symbols_load_global_tags(fname, ft))
1416 /* For translators: the first wildcard is the filetype, the second the filename */
1417 ui_set_statusbar(TRUE, _("Loaded %s tags file '%s'."), ft->name, utf8_fname);
1418 else
1419 ui_set_statusbar(TRUE, _("Could not load tags file '%s'."), utf8_fname);
1421 g_free(utf8_fname);
1422 g_free(fname);
1424 g_slist_free(flist);
1426 gtk_widget_destroy(dialog);
1430 /* Fills a hash table with filetype keys that hold a linked list of filenames. */
1431 static GHashTable *get_tagfile_hash(const GSList *file_list)
1433 const GSList *node;
1434 GHashTable *hash = g_hash_table_new(NULL, NULL);
1436 for (node = file_list; node != NULL; node = g_slist_next(node))
1438 GList *fnames;
1439 gchar *fname = node->data;
1440 gchar *utf8_fname = utils_get_utf8_from_locale(fname);
1441 GeanyFiletype *ft = detect_global_tags_filetype(utf8_fname);
1443 g_free(utf8_fname);
1445 if (FILETYPE_ID(ft) != GEANY_FILETYPES_NONE)
1447 fnames = g_hash_table_lookup(hash, ft); /* may be NULL */
1448 fnames = g_list_append(fnames, fname);
1449 g_hash_table_insert(hash, ft, fnames);
1451 else
1452 geany_debug("Unknown filetype for file '%s'.", fname);
1454 return hash;
1458 static GHashTable *init_user_tags(void)
1460 GSList *file_list = NULL, *list = NULL;
1461 GHashTable *lang_hash;
1462 const gchar *dir;
1464 dir = utils_build_path(app->configdir, "tags", NULL);
1465 /* create the user tags dir for next time if it doesn't exist */
1466 if (! g_file_test(dir, G_FILE_TEST_IS_DIR))
1468 utils_mkdir(dir, FALSE);
1470 file_list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
1472 dir = utils_build_path(app->datadir, "tags", NULL);
1473 list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
1474 file_list = g_slist_concat(file_list, list);
1476 lang_hash = get_tagfile_hash(file_list);
1477 /* don't need to delete list contents because they are now used for hash contents */
1478 g_slist_free(file_list);
1480 return lang_hash;
1484 static void load_user_tags(filetype_id ft_id)
1486 static guchar *tags_loaded = NULL;
1487 static GHashTable *lang_hash = NULL;
1488 GList *fnames;
1489 const GList *node;
1490 GeanyFiletype *ft = filetypes[ft_id];
1492 g_return_if_fail(ft_id > 0);
1494 if (!tags_loaded)
1495 tags_loaded = g_new0(guchar, filetypes_array->len);
1496 if (tags_loaded[ft_id])
1497 return;
1498 tags_loaded[ft_id] = TRUE; /* prevent reloading */
1500 if (lang_hash == NULL)
1501 lang_hash = init_user_tags();
1503 fnames = g_hash_table_lookup(lang_hash, ft);
1505 for (node = fnames; node != NULL; node = g_list_next(node))
1507 const gchar *fname = node->data;
1509 symbols_load_global_tags(fname, ft);
1511 g_list_foreach(fnames, (GFunc) g_free, NULL);
1512 g_list_free(fnames);
1513 g_hash_table_remove(lang_hash, (gpointer) ft);
1517 gboolean symbols_goto_tag(const gchar *name, gboolean definition)
1519 const gint forward_types = tm_tag_prototype_t | tm_tag_externvar_t;
1520 gint type;
1521 TMTag *tmtag = NULL;
1522 GeanyDocument *old_doc = document_get_current();
1524 /* goto tag definition: all except prototypes / forward declarations / externs */
1525 type = (definition) ? tm_tag_max_t - forward_types : forward_types;
1527 /* first look in the current document */
1528 if (old_doc != NULL && old_doc->tm_file)
1529 tmtag = find_work_object_tag(old_doc->tm_file, name, type);
1531 /* if not found, look in the workspace */
1532 if (tmtag == NULL)
1533 tmtag = find_workspace_tag(name, type);
1535 if (tmtag != NULL)
1537 GeanyDocument *new_doc = document_find_by_real_path(
1538 tmtag->atts.entry.file->work_object.file_name);
1540 /* not found in opened document, should open */
1541 if (new_doc == NULL)
1543 new_doc = document_open_file(tmtag->atts.entry.file->work_object.file_name, FALSE, NULL, NULL);
1546 if (navqueue_goto_line(old_doc, new_doc, tmtag->atts.entry.line))
1547 return TRUE;
1549 /* if we are here, there was no match and we are beeping ;-) */
1550 utils_beep();
1551 if (type == forward_types)
1552 ui_set_statusbar(FALSE, _("Forward declaration \"%s\" not found."), name);
1553 else
1554 ui_set_statusbar(FALSE, _("Definition of \"%s\" not found."), name);
1555 return FALSE;
1559 /* This could perhaps be improved to check for #if, class etc. */
1560 static gint get_function_fold_number(GeanyDocument *doc)
1562 /* for Java the functions are always one fold level above the class scope */
1563 if (FILETYPE_ID(doc->file_type) == GEANY_FILETYPES_JAVA)
1564 return SC_FOLDLEVELBASE + 1;
1565 else
1566 return SC_FOLDLEVELBASE;
1570 /* Should be used only with symbols_get_current_function. */
1571 static gboolean current_function_changed(GeanyDocument *doc, gint cur_line, gint fold_level)
1573 static gint old_line = -2;
1574 static GeanyDocument *old_doc = NULL;
1575 static gint old_fold_num = -1;
1576 const gint fold_num = fold_level & SC_FOLDLEVELNUMBERMASK;
1577 gboolean ret;
1579 /* check if the cached line and file index have changed since last time: */
1580 if (doc == NULL || doc != old_doc)
1581 ret = TRUE;
1582 else
1583 if (cur_line == old_line)
1584 ret = FALSE;
1585 else
1587 /* if the line has only changed by 1 */
1588 if (abs(cur_line - old_line) == 1)
1590 const gint fn_fold =
1591 get_function_fold_number(doc);
1592 /* It's the same function if the fold number hasn't changed, or both the new
1593 * and old fold numbers are above the function fold number. */
1594 gboolean same =
1595 fold_num == old_fold_num ||
1596 (old_fold_num > fn_fold && fold_num > fn_fold);
1598 ret = ! same;
1600 else ret = TRUE;
1603 /* record current line and file index for next time */
1604 old_line = cur_line;
1605 old_doc = doc;
1606 old_fold_num = fold_num;
1607 return ret;
1611 /* Parse the function name up to 2 lines before tag_line.
1612 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1613 * type or argument names can be confused with the function name. */
1614 static gchar *parse_function_at_line(ScintillaObject *sci, gint tag_line)
1616 gint start, end, max_pos;
1617 gchar *cur_tag;
1618 gint fn_style;
1620 switch (sci_get_lexer(sci))
1622 case SCLEX_RUBY: fn_style = SCE_RB_DEFNAME; break;
1623 case SCLEX_PYTHON: fn_style = SCE_P_DEFNAME; break;
1624 default: fn_style = SCE_C_IDENTIFIER; /* several lexers use SCE_C_IDENTIFIER */
1626 start = sci_get_position_from_line(sci, tag_line - 2);
1627 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1628 while (sci_get_style_at(sci, start) != fn_style
1629 && start < max_pos) start++;
1631 end = start;
1632 while (sci_get_style_at(sci, end) == fn_style
1633 && end < max_pos) end++;
1635 if (start == end)
1636 return NULL;
1637 cur_tag = g_malloc(end - start + 1);
1638 sci_get_text_range(sci, start, end, cur_tag);
1639 return cur_tag;
1643 /* Parse the function name */
1644 static gchar *parse_cpp_function_at_line(ScintillaObject *sci, gint tag_line)
1646 gint start, end, first_pos, max_pos;
1647 gint tmp;
1648 gchar c;
1649 gchar *cur_tag;
1651 first_pos = end = sci_get_position_from_line(sci, tag_line);
1652 max_pos = sci_get_position_from_line(sci, tag_line + 1);
1653 tmp = 0;
1654 /* goto the begin of function body */
1655 while (end < max_pos &&
1656 (tmp = sci_get_char_at(sci, end)) != '{' &&
1657 tmp != 0) end++;
1658 if (tmp == 0) end --;
1660 /* go back to the end of function identifier */
1661 while (end > 0 && end > first_pos - 500 &&
1662 (tmp = sci_get_char_at(sci, end)) != '(' &&
1663 tmp != 0) end--;
1664 end--;
1665 if (end < 0) end = 0;
1667 /* skip whitespaces between identifier and ( */
1668 while (end > 0 && isspace(sci_get_char_at(sci, end))) end--;
1670 start = end;
1671 c = 0;
1672 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1673 while (start >= 0 && ((tmp = sci_get_style_at(sci, start)) == SCE_C_IDENTIFIER
1674 || tmp == SCE_C_GLOBALCLASS
1675 || (c = sci_get_char_at(sci, start)) == '~'
1676 || c == ':'))
1677 start--;
1678 if (start != 0 && start < end) start++; /* correct for last non-matching char */
1680 if (start == end) return NULL;
1681 cur_tag = g_malloc(end - start + 2);
1682 sci_get_text_range(sci, start, end + 1, cur_tag);
1683 return cur_tag;
1687 /* Sets *tagname to point at the current function or tag name.
1688 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
1689 * call to this function.
1690 * Returns: line number of the current tag, or -1 if unknown. */
1691 gint symbols_get_current_function(GeanyDocument *doc, const gchar **tagname)
1693 static gint tag_line = -1;
1694 static gchar *cur_tag = NULL;
1695 gint line;
1696 gint fold_level;
1697 TMWorkObject *tm_file;
1699 if (doc == NULL) /* reset current function */
1701 current_function_changed(NULL, -1, -1);
1702 g_free(cur_tag);
1703 cur_tag = g_strdup(_("unknown"));
1704 if (tagname != NULL)
1705 *tagname = cur_tag;
1706 tag_line = -1;
1707 return tag_line;
1710 line = sci_get_current_line(doc->editor->sci);
1711 fold_level = sci_get_fold_level(doc->editor->sci, line);
1712 /* check if the cached line and file index have changed since last time: */
1713 if (! current_function_changed(doc, line, fold_level))
1715 /* we can assume same current function as before */
1716 *tagname = cur_tag;
1717 return tag_line;
1719 g_free(cur_tag); /* free the old tag, it will be replaced. */
1721 /* if line is at base fold level, we're not in a function */
1722 if ((fold_level & SC_FOLDLEVELNUMBERMASK) == SC_FOLDLEVELBASE)
1724 cur_tag = g_strdup(_("unknown"));
1725 *tagname = cur_tag;
1726 tag_line = -1;
1727 return tag_line;
1729 tm_file = doc->tm_file;
1731 /* if the document has no changes, get the previous function name from TM */
1732 if (! doc->changed && tm_file != NULL && tm_file->tags_array != NULL)
1734 const TMTag *tag = (const TMTag*) tm_get_current_function(tm_file->tags_array, line);
1736 if (tag != NULL)
1738 gchar *tmp;
1739 tmp = tag->atts.entry.scope;
1740 cur_tag = tmp ? g_strconcat(tmp, "::", tag->name, NULL) : g_strdup(tag->name);
1741 *tagname = cur_tag;
1742 tag_line = tag->atts.entry.line;
1743 return tag_line;
1747 /* parse the current function name here because TM line numbers may have changed,
1748 * and it would take too long to reparse the whole file. */
1749 if (doc->file_type != NULL && doc->file_type->id != GEANY_FILETYPES_NONE)
1751 const gint fn_fold = get_function_fold_number(doc);
1753 tag_line = line;
1754 do /* find the top level fold point */
1756 tag_line = sci_get_fold_parent(doc->editor->sci, tag_line);
1757 fold_level = sci_get_fold_level(doc->editor->sci, tag_line);
1758 } while (tag_line >= 0 &&
1759 (fold_level & SC_FOLDLEVELNUMBERMASK) != fn_fold);
1761 if (tag_line >= 0)
1763 if (sci_get_lexer(doc->editor->sci) == SCLEX_CPP)
1764 cur_tag = parse_cpp_function_at_line(doc->editor->sci, tag_line);
1765 else
1766 cur_tag = parse_function_at_line(doc->editor->sci, tag_line);
1768 if (cur_tag != NULL)
1770 *tagname = cur_tag;
1771 return tag_line;
1776 cur_tag = g_strdup(_("unknown"));
1777 *tagname = cur_tag;
1778 tag_line = -1;
1779 return tag_line;
1783 static void on_symbol_tree_sort_clicked(GtkMenuItem *menuitem, gpointer user_data)
1785 gint sort_mode = GPOINTER_TO_INT(user_data);
1786 GeanyDocument *doc = document_get_current();
1788 if (ignore_callback)
1789 return;
1791 if (doc != NULL)
1792 doc->has_tags = symbols_recreate_tag_list(doc, sort_mode);
1796 static void on_symbol_tree_menu_show(GtkWidget *widget,
1797 gpointer user_data)
1799 GeanyDocument *doc = document_get_current();
1800 gboolean enable;
1802 enable = doc && doc->has_tags;
1803 gtk_widget_set_sensitive(symbol_menu.sort_by_name, enable);
1804 gtk_widget_set_sensitive(symbol_menu.sort_by_appearance, enable);
1805 gtk_widget_set_sensitive(symbol_menu.expand_all, enable);
1806 gtk_widget_set_sensitive(symbol_menu.collapse_all, enable);
1808 if (! doc)
1809 return;
1811 ignore_callback = TRUE;
1813 if (doc->priv->symbol_list_sort_mode == SYMBOLS_SORT_BY_NAME)
1814 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_name), TRUE);
1815 else
1816 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu.sort_by_appearance), TRUE);
1818 ignore_callback = FALSE;
1822 static void on_expand_collapse(GtkWidget *widget, gpointer user_data)
1824 gboolean expand = utils_str_equal(user_data, GTK_STOCK_ADD);
1825 GeanyDocument *doc = document_get_current();
1827 if (! doc)
1828 return;
1830 g_return_if_fail(doc->priv->tag_tree);
1832 if (expand)
1833 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc->priv->tag_tree));
1834 else
1835 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc->priv->tag_tree));
1839 static void create_taglist_popup_menu(void)
1841 GtkWidget *item, *menu;
1843 tv.popup_taglist = menu = gtk_menu_new();
1845 symbol_menu.expand_all = item = ui_image_menu_item_new(GTK_STOCK_ADD, _("_Expand All"));
1846 gtk_widget_show(item);
1847 gtk_container_add(GTK_CONTAINER(menu), item);
1848 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GTK_STOCK_ADD);
1850 symbol_menu.collapse_all = item = ui_image_menu_item_new(GTK_STOCK_REMOVE, _("_Collapse All"));
1851 gtk_widget_show(item);
1852 gtk_container_add(GTK_CONTAINER(menu), item);
1853 g_signal_connect(item, "activate", G_CALLBACK(on_expand_collapse), GTK_STOCK_REMOVE);
1855 item = gtk_separator_menu_item_new();
1856 gtk_widget_show(item);
1857 gtk_container_add(GTK_CONTAINER(menu), item);
1859 symbol_menu.sort_by_name = item = gtk_radio_menu_item_new_with_mnemonic(NULL,
1860 _("Sort by _Name"));
1861 gtk_widget_show(item);
1862 gtk_container_add(GTK_CONTAINER(menu), item);
1863 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
1864 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME));
1866 symbol_menu.sort_by_appearance = item = gtk_radio_menu_item_new_with_mnemonic_from_widget(
1867 GTK_RADIO_MENU_ITEM(item), _("Sort by _Appearance"));
1868 gtk_widget_show(item);
1869 gtk_container_add(GTK_CONTAINER(menu), item);
1870 g_signal_connect(item, "activate", G_CALLBACK(on_symbol_tree_sort_clicked),
1871 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE));
1873 g_signal_connect(menu, "show", G_CALLBACK(on_symbol_tree_menu_show), NULL);
1875 sidebar_add_common_menu_items(GTK_MENU(menu));
1879 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
1881 g_return_if_fail(NZV(doc->real_path));
1883 if (utils_str_equal(doc->real_path,
1884 utils_build_path(app->configdir, "ignore.tags", NULL)))
1885 load_c_ignore_tags();
1889 void symbols_init(void)
1891 create_taglist_popup_menu();
1893 ui_add_config_file_menu_item(utils_build_path(app->configdir, "ignore.tags", NULL),
1894 NULL, NULL);
1895 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
1899 void symbols_finalize(void)
1901 g_strfreev(html_entities);
1902 g_strfreev(c_tags_ignore);