Revision: 869 http://svn.sourceforge.net/geany/?rev=869&view=rev Author: eht16 Date: 2006-10-06 16:23:05 -0700 (Fri, 06 Oct 2006)
Log Message: ----------- Implemented simple "shebang" detection.
Modified Paths: -------------- trunk/ChangeLog trunk/src/document.c trunk/src/filetypes.c trunk/src/filetypes.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2006-10-06 22:29:04 UTC (rev 868) +++ trunk/ChangeLog 2006-10-06 23:23:05 UTC (rev 869) @@ -1,6 +1,9 @@ 2006-10-06 Enrico Tröger enrico.troeger@uvena.de
* tagmanager/python.c: Improved Python parser to support nested classes. + * src/filetypes.c, src/document.c: + Implemented simple "shebang" detection. It is used prior to the + filename extension detection.
2006-10-04 Enrico Tröger enrico.troeger@uvena.de
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2006-10-06 22:29:04 UTC (rev 868) +++ trunk/src/document.c 2006-10-06 23:23:05 UTC (rev 869) @@ -660,7 +660,7 @@
if (! reload) { - filetype *use_ft = (ft != NULL) ? ft : filetypes_get_from_filename(utf8_filename); + filetype *use_ft = (ft != NULL) ? ft : filetypes_get_from_filename(idx);
doc_list[idx].readonly = readonly; sci_set_readonly(doc_list[idx].sci, readonly); @@ -828,7 +828,7 @@ doc_list[idx].mtime = time(NULL); if (doc_list[idx].file_type == NULL || doc_list[idx].file_type->id == GEANY_FILETYPES_ALL) { - doc_list[idx].file_type = filetypes_get_from_filename(doc_list[idx].file_name); + doc_list[idx].file_type = filetypes_get_from_filename(idx); filetypes_select_radio_item(doc_list[idx].file_type); } document_set_filetype(idx, doc_list[idx].file_type);
Modified: trunk/src/filetypes.c =================================================================== --- trunk/src/filetypes.c 2006-10-06 22:29:04 UTC (rev 868) +++ trunk/src/filetypes.c 2006-10-06 23:23:05 UTC (rev 869) @@ -31,6 +31,8 @@ #include "templates.h" #include "msgwindow.h" #include "utils.h" +#include "document.h" +#include "sciwrappers.h"
/* This is the order of unique ids used in the config file. @@ -111,7 +113,7 @@
/* inits the filetype array and fill it with the known filetypes * and create the filetype menu*/ -void filetypes_init_types(void) +void filetypes_init_types() { GtkWidget *filetype_menu = lookup_widget(app->window, "set_filetype1_menu"); GtkWidget *template_menu = lookup_widget(app->window, "menu_new_with_template1_menu"); @@ -693,18 +695,67 @@ }
+static filetype *find_shebang(gint idx) +{ + gchar *line = sci_get_line(doc_list[idx].sci, 0); + filetype *ft = NULL; + + if (strlen(line) > 2 && line[0] == '#' && line[1]=='!') + { + /// TODO does g_path_get_basename() also work under Win32 for Unix filenames? + gchar *basename_interpreter = g_path_get_basename(line + 2); + + if (strncmp(basename_interpreter, "sh", 2) == 0) + ft = filetypes[GEANY_FILETYPES_SH]; + else if (strncmp(basename_interpreter, "bash", 4) == 0) + ft = filetypes[GEANY_FILETYPES_SH]; + else if (strncmp(basename_interpreter, "perl", 4) == 0) + ft = filetypes[GEANY_FILETYPES_PERL]; + else if (strncmp(basename_interpreter, "python", 6) == 0) + ft = filetypes[GEANY_FILETYPES_PYTHON]; + else if (strncmp(basename_interpreter, "php", 3) == 0) + ft = filetypes[GEANY_FILETYPES_PHP]; + else if (strncmp(basename_interpreter, "ruby", 4) == 0) + ft = filetypes[GEANY_FILETYPES_RUBY]; + else if (strncmp(basename_interpreter, "tcl", 3) == 0) + ft = filetypes[GEANY_FILETYPES_TCL]; + else if (strncmp(basename_interpreter, "zsh", 3) == 0) + ft = filetypes[GEANY_FILETYPES_SH]; + else if (strncmp(basename_interpreter, "ksh", 3) == 0) + ft = filetypes[GEANY_FILETYPES_SH]; + else if (strncmp(basename_interpreter, "csh", 3) == 0) + ft = filetypes[GEANY_FILETYPES_SH]; + // what else to add? + + g_free(basename_interpreter); + } + + g_free(line); + return ft; +} + + /* simple filetype selection based on the filename extension */ -filetype *filetypes_get_from_filename(const gchar *filename) +filetype *filetypes_get_from_filename(gint idx) { GPatternSpec *pattern; + filetype *ft; + gchar *filename; gchar *base_filename, *utf8_filename; gint i, j;
- if (filename == NULL) - { + if (! DOC_IDX_VALID(idx)) return filetypes[GEANY_FILETYPES_ALL]; - }
+ // try to find a shebang and if found use it prior to the filename extension + ft = find_shebang(idx); + if (ft != NULL) return ft; + + if (doc_list[idx].file_name == NULL) + return filetypes[GEANY_FILETYPES_ALL]; + else + filename = doc_list[idx].file_name; + // try to get the UTF-8 equivalent for the filename utf8_filename = g_locale_to_utf8(filename, -1, NULL, NULL, NULL); if (utf8_filename == NULL) @@ -774,7 +825,7 @@
/* frees the array and all related pointers */ -void filetypes_free_types(void) +void filetypes_free_types() { gint i;
Modified: trunk/src/filetypes.h =================================================================== --- trunk/src/filetypes.h 2006-10-06 22:29:04 UTC (rev 868) +++ trunk/src/filetypes.h 2006-10-06 23:23:05 UTC (rev 869) @@ -113,13 +113,13 @@
/* inits the filetype array and fill it with the known filetypes * and create the filetype menu*/ -void filetypes_init_types(void); +void filetypes_init_types();
/* simple filetype selection based on the filename extension */ -filetype *filetypes_get_from_filename(const gchar *filename); +filetype *filetypes_get_from_filename(gint idx);
/* frees the array and all related pointers */ -void filetypes_free_types(void); +void filetypes_free_types();
gchar *filetypes_get_template(filetype *ft);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.