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.
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. */
33 #include "filetypes.h"
36 #include "callbacks.h" /* FIXME: for ignore_callback */
38 #include "filetypesprivate.h"
40 #include "geanyobject.h"
41 #include "highlighting.h"
42 #include "projectprivate.h"
43 #include "sciwrappers.h"
46 #include "tm_parser.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
);
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
);
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
];
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)
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 */
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
))
220 if (G_UNLIKELY(ft2
->id
== GEANY_FILETYPES_NONE
))
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. */
234 const GSList
*filetypes_get_sorted_by_name(void)
236 static GSList
*list
= NULL
;
238 g_return_val_if_fail(filetypes_by_title
, NULL
);
242 list
= g_slist_copy(filetypes_by_title
);
243 list
= g_slist_sort_with_data(list
, cmp_filetype
, GINT_TO_POINTER(TRUE
));
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");
271 g_return_if_fail(dot
);
275 if (g_hash_table_lookup(filetypes_hash
, fn
))
279 ft
->name
= g_strdup(fn
);
280 ft
->title
= filetype_make_title(ft
->name
, TITLE_FILE
);
281 ft
->priv
->custom
= TRUE
;
283 geany_debug("Added filetype %s (%d).", ft
->name
, ft
->id
);
287 static void init_custom_filetypes(const gchar
*path
)
290 const gchar
*filename
;
292 g_return_if_fail(path
);
294 dir
= g_dir_open(path
, 0, NULL
);
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
);
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
;
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
);
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 basename
= g_path_get_basename(doc
->real_path
);
364 if (g_str_has_prefix(basename
, "filetypes."))
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
))
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
);
382 document_reload_config(documents
[j
]);
394 static void setup_config_file_menus(void)
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
);
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
);
424 static void create_set_filetype_menu(gboolean config
)
426 GtkWidget
*group_menus
[GEANY_FILETYPE_GROUP_COUNT
] = {NULL
};
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
)
450 gchar
*filename
= filetypes_get_filename(ft
, TRUE
);
452 ui_add_config_file_menu_item(filename
, NULL
, GTK_CONTAINER(parent
));
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
))
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
))
487 static GeanyFiletype
*detect_filetype_conf_file(const gchar
*utf8_filename
)
491 gboolean found
= FALSE
;
494 /* use lower case basename */
495 lfn
= g_utf8_strdown(utf8_filename
, -1);
497 lfn
= g_strdup(utf8_filename
);
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
))
505 SETPTR(path
, g_build_filename(app
->datadir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.", NULL
));
506 if (g_str_has_prefix(lfn
, path
))
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
;
523 ft
= detect_filetype_conf_file(utf8_filename
);
527 /* to match against the basename of the file (because of Makefile*) */
528 base_filename
= g_path_get_basename(utf8_filename
);
530 /* use lower case basename */
531 SETPTR(base_filename
, g_utf8_strdown(base_filename
, -1));
534 for (guint i
= 0; i
< filetypes_array
->len
; i
++)
536 guint mlen
= match_basename(filetypes
[i
], base_filename
);
539 { // longest pattern match wins
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
550 ft
= filetypes
[GEANY_FILETYPES_NONE
];
552 g_free(base_filename
);
557 /* This detects the filetype of the file pointed by 'utf8_filename' and a list of filetype id's,
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
;
565 gboolean result
= FALSE
;
568 ft
= filetypes_detect_from_extension(utf8_filename
);
569 if (ft
== NULL
|| ft
->id
>= filetypes_array
->len
)
572 va_start(args
, first
);
579 if (ft
->id
== (guint
) test
)
584 test
= va_arg(args
, gint
);
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 {
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
;
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
];
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
];
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
,
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]);
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, ®ex_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);
699 ft
= filetypes_lookup_by_name(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
);
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
)
726 gchar
*lines
[GEANY_FILETYPE_SEARCH_LINES
+ 1];
729 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, filetypes
[GEANY_FILETYPES_NONE
]);
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
);
739 ft
= filetypes_detect_from_file_internal(doc
->file_name
, lines
);
740 for (i
= 0; i
< GEANY_FILETYPE_SEARCH_LINES
; ++i
)
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.
759 GeanyFiletype
*filetypes_detect_from_file(const gchar
*utf8_filename
)
764 gchar
*locale_name
= utils_get_locale_from_utf8(utf8_filename
);
766 f
= g_fopen(locale_name
, "r");
770 if (fgets(line
, sizeof(line
), f
) != NULL
)
775 return filetypes_detect_from_file_internal(utf8_filename
, lines
);
779 return filetypes_detect_from_extension(utf8_filename
);
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
);
790 ft
= filetypes
[GEANY_FILETYPES_NONE
];
792 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ft
->priv
->menu_item
), TRUE
);
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
))
803 document_set_filetype(doc
, (GeanyFiletype
*)user_data
);
807 static void create_radio_menu_item(GtkWidget
*menu
, GeanyFiletype
*ftype
)
809 static GSList
*group
= NULL
;
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
);
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
);
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
);
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
:
879 g_warning("Invalid indent type %d in file type %s", ft
->indent_type
, ft
->name
);
880 ft
->indent_type
= -1;
886 static void load_settings(guint ft_id
, GKeyFile
*config
, GKeyFile
*configh
)
888 GeanyFiletype
*ft
= filetypes
[ft_id
];
891 /* default extension */
892 result
= utils_get_setting(string
, configh
, config
, "settings", "extension", NULL
);
895 SETPTR(filetypes
[ft_id
]->extension
, result
);
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
);
906 SETPTR(filetypes
[ft_id
]->comment_open
, result
);
909 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_close", NULL
);
912 SETPTR(filetypes
[ft_id
]->comment_close
, result
);
915 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_single", 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
);
934 SETPTR(filetypes
[ft_id
]->context_action_cmd
, result
);
937 result
= utils_get_setting(string
, configh
, config
, "settings", "tag_parser", 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
);
946 result
= utils_get_setting(string
, configh
, config
, "settings", "lexer_filetype", 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
);
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
);
975 foreach_strv(ptr
, keys
)
978 gchar
*value
= g_key_file_get_value(src
, src_group
, key
, NULL
);
980 g_key_file_set_value(dest
, dest_group
, key
, value
);
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
);
994 file_name
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, base_name
, NULL
);
996 file_name
= g_build_filename(app
->datadir
, GEANY_FILEDEFS_SUBDIR
, base_name
, NULL
);
1005 static void add_group_keys(GKeyFile
*kf
, const gchar
*group
, GeanyFiletype
*ft
)
1008 gboolean loaded
= FALSE
;
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
);
1023 g_key_file_free(src
);
1027 geany_debug("Could not read config file %s for [%s=%s]!", files
[0], group
, ft
->name
);
1034 static void copy_ft_groups(GKeyFile
*kf
)
1036 gchar
**groups
= g_key_file_get_groups(kf
, NULL
);
1039 foreach_strv(ptr
, groups
)
1041 gchar
*group
= *ptr
;
1043 gchar
*name
= strchr(*ptr
, '=');
1046 if (!name
|| !name
[1]) /* no name or no parent name */
1049 old_group
= g_strdup(group
);
1051 /* terminate group at '=' */
1055 ft
= filetypes_lookup_by_name(name
);
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
);
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
,
1072 GError
*error
= NULL
;
1073 gboolean done
= g_key_file_load_from_file(key_file
, file
, flags
, &error
);
1077 if (!done
&& !ft
->priv
->custom
)
1078 geany_debug("Failed to open %s (%s)", file
, error
->message
);
1080 g_error_free(error
);
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
;
1095 g_return_if_fail(ft_id
< filetypes_array
->len
);
1097 ft
= filetypes
[ft_id
];
1100 /* when reloading, proceed only if the settings were already loaded */
1101 if (G_UNLIKELY(reload
&& ! pft
->keyfile_loaded
))
1104 /* when not reloading, load the settings only once */
1105 if (G_LIKELY(! reload
&& pft
->keyfile_loaded
))
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 */
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
);
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
);
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
)
1142 if (ft
->priv
->custom
)
1143 return g_strconcat(ft
->name
, ".conf", NULL
);
1145 /* Handle any special extensions different from lowercase filetype->name */
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;
1157 result
= g_ascii_strdown(ft
->name
, -1);
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
);
1176 g_key_file_free(config_home
);
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
;
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
))
1195 for (j
= 0; filetypes
[i
]->pattern
[j
]; j
++)
1197 gtk_file_filter_add_pattern(new_filter
, filetypes
[i
]->pattern
[j
]);
1204 GtkFileFilter
*filetypes_create_file_filter(const GeanyFiletype
*ft
)
1206 GtkFileFilter
*new_filter
;
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
]);
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.
1242 GeanyFiletype
*filetypes_lookup_by_name(const gchar
*name
)
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
);
1255 static void compile_regex(GeanyFiletype
*ft
, gchar
*regstr
)
1257 GError
*error
= NULL
;
1258 GRegex
*regex
= g_regex_new(regstr
, 0, 0, &error
);
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
)
1279 gint i
, n_match_groups
;
1280 gchar
*first
, *second
;
1284 doc
= document_get_current();
1286 ft
= doc
->file_type
;
1288 tmp
= build_get_regex(build_info
.grp
, ft
, NULL
);
1296 if (G_UNLIKELY(EMPTY(regstr
)))
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
)
1307 if (!g_regex_match(ft
->priv
->error_regex
, message
, 0, &minfo
))
1309 g_match_info_free(minfo
);
1313 n_match_groups
= g_match_info_get_match_count(minfo
);
1314 first
= second
= NULL
;
1316 for (i
= 1; i
< n_match_groups
; i
++)
1320 g_match_info_fetch_pos(minfo
, i
, &start_pos
, NULL
);
1321 if (start_pos
!= -1)
1324 first
= g_match_info_fetch(minfo
, i
);
1327 second
= g_match_info_fetch(minfo
, i
);
1338 l
= strtol(first
, &end
, 10);
1339 if (*end
== '\0') /* first is purely decimals */
1347 l
= strtol(second
, &end
, 10);
1364 g_match_info_free(minfo
);
1365 return *filename
!= NULL
;
1370 static void convert_filetype_extensions_to_lower_case(gchar
**patterns
, gsize len
)
1373 for (i
= 0; i
< len
; i
++)
1375 SETPTR(patterns
[i
], g_ascii_strdown(patterns
[i
], -1));
1381 static void read_extensions(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
1387 for (i
= 0; i
< filetypes_array
->len
; i
++)
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 */
1398 list
= g_new0(gchar
*, 1);
1399 filetypes
[i
]->pattern
= list
;
1402 convert_filetype_extensions_to_lower_case(filetypes
[i
]->pattern
, len
);
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
);
1413 foreach_strv(name
, names
)
1415 GeanyFiletype
*ft
= filetypes_lookup_by_name(*name
);
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
));
1427 geany_debug("Filetype '%s' not found for group '%s'!", *name
, group_name
);
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)
1468 read_filetype_config();
1470 /* Redetect filetype of any documents with none set */
1473 GeanyDocument
*doc
= documents
[i
];
1474 if (doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
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.
1489 GeanyFiletype
*filetypes_index(gint idx
)
1491 return (idx
>= 0 && idx
< (gint
) filetypes_array
->len
) ? filetypes
[idx
] : NULL
;
1495 void filetypes_reload(void)
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();
1511 /* update document styling */
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.
1525 * @since Geany 0.20 */
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
);
1545 *co
= ft
->comment_single
;
1550 *co
= ft
->comment_open
;
1551 *cc
= ft
->comment_close
;
1556 *co
= ft
->comment_open
;
1558 *cc
= ft
->comment_close
;
1561 *co
= ft
->comment_single
;
1569 static void *copy_(void *src
) { return src
; }
1570 static void free_(void *doc
) { }
1573 * Gets the GType of GeanyFiletype
1575 * @return the GeanyFiletype type */
1577 GType
filetype_get_type (void);
1579 G_DEFINE_BOXED_TYPE(GeanyFiletype
, filetype
, copy_
, free_
);