When using custom file templates, add the extension of the template file to the filename of the new document.

Make {datetime} wildcard generally available for most template types.
Allow use of most available wildcards for custom file templates, too.

git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3202 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Enrico Tröger 2008-11-10 18:27:23 +00:00
parent 0e54a39c9b
commit 98cf47a6a8
6 changed files with 668 additions and 643 deletions

View File

@ -1,3 +1,13 @@
2008-11-10 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* doc/geany.txt, doc/geany.html, src/templates.c, src/templates.c,
src/editor.c:
When using custom file templates, add the extension of the template
file to the filename of the new document.
Make {datetime} wildcard generally available for most template types.
Allow use of most available wildcards for custom file templates, too.
2008-11-10 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com> 2008-11-10 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* src/about.c, src/treeviews.c, src/treeviews.h, src/symbols.c, * src/about.c, src/treeviews.c, src/treeviews.h, src/symbols.c,

File diff suppressed because it is too large Load Diff

View File

@ -3178,46 +3178,48 @@ Template wildcards
All wildcards must be enclosed by "{" and "}", e.g. {date}. All wildcards must be enclosed by "{" and "}", e.g. {date}.
============== ============================================= ============== ============== ============================================= =======================================
Wildcard Description Available in Wildcard Description Available in
============== ============================================= ============== ============== ============================================= =======================================
developer The name of the developer. filetypes, file header, function developer The name of the developer. filetype templates, file header,
description, ChangeLog entry, bsd, function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
initial The developer's initials, e.g. "ET" for filetypes, file header, function initial The developer's initials, e.g. "ET" for filetype templates, file header,
Enrico Tröger or "JFD" for John Foobar Doe. description, ChangeLog entry, bsd, Enrico Tröger or "JFD" for John Foobar Doe. function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
mail The email address of the developer. file header, function description, mail The email address of the developer. filetype templates, file header,
ChangeLog entry, bsd, gpl, snippets function description, ChangeLog entry,
bsd, gpl, snippets
company The company the developer is working for. filetypes, file header, function company The company the developer is working for. filetype templates, file header,
description, ChangeLog entry, bsd, function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
year [1]_ The current year. Default format is: YYYY filetypes, file header, function year [1]_ The current year. Default format is: YYYY filetype templates, file header,
description, ChangeLog entry, bsd, function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
version The initial version of a new file. filetypes, file header, function version The initial version of a new file. filetype templates, file header,
description, ChangeLog entry, bsd, function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
date [1]_ The current date. Default format: YYYY-MM-DD. filetypes, file header, function date [1]_ The current date. Default format: YYYY-MM-DD. filetype templates, file header,
description, ChangeLog entry, bsd, function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
untitled The string "untitled" (this will be filetypes, file header, function untitled The string "untitled" (this will be filetype templates, file header,
translated to your locale), used in description, ChangeLog entry, bsd, translated to your locale), used in function description, ChangeLog entry,
filetype templates. gpl, snippets filetype templates. bsd, gpl, snippets
geanyversion The actual Geany version, e.g. filetypes, file header, function geanyversion The actual Geany version, e.g. filetype templates, file header,
"Geany |(version)|". description, ChangeLog entry, bsd, "Geany |(version)|". function description, ChangeLog entry,
gpl, snippets bsd, gpl, snippets
datetime [1]_ The current date and time. Default format: file header, function description, datetime [1]_ The current date and time. Default format: filetype templates, file header,
DD.MM.YYYY HH:mm:ss ZZZZ. snippets DD.MM.YYYY HH:mm:ss ZZZZ. function description, ChangeLog entry,
bsd, gpl, snippets
filename The filename of the current file. file header, snippets filename The filename of the current file. file header, snippets
@ -3230,10 +3232,10 @@ functionname The function name of the function at the function descriptio
replaced in the function description replaced in the function description
template. template.
fileheader The file header template. This wildcard filetypes fileheader The file header template. This wildcard file header, snippets, custom filetype
will only be replaced in filetype will only be replaced in filetype templates
templates. templates.
============== ============================================= ============== ============== ============================================= =======================================
.. [1] The format for the ``year``, ``date`` and ``datetime`` wildcards can be changed .. [1] The format for the ``year``, ``date`` and ``datetime`` wildcards can be changed

View File

@ -1690,14 +1690,10 @@ static gchar *snippets_replace_wildcards(GeanyEditor *editor, gchar *text)
gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL); gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
gchar *basename = g_path_get_basename(DOC_FILENAME(editor->document)); gchar *basename = g_path_get_basename(DOC_FILENAME(editor->document));
text = templates_replace_all(text, year, date); text = templates_replace_all(text, year, date, datetime);
text = utils_str_replace(text, "{datetime}", datetime);
text = utils_str_replace(text, "{filename}", basename); text = utils_str_replace(text, "{filename}", basename);
g_free(year); utils_free_pointers(year, date, datetime, basename, NULL);
g_free(date);
g_free(datetime);
g_free(basename);
return text; return text;
} }

View File

@ -244,7 +244,7 @@ static gchar *ft_templates[GEANY_MAX_BUILT_IN_FILETYPES] = {NULL};
static void init_general_templates(const gchar *year, const gchar *date) static void init_general_templates(const gchar *year, const gchar *date, const gchar *datetime)
{ {
gchar *template_filename_fileheader = TEMPLATES_GET_FILENAME("fileheader"); gchar *template_filename_fileheader = TEMPLATES_GET_FILENAME("fileheader");
gchar *template_filename_gpl = TEMPLATES_GET_FILENAME("gpl"); gchar *template_filename_gpl = TEMPLATES_GET_FILENAME("gpl");
@ -261,19 +261,19 @@ static void init_general_templates(const gchar *year, const gchar *date)
/* read the contents */ /* read the contents */
TEMPLATES_READ_FILE(template_filename_fileheader, &templates[GEANY_TEMPLATE_FILEHEADER]); TEMPLATES_READ_FILE(template_filename_fileheader, &templates[GEANY_TEMPLATE_FILEHEADER]);
templates[GEANY_TEMPLATE_FILEHEADER] = templates_replace_all(templates[GEANY_TEMPLATE_FILEHEADER], year, date); templates[GEANY_TEMPLATE_FILEHEADER] = templates_replace_all(templates[GEANY_TEMPLATE_FILEHEADER], year, date, datetime);
TEMPLATES_READ_FILE(template_filename_gpl, &templates[GEANY_TEMPLATE_GPL]); TEMPLATES_READ_FILE(template_filename_gpl, &templates[GEANY_TEMPLATE_GPL]);
templates[GEANY_TEMPLATE_GPL] = templates_replace_all(templates[GEANY_TEMPLATE_GPL], year, date); templates[GEANY_TEMPLATE_GPL] = templates_replace_all(templates[GEANY_TEMPLATE_GPL], year, date, datetime);
TEMPLATES_READ_FILE(template_filename_bsd, &templates[GEANY_TEMPLATE_BSD]); TEMPLATES_READ_FILE(template_filename_bsd, &templates[GEANY_TEMPLATE_BSD]);
templates[GEANY_TEMPLATE_BSD] = templates_replace_all(templates[GEANY_TEMPLATE_BSD], year, date); templates[GEANY_TEMPLATE_BSD] = templates_replace_all(templates[GEANY_TEMPLATE_BSD], year, date, datetime);
TEMPLATES_READ_FILE(template_filename_function, &templates[GEANY_TEMPLATE_FUNCTION]); TEMPLATES_READ_FILE(template_filename_function, &templates[GEANY_TEMPLATE_FUNCTION]);
templates[GEANY_TEMPLATE_FUNCTION] = templates_replace_all(templates[GEANY_TEMPLATE_FUNCTION], year, date); templates[GEANY_TEMPLATE_FUNCTION] = templates_replace_all(templates[GEANY_TEMPLATE_FUNCTION], year, date, datetime);
TEMPLATES_READ_FILE(template_filename_changelog, &templates[GEANY_TEMPLATE_CHANGELOG]); TEMPLATES_READ_FILE(template_filename_changelog, &templates[GEANY_TEMPLATE_CHANGELOG]);
templates[GEANY_TEMPLATE_CHANGELOG] = templates_replace_all(templates[GEANY_TEMPLATE_CHANGELOG], year, date); templates[GEANY_TEMPLATE_CHANGELOG] = templates_replace_all(templates[GEANY_TEMPLATE_CHANGELOG], year, date, datetime);
/* free the whole stuff */ /* free the whole stuff */
g_free(template_filename_fileheader); g_free(template_filename_fileheader);
@ -284,7 +284,7 @@ static void init_general_templates(const gchar *year, const gchar *date)
} }
static void init_ft_templates(const gchar *year, const gchar *date) static void init_ft_templates(const gchar *year, const gchar *date, const gchar *datetime)
{ {
filetype_id ft_id; filetype_id ft_id;
@ -309,7 +309,7 @@ static void init_ft_templates(const gchar *year, const gchar *date)
default: break; default: break;
} }
TEMPLATES_READ_FILE(fname, &ft_templates[ft_id]); TEMPLATES_READ_FILE(fname, &ft_templates[ft_id]);
ft_templates[ft_id] = templates_replace_all(ft_templates[ft_id], year, date); ft_templates[ft_id] = templates_replace_all(ft_templates[ft_id], year, date, datetime);
g_free(fname); g_free(fname);
g_free(shortname); g_free(shortname);
@ -357,21 +357,28 @@ static void create_new_menu_items(void)
} }
static gchar *get_template_from_file(const gchar *locale_fname, GeanyFiletype *ft) static gchar *get_template_from_file(const gchar *locale_fname, const gchar *doc_filename,
GeanyFiletype *ft)
{ {
GString template = {NULL, 0, 0}; GString template = { NULL, 0, 0 };
g_file_get_contents(locale_fname, &template.str, &template.len, NULL); g_file_get_contents(locale_fname, &template.str, &template.len, NULL);
if (template.len > 0) if (template.len > 0)
{ {
gchar *file_header; gchar *file_header;
gchar *year = utils_get_date_time(template_prefs.year_format, NULL);
gchar *date = utils_get_date_time(template_prefs.date_format, NULL);
gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
template.allocated_len = template.len + 1; template.allocated_len = template.len + 1;
file_header = templates_get_template_fileheader(FILETYPE_ID(ft), NULL); file_header = templates_get_template_fileheader(FILETYPE_ID(ft), doc_filename);
template.str = templates_replace_all(template.str, year, date, datetime);
utils_string_replace_all(&template, "{filename}", doc_filename);
utils_string_replace_all(&template, "{fileheader}", file_header); utils_string_replace_all(&template, "{fileheader}", file_header);
g_free(file_header);
utils_free_pointers(year, date, datetime, file_header, NULL);
} }
return template.str; return template.str;
} }
@ -383,17 +390,20 @@ on_new_with_file_template(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_dat
gchar *fname = ui_menu_item_get_text(menuitem); gchar *fname = ui_menu_item_get_text(menuitem);
GeanyFiletype *ft; GeanyFiletype *ft;
gchar *template; gchar *template;
gchar *extension = strrchr(fname, '.'); /* easy way to get the file extension */
gchar *new_filename = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL);
ft = filetypes_detect_from_extension(fname); ft = filetypes_detect_from_extension(fname);
setptr(fname, utils_get_locale_from_utf8(fname)); setptr(fname, utils_get_locale_from_utf8(fname));
/* fname is just the basename from the menu item, so prepend the custom files path */ /* fname is just the basename from the menu item, so prepend the custom files path */
setptr(fname, g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_TEMPLATES_SUBDIR, setptr(fname, g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_TEMPLATES_SUBDIR,
"files", fname, NULL)); "files", fname, NULL));
template = get_template_from_file(fname, ft); template = get_template_from_file(fname, new_filename, ft);
g_free(fname); g_free(fname);
document_new_file(NULL, ft, template); document_new_file(new_filename, ft, template);
g_free(template); g_free(template);
g_free(new_filename);
} }
@ -484,9 +494,10 @@ void templates_init(void)
{ {
gchar *year = utils_get_date_time(template_prefs.year_format, NULL); gchar *year = utils_get_date_time(template_prefs.year_format, NULL);
gchar *date = utils_get_date_time(template_prefs.date_format, NULL); gchar *date = utils_get_date_time(template_prefs.date_format, NULL);
gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
init_general_templates(year, date); init_general_templates(year, date, datetime);
init_ft_templates(year, date); init_ft_templates(year, date, datetime);
g_free(date); g_free(date);
g_free(year); g_free(year);
@ -770,10 +781,12 @@ void templates_free_templates(void)
} }
gchar *templates_replace_all(gchar *text, const gchar *year, const gchar *date) gchar *templates_replace_all(gchar *text, const gchar *year, const gchar *date,
const gchar *datetime)
{ {
text = utils_str_replace(text, "{year}", year); text = utils_str_replace(text, "{year}", year);
text = utils_str_replace(text, "{date}", date); text = utils_str_replace(text, "{date}", date);
text = utils_str_replace(text, "{datetime}", datetime);
text = utils_str_replace(text, "{version}", template_prefs.version); text = utils_str_replace(text, "{version}", template_prefs.version);
text = utils_str_replace(text, "{initial}", template_prefs.initials); text = utils_str_replace(text, "{initial}", template_prefs.initials);
text = utils_str_replace(text, "{developer}", template_prefs.developer); text = utils_str_replace(text, "{developer}", template_prefs.developer);

View File

@ -67,7 +67,8 @@ gchar *templates_get_template_function(gint filetype_idx, const gchar *func_name
gchar *templates_get_template_licence(gint filetype_idx, gint licence_type); gchar *templates_get_template_licence(gint filetype_idx, gint licence_type);
gchar *templates_replace_all(gchar *source, const gchar *year, const gchar *date); gchar *templates_replace_all(gchar *source, const gchar *year, const gchar *date,
const gchar *datetime);
void templates_free_templates(void); void templates_free_templates(void);