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.
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. */
34 #include "filetypes.h"
37 #include "callbacks.h" /* FIXME: for ignore_callback */
39 #include "filetypesprivate.h"
41 #include "geanyobject.h"
42 #include "highlighting.h"
43 #include "projectprivate.h"
44 #include "sciwrappers.h"
47 #include "tm_parser.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
;
69 static GtkWidget
*group_menus
[GEANY_FILETYPE_GROUP_COUNT
] = {NULL
};
72 static void create_radio_menu_item(GtkWidget
*menu
, GeanyFiletype
*ftype
);
74 static gchar
*filetypes_get_conf_extension(const GeanyFiletype
*ft
);
75 static void read_filetype_config(void);
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
);
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
];
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)
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
, NONE
, "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
, NONE
, "PowerShell", NULL
, SOURCE_FILE
, SCRIPT
);
192 FT_INIT( RUST
, RUST
, "Rust", NULL
, SOURCE_FILE
, COMPILED
);
193 FT_INIT( GO
, GO
, "Go", NULL
, SOURCE_FILE
, COMPILED
);
197 /* initialize fields. */
198 static GeanyFiletype
*filetype_new(void)
200 GeanyFiletype
*ft
= g_new0(GeanyFiletype
, 1);
202 ft
->group
= GEANY_FILETYPE_GROUP_NONE
;
203 ft
->lang
= -2; /* assume no tagmanager parser */
204 /* pattern must not be null */
205 ft
->pattern
= g_new0(gchar
*, 1);
206 ft
->indent_width
= -1;
207 ft
->indent_type
= -1;
209 ft
->priv
= g_new0(GeanyFiletypePrivate
, 1);
210 ft
->priv
->project_list_entry
= -1; /* no entry */
216 static gint
cmp_filetype(gconstpointer pft1
, gconstpointer pft2
, gpointer data
)
218 gboolean by_name
= GPOINTER_TO_INT(data
);
219 const GeanyFiletype
*ft1
= pft1
, *ft2
= pft2
;
221 if (G_UNLIKELY(ft1
->id
== GEANY_FILETYPES_NONE
))
223 if (G_UNLIKELY(ft2
->id
== GEANY_FILETYPES_NONE
))
227 utils_str_casecmp(ft1
->name
, ft2
->name
) :
228 utils_str_casecmp(ft1
->title
, ft2
->title
);
232 /** Gets a list of filetype pointers sorted by name.
233 * The list does not change on subsequent calls.
234 * @return The list - do not free.
235 * @see filetypes_by_title. */
236 const GSList
*filetypes_get_sorted_by_name(void)
238 static GSList
*list
= NULL
;
240 g_return_val_if_fail(filetypes_by_title
, NULL
);
244 list
= g_slist_copy(filetypes_by_title
);
245 list
= g_slist_sort_with_data(list
, cmp_filetype
, GINT_TO_POINTER(TRUE
));
251 /* Add a filetype pointer to the lists of available filetypes,
252 * and set the filetype::id field. */
253 static void filetype_add(GeanyFiletype
*ft
)
255 g_return_if_fail(ft
);
256 g_return_if_fail(ft
->name
);
258 ft
->id
= filetypes_array
->len
; /* len will be the index for filetype_array */
259 g_ptr_array_add(filetypes_array
, ft
);
260 g_hash_table_insert(filetypes_hash
, ft
->name
, ft
);
262 /* list will be sorted later */
263 filetypes_by_title
= g_slist_prepend(filetypes_by_title
, ft
);
267 static void add_custom_filetype(const gchar
*filename
)
269 gchar
*fn
= utils_strdupa(strstr(filename
, ".") + 1);
270 gchar
*dot
= g_strrstr(fn
, ".conf");
273 g_return_if_fail(dot
);
277 if (g_hash_table_lookup(filetypes_hash
, fn
))
281 ft
->name
= g_strdup(fn
);
282 ft
->title
= filetype_make_title(ft
->name
, TITLE_FILE
);
283 ft
->priv
->custom
= TRUE
;
285 geany_debug("Added filetype %s (%d).", ft
->name
, ft
->id
);
289 static void init_custom_filetypes(const gchar
*path
)
292 const gchar
*filename
;
294 g_return_if_fail(path
);
296 dir
= g_dir_open(path
, 0, NULL
);
300 foreach_dir(filename
, dir
)
302 const gchar prefix
[] = "filetypes.";
304 if (g_str_has_prefix(filename
, prefix
) &&
305 g_str_has_suffix(filename
+ strlen(prefix
), ".conf"))
307 add_custom_filetype(filename
);
314 /* Create the filetypes array and fill it with the known filetypes.
315 * Warning: GTK isn't necessarily initialized yet. */
316 void filetypes_init_types(void)
321 g_return_if_fail(filetypes_array
== NULL
);
322 g_return_if_fail(filetypes_hash
== NULL
);
324 filetypes_array
= g_ptr_array_sized_new(GEANY_MAX_BUILT_IN_FILETYPES
);
325 filetypes_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
327 /* Create built-in filetypes */
328 for (ft_id
= 0; ft_id
< GEANY_MAX_BUILT_IN_FILETYPES
; ft_id
++)
330 filetypes
[ft_id
] = filetype_new();
332 init_builtin_filetypes();
334 /* Add built-in filetypes to the hash now the name fields are set */
335 for (ft_id
= 0; ft_id
< GEANY_MAX_BUILT_IN_FILETYPES
; ft_id
++)
337 filetype_add(filetypes
[ft_id
]);
339 init_custom_filetypes(app
->datadir
);
340 f
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, NULL
);
341 init_custom_filetypes(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
)
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();
363 f
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.common", NULL
);
364 if (utils_str_equal(doc
->real_path
, f
))
368 /* Note: we don't reload other filetypes, even though the named styles may have changed.
369 * The user can do this manually with 'Tools->Reload Configuration' */
370 filetypes_load_config(GEANY_FILETYPES_NONE
, TRUE
);
373 document_reload_config(documents
[i
]);
379 static void setup_config_file_menus(void)
383 f
= g_build_filename(app
->configdir
, "filetype_extensions.conf", NULL
);
384 ui_add_config_file_menu_item(f
, NULL
, NULL
);
385 SETPTR(f
, g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.common", NULL
));
386 ui_add_config_file_menu_item(f
, NULL
, NULL
);
389 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
393 static void create_sub_menu(GtkWidget
*parent
, GeanyFiletypeGroupID group_id
, const gchar
*title
)
395 GtkWidget
*menu
, *item
;
397 menu
= gtk_menu_new();
398 item
= gtk_menu_item_new_with_mnemonic((title
));
399 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), menu
);
400 gtk_container_add(GTK_CONTAINER(parent
), item
);
401 gtk_widget_show(item
);
402 group_menus
[group_id
] = menu
;
406 static void create_set_filetype_menu(void)
409 GtkWidget
*filetype_menu
= ui_lookup_widget(main_widgets
.window
, "set_filetype1_menu");
411 create_sub_menu(filetype_menu
, GEANY_FILETYPE_GROUP_COMPILED
, _("_Programming Languages"));
412 create_sub_menu(filetype_menu
, GEANY_FILETYPE_GROUP_SCRIPT
, _("_Scripting Languages"));
413 create_sub_menu(filetype_menu
, GEANY_FILETYPE_GROUP_MARKUP
, _("_Markup Languages"));
414 create_sub_menu(filetype_menu
, GEANY_FILETYPE_GROUP_MISC
, _("M_iscellaneous"));
416 /* Append all filetypes to the filetype menu */
417 foreach_slist(node
, filetypes_by_title
)
419 GeanyFiletype
*ft
= node
->data
;
421 if (ft
->group
!= GEANY_FILETYPE_GROUP_NONE
)
422 create_radio_menu_item(group_menus
[ft
->group
], ft
);
424 create_radio_menu_item(filetype_menu
, ft
);
429 void filetypes_init(void)
431 filetypes_init_types();
432 create_set_filetype_menu();
433 setup_config_file_menus();
437 /* Find a filetype that predicate returns TRUE for, otherwise return NULL. */
438 GeanyFiletype
*filetypes_find(GCompareFunc predicate
, gpointer user_data
)
442 for (i
= 0; i
< filetypes_array
->len
; i
++)
444 GeanyFiletype
*ft
= filetypes
[i
];
446 if (predicate(ft
, user_data
))
453 static gboolean
match_basename(gconstpointer pft
, gconstpointer user_data
)
455 const GeanyFiletype
*ft
= pft
;
456 const gchar
*base_filename
= user_data
;
458 gboolean ret
= FALSE
;
460 if (G_UNLIKELY(ft
->id
== GEANY_FILETYPES_NONE
))
463 for (j
= 0; ft
->pattern
[j
] != NULL
; j
++)
465 GPatternSpec
*pattern
= g_pattern_spec_new(ft
->pattern
[j
]);
467 if (g_pattern_match_string(pattern
, base_filename
))
470 g_pattern_spec_free(pattern
);
473 g_pattern_spec_free(pattern
);
479 static GeanyFiletype
*check_builtin_filenames(const gchar
*utf8_filename
)
483 gboolean found
= FALSE
;
486 /* use lower case basename */
487 lfn
= g_utf8_strdown(utf8_filename
, -1);
489 lfn
= g_strdup(utf8_filename
);
491 SETPTR(lfn
, utils_get_locale_from_utf8(lfn
));
493 path
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.", NULL
);
494 if (g_str_has_prefix(lfn
, path
))
497 SETPTR(path
, g_build_filename(app
->datadir
, "filetypes.", NULL
));
498 if (g_str_has_prefix(lfn
, path
))
503 return found
? filetypes
[GEANY_FILETYPES_CONF
] : NULL
;
507 /* Detect filetype only based on the filename extension.
508 * utf8_filename can include the full path. */
509 GeanyFiletype
*filetypes_detect_from_extension(const gchar
*utf8_filename
)
511 gchar
*base_filename
;
514 ft
= check_builtin_filenames(utf8_filename
);
518 /* to match against the basename of the file (because of Makefile*) */
519 base_filename
= g_path_get_basename(utf8_filename
);
521 /* use lower case basename */
522 SETPTR(base_filename
, g_utf8_strdown(base_filename
, -1));
525 ft
= filetypes_find(match_basename
, base_filename
);
527 ft
= filetypes
[GEANY_FILETYPES_NONE
];
529 g_free(base_filename
);
534 /* This detects the filetype of the file pointed by 'utf8_filename' and a list of filetype id's,
536 * The detected filetype of the file is checked against every id in the passed list and if
537 * there is a match, TRUE is returned. */
538 static gboolean
shebang_find_and_match_filetype(const gchar
*utf8_filename
, gint first
, ...)
540 GeanyFiletype
*ft
= NULL
;
542 gboolean result
= FALSE
;
545 ft
= filetypes_detect_from_extension(utf8_filename
);
546 if (ft
== NULL
|| ft
->id
>= filetypes_array
->len
)
549 va_start(args
, first
);
556 if (ft
->id
== (guint
) test
)
561 test
= va_arg(args
, gint
);
569 static GeanyFiletype
*find_shebang(const gchar
*utf8_filename
, const gchar
*line
)
571 GeanyFiletype
*ft
= NULL
;
573 if (strlen(line
) > 2 && line
[0] == '#' && line
[1] == '!')
575 static const struct {
577 filetype_id filetype
;
578 } intepreter_map
[] = {
579 { "sh", GEANY_FILETYPES_SH
},
580 { "bash", GEANY_FILETYPES_SH
},
581 { "dash", GEANY_FILETYPES_SH
},
582 { "perl", GEANY_FILETYPES_PERL
},
583 { "python", GEANY_FILETYPES_PYTHON
},
584 { "php", GEANY_FILETYPES_PHP
},
585 { "ruby", GEANY_FILETYPES_RUBY
},
586 { "tcl", GEANY_FILETYPES_TCL
},
587 { "make", GEANY_FILETYPES_MAKE
},
588 { "zsh", GEANY_FILETYPES_SH
},
589 { "ksh", GEANY_FILETYPES_SH
},
590 { "mksh", GEANY_FILETYPES_SH
},
591 { "csh", GEANY_FILETYPES_SH
},
592 { "tcsh", GEANY_FILETYPES_SH
},
593 { "ash", GEANY_FILETYPES_SH
},
594 { "dmd", GEANY_FILETYPES_D
},
595 { "wish", GEANY_FILETYPES_TCL
},
596 { "node", GEANY_FILETYPES_JS
},
597 { "rust", GEANY_FILETYPES_RUST
}
599 gchar
*tmp
= g_path_get_basename(line
+ 2);
600 gchar
*basename_interpreter
= tmp
;
603 if (g_str_has_prefix(tmp
, "env "))
604 { /* skip "env" and read the following interpreter */
605 basename_interpreter
+= 4;
608 for (i
= 0; ! ft
&& i
< G_N_ELEMENTS(intepreter_map
); i
++)
610 if (g_str_has_prefix(basename_interpreter
, intepreter_map
[i
].name
))
611 ft
= filetypes
[intepreter_map
[i
].filetype
];
615 /* detect HTML files */
616 if (g_str_has_prefix(line
, "<!DOCTYPE html") || g_str_has_prefix(line
, "<html"))
618 /* PHP, Perl and Python files might also start with <html, so detect them based on filename
619 * extension and use the detected filetype, else assume HTML */
620 if (! shebang_find_and_match_filetype(utf8_filename
,
621 GEANY_FILETYPES_PERL
, GEANY_FILETYPES_PHP
, GEANY_FILETYPES_PYTHON
, -1))
623 ft
= filetypes
[GEANY_FILETYPES_HTML
];
626 /* detect XML files */
627 else if (utf8_filename
&& g_str_has_prefix(line
, "<?xml"))
629 /* HTML and DocBook files might also start with <?xml, so detect them based on filename
630 * extension and use the detected filetype, else assume XML */
631 if (! shebang_find_and_match_filetype(utf8_filename
,
632 GEANY_FILETYPES_HTML
, GEANY_FILETYPES_DOCBOOK
,
633 /* Perl, Python and PHP only to be safe */
634 GEANY_FILETYPES_PERL
, GEANY_FILETYPES_PHP
, GEANY_FILETYPES_PYTHON
, -1))
636 ft
= filetypes
[GEANY_FILETYPES_XML
];
639 else if (g_str_has_prefix(line
, "<?php"))
641 ft
= filetypes
[GEANY_FILETYPES_PHP
];
647 /* Detect the filetype checking for a shebang, then filename extension.
648 * @lines: an strv of the lines to scan (must containing at least one line) */
649 static GeanyFiletype
*filetypes_detect_from_file_internal(const gchar
*utf8_filename
,
656 GError
*regex_error
= NULL
;
658 /* try to find a shebang and if found use it prior to the filename extension
659 * also checks for <?xml */
660 ft
= find_shebang(utf8_filename
, lines
[0]);
664 /* try to extract the filetype using a regex capture */
665 ft_regex
= g_regex_new(file_prefs
.extract_filetype_regex
,
666 G_REGEX_RAW
| G_REGEX_MULTILINE
, 0, ®ex_error
);
667 if (ft_regex
!= NULL
)
669 for (i
= 0; ft
== NULL
&& lines
[i
] != NULL
; i
++)
671 if (g_regex_match(ft_regex
, lines
[i
], 0, &match
))
673 gchar
*capture
= g_match_info_fetch(match
, 1);
676 ft
= filetypes_lookup_by_name(capture
);
680 g_match_info_free(match
);
682 g_regex_unref(ft_regex
);
684 else if (regex_error
!= NULL
)
686 geany_debug("Filetype extract regex ignored: %s", regex_error
->message
);
687 g_error_free(regex_error
);
692 if (utf8_filename
== NULL
)
693 return filetypes
[GEANY_FILETYPES_NONE
];
695 return filetypes_detect_from_extension(utf8_filename
);
699 /* Detect the filetype for the document, checking for a shebang, then filename extension. */
700 GeanyFiletype
*filetypes_detect_from_document(GeanyDocument
*doc
)
703 gchar
*lines
[GEANY_FILETYPE_SEARCH_LINES
+ 1];
706 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, filetypes
[GEANY_FILETYPES_NONE
]);
709 return filetypes
[GEANY_FILETYPES_NONE
];
711 for (i
= 0; i
< GEANY_FILETYPE_SEARCH_LINES
; ++i
)
713 lines
[i
] = sci_get_line(doc
->editor
->sci
, i
);
716 ft
= filetypes_detect_from_file_internal(doc
->file_name
, lines
);
717 for (i
= 0; i
< GEANY_FILETYPE_SEARCH_LINES
; ++i
)
726 /* Currently only used by external plugins (e.g. geanyprj). */
728 * Detects filetype based on a shebang line in the file or the filename extension.
730 * @param utf8_filename The filename in UTF-8 encoding.
732 * @return The detected filetype for @a utf8_filename or @c filetypes[GEANY_FILETYPES_NONE]
733 * if it could not be detected.
735 GeanyFiletype
*filetypes_detect_from_file(const gchar
*utf8_filename
)
740 gchar
*locale_name
= utils_get_locale_from_utf8(utf8_filename
);
742 f
= g_fopen(locale_name
, "r");
746 if (fgets(line
, sizeof(line
), f
) != NULL
)
751 return filetypes_detect_from_file_internal(utf8_filename
, lines
);
755 return filetypes_detect_from_extension(utf8_filename
);
760 void filetypes_select_radio_item(const GeanyFiletype
*ft
)
762 /* ignore_callback has to be set by the caller */
763 g_return_if_fail(ignore_callback
);
766 ft
= filetypes
[GEANY_FILETYPES_NONE
];
768 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ft
->priv
->menu_item
), TRUE
);
773 on_filetype_change(GtkCheckMenuItem
*menuitem
, gpointer user_data
)
775 GeanyDocument
*doc
= document_get_current();
776 if (ignore_callback
|| doc
== NULL
|| ! gtk_check_menu_item_get_active(menuitem
))
779 document_set_filetype(doc
, (GeanyFiletype
*)user_data
);
783 static void create_radio_menu_item(GtkWidget
*menu
, GeanyFiletype
*ftype
)
785 static GSList
*group
= NULL
;
788 tmp
= gtk_radio_menu_item_new_with_label(group
, ftype
->title
);
789 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(tmp
));
790 ftype
->priv
->menu_item
= tmp
;
791 gtk_widget_show(tmp
);
792 gtk_container_add(GTK_CONTAINER(menu
), tmp
);
793 g_signal_connect(tmp
, "activate", G_CALLBACK(on_filetype_change
), (gpointer
) ftype
);
797 static void filetype_free(gpointer data
, G_GNUC_UNUSED gpointer user_data
)
799 GeanyFiletype
*ft
= data
;
801 g_return_if_fail(ft
!= NULL
);
805 g_free(ft
->extension
);
806 g_free(ft
->mime_type
);
807 g_free(ft
->comment_open
);
808 g_free(ft
->comment_close
);
809 g_free(ft
->comment_single
);
810 g_free(ft
->context_action_cmd
);
811 g_free(ft
->priv
->filecmds
);
812 g_free(ft
->priv
->ftdefcmds
);
813 g_free(ft
->priv
->execcmds
);
814 g_free(ft
->error_regex_string
);
816 g_object_unref(ft
->icon
);
817 g_strfreev(ft
->pattern
);
819 if (ft
->priv
->error_regex
)
820 g_regex_unref(ft
->priv
->error_regex
);
821 g_slist_foreach(ft
->priv
->tag_files
, (GFunc
) g_free
, NULL
);
822 g_slist_free(ft
->priv
->tag_files
);
829 /* frees the array and all related pointers */
830 void filetypes_free_types(void)
832 g_return_if_fail(filetypes_array
!= NULL
);
833 g_return_if_fail(filetypes_hash
!= NULL
);
835 g_ptr_array_foreach(filetypes_array
, filetype_free
, NULL
);
836 g_ptr_array_free(filetypes_array
, TRUE
);
837 g_hash_table_destroy(filetypes_hash
);
841 static void load_indent_settings(GeanyFiletype
*ft
, GKeyFile
*config
, GKeyFile
*configh
)
843 ft
->indent_width
= utils_get_setting(integer
, configh
, config
, "indentation", "width", -1);
844 ft
->indent_type
= utils_get_setting(integer
, configh
, config
, "indentation", "type", -1);
845 /* check whether the indent type is OK */
846 switch (ft
->indent_type
)
848 case GEANY_INDENT_TYPE_TABS
:
849 case GEANY_INDENT_TYPE_SPACES
:
850 case GEANY_INDENT_TYPE_BOTH
:
855 g_warning("Invalid indent type %d in file type %s", ft
->indent_type
, ft
->name
);
856 ft
->indent_type
= -1;
862 static void load_settings(guint ft_id
, GKeyFile
*config
, GKeyFile
*configh
)
864 GeanyFiletype
*ft
= filetypes
[ft_id
];
867 /* default extension */
868 result
= utils_get_setting(string
, configh
, config
, "settings", "extension", NULL
);
871 SETPTR(filetypes
[ft_id
]->extension
, result
);
875 result
= utils_get_setting(string
, configh
, config
, "settings", "mime_type", "text/plain");
876 SETPTR(filetypes
[ft_id
]->mime_type
, result
);
878 /* read comment notes */
879 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_open", NULL
);
882 SETPTR(filetypes
[ft_id
]->comment_open
, result
);
885 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_close", NULL
);
888 SETPTR(filetypes
[ft_id
]->comment_close
, result
);
891 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_single", NULL
);
894 SETPTR(filetypes
[ft_id
]->comment_single
, result
);
896 /* import correctly filetypes that use old-style single comments */
897 else if (EMPTY(filetypes
[ft_id
]->comment_close
))
899 SETPTR(filetypes
[ft_id
]->comment_single
, filetypes
[ft_id
]->comment_open
);
900 filetypes
[ft_id
]->comment_open
= NULL
;
903 filetypes
[ft_id
]->comment_use_indent
= utils_get_setting(boolean
, configh
, config
,
904 "settings", "comment_use_indent", FALSE
);
906 /* read context action */
907 result
= utils_get_setting(string
, configh
, config
, "settings", "context_action_cmd", NULL
);
910 SETPTR(filetypes
[ft_id
]->context_action_cmd
, result
);
913 result
= utils_get_setting(string
, configh
, config
, "settings", "tag_parser", NULL
);
916 ft
->lang
= tm_source_file_get_named_lang(result
);
918 geany_debug("Cannot find tag parser '%s' for custom filetype '%s'.", result
, ft
->name
);
922 result
= utils_get_setting(string
, configh
, config
, "settings", "lexer_filetype", NULL
);
925 ft
->lexer_filetype
= filetypes_lookup_by_name(result
);
926 if (!ft
->lexer_filetype
)
927 geany_debug("Cannot find lexer filetype '%s' for custom filetype '%s'.", result
, ft
->name
);
931 ft
->priv
->symbol_list_sort_mode
= utils_get_setting(integer
, configh
, config
, "settings",
932 "symbol_list_sort_mode", SYMBOLS_SORT_BY_NAME
);
933 ft
->priv
->xml_indent_tags
= utils_get_setting(boolean
, configh
, config
, "settings",
934 "xml_indent_tags", FALSE
);
936 /* read indent settings */
937 load_indent_settings(ft
, config
, configh
);
939 /* read build settings */
940 build_load_menu(config
, GEANY_BCS_FT
, (gpointer
)ft
);
941 build_load_menu(configh
, GEANY_BCS_HOME_FT
, (gpointer
)ft
);
945 static void copy_keys(GKeyFile
*dest
, const gchar
*dest_group
,
946 GKeyFile
*src
, const gchar
*src_group
)
948 gchar
**keys
= g_key_file_get_keys(src
, src_group
, NULL
, NULL
);
951 foreach_strv(ptr
, keys
)
954 gchar
*value
= g_key_file_get_value(src
, src_group
, key
, NULL
);
956 g_key_file_set_value(dest
, dest_group
, key
, value
);
963 static gchar
*filetypes_get_filename(GeanyFiletype
*ft
, gboolean user
)
965 gchar
*ext
= filetypes_get_conf_extension(ft
);
966 gchar
*base_name
= g_strconcat("filetypes.", ext
, NULL
);
970 file_name
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, base_name
, NULL
);
972 file_name
= g_build_filename(app
->datadir
, base_name
, NULL
);
981 static void add_group_keys(GKeyFile
*kf
, const gchar
*group
, GeanyFiletype
*ft
)
984 gboolean loaded
= FALSE
;
987 files
[0] = filetypes_get_filename(ft
, FALSE
);
988 files
[1] = filetypes_get_filename(ft
, TRUE
);
990 for (i
= 0; i
< G_N_ELEMENTS(files
); i
++)
992 GKeyFile
*src
= g_key_file_new();
994 if (g_key_file_load_from_file(src
, files
[i
], G_KEY_FILE_NONE
, NULL
))
996 copy_keys(kf
, group
, src
, group
);
999 g_key_file_free(src
);
1003 geany_debug("Could not read config file %s for [%s=%s]!", files
[0], group
, ft
->name
);
1010 static void copy_ft_groups(GKeyFile
*kf
)
1012 gchar
**groups
= g_key_file_get_groups(kf
, NULL
);
1015 foreach_strv(ptr
, groups
)
1017 gchar
*group
= *ptr
;
1019 gchar
*name
= strchr(*ptr
, '=');
1022 if (!name
|| !name
[1]) /* no name or no parent name */
1025 old_group
= g_strdup(group
);
1027 /* terminate group at '=' */
1031 ft
= filetypes_lookup_by_name(name
);
1034 add_group_keys(kf
, group
, ft
);
1035 /* move old group keys (foo=bar) to proper group name (foo) */
1036 copy_keys(kf
, group
, kf
, old_group
);
1044 /* simple wrapper function to print file errors in DEBUG mode */
1045 static void load_system_keyfile(GKeyFile
*key_file
, const gchar
*file
, GKeyFileFlags flags
,
1048 GError
*error
= NULL
;
1049 gboolean done
= g_key_file_load_from_file(key_file
, file
, flags
, &error
);
1053 if (!done
&& !ft
->priv
->custom
)
1054 geany_debug("Failed to open %s (%s)", file
, error
->message
);
1056 g_error_free(error
);
1062 /* Load the configuration file for the associated filetype id.
1063 * This should only be called when the filetype is needed, to save loading
1064 * 20+ configuration files all at once. */
1065 void filetypes_load_config(guint ft_id
, gboolean reload
)
1067 GKeyFile
*config
, *config_home
;
1068 GeanyFiletypePrivate
*pft
;
1071 g_return_if_fail(ft_id
< filetypes_array
->len
);
1073 ft
= filetypes
[ft_id
];
1076 /* when reloading, proceed only if the settings were already loaded */
1077 if (G_UNLIKELY(reload
&& ! pft
->keyfile_loaded
))
1080 /* when not reloading, load the settings only once */
1081 if (G_LIKELY(! reload
&& pft
->keyfile_loaded
))
1083 pft
->keyfile_loaded
= TRUE
;
1085 config
= g_key_file_new();
1086 config_home
= g_key_file_new();
1088 /* highlighting uses GEANY_FILETYPES_NONE for common settings */
1091 f
= filetypes_get_filename(ft
, FALSE
);
1092 load_system_keyfile(config
, f
, G_KEY_FILE_KEEP_COMMENTS
, ft
);
1094 SETPTR(f
, filetypes_get_filename(ft
, TRUE
));
1095 g_key_file_load_from_file(config_home
, f
, G_KEY_FILE_KEEP_COMMENTS
, NULL
);
1098 /* Copy keys for any groups with [group=C] from system keyfile */
1099 copy_ft_groups(config
);
1100 copy_ft_groups(config_home
);
1102 load_settings(ft_id
, config
, config_home
);
1103 highlighting_init_styles(ft_id
, config
, config_home
);
1106 g_object_unref(ft
->icon
);
1107 ft
->icon
= ui_get_mime_icon(ft
->mime_type
);
1109 g_key_file_free(config
);
1110 g_key_file_free(config_home
);
1114 static gchar
*filetypes_get_conf_extension(const GeanyFiletype
*ft
)
1118 if (ft
->priv
->custom
)
1119 return g_strconcat(ft
->name
, ".conf", NULL
);
1121 /* Handle any special extensions different from lowercase filetype->name */
1124 case GEANY_FILETYPES_CPP
: result
= g_strdup("cpp"); break;
1125 case GEANY_FILETYPES_CS
: result
= g_strdup("cs"); break;
1126 case GEANY_FILETYPES_MAKE
: result
= g_strdup("makefile"); break;
1127 case GEANY_FILETYPES_NONE
: result
= g_strdup("common"); break;
1128 /* name is Matlab/Octave */
1129 case GEANY_FILETYPES_MATLAB
: result
= g_strdup("matlab"); break;
1130 /* name is Objective-C, and we don't want the hyphen */
1131 case GEANY_FILETYPES_OBJECTIVEC
: result
= g_strdup("objectivec"); break;
1133 result
= g_ascii_strdown(ft
->name
, -1);
1140 void filetypes_save_commands(GeanyFiletype
*ft
)
1142 GKeyFile
*config_home
;
1143 gchar
*fname
, *data
;
1145 fname
= filetypes_get_filename(ft
, TRUE
);
1146 config_home
= g_key_file_new();
1147 g_key_file_load_from_file(config_home
, fname
, G_KEY_FILE_KEEP_COMMENTS
, NULL
);
1148 build_save_menu(config_home
, ft
, GEANY_BCS_HOME_FT
);
1149 data
= g_key_file_to_data(config_home
, NULL
, NULL
);
1150 utils_write_file(fname
, data
);
1152 g_key_file_free(config_home
);
1157 /* create one file filter which has each file pattern of each filetype */
1158 GtkFileFilter
*filetypes_create_file_filter_all_source(void)
1160 GtkFileFilter
*new_filter
;
1163 new_filter
= gtk_file_filter_new();
1164 gtk_file_filter_set_name(new_filter
, _("All Source"));
1166 for (i
= 0; i
< filetypes_array
->len
; i
++)
1168 if (G_UNLIKELY(i
== GEANY_FILETYPES_NONE
))
1171 for (j
= 0; filetypes
[i
]->pattern
[j
]; j
++)
1173 gtk_file_filter_add_pattern(new_filter
, filetypes
[i
]->pattern
[j
]);
1180 GtkFileFilter
*filetypes_create_file_filter(const GeanyFiletype
*ft
)
1182 GtkFileFilter
*new_filter
;
1186 g_return_val_if_fail(ft
!= NULL
, NULL
);
1188 new_filter
= gtk_file_filter_new();
1189 title
= ft
->id
== GEANY_FILETYPES_NONE
? _("All files") : ft
->title
;
1190 gtk_file_filter_set_name(new_filter
, title
);
1192 for (i
= 0; ft
->pattern
[i
]; i
++)
1194 gtk_file_filter_add_pattern(new_filter
, ft
->pattern
[i
]);
1201 /* Indicates whether there is a tag parser for the filetype or not.
1202 * Only works for custom filetypes if the filetype settings have been loaded. */
1203 gboolean
filetype_has_tags(GeanyFiletype
*ft
)
1205 g_return_val_if_fail(ft
!= NULL
, FALSE
);
1207 return ft
->lang
>= 0;
1211 /** Finds a filetype pointer from its @a name field.
1212 * @param name Filetype name.
1213 * @return The filetype found, or @c NULL.
1217 GeanyFiletype
*filetypes_lookup_by_name(const gchar
*name
)
1221 g_return_val_if_fail(!EMPTY(name
), NULL
);
1223 ft
= g_hash_table_lookup(filetypes_hash
, name
);
1224 if (G_UNLIKELY(ft
== NULL
))
1225 geany_debug("Could not find filetype '%s'.", name
);
1230 static void compile_regex(GeanyFiletype
*ft
, gchar
*regstr
)
1232 GError
*error
= NULL
;
1233 GRegex
*regex
= g_regex_new(regstr
, 0, 0, &error
);
1237 ui_set_statusbar(TRUE
, _("Bad regex for filetype %s: %s"),
1238 filetypes_get_display_name(ft
), error
->message
);
1239 g_error_free(error
);
1241 if (ft
->priv
->error_regex
)
1242 g_regex_unref(ft
->priv
->error_regex
);
1243 ft
->priv
->error_regex
= regex
;
1247 gboolean
filetypes_parse_error_message(GeanyFiletype
*ft
, const gchar
*message
,
1248 gchar
**filename
, gint
*line
)
1254 gint i
, n_match_groups
;
1255 gchar
*first
, *second
;
1259 doc
= document_get_current();
1261 ft
= doc
->file_type
;
1263 tmp
= build_get_regex(build_info
.grp
, ft
, NULL
);
1271 if (G_UNLIKELY(EMPTY(regstr
)))
1274 if (!ft
->priv
->error_regex
|| regstr
!= ft
->priv
->last_error_pattern
)
1276 compile_regex(ft
, regstr
);
1277 ft
->priv
->last_error_pattern
= regstr
;
1279 if (!ft
->priv
->error_regex
)
1282 if (!g_regex_match(ft
->priv
->error_regex
, message
, 0, &minfo
))
1284 g_match_info_free(minfo
);
1288 n_match_groups
= g_match_info_get_match_count(minfo
);
1289 first
= second
= NULL
;
1291 for (i
= 1; i
< n_match_groups
; i
++)
1295 g_match_info_fetch_pos(minfo
, i
, &start_pos
, NULL
);
1296 if (start_pos
!= -1)
1299 first
= g_match_info_fetch(minfo
, i
);
1302 second
= g_match_info_fetch(minfo
, i
);
1313 l
= strtol(first
, &end
, 10);
1314 if (*end
== '\0') /* first is purely decimals */
1322 l
= strtol(second
, &end
, 10);
1339 g_match_info_free(minfo
);
1340 return *filename
!= NULL
;
1345 static void convert_filetype_extensions_to_lower_case(gchar
**patterns
, gsize len
)
1348 for (i
= 0; i
< len
; i
++)
1350 SETPTR(patterns
[i
], g_ascii_strdown(patterns
[i
], -1));
1356 static void read_extensions(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
1362 for (i
= 0; i
< filetypes_array
->len
; i
++)
1365 g_key_file_has_key(userconfig
, "Extensions", filetypes
[i
]->name
, NULL
);
1366 gchar
**list
= g_key_file_get_string_list(
1367 (userset
) ? userconfig
: sysconfig
, "Extensions", filetypes
[i
]->name
, &len
, NULL
);
1369 g_strfreev(filetypes
[i
]->pattern
);
1370 /* Note: we allow 'Foo=' to remove all patterns */
1372 list
= g_new0(gchar
*, 1);
1373 filetypes
[i
]->pattern
= list
;
1376 convert_filetype_extensions_to_lower_case(filetypes
[i
]->pattern
, len
);
1382 static void read_group(GKeyFile
*config
, const gchar
*group_name
, GeanyFiletypeGroupID group_id
)
1384 gchar
**names
= g_key_file_get_string_list(config
, "Groups", group_name
, NULL
, NULL
);
1387 foreach_strv(name
, names
)
1389 GeanyFiletype
*ft
= filetypes_lookup_by_name(*name
);
1393 ft
->group
= group_id
;
1394 if (ft
->priv
->custom
&&
1395 (group_id
== GEANY_FILETYPE_GROUP_COMPILED
|| group_id
== GEANY_FILETYPE_GROUP_SCRIPT
))
1397 SETPTR(ft
->title
, filetype_make_title(ft
->name
, TITLE_SOURCE_FILE
));
1401 geany_debug("Filetype '%s' not found for group '%s'!", *name
, group_name
);
1407 static void read_groups(GKeyFile
*config
)
1409 read_group(config
, "Programming", GEANY_FILETYPE_GROUP_COMPILED
);
1410 read_group(config
, "Script", GEANY_FILETYPE_GROUP_SCRIPT
);
1411 read_group(config
, "Markup", GEANY_FILETYPE_GROUP_MARKUP
);
1412 read_group(config
, "Misc", GEANY_FILETYPE_GROUP_MISC
);
1413 read_group(config
, "None", GEANY_FILETYPE_GROUP_NONE
);
1417 static void read_filetype_config(void)
1419 gchar
*sysconfigfile
= g_build_filename(app
->datadir
, "filetype_extensions.conf", NULL
);
1420 gchar
*userconfigfile
= g_build_filename(app
->configdir
, "filetype_extensions.conf", NULL
);
1421 GKeyFile
*sysconfig
= g_key_file_new();
1422 GKeyFile
*userconfig
= g_key_file_new();
1424 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
1425 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
1427 read_extensions(sysconfig
, userconfig
);
1428 read_groups(sysconfig
);
1429 read_groups(userconfig
);
1431 g_free(sysconfigfile
);
1432 g_free(userconfigfile
);
1433 g_key_file_free(sysconfig
);
1434 g_key_file_free(userconfig
);
1438 void filetypes_reload_extensions(void)
1442 read_filetype_config();
1444 /* Redetect filetype of any documents with none set */
1447 GeanyDocument
*doc
= documents
[i
];
1448 if (doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
1450 document_set_filetype(doc
, filetypes_detect_from_document(doc
));
1455 /** Accessor function for @ref GeanyData::filetypes_array items.
1456 * Example: @code ft = filetypes_index(GEANY_FILETYPES_C); @endcode
1457 * @param idx @c filetypes_array index.
1458 * @return The filetype, or @c NULL if @a idx is out of range.
1462 GeanyFiletype
*filetypes_index(gint idx
)
1464 return (idx
>= 0 && idx
< (gint
) filetypes_array
->len
) ? filetypes
[idx
] : NULL
;
1468 void filetypes_reload(void)
1471 GeanyDocument
*current_doc
;
1473 /* reload filetype configs */
1474 for (i
= 0; i
< filetypes_array
->len
; i
++)
1476 /* filetypes_load_config() will skip not loaded filetypes */
1477 filetypes_load_config(i
, TRUE
);
1480 current_doc
= document_get_current();
1484 /* update document styling */
1487 if (current_doc
!= documents
[i
])
1488 document_reload_config(documents
[i
]);
1490 /* process the current document at last */
1491 document_reload_config(current_doc
);
1495 /** Gets @c ft->name or a translation for filetype None.
1498 * @since Geany 0.20 */
1499 const gchar
*filetypes_get_display_name(GeanyFiletype
*ft
)
1501 return ft
->id
== GEANY_FILETYPES_NONE
? _("None") : ft
->name
;
1505 /* gets comment_open/comment_close/comment_single strings from the filetype
1506 * @param single_first: whether single comment is preferred if both available
1507 * returns true if at least comment_open is set, false otherwise */
1508 gboolean
filetype_get_comment_open_close(const GeanyFiletype
*ft
, gboolean single_first
,
1509 const gchar
**co
, const gchar
**cc
)
1511 g_return_val_if_fail(ft
!= NULL
, FALSE
);
1512 g_return_val_if_fail(co
!= NULL
, FALSE
);
1513 g_return_val_if_fail(cc
!= NULL
, FALSE
);
1517 *co
= ft
->comment_single
;
1522 *co
= ft
->comment_open
;
1523 *cc
= ft
->comment_close
;
1528 *co
= ft
->comment_open
;
1530 *cc
= ft
->comment_close
;
1533 *co
= ft
->comment_single
;