2 * templates.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Templates to insert into the current document, or file templates to create a new
31 #include "templates.h"
35 #include "encodings.h"
36 #include "filetypes.h"
38 #include "geanymenubuttonaction.h"
39 #include "geanyobject.h"
46 #include "gtkcompat.h"
52 GeanyTemplatePrefs template_prefs
;
54 static GtkWidget
*new_with_template_menu
= NULL
;
55 static GtkWidget
*new_with_template_toolbar_menu
= NULL
;
57 /* TODO: implement custom insertion templates instead? */
58 static gchar
*templates
[GEANY_MAX_TEMPLATES
];
61 static void replace_static_values(GString
*text
);
62 static gchar
*get_template_fileheader(GeanyFiletype
*ft
);
64 /* called by templates_replace_common */
65 static void templates_replace_default_dates(GString
*text
);
66 static void templates_replace_command(GString
*text
, const gchar
*file_name
,
67 const gchar
*file_type
, const gchar
*func_name
);
70 static gchar
*read_file(const gchar
*locale_fname
)
76 if (! g_file_get_contents(locale_fname
, &contents
, &length
, NULL
))
79 if (! encodings_convert_to_utf8_auto(&contents
, &length
, NULL
, NULL
, NULL
, NULL
))
81 gchar
*utf8_fname
= utils_get_utf8_from_locale(locale_fname
);
83 ui_set_statusbar(TRUE
, _("Failed to convert template file \"%s\" to UTF-8"), utf8_fname
);
89 str
= g_string_new(contents
);
92 /* convert to LF endings for consistency in mixing templates */
93 utils_ensure_same_eol_characters(str
, SC_EOL_LF
);
94 return g_string_free(str
, FALSE
);
98 static void read_template(const gchar
*name
, gint id
)
100 gchar
*fname
= g_build_path(G_DIR_SEPARATOR_S
, app
->configdir
,
101 GEANY_TEMPLATES_SUBDIR
, name
, NULL
);
103 /* try system if user template doesn't exist */
104 if (!g_file_test(fname
, G_FILE_TEST_EXISTS
))
105 SETPTR(fname
, g_build_path(G_DIR_SEPARATOR_S
, app
->datadir
,
106 GEANY_TEMPLATES_SUBDIR
, name
, NULL
));
108 templates
[id
] = read_file(fname
);
113 /* called when inserting templates into an existing document */
114 static void convert_eol_characters(GString
*template, GeanyDocument
*doc
)
118 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
121 doc
= document_get_current();
123 g_return_if_fail(doc
!= NULL
);
125 doc_eol_mode
= editor_get_eol_char_mode(doc
->editor
);
126 utils_ensure_same_eol_characters(template, doc_eol_mode
);
130 static void init_general_templates(void)
132 /* read the contents */
133 read_template("fileheader", GEANY_TEMPLATE_FILEHEADER
);
134 read_template("gpl", GEANY_TEMPLATE_GPL
);
135 read_template("bsd", GEANY_TEMPLATE_BSD
);
136 read_template("function", GEANY_TEMPLATE_FUNCTION
);
137 read_template("changelog", GEANY_TEMPLATE_CHANGELOG
);
141 void templates_replace_common(GString
*tmpl
, const gchar
*fname
,
142 GeanyFiletype
*ft
, const gchar
*func_name
)
149 shortname
= g_strdup(GEANY_STRING_UNTITLED
);
151 shortname
= g_strconcat(GEANY_STRING_UNTITLED
, ".", ft
->extension
, NULL
);
154 shortname
= g_path_get_basename(fname
);
156 templates_replace_valist(tmpl
,
157 "{filename}", shortname
,
158 "{project}", app
->project
? app
->project
->name
: "",
159 "{description}", app
->project
? app
->project
->description
: "",
163 templates_replace_default_dates(tmpl
);
164 templates_replace_command(tmpl
, fname
, ft
->name
, func_name
);
165 /* Bug: command results could have {ob} {cb} strings in! */
166 /* replace braces last */
167 templates_replace_valist(tmpl
,
174 static gchar
*get_template_from_file(const gchar
*locale_fname
, const gchar
*doc_filename
,
178 GString
*template = NULL
;
180 content
= read_file(locale_fname
);
186 template = g_string_new(content
);
188 file_header
= get_template_fileheader(ft
);
189 templates_replace_valist(template,
190 "{fileheader}", file_header
,
192 templates_replace_common(template, doc_filename
, ft
, NULL
);
194 utils_free_pointers(2, file_header
, content
, NULL
);
195 return g_string_free(template, FALSE
);
202 on_new_with_file_template(GtkMenuItem
*menuitem
, G_GNUC_UNUSED gpointer user_data
)
204 gchar
*fname
= ui_menu_item_get_text(menuitem
);
207 const gchar
*extension
= strrchr(fname
, '.'); /* easy way to get the file extension */
208 gchar
*new_filename
= g_strconcat(GEANY_STRING_UNTITLED
, extension
, NULL
);
211 ft
= filetypes_detect_from_extension(fname
);
212 SETPTR(fname
, utils_get_locale_from_utf8(fname
));
214 /* fname is just the basename from the menu item, so prepend the custom files path */
215 path
= g_build_path(G_DIR_SEPARATOR_S
, app
->configdir
, GEANY_TEMPLATES_SUBDIR
,
216 "files", fname
, NULL
);
217 template = get_template_from_file(path
, new_filename
, ft
);
220 /* try the system path */
222 path
= g_build_path(G_DIR_SEPARATOR_S
, app
->datadir
, GEANY_TEMPLATES_SUBDIR
,
223 "files", fname
, NULL
);
224 template = get_template_from_file(path
, new_filename
, ft
);
228 /* line endings will be converted */
229 document_new_file(new_filename
, ft
, template);
233 SETPTR(fname
, utils_get_utf8_from_locale(fname
));
234 ui_set_statusbar(TRUE
, _("Could not find file '%s'."), fname
);
238 g_free(new_filename
);
243 static void add_file_item(const gchar
*fname
, GtkWidget
*menu
)
245 GtkWidget
*tmp_button
;
248 g_return_if_fail(fname
);
249 g_return_if_fail(menu
);
251 label
= utils_get_utf8_from_locale(fname
);
253 tmp_button
= gtk_menu_item_new_with_label(label
);
254 gtk_widget_show(tmp_button
);
255 gtk_container_add(GTK_CONTAINER(menu
), tmp_button
);
256 g_signal_connect(tmp_button
, "activate", G_CALLBACK(on_new_with_file_template
), NULL
);
262 static void populate_file_template_menu(GtkWidget
*menu
)
264 GSList
*list
= utils_get_config_files(GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S
"files");
267 foreach_slist(node
, list
)
269 gchar
*fname
= node
->data
;
271 add_file_item(fname
, menu
);
278 static void create_file_template_menu(void)
282 new_with_template_menu
= gtk_menu_new();
283 item
= ui_lookup_widget(main_widgets
.window
, "menu_new_with_template1");
284 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item
), new_with_template_menu
);
286 new_with_template_toolbar_menu
= gtk_menu_new();
287 g_object_ref(new_with_template_toolbar_menu
);
288 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")),
289 new_with_template_toolbar_menu
);
293 /* reload templates if any file in the templates path is saved */
294 static void on_document_save(G_GNUC_UNUSED GObject
*object
, GeanyDocument
*doc
)
298 g_return_if_fail(!EMPTY(doc
->real_path
));
300 path
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
, NULL
);
301 if (strncmp(doc
->real_path
, path
, strlen(path
)) == 0)
303 /* reload templates */
304 templates_free_templates();
311 /* warning: also called when reloading template settings */
312 void templates_init(void)
314 static gboolean init_done
= FALSE
;
316 init_general_templates();
320 create_file_template_menu();
321 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
325 populate_file_template_menu(new_with_template_menu
);
326 populate_file_template_menu(new_with_template_toolbar_menu
);
330 /* indent is used to make some whitespace between comment char and real start of the line
331 * e.g. indent = 8 prints " * here comes the text of the line"
332 * indent is meant to be the whole amount of characters before the real line content follows, i.e.
333 * 6 characters are filled with whitespace when the comment characters include " *" */
334 static void make_comment_block(GString
*comment_text
, gint filetype_idx
, guint indent
)
336 gchar
*frame_start
; /* to add before comment_text */
337 gchar
*frame_end
; /* to add after comment_text */
338 const gchar
*line_prefix
; /* to add before every line in comment_text */
343 gint template_eol_mode
;
344 const gchar
*template_eol_char
;
345 GeanyFiletype
*ft
= filetypes_index(filetype_idx
);
349 g_return_if_fail(comment_text
!= NULL
);
350 g_return_if_fail(ft
!= NULL
);
352 template_eol_mode
= utils_get_line_endings(comment_text
->str
, comment_text
->len
);
353 template_eol_char
= utils_get_eol_char(template_eol_mode
);
355 filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
);
360 frame_start
= g_strconcat(co
, template_eol_char
, NULL
);
361 frame_end
= g_strconcat(cc
, template_eol_char
, NULL
);
372 { /* use C-like multi-line comments as fallback */
373 frame_start
= g_strconcat("/*", template_eol_char
, NULL
);
374 frame_end
= g_strconcat("*/", template_eol_char
, NULL
);
378 /* do some magic to nicely format C-like multi-line comments */
379 if (!EMPTY(frame_start
) && frame_start
[1] == '*')
381 /* prefix the string with a space */
382 SETPTR(frame_end
, g_strconcat(" ", frame_end
, NULL
));
386 /* construct the real prefix with given amount of whitespace */
387 i
= (indent
> strlen(line_prefix
)) ? (indent
- strlen(line_prefix
)) : strlen(line_prefix
);
388 tmp
= g_strnfill(i
, ' ');
389 prefix
= g_strconcat(line_prefix
, tmp
, NULL
);
392 /* add line_prefix to every line of comment_text */
393 lines
= g_strsplit(comment_text
->str
, template_eol_char
, -1);
394 len
= g_strv_length(lines
);
395 if (len
> 0) /* prevent unsigned wraparound if comment_text is empty */
397 for (i
= 0; i
< len
- 1; i
++)
400 lines
[i
] = g_strconcat(prefix
, tmp
, NULL
);
404 tmp
= g_strjoinv(template_eol_char
, lines
);
406 /* clear old contents */
407 g_string_erase(comment_text
, 0, -1);
410 if (frame_start
!= NULL
)
411 g_string_append(comment_text
, frame_start
);
412 /* add the new main content */
413 g_string_append(comment_text
, tmp
);
414 /* add frame_start */
415 if (frame_end
!= NULL
)
416 g_string_append(comment_text
, frame_end
);
418 utils_free_pointers(4, prefix
, tmp
, frame_start
, frame_end
, NULL
);
423 gchar
*templates_get_template_licence(GeanyDocument
*doc
, gint licence_type
)
427 g_return_val_if_fail(DOC_VALID(doc
), NULL
);
428 g_return_val_if_fail(licence_type
== GEANY_TEMPLATE_GPL
|| licence_type
== GEANY_TEMPLATE_BSD
, NULL
);
430 template = g_string_new(templates
[licence_type
]);
431 replace_static_values(template);
432 templates_replace_default_dates(template);
433 templates_replace_command(template, DOC_FILENAME(doc
), doc
->file_type
->name
, NULL
);
435 make_comment_block(template, doc
->file_type
->id
, GEANY_TEMPLATES_INDENT
);
436 convert_eol_characters(template, doc
);
438 return g_string_free(template, FALSE
);
442 static gchar
*get_template_fileheader(GeanyFiletype
*ft
)
444 GString
*template = g_string_new(templates
[GEANY_TEMPLATE_FILEHEADER
]);
446 filetypes_load_config(ft
->id
, FALSE
); /* load any user extension setting */
448 templates_replace_valist(template,
449 "{gpl}", templates
[GEANY_TEMPLATE_GPL
],
450 "{bsd}", templates
[GEANY_TEMPLATE_BSD
],
453 /* we don't replace other wildcards here otherwise they would get done twice for files */
454 make_comment_block(template, ft
->id
, GEANY_TEMPLATES_INDENT
);
455 return g_string_free(template, FALSE
);
459 /* TODO change the signature to take a GeanyDocument? this would break plugin API/ABI */
461 gchar
*templates_get_template_fileheader(gint filetype_idx
, const gchar
*fname
)
463 GeanyFiletype
*ft
= filetypes
[filetype_idx
];
464 gchar
*str
= get_template_fileheader(ft
);
465 GString
*template = g_string_new(str
);
468 templates_replace_common(template, fname
, ft
, NULL
);
469 convert_eol_characters(template, NULL
);
470 return g_string_free(template, FALSE
);
474 gchar
*templates_get_template_function(GeanyDocument
*doc
, const gchar
*func_name
)
478 func_name
= (func_name
!= NULL
) ? func_name
: "";
479 text
= g_string_new(templates
[GEANY_TEMPLATE_FUNCTION
]);
481 templates_replace_valist(text
, "{functionname}", func_name
, NULL
);
482 templates_replace_default_dates(text
);
483 templates_replace_command(text
, DOC_FILENAME(doc
), doc
->file_type
->name
, func_name
);
485 make_comment_block(text
, doc
->file_type
->id
, GEANY_TEMPLATES_INDENT
);
486 convert_eol_characters(text
, doc
);
488 return g_string_free(text
, FALSE
);
492 gchar
*templates_get_template_changelog(GeanyDocument
*doc
)
495 const gchar
*file_type_name
;
497 g_return_val_if_fail(DOC_VALID(doc
), NULL
);
499 result
= g_string_new(templates
[GEANY_TEMPLATE_CHANGELOG
]);
500 file_type_name
= (doc
->file_type
!= NULL
) ? doc
->file_type
->name
: "";
501 replace_static_values(result
);
502 templates_replace_default_dates(result
);
503 templates_replace_command(result
, DOC_FILENAME(doc
), file_type_name
, NULL
);
504 convert_eol_characters(result
, doc
);
506 return g_string_free(result
, FALSE
);
510 static void free_template_menu_items(GtkWidget
*menu
)
512 GList
*children
, *item
;
514 children
= gtk_container_get_children(GTK_CONTAINER(menu
));
515 foreach_list(item
, children
)
516 gtk_widget_destroy(GTK_WIDGET(item
->data
));
517 g_list_free(children
);
521 void templates_free_templates(void)
525 for (i
= 0; i
< GEANY_MAX_TEMPLATES
; i
++)
526 g_free(templates
[i
]);
527 free_template_menu_items(new_with_template_menu
);
528 free_template_menu_items(new_with_template_toolbar_menu
);
532 static void replace_static_values(GString
*text
)
534 utils_string_replace_all(text
, "{version}", template_prefs
.version
);
535 utils_string_replace_all(text
, "{initial}", template_prefs
.initials
);
536 utils_string_replace_all(text
, "{developer}", template_prefs
.developer
);
537 utils_string_replace_all(text
, "{mail}", template_prefs
.mail
);
538 utils_string_replace_all(text
, "{company}", template_prefs
.company
);
539 utils_string_replace_all(text
, "{untitled}", GEANY_STRING_UNTITLED
);
540 utils_string_replace_all(text
, "{geanyversion}", "Geany " VERSION
);
544 /* Replaces all static template wildcards (version, mail, company, name, ...)
545 * plus those wildcard, value pairs which are passed, e.g.
547 * templates_replace_valist(text, "{some_wildcard}", "some value",
548 * "{another_wildcard}", "another value", NULL);
550 * The argument list must be terminated with NULL. */
551 void templates_replace_valist(GString
*text
, const gchar
*first_wildcard
, ...)
554 const gchar
*key
, *value
;
556 g_return_if_fail(text
!= NULL
);
558 va_start(args
, first_wildcard
);
560 key
= first_wildcard
;
561 value
= va_arg(args
, gchar
*);
565 utils_string_replace_all(text
, key
, value
);
567 key
= va_arg(args
, gchar
*);
568 if (key
== NULL
|| text
== NULL
)
570 value
= va_arg(args
, gchar
*);
574 replace_static_values(text
);
578 static void templates_replace_default_dates(GString
*text
)
580 gchar
*year
= utils_get_date_time(template_prefs
.year_format
, NULL
);
581 gchar
*date
= utils_get_date_time(template_prefs
.date_format
, NULL
);
582 gchar
*datetime
= utils_get_date_time(template_prefs
.datetime_format
, NULL
);
584 g_return_if_fail(text
!= NULL
);
586 templates_replace_valist(text
,
589 "{datetime}", datetime
,
592 utils_free_pointers(3, year
, date
, datetime
, NULL
);
596 static gchar
*run_command(const gchar
*command
, const gchar
*file_name
,
597 const gchar
*file_type
, const gchar
*func_name
)
599 GString
*output
= g_string_new(NULL
);
600 gchar
*result
= NULL
;
601 GError
*error
= NULL
;
604 file_name
= (file_name
!= NULL
) ? file_name
: "";
605 file_type
= (file_type
!= NULL
) ? file_type
: "";
606 func_name
= (func_name
!= NULL
) ? func_name
: "";
608 env
= utils_copy_environment(NULL
,
609 "GEANY_FILENAME", file_name
,
610 "GEANY_FILETYPE", file_type
,
611 "GEANY_FUNCNAME", func_name
,
614 if (spawn_sync(NULL
, command
, NULL
, env
, NULL
, output
, NULL
, NULL
, &error
))
616 result
= g_string_free(output
, FALSE
);
620 g_warning(_("Cannot execute command \"%s\" from the template: %s. "
621 "Check the path in the template."), command
, error
->message
);
630 static void templates_replace_command(GString
*text
, const gchar
*file_name
,
631 const gchar
*file_type
, const gchar
*func_name
)
634 gchar
*wildcard
= NULL
;
638 g_return_if_fail(text
!= NULL
);
640 while ((match
= strstr(text
->str
, "{command:")) != NULL
)
643 while (*match
!= '}' && *match
!= '\0')
646 wildcard
= g_strndup(cmd
, (gsize
) (match
- cmd
+ 1));
647 cmd
= g_strndup(wildcard
+ 9, strlen(wildcard
) - 10);
649 result
= run_command(cmd
, file_name
, file_type
, func_name
);
652 result
= g_strstrip(result
);
653 utils_string_replace_first(text
, wildcard
, result
);
657 utils_string_replace_first(text
, wildcard
, "");