Revision: 994 http://svn.sourceforge.net/geany/?rev=994&view=rev Author: ntrel Date: 2006-11-14 08:03:25 -0800 (Tue, 14 Nov 2006)
Log Message: ----------- Add NULL-safe FILETYPE_ID macro to get filetype::id from pointer. Allow insertion of GPL notice and file header when the filetype is not set. Add commenting for PHP & HTML GPL notice and file header templates. Add make_comment_block() to templates.c. Move document_prepare_template(), filetypes_get_template() to templates.c.
Modified Paths: -------------- trunk/ChangeLog trunk/src/callbacks.c trunk/src/document.c trunk/src/document.h trunk/src/filetypes.c trunk/src/filetypes.h trunk/src/templates.c trunk/src/templates.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/ChangeLog 2006-11-14 16:03:25 UTC (rev 994) @@ -1,3 +1,16 @@ +2006-11-14 Nick Treleaven nick.treleaven@btinternet.com + + * src/templates.c, src/templates.h, src/callbacks.c, src/filetypes.c, + src/filetypes.h, src/document.c, src/document.h: + Add NULL-safe FILETYPE_ID macro to get filetype::id from pointer. + Allow insertion of GPL notice and file header when the filetype is + not set. + Add commenting for PHP & HTML GPL notice and file header templates. + Add make_comment_block() to templates.c. + Move document_prepare_template(), filetypes_get_template() to + templates.c. + + 2006-11-13 Enrico Tröger enrico.troeger@uvena.de
* src/filetypes.c, src/filetypes.h, src/highlighting.c,
Modified: trunk/src/callbacks.c =================================================================== --- trunk/src/callbacks.c 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/callbacks.c 2006-11-14 16:03:25 UTC (rev 994) @@ -1582,34 +1582,8 @@ gint idx = document_get_cur_idx(); gchar *text;
- if (doc_list[idx].file_type == NULL) - { - msgwin_status_add(_("Please set the filetype for the current file before using this function.")); - return; - } + text = templates_get_template_gpl(FILETYPE_ID(doc_list[idx].file_type));
- switch (doc_list[idx].file_type->id) - { - case GEANY_FILETYPES_PASCAL: - { - text = templates_get_template_gpl(GEANY_TEMPLATE_GPL_PASCAL); - break; - } - case GEANY_FILETYPES_PYTHON: - case GEANY_FILETYPES_RUBY: - case GEANY_FILETYPES_SH: - case GEANY_FILETYPES_MAKE: - case GEANY_FILETYPES_PERL: - { - text = templates_get_template_gpl(GEANY_TEMPLATE_GPL_ROUTE); - break; - } - default: - { - text = templates_get_template_gpl(GEANY_TEMPLATE_GPL); - } - } - sci_insert_text(doc_list[idx].sci, editor_info.click_pos, text); g_free(text); } @@ -1638,39 +1612,9 @@ { gint idx = document_get_cur_idx(); gchar *text; - gchar *ext = NULL;
- if (doc_list[idx].file_type == NULL) - { - msgwin_status_add(_("Please set the filetype for the current file before using this function.")); - return; - } + text = templates_get_template_fileheader(idx);
- if (doc_list[idx].file_name == NULL) - ext = doc_list[idx].file_type->extension; - - switch (doc_list[idx].file_type->id) - { - case GEANY_FILETYPES_PASCAL: - { - text = templates_get_template_fileheader(GEANY_TEMPLATE_FILEHEADER_PASCAL, ext, idx); - break; - } - case GEANY_FILETYPES_PYTHON: - case GEANY_FILETYPES_RUBY: - case GEANY_FILETYPES_SH: - case GEANY_FILETYPES_MAKE: - case GEANY_FILETYPES_PERL: - { - text = templates_get_template_fileheader(GEANY_TEMPLATE_FILEHEADER_ROUTE, ext, idx); - break; - } - default: - { - text = templates_get_template_fileheader(GEANY_TEMPLATE_FILEHEADER, ext, idx); - } - } - sci_insert_text(doc_list[idx].sci, 0, text); sci_goto_pos(doc_list[idx].sci, 0, FALSE); g_free(text);
Modified: trunk/src/document.c =================================================================== --- trunk/src/document.c 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/document.c 2006-11-14 16:03:25 UTC (rev 994) @@ -408,7 +408,7 @@ void document_new_file(filetype *ft) { gint idx = document_create_new_sci(NULL); - gchar *template = document_prepare_template(ft); + gchar *template = templates_get_template_new_file(ft);
g_assert(idx != -1);
@@ -1309,68 +1309,6 @@ }
-/// TODO move me to filetypes.c -gchar *document_prepare_template(filetype *ft) -{ - gchar *gpl_notice = NULL; - gchar *template = NULL; - gchar *ft_template = NULL; - - if (ft != NULL) - { - switch (ft->id) - { - case GEANY_FILETYPES_PHP: - { // PHP: include the comment in <?php ?> - tags - gchar *tmp = templates_get_template_fileheader( - GEANY_TEMPLATE_FILEHEADER, ft->extension, -1); - gpl_notice = g_strconcat("<?php\n", tmp, "?>\n\n", NULL); - g_free(tmp); - break; - } - case GEANY_FILETYPES_HTML: - { // HTML: include the comment in <!-- --> - tags - gchar *tmp = templates_get_template_fileheader( - GEANY_TEMPLATE_FILEHEADER, ft->extension, -1); - gpl_notice = g_strconcat("<!--\n", tmp, "-->\n\n", NULL); - g_free(tmp); - break; - } - case GEANY_FILETYPES_PASCAL: - { // Pascal: comments are in { } brackets - gpl_notice = templates_get_template_fileheader( - GEANY_TEMPLATE_FILEHEADER_PASCAL, ft->extension, -1); - break; - } - case GEANY_FILETYPES_PYTHON: - case GEANY_FILETYPES_RUBY: - case GEANY_FILETYPES_SH: - case GEANY_FILETYPES_MAKE: - case GEANY_FILETYPES_PERL: - { - gpl_notice = templates_get_template_fileheader( - GEANY_TEMPLATE_FILEHEADER_ROUTE, ft->extension, -1); - break; - } - default: - { // -> C, C++, Java, ... - gpl_notice = templates_get_template_fileheader( - GEANY_TEMPLATE_FILEHEADER, ft->extension, -1); - } - } - ft_template = filetypes_get_template(ft); - template = g_strconcat(gpl_notice, ft_template, NULL); - g_free(ft_template); - g_free(gpl_notice); - return template; - } - else - { // new file w/o template - return templates_get_template_fileheader(GEANY_TEMPLATE_FILETYPE_NONE, NULL, -1); - } -} - - void document_unfold_all(gint idx) { gint lines, pos, i;
Modified: trunk/src/document.h =================================================================== --- trunk/src/document.h 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/document.h 2006-11-14 16:03:25 UTC (rev 994) @@ -161,8 +161,6 @@
gchar *document_get_eol_mode(gint);
-gchar *document_prepare_template(filetype *ft); - void document_fold_all(gint idx);
void document_unfold_all(gint idx);
Modified: trunk/src/filetypes.c =================================================================== --- trunk/src/filetypes.c 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/filetypes.c 2006-11-14 16:03:25 UTC (rev 994) @@ -513,15 +513,11 @@ filetypes[GEANY_FILETYPES_HTML]->has_tags = FALSE; filetypes[GEANY_FILETYPES_HTML]->title = g_strdup(_("HTML source file")); filetypes[GEANY_FILETYPES_HTML]->extension = g_strdup("html"); - filetypes[GEANY_FILETYPES_HTML]->pattern = g_new0(gchar*, 8); - filetypes[GEANY_FILETYPES_HTML]->pattern[0] = g_strdup("*.htm"); - filetypes[GEANY_FILETYPES_HTML]->pattern[1] = g_strdup("*.html"); - filetypes[GEANY_FILETYPES_HTML]->pattern[2] = g_strdup("*.shtml"); - filetypes[GEANY_FILETYPES_HTML]->pattern[3] = g_strdup("*.hta"); - filetypes[GEANY_FILETYPES_HTML]->pattern[4] = g_strdup("*.htd"); - filetypes[GEANY_FILETYPES_HTML]->pattern[5] = g_strdup("*.htt"); - filetypes[GEANY_FILETYPES_HTML]->pattern[6] = g_strdup("*.cfm"); - filetypes[GEANY_FILETYPES_HTML]->pattern[7] = NULL; + { + gchar *patterns[] = {"*.htm", "*.html", "*.shtml", "*.hta", "*.htd", "*.htt", + "*.cfm", NULL}; + filetypes[GEANY_FILETYPES_HTML]->pattern = g_strdupv(patterns); + } filetypes[GEANY_FILETYPES_HTML]->style_func_ptr = styleset_html; filetypes[GEANY_FILETYPES_HTML]->comment_open = g_strdup("<!--"); filetypes[GEANY_FILETYPES_HTML]->comment_close = g_strdup("-->"); @@ -893,33 +889,6 @@ }
-gchar *filetypes_get_template(filetype *ft) -{ - if (ft == NULL) return NULL; - - switch (ft->id) - { - 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; - } -} - - void filetypes_get_config(GKeyFile *config, GKeyFile *configh, gint ft) { gchar *result;
Modified: trunk/src/filetypes.h =================================================================== --- trunk/src/filetypes.h 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/filetypes.h 2006-11-14 16:03:25 UTC (rev 994) @@ -64,7 +64,11 @@ GEANY_MAX_FILE_TYPES };
+// Safe wrapper to get the id field of a possibly NULL filetype pointer. +#define FILETYPE_ID(filetype_ptr) \ + (((filetype_ptr) != NULL) ? (filetype_ptr)->id : GEANY_FILETYPES_ALL)
+ struct build_menu_items { GtkWidget *menu; @@ -124,8 +128,6 @@ /* frees the array and all related pointers */ void filetypes_free_types();
-gchar *filetypes_get_template(filetype *ft); - void filetypes_get_config(GKeyFile *config, GKeyFile *configh, gint ft);
void filetypes_save_commands();
Modified: trunk/src/templates.c =================================================================== --- trunk/src/templates.c 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/templates.c 2006-11-14 16:03:25 UTC (rev 994) @@ -84,7 +84,7 @@ * name: {functionname}\n\ * @param\n\ * @return\n\ -*/\n"; + */\n";
static const gchar templates_function_description_pascal[] = "\ {\n\ @@ -103,7 +103,7 @@ static const gchar templates_multiline[] = "\ /* \n\ * \n\ -*/"; + */";
static const gchar templates_multiline_pascal[] = "\ {\n\ @@ -121,7 +121,7 @@ * Copyright {year} {developer} <{mail}>\n\ *\n\ {gpl}\ - */\n\n"; + */\n";
static const gchar templates_fileheader_pascal[] = "\ {\n\ @@ -130,7 +130,7 @@ Copyright {year} {developer} <{mail}>\n\ \n\ {gpl}\ -}\n\n"; +}\n";
static const gchar templates_fileheader_route[] = "\ #\n\ @@ -139,7 +139,7 @@ # Copyright {year} {developer} <{mail}>\n\ #\n\ {gpl}\ -#\n\n"; +#\n";
static const gchar templates_changelog[] = "\ {date} {developer} <{mail}>\n\ @@ -409,28 +409,97 @@ }
+/* double_comment is a hack for PHP/HTML for whether to first add C style commenting. + * In future we could probably remove the need for this by making most templates + * automatically commented (so template files are not commented) */ +static gchar *make_comment_block(const gchar *comment_text, gint filetype_id, + gboolean double_comment) +{ + switch (filetype_id) + { + case GEANY_FILETYPES_ALL: + return g_strdup(comment_text); // no need to add to the text + + case GEANY_FILETYPES_PHP: + case GEANY_FILETYPES_HTML: + { // double comment + gchar *tmp = (double_comment) ? + make_comment_block(comment_text, GEANY_FILETYPES_C, FALSE) : + g_strdup(comment_text); + gchar *block = (filetype_id == GEANY_FILETYPES_PHP) ? + g_strconcat("<?php\n", tmp, "?>\n", NULL) : + g_strconcat("<!--\n", tmp, "-->\n", NULL); + g_free(tmp); + return block; + } + + case GEANY_FILETYPES_PYTHON: + case GEANY_FILETYPES_RUBY: + case GEANY_FILETYPES_SH: + case GEANY_FILETYPES_MAKE: + case GEANY_FILETYPES_PERL: + return g_strconcat("#\n", comment_text, "#\n", NULL); + + case GEANY_FILETYPES_PASCAL: + return g_strconcat("{\n", comment_text, "}\n", NULL); + + default: + return g_strconcat("/*\n", comment_text, " */\n", NULL); + } +} + + +gchar *templates_get_template_gpl(gint filetype_id) +{ + const gchar *text; + + switch (filetype_id) + { + case GEANY_FILETYPES_PYTHON: + case GEANY_FILETYPES_RUBY: + case GEANY_FILETYPES_SH: + case GEANY_FILETYPES_MAKE: + case GEANY_FILETYPES_PERL: + text = templates[GEANY_TEMPLATE_GPL_ROUTE]; + break; + + case GEANY_FILETYPES_PASCAL: + case GEANY_FILETYPES_ALL: + text = templates[GEANY_TEMPLATE_GPL_PASCAL]; + break; + + case GEANY_FILETYPES_HTML: + case GEANY_FILETYPES_PHP: + default: + text = templates[GEANY_TEMPLATE_GPL]; + break; + } + return make_comment_block(text, filetype_id, TRUE); +} + + /* Returns a template chosen by template with GEANY_STRING_UNTITLED.extension * as filename if idx is -1, or the real filename if idx is greater than -1. - * The flag gpl decides wether a GPL notice is appended or not -*/ -gchar *templates_get_template_fileheader(gint template, const gchar *extension, gint idx) + * The flag gpl decides whether a GPL notice is appended or not */ +static gchar * +prepare_file_header(gint template, const gchar *extension, const gchar *filename) { gchar *result = g_strdup(templates[template]); - gchar *filename; + gchar *shortname; gchar *date = utils_get_date_time();
- if (idx == -1 || doc_list[idx].file_name == NULL) + if (filename == NULL) { if (extension != NULL) - filename = g_strconcat(GEANY_STRING_UNTITLED, ".", extension, NULL); + shortname = g_strconcat(GEANY_STRING_UNTITLED, ".", extension, NULL); else - filename = g_strdup(GEANY_STRING_UNTITLED); + shortname = g_strdup(GEANY_STRING_UNTITLED); } else { - filename = g_path_get_basename(doc_list[idx].file_name); + shortname = g_path_get_basename(filename); } - result = utils_str_replace(result, "{filename}", filename); + result = utils_str_replace(result, "{filename}", shortname);
if (template == GEANY_TEMPLATE_FILEHEADER_PASCAL) { @@ -446,12 +515,117 @@ } result = utils_str_replace(result, "{datetime}", date);
- g_free(filename); + g_free(shortname); g_free(date); return result; }
+// ft, fname can be NULL +static gchar *get_file_header(filetype *ft, const gchar *fname) +{ + gchar *text = NULL; + + switch (FILETYPE_ID(ft)) + { + case GEANY_FILETYPES_ALL: // ft may be NULL + { + text = prepare_file_header(GEANY_TEMPLATE_FILEHEADER, NULL, fname); + break; + } + case GEANY_FILETYPES_PHP: + case GEANY_FILETYPES_HTML: + { + gchar *tmp = prepare_file_header( + GEANY_TEMPLATE_FILEHEADER, ft->extension, fname); + text = make_comment_block(tmp, ft->id, FALSE); + g_free(tmp); + break; + } + case GEANY_FILETYPES_PASCAL: + { // Pascal: comments are in { } brackets + text = prepare_file_header( + GEANY_TEMPLATE_FILEHEADER_PASCAL, ft->extension, fname); + break; + } + case GEANY_FILETYPES_PYTHON: + case GEANY_FILETYPES_RUBY: + case GEANY_FILETYPES_SH: + case GEANY_FILETYPES_MAKE: + case GEANY_FILETYPES_PERL: + { + text = prepare_file_header( + GEANY_TEMPLATE_FILEHEADER_ROUTE, ft->extension, fname); + break; + } + default: + { // -> C, C++, Java, ... + text = prepare_file_header( + GEANY_TEMPLATE_FILEHEADER, ft->extension, fname); + } + } + return text; +} + + +gchar *templates_get_template_fileheader(gint idx) +{ + gchar *fname; + filetype *ft; + + g_return_val_if_fail(DOC_IDX_VALID(idx), NULL); + ft = doc_list[idx].file_type; + fname = doc_list[idx].file_name; + + return get_file_header(ft, fname); +} + + +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; + } +} + + +gchar *templates_get_template_new_file(filetype *ft) +{ + gchar *template = NULL; + gchar *ft_template = NULL; + gchar *file_header = NULL; + + if (FILETYPE_ID(ft) == GEANY_FILETYPES_ALL) + return get_file_template(ft); + + file_header = get_file_header(ft, NULL); // file template only used for new files + ft_template = get_file_template(ft); + template = g_strconcat(file_header, "\n", ft_template, NULL); + g_free(ft_template); + g_free(file_header); + return template; +} + + gchar *templates_get_template_generic(gint template) { return g_strdup(templates[template]); @@ -473,28 +647,6 @@ return result; }
-gchar *templates_get_template_gpl(gint template) -{ - switch (template) - { - case GEANY_TEMPLATE_GPL_PASCAL: - { - return g_strconcat("{\n", templates[template], "}\n", NULL); - break; - } - case GEANY_TEMPLATE_GPL_ROUTE: - { - return g_strconcat("#\n", templates[template], "#\n", NULL); - break; - } - default: - { - return g_strconcat("/*\n", templates[template], "*/\n", NULL); - } - } -} - - gchar *templates_get_template_changelog(void) { gchar *date = utils_get_date_time();
Modified: trunk/src/templates.h =================================================================== --- trunk/src/templates.h 2006-11-14 01:27:12 UTC (rev 993) +++ trunk/src/templates.h 2006-11-14 16:03:25 UTC (rev 994) @@ -23,18 +23,22 @@ #ifndef GEANY_TEMPLATES_H #define GEANY_TEMPLATES_H 1
+#include "filetypes.h"
+ void templates_init(void);
-gchar *templates_get_template_fileheader(gint template, const gchar *extension, gint idx); +gchar *templates_get_template_fileheader(gint idx);
+gchar *templates_get_template_new_file(filetype *ft); + gchar *templates_get_template_changelog(void);
gchar *templates_get_template_generic(gint template);
gchar *templates_get_template_function(gint template, const gchar *func_name);
-gchar *templates_get_template_gpl(gint template); +gchar *templates_get_template_gpl(gint filetype_id);
void templates_free_templates(void);
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.