Fixup various filedefs mappings
[geany-mirror.git] / src / build.c
blobe9bed7650726b5f45dd48241f018ca2c9f671c9f
1 /*
2 * build.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 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"
65 /* g_spawn_async_with_pipes doesn't work on Windows */
66 #ifdef G_OS_WIN32
67 #define SYNC_SPAWN
68 #endif
70 /* Number of editor indicators to draw - limited as this can affect performance */
71 #define GEANY_BUILD_ERR_HIGHLIGHT_MAX 50
74 GeanyBuildInfo build_info = {GEANY_GBG_FT, 0, 0, NULL, GEANY_FILETYPES_NONE, NULL, 0};
76 static gchar *current_dir_entered = NULL;
78 typedef struct RunInfo
80 GPid pid;
81 gint file_type_id;
82 } RunInfo;
84 static RunInfo *run_info;
86 #ifdef G_OS_WIN32
87 static const gchar RUN_SCRIPT_CMD[] = "geany_run_script.bat";
88 #else
89 static const gchar RUN_SCRIPT_CMD[] = "./geany_run_script.sh";
90 #endif
92 /* pack group (<8) and command (<32) into a user_data pointer */
93 #define GRP_CMD_TO_POINTER(grp, cmd) GINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
94 #define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
95 #define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_INT(gptr)&0x1f)
96 #define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_INT(gptr)&0xe0) >> 5)
98 static gpointer last_toolbutton_action = GBO_TO_POINTER(GEANY_GBO_BUILD);
100 static BuildMenuItems menu_items = {NULL, {NULL, NULL, NULL, NULL}};
102 static struct
104 GtkAction *run_action;
105 GtkAction *compile_action;
106 GtkAction *build_action;
107 GtkWidget *toolmenu;
109 GtkWidget *toolitem_build;
110 GtkWidget *toolitem_make_all;
111 GtkWidget *toolitem_make_custom;
112 GtkWidget *toolitem_make_object;
113 GtkWidget *toolitem_set_args;
115 widgets;
117 static gint build_groups_count[GEANY_GBG_COUNT] = { 3, 4, 2 };
118 static gint build_items_count = 9;
120 #ifndef SYNC_SPAWN
121 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data);
122 static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data);
123 #endif
124 static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose);
125 static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir);
126 static void set_stop_button(gboolean stop);
127 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data);
128 static void on_set_build_commands_activate(GtkWidget *w, gpointer u);
129 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data);
130 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data);
131 static void kill_process(GPid *pid);
132 static void show_build_result_message(gboolean failure);
133 static void process_build_output_line(const gchar *line, gint color);
134 static void show_build_commands_dialog(void);
137 void build_finalize(void)
139 g_free(build_info.dir);
140 g_free(build_info.custom_target);
142 if (menu_items.menu != NULL && GTK_IS_WIDGET(menu_items.menu))
143 gtk_widget_destroy(menu_items.menu);
147 /* note: copied from keybindings.c, may be able to go away */
148 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id,
149 GtkAccelGroup *accel_group, GtkWidget *menuitem)
151 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
153 if (kb->key != 0)
154 gtk_widget_add_accelerator(menuitem, "activate", accel_group,
155 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
159 /* convenience routines to access parts of GeanyBuildCommand */
160 static gchar *id_to_str(GeanyBuildCommand *bc, gint id)
162 switch (id)
164 case GEANY_BC_LABEL:
165 return bc->label;
166 case GEANY_BC_COMMAND:
167 return bc->command;
168 case GEANY_BC_WORKING_DIR:
169 return bc->working_dir;
171 g_assert(0);
172 return NULL;
176 static void set_command(GeanyBuildCommand *bc, gint id, gchar *str)
178 switch (id)
180 case GEANY_BC_LABEL:
181 setptr(bc->label, str);
182 break;
183 case GEANY_BC_COMMAND:
184 setptr(bc->command, str);
185 break;
186 case GEANY_BC_WORKING_DIR:
187 setptr(bc->working_dir, str);
188 break;
189 default:
190 g_assert(0);
195 static const gchar *config_keys[GEANY_BC_CMDENTRIES_COUNT] = {
196 "LB", /* label */
197 "CM", /* command */
198 "WD" /* working directory */
201 /*-----------------------------------------------------
203 * Execute commands and handle results
205 *-----------------------------------------------------*/
207 /* the various groups of commands not in the filetype struct */
208 static GeanyBuildCommand *ft_def = NULL;
209 static GeanyBuildCommand *non_ft_proj = NULL;
210 static GeanyBuildCommand *non_ft_pref = NULL;
211 static GeanyBuildCommand *non_ft_def = NULL;
212 static GeanyBuildCommand *exec_proj = NULL;
213 static GeanyBuildCommand *exec_pref = NULL;
214 static GeanyBuildCommand *exec_def = NULL;
215 /* and the regexen not in the filetype structure */
216 static gchar *regex_pref = NULL;
217 /* project non-fileregex string */
218 static gchar *regex_proj = NULL;
220 /* control if build commands are printed by get_build_cmd, for debug purposes only*/
221 #ifndef PRINTBUILDCMDS
222 #define PRINTBUILDCMDS FALSE
223 #endif
224 static gboolean printbuildcmds = PRINTBUILDCMDS;
227 /* for debug only, print the commands structures in priority order */
228 static void printfcmds(void)
230 #if 0
231 GeanyBuildCommand **cl[GEANY_GBG_COUNT][GEANY_BCS_COUNT] = {
232 /* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
233 * GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
234 { &ft_def, NULL, NULL, NULL, NULL, NULL },
235 { &non_ft_def, NULL, NULL, &non_ft_pref, NULL, &non_ft_proj },
236 { &exec_def, NULL, NULL, &exec_pref, NULL, &exec_proj }
238 GeanyFiletype *ft = NULL;
239 GeanyDocument *doc;
240 gint i, j, k, l, m;
241 enum GeanyBuildCmdEntries n;
242 gint cc[GEANY_BCS_COUNT];
243 gchar c;
245 doc = document_get_current();
246 if (doc != NULL)
247 ft = doc->file_type;
248 if (ft != NULL)
250 printf("filetype %s\n",ft->name);
251 cl[GEANY_GBG_FT][GEANY_BCS_FT] = &(ft->filecmds);
252 cl[GEANY_GBG_FT][GEANY_BCS_HOME_FT] = &(ft->homefilecmds);
253 cl[GEANY_GBG_FT][GEANY_BCS_PROJ] = &(ft->projfilecmds);
254 cl[GEANY_GBG_NON_FT][GEANY_BCS_FT] = &(ft->ftdefcmds);
255 cl[GEANY_GBG_EXEC][GEANY_BCS_FT] = &(ft->execcmds);
256 cl[GEANY_GBG_EXEC][GEANY_BCS_HOME_FT] = &(ft->homeexeccmds);
257 cl[GEANY_GBG_EXEC][GEANY_BCS_PROJ_FT] = &(ft->projexeccmds);
259 for (i = 0; i < GEANY_BCS_COUNT; ++i)
261 m = 1;
262 for (j = 0; j < GEANY_GBG_COUNT; ++j)
264 for (k = 0; k < build_groups_count[j]; ++k)
265 if (cl[j][i] != NULL && *(cl[j][i]) != NULL && (*(cl[j][i]))[k].exists)
267 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
269 if ((*(cl[j][i]))[k].entries[n] != NULL &&
270 (l = strlen((*(cl[j][i]))[k].entries[n])) > m)
272 m = l;
277 cc[i] = m;
279 for (i = 0; i < GEANY_GBG_COUNT; ++i)
281 for (k = 0; k < build_groups_count[i]; ++k)
283 for (l = 0; l < 2; ++l)
285 c = ' ';
286 for (j = 0; j < GEANY_BCS_COUNT; ++j)
288 if (cl[i][j] != NULL && *(cl[i][j]) != NULL && (*(cl[i][j]))[k].exists)
290 for (n = 0; n < GEANY_BC_CMDENTRIES_COUNT; n++)
292 if ((*(cl[i][j]))[k].entries[i] != NULL)
293 printf("%c %*.*s",c,cc[j],cc[j],(*(cl[i][j]))[k].entries[i]);
294 else
295 printf("%c %*.*s",c,cc[j],cc[j]," ");
298 else
299 printf("%c %*.*s",c,cc[j],cc[j]," ");
300 c = ',';
302 printf("\n");
305 printf("\n");
307 #endif
311 /* macros to save typing and make the logic visible */
312 #define return_cmd_if(src, cmds)\
313 if (cmds != NULL && cmds[cmdindex].exists && below>src)\
315 *fr=src; \
316 if (printbuildcmds) \
317 printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
318 return &(cmds[cmdindex]); \
321 #define return_ft_cmd_if(src, cmds)\
322 if (ft != NULL && ft->cmds != NULL \
323 && ft->cmds[cmdindex].exists && below>src)\
325 *fr=src; \
326 if (printbuildcmds) \
327 printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
328 return &(ft->cmds[cmdindex]); \
332 /* get the next lowest command taking priority into account */
333 static GeanyBuildCommand *get_next_build_cmd(GeanyDocument *doc, gint cmdgrp, gint cmdindex,
334 gint below, gint *from)
336 /* Note: parameter below used in macros above */
337 GeanyFiletype *ft = NULL;
338 gint sink, *fr = &sink;
340 if (printbuildcmds)
341 printfcmds();
342 if (cmdgrp >= GEANY_GBG_COUNT)
343 return NULL;
344 if (from != NULL)
345 fr = from;
346 if (doc == NULL)
347 doc = document_get_current();
348 if (doc != NULL)
349 ft = doc->file_type;
351 switch (cmdgrp)
353 case GEANY_GBG_FT: /* order proj ft, home ft, ft, defft */
354 if (ft != NULL)
356 return_ft_cmd_if(GEANY_BCS_PROJ, projfilecmds);
357 return_ft_cmd_if(GEANY_BCS_PREF, homefilecmds);
358 return_ft_cmd_if(GEANY_BCS_FT, filecmds);
360 return_cmd_if(GEANY_BCS_DEF, ft_def);
361 break;
362 case GEANY_GBG_NON_FT: /* order proj, pref, def */
363 return_cmd_if(GEANY_BCS_PROJ, non_ft_proj);
364 return_cmd_if(GEANY_BCS_PREF, non_ft_pref);
365 return_ft_cmd_if(GEANY_BCS_FT, ftdefcmds);
366 return_cmd_if(GEANY_BCS_DEF, non_ft_def);
367 break;
368 case GEANY_GBG_EXEC: /* order proj, proj ft, pref, home ft, ft, def */
369 return_cmd_if(GEANY_BCS_PROJ, exec_proj);
370 return_ft_cmd_if(GEANY_BCS_PROJ_FT, projexeccmds);
371 return_cmd_if(GEANY_BCS_PREF, exec_pref);
372 return_ft_cmd_if(GEANY_BCS_FT, homeexeccmds);
373 return_ft_cmd_if(GEANY_BCS_FT, execcmds);
374 return_cmd_if(GEANY_BCS_DEF, exec_def);
375 break;
376 default:
377 break;
379 return NULL;
383 /* shortcut to start looking at the top */
384 static GeanyBuildCommand *get_build_cmd(GeanyDocument *doc, gint grp, gint cmdindex, gint *from)
386 return get_next_build_cmd(doc, grp, cmdindex, GEANY_BCS_COUNT, from);
390 #define return_nonblank_regex(src, ptr)\
391 if (NZV(ptr)) \
392 { *fr = (src); return &(ptr); }
395 /* like get_build_cmd, but for regexen, used by filetypes */
396 gchar **build_get_regex(GeanyBuildGroup grp, GeanyFiletype *ft, gint *from)
398 gint sink, *fr = &sink;
400 if (from != NULL)
401 fr = from;
402 if (grp == GEANY_GBG_FT)
404 if (ft == NULL)
406 GeanyDocument *doc = document_get_current();
407 if (doc != NULL)
408 ft = doc->file_type;
410 if (ft == NULL)
411 return NULL;
412 return_nonblank_regex(GEANY_BCS_PROJ, ft->projerror_regex_string);
413 return_nonblank_regex(GEANY_BCS_HOME_FT, ft->homeerror_regex_string);
414 return_nonblank_regex(GEANY_BCS_FT, ft->error_regex_string);
416 else if (grp == GEANY_GBG_NON_FT)
418 return_nonblank_regex(GEANY_BCS_PROJ, regex_proj);
419 return_nonblank_regex(GEANY_BCS_PREF, regex_pref);
421 return NULL;
425 /* get pointer to the command group array */
426 static GeanyBuildCommand *get_build_group(GeanyBuildSource src, GeanyBuildGroup grp)
428 GeanyDocument *doc;
429 GeanyFiletype *ft = NULL;
431 switch (grp)
433 case GEANY_GBG_FT:
434 if ((doc = document_get_current()) == NULL)
435 return NULL;
436 if ((ft = doc->file_type) == NULL)
437 return NULL;
439 switch (src)
441 case GEANY_BCS_DEF: return ft->ftdefcmds;
442 case GEANY_BCS_FT: return ft->filecmds;
443 case GEANY_BCS_HOME_FT: return ft->homefilecmds;
444 case GEANY_BCS_PREF: return ft->homefilecmds;
445 case GEANY_BCS_PROJ: return ft->projfilecmds;
446 default: return NULL;
448 break;
449 case GEANY_GBG_NON_FT:
450 switch (src)
452 case GEANY_BCS_DEF: return non_ft_def;
453 case GEANY_BCS_PREF: return non_ft_pref;
454 case GEANY_BCS_PROJ: return non_ft_proj;
455 default: return NULL;
457 break;
458 case GEANY_GBG_EXEC:
459 if ((doc = document_get_current()) != NULL)
460 ft = doc->file_type;
461 switch (src)
463 case GEANY_BCS_DEF: return exec_def;
464 case GEANY_BCS_FT: return ft ? ft->execcmds: NULL;
465 case GEANY_BCS_HOME_FT: return ft ? ft->homeexeccmds: NULL;
466 case GEANY_BCS_PROJ_FT: return ft ? ft->projexeccmds: NULL;
467 case GEANY_BCS_PREF: return exec_pref;
468 case GEANY_BCS_PROJ: return exec_proj;
469 default: return NULL;
471 break;
472 default:
473 return NULL;
478 /* * Remove the specified Build menu item.
480 * Makes the specified menu item configuration no longer exist. This
481 * is different to setting fields to blank because the menu item
482 * will be deleted from the configuration file on saving
483 * (except the system filetypes settings @see Build Menu Configuration
484 * section of the Manual).
486 * @param src the source of the menu item to remove.
487 * @param grp the group of the command to remove.
488 * @param cmd the index (from 0) of the command within the group. A negative
489 * value will remove the whole group.
491 * If any parameter is out of range does nothing.
493 * @see build_menu_update
495 void build_remove_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, gint cmd)
497 GeanyBuildCommand *bc;
498 gint i;
500 bc = get_build_group(src, grp);
501 if (bc == NULL)
502 return;
503 if (cmd < 0)
505 for (i = 0; i < build_groups_count[grp]; ++i)
506 bc[i].exists = FALSE;
508 else if (cmd < build_groups_count[grp])
509 bc[cmd].exists = FALSE;
513 /* * Get the @a GeanyBuildCommand structure for the specified Build menu item.
515 * Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
516 * hidden by higher priority commands.
518 * @param src the source of the specified menu item.
519 * @param grp the group of the specified menu item.
520 * @param cmd the index of the command within the group.
522 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
523 * This is a pointer to an internal structure and must not be freed.
525 * @see build_menu_update
527 GeanyBuildCommand *build_get_menu_item(GeanyBuildSource src, GeanyBuildGroup grp, gint cmd)
529 GeanyBuildCommand *bc;
531 if (src >= GEANY_BCS_COUNT || grp >= GEANY_GBG_COUNT || cmd >= build_groups_count[grp])
532 return NULL;
533 bc = get_build_group(src, grp);
534 if (bc == NULL)
535 return NULL;
536 return &(bc[cmd]);
540 /* * Get the @a GeanyBuildCommand structure for the menu item.
542 * Get the current highest priority command specified by @a grp and @a cmd. This is the one
543 * that the menu item will use if activated.
545 * @param grp the group of the specified menu item.
546 * @param cmd the index of the command within the group.
547 * @param src pointer to @a gint to return which source provided the command. Ignored if @a NULL.
548 * Values are one of @a GeanyBuildSource but returns a signed type not the enum.
550 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
551 * This is a pointer to an internal structure and must not be freed.
553 * @see build_menu_update
555 /* parameter checked version of get_build_cmd for external interface */
556 GeanyBuildCommand *build_get_current_menu_item(GeanyBuildGroup grp, gint cmd, gint *src)
558 if (*src >= GEANY_BCS_COUNT || grp >= GEANY_GBG_COUNT || cmd >= build_groups_count[grp])
559 return NULL;
560 return get_build_cmd(NULL, grp, cmd, src);
564 /* Clear all error indicators in all documents. */
565 static void clear_errors(GeanyDocument *doc)
567 guint i;
569 for (i = 0; i < documents_array->len; i++)
571 if (documents[i]->is_valid)
572 editor_indicator_clear_errors(documents[i]->editor);
577 #ifdef SYNC_SPAWN
578 static void parse_build_output(const gchar **output, gint status)
580 guint x, i, len;
581 gchar *line, **lines;
583 for (x = 0; x < 2; x++)
585 if (NZV(output[x]))
587 lines = g_strsplit_set(output[x], "\r\n", -1);
588 len = g_strv_length(lines);
590 for (i = 0; i < len; i++)
592 if (NZV(lines[i]))
594 line = lines[i];
595 while (*line != '\0')
596 { /* replace any control characters in the output */
597 if (*line < 32)
598 *line = 32;
599 line++;
601 process_build_output_line(lines[i], COLOR_BLACK);
604 g_strfreev(lines);
608 show_build_result_message(status != 0);
609 utils_beep();
611 build_info.pid = 0;
612 /* enable build items again */
613 build_menu_update(NULL);
615 #endif
618 /* Replaces occurences of %e and %p with the appropriate filenames,
619 * %d and %p replacements should be in UTF8 */
620 static gchar *build_replace_placeholder(const GeanyDocument *doc, const gchar *src)
622 GString *stack;
623 gchar *filename = NULL;
624 gchar *replacement;
625 gchar *executable = NULL;
626 gchar *ret_str; /* to be freed when not in use anymore */
628 stack = g_string_new(src);
629 if (doc != NULL && doc->file_name != NULL)
631 filename = utils_get_utf8_from_locale(doc->file_name);
633 /* replace %f with the filename (including extension) */
634 replacement = g_path_get_basename(filename);
635 utils_string_replace_all(stack, "%f", replacement);
636 g_free(replacement);
638 /* replace %d with the absolute path of the dir of the current file */
639 replacement = g_path_get_dirname(filename);
640 utils_string_replace_all(stack, "%d", replacement);
641 g_free(replacement);
643 /* replace %e with the filename (excluding extension) */
644 executable = utils_remove_ext_from_filename(filename);
645 replacement = g_path_get_basename(executable);
646 utils_string_replace_all(stack, "%e", replacement);
647 g_free(replacement);
650 /* replace %p with the current project's (absolute) base directory */
651 replacement = NULL; /* prevent double free if no replacement found */
652 if (app->project)
654 replacement = project_get_base_path();
656 else if (strstr(stack->str, "%p"))
657 { /* fall back to %d */
658 ui_set_statusbar(FALSE, _("failed to substitute %%p, no project active"));
659 if (doc != NULL && filename != NULL)
660 replacement = g_path_get_dirname(filename);
663 utils_string_replace_all(stack, "%p", replacement);
664 g_free(replacement);
666 ret_str = utils_get_utf8_from_locale(stack->str);
667 g_free(executable);
668 g_free(filename);
669 g_string_free(stack, TRUE);
671 return ret_str; /* don't forget to free src also if needed */
675 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
676 * idx document directory */
677 static GPid build_spawn_cmd(GeanyDocument *doc, const gchar *cmd, const gchar *dir)
679 GError *error = NULL;
680 gchar **argv;
681 gchar *working_dir;
682 gchar *utf8_working_dir;
683 gchar *cmd_string;
684 gchar *utf8_cmd_string;
685 #ifdef SYNC_SPAWN
686 gchar *output[2];
687 gint status;
688 #else
689 gint stdout_fd;
690 gint stderr_fd;
691 #endif
693 if (!((doc != NULL && NZV(doc->file_name)) || NZV(dir)))
695 geany_debug("Failed to run command with no working directory");
696 ui_set_statusbar(TRUE, _("Process failed, no working directory"));
697 return (GPid) 1;
700 if (doc != NULL)
701 clear_errors(doc);
702 setptr(current_dir_entered, NULL);
704 cmd_string = g_strdup(cmd);
706 #ifdef G_OS_WIN32
707 argv = g_strsplit(cmd_string, " ", 0);
708 #else
709 argv = g_new0(gchar *, 4);
710 argv[0] = g_strdup("/bin/sh");
711 argv[1] = g_strdup("-c");
712 argv[2] = cmd_string;
713 argv[3] = NULL;
714 #endif
716 utf8_cmd_string = utils_get_utf8_from_locale(cmd_string);
717 utf8_working_dir = NZV(dir) ? g_strdup(dir) : g_path_get_dirname(doc->file_name);
718 working_dir = utils_get_locale_from_utf8(utf8_working_dir);
720 gtk_list_store_clear(msgwindow.store_compiler);
721 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
722 msgwin_compiler_add(COLOR_BLUE, _("%s (in directory: %s)"), utf8_cmd_string, utf8_working_dir);
723 g_free(utf8_working_dir);
724 g_free(utf8_cmd_string);
726 /* set the build info for the message window */
727 g_free(build_info.dir);
728 build_info.dir = g_strdup(working_dir);
729 build_info.file_type_id = (doc == NULL) ? GEANY_FILETYPES_NONE : doc->file_type->id;
730 build_info.message_count = 0;
732 #ifdef SYNC_SPAWN
733 if (! utils_spawn_sync(working_dir, argv, NULL, G_SPAWN_SEARCH_PATH,
734 NULL, NULL, &output[0], &output[1], &status, &error))
735 #else
736 if (! g_spawn_async_with_pipes(working_dir, argv, NULL,
737 G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL,
738 &(build_info.pid), NULL, &stdout_fd, &stderr_fd, &error))
739 #endif
741 geany_debug("g_spawn_async_with_pipes() failed: %s", error->message);
742 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
743 g_strfreev(argv);
744 g_error_free(error);
745 g_free(working_dir);
746 error = NULL;
747 return (GPid) 0;
750 #ifdef SYNC_SPAWN
751 parse_build_output((const gchar**) output, status);
752 g_free(output[0]);
753 g_free(output[1]);
754 #else
755 if (build_info.pid > 0)
757 g_child_watch_add(build_info.pid, (GChildWatchFunc) build_exit_cb, NULL);
758 build_menu_update(doc);
759 ui_progress_bar_start(NULL);
762 /* use GIOChannels to monitor stdout and stderr */
763 utils_set_up_io_channel(stdout_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
764 TRUE, build_iofunc, GINT_TO_POINTER(0));
765 utils_set_up_io_channel(stderr_fd, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
766 TRUE, build_iofunc, GINT_TO_POINTER(1));
767 #endif
769 g_strfreev(argv);
770 g_free(working_dir);
772 return build_info.pid;
776 /* Returns: NULL if there was an error, or the working directory the script was created in.
777 * vte_cmd_nonscript is the location of a string which is filled with the command to be used
778 * when vc->skip_run_script is set, otherwise it will be set to NULL */
779 static gchar *prepare_run_script(GeanyDocument *doc, gchar **vte_cmd_nonscript, gint cmdindex)
781 gchar *locale_filename = NULL;
782 GeanyBuildCommand *cmd = NULL;
783 gchar *executable = NULL;
784 gchar *working_dir = NULL;
785 const gchar *cmd_working_dir;
786 gboolean autoclose = FALSE;
787 gboolean result = FALSE;
788 gchar *tmp;
789 gchar *cmd_string;
791 if (vte_cmd_nonscript != NULL)
792 *vte_cmd_nonscript = NULL;
794 locale_filename = utils_get_locale_from_utf8(doc->file_name);
796 cmd = get_build_cmd(doc, GEANY_GBG_EXEC, cmdindex, NULL);
798 cmd_string = build_replace_placeholder(doc, cmd->command);
799 cmd_working_dir = cmd->working_dir;
800 if (! NZV(cmd_working_dir))
801 cmd_working_dir = "%d";
802 working_dir = build_replace_placeholder(doc, cmd_working_dir); /* in utf-8 */
804 /* only test whether working dir exists, don't change it or else Windows support will break
805 * (gspawn-win32-helper.exe is used by GLib and must be in $PATH which means current working
806 * dir where geany.exe was started from, so we can't change it) */
807 if (!NZV(working_dir) || ! g_file_test(working_dir, G_FILE_TEST_EXISTS) ||
808 ! g_file_test(working_dir, G_FILE_TEST_IS_DIR))
810 ui_set_statusbar(TRUE, _("Failed to change the working directory to \"%s\""),
811 NZV(working_dir) ? working_dir : "<NULL>" );
812 utils_free_pointers(2, cmd_string, working_dir, NULL);
813 return NULL;
816 #ifdef HAVE_VTE
817 if (vte_info.have_vte && vc->run_in_vte)
819 if (vc->skip_run_script)
821 if (vte_cmd_nonscript != NULL)
822 *vte_cmd_nonscript = cmd_string;
824 utils_free_pointers(2, executable, locale_filename, NULL);
825 return working_dir;
827 else
828 /* don't wait for user input at the end of script when we are running in VTE */
829 autoclose = TRUE;
831 #endif
833 /* RUN_SCRIPT_CMD should be ok in UTF8 without converting in locale because it
834 * contains no umlauts */
835 tmp = g_build_filename(working_dir, RUN_SCRIPT_CMD, NULL);
836 result = build_create_shellscript(tmp, cmd_string, autoclose);
837 if (! result)
839 ui_set_statusbar(TRUE, _("Failed to execute \"%s\" (start-script could not be created)"),
840 NZV(cmd_string) ? cmd_string : NULL);
843 utils_free_pointers(4, cmd_string, tmp, executable, locale_filename, NULL);
845 if (result)
846 return working_dir;
848 g_free(working_dir);
849 return NULL;
853 static GPid build_run_cmd(GeanyDocument *doc, gint cmdindex)
855 gchar *working_dir;
856 gchar *vte_cmd_nonscript = NULL;
857 GError *error = NULL;
859 if (doc == NULL || doc->file_name == NULL)
860 return (GPid) 0;
862 working_dir = prepare_run_script(doc, &vte_cmd_nonscript, cmdindex);
863 if (working_dir == NULL)
864 return (GPid) 0;
866 run_info[cmdindex].file_type_id = doc->file_type->id;
868 #ifdef HAVE_VTE
869 if (vte_info.have_vte && vc->run_in_vte)
871 gchar *vte_cmd;
873 if (vc->skip_run_script)
875 setptr(vte_cmd_nonscript, utils_get_utf8_from_locale(vte_cmd_nonscript));
876 vte_cmd = g_strconcat(vte_cmd_nonscript, "\n", NULL);
877 g_free(vte_cmd_nonscript);
879 else
880 vte_cmd = g_strconcat("\n/bin/sh ", RUN_SCRIPT_CMD, "\n", NULL);
882 /* change into current directory if it is not done by default */
883 if (! vc->follow_path)
885 /* we need to convert the working_dir back to UTF-8 because the VTE expects it */
886 gchar *utf8_working_dir = utils_get_utf8_from_locale(working_dir);
887 vte_cwd(utf8_working_dir, TRUE);
888 g_free(utf8_working_dir);
890 if (! vte_send_cmd(vte_cmd))
892 ui_set_statusbar(FALSE,
893 _("Could not execute the file in the VTE because it probably contains a command."));
894 geany_debug("Could not execute the file in the VTE because it probably contains a command.");
897 /* show the VTE */
898 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_VTE);
899 gtk_widget_grab_focus(vc->vte);
900 msgwin_show_hide(TRUE);
902 run_info[cmdindex].pid = 1;
904 g_free(vte_cmd);
906 else
907 #endif
909 gchar *locale_term_cmd = NULL;
910 gchar **term_argv = NULL;
911 guint term_argv_len, i;
912 gchar **argv = NULL;
914 /* get the terminal path */
915 locale_term_cmd = utils_get_locale_from_utf8(tool_prefs.term_cmd);
916 /* split the term_cmd, so arguments will work too */
917 term_argv = g_strsplit(locale_term_cmd, " ", -1);
918 term_argv_len = g_strv_length(term_argv);
920 /* check that terminal exists (to prevent misleading error messages) */
921 if (term_argv[0] != NULL)
923 gchar *tmp = term_argv[0];
924 /* g_find_program_in_path checks whether tmp exists and is executable */
925 term_argv[0] = g_find_program_in_path(tmp);
926 g_free(tmp);
928 if (term_argv[0] == NULL)
930 ui_set_statusbar(TRUE,
931 _("Could not find terminal \"%s\" "
932 "(check path for Terminal tool setting in Preferences)"), tool_prefs.term_cmd);
933 run_info[cmdindex].pid = (GPid) 1;
934 goto free_strings;
937 argv = g_new0(gchar *, term_argv_len + 3);
938 for (i = 0; i < term_argv_len; i++)
940 argv[i] = g_strdup(term_argv[i]);
942 #ifdef G_OS_WIN32
943 /* command line arguments only for cmd.exe */
944 if (strstr(argv[0], "cmd.exe") != NULL)
946 argv[term_argv_len] = g_strdup("/Q /C");
947 argv[term_argv_len + 1] = g_strdup(RUN_SCRIPT_CMD);
949 else
951 argv[term_argv_len] = g_strdup(RUN_SCRIPT_CMD);
952 argv[term_argv_len + 1] = NULL;
954 #else
955 argv[term_argv_len ] = g_strdup("-e");
956 argv[term_argv_len + 1] = g_strconcat("/bin/sh ", RUN_SCRIPT_CMD, NULL);
957 #endif
958 argv[term_argv_len + 2] = NULL;
960 if (! g_spawn_async(working_dir, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD,
961 NULL, NULL, &(run_info[cmdindex].pid), &error))
963 geany_debug("g_spawn_async() failed: %s", error->message);
964 ui_set_statusbar(TRUE, _("Process failed (%s)"), error->message);
965 g_unlink(RUN_SCRIPT_CMD);
966 g_error_free(error);
967 error = NULL;
968 run_info[cmdindex].pid = (GPid) 0;
971 if (run_info[cmdindex].pid > 0)
973 g_child_watch_add(run_info[cmdindex].pid, (GChildWatchFunc) run_exit_cb,
974 (gpointer)&(run_info[cmdindex]));
975 build_menu_update(doc);
977 free_strings:
978 g_strfreev(argv);
979 g_strfreev(term_argv);
980 g_free(locale_term_cmd);
983 g_free(working_dir);
984 return run_info[cmdindex].pid;
988 static void process_build_output_line(const gchar *str, gint color)
990 gchar *msg, *tmp;
991 gchar *filename;
992 gint line;
994 msg = g_strdup(str);
996 g_strchomp(msg);
998 if (! NZV(msg))
1000 g_free(msg);
1001 return;
1004 if (build_parse_make_dir(msg, &tmp))
1006 setptr(current_dir_entered, tmp);
1008 msgwin_parse_compiler_error_line(msg, current_dir_entered, &filename, &line);
1010 if (line != -1 && filename != NULL)
1012 GeanyDocument *doc = document_find_by_filename(filename);
1014 /* limit number of indicators */
1015 if (doc && editor_prefs.use_indicators &&
1016 build_info.message_count < GEANY_BUILD_ERR_HIGHLIGHT_MAX)
1018 if (line > 0) /* some compilers, like pdflatex report errors on line 0 */
1019 line--; /* so only adjust the line number if it is greater than 0 */
1020 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line);
1022 build_info.message_count++;
1023 color = COLOR_RED; /* error message parsed on the line */
1025 g_free(filename);
1027 msgwin_compiler_add_string(color, msg);
1028 g_free(msg);
1032 #ifndef SYNC_SPAWN
1033 static gboolean build_iofunc(GIOChannel *ioc, GIOCondition cond, gpointer data)
1035 if (cond & (G_IO_IN | G_IO_PRI))
1037 gchar *msg;
1038 GIOStatus st;
1040 while ((st = g_io_channel_read_line(ioc, &msg, NULL, NULL, NULL)) == G_IO_STATUS_NORMAL && msg)
1042 gint color = (GPOINTER_TO_INT(data)) ? COLOR_DARK_RED : COLOR_BLACK;
1044 process_build_output_line(msg, color);
1045 g_free(msg);
1047 if (st == G_IO_STATUS_ERROR || st == G_IO_STATUS_EOF) return FALSE;
1049 if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
1050 return FALSE;
1052 return TRUE;
1054 #endif
1057 gboolean build_parse_make_dir(const gchar *string, gchar **prefix)
1059 const gchar *pos;
1061 *prefix = NULL;
1063 if (string == NULL)
1064 return FALSE;
1066 if ((pos = strstr(string, "Entering directory")) != NULL)
1068 gsize len;
1069 gchar *input;
1071 /* get the start of the path */
1072 pos = strstr(string, "/");
1074 if (pos == NULL)
1075 return FALSE;
1077 input = g_strdup(pos);
1079 /* kill the ' at the end of the path */
1080 len = strlen(input);
1081 input[len - 1] = '\0';
1082 input = g_realloc(input, len); /* shorten by 1 */
1083 *prefix = input;
1085 return TRUE;
1088 if (strstr(string, "Leaving directory") != NULL)
1090 *prefix = NULL;
1091 return TRUE;
1094 return FALSE;
1098 static void show_build_result_message(gboolean failure)
1100 gchar *msg;
1102 if (failure)
1104 msg = _("Compilation failed.");
1105 msgwin_compiler_add_string(COLOR_BLUE, msg);
1106 /* If msgwindow is hidden, user will want to display it to see the error */
1107 if (! ui_prefs.msgwindow_visible)
1109 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1110 msgwin_show_hide(TRUE);
1112 else
1113 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1114 ui_set_statusbar(FALSE, "%s", msg);
1116 else
1118 msg = _("Compilation finished successfully.");
1119 msgwin_compiler_add_string(COLOR_BLUE, msg);
1120 if (! ui_prefs.msgwindow_visible ||
1121 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook)) != MSG_COMPILER)
1122 ui_set_statusbar(FALSE, "%s", msg);
1127 #ifndef SYNC_SPAWN
1128 static void build_exit_cb(GPid child_pid, gint status, gpointer user_data)
1130 gboolean failure = FALSE;
1132 #ifdef G_OS_WIN32
1133 failure = status;
1134 #else
1135 if (WIFEXITED(status))
1137 if (WEXITSTATUS(status) != EXIT_SUCCESS)
1138 failure = TRUE;
1140 else if (WIFSIGNALED(status))
1142 /* the terminating signal: WTERMSIG (status)); */
1143 failure = TRUE;
1145 else
1146 { /* any other failure occured */
1147 failure = TRUE;
1149 #endif
1150 show_build_result_message(failure);
1152 utils_beep();
1153 g_spawn_close_pid(child_pid);
1155 build_info.pid = 0;
1156 /* enable build items again */
1157 build_menu_update(NULL);
1158 ui_progress_bar_stop();
1160 #endif
1163 static void run_exit_cb(GPid child_pid, gint status, gpointer user_data)
1165 RunInfo *run_info_data = user_data;
1167 g_spawn_close_pid(child_pid);
1169 run_info_data->pid = 0;
1170 /* reset the stop button and menu item to the original meaning */
1171 build_menu_update(NULL);
1175 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1176 * fname is the full file name (including path) for the script to create */
1177 static gboolean build_create_shellscript(const gchar *fname, const gchar *cmd, gboolean autoclose)
1179 FILE *fp;
1180 gchar *str;
1181 #ifdef G_OS_WIN32
1182 gchar *expanded_cmd;
1183 #endif
1185 fp = g_fopen(fname, "w");
1186 if (! fp)
1187 return FALSE;
1188 #ifdef G_OS_WIN32
1189 /* Expand environment variables like %blah%. */
1190 expanded_cmd = win32_expand_environment_variables(cmd);
1191 str = g_strdup_printf("%s\n\n%s\ndel \"%%0\"\n\npause\n", expanded_cmd, (autoclose) ? "" : "pause");
1192 g_free(expanded_cmd);
1193 #else
1194 str = g_strdup_printf(
1195 "#!/bin/sh\n\nrm $0\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1196 \n\n%s\n", cmd, (autoclose) ? "" :
1197 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1198 "dash\ndummy_var=\"\"\nread dummy_var");
1199 #endif
1201 fputs(str, fp);
1202 g_free(str);
1204 fclose(fp);
1206 return TRUE;
1210 typedef void Callback(GtkWidget *w, gpointer u);
1212 /* run the command catenating cmd_cat if present */
1213 static void build_command(GeanyDocument *doc, GeanyBuildGroup grp, gint cmd, gchar *cmd_cat)
1215 gchar *dir;
1216 gchar *full_command, *subs_command;
1217 GeanyBuildCommand *buildcmd = get_build_cmd(doc, grp, cmd, NULL);
1218 gchar *cmdstr;
1220 if (buildcmd == NULL)
1221 return;
1223 cmdstr = buildcmd->command;
1225 if (cmd_cat != NULL)
1227 if (cmdstr != NULL)
1228 full_command = g_strconcat(cmdstr, cmd_cat, NULL);
1229 else
1230 full_command = g_strdup(cmd_cat);
1232 else
1233 full_command = cmdstr;
1235 dir = build_replace_placeholder(doc, buildcmd->working_dir);
1236 subs_command = build_replace_placeholder(doc, full_command);
1237 build_info.grp = grp;
1238 build_info.cmd = cmd;
1239 build_spawn_cmd(doc, subs_command, dir);
1240 g_free(subs_command);
1241 g_free(dir);
1242 if (cmd_cat != NULL)
1243 g_free(full_command);
1244 build_menu_update(doc);
1249 /*----------------------------------------------------------------
1251 * Create build menu and handle callbacks (&toolbar callbacks)
1253 *----------------------------------------------------------------*/
1254 static void on_make_custom_input_response(const gchar *input)
1256 GeanyDocument *doc = document_get_current();
1258 setptr(build_info.custom_target, g_strdup(input));
1259 build_command(doc, GBO_TO_GBG(GEANY_GBO_CUSTOM), GBO_TO_CMD(GEANY_GBO_CUSTOM),
1260 build_info.custom_target);
1264 static void on_build_menu_item(GtkWidget *w, gpointer user_data)
1266 GeanyDocument *doc = document_get_current();
1267 GeanyBuildCommand *bc;
1268 gint grp = GPOINTER_TO_GRP(user_data);
1269 gint cmd = GPOINTER_TO_CMD(user_data);
1271 if (doc && doc->changed)
1273 if (document_need_save_as(doc) && !dialogs_show_save_as())
1274 return;
1276 if (!document_save_file(doc, FALSE))
1277 return;
1279 g_signal_emit_by_name(geany_object, "build-start");
1281 if (grp == GEANY_GBG_NON_FT && cmd == GBO_TO_CMD(GEANY_GBO_CUSTOM))
1283 static GtkWidget *dialog = NULL; /* keep dialog for combo history */
1285 if (! dialog)
1287 dialog = dialogs_show_input_persistent(_("Custom Text"), GTK_WINDOW(main_widgets.window),
1288 _("Enter custom text here, all entered text is appended to the command."),
1289 build_info.custom_target, &on_make_custom_input_response);
1291 else
1293 gtk_widget_show(dialog);
1295 return;
1297 else if (grp == GEANY_GBG_EXEC)
1299 if (run_info[cmd].pid > (GPid) 1)
1301 kill_process(&run_info[cmd].pid);
1302 return;
1304 bc = get_build_cmd(doc, grp, cmd, NULL);
1305 if (bc != NULL && strcmp(bc->command, "builtin") == 0)
1307 gchar *uri;
1308 if (doc == NULL)
1309 return;
1310 uri = g_strconcat("file:///", g_path_skip_root(doc->file_name), NULL);
1311 utils_open_browser(uri);
1312 g_free(uri);
1315 else
1316 build_run_cmd(doc, cmd);
1318 else
1319 build_command(doc, grp, cmd, NULL);
1323 /* group codes for menu items other than the known commands
1324 * value order is important, see the following table for use */
1326 /* the rest in each group */
1327 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1328 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1329 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1330 /* the separator */
1331 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1332 /* the fixed items */
1333 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1334 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1335 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1336 #define MENU_DONE (MENU_COMMANDS + 1)
1339 static struct BuildMenuItemSpec {
1340 const gchar *stock_id;
1341 const gint key_binding;
1342 const gint build_grp;
1343 const gint build_cmd;
1344 const gchar *fix_label;
1345 Callback *cb;
1346 } build_menu_specs[] = {
1347 {GTK_STOCK_CONVERT, GEANY_KEYS_BUILD_COMPILE, GBO_TO_GBG(GEANY_GBO_COMPILE),
1348 GBO_TO_CMD(GEANY_GBO_COMPILE), NULL, on_build_menu_item},
1349 {GEANY_STOCK_BUILD, GEANY_KEYS_BUILD_LINK, GBO_TO_GBG(GEANY_GBO_BUILD),
1350 GBO_TO_CMD(GEANY_GBO_BUILD), NULL, on_build_menu_item},
1351 {NULL, -1, MENU_FT_REST,
1352 GBO_TO_CMD(GEANY_GBO_BUILD) + 1, NULL, on_build_menu_item},
1353 {NULL, -1, MENU_SEPARATOR,
1354 GBF_SEP_1, NULL, NULL},
1355 {NULL, GEANY_KEYS_BUILD_MAKE, GBO_TO_GBG(GEANY_GBO_MAKE_ALL),
1356 GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL, on_build_menu_item},
1357 {NULL, GEANY_KEYS_BUILD_MAKEOWNTARGET, GBO_TO_GBG(GEANY_GBO_CUSTOM),
1358 GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL, on_build_menu_item},
1359 {NULL, GEANY_KEYS_BUILD_MAKEOBJECT, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT),
1360 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL, on_build_menu_item},
1361 {NULL, -1, MENU_NON_FT_REST,
1362 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT) + 1, NULL, on_build_menu_item},
1363 {NULL, -1, MENU_SEPARATOR,
1364 GBF_SEP_2, NULL, NULL},
1365 {GTK_STOCK_GO_DOWN, GEANY_KEYS_BUILD_NEXTERROR, MENU_NEXT_ERROR,
1366 GBF_NEXT_ERROR, N_("_Next Error"), on_build_next_error},
1367 {GTK_STOCK_GO_UP, GEANY_KEYS_BUILD_PREVIOUSERROR, MENU_PREV_ERROR,
1368 GBF_PREV_ERROR, N_("_Previous Error"), on_build_previous_error},
1369 {NULL, -1, MENU_SEPARATOR,
1370 GBF_SEP_3, NULL, NULL},
1371 {GTK_STOCK_EXECUTE, GEANY_KEYS_BUILD_RUN, GBO_TO_GBG(GEANY_GBO_EXEC),
1372 GBO_TO_CMD(GEANY_GBO_EXEC), NULL, on_build_menu_item},
1373 {NULL, -1, MENU_EXEC_REST,
1374 GBO_TO_CMD(GEANY_GBO_EXEC) + 1, NULL, on_build_menu_item},
1375 {NULL, -1, MENU_SEPARATOR,
1376 GBF_SEP_4, NULL, NULL},
1377 {GTK_STOCK_PREFERENCES, GEANY_KEYS_BUILD_OPTIONS, MENU_COMMANDS,
1378 GBF_COMMANDS, N_("_Set Build Commands"), on_set_build_commands_activate},
1379 {NULL, -1, MENU_DONE,
1380 0, NULL, NULL}
1384 static void create_build_menu_item(GtkWidget *menu, GeanyKeyGroup *group, GtkAccelGroup *ag,
1385 struct BuildMenuItemSpec *bs, const gchar *lbl, gint grp, gint cmd)
1387 GtkWidget *item = gtk_image_menu_item_new_with_mnemonic(lbl);
1389 if (bs->stock_id != NULL)
1391 GtkWidget *image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1392 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item), image);
1394 gtk_widget_show(item);
1395 if (bs->key_binding >= 0)
1396 add_menu_accel(group, bs->key_binding, ag, item);
1397 gtk_container_add(GTK_CONTAINER(menu), item);
1398 if (bs->cb != NULL)
1400 g_signal_connect(item, "activate", G_CALLBACK(bs->cb), GRP_CMD_TO_POINTER(grp,cmd));
1402 menu_items.menu_item[grp][cmd] = item;
1406 static void create_build_menu(BuildMenuItems *build_menu_items)
1408 GtkWidget *menu;
1409 GtkAccelGroup *accel_group = gtk_accel_group_new();
1410 GeanyKeyGroup *keygroup = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
1411 gint i, j;
1413 menu = gtk_menu_new();
1414 build_menu_items->menu_item[GEANY_GBG_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_FT]);
1415 build_menu_items->menu_item[GEANY_GBG_NON_FT] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_NON_FT]);
1416 build_menu_items->menu_item[GEANY_GBG_EXEC] = g_new0(GtkWidget*, build_groups_count[GEANY_GBG_EXEC]);
1417 build_menu_items->menu_item[GBG_FIXED] = g_new0(GtkWidget*, GBF_COUNT);
1419 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1421 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1422 if (bs->build_grp == MENU_SEPARATOR)
1424 GtkWidget *item = gtk_separator_menu_item_new();
1425 gtk_widget_show(item);
1426 gtk_container_add(GTK_CONTAINER(menu), item);
1427 build_menu_items->menu_item[GBG_FIXED][bs->build_cmd] = item;
1429 else if (bs->fix_label != NULL)
1431 create_build_menu_item(menu, keygroup, accel_group, bs, _(bs->fix_label),
1432 GBG_FIXED, bs->build_cmd);
1434 else if (bs->build_grp >= MENU_FT_REST && bs->build_grp <= MENU_SEPARATOR)
1436 gint grp = bs->build_grp - GEANY_GBG_COUNT;
1437 for (j = bs->build_cmd; j < build_groups_count[grp]; ++j)
1439 GeanyBuildCommand *bc = get_build_cmd(NULL, grp, j, NULL);
1440 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1441 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, grp, j);
1444 else
1446 GeanyBuildCommand *bc = get_build_cmd(NULL, bs->build_grp, bs->build_cmd, NULL);
1447 const gchar *lbl = (bc == NULL) ? "" : bc->label;
1448 create_build_menu_item(menu, keygroup, accel_group, bs, lbl, bs->build_grp, bs->build_cmd);
1451 build_menu_items->menu = menu;
1452 gtk_widget_show(menu);
1453 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_build1")), menu);
1457 /* portability to various GTK versions needs checking
1458 * conforms to description of gtk_accel_label as child of menu item
1459 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1460 static void geany_menu_item_set_label(GtkWidget *w, const gchar *label)
1462 GtkWidget *c = gtk_bin_get_child(GTK_BIN(w));
1464 gtk_label_set_text_with_mnemonic(GTK_LABEL(c), label);
1468 /* * Update the build menu to reflect changes in configuration or status.
1470 * Sets the labels and number of visible items to match the highest
1471 * priority configured commands. Also sets sensitivity if build commands are
1472 * running and switches executes to stop when commands are running.
1474 * @param doc The current document, if available, to save looking it up.
1475 * If @c NULL it will be looked up.
1477 * Call this after modifying any fields of a GeanyBuildCommand structure.
1479 * @see Build Menu Configuration section of the Manual.
1482 void build_menu_update(GeanyDocument *doc)
1484 gint i, cmdcount, cmd, grp;
1485 gboolean vis = FALSE;
1486 gboolean have_path, build_running, exec_running, have_errors, cmd_sensitivity;
1487 gboolean can_compile, can_build, can_make, run_sensitivity = FALSE, run_running = FALSE;
1488 GeanyBuildCommand *bc;
1490 if (menu_items.menu == NULL)
1491 create_build_menu(&menu_items);
1492 if (doc == NULL)
1493 doc = document_get_current();
1494 have_path = doc != NULL && doc->file_name != NULL;
1495 build_running = build_info.pid > (GPid) 1;
1496 have_errors = gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow.store_compiler), NULL) > 0;
1497 for (i = 0; build_menu_specs[i].build_grp != MENU_DONE; ++i)
1499 struct BuildMenuItemSpec *bs = &(build_menu_specs[i]);
1500 switch (bs->build_grp)
1502 case MENU_SEPARATOR:
1503 if (vis == TRUE)
1505 gtk_widget_show_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1506 vis = FALSE;
1508 else
1509 gtk_widget_hide_all(menu_items.menu_item[GBG_FIXED][bs->build_cmd]);
1510 break;
1511 case MENU_NEXT_ERROR:
1512 case MENU_PREV_ERROR:
1513 gtk_widget_set_sensitive(menu_items.menu_item[GBG_FIXED][bs->build_cmd], have_errors);
1514 vis |= TRUE;
1515 break;
1516 case MENU_COMMANDS:
1517 vis |= TRUE;
1518 break;
1519 default: /* all configurable commands */
1520 if (bs->build_grp >= GEANY_GBG_COUNT)
1522 grp = bs->build_grp - GEANY_GBG_COUNT;
1523 cmdcount = build_groups_count[grp];
1525 else
1527 grp = bs->build_grp;
1528 cmdcount = bs->build_cmd + 1;
1530 for (cmd = bs->build_cmd; cmd < cmdcount; ++cmd)
1532 GtkWidget *menu_item = menu_items.menu_item[grp][cmd];
1533 const gchar *label;
1534 bc = get_build_cmd(doc, grp, cmd, NULL);
1535 if (bc)
1536 label = bc->label;
1537 else
1538 label = NULL;
1540 if (grp < GEANY_GBG_EXEC)
1542 cmd_sensitivity =
1543 (grp == GEANY_GBG_FT && bc != NULL && have_path && ! build_running) ||
1544 (grp == GEANY_GBG_NON_FT && bc != NULL && ! build_running);
1545 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1546 if (bc != NULL && NZV(label))
1548 geany_menu_item_set_label(menu_item, label);
1549 gtk_widget_show_all(menu_item);
1550 vis |= TRUE;
1552 else
1553 gtk_widget_hide_all(menu_item);
1555 else
1557 GtkWidget *image;
1558 exec_running = run_info[cmd].pid > (GPid) 1;
1559 cmd_sensitivity = (bc != NULL) || exec_running;
1560 gtk_widget_set_sensitive(menu_item, cmd_sensitivity);
1561 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1562 run_sensitivity = cmd_sensitivity;
1563 if (! exec_running)
1565 image = gtk_image_new_from_stock(bs->stock_id, GTK_ICON_SIZE_MENU);
1567 else
1569 image = gtk_image_new_from_stock(GTK_STOCK_STOP, GTK_ICON_SIZE_MENU);
1571 if (cmd == GBO_TO_CMD(GEANY_GBO_EXEC))
1572 run_running = exec_running;
1573 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), image);
1574 if (bc != NULL && NZV(label))
1576 geany_menu_item_set_label(menu_item, label);
1577 gtk_widget_show_all(menu_item);
1578 vis |= TRUE;
1580 else
1581 gtk_widget_hide_all(menu_item);
1587 run_sensitivity &= (doc != NULL);
1588 can_build = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_BUILD), NULL) != NULL
1589 && have_path && ! build_running;
1590 if (widgets.toolitem_build != NULL)
1591 gtk_widget_set_sensitive(widgets.toolitem_build, can_build);
1592 can_make = FALSE;
1593 if (widgets.toolitem_make_all != NULL)
1594 gtk_widget_set_sensitive(widgets.toolitem_make_all,
1595 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_ALL), NULL) != NULL
1596 && ! build_running));
1597 if (widgets.toolitem_make_custom != NULL)
1598 gtk_widget_set_sensitive(widgets.toolitem_make_custom,
1599 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_CUSTOM), NULL) != NULL
1600 && ! build_running));
1601 if (widgets.toolitem_make_object != NULL)
1602 gtk_widget_set_sensitive(widgets.toolitem_make_object,
1603 (can_make |= get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT), NULL) != NULL
1604 && ! build_running));
1605 if (widgets.toolitem_set_args != NULL)
1606 gtk_widget_set_sensitive(widgets.toolitem_set_args, TRUE);
1608 can_compile = get_build_cmd(doc, GEANY_GBG_FT, GBO_TO_CMD(GEANY_GBO_COMPILE), NULL) != NULL
1609 && have_path && ! build_running;
1610 gtk_action_set_sensitive(widgets.compile_action, can_compile);
1611 gtk_action_set_sensitive(widgets.build_action, can_make);
1612 gtk_action_set_sensitive(widgets.run_action, run_sensitivity);
1614 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1615 set_stop_button(run_running);
1620 /* Call build_menu_update() instead of calling this directly. */
1621 static void set_stop_button(gboolean stop)
1623 const gchar *button_stock_id = NULL;
1624 GtkToolButton *run_button;
1626 run_button = GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1627 if (run_button != NULL)
1628 button_stock_id = gtk_tool_button_get_stock_id(run_button);
1630 if (stop && utils_str_equal(button_stock_id, "gtk-stop"))
1631 return;
1632 if (! stop && utils_str_equal(button_stock_id, "gtk-execute"))
1633 return;
1635 /* use the run button also as stop button */
1636 if (stop)
1638 if (run_button != NULL)
1639 gtk_tool_button_set_stock_id(run_button, "gtk-stop");
1641 else
1643 if (run_button != NULL)
1644 gtk_tool_button_set_stock_id(run_button, "gtk-execute");
1649 static void on_set_build_commands_activate(GtkWidget *w, gpointer u)
1651 /* For now, just show the project dialog */
1652 if (app->project)
1653 project_build_properties();
1654 else
1655 show_build_commands_dialog();
1659 static void on_toolbutton_build_activate(GtkWidget *menuitem, gpointer user_data)
1661 last_toolbutton_action = user_data;
1662 g_object_set(widgets.build_action, "tooltip", _("Build the current file"), NULL);
1663 on_build_menu_item(menuitem, user_data);
1667 static void on_toolbutton_make_activate(GtkWidget *menuitem, gpointer user_data)
1669 gchar *msg;
1671 last_toolbutton_action = user_data;
1672 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_ALL))
1673 msg = _("Build the current file with Make and the default target");
1674 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_CUSTOM))
1675 msg = _("Build the current file with Make and the specified target");
1676 else if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT))
1677 msg = _("Compile the current file with Make");
1678 else
1679 msg = NULL;
1680 g_object_set(widgets.build_action, "tooltip", msg, NULL);
1681 on_build_menu_item(menuitem, user_data);
1685 static void kill_process(GPid *pid)
1687 /* Unix: SIGQUIT is not the best signal to use because it causes a core dump (this should not
1688 * perforce necessary for just killing a process). But we must use a signal which we can
1689 * ignore because the main process get it too, it is declared to ignore in main.c. */
1690 gint result;
1692 #ifdef G_OS_WIN32
1693 g_return_if_fail(*pid != NULL);
1694 result = TerminateProcess(*pid, 0);
1695 /* TerminateProcess() returns TRUE on success, for the check below we have to convert
1696 * it to FALSE (and vice versa) */
1697 result = ! result;
1698 #else
1699 g_return_if_fail(*pid > 1);
1700 result = kill(*pid, SIGQUIT);
1701 #endif
1703 if (result != 0)
1704 ui_set_statusbar(TRUE, _("Process could not be stopped (%s)."), g_strerror(errno));
1705 else
1707 *pid = 0;
1708 build_menu_update(NULL);
1713 static void on_build_next_error(GtkWidget *menuitem, gpointer user_data)
1715 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow.tree_compiler),
1716 msgwin_goto_compiler_file_line))
1718 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1720 else
1721 ui_set_statusbar(FALSE, _("No more build errors."));
1725 static void on_build_previous_error(GtkWidget *menuitem, gpointer user_data)
1727 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow.tree_compiler),
1728 msgwin_goto_compiler_file_line))
1730 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_COMPILER);
1732 else
1733 ui_set_statusbar(FALSE, _("No more build errors."));
1737 void build_toolbutton_build_clicked(GtkAction *action, gpointer unused)
1739 if (last_toolbutton_action == GBO_TO_POINTER(GEANY_GBO_BUILD))
1741 on_build_menu_item(NULL, GBO_TO_POINTER(GEANY_GBO_BUILD));
1743 else
1745 on_build_menu_item(NULL, last_toolbutton_action);
1750 /*------------------------------------------------------
1752 * Create and handle the build menu configuration dialog
1754 *-------------------------------------------------------*/
1755 typedef struct RowWidgets
1757 GtkWidget *entries[GEANY_BC_CMDENTRIES_COUNT];
1758 GeanyBuildSource src;
1759 GeanyBuildSource dst;
1760 GeanyBuildCommand *cmdsrc;
1761 gint grp;
1762 gint cmd;
1763 gboolean cleared;
1764 gboolean used_dst;
1765 } RowWidgets;
1767 static GdkColor *insensitive_color;
1769 static void set_row_color(RowWidgets *r, GdkColor *color )
1771 enum GeanyBuildCmdEntries i;
1773 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1774 gtk_widget_modify_text(r->entries[i], GTK_STATE_NORMAL, color);
1778 static void set_build_command_entry_text(GtkWidget *wid, const gchar *text)
1780 if (GTK_IS_BUTTON(wid))
1781 gtk_button_set_label(GTK_BUTTON(wid), text);
1782 else
1783 gtk_entry_set_text(GTK_ENTRY(wid), text);
1787 static void on_clear_dialog_row(GtkWidget *unused, gpointer user_data)
1789 RowWidgets *r = user_data;
1790 gint src;
1791 enum GeanyBuildCmdEntries i;
1792 GeanyBuildCommand *bc = get_next_build_cmd(NULL, r->grp, r->cmd, r->dst, &src);
1794 if (bc != NULL)
1796 r->cmdsrc = bc;
1797 r->src = src;
1798 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1800 set_build_command_entry_text(r->entries[i],
1801 id_to_str(bc,i) != NULL ? id_to_str(bc,i) : "");
1804 else
1806 r->cmdsrc = NULL;
1807 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1809 set_build_command_entry_text(r->entries[i], "");
1812 r->used_dst = FALSE;
1813 set_row_color(r, insensitive_color);
1814 r->cleared = TRUE;
1818 static void on_clear_dialog_regex_row(GtkEntry *regex, gpointer unused)
1820 gtk_entry_set_text(regex,"");
1824 static void on_label_button_clicked(GtkWidget *wid, gpointer user_data)
1826 RowWidgets *r = user_data;
1827 const gchar *old = gtk_button_get_label(GTK_BUTTON(wid));
1828 /* FIXME: we should pass either build dialog or project dialog instead of NULL for parent */
1829 gchar *str = dialogs_show_input(_("Set menu item label"),
1830 NULL, NULL, old);
1832 if (!str)
1833 return;
1835 gtk_button_set_label(GTK_BUTTON(wid), str);
1836 g_free(str);
1837 r->used_dst = TRUE;
1838 set_row_color(r, NULL);
1842 static void on_entry_focus(GtkWidget *wid, GdkEventFocus *unused, gpointer user_data)
1844 RowWidgets *r = user_data;
1846 r->used_dst = TRUE;
1847 set_row_color(r, NULL);
1851 /* Column headings, array NULL-terminated */
1852 static const gchar *colheads[] =
1854 "#",
1855 N_("Label"),
1856 N_("Command"),
1857 N_("Working directory"),
1858 N_("Reset"),
1859 NULL
1862 /* column names */
1863 #define DC_ITEM 0
1864 #define DC_ENTRIES 1
1865 #define DC_CLEAR 4
1866 #define DC_N_COL 5
1868 static const guint entry_x_padding = 3;
1869 static const guint entry_y_padding = 0;
1872 static RowWidgets *build_add_dialog_row(GeanyDocument *doc, GtkTable *table, guint row,
1873 GeanyBuildSource dst, gint grp, gint cmd, gboolean dir)
1875 GtkWidget *label, *clear, *clearicon;
1876 RowWidgets *roww;
1877 GeanyBuildCommand *bc;
1878 gint src;
1879 enum GeanyBuildCmdEntries i;
1880 guint column = 0;
1881 gchar *text;
1883 text = g_strdup_printf("%d.", cmd + 1);
1884 label = gtk_label_new(text);
1885 g_free(text);
1886 insensitive_color = &(gtk_widget_get_style(label)->text[GTK_STATE_INSENSITIVE]);
1887 gtk_table_attach(table, label, column, column + 1, row, row + 1, GTK_FILL,
1888 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1889 roww = g_new0(RowWidgets, 1);
1890 roww->src = GEANY_BCS_COUNT;
1891 roww->grp = grp;
1892 roww->cmd = cmd;
1893 roww->dst = dst;
1894 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1896 gint xflags = (i == GEANY_BC_COMMAND) ? GTK_FILL | GTK_EXPAND : GTK_FILL;
1898 column += 1;
1899 if (i == GEANY_BC_LABEL)
1901 GtkWidget *wid = roww->entries[i] = gtk_button_new();
1902 gtk_button_set_use_underline(GTK_BUTTON(wid), TRUE);
1903 gtk_widget_set_tooltip_text(wid, _("Click to set menu item label"));
1904 g_signal_connect(wid, "clicked", G_CALLBACK(on_label_button_clicked), roww);
1906 else
1908 roww->entries[i] = gtk_entry_new();
1909 g_signal_connect(roww->entries[i], "focus-in-event", G_CALLBACK(on_entry_focus), roww);
1911 gtk_table_attach(table, roww->entries[i], column, column + 1, row, row + 1, xflags,
1912 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1914 column++;
1915 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
1916 clear = gtk_button_new();
1917 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
1918 g_signal_connect(clear, "clicked", G_CALLBACK(on_clear_dialog_row), roww);
1919 gtk_table_attach(table, clear, column, column + 1, row, row + 1, GTK_FILL,
1920 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1921 roww->cmdsrc = bc = get_build_cmd(doc, grp, cmd, &src);
1922 if (bc != NULL)
1923 roww->src = src;
1925 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1927 const gchar *str = "";
1929 if (bc != NULL )
1931 if ((str = id_to_str(bc, i)) == NULL)
1932 str = "";
1933 else if ((gint)dst == src)
1934 roww->used_dst = TRUE;
1936 set_build_command_entry_text(roww->entries[i], str);
1938 if (bc != NULL && ((gint)dst > src))
1939 set_row_color(roww, insensitive_color);
1940 if (bc != NULL && (src > (gint)dst || (grp == GEANY_GBG_FT && (doc == NULL || doc->file_type == NULL))))
1942 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
1943 gtk_widget_set_sensitive(roww->entries[i], FALSE);
1944 gtk_widget_set_sensitive(clear, FALSE);
1946 return roww;
1950 typedef struct BuildTableFields
1952 RowWidgets **rows;
1953 GtkWidget *fileregex;
1954 GtkWidget *nonfileregex;
1955 gchar **fileregexstring;
1956 gchar **nonfileregexstring;
1957 } BuildTableFields;
1960 GtkWidget *build_commands_table(GeanyDocument *doc, GeanyBuildSource dst, BuildTableData *table_data,
1961 GeanyFiletype *ft)
1963 GtkWidget *label, *sep, *clearicon, *clear;
1964 BuildTableFields *fields;
1965 GtkTable *table;
1966 const gchar **ch;
1967 gchar *txt;
1968 guint col, row, cmdindex;
1969 gint cmd;
1970 gint src;
1971 gboolean sensitivity;
1972 guint sep_padding = entry_y_padding + 3;
1974 table = GTK_TABLE(gtk_table_new(build_items_count + 12, 5, FALSE));
1975 fields = g_new0(BuildTableFields, 1);
1976 fields->rows = g_new0(RowWidgets*, build_items_count);
1977 for (ch = colheads, col = 0; *ch != NULL; ch++, col++)
1979 label = gtk_label_new(_(*ch));
1980 gtk_table_attach(table, label, col, col + 1, 0, 1,
1981 GTK_FILL, GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
1983 sep = gtk_hseparator_new();
1984 gtk_table_attach(table, sep, 0, DC_N_COL, 1, 2, GTK_FILL, GTK_FILL | GTK_EXPAND,
1985 entry_x_padding, sep_padding);
1986 if (ft != NULL && ft->id != GEANY_FILETYPES_NONE)
1987 txt = g_strdup_printf(_("%s commands"), ft->name);
1988 else
1989 txt = g_strdup_printf(_("%s commands"), _("No filetype"));
1991 label = ui_label_new_bold(txt);
1992 g_free(txt);
1993 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
1994 gtk_table_attach(table, label, 0, DC_N_COL, 2, 3, GTK_FILL, GTK_FILL | GTK_EXPAND,
1995 entry_x_padding, entry_y_padding);
1996 for (row = 3, cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++row, ++cmdindex, ++cmd)
1997 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_FT, cmd, FALSE);
1998 label = gtk_label_new(_("Error regular expression:"));
1999 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2000 entry_x_padding, entry_y_padding);
2001 fields->fileregex = gtk_entry_new();
2002 fields->fileregexstring = build_get_regex(GEANY_GBG_FT, NULL, &src);
2003 sensitivity = (ft == NULL) ? FALSE : TRUE;
2004 if (fields->fileregexstring != NULL && *(fields->fileregexstring) != NULL)
2006 gtk_entry_set_text(GTK_ENTRY(fields->fileregex), *(fields->fileregexstring));
2007 if (src > (gint)dst)
2008 sensitivity = FALSE;
2010 gtk_table_attach(table, fields->fileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
2011 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2012 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2013 clear = gtk_button_new();
2014 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2015 g_signal_connect_swapped(clear, "clicked",
2016 G_CALLBACK(on_clear_dialog_regex_row), (fields->fileregex));
2017 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
2018 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2019 gtk_widget_set_sensitive(fields->fileregex, sensitivity);
2020 gtk_widget_set_sensitive(clear, sensitivity);
2021 ++row;
2022 sep = gtk_hseparator_new();
2023 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2024 entry_x_padding, sep_padding);
2025 ++row;
2026 label = ui_label_new_bold(_("Independent commands"));
2027 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2028 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2029 entry_x_padding, entry_y_padding);
2030 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++row, ++cmdindex, ++cmd)
2031 fields->rows[cmdindex] = build_add_dialog_row(
2032 doc, table, row, dst, GEANY_GBG_NON_FT, cmd, TRUE);
2033 label = gtk_label_new(_("Error regular expression:"));
2034 gtk_table_attach(table, label, 0, DC_ENTRIES + 1, row, row + 1, GTK_FILL,
2035 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2036 fields->nonfileregex = gtk_entry_new();
2037 fields->nonfileregexstring = build_get_regex(GEANY_GBG_NON_FT, NULL, &src);
2038 sensitivity = TRUE;
2039 if (fields->nonfileregexstring != NULL && *(fields->nonfileregexstring) != NULL)
2041 gtk_entry_set_text(GTK_ENTRY(fields->nonfileregex), *(fields->nonfileregexstring));
2042 sensitivity = src > (gint)dst ? FALSE : TRUE;
2044 gtk_table_attach(table, fields->nonfileregex, DC_ENTRIES + 1, DC_CLEAR, row, row + 1, GTK_FILL,
2045 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2046 clearicon = gtk_image_new_from_stock(GTK_STOCK_CLEAR, GTK_ICON_SIZE_MENU);
2047 clear = gtk_button_new();
2048 gtk_button_set_image(GTK_BUTTON(clear), clearicon);
2049 g_signal_connect_swapped(clear, "clicked",
2050 G_CALLBACK(on_clear_dialog_regex_row), (fields->nonfileregex));
2051 gtk_table_attach(table, clear, DC_CLEAR, DC_CLEAR + 1, row, row + 1, GTK_FILL,
2052 GTK_FILL | GTK_EXPAND, entry_x_padding, entry_y_padding);
2053 gtk_widget_set_sensitive(fields->nonfileregex, sensitivity);
2054 gtk_widget_set_sensitive(clear, sensitivity);
2055 ++row;
2056 label = gtk_label_new(NULL);
2057 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2058 _("Note: Item 2 opens a dialog and appends the response to the command."));
2059 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2060 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2061 entry_x_padding, entry_y_padding);
2062 ++row;
2063 sep = gtk_hseparator_new();
2064 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2065 entry_x_padding, sep_padding);
2066 ++row;
2067 label = ui_label_new_bold(_("Execute commands"));
2068 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2069 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2070 entry_x_padding, entry_y_padding);
2071 for (++row, cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++row, ++cmdindex, ++cmd)
2072 fields->rows[cmdindex] = build_add_dialog_row(doc, table, row, dst, GEANY_GBG_EXEC, cmd, TRUE);
2073 sep = gtk_hseparator_new();
2074 gtk_table_attach(table, sep, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2075 entry_x_padding, sep_padding);
2076 ++row;
2077 label = gtk_label_new(NULL);
2078 ui_label_set_markup(GTK_LABEL(label), "<i>%s</i>",
2079 _("%d, %e, %f, %p are substituted in command and directory fields, see manual for details."));
2080 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
2081 gtk_table_attach(table, label, 0, DC_N_COL, row, row + 1, GTK_FILL, GTK_FILL | GTK_EXPAND,
2082 entry_x_padding, entry_y_padding);
2083 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
2084 ++row;
2085 *table_data = fields;
2086 return GTK_WIDGET(table);
2090 void build_free_fields(BuildTableData table_data)
2092 gint cmdindex;
2094 for (cmdindex = 0; cmdindex < build_items_count; ++cmdindex)
2095 g_free(table_data->rows[cmdindex]);
2096 g_free(table_data->rows);
2097 g_free(table_data);
2101 /* string compare where null pointers match null or 0 length strings */
2102 #if 0
2103 static gint stcmp(const gchar *a, const gchar *b)
2105 if (a == NULL && b == NULL)
2106 return 0;
2107 if (a == NULL && b != NULL)
2108 return strlen(b);
2109 if (a != NULL && b == NULL)
2110 return strlen(a);
2111 return strcmp(a, b);
2113 #endif
2116 static const gchar *get_build_command_entry_text(GtkWidget *wid)
2118 if (GTK_IS_BUTTON(wid))
2119 return gtk_button_get_label(GTK_BUTTON(wid));
2120 else
2121 return gtk_entry_get_text(GTK_ENTRY(wid));
2125 static gboolean read_row(BuildDestination *dst, BuildTableData table_data, gint drow, gint grp, gint cmd)
2127 gchar *entries[GEANY_BC_CMDENTRIES_COUNT];
2128 gboolean changed = FALSE;
2129 enum GeanyBuildCmdEntries i;
2131 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2133 entries[i] = g_strdup(get_build_command_entry_text(table_data->rows[drow]->entries[i]));
2135 if (table_data->rows[drow]->cleared)
2137 if (dst->dst[grp] != NULL)
2139 if (*(dst->dst[grp]) == NULL)
2140 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2141 (*(dst->dst[grp]))[cmd].exists = FALSE;
2142 (*(dst->dst[grp]))[cmd].changed = TRUE;
2143 changed = TRUE;
2146 if (table_data->rows[drow]->used_dst == TRUE)
2148 if (dst->dst[grp] != NULL)
2150 if (*(dst->dst[grp]) == NULL)
2151 *(dst->dst[grp]) = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2152 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2153 set_command(&(*(dst->dst[grp]))[cmd], i, entries[i]);
2154 (*(dst->dst[grp]))[cmd].exists = TRUE;
2155 (*(dst->dst[grp]))[cmd].changed = TRUE;
2156 changed = TRUE;
2159 else
2161 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2162 g_free(entries[i]);
2164 return changed;
2168 static gboolean read_regex(GtkWidget *regexentry, gchar **src, gchar **dst)
2170 gboolean changed = FALSE;
2171 const gchar *reg = gtk_entry_get_text(GTK_ENTRY(regexentry));
2173 if (((src == NULL /* originally there was no regex */
2174 || *src == NULL) /* or it was NULL*/
2175 && NZV(reg)) /* and something was typed */
2176 || (src != NULL /* originally there was a regex*/
2177 && strcmp(*src, reg) != 0)) /* and it has been changed */
2179 if (dst != NULL)
2181 setptr(*dst, g_strdup(reg));
2182 changed = TRUE;
2185 return changed;
2189 static gboolean build_read_commands(BuildDestination *dst, BuildTableData table_data, gint response)
2191 gint cmdindex, cmd;
2192 gboolean changed = FALSE;
2194 if (response == GTK_RESPONSE_ACCEPT)
2196 for (cmdindex = 0, cmd = 0; cmd < build_groups_count[GEANY_GBG_FT]; ++cmdindex, ++cmd)
2197 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_FT, cmd);
2198 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_NON_FT]; ++cmdindex, ++cmd)
2199 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_NON_FT, cmd);
2200 for (cmd = 0; cmd < build_groups_count[GEANY_GBG_EXEC]; ++cmdindex, ++cmd)
2201 changed |= read_row(dst, table_data, cmdindex, GEANY_GBG_EXEC, cmd);
2202 changed |= read_regex(table_data->fileregex, table_data->fileregexstring, dst->fileregexstr);
2203 changed |= read_regex(table_data->nonfileregex, table_data->nonfileregexstring, dst->nonfileregexstr);
2205 return changed;
2209 void build_read_project(GeanyFiletype *ft, BuildTableData build_properties)
2211 BuildDestination menu_dst;
2213 if (ft != NULL)
2215 menu_dst.dst[GEANY_GBG_FT] = &(ft->projfilecmds);
2216 menu_dst.fileregexstr = &(ft->projerror_regex_string);
2218 else
2220 menu_dst.dst[GEANY_GBG_FT] = NULL;
2221 menu_dst.fileregexstr = NULL;
2223 menu_dst.dst[GEANY_GBG_NON_FT] = &non_ft_proj;
2224 menu_dst.dst[GEANY_GBG_EXEC] = &exec_proj;
2225 menu_dst.nonfileregexstr = &regex_proj;
2227 build_read_commands(&menu_dst, build_properties, GTK_RESPONSE_ACCEPT);
2231 static void show_build_commands_dialog(void)
2233 GtkWidget *dialog, *table, *vbox;
2234 GeanyDocument *doc = document_get_current();
2235 GeanyFiletype *ft = NULL;
2236 const gchar *title = _("Set Build Commands");
2237 gint response;
2238 BuildTableData table_data;
2239 BuildDestination prefdsts;
2241 if (doc != NULL)
2242 ft = doc->file_type;
2243 dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(main_widgets.window),
2244 GTK_DIALOG_DESTROY_WITH_PARENT,
2245 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
2246 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
2247 table = build_commands_table(doc, GEANY_BCS_PREF, &table_data, ft);
2248 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
2249 gtk_box_pack_start(GTK_BOX(vbox), table, TRUE, TRUE, 0);
2250 gtk_widget_show_all(dialog);
2251 /* run modally to prevent user changing idx filetype */
2252 response = gtk_dialog_run(GTK_DIALOG(dialog));
2254 prefdsts.dst[GEANY_GBG_NON_FT] = &non_ft_pref;
2255 if (ft != NULL)
2257 prefdsts.dst[GEANY_GBG_FT] = &(ft->homefilecmds);
2258 prefdsts.fileregexstr = &(ft->homeerror_regex_string);
2259 prefdsts.dst[GEANY_GBG_EXEC] = &(ft->homeexeccmds);
2261 else
2263 prefdsts.dst[GEANY_GBG_FT] = NULL;
2264 prefdsts.fileregexstr = NULL;
2265 prefdsts.dst[GEANY_GBG_EXEC] = NULL;
2267 prefdsts.nonfileregexstr = &regex_pref;
2268 if (build_read_commands(&prefdsts, table_data, response) && ft != NULL)
2269 filetypes_save_commands(ft);
2270 build_free_fields(table_data);
2272 build_menu_update(doc);
2273 gtk_widget_destroy(dialog);
2277 /* Creates the relevant build menu if necessary. */
2278 BuildMenuItems *build_get_menu_items(gint filetype_idx)
2280 BuildMenuItems *items;
2282 items = &menu_items;
2283 if (items->menu == NULL)
2284 create_build_menu(items);
2285 return items;
2289 /*----------------------------------------------------------
2291 * Load and store configuration
2293 * ---------------------------------------------------------*/
2294 static const gchar *build_grp_name = "build-menu";
2296 /* config format for build-menu group is prefix_gg_nn_xx=value
2297 * where gg = FT, NF, EX for the command group
2298 * nn = 2 digit command number
2299 * xx = LB for label, CM for command and WD for working dir */
2300 static const gchar *groups[GEANY_GBG_COUNT] = { "FT", "NF", "EX" };
2301 static const gchar *fixedkey="xx_xx_xx";
2303 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2304 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2305 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2307 static void build_load_menu_grp(GKeyFile *config, GeanyBuildCommand **dst, gint grp,
2308 gchar *prefix, gboolean loc)
2310 gint cmd, prefixlen; /* NOTE prefixlen used in macros above */
2311 GeanyBuildCommand *dstcmd;
2312 gchar *key;
2313 static gchar cmdbuf[3] = " ";
2315 if (*dst == NULL)
2316 *dst = g_new0(GeanyBuildCommand, build_groups_count[grp]);
2317 dstcmd = *dst;
2318 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2319 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2320 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2322 gchar *label;
2323 if (cmd < 0 || cmd >= 100)
2324 return; /* ensure no buffer overflow */
2325 sprintf(cmdbuf, "%02d", cmd);
2326 set_key_grp(key, groups[grp]);
2327 set_key_cmd(key, cmdbuf);
2328 set_key_fld(key, "LB");
2329 if (loc)
2330 label = g_key_file_get_locale_string(config, build_grp_name, key, NULL, NULL);
2331 else
2332 label = g_key_file_get_string(config, build_grp_name, key, NULL);
2333 if (label != NULL)
2335 dstcmd[cmd].exists = TRUE;
2336 setptr(dstcmd[cmd].label, label);
2337 set_key_fld(key,"CM");
2338 setptr(dstcmd[cmd].command,
2339 g_key_file_get_string(config, build_grp_name, key, NULL));
2340 set_key_fld(key,"WD");
2341 setptr(dstcmd[cmd].working_dir,
2342 g_key_file_get_string(config, build_grp_name, key, NULL));
2344 else dstcmd[cmd].exists = FALSE;
2346 g_free(key);
2350 /* for the specified source load new format build menu items or try to make some sense of
2351 * old format setings, not done perfectly but better than ignoring them */
2352 void build_load_menu(GKeyFile *config, GeanyBuildSource src, gpointer p)
2354 GeanyFiletype *ft;
2355 GeanyProject *pj;
2356 gchar **ftlist;
2357 gchar *value, *basedir, *makebasedir;
2358 gboolean bvalue = FALSE;
2360 if (g_key_file_has_group(config, build_grp_name))
2362 switch (src)
2364 case GEANY_BCS_FT:
2365 ft = (GeanyFiletype*)p;
2366 if (ft == NULL)
2367 return;
2368 build_load_menu_grp(config, &(ft->filecmds), GEANY_GBG_FT, NULL, TRUE);
2369 build_load_menu_grp(config, &(ft->ftdefcmds), GEANY_GBG_NON_FT, NULL, TRUE);
2370 build_load_menu_grp(config, &(ft->execcmds), GEANY_GBG_EXEC, NULL, TRUE);
2371 setptr(ft->error_regex_string,
2372 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2373 break;
2374 case GEANY_BCS_HOME_FT:
2375 ft = (GeanyFiletype*)p;
2376 if (ft == NULL)
2377 return;
2378 build_load_menu_grp(config, &(ft->homefilecmds), GEANY_GBG_FT, NULL, FALSE);
2379 build_load_menu_grp(config, &(ft->homeexeccmds), GEANY_GBG_EXEC, NULL, FALSE);
2380 setptr(ft->homeerror_regex_string,
2381 g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2382 break;
2383 case GEANY_BCS_PREF:
2384 build_load_menu_grp(config, &non_ft_pref, GEANY_GBG_NON_FT, NULL, FALSE);
2385 build_load_menu_grp(config, &exec_pref, GEANY_GBG_EXEC, NULL, FALSE);
2386 setptr(regex_pref, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2387 break;
2388 case GEANY_BCS_PROJ:
2389 build_load_menu_grp(config, &non_ft_proj, GEANY_GBG_NON_FT, NULL, FALSE);
2390 build_load_menu_grp(config, &exec_proj, GEANY_GBG_EXEC, NULL, FALSE);
2391 setptr(regex_proj, g_key_file_get_string(config, build_grp_name, "error_regex", NULL));
2392 pj = (GeanyProject*)p;
2393 if (p == NULL)
2394 return;
2395 ftlist = g_key_file_get_string_list(config, build_grp_name, "filetypes", NULL, NULL);
2396 if (ftlist != NULL)
2398 gchar **ftname;
2399 if (pj->build_filetypes_list == NULL)
2400 pj->build_filetypes_list = g_ptr_array_new();
2401 g_ptr_array_set_size(pj->build_filetypes_list, 0);
2402 for (ftname = ftlist; *ftname != NULL; ++ftname)
2404 ft = filetypes_lookup_by_name(*ftname);
2405 if (ft != NULL)
2407 gchar *regkey = g_strdup_printf("%serror_regex", *ftname);
2408 g_ptr_array_add(pj->build_filetypes_list, ft);
2409 setptr(ft->projerror_regex_string,
2410 g_key_file_get_string(config, build_grp_name, regkey, NULL));
2411 g_free(regkey);
2412 build_load_menu_grp(config, &(ft->projfilecmds), GEANY_GBG_FT, *ftname, FALSE);
2413 build_load_menu_grp(config, &(ft->projexeccmds), GEANY_GBG_EXEC, *ftname, FALSE);
2416 g_free(ftlist);
2418 break;
2419 default: /* defaults don't load from config, see build_init */
2420 break;
2424 /* load old [build_settings] values if there is no value defined by [build-menu] */
2426 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2427 /* TODO: rewrite as function */
2428 #define ASSIGNIF(type, id, string, value) \
2429 if (NZV(value) && ! type[GBO_TO_CMD(id)].exists) { \
2430 type[GBO_TO_CMD(id)].exists = TRUE; \
2431 setptr(type[GBO_TO_CMD(id)].label, g_strdup(string)); \
2432 setptr(type[GBO_TO_CMD(id)].command, (value)); \
2433 setptr(type[GBO_TO_CMD(id)].working_dir, NULL); \
2434 type[GBO_TO_CMD(id)].old = TRUE; \
2435 } else \
2436 g_free(value);
2438 switch (src)
2440 case GEANY_BCS_FT:
2441 ft = (GeanyFiletype*)p;
2442 value = g_key_file_get_string(config, "build_settings", "compiler", NULL);
2443 if (value != NULL)
2445 if (ft->filecmds == NULL)
2446 ft->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2447 ASSIGNIF(ft->filecmds, GEANY_GBO_COMPILE, _("_Compile"), value);
2449 value = g_key_file_get_string(config, "build_settings", "linker", NULL);
2450 if (value != NULL)
2452 if (ft->filecmds == NULL)
2453 ft->filecmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2454 ASSIGNIF(ft->filecmds, GEANY_GBO_BUILD, _("_Build"), value);
2456 value = g_key_file_get_string(config, "build_settings", "run_cmd", NULL);
2457 if (value != NULL)
2459 if (ft->execcmds == NULL)
2460 ft->execcmds = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2461 ASSIGNIF(ft->execcmds, GEANY_GBO_EXEC, _("_Execute"), value);
2463 if (ft->error_regex_string == NULL)
2464 ft->error_regex_string = g_key_file_get_string(config, "build_settings", "error_regex", NULL);
2465 break;
2466 case GEANY_BCS_PROJ:
2467 if (non_ft_pref == NULL)
2468 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2469 basedir = project_get_base_path();
2470 if (basedir == NULL)
2471 basedir = g_strdup("%d");
2472 bvalue = g_key_file_get_boolean(config, "project", "make_in_base_path", NULL);
2473 if (bvalue)
2474 makebasedir = g_strdup(basedir);
2475 else
2476 makebasedir = g_strdup("%d");
2477 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].old)
2478 setptr(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_ALL)].working_dir, g_strdup(makebasedir));
2479 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].old)
2480 setptr(non_ft_pref[GBO_TO_CMD(GEANY_GBO_CUSTOM)].working_dir, g_strdup(makebasedir));
2481 if (non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].old)
2482 setptr(non_ft_pref[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)].working_dir, g_strdup("%d"));
2483 value = g_key_file_get_string(config, "project", "run_cmd", NULL);
2484 if (NZV(value))
2486 if (exec_proj == NULL)
2487 exec_proj = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2488 if (! exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists)
2490 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].exists = TRUE;
2491 setptr(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].label, g_strdup(_("_Execute")));
2492 setptr(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].command, value);
2493 setptr(exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].working_dir, g_strdup(basedir));
2494 exec_proj[GBO_TO_CMD(GEANY_GBO_EXEC)].old = TRUE;
2497 g_free(makebasedir);
2498 g_free(basedir);
2499 break;
2500 case GEANY_BCS_PREF:
2501 value = g_key_file_get_string(config, "tools", "make_cmd", NULL);
2502 if (value != NULL)
2504 if (non_ft_pref == NULL)
2505 non_ft_pref = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2506 ASSIGNIF(non_ft_pref, GEANY_GBO_CUSTOM, _("Make Custom _Target"),
2507 g_strdup_printf("%s ", value));
2508 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_OBJECT, _("Make _Object"),
2509 g_strdup_printf("%s %%e.o",value));
2510 ASSIGNIF(non_ft_pref, GEANY_GBO_MAKE_ALL, _("_Make"), value);
2512 break;
2513 default:
2514 break;
2519 static gint build_save_menu_grp(GKeyFile *config, GeanyBuildCommand *src, gint grp, gchar *prefix)
2521 gint cmd, prefixlen; /* NOTE prefixlen used in macros above */
2522 gchar *key;
2523 gint count = 0;
2524 enum GeanyBuildCmdEntries i;
2526 if (src == NULL)
2527 return 0;
2528 prefixlen = prefix == NULL ? 0 : strlen(prefix);
2529 key = g_strconcat(prefix == NULL ? "" : prefix, fixedkey, NULL);
2530 for (cmd = 0; cmd < build_groups_count[grp]; ++cmd)
2532 if (src[cmd].exists) ++count;
2533 if (src[cmd].changed)
2535 static gchar cmdbuf[4] = " ";
2536 if (cmd < 0 || cmd >= 100)
2537 return count; /* ensure no buffer overflow */
2538 sprintf(cmdbuf, "%02d", cmd);
2539 set_key_grp(key, groups[grp]);
2540 set_key_cmd(key, cmdbuf);
2541 if (src[cmd].exists)
2543 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2545 set_key_fld(key, config_keys[i]);
2546 g_key_file_set_string(config, build_grp_name, key, id_to_str(&src[cmd], i));
2549 else
2551 for (i = 0; i < GEANY_BC_CMDENTRIES_COUNT; i++)
2553 set_key_fld(key, config_keys[i]);
2554 g_key_file_remove_key(config, build_grp_name, key, NULL);
2559 g_free(key);
2560 return count;
2564 typedef struct ForEachData
2566 GKeyFile *config;
2567 GPtrArray *ft_names;
2568 } ForEachData;
2571 static void foreach_project_filetype(gpointer data, gpointer user_data)
2573 GeanyFiletype *ft = data;
2574 ForEachData *d = user_data;
2575 gint i = 0;
2576 gchar *regkey = g_strdup_printf("%serror_regex", ft->name);
2578 i += build_save_menu_grp(d->config, ft->projfilecmds, GEANY_GBG_FT, ft->name);
2579 i += build_save_menu_grp(d->config, ft->projexeccmds, GEANY_GBG_EXEC, ft->name);
2580 if (NZV(ft->projerror_regex_string))
2582 g_key_file_set_string(d->config, build_grp_name, regkey, ft->projerror_regex_string);
2583 i++;
2585 else
2586 g_key_file_remove_key(d->config, build_grp_name, regkey, NULL);
2587 g_free(regkey);
2588 if (i > 0)
2589 g_ptr_array_add(d->ft_names, ft->name);
2593 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
2594 void build_save_menu(GKeyFile *config, gpointer ptr, GeanyBuildSource src)
2596 GeanyFiletype *ft;
2597 GeanyProject *pj;
2598 ForEachData data;
2600 switch (src)
2602 case GEANY_BCS_HOME_FT:
2603 ft = (GeanyFiletype*)ptr;
2604 if (ft == NULL)
2605 return;
2606 build_save_menu_grp(config, ft->homefilecmds, GEANY_GBG_FT, NULL);
2607 build_save_menu_grp(config, ft->homeexeccmds, GEANY_GBG_EXEC, NULL);
2608 if (NZV(ft->homeerror_regex_string))
2609 g_key_file_set_string(config, build_grp_name, "error_regex", ft->homeerror_regex_string);
2610 else
2611 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2612 break;
2613 case GEANY_BCS_PREF:
2614 build_save_menu_grp(config, non_ft_pref, GEANY_GBG_NON_FT, NULL);
2615 build_save_menu_grp(config, exec_pref, GEANY_GBG_EXEC, NULL);
2616 if (NZV(regex_pref))
2617 g_key_file_set_string(config, build_grp_name, "error_regex", regex_pref);
2618 else
2619 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2620 break;
2621 case GEANY_BCS_PROJ:
2622 pj = (GeanyProject*)ptr;
2623 build_save_menu_grp(config, non_ft_proj, GEANY_GBG_NON_FT, NULL);
2624 build_save_menu_grp(config, exec_proj, GEANY_GBG_EXEC, NULL);
2625 if (NZV(regex_proj))
2626 g_key_file_set_string(config, build_grp_name, "error_regex", regex_proj);
2627 else
2628 g_key_file_remove_key(config, build_grp_name, "error_regex", NULL);
2629 if (pj->build_filetypes_list != NULL)
2631 data.config = config;
2632 data.ft_names = g_ptr_array_new();
2633 g_ptr_array_foreach(pj->build_filetypes_list, foreach_project_filetype, (gpointer)(&data));
2634 if (data.ft_names->pdata != NULL)
2635 g_key_file_set_string_list(config, build_grp_name, "filetypes",
2636 (const gchar**)(data.ft_names->pdata), data.ft_names->len);
2637 else
2638 g_key_file_remove_key(config, build_grp_name, "filetypes", NULL);
2639 g_ptr_array_free(data.ft_names, TRUE);
2641 break;
2642 default: /* defaults and GEANY_BCS_FT can't save */
2643 break;
2648 void build_set_group_count(GeanyBuildGroup grp, gint count)
2650 gint i, sum;
2652 if (count > build_groups_count[grp])
2653 build_groups_count[grp] = count;
2654 for (i = 0, sum = 0; i < GEANY_GBG_COUNT; ++i)
2655 sum += build_groups_count[i];
2656 build_items_count = sum;
2660 gint build_get_group_count(GeanyBuildGroup grp)
2662 return build_groups_count[grp];
2666 static void on_project_close(void)
2668 /* remove project regexen */
2669 setptr(regex_proj, NULL);
2673 static struct
2675 const gchar *label;
2676 const gchar *command;
2677 const gchar *working_dir;
2678 GeanyBuildCommand **ptr;
2679 gint index;
2680 } default_cmds[] = {
2681 { N_("_Make"), "make", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_ALL)},
2682 { N_("Make Custom _Target"), "make ", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_CUSTOM)},
2683 { N_("Make _Object"), "make %e.o", NULL, &non_ft_def, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)},
2684 { N_("_Execute"), "./%e", NULL, &exec_def, GBO_TO_CMD(GEANY_GBO_EXEC)},
2685 { NULL, NULL, NULL, NULL, 0 }
2689 void build_init(void)
2691 GtkWidget *item;
2692 GtkWidget *toolmenu;
2693 gint cmdindex;
2695 g_signal_connect(geany_object, "project-close", on_project_close, NULL);
2697 ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_FT]);
2698 non_ft_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_NON_FT]);
2699 exec_def = g_new0(GeanyBuildCommand, build_groups_count[GEANY_GBG_EXEC]);
2700 run_info = g_new0(RunInfo, build_groups_count[GEANY_GBG_EXEC]);
2702 for (cmdindex = 0; default_cmds[cmdindex].command != NULL; ++cmdindex)
2704 GeanyBuildCommand *cmd = &((*(default_cmds[cmdindex].ptr))[ default_cmds[cmdindex].index ]);
2705 cmd->exists = TRUE;
2706 cmd->label = g_strdup(_(default_cmds[cmdindex].label));
2707 cmd->command = g_strdup(default_cmds[cmdindex].command);
2708 cmd->working_dir = g_strdup(default_cmds[cmdindex].working_dir);
2711 /* create the toolbar Build item sub menu */
2712 toolmenu = gtk_menu_new();
2713 g_object_ref(toolmenu);
2715 /* build the code */
2716 item = ui_image_menu_item_new(GEANY_STOCK_BUILD, _("_Build"));
2717 gtk_widget_show(item);
2718 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2719 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_build_activate),
2720 GBO_TO_POINTER(GEANY_GBO_BUILD));
2721 widgets.toolitem_build = item;
2723 item = gtk_separator_menu_item_new();
2724 gtk_widget_show(item);
2725 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2727 /* build the code with make all */
2728 item = gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2729 gtk_widget_show(item);
2730 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2731 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2732 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL));
2733 widgets.toolitem_make_all = item;
2735 /* build the code with make custom */
2736 item = gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target"));
2737 gtk_widget_show(item);
2738 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2739 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2740 GBO_TO_POINTER(GEANY_GBO_CUSTOM));
2741 widgets.toolitem_make_custom = item;
2743 /* build the code with make object */
2744 item = gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2745 gtk_widget_show(item);
2746 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2747 g_signal_connect(item, "activate", G_CALLBACK(on_toolbutton_make_activate),
2748 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT));
2749 widgets.toolitem_make_object = item;
2751 item = gtk_separator_menu_item_new();
2752 gtk_widget_show(item);
2753 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2755 /* arguments */
2756 item = ui_image_menu_item_new(GTK_STOCK_PREFERENCES, _("_Set Build Commands"));
2757 gtk_widget_show(item);
2758 gtk_container_add(GTK_CONTAINER(toolmenu), item);
2759 g_signal_connect(item, "activate", G_CALLBACK(on_set_build_commands_activate), NULL);
2760 widgets.toolitem_set_args = item;
2762 /* get toolbar action pointers */
2763 widgets.build_action = toolbar_get_action_by_name("Build");
2764 widgets.compile_action = toolbar_get_action_by_name("Compile");
2765 widgets.run_action = toolbar_get_action_by_name("Run");
2766 widgets.toolmenu = toolmenu;
2767 /* set the submenu to the toolbar item */
2768 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets.build_action), toolmenu);