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
, FORTRAN
, "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
, TCLOO
, "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
, LISP
, "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
, ADA
, "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
, BATCH
, "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
);
191 FT_INIT( AU3
, AUTOIT
, "AutoIt", NULL
, SCRIPT
, SCRIPT
);
195 /* initialize fields. */
196 static GeanyFiletype
*filetype_new(void)
198 GeanyFiletype
*ft
= g_new0(GeanyFiletype
, 1);
200 ft
->group
= GEANY_FILETYPE_GROUP_NONE
;
201 ft
->lang
= TM_PARSER_NONE
; /* assume no tagmanager parser */
202 /* pattern must not be null */
203 ft
->pattern
= g_new0(gchar
*, 1);
204 ft
->indent_width
= -1;
205 ft
->indent_type
= -1;
207 ft
->priv
= g_new0(GeanyFiletypePrivate
, 1);
208 ft
->priv
->project_list_entry
= -1; /* no entry */
214 static gint
cmp_filetype(gconstpointer pft1
, gconstpointer pft2
, gpointer data
)
216 gboolean by_name
= GPOINTER_TO_INT(data
);
217 const GeanyFiletype
*ft1
= pft1
, *ft2
= pft2
;
219 if (G_UNLIKELY(ft1
->id
== GEANY_FILETYPES_NONE
))
221 if (G_UNLIKELY(ft2
->id
== GEANY_FILETYPES_NONE
))
225 utils_str_casecmp(ft1
->name
, ft2
->name
) :
226 utils_str_casecmp(ft1
->title
, ft2
->title
);
230 /** Gets a list of filetype pointers sorted by name.
231 * The list does not change on subsequent calls.
232 * @return @elementtype{GeanyFiletype} @transfer{none} The list - do not free.
233 * @see filetypes_by_title. */
235 const GSList
*filetypes_get_sorted_by_name(void)
237 static GSList
*list
= NULL
;
239 g_return_val_if_fail(filetypes_by_title
, NULL
);
243 list
= g_slist_copy(filetypes_by_title
);
244 list
= g_slist_sort_with_data(list
, cmp_filetype
, GINT_TO_POINTER(TRUE
));
250 /* Add a filetype pointer to the lists of available filetypes,
251 * and set the filetype::id field. */
252 static void filetype_add(GeanyFiletype
*ft
)
254 g_return_if_fail(ft
);
255 g_return_if_fail(ft
->name
);
257 ft
->id
= filetypes_array
->len
; /* len will be the index for filetype_array */
258 g_ptr_array_add(filetypes_array
, ft
);
259 g_hash_table_insert(filetypes_hash
, ft
->name
, ft
);
261 /* list will be sorted later */
262 filetypes_by_title
= g_slist_prepend(filetypes_by_title
, ft
);
266 static void add_custom_filetype(const gchar
*filename
)
268 gchar
*fn
= utils_strdupa(strstr(filename
, ".") + 1);
269 gchar
*dot
= g_strrstr(fn
, ".conf");
272 g_return_if_fail(dot
);
276 if (g_hash_table_lookup(filetypes_hash
, fn
))
280 ft
->name
= g_strdup(fn
);
281 ft
->title
= filetype_make_title(ft
->name
, TITLE_FILE
);
282 ft
->priv
->custom
= TRUE
;
284 geany_debug("Added filetype %s (%d).", ft
->name
, ft
->id
);
288 static void init_custom_filetypes(const gchar
*path
)
291 const gchar
*filename
;
293 g_return_if_fail(path
);
295 dir
= g_dir_open(path
, 0, NULL
);
299 foreach_dir(filename
, dir
)
301 const gchar prefix
[] = "filetypes.";
303 if (g_str_has_prefix(filename
, prefix
) &&
304 g_str_has_suffix(filename
+ strlen(prefix
), ".conf"))
306 add_custom_filetype(filename
);
313 /* Create the filetypes array and fill it with the known filetypes.
314 * Warning: GTK isn't necessarily initialized yet. */
315 void filetypes_init_types(void)
317 GeanyFiletypeID ft_id
;
320 g_return_if_fail(filetypes_array
== NULL
);
321 g_return_if_fail(filetypes_hash
== NULL
);
323 filetypes_array
= g_ptr_array_sized_new(GEANY_MAX_BUILT_IN_FILETYPES
);
324 filetypes_hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
326 /* Create built-in filetypes */
327 for (ft_id
= 0; ft_id
< GEANY_MAX_BUILT_IN_FILETYPES
; ft_id
++)
329 filetypes
[ft_id
] = filetype_new();
331 init_builtin_filetypes();
333 /* Add built-in filetypes to the hash now the name fields are set */
334 for (ft_id
= 0; ft_id
< GEANY_MAX_BUILT_IN_FILETYPES
; ft_id
++)
336 filetype_add(filetypes
[ft_id
]);
338 f
= g_build_filename(app
->datadir
, GEANY_FILEDEFS_SUBDIR
, NULL
);
339 init_custom_filetypes(f
);
341 f
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, NULL
);
342 init_custom_filetypes(f
);
345 /* sort last instead of on insertion to prevent exponential time */
346 filetypes_by_title
= g_slist_sort_with_data(filetypes_by_title
,
347 cmp_filetype
, GINT_TO_POINTER(FALSE
));
349 read_filetype_config();
353 static void on_document_save(G_GNUC_UNUSED GObject
*object
, GeanyDocument
*doc
)
357 g_return_if_fail(!EMPTY(doc
->real_path
));
359 f
= g_build_filename(app
->configdir
, "filetype_extensions.conf", NULL
);
360 if (utils_str_equal(doc
->real_path
, f
))
361 filetypes_reload_extensions();
364 basename
= g_path_get_basename(doc
->real_path
);
365 if (g_str_has_prefix(basename
, "filetypes."))
369 for (i
= 0; i
< filetypes_array
->len
; i
++)
371 GeanyFiletype
*ft
= filetypes
[i
];
373 f
= filetypes_get_filename(ft
, TRUE
);
374 if (utils_str_equal(doc
->real_path
, f
))
378 /* Note: we don't reload other filetypes, even though the named styles may have changed.
379 * The user can do this manually with 'Tools->Reload Configuration' */
380 filetypes_load_config(i
, TRUE
);
383 document_reload_config(documents
[j
]);
395 static void setup_config_file_menus(void)
399 f
= g_build_filename(app
->configdir
, "filetype_extensions.conf", NULL
);
400 ui_add_config_file_menu_item(f
, NULL
, NULL
);
401 SETPTR(f
, g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.common", NULL
));
402 ui_add_config_file_menu_item(f
, NULL
, NULL
);
405 create_set_filetype_menu(TRUE
);
407 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
411 static GtkWidget
*create_sub_menu(GtkWidget
*parent
, const gchar
*title
)
413 GtkWidget
*menu
, *item
;
415 menu
= gtk_menu_new();
416 item
= gtk_menu_item_new_with_mnemonic((title
));
417 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), menu
);
418 gtk_container_add(GTK_CONTAINER(parent
), item
);
419 gtk_widget_show(item
);
425 static void create_set_filetype_menu(gboolean config
)
427 GtkWidget
*group_menus
[GEANY_FILETYPE_GROUP_COUNT
] = {NULL
};
431 menu
= config
? ui_widgets
.config_files_filetype_menu
:
432 ui_lookup_widget(main_widgets
.window
, "set_filetype1_menu");
434 group_menus
[GEANY_FILETYPE_GROUP_COMPILED
] = create_sub_menu(menu
, _("_Programming Languages"));
435 group_menus
[GEANY_FILETYPE_GROUP_SCRIPT
] = create_sub_menu(menu
, _("_Scripting Languages"));
436 group_menus
[GEANY_FILETYPE_GROUP_MARKUP
] = create_sub_menu(menu
, _("_Markup Languages"));
437 group_menus
[GEANY_FILETYPE_GROUP_MISC
] = create_sub_menu(menu
, _("M_iscellaneous"));
439 /* Append all filetypes to the menu */
440 foreach_slist(node
, filetypes_by_title
)
442 GeanyFiletype
*ft
= node
->data
;
443 GtkWidget
*parent
= (ft
->group
!= GEANY_FILETYPE_GROUP_NONE
) ? group_menus
[ft
->group
] : menu
;
445 /* we already have filetypes.common config entry */
446 if (config
&& ft
->id
== GEANY_FILETYPES_NONE
)
451 gchar
*filename
= filetypes_get_filename(ft
, TRUE
);
453 ui_add_config_file_menu_item(filename
, NULL
, GTK_CONTAINER(parent
));
457 create_radio_menu_item(parent
, ft
);
462 void filetypes_init(void)
464 filetypes_init_types();
465 create_set_filetype_menu(FALSE
);
466 setup_config_file_menus();
470 static guint
match_basename(const GeanyFiletype
*ft
, const gchar
*base_filename
)
472 if (G_UNLIKELY(ft
->id
== GEANY_FILETYPES_NONE
))
475 for (guint j
= 0; ft
->pattern
[j
] != NULL
; j
++)
477 gchar
*pat
= ft
->pattern
[j
];
479 if (g_pattern_match_simple(pat
, base_filename
))
488 static GeanyFiletype
*detect_filetype_conf_file(const gchar
*utf8_filename
)
492 gboolean found
= FALSE
;
495 /* use lower case basename */
496 lfn
= g_utf8_strdown(utf8_filename
, -1);
498 lfn
= g_strdup(utf8_filename
);
500 SETPTR(lfn
, utils_get_locale_from_utf8(lfn
));
502 path
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.", NULL
);
503 if (g_str_has_prefix(lfn
, path
))
506 SETPTR(path
, g_build_filename(app
->datadir
, GEANY_FILEDEFS_SUBDIR
, "filetypes.", NULL
));
507 if (g_str_has_prefix(lfn
, path
))
512 return found
? filetypes
[GEANY_FILETYPES_CONF
] : NULL
;
516 /* Detect filetype only based on the filename extension.
517 * utf8_filename can include the full path.
518 * Returns: non-NULL */
519 GeanyFiletype
*filetypes_detect_from_extension(const gchar
*utf8_filename
)
521 gchar
*base_filename
;
525 ft
= detect_filetype_conf_file(utf8_filename
);
529 /* to match against the basename of the file (because of Makefile*) */
530 base_filename
= g_path_get_basename(utf8_filename
);
532 /* use lower case basename */
533 SETPTR(base_filename
, g_utf8_strdown(base_filename
, -1));
536 for (guint i
= 0; i
< filetypes_array
->len
; i
++)
538 guint mlen
= match_basename(filetypes
[i
], base_filename
);
541 { // longest pattern match wins
545 else if (mlen
== plen
&& ft
&& !ft
->priv
->user_extensions
&&
546 filetypes
[i
]->priv
->user_extensions
)
547 { // user config overrides system if pattern len same
552 ft
= filetypes
[GEANY_FILETYPES_NONE
];
554 g_free(base_filename
);
559 /* This detects the filetype of the file pointed by 'utf8_filename' and a list of filetype id's,
561 * The detected filetype of the file is checked against every id in the passed list and if
562 * there is a match, TRUE is returned. */
563 static gboolean
shebang_find_and_match_filetype(const gchar
*utf8_filename
, gint first
, ...)
565 GeanyFiletype
*ft
= NULL
;
567 gboolean result
= FALSE
;
570 ft
= filetypes_detect_from_extension(utf8_filename
);
571 if (ft
== NULL
|| ft
->id
>= filetypes_array
->len
)
574 va_start(args
, first
);
581 if (ft
->id
== (guint
) test
)
586 test
= va_arg(args
, gint
);
594 static GeanyFiletype
*find_shebang(const gchar
*utf8_filename
, const gchar
*line
)
596 GeanyFiletype
*ft
= NULL
;
598 if (strlen(line
) > 2 && line
[0] == '#' && line
[1] == '!')
600 static const struct {
602 GeanyFiletypeID filetype
;
603 } intepreter_map
[] = {
604 { "sh", GEANY_FILETYPES_SH
},
605 { "bash", GEANY_FILETYPES_SH
},
606 { "dash", GEANY_FILETYPES_SH
},
607 { "perl", GEANY_FILETYPES_PERL
},
608 { "python", GEANY_FILETYPES_PYTHON
},
609 { "php", GEANY_FILETYPES_PHP
},
610 { "ruby", GEANY_FILETYPES_RUBY
},
611 { "tcl", GEANY_FILETYPES_TCL
},
612 { "make", GEANY_FILETYPES_MAKE
},
613 { "zsh", GEANY_FILETYPES_SH
},
614 { "ksh", GEANY_FILETYPES_SH
},
615 { "mksh", GEANY_FILETYPES_SH
},
616 { "csh", GEANY_FILETYPES_SH
},
617 { "tcsh", GEANY_FILETYPES_SH
},
618 { "ash", GEANY_FILETYPES_SH
},
619 { "dmd", GEANY_FILETYPES_D
},
620 { "wish", GEANY_FILETYPES_TCL
},
621 { "node", GEANY_FILETYPES_JS
},
622 { "rust", GEANY_FILETYPES_RUST
}
624 gchar
*tmp
= g_path_get_basename(line
+ 2);
625 gchar
*basename_interpreter
= tmp
;
628 if (g_str_has_prefix(tmp
, "env "))
629 { /* skip "env" and read the following interpreter */
630 basename_interpreter
+= 4;
633 for (i
= 0; ! ft
&& i
< G_N_ELEMENTS(intepreter_map
); i
++)
635 if (g_str_has_prefix(basename_interpreter
, intepreter_map
[i
].name
))
636 ft
= filetypes
[intepreter_map
[i
].filetype
];
640 /* detect HTML files */
641 if (g_str_has_prefix(line
, "<!DOCTYPE html") || g_str_has_prefix(line
, "<html"))
643 /* PHP, Perl and Python files might also start with <html, so detect them based on filename
644 * extension and use the detected filetype, else assume HTML */
645 if (! shebang_find_and_match_filetype(utf8_filename
,
646 GEANY_FILETYPES_PERL
, GEANY_FILETYPES_PHP
, GEANY_FILETYPES_PYTHON
, -1))
648 ft
= filetypes
[GEANY_FILETYPES_HTML
];
651 /* detect XML files */
652 else if (utf8_filename
&& g_str_has_prefix(line
, "<?xml"))
654 /* HTML and DocBook files might also start with <?xml, so detect them based on filename
655 * extension and use the detected filetype, else assume XML */
656 if (! shebang_find_and_match_filetype(utf8_filename
,
657 GEANY_FILETYPES_HTML
, GEANY_FILETYPES_DOCBOOK
,
658 /* Perl, Python and PHP only to be safe */
659 GEANY_FILETYPES_PERL
, GEANY_FILETYPES_PHP
, GEANY_FILETYPES_PYTHON
, -1))
661 ft
= filetypes
[GEANY_FILETYPES_XML
];
664 else if (g_str_has_prefix(line
, "<?php"))
666 ft
= filetypes
[GEANY_FILETYPES_PHP
];
672 /* Detect the filetype checking for a shebang, then filename extension.
673 * @lines: an strv of the lines to scan (must containing at least one line) */
674 static GeanyFiletype
*filetypes_detect_from_file_internal(const gchar
*utf8_filename
,
681 GError
*regex_error
= NULL
;
683 /* try to find a shebang and if found use it prior to the filename extension
684 * also checks for <?xml */
685 ft
= find_shebang(utf8_filename
, lines
[0]);
689 /* try to extract the filetype using a regex capture */
690 ft_regex
= g_regex_new(file_prefs
.extract_filetype_regex
,
691 G_REGEX_RAW
| G_REGEX_MULTILINE
, 0, ®ex_error
);
692 if (ft_regex
!= NULL
)
694 for (i
= 0; ft
== NULL
&& lines
[i
] != NULL
; i
++)
696 if (g_regex_match(ft_regex
, lines
[i
], 0, &match
))
698 gchar
*capture
= g_match_info_fetch(match
, 1);
701 ft
= filetypes_lookup_by_name(capture
);
705 g_match_info_free(match
);
707 g_regex_unref(ft_regex
);
709 else if (regex_error
!= NULL
)
711 geany_debug("Filetype extract regex ignored: %s", regex_error
->message
);
712 g_error_free(regex_error
);
717 if (utf8_filename
== NULL
)
718 return filetypes
[GEANY_FILETYPES_NONE
];
720 return filetypes_detect_from_extension(utf8_filename
);
724 /* Detect the filetype for the document, checking for a shebang, then filename extension. */
725 GeanyFiletype
*filetypes_detect_from_document(GeanyDocument
*doc
)
728 gchar
*lines
[GEANY_FILETYPE_SEARCH_LINES
+ 1];
731 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, filetypes
[GEANY_FILETYPES_NONE
]);
734 return filetypes
[GEANY_FILETYPES_NONE
];
736 for (i
= 0; i
< GEANY_FILETYPE_SEARCH_LINES
; ++i
)
738 lines
[i
] = sci_get_line(doc
->editor
->sci
, i
);
741 ft
= filetypes_detect_from_file_internal(doc
->file_name
, lines
);
742 for (i
= 0; i
< GEANY_FILETYPE_SEARCH_LINES
; ++i
)
751 /* Currently only used by external plugins (e.g. geanyprj). */
753 * Detects filetype based on a shebang line in the file or the filename extension.
755 * @param utf8_filename The filename in UTF-8 encoding.
757 * @return @transfer{none} The detected filetype for @a utf8_filename or
758 * @c filetypes[GEANY_FILETYPES_NONE] if it could not be detected.
761 GeanyFiletype
*filetypes_detect_from_file(const gchar
*utf8_filename
)
766 gchar
*locale_name
= utils_get_locale_from_utf8(utf8_filename
);
768 f
= g_fopen(locale_name
, "r");
772 if (fgets(line
, sizeof(line
), f
) != NULL
)
777 return filetypes_detect_from_file_internal(utf8_filename
, lines
);
781 return filetypes_detect_from_extension(utf8_filename
);
786 void filetypes_select_radio_item(const GeanyFiletype
*ft
)
788 /* ignore_callback has to be set by the caller */
789 g_return_if_fail(ignore_callback
);
792 ft
= filetypes
[GEANY_FILETYPES_NONE
];
794 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ft
->priv
->menu_item
), TRUE
);
799 on_filetype_change(GtkCheckMenuItem
*menuitem
, gpointer user_data
)
801 GeanyDocument
*doc
= document_get_current();
802 if (ignore_callback
|| doc
== NULL
|| ! gtk_check_menu_item_get_active(menuitem
))
805 document_set_filetype(doc
, (GeanyFiletype
*)user_data
);
809 static void create_radio_menu_item(GtkWidget
*menu
, GeanyFiletype
*ftype
)
811 static GSList
*group
= NULL
;
814 tmp
= gtk_radio_menu_item_new_with_label(group
, ftype
->title
);
815 group
= gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(tmp
));
816 ftype
->priv
->menu_item
= tmp
;
817 gtk_widget_show(tmp
);
818 gtk_container_add(GTK_CONTAINER(menu
), tmp
);
819 g_signal_connect(tmp
, "activate", G_CALLBACK(on_filetype_change
), (gpointer
) ftype
);
823 static void filetype_free(gpointer data
, G_GNUC_UNUSED gpointer user_data
)
825 GeanyFiletype
*ft
= data
;
827 g_return_if_fail(ft
!= NULL
);
831 g_free(ft
->extension
);
832 g_free(ft
->mime_type
);
833 g_free(ft
->comment_open
);
834 g_free(ft
->comment_close
);
835 g_free(ft
->comment_single
);
836 g_free(ft
->context_action_cmd
);
837 g_free(ft
->priv
->filecmds
);
838 g_free(ft
->priv
->ftdefcmds
);
839 g_free(ft
->priv
->execcmds
);
840 g_free(ft
->error_regex_string
);
842 g_object_unref(ft
->icon
);
843 g_strfreev(ft
->pattern
);
845 if (ft
->priv
->error_regex
)
846 g_regex_unref(ft
->priv
->error_regex
);
847 g_slist_foreach(ft
->priv
->tag_files
, (GFunc
) g_free
, NULL
);
848 g_slist_free(ft
->priv
->tag_files
);
855 /* frees the array and all related pointers */
856 void filetypes_free_types(void)
858 g_return_if_fail(filetypes_array
!= NULL
);
859 g_return_if_fail(filetypes_hash
!= NULL
);
861 g_ptr_array_foreach(filetypes_array
, filetype_free
, NULL
);
862 g_ptr_array_free(filetypes_array
, TRUE
);
863 g_hash_table_destroy(filetypes_hash
);
867 static void load_indent_settings(GeanyFiletype
*ft
, GKeyFile
*config
, GKeyFile
*configh
)
869 ft
->indent_width
= utils_get_setting(integer
, configh
, config
, "indentation", "width", -1);
870 ft
->indent_type
= utils_get_setting(integer
, configh
, config
, "indentation", "type", -1);
871 /* check whether the indent type is OK */
872 switch (ft
->indent_type
)
874 case GEANY_INDENT_TYPE_TABS
:
875 case GEANY_INDENT_TYPE_SPACES
:
876 case GEANY_INDENT_TYPE_BOTH
:
881 g_warning("Invalid indent type %d in file type %s", ft
->indent_type
, ft
->name
);
882 ft
->indent_type
= -1;
888 static void load_settings(guint ft_id
, GKeyFile
*config
, GKeyFile
*configh
)
890 GeanyFiletype
*ft
= filetypes
[ft_id
];
893 /* default extension */
894 result
= utils_get_setting(string
, configh
, config
, "settings", "extension", NULL
);
897 SETPTR(filetypes
[ft_id
]->extension
, result
);
901 result
= utils_get_setting(string
, configh
, config
, "settings", "mime_type", "text/plain");
902 SETPTR(filetypes
[ft_id
]->mime_type
, result
);
904 /* read comment notes */
905 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_open", NULL
);
908 SETPTR(filetypes
[ft_id
]->comment_open
, result
);
911 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_close", NULL
);
914 SETPTR(filetypes
[ft_id
]->comment_close
, result
);
917 result
= utils_get_setting(string
, configh
, config
, "settings", "comment_single", NULL
);
920 SETPTR(filetypes
[ft_id
]->comment_single
, result
);
922 /* import correctly filetypes that use old-style single comments */
923 else if (EMPTY(filetypes
[ft_id
]->comment_close
))
925 SETPTR(filetypes
[ft_id
]->comment_single
, filetypes
[ft_id
]->comment_open
);
926 filetypes
[ft_id
]->comment_open
= NULL
;
929 filetypes
[ft_id
]->comment_use_indent
= utils_get_setting(boolean
, configh
, config
,
930 "settings", "comment_use_indent", FALSE
);
932 /* read context action */
933 result
= utils_get_setting(string
, configh
, config
, "settings", "context_action_cmd", NULL
);
936 SETPTR(filetypes
[ft_id
]->context_action_cmd
, result
);
939 result
= utils_get_setting(string
, configh
, config
, "settings", "tag_parser", NULL
);
942 ft
->lang
= tm_source_file_get_named_lang(result
);
943 if (ft
->lang
== TM_PARSER_NONE
)
944 geany_debug("Cannot find tags parser '%s' for custom filetype '%s'.", result
, ft
->name
);
948 result
= utils_get_setting(string
, configh
, config
, "settings", "lexer_filetype", NULL
);
951 ft
->lexer_filetype
= filetypes_lookup_by_name(result
);
952 if (!ft
->lexer_filetype
)
953 geany_debug("Cannot find lexer filetype '%s' for custom filetype '%s'.", result
, ft
->name
);
957 ft
->priv
->symbol_list_sort_mode
= utils_get_setting(integer
, configh
, config
, "settings",
958 "symbol_list_sort_mode", SYMBOLS_SORT_USE_PREVIOUS
);
959 ft
->priv
->xml_indent_tags
= utils_get_setting(boolean
, configh
, config
, "settings",
960 "xml_indent_tags", FALSE
);
962 /* read indent settings */
963 load_indent_settings(ft
, config
, configh
);
965 /* read build settings */
966 build_load_menu(config
, GEANY_BCS_FT
, (gpointer
)ft
);
967 build_load_menu(configh
, GEANY_BCS_HOME_FT
, (gpointer
)ft
);
971 static void copy_keys(GKeyFile
*dest
, const gchar
*dest_group
,
972 GKeyFile
*src
, const gchar
*src_group
)
974 gchar
**keys
= g_key_file_get_keys(src
, src_group
, NULL
, NULL
);
977 foreach_strv(ptr
, keys
)
980 gchar
*value
= g_key_file_get_value(src
, src_group
, key
, NULL
);
982 g_key_file_set_value(dest
, dest_group
, key
, value
);
989 static gchar
*filetypes_get_filename(GeanyFiletype
*ft
, gboolean user
)
991 gchar
*ext
= filetypes_get_conf_extension(ft
);
992 gchar
*base_name
= g_strconcat("filetypes.", ext
, NULL
);
996 file_name
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, base_name
, NULL
);
998 file_name
= g_build_filename(app
->datadir
, GEANY_FILEDEFS_SUBDIR
, base_name
, NULL
);
1007 static void add_group_keys(GKeyFile
*kf
, const gchar
*group
, GeanyFiletype
*ft
)
1010 gboolean loaded
= FALSE
;
1013 files
[0] = filetypes_get_filename(ft
, FALSE
);
1014 files
[1] = filetypes_get_filename(ft
, TRUE
);
1016 for (i
= 0; i
< G_N_ELEMENTS(files
); i
++)
1018 GKeyFile
*src
= g_key_file_new();
1020 if (g_key_file_load_from_file(src
, files
[i
], G_KEY_FILE_NONE
, NULL
))
1022 copy_keys(kf
, group
, src
, group
);
1025 g_key_file_free(src
);
1029 geany_debug("Could not read config file %s for [%s=%s]!", files
[0], group
, ft
->name
);
1036 static void copy_ft_groups(GKeyFile
*kf
)
1038 gchar
**groups
= g_key_file_get_groups(kf
, NULL
);
1041 foreach_strv(ptr
, groups
)
1043 gchar
*group
= *ptr
;
1045 gchar
*name
= strchr(*ptr
, '=');
1048 if (!name
|| !name
[1]) /* no name or no parent name */
1051 old_group
= g_strdup(group
);
1053 /* terminate group at '=' */
1057 ft
= filetypes_lookup_by_name(name
);
1060 add_group_keys(kf
, group
, ft
);
1061 /* move old group keys (foo=bar) to proper group name (foo) */
1062 copy_keys(kf
, group
, kf
, old_group
);
1070 /* simple wrapper function to print file errors in DEBUG mode */
1071 static void load_system_keyfile(GKeyFile
*key_file
, const gchar
*file
, GKeyFileFlags flags
,
1074 GError
*error
= NULL
;
1075 gboolean done
= g_key_file_load_from_file(key_file
, file
, flags
, &error
);
1079 if (!done
&& !ft
->priv
->custom
)
1080 geany_debug("Failed to open %s (%s)", file
, error
->message
);
1082 g_error_free(error
);
1088 /* Load the configuration file for the associated filetype id.
1089 * This should only be called when the filetype is needed, to save loading
1090 * 20+ configuration files all at once. */
1091 void filetypes_load_config(guint ft_id
, gboolean reload
)
1093 GKeyFile
*config
, *config_home
;
1094 GeanyFiletypePrivate
*pft
;
1097 g_return_if_fail(ft_id
< filetypes_array
->len
);
1099 ft
= filetypes
[ft_id
];
1102 /* when reloading, proceed only if the settings were already loaded */
1103 if (G_UNLIKELY(reload
&& ! pft
->keyfile_loaded
))
1106 /* when not reloading, load the settings only once */
1107 if (G_LIKELY(! reload
&& pft
->keyfile_loaded
))
1109 pft
->keyfile_loaded
= TRUE
;
1111 config
= g_key_file_new();
1112 config_home
= g_key_file_new();
1114 /* highlighting uses GEANY_FILETYPES_NONE for common settings */
1117 f
= filetypes_get_filename(ft
, FALSE
);
1118 load_system_keyfile(config
, f
, G_KEY_FILE_KEEP_COMMENTS
, ft
);
1120 SETPTR(f
, filetypes_get_filename(ft
, TRUE
));
1121 g_key_file_load_from_file(config_home
, f
, G_KEY_FILE_KEEP_COMMENTS
, NULL
);
1124 /* Copy keys for any groups with [group=C] from system keyfile */
1125 copy_ft_groups(config
);
1126 copy_ft_groups(config_home
);
1128 load_settings(ft_id
, config
, config_home
);
1129 highlighting_init_styles(ft_id
, config
, config_home
);
1132 g_object_unref(ft
->icon
);
1133 ft
->icon
= ui_get_mime_icon(ft
->mime_type
);
1135 g_key_file_free(config
);
1136 g_key_file_free(config_home
);
1140 static gchar
*filetypes_get_conf_extension(const GeanyFiletype
*ft
)
1144 if (ft
->priv
->custom
)
1145 return g_strconcat(ft
->name
, ".conf", NULL
);
1147 /* Handle any special extensions different from lowercase filetype->name */
1150 case GEANY_FILETYPES_CPP
: result
= g_strdup("cpp"); break;
1151 case GEANY_FILETYPES_CS
: result
= g_strdup("cs"); break;
1152 case GEANY_FILETYPES_MAKE
: result
= g_strdup("makefile"); break;
1153 case GEANY_FILETYPES_NONE
: result
= g_strdup("common"); break;
1154 /* name is Matlab/Octave */
1155 case GEANY_FILETYPES_MATLAB
: result
= g_strdup("matlab"); break;
1156 /* name is Objective-C, and we don't want the hyphen */
1157 case GEANY_FILETYPES_OBJECTIVEC
: result
= g_strdup("objectivec"); break;
1159 result
= g_ascii_strdown(ft
->name
, -1);
1166 void filetypes_save_commands(GeanyFiletype
*ft
)
1168 GKeyFile
*config_home
;
1169 gchar
*fname
, *data
;
1171 fname
= filetypes_get_filename(ft
, TRUE
);
1172 config_home
= g_key_file_new();
1173 g_key_file_load_from_file(config_home
, fname
, G_KEY_FILE_KEEP_COMMENTS
, NULL
);
1174 build_save_menu(config_home
, ft
, GEANY_BCS_HOME_FT
);
1175 data
= g_key_file_to_data(config_home
, NULL
, NULL
);
1176 utils_write_file(fname
, data
);
1178 g_key_file_free(config_home
);
1183 /* create one file filter which has each file pattern of each filetype */
1184 GtkFileFilter
*filetypes_create_file_filter_all_source(void)
1186 GtkFileFilter
*new_filter
;
1189 new_filter
= gtk_file_filter_new();
1190 gtk_file_filter_set_name(new_filter
, _("All Source"));
1192 for (i
= 0; i
< filetypes_array
->len
; i
++)
1194 if (G_UNLIKELY(i
== GEANY_FILETYPES_NONE
))
1197 for (j
= 0; filetypes
[i
]->pattern
[j
]; j
++)
1199 gtk_file_filter_add_pattern(new_filter
, filetypes
[i
]->pattern
[j
]);
1206 GtkFileFilter
*filetypes_create_file_filter(const GeanyFiletype
*ft
)
1208 GtkFileFilter
*new_filter
;
1212 g_return_val_if_fail(ft
!= NULL
, NULL
);
1214 new_filter
= gtk_file_filter_new();
1215 title
= ft
->id
== GEANY_FILETYPES_NONE
? _("All files") : ft
->title
;
1216 gtk_file_filter_set_name(new_filter
, title
);
1218 for (i
= 0; ft
->pattern
[i
]; i
++)
1220 gtk_file_filter_add_pattern(new_filter
, ft
->pattern
[i
]);
1227 /* Indicates whether there is a tag parser for the filetype or not.
1228 * Only works for custom filetypes if the filetype settings have been loaded. */
1229 gboolean
filetype_has_tags(GeanyFiletype
*ft
)
1231 g_return_val_if_fail(ft
!= NULL
, FALSE
);
1233 return ft
->lang
!= TM_PARSER_NONE
;
1237 /** Finds a filetype pointer from its @a name field.
1238 * @param name Filetype name.
1239 * @return @transfer{none} @nullable The filetype found, or @c NULL.
1244 GeanyFiletype
*filetypes_lookup_by_name(const gchar
*name
)
1248 g_return_val_if_fail(!EMPTY(name
), NULL
);
1250 ft
= g_hash_table_lookup(filetypes_hash
, name
);
1251 if (G_UNLIKELY(ft
== NULL
))
1252 geany_debug("Could not find filetype '%s'.", name
);
1257 static void compile_regex(GeanyFiletype
*ft
, gchar
*regstr
)
1259 GError
*error
= NULL
;
1260 GRegex
*regex
= g_regex_new(regstr
, 0, 0, &error
);
1264 ui_set_statusbar(TRUE
, _("Bad regex for filetype %s: %s"),
1265 filetypes_get_display_name(ft
), error
->message
);
1266 g_error_free(error
);
1268 if (ft
->priv
->error_regex
)
1269 g_regex_unref(ft
->priv
->error_regex
);
1270 ft
->priv
->error_regex
= regex
;
1274 gboolean
filetypes_parse_error_message(GeanyFiletype
*ft
, const gchar
*message
,
1275 gchar
**filename
, gint
*line
)
1281 gint i
, n_match_groups
;
1282 gchar
*first
, *second
;
1286 doc
= document_get_current();
1288 ft
= doc
->file_type
;
1290 tmp
= build_get_regex(build_info
.grp
, ft
, NULL
);
1298 if (G_UNLIKELY(EMPTY(regstr
)))
1301 if (!ft
->priv
->error_regex
|| regstr
!= ft
->priv
->last_error_pattern
)
1303 compile_regex(ft
, regstr
);
1304 ft
->priv
->last_error_pattern
= regstr
;
1306 if (!ft
->priv
->error_regex
)
1309 if (!g_regex_match(ft
->priv
->error_regex
, message
, 0, &minfo
))
1311 g_match_info_free(minfo
);
1315 n_match_groups
= g_match_info_get_match_count(minfo
);
1316 first
= second
= NULL
;
1318 for (i
= 1; i
< n_match_groups
; i
++)
1322 g_match_info_fetch_pos(minfo
, i
, &start_pos
, NULL
);
1323 if (start_pos
!= -1)
1326 first
= g_match_info_fetch(minfo
, i
);
1329 second
= g_match_info_fetch(minfo
, i
);
1340 l
= strtol(first
, &end
, 10);
1341 if (*end
== '\0') /* first is purely decimals */
1349 l
= strtol(second
, &end
, 10);
1366 g_match_info_free(minfo
);
1367 return *filename
!= NULL
;
1372 static void convert_filetype_extensions_to_lower_case(gchar
**patterns
, gsize len
)
1375 for (i
= 0; i
< len
; i
++)
1377 SETPTR(patterns
[i
], g_ascii_strdown(patterns
[i
], -1));
1383 static void read_extensions(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
1389 for (i
= 0; i
< filetypes_array
->len
; i
++)
1392 g_key_file_has_key(userconfig
, "Extensions", filetypes
[i
]->name
, NULL
);
1393 gchar
**list
= g_key_file_get_string_list(
1394 (userset
) ? userconfig
: sysconfig
, "Extensions", filetypes
[i
]->name
, &len
, NULL
);
1396 filetypes
[i
]->priv
->user_extensions
= userset
;
1397 g_strfreev(filetypes
[i
]->pattern
);
1398 /* Note: we allow 'Foo=' to remove all patterns */
1400 list
= g_new0(gchar
*, 1);
1401 filetypes
[i
]->pattern
= list
;
1404 convert_filetype_extensions_to_lower_case(filetypes
[i
]->pattern
, len
);
1410 static void read_group(GKeyFile
*config
, const gchar
*group_name
, GeanyFiletypeGroupID group_id
)
1412 gchar
**names
= g_key_file_get_string_list(config
, "Groups", group_name
, NULL
, NULL
);
1415 foreach_strv(name
, names
)
1417 GeanyFiletype
*ft
= filetypes_lookup_by_name(*name
);
1421 ft
->group
= group_id
;
1422 if (ft
->priv
->custom
&&
1423 (group_id
== GEANY_FILETYPE_GROUP_COMPILED
|| group_id
== GEANY_FILETYPE_GROUP_SCRIPT
))
1425 SETPTR(ft
->title
, filetype_make_title(ft
->name
, TITLE_SOURCE_FILE
));
1429 geany_debug("Filetype '%s' not found for group '%s'!", *name
, group_name
);
1435 static void read_groups(GKeyFile
*config
)
1437 read_group(config
, "Programming", GEANY_FILETYPE_GROUP_COMPILED
);
1438 read_group(config
, "Script", GEANY_FILETYPE_GROUP_SCRIPT
);
1439 read_group(config
, "Markup", GEANY_FILETYPE_GROUP_MARKUP
);
1440 read_group(config
, "Misc", GEANY_FILETYPE_GROUP_MISC
);
1441 read_group(config
, "None", GEANY_FILETYPE_GROUP_NONE
);
1445 static void read_filetype_config(void)
1447 gchar
*sysconfigfile
= g_build_filename(app
->datadir
, "filetype_extensions.conf", NULL
);
1448 gchar
*userconfigfile
= g_build_filename(app
->configdir
, "filetype_extensions.conf", NULL
);
1449 GKeyFile
*sysconfig
= g_key_file_new();
1450 GKeyFile
*userconfig
= g_key_file_new();
1452 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
1453 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
1455 read_extensions(sysconfig
, userconfig
);
1456 read_groups(sysconfig
);
1457 read_groups(userconfig
);
1459 g_free(sysconfigfile
);
1460 g_free(userconfigfile
);
1461 g_key_file_free(sysconfig
);
1462 g_key_file_free(userconfig
);
1466 void filetypes_reload_extensions(void)
1470 read_filetype_config();
1472 /* Redetect filetype of any documents with none set */
1475 GeanyDocument
*doc
= documents
[i
];
1476 if (doc
->file_type
->id
!= GEANY_FILETYPES_NONE
)
1478 document_set_filetype(doc
, filetypes_detect_from_document(doc
));
1483 /** Accessor function for @ref GeanyData::filetypes_array items.
1484 * Example: @code ft = filetypes_index(GEANY_FILETYPES_C); @endcode
1485 * @param idx @c filetypes_array index.
1486 * @return @transfer{none} @nullable The filetype, or @c NULL if @a idx is out of range.
1491 GeanyFiletype
*filetypes_index(gint idx
)
1493 return (idx
>= 0 && idx
< (gint
) filetypes_array
->len
) ? filetypes
[idx
] : NULL
;
1497 void filetypes_reload(void)
1500 GeanyDocument
*current_doc
;
1502 /* reload filetype configs */
1503 for (i
= 0; i
< filetypes_array
->len
; i
++)
1505 /* filetypes_load_config() will skip not loaded filetypes */
1506 filetypes_load_config(i
, TRUE
);
1509 current_doc
= document_get_current();
1513 /* update document styling */
1516 if (current_doc
!= documents
[i
])
1517 document_reload_config(documents
[i
]);
1519 /* process the current document at last */
1520 document_reload_config(current_doc
);
1524 /** Gets @c ft->name or a translation for filetype None.
1527 * @since Geany 0.20 */
1529 const gchar
*filetypes_get_display_name(GeanyFiletype
*ft
)
1531 return ft
->id
== GEANY_FILETYPES_NONE
? _("None") : ft
->name
;
1535 /* gets comment_open/comment_close/comment_single strings from the filetype
1536 * @param single_first: whether single comment is preferred if both available
1537 * returns true if at least comment_open is set, false otherwise */
1538 gboolean
filetype_get_comment_open_close(const GeanyFiletype
*ft
, gboolean single_first
,
1539 const gchar
**co
, const gchar
**cc
)
1541 g_return_val_if_fail(ft
!= NULL
, FALSE
);
1542 g_return_val_if_fail(co
!= NULL
, FALSE
);
1543 g_return_val_if_fail(cc
!= NULL
, FALSE
);
1547 *co
= ft
->comment_single
;
1552 *co
= ft
->comment_open
;
1553 *cc
= ft
->comment_close
;
1558 *co
= ft
->comment_open
;
1560 *cc
= ft
->comment_close
;
1563 *co
= ft
->comment_single
;
1571 static void *copy_(void *src
) { return src
; }
1572 static void free_(void *doc
) { }
1575 * Gets the GType of GeanyFiletype
1577 * @return the GeanyFiletype type */
1579 GType
filetype_get_type (void);
1581 G_DEFINE_BOXED_TYPE(GeanyFiletype
, filetype
, copy_
, free_
);