2 * symbols.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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.
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.
41 #include "filetypes.h"
42 #include "encodings.h"
44 #include "documentprivate.h"
46 #include "msgwindow.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
;
65 const gchar
*tag_file
;
68 /* Check before adding any more tags files, usually they should be downloaded separately. */
69 enum /* Geany tag files */
80 static TagFileInfo tag_file_info
[GTF_MAX
] =
83 {FALSE
, "pascal.tags"},
85 {FALSE
, "html_entities.tags"},
86 {FALSE
, "latex.tags"},
87 {FALSE
, "python.tags"}
90 static GPtrArray
*top_level_iter_names
= NULL
;
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
);
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);
125 void symbols_reload_config_files(void)
127 load_c_ignore_tags();
131 static gsize
get_tag_count(void)
133 GPtrArray
*tags
= tm_get_workspace()->global_tags
;
134 gsize count
= tags
? tags
->len
: 0;
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
)
146 gsize old_tag_count
= get_tag_count();
148 result
= tm_workspace_load_global_tags(tags_file
, ft
->lang
);
151 geany_debug("Loaded %s (%s), %u tag(s).", tags_file
, ft
->name
,
152 (guint
) (get_tag_count() - old_tag_count
));
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
)
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
)
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
:
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 */
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;
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
;
213 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&') */
214 static void html_tags_loaded(void)
218 if (cl_options
.ignore_global_tags
)
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
;
233 GString
*symbols_find_tags_as_string(GPtrArray
*tags_array
, guint tag_types
, gint 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))
261 g_string_append_c(s
, ' ');
262 g_string_append(s
, tag
->name
);
267 g_ptr_array_free(typedefs
, TRUE
);
272 const gchar
*symbols_get_context_separator(gint 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*/
282 /* avoid confusion with other possible separators in group/section name */
283 case GEANY_FILETYPES_CONF
:
284 case GEANY_FILETYPES_REST
:
293 GString
*symbols_get_macro_list(gint lang
)
301 if (app
->tm_workspace
->work_objects
== 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
++)
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
);
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
);
330 g_ptr_array_free(ftags
, TRUE
);
331 g_string_free(words
, TRUE
);
335 tm_tags_sort(ftags
, NULL
, FALSE
);
336 for (j
= 0; j
< ftags
->len
; j
++)
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
);
347 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
349 symbols_find_tm_tag(const GPtrArray
*tags
, const gchar
*tag_name
)
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
]);
363 static TMTag
*find_work_object_tag(const TMWorkObject
*workobj
,
364 const gchar
*tag_name
, gint type
)
369 if (G_LIKELY(workobj
!= NULL
))
371 tags
= tm_tags_extract(workobj
->tags_array
, type
);
374 tmtag
= symbols_find_tm_tag(tags
, tag_name
);
376 g_ptr_array_free(tags
, TRUE
);
382 return NULL
; /* not found */
386 static TMTag
*find_workspace_tag(const gchar
*tag_name
, gint type
)
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
]);
401 tmtag
= find_work_object_tag(workobj
, tag_name
, type
);
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
)
424 if (tag_a
== NULL
|| tag_b
== NULL
)
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
);
436 return tag_a
->atts
.entry
.line
- tag_b
->atts
.entry
.line
;
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
)
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
;
461 g_return_val_if_fail(doc
, NULL
);
463 if (! doc
->tm_file
|| ! doc
->tm_file
->tags_array
)
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
))
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
);
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
;
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
;
520 if (G_UNLIKELY(icon_theme
== NULL
))
523 gchar
*path
= g_strconcat(GEANY_DATADIR
, "/icons", NULL
);
525 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &x
, &y
);
526 icon_theme
= gtk_icon_theme_get_default();
528 gtk_icon_theme_append_search_path(icon_theme
, "share\\icons");
530 gtk_icon_theme_append_search_path(icon_theme
, path
);
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
, ...)
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
;
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);
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();
584 g_ptr_array_set_size(top_level_iter_names
, 0);
590 case GEANY_FILETYPES_DIFF
:
592 tag_list_add_groups(tag_store
,
593 &(tv_iters
.tag_function
), _("Files"), NULL
, NULL
);
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
,
609 case GEANY_FILETYPES_HASKELL
:
610 tag_list_add_groups(tag_store
,
611 &tv_iters
.tag_namespace
, _("Module"), NULL
,
612 &tv_iters
.tag_type
, _("Types"), NULL
,
613 &tv_iters
.tag_macro
, _("Type constructors"), NULL
,
614 &tv_iters
.tag_function
, _("Functions"), "classviewer-method",
617 case GEANY_FILETYPES_CONF
:
618 tag_list_add_groups(tag_store
,
619 &tv_iters
.tag_namespace
, _("Sections"), "classviewer-other",
620 &tv_iters
.tag_macro
, _("Keys"), "classviewer-var",
623 case GEANY_FILETYPES_NSIS
:
624 tag_list_add_groups(tag_store
,
625 &tv_iters
.tag_namespace
, _("Sections"), "classviewer-other",
626 &tv_iters
.tag_function
, _("Functions"), "classviewer-method",
627 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
630 case GEANY_FILETYPES_LATEX
:
632 tag_list_add_groups(tag_store
,
633 &(tv_iters
.tag_function
), _("Command"), NULL
,
634 &(tv_iters
.tag_class
), _("Environment"), NULL
,
635 &(tv_iters
.tag_member
), _("Section"), NULL
,
636 &(tv_iters
.tag_macro
), _("Subsection"), NULL
,
637 &(tv_iters
.tag_variable
), _("Subsubsection"), NULL
,
638 &(tv_iters
.tag_struct
), _("Label"), NULL
,
639 &(tv_iters
.tag_namespace
), _("Chapter"), NULL
,
640 &(tv_iters
.tag_other
), _("Other"), NULL
,
644 case GEANY_FILETYPES_MATLAB
:
646 tag_list_add_groups(tag_store
,
647 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
648 &(tv_iters
.tag_struct
), _("Structures"), "classviewer-struct",
652 case GEANY_FILETYPES_R
:
654 tag_list_add_groups(tag_store
,
655 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
656 &(tv_iters
.tag_struct
), _("Other"), NULL
,
660 case GEANY_FILETYPES_PERL
:
662 tag_list_add_groups(tag_store
,
663 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
664 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
665 &(tv_iters
.tag_macro
), _("Labels"), NULL
,
666 &(tv_iters
.tag_type
), _("Constants"), NULL
,
667 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
671 case GEANY_FILETYPES_PHP
:
673 tag_list_add_groups(tag_store
,
674 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
675 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
676 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
677 &(tv_iters
.tag_macro
), _("Constants"), "classviewer-macro",
678 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
682 case GEANY_FILETYPES_HTML
:
684 tag_list_add_groups(tag_store
,
685 &(tv_iters
.tag_function
), _("Functions"), NULL
,
686 &(tv_iters
.tag_member
), _("Anchors"), NULL
,
687 &(tv_iters
.tag_namespace
), _("H1 Headings"), NULL
,
688 &(tv_iters
.tag_class
), _("H2 Headings"), NULL
,
689 &(tv_iters
.tag_variable
), _("H3 Headings"), NULL
,
693 case GEANY_FILETYPES_CSS
:
695 tag_list_add_groups(tag_store
,
696 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
697 &(tv_iters
.tag_variable
), _("ID Selectors"), "classviewer-var",
698 &(tv_iters
.tag_struct
), _("Type Selectors"), "classviewer-struct", NULL
);
701 case GEANY_FILETYPES_REST
:
702 case GEANY_FILETYPES_TXT2TAGS
:
703 case GEANY_FILETYPES_ABC
:
705 tag_list_add_groups(tag_store
,
706 &(tv_iters
.tag_namespace
), _("Chapter"), NULL
,
707 &(tv_iters
.tag_member
), _("Section"), NULL
,
708 &(tv_iters
.tag_macro
), _("Subsection"), NULL
,
709 &(tv_iters
.tag_variable
), _("Subsubsection"), NULL
,
713 case GEANY_FILETYPES_RUBY
:
715 tag_list_add_groups(tag_store
,
716 &(tv_iters
.tag_namespace
), _("Modules"), NULL
,
717 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
718 &(tv_iters
.tag_member
), _("Singletons"), "classviewer-struct",
719 &(tv_iters
.tag_function
), _("Methods"), "classviewer-method",
723 case GEANY_FILETYPES_TCL
:
725 tag_list_add_groups(tag_store
,
726 &(tv_iters
.tag_namespace
), _("Namespaces"), "classviewer-namespace",
727 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
728 &(tv_iters
.tag_member
), _("Methods"), "classviewer-method",
729 &(tv_iters
.tag_function
), _("Procedures"), "classviewer-other",
733 case GEANY_FILETYPES_PYTHON
:
735 tag_list_add_groups(tag_store
,
736 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
737 &(tv_iters
.tag_member
), _("Methods"), "classviewer-macro",
738 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
739 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
740 &(tv_iters
.tag_namespace
), _("Imports"), "classviewer-namespace",
744 case GEANY_FILETYPES_VHDL
:
746 tag_list_add_groups(tag_store
,
747 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
748 &(tv_iters
.tag_class
), _("Entities"), "classviewer-class",
749 &(tv_iters
.tag_struct
), _("Architectures"), "classviewer-struct",
750 &(tv_iters
.tag_type
), _("Types"), "classviewer-other",
751 &(tv_iters
.tag_function
), _("Functions / Procedures"), "classviewer-method",
752 &(tv_iters
.tag_variable
), _("Variables / Signals"), "classviewer-var",
753 &(tv_iters
.tag_member
), _("Processes / Components"), "classviewer-member",
754 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
758 case GEANY_FILETYPES_VERILOG
:
760 tag_list_add_groups(tag_store
,
761 &(tv_iters
.tag_type
), _("Events"), "classviewer-macro",
762 &(tv_iters
.tag_class
), _("Modules"), "classviewer-class",
763 &(tv_iters
.tag_function
), _("Functions / Tasks"), "classviewer-method",
764 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
765 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
769 case GEANY_FILETYPES_JAVA
:
771 tag_list_add_groups(tag_store
,
772 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
773 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
774 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
775 &(tv_iters
.tag_function
), _("Methods"), "classviewer-method",
776 &(tv_iters
.tag_member
), _("Members"), "classviewer-member",
777 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
781 case GEANY_FILETYPES_AS
:
783 tag_list_add_groups(tag_store
,
784 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
785 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
786 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
787 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
788 &(tv_iters
.tag_member
), _("Properties"), "classviewer-member",
789 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
790 &(tv_iters
.tag_macro
), _("Constants"), "classviewer-macro",
791 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
795 case GEANY_FILETYPES_HAXE
:
797 tag_list_add_groups(tag_store
,
798 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
799 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
800 &(tv_iters
.tag_function
), _("Methods"), "classviewer-method",
801 &(tv_iters
.tag_type
), _("Types"), "classviewer-macro",
802 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
803 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
807 case GEANY_FILETYPES_BASIC
:
809 tag_list_add_groups(tag_store
,
810 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
811 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
812 &(tv_iters
.tag_macro
), _("Constants"), "classviewer-macro",
813 &(tv_iters
.tag_struct
), _("Types"), "classviewer-namespace",
814 &(tv_iters
.tag_namespace
), _("Labels"), "classviewer-member",
815 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
819 case GEANY_FILETYPES_F77
:
820 case GEANY_FILETYPES_FORTRAN
:
822 tag_list_add_groups(tag_store
,
823 &(tv_iters
.tag_namespace
), _("Module"), "classviewer-class",
824 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
825 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
826 &(tv_iters
.tag_member
), _("Subroutines"), "classviewer-method",
827 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
828 &(tv_iters
.tag_type
), _("Types"), "classviewer-namespace",
829 &(tv_iters
.tag_macro
), _("Blocks"), "classviewer-member",
830 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
834 case GEANY_FILETYPES_ASM
:
836 tag_list_add_groups(tag_store
,
837 &(tv_iters
.tag_namespace
), _("Labels"), "classviewer-namespace",
838 &(tv_iters
.tag_function
), _("Macros"), "classviewer-method",
839 &(tv_iters
.tag_macro
), _("Defines"), "classviewer-macro",
840 &(tv_iters
.tag_struct
), _("Types"), "classviewer-struct",
844 case GEANY_FILETYPES_MAKE
:
845 tag_list_add_groups(tag_store
,
846 &tv_iters
.tag_function
, _("Targets"), "classviewer-method",
847 &tv_iters
.tag_macro
, _("Macros"), "classviewer-macro",
850 case GEANY_FILETYPES_D
:
853 if (ft_id
== GEANY_FILETYPES_D
)
854 tag_list_add_groups(tag_store
,
855 &(tv_iters
.tag_namespace
), _("Module"), NULL
, NULL
);
857 tag_list_add_groups(tag_store
,
858 &(tv_iters
.tag_namespace
), _("Namespaces"), "classviewer-namespace", NULL
);
860 tag_list_add_groups(tag_store
,
861 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
862 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
863 &(tv_iters
.tag_member
), _("Members"), "classviewer-member",
864 &(tv_iters
.tag_struct
), _("Structs"), "classviewer-struct",
865 &(tv_iters
.tag_type
), _("Typedefs / Enums"), "classviewer-struct",
868 if (ft_id
!= GEANY_FILETYPES_D
)
870 tag_list_add_groups(tag_store
,
871 &(tv_iters
.tag_macro
), _("Macros"), "classviewer-macro", NULL
);
873 tag_list_add_groups(tag_store
,
874 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
875 &(tv_iters
.tag_other
), _("Other"), "classviewer-other", NULL
);
881 /* the following code surely can be improved, at the moment it collects some iters
882 * for removal and after that the actual removal is done. I didn't find a way to find and remove
883 * an empty row in one loop (next iter fails then) */
884 static void hide_empty_rows(GtkTreeStore
*store
)
886 GtkTreeIter iter
, *iters
[MAX_SYMBOL_TYPES
] = { NULL
};
889 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store
), &iter
))
890 return; /* stop when first iter is invalid, i.e. no elements */
892 do /* first collect the iters we need to delete empty rows */
894 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store
), &iter
))
895 iters
[i
++] = gtk_tree_iter_copy(&iter
);
896 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store
), &iter
));
898 /* now actually delete the collected iters */
899 for (i
= 0; i
< MAX_SYMBOL_TYPES
; i
++)
901 if (G_UNLIKELY(iters
[i
] == NULL
))
903 gtk_tree_store_remove(store
, iters
[i
]);
904 gtk_tree_iter_free(iters
[i
]);
909 static const gchar
*get_symbol_name(GeanyDocument
*doc
, const TMTag
*tag
, gboolean found_parent
)
912 const gchar
*scope
= tag
->atts
.entry
.scope
;
913 static GString
*buffer
= NULL
; /* buffer will be small so we can keep it for reuse */
914 gboolean doc_is_utf8
= FALSE
;
916 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
917 * for None at this point completely */
918 if (utils_str_equal(doc
->encoding
, "UTF-8") ||
919 utils_str_equal(doc
->encoding
, "None"))
923 utf8_name
= encodings_convert_to_utf8_from_charset(tag
->name
,
924 (gsize
) -1, doc
->encoding
, TRUE
);
926 utf8_name
= tag
->name
;
928 if (utf8_name
== NULL
)
932 buffer
= g_string_new(NULL
);
934 g_string_truncate(buffer
, 0);
936 /* check first char of scope is a wordchar */
937 if (!found_parent
&& scope
&&
938 strpbrk(scope
, GEANY_WORDCHARS
) == scope
)
940 const gchar
*sep
= symbols_get_context_separator(FILETYPE_ID(doc
->file_type
));
942 g_string_append(buffer
, scope
);
943 g_string_append(buffer
, sep
);
945 g_string_append(buffer
, utf8_name
);
950 g_string_append_printf(buffer
, " [%lu]", tag
->atts
.entry
.line
);
956 static gchar
*get_symbol_tooltip(GeanyDocument
*doc
, const TMTag
*tag
)
958 gchar
*utf8_name
= editor_get_calltip_text(doc
->editor
, tag
);
960 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
961 * for None at this point completely */
962 if (utf8_name
!= NULL
&&
963 ! utils_str_equal(doc
->encoding
, "UTF-8") &&
964 ! utils_str_equal(doc
->encoding
, "None"))
967 encodings_convert_to_utf8_from_charset(utf8_name
, (gsize
) -1, doc
->encoding
, TRUE
));
970 if (utf8_name
!= NULL
)
971 setptr(utf8_name
, g_markup_escape_text(utf8_name
, -1));
977 /* find the last word in "foo::bar::blah", e.g. "blah" */
978 static const gchar
*get_parent_name(const TMTag
*tag
, filetype_id ft_id
)
980 const gchar
*scope
= tag
->atts
.entry
.scope
;
981 const gchar
*separator
= symbols_get_context_separator(ft_id
);
982 const gchar
*str
, *ptr
;
991 ptr
= strstr(str
, separator
);
994 str
= ptr
+ strlen(separator
);
1000 return NZV(str
) ? str
: NULL
;
1004 static GtkTreeIter
*get_tag_type_iter(TMTagType tag_type
, filetype_id ft_id
)
1006 GtkTreeIter
*iter
= NULL
;
1010 case tm_tag_prototype_t
:
1011 case tm_tag_method_t
:
1012 case tm_tag_function_t
:
1014 iter
= &tv_iters
.tag_function
;
1017 case tm_tag_macro_t
:
1018 case tm_tag_macro_with_arg_t
:
1020 iter
= &tv_iters
.tag_macro
;
1023 case tm_tag_class_t
:
1025 iter
= &tv_iters
.tag_class
;
1028 case tm_tag_member_t
:
1029 case tm_tag_field_t
:
1031 iter
= &tv_iters
.tag_member
;
1034 case tm_tag_typedef_t
:
1037 iter
= &tv_iters
.tag_type
;
1040 case tm_tag_union_t
:
1041 case tm_tag_struct_t
:
1042 case tm_tag_interface_t
:
1044 iter
= &tv_iters
.tag_struct
;
1047 case tm_tag_variable_t
:
1049 iter
= &tv_iters
.tag_variable
;
1052 case tm_tag_namespace_t
:
1053 case tm_tag_package_t
:
1055 iter
= &tv_iters
.tag_namespace
;
1060 iter
= &tv_iters
.tag_other
;
1063 if (G_LIKELY(iter
->stamp
!= -1))
1070 static GdkPixbuf
*get_child_icon(GtkTreeStore
*tree_store
, GtkTreeIter
*parent
)
1072 GdkPixbuf
*icon
= NULL
;
1074 if (parent
== &tv_iters
.tag_other
)
1076 return get_tag_icon("classviewer-var");
1078 /* copy parent icon */
1079 gtk_tree_model_get(GTK_TREE_MODEL(tree_store
), parent
,
1080 SYMBOLS_COLUMN_ICON
, &icon
, -1);
1085 static void add_tree_tag(GeanyDocument
*doc
, const TMTag
*tag
, GHashTable
*parent_hash
)
1087 filetype_id ft_id
= FILETYPE_ID(doc
->file_type
);
1088 GtkTreeStore
*tree_store
= doc
->priv
->tag_store
;
1089 GtkTreeIter
*parent
= NULL
;
1091 parent
= get_tag_type_iter(tag
->type
, ft_id
);
1093 if (G_LIKELY(parent
))
1096 const gchar
*parent_name
= get_parent_name(tag
, ft_id
);
1098 GtkTreeIter
*child
= NULL
;
1099 GdkPixbuf
*icon
= NULL
;
1102 icon
= get_child_icon(tree_store
, parent
);
1106 GtkTreeIter
*parent_search
=
1107 (GtkTreeIter
*)g_hash_table_lookup(parent_hash
, parent_name
);
1110 parent
= parent_search
;
1115 /* check if the current tag is a parent of other tags */
1116 if (g_hash_table_lookup_extended(parent_hash
, tag
->name
, NULL
, NULL
) &&
1117 !utils_str_equal(tag
->name
, parent_name
)) /* prevent Foo::Foo from making parent = child */
1119 GtkTreeIter
*new_iter
= g_new0(GtkTreeIter
, 1);
1121 /* set an iter value for the hash key */
1122 g_hash_table_insert(parent_hash
, tag
->name
, new_iter
);
1123 /* instead of ignoring the appended child iter below, use the one in the hash table */
1127 gtk_tree_store_append(tree_store
, child
, parent
);
1129 name
= get_symbol_name(doc
, tag
, (parent_name
!= NULL
));
1130 gtk_tree_store_set(tree_store
, child
,
1131 SYMBOLS_COLUMN_ICON
, icon
,
1132 SYMBOLS_COLUMN_NAME
, name
,
1133 SYMBOLS_COLUMN_TAG
, tag
,
1136 if (gtk_check_version(2, 12, 0) == NULL
)
1138 gchar
*tooltip
= get_symbol_tooltip(doc
, tag
);
1139 gtk_tree_store_set(tree_store
, child
, SYMBOLS_COLUMN_TOOLTIP
, tooltip
, -1);
1143 if (G_LIKELY(G_IS_OBJECT(icon
)))
1144 g_object_unref(icon
);
1147 geany_debug("Missing symbol-tree parent iter for type %d!", tag
->type
);
1151 static void add_tree_tags(GeanyDocument
*doc
, const GList
*tags
)
1154 GHashTable
*parent_hash
;
1156 /* Create a hash table "parent_tag_name":(GtkTreeIter*) */
1157 parent_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
, g_free
);
1159 /* find and store all parent names in the hash table */
1160 for (item
= tags
; item
; item
= g_list_next(item
))
1162 const TMTag
*tag
= item
->data
;
1163 const gchar
*name
= get_parent_name(tag
, FILETYPE_ID(doc
->file_type
));
1166 g_hash_table_insert(parent_hash
, (gpointer
)name
, NULL
);
1168 for (item
= tags
; item
; item
= g_list_next(item
))
1170 const TMTag
*tag
= item
->data
;
1172 add_tree_tag(doc
, tag
, parent_hash
);
1174 g_hash_table_destroy(parent_hash
);
1178 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1179 * is not stable, so the order is already lost. */
1180 static gint
compare_top_level_names(const gchar
*a
, const gchar
*b
)
1185 foreach_ptr_array(name
, i
, top_level_iter_names
)
1187 if (utils_str_equal(name
, a
))
1189 if (utils_str_equal(name
, b
))
1191 /* This should never happen as it would mean that two or more top
1192 * level items have the same name but it can happen by typos in the translations. */
1193 if (utils_str_equal(a
, b
))
1196 g_warning("Couldn't find top level node '%s' or '%s'!", a
, b
);
1201 static gboolean
tag_has_missing_parent(const TMTag
*tag
, GtkTreeStore
*store
,
1204 /* if the tag has a parent tag, it should be at depth >= 2 */
1205 return NZV(tag
->atts
.entry
.scope
) &&
1206 gtk_tree_store_iter_depth(store
, iter
) == 1;
1210 static gint
tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1213 gboolean sort_by_name
= GPOINTER_TO_INT(user_data
);
1214 const TMTag
*tag_a
, *tag_b
;
1216 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_TAG
, &tag_a
, -1);
1217 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_TAG
, &tag_b
, -1);
1219 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1220 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1221 if (tag_a
&& !tag_has_missing_parent(tag_a
, GTK_TREE_STORE(model
), a
) &&
1222 tag_b
&& !tag_has_missing_parent(tag_b
, GTK_TREE_STORE(model
), b
))
1224 return sort_by_name
? compare_symbol(tag_a
, tag_b
) :
1225 compare_symbol_lines(tag_a
, tag_b
);
1232 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_NAME
, &astr
, -1);
1233 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_NAME
, &bstr
, -1);
1235 /* if a is toplevel, b must be also */
1236 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model
), a
) == 0)
1238 cmp
= compare_top_level_names(astr
, bstr
);
1242 cmp
= strcmp(astr
, bstr
);
1244 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1246 if (!sort_by_name
||
1247 (utils_str_equal(tag_a
->name
, tag_b
->name
) &&
1248 utils_str_equal(tag_a
->atts
.entry
.scope
, tag_b
->atts
.entry
.scope
)))
1249 cmp
= compare_symbol_lines(tag_a
, tag_b
);
1258 static void sort_tree(GtkTreeStore
*store
, gboolean sort_by_name
)
1260 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, tree_sort_func
,
1261 GINT_TO_POINTER(sort_by_name
), NULL
);
1263 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, GTK_SORT_ASCENDING
);
1267 gboolean
symbols_recreate_tag_list(GeanyDocument
*doc
, gint sort_mode
)
1271 g_return_val_if_fail(doc
!= NULL
, FALSE
);
1273 tags
= get_tag_list(doc
, tm_tag_max_t
);
1277 /* Make sure the model stays with us after the tree view unrefs it */
1278 g_object_ref(GTK_TREE_MODEL(doc
->priv
->tag_store
));
1279 /* Detach model from view */
1280 gtk_tree_view_set_model(GTK_TREE_VIEW(doc
->priv
->tag_tree
), NULL
);
1281 /* Clear all contents */
1282 gtk_tree_store_clear(doc
->priv
->tag_store
);
1284 /* add grandparent type iters */
1285 add_top_level_items(doc
);
1287 add_tree_tags(doc
, tags
);
1290 hide_empty_rows(doc
->priv
->tag_store
);
1292 if (sort_mode
== SYMBOLS_SORT_USE_PREVIOUS
)
1293 sort_mode
= doc
->priv
->symbol_list_sort_mode
;
1295 sort_tree(doc
->priv
->tag_store
, sort_mode
== SYMBOLS_SORT_BY_NAME
);
1296 doc
->priv
->symbol_list_sort_mode
= sort_mode
;
1298 /* Re-attach model to view */
1299 gtk_tree_view_set_model(GTK_TREE_VIEW(doc
->priv
->tag_tree
),
1300 GTK_TREE_MODEL(doc
->priv
->tag_store
));
1301 g_object_unref(GTK_TREE_MODEL(doc
->priv
->tag_store
));
1302 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
1308 /* Detects a global tags filetype from the *.lang.* language extension.
1309 * Returns NULL if there was no matching TM language. */
1310 static GeanyFiletype
*detect_global_tags_filetype(const gchar
*utf8_filename
)
1313 gchar
*shortname
= g_strdup(utf8_filename
);
1314 GeanyFiletype
*ft
= NULL
;
1316 tags_ext
= strstr(shortname
, ".tags");
1319 *tags_ext
= '\0'; /* remove .tags extension */
1320 ft
= filetypes_detect_from_extension(shortname
);
1327 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1328 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1329 * the relevant path.
1331 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1332 int symbols_generate_global_tags(int argc
, char **argv
, gboolean want_preprocess
)
1334 /* -E pre-process, -dD output user macros, -p prof info (?),
1335 * -undef remove builtin macros (seems to be needed with FC5 gcc 4.1.1) */
1336 const char pre_process
[] = "gcc -E -dD -p -undef";
1340 /* Create global taglist */
1343 const char *tags_file
= argv
[1];
1347 utf8_fname
= utils_get_utf8_from_locale(tags_file
);
1348 ft
= detect_global_tags_filetype(utf8_fname
);
1353 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file
);
1356 /* load config in case of custom filetypes */
1357 filetypes_load_config(ft
->id
, FALSE
);
1359 /* load ignore list for C/C++ parser */
1360 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
1361 load_c_ignore_tags();
1363 if (want_preprocess
&& (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
))
1364 command
= g_strdup_printf("%s %s", pre_process
, NVL(getenv("CFLAGS"), ""));
1366 command
= NULL
; /* don't preprocess */
1368 geany_debug("Generating %s tags file.", ft
->name
);
1370 status
= tm_workspace_create_global_tags(app
->configdir
, command
,
1371 (const char **) (argv
+ 2),
1372 argc
- 2, tags_file
, ft
->lang
);
1374 symbols_finalize(); /* free c_tags_ignore data */
1377 g_printerr(_("Failed to create tags file, perhaps because no tags "
1384 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv
[0]);
1385 g_printerr(_("Example:\n"
1386 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1387 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv
[0]);
1394 void symbols_show_load_tags_dialog(void)
1397 GtkFileFilter
*filter
;
1399 dialog
= gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets
.window
),
1400 GTK_FILE_CHOOSER_ACTION_OPEN
,
1401 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1402 GTK_STOCK_OPEN
, GTK_RESPONSE_OK
,
1404 gtk_widget_set_name(dialog
, "GeanyDialog");
1405 filter
= gtk_file_filter_new();
1406 gtk_file_filter_set_name(filter
, _("Geany tag files (*.tags)"));
1407 gtk_file_filter_add_pattern(filter
, "*.tags");
1408 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
1410 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_OK
)
1412 GSList
*flist
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog
));
1415 for (item
= flist
; item
!= NULL
; item
= g_slist_next(item
))
1417 gchar
*fname
= item
->data
;
1421 utf8_fname
= utils_get_utf8_from_locale(fname
);
1422 ft
= detect_global_tags_filetype(utf8_fname
);
1424 if (ft
!= NULL
&& symbols_load_global_tags(fname
, ft
))
1425 /* For translators: the first wildcard is the filetype, the second the filename */
1426 ui_set_statusbar(TRUE
, _("Loaded %s tags file '%s'."), ft
->name
, utf8_fname
);
1428 ui_set_statusbar(TRUE
, _("Could not load tags file '%s'."), utf8_fname
);
1433 g_slist_free(flist
);
1435 gtk_widget_destroy(dialog
);
1439 /* Fills a hash table with filetype keys that hold a linked list of filenames. */
1440 static GHashTable
*get_tagfile_hash(const GSList
*file_list
)
1443 GHashTable
*hash
= g_hash_table_new(NULL
, NULL
);
1445 for (node
= file_list
; node
!= NULL
; node
= g_slist_next(node
))
1448 gchar
*fname
= node
->data
;
1449 gchar
*utf8_fname
= utils_get_utf8_from_locale(fname
);
1450 GeanyFiletype
*ft
= detect_global_tags_filetype(utf8_fname
);
1454 if (FILETYPE_ID(ft
) != GEANY_FILETYPES_NONE
)
1456 fnames
= g_hash_table_lookup(hash
, ft
); /* may be NULL */
1457 fnames
= g_list_append(fnames
, fname
);
1458 g_hash_table_insert(hash
, ft
, fnames
);
1461 geany_debug("Unknown filetype for file '%s'.", fname
);
1467 static GHashTable
*init_user_tags(void)
1469 GSList
*file_list
= NULL
, *list
= NULL
;
1470 GHashTable
*lang_hash
;
1473 dir
= utils_build_path(app
->configdir
, "tags", NULL
);
1474 /* create the user tags dir for next time if it doesn't exist */
1475 if (! g_file_test(dir
, G_FILE_TEST_IS_DIR
))
1477 utils_mkdir(dir
, FALSE
);
1479 file_list
= utils_get_file_list_full(dir
, TRUE
, TRUE
, NULL
);
1481 dir
= utils_build_path(app
->datadir
, "tags", NULL
);
1482 list
= utils_get_file_list_full(dir
, TRUE
, TRUE
, NULL
);
1483 file_list
= g_slist_concat(file_list
, list
);
1485 lang_hash
= get_tagfile_hash(file_list
);
1486 /* don't need to delete list contents because they are now used for hash contents */
1487 g_slist_free(file_list
);
1493 static void load_user_tags(filetype_id ft_id
)
1495 static guchar
*tags_loaded
= NULL
;
1496 static GHashTable
*lang_hash
= NULL
;
1499 GeanyFiletype
*ft
= filetypes
[ft_id
];
1501 g_return_if_fail(ft_id
> 0);
1504 tags_loaded
= g_new0(guchar
, filetypes_array
->len
);
1505 if (tags_loaded
[ft_id
])
1507 tags_loaded
[ft_id
] = TRUE
; /* prevent reloading */
1509 if (lang_hash
== NULL
)
1510 lang_hash
= init_user_tags();
1512 fnames
= g_hash_table_lookup(lang_hash
, ft
);
1514 for (node
= fnames
; node
!= NULL
; node
= g_list_next(node
))
1516 const gchar
*fname
= node
->data
;
1518 symbols_load_global_tags(fname
, ft
);
1520 g_list_foreach(fnames
, (GFunc
) g_free
, NULL
);
1521 g_list_free(fnames
);
1522 g_hash_table_remove(lang_hash
, (gpointer
) ft
);
1526 gboolean
symbols_goto_tag(const gchar
*name
, gboolean definition
)
1528 const gint forward_types
= tm_tag_prototype_t
| tm_tag_externvar_t
;
1530 TMTag
*tmtag
= NULL
;
1531 GeanyDocument
*old_doc
= document_get_current();
1533 /* goto tag definition: all except prototypes / forward declarations / externs */
1534 type
= (definition
) ? tm_tag_max_t
- forward_types
: forward_types
;
1536 /* first look in the current document */
1537 if (old_doc
!= NULL
&& old_doc
->tm_file
)
1538 tmtag
= find_work_object_tag(old_doc
->tm_file
, name
, type
);
1540 /* if not found, look in the workspace */
1542 tmtag
= find_workspace_tag(name
, type
);
1546 GeanyDocument
*new_doc
= document_find_by_real_path(
1547 tmtag
->atts
.entry
.file
->work_object
.file_name
);
1549 /* not found in opened document, should open */
1550 if (new_doc
== NULL
)
1552 new_doc
= document_open_file(tmtag
->atts
.entry
.file
->work_object
.file_name
, FALSE
, NULL
, NULL
);
1555 if (navqueue_goto_line(old_doc
, new_doc
, tmtag
->atts
.entry
.line
))
1558 /* if we are here, there was no match and we are beeping ;-) */
1560 if (type
== forward_types
)
1561 ui_set_statusbar(FALSE
, _("Forward declaration \"%s\" not found."), name
);
1563 ui_set_statusbar(FALSE
, _("Definition of \"%s\" not found."), name
);
1568 /* This could perhaps be improved to check for #if, class etc. */
1569 static gint
get_function_fold_number(GeanyDocument
*doc
)
1571 /* for Java the functions are always one fold level above the class scope */
1572 if (FILETYPE_ID(doc
->file_type
) == GEANY_FILETYPES_JAVA
)
1573 return SC_FOLDLEVELBASE
+ 1;
1575 return SC_FOLDLEVELBASE
;
1579 /* Should be used only with symbols_get_current_function. */
1580 static gboolean
current_function_changed(GeanyDocument
*doc
, gint cur_line
, gint fold_level
)
1582 static gint old_line
= -2;
1583 static GeanyDocument
*old_doc
= NULL
;
1584 static gint old_fold_num
= -1;
1585 const gint fold_num
= fold_level
& SC_FOLDLEVELNUMBERMASK
;
1588 /* check if the cached line and file index have changed since last time: */
1589 if (doc
== NULL
|| doc
!= old_doc
)
1592 if (cur_line
== old_line
)
1596 /* if the line has only changed by 1 */
1597 if (abs(cur_line
- old_line
) == 1)
1599 const gint fn_fold
=
1600 get_function_fold_number(doc
);
1601 /* It's the same function if the fold number hasn't changed, or both the new
1602 * and old fold numbers are above the function fold number. */
1604 fold_num
== old_fold_num
||
1605 (old_fold_num
> fn_fold
&& fold_num
> fn_fold
);
1612 /* record current line and file index for next time */
1613 old_line
= cur_line
;
1615 old_fold_num
= fold_num
;
1620 /* Parse the function name up to 2 lines before tag_line.
1621 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1622 * type or argument names can be confused with the function name. */
1623 static gchar
*parse_function_at_line(ScintillaObject
*sci
, gint tag_line
)
1625 gint start
, end
, max_pos
;
1629 switch (sci_get_lexer(sci
))
1631 case SCLEX_RUBY
: fn_style
= SCE_RB_DEFNAME
; break;
1632 case SCLEX_PYTHON
: fn_style
= SCE_P_DEFNAME
; break;
1633 default: fn_style
= SCE_C_IDENTIFIER
; /* several lexers use SCE_C_IDENTIFIER */
1635 start
= sci_get_position_from_line(sci
, tag_line
- 2);
1636 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
1637 while (sci_get_style_at(sci
, start
) != fn_style
1638 && start
< max_pos
) start
++;
1641 while (sci_get_style_at(sci
, end
) == fn_style
1642 && end
< max_pos
) end
++;
1646 cur_tag
= g_malloc(end
- start
+ 1);
1647 sci_get_text_range(sci
, start
, end
, cur_tag
);
1652 /* Parse the function name */
1653 static gchar
*parse_cpp_function_at_line(ScintillaObject
*sci
, gint tag_line
)
1655 gint start
, end
, first_pos
, max_pos
;
1660 first_pos
= end
= sci_get_position_from_line(sci
, tag_line
);
1661 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
1663 /* goto the begin of function body */
1664 while (end
< max_pos
&&
1665 (tmp
= sci_get_char_at(sci
, end
)) != '{' &&
1667 if (tmp
== 0) end
--;
1669 /* go back to the end of function identifier */
1670 while (end
> 0 && end
> first_pos
- 500 &&
1671 (tmp
= sci_get_char_at(sci
, end
)) != '(' &&
1674 if (end
< 0) end
= 0;
1676 /* skip whitespaces between identifier and ( */
1677 while (end
> 0 && isspace(sci_get_char_at(sci
, end
))) end
--;
1681 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1682 while (start
>= 0 && ((tmp
= sci_get_style_at(sci
, start
)) == SCE_C_IDENTIFIER
1683 || tmp
== SCE_C_GLOBALCLASS
1684 || (c
= sci_get_char_at(sci
, start
)) == '~'
1687 if (start
!= 0 && start
< end
) start
++; /* correct for last non-matching char */
1689 if (start
== end
) return NULL
;
1690 cur_tag
= g_malloc(end
- start
+ 2);
1691 sci_get_text_range(sci
, start
, end
+ 1, cur_tag
);
1696 /* Sets *tagname to point at the current function or tag name.
1697 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
1698 * call to this function.
1699 * Returns: line number of the current tag, or -1 if unknown. */
1700 gint
symbols_get_current_function(GeanyDocument
*doc
, const gchar
**tagname
)
1702 static gint tag_line
= -1;
1703 static gchar
*cur_tag
= NULL
;
1706 TMWorkObject
*tm_file
;
1708 if (doc
== NULL
) /* reset current function */
1710 current_function_changed(NULL
, -1, -1);
1712 cur_tag
= g_strdup(_("unknown"));
1713 if (tagname
!= NULL
)
1719 line
= sci_get_current_line(doc
->editor
->sci
);
1720 fold_level
= sci_get_fold_level(doc
->editor
->sci
, line
);
1721 /* check if the cached line and file index have changed since last time: */
1722 if (! current_function_changed(doc
, line
, fold_level
))
1724 /* we can assume same current function as before */
1728 g_free(cur_tag
); /* free the old tag, it will be replaced. */
1730 /* if line is at base fold level, we're not in a function */
1731 if ((fold_level
& SC_FOLDLEVELNUMBERMASK
) == SC_FOLDLEVELBASE
)
1733 cur_tag
= g_strdup(_("unknown"));
1738 tm_file
= doc
->tm_file
;
1740 /* if the document has no changes, get the previous function name from TM */
1741 if (! doc
->changed
&& tm_file
!= NULL
&& tm_file
->tags_array
!= NULL
)
1743 const TMTag
*tag
= (const TMTag
*) tm_get_current_function(tm_file
->tags_array
, line
);
1748 tmp
= tag
->atts
.entry
.scope
;
1749 cur_tag
= tmp
? g_strconcat(tmp
, "::", tag
->name
, NULL
) : g_strdup(tag
->name
);
1751 tag_line
= tag
->atts
.entry
.line
;
1756 /* parse the current function name here because TM line numbers may have changed,
1757 * and it would take too long to reparse the whole file. */
1758 if (doc
->file_type
!= NULL
&& doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
1760 const gint fn_fold
= get_function_fold_number(doc
);
1763 do /* find the top level fold point */
1765 tag_line
= sci_get_fold_parent(doc
->editor
->sci
, tag_line
);
1766 fold_level
= sci_get_fold_level(doc
->editor
->sci
, tag_line
);
1767 } while (tag_line
>= 0 &&
1768 (fold_level
& SC_FOLDLEVELNUMBERMASK
) != fn_fold
);
1772 if (sci_get_lexer(doc
->editor
->sci
) == SCLEX_CPP
)
1773 cur_tag
= parse_cpp_function_at_line(doc
->editor
->sci
, tag_line
);
1775 cur_tag
= parse_function_at_line(doc
->editor
->sci
, tag_line
);
1777 if (cur_tag
!= NULL
)
1785 cur_tag
= g_strdup(_("unknown"));
1792 static void on_symbol_tree_sort_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
1794 gint sort_mode
= GPOINTER_TO_INT(user_data
);
1795 GeanyDocument
*doc
= document_get_current();
1797 if (ignore_callback
)
1801 doc
->has_tags
= symbols_recreate_tag_list(doc
, sort_mode
);
1805 static void on_symbol_tree_menu_show(GtkWidget
*widget
,
1808 GeanyDocument
*doc
= document_get_current();
1811 enable
= doc
&& doc
->has_tags
;
1812 gtk_widget_set_sensitive(symbol_menu
.sort_by_name
, enable
);
1813 gtk_widget_set_sensitive(symbol_menu
.sort_by_appearance
, enable
);
1814 gtk_widget_set_sensitive(symbol_menu
.expand_all
, enable
);
1815 gtk_widget_set_sensitive(symbol_menu
.collapse_all
, enable
);
1820 ignore_callback
= TRUE
;
1822 if (doc
->priv
->symbol_list_sort_mode
== SYMBOLS_SORT_BY_NAME
)
1823 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_name
), TRUE
);
1825 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_appearance
), TRUE
);
1827 ignore_callback
= FALSE
;
1831 static void on_expand_collapse(GtkWidget
*widget
, gpointer user_data
)
1833 gboolean expand
= utils_str_equal(user_data
, GTK_STOCK_ADD
);
1834 GeanyDocument
*doc
= document_get_current();
1839 g_return_if_fail(doc
->priv
->tag_tree
);
1842 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
1844 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
1848 static void create_taglist_popup_menu(void)
1850 GtkWidget
*item
, *menu
;
1852 tv
.popup_taglist
= menu
= gtk_menu_new();
1854 symbol_menu
.expand_all
= item
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
1855 gtk_widget_show(item
);
1856 gtk_container_add(GTK_CONTAINER(menu
), item
);
1857 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GTK_STOCK_ADD
);
1859 symbol_menu
.collapse_all
= item
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
1860 gtk_widget_show(item
);
1861 gtk_container_add(GTK_CONTAINER(menu
), item
);
1862 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GTK_STOCK_REMOVE
);
1864 item
= gtk_separator_menu_item_new();
1865 gtk_widget_show(item
);
1866 gtk_container_add(GTK_CONTAINER(menu
), item
);
1868 symbol_menu
.sort_by_name
= item
= gtk_radio_menu_item_new_with_mnemonic(NULL
,
1869 _("Sort by _Name"));
1870 gtk_widget_show(item
);
1871 gtk_container_add(GTK_CONTAINER(menu
), item
);
1872 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
1873 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME
));
1875 symbol_menu
.sort_by_appearance
= item
= gtk_radio_menu_item_new_with_mnemonic_from_widget(
1876 GTK_RADIO_MENU_ITEM(item
), _("Sort by _Appearance"));
1877 gtk_widget_show(item
);
1878 gtk_container_add(GTK_CONTAINER(menu
), item
);
1879 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
1880 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE
));
1882 g_signal_connect(menu
, "show", G_CALLBACK(on_symbol_tree_menu_show
), NULL
);
1884 sidebar_add_common_menu_items(GTK_MENU(menu
));
1888 static void on_document_save(G_GNUC_UNUSED GObject
*object
, GeanyDocument
*doc
)
1890 g_return_if_fail(NZV(doc
->real_path
));
1892 if (utils_str_equal(doc
->real_path
,
1893 utils_build_path(app
->configdir
, "ignore.tags", NULL
)))
1894 load_c_ignore_tags();
1898 void symbols_init(void)
1900 create_taglist_popup_menu();
1902 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "ignore.tags", NULL
),
1904 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
1908 void symbols_finalize(void)
1910 g_strfreev(html_entities
);
1911 g_strfreev(c_tags_ignore
);