Merge pull request #613 from SiegeLord/even_more_rust_updates
[geany-mirror.git] / src / build.c
blob6703b16ce55a736bc84b4b443ee0ef17fa175561
1 /*
2 * build.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>
6 * Copyright 2009 Lex Trotman <elextr(at)gmail(dot)com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Build commands and menu items.
26 /* TODO: tidy code:
27 * Use intermediate pointers for common subexpressions.
28 * Replace defines with enums.
29 * Other TODOs in code. */
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include "app.h"
36 #include "build.h"
37 #include "dialogs.h"
38 #include "document.h"
39 #include "filetypesprivate.h"
40 #include "geanymenubuttonaction.h"
41 #include "geanyobject.h"
42 #include "keybindingsprivate.h"
43 #include "msgwindow.h"
44 #include "prefs.h"
45 #include "projectprivate.h"
46 #include "sciwrappers.h"
47 #include "spawn.h"
48 #include "support.h"
49 #include "toolbar.h"
50 #include "ui_utils.h"
51 #include "utils.h"
52 #include "vte.h"
53 #include "win32.h"
55 #include "gtkcompat.h"
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sys/stat.h>
60 #include <unistd.h>
61 #include <errno.h>
62 #include <glib/gstdio.h>
66 /* Number of editor indicators to draw - limited as this can affect performance */
67 #define GEANY_BUILD_ERR_HIGHLIGHT_MAX 50
70 GeanyBuildInfo build_info = {GEANY_GBG_FT, 0, 0, NULL, GEANY_FILETYPES_NONE, NULL, 0};
72 static gchar *current_dir_entered = NULL;
74 typedef struct RunInfo
76 GPid pid;
77 gint file_type_id;
78 } RunInfo;
80 static RunInfo *run_info;
82 #ifdef G_OS_WIN32
83 static const gchar RUN_SCRIPT_CMD[] = "geany_run_script_XXXXXX.bat";
84 #else
85 static const gchar RUN_SCRIPT_CMD[] = "geany_run_script_XXXXXX.sh";
86 #endif
88 /* pack group (<8) and command (<32) into a user_data pointer */
89 #define GRP_CMD_TO_POINTER(grp, cmd) GUINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
90 #define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
91 #define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_UINT(gptr)&0x1f)
92 #define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_UINT(gptr)&0xe0) >> 5)
94 static gpointer last_toolbutton_action = GBO_TO_POINTER(GEANY_GBO_BUILD);
96 static BuildMenuItems menu_items = {NULL, {NULL, NULL, NULL, NULL}};
98 static struct
100 GtkAction *run_action;
101 GtkAction *compile_action;
102 GtkAction *build_action;
103 GtkWidget *toolmenu;
105 GtkWidget *toolitem_build;
106 GtkWidget *toolitem_make_all;
107 GtkWidget *toolitem_make_custom;
108 GtkWidget *toolitem_make_object;
109 GtkWidget *toolitem_set_args;
111 widgets;
113 static guint build_groups_count[GEANY_GBG_COUNT] = { 3, 4, 2 };
114 static guint build_items_count = 9;
116 static void build_exit_cb(GPid pid, gint status, gpointer user_data);
117 static void build_iofunc(GString *string, GIOCondition condition, gpointer data);
118 static gchar *build_create_shellscript(const gchar *working_dir, const gchar *cmd, gboolean autoclose, GError **error);
119 static void build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir);
120 static void set_stop_button(gboolean stop);
121 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data);
122 static void on_set_build_commands_activate(GtkWidget *w, gpointer u);
123 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data);
124 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data);
125 static void kill_process(GPid *pid);
126 static void show_build_result_message(gboolean failure);
127 static void process_build_output_line(gchar *msg, gint color);
128 static void show_build_commands_dialog(void);
129 static void on_build_menu_item(GtkWidget *w, gpointer user_data);
131 void build_finalize(void)
133 g_free(build_info.dir);
134 g_free(build_info.custom_target);
136 if (menu_items.menu != NULL && GTK_IS_WIDGET(menu_items.menu))
137 gtk_widget_destroy(menu_items.menu);
141 /* note: copied from keybindings.c, may be able to go away */
142 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id,
143 GtkAccelGroup *accel_group, GtkWidget *menuitem)
145 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
147 if (kb->key != 0)
148 gtk_widget_add_accelerator(menuitem, "activate", accel_group,
149 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
153 /* convenience routines to access parts of GeanyBuildCommand */
154 static gchar *id_to_str(GeanyBuildCommand *bc, gint id)
156 switch (id)
158 case GEANY_BC_LABEL:
159 return bc->label;
160 case GEANY_BC_COMMAND:
161 return bc->command;
162 case GEANY_BC_WORKING_DIR:
163 return bc->working_dir;
165 g_assert(0);
166 return NULL;
170 static void set_command(GeanyBuildCommand *bc, gint id, gchar *str)
172 switch (id)
174 case GEANY_BC_LABEL:
175 SETPTR(bc->label, str);
176 break;
177 case GEANY_BC_COMMAND:
178 SETPTR(bc->command, str);
179 break;
180 case GEANY_BC_WORKING_DIR:
181 SETPTR(bc->working_dir, str);
182 break;
183 default:
184 g_assert(0);
189 static const gchar *config_keys[GEANY_BC_CMDENTRIES_COUNT] = {
190 "LB", /* label */
191 "CM", /* command */
192 "WD" /* working directory */
195 /*-----------------------------------------------------
197 * Execute commands and handle results
199 *-----------------------------------------------------*/
201 /* the various groups of commands not in the filetype struct */
202 static GeanyBuildCommand *ft_def = NULL;
203 static GeanyBuildCommand *non_ft_proj = NULL;
204 static GeanyBuildCommand *non_ft_pref = NULL;
205 static GeanyBuildCommand *non_ft_def = NULL;
206 static GeanyBuildCommand *exec_proj = NULL;
207 static GeanyBuildCommand *exec_pref = NULL;
208 static GeanyBuildCommand *exec_def = NULL;
209 /* and the regexen not in the filetype structure */
210 static gchar *regex_pref = NULL;
211 /* project non-fileregex string */
212 static gchar *regex_proj = NULL;
214 /* control if build commands are printed by get_build_cmd, for debug purposes only*/
215 #ifndef PRINTBUILDCMDS
216 #define PRINTBUILDCMDS FALSE
217 #endif
218 static gboolean printbuildcmds = PRINTBUILDCMDS;
221 /* for debug only, print the commands structures in priority order */
222 static void printfcmds(void)
224 #if 0
225 GeanyBuildCommand **cl[GEANY_GBG_COUNT][GEANY_BCS_COUNT] = {
226 /* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
227 * GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
228 { &ft_def, NULL, NULL, NULL, NULL, NULL },
229 { &non_ft_def, NULL, NULL, &non_ft_pref, NULL, &non_ft_proj },
230 { &exec_def, NULL, NULL, &exec_pref, NULL, &exec_proj }
232 GeanyFiletype *ft = NULL;
233 GeanyDocument *doc;
234 gint i, j, k, l, m;
235 enum GeanyBuildCmdEntries n;
236 gint cc[GEANY_BCS_COUNT];
237 gchar c;
239 doc = document_get_current();
240 if (doc != NULL)
241 ft = doc->file_type;
242 if (ft != NULL)
244 printf("filetype %s\n",ft->name);
245 cl[GEANY_GBG_FT][GEANY_BCS_FT] = &(ft->priv->filecmds);
246 cl[GEANY_GBG_FT][GEANY_BCS_HOME_FT] = &(ft->priv->homefilecmds);
247 cl[GEANY_GBG_FT][GEANY_BCS_PROJ] = &(ft->priv->projfilecmds);
248 cl[GEANY_GBG_NON_FT][GEANY_BCS_FT] = &(ft->priv->ftdefcmds);
249 cl[GEANY_GBG_EXEC][GEANY_BCS_FT] = &(ft->priv->execcmds);
250 cl[GEANY_GBG_EXEC][GEANY_BCS_HOME_FT] = &(ft->priv->homeexeccmds);
251 cl[GEANY_GBG_EXEC][GEANY_BCS_PROJ_FT] = &(ft->priv->projexeccmds);
253 for (i = 0; i < GEANY_BCS_COUNT; ++i)
255 m = 1;
256 for (j = 0; j < GEANY_GBG_COUNT; ++j)
258 for (k = 0; k < build_groups_count[j]; ++k)
259 if (cl[j][i] != NULL && *(cl[j][i]) != NULL && (*(cl[j][i]))[k].exists)
261 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
263 if ((*(cl[j][i]))[k].entries[n] != NULL &&
264 (l = strlen((*(cl[j][i]))[k].entries[n])) > m)
266 m = l;
271 cc[i] = m;
273 for (i = 0; i < GEANY_GBG_COUNT; ++i)
275 for (k = 0; k < build_groups_count[i]; ++k)
277 for (l = 0; l < 2; ++l)
279 c = ' ';
280 for (j = 0; j < GEANY_BCS_COUNT; ++j)
282 if (cl[i][j] != NULL && *(cl[i][j]) != NULL && (*(cl[i][j]))[k].exists)
284 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
286 if ((*(cl[i][j]))[k].entries[i] != NULL)
287 printf("%c %*.*s",c,cc[j],cc[j],(*(cl[i][j]))[k].entries[i]);
288 else
289 printf("%c %*.*s",c,cc[j],cc[j]," ");
292 else
293 printf("%c %*.*s",c,cc[j],cc[j]," ");
294 c = ',';
296 printf("\n");
299 printf("\n");
301 #endif
305 /* macros to save typing and make the logic visible */
306 #define return_cmd_if(src, cmds)\
307 if (cmds != NULL && cmds[cmdindex].exists && below>src)\
309 *fr=src; \
310 if (printbuildcmds) \
311 printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
312 return &(cmds[cmdindex]); \
315 #define return_ft_cmd_if(src, cmds)\
316 if (ft != NULL && ft->priv->cmds != NULL \
317 && ft->priv->cmds[cmdindex].exists && below>src)\
319 *fr=src; \
320 if (printbuildcmds) \
321 printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
322 return &(ft->priv->cmds[cmdindex]); \
326 /* get the next lowest command taking priority into account */
327 static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, guint cmdgrp, guint cmdindex,
328 guint below, guint *from)
330 /* Note: parameter below used in macros above */
331 GeanyFiletype *ft = NULL;
332 guint sink, *fr = &sink;
334 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
336 if (printbuildcmds)
337 printfcmds();
338 if (cmdgrp >= GEANY_GBG_COUNT)
339 return NULL;
340 if (from != NULL)
341 fr = from;
342 if (doc == NULL)
343 doc = document_get_current();
344 if (doc != NULL)
345 ft = doc->file_type;
347 switch (cmdgrp)
349 case GEANY_GBG_FT: /* order proj ft, home ft, ft, defft */
350 if (ft != NULL)
352 return_ft_cmd_if(GEANY_BCS_PROJ, projfilecmds);
353 return_ft_cmd_if(GEANY_BCS_PREF, homefilecmds);
354 return_ft_cmd_if(GEANY_BCS_FT, filecmds);
356 return_cmd_if(GEANY_BCS_DEF, ft_def);
357 break;
358 case GEANY_GBG_NON_FT: /* order proj, pref, def */
359 return_cmd_if(GEANY_BCS_PROJ, non_ft_proj);
360 return_cmd_if(GEANY_BCS_PREF, non_ft_pref);
361 return_ft_cmd_if(GEANY_BCS_FT, ftdefcmds);
362 return_cmd_if(GEANY_BCS_DEF, non_ft_def);
363 break;
364 case GEANY_GBG_EXEC: /* order proj, proj ft, pref, home ft, ft, def */
365 return_cmd_if(GEANY_BCS_PROJ, exec_proj);
366 return_ft_cmd_if(GEANY_BCS_PROJ_FT, projexeccmds);
367 return_cmd_if(GEANY_BCS_PREF, exec_pref);
368 return_ft_cmd_if(GEANY_BCS_FT, homeexeccmds);
369 return_ft_cmd_if(GEANY_BCS_FT, execcmds);
370 return_cmd_if(GEANY_BCS_DEF, exec_def);
371 break;
372 default:
373 break;
375 return NULL;
379 /* shortcut to start looking at the top */
380 static GeanyBuildCommand *get_build_cmd(GeanyDocument *doc, guint grp, guint cmdindex, guint *from)
382 return get_next_build_cmd(doc, grp, cmdindex, GEANY_BCS_COUNT, from);
386 #define return_nonblank_regex(src, ptr)\
387 if (!EMPTY(ptr)) \
388 { *fr = (src); return &(ptr); }
391 /* like get_build_cmd, but for regexen, used by filetypes */
392 gchar **build_get_regex(GeanyBuildGroup grp, GeanyFiletype *ft, guint *from)
394 guint sink, *fr = &sink;
396 if (from != NULL)
397 fr = from;
398 if (grp == GEANY_GBG_FT)
400 if (ft == NULL)
402 GeanyDocument *doc = document_get_current();
403 if (doc != NULL)
404 ft = doc->file_type;
406 if (ft == NULL)
407 return NULL;
408 return_nonblank_regex(GEANY_BCS_PROJ, ft->priv->projerror_regex_string);
409 return_nonblank_regex(GEANY_BCS_HOME_FT, ft->priv->homeerror_regex_string);
410 return_nonblank_regex(GEANY_BCS_FT, ft->error_regex_string);
412 else if (grp == GEANY_GBG_NON_FT)
414 return_nonblank_regex(GEANY_BCS_PROJ, regex_proj);
415 return_nonblank_regex(GEANY_BCS_PREF, regex_pref);
417 return NULL;
421 static GeanyBuildCommand **get_build_group_pointer(const GeanyBuildSource src, const GeanyBuildGroup grp)
423 GeanyDocument *doc;
424 GeanyFiletype *ft = NULL;
426 switch (grp)
428 case GEANY_GBG_FT:
429 if ((doc = document_get_current()) == NULL)
430 return NULL;
431 if ((ft = doc->file_type) == NULL)
432 return NULL;
433 switch (src)
435 case GEANY_BCS_DEF: return &(ft->priv->ftdefcmds);
436 case GEANY_BCS_FT: return &(ft->priv->filecmds);
437 case GEANY_BCS_HOME_FT: return &(ft->priv->homefilecmds);
438 case GEANY_BCS_PREF: return &(ft->priv->homefilecmds);
439 case GEANY_BCS_PROJ: return &(ft->priv->projfilecmds);
440 default: return NULL;
442 break;
443 case GEANY_GBG_NON_FT:
444 switch (src)
446 case GEANY_BCS_DEF: return &(non_ft_def);
447 case GEANY_BCS_PREF: return &(non_ft_pref);
448 case GEANY_BCS_PROJ: return &(non_ft_proj);
449 default: return NULL;
451 break;
452 case GEANY_GBG_EXEC:
453 if ((doc = document_get_current()) != NULL)
454 ft = doc->file_type;
455 switch (src)
457 case GEANY_BCS_DEF: return &(exec_def);
458 case GEANY_BCS_FT: return ft ? &(ft->priv->execcmds): NULL;
459 case GEANY_BCS_HOME_FT: return ft ? &(ft->priv->homeexeccmds): NULL;
460 case GEANY_BCS_PROJ_FT: return ft ? &(ft->priv->projexeccmds): NULL;
461 case GEANY_BCS_PREF: return &(exec_pref);
462 case GEANY_BCS_PROJ: return &(exec_proj);
463 default: return NULL;
465 break;
466 default:
467 return NULL;
472 /* get pointer to the command group array */
473 static GeanyBuildCommand *get_build_group(const GeanyBuildSource src, const GeanyBuildGroup grp)
475 GeanyBuildCommand **g = get_build_group_pointer(src, grp);
476 if (g == NULL) return NULL;
477 return *g;
481 /** Remove the specified Build menu item.
483 * Makes the specified menu item configuration no longer exist. This
484 * is different to setting fields to blank because the menu item
485 * will be deleted from the configuration file on saving
486 * (except the system filetypes settings @see Build Menu Configuration
487 * section of the Manual).
489 * @param src the source of the menu item to remove.
490 * @param grp the group of the command to remove.
491 * @param cmd the index (from 0) of the command within the group. A negative
492 * value will remove the whole group.
494 * If any parameter is out of range does nothing.
496 * Updates the menu.
499 GEANY_API_SYMBOL
500 void build_remove_menu_item(const GeanyBuildSource src, const GeanyBuildGroup grp, const gint cmd)
502 GeanyBuildCommand *bc;
503 guint i;
505 bc = get_build_group(src, grp);
506 if (bc == NULL)
507 return;
508 if (cmd < 0)
510 for (i = 0; i < build_groups_count[grp]; ++i)
511 bc[i].exists = FALSE;
513 else if ((guint) cmd < build_groups_count[grp])
514 bc[cmd].exists = FALSE;
518 /* * Get the @a GeanyBuildCommand structure for the specified Build menu item.
520 * Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
521 * hidden by higher priority commands.
523 * @param src the source of the specified menu item.
524 * @param grp the group of the specified menu item.
525 * @param cmd the index of the command within the group.
527 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
528 * This is a pointer to an internal structure and must not be freed.
530 * @see build_menu_update
532 GeanyBuildCommand *build_get_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, guint cmd)
534 GeanyBuildCommand *bc;
536 g_return_val_if_fail(src < GEANY_BCS_COUNT, NULL);
537 g_return_val_if_fail(grp < GEANY_GBG_COUNT, NULL);
538 g_return_val_if_fail(cmd < build_groups_count[grp], NULL);
540 bc = get_build_group(src, grp);
541 if (bc == NULL)
542 return NULL;
543 return &(bc[cmd]);
547 /** Get the string for the menu item field.
549 * Get the current highest priority command specified by @a grp and @a cmd. This is the one
550 * that the menu item will use if activated.
552 * @param grp the group of the specified menu item.
553 * @param cmd the index of the command within the group.
554 * @param fld the field to return
556 * @return a pointer to the constant string or @a NULL if it doesn't exist.
557 * This is a pointer to an internal structure and must not be freed.
560 GEANY_API_SYMBOL
561 const gchar *build_get_current_menu_item(const GeanyBuildGroup grp, const guint cmd,
562 const GeanyBuildCmdEntries fld)
564 GeanyBuildCommand *c;
565 gchar *str = NULL;
567 g_return_val_if_fail(grp < GEANY_GBG_COUNT, NULL);
568 g_return_val_if_fail(fld < GEANY_BC_CMDENTRIES_COUNT, NULL);
569 g_return_val_if_fail(cmd < build_groups_count[grp], NULL);
571 c = get_build_cmd(NULL, grp, cmd, NULL);
572 if (c == NULL) return NULL;
573 switch (fld)
575 case GEANY_BC_COMMAND:
576 str = c->command;
577 break;
578 case GEANY_BC_LABEL:
579 str = c->label;
580 break;
581 case GEANY_BC_WORKING_DIR:
582 str = c->working_dir;
583 break;
584 default:
585 break;
587 return str;
590 /** Set the string for the menu item field.
592 * Set the specified field of the command specified by @a src, @a grp and @a cmd.
594 * @param src the source of the menu item
595 * @param grp the group of the specified menu item.
596 * @param cmd the index of the menu item within the group.
597 * @param fld the field in the menu item command to set
598 * @param val the value to set the field to, is copied
601 GEANY_API_SYMBOL
602 void build_set_menu_item(const GeanyBuildSource src, const GeanyBuildGroup grp,
603 const guint cmd, const GeanyBuildCmdEntries fld, const gchar *val)
605 GeanyBuildCommand **g;
607 g_return_if_fail(src < GEANY_BCS_COUNT);
608 g_return_if_fail(grp < GEANY_GBG_COUNT);
609 g_return_if_fail(fld < GEANY_BC_CMDENTRIES_COUNT);
610 g_return_if_fail(cmd < build_groups_count[grp]);
612 g = get_build_group_pointer(src, grp);
613 if (g == NULL) return;
614 if (*g == NULL )
616 *g = g_new0(GeanyBuildCommand, build_groups_count[grp]);
618 switch (fld)
620 case GEANY_BC_COMMAND:
621 SETPTR((*g)[cmd].command, g_strdup(val));
622 (*g)[cmd].exists = TRUE;
623 break;
624 case GEANY_BC_LABEL:
625 SETPTR((*g)[cmd].label, g_strdup(val));
626 (*g)[cmd].exists = TRUE;
627 break;
628 case GEANY_BC_WORKING_DIR:
629 SETPTR((*g)[cmd].working_dir, g_strdup(val));
630 (*g)[cmd].exists = TRUE;
631 break;
632 default:
633 break;
635 build_menu_update(NULL);
638 /** Activate the menu item.
640 * Activate the menu item specified by @a grp and @a cmd.
642 * @param grp the group of the specified menu item.
643 * @param cmd the index of the command within the group.
646 GEANY_API_SYMBOL
647 void build_activate_menu_item(const GeanyBuildGroup grp, const guint cmd)
649 on_build_menu_item(NULL, GRP_CMD_TO_POINTER(grp, cmd));
653 /* Clear all error indicators in all documents. */
654 static void clear_all_errors(void)
656 guint i;
658 foreach_document(i)
660 editor_indicator_clear_errors(documents[i]->editor);
665 /* Replaces occurrences of %e and %p with the appropriate filenames and
666 * %l with current line number. %d and %p replacements should be in UTF8 */
667 static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *src)
669 GString *stack;
670 gchar *filename = NULL;
671 gchar *replacement;
672 gchar *executable = NULL;
673 gchar *ret_str; /* to be freed when not in use anymore */
674 gint line_num;
676 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
678 stack = g_string_new(src);
679 if (doc != NULL && doc->file_name != NULL)
681 filename = utils_get_utf8_from_locale(doc->file_name);
683 /* replace %f with the filename (including extension) */
684 replacement = g_path_get_basename(filename);
685 utils_string_replace_all(stack, "%f", replacement);
686 g_free(replacement);
688 /* replace %d with the absolute path of the dir of the current file */
689 replacement = g_path_get_dirname(filename);
690 utils_string_replace_all(stack, "%d", replacement);
691 g_free(replacement);
693 /* replace %e with the filename (excluding extension) */
694 executable = utils_remove_ext_from_filename(filename);
695 replacement = g_path_get_basename(executable);
696 utils_string_replace_all(stack, "%e", replacement);
697 g_free(replacement);
699 /* replace %l with the current 1-based line number */
700 line_num = sci_get_current_line(doc->editor->sci) + 1;
701 replacement = g_strdup_printf("%i", line_num);
702 utils_string_replace_all(stack, "%l", replacement);
703 g_free(replacement);
706 /* replace %p with the current project's (absolute) base directory */
707 replacement = NULL; /* prevent double free if no replacement found */
708 if (app->project)
710 replacement = project_get_base_path();
712 else if (strstr(stack->str, "%p"))
713 { /* fall back to %d */
714 ui_set_statusbar(FALSE, _("failed to substitute %%p, no project active"));
715 if (doc != NULL && filename != NULL)
716 replacement = g_path_get_dirname(filename);
719 utils_string_replace_all(stack, "%p", replacement);
720 g_free(replacement);
722 ret_str = utils_get_utf8_from_locale(stack->str);
723 g_free(executable);
724 g_free(filename);
725 g_string_free(stack, TRUE);
727 return ret_str; /* don't forget to free src also if needed */
731 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
732 * idx document directory */
733 static void build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
735 GError *error = NULL;
736 gchar *argv[] = { "/bin/sh", "-c", (gchar *) cmd, NULL };
737 gchar *working_dir;
738 gchar *utf8_working_dir;
739 gchar *utf8_cmd_string;
741 g_return_if_fail(doc == NULL || doc->is_valid);
743 if ((doc == NULL || EMPTY(doc->file_name)) && EMPTY(dir))
745 geany_debug("Failed to run command with no working directory");
746 ui_set_statusbar(TRUE, _("Process failed, no working directory"));
747 return;
750 clear_all_errors();
751 SETPTR(current_dir_entered, NULL);
753 utf8_cmd_string = utils_get_utf8_from_locale(cmd);
754 utf8_working_dir = !EMPTY(dir) ? g_strdup(dir) : g_path_get_dirname(doc->file_name);
755 working_dir = utils_get_locale_from_utf8(utf8_working_dir);
757 gtk_list_store_clear(msgwindow.store_compiler);
758 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
759 msgwin_compiler_add(COLOR_BLUE, _("%s (in directory: %s)"), utf8_cmd_string, utf8_working_dir);
760 g_free(utf8_working_dir);
761 g_free(utf8_cmd_string);
763 #ifdef G_OS_UNIX
764 cmd = NULL; /* under Unix, use argv to start cmd via sh for compatibility */
765 #else
766 argv[0] = NULL; /* under Windows, run cmd directly */
767 #endif
769 /* set the build info for the message window */
770 g_free(build_info.dir);
771 build_info.dir = g_strdup(working_dir);
772 build_info.file_type_id = (doc == NULL) ? GEANY_FILETYPES_NONE : doc->file_type->id;
773 build_info.message_count = 0;
775 if (!spawn_with_callbacks(working_dir, cmd, argv, NULL, 0, NULL, NULL, build_iofunc,
776 GINT_TO_POINTER(0), 0, build_iofunc, GINT_TO_POINTER(1), 0, build_exit_cb, NULL,
777 &build_info.pid, &error))
779 geany_debug("build command spawning failed: %s", error->message);
780 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
781 g_error_free(error);
784 g_free(working_dir);
788 /* Returns: NULL if there was an error, or the command to be executed. If Geany is
789 * set to use a run script, the returned value is a path to the script that runs
790 * the command; otherwise the command itself is returned. working_dir is a pointer
791 * to the working directory from which the command is executed. Both strings are
792 * in the locale encoding. */
793 static gchar *prepare_run_cmd(GeanyDocument *doc, gchar **working_dir, guint cmdindex)
795 GeanyBuildCommand *cmd = NULL;
796 const gchar *cmd_working_dir;
797 gboolean autoclose = FALSE;
798 gchar *cmd_string_utf8, *working_dir_utf8, *run_cmd, *cmd_string;
799 GError *error = NULL;
801 cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
803 cmd_string_utf8 = build_replace_placeholder(doc, cmd->command);
804 cmd_working_dir = cmd->working_dir;
805 if (EMPTY(cmd_working_dir))
806 cmd_working_dir = "%d";
807 working_dir_utf8 = build_replace_placeholder(doc, cmd_working_dir);
808 *working_dir = utils_get_locale_from_utf8(working_dir_utf8);
810 if (EMPTY(*working_dir) || ! g_file_test(*working_dir, G_FILE_TEST_EXISTS) ||
811 ! g_file_test(*working_dir, G_FILE_TEST_IS_DIR))
813 ui_set_statusbar(TRUE, _("Invalid working directory \"%s\""),
814 !EMPTY(working_dir_utf8) ? working_dir_utf8 : "<NULL>" );
815 utils_free_pointers(3, cmd_string_utf8, working_dir_utf8, *working_dir, NULL);
816 return NULL;
819 cmd_string = utils_get_locale_from_utf8(cmd_string_utf8);
821 #ifdef HAVE_VTE
822 if (vte_info.have_vte && vc->run_in_vte)
824 if (vc->skip_run_script)
826 utils_free_pointers(2, cmd_string_utf8, working_dir_utf8, NULL);
827 return cmd_string;
829 else
830 /* don't wait for user input at the end of script when we are running in VTE */
831 autoclose = TRUE;
833 #endif
835 run_cmd = build_create_shellscript(*working_dir, cmd_string, autoclose, &error);
836 if (!run_cmd)
838 ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created: %s)"),
839 !EMPTY(cmd_string_utf8) ? cmd_string_utf8 : NULL, error->message);
840 g_error_free(error);
841 g_free(*working_dir);
844 utils_free_pointers(3, cmd_string_utf8, working_dir_utf8, cmd_string, NULL);
845 return run_cmd;
849 static void build_run_cmd(GeanyDocument *doc, guint cmdindex)
851 gchar *working_dir;
852 gchar *run_cmd = NULL;
854 if (! DOC_VALID(doc) || doc->file_name == NULL)
855 return;
857 run_cmd = prepare_run_cmd(doc, &working_dir, cmdindex);
858 if (run_cmd == NULL)
859 return;
861 run_info[cmdindex].file_type_id = doc->file_type->id;
863 #ifdef HAVE_VTE
864 if (vte_info.have_vte && vc->run_in_vte)
866 gchar *vte_cmd;
868 /* VTE expects commands in UTF-8 */
869 SETPTR(run_cmd, utils_get_utf8_from_locale(run_cmd));
870 SETPTR(working_dir, utils_get_utf8_from_locale(working_dir));
872 if (vc->skip_run_script)
873 vte_cmd = g_strconcat(run_cmd, "\n", NULL);
874 else
875 vte_cmd = g_strconcat("\n/bin/sh ", run_cmd, "\n", NULL);
877 vte_cwd(working_dir, TRUE);
878 if (! vte_send_cmd(vte_cmd))
880 const gchar *msg = _("File not executed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
881 ui_set_statusbar(FALSE, "%s", msg);
882 geany_debug("%s", msg);
883 if (!vc->skip_run_script)
884 g_unlink(run_cmd);
887 /* show the VTE */
888 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
889 gtk_widget_grab_focus(vc->vte);
890 msgwin_show_hide(TRUE);
892 run_info[cmdindex].pid = 1;
893 g_free(vte_cmd);
895 else
896 #endif
898 gchar *locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
899 GError *error = NULL;
901 utils_str_replace_all(&locale_term_cmd, "%c", run_cmd);
903 if (spawn_async(working_dir, locale_term_cmd, NULL, NULL, &(run_info[cmdindex].pid),
904 &error))
906 g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
907 (gpointer) &(run_info[cmdindex]));
908 build_menu_update(doc);
910 else
912 geany_debug("spawn_async() failed: %s", error->message);
913 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
914 g_error_free(error);
915 g_unlink(run_cmd);
916 run_info[cmdindex].pid = (GPid) 0;
920 g_free(working_dir);
921 g_free(run_cmd);
925 static void process_build_output_line(gchar *msg, gint color)
927 gchar *tmp;
928 gchar *filename;
929 gint line;
931 g_strchomp(msg);
933 if (EMPTY(msg))
934 return;
936 if (build_parse_make_dir(msg, &tmp))
938 SETPTR(current_dir_entered, tmp);
940 msgwin_parse_compiler_error_line(msg, current_dir_entered, &filename, &line);
942 if (line != -1 && filename != NULL)
944 GeanyDocument *doc = document_find_by_filename(filename);
946 /* limit number of indicators */
947 if (doc && editor_prefs.use_indicators &&
948 build_info.message_count < GEANY_BUILD_ERR_HIGHLIGHT_MAX)
950 if (line > 0) /* some compilers, like pdflatex report errors on line 0 */
951 line--; /* so only adjust the line number if it is greater than 0 */
952 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line);
954 build_info.message_count++;
955 color = COLOR_RED; /* error message parsed on the line */
957 g_free(filename);
959 msgwin_compiler_add_string(color, msg);
963 static void build_iofunc(GString *string, GIOCondition condition, gpointer data)
965 if (condition & (G_IO_IN | G_IO_PRI))
967 process_build_output_line(string->str,
968 (GPOINTER_TO_INT(data)) ? COLOR_DARK_RED : COLOR_BLACK);
973 gboolean build_parse_make_dir(const gchar *string, gchar **prefix)
975 const gchar *pos;
977 *prefix = NULL;
979 if (string == NULL)
980 return FALSE;
982 if ((pos = strstr(string, "Entering directory")) != NULL)
984 gsize len;
985 gchar *input;
987 /* get the start of the path */
988 pos = strstr(string, "/");
990 if (pos == NULL)
991 return FALSE;
993 input = g_strdup(pos);
995 /* kill the ' at the end of the path */
996 len = strlen(input);
997 input[len - 1] = '\0';
998 input = g_realloc(input, len); /* shorten by 1 */
999 *prefix = input;
1001 return TRUE;
1004 if (strstr(string, "Leaving directory") != NULL)
1006 *prefix = NULL;
1007 return TRUE;
1010 return FALSE;
1014 static void show_build_result_message(gboolean failure)
1016 gchar *msg;
1018 if (failure)
1020 msg = _("Compilation failed.");
1021 msgwin_compiler_add_string(COLOR_BLUE, msg);
1022 /* If msgwindow is hidden, user will want to display it to see the error */
1023 if (! ui_prefs.msgwindow_visible)
1025 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1026 msgwin_show_hide(TRUE);
1028 else
1029 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1030 ui_set_statusbar(FALSE, "%s", msg);
1032 else
1034 msg = _("Compilation finished successfully.");
1035 msgwin_compiler_add_string(COLOR_BLUE, msg);
1036 if (! ui_prefs.msgwindow_visible ||
1037 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1038 ui_set_statusbar(FALSE, "%s", msg);
1043 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data)
1045 show_build_result_message(!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS);
1046 utils_beep();
1048 build_info.pid = 0;
1049 /* enable build items again */
1050 build_menu_update(NULL);
1051 ui_progress_bar_stop();
1055 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data)
1057 RunInfo *run_info_data = user_data;
1059 g_spawn_close_pid(child_pid);
1061 run_info_data->pid = 0;
1062 /* reset the stop button and menu item to the original meaning */
1063 build_menu_update(NULL);
1067 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1068 * working_dir and cmd are both in the locale encoding
1069 * it returns the full file name (including path) of the created script in the locale encoding */
1070 static gchar *build_create_shellscript(const gchar *working_dir, const gchar *cmd, gboolean autoclose, GError **error)
1072 gint fd;
1073 gchar *str, *fname;
1074 gboolean success = TRUE;
1075 #ifdef G_OS_WIN32
1076 gchar *expanded_cmd;
1077 #else
1078 gchar *escaped_dir;
1079 #endif
1080 fd = g_file_open_tmp (RUN_SCRIPT_CMD, &fname, error);
1081 if (fd < 0)
1082 return NULL;
1083 close(fd);
1085 #ifdef G_OS_WIN32
1086 /* Expand environment variables like %blah%. */
1087 expanded_cmd = win32_expand_environment_variables(cmd);
1088 str = g_strdup_printf("cd \"%s\"\n\n%s\n\n%s\ndel \"%%0\"\n\npause\n", working_dir, expanded_cmd, (autoclose) ? "" : "pause");
1089 g_free(expanded_cmd);
1090 #else
1091 escaped_dir = g_shell_quote(working_dir);
1092 str = g_strdup_printf(
1093 "#!/bin/sh\n\nrm $0\n\ncd %s\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1094 \n\n%s\n", escaped_dir, cmd, (autoclose) ? "" :
1095 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1096 "dash\ndummy_var=\"\"\nread dummy_var");
1097 g_free(escaped_dir);
1098 #endif
1100 if (!g_file_set_contents(fname, str, -1, error))
1101 success = FALSE;
1102 g_free(str);
1103 #ifdef __APPLE__
1104 if (success && g_chmod(fname, 0777) != 0)
1106 if (error)
1108 gint errsv = errno;
1110 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv),
1111 "Failed to make file executable: %s", g_strerror(errsv));
1113 success = FALSE;
1115 #endif
1117 if (!success)
1119 g_unlink(fname);
1120 g_free(fname);
1121 fname = NULL;
1124 return fname;
1128 typedef void Callback(GtkWidget *w, gpointer u);
1130 /* run the command catenating cmd_cat if present */
1131 static void build_command(GeanyDocument *doc, GeanyBuildGroup grp, guint cmd, gchar *cmd_cat)
1133 gchar *dir;
1134 gchar *full_command, *subs_command;
1135 GeanyBuildCommand *buildcmd = get_build_cmd(doc, grp, cmd, NULL);
1136 gchar *cmdstr;
1138 if (buildcmd == NULL)
1139 return;
1141 cmdstr = buildcmd->command;
1143 if (cmd_cat != NULL)
1145 if (cmdstr != NULL)
1146 full_command = g_strconcat(cmdstr, cmd_cat, NULL);
1147 else
1148 full_command = g_strdup(cmd_cat);
1150 else
1151 full_command = cmdstr;
1153 dir = build_replace_placeholder(doc, buildcmd->working_dir);
1154 subs_command = build_replace_placeholder(doc, full_command);
1155 build_info.grp = grp;
1156 build_info.cmd = cmd;
1157 build_spawn_cmd(doc, subs_command, dir);
1158 g_free(subs_command);
1159 g_free(dir);
1160 if (cmd_cat != NULL)
1161 g_free(full_command);
1162 build_menu_update(doc);
1167 /*----------------------------------------------------------------
1169 * Create build menu and handle callbacks (&toolbar callbacks)
1171 *----------------------------------------------------------------*/
1172 static void on_make_custom_input_response(const gchar *input, gpointer data)
1174 GeanyDocument *doc = document_get_current();
1176 SETPTR(build_info.custom_target, g_strdup(input));
1177 build_command(doc, GBO_TO_GBG(GEANY_GBO_CUSTOM), GBO_TO_CMD(GEANY_GBO_CUSTOM),
1178 build_info.custom_target);
1182 static void on_build_menu_item(GtkWidget *w, gpointer user_data)
1184 GeanyDocument *doc = document_get_current();
1185 GeanyBuildCommand *bc;
1186 guint grp = GPOINTER_TO_GRP(user_data);
1187 guint cmd = GPOINTER_TO_CMD(user_data);
1189 if (doc && doc->changed)
1191 if (!document_save_file(doc, FALSE))
1192 return;
1194 g_signal_emit_by_name(geany_object, "build-start");
1196 if (grp == GEANY_GBG_NON_FT && cmd == GBO_TO_CMD(GEANY_GBO_CUSTOM))
1198 static GtkWidget *dialog = NULL; /* keep dialog for combo history */
1200 if (! dialog)
1202 dialog = dialogs_show_input_persistent(_("Custom Text"), GTK_WINDOW(main_widgets.window),
1203 _("Enter custom text here, all entered text is appended to the command."),
1204 build_info.custom_target, &on_make_custom_input_response, NULL);
1206 else
1208 gtk_widget_show(dialog);
1210 return;
1212 else if (grp == GEANY_GBG_EXEC)
1214 if (run_info[cmd].pid > (GPid) 1)
1216 kill_process(&run_info[cmd].pid);
1217 return;
1219 bc = get_build_cmd(doc, grp, cmd, NULL);
1220 if (bc != NULL && strcmp(bc->command, "builtin") == 0)
1222 gchar *uri;
1223 if (doc == NULL)
1224 return;
1225 uri = g_strconcat("file:///", g_path_skip_root(doc->file_name), NULL);
1226 utils_open_browser(uri);
1227 g_free(uri);
1230 else
1231 build_run_cmd(doc, cmd);
1233 else
1234 build_command(doc, grp, cmd, NULL);
1238 /* group codes for menu items other than the known commands
1239 * value order is important, see the following table for use */
1241 /* the rest in each group */
1242 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1243 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1244 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1245 /* the separator */
1246 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1247 /* the fixed items */
1248 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1249 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1250 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1251 #define MENU_DONE (MENU_COMMANDS + 1)
1254 static struct BuildMenuItemSpec {
1255 const gchar *stock_id;
1256 const gint key_binding;
1257 const guint build_grp;
1258 const guint build_cmd;
1259 const gchar *fix_label;
1260 Callback *cb;
1261 } build_menu_specs[] = {
1262 {GTK_STOCK_CONVERT, GEANY_KEYS_BUILD_COMPILE, GBO_TO_GBG(GEANY_GBO_COMPILE),
1263 GBO_TO_CMD(GEANY_GBO_COMPILE), NULL, on_build_menu_item},
1264 {GEANY_STOCK_BUILD, GEANY_KEYS_BUILD_LINK, GBO_TO_GBG(GEANY_GBO_BUILD),
1265 GBO_TO_CMD(GEANY_GBO_BUILD), NULL, on_build_menu_item},
1266 {NULL, -1, MENU_FT_REST,
1267 GBO_TO_CMD(GEANY_GBO_BUILD) + 1, NULL, on_build_menu_item},
1268 {NULL, -1, MENU_SEPARATOR,
1269 GBF_SEP_1, NULL, NULL},
1270 {NULL, GEANY_KEYS_BUILD_MAKE, GBO_TO_GBG(GEANY_GBO_MAKE_ALL),
1271 GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL, on_build_menu_item},
1272 {NULL, GEANY_KEYS_BUILD_MAKEOWNTARGET, GBO_TO_GBG(GEANY_GBO_CUSTOM),
1273 GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL, on_build_menu_item},
1274 {NULL, GEANY_KEYS_BUILD_MAKEOBJECT, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT),
1275 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL, on_build_menu_item},
1276 {NULL, -1, MENU_NON_FT_REST,
1277 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT) + 1, NULL, on_build_menu_item},
1278 {NULL, -1, MENU_SEPARATOR,
1279 GBF_SEP_2, NULL, NULL},
1280 {GTK_STOCK_GO_DOWN, GEANY_KEYS_BUILD_NEXTERROR, MENU_NEXT_ERROR,
1281 GBF_NEXT_ERROR, N_("_Next Error"), on_build_next_error},
1282 {GTK_STOCK_GO_UP, GEANY_KEYS_BUILD_PREVIOUSERROR, MENU_PREV_ERROR,
1283 GBF_PREV_ERROR, N_("_Previous Error"), on_build_previous_error},
1284 {NULL, -1, MENU_SEPARATOR,
1285 GBF_SEP_3, NULL, NULL},
1286 {GTK_STOCK_EXECUTE, GEANY_KEYS_BUILD_RUN, GBO_TO_GBG(GEANY_GBO_EXEC),
1287 GBO_TO_CMD(GEANY_GBO_EXEC), NULL, on_build_menu_item},
1288 {NULL, -1, MENU_EXEC_REST,
1289 GBO_TO_CMD(GEANY_GBO_EXEC) + 1, NULL, on_build_menu_item},
1290 {NULL, -1, MENU_SEPARATOR,
1291 GBF_SEP_4, NULL, NULL},
1292 {GTK_STOCK_PREFERENCES, GEANY_KEYS_BUILD_OPTIONS, MENU_COMMANDS,
1293 GBF_COMMANDS, N_("_Set Build Commands"), on_set_build_commands_activate},
1294 {NULL, -1, MENU_DONE,
1295 0, NULL, NULL}
1299 static void create_build_menu_item(GtkWidget *menu, GeanyKeyGroup *group, GtkAccelGroup *ag,
1300 struct BuildMenuItemSpec *bs, const gchar *lbl, guint grp, guint cmd)
1302 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(lbl);
1304 if (bs->stock_id != NULL)
1306 GtkWidget *image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1307 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1309 gtk_widget_show(item);
1310 if (bs->key_binding >= 0)
1311 add_menu_accel(group, (guint) bs->key_binding, ag, item);
1312 gtk_container_add(GTK_CONTAINER(menu), item);
1313 if (bs->cb != NULL)
1315 g_signal_connect(item, "activate", G_CALLBACK(bs->cb), GRP_CMD_TO_POINTER(grp,cmd));
1317 menu_items.menu_item[grp][cmd] = item;
1321 static void create_build_menu(BuildMenuItems *build_menu_items)
1323 GtkWidget *menu;
1324 GtkAccelGroup *accel_group = gtk_accel_group_new();
1325 GeanyKeyGroup *keygroup = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
1326 guint i, j;
1328 menu = gtk_menu_new();
1329 build_menu_items->menu_item[GEANY_GBG_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_FT]);
1330 build_menu_items->menu_item[GEANY_GBG_NON_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_NON_FT]);
1331 build_menu_items->menu_item[GEANY_GBG_EXEC] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_EXEC]);
1332 build_menu_items->menu_item[GBG_FIXED] = g_new0(GtkWidget*, GBF_COUNT);
1334 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1336 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1337 if (bs->build_grp == MENU_SEPARATOR)
1339 GtkWidget *item = gtk_separator_menu_item_new();
1340 gtk_widget_show(item);
1341 gtk_container_add(GTK_CONTAINER(menu), item);
1342 build_menu_items->menu_item[GBG_FIXED][bs->build_cmd] = item;
1344 else if (bs->fix_label != NULL)
1346 create_build_menu_item(menu, keygroup, accel_group, bs, _(bs->fix_label),
1347 GBG_FIXED, bs->build_cmd);
1349 else if (bs->build_grp >= MENU_FT_REST && bs->build_grp <= MENU_SEPARATOR)
1351 guint grp = bs->build_grp - GEANY_GBG_COUNT;
1352 for (j = bs->build_cmd; j < build_groups_count[grp]; ++j)
1354 GeanyBuildCommand *bc = get_build_cmd(NULL, grp, j, NULL);
1355 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1356 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, grp, j);
1359 else
1361 GeanyBuildCommand *bc = get_build_cmd(NULL, bs->build_grp, bs->build_cmd, NULL);
1362 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1363 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, bs->build_grp, bs->build_cmd);
1366 build_menu_items->menu = menu;
1367 gtk_widget_show(menu);
1368 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_build1")), menu);
1372 /* portability to various GTK versions needs checking
1373 * conforms to description of gtk_accel_label as child of menu item
1374 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1375 static void geany_menu_item_set_label(GtkWidget *w, const gchar *label)
1377 GtkWidget *c = gtk_bin_get_child(GTK_BIN(w));
1379 gtk_label_set_text_with_mnemonic(GTK_LABEL(c), label);
1383 /* * Update the build menu to reflect changes in configuration or status.
1385 * Sets the labels and number of visible items to match the highest
1386 * priority configured commands. Also sets sensitivity if build commands are
1387 * running and switches executes to stop when commands are running.
1389 * @param doc The current document, if available, to save looking it up.
1390 * If @c NULL it will be looked up.
1392 * Call this after modifying any fields of a GeanyBuildCommand structure.
1394 * @see Build Menu Configuration section of the Manual.
1397 void build_menu_update(GeanyDocument *doc)
1399 guint i, cmdcount, cmd, grp;
1400 gboolean vis = FALSE;
1401 gboolean have_path, build_running, exec_running, have_errors, cmd_sensitivity;
1402 gboolean can_compile, can_build, can_make, run_sensitivity = FALSE, run_running = FALSE;
1403 GeanyBuildCommand *bc;
1405 g_return_if_fail(doc == NULL || doc->is_valid);
1407 if (menu_items.menu == NULL)
1408 create_build_menu(&menu_items);
1409 if (doc == NULL)
1410 doc = document_get_current();
1411 have_path = doc != NULL && doc->file_name != NULL;
1412 build_running = build_info.pid > (GPid) 1;
1413 have_errors = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_compiler), NULL) > 0;
1414 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1416 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1417 switch (bs->build_grp)
1419 case MENU_SEPARATOR:
1420 if (vis == TRUE)
1422 gtk_widget_show_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1423 vis = FALSE;
1425 else
1426 gtk_widget_hide(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1427 break;
1428 case MENU_NEXT_ERROR:
1429 case MENU_PREV_ERROR:
1430 gtk_widget_set_sensitive(menu_items.menu_item[GBG_FIXED][bs->build_cmd], have_errors);
1431 vis |= TRUE;
1432 break;
1433 case MENU_COMMANDS:
1434 vis |= TRUE;
1435 break;
1436 default: /* all configurable commands */
1437 if (bs->build_grp >= GEANY_GBG_COUNT)
1439 grp = bs->build_grp - GEANY_GBG_COUNT;
1440 cmdcount = build_groups_count[grp];
1442 else
1444 grp = bs->build_grp;
1445 cmdcount = bs->build_cmd + 1;
1447 for (cmd = bs->build_cmd; cmd < cmdcount; ++cmd)
1449 GtkWidget *menu_item = menu_items.menu_item[grp][cmd];
1450 const gchar *label;
1451 bc = get_build_cmd(doc, grp, cmd, NULL);
1452 if (bc)
1453 label = bc->label;
1454 else
1455 label = NULL;
1457 if (grp < GEANY_GBG_EXEC)
1459 cmd_sensitivity =
1460 (grp == GEANY_GBG_FT && bc != NULL && have_path && ! build_running) ||
1461 (grp == GEANY_GBG_NON_FT && bc != NULL && ! build_running);
1462 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1463 if (bc != NULL && !EMPTY(label))
1465 geany_menu_item_set_label(menu_item, label);
1466 gtk_widget_show_all(menu_item);
1467 vis |= TRUE;
1469 else
1470 gtk_widget_hide(menu_item);
1472 else
1474 GtkWidget *image;
1475 exec_running = run_info[cmd].pid > (GPid) 1;
1476 cmd_sensitivity = (bc != NULL) || exec_running;
1477 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1478 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1479 run_sensitivity = cmd_sensitivity;
1480 if (! exec_running)
1482 image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1484 else
1486 image = gtk_image_new_from_stock(GTK_STOCK_STOP, GTK_ICON_SIZE_MENU);
1488 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1489 run_running = exec_running;
1490 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
1491 if (bc != NULL && !EMPTY(label))
1493 geany_menu_item_set_label(menu_item, label);
1494 gtk_widget_show_all(menu_item);
1495 vis |= TRUE;
1497 else
1498 gtk_widget_hide(menu_item);
1504 run_sensitivity &= (doc != NULL);
1505 can_build = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_BUILD), NULL) != NULL
1506 && have_path && ! build_running;
1507 if (widgets.toolitem_build != NULL)
1508 gtk_widget_set_sensitive(widgets.toolitem_build, can_build);
1509 can_make = FALSE;
1510 if (widgets.toolitem_make_all != NULL)
1511 gtk_widget_set_sensitive(widgets.toolitem_make_all,
1512 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL) != NULL
1513 && ! build_running));
1514 if (widgets.toolitem_make_custom != NULL)
1515 gtk_widget_set_sensitive(widgets.toolitem_make_custom,
1516 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL) != NULL
1517 && ! build_running));
1518 if (widgets.toolitem_make_object != NULL)
1519 gtk_widget_set_sensitive(widgets.toolitem_make_object,
1520 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL) != NULL
1521 && ! build_running));
1522 if (widgets.toolitem_set_args != NULL)
1523 gtk_widget_set_sensitive(widgets.toolitem_set_args, TRUE);
1525 can_compile = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_COMPILE), NULL) != NULL
1526 && have_path && ! build_running;
1527 gtk_action_set_sensitive(widgets.compile_action, can_compile);
1528 gtk_action_set_sensitive(widgets.build_action, can_make);
1529 gtk_action_set_sensitive(widgets.run_action, run_sensitivity);
1531 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1532 set_stop_button(run_running);
1537 /* Call build_menu_update() instead of calling this directly. */
1538 static void set_stop_button(gboolean stop)
1540 const gchar *button_stock_id = NULL;
1541 GtkToolButton *run_button;
1543 run_button = GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1544 if (run_button != NULL)
1545 button_stock_id = gtk_tool_button_get_stock_id(run_button);
1547 if (stop && utils_str_equal(button_stock_id, GTK_STOCK_STOP))
1548 return;
1549 if (! stop && utils_str_equal(button_stock_id, GTK_STOCK_EXECUTE))
1550 return;
1552 /* use the run button also as stop button */
1553 if (stop)
1555 if (run_button != NULL)
1556 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_STOP);
1558 else
1560 if (run_button != NULL)
1561 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_EXECUTE);
1566 static void on_set_build_commands_activate(GtkWidget *w, gpointer u)
1568 /* For now, just show the project dialog */
1569 if (app->project)
1570 project_build_properties();
1571 else
1572 show_build_commands_dialog();
1576 static void on_toolbutton_build_activate(GtkWidget *menuitem, gpointer user_data)
1578 last_toolbutton_action = user_data;
1579 g_object_set(widgets.build_action, "tooltip", _("Build the current file"), NULL);
1580 on_build_menu_item(menuitem, user_data);
1584 static void on_toolbutton_make_activate(GtkWidget *menuitem, gpointer user_data)
1586 gchar *msg;
1588 last_toolbutton_action = user_data;
1589 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_ALL))
1590 msg = _("Build the current file with Make and the default target");
1591 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_CUSTOM))
1592 msg = _("Build the current file with Make and the specified target");
1593 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT))
1594 msg = _("Compile the current file with Make");
1595 else
1596 msg = NULL;
1597 g_object_set(widgets.build_action, "tooltip", msg, NULL);
1598 on_build_menu_item(menuitem, user_data);
1602 static void kill_process(GPid *pid)
1604 GError *error = NULL;
1606 if (spawn_kill_process(*pid, &error))
1608 *pid = 0;
1609 build_menu_update(NULL);
1611 else
1613 ui_set_statusbar(TRUE, _("Process could not be stopped (%s)."), error->message);
1614 g_error_free(error);
1619 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data)
1621 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_compiler),
1622 msgwin_goto_compiler_file_line))
1624 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1626 else
1627 ui_set_statusbar(FALSE, _("No more build errors."));
1631 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data)
1633 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_compiler),
1634 msgwin_goto_compiler_file_line))
1636 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1638 else
1639 ui_set_statusbar(FALSE, _("No more build errors."));
1643 void build_toolbutton_build_clicked(GtkAction *action, gpointer unused)
1645 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_BUILD))
1647 on_build_menu_item(NULL, GBO_TO_POINTER(GEANY_GBO_BUILD));
1649 else
1651 on_build_menu_item(NULL, last_toolbutton_action);
1656 /*------------------------------------------------------
1658 * Create and handle the build menu configuration dialog
1660 *-------------------------------------------------------*/
1661 typedef struct RowWidgets
1663 GtkWidget *entries[GEANY_BC_CMDENTRIES_COUNT];
1664 GeanyBuildSource src;
1665 GeanyBuildSource dst;
1666 GeanyBuildCommand *cmdsrc;
1667 guint grp;
1668 guint cmd;
1669 gboolean cleared;
1670 gboolean used_dst;
1671 } RowWidgets;
1673 #if GTK_CHECK_VERSION(3,0,0)
1674 typedef GdkRGBA InsensitiveColor;
1675 #else
1676 typedef GdkColor InsensitiveColor;
1677 #endif
1678 static InsensitiveColor insensitive_color;
1680 static void set_row_color(RowWidgets *r, InsensitiveColor *color)
1682 enum GeanyBuildCmdEntries i;
1684 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1686 if (i == GEANY_BC_LABEL)
1687 continue;
1689 #if GTK_CHECK_VERSION(3,0,0)
1690 gtk_widget_override_color(r->entries[i], GTK_STATE_FLAG_NORMAL, color);
1691 #else
1692 gtk_widget_modify_text(r->entries[i], GTK_STATE_NORMAL, color);
1693 #endif
1698 static void set_build_command_entry_text(GtkWidget *wid, const gchar *text)
1700 if (GTK_IS_BUTTON(wid))
1701 gtk_button_set_label(GTK_BUTTON(wid), text);
1702 else
1703 gtk_entry_set_text(GTK_ENTRY(wid), text);
1707 static void on_clear_dialog_row(GtkWidget *unused, gpointer user_data)
1709 RowWidgets *r = user_data;
1710 guint src;
1711 enum GeanyBuildCmdEntries i;
1712 GeanyBuildCommand *bc = get_next_build_cmd(NULL, r->grp, r->cmd, r->dst, &src);
1714 if (bc != NULL)
1716 r->cmdsrc = bc;
1717 r->src = src;
1718 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1720 set_build_command_entry_text(r->entries[i],
1721 id_to_str(bc,i) != NULL ? id_to_str(bc,i) : "");
1724 else
1726 r->cmdsrc = NULL;
1727 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1729 set_build_command_entry_text(r->entries[i], "");
1732 r->used_dst = FALSE;
1733 set_row_color(r, &insensitive_color);
1734 r->cleared = TRUE;
1738 static void on_clear_dialog_regex_row(GtkEntry *regex, gpointer unused)
1740 gtk_entry_set_text(regex,"");
1744 static void on_label_button_clicked(GtkWidget *wid, gpointer user_data)
1746 RowWidgets *r = user_data;
1747 GtkWidget *top_level = gtk_widget_get_toplevel(wid);
1748 const gchar *old = gtk_button_get_label(GTK_BUTTON(wid));
1749 gchar *str;
1751 if (gtk_widget_is_toplevel(top_level) && GTK_IS_WINDOW(top_level))
1752 str = dialogs_show_input(_("Set menu item label"), GTK_WINDOW(top_level), NULL, old);
1753 else
1754 str = dialogs_show_input(_("Set menu item label"), NULL, NULL, old);
1756 if (!str)
1757 return;
1759 gtk_button_set_label(GTK_BUTTON(wid), str);
1760 g_free(str);
1761 r->used_dst = TRUE;
1762 set_row_color(r, NULL);
1766 static void on_entry_focus(GtkWidget *wid, GdkEventFocus *unused, gpointer user_data)
1768 RowWidgets *r = user_data;
1770 r->used_dst = TRUE;
1771 set_row_color(r, NULL);
1775 /* Column headings, array NULL-terminated */
1776 static const gchar *colheads[] =
1778 "#",
1779 N_("Label"),
1780 N_("Command"),
1781 N_("Working directory"),
1782 N_("Reset"),
1783 NULL
1786 /* column names */
1787 #define DC_ITEM 0
1788 #define DC_ENTRIES 1
1789 #define DC_CLEAR 4
1790 #define DC_N_COL 5
1792 static const guint entry_x_padding = 3;
1793 static const guint entry_y_padding = 0;
1796 static RowWidgets *build_add_dialog_row(GeanyDocument *doc, GtkTable *table, guint row,
1797 GeanyBuildSource dst, guint grp, guint cmd, gboolean dir)
1799 GtkWidget *label, *clear, *clearicon;
1800 RowWidgets *roww;
1801 GeanyBuildCommand *bc;
1802 guint src;
1803 enum GeanyBuildCmdEntries i;
1804 guint column = 0;
1805 gchar *text;
1807 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
1809 text = g_strdup_printf("%d.", cmd + 1);
1810 label = gtk_label_new(text);
1811 g_free(text);
1812 #if GTK_CHECK_VERSION(3,0,0)
1813 gtk_style_context_get_color(gtk_widget_get_style_context(label), GTK_STATE_FLAG_INSENSITIVE, &insensitive_color);
1814 #else
1815 insensitive_color = gtk_widget_get_style(label)->text[GTK_STATE_INSENSITIVE];
1816 #endif
1817 gtk_table_attach(table, label, column, column + 1, row, row + 1, GTK_FILL,
1818 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1819 roww = g_new0(RowWidgets, 1);
1820 roww->src = GEANY_BCS_COUNT;
1821 roww->grp = grp;
1822 roww->cmd = cmd;
1823 roww->dst = dst;
1824 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1826 gint xflags = (i == GEANY_BC_COMMAND) ? GTK_FILL | GTK_EXPAND : GTK_FILL;
1828 column += 1;
1829 if (i == GEANY_BC_LABEL)
1831 GtkWidget *wid = roww->entries[i] = gtk_button_new();
1832 gtk_button_set_use_underline(GTK_BUTTON(wid), TRUE);
1833 gtk_widget_set_tooltip_text(wid, _("Click to set menu item label"));
1834 g_signal_connect(wid, "clicked", G_CALLBACK(on_label_button_clicked), roww);
1836 else
1838 roww->entries[i] = gtk_entry_new();
1839 g_signal_connect(roww->entries[i], "focus-in-event", G_CALLBACK(on_entry_focus), roww);
1841 gtk_table_attach(table, roww->entries[i], column, column + 1, row, row + 1, xflags,
1842 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1844 column++;
1845 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1846 clear = gtk_button_new();
1847 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1848 g_signal_connect(clear, "clicked", G_CALLBACK(on_clear_dialog_row), roww);
1849 gtk_table_attach(table, clear, column, column + 1, row, row + 1, GTK_FILL,
1850 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1851 roww->cmdsrc = bc = get_build_cmd(doc, grp, cmd, &src);
1852 if (bc != NULL)
1853 roww->src = src;
1855 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1857 const gchar *str = "";
1859 if (bc != NULL )
1861 if ((str = id_to_str(bc, i)) == NULL)
1862 str = "";
1863 else if (dst == src)
1864 roww->used_dst = TRUE;
1866 set_build_command_entry_text(roww->entries[i], str);
1868 if (bc != NULL && (dst > src))
1869 set_row_color(roww, &insensitive_color);
1870 if (bc != NULL && (src > dst || (grp == GEANY_GBG_FT && (doc == NULL || doc->file_type == NULL))))
1872 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1873 gtk_widget_set_sensitive(roww->entries[i], FALSE);
1874 gtk_widget_set_sensitive(clear, FALSE);
1876 return roww;
1880 typedef struct BuildTableFields
1882 RowWidgets **rows;
1883 GtkWidget *fileregex;
1884 GtkWidget *nonfileregex;
1885 gchar **fileregexstring;
1886 gchar **nonfileregexstring;
1887 } BuildTableFields;
1890 GtkWidget *build_commands_table(GeanyDocument *doc, GeanyBuildSource dst, BuildTableData *table_data,
1891 GeanyFiletype *ft)
1893 GtkWidget *label, *sep, *clearicon, *clear;
1894 BuildTableFields *fields;
1895 GtkTable *table;
1896 const gchar **ch;
1897 gchar *txt;
1898 guint col, row, cmdindex;
1899 guint cmd;
1900 guint src;
1901 gboolean sensitivity;
1902 guint sep_padding = entry_y_padding + 3;
1904 table = GTK_TABLE(gtk_table_new(build_items_count + 12, 5, FALSE));
1905 fields = g_new0(BuildTableFields, 1);
1906 fields->rows = g_new0(RowWidgets*, build_items_count);
1907 for (ch = colheads, col = 0; *ch != NULL; ch++, col++)
1909 label = gtk_label_new(_(*ch));
1910 gtk_table_attach(table, label, col, col + 1, 0, 1,
1911 GTK_FILL, GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1913 sep = gtk_hseparator_new();
1914 gtk_table_attach(table, sep, 0, DC_N_COL, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND,
1915 entry_x_padding, sep_padding);
1916 if (ft != NULL && ft->id != GEANY_FILETYPES_NONE)
1917 txt = g_strdup_printf(_("%s commands"), ft->name);
1918 else
1919 txt = g_strdup_printf(_("%s commands"), _("No filetype"));
1921 label = ui_label_new_bold(txt);
1922 g_free(txt);
1923 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1924 gtk_table_attach(table, label, 0, DC_N_COL, 2, 3, GTK_FILL, GTK_FILL | GTK_EXPAND,
1925 entry_x_padding, entry_y_padding);
1926 for (row = 3, cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++row, ++cmdindex, ++cmd)
1927 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_FT, cmd, FALSE);
1928 label = gtk_label_new(_("Error regular expression:"));
1929 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1930 entry_x_padding, entry_y_padding);
1931 fields->fileregex = gtk_entry_new();
1932 fields->fileregexstring = build_get_regex(GEANY_GBG_FT, NULL, &src);
1933 sensitivity = (ft == NULL) ? FALSE : TRUE;
1934 if (fields->fileregexstring != NULL && *(fields->fileregexstring) != NULL)
1936 gtk_entry_set_text(GTK_ENTRY(fields->fileregex), *(fields->fileregexstring));
1937 if (src > dst)
1938 sensitivity = FALSE;
1940 gtk_table_attach(table, fields->fileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
1941 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1942 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1943 clear = gtk_button_new();
1944 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1945 g_signal_connect_swapped(clear, "clicked",
1946 G_CALLBACK(on_clear_dialog_regex_row), (fields->fileregex));
1947 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
1948 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1949 gtk_widget_set_sensitive(fields->fileregex, sensitivity);
1950 gtk_widget_set_sensitive(clear, sensitivity);
1951 ++row;
1952 sep = gtk_hseparator_new();
1953 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1954 entry_x_padding, sep_padding);
1955 ++row;
1956 label = ui_label_new_bold(_("Independent commands"));
1957 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1958 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1959 entry_x_padding, entry_y_padding);
1960 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++row, ++cmdindex, ++cmd)
1961 fields->rows[cmdindex] = build_add_dialog_row(
1962 doc, table, row, dst, GEANY_GBG_NON_FT, cmd, TRUE);
1963 label = gtk_label_new(_("Error regular expression:"));
1964 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL,
1965 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1966 fields->nonfileregex = gtk_entry_new();
1967 fields->nonfileregexstring = build_get_regex(GEANY_GBG_NON_FT, NULL, &src);
1968 sensitivity = TRUE;
1969 if (fields->nonfileregexstring != NULL && *(fields->nonfileregexstring) != NULL)
1971 gtk_entry_set_text(GTK_ENTRY(fields->nonfileregex), *(fields->nonfileregexstring));
1972 sensitivity = src > dst ? FALSE : TRUE;
1974 gtk_table_attach(table, fields->nonfileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
1975 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1976 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1977 clear = gtk_button_new();
1978 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1979 g_signal_connect_swapped(clear, "clicked",
1980 G_CALLBACK(on_clear_dialog_regex_row), (fields->nonfileregex));
1981 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
1982 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1983 gtk_widget_set_sensitive(fields->nonfileregex, sensitivity);
1984 gtk_widget_set_sensitive(clear, sensitivity);
1985 ++row;
1986 label = gtk_label_new(NULL);
1987 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
1988 _("Note: Item 2 opens a dialog and appends the response to the command."));
1989 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1990 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1991 entry_x_padding, entry_y_padding);
1992 ++row;
1993 sep = gtk_hseparator_new();
1994 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1995 entry_x_padding, sep_padding);
1996 ++row;
1997 label = ui_label_new_bold(_("Execute commands"));
1998 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1999 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2000 entry_x_padding, entry_y_padding);
2001 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++row, ++cmdindex, ++cmd)
2002 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_EXEC, cmd, TRUE);
2003 sep = gtk_hseparator_new();
2004 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2005 entry_x_padding, sep_padding);
2006 ++row;
2007 label = gtk_label_new(NULL);
2008 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2009 _("%d, %e, %f, %p, %l are substituted in command and directory fields, see manual for details."));
2010 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2011 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2012 entry_x_padding, entry_y_padding);
2013 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
2014 ++row;
2015 *table_data = fields;
2016 return GTK_WIDGET(table);
2020 void build_free_fields(BuildTableData table_data)
2022 guint cmdindex;
2024 for (cmdindex = 0; cmdindex < build_items_count; ++cmdindex)
2025 g_free(table_data->rows[cmdindex]);
2026 g_free(table_data->rows);
2027 g_free(table_data);
2031 /* string compare where null pointers match null or 0 length strings */
2032 #if 0
2033 static gint stcmp(const gchar *a, const gchar *b)
2035 if (a == NULL && b == NULL)
2036 return 0;
2037 if (a == NULL && b != NULL)
2038 return strlen(b);
2039 if (a != NULL && b == NULL)
2040 return strlen(a);
2041 return strcmp(a, b);
2043 #endif
2046 static const gchar *get_build_command_entry_text(GtkWidget *wid)
2048 if (GTK_IS_BUTTON(wid))
2049 return gtk_button_get_label(GTK_BUTTON(wid));
2050 else
2051 return gtk_entry_get_text(GTK_ENTRY(wid));
2055 static gboolean read_row(BuildDestination *dst, BuildTableData table_data, guint drow, guint grp, guint cmd)
2057 gchar *entries[GEANY_BC_CMDENTRIES_COUNT];
2058 gboolean changed = FALSE;
2059 enum GeanyBuildCmdEntries i;
2061 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2063 entries[i] = g_strdup(get_build_command_entry_text(table_data->rows[drow]->entries[i]));
2065 if (table_data->rows[drow]->cleared)
2067 if (dst->dst[grp] != NULL)
2069 if (*(dst->dst[grp]) == NULL)
2070 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2071 (*(dst->dst[grp]))[cmd].exists = FALSE;
2072 (*(dst->dst[grp]))[cmd].changed = TRUE;
2073 changed = TRUE;
2076 if (table_data->rows[drow]->used_dst == TRUE)
2078 if (dst->dst[grp] != NULL)
2080 if (*(dst->dst[grp]) == NULL)
2081 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2082 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2083 set_command(&(*(dst->dst[grp]))[cmd], i, entries[i]);
2084 (*(dst->dst[grp]))[cmd].exists = TRUE;
2085 (*(dst->dst[grp]))[cmd].changed = TRUE;
2086 changed = TRUE;
2089 else
2091 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2092 g_free(entries[i]);
2094 return changed;
2098 static gboolean read_regex(GtkWidget *regexentry, gchar **src, gchar **dst)
2100 gboolean changed = FALSE;
2101 const gchar *reg = gtk_entry_get_text(GTK_ENTRY(regexentry));
2103 if (((src == NULL /* originally there was no regex */
2104 || *src == NULL) /* or it was NULL*/
2105 && !EMPTY(reg)) /* and something was typed */
2106 || (src != NULL /* originally there was a regex*/
2107 && (*src == NULL /* and either it was NULL */
2108 || strcmp(*src, reg) != 0))) /* or it has been changed */
2110 if (dst != NULL)
2112 SETPTR(*dst, g_strdup(reg));
2113 changed = TRUE;
2116 return changed;
2120 static gboolean build_read_commands(BuildDestination *dst, BuildTableData table_data, gint response)
2122 guint cmdindex, cmd;
2123 gboolean changed = FALSE;
2125 if (response == GTK_RESPONSE_ACCEPT)
2127 for (cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++cmdindex, ++cmd)
2128 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_FT, cmd);
2129 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++cmdindex, ++cmd)
2130 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_NON_FT, cmd);
2131 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++cmdindex, ++cmd)
2132 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_EXEC, cmd);
2133 changed |= read_regex(table_data->fileregex, table_data->fileregexstring, dst->fileregexstr);
2134 changed |= read_regex(table_data->nonfileregex, table_data->nonfileregexstring, dst->nonfileregexstr);
2136 return changed;
2140 void build_read_project(GeanyFiletype *ft, BuildTableData build_properties)
2142 BuildDestination menu_dst;
2144 if (ft != NULL)
2146 menu_dst.dst[GEANY_GBG_FT] = &(ft->priv->projfilecmds);
2147 menu_dst.fileregexstr = &(ft->priv->projerror_regex_string);
2149 else
2151 menu_dst.dst[GEANY_GBG_FT] = NULL;
2152 menu_dst.fileregexstr = NULL;
2154 menu_dst.dst[GEANY_GBG_NON_FT] = &non_ft_proj;
2155 menu_dst.dst[GEANY_GBG_EXEC] = &exec_proj;
2156 menu_dst.nonfileregexstr = &regex_proj;
2158 build_read_commands(&menu_dst, build_properties, GTK_RESPONSE_ACCEPT);
2162 static void show_build_commands_dialog(void)
2164 GtkWidget *dialog, *table, *vbox;
2165 GeanyDocument *doc = document_get_current();
2166 GeanyFiletype *ft = NULL;
2167 const gchar *title = _("Set Build Commands");
2168 gint response;
2169 BuildTableData table_data;
2170 BuildDestination prefdsts;
2172 if (doc != NULL)
2173 ft = doc->file_type;
2174 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
2175 GTK_DIALOG_DESTROY_WITH_PARENT,
2176 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2177 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
2178 table = build_commands_table(doc, GEANY_BCS_PREF, &table_data, ft);
2179 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
2180 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
2181 gtk_widget_show_all(dialog);
2182 /* run modally to prevent user changing idx filetype */
2183 response = gtk_dialog_run(GTK_DIALOG(dialog));
2185 prefdsts.dst[GEANY_GBG_NON_FT] = &non_ft_pref;
2186 if (ft != NULL)
2188 prefdsts.dst[GEANY_GBG_FT] = &(ft->priv->homefilecmds);
2189 prefdsts.fileregexstr = &(ft->priv->homeerror_regex_string);
2190 prefdsts.dst[GEANY_GBG_EXEC] = &(ft->priv->homeexeccmds);
2192 else
2194 prefdsts.dst[GEANY_GBG_FT] = NULL;
2195 prefdsts.fileregexstr = NULL;
2196 prefdsts.dst[GEANY_GBG_EXEC] = NULL;
2198 prefdsts.nonfileregexstr = &regex_pref;
2199 if (build_read_commands(&prefdsts, table_data, response) && ft != NULL)
2200 filetypes_save_commands(ft);
2201 build_free_fields(table_data);
2203 build_menu_update(doc);
2204 gtk_widget_destroy(dialog);
2208 /* Creates the relevant build menu if necessary. */
2209 BuildMenuItems *build_get_menu_items(gint filetype_idx)
2211 BuildMenuItems *items;
2213 items = &menu_items;
2214 if (items->menu == NULL)
2215 create_build_menu(items);
2216 return items;
2220 /*----------------------------------------------------------
2222 * Load and store configuration
2224 * ---------------------------------------------------------*/
2225 static const gchar *build_grp_name = "build-menu";
2227 /* config format for build-menu group is prefix_gg_nn_xx=value
2228 * where gg = FT, NF, EX for the command group
2229 * nn = 2 digit command number
2230 * xx = LB for label, CM for command and WD for working dir */
2231 static const gchar *groups[GEANY_GBG_COUNT] = { "FT", "NF", "EX" };
2232 static const gchar *fixedkey="xx_xx_xx";
2234 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2235 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2236 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2238 static void build_load_menu_grp(GKeyFile *config, GeanyBuildCommand **dst, gint grp,
2239 gchar *prefix, gboolean loc)
2241 guint cmd;
2242 gsize prefixlen; /* NOTE prefixlen used in macros above */
2243 GeanyBuildCommand *dstcmd;
2244 gchar *key;
2245 static gchar cmdbuf[3] = " ";
2247 if (*dst == NULL)
2248 *dst = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2249 dstcmd = *dst;
2250 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2251 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2252 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2254 gchar *label;
2255 if (cmd >= 100)
2256 break; /* ensure no buffer overflow */
2257 sprintf(cmdbuf, "%02u", cmd);
2258 set_key_grp(key, groups[grp]);
2259 set_key_cmd(key, cmdbuf);
2260 set_key_fld(key, "LB");
2261 if (loc)
2262 label = g_key_file_get_locale_string(config, build_grp_name, key, NULL, NULL);
2263 else
2264 label = g_key_file_get_string(config, build_grp_name, key, NULL);
2265 if (label != NULL)
2267 dstcmd[cmd].exists = TRUE;
2268 SETPTR(dstcmd[cmd].label, label);
2269 set_key_fld(key,"CM");
2270 SETPTR(dstcmd[cmd].command,
2271 g_key_file_get_string(config, build_grp_name, key, NULL));
2272 set_key_fld(key,"WD");
2273 SETPTR(dstcmd[cmd].working_dir,
2274 g_key_file_get_string(config, build_grp_name, key, NULL));
2276 else dstcmd[cmd].exists = FALSE;
2278 g_free(key);
2282 /* for the specified source load new format build menu items or try to make some sense of
2283 * old format setings, not done perfectly but better than ignoring them */
2284 void build_load_menu(GKeyFile *config, GeanyBuildSource src, gpointer p)
2286 GeanyFiletype *ft;
2287 GeanyProject *pj;
2288 gchar **ftlist;
2289 gchar *value, *basedir, *makebasedir;
2290 gboolean bvalue = FALSE;
2292 if (g_key_file_has_group(config, build_grp_name))
2294 switch (src)
2296 case GEANY_BCS_FT:
2297 ft = (GeanyFiletype*)p;
2298 if (ft == NULL)
2299 return;
2300 build_load_menu_grp(config, &(ft->priv->filecmds), GEANY_GBG_FT, NULL, TRUE);
2301 build_load_menu_grp(config, &(ft->priv->ftdefcmds), GEANY_GBG_NON_FT, NULL, TRUE);
2302 build_load_menu_grp(config, &(ft->priv->execcmds), GEANY_GBG_EXEC, NULL, TRUE);
2303 SETPTR(ft->error_regex_string,
2304 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2305 break;
2306 case GEANY_BCS_HOME_FT:
2307 ft = (GeanyFiletype*)p;
2308 if (ft == NULL)
2309 return;
2310 build_load_menu_grp(config, &(ft->priv->homefilecmds), GEANY_GBG_FT, NULL, FALSE);
2311 build_load_menu_grp(config, &(ft->priv->homeexeccmds), GEANY_GBG_EXEC, NULL, FALSE);
2312 SETPTR(ft->priv->homeerror_regex_string,
2313 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2314 break;
2315 case GEANY_BCS_PREF:
2316 build_load_menu_grp(config, &non_ft_pref, GEANY_GBG_NON_FT, NULL, FALSE);
2317 build_load_menu_grp(config, &exec_pref, GEANY_GBG_EXEC, NULL, FALSE);
2318 SETPTR(regex_pref, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2319 break;
2320 case GEANY_BCS_PROJ:
2321 build_load_menu_grp(config, &non_ft_proj, GEANY_GBG_NON_FT, NULL, FALSE);
2322 build_load_menu_grp(config, &exec_proj, GEANY_GBG_EXEC, NULL, FALSE);
2323 SETPTR(regex_proj, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2324 pj = (GeanyProject*)p;
2325 if (p == NULL)
2326 return;
2327 ftlist = g_key_file_get_string_list(config, build_grp_name, "filetypes", NULL, NULL);
2328 if (ftlist != NULL)
2330 gchar **ftname;
2331 if (pj->priv->build_filetypes_list == NULL)
2332 pj->priv->build_filetypes_list = g_ptr_array_new();
2333 g_ptr_array_set_size(pj->priv->build_filetypes_list, 0);
2334 for (ftname = ftlist; *ftname != NULL; ++ftname)
2336 ft = filetypes_lookup_by_name(*ftname);
2337 if (ft != NULL)
2339 gchar *regkey = g_strdup_printf("%serror_regex", *ftname);
2340 g_ptr_array_add(pj->priv->build_filetypes_list, ft);
2341 SETPTR(ft->priv->projerror_regex_string,
2342 g_key_file_get_string(config, build_grp_name, regkey, NULL));
2343 g_free(regkey);
2344 build_load_menu_grp(config, &(ft->priv->projfilecmds), GEANY_GBG_FT, *ftname, FALSE);
2345 build_load_menu_grp(config, &(ft->priv->projexeccmds), GEANY_GBG_EXEC, *ftname, FALSE);
2348 g_free(ftlist);
2350 break;
2351 default: /* defaults don't load from config, see build_init */
2352 break;
2356 /* load old [build_settings] values if there is no value defined by [build-menu] */
2358 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2359 /* TODO: rewrite as function */
2360 #define ASSIGNIF(type, id, string, value) \
2361 do { \
2362 gchar *ASSIGNF__value = (value); \
2363 if (!EMPTY(ASSIGNF__value) && ! type[GBO_TO_CMD(id)].exists) { \
2364 type[GBO_TO_CMD(id)].exists = TRUE; \
2365 SETPTR(type[GBO_TO_CMD(id)].label, g_strdup(string)); \
2366 SETPTR(type[GBO_TO_CMD(id)].command, ASSIGNF__value); \
2367 SETPTR(type[GBO_TO_CMD(id)].working_dir, NULL); \
2368 type[GBO_TO_CMD(id)].old = TRUE; \
2369 } else \
2370 g_free(ASSIGNF__value); \
2371 } while (0)
2373 switch (src)
2375 case GEANY_BCS_FT:
2376 ft = (GeanyFiletype*)p;
2377 value = g_key_file_get_string(config, "build_settings", "compiler", NULL);
2378 if (value != NULL)
2380 if (ft->priv->filecmds == NULL)
2381 ft->priv->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2382 ASSIGNIF(ft->priv->filecmds, GEANY_GBO_COMPILE, _("_Compile"), value);
2384 value = g_key_file_get_string(config, "build_settings", "linker", NULL);
2385 if (value != NULL)
2387 if (ft->priv->filecmds == NULL)
2388 ft->priv->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2389 ASSIGNIF(ft->priv->filecmds, GEANY_GBO_BUILD, _("_Build"), value);
2391 value = g_key_file_get_string(config, "build_settings", "run_cmd", NULL);
2392 if (value != NULL)
2394 if (ft->priv->execcmds == NULL)
2395 ft->priv->execcmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2396 ASSIGNIF(ft->priv->execcmds, GEANY_GBO_EXEC, _("_Execute"), value);
2398 if (ft->error_regex_string == NULL)
2399 ft->error_regex_string = g_key_file_get_string(config, "build_settings", "error_regex", NULL);
2400 break;
2401 case GEANY_BCS_PROJ:
2402 if (non_ft_pref == NULL)
2403 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2404 basedir = project_get_base_path();
2405 if (basedir == NULL)
2406 basedir = g_strdup("%d");
2407 bvalue = g_key_file_get_boolean(config, "project", "make_in_base_path", NULL);
2408 if (bvalue)
2409 makebasedir = g_strdup(basedir);
2410 else
2411 makebasedir = g_strdup("%d");
2412 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].old)
2413 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].working_dir, g_strdup(makebasedir));
2414 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].old)
2415 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].working_dir, g_strdup(makebasedir));
2416 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].old)
2417 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].working_dir, g_strdup("%d"));
2418 value = g_key_file_get_string(config, "project", "run_cmd", NULL);
2419 if (!EMPTY(value))
2421 if (exec_proj == NULL)
2422 exec_proj = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2423 if (! exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists)
2425 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists = TRUE;
2426 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].label, g_strdup(_("_Execute")));
2427 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].command, value);
2428 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].working_dir, g_strdup(basedir));
2429 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].old = TRUE;
2432 g_free(makebasedir);
2433 g_free(basedir);
2434 break;
2435 case GEANY_BCS_PREF:
2436 value = g_key_file_get_string(config, "tools", "make_cmd", NULL);
2437 if (value != NULL)
2439 if (non_ft_pref == NULL)
2440 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2441 ASSIGNIF(non_ft_pref, GEANY_GBO_CUSTOM, _("Make Custom _Target..."),
2442 g_strdup_printf("%s ", value));
2443 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_OBJECT, _("Make _Object"),
2444 g_strdup_printf("%s %%e.o",value));
2445 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_ALL, _("_Make"), value);
2447 break;
2448 default:
2449 break;
2454 static guint build_save_menu_grp(GKeyFile *config, GeanyBuildCommand *src, gint grp, gchar *prefix)
2456 guint cmd;
2457 gsize prefixlen; /* NOTE prefixlen used in macros above */
2458 gchar *key;
2459 guint count = 0;
2460 enum GeanyBuildCmdEntries i;
2462 if (src == NULL)
2463 return 0;
2464 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2465 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2466 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2468 if (src[cmd].exists) ++count;
2469 if (src[cmd].changed)
2471 static gchar cmdbuf[4] = " ";
2472 if (cmd >= 100)
2473 break; /* ensure no buffer overflow */
2474 sprintf(cmdbuf, "%02u", cmd);
2475 set_key_grp(key, groups[grp]);
2476 set_key_cmd(key, cmdbuf);
2477 if (src[cmd].exists)
2479 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2481 set_key_fld(key, config_keys[i]);
2482 g_key_file_set_string(config, build_grp_name, key, id_to_str(&src[cmd], i));
2485 else
2487 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2489 set_key_fld(key, config_keys[i]);
2490 g_key_file_remove_key(config, build_grp_name, key, NULL);
2495 g_free(key);
2496 return count;
2500 typedef struct ForEachData
2502 GKeyFile *config;
2503 GPtrArray *ft_names;
2504 } ForEachData;
2507 static void foreach_project_filetype(gpointer data, gpointer user_data)
2509 GeanyFiletype *ft = data;
2510 ForEachData *d = user_data;
2511 guint i = 0;
2512 gchar *regkey = g_strdup_printf("%serror_regex", ft->name);
2514 i += build_save_menu_grp(d->config, ft->priv->projfilecmds, GEANY_GBG_FT, ft->name);
2515 i += build_save_menu_grp(d->config, ft->priv->projexeccmds, GEANY_GBG_EXEC, ft->name);
2516 if (!EMPTY(ft->priv->projerror_regex_string))
2518 g_key_file_set_string(d->config, build_grp_name, regkey, ft->priv->projerror_regex_string);
2519 i++;
2521 else
2522 g_key_file_remove_key(d->config, build_grp_name, regkey, NULL);
2523 g_free(regkey);
2524 if (i > 0)
2525 g_ptr_array_add(d->ft_names, ft->name);
2529 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
2530 void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
2532 GeanyFiletype *ft;
2533 GeanyProject *pj;
2534 ForEachData data;
2536 switch (src)
2538 case GEANY_BCS_HOME_FT:
2539 ft = (GeanyFiletype*)ptr;
2540 if (ft == NULL)
2541 return;
2542 build_save_menu_grp(config, ft->priv->homefilecmds, GEANY_GBG_FT, NULL);
2543 build_save_menu_grp(config, ft->priv->homeexeccmds, GEANY_GBG_EXEC, NULL);
2544 if (!EMPTY(ft->priv->homeerror_regex_string))
2545 g_key_file_set_string(config, build_grp_name, "error_regex", ft->priv->homeerror_regex_string);
2546 else
2547 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2548 break;
2549 case GEANY_BCS_PREF:
2550 build_save_menu_grp(config, non_ft_pref, GEANY_GBG_NON_FT, NULL);
2551 build_save_menu_grp(config, exec_pref, GEANY_GBG_EXEC, NULL);
2552 if (!EMPTY(regex_pref))
2553 g_key_file_set_string(config, build_grp_name, "error_regex", regex_pref);
2554 else
2555 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2556 break;
2557 case GEANY_BCS_PROJ:
2558 pj = (GeanyProject*)ptr;
2559 build_save_menu_grp(config, non_ft_proj, GEANY_GBG_NON_FT, NULL);
2560 build_save_menu_grp(config, exec_proj, GEANY_GBG_EXEC, NULL);
2561 if (!EMPTY(regex_proj))
2562 g_key_file_set_string(config, build_grp_name, "error_regex", regex_proj);
2563 else
2564 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2565 if (pj->priv->build_filetypes_list != NULL)
2567 data.config = config;
2568 data.ft_names = g_ptr_array_new();
2569 g_ptr_array_foreach(pj->priv->build_filetypes_list, foreach_project_filetype, (gpointer)(&data));
2570 if (data.ft_names->pdata != NULL)
2571 g_key_file_set_string_list(config, build_grp_name, "filetypes",
2572 (const gchar**)(data.ft_names->pdata), data.ft_names->len);
2573 else
2574 g_key_file_remove_key(config, build_grp_name, "filetypes", NULL);
2575 g_ptr_array_free(data.ft_names, TRUE);
2577 break;
2578 default: /* defaults and GEANY_BCS_FT can't save */
2579 break;
2584 /* FIXME: count is int only because calling code doesn't handle checking its value itself */
2585 void build_set_group_count(GeanyBuildGroup grp, gint count)
2587 guint i, sum;
2589 g_return_if_fail(count >= 0);
2591 if ((guint) count > build_groups_count[grp])
2592 build_groups_count[grp] = (guint) count;
2593 for (i = 0, sum = 0; i < GEANY_GBG_COUNT; ++i)
2594 sum += build_groups_count[i];
2595 build_items_count = sum;
2599 /** Get the count of commands for the group
2601 * Get the number of commands in the group specified by @a grp.
2603 * @param grp the group of the specified menu item.
2605 * @return a count of the number of commands in the group
2608 GEANY_API_SYMBOL
2609 guint build_get_group_count(const GeanyBuildGroup grp)
2611 g_return_val_if_fail(grp < GEANY_GBG_COUNT, 0);
2612 return build_groups_count[grp];
2616 static void on_project_close(void)
2618 /* remove project regexen */
2619 SETPTR(regex_proj, NULL);
2623 static struct
2625 const gchar *label;
2626 const gchar *command;
2627 const gchar *working_dir;
2628 GeanyBuildCommand **ptr;
2629 gint index;
2630 } default_cmds[] = {
2631 { N_("_Make"), "make", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_ALL)},
2632 { N_("Make Custom _Target..."), "make ", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_CUSTOM)},
2633 { N_("Make _Object"), "make %e.o", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)},
2634 { N_("_Execute"), "./%e", NULL, &exec_def, GBO_TO_CMD(GEANY_GBO_EXEC)},
2635 { NULL, NULL, NULL, NULL, 0 }
2639 void build_init(void)
2641 GtkWidget *item;
2642 GtkWidget *toolmenu;
2643 gint cmdindex;
2645 g_signal_connect(geany_object, "project-close", on_project_close, NULL);
2647 ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2648 non_ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2649 exec_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2650 run_info = g_new0(RunInfo, build_groups_count[GEANY_GBG_EXEC]);
2652 for (cmdindex = 0; default_cmds[cmdindex].command != NULL; ++cmdindex)
2654 GeanyBuildCommand *cmd = &((*(default_cmds[cmdindex].ptr))[ default_cmds[cmdindex].index ]);
2655 cmd->exists = TRUE;
2656 cmd->label = g_strdup(_(default_cmds[cmdindex].label));
2657 cmd->command = g_strdup(default_cmds[cmdindex].command);
2658 cmd->working_dir = g_strdup(default_cmds[cmdindex].working_dir);
2661 /* create the toolbar Build item sub menu */
2662 toolmenu = gtk_menu_new();
2663 g_object_ref(toolmenu);
2665 /* build the code */
2666 item = ui_image_menu_item_new(GEANY_STOCK_BUILD, _("_Build"));
2667 gtk_widget_show(item);
2668 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2669 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_build_activate),
2670 GBO_TO_POINTER(GEANY_GBO_BUILD));
2671 widgets.toolitem_build = item;
2673 item = gtk_separator_menu_item_new();
2674 gtk_widget_show(item);
2675 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2677 /* build the code with make all */
2678 item = gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2679 gtk_widget_show(item);
2680 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2681 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2682 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL));
2683 widgets.toolitem_make_all = item;
2685 /* build the code with make custom */
2686 item = gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target..."));
2687 gtk_widget_show(item);
2688 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2689 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2690 GBO_TO_POINTER(GEANY_GBO_CUSTOM));
2691 widgets.toolitem_make_custom = item;
2693 /* build the code with make object */
2694 item = gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2695 gtk_widget_show(item);
2696 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2697 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2698 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT));
2699 widgets.toolitem_make_object = item;
2701 item = gtk_separator_menu_item_new();
2702 gtk_widget_show(item);
2703 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2705 /* arguments */
2706 item = ui_image_menu_item_new(GTK_STOCK_PREFERENCES, _("_Set Build Commands"));
2707 gtk_widget_show(item);
2708 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2709 g_signal_connect(item, "activate", G_CALLBACK(on_set_build_commands_activate), NULL);
2710 widgets.toolitem_set_args = item;
2712 /* get toolbar action pointers */
2713 widgets.build_action = toolbar_get_action_by_name("Build");
2714 widgets.compile_action = toolbar_get_action_by_name("Compile");
2715 widgets.run_action = toolbar_get_action_by_name("Run");
2716 widgets.toolmenu = toolmenu;
2717 /* set the submenu to the toolbar item */
2718 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets.build_action), toolmenu);