Revision: 1305 http://svn.sourceforge.net/geany/?rev=1305&view=rev Author: ntrel Date: 2007-02-19 05:26:06 -0800 (Mon, 19 Feb 2007)
Log Message: ----------- Support filetype templates for all filetypes (scan ~/.geany/templates/filetype.* at startup). Default templates are now created in init_ft_templates(). Move on_new_with_template() to templates.c. Move on_filetype_change() to filetypes.c.
Modified Paths: -------------- trunk/ChangeLog trunk/src/callbacks.c trunk/src/callbacks.h trunk/src/filetypes.c trunk/src/highlighting.c trunk/src/main.c trunk/src/templates.c trunk/src/templates.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/ChangeLog 2007-02-19 13:26:06 UTC (rev 1305) @@ -1,3 +1,14 @@ +2007-02-19 Nick Treleaven nick.treleaven@btinternet.com + + * src/templates.c, src/templates.h, src/highlighting.c, + src/callbacks.c, src/callbacks.h, src/filetypes.c, src/main.c: + Support filetype templates for all filetypes (scan + ~/.geany/templates/filetype.* at startup). + Default filetype templates are now created in init_ft_templates(). + Move on_new_with_template() to templates.c. + Move on_filetype_change() to filetypes.c. + + 2007-02-18 Enrico Troeger enrico.troeger@uvena.de
* data/filetypes.ruby, src/highlighting.c:
Modified: trunk/src/callbacks.c =================================================================== --- trunk/src/callbacks.c 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/callbacks.c 2007-02-19 13:26:06 UTC (rev 1305) @@ -196,15 +196,6 @@
void -on_new_with_template (GtkMenuItem *menuitem, - gpointer user_data) -{ - document_new_file(NULL, (filetype*) user_data); -} - - - -void on_save1_activate (GtkMenuItem *menuitem, gpointer user_data) { @@ -1070,17 +1061,6 @@
void -on_filetype_change (GtkMenuItem *menuitem, - gpointer user_data) -{ - gint idx = document_get_cur_idx(); - if (app->ignore_callback || idx < 0 || ! doc_list[idx].is_valid) return; - - document_set_filetype(idx, (filetype*)user_data); -} - - -void on_to_lower_case1_activate (GtkMenuItem *menuitem, gpointer user_data) {
Modified: trunk/src/callbacks.h =================================================================== --- trunk/src/callbacks.h 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/callbacks.h 2007-02-19 13:26:06 UTC (rev 1305) @@ -221,10 +221,6 @@ gpointer user_data);
void -on_filetype_change (GtkMenuItem *menuitem, - gpointer user_data); - -void on_to_lower_case1_activate (GtkMenuItem *menuitem, gpointer user_data);
@@ -324,10 +320,6 @@ gpointer user_data);
void -on_new_with_template (GtkMenuItem *menuitem, - gpointer user_data); - -void on_toolbutton_new_clicked (GtkToolButton *toolbutton, gpointer user_data);
Modified: trunk/src/filetypes.c =================================================================== --- trunk/src/filetypes.c 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/filetypes.c 2007-02-19 13:26:06 UTC (rev 1305) @@ -28,7 +28,6 @@ #include "filetypes.h" #include "highlighting.h" #include "support.h" -#include "callbacks.h" #include "templates.h" #include "msgwindow.h" #include "utils.h" @@ -73,7 +72,6 @@
static void filetypes_create_menu_item(GtkWidget *menu, gchar *label, filetype *ftype); -static void filetypes_create_newmenu_item(GtkWidget *menu, gchar *label, filetype *ftype); static void filetypes_init_build_programs(filetype *ftype);
static GtkWidget *radio_items[GEANY_MAX_FILE_TYPES]; @@ -123,7 +121,6 @@ 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");
#define C // these macros are only to ease navigation filetypes[GEANY_FILETYPES_C] = g_new0(filetype, 1); @@ -716,25 +713,6 @@ filetypes[GEANY_FILETYPES_ALL]->comment_close = NULL; filetypes_init_build_programs(filetypes[GEANY_FILETYPES_ALL]); filetypes_create_menu_item(filetype_menu, _("None"), filetypes[GEANY_FILETYPES_ALL]); - - // now add the items for the new file menu - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_C]->title, - filetypes[GEANY_FILETYPES_C]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_CPP]->title, - filetypes[GEANY_FILETYPES_CPP]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_D]->title, - filetypes[GEANY_FILETYPES_D]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_JAVA]->title, - filetypes[GEANY_FILETYPES_JAVA]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_PASCAL]->title, - filetypes[GEANY_FILETYPES_PASCAL]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_RUBY]->title, - filetypes[GEANY_FILETYPES_RUBY]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_PHP]->title, - filetypes[GEANY_FILETYPES_PHP]); - filetypes_create_newmenu_item(template_menu, filetypes[GEANY_FILETYPES_HTML]->title, - filetypes[GEANY_FILETYPES_HTML]); - }
@@ -852,6 +830,17 @@ }
+static void +on_filetype_change (GtkMenuItem *menuitem, + gpointer user_data) +{ + gint idx = document_get_cur_idx(); + if (app->ignore_callback || idx < 0 || ! doc_list[idx].is_valid) return; + + document_set_filetype(idx, (filetype*)user_data); +} + + static void filetypes_create_menu_item(GtkWidget *menu, gchar *label, filetype *ftype) { static GSList *group = NULL; @@ -865,19 +854,6 @@ }
-static void filetypes_create_newmenu_item(GtkWidget *menu, gchar *label, filetype *ftype) -{ - GtkWidget *tmp_menu = gtk_menu_item_new_with_label(label); - GtkWidget *tmp_button = gtk_menu_item_new_with_label(label); - gtk_widget_show(tmp_menu); - gtk_widget_show(tmp_button); - gtk_container_add(GTK_CONTAINER(menu), tmp_menu); - gtk_container_add(GTK_CONTAINER(app->new_file_menu), tmp_button); - g_signal_connect((gpointer) tmp_menu, "activate", G_CALLBACK(on_new_with_template), (gpointer) ftype); - g_signal_connect((gpointer) tmp_button, "activate", G_CALLBACK(on_new_with_template), (gpointer) ftype); -} - - /* frees the array and all related pointers */ void filetypes_free_types() { @@ -981,9 +957,9 @@ { gchar *result, *tmp = g_strdup(filetypes[filetype_idx]->name);
+ // Handle any special extensions different from lowercase filetype->name switch (filetype_idx) { - case GEANY_FILETYPES_ALL: result = g_strdup("common"); break; case GEANY_FILETYPES_CPP: result = g_strdup("cpp"); break; case GEANY_FILETYPES_MAKE: result = g_strdup("makefile"); break; case GEANY_FILETYPES_OMS: result = g_strdup("oms"); break;
Modified: trunk/src/highlighting.c =================================================================== --- trunk/src/highlighting.c 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/highlighting.c 2007-02-19 13:26:06 UTC (rev 1305) @@ -122,9 +122,11 @@ }
-static void load_keyfiles(GKeyFile *config, GKeyFile *config_home, gint filetype_idx) +static void load_keyfiles(GKeyFile *config, GKeyFile *config_home, filetype_id ft_id) { - gchar *ext = filetypes_get_conf_extension(filetype_idx); + // highlighting uses GEANY_FILETYPES_ALL for common settings + gchar *ext = (ft_id != GEANY_FILETYPES_ALL) ? + filetypes_get_conf_extension(ft_id) : g_strdup("common"); gchar *f0 = g_strconcat(app->datadir, G_DIR_SEPARATOR_S "filetypes.", ext, NULL); gchar *f = g_strconcat(app->configdir, G_DIR_SEPARATOR_S GEANY_FILEDEFS_SUBDIR G_DIR_SEPARATOR_S "filetypes.", ext, NULL);
Modified: trunk/src/main.c =================================================================== --- trunk/src/main.c 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/main.c 2007-02-19 13:26:06 UTC (rev 1305) @@ -597,8 +597,6 @@ ui_create_insert_date_menu_items(); keybindings_init(); notebook_init(); - templates_init(); - document_init_doclist(); filetypes_init_types(); #ifdef GEANY_DEBUG if (generate_datafiles) @@ -607,6 +605,8 @@ exit(0); } #endif + templates_init(); + document_init_doclist(); configuration_read_filetype_extensions();
// set window icon
Modified: trunk/src/templates.c =================================================================== --- trunk/src/templates.c 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/templates.c 2007-02-19 13:26:06 UTC (rev 1305) @@ -201,57 +201,40 @@
static gchar *templates[GEANY_MAX_TEMPLATES]; +static gchar *ft_templates[GEANY_MAX_FILE_TYPES] = {NULL};
// some simple macros to reduce code size and make the code readable -#define TEMPLATES_GET_FILENAME(x) g_strconcat(app->configdir, \ - G_DIR_SEPARATOR_S GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S, x, NULL) -#define TEMPLATES_CREATE_FILE(x, y) if (! g_file_test(x, G_FILE_TEST_EXISTS)) utils_write_file(x, y) -#define TEMPLATES_READ_FILE(x, y) g_file_get_contents(x, y, NULL, NULL); +#define TEMPLATES_GET_FILENAME(shortname) \ + g_strconcat(app->configdir, \ + G_DIR_SEPARATOR_S GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S, shortname, NULL)
+#define TEMPLATES_CREATE_FILE(fname, text) \ + if (! g_file_test(fname, G_FILE_TEST_EXISTS)) \ + utils_write_file(fname, text)
+#define TEMPLATES_READ_FILE(fname, contents_ptr) \ + g_file_get_contents(fname, contents_ptr, NULL, NULL); + + // prototype, because this function should never be used outside of templates.c -static gchar *templates_replace_all(gchar *source, gchar *year, gchar *date); +static gchar *templates_replace_all(gchar *source, const gchar *year, const gchar *date);
-void templates_init(void) +static void init_general_templates(const gchar *year, const gchar *date) { gchar *template_filename_fileheader = TEMPLATES_GET_FILENAME("fileheader"); gchar *template_filename_gpl = TEMPLATES_GET_FILENAME("gpl"); gchar *template_filename_bsd = TEMPLATES_GET_FILENAME("bsd"); gchar *template_filename_function = TEMPLATES_GET_FILENAME("function"); gchar *template_filename_changelog = TEMPLATES_GET_FILENAME("changelog"); - gchar *template_filename_filetype_none = TEMPLATES_GET_FILENAME("filetype.none"); - gchar *template_filename_filetype_c = TEMPLATES_GET_FILENAME("filetype.c"); - gchar *template_filename_filetype_cpp = TEMPLATES_GET_FILENAME("filetype.cpp"); - gchar *template_filename_filetype_d = TEMPLATES_GET_FILENAME("filetype.d"); - gchar *template_filename_filetype_java = TEMPLATES_GET_FILENAME("filetype.java"); - gchar *template_filename_filetype_pascal = TEMPLATES_GET_FILENAME("filetype.pascal"); - gchar *template_filename_filetype_php = TEMPLATES_GET_FILENAME("filetype.php"); - gchar *template_filename_filetype_html = TEMPLATES_GET_FILENAME("filetype.html"); - gchar *template_filename_filetype_ruby = TEMPLATES_GET_FILENAME("filetype.ruby");
- time_t tp = time(NULL); - const struct tm *tm = localtime(&tp); - gchar *year = g_malloc0(5); - gchar *date = utils_get_date(); - strftime(year, 5, "%Y", tm); - // create the template files in the configuration directory, if they don't exist TEMPLATES_CREATE_FILE(template_filename_fileheader, templates_fileheader); TEMPLATES_CREATE_FILE(template_filename_gpl, templates_gpl_notice); TEMPLATES_CREATE_FILE(template_filename_bsd, templates_bsd_notice); TEMPLATES_CREATE_FILE(template_filename_function, templates_function_description); TEMPLATES_CREATE_FILE(template_filename_changelog, templates_changelog); - TEMPLATES_CREATE_FILE(template_filename_filetype_none, templates_filetype_none); - TEMPLATES_CREATE_FILE(template_filename_filetype_c, templates_filetype_c); - TEMPLATES_CREATE_FILE(template_filename_filetype_cpp, templates_filetype_cpp); - TEMPLATES_CREATE_FILE(template_filename_filetype_d, templates_filetype_d); - TEMPLATES_CREATE_FILE(template_filename_filetype_java, templates_filetype_java); - TEMPLATES_CREATE_FILE(template_filename_filetype_pascal, templates_filetype_pascal); - TEMPLATES_CREATE_FILE(template_filename_filetype_php, templates_filetype_php); - TEMPLATES_CREATE_FILE(template_filename_filetype_html, templates_filetype_html); - TEMPLATES_CREATE_FILE(template_filename_filetype_ruby, templates_filetype_ruby);
// read the contents TEMPLATES_READ_FILE(template_filename_fileheader, &templates[GEANY_TEMPLATE_FILEHEADER]); @@ -269,51 +252,101 @@ TEMPLATES_READ_FILE(template_filename_changelog, &templates[GEANY_TEMPLATE_CHANGELOG]); templates[GEANY_TEMPLATE_CHANGELOG] = templates_replace_all(templates[GEANY_TEMPLATE_CHANGELOG], year, date);
- TEMPLATES_READ_FILE(template_filename_filetype_none, &templates[GEANY_TEMPLATE_FILETYPE_NONE]); - templates[GEANY_TEMPLATE_FILETYPE_NONE] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_NONE], year, date); + // free the whole stuff + g_free(template_filename_fileheader); + g_free(template_filename_gpl); + g_free(template_filename_bsd); + g_free(template_filename_function); + g_free(template_filename_changelog); +}
- TEMPLATES_READ_FILE(template_filename_filetype_c, &templates[GEANY_TEMPLATE_FILETYPE_C]); - templates[GEANY_TEMPLATE_FILETYPE_C] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_C], year, date);
- TEMPLATES_READ_FILE(template_filename_filetype_d, &templates[GEANY_TEMPLATE_FILETYPE_D]); - templates[GEANY_TEMPLATE_FILETYPE_D] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_D], year, date); +static void init_ft_templates(const gchar *year, const gchar *date) +{ + filetype_id ft_id;
- TEMPLATES_READ_FILE(template_filename_filetype_cpp, &templates[GEANY_TEMPLATE_FILETYPE_CPP]); - templates[GEANY_TEMPLATE_FILETYPE_CPP] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_CPP], year, date); + for (ft_id = 0; ft_id < GEANY_MAX_FILE_TYPES; ft_id++) + { + gchar *ext = filetypes_get_conf_extension(ft_id); + gchar *shortname = g_strconcat("filetype.", ext, NULL); + gchar *fname = TEMPLATES_GET_FILENAME(shortname);
- TEMPLATES_READ_FILE(template_filename_filetype_java, &templates[GEANY_TEMPLATE_FILETYPE_JAVA]); - templates[GEANY_TEMPLATE_FILETYPE_JAVA] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_JAVA], year, date); + switch (ft_id) + { + case GEANY_FILETYPES_ALL: TEMPLATES_CREATE_FILE(fname, templates_filetype_none); break; + case GEANY_FILETYPES_C: TEMPLATES_CREATE_FILE(fname, templates_filetype_c); break; + case GEANY_FILETYPES_CPP: TEMPLATES_CREATE_FILE(fname, templates_filetype_cpp); break; + case GEANY_FILETYPES_D: TEMPLATES_CREATE_FILE(fname, templates_filetype_d); break; + case GEANY_FILETYPES_JAVA: TEMPLATES_CREATE_FILE(fname, templates_filetype_java); break; + case GEANY_FILETYPES_PASCAL:TEMPLATES_CREATE_FILE(fname, templates_filetype_pascal); break; + case GEANY_FILETYPES_PHP: TEMPLATES_CREATE_FILE(fname, templates_filetype_php); break; + case GEANY_FILETYPES_HTML: TEMPLATES_CREATE_FILE(fname, templates_filetype_html); break; + case GEANY_FILETYPES_RUBY: TEMPLATES_CREATE_FILE(fname, templates_filetype_ruby); break; + default: break; + } + TEMPLATES_READ_FILE(fname, &ft_templates[ft_id]); + ft_templates[ft_id] = templates_replace_all(ft_templates[ft_id], year, date);
- TEMPLATES_READ_FILE(template_filename_filetype_pascal, &templates[GEANY_TEMPLATE_FILETYPE_PASCAL]); - templates[GEANY_TEMPLATE_FILETYPE_PASCAL] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_PASCAL], year, date); + g_free(fname); + g_free(shortname); + g_free(ext); + } +}
- TEMPLATES_READ_FILE(template_filename_filetype_php, &templates[GEANY_TEMPLATE_FILETYPE_PHP]); - templates[GEANY_TEMPLATE_FILETYPE_PHP] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_PHP], year, date);
- TEMPLATES_READ_FILE(template_filename_filetype_html, &templates[GEANY_TEMPLATE_FILETYPE_HTML]); - templates[GEANY_TEMPLATE_FILETYPE_HTML] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_HTML], year, date); +static void +on_new_with_template (GtkMenuItem *menuitem, + gpointer user_data) +{ + document_new_file(NULL, (filetype*) user_data); +}
- TEMPLATES_READ_FILE(template_filename_filetype_ruby, &templates[GEANY_TEMPLATE_FILETYPE_RUBY]); - templates[GEANY_TEMPLATE_FILETYPE_RUBY] = templates_replace_all(templates[GEANY_TEMPLATE_FILETYPE_RUBY], year, date);
+// template items for the new file menu +static void create_new_menu_items() +{ + GtkWidget *template_menu = lookup_widget(app->window, "menu_new_with_template1_menu"); + filetype_id ft_id;
- // free the whole stuff + for (ft_id = 0; ft_id < GEANY_MAX_FILE_TYPES; ft_id++) + { + GtkWidget *tmp_menu, *tmp_button; + filetype *ft = filetypes[ft_id]; + const gchar *label = ft->title; + + if (ft_templates[ft_id] == NULL) + { + continue; + } + tmp_menu = gtk_menu_item_new_with_label(label); + tmp_button = gtk_menu_item_new_with_label(label); + gtk_widget_show(tmp_menu); + gtk_widget_show(tmp_button); + gtk_container_add(GTK_CONTAINER(template_menu), tmp_menu); + gtk_container_add(GTK_CONTAINER(app->new_file_menu), tmp_button); + g_signal_connect((gpointer) tmp_menu, "activate", + G_CALLBACK(on_new_with_template), (gpointer) ft); + g_signal_connect((gpointer) tmp_button, "activate", + G_CALLBACK(on_new_with_template), (gpointer) ft); + } +} + + +void templates_init(void) +{ + time_t tp = time(NULL); + const struct tm *tm = localtime(&tp); + gchar *year = g_malloc0(5); + gchar *date = utils_get_date(); + + strftime(year, 5, "%Y", tm); + + init_general_templates(year, date); + init_ft_templates(year, date); + create_new_menu_items(); + g_free(date); g_free(year); - g_free(template_filename_fileheader); - g_free(template_filename_gpl); - g_free(template_filename_bsd); - g_free(template_filename_function); - g_free(template_filename_changelog); - g_free(template_filename_filetype_none); - g_free(template_filename_filetype_c); - g_free(template_filename_filetype_cpp); - g_free(template_filename_filetype_d); - g_free(template_filename_filetype_java); - g_free(template_filename_filetype_php); - g_free(template_filename_filetype_html); - g_free(template_filename_filetype_pascal); - g_free(template_filename_filetype_ruby); }
@@ -514,28 +547,9 @@
static gchar *get_file_template(filetype *ft) { - switch (FILETYPE_ID(ft)) - { - case GEANY_FILETYPES_ALL: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_NONE); break; - case GEANY_FILETYPES_C: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_C); break; - case GEANY_FILETYPES_CPP: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_CPP); break; - case GEANY_FILETYPES_PHP: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_PHP); break; - case GEANY_FILETYPES_JAVA: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_JAVA); break; - case GEANY_FILETYPES_PASCAL: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_PASCAL); break; - case GEANY_FILETYPES_RUBY: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_RUBY); break; - case GEANY_FILETYPES_D: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_D); break; - case GEANY_FILETYPES_HTML: - return templates_get_template_generic(GEANY_TEMPLATE_FILETYPE_HTML); break; - default: return NULL; - } + filetype_id ft_id = FILETYPE_ID(ft); + + return g_strdup(ft_templates[ft_id]); }
@@ -604,7 +618,7 @@ }
-static gchar *templates_replace_all(gchar *text, gchar *year, gchar *date) +static gchar *templates_replace_all(gchar *text, const gchar *year, const gchar *date) { text = utils_str_replace(text, "{year}", year); text = utils_str_replace(text, "{date}", date);
Modified: trunk/src/templates.h =================================================================== --- trunk/src/templates.h 2007-02-18 13:24:20 UTC (rev 1304) +++ trunk/src/templates.h 2007-02-19 13:26:06 UTC (rev 1305) @@ -50,17 +50,6 @@ GEANY_TEMPLATE_FILEHEADER, GEANY_TEMPLATE_CHANGELOG, GEANY_TEMPLATE_FUNCTION, - - GEANY_TEMPLATE_FILETYPE_NONE, - GEANY_TEMPLATE_FILETYPE_C, - GEANY_TEMPLATE_FILETYPE_CPP, - GEANY_TEMPLATE_FILETYPE_JAVA, - GEANY_TEMPLATE_FILETYPE_PHP, - GEANY_TEMPLATE_FILETYPE_PASCAL, - GEANY_TEMPLATE_FILETYPE_RUBY, - GEANY_TEMPLATE_FILETYPE_D, - GEANY_TEMPLATE_FILETYPE_HTML, - GEANY_MAX_TEMPLATES };
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.