Perform scope autocompletion based on function return types
[geany-mirror.git] / src / filetypes.c
blob5cfe4bc8d92c7a27d95c176e908fcefef0b22b5a
1 /*
2 * filetypes.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /**
23 * @file filetypes.h
24 * Filetype detection, file extensions and filetype menu items.
27 /* Note: we use filetype_id for some function arguments, but GeanyFiletype is better; we should
28 * only use GeanyFiletype for API functions. */
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "filetypes.h"
36 #include "app.h"
37 #include "callbacks.h" /* FIXME: for ignore_callback */
38 #include "document.h"
39 #include "filetypesprivate.h"
40 #include "geany.h"
41 #include "geanyobject.h"
42 #include "highlighting.h"
43 #include "projectprivate.h"
44 #include "sciwrappers.h"
45 #include "support.h"
46 #include "symbols.h"
47 #include "tm_parser.h"
48 #include "utils.h"
49 #include "ui_utils.h"
51 #include <stdlib.h>
52 #include <string.h>
54 #include <glib/gstdio.h>
56 #define GEANY_FILETYPE_SEARCH_LINES 2 /* lines of file to search for filetype */
58 GPtrArray *filetypes_array = NULL; /* Dynamic array of filetype pointers */
60 static GHashTable *filetypes_hash = NULL; /* Hash of filetype pointers based on name keys */
62 /** List of filetype pointers sorted by name, but with @c filetypes_index(GEANY_FILETYPES_NONE)
63 * first, as this is usually treated specially.
64 * The list does not change (after filetypes have been initialized), so you can use
65 * @code g_slist_nth_data(filetypes_by_title, n) @endcode and expect the same result at different times.
66 * @see filetypes_get_sorted_by_name(). */
67 GSList *filetypes_by_title = NULL;
70 static void create_radio_menu_item(GtkWidget *menu, GeanyFiletype *ftype);
72 static gchar *filetypes_get_conf_extension(const GeanyFiletype *ft);
73 static void read_filetype_config(void);
74 static void create_set_filetype_menu(gboolean config);
75 static gchar *filetypes_get_filename(GeanyFiletype *ft, gboolean user);
78 enum TitleType
80 TITLE_NONE,
81 TITLE_SOURCE_FILE,
82 TITLE_FILE,
83 TITLE_SCRIPT,
84 TITLE_DOCUMENT
87 /* Save adding many translation strings if the filetype name doesn't need translating */
88 static gchar *filetype_make_title(const char *name, enum TitleType type)
90 g_return_val_if_fail(name != NULL, NULL);
92 switch (type)
94 case TITLE_SOURCE_FILE: return g_strdup_printf(_("%s source file"), name);
95 case TITLE_FILE: return g_strdup_printf(_("%s file"), name);
96 case TITLE_SCRIPT: return g_strdup_printf(_("%s script"), name);
97 case TITLE_DOCUMENT: return g_strdup_printf(_("%s document"), name);
98 case TITLE_NONE: /* fall through */
99 default: return g_strdup(name);
104 /* name argument (ie filetype name) must not be translated as it is used for
105 * filetype lookup. Use filetypes_get_display_name() instead.*/
106 static void ft_init(filetype_id ft_id, int lang, const char *name,
107 const char *title_name, enum TitleType title_type,
108 GeanyFiletypeGroupID group_id)
110 GeanyFiletype *ft = filetypes[ft_id];
111 ft->lang = lang;
112 ft->name = g_strdup(name);
113 ft->title = filetype_make_title((title_name != NULL) ? title_name : ft->name, title_type);
114 ft->group = group_id;
117 /* Evil macro to save typing and make init_builtin_filetypes() more readable */
118 #define FT_INIT(ft_id, parser_id, name, title_name, title_type, group_id) \
119 ft_init(GEANY_FILETYPES_##ft_id, TM_PARSER_##parser_id, name, title_name, \
120 TITLE_##title_type, GEANY_FILETYPE_GROUP_##group_id)
123 /* Note: remember to update HACKING if this function is renamed. */
124 static void init_builtin_filetypes(void)
126 /* Column legend:
127 * [0] = Filetype constant (GEANY_FILETYPES_*)
128 * [1] = CTags parser (TM_PARSER_*)
129 * [2] = Non-translated filetype name (*not* label for display)
130 * [3] = Translatable human filetype title prefix or NULL to use [2]
131 * [4] = Title type (TITLE_*) constant (ex. TITLE_SOURCE_FILE is 'source file' suffix)
132 * [5] = The filetype group constant (GEANY_FILETYPE_GROUP_*)
133 * --------------------------------------------------------------------------------------------------------------------------
134 * [0] [1] [2] [3] [4] [5] */
135 FT_INIT( NONE, NONE, "None", _("None"), NONE, NONE );
136 FT_INIT( C, C, "C", NULL, SOURCE_FILE, COMPILED );
137 FT_INIT( CPP, CPP, "C++", NULL, SOURCE_FILE, COMPILED );
138 FT_INIT( OBJECTIVEC, OBJC, "Objective-C", NULL, SOURCE_FILE, COMPILED );
139 FT_INIT( CS, CSHARP, "C#", NULL, SOURCE_FILE, COMPILED );
140 FT_INIT( VALA, VALA, "Vala", NULL, SOURCE_FILE, COMPILED );
141 FT_INIT( D, D, "D", NULL, SOURCE_FILE, COMPILED );
142 FT_INIT( JAVA, JAVA, "Java", NULL, SOURCE_FILE, COMPILED );
143 FT_INIT( PASCAL, PASCAL, "Pascal", NULL, SOURCE_FILE, COMPILED );
144 FT_INIT( ASM, ASM, "ASM", "Assembler", SOURCE_FILE, COMPILED );
145 FT_INIT( BASIC, FREEBASIC, "FreeBasic", NULL, SOURCE_FILE, COMPILED );
146 FT_INIT( FORTRAN, FORTRAN, "Fortran", "Fortran (F90)", SOURCE_FILE, COMPILED );
147 FT_INIT( F77, F77, "F77", "Fortran (F77)", SOURCE_FILE, COMPILED );
148 FT_INIT( GLSL, GLSL, "GLSL", NULL, SOURCE_FILE, COMPILED );
149 FT_INIT( CAML, NONE, "CAML", "(O)Caml", SOURCE_FILE, COMPILED );
150 FT_INIT( PERL, PERL, "Perl", NULL, SOURCE_FILE, SCRIPT );
151 FT_INIT( PHP, PHP, "PHP", NULL, SOURCE_FILE, SCRIPT );
152 FT_INIT( JS, JAVASCRIPT, "Javascript", NULL, SOURCE_FILE, SCRIPT );
153 FT_INIT( PYTHON, PYTHON, "Python", NULL, SOURCE_FILE, SCRIPT );
154 FT_INIT( RUBY, RUBY, "Ruby", NULL, SOURCE_FILE, SCRIPT );
155 FT_INIT( TCL, TCL, "Tcl", NULL, SOURCE_FILE, SCRIPT );
156 FT_INIT( LUA, LUA, "Lua", NULL, SOURCE_FILE, SCRIPT );
157 FT_INIT( FERITE, FERITE, "Ferite", NULL, SOURCE_FILE, SCRIPT );
158 FT_INIT( HASKELL, HASKELL, "Haskell", NULL, SOURCE_FILE, COMPILED );
159 FT_INIT( MARKDOWN, MARKDOWN, "Markdown", NULL, SOURCE_FILE, MARKUP );
160 FT_INIT( TXT2TAGS, TXT2TAGS, "Txt2tags", NULL, SOURCE_FILE, MARKUP );
161 FT_INIT( ABC, ABC, "Abc", NULL, FILE, MISC );
162 FT_INIT( SH, SH, "Sh", _("Shell"), SCRIPT, SCRIPT );
163 FT_INIT( MAKE, MAKEFILE, "Make", _("Makefile"), NONE, SCRIPT );
164 FT_INIT( XML, NONE, "XML", NULL, DOCUMENT, MARKUP );
165 FT_INIT( DOCBOOK, DOCBOOK, "Docbook", NULL, DOCUMENT, MARKUP );
166 FT_INIT( HTML, HTML, "HTML", NULL, DOCUMENT, MARKUP );
167 FT_INIT( CSS, CSS, "CSS", _("Cascading Stylesheet"), NONE, MARKUP ); /* not really markup but fit quite well to HTML */
168 FT_INIT( SQL, SQL, "SQL", NULL, FILE, MISC );
169 FT_INIT( COBOL, COBOL, "COBOL", NULL, SOURCE_FILE, COMPILED );
170 FT_INIT( LATEX, LATEX, "LaTeX", NULL, SOURCE_FILE, MARKUP );
171 FT_INIT( VHDL, VHDL, "VHDL", NULL, SOURCE_FILE, COMPILED );
172 FT_INIT( VERILOG, VERILOG, "Verilog", NULL, SOURCE_FILE, COMPILED );
173 FT_INIT( DIFF, DIFF, "Diff", NULL, FILE, MISC );
174 FT_INIT( LISP, NONE, "Lisp", NULL, SOURCE_FILE, SCRIPT );
175 FT_INIT( ERLANG, ERLANG, "Erlang", NULL, SOURCE_FILE, COMPILED );
176 FT_INIT( CONF, CONF, "Conf", _("Config"), FILE, MISC );
177 FT_INIT( PO, NONE, "Po", _("Gettext translation"), FILE, MISC );
178 FT_INIT( HAXE, HAXE, "Haxe", NULL, SOURCE_FILE, COMPILED );
179 FT_INIT( AS, ACTIONSCRIPT, "ActionScript", NULL, SOURCE_FILE, SCRIPT );
180 FT_INIT( R, R, "R", NULL, SOURCE_FILE, SCRIPT );
181 FT_INIT( REST, REST, "reStructuredText", NULL, SOURCE_FILE, MARKUP );
182 FT_INIT( MATLAB, MATLAB, "Matlab/Octave", NULL, SOURCE_FILE, SCRIPT );
183 FT_INIT( YAML, NONE, "YAML", NULL, FILE, MISC );
184 FT_INIT( CMAKE, NONE, "CMake", NULL, SOURCE_FILE, SCRIPT );
185 FT_INIT( NSIS, NSIS, "NSIS", NULL, SOURCE_FILE, SCRIPT );
186 FT_INIT( ADA, NONE, "Ada", NULL, SOURCE_FILE, COMPILED );
187 FT_INIT( FORTH, NONE, "Forth", NULL, SOURCE_FILE, SCRIPT );
188 FT_INIT( ASCIIDOC, ASCIIDOC, "Asciidoc", NULL, SOURCE_FILE, MARKUP );
189 FT_INIT( ABAQUS, ABAQUS, "Abaqus", NULL, SOURCE_FILE, SCRIPT );
190 FT_INIT( BATCH, NONE, "Batch", NULL, SCRIPT, SCRIPT );
191 FT_INIT( POWERSHELL, POWERSHELL, "PowerShell", NULL, SOURCE_FILE, SCRIPT );
192 FT_INIT( RUST, RUST, "Rust", NULL, SOURCE_FILE, COMPILED );
193 FT_INIT( COFFEESCRIPT, NONE, "CoffeeScript", NULL, SOURCE_FILE, SCRIPT );
194 FT_INIT( GO, GO, "Go", NULL, SOURCE_FILE, COMPILED );
195 FT_INIT( ZEPHIR, ZEPHIR, "Zephir", NULL, SOURCE_FILE, COMPILED );
199 /* initialize fields. */
200 static GeanyFiletype *filetype_new(void)
202 GeanyFiletype *ft = g_new0(GeanyFiletype, 1);
204 ft->group = GEANY_FILETYPE_GROUP_NONE;
205 ft->lang = -2; /* assume no tagmanager parser */
206 /* pattern must not be null */
207 ft->pattern = g_new0(gchar*, 1);
208 ft->indent_width = -1;
209 ft->indent_type = -1;
211 ft->priv = g_new0(GeanyFiletypePrivate, 1);
212 ft->priv->project_list_entry = -1; /* no entry */
214 return ft;
218 static gint cmp_filetype(gconstpointer pft1, gconstpointer pft2, gpointer data)
220 gboolean by_name = GPOINTER_TO_INT(data);
221 const GeanyFiletype *ft1 = pft1, *ft2 = pft2;
223 if (G_UNLIKELY(ft1->id == GEANY_FILETYPES_NONE))
224 return -1;
225 if (G_UNLIKELY(ft2->id == GEANY_FILETYPES_NONE))
226 return 1;
228 return by_name ?
229 utils_str_casecmp(ft1->name, ft2->name) :
230 utils_str_casecmp(ft1->title, ft2->title);
234 /** Gets a list of filetype pointers sorted by name.
235 * The list does not change on subsequent calls.
236 * @return The list - do not free.
237 * @see filetypes_by_title. */
238 GEANY_API_SYMBOL
239 const GSList *filetypes_get_sorted_by_name(void)
241 static GSList *list = NULL;
243 g_return_val_if_fail(filetypes_by_title, NULL);
245 if (!list)
247 list = g_slist_copy(filetypes_by_title);
248 list = g_slist_sort_with_data(list, cmp_filetype, GINT_TO_POINTER(TRUE));
250 return list;
254 /* Add a filetype pointer to the lists of available filetypes,
255 * and set the filetype::id field. */
256 static void filetype_add(GeanyFiletype *ft)
258 g_return_if_fail(ft);
259 g_return_if_fail(ft->name);
261 ft->id = filetypes_array->len; /* len will be the index for filetype_array */
262 g_ptr_array_add(filetypes_array, ft);
263 g_hash_table_insert(filetypes_hash, ft->name, ft);
265 /* list will be sorted later */
266 filetypes_by_title = g_slist_prepend(filetypes_by_title, ft);
270 static void add_custom_filetype(const gchar *filename)
272 gchar *fn = utils_strdupa(strstr(filename, ".") + 1);
273 gchar *dot = g_strrstr(fn, ".conf");
274 GeanyFiletype *ft;
276 g_return_if_fail(dot);
278 *dot = 0x0;
280 if (g_hash_table_lookup(filetypes_hash, fn))
281 return;
283 ft = filetype_new();
284 ft->name = g_strdup(fn);
285 ft->title = filetype_make_title(ft->name, TITLE_FILE);
286 ft->priv->custom = TRUE;
287 filetype_add(ft);
288 geany_debug("Added filetype %s (%d).", ft->name, ft->id);
292 static void init_custom_filetypes(const gchar *path)
294 GDir *dir;
295 const gchar *filename;
297 g_return_if_fail(path);
299 dir = g_dir_open(path, 0, NULL);
300 if (dir == NULL)
301 return;
303 foreach_dir(filename, dir)
305 const gchar prefix[] = "filetypes.";
307 if (g_str_has_prefix(filename, prefix) &&
308 g_str_has_suffix(filename + strlen(prefix), ".conf"))
310 add_custom_filetype(filename);
313 g_dir_close(dir);
317 /* Create the filetypes array and fill it with the known filetypes.
318 * Warning: GTK isn't necessarily initialized yet. */
319 void filetypes_init_types(void)
321 filetype_id ft_id;
322 gchar *f;
324 g_return_if_fail(filetypes_array == NULL);
325 g_return_if_fail(filetypes_hash == NULL);
327 filetypes_array = g_ptr_array_sized_new(GEANY_MAX_BUILT_IN_FILETYPES);
328 filetypes_hash = g_hash_table_new(g_str_hash, g_str_equal);
330 /* Create built-in filetypes */
331 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
333 filetypes[ft_id] = filetype_new();
335 init_builtin_filetypes();
337 /* Add built-in filetypes to the hash now the name fields are set */
338 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
340 filetype_add(filetypes[ft_id]);
342 init_custom_filetypes(app->datadir);
343 f = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
344 init_custom_filetypes(f);
345 g_free(f);
347 /* sort last instead of on insertion to prevent exponential time */
348 filetypes_by_title = g_slist_sort_with_data(filetypes_by_title,
349 cmp_filetype, GINT_TO_POINTER(FALSE));
351 read_filetype_config();
355 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
357 gchar *f, *basename;
359 g_return_if_fail(!EMPTY(doc->real_path));
361 f = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
362 if (utils_str_equal(doc->real_path, f))
363 filetypes_reload_extensions();
364 g_free(f);
366 basename = g_path_get_basename(doc->real_path);
367 if (g_str_has_prefix(basename, "filetypes."))
369 guint i;
371 for (i = 0; i < filetypes_array->len; i++)
373 GeanyFiletype *ft = filetypes[i];
375 f = filetypes_get_filename(ft, TRUE);
376 if (utils_str_equal(doc->real_path, f))
378 guint j;
380 /* Note: we don't reload other filetypes, even though the named styles may have changed.
381 * The user can do this manually with 'Tools->Reload Configuration' */
382 filetypes_load_config(i, TRUE);
384 foreach_document(j)
385 document_reload_config(documents[j]);
387 g_free(f);
388 break;
390 g_free(f);
393 g_free(basename);
397 static void setup_config_file_menus(void)
399 gchar *f;
401 f = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
402 ui_add_config_file_menu_item(f, NULL, NULL);
403 SETPTR(f, g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL));
404 ui_add_config_file_menu_item(f, NULL, NULL);
405 g_free(f);
407 create_set_filetype_menu(TRUE);
409 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
413 static GtkWidget *create_sub_menu(GtkWidget *parent, const gchar *title)
415 GtkWidget *menu, *item;
417 menu = gtk_menu_new();
418 item = gtk_menu_item_new_with_mnemonic((title));
419 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
420 gtk_container_add(GTK_CONTAINER(parent), item);
421 gtk_widget_show(item);
423 return menu;
427 static void create_set_filetype_menu(gboolean config)
429 GtkWidget *group_menus[GEANY_FILETYPE_GROUP_COUNT] = {NULL};
430 GSList *node;
431 GtkWidget *menu;
433 menu = config ? ui_widgets.config_files_filetype_menu :
434 ui_lookup_widget(main_widgets.window, "set_filetype1_menu");
436 group_menus[GEANY_FILETYPE_GROUP_COMPILED] = create_sub_menu(menu, _("_Programming Languages"));
437 group_menus[GEANY_FILETYPE_GROUP_SCRIPT] = create_sub_menu(menu, _("_Scripting Languages"));
438 group_menus[GEANY_FILETYPE_GROUP_MARKUP] = create_sub_menu(menu, _("_Markup Languages"));
439 group_menus[GEANY_FILETYPE_GROUP_MISC] = create_sub_menu(menu, _("M_iscellaneous"));
441 /* Append all filetypes to the menu */
442 foreach_slist(node, filetypes_by_title)
444 GeanyFiletype *ft = node->data;
445 GtkWidget *parent = (ft->group != GEANY_FILETYPE_GROUP_NONE) ? group_menus[ft->group] : menu;
447 /* we already have filetypes.common config entry */
448 if (config && ft->id == GEANY_FILETYPES_NONE)
449 continue;
451 if (config)
453 gchar *filename = filetypes_get_filename(ft, TRUE);
455 ui_add_config_file_menu_item(filename, NULL, GTK_CONTAINER(parent));
456 g_free(filename);
458 else
459 create_radio_menu_item(parent, ft);
464 void filetypes_init(void)
466 filetypes_init_types();
467 create_set_filetype_menu(FALSE);
468 setup_config_file_menus();
472 /* Find a filetype that predicate returns TRUE for, otherwise return NULL. */
473 GeanyFiletype *filetypes_find(GCompareFunc predicate, gpointer user_data)
475 guint i;
477 for (i = 0; i < filetypes_array->len; i++)
479 GeanyFiletype *ft = filetypes[i];
481 if (predicate(ft, user_data))
482 return ft;
484 return NULL;
488 static gboolean match_basename(gconstpointer pft, gconstpointer user_data)
490 const GeanyFiletype *ft = pft;
491 const gchar *base_filename = user_data;
492 gint j;
493 gboolean ret = FALSE;
495 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
496 return FALSE;
498 for (j = 0; ft->pattern[j] != NULL; j++)
500 GPatternSpec *pattern = g_pattern_spec_new(ft->pattern[j]);
502 if (g_pattern_match_string(pattern, base_filename))
504 ret = TRUE;
505 g_pattern_spec_free(pattern);
506 break;
508 g_pattern_spec_free(pattern);
510 return ret;
514 static GeanyFiletype *check_builtin_filenames(const gchar *utf8_filename)
516 gchar *lfn = NULL;
517 gchar *path;
518 gboolean found = FALSE;
520 #ifdef G_OS_WIN32
521 /* use lower case basename */
522 lfn = g_utf8_strdown(utf8_filename, -1);
523 #else
524 lfn = g_strdup(utf8_filename);
525 #endif
526 SETPTR(lfn, utils_get_locale_from_utf8(lfn));
528 path = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.", NULL);
529 if (g_str_has_prefix(lfn, path))
530 found = TRUE;
532 SETPTR(path, g_build_filename(app->datadir, "filetypes.", NULL));
533 if (g_str_has_prefix(lfn, path))
534 found = TRUE;
536 g_free(path);
537 g_free(lfn);
538 return found ? filetypes[GEANY_FILETYPES_CONF] : NULL;
542 /* Detect filetype only based on the filename extension.
543 * utf8_filename can include the full path. */
544 GeanyFiletype *filetypes_detect_from_extension(const gchar *utf8_filename)
546 gchar *base_filename;
547 GeanyFiletype *ft;
549 ft = check_builtin_filenames(utf8_filename);
550 if (ft)
551 return ft;
553 /* to match against the basename of the file (because of Makefile*) */
554 base_filename = g_path_get_basename(utf8_filename);
555 #ifdef G_OS_WIN32
556 /* use lower case basename */
557 SETPTR(base_filename, g_utf8_strdown(base_filename, -1));
558 #endif
560 ft = filetypes_find(match_basename, base_filename);
561 if (ft == NULL)
562 ft = filetypes[GEANY_FILETYPES_NONE];
564 g_free(base_filename);
565 return ft;
569 /* This detects the filetype of the file pointed by 'utf8_filename' and a list of filetype id's,
570 * terminated by -1.
571 * The detected filetype of the file is checked against every id in the passed list and if
572 * there is a match, TRUE is returned. */
573 static gboolean shebang_find_and_match_filetype(const gchar *utf8_filename, gint first, ...)
575 GeanyFiletype *ft = NULL;
576 gint test;
577 gboolean result = FALSE;
578 va_list args;
580 ft = filetypes_detect_from_extension(utf8_filename);
581 if (ft == NULL || ft->id >= filetypes_array->len)
582 return FALSE;
584 va_start(args, first);
585 test = first;
586 while (1)
588 if (test == -1)
589 break;
591 if (ft->id == (guint) test)
593 result = TRUE;
594 break;
596 test = va_arg(args, gint);
598 va_end(args);
600 return result;
604 static GeanyFiletype *find_shebang(const gchar *utf8_filename, const gchar *line)
606 GeanyFiletype *ft = NULL;
608 if (strlen(line) > 2 && line[0] == '#' && line[1] == '!')
610 static const struct {
611 const gchar *name;
612 filetype_id filetype;
613 } intepreter_map[] = {
614 { "sh", GEANY_FILETYPES_SH },
615 { "bash", GEANY_FILETYPES_SH },
616 { "dash", GEANY_FILETYPES_SH },
617 { "perl", GEANY_FILETYPES_PERL },
618 { "python", GEANY_FILETYPES_PYTHON },
619 { "php", GEANY_FILETYPES_PHP },
620 { "ruby", GEANY_FILETYPES_RUBY },
621 { "tcl", GEANY_FILETYPES_TCL },
622 { "make", GEANY_FILETYPES_MAKE },
623 { "zsh", GEANY_FILETYPES_SH },
624 { "ksh", GEANY_FILETYPES_SH },
625 { "mksh", GEANY_FILETYPES_SH },
626 { "csh", GEANY_FILETYPES_SH },
627 { "tcsh", GEANY_FILETYPES_SH },
628 { "ash", GEANY_FILETYPES_SH },
629 { "dmd", GEANY_FILETYPES_D },
630 { "wish", GEANY_FILETYPES_TCL },
631 { "node", GEANY_FILETYPES_JS },
632 { "rust", GEANY_FILETYPES_RUST }
634 gchar *tmp = g_path_get_basename(line + 2);
635 gchar *basename_interpreter = tmp;
636 guint i;
638 if (g_str_has_prefix(tmp, "env "))
639 { /* skip "env" and read the following interpreter */
640 basename_interpreter += 4;
643 for (i = 0; ! ft && i < G_N_ELEMENTS(intepreter_map); i++)
645 if (g_str_has_prefix(basename_interpreter, intepreter_map[i].name))
646 ft = filetypes[intepreter_map[i].filetype];
648 g_free(tmp);
650 /* detect HTML files */
651 if (g_str_has_prefix(line, "<!DOCTYPE html") || g_str_has_prefix(line, "<html"))
653 /* PHP, Perl and Python files might also start with <html, so detect them based on filename
654 * extension and use the detected filetype, else assume HTML */
655 if (! shebang_find_and_match_filetype(utf8_filename,
656 GEANY_FILETYPES_PERL, GEANY_FILETYPES_PHP, GEANY_FILETYPES_PYTHON, -1))
658 ft = filetypes[GEANY_FILETYPES_HTML];
661 /* detect XML files */
662 else if (utf8_filename && g_str_has_prefix(line, "<?xml"))
664 /* HTML and DocBook files might also start with <?xml, so detect them based on filename
665 * extension and use the detected filetype, else assume XML */
666 if (! shebang_find_and_match_filetype(utf8_filename,
667 GEANY_FILETYPES_HTML, GEANY_FILETYPES_DOCBOOK,
668 /* Perl, Python and PHP only to be safe */
669 GEANY_FILETYPES_PERL, GEANY_FILETYPES_PHP, GEANY_FILETYPES_PYTHON, -1))
671 ft = filetypes[GEANY_FILETYPES_XML];
674 else if (g_str_has_prefix(line, "<?php"))
676 ft = filetypes[GEANY_FILETYPES_PHP];
678 return ft;
682 /* Detect the filetype checking for a shebang, then filename extension.
683 * @lines: an strv of the lines to scan (must containing at least one line) */
684 static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_filename,
685 gchar **lines)
687 GeanyFiletype *ft;
688 gint i;
689 GRegex *ft_regex;
690 GMatchInfo *match;
691 GError *regex_error = NULL;
693 /* try to find a shebang and if found use it prior to the filename extension
694 * also checks for <?xml */
695 ft = find_shebang(utf8_filename, lines[0]);
696 if (ft != NULL)
697 return ft;
699 /* try to extract the filetype using a regex capture */
700 ft_regex = g_regex_new(file_prefs.extract_filetype_regex,
701 G_REGEX_RAW | G_REGEX_MULTILINE, 0, &regex_error);
702 if (ft_regex != NULL)
704 for (i = 0; ft == NULL && lines[i] != NULL; i++)
706 if (g_regex_match(ft_regex, lines[i], 0, &match))
708 gchar *capture = g_match_info_fetch(match, 1);
709 if (capture != NULL)
711 ft = filetypes_lookup_by_name(capture);
712 g_free(capture);
715 g_match_info_free(match);
717 g_regex_unref(ft_regex);
719 else if (regex_error != NULL)
721 geany_debug("Filetype extract regex ignored: %s", regex_error->message);
722 g_error_free(regex_error);
724 if (ft != NULL)
725 return ft;
727 if (utf8_filename == NULL)
728 return filetypes[GEANY_FILETYPES_NONE];
730 return filetypes_detect_from_extension(utf8_filename);
734 /* Detect the filetype for the document, checking for a shebang, then filename extension. */
735 GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
737 GeanyFiletype *ft;
738 gchar *lines[GEANY_FILETYPE_SEARCH_LINES + 1];
739 gint i;
741 g_return_val_if_fail(doc == NULL || doc->is_valid, filetypes[GEANY_FILETYPES_NONE]);
743 if (doc == NULL)
744 return filetypes[GEANY_FILETYPES_NONE];
746 for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
748 lines[i] = sci_get_line(doc->editor->sci, i);
750 lines[i] = NULL;
751 ft = filetypes_detect_from_file_internal(doc->file_name, lines);
752 for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
754 g_free(lines[i]);
756 return ft;
760 #ifdef HAVE_PLUGINS
761 /* Currently only used by external plugins (e.g. geanyprj). */
763 * Detects filetype based on a shebang line in the file or the filename extension.
765 * @param utf8_filename The filename in UTF-8 encoding.
767 * @return The detected filetype for @a utf8_filename or @c filetypes[GEANY_FILETYPES_NONE]
768 * if it could not be detected.
770 GEANY_API_SYMBOL
771 GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename)
773 gchar line[1024];
774 gchar *lines[2];
775 FILE *f;
776 gchar *locale_name = utils_get_locale_from_utf8(utf8_filename);
778 f = g_fopen(locale_name, "r");
779 g_free(locale_name);
780 if (f != NULL)
782 if (fgets(line, sizeof(line), f) != NULL)
784 fclose(f);
785 lines[0] = line;
786 lines[1] = NULL;
787 return filetypes_detect_from_file_internal(utf8_filename, lines);
789 fclose(f);
791 return filetypes_detect_from_extension(utf8_filename);
793 #endif
796 void filetypes_select_radio_item(const GeanyFiletype *ft)
798 /* ignore_callback has to be set by the caller */
799 g_return_if_fail(ignore_callback);
801 if (ft == NULL)
802 ft = filetypes[GEANY_FILETYPES_NONE];
804 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ft->priv->menu_item), TRUE);
808 static void
809 on_filetype_change(GtkCheckMenuItem *menuitem, gpointer user_data)
811 GeanyDocument *doc = document_get_current();
812 if (ignore_callback || doc == NULL || ! gtk_check_menu_item_get_active(menuitem))
813 return;
815 document_set_filetype(doc, (GeanyFiletype*)user_data);
819 static void create_radio_menu_item(GtkWidget *menu, GeanyFiletype *ftype)
821 static GSList *group = NULL;
822 GtkWidget *tmp;
824 tmp = gtk_radio_menu_item_new_with_label(group, ftype->title);
825 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(tmp));
826 ftype->priv->menu_item = tmp;
827 gtk_widget_show(tmp);
828 gtk_container_add(GTK_CONTAINER(menu), tmp);
829 g_signal_connect(tmp, "activate", G_CALLBACK(on_filetype_change), (gpointer) ftype);
833 static void filetype_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
835 GeanyFiletype *ft = data;
837 g_return_if_fail(ft != NULL);
839 g_free(ft->name);
840 g_free(ft->title);
841 g_free(ft->extension);
842 g_free(ft->mime_type);
843 g_free(ft->comment_open);
844 g_free(ft->comment_close);
845 g_free(ft->comment_single);
846 g_free(ft->context_action_cmd);
847 g_free(ft->priv->filecmds);
848 g_free(ft->priv->ftdefcmds);
849 g_free(ft->priv->execcmds);
850 g_free(ft->error_regex_string);
851 if (ft->icon)
852 g_object_unref(ft->icon);
853 g_strfreev(ft->pattern);
855 if (ft->priv->error_regex)
856 g_regex_unref(ft->priv->error_regex);
857 g_slist_foreach(ft->priv->tag_files, (GFunc) g_free, NULL);
858 g_slist_free(ft->priv->tag_files);
860 g_free(ft->priv);
861 g_free(ft);
865 /* frees the array and all related pointers */
866 void filetypes_free_types(void)
868 g_return_if_fail(filetypes_array != NULL);
869 g_return_if_fail(filetypes_hash != NULL);
871 g_ptr_array_foreach(filetypes_array, filetype_free, NULL);
872 g_ptr_array_free(filetypes_array, TRUE);
873 g_hash_table_destroy(filetypes_hash);
877 static void load_indent_settings(GeanyFiletype *ft, GKeyFile *config, GKeyFile *configh)
879 ft->indent_width = utils_get_setting(integer, configh, config, "indentation", "width", -1);
880 ft->indent_type = utils_get_setting(integer, configh, config, "indentation", "type", -1);
881 /* check whether the indent type is OK */
882 switch (ft->indent_type)
884 case GEANY_INDENT_TYPE_TABS:
885 case GEANY_INDENT_TYPE_SPACES:
886 case GEANY_INDENT_TYPE_BOTH:
887 case -1:
888 break;
890 default:
891 g_warning("Invalid indent type %d in file type %s", ft->indent_type, ft->name);
892 ft->indent_type = -1;
893 break;
898 static void load_settings(guint ft_id, GKeyFile *config, GKeyFile *configh)
900 GeanyFiletype *ft = filetypes[ft_id];
901 gchar *result;
903 /* default extension */
904 result = utils_get_setting(string, configh, config, "settings", "extension", NULL);
905 if (result != NULL)
907 SETPTR(filetypes[ft_id]->extension, result);
910 /* MIME type */
911 result = utils_get_setting(string, configh, config, "settings", "mime_type", "text/plain");
912 SETPTR(filetypes[ft_id]->mime_type, result);
914 /* read comment notes */
915 result = utils_get_setting(string, configh, config, "settings", "comment_open", NULL);
916 if (result != NULL)
918 SETPTR(filetypes[ft_id]->comment_open, result);
921 result = utils_get_setting(string, configh, config, "settings", "comment_close", NULL);
922 if (result != NULL)
924 SETPTR(filetypes[ft_id]->comment_close, result);
927 result = utils_get_setting(string, configh, config, "settings", "comment_single", NULL);
928 if (result != NULL)
930 SETPTR(filetypes[ft_id]->comment_single, result);
932 /* import correctly filetypes that use old-style single comments */
933 else if (EMPTY(filetypes[ft_id]->comment_close))
935 SETPTR(filetypes[ft_id]->comment_single, filetypes[ft_id]->comment_open);
936 filetypes[ft_id]->comment_open = NULL;
939 filetypes[ft_id]->comment_use_indent = utils_get_setting(boolean, configh, config,
940 "settings", "comment_use_indent", FALSE);
942 /* read context action */
943 result = utils_get_setting(string, configh, config, "settings", "context_action_cmd", NULL);
944 if (result != NULL)
946 SETPTR(filetypes[ft_id]->context_action_cmd, result);
949 result = utils_get_setting(string, configh, config, "settings", "tag_parser", NULL);
950 if (result != NULL)
952 ft->lang = tm_source_file_get_named_lang(result);
953 if (ft->lang < 0)
954 geany_debug("Cannot find tag parser '%s' for custom filetype '%s'.", result, ft->name);
955 g_free(result);
958 result = utils_get_setting(string, configh, config, "settings", "lexer_filetype", NULL);
959 if (result != NULL)
961 ft->lexer_filetype = filetypes_lookup_by_name(result);
962 if (!ft->lexer_filetype)
963 geany_debug("Cannot find lexer filetype '%s' for custom filetype '%s'.", result, ft->name);
964 g_free(result);
967 ft->priv->symbol_list_sort_mode = utils_get_setting(integer, configh, config, "settings",
968 "symbol_list_sort_mode", SYMBOLS_SORT_BY_NAME);
969 ft->priv->xml_indent_tags = utils_get_setting(boolean, configh, config, "settings",
970 "xml_indent_tags", FALSE);
972 /* read indent settings */
973 load_indent_settings(ft, config, configh);
975 /* read build settings */
976 build_load_menu(config, GEANY_BCS_FT, (gpointer)ft);
977 build_load_menu(configh, GEANY_BCS_HOME_FT, (gpointer)ft);
981 static void copy_keys(GKeyFile *dest, const gchar *dest_group,
982 GKeyFile *src, const gchar *src_group)
984 gchar **keys = g_key_file_get_keys(src, src_group, NULL, NULL);
985 gchar **ptr;
987 foreach_strv(ptr, keys)
989 gchar *key = *ptr;
990 gchar *value = g_key_file_get_value(src, src_group, key, NULL);
992 g_key_file_set_value(dest, dest_group, key, value);
993 g_free(value);
995 g_strfreev(keys);
999 static gchar *filetypes_get_filename(GeanyFiletype *ft, gboolean user)
1001 gchar *ext = filetypes_get_conf_extension(ft);
1002 gchar *base_name = g_strconcat("filetypes.", ext, NULL);
1003 gchar *file_name;
1005 if (user)
1006 file_name = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, base_name, NULL);
1007 else
1008 file_name = g_build_filename(app->datadir, base_name, NULL);
1010 g_free(ext);
1011 g_free(base_name);
1013 return file_name;
1017 static void add_group_keys(GKeyFile *kf, const gchar *group, GeanyFiletype *ft)
1019 gchar *files[2];
1020 gboolean loaded = FALSE;
1021 guint i;
1023 files[0] = filetypes_get_filename(ft, FALSE);
1024 files[1] = filetypes_get_filename(ft, TRUE);
1026 for (i = 0; i < G_N_ELEMENTS(files); i++)
1028 GKeyFile *src = g_key_file_new();
1030 if (g_key_file_load_from_file(src, files[i], G_KEY_FILE_NONE, NULL))
1032 copy_keys(kf, group, src, group);
1033 loaded = TRUE;
1035 g_key_file_free(src);
1038 if (!loaded)
1039 geany_debug("Could not read config file %s for [%s=%s]!", files[0], group, ft->name);
1041 g_free(files[0]);
1042 g_free(files[1]);
1046 static void copy_ft_groups(GKeyFile *kf)
1048 gchar **groups = g_key_file_get_groups(kf, NULL);
1049 gchar **ptr;
1051 foreach_strv(ptr, groups)
1053 gchar *group = *ptr;
1054 gchar *old_group;
1055 gchar *name = strchr(*ptr, '=');
1056 GeanyFiletype *ft;
1058 if (!name || !name[1]) /* no name or no parent name */
1059 continue;
1061 old_group = g_strdup(group);
1063 /* terminate group at '=' */
1064 *name = 0;
1065 name++;
1067 ft = filetypes_lookup_by_name(name);
1068 if (ft)
1070 add_group_keys(kf, group, ft);
1071 /* move old group keys (foo=bar) to proper group name (foo) */
1072 copy_keys(kf, group, kf, old_group);
1074 g_free(old_group);
1076 g_strfreev(groups);
1080 /* simple wrapper function to print file errors in DEBUG mode */
1081 static void load_system_keyfile(GKeyFile *key_file, const gchar *file, GKeyFileFlags flags,
1082 GeanyFiletype *ft)
1084 GError *error = NULL;
1085 gboolean done = g_key_file_load_from_file(key_file, file, flags, &error);
1087 if (error != NULL)
1089 if (!done && !ft->priv->custom)
1090 geany_debug("Failed to open %s (%s)", file, error->message);
1092 g_error_free(error);
1093 error = NULL;
1098 /* Load the configuration file for the associated filetype id.
1099 * This should only be called when the filetype is needed, to save loading
1100 * 20+ configuration files all at once. */
1101 void filetypes_load_config(guint ft_id, gboolean reload)
1103 GKeyFile *config, *config_home;
1104 GeanyFiletypePrivate *pft;
1105 GeanyFiletype *ft;
1107 g_return_if_fail(ft_id < filetypes_array->len);
1109 ft = filetypes[ft_id];
1110 pft = ft->priv;
1112 /* when reloading, proceed only if the settings were already loaded */
1113 if (G_UNLIKELY(reload && ! pft->keyfile_loaded))
1114 return;
1116 /* when not reloading, load the settings only once */
1117 if (G_LIKELY(! reload && pft->keyfile_loaded))
1118 return;
1119 pft->keyfile_loaded = TRUE;
1121 config = g_key_file_new();
1122 config_home = g_key_file_new();
1124 /* highlighting uses GEANY_FILETYPES_NONE for common settings */
1125 gchar *f;
1127 f = filetypes_get_filename(ft, FALSE);
1128 load_system_keyfile(config, f, G_KEY_FILE_KEEP_COMMENTS, ft);
1130 SETPTR(f, filetypes_get_filename(ft, TRUE));
1131 g_key_file_load_from_file(config_home, f, G_KEY_FILE_KEEP_COMMENTS, NULL);
1132 g_free(f);
1134 /* Copy keys for any groups with [group=C] from system keyfile */
1135 copy_ft_groups(config);
1136 copy_ft_groups(config_home);
1138 load_settings(ft_id, config, config_home);
1139 highlighting_init_styles(ft_id, config, config_home);
1141 if (ft->icon)
1142 g_object_unref(ft->icon);
1143 ft->icon = ui_get_mime_icon(ft->mime_type);
1145 g_key_file_free(config);
1146 g_key_file_free(config_home);
1150 static gchar *filetypes_get_conf_extension(const GeanyFiletype *ft)
1152 gchar *result;
1154 if (ft->priv->custom)
1155 return g_strconcat(ft->name, ".conf", NULL);
1157 /* Handle any special extensions different from lowercase filetype->name */
1158 switch (ft->id)
1160 case GEANY_FILETYPES_CPP: result = g_strdup("cpp"); break;
1161 case GEANY_FILETYPES_CS: result = g_strdup("cs"); break;
1162 case GEANY_FILETYPES_MAKE: result = g_strdup("makefile"); break;
1163 case GEANY_FILETYPES_NONE: result = g_strdup("common"); break;
1164 /* name is Matlab/Octave */
1165 case GEANY_FILETYPES_MATLAB: result = g_strdup("matlab"); break;
1166 /* name is Objective-C, and we don't want the hyphen */
1167 case GEANY_FILETYPES_OBJECTIVEC: result = g_strdup("objectivec"); break;
1168 default:
1169 result = g_ascii_strdown(ft->name, -1);
1170 break;
1172 return result;
1176 void filetypes_save_commands(GeanyFiletype *ft)
1178 GKeyFile *config_home;
1179 gchar *fname, *data;
1181 fname = filetypes_get_filename(ft, TRUE);
1182 config_home = g_key_file_new();
1183 g_key_file_load_from_file(config_home, fname, G_KEY_FILE_KEEP_COMMENTS, NULL);
1184 build_save_menu(config_home, ft, GEANY_BCS_HOME_FT);
1185 data = g_key_file_to_data(config_home, NULL, NULL);
1186 utils_write_file(fname, data);
1187 g_free(data);
1188 g_key_file_free(config_home);
1189 g_free(fname);
1193 /* create one file filter which has each file pattern of each filetype */
1194 GtkFileFilter *filetypes_create_file_filter_all_source(void)
1196 GtkFileFilter *new_filter;
1197 guint i, j;
1199 new_filter = gtk_file_filter_new();
1200 gtk_file_filter_set_name(new_filter, _("All Source"));
1202 for (i = 0; i < filetypes_array->len; i++)
1204 if (G_UNLIKELY(i == GEANY_FILETYPES_NONE))
1205 continue;
1207 for (j = 0; filetypes[i]->pattern[j]; j++)
1209 gtk_file_filter_add_pattern(new_filter, filetypes[i]->pattern[j]);
1212 return new_filter;
1216 GtkFileFilter *filetypes_create_file_filter(const GeanyFiletype *ft)
1218 GtkFileFilter *new_filter;
1219 gint i;
1220 const gchar *title;
1222 g_return_val_if_fail(ft != NULL, NULL);
1224 new_filter = gtk_file_filter_new();
1225 title = ft->id == GEANY_FILETYPES_NONE ? _("All files") : ft->title;
1226 gtk_file_filter_set_name(new_filter, title);
1228 for (i = 0; ft->pattern[i]; i++)
1230 gtk_file_filter_add_pattern(new_filter, ft->pattern[i]);
1233 return new_filter;
1237 /* Indicates whether there is a tag parser for the filetype or not.
1238 * Only works for custom filetypes if the filetype settings have been loaded. */
1239 gboolean filetype_has_tags(GeanyFiletype *ft)
1241 g_return_val_if_fail(ft != NULL, FALSE);
1243 return ft->lang >= 0;
1247 /** Finds a filetype pointer from its @a name field.
1248 * @param name Filetype name.
1249 * @return The filetype found, or @c NULL.
1251 * @since 0.15
1253 GEANY_API_SYMBOL
1254 GeanyFiletype *filetypes_lookup_by_name(const gchar *name)
1256 GeanyFiletype *ft;
1258 g_return_val_if_fail(!EMPTY(name), NULL);
1260 ft = g_hash_table_lookup(filetypes_hash, name);
1261 if (G_UNLIKELY(ft == NULL))
1262 geany_debug("Could not find filetype '%s'.", name);
1263 return ft;
1267 static void compile_regex(GeanyFiletype *ft, gchar *regstr)
1269 GError *error = NULL;
1270 GRegex *regex = g_regex_new(regstr, 0, 0, &error);
1272 if (!regex)
1274 ui_set_statusbar(TRUE, _("Bad regex for filetype %s: %s"),
1275 filetypes_get_display_name(ft), error->message);
1276 g_error_free(error);
1278 if (ft->priv->error_regex)
1279 g_regex_unref(ft->priv->error_regex);
1280 ft->priv->error_regex = regex;
1284 gboolean filetypes_parse_error_message(GeanyFiletype *ft, const gchar *message,
1285 gchar **filename, gint *line)
1287 gchar *regstr;
1288 gchar **tmp;
1289 GeanyDocument *doc;
1290 GMatchInfo *minfo;
1291 gint i, n_match_groups;
1292 gchar *first, *second;
1294 if (ft == NULL)
1296 doc = document_get_current();
1297 if (doc != NULL)
1298 ft = doc->file_type;
1300 tmp = build_get_regex(build_info.grp, ft, NULL);
1301 if (tmp == NULL)
1302 return FALSE;
1303 regstr = *tmp;
1305 *filename = NULL;
1306 *line = -1;
1308 if (G_UNLIKELY(EMPTY(regstr)))
1309 return FALSE;
1311 if (!ft->priv->error_regex || regstr != ft->priv->last_error_pattern)
1313 compile_regex(ft, regstr);
1314 ft->priv->last_error_pattern = regstr;
1316 if (!ft->priv->error_regex)
1317 return FALSE;
1319 if (!g_regex_match(ft->priv->error_regex, message, 0, &minfo))
1321 g_match_info_free(minfo);
1322 return FALSE;
1325 n_match_groups = g_match_info_get_match_count(minfo);
1326 first = second = NULL;
1328 for (i = 1; i < n_match_groups; i++)
1330 gint start_pos;
1332 g_match_info_fetch_pos(minfo, i, &start_pos, NULL);
1333 if (start_pos != -1)
1335 if (first == NULL)
1336 first = g_match_info_fetch(minfo, i);
1337 else
1339 second = g_match_info_fetch(minfo, i);
1340 break;
1345 if (second)
1347 gchar *end;
1348 glong l;
1350 l = strtol(first, &end, 10);
1351 if (*end == '\0') /* first is purely decimals */
1353 *line = l;
1354 g_free(first);
1355 *filename = second;
1357 else
1359 l = strtol(second, &end, 10);
1360 if (*end == '\0')
1362 *line = l;
1363 g_free(second);
1364 *filename = first;
1366 else
1368 g_free(first);
1369 g_free(second);
1373 else
1374 g_free(first);
1376 g_match_info_free(minfo);
1377 return *filename != NULL;
1381 #ifdef G_OS_WIN32
1382 static void convert_filetype_extensions_to_lower_case(gchar **patterns, gsize len)
1384 guint i;
1385 for (i = 0; i < len; i++)
1387 SETPTR(patterns[i], g_ascii_strdown(patterns[i], -1));
1390 #endif
1393 static void read_extensions(GKeyFile *sysconfig, GKeyFile *userconfig)
1395 guint i;
1396 gsize len = 0;
1398 /* read the keys */
1399 for (i = 0; i < filetypes_array->len; i++)
1401 gboolean userset =
1402 g_key_file_has_key(userconfig, "Extensions", filetypes[i]->name, NULL);
1403 gchar **list = g_key_file_get_string_list(
1404 (userset) ? userconfig : sysconfig, "Extensions", filetypes[i]->name, &len, NULL);
1406 g_strfreev(filetypes[i]->pattern);
1407 /* Note: we allow 'Foo=' to remove all patterns */
1408 if (!list)
1409 list = g_new0(gchar*, 1);
1410 filetypes[i]->pattern = list;
1412 #ifdef G_OS_WIN32
1413 convert_filetype_extensions_to_lower_case(filetypes[i]->pattern, len);
1414 #endif
1419 static void read_group(GKeyFile *config, const gchar *group_name, GeanyFiletypeGroupID group_id)
1421 gchar **names = g_key_file_get_string_list(config, "Groups", group_name, NULL, NULL);
1422 gchar **name;
1424 foreach_strv(name, names)
1426 GeanyFiletype *ft = filetypes_lookup_by_name(*name);
1428 if (ft)
1430 ft->group = group_id;
1431 if (ft->priv->custom &&
1432 (group_id == GEANY_FILETYPE_GROUP_COMPILED || group_id == GEANY_FILETYPE_GROUP_SCRIPT))
1434 SETPTR(ft->title, filetype_make_title(ft->name, TITLE_SOURCE_FILE));
1437 else
1438 geany_debug("Filetype '%s' not found for group '%s'!", *name, group_name);
1440 g_strfreev(names);
1444 static void read_groups(GKeyFile *config)
1446 read_group(config, "Programming", GEANY_FILETYPE_GROUP_COMPILED);
1447 read_group(config, "Script", GEANY_FILETYPE_GROUP_SCRIPT);
1448 read_group(config, "Markup", GEANY_FILETYPE_GROUP_MARKUP);
1449 read_group(config, "Misc", GEANY_FILETYPE_GROUP_MISC);
1450 read_group(config, "None", GEANY_FILETYPE_GROUP_NONE);
1454 static void read_filetype_config(void)
1456 gchar *sysconfigfile = g_build_filename(app->datadir, "filetype_extensions.conf", NULL);
1457 gchar *userconfigfile = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
1458 GKeyFile *sysconfig = g_key_file_new();
1459 GKeyFile *userconfig = g_key_file_new();
1461 g_key_file_load_from_file(sysconfig, sysconfigfile, G_KEY_FILE_NONE, NULL);
1462 g_key_file_load_from_file(userconfig, userconfigfile, G_KEY_FILE_NONE, NULL);
1464 read_extensions(sysconfig, userconfig);
1465 read_groups(sysconfig);
1466 read_groups(userconfig);
1468 g_free(sysconfigfile);
1469 g_free(userconfigfile);
1470 g_key_file_free(sysconfig);
1471 g_key_file_free(userconfig);
1475 void filetypes_reload_extensions(void)
1477 guint i;
1479 read_filetype_config();
1481 /* Redetect filetype of any documents with none set */
1482 foreach_document(i)
1484 GeanyDocument *doc = documents[i];
1485 if (doc->file_type->id != GEANY_FILETYPES_NONE)
1486 continue;
1487 document_set_filetype(doc, filetypes_detect_from_document(doc));
1492 /** Accessor function for @ref GeanyData::filetypes_array items.
1493 * Example: @code ft = filetypes_index(GEANY_FILETYPES_C); @endcode
1494 * @param idx @c filetypes_array index.
1495 * @return The filetype, or @c NULL if @a idx is out of range.
1497 * @since 0.16
1499 GEANY_API_SYMBOL
1500 GeanyFiletype *filetypes_index(gint idx)
1502 return (idx >= 0 && idx < (gint) filetypes_array->len) ? filetypes[idx] : NULL;
1506 void filetypes_reload(void)
1508 guint i;
1509 GeanyDocument *current_doc;
1511 /* reload filetype configs */
1512 for (i = 0; i < filetypes_array->len; i++)
1514 /* filetypes_load_config() will skip not loaded filetypes */
1515 filetypes_load_config(i, TRUE);
1518 current_doc = document_get_current();
1519 if (!current_doc)
1520 return;
1522 /* update document styling */
1523 foreach_document(i)
1525 if (current_doc != documents[i])
1526 document_reload_config(documents[i]);
1528 /* process the current document at last */
1529 document_reload_config(current_doc);
1533 /** Gets @c ft->name or a translation for filetype None.
1534 * @param ft .
1535 * @return .
1536 * @since Geany 0.20 */
1537 GEANY_API_SYMBOL
1538 const gchar *filetypes_get_display_name(GeanyFiletype *ft)
1540 return ft->id == GEANY_FILETYPES_NONE ? _("None") : ft->name;
1544 /* gets comment_open/comment_close/comment_single strings from the filetype
1545 * @param single_first: whether single comment is preferred if both available
1546 * returns true if at least comment_open is set, false otherwise */
1547 gboolean filetype_get_comment_open_close(const GeanyFiletype *ft, gboolean single_first,
1548 const gchar **co, const gchar **cc)
1550 g_return_val_if_fail(ft != NULL, FALSE);
1551 g_return_val_if_fail(co != NULL, FALSE);
1552 g_return_val_if_fail(cc != NULL, FALSE);
1554 if (single_first)
1556 *co = ft->comment_single;
1557 if (!EMPTY(*co))
1558 *cc = NULL;
1559 else
1561 *co = ft->comment_open;
1562 *cc = ft->comment_close;
1565 else
1567 *co = ft->comment_open;
1568 if (!EMPTY(*co))
1569 *cc = ft->comment_close;
1570 else
1572 *co = ft->comment_single;
1573 *cc = NULL;
1577 return !EMPTY(*co);