Use pre-compiled 'grep' binary from MSYS2 on Windows
[geany-mirror.git] / src / build.c
blob3cfcbe423882f3f7085af1951235f159806e5eec
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 @c 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 @nullable a pointer to the constant string or @c 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 *replacement;
671 gchar *executable = NULL;
672 gint line_num;
674 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
676 stack = g_string_new(src);
677 if (doc != NULL && doc->file_name != NULL)
679 /* replace %f with the filename (including extension) */
680 replacement = g_path_get_basename(doc->file_name);
681 utils_string_replace_all(stack, "%f", replacement);
682 g_free(replacement);
684 /* replace %d with the absolute path of the dir of the current file */
685 replacement = g_path_get_dirname(doc->file_name);
686 utils_string_replace_all(stack, "%d", replacement);
687 g_free(replacement);
689 /* replace %e with the filename (excluding extension) */
690 executable = utils_remove_ext_from_filename(doc->file_name);
691 replacement = g_path_get_basename(executable);
692 utils_string_replace_all(stack, "%e", replacement);
693 g_free(replacement);
695 /* replace %l with the current 1-based line number */
696 line_num = sci_get_current_line(doc->editor->sci) + 1;
697 replacement = g_strdup_printf("%i", line_num);
698 utils_string_replace_all(stack, "%l", replacement);
699 g_free(replacement);
702 /* replace %p with the current project's (absolute) base directory */
703 replacement = NULL; /* prevent double free if no replacement found */
704 if (app->project)
706 replacement = project_get_base_path();
708 else if (strstr(stack->str, "%p"))
709 { /* fall back to %d */
710 ui_set_statusbar(FALSE, _("failed to substitute %%p, no project active"));
711 if (doc != NULL && doc->file_name != NULL)
712 replacement = g_path_get_dirname(doc->file_name);
715 utils_string_replace_all(stack, "%p", replacement);
716 g_free(replacement);
717 g_free(executable);
719 return g_string_free(stack, FALSE); /* don't forget to free src also if needed */
723 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
724 * idx document directory */
725 static void build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
727 GError *error = NULL;
728 gchar *argv[] = { "/bin/sh", "-c", NULL, NULL };
729 gchar *working_dir;
730 gchar *utf8_working_dir;
731 gchar *cmd_string;
733 g_return_if_fail(doc == NULL || doc->is_valid);
735 if ((doc == NULL || EMPTY(doc->file_name)) && EMPTY(dir))
737 geany_debug("Failed to run command with no working directory");
738 ui_set_statusbar(TRUE, _("Process failed, no working directory"));
739 return;
742 clear_all_errors();
743 SETPTR(current_dir_entered, NULL);
745 utf8_working_dir = !EMPTY(dir) ? g_strdup(dir) : g_path_get_dirname(doc->file_name);
746 working_dir = utils_get_locale_from_utf8(utf8_working_dir);
748 gtk_list_store_clear(msgwindow.store_compiler);
749 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
750 msgwin_compiler_add(COLOR_BLUE, _("%s (in directory: %s)"), cmd, utf8_working_dir);
751 g_free(utf8_working_dir);
753 cmd_string = utils_get_locale_from_utf8(cmd);
754 argv[2] = cmd_string;
756 #ifdef G_OS_UNIX
757 cmd = NULL; /* under Unix, use argv to start cmd via sh for compatibility */
758 #else
759 argv[0] = NULL; /* under Windows, run cmd directly */
760 #endif
762 /* set the build info for the message window */
763 g_free(build_info.dir);
764 build_info.dir = g_strdup(working_dir);
765 build_info.file_type_id = (doc == NULL) ? GEANY_FILETYPES_NONE : doc->file_type->id;
766 build_info.message_count = 0;
768 if (!spawn_with_callbacks(working_dir, cmd, argv, NULL, 0, NULL, NULL, build_iofunc,
769 GINT_TO_POINTER(0), 0, build_iofunc, GINT_TO_POINTER(1), 0, build_exit_cb, NULL,
770 &build_info.pid, &error))
772 geany_debug("build command spawning failed: %s", error->message);
773 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
774 g_error_free(error);
777 g_free(working_dir);
778 g_free(cmd_string);
782 /* Returns: NULL if there was an error, or the command to be executed. If Geany is
783 * set to use a run script, the returned value is a path to the script that runs
784 * the command; otherwise the command itself is returned. working_dir is a pointer
785 * to the working directory from which the command is executed. Both strings are
786 * in the locale encoding. */
787 static gchar *prepare_run_cmd(GeanyDocument *doc, gchar **working_dir, guint cmdindex)
789 GeanyBuildCommand *cmd = NULL;
790 const gchar *cmd_working_dir;
791 gboolean autoclose = FALSE;
792 gchar *cmd_string_utf8, *working_dir_utf8, *run_cmd, *cmd_string;
793 GError *error = NULL;
795 cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
797 cmd_string_utf8 = build_replace_placeholder(doc, cmd->command);
798 cmd_working_dir = cmd->working_dir;
799 if (EMPTY(cmd_working_dir))
800 cmd_working_dir = "%d";
801 working_dir_utf8 = build_replace_placeholder(doc, cmd_working_dir);
802 *working_dir = utils_get_locale_from_utf8(working_dir_utf8);
804 if (EMPTY(*working_dir) || ! g_file_test(*working_dir, G_FILE_TEST_EXISTS) ||
805 ! g_file_test(*working_dir, G_FILE_TEST_IS_DIR))
807 ui_set_statusbar(TRUE, _("Invalid working directory \"%s\""),
808 !EMPTY(working_dir_utf8) ? working_dir_utf8 : "<NULL>" );
809 utils_free_pointers(3, cmd_string_utf8, working_dir_utf8, *working_dir, NULL);
810 return NULL;
813 cmd_string = utils_get_locale_from_utf8(cmd_string_utf8);
815 #ifdef HAVE_VTE
816 if (vte_info.have_vte && vc->run_in_vte)
818 if (vc->skip_run_script)
820 utils_free_pointers(2, cmd_string_utf8, working_dir_utf8, NULL);
821 return cmd_string;
823 else
824 /* don't wait for user input at the end of script when we are running in VTE */
825 autoclose = TRUE;
827 #endif
829 run_cmd = build_create_shellscript(*working_dir, cmd_string, autoclose, &error);
830 if (!run_cmd)
832 ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created: %s)"),
833 !EMPTY(cmd_string_utf8) ? cmd_string_utf8 : NULL, error->message);
834 g_error_free(error);
835 g_free(*working_dir);
838 utils_free_pointers(3, cmd_string_utf8, working_dir_utf8, cmd_string, NULL);
839 return run_cmd;
843 static void build_run_cmd(GeanyDocument *doc, guint cmdindex)
845 gchar *working_dir;
846 gchar *run_cmd = NULL;
848 if (! DOC_VALID(doc) || doc->file_name == NULL)
849 return;
851 run_cmd = prepare_run_cmd(doc, &working_dir, cmdindex);
852 if (run_cmd == NULL)
853 return;
855 run_info[cmdindex].file_type_id = doc->file_type->id;
857 #ifdef HAVE_VTE
858 if (vte_info.have_vte && vc->run_in_vte)
860 gchar *vte_cmd;
862 /* VTE expects commands in UTF-8 */
863 SETPTR(run_cmd, utils_get_utf8_from_locale(run_cmd));
864 SETPTR(working_dir, utils_get_utf8_from_locale(working_dir));
866 if (vc->skip_run_script)
867 vte_cmd = g_strconcat(run_cmd, "\n", NULL);
868 else
869 vte_cmd = g_strconcat("\n/bin/sh ", run_cmd, "\n", NULL);
871 vte_cwd(working_dir, TRUE);
872 if (! vte_send_cmd(vte_cmd))
874 const gchar *msg = _("File not executed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
875 ui_set_statusbar(FALSE, "%s", msg);
876 geany_debug("%s", msg);
877 if (!vc->skip_run_script)
878 g_unlink(run_cmd);
881 /* show the VTE */
882 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
883 gtk_widget_grab_focus(vc->vte);
884 msgwin_show_hide(TRUE);
886 run_info[cmdindex].pid = 1;
887 g_free(vte_cmd);
889 else
890 #endif
892 gchar *locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
893 GError *error = NULL;
895 utils_str_replace_all(&locale_term_cmd, "%c", run_cmd);
897 if (spawn_async(working_dir, locale_term_cmd, NULL, NULL, &(run_info[cmdindex].pid),
898 &error))
900 g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
901 (gpointer) &(run_info[cmdindex]));
902 build_menu_update(doc);
904 else
906 gchar *utf8_term_cmd = utils_get_utf8_from_locale(locale_term_cmd);
907 ui_set_statusbar(TRUE, _("Cannot execute build command \"%s\": %s. "
908 "Check the Terminal setting in Preferences"), utf8_term_cmd, error->message);
909 g_free(utf8_term_cmd);
910 g_error_free(error);
911 g_unlink(run_cmd);
912 run_info[cmdindex].pid = (GPid) 0;
916 g_free(working_dir);
917 g_free(run_cmd);
921 static void process_build_output_line(gchar *msg, gint color)
923 gchar *tmp;
924 gchar *filename;
925 gint line;
927 g_strchomp(msg);
929 if (EMPTY(msg))
930 return;
932 if (build_parse_make_dir(msg, &tmp))
934 SETPTR(current_dir_entered, tmp);
936 msgwin_parse_compiler_error_line(msg, current_dir_entered, &filename, &line);
938 if (line != -1 && filename != NULL)
940 GeanyDocument *doc = document_find_by_filename(filename);
942 /* limit number of indicators */
943 if (doc && editor_prefs.use_indicators &&
944 build_info.message_count < GEANY_BUILD_ERR_HIGHLIGHT_MAX)
946 if (line > 0) /* some compilers, like pdflatex report errors on line 0 */
947 line--; /* so only adjust the line number if it is greater than 0 */
948 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line);
950 build_info.message_count++;
951 color = COLOR_RED; /* error message parsed on the line */
953 g_free(filename);
955 msgwin_compiler_add_string(color, msg);
959 static void build_iofunc(GString *string, GIOCondition condition, gpointer data)
961 if (condition & (G_IO_IN | G_IO_PRI))
963 process_build_output_line(string->str,
964 (GPOINTER_TO_INT(data)) ? COLOR_DARK_RED : COLOR_BLACK);
969 gboolean build_parse_make_dir(const gchar *string, gchar **prefix)
971 const gchar *pos;
973 *prefix = NULL;
975 if (string == NULL)
976 return FALSE;
978 if ((pos = strstr(string, "Entering directory")) != NULL)
980 gsize len;
981 gchar *input;
983 /* get the start of the path */
984 pos = strstr(string, "/");
986 if (pos == NULL)
987 return FALSE;
989 input = g_strdup(pos);
991 /* kill the ' at the end of the path */
992 len = strlen(input);
993 input[len - 1] = '\0';
994 input = g_realloc(input, len); /* shorten by 1 */
995 *prefix = input;
997 return TRUE;
1000 if (strstr(string, "Leaving directory") != NULL)
1002 *prefix = NULL;
1003 return TRUE;
1006 return FALSE;
1010 static void show_build_result_message(gboolean failure)
1012 gchar *msg;
1014 if (failure)
1016 msg = _("Compilation failed.");
1017 msgwin_compiler_add_string(COLOR_BLUE, msg);
1018 /* If msgwindow is hidden, user will want to display it to see the error */
1019 if (! ui_prefs.msgwindow_visible)
1021 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1022 msgwin_show_hide(TRUE);
1024 else
1025 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1026 ui_set_statusbar(FALSE, "%s", msg);
1028 else
1030 msg = _("Compilation finished successfully.");
1031 msgwin_compiler_add_string(COLOR_BLUE, msg);
1032 if (! ui_prefs.msgwindow_visible ||
1033 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1034 ui_set_statusbar(FALSE, "%s", msg);
1039 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data)
1041 show_build_result_message(!SPAWN_WIFEXITED(status) || SPAWN_WEXITSTATUS(status) != EXIT_SUCCESS);
1042 utils_beep();
1044 build_info.pid = 0;
1045 /* enable build items again */
1046 build_menu_update(NULL);
1047 ui_progress_bar_stop();
1051 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data)
1053 RunInfo *run_info_data = user_data;
1055 g_spawn_close_pid(child_pid);
1057 run_info_data->pid = 0;
1058 /* reset the stop button and menu item to the original meaning */
1059 build_menu_update(NULL);
1063 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1064 * working_dir and cmd are both in the locale encoding
1065 * it returns the full file name (including path) of the created script in the locale encoding */
1066 static gchar *build_create_shellscript(const gchar *working_dir, const gchar *cmd, gboolean autoclose, GError **error)
1068 gint fd;
1069 gchar *str, *fname;
1070 gboolean success = TRUE;
1071 #ifdef G_OS_WIN32
1072 gchar *expanded_cmd;
1073 #else
1074 gchar *escaped_dir;
1075 #endif
1076 fd = g_file_open_tmp (RUN_SCRIPT_CMD, &fname, error);
1077 if (fd < 0)
1078 return NULL;
1079 close(fd);
1081 #ifdef G_OS_WIN32
1082 /* Expand environment variables like %blah%. */
1083 expanded_cmd = win32_expand_environment_variables(cmd);
1084 str = g_strdup_printf("cd \"%s\"\n\n%s\n\n%s\ndel \"%%0\"\n\npause\n", working_dir, expanded_cmd, (autoclose) ? "" : "pause");
1085 g_free(expanded_cmd);
1086 #else
1087 escaped_dir = g_shell_quote(working_dir);
1088 str = g_strdup_printf(
1089 "#!/bin/sh\n\nrm $0\n\ncd %s\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1090 \n\n%s\n", escaped_dir, cmd, (autoclose) ? "" :
1091 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1092 "dash\ndummy_var=\"\"\nread dummy_var");
1093 g_free(escaped_dir);
1094 #endif
1096 if (!g_file_set_contents(fname, str, -1, error))
1097 success = FALSE;
1098 g_free(str);
1099 #ifdef __APPLE__
1100 if (success && g_chmod(fname, 0777) != 0)
1102 if (error)
1104 gint errsv = errno;
1106 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errsv),
1107 "Failed to make file executable: %s", g_strerror(errsv));
1109 success = FALSE;
1111 #endif
1113 if (!success)
1115 g_unlink(fname);
1116 g_free(fname);
1117 fname = NULL;
1120 return fname;
1124 typedef void Callback(GtkWidget *w, gpointer u);
1126 /* run the command catenating cmd_cat if present */
1127 static void build_command(GeanyDocument *doc, GeanyBuildGroup grp, guint cmd, gchar *cmd_cat)
1129 gchar *dir;
1130 gchar *full_command, *subs_command;
1131 GeanyBuildCommand *buildcmd = get_build_cmd(doc, grp, cmd, NULL);
1132 gchar *cmdstr;
1134 if (buildcmd == NULL)
1135 return;
1137 cmdstr = buildcmd->command;
1139 if (cmd_cat != NULL)
1141 if (cmdstr != NULL)
1142 full_command = g_strconcat(cmdstr, cmd_cat, NULL);
1143 else
1144 full_command = g_strdup(cmd_cat);
1146 else
1147 full_command = cmdstr;
1149 dir = build_replace_placeholder(doc, buildcmd->working_dir);
1150 subs_command = build_replace_placeholder(doc, full_command);
1151 build_info.grp = grp;
1152 build_info.cmd = cmd;
1153 build_spawn_cmd(doc, subs_command, dir);
1154 g_free(subs_command);
1155 g_free(dir);
1156 if (cmd_cat != NULL)
1157 g_free(full_command);
1158 build_menu_update(doc);
1159 if (build_info.pid)
1160 ui_progress_bar_start(NULL);
1164 /*----------------------------------------------------------------
1166 * Create build menu and handle callbacks (&toolbar callbacks)
1168 *----------------------------------------------------------------*/
1169 static void on_make_custom_input_response(const gchar *input, gpointer data)
1171 GeanyDocument *doc = document_get_current();
1173 SETPTR(build_info.custom_target, g_strdup(input));
1174 build_command(doc, GBO_TO_GBG(GEANY_GBO_CUSTOM), GBO_TO_CMD(GEANY_GBO_CUSTOM),
1175 build_info.custom_target);
1179 static void on_build_menu_item(GtkWidget *w, gpointer user_data)
1181 GeanyDocument *doc = document_get_current();
1182 GeanyBuildCommand *bc;
1183 guint grp = GPOINTER_TO_GRP(user_data);
1184 guint cmd = GPOINTER_TO_CMD(user_data);
1186 if (doc && doc->changed)
1188 if (!document_save_file(doc, FALSE))
1189 return;
1191 g_signal_emit_by_name(geany_object, "build-start");
1193 if (grp == GEANY_GBG_NON_FT && cmd == GBO_TO_CMD(GEANY_GBO_CUSTOM))
1195 static GtkWidget *dialog = NULL; /* keep dialog for combo history */
1197 if (! dialog)
1199 dialog = dialogs_show_input_persistent(_("Custom Text"), GTK_WINDOW(main_widgets.window),
1200 _("Enter custom text here, all entered text is appended to the command."),
1201 build_info.custom_target, &on_make_custom_input_response, NULL);
1203 else
1205 gtk_widget_show(dialog);
1207 return;
1209 else if (grp == GEANY_GBG_EXEC)
1211 if (run_info[cmd].pid > (GPid) 1)
1213 kill_process(&run_info[cmd].pid);
1214 return;
1216 bc = get_build_cmd(doc, grp, cmd, NULL);
1217 if (bc != NULL && strcmp(bc->command, "builtin") == 0)
1219 gchar *uri;
1220 if (doc == NULL)
1221 return;
1222 uri = g_strconcat("file:///", g_path_skip_root(doc->file_name), NULL);
1223 utils_open_browser(uri);
1224 g_free(uri);
1227 else
1228 build_run_cmd(doc, cmd);
1230 else
1231 build_command(doc, grp, cmd, NULL);
1235 /* group codes for menu items other than the known commands
1236 * value order is important, see the following table for use */
1238 /* the rest in each group */
1239 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1240 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1241 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1242 /* the separator */
1243 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1244 /* the fixed items */
1245 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1246 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1247 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1248 #define MENU_DONE (MENU_COMMANDS + 1)
1251 static struct BuildMenuItemSpec {
1252 const gchar *stock_id;
1253 const gint key_binding;
1254 const guint build_grp;
1255 const guint build_cmd;
1256 const gchar *fix_label;
1257 Callback *cb;
1258 } build_menu_specs[] = {
1259 {GTK_STOCK_CONVERT, GEANY_KEYS_BUILD_COMPILE, GBO_TO_GBG(GEANY_GBO_COMPILE),
1260 GBO_TO_CMD(GEANY_GBO_COMPILE), NULL, on_build_menu_item},
1261 {GEANY_STOCK_BUILD, GEANY_KEYS_BUILD_LINK, GBO_TO_GBG(GEANY_GBO_BUILD),
1262 GBO_TO_CMD(GEANY_GBO_BUILD), NULL, on_build_menu_item},
1263 {NULL, -1, MENU_FT_REST,
1264 GBO_TO_CMD(GEANY_GBO_BUILD) + 1, NULL, on_build_menu_item},
1265 {NULL, -1, MENU_SEPARATOR,
1266 GBF_SEP_1, NULL, NULL},
1267 {NULL, GEANY_KEYS_BUILD_MAKE, GBO_TO_GBG(GEANY_GBO_MAKE_ALL),
1268 GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL, on_build_menu_item},
1269 {NULL, GEANY_KEYS_BUILD_MAKEOWNTARGET, GBO_TO_GBG(GEANY_GBO_CUSTOM),
1270 GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL, on_build_menu_item},
1271 {NULL, GEANY_KEYS_BUILD_MAKEOBJECT, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT),
1272 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL, on_build_menu_item},
1273 {NULL, -1, MENU_NON_FT_REST,
1274 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT) + 1, NULL, on_build_menu_item},
1275 {NULL, -1, MENU_SEPARATOR,
1276 GBF_SEP_2, NULL, NULL},
1277 {GTK_STOCK_GO_DOWN, GEANY_KEYS_BUILD_NEXTERROR, MENU_NEXT_ERROR,
1278 GBF_NEXT_ERROR, N_("_Next Error"), on_build_next_error},
1279 {GTK_STOCK_GO_UP, GEANY_KEYS_BUILD_PREVIOUSERROR, MENU_PREV_ERROR,
1280 GBF_PREV_ERROR, N_("_Previous Error"), on_build_previous_error},
1281 {NULL, -1, MENU_SEPARATOR,
1282 GBF_SEP_3, NULL, NULL},
1283 {GTK_STOCK_EXECUTE, GEANY_KEYS_BUILD_RUN, GBO_TO_GBG(GEANY_GBO_EXEC),
1284 GBO_TO_CMD(GEANY_GBO_EXEC), NULL, on_build_menu_item},
1285 {NULL, -1, MENU_EXEC_REST,
1286 GBO_TO_CMD(GEANY_GBO_EXEC) + 1, NULL, on_build_menu_item},
1287 {NULL, -1, MENU_SEPARATOR,
1288 GBF_SEP_4, NULL, NULL},
1289 {GTK_STOCK_PREFERENCES, GEANY_KEYS_BUILD_OPTIONS, MENU_COMMANDS,
1290 GBF_COMMANDS, N_("_Set Build Commands"), on_set_build_commands_activate},
1291 {NULL, -1, MENU_DONE,
1292 0, NULL, NULL}
1296 static void create_build_menu_item(GtkWidget *menu, GeanyKeyGroup *group, GtkAccelGroup *ag,
1297 struct BuildMenuItemSpec *bs, const gchar *lbl, guint grp, guint cmd)
1299 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(lbl);
1301 if (bs->stock_id != NULL)
1303 GtkWidget *image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1304 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1306 gtk_widget_show(item);
1307 if (bs->key_binding >= 0)
1308 add_menu_accel(group, (guint) bs->key_binding, ag, item);
1309 gtk_container_add(GTK_CONTAINER(menu), item);
1310 if (bs->cb != NULL)
1312 g_signal_connect(item, "activate", G_CALLBACK(bs->cb), GRP_CMD_TO_POINTER(grp,cmd));
1314 menu_items.menu_item[grp][cmd] = item;
1318 static void create_build_menu(BuildMenuItems *build_menu_items)
1320 GtkWidget *menu;
1321 GtkAccelGroup *accel_group = gtk_accel_group_new();
1322 GeanyKeyGroup *keygroup = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
1323 guint i, j;
1325 menu = gtk_menu_new();
1326 build_menu_items->menu_item[GEANY_GBG_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_FT]);
1327 build_menu_items->menu_item[GEANY_GBG_NON_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_NON_FT]);
1328 build_menu_items->menu_item[GEANY_GBG_EXEC] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_EXEC]);
1329 build_menu_items->menu_item[GBG_FIXED] = g_new0(GtkWidget*, GBF_COUNT);
1331 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1333 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1334 if (bs->build_grp == MENU_SEPARATOR)
1336 GtkWidget *item = gtk_separator_menu_item_new();
1337 gtk_widget_show(item);
1338 gtk_container_add(GTK_CONTAINER(menu), item);
1339 build_menu_items->menu_item[GBG_FIXED][bs->build_cmd] = item;
1341 else if (bs->fix_label != NULL)
1343 create_build_menu_item(menu, keygroup, accel_group, bs, _(bs->fix_label),
1344 GBG_FIXED, bs->build_cmd);
1346 else if (bs->build_grp >= MENU_FT_REST && bs->build_grp <= MENU_SEPARATOR)
1348 guint grp = bs->build_grp - GEANY_GBG_COUNT;
1349 for (j = bs->build_cmd; j < build_groups_count[grp]; ++j)
1351 GeanyBuildCommand *bc = get_build_cmd(NULL, grp, j, NULL);
1352 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1353 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, grp, j);
1356 else
1358 GeanyBuildCommand *bc = get_build_cmd(NULL, bs->build_grp, bs->build_cmd, NULL);
1359 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1360 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, bs->build_grp, bs->build_cmd);
1363 build_menu_items->menu = menu;
1364 gtk_widget_show(menu);
1365 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_build1")), menu);
1369 /* portability to various GTK versions needs checking
1370 * conforms to description of gtk_accel_label as child of menu item
1371 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1372 static void geany_menu_item_set_label(GtkWidget *w, const gchar *label)
1374 GtkWidget *c = gtk_bin_get_child(GTK_BIN(w));
1376 gtk_label_set_text_with_mnemonic(GTK_LABEL(c), label);
1380 /* * Update the build menu to reflect changes in configuration or status.
1382 * Sets the labels and number of visible items to match the highest
1383 * priority configured commands. Also sets sensitivity if build commands are
1384 * running and switches executes to stop when commands are running.
1386 * @param doc The current document, if available, to save looking it up.
1387 * If @c NULL it will be looked up.
1389 * Call this after modifying any fields of a GeanyBuildCommand structure.
1391 * @see Build Menu Configuration section of the Manual.
1394 void build_menu_update(GeanyDocument *doc)
1396 guint i, cmdcount, cmd, grp;
1397 gboolean vis = FALSE;
1398 gboolean have_path, build_running, exec_running, have_errors, cmd_sensitivity;
1399 gboolean can_compile, can_build, can_make, run_sensitivity = FALSE, run_running = FALSE;
1400 GeanyBuildCommand *bc;
1402 g_return_if_fail(doc == NULL || doc->is_valid);
1404 if (menu_items.menu == NULL)
1405 create_build_menu(&menu_items);
1406 if (doc == NULL)
1407 doc = document_get_current();
1408 have_path = doc != NULL && doc->file_name != NULL;
1409 build_running = build_info.pid > (GPid) 1;
1410 have_errors = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_compiler), NULL) > 0;
1411 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1413 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1414 switch (bs->build_grp)
1416 case MENU_SEPARATOR:
1417 if (vis == TRUE)
1419 gtk_widget_show_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1420 vis = FALSE;
1422 else
1423 gtk_widget_hide(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1424 break;
1425 case MENU_NEXT_ERROR:
1426 case MENU_PREV_ERROR:
1427 gtk_widget_set_sensitive(menu_items.menu_item[GBG_FIXED][bs->build_cmd], have_errors);
1428 vis |= TRUE;
1429 break;
1430 case MENU_COMMANDS:
1431 vis |= TRUE;
1432 break;
1433 default: /* all configurable commands */
1434 if (bs->build_grp >= GEANY_GBG_COUNT)
1436 grp = bs->build_grp - GEANY_GBG_COUNT;
1437 cmdcount = build_groups_count[grp];
1439 else
1441 grp = bs->build_grp;
1442 cmdcount = bs->build_cmd + 1;
1444 for (cmd = bs->build_cmd; cmd < cmdcount; ++cmd)
1446 GtkWidget *menu_item = menu_items.menu_item[grp][cmd];
1447 const gchar *label;
1448 bc = get_build_cmd(doc, grp, cmd, NULL);
1449 if (bc)
1450 label = bc->label;
1451 else
1452 label = NULL;
1454 if (grp < GEANY_GBG_EXEC)
1456 cmd_sensitivity =
1457 (grp == GEANY_GBG_FT && bc != NULL && have_path && ! build_running) ||
1458 (grp == GEANY_GBG_NON_FT && bc != NULL && ! build_running);
1459 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1460 if (bc != NULL && !EMPTY(label))
1462 geany_menu_item_set_label(menu_item, label);
1463 gtk_widget_show_all(menu_item);
1464 vis |= TRUE;
1466 else
1467 gtk_widget_hide(menu_item);
1469 else
1471 GtkWidget *image;
1472 exec_running = run_info[cmd].pid > (GPid) 1;
1473 cmd_sensitivity = (bc != NULL) || exec_running;
1474 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1475 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1476 run_sensitivity = cmd_sensitivity;
1477 if (! exec_running)
1479 image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1481 else
1483 image = gtk_image_new_from_stock(GTK_STOCK_STOP, GTK_ICON_SIZE_MENU);
1485 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1486 run_running = exec_running;
1487 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
1488 if (bc != NULL && !EMPTY(label))
1490 geany_menu_item_set_label(menu_item, label);
1491 gtk_widget_show_all(menu_item);
1492 vis |= TRUE;
1494 else
1495 gtk_widget_hide(menu_item);
1501 run_sensitivity &= (doc != NULL);
1502 can_build = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_BUILD), NULL) != NULL
1503 && have_path && ! build_running;
1504 if (widgets.toolitem_build != NULL)
1505 gtk_widget_set_sensitive(widgets.toolitem_build, can_build);
1506 can_make = FALSE;
1507 if (widgets.toolitem_make_all != NULL)
1508 gtk_widget_set_sensitive(widgets.toolitem_make_all,
1509 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL) != NULL
1510 && ! build_running));
1511 if (widgets.toolitem_make_custom != NULL)
1512 gtk_widget_set_sensitive(widgets.toolitem_make_custom,
1513 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL) != NULL
1514 && ! build_running));
1515 if (widgets.toolitem_make_object != NULL)
1516 gtk_widget_set_sensitive(widgets.toolitem_make_object,
1517 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL) != NULL
1518 && ! build_running));
1519 if (widgets.toolitem_set_args != NULL)
1520 gtk_widget_set_sensitive(widgets.toolitem_set_args, TRUE);
1522 can_compile = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_COMPILE), NULL) != NULL
1523 && have_path && ! build_running;
1524 gtk_action_set_sensitive(widgets.compile_action, can_compile);
1525 gtk_action_set_sensitive(widgets.build_action, can_make);
1526 gtk_action_set_sensitive(widgets.run_action, run_sensitivity);
1528 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1529 set_stop_button(run_running);
1534 /* Call build_menu_update() instead of calling this directly. */
1535 static void set_stop_button(gboolean stop)
1537 const gchar *button_stock_id = NULL;
1538 GtkToolButton *run_button;
1540 run_button = GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1541 if (run_button != NULL)
1542 button_stock_id = gtk_tool_button_get_stock_id(run_button);
1544 if (stop && utils_str_equal(button_stock_id, GTK_STOCK_STOP))
1545 return;
1546 if (! stop && utils_str_equal(button_stock_id, GTK_STOCK_EXECUTE))
1547 return;
1549 /* use the run button also as stop button */
1550 if (stop)
1552 if (run_button != NULL)
1553 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_STOP);
1555 else
1557 if (run_button != NULL)
1558 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_EXECUTE);
1563 static void on_set_build_commands_activate(GtkWidget *w, gpointer u)
1565 /* For now, just show the project dialog */
1566 if (app->project)
1567 project_build_properties();
1568 else
1569 show_build_commands_dialog();
1573 static void on_toolbutton_build_activate(GtkWidget *menuitem, gpointer user_data)
1575 last_toolbutton_action = user_data;
1576 g_object_set(widgets.build_action, "tooltip", _("Build the current file"), NULL);
1577 on_build_menu_item(menuitem, user_data);
1581 static void on_toolbutton_make_activate(GtkWidget *menuitem, gpointer user_data)
1583 gchar *msg;
1585 last_toolbutton_action = user_data;
1586 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_ALL))
1587 msg = _("Build the current file with Make and the default target");
1588 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_CUSTOM))
1589 msg = _("Build the current file with Make and the specified target");
1590 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT))
1591 msg = _("Compile the current file with Make");
1592 else
1593 msg = NULL;
1594 g_object_set(widgets.build_action, "tooltip", msg, NULL);
1595 on_build_menu_item(menuitem, user_data);
1599 static void kill_process(GPid *pid)
1601 GError *error = NULL;
1603 if (spawn_kill_process(*pid, &error))
1605 *pid = 0;
1606 build_menu_update(NULL);
1608 else
1610 ui_set_statusbar(TRUE, _("Process could not be stopped (%s)."), error->message);
1611 g_error_free(error);
1616 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data)
1618 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_compiler),
1619 msgwin_goto_compiler_file_line))
1621 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1623 else
1624 ui_set_statusbar(FALSE, _("No more build errors."));
1628 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data)
1630 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_compiler),
1631 msgwin_goto_compiler_file_line))
1633 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1635 else
1636 ui_set_statusbar(FALSE, _("No more build errors."));
1640 void build_toolbutton_build_clicked(GtkAction *action, gpointer unused)
1642 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_BUILD))
1644 on_build_menu_item(NULL, GBO_TO_POINTER(GEANY_GBO_BUILD));
1646 else
1648 on_build_menu_item(NULL, last_toolbutton_action);
1653 /*------------------------------------------------------
1655 * Create and handle the build menu configuration dialog
1657 *-------------------------------------------------------*/
1658 typedef struct RowWidgets
1660 GtkWidget *entries[GEANY_BC_CMDENTRIES_COUNT];
1661 GeanyBuildSource src;
1662 GeanyBuildSource dst;
1663 GeanyBuildCommand *cmdsrc;
1664 guint grp;
1665 guint cmd;
1666 gboolean cleared;
1667 gboolean used_dst;
1668 } RowWidgets;
1670 #if GTK_CHECK_VERSION(3,0,0)
1671 typedef GdkRGBA InsensitiveColor;
1672 #else
1673 typedef GdkColor InsensitiveColor;
1674 #endif
1675 static InsensitiveColor insensitive_color;
1677 static void set_row_color(RowWidgets *r, InsensitiveColor *color)
1679 enum GeanyBuildCmdEntries i;
1681 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1683 if (i == GEANY_BC_LABEL)
1684 continue;
1686 #if GTK_CHECK_VERSION(3,0,0)
1687 gtk_widget_override_color(r->entries[i], GTK_STATE_FLAG_NORMAL, color);
1688 #else
1689 gtk_widget_modify_text(r->entries[i], GTK_STATE_NORMAL, color);
1690 #endif
1695 static void set_build_command_entry_text(GtkWidget *wid, const gchar *text)
1697 if (GTK_IS_BUTTON(wid))
1698 gtk_button_set_label(GTK_BUTTON(wid), text);
1699 else
1700 gtk_entry_set_text(GTK_ENTRY(wid), text);
1704 static void on_clear_dialog_row(GtkWidget *unused, gpointer user_data)
1706 RowWidgets *r = user_data;
1707 guint src;
1708 enum GeanyBuildCmdEntries i;
1709 GeanyBuildCommand *bc = get_next_build_cmd(NULL, r->grp, r->cmd, r->dst, &src);
1711 if (bc != NULL)
1713 r->cmdsrc = bc;
1714 r->src = src;
1715 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1717 set_build_command_entry_text(r->entries[i],
1718 id_to_str(bc,i) != NULL ? id_to_str(bc,i) : "");
1721 else
1723 r->cmdsrc = NULL;
1724 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1726 set_build_command_entry_text(r->entries[i], "");
1729 r->used_dst = FALSE;
1730 set_row_color(r, &insensitive_color);
1731 r->cleared = TRUE;
1735 static void on_clear_dialog_regex_row(GtkEntry *regex, gpointer unused)
1737 gtk_entry_set_text(regex,"");
1741 static void on_label_button_clicked(GtkWidget *wid, gpointer user_data)
1743 RowWidgets *r = user_data;
1744 GtkWidget *top_level = gtk_widget_get_toplevel(wid);
1745 const gchar *old = gtk_button_get_label(GTK_BUTTON(wid));
1746 gchar *str;
1748 if (gtk_widget_is_toplevel(top_level) && GTK_IS_WINDOW(top_level))
1749 str = dialogs_show_input(_("Set menu item label"), GTK_WINDOW(top_level), NULL, old);
1750 else
1751 str = dialogs_show_input(_("Set menu item label"), NULL, NULL, old);
1753 if (!str)
1754 return;
1756 gtk_button_set_label(GTK_BUTTON(wid), str);
1757 g_free(str);
1758 r->used_dst = TRUE;
1759 set_row_color(r, NULL);
1763 static void on_entry_focus(GtkWidget *wid, GdkEventFocus *unused, gpointer user_data)
1765 RowWidgets *r = user_data;
1767 r->used_dst = TRUE;
1768 set_row_color(r, NULL);
1772 /* Column headings, array NULL-terminated */
1773 static const gchar *colheads[] =
1775 "#",
1776 N_("Label"),
1777 N_("Command"),
1778 N_("Working directory"),
1779 N_("Reset"),
1780 NULL
1783 /* column names */
1784 #define DC_ITEM 0
1785 #define DC_ENTRIES 1
1786 #define DC_CLEAR 4
1787 #define DC_N_COL 5
1789 static const guint entry_x_padding = 3;
1790 static const guint entry_y_padding = 0;
1793 static RowWidgets *build_add_dialog_row(GeanyDocument *doc, GtkTable *table, guint row,
1794 GeanyBuildSource dst, guint grp, guint cmd, gboolean dir)
1796 GtkWidget *label, *clear, *clearicon;
1797 RowWidgets *roww;
1798 GeanyBuildCommand *bc;
1799 guint src;
1800 enum GeanyBuildCmdEntries i;
1801 guint column = 0;
1802 gchar *text;
1804 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
1806 text = g_strdup_printf("%d.", cmd + 1);
1807 label = gtk_label_new(text);
1808 g_free(text);
1809 #if GTK_CHECK_VERSION(3,0,0)
1811 GtkStyleContext *ctx = gtk_widget_get_style_context(label);
1813 gtk_style_context_save(ctx);
1814 gtk_style_context_get_color(ctx, GTK_STATE_FLAG_INSENSITIVE, &insensitive_color);
1815 gtk_style_context_restore(ctx);
1817 #else
1818 insensitive_color = gtk_widget_get_style(label)->text[GTK_STATE_INSENSITIVE];
1819 #endif
1820 gtk_table_attach(table, label, column, column + 1, row, row + 1, GTK_FILL,
1821 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1822 roww = g_new0(RowWidgets, 1);
1823 roww->src = GEANY_BCS_COUNT;
1824 roww->grp = grp;
1825 roww->cmd = cmd;
1826 roww->dst = dst;
1827 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1829 gint xflags = (i == GEANY_BC_COMMAND) ? GTK_FILL | GTK_EXPAND : GTK_FILL;
1831 column += 1;
1832 if (i == GEANY_BC_LABEL)
1834 GtkWidget *wid = roww->entries[i] = gtk_button_new();
1835 gtk_button_set_use_underline(GTK_BUTTON(wid), TRUE);
1836 gtk_widget_set_tooltip_text(wid, _("Click to set menu item label"));
1837 g_signal_connect(wid, "clicked", G_CALLBACK(on_label_button_clicked), roww);
1839 else
1841 roww->entries[i] = gtk_entry_new();
1842 g_signal_connect(roww->entries[i], "focus-in-event", G_CALLBACK(on_entry_focus), roww);
1844 gtk_table_attach(table, roww->entries[i], column, column + 1, row, row + 1, xflags,
1845 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1847 column++;
1848 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1849 clear = gtk_button_new();
1850 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1851 g_signal_connect(clear, "clicked", G_CALLBACK(on_clear_dialog_row), roww);
1852 gtk_table_attach(table, clear, column, column + 1, row, row + 1, GTK_FILL,
1853 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1854 roww->cmdsrc = bc = get_build_cmd(doc, grp, cmd, &src);
1855 if (bc != NULL)
1856 roww->src = src;
1858 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1860 const gchar *str = "";
1862 if (bc != NULL )
1864 if ((str = id_to_str(bc, i)) == NULL)
1865 str = "";
1866 else if (dst == src)
1867 roww->used_dst = TRUE;
1869 set_build_command_entry_text(roww->entries[i], str);
1871 if (bc != NULL && (dst > src))
1872 set_row_color(roww, &insensitive_color);
1873 if (bc != NULL && (src > dst || (grp == GEANY_GBG_FT && (doc == NULL || doc->file_type == NULL))))
1875 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1876 gtk_widget_set_sensitive(roww->entries[i], FALSE);
1877 gtk_widget_set_sensitive(clear, FALSE);
1879 return roww;
1883 typedef struct BuildTableFields
1885 RowWidgets **rows;
1886 GtkWidget *fileregex;
1887 GtkWidget *nonfileregex;
1888 gchar **fileregexstring;
1889 gchar **nonfileregexstring;
1890 } BuildTableFields;
1893 GtkWidget *build_commands_table(GeanyDocument *doc, GeanyBuildSource dst, BuildTableData *table_data,
1894 GeanyFiletype *ft)
1896 GtkWidget *label, *sep, *clearicon, *clear;
1897 BuildTableFields *fields;
1898 GtkTable *table;
1899 const gchar **ch;
1900 gchar *txt;
1901 guint col, row, cmdindex;
1902 guint cmd;
1903 guint src;
1904 gboolean sensitivity;
1905 guint sep_padding = entry_y_padding + 3;
1907 table = GTK_TABLE(gtk_table_new(build_items_count + 12, 5, FALSE));
1908 fields = g_new0(BuildTableFields, 1);
1909 fields->rows = g_new0(RowWidgets*, build_items_count);
1910 for (ch = colheads, col = 0; *ch != NULL; ch++, col++)
1912 label = gtk_label_new(_(*ch));
1913 gtk_table_attach(table, label, col, col + 1, 0, 1,
1914 GTK_FILL, GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1916 sep = gtk_hseparator_new();
1917 gtk_table_attach(table, sep, 0, DC_N_COL, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND,
1918 entry_x_padding, sep_padding);
1919 if (ft != NULL && ft->id != GEANY_FILETYPES_NONE)
1920 txt = g_strdup_printf(_("%s commands"), ft->name);
1921 else
1922 txt = g_strdup_printf(_("%s commands"), _("No filetype"));
1924 label = ui_label_new_bold(txt);
1925 g_free(txt);
1926 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1927 gtk_table_attach(table, label, 0, DC_N_COL, 2, 3, GTK_FILL, GTK_FILL | GTK_EXPAND,
1928 entry_x_padding, entry_y_padding);
1929 for (row = 3, cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++row, ++cmdindex, ++cmd)
1930 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_FT, cmd, FALSE);
1931 label = gtk_label_new(_("Error regular expression:"));
1932 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1933 entry_x_padding, entry_y_padding);
1934 fields->fileregex = gtk_entry_new();
1935 fields->fileregexstring = build_get_regex(GEANY_GBG_FT, NULL, &src);
1936 sensitivity = (ft == NULL) ? FALSE : TRUE;
1937 if (fields->fileregexstring != NULL && *(fields->fileregexstring) != NULL)
1939 gtk_entry_set_text(GTK_ENTRY(fields->fileregex), *(fields->fileregexstring));
1940 if (src > dst)
1941 sensitivity = FALSE;
1943 gtk_table_attach(table, fields->fileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
1944 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1945 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1946 clear = gtk_button_new();
1947 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1948 g_signal_connect_swapped(clear, "clicked",
1949 G_CALLBACK(on_clear_dialog_regex_row), (fields->fileregex));
1950 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
1951 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1952 gtk_widget_set_sensitive(fields->fileregex, sensitivity);
1953 gtk_widget_set_sensitive(clear, sensitivity);
1954 ++row;
1955 sep = gtk_hseparator_new();
1956 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1957 entry_x_padding, sep_padding);
1958 ++row;
1959 label = ui_label_new_bold(_("Independent commands"));
1960 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1961 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1962 entry_x_padding, entry_y_padding);
1963 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++row, ++cmdindex, ++cmd)
1964 fields->rows[cmdindex] = build_add_dialog_row(
1965 doc, table, row, dst, GEANY_GBG_NON_FT, cmd, TRUE);
1966 label = gtk_label_new(_("Error regular expression:"));
1967 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL,
1968 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1969 fields->nonfileregex = gtk_entry_new();
1970 fields->nonfileregexstring = build_get_regex(GEANY_GBG_NON_FT, NULL, &src);
1971 sensitivity = TRUE;
1972 if (fields->nonfileregexstring != NULL && *(fields->nonfileregexstring) != NULL)
1974 gtk_entry_set_text(GTK_ENTRY(fields->nonfileregex), *(fields->nonfileregexstring));
1975 sensitivity = src > dst ? FALSE : TRUE;
1977 gtk_table_attach(table, fields->nonfileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
1978 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1979 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1980 clear = gtk_button_new();
1981 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1982 g_signal_connect_swapped(clear, "clicked",
1983 G_CALLBACK(on_clear_dialog_regex_row), (fields->nonfileregex));
1984 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
1985 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1986 gtk_widget_set_sensitive(fields->nonfileregex, sensitivity);
1987 gtk_widget_set_sensitive(clear, sensitivity);
1988 ++row;
1989 label = gtk_label_new(NULL);
1990 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
1991 _("Note: Item 2 opens a dialog and appends the response to the command."));
1992 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1993 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1994 entry_x_padding, entry_y_padding);
1995 ++row;
1996 sep = gtk_hseparator_new();
1997 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
1998 entry_x_padding, sep_padding);
1999 ++row;
2000 label = ui_label_new_bold(_("Execute commands"));
2001 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2002 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2003 entry_x_padding, entry_y_padding);
2004 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++row, ++cmdindex, ++cmd)
2005 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_EXEC, cmd, TRUE);
2006 sep = gtk_hseparator_new();
2007 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2008 entry_x_padding, sep_padding);
2009 ++row;
2010 label = gtk_label_new(NULL);
2011 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2012 _("%d, %e, %f, %p, %l are substituted in command and directory fields, see manual for details."));
2013 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2014 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2015 entry_x_padding, entry_y_padding);
2016 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
2017 ++row;
2018 *table_data = fields;
2019 return GTK_WIDGET(table);
2023 void build_free_fields(BuildTableData table_data)
2025 guint cmdindex;
2027 for (cmdindex = 0; cmdindex < build_items_count; ++cmdindex)
2028 g_free(table_data->rows[cmdindex]);
2029 g_free(table_data->rows);
2030 g_free(table_data);
2034 /* string compare where null pointers match null or 0 length strings */
2035 #if 0
2036 static gint stcmp(const gchar *a, const gchar *b)
2038 if (a == NULL && b == NULL)
2039 return 0;
2040 if (a == NULL && b != NULL)
2041 return strlen(b);
2042 if (a != NULL && b == NULL)
2043 return strlen(a);
2044 return strcmp(a, b);
2046 #endif
2049 static const gchar *get_build_command_entry_text(GtkWidget *wid)
2051 if (GTK_IS_BUTTON(wid))
2052 return gtk_button_get_label(GTK_BUTTON(wid));
2053 else
2054 return gtk_entry_get_text(GTK_ENTRY(wid));
2058 static gboolean read_row(BuildDestination *dst, BuildTableData table_data, guint drow, guint grp, guint cmd)
2060 gchar *entries[GEANY_BC_CMDENTRIES_COUNT];
2061 gboolean changed = FALSE;
2062 enum GeanyBuildCmdEntries i;
2064 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2066 entries[i] = g_strdup(get_build_command_entry_text(table_data->rows[drow]->entries[i]));
2068 if (table_data->rows[drow]->cleared)
2070 if (dst->dst[grp] != NULL)
2072 if (*(dst->dst[grp]) == NULL)
2073 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2074 (*(dst->dst[grp]))[cmd].exists = FALSE;
2075 (*(dst->dst[grp]))[cmd].changed = TRUE;
2076 changed = TRUE;
2079 if (table_data->rows[drow]->used_dst == TRUE)
2081 if (dst->dst[grp] != NULL)
2083 if (*(dst->dst[grp]) == NULL)
2084 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2085 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2086 set_command(&(*(dst->dst[grp]))[cmd], i, entries[i]);
2087 (*(dst->dst[grp]))[cmd].exists = TRUE;
2088 (*(dst->dst[grp]))[cmd].changed = TRUE;
2089 changed = TRUE;
2092 else
2094 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2095 g_free(entries[i]);
2097 return changed;
2101 static gboolean read_regex(GtkWidget *regexentry, gchar **src, gchar **dst)
2103 gboolean changed = FALSE;
2104 const gchar *reg = gtk_entry_get_text(GTK_ENTRY(regexentry));
2106 if (((src == NULL /* originally there was no regex */
2107 || *src == NULL) /* or it was NULL*/
2108 && !EMPTY(reg)) /* and something was typed */
2109 || (src != NULL /* originally there was a regex*/
2110 && (*src == NULL /* and either it was NULL */
2111 || strcmp(*src, reg) != 0))) /* or it has been changed */
2113 if (dst != NULL)
2115 SETPTR(*dst, g_strdup(reg));
2116 changed = TRUE;
2119 return changed;
2123 static gboolean build_read_commands(BuildDestination *dst, BuildTableData table_data, gint response)
2125 guint cmdindex, cmd;
2126 gboolean changed = FALSE;
2128 if (response == GTK_RESPONSE_ACCEPT)
2130 for (cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++cmdindex, ++cmd)
2131 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_FT, cmd);
2132 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++cmdindex, ++cmd)
2133 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_NON_FT, cmd);
2134 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++cmdindex, ++cmd)
2135 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_EXEC, cmd);
2136 changed |= read_regex(table_data->fileregex, table_data->fileregexstring, dst->fileregexstr);
2137 changed |= read_regex(table_data->nonfileregex, table_data->nonfileregexstring, dst->nonfileregexstr);
2139 return changed;
2143 void build_read_project(GeanyFiletype *ft, BuildTableData build_properties)
2145 BuildDestination menu_dst;
2147 if (ft != NULL)
2149 menu_dst.dst[GEANY_GBG_FT] = &(ft->priv->projfilecmds);
2150 menu_dst.fileregexstr = &(ft->priv->projerror_regex_string);
2152 else
2154 menu_dst.dst[GEANY_GBG_FT] = NULL;
2155 menu_dst.fileregexstr = NULL;
2157 menu_dst.dst[GEANY_GBG_NON_FT] = &non_ft_proj;
2158 menu_dst.dst[GEANY_GBG_EXEC] = &exec_proj;
2159 menu_dst.nonfileregexstr = &regex_proj;
2161 build_read_commands(&menu_dst, build_properties, GTK_RESPONSE_ACCEPT);
2165 static void show_build_commands_dialog(void)
2167 GtkWidget *dialog, *table, *vbox;
2168 GeanyDocument *doc = document_get_current();
2169 GeanyFiletype *ft = NULL;
2170 const gchar *title = _("Set Build Commands");
2171 gint response;
2172 BuildTableData table_data;
2173 BuildDestination prefdsts;
2175 if (doc != NULL)
2176 ft = doc->file_type;
2177 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
2178 GTK_DIALOG_DESTROY_WITH_PARENT,
2179 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2180 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
2181 table = build_commands_table(doc, GEANY_BCS_PREF, &table_data, ft);
2182 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
2183 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
2184 gtk_widget_show_all(dialog);
2185 /* run modally to prevent user changing idx filetype */
2186 response = gtk_dialog_run(GTK_DIALOG(dialog));
2188 prefdsts.dst[GEANY_GBG_NON_FT] = &non_ft_pref;
2189 if (ft != NULL)
2191 prefdsts.dst[GEANY_GBG_FT] = &(ft->priv->homefilecmds);
2192 prefdsts.fileregexstr = &(ft->priv->homeerror_regex_string);
2193 prefdsts.dst[GEANY_GBG_EXEC] = &(ft->priv->homeexeccmds);
2195 else
2197 prefdsts.dst[GEANY_GBG_FT] = NULL;
2198 prefdsts.fileregexstr = NULL;
2199 prefdsts.dst[GEANY_GBG_EXEC] = NULL;
2201 prefdsts.nonfileregexstr = &regex_pref;
2202 if (build_read_commands(&prefdsts, table_data, response) && ft != NULL)
2203 filetypes_save_commands(ft);
2204 build_free_fields(table_data);
2206 build_menu_update(doc);
2207 gtk_widget_destroy(dialog);
2211 /* Creates the relevant build menu if necessary. */
2212 BuildMenuItems *build_get_menu_items(gint filetype_idx)
2214 BuildMenuItems *items;
2216 items = &menu_items;
2217 if (items->menu == NULL)
2218 create_build_menu(items);
2219 return items;
2223 /*----------------------------------------------------------
2225 * Load and store configuration
2227 * ---------------------------------------------------------*/
2228 static const gchar *build_grp_name = "build-menu";
2230 /* config format for build-menu group is prefix_gg_nn_xx=value
2231 * where gg = FT, NF, EX for the command group
2232 * nn = 2 digit command number
2233 * xx = LB for label, CM for command and WD for working dir */
2234 static const gchar *groups[GEANY_GBG_COUNT] = { "FT", "NF", "EX" };
2235 static const gchar *fixedkey="xx_xx_xx";
2237 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2238 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2239 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2241 static void build_load_menu_grp(GKeyFile *config, GeanyBuildCommand **dst, gint grp,
2242 gchar *prefix, gboolean loc)
2244 guint cmd;
2245 gsize prefixlen; /* NOTE prefixlen used in macros above */
2246 GeanyBuildCommand *dstcmd;
2247 gchar *key;
2248 static gchar cmdbuf[3] = " ";
2250 if (*dst == NULL)
2251 *dst = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2252 dstcmd = *dst;
2253 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2254 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2255 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2257 gchar *label;
2258 if (cmd >= 100)
2259 break; /* ensure no buffer overflow */
2260 sprintf(cmdbuf, "%02u", cmd);
2261 set_key_grp(key, groups[grp]);
2262 set_key_cmd(key, cmdbuf);
2263 set_key_fld(key, "LB");
2264 if (loc)
2265 label = g_key_file_get_locale_string(config, build_grp_name, key, NULL, NULL);
2266 else
2267 label = g_key_file_get_string(config, build_grp_name, key, NULL);
2268 if (label != NULL)
2270 dstcmd[cmd].exists = TRUE;
2271 SETPTR(dstcmd[cmd].label, label);
2272 set_key_fld(key,"CM");
2273 SETPTR(dstcmd[cmd].command,
2274 g_key_file_get_string(config, build_grp_name, key, NULL));
2275 set_key_fld(key,"WD");
2276 SETPTR(dstcmd[cmd].working_dir,
2277 g_key_file_get_string(config, build_grp_name, key, NULL));
2279 else dstcmd[cmd].exists = FALSE;
2281 g_free(key);
2285 /* for the specified source load new format build menu items or try to make some sense of
2286 * old format setings, not done perfectly but better than ignoring them */
2287 void build_load_menu(GKeyFile *config, GeanyBuildSource src, gpointer p)
2289 GeanyFiletype *ft;
2290 GeanyProject *pj;
2291 gchar **ftlist;
2292 gchar *value, *basedir, *makebasedir;
2293 gboolean bvalue = FALSE;
2295 if (g_key_file_has_group(config, build_grp_name))
2297 switch (src)
2299 case GEANY_BCS_FT:
2300 ft = (GeanyFiletype*)p;
2301 if (ft == NULL)
2302 return;
2303 build_load_menu_grp(config, &(ft->priv->filecmds), GEANY_GBG_FT, NULL, TRUE);
2304 build_load_menu_grp(config, &(ft->priv->ftdefcmds), GEANY_GBG_NON_FT, NULL, TRUE);
2305 build_load_menu_grp(config, &(ft->priv->execcmds), GEANY_GBG_EXEC, NULL, TRUE);
2306 SETPTR(ft->error_regex_string,
2307 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2308 break;
2309 case GEANY_BCS_HOME_FT:
2310 ft = (GeanyFiletype*)p;
2311 if (ft == NULL)
2312 return;
2313 build_load_menu_grp(config, &(ft->priv->homefilecmds), GEANY_GBG_FT, NULL, FALSE);
2314 build_load_menu_grp(config, &(ft->priv->homeexeccmds), GEANY_GBG_EXEC, NULL, FALSE);
2315 SETPTR(ft->priv->homeerror_regex_string,
2316 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2317 break;
2318 case GEANY_BCS_PREF:
2319 build_load_menu_grp(config, &non_ft_pref, GEANY_GBG_NON_FT, NULL, FALSE);
2320 build_load_menu_grp(config, &exec_pref, GEANY_GBG_EXEC, NULL, FALSE);
2321 SETPTR(regex_pref, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2322 break;
2323 case GEANY_BCS_PROJ:
2324 build_load_menu_grp(config, &non_ft_proj, GEANY_GBG_NON_FT, NULL, FALSE);
2325 build_load_menu_grp(config, &exec_proj, GEANY_GBG_EXEC, NULL, FALSE);
2326 SETPTR(regex_proj, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2327 pj = (GeanyProject*)p;
2328 if (p == NULL)
2329 return;
2330 ftlist = g_key_file_get_string_list(config, build_grp_name, "filetypes", NULL, NULL);
2331 if (ftlist != NULL)
2333 gchar **ftname;
2334 if (pj->priv->build_filetypes_list == NULL)
2335 pj->priv->build_filetypes_list = g_ptr_array_new();
2336 g_ptr_array_set_size(pj->priv->build_filetypes_list, 0);
2337 for (ftname = ftlist; *ftname != NULL; ++ftname)
2339 ft = filetypes_lookup_by_name(*ftname);
2340 if (ft != NULL)
2342 gchar *regkey = g_strdup_printf("%serror_regex", *ftname);
2343 g_ptr_array_add(pj->priv->build_filetypes_list, ft);
2344 SETPTR(ft->priv->projerror_regex_string,
2345 g_key_file_get_string(config, build_grp_name, regkey, NULL));
2346 g_free(regkey);
2347 build_load_menu_grp(config, &(ft->priv->projfilecmds), GEANY_GBG_FT, *ftname, FALSE);
2348 build_load_menu_grp(config, &(ft->priv->projexeccmds), GEANY_GBG_EXEC, *ftname, FALSE);
2351 g_free(ftlist);
2353 break;
2354 default: /* defaults don't load from config, see build_init */
2355 break;
2359 /* load old [build_settings] values if there is no value defined by [build-menu] */
2361 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2362 /* TODO: rewrite as function */
2363 #define ASSIGNIF(type, id, string, value) \
2364 do { \
2365 gchar *ASSIGNF__value = (value); \
2366 if (!EMPTY(ASSIGNF__value) && ! type[GBO_TO_CMD(id)].exists) { \
2367 type[GBO_TO_CMD(id)].exists = TRUE; \
2368 SETPTR(type[GBO_TO_CMD(id)].label, g_strdup(string)); \
2369 SETPTR(type[GBO_TO_CMD(id)].command, ASSIGNF__value); \
2370 SETPTR(type[GBO_TO_CMD(id)].working_dir, NULL); \
2371 type[GBO_TO_CMD(id)].old = TRUE; \
2372 } else \
2373 g_free(ASSIGNF__value); \
2374 } while (0)
2376 switch (src)
2378 case GEANY_BCS_FT:
2379 ft = (GeanyFiletype*)p;
2380 value = g_key_file_get_string(config, "build_settings", "compiler", NULL);
2381 if (value != NULL)
2383 if (ft->priv->filecmds == NULL)
2384 ft->priv->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2385 ASSIGNIF(ft->priv->filecmds, GEANY_GBO_COMPILE, _("_Compile"), value);
2387 value = g_key_file_get_string(config, "build_settings", "linker", NULL);
2388 if (value != NULL)
2390 if (ft->priv->filecmds == NULL)
2391 ft->priv->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2392 ASSIGNIF(ft->priv->filecmds, GEANY_GBO_BUILD, _("_Build"), value);
2394 value = g_key_file_get_string(config, "build_settings", "run_cmd", NULL);
2395 if (value != NULL)
2397 if (ft->priv->execcmds == NULL)
2398 ft->priv->execcmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2399 ASSIGNIF(ft->priv->execcmds, GEANY_GBO_EXEC, _("_Execute"), value);
2401 if (ft->error_regex_string == NULL)
2402 ft->error_regex_string = g_key_file_get_string(config, "build_settings", "error_regex", NULL);
2403 break;
2404 case GEANY_BCS_PROJ:
2405 if (non_ft_pref == NULL)
2406 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2407 basedir = project_get_base_path();
2408 if (basedir == NULL)
2409 basedir = g_strdup("%d");
2410 bvalue = g_key_file_get_boolean(config, "project", "make_in_base_path", NULL);
2411 if (bvalue)
2412 makebasedir = g_strdup(basedir);
2413 else
2414 makebasedir = g_strdup("%d");
2415 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].old)
2416 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].working_dir, g_strdup(makebasedir));
2417 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].old)
2418 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].working_dir, g_strdup(makebasedir));
2419 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].old)
2420 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].working_dir, g_strdup("%d"));
2421 value = g_key_file_get_string(config, "project", "run_cmd", NULL);
2422 if (!EMPTY(value))
2424 if (exec_proj == NULL)
2425 exec_proj = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2426 if (! exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists)
2428 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists = TRUE;
2429 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].label, g_strdup(_("_Execute")));
2430 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].command, value);
2431 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].working_dir, g_strdup(basedir));
2432 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].old = TRUE;
2435 g_free(makebasedir);
2436 g_free(basedir);
2437 break;
2438 case GEANY_BCS_PREF:
2439 value = g_key_file_get_string(config, "tools", "make_cmd", NULL);
2440 if (value != NULL)
2442 if (non_ft_pref == NULL)
2443 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2444 ASSIGNIF(non_ft_pref, GEANY_GBO_CUSTOM, _("Make Custom _Target..."),
2445 g_strdup_printf("%s ", value));
2446 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_OBJECT, _("Make _Object"),
2447 g_strdup_printf("%s %%e.o",value));
2448 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_ALL, _("_Make"), value);
2450 break;
2451 default:
2452 break;
2457 static guint build_save_menu_grp(GKeyFile *config, GeanyBuildCommand *src, gint grp, gchar *prefix)
2459 guint cmd;
2460 gsize prefixlen; /* NOTE prefixlen used in macros above */
2461 gchar *key;
2462 guint count = 0;
2463 enum GeanyBuildCmdEntries i;
2465 if (src == NULL)
2466 return 0;
2467 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2468 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2469 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2471 if (src[cmd].exists) ++count;
2472 if (src[cmd].changed)
2474 static gchar cmdbuf[4] = " ";
2475 if (cmd >= 100)
2476 break; /* ensure no buffer overflow */
2477 sprintf(cmdbuf, "%02u", cmd);
2478 set_key_grp(key, groups[grp]);
2479 set_key_cmd(key, cmdbuf);
2480 if (src[cmd].exists)
2482 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2484 set_key_fld(key, config_keys[i]);
2485 g_key_file_set_string(config, build_grp_name, key, id_to_str(&src[cmd], i));
2488 else
2490 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2492 set_key_fld(key, config_keys[i]);
2493 g_key_file_remove_key(config, build_grp_name, key, NULL);
2498 g_free(key);
2499 return count;
2503 typedef struct ForEachData
2505 GKeyFile *config;
2506 GPtrArray *ft_names;
2507 } ForEachData;
2510 static void foreach_project_filetype(gpointer data, gpointer user_data)
2512 GeanyFiletype *ft = data;
2513 ForEachData *d = user_data;
2514 guint i = 0;
2515 gchar *regkey = g_strdup_printf("%serror_regex", ft->name);
2517 i += build_save_menu_grp(d->config, ft->priv->projfilecmds, GEANY_GBG_FT, ft->name);
2518 i += build_save_menu_grp(d->config, ft->priv->projexeccmds, GEANY_GBG_EXEC, ft->name);
2519 if (!EMPTY(ft->priv->projerror_regex_string))
2521 g_key_file_set_string(d->config, build_grp_name, regkey, ft->priv->projerror_regex_string);
2522 i++;
2524 else
2525 g_key_file_remove_key(d->config, build_grp_name, regkey, NULL);
2526 g_free(regkey);
2527 if (i > 0)
2528 g_ptr_array_add(d->ft_names, ft->name);
2532 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
2533 void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
2535 GeanyFiletype *ft;
2536 GeanyProject *pj;
2537 ForEachData data;
2539 switch (src)
2541 case GEANY_BCS_HOME_FT:
2542 ft = (GeanyFiletype*)ptr;
2543 if (ft == NULL)
2544 return;
2545 build_save_menu_grp(config, ft->priv->homefilecmds, GEANY_GBG_FT, NULL);
2546 build_save_menu_grp(config, ft->priv->homeexeccmds, GEANY_GBG_EXEC, NULL);
2547 if (!EMPTY(ft->priv->homeerror_regex_string))
2548 g_key_file_set_string(config, build_grp_name, "error_regex", ft->priv->homeerror_regex_string);
2549 else
2550 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2551 break;
2552 case GEANY_BCS_PREF:
2553 build_save_menu_grp(config, non_ft_pref, GEANY_GBG_NON_FT, NULL);
2554 build_save_menu_grp(config, exec_pref, GEANY_GBG_EXEC, NULL);
2555 if (!EMPTY(regex_pref))
2556 g_key_file_set_string(config, build_grp_name, "error_regex", regex_pref);
2557 else
2558 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2559 break;
2560 case GEANY_BCS_PROJ:
2561 pj = (GeanyProject*)ptr;
2562 build_save_menu_grp(config, non_ft_proj, GEANY_GBG_NON_FT, NULL);
2563 build_save_menu_grp(config, exec_proj, GEANY_GBG_EXEC, NULL);
2564 if (!EMPTY(regex_proj))
2565 g_key_file_set_string(config, build_grp_name, "error_regex", regex_proj);
2566 else
2567 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2568 if (pj->priv->build_filetypes_list != NULL)
2570 data.config = config;
2571 data.ft_names = g_ptr_array_new();
2572 g_ptr_array_foreach(pj->priv->build_filetypes_list, foreach_project_filetype, (gpointer)(&data));
2573 if (data.ft_names->pdata != NULL)
2574 g_key_file_set_string_list(config, build_grp_name, "filetypes",
2575 (const gchar**)(data.ft_names->pdata), data.ft_names->len);
2576 else
2577 g_key_file_remove_key(config, build_grp_name, "filetypes", NULL);
2578 g_ptr_array_free(data.ft_names, TRUE);
2580 break;
2581 default: /* defaults and GEANY_BCS_FT can't save */
2582 break;
2587 /* FIXME: count is int only because calling code doesn't handle checking its value itself */
2588 void build_set_group_count(GeanyBuildGroup grp, gint count)
2590 guint i, sum;
2592 g_return_if_fail(count >= 0);
2594 if ((guint) count > build_groups_count[grp])
2595 build_groups_count[grp] = (guint) count;
2596 for (i = 0, sum = 0; i < GEANY_GBG_COUNT; ++i)
2597 sum += build_groups_count[i];
2598 build_items_count = sum;
2602 /** Get the count of commands for the group
2604 * Get the number of commands in the group specified by @a grp.
2606 * @param grp the group of the specified menu item.
2608 * @return a count of the number of commands in the group
2611 GEANY_API_SYMBOL
2612 guint build_get_group_count(const GeanyBuildGroup grp)
2614 g_return_val_if_fail(grp < GEANY_GBG_COUNT, 0);
2615 return build_groups_count[grp];
2619 static void on_project_close(void)
2621 /* remove project regexen */
2622 SETPTR(regex_proj, NULL);
2626 static struct
2628 const gchar *label;
2629 const gchar *command;
2630 const gchar *working_dir;
2631 GeanyBuildCommand **ptr;
2632 gint index;
2633 } default_cmds[] = {
2634 { N_("_Make"), "make", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_ALL)},
2635 { N_("Make Custom _Target..."), "make ", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_CUSTOM)},
2636 { N_("Make _Object"), "make %e.o", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)},
2637 { N_("_Execute"), "./%e", NULL, &exec_def, GBO_TO_CMD(GEANY_GBO_EXEC)},
2638 { NULL, NULL, NULL, NULL, 0 }
2642 void build_init(void)
2644 GtkWidget *item;
2645 GtkWidget *toolmenu;
2646 gint cmdindex;
2648 g_signal_connect(geany_object, "project-close", on_project_close, NULL);
2650 ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2651 non_ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2652 exec_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2653 run_info = g_new0(RunInfo, build_groups_count[GEANY_GBG_EXEC]);
2655 for (cmdindex = 0; default_cmds[cmdindex].command != NULL; ++cmdindex)
2657 GeanyBuildCommand *cmd = &((*(default_cmds[cmdindex].ptr))[ default_cmds[cmdindex].index ]);
2658 cmd->exists = TRUE;
2659 cmd->label = g_strdup(_(default_cmds[cmdindex].label));
2660 cmd->command = g_strdup(default_cmds[cmdindex].command);
2661 cmd->working_dir = g_strdup(default_cmds[cmdindex].working_dir);
2664 /* create the toolbar Build item sub menu */
2665 toolmenu = gtk_menu_new();
2666 g_object_ref(toolmenu);
2668 /* build the code */
2669 item = ui_image_menu_item_new(GEANY_STOCK_BUILD, _("_Build"));
2670 gtk_widget_show(item);
2671 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2672 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_build_activate),
2673 GBO_TO_POINTER(GEANY_GBO_BUILD));
2674 widgets.toolitem_build = item;
2676 item = gtk_separator_menu_item_new();
2677 gtk_widget_show(item);
2678 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2680 /* build the code with make all */
2681 item = gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2682 gtk_widget_show(item);
2683 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2684 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2685 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL));
2686 widgets.toolitem_make_all = item;
2688 /* build the code with make custom */
2689 item = gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target..."));
2690 gtk_widget_show(item);
2691 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2692 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2693 GBO_TO_POINTER(GEANY_GBO_CUSTOM));
2694 widgets.toolitem_make_custom = item;
2696 /* build the code with make object */
2697 item = gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2698 gtk_widget_show(item);
2699 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2700 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2701 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT));
2702 widgets.toolitem_make_object = item;
2704 item = gtk_separator_menu_item_new();
2705 gtk_widget_show(item);
2706 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2708 /* arguments */
2709 item = ui_image_menu_item_new(GTK_STOCK_PREFERENCES, _("_Set Build Commands"));
2710 gtk_widget_show(item);
2711 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2712 g_signal_connect(item, "activate", G_CALLBACK(on_set_build_commands_activate), NULL);
2713 widgets.toolitem_set_args = item;
2715 /* get toolbar action pointers */
2716 widgets.build_action = toolbar_get_action_by_name("Build");
2717 widgets.compile_action = toolbar_get_action_by_name("Compile");
2718 widgets.run_action = toolbar_get_action_by_name("Run");
2719 widgets.toolmenu = toolmenu;
2720 /* set the submenu to the toolbar item */
2721 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets.build_action), toolmenu);