Add keybindings for "Project->New from Folder"
[geany-mirror.git] / src / filetypes.c
blobaab9b6056bb61b3c782a4571ca5dc0e9601f6dfd
1 /*
2 * filetypes.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 /**
22 * @file filetypes.h
23 * Filetype detection, file extensions and filetype menu items.
26 /* Note: we use GeanyFiletypeID for some function arguments, but GeanyFiletype is better; we should
27 * only use GeanyFiletype for API functions. */
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include "filetypes.h"
35 #include "app.h"
36 #include "callbacks.h" /* FIXME: for ignore_callback */
37 #include "document.h"
38 #include "filetypesprivate.h"
39 #include "geany.h"
40 #include "geanyobject.h"
41 #include "highlighting.h"
42 #include "projectprivate.h"
43 #include "sciwrappers.h"
44 #include "support.h"
45 #include "symbols.h"
46 #include "tm_parser.h"
47 #include "utils.h"
48 #include "ui_utils.h"
50 #include <stdlib.h>
51 #include <string.h>
53 #include <glib/gstdio.h>
55 #define GEANY_FILETYPE_SEARCH_LINES 2 /* lines of file to search for filetype */
57 GPtrArray *filetypes_array = NULL;
58 static GHashTable *filetypes_hash = NULL; /* Hash of filetype pointers based on name keys */
59 GSList *filetypes_by_title = NULL;
62 static void create_radio_menu_item(GtkWidget *menu, GeanyFiletype *ftype);
64 static gchar *filetypes_get_conf_extension(const GeanyFiletype *ft);
65 static void read_filetype_config(void);
66 static void create_set_filetype_menu(gboolean config);
67 static gchar *filetypes_get_filename(GeanyFiletype *ft, gboolean user);
70 enum TitleType
72 TITLE_NONE,
73 TITLE_SOURCE_FILE,
74 TITLE_FILE,
75 TITLE_SCRIPT,
76 TITLE_DOCUMENT
79 /* Save adding many translation strings if the filetype name doesn't need translating */
80 static gchar *filetype_make_title(const char *name, enum TitleType type)
82 g_return_val_if_fail(name != NULL, NULL);
84 switch (type)
86 case TITLE_SOURCE_FILE: return g_strdup_printf(_("%s source file"), name);
87 case TITLE_FILE: return g_strdup_printf(_("%s file"), name);
88 case TITLE_SCRIPT: return g_strdup_printf(_("%s script"), name);
89 case TITLE_DOCUMENT: return g_strdup_printf(_("%s document"), name);
90 case TITLE_NONE: /* fall through */
91 default: return g_strdup(name);
96 /* name argument (ie filetype name) must not be translated as it is used for
97 * filetype lookup. Use filetypes_get_display_name() instead.*/
98 static void ft_init(GeanyFiletypeID ft_id, TMParserType lang, const char *name,
99 const char *title_name, enum TitleType title_type,
100 GeanyFiletypeGroupID group_id)
102 GeanyFiletype *ft = filetypes[ft_id];
103 ft->lang = lang;
104 ft->name = g_strdup(name);
105 ft->title = filetype_make_title((title_name != NULL) ? title_name : ft->name, title_type);
106 ft->group = group_id;
109 /* Evil macro to save typing and make init_builtin_filetypes() more readable */
110 #define FT_INIT(ft_id, parser_id, name, title_name, title_type, group_id) \
111 ft_init(GEANY_FILETYPES_##ft_id, TM_PARSER_##parser_id, name, title_name, \
112 TITLE_##title_type, GEANY_FILETYPE_GROUP_##group_id)
115 /* Note: remember to update HACKING if this function is renamed. */
116 static void init_builtin_filetypes(void)
118 /* Column legend:
119 * [0] = Filetype constant (GEANY_FILETYPES_*)
120 * [1] = CTags parser (TM_PARSER_*)
121 * [2] = Non-translated filetype name (*not* label for display)
122 * [3] = Translatable human filetype title prefix or NULL to use [2]
123 * [4] = Title type (TITLE_*) constant (ex. TITLE_SOURCE_FILE is 'source file' suffix)
124 * [5] = The filetype group constant (GEANY_FILETYPE_GROUP_*)
125 * --------------------------------------------------------------------------------------------------------------------------
126 * [0] [1] [2] [3] [4] [5] */
127 FT_INIT( NONE, NONE, "None", _("None"), NONE, NONE );
128 FT_INIT( C, C, "C", NULL, SOURCE_FILE, COMPILED );
129 FT_INIT( CPP, CPP, "C++", NULL, SOURCE_FILE, COMPILED );
130 FT_INIT( OBJECTIVEC, OBJC, "Objective-C", NULL, SOURCE_FILE, COMPILED );
131 FT_INIT( CS, CSHARP, "C#", NULL, SOURCE_FILE, COMPILED );
132 FT_INIT( VALA, VALA, "Vala", NULL, SOURCE_FILE, COMPILED );
133 FT_INIT( D, D, "D", NULL, SOURCE_FILE, COMPILED );
134 FT_INIT( JAVA, JAVA, "Java", NULL, SOURCE_FILE, COMPILED );
135 FT_INIT( PASCAL, PASCAL, "Pascal", NULL, SOURCE_FILE, COMPILED );
136 FT_INIT( ASM, ASM, "ASM", "Assembler", SOURCE_FILE, COMPILED );
137 FT_INIT( BASIC, FREEBASIC, "FreeBasic", NULL, SOURCE_FILE, COMPILED );
138 FT_INIT( FORTRAN, FORTRAN, "Fortran", "Fortran (F90)", SOURCE_FILE, COMPILED );
139 FT_INIT( F77, F77, "F77", "Fortran (F77)", SOURCE_FILE, COMPILED );
140 FT_INIT( GLSL, C, "GLSL", NULL, SOURCE_FILE, COMPILED );
141 FT_INIT( CAML, NONE, "CAML", "(O)Caml", SOURCE_FILE, COMPILED );
142 FT_INIT( PERL, PERL, "Perl", NULL, SOURCE_FILE, SCRIPT );
143 FT_INIT( PHP, PHP, "PHP", NULL, SOURCE_FILE, SCRIPT );
144 FT_INIT( JS, JAVASCRIPT, "Javascript", NULL, SOURCE_FILE, SCRIPT );
145 FT_INIT( PYTHON, PYTHON, "Python", NULL, SOURCE_FILE, SCRIPT );
146 FT_INIT( RUBY, RUBY, "Ruby", NULL, SOURCE_FILE, SCRIPT );
147 FT_INIT( TCL, TCL, "Tcl", NULL, SOURCE_FILE, SCRIPT );
148 FT_INIT( LUA, LUA, "Lua", NULL, SOURCE_FILE, SCRIPT );
149 FT_INIT( GDSCRIPT, GDSCRIPT, "GDScript", NULL, SOURCE_FILE, SCRIPT );
150 FT_INIT( HASKELL, HASKELL, "Haskell", NULL, SOURCE_FILE, COMPILED );
151 FT_INIT( MARKDOWN, MARKDOWN, "Markdown", NULL, SOURCE_FILE, MARKUP );
152 FT_INIT( TXT2TAGS, TXT2TAGS, "Txt2tags", NULL, SOURCE_FILE, MARKUP );
153 FT_INIT( ABC, ABC, "Abc", NULL, FILE, MISC );
154 FT_INIT( SH, SH, "Sh", _("Shell"), SCRIPT, SCRIPT );
155 FT_INIT( MAKE, MAKEFILE, "Make", _("Makefile"), NONE, SCRIPT );
156 FT_INIT( XML, NONE, "XML", NULL, DOCUMENT, MARKUP );
157 FT_INIT( DOCBOOK, DOCBOOK, "Docbook", NULL, DOCUMENT, MARKUP );
158 FT_INIT( HTML, HTML, "HTML", NULL, DOCUMENT, MARKUP );
159 FT_INIT( CSS, CSS, "CSS", _("Cascading Stylesheet"), NONE, MARKUP ); /* not really markup but fit quite well to HTML */
160 FT_INIT( SQL, SQL, "SQL", NULL, FILE, MISC );
161 FT_INIT( COBOL, COBOL, "COBOL", NULL, SOURCE_FILE, COMPILED );
162 FT_INIT( LATEX, LATEX, "LaTeX", NULL, SOURCE_FILE, MARKUP );
163 FT_INIT( BIBTEX, BIBTEX, "BibTeX", 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 );
189 FT_INIT( SMALLTALK, NONE, "Smalltalk", NULL, SOURCE_FILE, SCRIPT );
190 FT_INIT( JULIA, JULIA, "Julia", NULL, SOURCE_FILE, SCRIPT );
194 /* initialize fields. */
195 static GeanyFiletype *filetype_new(void)
197 GeanyFiletype *ft = g_new0(GeanyFiletype, 1);
199 ft->group = GEANY_FILETYPE_GROUP_NONE;
200 ft->lang = TM_PARSER_NONE; /* assume no tagmanager parser */
201 /* pattern must not be null */
202 ft->pattern = g_new0(gchar*, 1);
203 ft->indent_width = -1;
204 ft->indent_type = -1;
206 ft->priv = g_new0(GeanyFiletypePrivate, 1);
207 ft->priv->project_list_entry = -1; /* no entry */
209 return ft;
213 static gint cmp_filetype(gconstpointer pft1, gconstpointer pft2, gpointer data)
215 gboolean by_name = GPOINTER_TO_INT(data);
216 const GeanyFiletype *ft1 = pft1, *ft2 = pft2;
218 if (G_UNLIKELY(ft1->id == GEANY_FILETYPES_NONE))
219 return -1;
220 if (G_UNLIKELY(ft2->id == GEANY_FILETYPES_NONE))
221 return 1;
223 return by_name ?
224 utils_str_casecmp(ft1->name, ft2->name) :
225 utils_str_casecmp(ft1->title, ft2->title);
229 /** Gets a list of filetype pointers sorted by name.
230 * The list does not change on subsequent calls.
231 * @return @elementtype{GeanyFiletype} @transfer{none} The list - do not free.
232 * @see filetypes_by_title. */
233 GEANY_API_SYMBOL
234 const GSList *filetypes_get_sorted_by_name(void)
236 static GSList *list = NULL;
238 g_return_val_if_fail(filetypes_by_title, NULL);
240 if (!list)
242 list = g_slist_copy(filetypes_by_title);
243 list = g_slist_sort_with_data(list, cmp_filetype, GINT_TO_POINTER(TRUE));
245 return list;
249 /* Add a filetype pointer to the lists of available filetypes,
250 * and set the filetype::id field. */
251 static void filetype_add(GeanyFiletype *ft)
253 g_return_if_fail(ft);
254 g_return_if_fail(ft->name);
256 ft->id = filetypes_array->len; /* len will be the index for filetype_array */
257 g_ptr_array_add(filetypes_array, ft);
258 g_hash_table_insert(filetypes_hash, ft->name, ft);
260 /* list will be sorted later */
261 filetypes_by_title = g_slist_prepend(filetypes_by_title, ft);
265 static void add_custom_filetype(const gchar *filename)
267 gchar *fn = utils_strdupa(strstr(filename, ".") + 1);
268 gchar *dot = g_strrstr(fn, ".conf");
269 GeanyFiletype *ft;
271 g_return_if_fail(dot);
273 *dot = 0x0;
275 if (g_hash_table_lookup(filetypes_hash, fn))
276 return;
278 ft = filetype_new();
279 ft->name = g_strdup(fn);
280 ft->title = filetype_make_title(ft->name, TITLE_FILE);
281 ft->priv->custom = TRUE;
282 filetype_add(ft);
283 geany_debug("Added filetype %s (%d).", ft->name, ft->id);
287 static void init_custom_filetypes(const gchar *path)
289 GDir *dir;
290 const gchar *filename;
292 g_return_if_fail(path);
294 dir = g_dir_open(path, 0, NULL);
295 if (dir == NULL)
296 return;
298 foreach_dir(filename, dir)
300 const gchar prefix[] = "filetypes.";
302 if (g_str_has_prefix(filename, prefix) &&
303 g_str_has_suffix(filename + strlen(prefix), ".conf"))
305 add_custom_filetype(filename);
308 g_dir_close(dir);
312 /* Create the filetypes array and fill it with the known filetypes.
313 * Warning: GTK isn't necessarily initialized yet. */
314 void filetypes_init_types(void)
316 GeanyFiletypeID ft_id;
317 gchar *f;
319 g_return_if_fail(filetypes_array == NULL);
320 g_return_if_fail(filetypes_hash == NULL);
322 filetypes_array = g_ptr_array_sized_new(GEANY_MAX_BUILT_IN_FILETYPES);
323 filetypes_hash = g_hash_table_new(g_str_hash, g_str_equal);
325 /* Create built-in filetypes */
326 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
328 filetypes[ft_id] = filetype_new();
330 init_builtin_filetypes();
332 /* Add built-in filetypes to the hash now the name fields are set */
333 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
335 filetype_add(filetypes[ft_id]);
337 f = g_build_filename(app->datadir, GEANY_FILEDEFS_SUBDIR, NULL);
338 init_custom_filetypes(f);
339 g_free(f);
340 f = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
341 init_custom_filetypes(f);
342 g_free(f);
344 /* sort last instead of on insertion to prevent exponential time */
345 filetypes_by_title = g_slist_sort_with_data(filetypes_by_title,
346 cmp_filetype, GINT_TO_POINTER(FALSE));
348 read_filetype_config();
352 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
354 gchar *f, *basename;
356 g_return_if_fail(!EMPTY(doc->real_path));
358 f = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
359 if (utils_str_equal(doc->real_path, f))
360 filetypes_reload_extensions();
361 g_free(f);
363 basename = g_path_get_basename(doc->real_path);
364 if (g_str_has_prefix(basename, "filetypes."))
366 guint i;
368 for (i = 0; i < filetypes_array->len; i++)
370 GeanyFiletype *ft = filetypes[i];
372 f = filetypes_get_filename(ft, TRUE);
373 if (utils_str_equal(doc->real_path, f))
375 guint j;
377 /* Note: we don't reload other filetypes, even though the named styles may have changed.
378 * The user can do this manually with 'Tools->Reload Configuration' */
379 filetypes_load_config(i, TRUE);
381 foreach_document(j)
382 document_reload_config(documents[j]);
384 g_free(f);
385 break;
387 g_free(f);
390 g_free(basename);
394 static void setup_config_file_menus(void)
396 gchar *f;
398 f = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
399 ui_add_config_file_menu_item(f, NULL, NULL);
400 SETPTR(f, g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.common", NULL));
401 ui_add_config_file_menu_item(f, NULL, NULL);
402 g_free(f);
404 create_set_filetype_menu(TRUE);
406 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
410 static GtkWidget *create_sub_menu(GtkWidget *parent, const gchar *title)
412 GtkWidget *menu, *item;
414 menu = gtk_menu_new();
415 item = gtk_menu_item_new_with_mnemonic((title));
416 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
417 gtk_container_add(GTK_CONTAINER(parent), item);
418 gtk_widget_show(item);
420 return menu;
424 static void create_set_filetype_menu(gboolean config)
426 GtkWidget *group_menus[GEANY_FILETYPE_GROUP_COUNT] = {NULL};
427 GSList *node;
428 GtkWidget *menu;
430 menu = config ? ui_widgets.config_files_filetype_menu :
431 ui_lookup_widget(main_widgets.window, "set_filetype1_menu");
433 group_menus[GEANY_FILETYPE_GROUP_COMPILED] = create_sub_menu(menu, _("_Programming Languages"));
434 group_menus[GEANY_FILETYPE_GROUP_SCRIPT] = create_sub_menu(menu, _("_Scripting Languages"));
435 group_menus[GEANY_FILETYPE_GROUP_MARKUP] = create_sub_menu(menu, _("_Markup Languages"));
436 group_menus[GEANY_FILETYPE_GROUP_MISC] = create_sub_menu(menu, _("M_iscellaneous"));
438 /* Append all filetypes to the menu */
439 foreach_slist(node, filetypes_by_title)
441 GeanyFiletype *ft = node->data;
442 GtkWidget *parent = (ft->group != GEANY_FILETYPE_GROUP_NONE) ? group_menus[ft->group] : menu;
444 /* we already have filetypes.common config entry */
445 if (config && ft->id == GEANY_FILETYPES_NONE)
446 continue;
448 if (config)
450 gchar *filename = filetypes_get_filename(ft, TRUE);
452 ui_add_config_file_menu_item(filename, NULL, GTK_CONTAINER(parent));
453 g_free(filename);
455 else
456 create_radio_menu_item(parent, ft);
461 void filetypes_init(void)
463 filetypes_init_types();
464 create_set_filetype_menu(FALSE);
465 setup_config_file_menus();
469 static guint match_basename(const GeanyFiletype *ft, const gchar *base_filename)
471 if (G_UNLIKELY(ft->id == GEANY_FILETYPES_NONE))
472 return 0;
474 for (guint j = 0; ft->pattern[j] != NULL; j++)
476 gchar *pat = ft->pattern[j];
478 if (g_pattern_match_simple(pat, base_filename))
480 return strlen(pat);
483 return 0;
487 static GeanyFiletype *detect_filetype_conf_file(const gchar *utf8_filename)
489 gchar *lfn = NULL;
490 gchar *path;
491 gboolean found = FALSE;
493 #ifdef G_OS_WIN32
494 /* use lower case basename */
495 lfn = g_utf8_strdown(utf8_filename, -1);
496 #else
497 lfn = g_strdup(utf8_filename);
498 #endif
499 SETPTR(lfn, utils_get_locale_from_utf8(lfn));
501 path = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, "filetypes.", NULL);
502 if (g_str_has_prefix(lfn, path))
503 found = TRUE;
505 SETPTR(path, g_build_filename(app->datadir, GEANY_FILEDEFS_SUBDIR, "filetypes.", NULL));
506 if (g_str_has_prefix(lfn, path))
507 found = TRUE;
509 g_free(path);
510 g_free(lfn);
511 return found ? filetypes[GEANY_FILETYPES_CONF] : NULL;
515 /* Detect filetype only based on the filename extension.
516 * utf8_filename can include the full path. */
517 GeanyFiletype *filetypes_detect_from_extension(const gchar *utf8_filename)
519 gchar *base_filename;
520 GeanyFiletype *ft;
521 guint plen = 0;
523 ft = detect_filetype_conf_file(utf8_filename);
524 if (ft)
525 return ft;
527 /* to match against the basename of the file (because of Makefile*) */
528 base_filename = g_path_get_basename(utf8_filename);
529 #ifdef G_OS_WIN32
530 /* use lower case basename */
531 SETPTR(base_filename, g_utf8_strdown(base_filename, -1));
532 #endif
534 for (guint i = 0; i < filetypes_array->len; i++)
536 guint mlen = match_basename(filetypes[i], base_filename);
538 if (mlen > plen)
539 { // longest pattern match wins
540 plen = mlen;
541 ft = filetypes[i];
543 else if (mlen == plen && ft && !ft->priv->user_extensions &&
544 filetypes[i]->priv->user_extensions)
545 { // user config overrides system if pattern len same
546 ft = filetypes[i];
549 if (ft == NULL)
550 ft = filetypes[GEANY_FILETYPES_NONE];
552 g_free(base_filename);
553 return ft;
557 /* This detects the filetype of the file pointed by 'utf8_filename' and a list of filetype id's,
558 * terminated by -1.
559 * The detected filetype of the file is checked against every id in the passed list and if
560 * there is a match, TRUE is returned. */
561 static gboolean shebang_find_and_match_filetype(const gchar *utf8_filename, gint first, ...)
563 GeanyFiletype *ft = NULL;
564 gint test;
565 gboolean result = FALSE;
566 va_list args;
568 ft = filetypes_detect_from_extension(utf8_filename);
569 if (ft == NULL || ft->id >= filetypes_array->len)
570 return FALSE;
572 va_start(args, first);
573 test = first;
574 while (1)
576 if (test == -1)
577 break;
579 if (ft->id == (guint) test)
581 result = TRUE;
582 break;
584 test = va_arg(args, gint);
586 va_end(args);
588 return result;
592 static GeanyFiletype *find_shebang(const gchar *utf8_filename, const gchar *line)
594 GeanyFiletype *ft = NULL;
596 if (strlen(line) > 2 && line[0] == '#' && line[1] == '!')
598 static const struct {
599 const gchar *name;
600 GeanyFiletypeID filetype;
601 } intepreter_map[] = {
602 { "sh", GEANY_FILETYPES_SH },
603 { "bash", GEANY_FILETYPES_SH },
604 { "dash", GEANY_FILETYPES_SH },
605 { "perl", GEANY_FILETYPES_PERL },
606 { "python", GEANY_FILETYPES_PYTHON },
607 { "php", GEANY_FILETYPES_PHP },
608 { "ruby", GEANY_FILETYPES_RUBY },
609 { "tcl", GEANY_FILETYPES_TCL },
610 { "make", GEANY_FILETYPES_MAKE },
611 { "zsh", GEANY_FILETYPES_SH },
612 { "ksh", GEANY_FILETYPES_SH },
613 { "mksh", GEANY_FILETYPES_SH },
614 { "csh", GEANY_FILETYPES_SH },
615 { "tcsh", GEANY_FILETYPES_SH },
616 { "ash", GEANY_FILETYPES_SH },
617 { "dmd", GEANY_FILETYPES_D },
618 { "wish", GEANY_FILETYPES_TCL },
619 { "node", GEANY_FILETYPES_JS },
620 { "rust", GEANY_FILETYPES_RUST }
622 gchar *tmp = g_path_get_basename(line + 2);
623 gchar *basename_interpreter = tmp;
624 guint i;
626 if (g_str_has_prefix(tmp, "env "))
627 { /* skip "env" and read the following interpreter */
628 basename_interpreter += 4;
631 for (i = 0; ! ft && i < G_N_ELEMENTS(intepreter_map); i++)
633 if (g_str_has_prefix(basename_interpreter, intepreter_map[i].name))
634 ft = filetypes[intepreter_map[i].filetype];
636 g_free(tmp);
638 /* detect HTML files */
639 if (g_str_has_prefix(line, "<!DOCTYPE html") || g_str_has_prefix(line, "<html"))
641 /* PHP, Perl and Python files might also start with <html, so detect them based on filename
642 * extension and use the detected filetype, else assume HTML */
643 if (! shebang_find_and_match_filetype(utf8_filename,
644 GEANY_FILETYPES_PERL, GEANY_FILETYPES_PHP, GEANY_FILETYPES_PYTHON, -1))
646 ft = filetypes[GEANY_FILETYPES_HTML];
649 /* detect XML files */
650 else if (utf8_filename && g_str_has_prefix(line, "<?xml"))
652 /* HTML and DocBook files might also start with <?xml, so detect them based on filename
653 * extension and use the detected filetype, else assume XML */
654 if (! shebang_find_and_match_filetype(utf8_filename,
655 GEANY_FILETYPES_HTML, GEANY_FILETYPES_DOCBOOK,
656 /* Perl, Python and PHP only to be safe */
657 GEANY_FILETYPES_PERL, GEANY_FILETYPES_PHP, GEANY_FILETYPES_PYTHON, -1))
659 ft = filetypes[GEANY_FILETYPES_XML];
662 else if (g_str_has_prefix(line, "<?php"))
664 ft = filetypes[GEANY_FILETYPES_PHP];
666 return ft;
670 /* Detect the filetype checking for a shebang, then filename extension.
671 * @lines: an strv of the lines to scan (must containing at least one line) */
672 static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_filename,
673 gchar **lines)
675 GeanyFiletype *ft;
676 gint i;
677 GRegex *ft_regex;
678 GMatchInfo *match;
679 GError *regex_error = NULL;
681 /* try to find a shebang and if found use it prior to the filename extension
682 * also checks for <?xml */
683 ft = find_shebang(utf8_filename, lines[0]);
684 if (ft != NULL)
685 return ft;
687 /* try to extract the filetype using a regex capture */
688 ft_regex = g_regex_new(file_prefs.extract_filetype_regex,
689 G_REGEX_RAW | G_REGEX_MULTILINE, 0, &regex_error);
690 if (ft_regex != NULL)
692 for (i = 0; ft == NULL && lines[i] != NULL; i++)
694 if (g_regex_match(ft_regex, lines[i], 0, &match))
696 gchar *capture = g_match_info_fetch(match, 1);
697 if (capture != NULL)
699 ft = filetypes_lookup_by_name(capture);
700 g_free(capture);
703 g_match_info_free(match);
705 g_regex_unref(ft_regex);
707 else if (regex_error != NULL)
709 geany_debug("Filetype extract regex ignored: %s", regex_error->message);
710 g_error_free(regex_error);
712 if (ft != NULL)
713 return ft;
715 if (utf8_filename == NULL)
716 return filetypes[GEANY_FILETYPES_NONE];
718 return filetypes_detect_from_extension(utf8_filename);
722 /* Detect the filetype for the document, checking for a shebang, then filename extension. */
723 GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
725 GeanyFiletype *ft;
726 gchar *lines[GEANY_FILETYPE_SEARCH_LINES + 1];
727 gint i;
729 g_return_val_if_fail(doc == NULL || doc->is_valid, filetypes[GEANY_FILETYPES_NONE]);
731 if (doc == NULL)
732 return filetypes[GEANY_FILETYPES_NONE];
734 for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
736 lines[i] = sci_get_line(doc->editor->sci, i);
738 lines[i] = NULL;
739 ft = filetypes_detect_from_file_internal(doc->file_name, lines);
740 for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
742 g_free(lines[i]);
744 return ft;
748 #ifdef HAVE_PLUGINS
749 /* Currently only used by external plugins (e.g. geanyprj). */
751 * Detects filetype based on a shebang line in the file or the filename extension.
753 * @param utf8_filename The filename in UTF-8 encoding.
755 * @return @transfer{none} The detected filetype for @a utf8_filename or
756 * @c filetypes[GEANY_FILETYPES_NONE] if it could not be detected.
758 GEANY_API_SYMBOL
759 GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename)
761 gchar line[1024];
762 gchar *lines[2];
763 FILE *f;
764 gchar *locale_name = utils_get_locale_from_utf8(utf8_filename);
766 f = g_fopen(locale_name, "r");
767 g_free(locale_name);
768 if (f != NULL)
770 if (fgets(line, sizeof(line), f) != NULL)
772 fclose(f);
773 lines[0] = line;
774 lines[1] = NULL;
775 return filetypes_detect_from_file_internal(utf8_filename, lines);
777 fclose(f);
779 return filetypes_detect_from_extension(utf8_filename);
781 #endif
784 void filetypes_select_radio_item(const GeanyFiletype *ft)
786 /* ignore_callback has to be set by the caller */
787 g_return_if_fail(ignore_callback);
789 if (ft == NULL)
790 ft = filetypes[GEANY_FILETYPES_NONE];
792 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ft->priv->menu_item), TRUE);
796 static void
797 on_filetype_change(GtkCheckMenuItem *menuitem, gpointer user_data)
799 GeanyDocument *doc = document_get_current();
800 if (ignore_callback || doc == NULL || ! gtk_check_menu_item_get_active(menuitem))
801 return;
803 document_set_filetype(doc, (GeanyFiletype*)user_data);
807 static void create_radio_menu_item(GtkWidget *menu, GeanyFiletype *ftype)
809 static GSList *group = NULL;
810 GtkWidget *tmp;
812 tmp = gtk_radio_menu_item_new_with_label(group, ftype->title);
813 group = gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(tmp));
814 ftype->priv->menu_item = tmp;
815 gtk_widget_show(tmp);
816 gtk_container_add(GTK_CONTAINER(menu), tmp);
817 g_signal_connect(tmp, "activate", G_CALLBACK(on_filetype_change), (gpointer) ftype);
821 static void filetype_free(gpointer data, G_GNUC_UNUSED gpointer user_data)
823 GeanyFiletype *ft = data;
825 g_return_if_fail(ft != NULL);
827 g_free(ft->name);
828 g_free(ft->title);
829 g_free(ft->extension);
830 g_free(ft->mime_type);
831 g_free(ft->comment_open);
832 g_free(ft->comment_close);
833 g_free(ft->comment_single);
834 g_free(ft->context_action_cmd);
835 g_free(ft->priv->filecmds);
836 g_free(ft->priv->ftdefcmds);
837 g_free(ft->priv->execcmds);
838 g_free(ft->error_regex_string);
839 if (ft->icon)
840 g_object_unref(ft->icon);
841 g_strfreev(ft->pattern);
843 if (ft->priv->error_regex)
844 g_regex_unref(ft->priv->error_regex);
845 g_slist_foreach(ft->priv->tag_files, (GFunc) g_free, NULL);
846 g_slist_free(ft->priv->tag_files);
848 g_free(ft->priv);
849 g_free(ft);
853 /* frees the array and all related pointers */
854 void filetypes_free_types(void)
856 g_return_if_fail(filetypes_array != NULL);
857 g_return_if_fail(filetypes_hash != NULL);
859 g_ptr_array_foreach(filetypes_array, filetype_free, NULL);
860 g_ptr_array_free(filetypes_array, TRUE);
861 g_hash_table_destroy(filetypes_hash);
865 static void load_indent_settings(GeanyFiletype *ft, GKeyFile *config, GKeyFile *configh)
867 ft->indent_width = utils_get_setting(integer, configh, config, "indentation", "width", -1);
868 ft->indent_type = utils_get_setting(integer, configh, config, "indentation", "type", -1);
869 /* check whether the indent type is OK */
870 switch (ft->indent_type)
872 case GEANY_INDENT_TYPE_TABS:
873 case GEANY_INDENT_TYPE_SPACES:
874 case GEANY_INDENT_TYPE_BOTH:
875 case -1:
876 break;
878 default:
879 g_warning("Invalid indent type %d in file type %s", ft->indent_type, ft->name);
880 ft->indent_type = -1;
881 break;
886 static void load_settings(guint ft_id, GKeyFile *config, GKeyFile *configh)
888 GeanyFiletype *ft = filetypes[ft_id];
889 gchar *result;
891 /* default extension */
892 result = utils_get_setting(string, configh, config, "settings", "extension", NULL);
893 if (result != NULL)
895 SETPTR(filetypes[ft_id]->extension, result);
898 /* MIME type */
899 result = utils_get_setting(string, configh, config, "settings", "mime_type", "text/plain");
900 SETPTR(filetypes[ft_id]->mime_type, result);
902 /* read comment notes */
903 result = utils_get_setting(string, configh, config, "settings", "comment_open", NULL);
904 if (result != NULL)
906 SETPTR(filetypes[ft_id]->comment_open, result);
909 result = utils_get_setting(string, configh, config, "settings", "comment_close", NULL);
910 if (result != NULL)
912 SETPTR(filetypes[ft_id]->comment_close, result);
915 result = utils_get_setting(string, configh, config, "settings", "comment_single", NULL);
916 if (result != NULL)
918 SETPTR(filetypes[ft_id]->comment_single, result);
920 /* import correctly filetypes that use old-style single comments */
921 else if (EMPTY(filetypes[ft_id]->comment_close))
923 SETPTR(filetypes[ft_id]->comment_single, filetypes[ft_id]->comment_open);
924 filetypes[ft_id]->comment_open = NULL;
927 filetypes[ft_id]->comment_use_indent = utils_get_setting(boolean, configh, config,
928 "settings", "comment_use_indent", FALSE);
930 /* read context action */
931 result = utils_get_setting(string, configh, config, "settings", "context_action_cmd", NULL);
932 if (result != NULL)
934 SETPTR(filetypes[ft_id]->context_action_cmd, result);
937 result = utils_get_setting(string, configh, config, "settings", "tag_parser", NULL);
938 if (result != NULL)
940 ft->lang = tm_source_file_get_named_lang(result);
941 if (ft->lang == TM_PARSER_NONE)
942 geany_debug("Cannot find tags parser '%s' for custom filetype '%s'.", result, ft->name);
943 g_free(result);
946 result = utils_get_setting(string, configh, config, "settings", "lexer_filetype", NULL);
947 if (result != NULL)
949 ft->lexer_filetype = filetypes_lookup_by_name(result);
950 if (!ft->lexer_filetype)
951 geany_debug("Cannot find lexer filetype '%s' for custom filetype '%s'.", result, ft->name);
952 g_free(result);
955 ft->priv->symbol_list_sort_mode = utils_get_setting(integer, configh, config, "settings",
956 "symbol_list_sort_mode", SYMBOLS_SORT_USE_PREVIOUS);
957 ft->priv->xml_indent_tags = utils_get_setting(boolean, configh, config, "settings",
958 "xml_indent_tags", FALSE);
960 /* read indent settings */
961 load_indent_settings(ft, config, configh);
963 /* read build settings */
964 build_load_menu(config, GEANY_BCS_FT, (gpointer)ft);
965 build_load_menu(configh, GEANY_BCS_HOME_FT, (gpointer)ft);
969 static void copy_keys(GKeyFile *dest, const gchar *dest_group,
970 GKeyFile *src, const gchar *src_group)
972 gchar **keys = g_key_file_get_keys(src, src_group, NULL, NULL);
973 gchar **ptr;
975 foreach_strv(ptr, keys)
977 gchar *key = *ptr;
978 gchar *value = g_key_file_get_value(src, src_group, key, NULL);
980 g_key_file_set_value(dest, dest_group, key, value);
981 g_free(value);
983 g_strfreev(keys);
987 static gchar *filetypes_get_filename(GeanyFiletype *ft, gboolean user)
989 gchar *ext = filetypes_get_conf_extension(ft);
990 gchar *base_name = g_strconcat("filetypes.", ext, NULL);
991 gchar *file_name;
993 if (user)
994 file_name = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, base_name, NULL);
995 else
996 file_name = g_build_filename(app->datadir, GEANY_FILEDEFS_SUBDIR, base_name, NULL);
998 g_free(ext);
999 g_free(base_name);
1001 return file_name;
1005 static void add_group_keys(GKeyFile *kf, const gchar *group, GeanyFiletype *ft)
1007 gchar *files[2];
1008 gboolean loaded = FALSE;
1009 guint i;
1011 files[0] = filetypes_get_filename(ft, FALSE);
1012 files[1] = filetypes_get_filename(ft, TRUE);
1014 for (i = 0; i < G_N_ELEMENTS(files); i++)
1016 GKeyFile *src = g_key_file_new();
1018 if (g_key_file_load_from_file(src, files[i], G_KEY_FILE_NONE, NULL))
1020 copy_keys(kf, group, src, group);
1021 loaded = TRUE;
1023 g_key_file_free(src);
1026 if (!loaded)
1027 geany_debug("Could not read config file %s for [%s=%s]!", files[0], group, ft->name);
1029 g_free(files[0]);
1030 g_free(files[1]);
1034 static void copy_ft_groups(GKeyFile *kf)
1036 gchar **groups = g_key_file_get_groups(kf, NULL);
1037 gchar **ptr;
1039 foreach_strv(ptr, groups)
1041 gchar *group = *ptr;
1042 gchar *old_group;
1043 gchar *name = strchr(*ptr, '=');
1044 GeanyFiletype *ft;
1046 if (!name || !name[1]) /* no name or no parent name */
1047 continue;
1049 old_group = g_strdup(group);
1051 /* terminate group at '=' */
1052 *name = 0;
1053 name++;
1055 ft = filetypes_lookup_by_name(name);
1056 if (ft)
1058 add_group_keys(kf, group, ft);
1059 /* move old group keys (foo=bar) to proper group name (foo) */
1060 copy_keys(kf, group, kf, old_group);
1062 g_free(old_group);
1064 g_strfreev(groups);
1068 /* simple wrapper function to print file errors in DEBUG mode */
1069 static void load_system_keyfile(GKeyFile *key_file, const gchar *file, GKeyFileFlags flags,
1070 GeanyFiletype *ft)
1072 GError *error = NULL;
1073 gboolean done = g_key_file_load_from_file(key_file, file, flags, &error);
1075 if (error != NULL)
1077 if (!done && !ft->priv->custom)
1078 geany_debug("Failed to open %s (%s)", file, error->message);
1080 g_error_free(error);
1081 error = NULL;
1086 /* Load the configuration file for the associated filetype id.
1087 * This should only be called when the filetype is needed, to save loading
1088 * 20+ configuration files all at once. */
1089 void filetypes_load_config(guint ft_id, gboolean reload)
1091 GKeyFile *config, *config_home;
1092 GeanyFiletypePrivate *pft;
1093 GeanyFiletype *ft;
1095 g_return_if_fail(ft_id < filetypes_array->len);
1097 ft = filetypes[ft_id];
1098 pft = ft->priv;
1100 /* when reloading, proceed only if the settings were already loaded */
1101 if (G_UNLIKELY(reload && ! pft->keyfile_loaded))
1102 return;
1104 /* when not reloading, load the settings only once */
1105 if (G_LIKELY(! reload && pft->keyfile_loaded))
1106 return;
1107 pft->keyfile_loaded = TRUE;
1109 config = g_key_file_new();
1110 config_home = g_key_file_new();
1112 /* highlighting uses GEANY_FILETYPES_NONE for common settings */
1113 gchar *f;
1115 f = filetypes_get_filename(ft, FALSE);
1116 load_system_keyfile(config, f, G_KEY_FILE_KEEP_COMMENTS, ft);
1118 SETPTR(f, filetypes_get_filename(ft, TRUE));
1119 g_key_file_load_from_file(config_home, f, G_KEY_FILE_KEEP_COMMENTS, NULL);
1120 g_free(f);
1122 /* Copy keys for any groups with [group=C] from system keyfile */
1123 copy_ft_groups(config);
1124 copy_ft_groups(config_home);
1126 load_settings(ft_id, config, config_home);
1127 highlighting_init_styles(ft_id, config, config_home);
1129 if (ft->icon)
1130 g_object_unref(ft->icon);
1131 ft->icon = ui_get_mime_icon(ft->mime_type);
1133 g_key_file_free(config);
1134 g_key_file_free(config_home);
1138 static gchar *filetypes_get_conf_extension(const GeanyFiletype *ft)
1140 gchar *result;
1142 if (ft->priv->custom)
1143 return g_strconcat(ft->name, ".conf", NULL);
1145 /* Handle any special extensions different from lowercase filetype->name */
1146 switch (ft->id)
1148 case GEANY_FILETYPES_CPP: result = g_strdup("cpp"); break;
1149 case GEANY_FILETYPES_CS: result = g_strdup("cs"); break;
1150 case GEANY_FILETYPES_MAKE: result = g_strdup("makefile"); break;
1151 case GEANY_FILETYPES_NONE: result = g_strdup("common"); break;
1152 /* name is Matlab/Octave */
1153 case GEANY_FILETYPES_MATLAB: result = g_strdup("matlab"); break;
1154 /* name is Objective-C, and we don't want the hyphen */
1155 case GEANY_FILETYPES_OBJECTIVEC: result = g_strdup("objectivec"); break;
1156 default:
1157 result = g_ascii_strdown(ft->name, -1);
1158 break;
1160 return result;
1164 void filetypes_save_commands(GeanyFiletype *ft)
1166 GKeyFile *config_home;
1167 gchar *fname, *data;
1169 fname = filetypes_get_filename(ft, TRUE);
1170 config_home = g_key_file_new();
1171 g_key_file_load_from_file(config_home, fname, G_KEY_FILE_KEEP_COMMENTS, NULL);
1172 build_save_menu(config_home, ft, GEANY_BCS_HOME_FT);
1173 data = g_key_file_to_data(config_home, NULL, NULL);
1174 utils_write_file(fname, data);
1175 g_free(data);
1176 g_key_file_free(config_home);
1177 g_free(fname);
1181 /* create one file filter which has each file pattern of each filetype */
1182 GtkFileFilter *filetypes_create_file_filter_all_source(void)
1184 GtkFileFilter *new_filter;
1185 guint i, j;
1187 new_filter = gtk_file_filter_new();
1188 gtk_file_filter_set_name(new_filter, _("All Source"));
1190 for (i = 0; i < filetypes_array->len; i++)
1192 if (G_UNLIKELY(i == GEANY_FILETYPES_NONE))
1193 continue;
1195 for (j = 0; filetypes[i]->pattern[j]; j++)
1197 gtk_file_filter_add_pattern(new_filter, filetypes[i]->pattern[j]);
1200 return new_filter;
1204 GtkFileFilter *filetypes_create_file_filter(const GeanyFiletype *ft)
1206 GtkFileFilter *new_filter;
1207 gint i;
1208 const gchar *title;
1210 g_return_val_if_fail(ft != NULL, NULL);
1212 new_filter = gtk_file_filter_new();
1213 title = ft->id == GEANY_FILETYPES_NONE ? _("All files") : ft->title;
1214 gtk_file_filter_set_name(new_filter, title);
1216 for (i = 0; ft->pattern[i]; i++)
1218 gtk_file_filter_add_pattern(new_filter, ft->pattern[i]);
1221 return new_filter;
1225 /* Indicates whether there is a tag parser for the filetype or not.
1226 * Only works for custom filetypes if the filetype settings have been loaded. */
1227 gboolean filetype_has_tags(GeanyFiletype *ft)
1229 g_return_val_if_fail(ft != NULL, FALSE);
1231 return ft->lang != TM_PARSER_NONE;
1235 /** Finds a filetype pointer from its @a name field.
1236 * @param name Filetype name.
1237 * @return @transfer{none} @nullable The filetype found, or @c NULL.
1239 * @since 0.15
1241 GEANY_API_SYMBOL
1242 GeanyFiletype *filetypes_lookup_by_name(const gchar *name)
1244 GeanyFiletype *ft;
1246 g_return_val_if_fail(!EMPTY(name), NULL);
1248 ft = g_hash_table_lookup(filetypes_hash, name);
1249 if (G_UNLIKELY(ft == NULL))
1250 geany_debug("Could not find filetype '%s'.", name);
1251 return ft;
1255 static void compile_regex(GeanyFiletype *ft, gchar *regstr)
1257 GError *error = NULL;
1258 GRegex *regex = g_regex_new(regstr, 0, 0, &error);
1260 if (!regex)
1262 ui_set_statusbar(TRUE, _("Bad regex for filetype %s: %s"),
1263 filetypes_get_display_name(ft), error->message);
1264 g_error_free(error);
1266 if (ft->priv->error_regex)
1267 g_regex_unref(ft->priv->error_regex);
1268 ft->priv->error_regex = regex;
1272 gboolean filetypes_parse_error_message(GeanyFiletype *ft, const gchar *message,
1273 gchar **filename, gint *line)
1275 gchar *regstr;
1276 gchar **tmp;
1277 GeanyDocument *doc;
1278 GMatchInfo *minfo;
1279 gint i, n_match_groups;
1280 gchar *first, *second;
1282 if (ft == NULL)
1284 doc = document_get_current();
1285 if (doc != NULL)
1286 ft = doc->file_type;
1288 tmp = build_get_regex(build_info.grp, ft, NULL);
1289 if (tmp == NULL)
1290 return FALSE;
1291 regstr = *tmp;
1293 *filename = NULL;
1294 *line = -1;
1296 if (G_UNLIKELY(EMPTY(regstr)))
1297 return FALSE;
1299 if (!ft->priv->error_regex || regstr != ft->priv->last_error_pattern)
1301 compile_regex(ft, regstr);
1302 ft->priv->last_error_pattern = regstr;
1304 if (!ft->priv->error_regex)
1305 return FALSE;
1307 if (!g_regex_match(ft->priv->error_regex, message, 0, &minfo))
1309 g_match_info_free(minfo);
1310 return FALSE;
1313 n_match_groups = g_match_info_get_match_count(minfo);
1314 first = second = NULL;
1316 for (i = 1; i < n_match_groups; i++)
1318 gint start_pos;
1320 g_match_info_fetch_pos(minfo, i, &start_pos, NULL);
1321 if (start_pos != -1)
1323 if (first == NULL)
1324 first = g_match_info_fetch(minfo, i);
1325 else
1327 second = g_match_info_fetch(minfo, i);
1328 break;
1333 if (second)
1335 gchar *end;
1336 glong l;
1338 l = strtol(first, &end, 10);
1339 if (*end == '\0') /* first is purely decimals */
1341 *line = l;
1342 g_free(first);
1343 *filename = second;
1345 else
1347 l = strtol(second, &end, 10);
1348 if (*end == '\0')
1350 *line = l;
1351 g_free(second);
1352 *filename = first;
1354 else
1356 g_free(first);
1357 g_free(second);
1361 else
1362 g_free(first);
1364 g_match_info_free(minfo);
1365 return *filename != NULL;
1369 #ifdef G_OS_WIN32
1370 static void convert_filetype_extensions_to_lower_case(gchar **patterns, gsize len)
1372 guint i;
1373 for (i = 0; i < len; i++)
1375 SETPTR(patterns[i], g_ascii_strdown(patterns[i], -1));
1378 #endif
1381 static void read_extensions(GKeyFile *sysconfig, GKeyFile *userconfig)
1383 guint i;
1384 gsize len = 0;
1386 /* read the keys */
1387 for (i = 0; i < filetypes_array->len; i++)
1389 gboolean userset =
1390 g_key_file_has_key(userconfig, "Extensions", filetypes[i]->name, NULL);
1391 gchar **list = g_key_file_get_string_list(
1392 (userset) ? userconfig : sysconfig, "Extensions", filetypes[i]->name, &len, NULL);
1394 filetypes[i]->priv->user_extensions = userset;
1395 g_strfreev(filetypes[i]->pattern);
1396 /* Note: we allow 'Foo=' to remove all patterns */
1397 if (!list)
1398 list = g_new0(gchar*, 1);
1399 filetypes[i]->pattern = list;
1401 #ifdef G_OS_WIN32
1402 convert_filetype_extensions_to_lower_case(filetypes[i]->pattern, len);
1403 #endif
1408 static void read_group(GKeyFile *config, const gchar *group_name, GeanyFiletypeGroupID group_id)
1410 gchar **names = g_key_file_get_string_list(config, "Groups", group_name, NULL, NULL);
1411 gchar **name;
1413 foreach_strv(name, names)
1415 GeanyFiletype *ft = filetypes_lookup_by_name(*name);
1417 if (ft)
1419 ft->group = group_id;
1420 if (ft->priv->custom &&
1421 (group_id == GEANY_FILETYPE_GROUP_COMPILED || group_id == GEANY_FILETYPE_GROUP_SCRIPT))
1423 SETPTR(ft->title, filetype_make_title(ft->name, TITLE_SOURCE_FILE));
1426 else
1427 geany_debug("Filetype '%s' not found for group '%s'!", *name, group_name);
1429 g_strfreev(names);
1433 static void read_groups(GKeyFile *config)
1435 read_group(config, "Programming", GEANY_FILETYPE_GROUP_COMPILED);
1436 read_group(config, "Script", GEANY_FILETYPE_GROUP_SCRIPT);
1437 read_group(config, "Markup", GEANY_FILETYPE_GROUP_MARKUP);
1438 read_group(config, "Misc", GEANY_FILETYPE_GROUP_MISC);
1439 read_group(config, "None", GEANY_FILETYPE_GROUP_NONE);
1443 static void read_filetype_config(void)
1445 gchar *sysconfigfile = g_build_filename(app->datadir, "filetype_extensions.conf", NULL);
1446 gchar *userconfigfile = g_build_filename(app->configdir, "filetype_extensions.conf", NULL);
1447 GKeyFile *sysconfig = g_key_file_new();
1448 GKeyFile *userconfig = g_key_file_new();
1450 g_key_file_load_from_file(sysconfig, sysconfigfile, G_KEY_FILE_NONE, NULL);
1451 g_key_file_load_from_file(userconfig, userconfigfile, G_KEY_FILE_NONE, NULL);
1453 read_extensions(sysconfig, userconfig);
1454 read_groups(sysconfig);
1455 read_groups(userconfig);
1457 g_free(sysconfigfile);
1458 g_free(userconfigfile);
1459 g_key_file_free(sysconfig);
1460 g_key_file_free(userconfig);
1464 void filetypes_reload_extensions(void)
1466 guint i;
1468 read_filetype_config();
1470 /* Redetect filetype of any documents with none set */
1471 foreach_document(i)
1473 GeanyDocument *doc = documents[i];
1474 if (doc->file_type->id != GEANY_FILETYPES_NONE)
1475 continue;
1476 document_set_filetype(doc, filetypes_detect_from_document(doc));
1481 /** Accessor function for @ref GeanyData::filetypes_array items.
1482 * Example: @code ft = filetypes_index(GEANY_FILETYPES_C); @endcode
1483 * @param idx @c filetypes_array index.
1484 * @return @transfer{none} @nullable The filetype, or @c NULL if @a idx is out of range.
1486 * @since 0.16
1488 GEANY_API_SYMBOL
1489 GeanyFiletype *filetypes_index(gint idx)
1491 return (idx >= 0 && idx < (gint) filetypes_array->len) ? filetypes[idx] : NULL;
1495 void filetypes_reload(void)
1497 guint i;
1498 GeanyDocument *current_doc;
1500 /* reload filetype configs */
1501 for (i = 0; i < filetypes_array->len; i++)
1503 /* filetypes_load_config() will skip not loaded filetypes */
1504 filetypes_load_config(i, TRUE);
1507 current_doc = document_get_current();
1508 if (!current_doc)
1509 return;
1511 /* update document styling */
1512 foreach_document(i)
1514 if (current_doc != documents[i])
1515 document_reload_config(documents[i]);
1517 /* process the current document at last */
1518 document_reload_config(current_doc);
1522 /** Gets @c ft->name or a translation for filetype None.
1523 * @param ft .
1524 * @return .
1525 * @since Geany 0.20 */
1526 GEANY_API_SYMBOL
1527 const gchar *filetypes_get_display_name(GeanyFiletype *ft)
1529 return ft->id == GEANY_FILETYPES_NONE ? _("None") : ft->name;
1533 /* gets comment_open/comment_close/comment_single strings from the filetype
1534 * @param single_first: whether single comment is preferred if both available
1535 * returns true if at least comment_open is set, false otherwise */
1536 gboolean filetype_get_comment_open_close(const GeanyFiletype *ft, gboolean single_first,
1537 const gchar **co, const gchar **cc)
1539 g_return_val_if_fail(ft != NULL, FALSE);
1540 g_return_val_if_fail(co != NULL, FALSE);
1541 g_return_val_if_fail(cc != NULL, FALSE);
1543 if (single_first)
1545 *co = ft->comment_single;
1546 if (!EMPTY(*co))
1547 *cc = NULL;
1548 else
1550 *co = ft->comment_open;
1551 *cc = ft->comment_close;
1554 else
1556 *co = ft->comment_open;
1557 if (!EMPTY(*co))
1558 *cc = ft->comment_close;
1559 else
1561 *co = ft->comment_single;
1562 *cc = NULL;
1566 return !EMPTY(*co);
1569 static void *copy_(void *src) { return src; }
1570 static void free_(void *doc) { }
1572 /** @gironly
1573 * Gets the GType of GeanyFiletype
1575 * @return the GeanyFiletype type */
1576 GEANY_API_SYMBOL
1577 GType filetype_get_type (void);
1579 G_DEFINE_BOXED_TYPE(GeanyFiletype, filetype, copy_, free_);