If the current word's tag is on the current line, make Go to Tag
[geany-mirror.git] / src / templates.c
blobad37b2c924508e610e3dc139d01d208bc7ab3080
1 /*
2 * templates.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
25 * Templates to insert into the current document, or filetype templates to create a new
26 * document from.
29 #include <time.h>
30 #include <string.h>
32 #include "geany.h"
34 #include "templates.h"
35 #include "support.h"
36 #include "utils.h"
37 #include "document.h"
38 #include "editor.h"
39 #include "filetypes.h"
40 #include "ui_utils.h"
41 #include "toolbar.h"
42 #include "geanymenubuttonaction.h"
43 #include "project.h"
46 GeanyTemplatePrefs template_prefs;
48 static GtkWidget *new_with_template_menu = NULL; /* submenu used for both file menu and toolbar */
50 /* TODO: implement custom insertion templates instead? */
51 static gchar *templates[GEANY_MAX_TEMPLATES];
53 /* We should probably remove filetype templates support soon - users can use custom
54 * file templates instead. */
55 static gchar *ft_templates[GEANY_MAX_BUILT_IN_FILETYPES] = {NULL};
58 static void replace_static_values(GString *text);
59 static gchar *get_template_fileheader(GeanyFiletype *ft);
61 /* called by templates_replace_common */
62 static void templates_replace_default_dates(GString *text);
63 static void templates_replace_command(GString *text, const gchar *file_name,
64 const gchar *file_type, const gchar *func_name);
67 /* some simple macros to reduce code size and make the code readable */
68 #define TEMPLATES_GET_FILENAME(shortname) \
69 g_strconcat(app->configdir, \
70 G_DIR_SEPARATOR_S GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S, shortname, NULL)
73 static gchar *read_file(const gchar *locale_fname)
75 gchar *contents;
76 GString *str;
78 if (!g_file_get_contents(locale_fname, &contents, NULL, NULL))
79 return NULL;
81 str = g_string_new(contents);
82 g_free(contents);
84 /* convert to LF endings for consistency in mixing templates */
85 utils_ensure_same_eol_characters(str, SC_EOL_LF);
86 return g_string_free(str, FALSE);
90 static void read_template(const gchar *name, gint id)
92 gchar *fname = TEMPLATES_GET_FILENAME(name);
94 /* try system if user template doesn't exist */
95 if (!g_file_test(fname, G_FILE_TEST_EXISTS))
96 setptr(fname, g_strconcat(app->datadir,
97 G_DIR_SEPARATOR_S GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S, name, NULL));
99 templates[id] = read_file(fname);
100 g_free(fname);
104 /* FIXME the callers should use GStrings instead of char arrays */
105 static gchar *replace_all(gchar *text, const gchar *year, const gchar *date, const gchar *datetime)
107 GString *str;
109 if (text == NULL)
110 return NULL;
112 str = g_string_new(text);
114 g_free(text);
115 templates_replace_valist(str,
116 "{year}", year,
117 "{date}", date,
118 "{datetime}", datetime,
119 NULL);
121 return g_string_free(str, FALSE);
125 /* called when inserting templates into an existing document */
126 static void convert_eol_characters(GString *template, GeanyDocument *doc)
128 gint doc_eol_mode;
130 if (doc == NULL)
131 doc = document_get_current();
133 g_return_if_fail(doc != NULL);
135 doc_eol_mode = editor_get_eol_char_mode(doc->editor);
136 utils_ensure_same_eol_characters(template, doc_eol_mode);
140 static void init_general_templates(const gchar *year, const gchar *date, const gchar *datetime)
142 guint id;
144 /* read the contents */
145 read_template("fileheader", GEANY_TEMPLATE_FILEHEADER);
146 read_template("gpl", GEANY_TEMPLATE_GPL);
147 read_template("bsd", GEANY_TEMPLATE_BSD);
148 read_template("function", GEANY_TEMPLATE_FUNCTION);
149 read_template("changelog", GEANY_TEMPLATE_CHANGELOG);
151 /* FIXME: we should replace the dates on insertion, not on loading */
152 for (id = 0; id < GEANY_MAX_TEMPLATES; id++)
153 templates[id] = replace_all(templates[id], year, date, datetime);
157 static void init_ft_templates(const gchar *year, const gchar *date, const gchar *datetime)
159 filetype_id ft_id;
161 for (ft_id = 0; ft_id < GEANY_MAX_BUILT_IN_FILETYPES; ft_id++)
163 gchar *ext = (ft_id != GEANY_FILETYPES_NONE) ?
164 filetypes_get_conf_extension(ft_id) : g_strdup("none");
165 gchar *shortname = g_strconcat("filetype.", ext, NULL);
166 gchar *fname = TEMPLATES_GET_FILENAME(shortname);
168 ft_templates[ft_id] = read_file(fname);
169 ft_templates[ft_id] = replace_all(ft_templates[ft_id], year, date, datetime);
171 g_free(fname);
172 g_free(shortname);
173 g_free(ext);
178 static void
179 on_new_with_filetype_template(GtkMenuItem *menuitem, gpointer user_data)
181 GeanyFiletype *ft = user_data;
182 gchar *template = templates_get_template_new_file(ft);
184 document_new_file(NULL, ft, template);
185 g_free(template);
189 /* TODO: remove filetype template support after 0.19 */
190 static gboolean create_new_filetype_items(void)
192 GSList *node;
193 gboolean ret = FALSE;
194 GtkWidget *menu = NULL;
196 foreach_slist(node, filetypes_by_title)
198 GeanyFiletype *ft = node->data;
199 GtkWidget *item;
201 if (ft->id >= GEANY_MAX_BUILT_IN_FILETYPES || ft_templates[ft->id] == NULL)
202 continue;
204 if (!menu)
206 item = gtk_menu_item_new_with_label(_("Old"));
207 menu = gtk_menu_new();
208 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), menu);
209 gtk_widget_show_all(item);
210 gtk_container_add(GTK_CONTAINER(new_with_template_menu), item);
212 item = gtk_menu_item_new_with_label(ft->title);
213 gtk_widget_show(item);
214 gtk_container_add(GTK_CONTAINER(menu), item);
215 g_signal_connect(item, "activate", G_CALLBACK(on_new_with_filetype_template), ft);
216 ret = TRUE;
218 return ret;
222 void templates_replace_common(GString *template, const gchar *fname,
223 GeanyFiletype *ft, const gchar *func_name)
225 gchar *shortname;
227 if (fname == NULL)
229 if (!ft->extension)
230 shortname = g_strdup(GEANY_STRING_UNTITLED);
231 else
232 shortname = g_strconcat(GEANY_STRING_UNTITLED, ".", ft->extension, NULL);
234 else
235 shortname = g_path_get_basename(fname);
237 templates_replace_valist(template,
238 "{filename}", shortname,
239 "{project}", app->project ? app->project->name : "",
240 "{description}", app->project ? app->project->description : "",
241 NULL);
242 g_free(shortname);
244 templates_replace_default_dates(template);
245 templates_replace_command(template, fname, ft->name, func_name);
246 /* Bug: command results could have {ob} {cb} strings in! */
247 /* replace braces last */
248 templates_replace_valist(template,
249 "{ob}", "{",
250 "{cb}", "}",
251 NULL);
255 static gchar *get_template_from_file(const gchar *locale_fname, const gchar *doc_filename,
256 GeanyFiletype *ft)
258 gchar *content;
259 GString *template = NULL;
261 content = read_file(locale_fname);
263 if (content != NULL)
265 gchar *file_header;
267 template = g_string_new(content);
269 file_header = get_template_fileheader(ft);
270 templates_replace_valist(template,
271 "{fileheader}", file_header,
272 NULL);
273 templates_replace_common(template, doc_filename, ft, NULL);
275 utils_free_pointers(2, file_header, content, NULL);
276 return g_string_free(template, FALSE);
278 return NULL;
282 static void
283 on_new_with_file_template(GtkMenuItem *menuitem, G_GNUC_UNUSED gpointer user_data)
285 gchar *fname = ui_menu_item_get_text(menuitem);
286 GeanyFiletype *ft;
287 gchar *template;
288 const gchar *extension = strrchr(fname, '.'); /* easy way to get the file extension */
289 gchar *new_filename = g_strconcat(GEANY_STRING_UNTITLED, extension, NULL);
290 gchar *path;
292 ft = filetypes_detect_from_extension(fname);
293 setptr(fname, utils_get_locale_from_utf8(fname));
295 /* fname is just the basename from the menu item, so prepend the custom files path */
296 path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_TEMPLATES_SUBDIR,
297 "files", fname, NULL);
298 template = get_template_from_file(path, new_filename, ft);
299 if (!template)
301 /* try the system path */
302 g_free(path);
303 path = g_build_path(G_DIR_SEPARATOR_S, app->datadir, GEANY_TEMPLATES_SUBDIR,
304 "files", fname, NULL);
305 template = get_template_from_file(path, new_filename, ft);
307 if (template)
309 /* line endings will be converted */
310 document_new_file(new_filename, ft, template);
312 else
314 setptr(fname, utils_get_utf8_from_locale(fname));
315 ui_set_statusbar(TRUE, _("Could not find file '%s'."), fname);
317 g_free(template);
318 g_free(path);
319 g_free(new_filename);
320 g_free(fname);
324 static void add_file_item(const gchar *fname, GtkWidget *menu)
326 GtkWidget *tmp_button;
327 gchar *label;
329 g_return_if_fail(fname);
330 g_return_if_fail(menu);
332 label = utils_get_utf8_from_locale(fname);
334 tmp_button = gtk_menu_item_new_with_label(label);
335 gtk_widget_show(tmp_button);
336 gtk_container_add(GTK_CONTAINER(menu), tmp_button);
337 g_signal_connect(tmp_button, "activate", G_CALLBACK(on_new_with_file_template), NULL);
339 g_free(label);
343 static gboolean add_custom_template_items(void)
345 GSList *list = utils_get_config_files(GEANY_TEMPLATES_SUBDIR G_DIR_SEPARATOR_S "files");
346 GSList *node;
348 foreach_slist(node, list)
350 gchar *fname = node->data;
352 add_file_item(fname, new_with_template_menu);
353 g_free(fname);
355 g_slist_free(list);
356 return list != NULL;
360 static void create_file_template_menu(void)
362 GtkWidget *sep = NULL;
364 new_with_template_menu = gtk_menu_new();
366 if (add_custom_template_items())
368 sep = gtk_separator_menu_item_new();
369 gtk_container_add(GTK_CONTAINER(new_with_template_menu), sep);
371 if (create_new_filetype_items() && sep)
373 gtk_widget_show(sep);
375 /* unless the file menu is showing, menu should be in the toolbar widget */
376 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(
377 toolbar_get_action_by_name("New")), new_with_template_menu);
381 static void on_file_menu_show(GtkWidget *item)
383 geany_menu_button_action_set_menu(
384 GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), NULL);
385 item = ui_lookup_widget(main_widgets.window, "menu_new_with_template1");
386 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), new_with_template_menu);
390 static void on_file_menu_hide(GtkWidget *item)
392 item = ui_lookup_widget(main_widgets.window, "menu_new_with_template1");
393 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), NULL);
394 geany_menu_button_action_set_menu(
395 GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), new_with_template_menu);
399 /* reload templates if any file in the templates path is saved */
400 static void on_document_save(G_GNUC_UNUSED GObject *object, GeanyDocument *doc)
402 const gchar *path = utils_build_path(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
404 g_return_if_fail(NZV(doc->real_path));
406 if (strncmp(doc->real_path, path, strlen(path)) == 0)
408 /* reload templates */
409 templates_free_templates();
410 templates_init();
415 /* warning: also called when reloading template settings */
416 void templates_init(void)
418 gchar *year = utils_get_date_time(template_prefs.year_format, NULL);
419 gchar *date = utils_get_date_time(template_prefs.date_format, NULL);
420 gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
421 static gboolean init_done = FALSE;
423 init_general_templates(year, date, datetime);
424 init_ft_templates(year, date, datetime);
426 g_free(date);
427 g_free(datetime);
428 g_free(year);
430 create_file_template_menu();
431 /* we hold our own ref for the menu as it has no parent whilst being moved */
432 g_object_ref(new_with_template_menu);
434 /* only connect signals to persistent objects once */
435 if (!init_done)
437 GtkWidget *item;
438 /* reparent the template menu as needed */
439 item = ui_lookup_widget(main_widgets.window, "file1");
440 item = gtk_menu_item_get_submenu(GTK_MENU_ITEM(item));
441 g_signal_connect(item, "show", G_CALLBACK(on_file_menu_show), NULL);
442 g_signal_connect(item, "hide", G_CALLBACK(on_file_menu_hide), NULL);
444 g_signal_connect(geany_object, "document-save", G_CALLBACK(on_document_save), NULL);
446 init_done = TRUE;
450 /* indent is used to make some whitespace between comment char and real start of the line
451 * e.g. indent = 8 prints " * here comes the text of the line"
452 * indent is meant to be the whole amount of characters before the real line content follows, i.e.
453 * 6 characters are filled with whitespace when the comment characters include " *" */
454 static void make_comment_block(GString *comment_text, gint filetype_idx, guint indent)
456 gchar *frame_start; /* to add before comment_text */
457 gchar *frame_end; /* to add after comment_text */
458 const gchar *line_prefix; /* to add before every line in comment_text */
459 gchar *tmp;
460 gchar *prefix;
461 gchar **lines;
462 guint i, len;
463 gint template_eol_mode;
464 const gchar *template_eol_char;
465 GeanyFiletype *ft = filetypes_index(filetype_idx);
467 g_return_if_fail(comment_text != NULL);
468 g_return_if_fail(ft != NULL);
470 template_eol_mode = utils_get_line_endings(comment_text->str, comment_text->len);
471 template_eol_char = utils_get_eol_char(template_eol_mode);
473 if (NZV(ft->comment_open))
475 if (NZV(ft->comment_close))
477 frame_start = g_strconcat(ft->comment_open, template_eol_char, NULL);
478 frame_end = g_strconcat(ft->comment_close, template_eol_char, NULL);
479 line_prefix = "";
481 else
483 frame_start = NULL;
484 frame_end = NULL;
485 line_prefix = ft->comment_open;
488 else
489 { /* use C-like multi-line comments as fallback */
490 frame_start = g_strconcat("/*", template_eol_char, NULL);
491 frame_end = g_strconcat("*/", template_eol_char, NULL);
492 line_prefix = "";
495 /* do some magic to nicely format C-like multi-line comments */
496 if (NZV(frame_start) && frame_start[1] == '*')
498 /* prefix the string with a space */
499 setptr(frame_end, g_strconcat(" ", frame_end, NULL));
500 line_prefix = " *";
503 /* construct the real prefix with given amount of whitespace */
504 i = (indent > strlen(line_prefix)) ? (indent - strlen(line_prefix)) : strlen(line_prefix);
505 tmp = g_strnfill(i, ' ');
506 prefix = g_strconcat(line_prefix, tmp, NULL);
507 g_free(tmp);
509 /* add line_prefix to every line of comment_text */
510 lines = g_strsplit(comment_text->str, template_eol_char, -1);
511 len = g_strv_length(lines) - 1;
512 for (i = 0; i < len; i++)
514 tmp = lines[i];
515 lines[i] = g_strconcat(prefix, tmp, NULL);
516 g_free(tmp);
518 tmp = g_strjoinv(template_eol_char, lines);
520 /* clear old contents */
521 g_string_erase(comment_text, 0, -1);
523 /* add frame_end */
524 if (frame_start != NULL)
525 g_string_append(comment_text, frame_start);
526 /* add the new main content */
527 g_string_append(comment_text, tmp);
528 /* add frame_start */
529 if (frame_end != NULL)
530 g_string_append(comment_text, frame_end);
532 utils_free_pointers(4, prefix, tmp, frame_start, frame_end, NULL);
533 g_strfreev(lines);
537 gchar *templates_get_template_licence(GeanyDocument *doc, gint licence_type)
539 GString *template;
541 g_return_val_if_fail(doc != NULL, NULL);
542 g_return_val_if_fail(licence_type == GEANY_TEMPLATE_GPL || licence_type == GEANY_TEMPLATE_BSD, NULL);
544 template = g_string_new(templates[licence_type]);
545 replace_static_values(template);
546 templates_replace_default_dates(template);
547 templates_replace_command(template, DOC_FILENAME(doc), doc->file_type->name, NULL);
549 make_comment_block(template, doc->file_type->id, 8);
550 convert_eol_characters(template, doc);
552 return g_string_free(template, FALSE);
556 static gchar *get_template_fileheader(GeanyFiletype *ft)
558 GString *template = g_string_new(templates[GEANY_TEMPLATE_FILEHEADER]);
560 filetypes_load_config(ft->id, FALSE); /* load any user extension setting */
562 templates_replace_valist(template,
563 "{gpl}", templates[GEANY_TEMPLATE_GPL],
564 "{bsd}", templates[GEANY_TEMPLATE_BSD],
565 NULL);
567 /* we don't replace other wildcards here otherwise they would get done twice for files */
568 make_comment_block(template, ft->id, 8);
569 return g_string_free(template, FALSE);
573 /* TODO change the signature to take a GeanyDocument? this would break plugin API/ABI */
574 gchar *templates_get_template_fileheader(gint filetype_idx, const gchar *fname)
576 GeanyFiletype *ft = filetypes[filetype_idx];
577 gchar *str = get_template_fileheader(ft);
578 GString *template = g_string_new(str);
580 g_free(str);
581 templates_replace_common(template, fname, ft, NULL);
582 convert_eol_characters(template, NULL);
583 return g_string_free(template, FALSE);
587 /* old filetype templates - use file templates instead */
588 gchar *templates_get_template_new_file(GeanyFiletype *ft)
590 GString *ft_template;
591 gchar *file_header = NULL;
593 g_return_val_if_fail(ft != NULL, NULL);
594 g_return_val_if_fail(ft->id < GEANY_MAX_BUILT_IN_FILETYPES, NULL);
596 ft_template = g_string_new(ft_templates[ft->id]);
597 if (FILETYPE_ID(ft) == GEANY_FILETYPES_NONE)
599 replace_static_values(ft_template);
601 else
602 { /* file template only used for new files */
603 file_header = get_template_fileheader(ft);
604 templates_replace_valist(ft_template, "{fileheader}", file_header, NULL);
606 templates_replace_common(ft_template, NULL, ft, NULL);
607 convert_eol_characters(ft_template, NULL);
609 g_free(file_header);
610 return g_string_free(ft_template, FALSE);
614 gchar *templates_get_template_function(GeanyDocument *doc, const gchar *func_name)
616 GString *text;
618 func_name = (func_name != NULL) ? func_name : "";
619 text = g_string_new(templates[GEANY_TEMPLATE_FUNCTION]);
621 templates_replace_valist(text, "{functionname}", func_name, NULL);
622 templates_replace_default_dates(text);
623 templates_replace_command(text, DOC_FILENAME(doc), doc->file_type->name, func_name);
625 make_comment_block(text, doc->file_type->id, 3);
626 convert_eol_characters(text, doc);
628 return g_string_free(text, FALSE);
632 gchar *templates_get_template_changelog(GeanyDocument *doc)
634 GString *result = g_string_new(templates[GEANY_TEMPLATE_CHANGELOG]);
635 const gchar *file_type_name = (doc != NULL) ? doc->file_type->name : "";
637 replace_static_values(result);
638 templates_replace_default_dates(result);
639 templates_replace_command(result, DOC_FILENAME(doc), file_type_name, NULL);
640 convert_eol_characters(result, doc);
642 return g_string_free(result, FALSE);
646 void templates_free_templates(void)
648 gint i;
649 GList *children, *item;
651 /* disconnect the menu from the action widget, so destroying the items below doesn't
652 * trigger rebuilding of the menu on each item destroy */
653 geany_menu_button_action_set_menu(
654 GEANY_MENU_BUTTON_ACTION(toolbar_get_action_by_name("New")), NULL);
656 for (i = 0; i < GEANY_MAX_TEMPLATES; i++)
658 g_free(templates[i]);
660 for (i = 0; i < GEANY_MAX_BUILT_IN_FILETYPES; i++)
662 g_free(ft_templates[i]);
664 /* destroy "New with template" sub menu items (in case we want to reload the templates) */
665 children = gtk_container_get_children(GTK_CONTAINER(new_with_template_menu));
666 foreach_list(item, children)
668 gtk_widget_destroy(GTK_WIDGET(item->data));
670 g_list_free(children);
672 g_object_unref(new_with_template_menu);
673 new_with_template_menu = NULL;
677 static void replace_static_values(GString *text)
679 utils_string_replace_all(text, "{version}", template_prefs.version);
680 utils_string_replace_all(text, "{initial}", template_prefs.initials);
681 utils_string_replace_all(text, "{developer}", template_prefs.developer);
682 utils_string_replace_all(text, "{mail}", template_prefs.mail);
683 utils_string_replace_all(text, "{company}", template_prefs.company);
684 utils_string_replace_all(text, "{untitled}", GEANY_STRING_UNTITLED);
685 utils_string_replace_all(text, "{geanyversion}", "Geany " VERSION);
689 /* Replaces all static template wildcards (version, mail, company, name, ...)
690 * plus those wildcard, value pairs which are passed, e.g.
692 * templates_replace_valist(text, "{some_wildcard}", "some value",
693 * "{another_wildcard}", "another value", NULL);
695 * The argument list must be terminated with NULL. */
696 void templates_replace_valist(GString *text, const gchar *first_wildcard, ...)
698 va_list args;
699 const gchar *key, *value;
701 g_return_if_fail(text != NULL);
703 va_start(args, first_wildcard);
705 key = first_wildcard;
706 value = va_arg(args, gchar*);
708 while (key != NULL)
710 utils_string_replace_all(text, key, value);
712 key = va_arg(args, gchar*);
713 if (key == NULL || text == NULL)
714 break;
715 value = va_arg(args, gchar*);
717 va_end(args);
719 replace_static_values(text);
723 static void templates_replace_default_dates(GString *text)
725 gchar *year = utils_get_date_time(template_prefs.year_format, NULL);
726 gchar *date = utils_get_date_time(template_prefs.date_format, NULL);
727 gchar *datetime = utils_get_date_time(template_prefs.datetime_format, NULL);
729 g_return_if_fail(text != NULL);
731 templates_replace_valist(text,
732 "{year}", year,
733 "{date}", date,
734 "{datetime}", datetime,
735 NULL);
737 utils_free_pointers(3, year, date, datetime, NULL);
741 static gchar *run_command(const gchar *command, const gchar *file_name,
742 const gchar *file_type, const gchar *func_name)
744 gchar *result = NULL;
745 gchar **argv;
747 if (g_shell_parse_argv(command, NULL, &argv, NULL))
749 GError *error = NULL;
750 gchar **env;
752 file_name = (file_name != NULL) ? file_name : "";
753 file_type = (file_type != NULL) ? file_type : "";
754 func_name = (func_name != NULL) ? func_name : "";
756 env = utils_copy_environment(NULL,
757 "GEANY_FILENAME", file_name,
758 "GEANY_FILETYPE", file_type,
759 "GEANY_FUNCNAME", func_name,
760 NULL);
761 if (! utils_spawn_sync(NULL, argv, env, G_SPAWN_SEARCH_PATH,
762 NULL, NULL, &result, NULL, NULL, &error))
764 g_warning("templates_replace_command: %s", error->message);
765 g_error_free(error);
766 return NULL;
768 g_strfreev(argv);
769 g_strfreev(env);
771 return result;
775 static void templates_replace_command(GString *text, const gchar *file_name,
776 const gchar *file_type, const gchar *func_name)
778 gchar *match = NULL;
779 gchar *wildcard = NULL;
780 gchar *cmd;
781 gchar *result;
783 g_return_if_fail(text != NULL);
785 while ((match = strstr(text->str, "{command:")) != NULL)
787 cmd = match;
788 while (*match != '}' && *match != '\0')
789 match++;
791 wildcard = g_strndup(cmd, match - cmd + 1);
792 cmd = g_strndup(wildcard + 9, strlen(wildcard) - 10);
794 result = run_command(cmd, file_name, file_type, func_name);
795 if (result != NULL)
797 result = g_strstrip(result);
798 utils_string_replace_first(text, wildcard, result);
799 g_free(result);
801 else
802 utils_string_replace_first(text, wildcard, "");
804 g_free(wildcard);
805 g_free(cmd);