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.
26 * Tag-related functions.
30 * Symbol Tree and TagManager-related convenience functions.
31 * TagManager parses tags for each document, and also adds them to the workspace (session).
32 * Global tags are lists of tags for each filetype, loaded when a document with a
33 * matching filetype is first loaded.
46 #include "filetypes.h"
47 #include "encodings.h"
49 #include "documentprivate.h"
51 #include "msgwindow.h"
57 #include "sciwrappers.h"
60 const guint TM_GLOBAL_TYPE_MASK
=
61 tm_tag_class_t
| tm_tag_enum_t
| tm_tag_interface_t
|
62 tm_tag_struct_t
| tm_tag_typedef_t
| tm_tag_union_t
| tm_tag_namespace_t
;
65 static gchar
**html_entities
= NULL
;
70 const gchar
*tag_file
;
73 /* Check before adding any more tags files, usually they should be downloaded separately. */
74 enum /* Geany tag files */
85 static TagFileInfo tag_file_info
[GTF_MAX
] =
88 {FALSE
, "pascal.tags"},
90 {FALSE
, "html_entities.tags"},
91 {FALSE
, "latex.tags"},
92 {FALSE
, "python.tags"}
95 static GPtrArray
*top_level_iter_names
= NULL
;
99 GtkWidget
*expand_all
;
100 GtkWidget
*collapse_all
;
101 GtkWidget
*sort_by_name
;
102 GtkWidget
*sort_by_appearance
;
104 symbol_menu
= {NULL
, NULL
, NULL
, NULL
};
107 static void html_tags_loaded(void);
108 static void load_user_tags(filetype_id ft_id
);
110 /* get the tags_ignore list, exported by tagmanager's options.c */
111 extern gchar
**c_tags_ignore
;
113 /* ignore certain tokens when parsing C-like syntax.
114 * Also works for reloading. */
115 static void load_c_ignore_tags(void)
117 gchar
*path
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
"ignore.tags", NULL
);
120 if (g_file_get_contents(path
, &content
, NULL
, NULL
))
122 g_strfreev(c_tags_ignore
);
123 c_tags_ignore
= g_strsplit_set(content
, " \n\r", -1);
130 void symbols_reload_config_files(void)
132 load_c_ignore_tags();
136 static gsize
get_tag_count(void)
138 GPtrArray
*tags
= tm_get_workspace()->global_tags
;
139 gsize count
= tags
? tags
->len
: 0;
145 /* wrapper for tm_workspace_load_global_tags().
146 * note that the tag count only counts new global tags added - if a tag has the same name,
147 * currently it replaces the existing tag, so loading a file twice will say 0 tags the 2nd time. */
148 static gboolean
symbols_load_global_tags(const gchar
*tags_file
, GeanyFiletype
*ft
)
151 gsize old_tag_count
= get_tag_count();
153 result
= tm_workspace_load_global_tags(tags_file
, ft
->lang
);
156 geany_debug("Loaded %s (%s), %u tag(s).", tags_file
, ft
->name
,
157 (guint
) (get_tag_count() - old_tag_count
));
163 /* Ensure that the global tags file(s) for the file_type_idx filetype is loaded.
164 * This provides autocompletion, calltips, etc. */
165 void symbols_global_tags_loaded(gint file_type_idx
)
170 /* load ignore list for C/C++ parser */
171 if ((file_type_idx
== GEANY_FILETYPES_C
|| file_type_idx
== GEANY_FILETYPES_CPP
) &&
172 c_tags_ignore
== NULL
)
174 load_c_ignore_tags();
177 if (cl_options
.ignore_global_tags
|| app
->tm_workspace
== NULL
)
180 /* load config in case of custom filetypes */
181 filetypes_load_config(file_type_idx
, FALSE
);
183 load_user_tags(file_type_idx
);
185 switch (file_type_idx
)
187 case GEANY_FILETYPES_PHP
:
188 case GEANY_FILETYPES_HTML
:
191 switch (file_type_idx
)
193 case GEANY_FILETYPES_CPP
:
194 symbols_global_tags_loaded(GEANY_FILETYPES_C
); /* load C global tags */
195 /* no C++ tagfile yet */
197 case GEANY_FILETYPES_C
: tag_type
= GTF_C
; break;
198 case GEANY_FILETYPES_PASCAL
:tag_type
= GTF_PASCAL
; break;
199 case GEANY_FILETYPES_PHP
: tag_type
= GTF_PHP
; break;
200 case GEANY_FILETYPES_LATEX
: tag_type
= GTF_LATEX
; break;
201 case GEANY_FILETYPES_PYTHON
:tag_type
= GTF_PYTHON
; break;
205 tfi
= &tag_file_info
[tag_type
];
207 if (! tfi
->tags_loaded
)
209 gchar
*fname
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, tfi
->tag_file
, NULL
);
211 symbols_load_global_tags(fname
, filetypes
[file_type_idx
]);
212 tfi
->tags_loaded
= TRUE
;
218 /* HTML tagfile is just a list of entities for autocompletion (e.g. '&') */
219 static void html_tags_loaded(void)
223 if (cl_options
.ignore_global_tags
)
226 tfi
= &tag_file_info
[GTF_HTML_ENTITIES
];
227 if (! tfi
->tags_loaded
)
229 gchar
*file
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, tfi
->tag_file
, NULL
);
231 html_entities
= utils_read_file_in_array(file
);
232 tfi
->tags_loaded
= TRUE
;
238 GString
*symbols_find_tags_as_string(GPtrArray
*tags_array
, guint tag_types
, gint lang
)
246 g_return_val_if_fail(tags_array
!= NULL
, NULL
);
248 typedefs
= tm_tags_extract(tags_array
, tag_types
);
250 if ((typedefs
) && (typedefs
->len
> 0))
252 s
= g_string_sized_new(typedefs
->len
* 10);
253 for (j
= 0; j
< typedefs
->len
; ++j
)
255 tag
= TM_TAG(typedefs
->pdata
[j
]);
256 /* tag->atts.file.lang contains (for some reason) the line of the tag if
257 * tag->atts.entry.file is not NULL */
258 tag_lang
= (tag
->atts
.entry
.file
) ? tag
->atts
.entry
.file
->lang
: tag
->atts
.file
.lang
;
260 /* the check for tag_lang == lang is necessary to avoid wrong type colouring of
261 * e.g. PHP classes in C++ files
262 * lang = -2 disables the check */
263 if (tag
->name
&& (tag_lang
== lang
|| lang
== -2))
266 g_string_append_c(s
, ' ');
267 g_string_append(s
, tag
->name
);
272 g_ptr_array_free(typedefs
, TRUE
);
277 /** Gets the context separator used by the tag manager for a particular file
279 * @param ft_id File type identifier.
280 * @return The context separator string.
284 const gchar
*symbols_get_context_separator(gint ft_id
)
288 case GEANY_FILETYPES_C
: /* for C++ .h headers or C structs */
289 case GEANY_FILETYPES_CPP
:
290 case GEANY_FILETYPES_GLSL
: /* for structs */
291 /*case GEANY_FILETYPES_RUBY:*/ /* not sure what to use atm*/
294 /* avoid confusion with other possible separators in group/section name */
295 case GEANY_FILETYPES_CONF
:
296 case GEANY_FILETYPES_REST
:
305 GString
*symbols_get_macro_list(gint lang
)
313 if (app
->tm_workspace
->work_objects
== NULL
)
316 ftags
= g_ptr_array_sized_new(50);
317 words
= g_string_sized_new(200);
319 for (j
= 0; j
< app
->tm_workspace
->work_objects
->len
; j
++)
323 tags
= tm_tags_extract(TM_WORK_OBJECT(app
->tm_workspace
->work_objects
->pdata
[j
])->tags_array
,
324 tm_tag_enum_t
| tm_tag_variable_t
| tm_tag_macro_t
| tm_tag_macro_with_arg_t
);
327 for (i
= 0; ((i
< tags
->len
) && (i
< editor_prefs
.autocompletion_max_entries
)); ++i
)
329 tag
= TM_TAG(tags
->pdata
[i
]);
330 tag_lang
= (tag
->atts
.entry
.file
) ?
331 tag
->atts
.entry
.file
->lang
: tag
->atts
.file
.lang
;
333 if (tag_lang
== lang
)
334 g_ptr_array_add(ftags
, (gpointer
) tags
->pdata
[i
]);
336 g_ptr_array_free(tags
, TRUE
);
342 g_ptr_array_free(ftags
, TRUE
);
343 g_string_free(words
, TRUE
);
347 tm_tags_sort(ftags
, NULL
, FALSE
);
348 for (j
= 0; j
< ftags
->len
; j
++)
351 g_string_append_c(words
, '\n');
352 g_string_append(words
, TM_TAG(ftags
->pdata
[j
])->name
);
354 g_ptr_array_free(ftags
, TRUE
);
359 /* Note: if tags is sorted, we can use bsearch or tm_tags_find() to speed this up. */
361 symbols_find_tm_tag(const GPtrArray
*tags
, const gchar
*tag_name
)
364 g_return_val_if_fail(tags
!= NULL
, NULL
);
366 for (i
= 0; i
< tags
->len
; ++i
)
368 if (utils_str_equal(TM_TAG(tags
->pdata
[i
])->name
, tag_name
))
369 return TM_TAG(tags
->pdata
[i
]);
375 static TMTag
*find_work_object_tag(const TMWorkObject
*workobj
,
376 const gchar
*tag_name
, gint type
)
381 if (G_LIKELY(workobj
!= NULL
))
383 tags
= tm_tags_extract(workobj
->tags_array
, type
);
386 tmtag
= symbols_find_tm_tag(tags
, tag_name
);
388 g_ptr_array_free(tags
, TRUE
);
394 return NULL
; /* not found */
398 static TMTag
*find_workspace_tag(const gchar
*tag_name
, gint type
)
401 const GPtrArray
*work_objects
= NULL
;
403 if (app
->tm_workspace
!= NULL
)
404 work_objects
= app
->tm_workspace
->work_objects
;
406 if (work_objects
!= NULL
)
408 for (j
= 0; j
< work_objects
->len
; j
++)
410 TMWorkObject
*workobj
= TM_WORK_OBJECT(work_objects
->pdata
[j
]);
413 tmtag
= find_work_object_tag(workobj
, tag_name
, type
);
418 return NULL
; /* not found */
422 const gchar
**symbols_get_html_entities(void)
424 if (html_entities
== NULL
)
425 html_tags_loaded(); /* if not yet created, force creation of the array but shouldn't occur */
427 return (const gchar
**) html_entities
;
431 /* sort by name, then line */
432 static gint
compare_symbol(const TMTag
*tag_a
, const TMTag
*tag_b
)
436 if (tag_a
== NULL
|| tag_b
== NULL
)
439 if (tag_a
->name
== NULL
)
440 return -(tag_a
->name
!= tag_b
->name
);
442 if (tag_b
->name
== NULL
)
443 return tag_a
->name
!= tag_b
->name
;
445 ret
= strcmp(tag_a
->name
, tag_b
->name
);
448 return tag_a
->atts
.entry
.line
- tag_b
->atts
.entry
.line
;
454 /* sort by line only */
455 static gint
compare_symbol_lines(gconstpointer a
, gconstpointer b
)
457 const TMTag
*tag_a
= TM_TAG(a
);
458 const TMTag
*tag_b
= TM_TAG(b
);
460 if (a
== NULL
|| b
== NULL
)
463 return tag_a
->atts
.entry
.line
- tag_b
->atts
.entry
.line
;
467 static GList
*get_tag_list(GeanyDocument
*doc
, guint tag_types
)
469 GList
*tag_names
= NULL
;
473 g_return_val_if_fail(doc
, NULL
);
475 if (! doc
->tm_file
|| ! doc
->tm_file
->tags_array
)
478 for (i
= 0; i
< doc
->tm_file
->tags_array
->len
; ++i
)
480 tag
= TM_TAG(doc
->tm_file
->tags_array
->pdata
[i
]);
481 if (G_UNLIKELY(tag
== NULL
))
484 if (tag
->type
& tag_types
)
486 tag_names
= g_list_append(tag_names
, tag
);
489 tag_names
= g_list_sort(tag_names
, compare_symbol_lines
);
494 /* amount of types in the symbol list (currently max. 8 are used) */
495 #define MAX_SYMBOL_TYPES (sizeof(tv_iters) / sizeof(GtkTreeIter))
497 struct TreeviewSymbols
499 GtkTreeIter tag_function
;
500 GtkTreeIter tag_class
;
501 GtkTreeIter tag_macro
;
502 GtkTreeIter tag_member
;
503 GtkTreeIter tag_variable
;
504 GtkTreeIter tag_namespace
;
505 GtkTreeIter tag_struct
;
506 GtkTreeIter tag_type
;
507 GtkTreeIter tag_other
;
511 static void init_tag_iters(void)
513 /* init all GtkTreeIters with -1 to make them invalid to avoid crashes when switching between
514 * filetypes(e.g. config file to Python crashes Geany without this) */
515 tv_iters
.tag_function
.stamp
= -1;
516 tv_iters
.tag_class
.stamp
= -1;
517 tv_iters
.tag_member
.stamp
= -1;
518 tv_iters
.tag_macro
.stamp
= -1;
519 tv_iters
.tag_variable
.stamp
= -1;
520 tv_iters
.tag_namespace
.stamp
= -1;
521 tv_iters
.tag_struct
.stamp
= -1;
522 tv_iters
.tag_type
.stamp
= -1;
523 tv_iters
.tag_other
.stamp
= -1;
527 static GdkPixbuf
*get_tag_icon(const gchar
*icon_name
)
529 static GtkIconTheme
*icon_theme
= NULL
;
532 if (G_UNLIKELY(icon_theme
== NULL
))
535 gchar
*path
= g_strconcat(GEANY_DATADIR
, "/icons", NULL
);
537 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &x
, &y
);
538 icon_theme
= gtk_icon_theme_get_default();
540 gtk_icon_theme_append_search_path(icon_theme
, "share\\icons");
542 gtk_icon_theme_append_search_path(icon_theme
, path
);
546 return gtk_icon_theme_load_icon(icon_theme
, icon_name
, x
, 0, NULL
);
550 /* Adds symbol list groups in (iter*, title) pairs.
551 * The list must be ended with NULL. */
552 static void G_GNUC_NULL_TERMINATED
553 tag_list_add_groups(GtkTreeStore
*tree_store
, ...)
558 g_return_if_fail(top_level_iter_names
);
560 va_start(args
, tree_store
);
561 for (; iter
= va_arg(args
, GtkTreeIter
*), iter
!= NULL
;)
563 gchar
*title
= va_arg(args
, gchar
*);
564 gchar
*icon_name
= va_arg(args
, gchar
*);
565 GdkPixbuf
*icon
= NULL
;
569 icon
= get_tag_icon(icon_name
);
572 g_assert(title
!= NULL
);
573 g_ptr_array_add(top_level_iter_names
, title
);
575 gtk_tree_store_append(tree_store
, iter
, NULL
);
577 if (G_IS_OBJECT(icon
))
579 gtk_tree_store_set(tree_store
, iter
, SYMBOLS_COLUMN_ICON
, icon
, -1);
580 g_object_unref(icon
);
582 gtk_tree_store_set(tree_store
, iter
, SYMBOLS_COLUMN_NAME
, title
, -1);
588 static void add_top_level_items(GeanyDocument
*doc
)
590 filetype_id ft_id
= doc
->file_type
->id
;
591 GtkTreeStore
*tag_store
= doc
->priv
->tag_store
;
593 if (top_level_iter_names
== NULL
)
594 top_level_iter_names
= g_ptr_array_new();
596 g_ptr_array_set_size(top_level_iter_names
, 0);
602 case GEANY_FILETYPES_DIFF
:
604 tag_list_add_groups(tag_store
,
605 &(tv_iters
.tag_function
), _("Files"), NULL
, NULL
);
608 case GEANY_FILETYPES_DOCBOOK
:
610 tag_list_add_groups(tag_store
,
611 &(tv_iters
.tag_function
), _("Chapter"), NULL
,
612 &(tv_iters
.tag_class
), _("Section"), NULL
,
613 &(tv_iters
.tag_member
), _("Sect1"), NULL
,
614 &(tv_iters
.tag_macro
), _("Sect2"), NULL
,
615 &(tv_iters
.tag_variable
), _("Sect3"), NULL
,
616 &(tv_iters
.tag_struct
), _("Appendix"), NULL
,
617 &(tv_iters
.tag_other
), _("Other"), NULL
,
621 case GEANY_FILETYPES_HASKELL
:
622 tag_list_add_groups(tag_store
,
623 &tv_iters
.tag_namespace
, _("Module"), NULL
,
624 &tv_iters
.tag_type
, _("Types"), NULL
,
625 &tv_iters
.tag_macro
, _("Type constructors"), NULL
,
626 &tv_iters
.tag_function
, _("Functions"), "classviewer-method",
629 case GEANY_FILETYPES_CONF
:
630 tag_list_add_groups(tag_store
,
631 &tv_iters
.tag_namespace
, _("Sections"), "classviewer-other",
632 &tv_iters
.tag_macro
, _("Keys"), "classviewer-var",
635 case GEANY_FILETYPES_NSIS
:
636 tag_list_add_groups(tag_store
,
637 &tv_iters
.tag_namespace
, _("Sections"), "classviewer-other",
638 &tv_iters
.tag_function
, _("Functions"), "classviewer-method",
639 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
642 case GEANY_FILETYPES_LATEX
:
644 tag_list_add_groups(tag_store
,
645 &(tv_iters
.tag_function
), _("Command"), NULL
,
646 &(tv_iters
.tag_class
), _("Environment"), NULL
,
647 &(tv_iters
.tag_member
), _("Section"), NULL
,
648 &(tv_iters
.tag_macro
), _("Subsection"), NULL
,
649 &(tv_iters
.tag_variable
), _("Subsubsection"), NULL
,
650 &(tv_iters
.tag_struct
), _("Label"), NULL
,
651 &(tv_iters
.tag_namespace
), _("Chapter"), NULL
,
652 &(tv_iters
.tag_other
), _("Other"), NULL
,
656 case GEANY_FILETYPES_MATLAB
:
658 tag_list_add_groups(tag_store
,
659 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
660 &(tv_iters
.tag_struct
), _("Structures"), "classviewer-struct",
664 case GEANY_FILETYPES_R
:
666 tag_list_add_groups(tag_store
,
667 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
668 &(tv_iters
.tag_struct
), _("Other"), NULL
,
672 case GEANY_FILETYPES_PERL
:
674 tag_list_add_groups(tag_store
,
675 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
676 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
677 &(tv_iters
.tag_macro
), _("Labels"), NULL
,
678 &(tv_iters
.tag_type
), _("Constants"), NULL
,
679 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
683 case GEANY_FILETYPES_PHP
:
685 tag_list_add_groups(tag_store
,
686 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
687 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
688 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
689 &(tv_iters
.tag_macro
), _("Constants"), "classviewer-macro",
690 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
694 case GEANY_FILETYPES_HTML
:
696 tag_list_add_groups(tag_store
,
697 &(tv_iters
.tag_function
), _("Functions"), NULL
,
698 &(tv_iters
.tag_member
), _("Anchors"), NULL
,
699 &(tv_iters
.tag_namespace
), _("H1 Headings"), NULL
,
700 &(tv_iters
.tag_class
), _("H2 Headings"), NULL
,
701 &(tv_iters
.tag_variable
), _("H3 Headings"), NULL
,
705 case GEANY_FILETYPES_CSS
:
707 tag_list_add_groups(tag_store
,
708 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
709 &(tv_iters
.tag_variable
), _("ID Selectors"), "classviewer-var",
710 &(tv_iters
.tag_struct
), _("Type Selectors"), "classviewer-struct", NULL
);
713 case GEANY_FILETYPES_REST
:
714 case GEANY_FILETYPES_TXT2TAGS
:
715 case GEANY_FILETYPES_ABC
:
717 tag_list_add_groups(tag_store
,
718 &(tv_iters
.tag_namespace
), _("Chapter"), NULL
,
719 &(tv_iters
.tag_member
), _("Section"), NULL
,
720 &(tv_iters
.tag_macro
), _("Subsection"), NULL
,
721 &(tv_iters
.tag_variable
), _("Subsubsection"), NULL
,
725 case GEANY_FILETYPES_RUBY
:
727 tag_list_add_groups(tag_store
,
728 &(tv_iters
.tag_namespace
), _("Modules"), NULL
,
729 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
730 &(tv_iters
.tag_member
), _("Singletons"), "classviewer-struct",
731 &(tv_iters
.tag_function
), _("Methods"), "classviewer-method",
735 case GEANY_FILETYPES_TCL
:
737 tag_list_add_groups(tag_store
,
738 &(tv_iters
.tag_namespace
), _("Namespaces"), "classviewer-namespace",
739 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
740 &(tv_iters
.tag_member
), _("Methods"), "classviewer-method",
741 &(tv_iters
.tag_function
), _("Procedures"), "classviewer-other",
745 case GEANY_FILETYPES_PYTHON
:
747 tag_list_add_groups(tag_store
,
748 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
749 &(tv_iters
.tag_member
), _("Methods"), "classviewer-macro",
750 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
751 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
752 &(tv_iters
.tag_namespace
), _("Imports"), "classviewer-namespace",
756 case GEANY_FILETYPES_VHDL
:
758 tag_list_add_groups(tag_store
,
759 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
760 &(tv_iters
.tag_class
), _("Entities"), "classviewer-class",
761 &(tv_iters
.tag_struct
), _("Architectures"), "classviewer-struct",
762 &(tv_iters
.tag_type
), _("Types"), "classviewer-other",
763 &(tv_iters
.tag_function
), _("Functions / Procedures"), "classviewer-method",
764 &(tv_iters
.tag_variable
), _("Variables / Signals"), "classviewer-var",
765 &(tv_iters
.tag_member
), _("Processes / Components"), "classviewer-member",
766 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
770 case GEANY_FILETYPES_VERILOG
:
772 tag_list_add_groups(tag_store
,
773 &(tv_iters
.tag_type
), _("Events"), "classviewer-macro",
774 &(tv_iters
.tag_class
), _("Modules"), "classviewer-class",
775 &(tv_iters
.tag_function
), _("Functions / Tasks"), "classviewer-method",
776 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
777 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
781 case GEANY_FILETYPES_JAVA
:
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
), _("Methods"), "classviewer-method",
788 &(tv_iters
.tag_member
), _("Members"), "classviewer-member",
789 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
793 case GEANY_FILETYPES_AS
:
795 tag_list_add_groups(tag_store
,
796 &(tv_iters
.tag_namespace
), _("Package"), "classviewer-namespace",
797 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
798 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
799 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
800 &(tv_iters
.tag_member
), _("Properties"), "classviewer-member",
801 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
802 &(tv_iters
.tag_macro
), _("Constants"), "classviewer-macro",
803 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
807 case GEANY_FILETYPES_HAXE
:
809 tag_list_add_groups(tag_store
,
810 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
811 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
812 &(tv_iters
.tag_function
), _("Methods"), "classviewer-method",
813 &(tv_iters
.tag_type
), _("Types"), "classviewer-macro",
814 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
815 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
819 case GEANY_FILETYPES_BASIC
:
821 tag_list_add_groups(tag_store
,
822 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
823 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
824 &(tv_iters
.tag_macro
), _("Constants"), "classviewer-macro",
825 &(tv_iters
.tag_struct
), _("Types"), "classviewer-namespace",
826 &(tv_iters
.tag_namespace
), _("Labels"), "classviewer-member",
827 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
831 case GEANY_FILETYPES_F77
:
832 case GEANY_FILETYPES_FORTRAN
:
834 tag_list_add_groups(tag_store
,
835 &(tv_iters
.tag_namespace
), _("Module"), "classviewer-class",
836 &(tv_iters
.tag_struct
), _("Interfaces"), "classviewer-struct",
837 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
838 &(tv_iters
.tag_member
), _("Subroutines"), "classviewer-method",
839 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
840 &(tv_iters
.tag_type
), _("Types"), "classviewer-namespace",
841 &(tv_iters
.tag_macro
), _("Blocks"), "classviewer-member",
842 &(tv_iters
.tag_other
), _("Other"), "classviewer-other",
846 case GEANY_FILETYPES_ASM
:
848 tag_list_add_groups(tag_store
,
849 &(tv_iters
.tag_namespace
), _("Labels"), "classviewer-namespace",
850 &(tv_iters
.tag_function
), _("Macros"), "classviewer-method",
851 &(tv_iters
.tag_macro
), _("Defines"), "classviewer-macro",
852 &(tv_iters
.tag_struct
), _("Types"), "classviewer-struct",
856 case GEANY_FILETYPES_MAKE
:
857 tag_list_add_groups(tag_store
,
858 &tv_iters
.tag_function
, _("Targets"), "classviewer-method",
859 &tv_iters
.tag_macro
, _("Macros"), "classviewer-macro",
862 case GEANY_FILETYPES_D
:
865 if (ft_id
== GEANY_FILETYPES_D
)
866 tag_list_add_groups(tag_store
,
867 &(tv_iters
.tag_namespace
), _("Module"), NULL
, NULL
);
869 tag_list_add_groups(tag_store
,
870 &(tv_iters
.tag_namespace
), _("Namespaces"), "classviewer-namespace", NULL
);
872 tag_list_add_groups(tag_store
,
873 &(tv_iters
.tag_class
), _("Classes"), "classviewer-class",
874 &(tv_iters
.tag_function
), _("Functions"), "classviewer-method",
875 &(tv_iters
.tag_member
), _("Members"), "classviewer-member",
876 &(tv_iters
.tag_struct
), _("Structs"), "classviewer-struct",
877 &(tv_iters
.tag_type
), _("Typedefs / Enums"), "classviewer-struct",
880 if (ft_id
!= GEANY_FILETYPES_D
)
882 tag_list_add_groups(tag_store
,
883 &(tv_iters
.tag_macro
), _("Macros"), "classviewer-macro", NULL
);
885 tag_list_add_groups(tag_store
,
886 &(tv_iters
.tag_variable
), _("Variables"), "classviewer-var",
887 &(tv_iters
.tag_other
), _("Other"), "classviewer-other", NULL
);
893 /* the following code surely can be improved, at the moment it collects some iters
894 * for removal and after that the actual removal is done. I didn't find a way to find and remove
895 * an empty row in one loop (next iter fails then) */
896 static void hide_empty_rows(GtkTreeStore
*store
)
898 GtkTreeIter iter
, *iters
[MAX_SYMBOL_TYPES
] = { NULL
};
901 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store
), &iter
))
902 return; /* stop when first iter is invalid, i.e. no elements */
904 do /* first collect the iters we need to delete empty rows */
906 if (! gtk_tree_model_iter_has_child(GTK_TREE_MODEL(store
), &iter
))
907 iters
[i
++] = gtk_tree_iter_copy(&iter
);
908 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store
), &iter
));
910 /* now actually delete the collected iters */
911 for (i
= 0; i
< MAX_SYMBOL_TYPES
; i
++)
913 if (G_UNLIKELY(iters
[i
] == NULL
))
915 gtk_tree_store_remove(store
, iters
[i
]);
916 gtk_tree_iter_free(iters
[i
]);
921 static const gchar
*get_symbol_name(GeanyDocument
*doc
, const TMTag
*tag
, gboolean found_parent
)
924 const gchar
*scope
= tag
->atts
.entry
.scope
;
925 static GString
*buffer
= NULL
; /* buffer will be small so we can keep it for reuse */
926 gboolean doc_is_utf8
= FALSE
;
928 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
929 * for None at this point completely */
930 if (utils_str_equal(doc
->encoding
, "UTF-8") ||
931 utils_str_equal(doc
->encoding
, "None"))
935 utf8_name
= encodings_convert_to_utf8_from_charset(tag
->name
,
936 (gsize
) -1, doc
->encoding
, TRUE
);
938 utf8_name
= tag
->name
;
940 if (utf8_name
== NULL
)
944 buffer
= g_string_new(NULL
);
946 g_string_truncate(buffer
, 0);
948 /* check first char of scope is a wordchar */
949 if (!found_parent
&& scope
&&
950 strpbrk(scope
, GEANY_WORDCHARS
) == scope
)
952 const gchar
*sep
= symbols_get_context_separator(FILETYPE_ID(doc
->file_type
));
954 g_string_append(buffer
, scope
);
955 g_string_append(buffer
, sep
);
957 g_string_append(buffer
, utf8_name
);
962 g_string_append_printf(buffer
, " [%lu]", tag
->atts
.entry
.line
);
968 static gchar
*get_symbol_tooltip(GeanyDocument
*doc
, const TMTag
*tag
)
970 gchar
*utf8_name
= editor_get_calltip_text(doc
->editor
, tag
);
972 /* encodings_convert_to_utf8_from_charset() fails with charset "None", so skip conversion
973 * for None at this point completely */
974 if (utf8_name
!= NULL
&&
975 ! utils_str_equal(doc
->encoding
, "UTF-8") &&
976 ! utils_str_equal(doc
->encoding
, "None"))
979 encodings_convert_to_utf8_from_charset(utf8_name
, (gsize
) -1, doc
->encoding
, TRUE
));
982 if (utf8_name
!= NULL
)
983 setptr(utf8_name
, g_markup_escape_text(utf8_name
, -1));
989 /* find the last word in "foo::bar::blah", e.g. "blah" */
990 static const gchar
*get_parent_name(const TMTag
*tag
, filetype_id ft_id
)
992 const gchar
*scope
= tag
->atts
.entry
.scope
;
993 const gchar
*separator
= symbols_get_context_separator(ft_id
);
994 const gchar
*str
, *ptr
;
1003 ptr
= strstr(str
, separator
);
1006 str
= ptr
+ strlen(separator
);
1012 return NZV(str
) ? str
: NULL
;
1016 static GtkTreeIter
*get_tag_type_iter(TMTagType tag_type
, filetype_id ft_id
)
1018 GtkTreeIter
*iter
= NULL
;
1022 case tm_tag_prototype_t
:
1023 case tm_tag_method_t
:
1024 case tm_tag_function_t
:
1026 iter
= &tv_iters
.tag_function
;
1029 case tm_tag_macro_t
:
1030 case tm_tag_macro_with_arg_t
:
1032 iter
= &tv_iters
.tag_macro
;
1035 case tm_tag_class_t
:
1037 iter
= &tv_iters
.tag_class
;
1040 case tm_tag_member_t
:
1041 case tm_tag_field_t
:
1043 iter
= &tv_iters
.tag_member
;
1046 case tm_tag_typedef_t
:
1049 iter
= &tv_iters
.tag_type
;
1052 case tm_tag_union_t
:
1053 case tm_tag_struct_t
:
1054 case tm_tag_interface_t
:
1056 iter
= &tv_iters
.tag_struct
;
1059 case tm_tag_variable_t
:
1061 iter
= &tv_iters
.tag_variable
;
1064 case tm_tag_namespace_t
:
1065 case tm_tag_package_t
:
1067 iter
= &tv_iters
.tag_namespace
;
1072 iter
= &tv_iters
.tag_other
;
1075 if (G_LIKELY(iter
->stamp
!= -1))
1082 static GdkPixbuf
*get_child_icon(GtkTreeStore
*tree_store
, GtkTreeIter
*parent
)
1084 GdkPixbuf
*icon
= NULL
;
1086 if (parent
== &tv_iters
.tag_other
)
1088 return get_tag_icon("classviewer-var");
1090 /* copy parent icon */
1091 gtk_tree_model_get(GTK_TREE_MODEL(tree_store
), parent
,
1092 SYMBOLS_COLUMN_ICON
, &icon
, -1);
1097 static void add_tree_tag(GeanyDocument
*doc
, const TMTag
*tag
, GHashTable
*parent_hash
)
1099 filetype_id ft_id
= FILETYPE_ID(doc
->file_type
);
1100 GtkTreeStore
*tree_store
= doc
->priv
->tag_store
;
1101 GtkTreeIter
*parent
= NULL
;
1103 parent
= get_tag_type_iter(tag
->type
, ft_id
);
1105 if (G_LIKELY(parent
))
1108 const gchar
*parent_name
= get_parent_name(tag
, ft_id
);
1110 GtkTreeIter
*child
= NULL
;
1111 GdkPixbuf
*icon
= NULL
;
1114 icon
= get_child_icon(tree_store
, parent
);
1118 GtkTreeIter
*parent_search
=
1119 (GtkTreeIter
*)g_hash_table_lookup(parent_hash
, parent_name
);
1122 parent
= parent_search
;
1127 /* check if the current tag is a parent of other tags */
1128 if (g_hash_table_lookup_extended(parent_hash
, tag
->name
, NULL
, NULL
) &&
1129 !utils_str_equal(tag
->name
, parent_name
)) /* prevent Foo::Foo from making parent = child */
1131 GtkTreeIter
*new_iter
= g_new0(GtkTreeIter
, 1);
1133 /* set an iter value for the hash key */
1134 g_hash_table_insert(parent_hash
, tag
->name
, new_iter
);
1135 /* instead of ignoring the appended child iter below, use the one in the hash table */
1139 gtk_tree_store_append(tree_store
, child
, parent
);
1141 name
= get_symbol_name(doc
, tag
, (parent_name
!= NULL
));
1142 gtk_tree_store_set(tree_store
, child
,
1143 SYMBOLS_COLUMN_ICON
, icon
,
1144 SYMBOLS_COLUMN_NAME
, name
,
1145 SYMBOLS_COLUMN_TAG
, tag
,
1148 if (gtk_check_version(2, 12, 0) == NULL
)
1150 gchar
*tooltip
= get_symbol_tooltip(doc
, tag
);
1151 gtk_tree_store_set(tree_store
, child
, SYMBOLS_COLUMN_TOOLTIP
, tooltip
, -1);
1155 if (G_LIKELY(G_IS_OBJECT(icon
)))
1156 g_object_unref(icon
);
1159 geany_debug("Missing symbol-tree parent iter for type %d!", tag
->type
);
1163 static void add_tree_tags(GeanyDocument
*doc
, const GList
*tags
)
1166 GHashTable
*parent_hash
;
1168 /* Create a hash table "parent_tag_name":(GtkTreeIter*) */
1169 parent_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
, g_free
);
1171 /* find and store all parent names in the hash table */
1172 for (item
= tags
; item
; item
= g_list_next(item
))
1174 const TMTag
*tag
= item
->data
;
1175 const gchar
*name
= get_parent_name(tag
, FILETYPE_ID(doc
->file_type
));
1178 g_hash_table_insert(parent_hash
, (gpointer
)name
, NULL
);
1180 for (item
= tags
; item
; item
= g_list_next(item
))
1182 const TMTag
*tag
= item
->data
;
1184 add_tree_tag(doc
, tag
, parent_hash
);
1186 g_hash_table_destroy(parent_hash
);
1190 /* we don't want to sort 1st-level nodes, but we can't return 0 because the tree sort
1191 * is not stable, so the order is already lost. */
1192 static gint
compare_top_level_names(const gchar
*a
, const gchar
*b
)
1197 foreach_ptr_array(name
, i
, top_level_iter_names
)
1199 if (utils_str_equal(name
, a
))
1201 if (utils_str_equal(name
, b
))
1203 /* This should never happen as it would mean that two or more top
1204 * level items have the same name but it can happen by typos in the translations. */
1205 if (utils_str_equal(a
, b
))
1208 g_warning("Couldn't find top level node '%s' or '%s'!", a
, b
);
1213 static gboolean
tag_has_missing_parent(const TMTag
*tag
, GtkTreeStore
*store
,
1216 /* if the tag has a parent tag, it should be at depth >= 2 */
1217 return NZV(tag
->atts
.entry
.scope
) &&
1218 gtk_tree_store_iter_depth(store
, iter
) == 1;
1222 static gint
tree_sort_func(GtkTreeModel
*model
, GtkTreeIter
*a
, GtkTreeIter
*b
,
1225 gboolean sort_by_name
= GPOINTER_TO_INT(user_data
);
1226 const TMTag
*tag_a
, *tag_b
;
1228 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_TAG
, &tag_a
, -1);
1229 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_TAG
, &tag_b
, -1);
1231 /* Check if the iters can be sorted based on tag name and line, not tree item name.
1232 * Sort by tree name if the scope was prepended, e.g. 'ScopeNameWithNoTag::TagName'. */
1233 if (tag_a
&& !tag_has_missing_parent(tag_a
, GTK_TREE_STORE(model
), a
) &&
1234 tag_b
&& !tag_has_missing_parent(tag_b
, GTK_TREE_STORE(model
), b
))
1236 return sort_by_name
? compare_symbol(tag_a
, tag_b
) :
1237 compare_symbol_lines(tag_a
, tag_b
);
1244 gtk_tree_model_get(model
, a
, SYMBOLS_COLUMN_NAME
, &astr
, -1);
1245 gtk_tree_model_get(model
, b
, SYMBOLS_COLUMN_NAME
, &bstr
, -1);
1247 /* if a is toplevel, b must be also */
1248 if (gtk_tree_store_iter_depth(GTK_TREE_STORE(model
), a
) == 0)
1250 cmp
= compare_top_level_names(astr
, bstr
);
1254 cmp
= strcmp(astr
, bstr
);
1256 /* sort duplicate 'ScopeName::OverloadedTagName' items by line as well */
1258 if (!sort_by_name
||
1259 (utils_str_equal(tag_a
->name
, tag_b
->name
) &&
1260 utils_str_equal(tag_a
->atts
.entry
.scope
, tag_b
->atts
.entry
.scope
)))
1261 cmp
= compare_symbol_lines(tag_a
, tag_b
);
1270 static void sort_tree(GtkTreeStore
*store
, gboolean sort_by_name
)
1272 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, tree_sort_func
,
1273 GINT_TO_POINTER(sort_by_name
), NULL
);
1275 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(store
), SYMBOLS_COLUMN_NAME
, GTK_SORT_ASCENDING
);
1279 gboolean
symbols_recreate_tag_list(GeanyDocument
*doc
, gint sort_mode
)
1283 g_return_val_if_fail(doc
!= NULL
, FALSE
);
1285 tags
= get_tag_list(doc
, tm_tag_max_t
);
1289 /* Make sure the model stays with us after the tree view unrefs it */
1290 g_object_ref(GTK_TREE_MODEL(doc
->priv
->tag_store
));
1291 /* Detach model from view */
1292 gtk_tree_view_set_model(GTK_TREE_VIEW(doc
->priv
->tag_tree
), NULL
);
1293 /* Clear all contents */
1294 gtk_tree_store_clear(doc
->priv
->tag_store
);
1296 /* add grandparent type iters */
1297 add_top_level_items(doc
);
1299 add_tree_tags(doc
, tags
);
1302 hide_empty_rows(doc
->priv
->tag_store
);
1304 if (sort_mode
== SYMBOLS_SORT_USE_PREVIOUS
)
1305 sort_mode
= doc
->priv
->symbol_list_sort_mode
;
1307 sort_tree(doc
->priv
->tag_store
, sort_mode
== SYMBOLS_SORT_BY_NAME
);
1308 doc
->priv
->symbol_list_sort_mode
= sort_mode
;
1310 /* Re-attach model to view */
1311 gtk_tree_view_set_model(GTK_TREE_VIEW(doc
->priv
->tag_tree
),
1312 GTK_TREE_MODEL(doc
->priv
->tag_store
));
1313 g_object_unref(GTK_TREE_MODEL(doc
->priv
->tag_store
));
1314 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
1320 /* Detects a global tags filetype from the *.lang.* language extension.
1321 * Returns NULL if there was no matching TM language. */
1322 static GeanyFiletype
*detect_global_tags_filetype(const gchar
*utf8_filename
)
1325 gchar
*shortname
= g_strdup(utf8_filename
);
1326 GeanyFiletype
*ft
= NULL
;
1328 tags_ext
= strstr(shortname
, ".tags");
1331 *tags_ext
= '\0'; /* remove .tags extension */
1332 ft
= filetypes_detect_from_extension(shortname
);
1339 /* Adapted from anjuta-2.0.2/global-tags/tm_global_tags.c, thanks.
1340 * Needs full paths for filenames, except for C/C++ tag files, when CFLAGS includes
1341 * the relevant path.
1343 * CFLAGS=-I/home/user/libname-1.x geany -g libname.d.tags libname.h */
1344 int symbols_generate_global_tags(int argc
, char **argv
, gboolean want_preprocess
)
1346 /* -E pre-process, -dD output user macros, -p prof info (?),
1347 * -undef remove builtin macros (seems to be needed with FC5 gcc 4.1.1) */
1348 const char pre_process
[] = "gcc -E -dD -p -undef";
1352 /* Create global taglist */
1355 const char *tags_file
= argv
[1];
1359 utf8_fname
= utils_get_utf8_from_locale(tags_file
);
1360 ft
= detect_global_tags_filetype(utf8_fname
);
1365 g_printerr(_("Unknown filetype extension for \"%s\".\n"), tags_file
);
1368 /* load config in case of custom filetypes */
1369 filetypes_load_config(ft
->id
, FALSE
);
1371 /* load ignore list for C/C++ parser */
1372 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
1373 load_c_ignore_tags();
1375 if (want_preprocess
&& (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
))
1376 command
= g_strdup_printf("%s %s", pre_process
, NVL(getenv("CFLAGS"), ""));
1378 command
= NULL
; /* don't preprocess */
1380 geany_debug("Generating %s tags file.", ft
->name
);
1382 status
= tm_workspace_create_global_tags(app
->configdir
, command
,
1383 (const char **) (argv
+ 2),
1384 argc
- 2, tags_file
, ft
->lang
);
1386 symbols_finalize(); /* free c_tags_ignore data */
1389 g_printerr(_("Failed to create tags file, perhaps because no tags "
1396 g_printerr(_("Usage: %s -g <Tag File> <File list>\n\n"), argv
[0]);
1397 g_printerr(_("Example:\n"
1398 "CFLAGS=`pkg-config gtk+-2.0 --cflags` %s -g gtk2.c.tags"
1399 " /usr/include/gtk-2.0/gtk/gtk.h\n"), argv
[0]);
1406 void symbols_show_load_tags_dialog(void)
1409 GtkFileFilter
*filter
;
1411 dialog
= gtk_file_chooser_dialog_new(_("Load Tags"), GTK_WINDOW(main_widgets
.window
),
1412 GTK_FILE_CHOOSER_ACTION_OPEN
,
1413 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
1414 GTK_STOCK_OPEN
, GTK_RESPONSE_OK
,
1416 gtk_widget_set_name(dialog
, "GeanyDialog");
1417 filter
= gtk_file_filter_new();
1418 gtk_file_filter_set_name(filter
, _("Geany tag files (*.tags)"));
1419 gtk_file_filter_add_pattern(filter
, "*.tags");
1420 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
1422 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_OK
)
1424 GSList
*flist
= gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog
));
1427 for (item
= flist
; item
!= NULL
; item
= g_slist_next(item
))
1429 gchar
*fname
= item
->data
;
1433 utf8_fname
= utils_get_utf8_from_locale(fname
);
1434 ft
= detect_global_tags_filetype(utf8_fname
);
1436 if (ft
!= NULL
&& symbols_load_global_tags(fname
, ft
))
1437 /* For translators: the first wildcard is the filetype, the second the filename */
1438 ui_set_statusbar(TRUE
, _("Loaded %s tags file '%s'."), ft
->name
, utf8_fname
);
1440 ui_set_statusbar(TRUE
, _("Could not load tags file '%s'."), utf8_fname
);
1445 g_slist_free(flist
);
1447 gtk_widget_destroy(dialog
);
1451 /* Fills a hash table with filetype keys that hold a linked list of filenames. */
1452 static GHashTable
*get_tagfile_hash(const GSList
*file_list
)
1455 GHashTable
*hash
= g_hash_table_new(NULL
, NULL
);
1457 for (node
= file_list
; node
!= NULL
; node
= g_slist_next(node
))
1460 gchar
*fname
= node
->data
;
1461 gchar
*utf8_fname
= utils_get_utf8_from_locale(fname
);
1462 GeanyFiletype
*ft
= detect_global_tags_filetype(utf8_fname
);
1466 if (FILETYPE_ID(ft
) != GEANY_FILETYPES_NONE
)
1468 fnames
= g_hash_table_lookup(hash
, ft
); /* may be NULL */
1469 fnames
= g_list_append(fnames
, fname
);
1470 g_hash_table_insert(hash
, ft
, fnames
);
1473 geany_debug("Unknown filetype for file '%s'.", fname
);
1479 static GHashTable
*init_user_tags(void)
1481 GSList
*file_list
= NULL
, *list
= NULL
;
1482 GHashTable
*lang_hash
;
1485 dir
= utils_build_path(app
->configdir
, "tags", NULL
);
1486 /* create the user tags dir for next time if it doesn't exist */
1487 if (! g_file_test(dir
, G_FILE_TEST_IS_DIR
))
1489 utils_mkdir(dir
, FALSE
);
1491 file_list
= utils_get_file_list_full(dir
, TRUE
, TRUE
, NULL
);
1493 dir
= utils_build_path(app
->datadir
, "tags", NULL
);
1494 list
= utils_get_file_list_full(dir
, TRUE
, TRUE
, NULL
);
1495 file_list
= g_slist_concat(file_list
, list
);
1497 lang_hash
= get_tagfile_hash(file_list
);
1498 /* don't need to delete list contents because they are now used for hash contents */
1499 g_slist_free(file_list
);
1505 static void load_user_tags(filetype_id ft_id
)
1507 static guchar
*tags_loaded
= NULL
;
1508 static GHashTable
*lang_hash
= NULL
;
1511 GeanyFiletype
*ft
= filetypes
[ft_id
];
1513 g_return_if_fail(ft_id
> 0);
1516 tags_loaded
= g_new0(guchar
, filetypes_array
->len
);
1517 if (tags_loaded
[ft_id
])
1519 tags_loaded
[ft_id
] = TRUE
; /* prevent reloading */
1521 if (lang_hash
== NULL
)
1522 lang_hash
= init_user_tags();
1524 fnames
= g_hash_table_lookup(lang_hash
, ft
);
1526 for (node
= fnames
; node
!= NULL
; node
= g_list_next(node
))
1528 const gchar
*fname
= node
->data
;
1530 symbols_load_global_tags(fname
, ft
);
1532 g_list_foreach(fnames
, (GFunc
) g_free
, NULL
);
1533 g_list_free(fnames
);
1534 g_hash_table_remove(lang_hash
, (gpointer
) ft
);
1538 gboolean
symbols_goto_tag(const gchar
*name
, gboolean definition
)
1540 const gint forward_types
= tm_tag_prototype_t
| tm_tag_externvar_t
;
1542 TMTag
*tmtag
= NULL
;
1543 GeanyDocument
*old_doc
= document_get_current();
1545 /* goto tag definition: all except prototypes / forward declarations / externs */
1546 type
= (definition
) ? tm_tag_max_t
- forward_types
: forward_types
;
1548 /* first look in the current document */
1549 if (old_doc
!= NULL
&& old_doc
->tm_file
)
1550 tmtag
= find_work_object_tag(old_doc
->tm_file
, name
, type
);
1552 /* if not found, look in the workspace */
1554 tmtag
= find_workspace_tag(name
, type
);
1558 GeanyDocument
*new_doc
= document_find_by_real_path(
1559 tmtag
->atts
.entry
.file
->work_object
.file_name
);
1561 /* not found in opened document, should open */
1562 if (new_doc
== NULL
)
1564 new_doc
= document_open_file(tmtag
->atts
.entry
.file
->work_object
.file_name
, FALSE
, NULL
, NULL
);
1567 if (navqueue_goto_line(old_doc
, new_doc
, tmtag
->atts
.entry
.line
))
1570 /* if we are here, there was no match and we are beeping ;-) */
1572 if (type
== forward_types
)
1573 ui_set_statusbar(FALSE
, _("Forward declaration \"%s\" not found."), name
);
1575 ui_set_statusbar(FALSE
, _("Definition of \"%s\" not found."), name
);
1580 /* This could perhaps be improved to check for #if, class etc. */
1581 static gint
get_function_fold_number(GeanyDocument
*doc
)
1583 /* for Java the functions are always one fold level above the class scope */
1584 if (FILETYPE_ID(doc
->file_type
) == GEANY_FILETYPES_JAVA
)
1585 return SC_FOLDLEVELBASE
+ 1;
1587 return SC_FOLDLEVELBASE
;
1591 /* Should be used only with symbols_get_current_function. */
1592 static gboolean
current_function_changed(GeanyDocument
*doc
, gint cur_line
, gint fold_level
)
1594 static gint old_line
= -2;
1595 static GeanyDocument
*old_doc
= NULL
;
1596 static gint old_fold_num
= -1;
1597 const gint fold_num
= fold_level
& SC_FOLDLEVELNUMBERMASK
;
1600 /* check if the cached line and file index have changed since last time: */
1601 if (doc
== NULL
|| doc
!= old_doc
)
1604 if (cur_line
== old_line
)
1608 /* if the line has only changed by 1 */
1609 if (abs(cur_line
- old_line
) == 1)
1611 const gint fn_fold
=
1612 get_function_fold_number(doc
);
1613 /* It's the same function if the fold number hasn't changed, or both the new
1614 * and old fold numbers are above the function fold number. */
1616 fold_num
== old_fold_num
||
1617 (old_fold_num
> fn_fold
&& fold_num
> fn_fold
);
1624 /* record current line and file index for next time */
1625 old_line
= cur_line
;
1627 old_fold_num
= fold_num
;
1632 /* Parse the function name up to 2 lines before tag_line.
1633 * C++ like syntax should be parsed by parse_cpp_function_at_line, otherwise the return
1634 * type or argument names can be confused with the function name. */
1635 static gchar
*parse_function_at_line(ScintillaObject
*sci
, gint tag_line
)
1637 gint start
, end
, max_pos
;
1641 switch (sci_get_lexer(sci
))
1643 case SCLEX_RUBY
: fn_style
= SCE_RB_DEFNAME
; break;
1644 case SCLEX_PYTHON
: fn_style
= SCE_P_DEFNAME
; break;
1645 default: fn_style
= SCE_C_IDENTIFIER
; /* several lexers use SCE_C_IDENTIFIER */
1647 start
= sci_get_position_from_line(sci
, tag_line
- 2);
1648 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
1649 while (sci_get_style_at(sci
, start
) != fn_style
1650 && start
< max_pos
) start
++;
1653 while (sci_get_style_at(sci
, end
) == fn_style
1654 && end
< max_pos
) end
++;
1658 cur_tag
= g_malloc(end
- start
+ 1);
1659 sci_get_text_range(sci
, start
, end
, cur_tag
);
1664 /* Parse the function name */
1665 static gchar
*parse_cpp_function_at_line(ScintillaObject
*sci
, gint tag_line
)
1667 gint start
, end
, first_pos
, max_pos
;
1672 first_pos
= end
= sci_get_position_from_line(sci
, tag_line
);
1673 max_pos
= sci_get_position_from_line(sci
, tag_line
+ 1);
1675 /* goto the begin of function body */
1676 while (end
< max_pos
&&
1677 (tmp
= sci_get_char_at(sci
, end
)) != '{' &&
1679 if (tmp
== 0) end
--;
1681 /* go back to the end of function identifier */
1682 while (end
> 0 && end
> first_pos
- 500 &&
1683 (tmp
= sci_get_char_at(sci
, end
)) != '(' &&
1686 if (end
< 0) end
= 0;
1688 /* skip whitespaces between identifier and ( */
1689 while (end
> 0 && isspace(sci_get_char_at(sci
, end
))) end
--;
1693 /* Use tmp to find SCE_C_IDENTIFIER or SCE_C_GLOBALCLASS chars */
1694 while (start
>= 0 && ((tmp
= sci_get_style_at(sci
, start
)) == SCE_C_IDENTIFIER
1695 || tmp
== SCE_C_GLOBALCLASS
1696 || (c
= sci_get_char_at(sci
, start
)) == '~'
1699 if (start
!= 0 && start
< end
) start
++; /* correct for last non-matching char */
1701 if (start
== end
) return NULL
;
1702 cur_tag
= g_malloc(end
- start
+ 2);
1703 sci_get_text_range(sci
, start
, end
+ 1, cur_tag
);
1708 /* Sets *tagname to point at the current function or tag name.
1709 * If doc is NULL, reset the cached current tag data to ensure it will be reparsed on the next
1710 * call to this function.
1711 * Returns: line number of the current tag, or -1 if unknown. */
1712 gint
symbols_get_current_function(GeanyDocument
*doc
, const gchar
**tagname
)
1714 static gint tag_line
= -1;
1715 static gchar
*cur_tag
= NULL
;
1718 TMWorkObject
*tm_file
;
1720 if (doc
== NULL
) /* reset current function */
1722 current_function_changed(NULL
, -1, -1);
1724 cur_tag
= g_strdup(_("unknown"));
1725 if (tagname
!= NULL
)
1731 line
= sci_get_current_line(doc
->editor
->sci
);
1732 fold_level
= sci_get_fold_level(doc
->editor
->sci
, line
);
1733 /* check if the cached line and file index have changed since last time: */
1734 if (! current_function_changed(doc
, line
, fold_level
))
1736 /* we can assume same current function as before */
1740 g_free(cur_tag
); /* free the old tag, it will be replaced. */
1742 /* if line is at base fold level, we're not in a function */
1743 if ((fold_level
& SC_FOLDLEVELNUMBERMASK
) == SC_FOLDLEVELBASE
)
1745 cur_tag
= g_strdup(_("unknown"));
1750 tm_file
= doc
->tm_file
;
1752 /* if the document has no changes, get the previous function name from TM */
1753 if (! doc
->changed
&& tm_file
!= NULL
&& tm_file
->tags_array
!= NULL
)
1755 const TMTag
*tag
= (const TMTag
*) tm_get_current_function(tm_file
->tags_array
, line
);
1760 tmp
= tag
->atts
.entry
.scope
;
1761 cur_tag
= tmp
? g_strconcat(tmp
, "::", tag
->name
, NULL
) : g_strdup(tag
->name
);
1763 tag_line
= tag
->atts
.entry
.line
;
1768 /* parse the current function name here because TM line numbers may have changed,
1769 * and it would take too long to reparse the whole file. */
1770 if (doc
->file_type
!= NULL
&& doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
1772 const gint fn_fold
= get_function_fold_number(doc
);
1775 do /* find the top level fold point */
1777 tag_line
= sci_get_fold_parent(doc
->editor
->sci
, tag_line
);
1778 fold_level
= sci_get_fold_level(doc
->editor
->sci
, tag_line
);
1779 } while (tag_line
>= 0 &&
1780 (fold_level
& SC_FOLDLEVELNUMBERMASK
) != fn_fold
);
1784 if (sci_get_lexer(doc
->editor
->sci
) == SCLEX_CPP
)
1785 cur_tag
= parse_cpp_function_at_line(doc
->editor
->sci
, tag_line
);
1787 cur_tag
= parse_function_at_line(doc
->editor
->sci
, tag_line
);
1789 if (cur_tag
!= NULL
)
1797 cur_tag
= g_strdup(_("unknown"));
1804 static void on_symbol_tree_sort_clicked(GtkMenuItem
*menuitem
, gpointer user_data
)
1806 gint sort_mode
= GPOINTER_TO_INT(user_data
);
1807 GeanyDocument
*doc
= document_get_current();
1809 if (ignore_callback
)
1813 doc
->has_tags
= symbols_recreate_tag_list(doc
, sort_mode
);
1817 static void on_symbol_tree_menu_show(GtkWidget
*widget
,
1820 GeanyDocument
*doc
= document_get_current();
1823 enable
= doc
&& doc
->has_tags
;
1824 gtk_widget_set_sensitive(symbol_menu
.sort_by_name
, enable
);
1825 gtk_widget_set_sensitive(symbol_menu
.sort_by_appearance
, enable
);
1826 gtk_widget_set_sensitive(symbol_menu
.expand_all
, enable
);
1827 gtk_widget_set_sensitive(symbol_menu
.collapse_all
, enable
);
1832 ignore_callback
= TRUE
;
1834 if (doc
->priv
->symbol_list_sort_mode
== SYMBOLS_SORT_BY_NAME
)
1835 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_name
), TRUE
);
1837 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(symbol_menu
.sort_by_appearance
), TRUE
);
1839 ignore_callback
= FALSE
;
1843 static void on_expand_collapse(GtkWidget
*widget
, gpointer user_data
)
1845 gboolean expand
= utils_str_equal(user_data
, GTK_STOCK_ADD
);
1846 GeanyDocument
*doc
= document_get_current();
1851 g_return_if_fail(doc
->priv
->tag_tree
);
1854 gtk_tree_view_expand_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
1856 gtk_tree_view_collapse_all(GTK_TREE_VIEW(doc
->priv
->tag_tree
));
1860 static void create_taglist_popup_menu(void)
1862 GtkWidget
*item
, *menu
;
1864 tv
.popup_taglist
= menu
= gtk_menu_new();
1866 symbol_menu
.expand_all
= item
= ui_image_menu_item_new(GTK_STOCK_ADD
, _("_Expand All"));
1867 gtk_widget_show(item
);
1868 gtk_container_add(GTK_CONTAINER(menu
), item
);
1869 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GTK_STOCK_ADD
);
1871 symbol_menu
.collapse_all
= item
= ui_image_menu_item_new(GTK_STOCK_REMOVE
, _("_Collapse All"));
1872 gtk_widget_show(item
);
1873 gtk_container_add(GTK_CONTAINER(menu
), item
);
1874 g_signal_connect(item
, "activate", G_CALLBACK(on_expand_collapse
), GTK_STOCK_REMOVE
);
1876 item
= gtk_separator_menu_item_new();
1877 gtk_widget_show(item
);
1878 gtk_container_add(GTK_CONTAINER(menu
), item
);
1880 symbol_menu
.sort_by_name
= item
= gtk_radio_menu_item_new_with_mnemonic(NULL
,
1881 _("Sort by _Name"));
1882 gtk_widget_show(item
);
1883 gtk_container_add(GTK_CONTAINER(menu
), item
);
1884 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
1885 GINT_TO_POINTER(SYMBOLS_SORT_BY_NAME
));
1887 symbol_menu
.sort_by_appearance
= item
= gtk_radio_menu_item_new_with_mnemonic_from_widget(
1888 GTK_RADIO_MENU_ITEM(item
), _("Sort by _Appearance"));
1889 gtk_widget_show(item
);
1890 gtk_container_add(GTK_CONTAINER(menu
), item
);
1891 g_signal_connect(item
, "activate", G_CALLBACK(on_symbol_tree_sort_clicked
),
1892 GINT_TO_POINTER(SYMBOLS_SORT_BY_APPEARANCE
));
1894 g_signal_connect(menu
, "show", G_CALLBACK(on_symbol_tree_menu_show
), NULL
);
1896 sidebar_add_common_menu_items(GTK_MENU(menu
));
1900 static void on_document_save(G_GNUC_UNUSED GObject
*object
, GeanyDocument
*doc
)
1902 g_return_if_fail(NZV(doc
->real_path
));
1904 if (utils_str_equal(doc
->real_path
,
1905 utils_build_path(app
->configdir
, "ignore.tags", NULL
)))
1906 load_c_ignore_tags();
1910 void symbols_init(void)
1912 create_taglist_popup_menu();
1914 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "ignore.tags", NULL
),
1916 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
1920 void symbols_finalize(void)
1922 g_strfreev(html_entities
);
1923 g_strfreev(c_tags_ignore
);