Merge pull request #232 from stefan-it/enhancement/lang-de-corrections
[geany-mirror.git] / src / build.c
blob07e18ea489ea56be48b75e4ccf259bc6e5c81e95
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 #include "geany.h"
32 #include "build.h"
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <glib/gstdio.h>
41 #ifdef G_OS_UNIX
42 # include <sys/types.h>
43 # include <sys/wait.h>
44 # include <signal.h>
45 #else
46 # include <windows.h>
47 #endif
49 #include "prefs.h"
50 #include "support.h"
51 #include "document.h"
52 #include "utils.h"
53 #include "ui_utils.h"
54 #include "dialogs.h"
55 #include "msgwindow.h"
56 #include "filetypes.h"
57 #include "keybindings.h"
58 #include "vte.h"
59 #include "project.h"
60 #include "editor.h"
61 #include "win32.h"
62 #include "toolbar.h"
63 #include "geanymenubuttonaction.h"
64 #include "gtkcompat.h"
66 /* g_spawn_async_with_pipes doesn't work on Windows */
67 #ifdef G_OS_WIN32
68 #define SYNC_SPAWN
69 #endif
71 /* Number of editor indicators to draw - limited as this can affect performance */
72 #define GEANY_BUILD_ERR_HIGHLIGHT_MAX 50
75 GeanyBuildInfo build_info = {GEANY_GBG_FT, 0, 0, NULL, GEANY_FILETYPES_NONE, NULL, 0};
77 static gchar *current_dir_entered = NULL;
79 typedef struct RunInfo
81 GPid pid;
82 gint file_type_id;
83 } RunInfo;
85 static RunInfo *run_info;
87 #ifdef G_OS_WIN32
88 static const gchar RUN_SCRIPT_CMD[] = "geany_run_script.bat";
89 #else
90 static const gchar RUN_SCRIPT_CMD[] = "./geany_run_script.sh";
91 #endif
93 /* pack group (<8) and command (<32) into a user_data pointer */
94 #define GRP_CMD_TO_POINTER(grp, cmd) GUINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
95 #define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
96 #define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_UINT(gptr)&0x1f)
97 #define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_UINT(gptr)&0xe0) >> 5)
99 static gpointer last_toolbutton_action = GBO_TO_POINTER(GEANY_GBO_BUILD);
101 static BuildMenuItems menu_items = {NULL, {NULL, NULL, NULL, NULL}};
103 static struct
105 GtkAction *run_action;
106 GtkAction *compile_action;
107 GtkAction *build_action;
108 GtkWidget *toolmenu;
110 GtkWidget *toolitem_build;
111 GtkWidget *toolitem_make_all;
112 GtkWidget *toolitem_make_custom;
113 GtkWidget *toolitem_make_object;
114 GtkWidget *toolitem_set_args;
116 widgets;
118 static guint build_groups_count[GEANY_GBG_COUNT] = { 3, 4, 2 };
119 static guint build_items_count = 9;
121 #ifndef SYNC_SPAWN
122 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data);
123 static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data);
124 #endif
125 static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose, GError **error);
126 static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir);
127 static void set_stop_button(gboolean stop);
128 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data);
129 static void on_set_build_commands_activate(GtkWidget *w, gpointer u);
130 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data);
131 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data);
132 static void kill_process(GPid *pid);
133 static void show_build_result_message(gboolean failure);
134 static void process_build_output_line(const gchar *line, gint color);
135 static void show_build_commands_dialog(void);
136 static void on_build_menu_item(GtkWidget *w, gpointer user_data);
138 void build_finalize(void)
140 g_free(build_info.dir);
141 g_free(build_info.custom_target);
143 if (menu_items.menu != NULL && GTK_IS_WIDGET(menu_items.menu))
144 gtk_widget_destroy(menu_items.menu);
148 /* note: copied from keybindings.c, may be able to go away */
149 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id,
150 GtkAccelGroup *accel_group, GtkWidget *menuitem)
152 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
154 if (kb->key != 0)
155 gtk_widget_add_accelerator(menuitem, "activate", accel_group,
156 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
160 /* convenience routines to access parts of GeanyBuildCommand */
161 static gchar *id_to_str(GeanyBuildCommand *bc, gint id)
163 switch (id)
165 case GEANY_BC_LABEL:
166 return bc->label;
167 case GEANY_BC_COMMAND:
168 return bc->command;
169 case GEANY_BC_WORKING_DIR:
170 return bc->working_dir;
172 g_assert(0);
173 return NULL;
177 static void set_command(GeanyBuildCommand *bc, gint id, gchar *str)
179 switch (id)
181 case GEANY_BC_LABEL:
182 SETPTR(bc->label, str);
183 break;
184 case GEANY_BC_COMMAND:
185 SETPTR(bc->command, str);
186 break;
187 case GEANY_BC_WORKING_DIR:
188 SETPTR(bc->working_dir, str);
189 break;
190 default:
191 g_assert(0);
196 static const gchar *config_keys[GEANY_BC_CMDENTRIES_COUNT] = {
197 "LB", /* label */
198 "CM", /* command */
199 "WD" /* working directory */
202 /*-----------------------------------------------------
204 * Execute commands and handle results
206 *-----------------------------------------------------*/
208 /* the various groups of commands not in the filetype struct */
209 static GeanyBuildCommand *ft_def = NULL;
210 static GeanyBuildCommand *non_ft_proj = NULL;
211 static GeanyBuildCommand *non_ft_pref = NULL;
212 static GeanyBuildCommand *non_ft_def = NULL;
213 static GeanyBuildCommand *exec_proj = NULL;
214 static GeanyBuildCommand *exec_pref = NULL;
215 static GeanyBuildCommand *exec_def = NULL;
216 /* and the regexen not in the filetype structure */
217 static gchar *regex_pref = NULL;
218 /* project non-fileregex string */
219 static gchar *regex_proj = NULL;
221 /* control if build commands are printed by get_build_cmd, for debug purposes only*/
222 #ifndef PRINTBUILDCMDS
223 #define PRINTBUILDCMDS FALSE
224 #endif
225 static gboolean printbuildcmds = PRINTBUILDCMDS;
228 /* for debug only, print the commands structures in priority order */
229 static void printfcmds(void)
231 #if 0
232 GeanyBuildCommand **cl[GEANY_GBG_COUNT][GEANY_BCS_COUNT] = {
233 /* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
234 * GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
235 { &ft_def, NULL, NULL, NULL, NULL, NULL },
236 { &non_ft_def, NULL, NULL, &non_ft_pref, NULL, &non_ft_proj },
237 { &exec_def, NULL, NULL, &exec_pref, NULL, &exec_proj }
239 GeanyFiletype *ft = NULL;
240 GeanyDocument *doc;
241 gint i, j, k, l, m;
242 enum GeanyBuildCmdEntries n;
243 gint cc[GEANY_BCS_COUNT];
244 gchar c;
246 doc = document_get_current();
247 if (doc != NULL)
248 ft = doc->file_type;
249 if (ft != NULL)
251 printf("filetype %s\n",ft->name);
252 cl[GEANY_GBG_FT][GEANY_BCS_FT] = &(ft->filecmds);
253 cl[GEANY_GBG_FT][GEANY_BCS_HOME_FT] = &(ft->homefilecmds);
254 cl[GEANY_GBG_FT][GEANY_BCS_PROJ] = &(ft->projfilecmds);
255 cl[GEANY_GBG_NON_FT][GEANY_BCS_FT] = &(ft->ftdefcmds);
256 cl[GEANY_GBG_EXEC][GEANY_BCS_FT] = &(ft->execcmds);
257 cl[GEANY_GBG_EXEC][GEANY_BCS_HOME_FT] = &(ft->homeexeccmds);
258 cl[GEANY_GBG_EXEC][GEANY_BCS_PROJ_FT] = &(ft->projexeccmds);
260 for (i = 0; i < GEANY_BCS_COUNT; ++i)
262 m = 1;
263 for (j = 0; j < GEANY_GBG_COUNT; ++j)
265 for (k = 0; k < build_groups_count[j]; ++k)
266 if (cl[j][i] != NULL && *(cl[j][i]) != NULL && (*(cl[j][i]))[k].exists)
268 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
270 if ((*(cl[j][i]))[k].entries[n] != NULL &&
271 (l = strlen((*(cl[j][i]))[k].entries[n])) > m)
273 m = l;
278 cc[i] = m;
280 for (i = 0; i < GEANY_GBG_COUNT; ++i)
282 for (k = 0; k < build_groups_count[i]; ++k)
284 for (l = 0; l < 2; ++l)
286 c = ' ';
287 for (j = 0; j < GEANY_BCS_COUNT; ++j)
289 if (cl[i][j] != NULL && *(cl[i][j]) != NULL && (*(cl[i][j]))[k].exists)
291 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
293 if ((*(cl[i][j]))[k].entries[i] != NULL)
294 printf("%c %*.*s",c,cc[j],cc[j],(*(cl[i][j]))[k].entries[i]);
295 else
296 printf("%c %*.*s",c,cc[j],cc[j]," ");
299 else
300 printf("%c %*.*s",c,cc[j],cc[j]," ");
301 c = ',';
303 printf("\n");
306 printf("\n");
308 #endif
312 /* macros to save typing and make the logic visible */
313 #define return_cmd_if(src, cmds)\
314 if (cmds != NULL && cmds[cmdindex].exists && below>src)\
316 *fr=src; \
317 if (printbuildcmds) \
318 printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
319 return &(cmds[cmdindex]); \
322 #define return_ft_cmd_if(src, cmds)\
323 if (ft != NULL && ft->cmds != NULL \
324 && ft->cmds[cmdindex].exists && below>src)\
326 *fr=src; \
327 if (printbuildcmds) \
328 printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
329 return &(ft->cmds[cmdindex]); \
333 /* get the next lowest command taking priority into account */
334 static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, guint cmdgrp, guint cmdindex,
335 guint below, guint *from)
337 /* Note: parameter below used in macros above */
338 GeanyFiletype *ft = NULL;
339 guint sink, *fr = &sink;
341 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
343 if (printbuildcmds)
344 printfcmds();
345 if (cmdgrp >= GEANY_GBG_COUNT)
346 return NULL;
347 if (from != NULL)
348 fr = from;
349 if (doc == NULL)
350 doc = document_get_current();
351 if (doc != NULL)
352 ft = doc->file_type;
354 switch (cmdgrp)
356 case GEANY_GBG_FT: /* order proj ft, home ft, ft, defft */
357 if (ft != NULL)
359 return_ft_cmd_if(GEANY_BCS_PROJ, projfilecmds);
360 return_ft_cmd_if(GEANY_BCS_PREF, homefilecmds);
361 return_ft_cmd_if(GEANY_BCS_FT, filecmds);
363 return_cmd_if(GEANY_BCS_DEF, ft_def);
364 break;
365 case GEANY_GBG_NON_FT: /* order proj, pref, def */
366 return_cmd_if(GEANY_BCS_PROJ, non_ft_proj);
367 return_cmd_if(GEANY_BCS_PREF, non_ft_pref);
368 return_ft_cmd_if(GEANY_BCS_FT, ftdefcmds);
369 return_cmd_if(GEANY_BCS_DEF, non_ft_def);
370 break;
371 case GEANY_GBG_EXEC: /* order proj, proj ft, pref, home ft, ft, def */
372 return_cmd_if(GEANY_BCS_PROJ, exec_proj);
373 return_ft_cmd_if(GEANY_BCS_PROJ_FT, projexeccmds);
374 return_cmd_if(GEANY_BCS_PREF, exec_pref);
375 return_ft_cmd_if(GEANY_BCS_FT, homeexeccmds);
376 return_ft_cmd_if(GEANY_BCS_FT, execcmds);
377 return_cmd_if(GEANY_BCS_DEF, exec_def);
378 break;
379 default:
380 break;
382 return NULL;
386 /* shortcut to start looking at the top */
387 static GeanyBuildCommand *get_build_cmd(GeanyDocument *doc, guint grp, guint cmdindex, guint *from)
389 return get_next_build_cmd(doc, grp, cmdindex, GEANY_BCS_COUNT, from);
393 #define return_nonblank_regex(src, ptr)\
394 if (!EMPTY(ptr)) \
395 { *fr = (src); return &(ptr); }
398 /* like get_build_cmd, but for regexen, used by filetypes */
399 gchar **build_get_regex(GeanyBuildGroup grp, GeanyFiletype *ft, guint *from)
401 guint sink, *fr = &sink;
403 if (from != NULL)
404 fr = from;
405 if (grp == GEANY_GBG_FT)
407 if (ft == NULL)
409 GeanyDocument *doc = document_get_current();
410 if (doc != NULL)
411 ft = doc->file_type;
413 if (ft == NULL)
414 return NULL;
415 return_nonblank_regex(GEANY_BCS_PROJ, ft->projerror_regex_string);
416 return_nonblank_regex(GEANY_BCS_HOME_FT, ft->homeerror_regex_string);
417 return_nonblank_regex(GEANY_BCS_FT, ft->error_regex_string);
419 else if (grp == GEANY_GBG_NON_FT)
421 return_nonblank_regex(GEANY_BCS_PROJ, regex_proj);
422 return_nonblank_regex(GEANY_BCS_PREF, regex_pref);
424 return NULL;
428 static GeanyBuildCommand **get_build_group_pointer(const GeanyBuildSource src, const GeanyBuildGroup grp)
430 GeanyDocument *doc;
431 GeanyFiletype *ft = NULL;
433 switch (grp)
435 case GEANY_GBG_FT:
436 if ((doc = document_get_current()) == NULL)
437 return NULL;
438 if ((ft = doc->file_type) == NULL)
439 return NULL;
440 switch (src)
442 case GEANY_BCS_DEF: return &(ft->ftdefcmds);
443 case GEANY_BCS_FT: return &(ft->filecmds);
444 case GEANY_BCS_HOME_FT: return &(ft->homefilecmds);
445 case GEANY_BCS_PREF: return &(ft->homefilecmds);
446 case GEANY_BCS_PROJ: return &(ft->projfilecmds);
447 default: return NULL;
449 break;
450 case GEANY_GBG_NON_FT:
451 switch (src)
453 case GEANY_BCS_DEF: return &(non_ft_def);
454 case GEANY_BCS_PREF: return &(non_ft_pref);
455 case GEANY_BCS_PROJ: return &(non_ft_proj);
456 default: return NULL;
458 break;
459 case GEANY_GBG_EXEC:
460 if ((doc = document_get_current()) != NULL)
461 ft = doc->file_type;
462 switch (src)
464 case GEANY_BCS_DEF: return &(exec_def);
465 case GEANY_BCS_FT: return ft ? &(ft->execcmds): NULL;
466 case GEANY_BCS_HOME_FT: return ft ? &(ft->homeexeccmds): NULL;
467 case GEANY_BCS_PROJ_FT: return ft ? &(ft->projexeccmds): NULL;
468 case GEANY_BCS_PREF: return &(exec_pref);
469 case GEANY_BCS_PROJ: return &(exec_proj);
470 default: return NULL;
472 break;
473 default:
474 return NULL;
479 /* get pointer to the command group array */
480 static GeanyBuildCommand *get_build_group(const GeanyBuildSource src, const GeanyBuildGroup grp)
482 GeanyBuildCommand **g = get_build_group_pointer(src, grp);
483 if (g == NULL) return NULL;
484 return *g;
488 /** Remove the specified Build menu item.
490 * Makes the specified menu item configuration no longer exist. This
491 * is different to setting fields to blank because the menu item
492 * will be deleted from the configuration file on saving
493 * (except the system filetypes settings @see Build Menu Configuration
494 * section of the Manual).
496 * @param src the source of the menu item to remove.
497 * @param grp the group of the command to remove.
498 * @param cmd the index (from 0) of the command within the group. A negative
499 * value will remove the whole group.
501 * If any parameter is out of range does nothing.
503 * Updates the menu.
506 void build_remove_menu_item(const GeanyBuildSource src, const GeanyBuildGroup grp, const gint cmd)
508 GeanyBuildCommand *bc;
509 guint i;
511 bc = get_build_group(src, grp);
512 if (bc == NULL)
513 return;
514 if (cmd < 0)
516 for (i = 0; i < build_groups_count[grp]; ++i)
517 bc[i].exists = FALSE;
519 else if ((guint) cmd < build_groups_count[grp])
520 bc[cmd].exists = FALSE;
524 /* * Get the @a GeanyBuildCommand structure for the specified Build menu item.
526 * Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
527 * hidden by higher priority commands.
529 * @param src the source of the specified menu item.
530 * @param grp the group of the specified menu item.
531 * @param cmd the index of the command within the group.
533 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
534 * This is a pointer to an internal structure and must not be freed.
536 * @see build_menu_update
538 GeanyBuildCommand *build_get_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, guint cmd)
540 GeanyBuildCommand *bc;
542 g_return_val_if_fail(src < GEANY_BCS_COUNT, NULL);
543 g_return_val_if_fail(grp < GEANY_GBG_COUNT, NULL);
544 g_return_val_if_fail(cmd < build_groups_count[grp], NULL);
546 bc = get_build_group(src, grp);
547 if (bc == NULL)
548 return NULL;
549 return &(bc[cmd]);
553 /** Get the string for the menu item field.
555 * Get the current highest priority command specified by @a grp and @a cmd. This is the one
556 * that the menu item will use if activated.
558 * @param grp the group of the specified menu item.
559 * @param cmd the index of the command within the group.
560 * @param fld the field to return
562 * @return a pointer to the constant string or @a NULL if it doesn't exist.
563 * This is a pointer to an internal structure and must not be freed.
566 const gchar *build_get_current_menu_item(const GeanyBuildGroup grp, const guint cmd,
567 const GeanyBuildCmdEntries fld)
569 GeanyBuildCommand *c;
570 gchar *str = NULL;
572 g_return_val_if_fail(grp < GEANY_GBG_COUNT, NULL);
573 g_return_val_if_fail(fld < GEANY_BC_CMDENTRIES_COUNT, NULL);
574 g_return_val_if_fail(cmd < build_groups_count[grp], NULL);
576 c = get_build_cmd(NULL, grp, cmd, NULL);
577 if (c == NULL) return NULL;
578 switch (fld)
580 case GEANY_BC_COMMAND:
581 str = c->command;
582 break;
583 case GEANY_BC_LABEL:
584 str = c->label;
585 break;
586 case GEANY_BC_WORKING_DIR:
587 str = c->working_dir;
588 break;
589 default:
590 break;
592 return str;
595 /** Set the string for the menu item field.
597 * Set the specified field of the command specified by @a src, @a grp and @a cmd.
599 * @param src the source of the menu item
600 * @param grp the group of the specified menu item.
601 * @param cmd the index of the menu item within the group.
602 * @param fld the field in the menu item command to set
603 * @param val the value to set the field to, is copied
607 void build_set_menu_item(const GeanyBuildSource src, const GeanyBuildGroup grp,
608 const guint cmd, const GeanyBuildCmdEntries fld, const gchar *val)
610 GeanyBuildCommand **g;
612 g_return_if_fail(src < GEANY_BCS_COUNT);
613 g_return_if_fail(grp < GEANY_GBG_COUNT);
614 g_return_if_fail(fld < GEANY_BC_CMDENTRIES_COUNT);
615 g_return_if_fail(cmd < build_groups_count[grp]);
617 g = get_build_group_pointer(src, grp);
618 if (g == NULL) return;
619 if (*g == NULL )
621 *g = g_new0(GeanyBuildCommand, build_groups_count[grp]);
623 switch (fld)
625 case GEANY_BC_COMMAND:
626 SETPTR((*g)[cmd].command, g_strdup(val));
627 (*g)[cmd].exists = TRUE;
628 break;
629 case GEANY_BC_LABEL:
630 SETPTR((*g)[cmd].label, g_strdup(val));
631 (*g)[cmd].exists = TRUE;
632 break;
633 case GEANY_BC_WORKING_DIR:
634 SETPTR((*g)[cmd].working_dir, g_strdup(val));
635 (*g)[cmd].exists = TRUE;
636 break;
637 default:
638 break;
640 build_menu_update(NULL);
643 /** Activate the menu item.
645 * Activate the menu item specified by @a grp and @a cmd.
647 * @param grp the group of the specified menu item.
648 * @param cmd the index of the command within the group.
652 void build_activate_menu_item(const GeanyBuildGroup grp, const guint cmd)
654 on_build_menu_item(NULL, GRP_CMD_TO_POINTER(grp, cmd));
658 /* Clear all error indicators in all documents. */
659 static void clear_all_errors(void)
661 guint i;
663 foreach_document(i)
665 editor_indicator_clear_errors(documents[i]->editor);
670 #ifdef SYNC_SPAWN
671 static void parse_build_output(const gchar **output, gint status)
673 guint x, i, len;
674 gchar *line, **lines;
676 for (x = 0; x < 2; x++)
678 if (!EMPTY(output[x]))
680 lines = g_strsplit_set(output[x], "\r\n", -1);
681 len = g_strv_length(lines);
683 for (i = 0; i < len; i++)
685 if (!EMPTY(lines[i]))
687 line = lines[i];
688 while (*line != '\0')
689 { /* replace any control characters in the output */
690 if (*line < 32)
691 *line = 32;
692 line++;
694 process_build_output_line(lines[i], COLOR_BLACK);
697 g_strfreev(lines);
701 show_build_result_message(status != 0);
702 utils_beep();
704 build_info.pid = 0;
705 /* enable build items again */
706 build_menu_update(NULL);
708 #endif
711 /* Replaces occurences of %e and %p with the appropriate filenames,
712 * %d and %p replacements should be in UTF8 */
713 static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *src)
715 GString *stack;
716 gchar *filename = NULL;
717 gchar *replacement;
718 gchar *executable = NULL;
719 gchar *ret_str; /* to be freed when not in use anymore */
721 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
723 stack = g_string_new(src);
724 if (doc != NULL && doc->file_name != NULL)
726 filename = utils_get_utf8_from_locale(doc->file_name);
728 /* replace %f with the filename (including extension) */
729 replacement = g_path_get_basename(filename);
730 utils_string_replace_all(stack, "%f", replacement);
731 g_free(replacement);
733 /* replace %d with the absolute path of the dir of the current file */
734 replacement = g_path_get_dirname(filename);
735 utils_string_replace_all(stack, "%d", replacement);
736 g_free(replacement);
738 /* replace %e with the filename (excluding extension) */
739 executable = utils_remove_ext_from_filename(filename);
740 replacement = g_path_get_basename(executable);
741 utils_string_replace_all(stack, "%e", replacement);
742 g_free(replacement);
745 /* replace %p with the current project's (absolute) base directory */
746 replacement = NULL; /* prevent double free if no replacement found */
747 if (app->project)
749 replacement = project_get_base_path();
751 else if (strstr(stack->str, "%p"))
752 { /* fall back to %d */
753 ui_set_statusbar(FALSE, _("failed to substitute %%p, no project active"));
754 if (doc != NULL && filename != NULL)
755 replacement = g_path_get_dirname(filename);
758 utils_string_replace_all(stack, "%p", replacement);
759 g_free(replacement);
761 ret_str = utils_get_utf8_from_locale(stack->str);
762 g_free(executable);
763 g_free(filename);
764 g_string_free(stack, TRUE);
766 return ret_str; /* don't forget to free src also if needed */
770 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
771 * idx document directory */
772 static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
774 GError *error = NULL;
775 gchar **argv;
776 gchar *working_dir;
777 gchar *utf8_working_dir;
778 gchar *utf8_cmd_string;
779 #ifdef SYNC_SPAWN
780 gchar *output[2];
781 gint status;
782 #else
783 gint stdout_fd;
784 gint stderr_fd;
785 #endif
787 g_return_val_if_fail(doc == NULL || doc->is_valid, (GPid) -1);
789 if (!((doc != NULL && !EMPTY(doc->file_name)) || !EMPTY(dir)))
791 geany_debug("Failed to run command with no working directory");
792 ui_set_statusbar(TRUE, _("Process failed, no working directory"));
793 return (GPid) 1;
796 clear_all_errors();
797 SETPTR(current_dir_entered, NULL);
799 #ifdef G_OS_WIN32
800 argv = g_strsplit(cmd, " ", 0);
801 #else
802 argv = g_new0(gchar *, 4);
803 argv[0] = g_strdup("/bin/sh");
804 argv[1] = g_strdup("-c");
805 argv[2] = g_strdup(cmd);
806 argv[3] = NULL;
807 #endif
809 utf8_cmd_string = utils_get_utf8_from_locale(cmd);
810 utf8_working_dir = !EMPTY(dir) ? g_strdup(dir) : g_path_get_dirname(doc->file_name);
811 working_dir = utils_get_locale_from_utf8(utf8_working_dir);
813 gtk_list_store_clear(msgwindow.store_compiler);
814 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
815 msgwin_compiler_add(COLOR_BLUE, _("%s (in directory: %s)"), utf8_cmd_string, utf8_working_dir);
816 g_free(utf8_working_dir);
817 g_free(utf8_cmd_string);
819 /* set the build info for the message window */
820 g_free(build_info.dir);
821 build_info.dir = g_strdup(working_dir);
822 build_info.file_type_id = (doc == NULL) ? GEANY_FILETYPES_NONE : doc->file_type->id;
823 build_info.message_count = 0;
825 #ifdef SYNC_SPAWN
826 if (! utils_spawn_sync(working_dir, argv, NULL, G_SPAWN_SEARCH_PATH,
827 NULL, NULL, &output[0], &output[1], &status, &error))
828 #else
829 if (! g_spawn_async_with_pipes(working_dir, argv, NULL,
830 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
831 &(build_info.pid), NULL, &stdout_fd, &stderr_fd, &error))
832 #endif
834 geany_debug("build command spawning failed: %s", error->message);
835 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
836 g_strfreev(argv);
837 g_error_free(error);
838 g_free(working_dir);
839 error = NULL;
840 return (GPid) 0;
843 #ifdef SYNC_SPAWN
844 parse_build_output((const gchar**) output, status);
845 g_free(output[0]);
846 g_free(output[1]);
847 #else
848 if (build_info.pid != 0)
850 g_child_watch_add(build_info.pid, (GChildWatchFunc) build_exit_cb, NULL);
851 build_menu_update(doc);
852 ui_progress_bar_start(NULL);
855 /* use GIOChannels to monitor stdout and stderr */
856 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
857 TRUE, build_iofunc, GINT_TO_POINTER(0));
858 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
859 TRUE, build_iofunc, GINT_TO_POINTER(1));
860 #endif
862 g_strfreev(argv);
863 g_free(working_dir);
865 return build_info.pid;
869 /* Returns: NULL if there was an error, or the working directory the script was created in.
870 * vte_cmd_nonscript is the location of a string which is filled with the command to be used
871 * when vc->skip_run_script is set, otherwise it will be set to NULL */
872 static gchar *prepare_run_script(GeanyDocument *doc, gchar **vte_cmd_nonscript, guint cmdindex)
874 GeanyBuildCommand *cmd = NULL;
875 gchar *working_dir = NULL;
876 const gchar *cmd_working_dir;
877 gboolean autoclose = FALSE;
878 gboolean result = FALSE;
879 gchar *tmp;
880 gchar *cmd_string;
881 GError *error = NULL;
883 if (vte_cmd_nonscript != NULL)
884 *vte_cmd_nonscript = NULL;
886 cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
888 cmd_string = build_replace_placeholder(doc, cmd->command);
889 cmd_working_dir = cmd->working_dir;
890 if (EMPTY(cmd_working_dir))
891 cmd_working_dir = "%d";
892 working_dir = build_replace_placeholder(doc, cmd_working_dir); /* in utf-8 */
894 /* only test whether working dir exists, don't change it or else Windows support will break
895 * (gspawn-win32-helper.exe is used by GLib and must be in $PATH which means current working
896 * dir where geany.exe was started from, so we can't change it) */
897 if (EMPTY(working_dir) || ! g_file_test(working_dir, G_FILE_TEST_EXISTS) ||
898 ! g_file_test(working_dir, G_FILE_TEST_IS_DIR))
900 ui_set_statusbar(TRUE, _("Failed to change the working directory to \"%s\""),
901 !EMPTY(working_dir) ? working_dir : "<NULL>" );
902 utils_free_pointers(2, cmd_string, working_dir, NULL);
903 return NULL;
906 #ifdef HAVE_VTE
907 if (vte_info.have_vte && vc->run_in_vte)
909 if (vc->skip_run_script)
911 if (vte_cmd_nonscript != NULL)
912 *vte_cmd_nonscript = cmd_string;
913 else
914 g_free(cmd_string);
916 return working_dir;
918 else
919 /* don't wait for user input at the end of script when we are running in VTE */
920 autoclose = TRUE;
922 #endif
924 /* RUN_SCRIPT_CMD should be ok in UTF8 without converting in locale because it
925 * contains no umlauts */
926 tmp = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
927 result = build_create_shellscript(tmp, cmd_string, autoclose, &error);
928 if (! result)
930 ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created: %s)"),
931 !EMPTY(cmd_string) ? cmd_string : NULL, error->message);
932 g_error_free(error);
935 utils_free_pointers(2, cmd_string, tmp, NULL);
937 if (result)
938 return working_dir;
940 g_free(working_dir);
941 return NULL;
945 static GPid build_run_cmd(GeanyDocument *doc, guint cmdindex)
947 gchar *working_dir;
948 gchar *vte_cmd_nonscript = NULL;
949 GError *error = NULL;
951 if (! DOC_VALID(doc) || doc->file_name == NULL)
952 return (GPid) 0;
954 working_dir = prepare_run_script(doc, &vte_cmd_nonscript, cmdindex);
955 if (working_dir == NULL)
956 return (GPid) 0;
958 run_info[cmdindex].file_type_id = doc->file_type->id;
960 #ifdef HAVE_VTE
961 if (vte_info.have_vte && vc->run_in_vte)
963 gchar *vte_cmd;
965 if (vc->skip_run_script)
967 SETPTR(vte_cmd_nonscript, utils_get_utf8_from_locale(vte_cmd_nonscript));
968 vte_cmd = g_strconcat(vte_cmd_nonscript, "\n", NULL);
969 g_free(vte_cmd_nonscript);
971 else
972 vte_cmd = g_strconcat("\n/bin/sh ", RUN_SCRIPT_CMD, "\n", NULL);
974 /* change into current directory if it is not done by default */
975 if (! vc->follow_path)
977 /* we need to convert the working_dir back to UTF-8 because the VTE expects it */
978 gchar *utf8_working_dir = utils_get_utf8_from_locale(working_dir);
979 vte_cwd(utf8_working_dir, TRUE);
980 g_free(utf8_working_dir);
982 if (! vte_send_cmd(vte_cmd))
984 ui_set_statusbar(FALSE,
985 _("Could not execute the file in the VTE because it probably contains a command."));
986 geany_debug("Could not execute the file in the VTE because it probably contains a command.");
989 /* show the VTE */
990 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
991 gtk_widget_grab_focus(vc->vte);
992 msgwin_show_hide(TRUE);
994 run_info[cmdindex].pid = 1;
996 g_free(vte_cmd);
998 else
999 #endif
1001 gchar *locale_term_cmd = NULL;
1002 gint argv_len, i;
1003 gchar **argv = NULL;
1004 gchar *script_path = NULL;
1006 /* get the terminal path */
1007 locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
1008 /* split the term_cmd, so arguments will work too */
1009 if (!g_shell_parse_argv(locale_term_cmd, &argv_len, &argv, NULL))
1011 ui_set_statusbar(TRUE,
1012 _("Could not parse terminal command \"%s\" "
1013 "(check Terminal tool setting in Preferences)"), tool_prefs.term_cmd);
1014 run_info[cmdindex].pid = (GPid) 1;
1015 script_path = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
1016 g_unlink(script_path);
1017 goto free_strings;
1020 /* check that terminal exists (to prevent misleading error messages) */
1021 if (argv[0] != NULL)
1023 gchar *tmp = argv[0];
1024 /* g_find_program_in_path checks whether tmp exists and is executable */
1025 argv[0] = g_find_program_in_path(tmp);
1026 g_free(tmp);
1028 if (argv[0] == NULL)
1030 ui_set_statusbar(TRUE,
1031 _("Could not find terminal \"%s\" "
1032 "(check path for Terminal tool setting in Preferences)"), tool_prefs.term_cmd);
1033 run_info[cmdindex].pid = (GPid) 1;
1034 script_path = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
1035 g_unlink(script_path);
1036 goto free_strings;
1039 for (i = 0; i < argv_len; i++)
1041 utils_str_replace_all(&(argv[i]), "%c", RUN_SCRIPT_CMD);
1044 if (! g_spawn_async(working_dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
1045 NULL, NULL, &(run_info[cmdindex].pid), &error))
1047 geany_debug("g_spawn_async() failed: %s", error->message);
1048 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
1049 g_error_free(error);
1050 script_path = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
1051 g_unlink(script_path);
1052 error = NULL;
1053 run_info[cmdindex].pid = (GPid) 0;
1056 if (run_info[cmdindex].pid != 0)
1058 g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
1059 (gpointer)&(run_info[cmdindex]));
1060 build_menu_update(doc);
1062 free_strings:
1063 g_strfreev(argv);
1064 g_free(locale_term_cmd);
1065 g_free(script_path);
1068 g_free(working_dir);
1069 return run_info[cmdindex].pid;
1073 static void process_build_output_line(const gchar *str, gint color)
1075 gchar *msg, *tmp;
1076 gchar *filename;
1077 gint line;
1079 msg = g_strdup(str);
1081 g_strchomp(msg);
1083 if (EMPTY(msg))
1085 g_free(msg);
1086 return;
1089 if (build_parse_make_dir(msg, &tmp))
1091 SETPTR(current_dir_entered, tmp);
1093 msgwin_parse_compiler_error_line(msg, current_dir_entered, &filename, &line);
1095 if (line != -1 && filename != NULL)
1097 GeanyDocument *doc = document_find_by_filename(filename);
1099 /* limit number of indicators */
1100 if (doc && editor_prefs.use_indicators &&
1101 build_info.message_count < GEANY_BUILD_ERR_HIGHLIGHT_MAX)
1103 if (line > 0) /* some compilers, like pdflatex report errors on line 0 */
1104 line--; /* so only adjust the line number if it is greater than 0 */
1105 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line);
1107 build_info.message_count++;
1108 color = COLOR_RED; /* error message parsed on the line */
1110 g_free(filename);
1112 msgwin_compiler_add_string(color, msg);
1113 g_free(msg);
1117 #ifndef SYNC_SPAWN
1118 static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
1120 if (cond & (G_IO_IN | G_IO_PRI))
1122 gchar *msg;
1123 GIOStatus st;
1125 while ((st = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL)) == G_IO_STATUS_NORMAL && msg)
1127 gint color = (GPOINTER_TO_INT(data)) ? COLOR_DARK_RED : COLOR_BLACK;
1129 process_build_output_line(msg, color);
1130 g_free(msg);
1132 if (st == G_IO_STATUS_ERROR || st == G_IO_STATUS_EOF) return FALSE;
1134 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
1135 return FALSE;
1137 return TRUE;
1139 #endif
1142 gboolean build_parse_make_dir(const gchar *string, gchar **prefix)
1144 const gchar *pos;
1146 *prefix = NULL;
1148 if (string == NULL)
1149 return FALSE;
1151 if ((pos = strstr(string, "Entering directory")) != NULL)
1153 gsize len;
1154 gchar *input;
1156 /* get the start of the path */
1157 pos = strstr(string, "/");
1159 if (pos == NULL)
1160 return FALSE;
1162 input = g_strdup(pos);
1164 /* kill the ' at the end of the path */
1165 len = strlen(input);
1166 input[len - 1] = '\0';
1167 input = g_realloc(input, len); /* shorten by 1 */
1168 *prefix = input;
1170 return TRUE;
1173 if (strstr(string, "Leaving directory") != NULL)
1175 *prefix = NULL;
1176 return TRUE;
1179 return FALSE;
1183 static void show_build_result_message(gboolean failure)
1185 gchar *msg;
1187 if (failure)
1189 msg = _("Compilation failed.");
1190 msgwin_compiler_add_string(COLOR_BLUE, msg);
1191 /* If msgwindow is hidden, user will want to display it to see the error */
1192 if (! ui_prefs.msgwindow_visible)
1194 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1195 msgwin_show_hide(TRUE);
1197 else
1198 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1199 ui_set_statusbar(FALSE, "%s", msg);
1201 else
1203 msg = _("Compilation finished successfully.");
1204 msgwin_compiler_add_string(COLOR_BLUE, msg);
1205 if (! ui_prefs.msgwindow_visible ||
1206 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1207 ui_set_statusbar(FALSE, "%s", msg);
1212 #ifndef SYNC_SPAWN
1213 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data)
1215 gboolean failure = FALSE;
1217 #ifdef G_OS_WIN32
1218 failure = status;
1219 #else
1220 if (WIFEXITED(status))
1222 if (WEXITSTATUS(status) != EXIT_SUCCESS)
1223 failure = TRUE;
1225 else if (WIFSIGNALED(status))
1227 /* the terminating signal: WTERMSIG (status)); */
1228 failure = TRUE;
1230 else
1231 { /* any other failure occured */
1232 failure = TRUE;
1234 #endif
1235 show_build_result_message(failure);
1237 utils_beep();
1238 g_spawn_close_pid(child_pid);
1240 build_info.pid = 0;
1241 /* enable build items again */
1242 build_menu_update(NULL);
1243 ui_progress_bar_stop();
1245 #endif
1248 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data)
1250 RunInfo *run_info_data = user_data;
1252 g_spawn_close_pid(child_pid);
1254 run_info_data->pid = 0;
1255 /* reset the stop button and menu item to the original meaning */
1256 build_menu_update(NULL);
1260 static void set_file_error_from_errno(GError **error, gint err, const gchar *prefix)
1262 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(err), "%s%s%s",
1263 prefix ? prefix : "", prefix ? ": " : "", g_strerror(err));
1267 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1268 * fname is the full file name (including path) for the script to create */
1269 static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose, GError **error)
1271 FILE *fp;
1272 gchar *str;
1273 gboolean success = TRUE;
1274 #ifdef G_OS_WIN32
1275 gchar *expanded_cmd;
1276 #endif
1278 fp = g_fopen(fname, "w");
1279 if (! fp)
1281 set_file_error_from_errno(error, errno, "Failed to create file");
1282 return FALSE;
1284 #ifdef G_OS_WIN32
1285 /* Expand environment variables like %blah%. */
1286 expanded_cmd = win32_expand_environment_variables(cmd);
1287 str = g_strdup_printf("%s\n\n%s\ndel \"%%0\"\n\npause\n", expanded_cmd, (autoclose) ? "" : "pause");
1288 g_free(expanded_cmd);
1289 #else
1290 str = g_strdup_printf(
1291 "#!/bin/sh\n\nrm $0\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1292 \n\n%s\n", cmd, (autoclose) ? "" :
1293 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1294 "dash\ndummy_var=\"\"\nread dummy_var");
1295 #endif
1297 if (fputs(str, fp) < 0)
1299 set_file_error_from_errno(error, errno, "Failed to write file");
1300 success = FALSE;
1302 g_free(str);
1304 if (fclose(fp) != 0)
1306 if (error && ! *error) /* don't set error twice */
1307 set_file_error_from_errno(error, errno, "Failed to close file");
1308 success = FALSE;
1311 return success;
1315 typedef void Callback(GtkWidget *w, gpointer u);
1317 /* run the command catenating cmd_cat if present */
1318 static void build_command(GeanyDocument *doc, GeanyBuildGroup grp, guint cmd, gchar *cmd_cat)
1320 gchar *dir;
1321 gchar *full_command, *subs_command;
1322 GeanyBuildCommand *buildcmd = get_build_cmd(doc, grp, cmd, NULL);
1323 gchar *cmdstr;
1325 if (buildcmd == NULL)
1326 return;
1328 cmdstr = buildcmd->command;
1330 if (cmd_cat != NULL)
1332 if (cmdstr != NULL)
1333 full_command = g_strconcat(cmdstr, cmd_cat, NULL);
1334 else
1335 full_command = g_strdup(cmd_cat);
1337 else
1338 full_command = cmdstr;
1340 dir = build_replace_placeholder(doc, buildcmd->working_dir);
1341 subs_command = build_replace_placeholder(doc, full_command);
1342 build_info.grp = grp;
1343 build_info.cmd = cmd;
1344 build_spawn_cmd(doc, subs_command, dir);
1345 g_free(subs_command);
1346 g_free(dir);
1347 if (cmd_cat != NULL)
1348 g_free(full_command);
1349 build_menu_update(doc);
1354 /*----------------------------------------------------------------
1356 * Create build menu and handle callbacks (&toolbar callbacks)
1358 *----------------------------------------------------------------*/
1359 static void on_make_custom_input_response(const gchar *input)
1361 GeanyDocument *doc = document_get_current();
1363 SETPTR(build_info.custom_target, g_strdup(input));
1364 build_command(doc, GBO_TO_GBG(GEANY_GBO_CUSTOM), GBO_TO_CMD(GEANY_GBO_CUSTOM),
1365 build_info.custom_target);
1369 static void on_build_menu_item(GtkWidget *w, gpointer user_data)
1371 GeanyDocument *doc = document_get_current();
1372 GeanyBuildCommand *bc;
1373 guint grp = GPOINTER_TO_GRP(user_data);
1374 guint cmd = GPOINTER_TO_CMD(user_data);
1376 if (doc && doc->changed)
1378 if (!document_save_file(doc, FALSE))
1379 return;
1381 g_signal_emit_by_name(geany_object, "build-start");
1383 if (grp == GEANY_GBG_NON_FT && cmd == GBO_TO_CMD(GEANY_GBO_CUSTOM))
1385 static GtkWidget *dialog = NULL; /* keep dialog for combo history */
1387 if (! dialog)
1389 dialog = dialogs_show_input_persistent(_("Custom Text"), GTK_WINDOW(main_widgets.window),
1390 _("Enter custom text here, all entered text is appended to the command."),
1391 build_info.custom_target, &on_make_custom_input_response);
1393 else
1395 gtk_widget_show(dialog);
1397 return;
1399 else if (grp == GEANY_GBG_EXEC)
1401 if (run_info[cmd].pid > (GPid) 1)
1403 kill_process(&run_info[cmd].pid);
1404 return;
1406 bc = get_build_cmd(doc, grp, cmd, NULL);
1407 if (bc != NULL && strcmp(bc->command, "builtin") == 0)
1409 gchar *uri;
1410 if (doc == NULL)
1411 return;
1412 uri = g_strconcat("file:///", g_path_skip_root(doc->file_name), NULL);
1413 utils_open_browser(uri);
1414 g_free(uri);
1417 else
1418 build_run_cmd(doc, cmd);
1420 else
1421 build_command(doc, grp, cmd, NULL);
1425 /* group codes for menu items other than the known commands
1426 * value order is important, see the following table for use */
1428 /* the rest in each group */
1429 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1430 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1431 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1432 /* the separator */
1433 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1434 /* the fixed items */
1435 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1436 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1437 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1438 #define MENU_DONE (MENU_COMMANDS + 1)
1441 static struct BuildMenuItemSpec {
1442 const gchar *stock_id;
1443 const gint key_binding;
1444 const guint build_grp;
1445 const guint build_cmd;
1446 const gchar *fix_label;
1447 Callback *cb;
1448 } build_menu_specs[] = {
1449 {GTK_STOCK_CONVERT, GEANY_KEYS_BUILD_COMPILE, GBO_TO_GBG(GEANY_GBO_COMPILE),
1450 GBO_TO_CMD(GEANY_GBO_COMPILE), NULL, on_build_menu_item},
1451 {GEANY_STOCK_BUILD, GEANY_KEYS_BUILD_LINK, GBO_TO_GBG(GEANY_GBO_BUILD),
1452 GBO_TO_CMD(GEANY_GBO_BUILD), NULL, on_build_menu_item},
1453 {NULL, -1, MENU_FT_REST,
1454 GBO_TO_CMD(GEANY_GBO_BUILD) + 1, NULL, on_build_menu_item},
1455 {NULL, -1, MENU_SEPARATOR,
1456 GBF_SEP_1, NULL, NULL},
1457 {NULL, GEANY_KEYS_BUILD_MAKE, GBO_TO_GBG(GEANY_GBO_MAKE_ALL),
1458 GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL, on_build_menu_item},
1459 {NULL, GEANY_KEYS_BUILD_MAKEOWNTARGET, GBO_TO_GBG(GEANY_GBO_CUSTOM),
1460 GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL, on_build_menu_item},
1461 {NULL, GEANY_KEYS_BUILD_MAKEOBJECT, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT),
1462 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL, on_build_menu_item},
1463 {NULL, -1, MENU_NON_FT_REST,
1464 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT) + 1, NULL, on_build_menu_item},
1465 {NULL, -1, MENU_SEPARATOR,
1466 GBF_SEP_2, NULL, NULL},
1467 {GTK_STOCK_GO_DOWN, GEANY_KEYS_BUILD_NEXTERROR, MENU_NEXT_ERROR,
1468 GBF_NEXT_ERROR, N_("_Next Error"), on_build_next_error},
1469 {GTK_STOCK_GO_UP, GEANY_KEYS_BUILD_PREVIOUSERROR, MENU_PREV_ERROR,
1470 GBF_PREV_ERROR, N_("_Previous Error"), on_build_previous_error},
1471 {NULL, -1, MENU_SEPARATOR,
1472 GBF_SEP_3, NULL, NULL},
1473 {GTK_STOCK_EXECUTE, GEANY_KEYS_BUILD_RUN, GBO_TO_GBG(GEANY_GBO_EXEC),
1474 GBO_TO_CMD(GEANY_GBO_EXEC), NULL, on_build_menu_item},
1475 {NULL, -1, MENU_EXEC_REST,
1476 GBO_TO_CMD(GEANY_GBO_EXEC) + 1, NULL, on_build_menu_item},
1477 {NULL, -1, MENU_SEPARATOR,
1478 GBF_SEP_4, NULL, NULL},
1479 {GTK_STOCK_PREFERENCES, GEANY_KEYS_BUILD_OPTIONS, MENU_COMMANDS,
1480 GBF_COMMANDS, N_("_Set Build Commands"), on_set_build_commands_activate},
1481 {NULL, -1, MENU_DONE,
1482 0, NULL, NULL}
1486 static void create_build_menu_item(GtkWidget *menu, GeanyKeyGroup *group, GtkAccelGroup *ag,
1487 struct BuildMenuItemSpec *bs, const gchar *lbl, guint grp, guint cmd)
1489 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(lbl);
1491 if (bs->stock_id != NULL)
1493 GtkWidget *image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1494 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1496 gtk_widget_show(item);
1497 if (bs->key_binding >= 0)
1498 add_menu_accel(group, (guint) bs->key_binding, ag, item);
1499 gtk_container_add(GTK_CONTAINER(menu), item);
1500 if (bs->cb != NULL)
1502 g_signal_connect(item, "activate", G_CALLBACK(bs->cb), GRP_CMD_TO_POINTER(grp,cmd));
1504 menu_items.menu_item[grp][cmd] = item;
1508 static void create_build_menu(BuildMenuItems *build_menu_items)
1510 GtkWidget *menu;
1511 GtkAccelGroup *accel_group = gtk_accel_group_new();
1512 GeanyKeyGroup *keygroup = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
1513 guint i, j;
1515 menu = gtk_menu_new();
1516 build_menu_items->menu_item[GEANY_GBG_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_FT]);
1517 build_menu_items->menu_item[GEANY_GBG_NON_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_NON_FT]);
1518 build_menu_items->menu_item[GEANY_GBG_EXEC] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_EXEC]);
1519 build_menu_items->menu_item[GBG_FIXED] = g_new0(GtkWidget*, GBF_COUNT);
1521 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1523 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1524 if (bs->build_grp == MENU_SEPARATOR)
1526 GtkWidget *item = gtk_separator_menu_item_new();
1527 gtk_widget_show(item);
1528 gtk_container_add(GTK_CONTAINER(menu), item);
1529 build_menu_items->menu_item[GBG_FIXED][bs->build_cmd] = item;
1531 else if (bs->fix_label != NULL)
1533 create_build_menu_item(menu, keygroup, accel_group, bs, _(bs->fix_label),
1534 GBG_FIXED, bs->build_cmd);
1536 else if (bs->build_grp >= MENU_FT_REST && bs->build_grp <= MENU_SEPARATOR)
1538 guint grp = bs->build_grp - GEANY_GBG_COUNT;
1539 for (j = bs->build_cmd; j < build_groups_count[grp]; ++j)
1541 GeanyBuildCommand *bc = get_build_cmd(NULL, grp, j, NULL);
1542 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1543 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, grp, j);
1546 else
1548 GeanyBuildCommand *bc = get_build_cmd(NULL, bs->build_grp, bs->build_cmd, NULL);
1549 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1550 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, bs->build_grp, bs->build_cmd);
1553 build_menu_items->menu = menu;
1554 gtk_widget_show(menu);
1555 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_build1")), menu);
1559 /* portability to various GTK versions needs checking
1560 * conforms to description of gtk_accel_label as child of menu item
1561 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1562 static void geany_menu_item_set_label(GtkWidget *w, const gchar *label)
1564 GtkWidget *c = gtk_bin_get_child(GTK_BIN(w));
1566 gtk_label_set_text_with_mnemonic(GTK_LABEL(c), label);
1570 /* * Update the build menu to reflect changes in configuration or status.
1572 * Sets the labels and number of visible items to match the highest
1573 * priority configured commands. Also sets sensitivity if build commands are
1574 * running and switches executes to stop when commands are running.
1576 * @param doc The current document, if available, to save looking it up.
1577 * If @c NULL it will be looked up.
1579 * Call this after modifying any fields of a GeanyBuildCommand structure.
1581 * @see Build Menu Configuration section of the Manual.
1584 void build_menu_update(GeanyDocument *doc)
1586 guint i, cmdcount, cmd, grp;
1587 gboolean vis = FALSE;
1588 gboolean have_path, build_running, exec_running, have_errors, cmd_sensitivity;
1589 gboolean can_compile, can_build, can_make, run_sensitivity = FALSE, run_running = FALSE;
1590 GeanyBuildCommand *bc;
1592 g_return_if_fail(doc == NULL || doc->is_valid);
1594 if (menu_items.menu == NULL)
1595 create_build_menu(&menu_items);
1596 if (doc == NULL)
1597 doc = document_get_current();
1598 have_path = doc != NULL && doc->file_name != NULL;
1599 build_running = build_info.pid > (GPid) 1;
1600 have_errors = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_compiler), NULL) > 0;
1601 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1603 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1604 switch (bs->build_grp)
1606 case MENU_SEPARATOR:
1607 if (vis == TRUE)
1609 gtk_widget_show_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1610 vis = FALSE;
1612 else
1613 gtk_widget_hide(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1614 break;
1615 case MENU_NEXT_ERROR:
1616 case MENU_PREV_ERROR:
1617 gtk_widget_set_sensitive(menu_items.menu_item[GBG_FIXED][bs->build_cmd], have_errors);
1618 vis |= TRUE;
1619 break;
1620 case MENU_COMMANDS:
1621 vis |= TRUE;
1622 break;
1623 default: /* all configurable commands */
1624 if (bs->build_grp >= GEANY_GBG_COUNT)
1626 grp = bs->build_grp - GEANY_GBG_COUNT;
1627 cmdcount = build_groups_count[grp];
1629 else
1631 grp = bs->build_grp;
1632 cmdcount = bs->build_cmd + 1;
1634 for (cmd = bs->build_cmd; cmd < cmdcount; ++cmd)
1636 GtkWidget *menu_item = menu_items.menu_item[grp][cmd];
1637 const gchar *label;
1638 bc = get_build_cmd(doc, grp, cmd, NULL);
1639 if (bc)
1640 label = bc->label;
1641 else
1642 label = NULL;
1644 if (grp < GEANY_GBG_EXEC)
1646 cmd_sensitivity =
1647 (grp == GEANY_GBG_FT && bc != NULL && have_path && ! build_running) ||
1648 (grp == GEANY_GBG_NON_FT && bc != NULL && ! build_running);
1649 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1650 if (bc != NULL && !EMPTY(label))
1652 geany_menu_item_set_label(menu_item, label);
1653 gtk_widget_show_all(menu_item);
1654 vis |= TRUE;
1656 else
1657 gtk_widget_hide(menu_item);
1659 else
1661 GtkWidget *image;
1662 exec_running = run_info[cmd].pid > (GPid) 1;
1663 cmd_sensitivity = (bc != NULL) || exec_running;
1664 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1665 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1666 run_sensitivity = cmd_sensitivity;
1667 if (! exec_running)
1669 image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1671 else
1673 image = gtk_image_new_from_stock(GTK_STOCK_STOP, GTK_ICON_SIZE_MENU);
1675 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1676 run_running = exec_running;
1677 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
1678 if (bc != NULL && !EMPTY(label))
1680 geany_menu_item_set_label(menu_item, label);
1681 gtk_widget_show_all(menu_item);
1682 vis |= TRUE;
1684 else
1685 gtk_widget_hide(menu_item);
1691 run_sensitivity &= (doc != NULL);
1692 can_build = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_BUILD), NULL) != NULL
1693 && have_path && ! build_running;
1694 if (widgets.toolitem_build != NULL)
1695 gtk_widget_set_sensitive(widgets.toolitem_build, can_build);
1696 can_make = FALSE;
1697 if (widgets.toolitem_make_all != NULL)
1698 gtk_widget_set_sensitive(widgets.toolitem_make_all,
1699 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL) != NULL
1700 && ! build_running));
1701 if (widgets.toolitem_make_custom != NULL)
1702 gtk_widget_set_sensitive(widgets.toolitem_make_custom,
1703 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL) != NULL
1704 && ! build_running));
1705 if (widgets.toolitem_make_object != NULL)
1706 gtk_widget_set_sensitive(widgets.toolitem_make_object,
1707 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL) != NULL
1708 && ! build_running));
1709 if (widgets.toolitem_set_args != NULL)
1710 gtk_widget_set_sensitive(widgets.toolitem_set_args, TRUE);
1712 can_compile = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_COMPILE), NULL) != NULL
1713 && have_path && ! build_running;
1714 gtk_action_set_sensitive(widgets.compile_action, can_compile);
1715 gtk_action_set_sensitive(widgets.build_action, can_make);
1716 gtk_action_set_sensitive(widgets.run_action, run_sensitivity);
1718 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1719 set_stop_button(run_running);
1724 /* Call build_menu_update() instead of calling this directly. */
1725 static void set_stop_button(gboolean stop)
1727 const gchar *button_stock_id = NULL;
1728 GtkToolButton *run_button;
1730 run_button = GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1731 if (run_button != NULL)
1732 button_stock_id = gtk_tool_button_get_stock_id(run_button);
1734 if (stop && utils_str_equal(button_stock_id, GTK_STOCK_STOP))
1735 return;
1736 if (! stop && utils_str_equal(button_stock_id, GTK_STOCK_EXECUTE))
1737 return;
1739 /* use the run button also as stop button */
1740 if (stop)
1742 if (run_button != NULL)
1743 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_STOP);
1745 else
1747 if (run_button != NULL)
1748 gtk_tool_button_set_stock_id(run_button, GTK_STOCK_EXECUTE);
1753 static void on_set_build_commands_activate(GtkWidget *w, gpointer u)
1755 /* For now, just show the project dialog */
1756 if (app->project)
1757 project_build_properties();
1758 else
1759 show_build_commands_dialog();
1763 static void on_toolbutton_build_activate(GtkWidget *menuitem, gpointer user_data)
1765 last_toolbutton_action = user_data;
1766 g_object_set(widgets.build_action, "tooltip", _("Build the current file"), NULL);
1767 on_build_menu_item(menuitem, user_data);
1771 static void on_toolbutton_make_activate(GtkWidget *menuitem, gpointer user_data)
1773 gchar *msg;
1775 last_toolbutton_action = user_data;
1776 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_ALL))
1777 msg = _("Build the current file with Make and the default target");
1778 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_CUSTOM))
1779 msg = _("Build the current file with Make and the specified target");
1780 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT))
1781 msg = _("Compile the current file with Make");
1782 else
1783 msg = NULL;
1784 g_object_set(widgets.build_action, "tooltip", msg, NULL);
1785 on_build_menu_item(menuitem, user_data);
1789 static void kill_process(GPid *pid)
1791 gint result;
1793 #ifdef G_OS_WIN32
1794 g_return_if_fail(*pid != NULL);
1795 result = TerminateProcess(*pid, 0);
1796 /* TerminateProcess() returns TRUE on success, for the check below we have to convert
1797 * it to FALSE (and vice versa) */
1798 result = ! result;
1799 #else
1800 g_return_if_fail(*pid > 1);
1801 result = kill(*pid, SIGTERM);
1802 #endif
1804 if (result != 0)
1805 ui_set_statusbar(TRUE, _("Process could not be stopped (%s)."), g_strerror(errno));
1806 else
1808 *pid = 0;
1809 build_menu_update(NULL);
1814 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data)
1816 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_compiler),
1817 msgwin_goto_compiler_file_line))
1819 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1821 else
1822 ui_set_statusbar(FALSE, _("No more build errors."));
1826 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data)
1828 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_compiler),
1829 msgwin_goto_compiler_file_line))
1831 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1833 else
1834 ui_set_statusbar(FALSE, _("No more build errors."));
1838 void build_toolbutton_build_clicked(GtkAction *action, gpointer unused)
1840 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_BUILD))
1842 on_build_menu_item(NULL, GBO_TO_POINTER(GEANY_GBO_BUILD));
1844 else
1846 on_build_menu_item(NULL, last_toolbutton_action);
1851 /*------------------------------------------------------
1853 * Create and handle the build menu configuration dialog
1855 *-------------------------------------------------------*/
1856 typedef struct RowWidgets
1858 GtkWidget *entries[GEANY_BC_CMDENTRIES_COUNT];
1859 GeanyBuildSource src;
1860 GeanyBuildSource dst;
1861 GeanyBuildCommand *cmdsrc;
1862 guint grp;
1863 guint cmd;
1864 gboolean cleared;
1865 gboolean used_dst;
1866 } RowWidgets;
1868 static GdkColor *insensitive_color;
1870 static void set_row_color(RowWidgets *r, GdkColor *color )
1872 enum GeanyBuildCmdEntries i;
1874 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1875 gtk_widget_modify_text(r->entries[i], GTK_STATE_NORMAL, color);
1879 static void set_build_command_entry_text(GtkWidget *wid, const gchar *text)
1881 if (GTK_IS_BUTTON(wid))
1882 gtk_button_set_label(GTK_BUTTON(wid), text);
1883 else
1884 gtk_entry_set_text(GTK_ENTRY(wid), text);
1888 static void on_clear_dialog_row(GtkWidget *unused, gpointer user_data)
1890 RowWidgets *r = user_data;
1891 guint src;
1892 enum GeanyBuildCmdEntries i;
1893 GeanyBuildCommand *bc = get_next_build_cmd(NULL, r->grp, r->cmd, r->dst, &src);
1895 if (bc != NULL)
1897 r->cmdsrc = bc;
1898 r->src = src;
1899 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1901 set_build_command_entry_text(r->entries[i],
1902 id_to_str(bc,i) != NULL ? id_to_str(bc,i) : "");
1905 else
1907 r->cmdsrc = NULL;
1908 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1910 set_build_command_entry_text(r->entries[i], "");
1913 r->used_dst = FALSE;
1914 set_row_color(r, insensitive_color);
1915 r->cleared = TRUE;
1919 static void on_clear_dialog_regex_row(GtkEntry *regex, gpointer unused)
1921 gtk_entry_set_text(regex,"");
1925 static void on_label_button_clicked(GtkWidget *wid, gpointer user_data)
1927 RowWidgets *r = user_data;
1928 GtkWidget *top_level = gtk_widget_get_toplevel(wid);
1929 const gchar *old = gtk_button_get_label(GTK_BUTTON(wid));
1930 gchar *str;
1932 if (gtk_widget_is_toplevel(top_level) && GTK_IS_WINDOW(top_level))
1933 str = dialogs_show_input(_("Set menu item label"), GTK_WINDOW(top_level), NULL, old);
1934 else
1935 str = dialogs_show_input(_("Set menu item label"), NULL, NULL, old);
1937 if (!str)
1938 return;
1940 gtk_button_set_label(GTK_BUTTON(wid), str);
1941 g_free(str);
1942 r->used_dst = TRUE;
1943 set_row_color(r, NULL);
1947 static void on_entry_focus(GtkWidget *wid, GdkEventFocus *unused, gpointer user_data)
1949 RowWidgets *r = user_data;
1951 r->used_dst = TRUE;
1952 set_row_color(r, NULL);
1956 /* Column headings, array NULL-terminated */
1957 static const gchar *colheads[] =
1959 "#",
1960 N_("Label"),
1961 N_("Command"),
1962 N_("Working directory"),
1963 N_("Reset"),
1964 NULL
1967 /* column names */
1968 #define DC_ITEM 0
1969 #define DC_ENTRIES 1
1970 #define DC_CLEAR 4
1971 #define DC_N_COL 5
1973 static const guint entry_x_padding = 3;
1974 static const guint entry_y_padding = 0;
1977 static RowWidgets *build_add_dialog_row(GeanyDocument *doc, GtkTable *table, guint row,
1978 GeanyBuildSource dst, guint grp, guint cmd, gboolean dir)
1980 GtkWidget *label, *clear, *clearicon;
1981 RowWidgets *roww;
1982 GeanyBuildCommand *bc;
1983 guint src;
1984 enum GeanyBuildCmdEntries i;
1985 guint column = 0;
1986 gchar *text;
1988 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
1990 text = g_strdup_printf("%d.", cmd + 1);
1991 label = gtk_label_new(text);
1992 g_free(text);
1993 insensitive_color = &(gtk_widget_get_style(label)->text[GTK_STATE_INSENSITIVE]);
1994 gtk_table_attach(table, label, column, column + 1, row, row + 1, GTK_FILL,
1995 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1996 roww = g_new0(RowWidgets, 1);
1997 roww->src = GEANY_BCS_COUNT;
1998 roww->grp = grp;
1999 roww->cmd = cmd;
2000 roww->dst = dst;
2001 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2003 gint xflags = (i == GEANY_BC_COMMAND) ? GTK_FILL | GTK_EXPAND : GTK_FILL;
2005 column += 1;
2006 if (i == GEANY_BC_LABEL)
2008 GtkWidget *wid = roww->entries[i] = gtk_button_new();
2009 gtk_button_set_use_underline(GTK_BUTTON(wid), TRUE);
2010 gtk_widget_set_tooltip_text(wid, _("Click to set menu item label"));
2011 g_signal_connect(wid, "clicked", G_CALLBACK(on_label_button_clicked), roww);
2013 else
2015 roww->entries[i] = gtk_entry_new();
2016 g_signal_connect(roww->entries[i], "focus-in-event", G_CALLBACK(on_entry_focus), roww);
2018 gtk_table_attach(table, roww->entries[i], column, column + 1, row, row + 1, xflags,
2019 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2021 column++;
2022 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2023 clear = gtk_button_new();
2024 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2025 g_signal_connect(clear, "clicked", G_CALLBACK(on_clear_dialog_row), roww);
2026 gtk_table_attach(table, clear, column, column + 1, row, row + 1, GTK_FILL,
2027 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2028 roww->cmdsrc = bc = get_build_cmd(doc, grp, cmd, &src);
2029 if (bc != NULL)
2030 roww->src = src;
2032 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2034 const gchar *str = "";
2036 if (bc != NULL )
2038 if ((str = id_to_str(bc, i)) == NULL)
2039 str = "";
2040 else if (dst == src)
2041 roww->used_dst = TRUE;
2043 set_build_command_entry_text(roww->entries[i], str);
2045 if (bc != NULL && (dst > src))
2046 set_row_color(roww, insensitive_color);
2047 if (bc != NULL && (src > dst || (grp == GEANY_GBG_FT && (doc == NULL || doc->file_type == NULL))))
2049 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2050 gtk_widget_set_sensitive(roww->entries[i], FALSE);
2051 gtk_widget_set_sensitive(clear, FALSE);
2053 return roww;
2057 typedef struct BuildTableFields
2059 RowWidgets **rows;
2060 GtkWidget *fileregex;
2061 GtkWidget *nonfileregex;
2062 gchar **fileregexstring;
2063 gchar **nonfileregexstring;
2064 } BuildTableFields;
2067 GtkWidget *build_commands_table(GeanyDocument *doc, GeanyBuildSource dst, BuildTableData *table_data,
2068 GeanyFiletype *ft)
2070 GtkWidget *label, *sep, *clearicon, *clear;
2071 BuildTableFields *fields;
2072 GtkTable *table;
2073 const gchar **ch;
2074 gchar *txt;
2075 guint col, row, cmdindex;
2076 guint cmd;
2077 guint src;
2078 gboolean sensitivity;
2079 guint sep_padding = entry_y_padding + 3;
2081 table = GTK_TABLE(gtk_table_new(build_items_count + 12, 5, FALSE));
2082 fields = g_new0(BuildTableFields, 1);
2083 fields->rows = g_new0(RowWidgets*, build_items_count);
2084 for (ch = colheads, col = 0; *ch != NULL; ch++, col++)
2086 label = gtk_label_new(_(*ch));
2087 gtk_table_attach(table, label, col, col + 1, 0, 1,
2088 GTK_FILL, GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2090 sep = gtk_hseparator_new();
2091 gtk_table_attach(table, sep, 0, DC_N_COL, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND,
2092 entry_x_padding, sep_padding);
2093 if (ft != NULL && ft->id != GEANY_FILETYPES_NONE)
2094 txt = g_strdup_printf(_("%s commands"), ft->name);
2095 else
2096 txt = g_strdup_printf(_("%s commands"), _("No filetype"));
2098 label = ui_label_new_bold(txt);
2099 g_free(txt);
2100 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2101 gtk_table_attach(table, label, 0, DC_N_COL, 2, 3, GTK_FILL, GTK_FILL | GTK_EXPAND,
2102 entry_x_padding, entry_y_padding);
2103 for (row = 3, cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++row, ++cmdindex, ++cmd)
2104 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_FT, cmd, FALSE);
2105 label = gtk_label_new(_("Error regular expression:"));
2106 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2107 entry_x_padding, entry_y_padding);
2108 fields->fileregex = gtk_entry_new();
2109 fields->fileregexstring = build_get_regex(GEANY_GBG_FT, NULL, &src);
2110 sensitivity = (ft == NULL) ? FALSE : TRUE;
2111 if (fields->fileregexstring != NULL && *(fields->fileregexstring) != NULL)
2113 gtk_entry_set_text(GTK_ENTRY(fields->fileregex), *(fields->fileregexstring));
2114 if (src > dst)
2115 sensitivity = FALSE;
2117 gtk_table_attach(table, fields->fileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
2118 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2119 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2120 clear = gtk_button_new();
2121 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2122 g_signal_connect_swapped(clear, "clicked",
2123 G_CALLBACK(on_clear_dialog_regex_row), (fields->fileregex));
2124 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
2125 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2126 gtk_widget_set_sensitive(fields->fileregex, sensitivity);
2127 gtk_widget_set_sensitive(clear, sensitivity);
2128 ++row;
2129 sep = gtk_hseparator_new();
2130 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2131 entry_x_padding, sep_padding);
2132 ++row;
2133 label = ui_label_new_bold(_("Independent commands"));
2134 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2135 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2136 entry_x_padding, entry_y_padding);
2137 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++row, ++cmdindex, ++cmd)
2138 fields->rows[cmdindex] = build_add_dialog_row(
2139 doc, table, row, dst, GEANY_GBG_NON_FT, cmd, TRUE);
2140 label = gtk_label_new(_("Error regular expression:"));
2141 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL,
2142 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2143 fields->nonfileregex = gtk_entry_new();
2144 fields->nonfileregexstring = build_get_regex(GEANY_GBG_NON_FT, NULL, &src);
2145 sensitivity = TRUE;
2146 if (fields->nonfileregexstring != NULL && *(fields->nonfileregexstring) != NULL)
2148 gtk_entry_set_text(GTK_ENTRY(fields->nonfileregex), *(fields->nonfileregexstring));
2149 sensitivity = src > dst ? FALSE : TRUE;
2151 gtk_table_attach(table, fields->nonfileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
2152 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2153 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2154 clear = gtk_button_new();
2155 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2156 g_signal_connect_swapped(clear, "clicked",
2157 G_CALLBACK(on_clear_dialog_regex_row), (fields->nonfileregex));
2158 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
2159 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2160 gtk_widget_set_sensitive(fields->nonfileregex, sensitivity);
2161 gtk_widget_set_sensitive(clear, sensitivity);
2162 ++row;
2163 label = gtk_label_new(NULL);
2164 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2165 _("Note: Item 2 opens a dialog and appends the response to the command."));
2166 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2167 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2168 entry_x_padding, entry_y_padding);
2169 ++row;
2170 sep = gtk_hseparator_new();
2171 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2172 entry_x_padding, sep_padding);
2173 ++row;
2174 label = ui_label_new_bold(_("Execute commands"));
2175 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2176 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2177 entry_x_padding, entry_y_padding);
2178 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++row, ++cmdindex, ++cmd)
2179 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_EXEC, cmd, TRUE);
2180 sep = gtk_hseparator_new();
2181 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2182 entry_x_padding, sep_padding);
2183 ++row;
2184 label = gtk_label_new(NULL);
2185 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2186 _("%d, %e, %f, %p are substituted in command and directory fields, see manual for details."));
2187 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2188 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2189 entry_x_padding, entry_y_padding);
2190 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
2191 ++row;
2192 *table_data = fields;
2193 return GTK_WIDGET(table);
2197 void build_free_fields(BuildTableData table_data)
2199 guint cmdindex;
2201 for (cmdindex = 0; cmdindex < build_items_count; ++cmdindex)
2202 g_free(table_data->rows[cmdindex]);
2203 g_free(table_data->rows);
2204 g_free(table_data);
2208 /* string compare where null pointers match null or 0 length strings */
2209 #if 0
2210 static gint stcmp(const gchar *a, const gchar *b)
2212 if (a == NULL && b == NULL)
2213 return 0;
2214 if (a == NULL && b != NULL)
2215 return strlen(b);
2216 if (a != NULL && b == NULL)
2217 return strlen(a);
2218 return strcmp(a, b);
2220 #endif
2223 static const gchar *get_build_command_entry_text(GtkWidget *wid)
2225 if (GTK_IS_BUTTON(wid))
2226 return gtk_button_get_label(GTK_BUTTON(wid));
2227 else
2228 return gtk_entry_get_text(GTK_ENTRY(wid));
2232 static gboolean read_row(BuildDestination *dst, BuildTableData table_data, guint drow, guint grp, guint cmd)
2234 gchar *entries[GEANY_BC_CMDENTRIES_COUNT];
2235 gboolean changed = FALSE;
2236 enum GeanyBuildCmdEntries i;
2238 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2240 entries[i] = g_strdup(get_build_command_entry_text(table_data->rows[drow]->entries[i]));
2242 if (table_data->rows[drow]->cleared)
2244 if (dst->dst[grp] != NULL)
2246 if (*(dst->dst[grp]) == NULL)
2247 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2248 (*(dst->dst[grp]))[cmd].exists = FALSE;
2249 (*(dst->dst[grp]))[cmd].changed = TRUE;
2250 changed = TRUE;
2253 if (table_data->rows[drow]->used_dst == TRUE)
2255 if (dst->dst[grp] != NULL)
2257 if (*(dst->dst[grp]) == NULL)
2258 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2259 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2260 set_command(&(*(dst->dst[grp]))[cmd], i, entries[i]);
2261 (*(dst->dst[grp]))[cmd].exists = TRUE;
2262 (*(dst->dst[grp]))[cmd].changed = TRUE;
2263 changed = TRUE;
2266 else
2268 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2269 g_free(entries[i]);
2271 return changed;
2275 static gboolean read_regex(GtkWidget *regexentry, gchar **src, gchar **dst)
2277 gboolean changed = FALSE;
2278 const gchar *reg = gtk_entry_get_text(GTK_ENTRY(regexentry));
2280 if (((src == NULL /* originally there was no regex */
2281 || *src == NULL) /* or it was NULL*/
2282 && !EMPTY(reg)) /* and something was typed */
2283 || (src != NULL /* originally there was a regex*/
2284 && (*src == NULL /* and either it was NULL */
2285 || strcmp(*src, reg) != 0))) /* or it has been changed */
2287 if (dst != NULL)
2289 SETPTR(*dst, g_strdup(reg));
2290 changed = TRUE;
2293 return changed;
2297 static gboolean build_read_commands(BuildDestination *dst, BuildTableData table_data, gint response)
2299 guint cmdindex, cmd;
2300 gboolean changed = FALSE;
2302 if (response == GTK_RESPONSE_ACCEPT)
2304 for (cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++cmdindex, ++cmd)
2305 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_FT, cmd);
2306 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++cmdindex, ++cmd)
2307 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_NON_FT, cmd);
2308 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++cmdindex, ++cmd)
2309 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_EXEC, cmd);
2310 changed |= read_regex(table_data->fileregex, table_data->fileregexstring, dst->fileregexstr);
2311 changed |= read_regex(table_data->nonfileregex, table_data->nonfileregexstring, dst->nonfileregexstr);
2313 return changed;
2317 void build_read_project(GeanyFiletype *ft, BuildTableData build_properties)
2319 BuildDestination menu_dst;
2321 if (ft != NULL)
2323 menu_dst.dst[GEANY_GBG_FT] = &(ft->projfilecmds);
2324 menu_dst.fileregexstr = &(ft->projerror_regex_string);
2326 else
2328 menu_dst.dst[GEANY_GBG_FT] = NULL;
2329 menu_dst.fileregexstr = NULL;
2331 menu_dst.dst[GEANY_GBG_NON_FT] = &non_ft_proj;
2332 menu_dst.dst[GEANY_GBG_EXEC] = &exec_proj;
2333 menu_dst.nonfileregexstr = &regex_proj;
2335 build_read_commands(&menu_dst, build_properties, GTK_RESPONSE_ACCEPT);
2339 static void show_build_commands_dialog(void)
2341 GtkWidget *dialog, *table, *vbox;
2342 GeanyDocument *doc = document_get_current();
2343 GeanyFiletype *ft = NULL;
2344 const gchar *title = _("Set Build Commands");
2345 gint response;
2346 BuildTableData table_data;
2347 BuildDestination prefdsts;
2349 if (doc != NULL)
2350 ft = doc->file_type;
2351 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
2352 GTK_DIALOG_DESTROY_WITH_PARENT,
2353 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2354 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
2355 table = build_commands_table(doc, GEANY_BCS_PREF, &table_data, ft);
2356 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
2357 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
2358 gtk_widget_show_all(dialog);
2359 /* run modally to prevent user changing idx filetype */
2360 response = gtk_dialog_run(GTK_DIALOG(dialog));
2362 prefdsts.dst[GEANY_GBG_NON_FT] = &non_ft_pref;
2363 if (ft != NULL)
2365 prefdsts.dst[GEANY_GBG_FT] = &(ft->homefilecmds);
2366 prefdsts.fileregexstr = &(ft->homeerror_regex_string);
2367 prefdsts.dst[GEANY_GBG_EXEC] = &(ft->homeexeccmds);
2369 else
2371 prefdsts.dst[GEANY_GBG_FT] = NULL;
2372 prefdsts.fileregexstr = NULL;
2373 prefdsts.dst[GEANY_GBG_EXEC] = NULL;
2375 prefdsts.nonfileregexstr = &regex_pref;
2376 if (build_read_commands(&prefdsts, table_data, response) && ft != NULL)
2377 filetypes_save_commands(ft);
2378 build_free_fields(table_data);
2380 build_menu_update(doc);
2381 gtk_widget_destroy(dialog);
2385 /* Creates the relevant build menu if necessary. */
2386 BuildMenuItems *build_get_menu_items(gint filetype_idx)
2388 BuildMenuItems *items;
2390 items = &menu_items;
2391 if (items->menu == NULL)
2392 create_build_menu(items);
2393 return items;
2397 /*----------------------------------------------------------
2399 * Load and store configuration
2401 * ---------------------------------------------------------*/
2402 static const gchar *build_grp_name = "build-menu";
2404 /* config format for build-menu group is prefix_gg_nn_xx=value
2405 * where gg = FT, NF, EX for the command group
2406 * nn = 2 digit command number
2407 * xx = LB for label, CM for command and WD for working dir */
2408 static const gchar *groups[GEANY_GBG_COUNT] = { "FT", "NF", "EX" };
2409 static const gchar *fixedkey="xx_xx_xx";
2411 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2412 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2413 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2415 static void build_load_menu_grp(GKeyFile *config, GeanyBuildCommand **dst, gint grp,
2416 gchar *prefix, gboolean loc)
2418 guint cmd;
2419 gsize prefixlen; /* NOTE prefixlen used in macros above */
2420 GeanyBuildCommand *dstcmd;
2421 gchar *key;
2422 static gchar cmdbuf[3] = " ";
2424 if (*dst == NULL)
2425 *dst = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2426 dstcmd = *dst;
2427 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2428 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2429 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2431 gchar *label;
2432 if (cmd >= 100)
2433 break; /* ensure no buffer overflow */
2434 sprintf(cmdbuf, "%02u", cmd);
2435 set_key_grp(key, groups[grp]);
2436 set_key_cmd(key, cmdbuf);
2437 set_key_fld(key, "LB");
2438 if (loc)
2439 label = g_key_file_get_locale_string(config, build_grp_name, key, NULL, NULL);
2440 else
2441 label = g_key_file_get_string(config, build_grp_name, key, NULL);
2442 if (label != NULL)
2444 dstcmd[cmd].exists = TRUE;
2445 SETPTR(dstcmd[cmd].label, label);
2446 set_key_fld(key,"CM");
2447 SETPTR(dstcmd[cmd].command,
2448 g_key_file_get_string(config, build_grp_name, key, NULL));
2449 set_key_fld(key,"WD");
2450 SETPTR(dstcmd[cmd].working_dir,
2451 g_key_file_get_string(config, build_grp_name, key, NULL));
2453 else dstcmd[cmd].exists = FALSE;
2455 g_free(key);
2459 /* for the specified source load new format build menu items or try to make some sense of
2460 * old format setings, not done perfectly but better than ignoring them */
2461 void build_load_menu(GKeyFile *config, GeanyBuildSource src, gpointer p)
2463 GeanyFiletype *ft;
2464 GeanyProject *pj;
2465 gchar **ftlist;
2466 gchar *value, *basedir, *makebasedir;
2467 gboolean bvalue = FALSE;
2469 if (g_key_file_has_group(config, build_grp_name))
2471 switch (src)
2473 case GEANY_BCS_FT:
2474 ft = (GeanyFiletype*)p;
2475 if (ft == NULL)
2476 return;
2477 build_load_menu_grp(config, &(ft->filecmds), GEANY_GBG_FT, NULL, TRUE);
2478 build_load_menu_grp(config, &(ft->ftdefcmds), GEANY_GBG_NON_FT, NULL, TRUE);
2479 build_load_menu_grp(config, &(ft->execcmds), GEANY_GBG_EXEC, NULL, TRUE);
2480 SETPTR(ft->error_regex_string,
2481 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2482 break;
2483 case GEANY_BCS_HOME_FT:
2484 ft = (GeanyFiletype*)p;
2485 if (ft == NULL)
2486 return;
2487 build_load_menu_grp(config, &(ft->homefilecmds), GEANY_GBG_FT, NULL, FALSE);
2488 build_load_menu_grp(config, &(ft->homeexeccmds), GEANY_GBG_EXEC, NULL, FALSE);
2489 SETPTR(ft->homeerror_regex_string,
2490 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2491 break;
2492 case GEANY_BCS_PREF:
2493 build_load_menu_grp(config, &non_ft_pref, GEANY_GBG_NON_FT, NULL, FALSE);
2494 build_load_menu_grp(config, &exec_pref, GEANY_GBG_EXEC, NULL, FALSE);
2495 SETPTR(regex_pref, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2496 break;
2497 case GEANY_BCS_PROJ:
2498 build_load_menu_grp(config, &non_ft_proj, GEANY_GBG_NON_FT, NULL, FALSE);
2499 build_load_menu_grp(config, &exec_proj, GEANY_GBG_EXEC, NULL, FALSE);
2500 SETPTR(regex_proj, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2501 pj = (GeanyProject*)p;
2502 if (p == NULL)
2503 return;
2504 ftlist = g_key_file_get_string_list(config, build_grp_name, "filetypes", NULL, NULL);
2505 if (ftlist != NULL)
2507 gchar **ftname;
2508 if (pj->build_filetypes_list == NULL)
2509 pj->build_filetypes_list = g_ptr_array_new();
2510 g_ptr_array_set_size(pj->build_filetypes_list, 0);
2511 for (ftname = ftlist; *ftname != NULL; ++ftname)
2513 ft = filetypes_lookup_by_name(*ftname);
2514 if (ft != NULL)
2516 gchar *regkey = g_strdup_printf("%serror_regex", *ftname);
2517 g_ptr_array_add(pj->build_filetypes_list, ft);
2518 SETPTR(ft->projerror_regex_string,
2519 g_key_file_get_string(config, build_grp_name, regkey, NULL));
2520 g_free(regkey);
2521 build_load_menu_grp(config, &(ft->projfilecmds), GEANY_GBG_FT, *ftname, FALSE);
2522 build_load_menu_grp(config, &(ft->projexeccmds), GEANY_GBG_EXEC, *ftname, FALSE);
2525 g_free(ftlist);
2527 break;
2528 default: /* defaults don't load from config, see build_init */
2529 break;
2533 /* load old [build_settings] values if there is no value defined by [build-menu] */
2535 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2536 /* TODO: rewrite as function */
2537 #define ASSIGNIF(type, id, string, value) \
2538 do { \
2539 gchar *ASSIGNF__value = (value); \
2540 if (!EMPTY(ASSIGNF__value) && ! type[GBO_TO_CMD(id)].exists) { \
2541 type[GBO_TO_CMD(id)].exists = TRUE; \
2542 SETPTR(type[GBO_TO_CMD(id)].label, g_strdup(string)); \
2543 SETPTR(type[GBO_TO_CMD(id)].command, ASSIGNF__value); \
2544 SETPTR(type[GBO_TO_CMD(id)].working_dir, NULL); \
2545 type[GBO_TO_CMD(id)].old = TRUE; \
2546 } else \
2547 g_free(ASSIGNF__value); \
2548 } while (0)
2550 switch (src)
2552 case GEANY_BCS_FT:
2553 ft = (GeanyFiletype*)p;
2554 value = g_key_file_get_string(config, "build_settings", "compiler", NULL);
2555 if (value != NULL)
2557 if (ft->filecmds == NULL)
2558 ft->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2559 ASSIGNIF(ft->filecmds, GEANY_GBO_COMPILE, _("_Compile"), value);
2561 value = g_key_file_get_string(config, "build_settings", "linker", NULL);
2562 if (value != NULL)
2564 if (ft->filecmds == NULL)
2565 ft->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2566 ASSIGNIF(ft->filecmds, GEANY_GBO_BUILD, _("_Build"), value);
2568 value = g_key_file_get_string(config, "build_settings", "run_cmd", NULL);
2569 if (value != NULL)
2571 if (ft->execcmds == NULL)
2572 ft->execcmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2573 ASSIGNIF(ft->execcmds, GEANY_GBO_EXEC, _("_Execute"), value);
2575 if (ft->error_regex_string == NULL)
2576 ft->error_regex_string = g_key_file_get_string(config, "build_settings", "error_regex", NULL);
2577 break;
2578 case GEANY_BCS_PROJ:
2579 if (non_ft_pref == NULL)
2580 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2581 basedir = project_get_base_path();
2582 if (basedir == NULL)
2583 basedir = g_strdup("%d");
2584 bvalue = g_key_file_get_boolean(config, "project", "make_in_base_path", NULL);
2585 if (bvalue)
2586 makebasedir = g_strdup(basedir);
2587 else
2588 makebasedir = g_strdup("%d");
2589 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].old)
2590 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].working_dir, g_strdup(makebasedir));
2591 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].old)
2592 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].working_dir, g_strdup(makebasedir));
2593 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].old)
2594 SETPTR(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].working_dir, g_strdup("%d"));
2595 value = g_key_file_get_string(config, "project", "run_cmd", NULL);
2596 if (!EMPTY(value))
2598 if (exec_proj == NULL)
2599 exec_proj = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2600 if (! exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists)
2602 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists = TRUE;
2603 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].label, g_strdup(_("_Execute")));
2604 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].command, value);
2605 SETPTR(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].working_dir, g_strdup(basedir));
2606 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].old = TRUE;
2609 g_free(makebasedir);
2610 g_free(basedir);
2611 break;
2612 case GEANY_BCS_PREF:
2613 value = g_key_file_get_string(config, "tools", "make_cmd", NULL);
2614 if (value != NULL)
2616 if (non_ft_pref == NULL)
2617 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2618 ASSIGNIF(non_ft_pref, GEANY_GBO_CUSTOM, _("Make Custom _Target..."),
2619 g_strdup_printf("%s ", value));
2620 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_OBJECT, _("Make _Object"),
2621 g_strdup_printf("%s %%e.o",value));
2622 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_ALL, _("_Make"), value);
2624 break;
2625 default:
2626 break;
2631 static guint build_save_menu_grp(GKeyFile *config, GeanyBuildCommand *src, gint grp, gchar *prefix)
2633 guint cmd;
2634 gsize prefixlen; /* NOTE prefixlen used in macros above */
2635 gchar *key;
2636 guint count = 0;
2637 enum GeanyBuildCmdEntries i;
2639 if (src == NULL)
2640 return 0;
2641 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2642 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2643 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2645 if (src[cmd].exists) ++count;
2646 if (src[cmd].changed)
2648 static gchar cmdbuf[4] = " ";
2649 if (cmd >= 100)
2650 break; /* ensure no buffer overflow */
2651 sprintf(cmdbuf, "%02u", cmd);
2652 set_key_grp(key, groups[grp]);
2653 set_key_cmd(key, cmdbuf);
2654 if (src[cmd].exists)
2656 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2658 set_key_fld(key, config_keys[i]);
2659 g_key_file_set_string(config, build_grp_name, key, id_to_str(&src[cmd], i));
2662 else
2664 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2666 set_key_fld(key, config_keys[i]);
2667 g_key_file_remove_key(config, build_grp_name, key, NULL);
2672 g_free(key);
2673 return count;
2677 typedef struct ForEachData
2679 GKeyFile *config;
2680 GPtrArray *ft_names;
2681 } ForEachData;
2684 static void foreach_project_filetype(gpointer data, gpointer user_data)
2686 GeanyFiletype *ft = data;
2687 ForEachData *d = user_data;
2688 guint i = 0;
2689 gchar *regkey = g_strdup_printf("%serror_regex", ft->name);
2691 i += build_save_menu_grp(d->config, ft->projfilecmds, GEANY_GBG_FT, ft->name);
2692 i += build_save_menu_grp(d->config, ft->projexeccmds, GEANY_GBG_EXEC, ft->name);
2693 if (!EMPTY(ft->projerror_regex_string))
2695 g_key_file_set_string(d->config, build_grp_name, regkey, ft->projerror_regex_string);
2696 i++;
2698 else
2699 g_key_file_remove_key(d->config, build_grp_name, regkey, NULL);
2700 g_free(regkey);
2701 if (i > 0)
2702 g_ptr_array_add(d->ft_names, ft->name);
2706 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
2707 void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
2709 GeanyFiletype *ft;
2710 GeanyProject *pj;
2711 ForEachData data;
2713 switch (src)
2715 case GEANY_BCS_HOME_FT:
2716 ft = (GeanyFiletype*)ptr;
2717 if (ft == NULL)
2718 return;
2719 build_save_menu_grp(config, ft->homefilecmds, GEANY_GBG_FT, NULL);
2720 build_save_menu_grp(config, ft->homeexeccmds, GEANY_GBG_EXEC, NULL);
2721 if (!EMPTY(ft->homeerror_regex_string))
2722 g_key_file_set_string(config, build_grp_name, "error_regex", ft->homeerror_regex_string);
2723 else
2724 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2725 break;
2726 case GEANY_BCS_PREF:
2727 build_save_menu_grp(config, non_ft_pref, GEANY_GBG_NON_FT, NULL);
2728 build_save_menu_grp(config, exec_pref, GEANY_GBG_EXEC, NULL);
2729 if (!EMPTY(regex_pref))
2730 g_key_file_set_string(config, build_grp_name, "error_regex", regex_pref);
2731 else
2732 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2733 break;
2734 case GEANY_BCS_PROJ:
2735 pj = (GeanyProject*)ptr;
2736 build_save_menu_grp(config, non_ft_proj, GEANY_GBG_NON_FT, NULL);
2737 build_save_menu_grp(config, exec_proj, GEANY_GBG_EXEC, NULL);
2738 if (!EMPTY(regex_proj))
2739 g_key_file_set_string(config, build_grp_name, "error_regex", regex_proj);
2740 else
2741 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2742 if (pj->build_filetypes_list != NULL)
2744 data.config = config;
2745 data.ft_names = g_ptr_array_new();
2746 g_ptr_array_foreach(pj->build_filetypes_list, foreach_project_filetype, (gpointer)(&data));
2747 if (data.ft_names->pdata != NULL)
2748 g_key_file_set_string_list(config, build_grp_name, "filetypes",
2749 (const gchar**)(data.ft_names->pdata), data.ft_names->len);
2750 else
2751 g_key_file_remove_key(config, build_grp_name, "filetypes", NULL);
2752 g_ptr_array_free(data.ft_names, TRUE);
2754 break;
2755 default: /* defaults and GEANY_BCS_FT can't save */
2756 break;
2761 /* FIXME: count is int only because calling code doesn't handle checking its value itself */
2762 void build_set_group_count(GeanyBuildGroup grp, gint count)
2764 guint i, sum;
2766 g_return_if_fail(count >= 0);
2768 if ((guint) count > build_groups_count[grp])
2769 build_groups_count[grp] = (guint) count;
2770 for (i = 0, sum = 0; i < GEANY_GBG_COUNT; ++i)
2771 sum += build_groups_count[i];
2772 build_items_count = sum;
2776 /** Get the count of commands for the group
2778 * Get the number of commands in the group specified by @a grp.
2780 * @param grp the group of the specified menu item.
2782 * @return a count of the number of commands in the group
2786 guint build_get_group_count(const GeanyBuildGroup grp)
2788 g_return_val_if_fail(grp < GEANY_GBG_COUNT, 0);
2789 return build_groups_count[grp];
2793 static void on_project_close(void)
2795 /* remove project regexen */
2796 SETPTR(regex_proj, NULL);
2800 static struct
2802 const gchar *label;
2803 const gchar *command;
2804 const gchar *working_dir;
2805 GeanyBuildCommand **ptr;
2806 gint index;
2807 } default_cmds[] = {
2808 { N_("_Make"), "make", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_ALL)},
2809 { N_("Make Custom _Target..."), "make ", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_CUSTOM)},
2810 { N_("Make _Object"), "make %e.o", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)},
2811 { N_("_Execute"), "./%e", NULL, &exec_def, GBO_TO_CMD(GEANY_GBO_EXEC)},
2812 { NULL, NULL, NULL, NULL, 0 }
2816 void build_init(void)
2818 GtkWidget *item;
2819 GtkWidget *toolmenu;
2820 gint cmdindex;
2822 g_signal_connect(geany_object, "project-close", on_project_close, NULL);
2824 ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2825 non_ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2826 exec_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2827 run_info = g_new0(RunInfo, build_groups_count[GEANY_GBG_EXEC]);
2829 for (cmdindex = 0; default_cmds[cmdindex].command != NULL; ++cmdindex)
2831 GeanyBuildCommand *cmd = &((*(default_cmds[cmdindex].ptr))[ default_cmds[cmdindex].index ]);
2832 cmd->exists = TRUE;
2833 cmd->label = g_strdup(_(default_cmds[cmdindex].label));
2834 cmd->command = g_strdup(default_cmds[cmdindex].command);
2835 cmd->working_dir = g_strdup(default_cmds[cmdindex].working_dir);
2838 /* create the toolbar Build item sub menu */
2839 toolmenu = gtk_menu_new();
2840 g_object_ref(toolmenu);
2842 /* build the code */
2843 item = ui_image_menu_item_new(GEANY_STOCK_BUILD, _("_Build"));
2844 gtk_widget_show(item);
2845 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2846 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_build_activate),
2847 GBO_TO_POINTER(GEANY_GBO_BUILD));
2848 widgets.toolitem_build = item;
2850 item = gtk_separator_menu_item_new();
2851 gtk_widget_show(item);
2852 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2854 /* build the code with make all */
2855 item = gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2856 gtk_widget_show(item);
2857 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2858 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2859 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL));
2860 widgets.toolitem_make_all = item;
2862 /* build the code with make custom */
2863 item = gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target..."));
2864 gtk_widget_show(item);
2865 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2866 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2867 GBO_TO_POINTER(GEANY_GBO_CUSTOM));
2868 widgets.toolitem_make_custom = item;
2870 /* build the code with make object */
2871 item = gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2872 gtk_widget_show(item);
2873 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2874 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2875 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT));
2876 widgets.toolitem_make_object = item;
2878 item = gtk_separator_menu_item_new();
2879 gtk_widget_show(item);
2880 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2882 /* arguments */
2883 item = ui_image_menu_item_new(GTK_STOCK_PREFERENCES, _("_Set Build Commands"));
2884 gtk_widget_show(item);
2885 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2886 g_signal_connect(item, "activate", G_CALLBACK(on_set_build_commands_activate), NULL);
2887 widgets.toolitem_set_args = item;
2889 /* get toolbar action pointers */
2890 widgets.build_action = toolbar_get_action_by_name("Build");
2891 widgets.compile_action = toolbar_get_action_by_name("Compile");
2892 widgets.run_action = toolbar_get_action_by_name("Run");
2893 widgets.toolmenu = toolmenu;
2894 /* set the submenu to the toolbar item */
2895 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets.build_action), toolmenu);