Remove unused functions from options.c/h
[geany-mirror.git] / src / filetypes.c
blobee2313c0aeb6426b0ce0399fc184c46ba8d4efe4
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 GeanyFiletypeID 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;
59 static GHashTable *filetypes_hash = NULL; /* Hash of filetype pointers based on name keys */
60 GSList *filetypes_by_title = NULL;
63 static void create_radio_menu_item(GtkWidget *menu, GeanyFiletype *ftype);
65 static gchar *filetypes_get_conf_extension(const GeanyFiletype *ft);
66 static void read_filetype_config(void);
67 static void create_set_filetype_menu(gboolean config);
68 static gchar *filetypes_get_filename(GeanyFiletype *ft, gboolean user);
71 enum TitleType
73 TITLE_NONE,
74 TITLE_SOURCE_FILE,
75 TITLE_FILE,
76 TITLE_SCRIPT,
77 TITLE_DOCUMENT
80 /* Save adding many translation strings if the filetype name doesn't need translating */
81 static gchar *filetype_make_title(const char *name, enum TitleType type)
83 g_return_val_if_fail(name != NULL, NULL);
85 switch (type)
87 case TITLE_SOURCE_FILE: return g_strdup_printf(_("%s source file"), name);
88 case TITLE_FILE: return g_strdup_printf(_("%s file"), name);
89 case TITLE_SCRIPT: return g_strdup_printf(_("%s script"), name);
90 case TITLE_DOCUMENT: return g_strdup_printf(_("%s document"), name);
91 case TITLE_NONE: /* fall through */
92 default: return g_strdup(name);
97 /* name argument (ie filetype name) must not be translated as it is used for
98 * filetype lookup. Use filetypes_get_display_name() instead.*/
99 static void ft_init(GeanyFiletypeID ft_id, TMParserType lang, const char *name,
100 const char *title_name, enum TitleType title_type,
101 GeanyFiletypeGroupID group_id)
103 GeanyFiletype *ft = filetypes[ft_id];
104 ft->lang = lang;
105 ft->name = g_strdup(name);
106 ft->title = filetype_make_title((title_name != NULL) ? title_name : ft->name, title_type);
107 ft->group = group_id;
110 /* Evil macro to save typing and make init_builtin_filetypes() more readable */
111 #define FT_INIT(ft_id, parser_id, name, title_name, title_type, group_id) \
112 ft_init(GEANY_FILETYPES_##ft_id, TM_PARSER_##parser_id, name, title_name, \
113 TITLE_##title_type, GEANY_FILETYPE_GROUP_##group_id)
116 /* Note: remember to update HACKING if this function is renamed. */
117 static void init_builtin_filetypes(void)
119 /* Column legend:
120 * [0] = Filetype constant (GEANY_FILETYPES_*)
121 * [1] = CTags parser (TM_PARSER_*)
122 * [2] = Non-translated filetype name (*not* label for display)
123 * [3] = Translatable human filetype title prefix or NULL to use [2]
124 * [4] = Title type (TITLE_*) constant (ex. TITLE_SOURCE_FILE is 'source file' suffix)
125 * [5] = The filetype group constant (GEANY_FILETYPE_GROUP_*)
126 * --------------------------------------------------------------------------------------------------------------------------
127 * [0] [1] [2] [3] [4] [5] */
128 FT_INIT( NONE, NONE, "None", _("None"), NONE, NONE );
129 FT_INIT( C, C, "C", NULL, SOURCE_FILE, COMPILED );
130 FT_INIT( CPP, CPP, "C++", NULL, SOURCE_FILE, COMPILED );
131 FT_INIT( OBJECTIVEC, OBJC, "Objective-C", NULL, SOURCE_FILE, COMPILED );
132 FT_INIT( CS, CSHARP, "C#", NULL, SOURCE_FILE, COMPILED );
133 FT_INIT( VALA, VALA, "Vala", NULL, SOURCE_FILE, COMPILED );
134 FT_INIT( D, D, "D", NULL, SOURCE_FILE, COMPILED );
135 FT_INIT( JAVA, JAVA, "Java", NULL, SOURCE_FILE, COMPILED );
136 FT_INIT( PASCAL, PASCAL, "Pascal", NULL, SOURCE_FILE, COMPILED );
137 FT_INIT( ASM, ASM, "ASM", "Assembler", SOURCE_FILE, COMPILED );
138 FT_INIT( BASIC, FREEBASIC, "FreeBasic", NULL, SOURCE_FILE, COMPILED );
139 FT_INIT( FORTRAN, FORTRAN, "Fortran", "Fortran (F90)", SOURCE_FILE, COMPILED );
140 FT_INIT( F77, F77, "F77", "Fortran (F77)", SOURCE_FILE, COMPILED );
141 FT_INIT( GLSL, GLSL, "GLSL", NULL, SOURCE_FILE, COMPILED );
142 FT_INIT( CAML, NONE, "CAML", "(O)Caml", SOURCE_FILE, COMPILED );
143 FT_INIT( PERL, PERL, "Perl", NULL, SOURCE_FILE, SCRIPT );
144 FT_INIT( PHP, PHP, "PHP", NULL, SOURCE_FILE, SCRIPT );
145 FT_INIT( JS, JAVASCRIPT, "Javascript", NULL, SOURCE_FILE, SCRIPT );
146 FT_INIT( PYTHON, PYTHON, "Python", NULL, SOURCE_FILE, SCRIPT );
147 FT_INIT( RUBY, RUBY, "Ruby", NULL, SOURCE_FILE, SCRIPT );
148 FT_INIT( TCL, TCL, "Tcl", NULL, SOURCE_FILE, SCRIPT );
149 FT_INIT( LUA, LUA, "Lua", NULL, SOURCE_FILE, SCRIPT );
150 FT_INIT( FERITE, FERITE, "Ferite", NULL, SOURCE_FILE, SCRIPT );
151 FT_INIT( HASKELL, HASKELL, "Haskell", NULL, SOURCE_FILE, COMPILED );
152 FT_INIT( MARKDOWN, MARKDOWN, "Markdown", NULL, SOURCE_FILE, MARKUP );
153 FT_INIT( TXT2TAGS, TXT2TAGS, "Txt2tags", NULL, SOURCE_FILE, MARKUP );
154 FT_INIT( ABC, ABC, "Abc", NULL, FILE, MISC );
155 FT_INIT( SH, SH, "Sh", _("Shell"), SCRIPT, SCRIPT );
156 FT_INIT( MAKE, MAKEFILE, "Make", _("Makefile"), NONE, SCRIPT );
157 FT_INIT( XML, NONE, "XML", NULL, DOCUMENT, MARKUP );
158 FT_INIT( DOCBOOK, DOCBOOK, "Docbook", NULL, DOCUMENT, MARKUP );
159 FT_INIT( HTML, HTML, "HTML", NULL, DOCUMENT, MARKUP );
160 FT_INIT( CSS, CSS, "CSS", _("Cascading Stylesheet"), NONE, MARKUP ); /* not really markup but fit quite well to HTML */
161 FT_INIT( SQL, SQL, "SQL", NULL, FILE, MISC );
162 FT_INIT( COBOL, COBOL, "COBOL", NULL, SOURCE_FILE, COMPILED );
163 FT_INIT( LATEX, LATEX, "LaTeX", NULL, SOURCE_FILE, MARKUP );
164 FT_INIT( VHDL, VHDL, "VHDL", NULL, SOURCE_FILE, COMPILED );
165 FT_INIT( VERILOG, VERILOG, "Verilog", NULL, SOURCE_FILE, COMPILED );
166 FT_INIT( DIFF, DIFF, "Diff", NULL, FILE, MISC );
167 FT_INIT( LISP, NONE, "Lisp", NULL, SOURCE_FILE, SCRIPT );
168 FT_INIT( ERLANG, ERLANG, "Erlang", NULL, SOURCE_FILE, COMPILED );
169 FT_INIT( CONF, CONF, "Conf", _("Config"), FILE, MISC );
170 FT_INIT( PO, NONE, "Po", _("Gettext translation"), FILE, MISC );
171 FT_INIT( HAXE, HAXE, "Haxe", NULL, SOURCE_FILE, COMPILED );
172 FT_INIT( AS, ACTIONSCRIPT, "ActionScript", NULL, SOURCE_FILE, SCRIPT );
173 FT_INIT( R, R, "R", NULL, SOURCE_FILE, SCRIPT );
174 FT_INIT( REST, REST, "reStructuredText", NULL, SOURCE_FILE, MARKUP );
175 FT_INIT( MATLAB, MATLAB, "Matlab/Octave", NULL, SOURCE_FILE, SCRIPT );
176 FT_INIT( YAML, NONE, "YAML", NULL, FILE, MISC );
177 FT_INIT( CMAKE, NONE, "CMake", NULL, SOURCE_FILE, SCRIPT );
178 FT_INIT( NSIS, NSIS, "NSIS", NULL, SOURCE_FILE, SCRIPT );
179 FT_INIT( ADA, NONE, "Ada", NULL, SOURCE_FILE, COMPILED );
180 FT_INIT( FORTH, NONE, "Forth", NULL, SOURCE_FILE, SCRIPT );
181 FT_INIT( ASCIIDOC, ASCIIDOC, "Asciidoc", NULL, SOURCE_FILE, MARKUP );
182 FT_INIT( ABAQUS, ABAQUS, "Abaqus", NULL, SOURCE_FILE, SCRIPT );
183 FT_INIT( BATCH, NONE, "Batch", NULL, SCRIPT, SCRIPT );
184 FT_INIT( POWERSHELL, POWERSHELL, "PowerShell", NULL, SOURCE_FILE, SCRIPT );
185 FT_INIT( RUST, RUST, "Rust", NULL, SOURCE_FILE, COMPILED );
186 FT_INIT( COFFEESCRIPT, NONE, "CoffeeScript", NULL, SOURCE_FILE, SCRIPT );
187 FT_INIT( GO, GO, "Go", NULL, SOURCE_FILE, COMPILED );
188 FT_INIT( ZEPHIR, ZEPHIR, "Zephir", NULL, SOURCE_FILE, COMPILED );
192 /* initialize fields. */
193 static GeanyFiletype *filetype_new(void)
195 GeanyFiletype *ft = g_new0(GeanyFiletype, 1);
197 ft->group = GEANY_FILETYPE_GROUP_NONE;
198 ft->lang = TM_PARSER_NONE; /* assume no tagmanager parser */
199 /* pattern must not be null */
200 ft->pattern = g_new0(gchar*, 1);
201 ft->indent_width = -1;
202 ft->indent_type = -1;
204 ft->priv = g_new0(GeanyFiletypePrivate, 1);
205 ft->priv->project_list_entry = -1; /* no entry */
207 return ft;
211 static gint cmp_filetype(gconstpointer pft1, gconstpointer pft2, gpointer data)
213 gboolean by_name = GPOINTER_TO_INT(data);
214 const GeanyFiletype *ft1 = pft1, *ft2 = pft2;
216 if (G_UNLIKELY(ft1->id == GEANY_FILETYPES_NONE))
217 return -1;
218 if (G_UNLIKELY(ft2->id == GEANY_FILETYPES_NONE))
219 return 1;
221 return by_name ?
222 utils_str_casecmp(ft1->name, ft2->name) :
223 utils_str_casecmp(ft1->title, ft2->title);
227 /** Gets a list of filetype pointers sorted by name.
228 * The list does not change on subsequent calls.
229 * @return @elementtype{GeanyFiletype} @transfer{none} The list - do not free.
230 * @see filetypes_by_title. */
231 GEANY_API_SYMBOL
232 const GSList *filetypes_get_sorted_by_name(void)
234 static GSList *list = NULL;
236 g_return_val_if_fail(filetypes_by_title, NULL);
238 if (!list)
240 list = g_slist_copy(filetypes_by_title);
241 list = g_slist_sort_with_data(list, cmp_filetype, GINT_TO_POINTER(TRUE));
243 return list;
247 /* Add a filetype pointer to the lists of available filetypes,
248 * and set the filetype::id field. */
249 static void filetype_add(GeanyFiletype *ft)
251 g_return_if_fail(ft);
252 g_return_if_fail(ft->name);
254 ft->id = filetypes_array->len; /* len will be the index for filetype_array */
255 g_ptr_array_add(filetypes_array, ft);
256 g_hash_table_insert(filetypes_hash, ft->name, ft);
258 /* list will be sorted later */
259 filetypes_by_title = g_slist_prepend(filetypes_by_title, ft);
263 static void add_custom_filetype(const gchar *filename)
265 gchar *fn = utils_strdupa(strstr(filename, ".") + 1);
266 gchar *dot = g_strrstr(fn, ".conf");
267 GeanyFiletype *ft;
269 g_return_if_fail(dot);
271 *dot = 0x0;
273 if (g_hash_table_lookup(filetypes_hash, fn))
274 return;
276 ft = filetype_new();
277 ft->name = g_strdup(fn);
278 ft->title = filetype_make_title(ft->name, TITLE_FILE);
279 ft->priv->custom = TRUE;
280 filetype_add(ft);
281 geany_debug("Added filetype %s (%d).", ft->name, ft->id);
285 static void init_custom_filetypes(const gchar *path)
287 GDir *dir;
288 const gchar *filename;
290 g_return_if_fail(path);
292 dir = g_dir_open(path, 0, NULL);
293 if (dir == NULL)
294 return;
296 foreach_dir(filename, dir)
298 const gchar prefix[] = "filetypes.";
300 if (g_str_has_prefix(filename, prefix) &&
301 g_str_has_suffix(filename + strlen(prefix), ".conf"))
303 add_custom_filetype(filename);
306 g_dir_close(dir);
310 /* Create the filetypes array and fill it with the known filetypes.
311 * Warning: GTK isn't necessarily initialized yet. */
312 void filetypes_init_types(void)
314 GeanyFiletypeID ft_id;
315 gchar *f;
317 g_return_if_fail(filetypes_array == NULL);
318 g_return_if_fail(filetypes_hash == NULL);
320 filetypes_array = g_ptr_array_sized_new(GEANY_MAX_BUILT_IN_FILETYPES);
321 filetypes_hash = g_hash_table_new(g_str_hash, g_str_equal);
323 /* Create built-in filetypes */
324 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
326 filetypes[ft_id] = filetype_new();
328 init_builtin_filetypes();
330 /* Add built-in filetypes to the hash now the name fields are set */
331 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
333 filetype_add(filetypes[ft_id]);
335 f = g_build_filename(app->datadir, GEANY_FILEDEFS_SUBDIR, NULL);
336 init_custom_filetypes(f);
337 g_free(f);
338 f = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
339 init_custom_filetypes(f);
340 g_free(f);
342 /* sort last instead of on insertion to prevent exponential time */
343 filetypes_by_title = g_slist_sort_with_data(filetypes_by_title,
344 cmp_filetype, GINT_TO_POINTER(FALSE));
346 read_filetype_config();
350 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
352 gchar *f, *basename;
354 g_return_if_fail(!EMPTY(doc->real_path));
356 f = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
357 if (utils_str_equal(doc->real_path, f))
358 filetypes_reload_extensions();
359 g_free(f);
361 basename = g_path_get_basename(doc->real_path);
362 if (g_str_has_prefix(basename, "filetypes."))
364 guint i;
366 for (i = 0; i < filetypes_array->len; i++)
368 GeanyFiletype *ft = filetypes[i];
370 f = filetypes_get_filename(ft, TRUE);
371 if (utils_str_equal(doc->real_path, f))
373 guint j;
375 /* Note: we don't reload other filetypes, even though the named styles may have changed.
376 * The user can do this manually with 'Tools->Reload Configuration' */
377 filetypes_load_config(i, TRUE);
379 foreach_document(j)
380 document_reload_config(documents[j]);
382 g_free(f);
383 break;
385 g_free(f);
388 g_free(basename);
392 static void setup_config_file_menus(void)
394 gchar *f;
396 f = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
397 ui_add_config_file_menu_item(f, NULL, NULL);
398 SETPTR(f, g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL));
399 ui_add_config_file_menu_item(f, NULL, NULL);
400 g_free(f);
402 create_set_filetype_menu(TRUE);
404 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
408 static GtkWidget *create_sub_menu(GtkWidget *parent, const gchar *title)
410 GtkWidget *menu, *item;
412 menu = gtk_menu_new();
413 item = gtk_menu_item_new_with_mnemonic((title));
414 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
415 gtk_container_add(GTK_CONTAINER(parent), item);
416 gtk_widget_show(item);
418 return menu;
422 static void create_set_filetype_menu(gboolean config)
424 GtkWidget *group_menus[GEANY_FILETYPE_GROUP_COUNT] = {NULL};
425 GSList *node;
426 GtkWidget *menu;
428 menu = config ? ui_widgets.config_files_filetype_menu :
429 ui_lookup_widget(main_widgets.window, "set_filetype1_menu");
431 group_menus[GEANY_FILETYPE_GROUP_COMPILED] = create_sub_menu(menu, _("_Programming Languages"));
432 group_menus[GEANY_FILETYPE_GROUP_SCRIPT] = create_sub_menu(menu, _("_Scripting Languages"));
433 group_menus[GEANY_FILETYPE_GROUP_MARKUP] = create_sub_menu(menu, _("_Markup Languages"));
434 group_menus[GEANY_FILETYPE_GROUP_MISC] = create_sub_menu(menu, _("M_iscellaneous"));
436 /* Append all filetypes to the menu */
437 foreach_slist(node, filetypes_by_title)
439 GeanyFiletype *ft = node->data;
440 GtkWidget *parent = (ft->group != GEANY_FILETYPE_GROUP_NONE) ? group_menus[ft->group] : menu;
442 /* we already have filetypes.common config entry */
443 if (config && ft->id == GEANY_FILETYPES_NONE)
444 continue;
446 if (config)
448 gchar *filename = filetypes_get_filename(ft, TRUE);
450 ui_add_config_file_menu_item(filename, NULL, GTK_CONTAINER(parent));
451 g_free(filename);
453 else
454 create_radio_menu_item(parent, ft);
459 void filetypes_init(void)
461 filetypes_init_types();
462 create_set_filetype_menu(FALSE);
463 setup_config_file_menus();
467 /* Find a filetype that predicate returns TRUE for, otherwise return NULL. */
468 GeanyFiletype *filetypes_find(GCompareFunc predicate, gpointer user_data)
470 guint i;
472 for (i = 0; i < filetypes_array->len; i++)
474 GeanyFiletype *ft = filetypes[i];
476 if (predicate(ft, user_data))
477 return ft;
479 return NULL;
483 static gboolean match_basename(gconstpointer pft, gconstpointer user_data)
485 const GeanyFiletype *ft = pft;
486 const gchar *base_filename = user_data;
487 gint j;
488 gboolean ret = FALSE;
490 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
491 return FALSE;
493 for (j = 0; ft->pattern[j] != NULL; j++)
495 GPatternSpec *pattern = g_pattern_spec_new(ft->pattern[j]);
497 if (g_pattern_match_string(pattern, base_filename))
499 ret = TRUE;
500 g_pattern_spec_free(pattern);
501 break;
503 g_pattern_spec_free(pattern);
505 return ret;
509 static GeanyFiletype *check_builtin_filenames(const gchar *utf8_filename)
511 gchar *lfn = NULL;
512 gchar *path;
513 gboolean found = FALSE;
515 #ifdef G_OS_WIN32
516 /* use lower case basename */
517 lfn = g_utf8_strdown(utf8_filename, -1);
518 #else
519 lfn = g_strdup(utf8_filename);
520 #endif
521 SETPTR(lfn, utils_get_locale_from_utf8(lfn));
523 path = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.", NULL);
524 if (g_str_has_prefix(lfn, path))
525 found = TRUE;
527 SETPTR(path, g_build_filename(app->datadir, GEANY_FILEDEFS_SUBDIR, "filetypes.", NULL));
528 if (g_str_has_prefix(lfn, path))
529 found = TRUE;
531 g_free(path);
532 g_free(lfn);
533 return found ? filetypes[GEANY_FILETYPES_CONF] : NULL;
537 /* Detect filetype only based on the filename extension.
538 * utf8_filename can include the full path. */
539 GeanyFiletype *filetypes_detect_from_extension(const gchar *utf8_filename)
541 gchar *base_filename;
542 GeanyFiletype *ft;
544 ft = check_builtin_filenames(utf8_filename);
545 if (ft)
546 return ft;
548 /* to match against the basename of the file (because of Makefile*) */
549 base_filename = g_path_get_basename(utf8_filename);
550 #ifdef G_OS_WIN32
551 /* use lower case basename */
552 SETPTR(base_filename, g_utf8_strdown(base_filename, -1));
553 #endif
555 ft = filetypes_find(match_basename, base_filename);
556 if (ft == NULL)
557 ft = filetypes[GEANY_FILETYPES_NONE];
559 g_free(base_filename);
560 return ft;
564 /* This detects the filetype of the file pointed by 'utf8_filename' and a list of filetype id's,
565 * terminated by -1.
566 * The detected filetype of the file is checked against every id in the passed list and if
567 * there is a match, TRUE is returned. */
568 static gboolean shebang_find_and_match_filetype(const gchar *utf8_filename, gint first, ...)
570 GeanyFiletype *ft = NULL;
571 gint test;
572 gboolean result = FALSE;
573 va_list args;
575 ft = filetypes_detect_from_extension(utf8_filename);
576 if (ft == NULL || ft->id >= filetypes_array->len)
577 return FALSE;
579 va_start(args, first);
580 test = first;
581 while (1)
583 if (test == -1)
584 break;
586 if (ft->id == (guint) test)
588 result = TRUE;
589 break;
591 test = va_arg(args, gint);
593 va_end(args);
595 return result;
599 static GeanyFiletype *find_shebang(const gchar *utf8_filename, const gchar *line)
601 GeanyFiletype *ft = NULL;
603 if (strlen(line) > 2 && line[0] == '#' && line[1] == '!')
605 static const struct {
606 const gchar *name;
607 GeanyFiletypeID filetype;
608 } intepreter_map[] = {
609 { "sh", GEANY_FILETYPES_SH },
610 { "bash", GEANY_FILETYPES_SH },
611 { "dash", GEANY_FILETYPES_SH },
612 { "perl", GEANY_FILETYPES_PERL },
613 { "python", GEANY_FILETYPES_PYTHON },
614 { "php", GEANY_FILETYPES_PHP },
615 { "ruby", GEANY_FILETYPES_RUBY },
616 { "tcl", GEANY_FILETYPES_TCL },
617 { "make", GEANY_FILETYPES_MAKE },
618 { "zsh", GEANY_FILETYPES_SH },
619 { "ksh", GEANY_FILETYPES_SH },
620 { "mksh", GEANY_FILETYPES_SH },
621 { "csh", GEANY_FILETYPES_SH },
622 { "tcsh", GEANY_FILETYPES_SH },
623 { "ash", GEANY_FILETYPES_SH },
624 { "dmd", GEANY_FILETYPES_D },
625 { "wish", GEANY_FILETYPES_TCL },
626 { "node", GEANY_FILETYPES_JS },
627 { "rust", GEANY_FILETYPES_RUST }
629 gchar *tmp = g_path_get_basename(line + 2);
630 gchar *basename_interpreter = tmp;
631 guint i;
633 if (g_str_has_prefix(tmp, "env "))
634 { /* skip "env" and read the following interpreter */
635 basename_interpreter += 4;
638 for (i = 0; ! ft && i < G_N_ELEMENTS(intepreter_map); i++)
640 if (g_str_has_prefix(basename_interpreter, intepreter_map[i].name))
641 ft = filetypes[intepreter_map[i].filetype];
643 g_free(tmp);
645 /* detect HTML files */
646 if (g_str_has_prefix(line, "<!DOCTYPE html") || g_str_has_prefix(line, "<html"))
648 /* PHP, Perl and Python files might also start with <html, so detect them based on filename
649 * extension and use the detected filetype, else assume HTML */
650 if (! shebang_find_and_match_filetype(utf8_filename,
651 GEANY_FILETYPES_PERL, GEANY_FILETYPES_PHP, GEANY_FILETYPES_PYTHON, -1))
653 ft = filetypes[GEANY_FILETYPES_HTML];
656 /* detect XML files */
657 else if (utf8_filename && g_str_has_prefix(line, "<?xml"))
659 /* HTML and DocBook files might also start with <?xml, so detect them based on filename
660 * extension and use the detected filetype, else assume XML */
661 if (! shebang_find_and_match_filetype(utf8_filename,
662 GEANY_FILETYPES_HTML, GEANY_FILETYPES_DOCBOOK,
663 /* Perl, Python and PHP only to be safe */
664 GEANY_FILETYPES_PERL, GEANY_FILETYPES_PHP, GEANY_FILETYPES_PYTHON, -1))
666 ft = filetypes[GEANY_FILETYPES_XML];
669 else if (g_str_has_prefix(line, "<?php"))
671 ft = filetypes[GEANY_FILETYPES_PHP];
673 return ft;
677 /* Detect the filetype checking for a shebang, then filename extension.
678 * @lines: an strv of the lines to scan (must containing at least one line) */
679 static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_filename,
680 gchar **lines)
682 GeanyFiletype *ft;
683 gint i;
684 GRegex *ft_regex;
685 GMatchInfo *match;
686 GError *regex_error = NULL;
688 /* try to find a shebang and if found use it prior to the filename extension
689 * also checks for <?xml */
690 ft = find_shebang(utf8_filename, lines[0]);
691 if (ft != NULL)
692 return ft;
694 /* try to extract the filetype using a regex capture */
695 ft_regex = g_regex_new(file_prefs.extract_filetype_regex,
696 G_REGEX_RAW | G_REGEX_MULTILINE, 0, &regex_error);
697 if (ft_regex != NULL)
699 for (i = 0; ft == NULL && lines[i] != NULL; i++)
701 if (g_regex_match(ft_regex, lines[i], 0, &match))
703 gchar *capture = g_match_info_fetch(match, 1);
704 if (capture != NULL)
706 ft = filetypes_lookup_by_name(capture);
707 g_free(capture);
710 g_match_info_free(match);
712 g_regex_unref(ft_regex);
714 else if (regex_error != NULL)
716 geany_debug("Filetype extract regex ignored: %s", regex_error->message);
717 g_error_free(regex_error);
719 if (ft != NULL)
720 return ft;
722 if (utf8_filename == NULL)
723 return filetypes[GEANY_FILETYPES_NONE];
725 return filetypes_detect_from_extension(utf8_filename);
729 /* Detect the filetype for the document, checking for a shebang, then filename extension. */
730 GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
732 GeanyFiletype *ft;
733 gchar *lines[GEANY_FILETYPE_SEARCH_LINES + 1];
734 gint i;
736 g_return_val_if_fail(doc == NULL || doc->is_valid, filetypes[GEANY_FILETYPES_NONE]);
738 if (doc == NULL)
739 return filetypes[GEANY_FILETYPES_NONE];
741 for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
743 lines[i] = sci_get_line(doc->editor->sci, i);
745 lines[i] = NULL;
746 ft = filetypes_detect_from_file_internal(doc->file_name, lines);
747 for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
749 g_free(lines[i]);
751 return ft;
755 #ifdef HAVE_PLUGINS
756 /* Currently only used by external plugins (e.g. geanyprj). */
758 * Detects filetype based on a shebang line in the file or the filename extension.
760 * @param utf8_filename The filename in UTF-8 encoding.
762 * @return @transfer{none} The detected filetype for @a utf8_filename or
763 * @c filetypes[GEANY_FILETYPES_NONE] if it could not be detected.
765 GEANY_API_SYMBOL
766 GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename)
768 gchar line[1024];
769 gchar *lines[2];
770 FILE *f;
771 gchar *locale_name = utils_get_locale_from_utf8(utf8_filename);
773 f = g_fopen(locale_name, "r");
774 g_free(locale_name);
775 if (f != NULL)
777 if (fgets(line, sizeof(line), f) != NULL)
779 fclose(f);
780 lines[0] = line;
781 lines[1] = NULL;
782 return filetypes_detect_from_file_internal(utf8_filename, lines);
784 fclose(f);
786 return filetypes_detect_from_extension(utf8_filename);
788 #endif
791 void filetypes_select_radio_item(const GeanyFiletype *ft)
793 /* ignore_callback has to be set by the caller */
794 g_return_if_fail(ignore_callback);
796 if (ft == NULL)
797 ft = filetypes[GEANY_FILETYPES_NONE];
799 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ft->priv->menu_item), TRUE);
803 static void
804 on_filetype_change(GtkCheckMenuItem *menuitem, gpointer user_data)
806 GeanyDocument *doc = document_get_current();
807 if (ignore_callback || doc == NULL || ! gtk_check_menu_item_get_active(menuitem))
808 return;
810 document_set_filetype(doc, (GeanyFiletype*)user_data);
814 static void create_radio_menu_item(GtkWidget *menu, GeanyFiletype *ftype)
816 static GSList *group = NULL;
817 GtkWidget *tmp;
819 tmp = gtk_radio_menu_item_new_with_label(group, ftype->title);
820 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(tmp));
821 ftype->priv->menu_item = tmp;
822 gtk_widget_show(tmp);
823 gtk_container_add(GTK_CONTAINER(menu), tmp);
824 g_signal_connect(tmp, "activate", G_CALLBACK(on_filetype_change), (gpointer) ftype);
828 static void filetype_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
830 GeanyFiletype *ft = data;
832 g_return_if_fail(ft != NULL);
834 g_free(ft->name);
835 g_free(ft->title);
836 g_free(ft->extension);
837 g_free(ft->mime_type);
838 g_free(ft->comment_open);
839 g_free(ft->comment_close);
840 g_free(ft->comment_single);
841 g_free(ft->context_action_cmd);
842 g_free(ft->priv->filecmds);
843 g_free(ft->priv->ftdefcmds);
844 g_free(ft->priv->execcmds);
845 g_free(ft->error_regex_string);
846 if (ft->icon)
847 g_object_unref(ft->icon);
848 g_strfreev(ft->pattern);
850 if (ft->priv->error_regex)
851 g_regex_unref(ft->priv->error_regex);
852 g_slist_foreach(ft->priv->tag_files, (GFunc) g_free, NULL);
853 g_slist_free(ft->priv->tag_files);
855 g_free(ft->priv);
856 g_free(ft);
860 /* frees the array and all related pointers */
861 void filetypes_free_types(void)
863 g_return_if_fail(filetypes_array != NULL);
864 g_return_if_fail(filetypes_hash != NULL);
866 g_ptr_array_foreach(filetypes_array, filetype_free, NULL);
867 g_ptr_array_free(filetypes_array, TRUE);
868 g_hash_table_destroy(filetypes_hash);
872 static void load_indent_settings(GeanyFiletype *ft, GKeyFile *config, GKeyFile *configh)
874 ft->indent_width = utils_get_setting(integer, configh, config, "indentation", "width", -1);
875 ft->indent_type = utils_get_setting(integer, configh, config, "indentation", "type", -1);
876 /* check whether the indent type is OK */
877 switch (ft->indent_type)
879 case GEANY_INDENT_TYPE_TABS:
880 case GEANY_INDENT_TYPE_SPACES:
881 case GEANY_INDENT_TYPE_BOTH:
882 case -1:
883 break;
885 default:
886 g_warning("Invalid indent type %d in file type %s", ft->indent_type, ft->name);
887 ft->indent_type = -1;
888 break;
893 static void load_settings(guint ft_id, GKeyFile *config, GKeyFile *configh)
895 GeanyFiletype *ft = filetypes[ft_id];
896 gchar *result;
898 /* default extension */
899 result = utils_get_setting(string, configh, config, "settings", "extension", NULL);
900 if (result != NULL)
902 SETPTR(filetypes[ft_id]->extension, result);
905 /* MIME type */
906 result = utils_get_setting(string, configh, config, "settings", "mime_type", "text/plain");
907 SETPTR(filetypes[ft_id]->mime_type, result);
909 /* read comment notes */
910 result = utils_get_setting(string, configh, config, "settings", "comment_open", NULL);
911 if (result != NULL)
913 SETPTR(filetypes[ft_id]->comment_open, result);
916 result = utils_get_setting(string, configh, config, "settings", "comment_close", NULL);
917 if (result != NULL)
919 SETPTR(filetypes[ft_id]->comment_close, result);
922 result = utils_get_setting(string, configh, config, "settings", "comment_single", NULL);
923 if (result != NULL)
925 SETPTR(filetypes[ft_id]->comment_single, result);
927 /* import correctly filetypes that use old-style single comments */
928 else if (EMPTY(filetypes[ft_id]->comment_close))
930 SETPTR(filetypes[ft_id]->comment_single, filetypes[ft_id]->comment_open);
931 filetypes[ft_id]->comment_open = NULL;
934 filetypes[ft_id]->comment_use_indent = utils_get_setting(boolean, configh, config,
935 "settings", "comment_use_indent", FALSE);
937 /* read context action */
938 result = utils_get_setting(string, configh, config, "settings", "context_action_cmd", NULL);
939 if (result != NULL)
941 SETPTR(filetypes[ft_id]->context_action_cmd, result);
944 result = utils_get_setting(string, configh, config, "settings", "tag_parser", NULL);
945 if (result != NULL)
947 ft->lang = tm_source_file_get_named_lang(result);
948 if (ft->lang == TM_PARSER_NONE)
949 geany_debug("Cannot find tags parser '%s' for custom filetype '%s'.", result, ft->name);
950 g_free(result);
953 result = utils_get_setting(string, configh, config, "settings", "lexer_filetype", NULL);
954 if (result != NULL)
956 ft->lexer_filetype = filetypes_lookup_by_name(result);
957 if (!ft->lexer_filetype)
958 geany_debug("Cannot find lexer filetype '%s' for custom filetype '%s'.", result, ft->name);
959 g_free(result);
962 ft->priv->symbol_list_sort_mode = utils_get_setting(integer, configh, config, "settings",
963 "symbol_list_sort_mode", SYMBOLS_SORT_USE_PREVIOUS);
964 ft->priv->xml_indent_tags = utils_get_setting(boolean, configh, config, "settings",
965 "xml_indent_tags", FALSE);
967 /* read indent settings */
968 load_indent_settings(ft, config, configh);
970 /* read build settings */
971 build_load_menu(config, GEANY_BCS_FT, (gpointer)ft);
972 build_load_menu(configh, GEANY_BCS_HOME_FT, (gpointer)ft);
976 static void copy_keys(GKeyFile *dest, const gchar *dest_group,
977 GKeyFile *src, const gchar *src_group)
979 gchar **keys = g_key_file_get_keys(src, src_group, NULL, NULL);
980 gchar **ptr;
982 foreach_strv(ptr, keys)
984 gchar *key = *ptr;
985 gchar *value = g_key_file_get_value(src, src_group, key, NULL);
987 g_key_file_set_value(dest, dest_group, key, value);
988 g_free(value);
990 g_strfreev(keys);
994 static gchar *filetypes_get_filename(GeanyFiletype *ft, gboolean user)
996 gchar *ext = filetypes_get_conf_extension(ft);
997 gchar *base_name = g_strconcat("filetypes.", ext, NULL);
998 gchar *file_name;
1000 if (user)
1001 file_name = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, base_name, NULL);
1002 else
1003 file_name = g_build_filename(app->datadir, GEANY_FILEDEFS_SUBDIR, base_name, NULL);
1005 g_free(ext);
1006 g_free(base_name);
1008 return file_name;
1012 static void add_group_keys(GKeyFile *kf, const gchar *group, GeanyFiletype *ft)
1014 gchar *files[2];
1015 gboolean loaded = FALSE;
1016 guint i;
1018 files[0] = filetypes_get_filename(ft, FALSE);
1019 files[1] = filetypes_get_filename(ft, TRUE);
1021 for (i = 0; i < G_N_ELEMENTS(files); i++)
1023 GKeyFile *src = g_key_file_new();
1025 if (g_key_file_load_from_file(src, files[i], G_KEY_FILE_NONE, NULL))
1027 copy_keys(kf, group, src, group);
1028 loaded = TRUE;
1030 g_key_file_free(src);
1033 if (!loaded)
1034 geany_debug("Could not read config file %s for [%s=%s]!", files[0], group, ft->name);
1036 g_free(files[0]);
1037 g_free(files[1]);
1041 static void copy_ft_groups(GKeyFile *kf)
1043 gchar **groups = g_key_file_get_groups(kf, NULL);
1044 gchar **ptr;
1046 foreach_strv(ptr, groups)
1048 gchar *group = *ptr;
1049 gchar *old_group;
1050 gchar *name = strchr(*ptr, '=');
1051 GeanyFiletype *ft;
1053 if (!name || !name[1]) /* no name or no parent name */
1054 continue;
1056 old_group = g_strdup(group);
1058 /* terminate group at '=' */
1059 *name = 0;
1060 name++;
1062 ft = filetypes_lookup_by_name(name);
1063 if (ft)
1065 add_group_keys(kf, group, ft);
1066 /* move old group keys (foo=bar) to proper group name (foo) */
1067 copy_keys(kf, group, kf, old_group);
1069 g_free(old_group);
1071 g_strfreev(groups);
1075 /* simple wrapper function to print file errors in DEBUG mode */
1076 static void load_system_keyfile(GKeyFile *key_file, const gchar *file, GKeyFileFlags flags,
1077 GeanyFiletype *ft)
1079 GError *error = NULL;
1080 gboolean done = g_key_file_load_from_file(key_file, file, flags, &error);
1082 if (error != NULL)
1084 if (!done && !ft->priv->custom)
1085 geany_debug("Failed to open %s (%s)", file, error->message);
1087 g_error_free(error);
1088 error = NULL;
1093 /* Load the configuration file for the associated filetype id.
1094 * This should only be called when the filetype is needed, to save loading
1095 * 20+ configuration files all at once. */
1096 void filetypes_load_config(guint ft_id, gboolean reload)
1098 GKeyFile *config, *config_home;
1099 GeanyFiletypePrivate *pft;
1100 GeanyFiletype *ft;
1102 g_return_if_fail(ft_id < filetypes_array->len);
1104 ft = filetypes[ft_id];
1105 pft = ft->priv;
1107 /* when reloading, proceed only if the settings were already loaded */
1108 if (G_UNLIKELY(reload && ! pft->keyfile_loaded))
1109 return;
1111 /* when not reloading, load the settings only once */
1112 if (G_LIKELY(! reload && pft->keyfile_loaded))
1113 return;
1114 pft->keyfile_loaded = TRUE;
1116 config = g_key_file_new();
1117 config_home = g_key_file_new();
1119 /* highlighting uses GEANY_FILETYPES_NONE for common settings */
1120 gchar *f;
1122 f = filetypes_get_filename(ft, FALSE);
1123 load_system_keyfile(config, f, G_KEY_FILE_KEEP_COMMENTS, ft);
1125 SETPTR(f, filetypes_get_filename(ft, TRUE));
1126 g_key_file_load_from_file(config_home, f, G_KEY_FILE_KEEP_COMMENTS, NULL);
1127 g_free(f);
1129 /* Copy keys for any groups with [group=C] from system keyfile */
1130 copy_ft_groups(config);
1131 copy_ft_groups(config_home);
1133 load_settings(ft_id, config, config_home);
1134 highlighting_init_styles(ft_id, config, config_home);
1136 if (ft->icon)
1137 g_object_unref(ft->icon);
1138 ft->icon = ui_get_mime_icon(ft->mime_type);
1140 g_key_file_free(config);
1141 g_key_file_free(config_home);
1145 static gchar *filetypes_get_conf_extension(const GeanyFiletype *ft)
1147 gchar *result;
1149 if (ft->priv->custom)
1150 return g_strconcat(ft->name, ".conf", NULL);
1152 /* Handle any special extensions different from lowercase filetype->name */
1153 switch (ft->id)
1155 case GEANY_FILETYPES_CPP: result = g_strdup("cpp"); break;
1156 case GEANY_FILETYPES_CS: result = g_strdup("cs"); break;
1157 case GEANY_FILETYPES_MAKE: result = g_strdup("makefile"); break;
1158 case GEANY_FILETYPES_NONE: result = g_strdup("common"); break;
1159 /* name is Matlab/Octave */
1160 case GEANY_FILETYPES_MATLAB: result = g_strdup("matlab"); break;
1161 /* name is Objective-C, and we don't want the hyphen */
1162 case GEANY_FILETYPES_OBJECTIVEC: result = g_strdup("objectivec"); break;
1163 default:
1164 result = g_ascii_strdown(ft->name, -1);
1165 break;
1167 return result;
1171 void filetypes_save_commands(GeanyFiletype *ft)
1173 GKeyFile *config_home;
1174 gchar *fname, *data;
1176 fname = filetypes_get_filename(ft, TRUE);
1177 config_home = g_key_file_new();
1178 g_key_file_load_from_file(config_home, fname, G_KEY_FILE_KEEP_COMMENTS, NULL);
1179 build_save_menu(config_home, ft, GEANY_BCS_HOME_FT);
1180 data = g_key_file_to_data(config_home, NULL, NULL);
1181 utils_write_file(fname, data);
1182 g_free(data);
1183 g_key_file_free(config_home);
1184 g_free(fname);
1188 /* create one file filter which has each file pattern of each filetype */
1189 GtkFileFilter *filetypes_create_file_filter_all_source(void)
1191 GtkFileFilter *new_filter;
1192 guint i, j;
1194 new_filter = gtk_file_filter_new();
1195 gtk_file_filter_set_name(new_filter, _("All Source"));
1197 for (i = 0; i < filetypes_array->len; i++)
1199 if (G_UNLIKELY(i == GEANY_FILETYPES_NONE))
1200 continue;
1202 for (j = 0; filetypes[i]->pattern[j]; j++)
1204 gtk_file_filter_add_pattern(new_filter, filetypes[i]->pattern[j]);
1207 return new_filter;
1211 GtkFileFilter *filetypes_create_file_filter(const GeanyFiletype *ft)
1213 GtkFileFilter *new_filter;
1214 gint i;
1215 const gchar *title;
1217 g_return_val_if_fail(ft != NULL, NULL);
1219 new_filter = gtk_file_filter_new();
1220 title = ft->id == GEANY_FILETYPES_NONE ? _("All files") : ft->title;
1221 gtk_file_filter_set_name(new_filter, title);
1223 for (i = 0; ft->pattern[i]; i++)
1225 gtk_file_filter_add_pattern(new_filter, ft->pattern[i]);
1228 return new_filter;
1232 /* Indicates whether there is a tag parser for the filetype or not.
1233 * Only works for custom filetypes if the filetype settings have been loaded. */
1234 gboolean filetype_has_tags(GeanyFiletype *ft)
1236 g_return_val_if_fail(ft != NULL, FALSE);
1238 return ft->lang != TM_PARSER_NONE;
1242 /** Finds a filetype pointer from its @a name field.
1243 * @param name Filetype name.
1244 * @return @transfer{none} @nullable The filetype found, or @c NULL.
1246 * @since 0.15
1248 GEANY_API_SYMBOL
1249 GeanyFiletype *filetypes_lookup_by_name(const gchar *name)
1251 GeanyFiletype *ft;
1253 g_return_val_if_fail(!EMPTY(name), NULL);
1255 ft = g_hash_table_lookup(filetypes_hash, name);
1256 if (G_UNLIKELY(ft == NULL))
1257 geany_debug("Could not find filetype '%s'.", name);
1258 return ft;
1262 static void compile_regex(GeanyFiletype *ft, gchar *regstr)
1264 GError *error = NULL;
1265 GRegex *regex = g_regex_new(regstr, 0, 0, &error);
1267 if (!regex)
1269 ui_set_statusbar(TRUE, _("Bad regex for filetype %s: %s"),
1270 filetypes_get_display_name(ft), error->message);
1271 g_error_free(error);
1273 if (ft->priv->error_regex)
1274 g_regex_unref(ft->priv->error_regex);
1275 ft->priv->error_regex = regex;
1279 gboolean filetypes_parse_error_message(GeanyFiletype *ft, const gchar *message,
1280 gchar **filename, gint *line)
1282 gchar *regstr;
1283 gchar **tmp;
1284 GeanyDocument *doc;
1285 GMatchInfo *minfo;
1286 gint i, n_match_groups;
1287 gchar *first, *second;
1289 if (ft == NULL)
1291 doc = document_get_current();
1292 if (doc != NULL)
1293 ft = doc->file_type;
1295 tmp = build_get_regex(build_info.grp, ft, NULL);
1296 if (tmp == NULL)
1297 return FALSE;
1298 regstr = *tmp;
1300 *filename = NULL;
1301 *line = -1;
1303 if (G_UNLIKELY(EMPTY(regstr)))
1304 return FALSE;
1306 if (!ft->priv->error_regex || regstr != ft->priv->last_error_pattern)
1308 compile_regex(ft, regstr);
1309 ft->priv->last_error_pattern = regstr;
1311 if (!ft->priv->error_regex)
1312 return FALSE;
1314 if (!g_regex_match(ft->priv->error_regex, message, 0, &minfo))
1316 g_match_info_free(minfo);
1317 return FALSE;
1320 n_match_groups = g_match_info_get_match_count(minfo);
1321 first = second = NULL;
1323 for (i = 1; i < n_match_groups; i++)
1325 gint start_pos;
1327 g_match_info_fetch_pos(minfo, i, &start_pos, NULL);
1328 if (start_pos != -1)
1330 if (first == NULL)
1331 first = g_match_info_fetch(minfo, i);
1332 else
1334 second = g_match_info_fetch(minfo, i);
1335 break;
1340 if (second)
1342 gchar *end;
1343 glong l;
1345 l = strtol(first, &end, 10);
1346 if (*end == '\0') /* first is purely decimals */
1348 *line = l;
1349 g_free(first);
1350 *filename = second;
1352 else
1354 l = strtol(second, &end, 10);
1355 if (*end == '\0')
1357 *line = l;
1358 g_free(second);
1359 *filename = first;
1361 else
1363 g_free(first);
1364 g_free(second);
1368 else
1369 g_free(first);
1371 g_match_info_free(minfo);
1372 return *filename != NULL;
1376 #ifdef G_OS_WIN32
1377 static void convert_filetype_extensions_to_lower_case(gchar **patterns, gsize len)
1379 guint i;
1380 for (i = 0; i < len; i++)
1382 SETPTR(patterns[i], g_ascii_strdown(patterns[i], -1));
1385 #endif
1388 static void read_extensions(GKeyFile *sysconfig, GKeyFile *userconfig)
1390 guint i;
1391 gsize len = 0;
1393 /* read the keys */
1394 for (i = 0; i < filetypes_array->len; i++)
1396 gboolean userset =
1397 g_key_file_has_key(userconfig, "Extensions", filetypes[i]->name, NULL);
1398 gchar **list = g_key_file_get_string_list(
1399 (userset) ? userconfig : sysconfig, "Extensions", filetypes[i]->name, &len, NULL);
1401 g_strfreev(filetypes[i]->pattern);
1402 /* Note: we allow 'Foo=' to remove all patterns */
1403 if (!list)
1404 list = g_new0(gchar*, 1);
1405 filetypes[i]->pattern = list;
1407 #ifdef G_OS_WIN32
1408 convert_filetype_extensions_to_lower_case(filetypes[i]->pattern, len);
1409 #endif
1414 static void read_group(GKeyFile *config, const gchar *group_name, GeanyFiletypeGroupID group_id)
1416 gchar **names = g_key_file_get_string_list(config, "Groups", group_name, NULL, NULL);
1417 gchar **name;
1419 foreach_strv(name, names)
1421 GeanyFiletype *ft = filetypes_lookup_by_name(*name);
1423 if (ft)
1425 ft->group = group_id;
1426 if (ft->priv->custom &&
1427 (group_id == GEANY_FILETYPE_GROUP_COMPILED || group_id == GEANY_FILETYPE_GROUP_SCRIPT))
1429 SETPTR(ft->title, filetype_make_title(ft->name, TITLE_SOURCE_FILE));
1432 else
1433 geany_debug("Filetype '%s' not found for group '%s'!", *name, group_name);
1435 g_strfreev(names);
1439 static void read_groups(GKeyFile *config)
1441 read_group(config, "Programming", GEANY_FILETYPE_GROUP_COMPILED);
1442 read_group(config, "Script", GEANY_FILETYPE_GROUP_SCRIPT);
1443 read_group(config, "Markup", GEANY_FILETYPE_GROUP_MARKUP);
1444 read_group(config, "Misc", GEANY_FILETYPE_GROUP_MISC);
1445 read_group(config, "None", GEANY_FILETYPE_GROUP_NONE);
1449 static void read_filetype_config(void)
1451 gchar *sysconfigfile = g_build_filename(app->datadir, "filetype_extensions.conf", NULL);
1452 gchar *userconfigfile = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
1453 GKeyFile *sysconfig = g_key_file_new();
1454 GKeyFile *userconfig = g_key_file_new();
1456 g_key_file_load_from_file(sysconfig, sysconfigfile, G_KEY_FILE_NONE, NULL);
1457 g_key_file_load_from_file(userconfig, userconfigfile, G_KEY_FILE_NONE, NULL);
1459 read_extensions(sysconfig, userconfig);
1460 read_groups(sysconfig);
1461 read_groups(userconfig);
1463 g_free(sysconfigfile);
1464 g_free(userconfigfile);
1465 g_key_file_free(sysconfig);
1466 g_key_file_free(userconfig);
1470 void filetypes_reload_extensions(void)
1472 guint i;
1474 read_filetype_config();
1476 /* Redetect filetype of any documents with none set */
1477 foreach_document(i)
1479 GeanyDocument *doc = documents[i];
1480 if (doc->file_type->id != GEANY_FILETYPES_NONE)
1481 continue;
1482 document_set_filetype(doc, filetypes_detect_from_document(doc));
1487 /** Accessor function for @ref GeanyData::filetypes_array items.
1488 * Example: @code ft = filetypes_index(GEANY_FILETYPES_C); @endcode
1489 * @param idx @c filetypes_array index.
1490 * @return @transfer{none} @nullable The filetype, or @c NULL if @a idx is out of range.
1492 * @since 0.16
1494 GEANY_API_SYMBOL
1495 GeanyFiletype *filetypes_index(gint idx)
1497 return (idx >= 0 && idx < (gint) filetypes_array->len) ? filetypes[idx] : NULL;
1501 void filetypes_reload(void)
1503 guint i;
1504 GeanyDocument *current_doc;
1506 /* reload filetype configs */
1507 for (i = 0; i < filetypes_array->len; i++)
1509 /* filetypes_load_config() will skip not loaded filetypes */
1510 filetypes_load_config(i, TRUE);
1513 current_doc = document_get_current();
1514 if (!current_doc)
1515 return;
1517 /* update document styling */
1518 foreach_document(i)
1520 if (current_doc != documents[i])
1521 document_reload_config(documents[i]);
1523 /* process the current document at last */
1524 document_reload_config(current_doc);
1528 /** Gets @c ft->name or a translation for filetype None.
1529 * @param ft .
1530 * @return .
1531 * @since Geany 0.20 */
1532 GEANY_API_SYMBOL
1533 const gchar *filetypes_get_display_name(GeanyFiletype *ft)
1535 return ft->id == GEANY_FILETYPES_NONE ? _("None") : ft->name;
1539 /* gets comment_open/comment_close/comment_single strings from the filetype
1540 * @param single_first: whether single comment is preferred if both available
1541 * returns true if at least comment_open is set, false otherwise */
1542 gboolean filetype_get_comment_open_close(const GeanyFiletype *ft, gboolean single_first,
1543 const gchar **co, const gchar **cc)
1545 g_return_val_if_fail(ft != NULL, FALSE);
1546 g_return_val_if_fail(co != NULL, FALSE);
1547 g_return_val_if_fail(cc != NULL, FALSE);
1549 if (single_first)
1551 *co = ft->comment_single;
1552 if (!EMPTY(*co))
1553 *cc = NULL;
1554 else
1556 *co = ft->comment_open;
1557 *cc = ft->comment_close;
1560 else
1562 *co = ft->comment_open;
1563 if (!EMPTY(*co))
1564 *cc = ft->comment_close;
1565 else
1567 *co = ft->comment_single;
1568 *cc = NULL;
1572 return !EMPTY(*co);
1575 static void *copy_(void *src) { return src; }
1576 static void free_(void *doc) { }
1578 /** @gironly
1579 * Gets the GType of GeanyFiletype
1581 * @return the GeanyFiletype type */
1582 GEANY_API_SYMBOL
1583 GType filetype_get_type (void);
1585 G_DEFINE_BOXED_TYPE(GeanyFiletype, filetype, copy_, free_);