2 * build.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
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.
26 * Build commands and menu items.
37 #include <glib/gstdio.h>
40 # include <sys/types.h>
41 # include <sys/wait.h>
53 #include "msgwindow.h"
54 #include "filetypes.h"
55 #include "keybindings.h"
61 #include "geanymenubuttonaction.h"
64 GeanyBuildInfo build_info
= {GEANY_GBG_FT
, 0, 0, NULL
, GEANY_FILETYPES_NONE
, NULL
, 0};
66 static gchar
*current_dir_entered
= NULL
;
68 typedef struct RunInfo
74 static RunInfo
*run_info
;
77 static const gchar RUN_SCRIPT_CMD
[] = "geany_run_script.bat";
79 static const gchar RUN_SCRIPT_CMD
[] = "./geany_run_script.sh";
82 /* pack group (<8) and command (<32) into a user_data pointer */
83 #define GRP_CMD_TO_POINTER(grp, cmd) GINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
84 #define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
85 #define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_INT(gptr)&0x1f)
86 #define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_INT(gptr)&0xe0) >> 5)
88 static gpointer last_toolbutton_action
= GBO_TO_POINTER(GEANY_GBO_BUILD
);
90 static BuildMenuItems menu_items
= {NULL
, {NULL
, NULL
, NULL
, NULL
}};
94 GtkAction
*run_action
;
95 GtkAction
*compile_action
;
96 GtkAction
*build_action
;
99 GtkWidget
*toolitem_build
;
100 GtkWidget
*toolitem_make_all
;
101 GtkWidget
*toolitem_make_custom
;
102 GtkWidget
*toolitem_make_object
;
103 GtkWidget
*toolitem_set_args
;
107 static gint build_groups_count
[GEANY_GBG_COUNT
] = { 3, 4, 2 };
108 static gint build_items_count
= 9;
111 static void build_exit_cb(GPid child_pid
, gint status
, gpointer user_data
);
112 static gboolean
build_iofunc(GIOChannel
*ioc
, GIOCondition cond
, gpointer data
);
114 static gboolean
build_create_shellscript(const gchar
*fname
, const gchar
*cmd
, gboolean autoclose
);
115 static GPid
build_spawn_cmd(GeanyDocument
*doc
, const gchar
*cmd
, const gchar
*dir
);
116 static void set_stop_button(gboolean stop
);
117 static void run_exit_cb(GPid child_pid
, gint status
, gpointer user_data
);
118 static void on_set_build_commands_activate(GtkWidget
*w
, gpointer u
);
119 static void on_build_next_error(GtkWidget
*menuitem
, gpointer user_data
);
120 static void on_build_previous_error(GtkWidget
*menuitem
, gpointer user_data
);
121 static void kill_process(GPid
*pid
);
122 static void show_build_result_message(gboolean failure
);
123 static void process_build_output_line(const gchar
*line
, gint color
);
124 static void show_build_commands_dialog(void);
127 void build_finalize(void)
129 g_free(build_info
.dir
);
130 g_free(build_info
.custom_target
);
132 if (menu_items
.menu
!= NULL
&& GTK_IS_WIDGET(menu_items
.menu
))
133 gtk_widget_destroy(menu_items
.menu
);
137 /* note: copied from keybindings.c, may be able to go away */
138 static void add_menu_accel(GeanyKeyGroup
*group
, guint kb_id
,
139 GtkAccelGroup
*accel_group
, GtkWidget
*menuitem
)
141 GeanyKeyBinding
*kb
= keybindings_get_item(group
, kb_id
);
144 gtk_widget_add_accelerator(menuitem
, "activate", accel_group
,
145 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
149 /* convenience routines to access parts of GeanyBuildCommand */
150 static gchar
*id_to_str(GeanyBuildCommand
*bc
, gint id
)
152 return bc
->entries
[id
];
156 static gchar
*buildcmd_label(GeanyBuildCommand
*bc
)
158 return id_to_str(bc
, GEANY_BC_LABEL
);
162 static gchar
*buildcmd_cmd(GeanyBuildCommand
*bc
)
164 return id_to_str(bc
, GEANY_BC_COMMAND
);
168 static gchar
*buildcmd_working_dir(GeanyBuildCommand
*bc
)
170 return id_to_str(bc
, GEANY_BC_WORKING_DIR
);
174 static const gchar
*config_keys
[] = {
175 [GEANY_BC_LABEL
] = "LB",
176 [GEANY_BC_COMMAND
] = "CM",
177 [GEANY_BC_WORKING_DIR
] = "WD"
180 /*-----------------------------------------------------
182 * Execute commands and handle results
184 *-----------------------------------------------------*/
186 /* the various groups of commands not in the filetype struct */
187 GeanyBuildCommand
*ft_def
= NULL
;
188 GeanyBuildCommand
*non_ft_proj
= NULL
;
189 GeanyBuildCommand
*non_ft_pref
= NULL
;
190 GeanyBuildCommand
*non_ft_def
= NULL
;
191 GeanyBuildCommand
*exec_proj
= NULL
;
192 GeanyBuildCommand
*exec_pref
= NULL
;
193 GeanyBuildCommand
*exec_def
= NULL
;
194 /* and the regexen not in the filetype structure */
195 gchar
*regex_pref
= NULL
;
196 gchar
*regex_proj
= NULL
;
198 /* control if build commands are printed by get_build_cmd, for debug purposes only*/
199 #ifndef PRINTBUILDCMDS
200 #define PRINTBUILDCMDS FALSE
202 gboolean printbuildcmds
= PRINTBUILDCMDS
;
204 static GeanyBuildCommand
**cl
[GEANY_GBG_COUNT
][GEANY_BCS_COUNT
] = {
205 /* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
206 * GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
207 { &ft_def
, NULL
, NULL
, NULL
, NULL
, NULL
},
208 { &non_ft_def
, NULL
, NULL
, &non_ft_pref
, NULL
, &non_ft_proj
},
209 { &exec_def
, NULL
, NULL
, &exec_pref
, NULL
, &exec_proj
}
213 /* for debug only, print the commands structures in priority order */
214 static void printfcmds()
216 GeanyFiletype
*ft
= NULL
;
219 enum GeanyBuildCmdEntries n
;
220 gint cc
[GEANY_BCS_COUNT
];
222 doc
= document_get_current();
227 printf("filetype %s\n",ft
->name
);
228 cl
[GEANY_GBG_FT
][GEANY_BCS_FT
] = &(ft
->filecmds
);
229 cl
[GEANY_GBG_FT
][GEANY_BCS_HOME_FT
] = &(ft
->homefilecmds
);
230 cl
[GEANY_GBG_FT
][GEANY_BCS_PROJ
] = &(ft
->projfilecmds
);
231 cl
[GEANY_GBG_NON_FT
][GEANY_BCS_FT
] = &(ft
->ftdefcmds
);
232 cl
[GEANY_GBG_EXEC
][GEANY_BCS_FT
] = &(ft
->execcmds
);
233 cl
[GEANY_GBG_EXEC
][GEANY_BCS_HOME_FT
] = &(ft
->homeexeccmds
);
234 cl
[GEANY_GBG_EXEC
][GEANY_BCS_PROJ_FT
] = &(ft
->projexeccmds
);
236 for (i
= 0; i
< GEANY_BCS_COUNT
; ++i
)
239 for (j
= 0; j
< GEANY_GBG_COUNT
; ++j
)
241 for (k
= 0; k
< build_groups_count
[j
]; ++k
)
242 if (cl
[j
][i
] != NULL
&& *(cl
[j
][i
]) != NULL
&& (*(cl
[j
][i
]))[k
].exists
)
244 for (n
= 0; n
< GEANY_BC_CMDENTRIES_COUNT
; n
++)
246 if ((*(cl
[j
][i
]))[k
].entries
[n
] != NULL
&&
247 (l
= strlen((*(cl
[j
][i
]))[k
].entries
[n
])) > m
)
256 for (i
= 0; i
< GEANY_GBG_COUNT
; ++i
)
258 for (k
= 0; k
< build_groups_count
[i
]; ++k
)
260 for (l
= 0; l
< 2; ++l
)
263 for (j
= 0; j
< GEANY_BCS_COUNT
; ++j
)
265 if (cl
[i
][j
] != NULL
&& *(cl
[i
][j
]) != NULL
&& (*(cl
[i
][j
]))[k
].exists
)
267 for (n
= 0; n
< GEANY_BC_CMDENTRIES_COUNT
; n
++)
269 if ((*(cl
[i
][j
]))[k
].entries
[i
] != NULL
)
270 printf("%c %*.*s",c
,cc
[j
],cc
[j
],(*(cl
[i
][j
]))[k
].entries
[i
]);
272 printf("%c %*.*s",c
,cc
[j
],cc
[j
]," ");
276 printf("%c %*.*s",c
,cc
[j
],cc
[j
]," ");
287 /* macros to save typing and make the logic visible */
288 #define return_cmd_if(src, cmds) if (cmds != NULL && cmds[cmdindex].exists && below>src)\
291 if (printbuildcmds) \
292 printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
293 return &(cmds[cmdindex]); \
295 #define return_ft_cmd_if(src, cmds) if (ft != NULL && ft->cmds != NULL \
296 && ft->cmds[cmdindex].exists && below>src)\
299 if (printbuildcmds) \
300 printf("cmd[%d,%d]=%d\n",cmdgrp,cmdindex,src); \
301 return &(ft->cmds[cmdindex]); \
305 /* get the next lowest command taking priority into account */
306 static GeanyBuildCommand
*get_next_build_cmd(GeanyDocument
*doc
, gint cmdgrp
, gint cmdindex
,
307 gint below
, gint
*from
)
309 /* Note: parameter below used in macros above */
311 GeanyFiletype
*ft
= NULL
;
312 gint sink
, *fr
= &sink
;
316 if (cmdgrp
>= GEANY_GBG_COUNT
)
321 doc
= document_get_current();
326 case GEANY_GBG_FT
: /* order proj ft, home ft, ft, defft */
329 return_ft_cmd_if(GEANY_BCS_PROJ
, projfilecmds
);
330 return_ft_cmd_if(GEANY_BCS_PREF
, homefilecmds
);
331 return_ft_cmd_if(GEANY_BCS_FT
, filecmds
);
333 return_cmd_if(GEANY_BCS_DEF
, ft_def
);
335 case GEANY_GBG_NON_FT
: /* order proj, pref, def */
336 return_cmd_if(GEANY_BCS_PROJ
, non_ft_proj
);
337 return_cmd_if(GEANY_BCS_PREF
, non_ft_pref
);
338 return_ft_cmd_if(GEANY_BCS_FT
, ftdefcmds
);
339 return_cmd_if(GEANY_BCS_DEF
, non_ft_def
);
341 case GEANY_GBG_EXEC
: /* order proj, proj ft, pref, home ft, ft, def */
342 return_cmd_if(GEANY_BCS_PROJ
, exec_proj
);
343 return_ft_cmd_if(GEANY_BCS_PROJ_FT
, projexeccmds
);
344 return_cmd_if(GEANY_BCS_PREF
, exec_pref
);
345 return_ft_cmd_if(GEANY_BCS_FT
, homeexeccmds
);
346 return_ft_cmd_if(GEANY_BCS_FT
, execcmds
);
347 return_cmd_if(GEANY_BCS_DEF
, exec_def
);
356 /* shortcut to start looking at the top */
357 static GeanyBuildCommand
*get_build_cmd(GeanyDocument
*doc
, gint grp
, gint cmdindex
, gint
*from
)
359 return get_next_build_cmd(doc
, grp
, cmdindex
, GEANY_BCS_COUNT
, from
);
363 #define return_nonblank_regex(src, ptr) if (NZV(ptr)) \
364 { *fr = (src); return &(ptr); }
367 /* like get_build_cmd, but for regexen, used by filetypes */
368 gchar
**build_get_regex(GeanyBuildGroup grp
, GeanyFiletype
*ft
, gint
*from
)
370 gint sink
, *fr
= &sink
;
373 if (grp
== GEANY_GBG_FT
)
377 GeanyDocument
*doc
= document_get_current();
383 return_nonblank_regex(GEANY_BCS_PROJ
, ft
->projerror_regex_string
);
384 return_nonblank_regex(GEANY_BCS_HOME_FT
, ft
->homeerror_regex_string
);
385 return_nonblank_regex(GEANY_BCS_FT
, ft
->error_regex_string
);
387 else if (grp
== GEANY_GBG_NON_FT
)
389 return_nonblank_regex(GEANY_BCS_PROJ
, regex_proj
);
390 return_nonblank_regex(GEANY_BCS_PREF
, regex_pref
);
396 /* get pointer to the command group array */
397 static GeanyBuildCommand
*get_build_group(GeanyBuildSource src
, GeanyBuildGroup grp
)
400 GeanyFiletype
*ft
= NULL
;
405 if ((doc
= document_get_current()) == NULL
)
407 if ((ft
= doc
->file_type
) == NULL
)
412 case GEANY_BCS_DEF
: return ft
->ftdefcmds
;
413 case GEANY_BCS_FT
: return ft
->filecmds
;
414 case GEANY_BCS_HOME_FT
: return ft
->homefilecmds
;
415 case GEANY_BCS_PREF
: return ft
->homefilecmds
;
416 case GEANY_BCS_PROJ
: return ft
->projfilecmds
;
417 default: return NULL
;
420 case GEANY_GBG_NON_FT
:
423 case GEANY_BCS_DEF
: return non_ft_def
;
424 case GEANY_BCS_PREF
: return non_ft_pref
;
425 case GEANY_BCS_PROJ
: return non_ft_proj
;
426 default: return NULL
;
430 if ((doc
= document_get_current()) != NULL
)
434 case GEANY_BCS_DEF
: return exec_def
;
435 case GEANY_BCS_FT
: return ft
? ft
->execcmds
: NULL
;
436 case GEANY_BCS_HOME_FT
: return ft
? ft
->homeexeccmds
: NULL
;
437 case GEANY_BCS_PROJ_FT
: return ft
? ft
->projexeccmds
: NULL
;
438 case GEANY_BCS_PREF
: return exec_pref
;
439 case GEANY_BCS_PROJ
: return exec_proj
;
440 default: return NULL
;
449 /** Remove the specified Build menu item.
451 * Makes the specified menu item configuration no longer exist. This
452 * is different to setting fields to blank because the menu item
453 * will be deleted from the configuration file on saving
454 * (except the system filetypes settings @see Build Menu Configuration
455 * section of the Manual).
457 * @param src the source of the menu item to remove.
458 * @param grp the group of the command to remove.
459 * @param cmd the index (from 0) of the command within the group. A negative
460 * value will remove the whole group.
462 * If any parameter is out of range does nothing.
464 * @see build_menu_update
466 void build_remove_menu_item(GeanyBuildSource src
, GeanyBuildGroup grp
, gint cmd
)
468 GeanyBuildCommand
*bc
;
470 bc
= get_build_group(src
, grp
);
475 for (i
= 0; i
< build_groups_count
[grp
]; ++i
)
476 bc
[i
].exists
= FALSE
;
478 else if (cmd
< build_groups_count
[grp
])
479 bc
[cmd
].exists
= FALSE
;
483 /** Get the @a GeanyBuildCommand structure for the specified Build menu item.
485 * Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
486 * hidden by higher priority commands.
488 * @param src the source of the specified menu item.
489 * @param grp the group of the specified menu item.
490 * @param cmd the index of the command within the group.
492 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
493 * This is a pointer to an internal structure and must not be freed.
495 * @see build_menu_update
497 GeanyBuildCommand
*build_get_menu_item(GeanyBuildSource src
, GeanyBuildGroup grp
, gint cmd
)
499 GeanyBuildCommand
*bc
;
500 if (src
>= GEANY_BCS_COUNT
|| grp
>= GEANY_GBG_COUNT
|| cmd
>= build_groups_count
[grp
])
502 bc
= get_build_group(src
, grp
);
509 /** Get the @a GeanyBuildCommand structure for the menu item.
511 * Get the current highest priority command specified by @a grp and @a cmd. This is the one
512 * that the menu item will use if activated.
514 * @param grp the group of the specified menu item.
515 * @param cmd the index of the command within the group.
516 * @param src pointer to @a gint to return which source provided the command. Ignored if @a NULL.
517 * Values are one of @a GeanyBuildSource but returns a signed type not the enum.
519 * @return a pointer to the @a GeanyBuildCommand structure or @a NULL if it doesn't exist.
520 * This is a pointer to an internal structure and must not be freed.
522 * @see build_menu_update
524 /* parameter checked version of get_build_cmd for external interface */
525 GeanyBuildCommand
*build_get_current_menu_item(GeanyBuildGroup grp
, gint cmd
, gint
*src
)
527 if (*src
>= GEANY_BCS_COUNT
|| grp
>= GEANY_GBG_COUNT
|| cmd
>= build_groups_count
[grp
])
529 return get_build_cmd(NULL
, grp
, cmd
, src
);
533 /* Clear all error indicators in all documents. */
534 static void clear_errors(GeanyDocument
*doc
)
538 for (i
= 0; i
< documents_array
->len
; i
++)
540 if (documents
[i
]->is_valid
)
541 editor_indicator_clear_errors(documents
[i
]->editor
);
547 static void parse_build_output(const gchar
**output
, gint status
)
550 gchar
*line
, **lines
;
552 for (x
= 0; x
< 2; x
++)
556 lines
= g_strsplit_set(output
[x
], "\r\n", -1);
557 len
= g_strv_length(lines
);
559 for (i
= 0; i
< len
; i
++)
564 while (*line
!= '\0')
565 { /* replace any conrol characters in the output */
570 process_build_output_line(lines
[i
], COLOR_BLACK
);
577 show_build_result_message(status
!= 0);
581 /* enable build items again */
582 build_menu_update(NULL
);
587 /* Replaces occurences of %e and %p with the appropriate filenames,
588 * %d and %p replacements should be in UTF8 */
589 static gchar
*build_replace_placeholder(const GeanyDocument
*doc
, const gchar
*src
)
592 gchar
*filename
= NULL
;
594 gchar
*executable
= NULL
;
595 gchar
*ret_str
; /* to be freed when not in use anymore */
597 stack
= g_string_new(src
);
598 if (doc
!= NULL
&& doc
->file_name
!= NULL
)
600 filename
= utils_get_utf8_from_locale(doc
->file_name
);
602 /* replace %f with the filename (including extension) */
603 replacement
= g_path_get_basename(filename
);
604 utils_string_replace_all(stack
, "%f", replacement
);
607 /* replace %d with the absolute path of the dir of the current file */
608 replacement
= g_path_get_dirname(filename
);
609 utils_string_replace_all(stack
, "%d", replacement
);
612 /* replace %e with the filename (excluding extension) */
613 executable
= utils_remove_ext_from_filename(filename
);
614 replacement
= g_path_get_basename(executable
);
615 utils_string_replace_all(stack
, "%e", replacement
);
619 /* replace %p with the current project's (absolute) base directory */
620 replacement
= NULL
; /* prevent double free if no replacement found */
623 replacement
= project_get_base_path();
625 else if (strstr(stack
->str
, "%p"))
626 { /* fall back to %d */
627 ui_set_statusbar(FALSE
, _("failed to substitute %%p, no project active"));
628 if (doc
!= NULL
&& filename
!= NULL
)
629 replacement
= g_path_get_dirname(filename
);
632 utils_string_replace_all(stack
, "%p", replacement
);
635 ret_str
= utils_get_utf8_from_locale(stack
->str
);
638 g_string_free(stack
, TRUE
);
640 return ret_str
; /* don't forget to free src also if needed */
644 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
645 * idx document directory */
646 static GPid
build_spawn_cmd(GeanyDocument
*doc
, const gchar
*cmd
, const gchar
*dir
)
648 GError
*error
= NULL
;
651 gchar
*utf8_working_dir
;
653 gchar
*utf8_cmd_string
;
662 if (!((doc
!= NULL
&& NZV(doc
->file_name
)) || NZV(dir
)))
664 geany_debug("Failed to run command with no working directory");
665 ui_set_statusbar(TRUE
, _("Process failed, no working directory"));
671 setptr(current_dir_entered
, NULL
);
673 cmd_string
= g_strdup(cmd
);
676 argv
= g_strsplit(cmd_string
, " ", 0);
678 argv
= g_new0(gchar
*, 4);
679 argv
[0] = g_strdup("/bin/sh");
680 argv
[1] = g_strdup("-c");
681 argv
[2] = cmd_string
;
685 utf8_cmd_string
= utils_get_utf8_from_locale(cmd_string
);
686 utf8_working_dir
= NZV(dir
) ? g_strdup(dir
) : g_path_get_dirname(doc
->file_name
);
687 working_dir
= utils_get_locale_from_utf8(utf8_working_dir
);
689 gtk_list_store_clear(msgwindow
.store_compiler
);
690 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
691 msgwin_compiler_add(COLOR_BLUE
, _("%s (in directory: %s)"), utf8_cmd_string
, utf8_working_dir
);
692 g_free(utf8_working_dir
);
693 g_free(utf8_cmd_string
);
695 /* set the build info for the message window */
696 g_free(build_info
.dir
);
697 build_info
.dir
= g_strdup(working_dir
);
698 build_info
.file_type_id
= (doc
== NULL
) ? GEANY_FILETYPES_NONE
: FILETYPE_ID(doc
->file_type
);
699 build_info
.message_count
= 0;
702 if (! utils_spawn_sync(working_dir
, argv
, NULL
, G_SPAWN_SEARCH_PATH
,
703 NULL
, NULL
, &output
[0], &output
[1], &status
, &error
))
705 if (! g_spawn_async_with_pipes(working_dir
, argv
, NULL
,
706 G_SPAWN_SEARCH_PATH
| G_SPAWN_DO_NOT_REAP_CHILD
, NULL
, NULL
,
707 &(build_info
.pid
), NULL
, &stdout_fd
, &stderr_fd
, &error
))
710 geany_debug("g_spawn_async_with_pipes() failed: %s", error
->message
);
711 ui_set_statusbar(TRUE
, _("Process failed (%s)"), error
->message
);
720 parse_build_output((const gchar
**) output
, status
);
724 if (build_info
.pid
> 0)
726 g_child_watch_add(build_info
.pid
, (GChildWatchFunc
) build_exit_cb
, NULL
);
727 build_menu_update(doc
);
728 ui_progress_bar_start(NULL
);
731 /* use GIOChannels to monitor stdout and stderr */
732 utils_set_up_io_channel(stdout_fd
, G_IO_IN
| G_IO_PRI
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
,
733 TRUE
, build_iofunc
, GINT_TO_POINTER(0));
734 utils_set_up_io_channel(stderr_fd
, G_IO_IN
| G_IO_PRI
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
,
735 TRUE
, build_iofunc
, GINT_TO_POINTER(1));
741 return build_info
.pid
;
745 /* Returns: NULL if there was an error, or the working directory the script was created in.
746 * vte_cmd_nonscript is the location of a string which is filled with the command to be used
747 * when vc->skip_run_script is set, otherwise it will be set to NULL */
748 static gchar
*prepare_run_script(GeanyDocument
*doc
, gchar
**vte_cmd_nonscript
, gint cmdindex
)
750 gchar
*locale_filename
= NULL
;
751 gboolean have_project
;
752 GeanyProject
*project
= app
->project
;
753 GeanyBuildCommand
*cmd
= NULL
;
754 gchar
*executable
= NULL
;
755 gchar
*working_dir
= NULL
, *cmd_working_dir
;
756 gboolean autoclose
= FALSE
;
757 gboolean result
= FALSE
;
761 if (vte_cmd_nonscript
!= NULL
)
762 *vte_cmd_nonscript
= NULL
;
764 locale_filename
= utils_get_locale_from_utf8(doc
->file_name
);
766 have_project
= project
!= NULL
;
767 cmd
= get_build_cmd(doc
, GEANY_GBG_EXEC
, cmdindex
, NULL
);
769 cmd_string
= build_replace_placeholder(doc
, buildcmd_cmd(cmd
));
770 cmd_working_dir
= buildcmd_working_dir(cmd
);
771 if (! NZV(cmd_working_dir
))
772 cmd_working_dir
= "%d";
773 working_dir
= build_replace_placeholder(doc
, cmd_working_dir
); /* in utf-8 */
775 /* only test whether working dir exists, don't change it or else Windows support will break
776 * (gspawn-win32-helper.exe is used by GLib and must be in $PATH which means current working
777 * dir where geany.exe was started from, so we can't change it) */
778 if (!NZV(working_dir
) || ! g_file_test(working_dir
, G_FILE_TEST_EXISTS
) ||
779 ! g_file_test(working_dir
, G_FILE_TEST_IS_DIR
))
781 ui_set_statusbar(TRUE
, _("Failed to change the working directory to \"%s\""),
782 NZV(working_dir
) ? working_dir
: "<NULL>" );
783 utils_free_pointers(2, cmd_string
, working_dir
, NULL
);
788 if (vte_info
.load_vte
&& vc
!= NULL
&& vc
->run_in_vte
)
790 if (vc
->skip_run_script
)
792 if (vte_cmd_nonscript
!= NULL
)
793 *vte_cmd_nonscript
= cmd_string
;
795 utils_free_pointers(2, executable
, locale_filename
, NULL
);
799 /* don't wait for user input at the end of script when we are running in VTE */
804 /* RUN_SCRIPT_CMD should be ok in UTF8 without converting in locale because it
805 * contains no umlauts */
806 tmp
= g_build_filename(working_dir
, RUN_SCRIPT_CMD
, NULL
);
807 result
= build_create_shellscript(tmp
, cmd_string
, autoclose
);
810 ui_set_statusbar(TRUE
, _("Failed to execute \"%s\" (start-script could not be created)"),
811 NZV(cmd_string
) ? cmd_string
: NULL
);
814 utils_free_pointers(4, cmd_string
, tmp
, executable
, locale_filename
, NULL
);
824 static GPid
build_run_cmd(GeanyDocument
*doc
, gint cmdindex
)
827 gchar
*vte_cmd_nonscript
= NULL
;
828 GError
*error
= NULL
;
830 if (doc
== NULL
|| doc
->file_name
== NULL
)
833 working_dir
= prepare_run_script(doc
, &vte_cmd_nonscript
, cmdindex
);
834 if (working_dir
== NULL
)
837 run_info
[cmdindex
].file_type_id
= FILETYPE_ID(doc
->file_type
);
840 if (vte_info
.load_vte
&& vc
!= NULL
&& vc
->run_in_vte
)
844 if (vc
->skip_run_script
)
846 setptr(vte_cmd_nonscript
, utils_get_utf8_from_locale(vte_cmd_nonscript
));
847 vte_cmd
= g_strconcat(vte_cmd_nonscript
, "\n", NULL
);
848 g_free(vte_cmd_nonscript
);
851 vte_cmd
= g_strconcat("\n/bin/sh ", RUN_SCRIPT_CMD
, "\n", NULL
);
853 /* change into current directory if it is not done by default */
854 if (! vc
->follow_path
)
856 /* we need to convert the working_dir back to UTF-8 because the VTE expects it */
857 gchar
*utf8_working_dir
= utils_get_utf8_from_locale(working_dir
);
858 vte_cwd(utf8_working_dir
, TRUE
);
859 g_free(utf8_working_dir
);
861 if (! vte_send_cmd(vte_cmd
))
863 ui_set_statusbar(FALSE
,
864 _("Could not execute the file in the VTE because it probably contains a command."));
865 geany_debug("Could not execute the file in the VTE because it probably contains a command.");
869 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
870 gtk_widget_grab_focus(vc
->vte
);
871 msgwin_show_hide(TRUE
);
873 run_info
[cmdindex
].pid
= 1;
880 gchar
*locale_term_cmd
= NULL
;
881 gchar
**term_argv
= NULL
;
882 guint term_argv_len
, i
;
885 /* get the terminal path */
886 locale_term_cmd
= utils_get_locale_from_utf8(tool_prefs
.term_cmd
);
887 /* split the term_cmd, so arguments will work too */
888 term_argv
= g_strsplit(locale_term_cmd
, " ", -1);
889 term_argv_len
= g_strv_length(term_argv
);
891 /* check that terminal exists (to prevent misleading error messages) */
892 if (term_argv
[0] != NULL
)
894 gchar
*tmp
= term_argv
[0];
895 /* g_find_program_in_path checks whether tmp exists and is executable */
896 term_argv
[0] = g_find_program_in_path(tmp
);
899 if (term_argv
[0] == NULL
)
901 ui_set_statusbar(TRUE
,
902 _("Could not find terminal \"%s\" "
903 "(check path for Terminal tool setting in Preferences)"), tool_prefs
.term_cmd
);
904 run_info
[cmdindex
].pid
= (GPid
) 1;
908 argv
= g_new0(gchar
*, term_argv_len
+ 3);
909 for (i
= 0; i
< term_argv_len
; i
++)
911 argv
[i
] = g_strdup(term_argv
[i
]);
914 /* command line arguments only for cmd.exe */
915 if (strstr(argv
[0], "cmd.exe") != NULL
)
917 argv
[term_argv_len
] = g_strdup("/Q /C");
918 argv
[term_argv_len
+ 1] = g_strdup(RUN_SCRIPT_CMD
);
922 argv
[term_argv_len
] = g_strdup(RUN_SCRIPT_CMD
);
923 argv
[term_argv_len
+ 1] = NULL
;
926 argv
[term_argv_len
] = g_strdup("-e");
927 argv
[term_argv_len
+ 1] = g_strconcat("/bin/sh ", RUN_SCRIPT_CMD
, NULL
);
929 argv
[term_argv_len
+ 2] = NULL
;
931 if (! g_spawn_async(working_dir
, argv
, NULL
, G_SPAWN_DO_NOT_REAP_CHILD
,
932 NULL
, NULL
, &(run_info
[cmdindex
].pid
), &error
))
934 geany_debug("g_spawn_async() failed: %s", error
->message
);
935 ui_set_statusbar(TRUE
, _("Process failed (%s)"), error
->message
);
936 g_unlink(RUN_SCRIPT_CMD
);
939 run_info
[cmdindex
].pid
= (GPid
) 0;
942 if (run_info
[cmdindex
].pid
> 0)
944 g_child_watch_add(run_info
[cmdindex
].pid
, (GChildWatchFunc
) run_exit_cb
,
945 (gpointer
)&(run_info
[cmdindex
]));
946 build_menu_update(doc
);
950 g_strfreev(term_argv
);
951 g_free(locale_term_cmd
);
955 return run_info
[cmdindex
].pid
;
959 static void process_build_output_line(const gchar
*str
, gint color
)
970 if (build_info
.message_count
< GEANY_BUILD_ERR_HIGHLIGHT_MAX
)
975 build_info
.message_count
++;
977 if (build_parse_make_dir(msg
, &tmp
))
979 setptr(current_dir_entered
, tmp
);
981 msgwin_parse_compiler_error_line(msg
, current_dir_entered
, &filename
, &line
);
982 if (line
!= -1 && filename
!= NULL
)
984 GeanyDocument
*doc
= document_find_by_filename(filename
);
986 if (doc
&& editor_prefs
.use_indicators
)
988 if (line
> 0) /* some compilers, like pdflatex report errors on line 0 */
989 line
--; /* so only adjust the line number if it is greater than 0 */
990 editor_indicator_set_on_line(doc
->editor
, GEANY_INDICATOR_ERROR
, line
);
992 color
= COLOR_RED
; /* error message parsed on the line */
996 msgwin_compiler_add_string(color
, msg
);
1003 static gboolean
build_iofunc(GIOChannel
*ioc
, GIOCondition cond
, gpointer data
)
1005 if (cond
& (G_IO_IN
| G_IO_PRI
))
1009 while (g_io_channel_read_line(ioc
, &msg
, NULL
, NULL
, NULL
) && msg
)
1011 gint color
= (GPOINTER_TO_INT(data
)) ? COLOR_DARK_RED
: COLOR_BLACK
;
1013 process_build_output_line(msg
, color
);
1017 if (cond
& (G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
))
1025 gboolean
build_parse_make_dir(const gchar
*string
, gchar
**prefix
)
1034 if ((pos
= strstr(string
, "Entering directory")) != NULL
)
1039 /* get the start of the path */
1040 pos
= strstr(string
, "/");
1045 input
= g_strdup(pos
);
1047 /* kill the ' at the end of the path */
1048 len
= strlen(input
);
1049 input
[len
- 1] = '\0';
1050 input
= g_realloc(input
, len
); /* shorten by 1 */
1056 if (strstr(string
, "Leaving directory") != NULL
)
1066 static void show_build_result_message(gboolean failure
)
1072 msg
= _("Compilation failed.");
1073 msgwin_compiler_add_string(COLOR_BLUE
, msg
);
1074 /* If msgwindow is hidden, user will want to display it to see the error */
1075 if (! ui_prefs
.msgwindow_visible
)
1077 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
1078 msgwin_show_hide(TRUE
);
1081 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow
.notebook
)) != MSG_COMPILER
)
1082 ui_set_statusbar(FALSE
, "%s", msg
);
1086 msg
= _("Compilation finished successfully.");
1087 msgwin_compiler_add_string(COLOR_BLUE
, msg
);
1088 if (! ui_prefs
.msgwindow_visible
||
1089 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow
.notebook
)) != MSG_COMPILER
)
1090 ui_set_statusbar(FALSE
, "%s", msg
);
1096 static void build_exit_cb(GPid child_pid
, gint status
, gpointer user_data
)
1098 gboolean failure
= FALSE
;
1100 if (WIFEXITED(status
))
1102 if (WEXITSTATUS(status
) != EXIT_SUCCESS
)
1105 else if (WIFSIGNALED(status
))
1107 /* the terminating signal: WTERMSIG (status)); */
1111 { /* any other failure occured */
1114 show_build_result_message(failure
);
1117 g_spawn_close_pid(child_pid
);
1120 /* enable build items again */
1121 build_menu_update(NULL
);
1122 ui_progress_bar_stop();
1127 static void run_exit_cb(GPid child_pid
, gint status
, gpointer user_data
)
1129 RunInfo
*run_info_data
= (RunInfo
*)user_data
;
1131 g_spawn_close_pid(child_pid
);
1133 run_info_data
->pid
= 0;
1134 /* reset the stop button and menu item to the original meaning */
1135 build_menu_update(NULL
);
1139 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1140 * fname is the full file name (including path) for the script to create */
1141 static gboolean
build_create_shellscript(const gchar
*fname
, const gchar
*cmd
, gboolean autoclose
)
1146 gchar
*expanded_cmd
;
1149 fp
= g_fopen(fname
, "w");
1153 /* Expand environment variables like %blah%. */
1154 expanded_cmd
= win32_expand_environment_variables(cmd
);
1155 str
= g_strdup_printf("%s\n\n%s\ndel \"%%0\"\n\npause\n", expanded_cmd
, (autoclose
) ? "" : "pause");
1156 g_free(expanded_cmd
);
1158 str
= g_strdup_printf(
1159 "#!/bin/sh\n\nrm $0\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1160 \n\n%s\n", cmd
, (autoclose
) ? "" :
1161 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1162 "dash\ndummy_var=\"\"\nread dummy_var");
1174 typedef void Callback(GtkWidget
*w
, gpointer u
);
1176 /* run the command catenating cmd_cat if present */
1177 static void build_command(GeanyDocument
*doc
, GeanyBuildGroup grp
, gint cmd
, gchar
*cmd_cat
)
1180 gchar
*full_command
, *subs_command
;
1181 GeanyBuildCommand
*buildcmd
= get_build_cmd(doc
, grp
, cmd
, NULL
);
1184 if (buildcmd
== NULL
)
1187 cmdstr
= buildcmd_cmd(buildcmd
);
1189 if (cmd_cat
!= NULL
)
1192 full_command
= g_strconcat(cmdstr
, cmd_cat
, NULL
);
1194 full_command
= g_strdup(cmd_cat
);
1197 full_command
= cmdstr
;
1199 dir
= build_replace_placeholder(doc
, buildcmd_working_dir(buildcmd
));
1200 subs_command
= build_replace_placeholder(doc
, full_command
);
1201 build_info
.grp
= grp
;
1202 build_info
.cmd
= cmd
;
1203 build_spawn_cmd(doc
, subs_command
, dir
);
1204 g_free(subs_command
);
1206 if (cmd_cat
!= NULL
)
1207 g_free(full_command
);
1208 build_menu_update(doc
);
1213 /*----------------------------------------------------------------
1215 * Create build menu and handle callbacks (&toolbar callbacks)
1217 *----------------------------------------------------------------*/
1218 static void on_make_custom_input_response(const gchar
*input
)
1220 GeanyDocument
*doc
= document_get_current();
1221 setptr(build_info
.custom_target
, g_strdup(input
));
1222 build_command(doc
, GBO_TO_GBG(GEANY_GBO_CUSTOM
), GBO_TO_CMD(GEANY_GBO_CUSTOM
),
1223 build_info
.custom_target
);
1227 static void on_build_menu_item(GtkWidget
*w
, gpointer user_data
)
1229 GeanyDocument
*doc
= document_get_current();
1230 GeanyBuildCommand
*bc
;
1231 gint grp
= GPOINTER_TO_GRP(user_data
);
1232 gint cmd
= GPOINTER_TO_CMD(user_data
);
1234 if (doc
&& doc
->changed
)
1235 document_save_file(doc
, FALSE
);
1236 if (grp
== GEANY_GBG_NON_FT
&& cmd
== GBO_TO_CMD(GEANY_GBO_CUSTOM
))
1238 static GtkWidget
*dialog
= NULL
; /* keep dialog for combo history */
1242 dialog
= dialogs_show_input_persistent(_("Custom Text"),
1243 _("Enter custom text here, all entered text is appended to the command."),
1244 build_info
.custom_target
, &on_make_custom_input_response
);
1248 gtk_widget_show(dialog
);
1252 else if (grp
== GEANY_GBG_EXEC
)
1254 if (run_info
[cmd
].pid
> (GPid
) 1)
1256 kill_process(&run_info
[cmd
].pid
);
1259 bc
= get_build_cmd(doc
, grp
, cmd
, NULL
);
1260 if (bc
!= NULL
&& strcmp(buildcmd_cmd(bc
), "builtin") == 0)
1265 uri
= g_strconcat("file:///", g_path_skip_root(doc
->file_name
), NULL
);
1266 utils_open_browser(uri
);
1271 build_run_cmd(doc
, cmd
);
1274 build_command(doc
, grp
, cmd
, NULL
);
1278 /* group codes for menu items other than the known commands
1279 * value order is important, see the following table for use */
1281 /* the rest in each group */
1282 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1283 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1284 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1286 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1287 /* the fixed items */
1288 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1289 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1290 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1291 #define MENU_DONE (MENU_COMMANDS + 1)
1294 static struct BuildMenuItemSpec
{
1295 const gchar
*stock_id
;
1296 const gint key_binding
;
1297 const gint build_grp
;
1298 const gint build_cmd
;
1299 const gchar
*fix_label
;
1301 } build_menu_specs
[] = {
1302 { GTK_STOCK_CONVERT
, GEANY_KEYS_BUILD_COMPILE
, GBO_TO_GBG(GEANY_GBO_COMPILE
),
1303 GBO_TO_CMD(GEANY_GBO_COMPILE
), NULL
, on_build_menu_item
},
1304 { GEANY_STOCK_BUILD
, GEANY_KEYS_BUILD_LINK
, GBO_TO_GBG(GEANY_GBO_BUILD
),
1305 GBO_TO_CMD(GEANY_GBO_BUILD
), NULL
, on_build_menu_item
},
1306 { NULL
, -1, MENU_FT_REST
,
1307 GBO_TO_CMD(GEANY_GBO_BUILD
) + 1, NULL
, on_build_menu_item
},
1308 { NULL
, -1, MENU_SEPARATOR
,
1309 GBF_SEP_1
, NULL
, NULL
},
1310 { NULL
, GEANY_KEYS_BUILD_MAKE
, GBO_TO_GBG(GEANY_GBO_MAKE_ALL
),
1311 GBO_TO_CMD(GEANY_GBO_MAKE_ALL
), NULL
, on_build_menu_item
},
1312 { NULL
, GEANY_KEYS_BUILD_MAKEOWNTARGET
, GBO_TO_GBG(GEANY_GBO_CUSTOM
),
1313 GBO_TO_CMD(GEANY_GBO_CUSTOM
), NULL
, on_build_menu_item
},
1314 { NULL
, GEANY_KEYS_BUILD_MAKEOBJECT
, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT
),
1315 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
), NULL
, on_build_menu_item
},
1316 { NULL
, -1, MENU_NON_FT_REST
,
1317 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
) + 1, NULL
, on_build_menu_item
},
1318 { NULL
, -1, MENU_SEPARATOR
,
1319 GBF_SEP_2
, NULL
, NULL
},
1320 { NULL
, GEANY_KEYS_BUILD_NEXTERROR
, MENU_NEXT_ERROR
,
1321 GBF_NEXT_ERROR
, N_("_Next Error"), on_build_next_error
},
1322 { NULL
, GEANY_KEYS_BUILD_PREVIOUSERROR
, MENU_PREV_ERROR
,
1323 GBF_PREV_ERROR
, N_("_Previous Error"), on_build_previous_error
},
1324 { NULL
, -1, MENU_SEPARATOR
,
1325 GBF_SEP_3
, NULL
, NULL
},
1326 { GTK_STOCK_EXECUTE
, GEANY_KEYS_BUILD_RUN
, GBO_TO_GBG(GEANY_GBO_EXEC
),
1327 GBO_TO_CMD(GEANY_GBO_EXEC
), NULL
, on_build_menu_item
},
1328 { NULL
, -1, MENU_EXEC_REST
,
1329 GBO_TO_CMD(GEANY_GBO_EXEC
) + 1, NULL
, on_build_menu_item
},
1330 { NULL
, -1, MENU_SEPARATOR
,
1331 GBF_SEP_4
, NULL
, NULL
},
1332 { GTK_STOCK_PREFERENCES
, GEANY_KEYS_BUILD_OPTIONS
, MENU_COMMANDS
,
1333 GBF_COMMANDS
, N_("_Set Build Commands"), on_set_build_commands_activate
},
1334 { NULL
, -1, MENU_DONE
,
1339 static void create_build_menu_item(GtkWidget
*menu
, GeanyKeyGroup
*group
, GtkAccelGroup
*ag
,
1340 struct BuildMenuItemSpec
*bs
, gchar
*lbl
, gint grp
, gint cmd
)
1342 GtkWidget
*item
= gtk_image_menu_item_new_with_mnemonic(lbl
);
1343 if (bs
->stock_id
!= NULL
)
1345 GtkWidget
*image
= gtk_image_new_from_stock(bs
->stock_id
, GTK_ICON_SIZE_MENU
);
1346 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
), image
);
1349 gtk_widget_show(item
);
1350 if (bs
->key_binding
>= 0)
1351 add_menu_accel(group
, bs
->key_binding
, ag
, item
);
1352 gtk_container_add(GTK_CONTAINER(menu
), item
);
1355 g_signal_connect(item
, "activate", G_CALLBACK(bs
->cb
), GRP_CMD_TO_POINTER(grp
,cmd
));
1357 menu_items
.menu_item
[grp
][cmd
] = item
;
1361 static void create_build_menu(BuildMenuItems
*build_menu_items
)
1364 GtkAccelGroup
*accel_group
= gtk_accel_group_new();
1365 GeanyKeyGroup
*keygroup
= g_ptr_array_index(keybinding_groups
, GEANY_KEY_GROUP_BUILD
);
1368 menu
= gtk_menu_new();
1369 build_menu_items
->menu_item
[GEANY_GBG_FT
] = g_new0(GtkWidget
*, build_groups_count
[GEANY_GBG_FT
]);
1370 build_menu_items
->menu_item
[GEANY_GBG_NON_FT
] = g_new0(GtkWidget
*, build_groups_count
[GEANY_GBG_NON_FT
]);
1371 build_menu_items
->menu_item
[GEANY_GBG_EXEC
] = g_new0(GtkWidget
*, build_groups_count
[GEANY_GBG_EXEC
]);
1372 build_menu_items
->menu_item
[GBG_FIXED
] = g_new0(GtkWidget
*, GBF_COUNT
);
1374 for (i
= 0; build_menu_specs
[i
].build_grp
!= MENU_DONE
; ++i
)
1376 struct BuildMenuItemSpec
*bs
= &(build_menu_specs
[i
]);
1377 if (bs
->build_grp
== MENU_SEPARATOR
)
1379 GtkWidget
*item
= gtk_separator_menu_item_new();
1380 gtk_widget_show(item
);
1381 gtk_container_add(GTK_CONTAINER(menu
), item
);
1382 build_menu_items
->menu_item
[GBG_FIXED
][bs
->build_cmd
] = item
;
1384 else if (bs
->fix_label
!= NULL
)
1386 create_build_menu_item(menu
, keygroup
, accel_group
, bs
, gettext(bs
->fix_label
),
1387 GBG_FIXED
, bs
->build_cmd
);
1389 else if (bs
->build_grp
>= MENU_FT_REST
&& bs
->build_grp
<= MENU_SEPARATOR
)
1391 gint grp
= bs
->build_grp
- GEANY_GBG_COUNT
;
1392 for (j
= bs
->build_cmd
; j
< build_groups_count
[grp
]; ++j
)
1394 GeanyBuildCommand
*bc
= get_build_cmd(NULL
, grp
, j
, NULL
);
1395 gchar
*lbl
= (bc
== NULL
) ? "" : buildcmd_label(bc
);
1396 create_build_menu_item(menu
, keygroup
, accel_group
, bs
, lbl
, grp
, j
);
1401 GeanyBuildCommand
*bc
= get_build_cmd(NULL
, bs
->build_grp
, bs
->build_cmd
, NULL
);
1402 gchar
*lbl
= (bc
== NULL
) ? "" : buildcmd_label(bc
);
1403 create_build_menu_item(menu
, keygroup
, accel_group
, bs
, lbl
, bs
->build_grp
, bs
->build_cmd
);
1406 build_menu_items
->menu
= menu
;
1407 gtk_widget_show(menu
);
1408 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_build1")), menu
);
1412 /* portability to various GTK versions needs checking
1413 * conforms to description of gtk_accel_label as child of menu item
1414 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1415 static void geany_menu_item_set_label(GtkWidget
*w
, gchar
*label
)
1417 GtkWidget
*c
= gtk_bin_get_child(GTK_BIN(w
));
1418 gtk_label_set_text_with_mnemonic(GTK_LABEL(c
), label
);
1422 /** Update the build menu to reflect changes in configuration or status.
1424 * Sets the labels and number of visible items to match the highest
1425 * priority configured commands. Also sets sensitivity if build commands are
1426 * running and switches executes to stop when commands are running.
1428 * @param doc The current document, if available, to save looking it up.
1429 * If @c NULL it will be looked up.
1431 * Call this after modifying any fields of a GeanyBuildCommand structure.
1433 * @see Build Menu Configuration section of the Manual.
1436 void build_menu_update(GeanyDocument
*doc
)
1438 gint i
, cmdcount
, cmd
, grp
;
1439 gboolean vis
= FALSE
;
1440 gboolean have_path
, build_running
, exec_running
, have_errors
, cmd_sensitivity
;
1441 gboolean can_compile
, can_build
, can_make
, run_sensitivity
= FALSE
, run_running
= FALSE
;
1442 GeanyBuildCommand
*bc
;
1444 if (menu_items
.menu
== NULL
)
1445 create_build_menu(&menu_items
);
1447 doc
= document_get_current();
1448 have_path
= doc
!= NULL
&& doc
->file_name
!= NULL
;
1449 build_running
= build_info
.pid
> (GPid
) 1;
1450 have_errors
= gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow
.store_compiler
), NULL
) > 0;
1451 for (i
= 0; build_menu_specs
[i
].build_grp
!= MENU_DONE
; ++i
)
1453 struct BuildMenuItemSpec
*bs
= &(build_menu_specs
[i
]);
1454 switch (bs
->build_grp
)
1456 case MENU_SEPARATOR
:
1459 gtk_widget_show_all(menu_items
.menu_item
[GBG_FIXED
][bs
->build_cmd
]);
1463 gtk_widget_hide_all(menu_items
.menu_item
[GBG_FIXED
][bs
->build_cmd
]);
1465 case MENU_NEXT_ERROR
:
1466 case MENU_PREV_ERROR
:
1467 gtk_widget_set_sensitive(menu_items
.menu_item
[GBG_FIXED
][bs
->build_cmd
], have_errors
);
1473 default: /* all configurable commands */
1474 if (bs
->build_grp
>= GEANY_GBG_COUNT
)
1476 grp
= bs
->build_grp
- GEANY_GBG_COUNT
;
1477 cmdcount
= build_groups_count
[grp
];
1481 grp
= bs
->build_grp
;
1482 cmdcount
= bs
->build_cmd
+ 1;
1484 for (cmd
= bs
->build_cmd
; cmd
< cmdcount
; ++cmd
)
1486 GtkWidget
*menu_item
= menu_items
.menu_item
[grp
][cmd
];
1488 bc
= get_build_cmd(doc
, grp
, cmd
, NULL
);
1490 label
= buildcmd_label(bc
);
1494 if (grp
< GEANY_GBG_EXEC
)
1497 (grp
== GEANY_GBG_FT
&& bc
!= NULL
&& have_path
&& ! build_running
) ||
1498 (grp
== GEANY_GBG_NON_FT
&& bc
!= NULL
&& ! build_running
);
1499 gtk_widget_set_sensitive(menu_item
, cmd_sensitivity
);
1500 if (bc
!= NULL
&& NZV(label
))
1502 geany_menu_item_set_label(menu_item
, label
);
1503 gtk_widget_show_all(menu_item
);
1507 gtk_widget_hide_all(menu_item
);
1512 exec_running
= run_info
[cmd
].pid
> (GPid
) 1;
1513 cmd_sensitivity
= (bc
!= NULL
) || exec_running
;
1514 gtk_widget_set_sensitive(menu_item
, cmd_sensitivity
);
1515 if (cmd
== GBO_TO_CMD(GEANY_GBO_EXEC
))
1516 run_sensitivity
= cmd_sensitivity
;
1519 image
= gtk_image_new_from_stock(bs
->stock_id
, GTK_ICON_SIZE_MENU
);
1523 image
= gtk_image_new_from_stock(GTK_STOCK_STOP
, GTK_ICON_SIZE_MENU
);
1525 if (cmd
== GBO_TO_CMD(GEANY_GBO_EXEC
))
1526 run_running
= exec_running
;
1527 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item
), image
);
1528 if (bc
!= NULL
&& NZV(label
))
1530 geany_menu_item_set_label(menu_item
, label
);
1531 gtk_widget_show_all(menu_item
);
1535 gtk_widget_hide_all(menu_item
);
1541 can_build
= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_BUILD
), NULL
) != NULL
1542 && have_path
&& ! build_running
;
1543 if (widgets
.toolitem_build
!= NULL
)
1544 gtk_widget_set_sensitive(widgets
.toolitem_build
, can_build
);
1546 if (widgets
.toolitem_make_all
!= NULL
)
1547 gtk_widget_set_sensitive(widgets
.toolitem_make_all
,
1548 (can_make
|= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_MAKE_ALL
), NULL
) != NULL
1549 && ! build_running
));
1550 if (widgets
.toolitem_make_custom
!= NULL
)
1551 gtk_widget_set_sensitive(widgets
.toolitem_make_custom
,
1552 (can_make
|= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_CUSTOM
), NULL
) != NULL
1553 && ! build_running
));
1554 if (widgets
.toolitem_make_object
!= NULL
)
1555 gtk_widget_set_sensitive(widgets
.toolitem_make_object
,
1556 (can_make
|= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
), NULL
) != NULL
1557 && ! build_running
));
1558 if (widgets
.toolitem_set_args
!= NULL
)
1559 gtk_widget_set_sensitive(widgets
.toolitem_set_args
, TRUE
);
1561 can_compile
= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_COMPILE
), NULL
) != NULL
1562 && have_path
&& ! build_running
;
1563 gtk_action_set_sensitive(widgets
.compile_action
, can_compile
);
1564 gtk_action_set_sensitive(widgets
.build_action
, can_make
);
1565 gtk_action_set_sensitive(widgets
.run_action
, run_sensitivity
);
1567 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1568 set_stop_button(run_running
);
1573 /* Call build_menu_update() instead of calling this directly. */
1574 static void set_stop_button(gboolean stop
)
1576 const gchar
*button_stock_id
= NULL
;
1577 GtkToolButton
*run_button
;
1579 run_button
= GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1580 if (run_button
!= NULL
)
1581 button_stock_id
= gtk_tool_button_get_stock_id(run_button
);
1583 if (stop
&& utils_str_equal(button_stock_id
, "gtk-stop"))
1585 if (! stop
&& utils_str_equal(button_stock_id
, "gtk-execute"))
1588 /* use the run button also as stop button */
1591 if (run_button
!= NULL
)
1592 gtk_tool_button_set_stock_id(run_button
, "gtk-stop");
1596 if (run_button
!= NULL
)
1597 gtk_tool_button_set_stock_id(run_button
, "gtk-execute");
1602 static void on_set_build_commands_activate(GtkWidget
*w
, gpointer u
)
1604 show_build_commands_dialog();
1608 static void on_toolbutton_build_activate(GtkWidget
*menuitem
, gpointer user_data
)
1610 last_toolbutton_action
= user_data
;
1611 g_object_set(widgets
.build_action
, "tooltip", _("Build the current file"), NULL
);
1612 on_build_menu_item(menuitem
, user_data
);
1616 static void on_toolbutton_make_activate(GtkWidget
*menuitem
, gpointer user_data
)
1621 last_toolbutton_action
= user_data
;
1622 grp
= GPOINTER_TO_GRP(user_data
);
1623 cmd
= GPOINTER_TO_CMD(user_data
);
1624 if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_MAKE_ALL
))
1625 msg
= _("Build the current file with Make and the default target");
1626 else if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_CUSTOM
))
1627 msg
= _("Build the current file with Make and the specified target");
1628 else if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT
))
1629 msg
= _("Compile the current file with Make");
1632 g_object_set(widgets
.build_action
, "tooltip", msg
, NULL
);
1633 on_build_menu_item(menuitem
, user_data
);
1637 static void kill_process(GPid
*pid
)
1639 /* Unix: SIGQUIT is not the best signal to use because it causes a core dump (this should not
1640 * perforce necessary for just killing a process). But we must use a signal which we can
1641 * ignore because the main process get it too, it is declared to ignore in main.c. */
1645 g_return_if_fail(*pid
!= NULL
);
1646 result
= TerminateProcess(*pid
, 0);
1647 /* TerminateProcess() returns TRUE on success, for the check below we have to convert
1648 * it to FALSE (and vice versa) */
1651 g_return_if_fail(*pid
> 1);
1652 result
= kill(*pid
, SIGQUIT
);
1656 ui_set_statusbar(TRUE
, _("Process could not be stopped (%s)."), g_strerror(errno
));
1660 build_menu_update(NULL
);
1665 static void on_build_next_error(GtkWidget
*menuitem
, gpointer user_data
)
1667 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow
.tree_compiler
),
1668 msgwin_goto_compiler_file_line
))
1670 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
1673 ui_set_statusbar(FALSE
, _("No more build errors."));
1677 static void on_build_previous_error(GtkWidget
*menuitem
, gpointer user_data
)
1679 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow
.tree_compiler
),
1680 msgwin_goto_compiler_file_line
))
1682 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
1685 ui_set_statusbar(FALSE
, _("No more build errors."));
1689 void build_toolbutton_build_clicked(GtkAction
*action
, gpointer unused
)
1691 if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_BUILD
))
1693 on_build_menu_item(NULL
, GBO_TO_POINTER(GEANY_GBO_BUILD
));
1697 on_build_menu_item(NULL
, last_toolbutton_action
);
1702 /*------------------------------------------------------
1704 * Create and handle the build menu configuration dialog
1706 *-------------------------------------------------------*/
1707 typedef struct RowWidgets
1709 GtkWidget
*entries
[GEANY_BC_CMDENTRIES_COUNT
];
1710 GeanyBuildSource src
;
1711 GeanyBuildSource dst
;
1712 GeanyBuildCommand
*cmdsrc
;
1719 static void set_build_command_entry_text(GtkWidget
*wid
, const gchar
*text
)
1721 if (GTK_IS_BUTTON(wid
))
1722 gtk_button_set_label(GTK_BUTTON(wid
), text
);
1724 gtk_entry_set_text(GTK_ENTRY(wid
), text
);
1728 static void on_clear_dialog_row(GtkWidget
*unused
, gpointer user_data
)
1730 RowWidgets
*r
= (RowWidgets
*)user_data
;
1732 enum GeanyBuildCmdEntries i
;
1733 GeanyBuildCommand
*bc
= get_next_build_cmd(NULL
, r
->grp
, r
->cmd
, r
->dst
, &src
);
1739 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1741 set_build_command_entry_text(r
->entries
[i
],
1742 id_to_str(bc
,i
) != NULL
? id_to_str(bc
,i
) : "");
1748 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1750 set_build_command_entry_text(r
->entries
[i
], "");
1757 static void on_clear_dialog_regex_row(GtkEntry
*regex
, gpointer unused
)
1759 gtk_entry_set_text(regex
,"");
1763 static void on_label_button_clicked(GtkWidget
*wid
)
1765 const gchar
*old
= gtk_button_get_label(GTK_BUTTON(wid
));
1766 gchar
*str
= dialogs_show_input(_("Set menu item label"), NULL
, old
);
1768 gtk_button_set_label(GTK_BUTTON(wid
), str
);
1773 /* Column headings, array NULL-terminated */
1774 static gchar
*colheads
[] =
1779 N_("Working directory"),
1784 #define DC_ENTRIES 1
1788 static const int entry_x_padding
= 3;
1789 static const int entry_y_padding
= 0;
1792 static RowWidgets
*build_add_dialog_row(GeanyDocument
*doc
, GtkTable
*table
, gint row
,
1793 GeanyBuildSource dst
, gint grp
, gint cmd
, gboolean dir
)
1795 GtkWidget
*label
, *clear
, *clearicon
;
1797 GeanyBuildCommand
*bc
;
1799 enum GeanyBuildCmdEntries i
;
1802 label
= gtk_label_new(g_strdup_printf("%d:", cmd
+ 1));
1803 gtk_table_attach(table
, label
, column
, column
+ 1, row
, row
+ 1, GTK_FILL
,
1804 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1805 roww
= g_new0(RowWidgets
, 1);
1806 roww
->src
= GEANY_BCS_COUNT
;
1810 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1812 gint xflags
= (i
== GEANY_BC_COMMAND
) ? GTK_FILL
| GTK_EXPAND
: GTK_FILL
;
1815 if (i
== GEANY_BC_LABEL
)
1817 GtkWidget
*wid
= roww
->entries
[i
] = gtk_button_new();
1818 gtk_button_set_use_underline(GTK_BUTTON(wid
), TRUE
);
1819 ui_widget_set_tooltip_text(wid
, _("Click to set menu item label"));
1820 g_signal_connect(wid
, "clicked", G_CALLBACK(on_label_button_clicked
), NULL
);
1823 roww
->entries
[i
] = gtk_entry_new();
1824 gtk_table_attach(table
, roww
->entries
[i
], column
, column
+ 1, row
, row
+ 1, xflags
,
1825 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1828 clearicon
= gtk_image_new_from_stock(GTK_STOCK_CLEAR
, GTK_ICON_SIZE_MENU
);
1829 clear
= gtk_button_new();
1830 gtk_button_set_image(GTK_BUTTON(clear
), clearicon
);
1831 g_signal_connect(clear
, "clicked", G_CALLBACK(on_clear_dialog_row
), roww
);
1832 gtk_table_attach(table
, clear
, column
, column
+ 1, row
, row
+ 1, GTK_FILL
,
1833 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1834 roww
->cmdsrc
= bc
= get_build_cmd(doc
, grp
, cmd
, &src
);
1838 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1841 if (bc
!= NULL
&& (str
= bc
->entries
[i
]) == NULL
)
1843 set_build_command_entry_text(roww
->entries
[i
], str
);
1845 if (bc
!= NULL
&& (src
> (gint
)dst
|| (grp
== GEANY_GBG_FT
&& (doc
== NULL
|| doc
->file_type
== NULL
))))
1847 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1848 gtk_widget_set_sensitive(roww
->entries
[i
], FALSE
);
1849 gtk_widget_set_sensitive(clear
, FALSE
);
1855 typedef struct TableFields
1858 GtkWidget
*fileregex
;
1859 GtkWidget
*nonfileregex
;
1860 gchar
**fileregexstring
;
1861 gchar
**nonfileregexstring
;
1865 GtkWidget
*build_commands_table(GeanyDocument
*doc
, GeanyBuildSource dst
, TableData
*table_data
,
1868 GtkWidget
*label
, *sep
, *clearicon
, *clear
;
1869 TableFields
*fields
;
1872 gint col
, row
, cmdindex
, cmd
;
1874 gboolean sensitivity
;
1875 gint sep_padding
= entry_y_padding
+ 3;
1877 table
= GTK_TABLE(gtk_table_new(build_items_count
+ 12, 5, FALSE
));
1878 fields
= g_new0(TableFields
, 1);
1879 fields
->rows
= g_new0(RowWidgets
*, build_items_count
);
1880 for (ch
= colheads
, col
= 0; *ch
!= NULL
; ch
++, col
++)
1882 label
= gtk_label_new(gettext(*ch
));
1883 gtk_table_attach(table
, label
, col
, col
+ 1, 0, 1,
1884 GTK_FILL
, GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1886 sep
= gtk_hseparator_new();
1887 gtk_table_attach(table
, sep
, 0, DC_N_COL
, 1, 2, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1888 entry_x_padding
, sep_padding
);
1889 if (ft
!= NULL
&& ft
->id
!= GEANY_FILETYPES_NONE
)
1890 txt
= g_strdup_printf(_("%s Commands"), ft
->title
);
1892 txt
= g_strdup_printf(_("%s Commands"), _("No Filetype"));
1893 label
= ui_label_new_bold(txt
);
1895 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1896 gtk_table_attach(table
, label
, 0, DC_N_COL
, 2, 3, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1897 entry_x_padding
, entry_y_padding
);
1898 for (row
= 3, cmdindex
= 0, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_FT
]; ++row
, ++cmdindex
, ++cmd
)
1899 fields
->rows
[cmdindex
] = build_add_dialog_row(doc
, table
, row
, dst
, GEANY_GBG_FT
, cmd
, FALSE
);
1900 label
= gtk_label_new(_("Error Regular Expression:"));
1901 gtk_table_attach(table
, label
, 0, DC_ENTRIES
+ 1, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1902 entry_x_padding
, entry_y_padding
);
1903 fields
->fileregex
= gtk_entry_new();
1904 fields
->fileregexstring
= build_get_regex(GEANY_GBG_FT
, NULL
, &src
);
1905 sensitivity
= (ft
== NULL
) ? FALSE
: TRUE
;
1906 if (fields
->fileregexstring
!= NULL
&& *(fields
->fileregexstring
) != NULL
)
1908 gtk_entry_set_text(GTK_ENTRY(fields
->fileregex
), *(fields
->fileregexstring
));
1909 if (src
> (gint
)dst
)
1910 sensitivity
= FALSE
;
1912 gtk_table_attach(table
, fields
->fileregex
, DC_ENTRIES
+ 1, DC_CLEAR
, row
, row
+ 1, GTK_FILL
,
1913 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1914 clearicon
= gtk_image_new_from_stock(GTK_STOCK_CLEAR
, GTK_ICON_SIZE_MENU
);
1915 clear
= gtk_button_new();
1916 gtk_button_set_image(GTK_BUTTON(clear
), clearicon
);
1917 g_signal_connect_swapped(clear
, "clicked",
1918 G_CALLBACK(on_clear_dialog_regex_row
), (fields
->fileregex
));
1919 gtk_table_attach(table
, clear
, DC_CLEAR
, DC_CLEAR
+ 1, row
, row
+ 1, GTK_FILL
,
1920 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1921 gtk_widget_set_sensitive(fields
->fileregex
, sensitivity
);
1922 gtk_widget_set_sensitive(clear
, sensitivity
);
1924 sep
= gtk_hseparator_new();
1925 gtk_table_attach(table
, sep
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1926 entry_x_padding
, sep_padding
);
1928 label
= ui_label_new_bold(_("Non-Filetype Commands"));
1929 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1930 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1931 entry_x_padding
, entry_y_padding
);
1932 for (++row
, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_NON_FT
]; ++row
, ++cmdindex
, ++cmd
)
1933 fields
->rows
[cmdindex
] = build_add_dialog_row(
1934 doc
, table
, row
, dst
, GEANY_GBG_NON_FT
, cmd
, TRUE
);
1935 label
= gtk_label_new(_("Error Regular Expression:"));
1936 gtk_table_attach(table
, label
, 0, DC_ENTRIES
+ 1, row
, row
+ 1, GTK_FILL
,
1937 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1938 fields
->nonfileregex
= gtk_entry_new();
1939 fields
->nonfileregexstring
= build_get_regex(GEANY_GBG_NON_FT
, NULL
, &src
);
1941 if (fields
->nonfileregexstring
!= NULL
&& *(fields
->nonfileregexstring
) != NULL
)
1943 gtk_entry_set_text(GTK_ENTRY(fields
->nonfileregex
), *(fields
->nonfileregexstring
));
1944 sensitivity
= src
> (gint
)dst
? FALSE
: TRUE
;
1946 gtk_table_attach(table
, fields
->nonfileregex
, DC_ENTRIES
+ 1, DC_CLEAR
, row
, row
+ 1, GTK_FILL
,
1947 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1948 clearicon
= gtk_image_new_from_stock(GTK_STOCK_CLEAR
, GTK_ICON_SIZE_MENU
);
1949 clear
= gtk_button_new();
1950 gtk_button_set_image(GTK_BUTTON(clear
), clearicon
);
1951 g_signal_connect_swapped(clear
, "clicked",
1952 G_CALLBACK(on_clear_dialog_regex_row
), (fields
->nonfileregex
));
1953 gtk_table_attach(table
, clear
, DC_CLEAR
, DC_CLEAR
+ 1, row
, row
+ 1, GTK_FILL
,
1954 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1955 gtk_widget_set_sensitive(fields
->nonfileregex
, sensitivity
);
1956 gtk_widget_set_sensitive(clear
, sensitivity
);
1958 label
= gtk_label_new(_("Note: Item 2 opens a dialog and appends the response to the command."));
1959 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1960 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1961 entry_x_padding
, entry_y_padding
);
1963 sep
= gtk_hseparator_new();
1964 gtk_table_attach(table
, sep
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1965 entry_x_padding
, sep_padding
);
1967 label
= ui_label_new_bold(_("Execute Commands"));
1968 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1969 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1970 entry_x_padding
, entry_y_padding
);
1971 for (++row
, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_EXEC
]; ++row
, ++cmdindex
, ++cmd
)
1972 fields
->rows
[cmdindex
] = build_add_dialog_row(doc
, table
, row
, dst
, GEANY_GBG_EXEC
, cmd
, TRUE
);
1973 sep
= gtk_hseparator_new();
1974 gtk_table_attach(table
, sep
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1975 entry_x_padding
, sep_padding
);
1977 label
= gtk_label_new(
1978 _("%d, %e, %f, %p are substituted in command and directory fields, see manual for details."));
1979 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1980 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1981 entry_x_padding
, entry_y_padding
);
1982 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
1984 *table_data
= fields
;
1985 return GTK_WIDGET(table
);
1989 void build_free_fields(TableData table_data
)
1992 for (cmdindex
= 0; cmdindex
< build_items_count
; ++cmdindex
)
1993 g_free(table_data
->rows
[cmdindex
]);
1994 g_free(table_data
->rows
);
1999 /* string compare where null pointers match null or 0 length strings */
2000 static int stcmp(const gchar
*a
, const gchar
*b
)
2002 if (a
== NULL
&& b
== NULL
)
2004 if (a
== NULL
&& b
!= NULL
)
2006 if (a
!= NULL
&& b
== NULL
)
2008 return strcmp(a
, b
);
2012 static const gchar
*get_build_command_entry_text(GtkWidget
*wid
)
2014 if (GTK_IS_BUTTON(wid
))
2015 return gtk_button_get_label(GTK_BUTTON(wid
));
2017 return gtk_entry_get_text(GTK_ENTRY(wid
));
2021 static gboolean
read_row(BuildDestination
*dst
, TableData table_data
, gint drow
, gint grp
, gint cmd
)
2023 gchar
*entries
[GEANY_BC_CMDENTRIES_COUNT
];
2024 gboolean changed
= FALSE
;
2025 GeanyBuildSource src
;
2026 enum GeanyBuildCmdEntries i
;
2028 src
= table_data
->rows
[drow
]->src
;
2030 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2032 entries
[i
] = g_strdup(get_build_command_entry_text(table_data
->rows
[drow
]->entries
[i
]));
2034 if (table_data
->rows
[drow
]->cleared
)
2036 if (dst
->dst
[grp
] != NULL
)
2038 if (*(dst
->dst
[grp
]) == NULL
)
2039 *(dst
->dst
[grp
]) = g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
2040 (*(dst
->dst
[grp
]))[cmd
].exists
= FALSE
;
2041 (*(dst
->dst
[grp
]))[cmd
].changed
= TRUE
;
2047 table_data
->rows
[drow
]->cmdsrc
== NULL
/* originally there was no content */
2050 NZV(entries
[GEANY_BC_LABEL
]) /* but now one field has some */
2051 || NZV(entries
[GEANY_BC_COMMAND
])
2052 || NZV(entries
[GEANY_BC_WORKING_DIR
])
2057 table_data
->rows
[drow
]->cmdsrc
!= NULL
/* originally there was content */
2059 ( /* and some of it was changed */
2060 stcmp(entries
[GEANY_BC_LABEL
], table_data
->rows
[drow
]->cmdsrc
->entries
[GEANY_BC_LABEL
]) != 0
2061 || stcmp(entries
[GEANY_BC_COMMAND
], table_data
->rows
[drow
]->cmdsrc
->entries
[GEANY_BC_COMMAND
]) != 0
2062 || stcmp(entries
[GEANY_BC_WORKING_DIR
],
2063 table_data
->rows
[drow
]->cmdsrc
->entries
[GEANY_BC_WORKING_DIR
]) != 0
2068 if (dst
->dst
[grp
] != NULL
)
2070 if (*(dst
->dst
[grp
]) == NULL
)
2071 *(dst
->dst
[grp
]) = g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
2072 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2073 setptr((*(dst
->dst
[grp
]))[cmd
].entries
[i
], entries
[i
]);
2074 (*(dst
->dst
[grp
]))[cmd
].exists
= TRUE
;
2075 (*(dst
->dst
[grp
]))[cmd
].changed
= TRUE
;
2081 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2088 static gboolean
read_regex(GtkWidget
*regexentry
, gchar
**src
, gchar
**dst
)
2090 gboolean changed
= FALSE
;
2091 gchar
*reg
= g_strdup(gtk_entry_get_text(GTK_ENTRY(regexentry
)));
2094 (src
== NULL
/* originally there was no regex */
2095 || *src
== NULL
/* or it was NULL*/
2097 && NZV(reg
) > 0 /* and something was typed */
2099 ||(src
!= NULL
/* originally there was a regex*/
2100 && strcmp(*src
, reg
) != 0 /* and it has been changed */
2114 gboolean
build_read_commands(BuildDestination
*dst
, TableData table_data
, gint response
)
2117 gboolean changed
= FALSE
;
2119 if (response
== GTK_RESPONSE_ACCEPT
)
2121 for (cmdindex
= 0, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_FT
]; ++cmdindex
, ++cmd
)
2122 changed
|= read_row(dst
, table_data
, cmdindex
, GEANY_GBG_FT
, cmd
);
2123 for (cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_NON_FT
]; ++cmdindex
, ++cmd
)
2124 changed
|= read_row(dst
, table_data
, cmdindex
, GEANY_GBG_NON_FT
, cmd
);
2125 for (cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_EXEC
]; ++cmdindex
, ++cmd
)
2126 changed
|= read_row(dst
, table_data
, cmdindex
, GEANY_GBG_EXEC
, cmd
);
2127 changed
|= read_regex(table_data
->fileregex
, table_data
->fileregexstring
, dst
->fileregexstr
);
2128 changed
|= read_regex(table_data
->nonfileregex
, table_data
->nonfileregexstring
, dst
->nonfileregexstr
);
2134 static void show_build_commands_dialog(void)
2136 GtkWidget
*dialog
, *table
, *vbox
;
2137 GeanyDocument
*doc
= document_get_current();
2138 GeanyFiletype
*ft
= NULL
;
2139 gchar
*title
= _("Set Build Commands");
2141 TableData table_data
;
2142 BuildDestination prefdsts
;
2145 ft
= doc
->file_type
;
2146 dialog
= gtk_dialog_new_with_buttons(title
, GTK_WINDOW(main_widgets
.window
),
2147 GTK_DIALOG_DESTROY_WITH_PARENT
,
2148 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
2149 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
, NULL
);
2150 table
= build_commands_table(doc
, GEANY_BCS_PREF
, &table_data
, ft
);
2151 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
2152 gtk_box_pack_start(GTK_BOX(vbox
), table
, TRUE
, TRUE
, 0);
2153 gtk_widget_show_all(dialog
);
2154 /* run modally to prevent user changing idx filetype */
2155 response
= gtk_dialog_run(GTK_DIALOG(dialog
));
2157 prefdsts
.dst
[GEANY_GBG_NON_FT
] = &non_ft_pref
;
2160 prefdsts
.dst
[GEANY_GBG_FT
] = &(ft
->homefilecmds
);
2161 prefdsts
.fileregexstr
= &(ft
->homeerror_regex_string
);
2162 prefdsts
.dst
[GEANY_GBG_EXEC
] = &(ft
->homeexeccmds
);
2166 prefdsts
.dst
[GEANY_GBG_FT
] = NULL
;
2167 prefdsts
.fileregexstr
= NULL
;
2168 prefdsts
.dst
[GEANY_GBG_EXEC
] = NULL
;
2170 prefdsts
.nonfileregexstr
= ®ex_pref
;
2171 if (build_read_commands(&prefdsts
, table_data
, response
) && ft
!= NULL
)
2172 ft
->home_save_needed
= TRUE
;
2173 build_free_fields(table_data
);
2175 build_menu_update(doc
);
2176 gtk_widget_destroy(dialog
);
2180 /* Creates the relevant build menu if necessary. */
2181 BuildMenuItems
*build_get_menu_items(gint filetype_idx
)
2183 BuildMenuItems
*items
;
2185 items
= &menu_items
;
2186 if (items
->menu
== NULL
)
2187 create_build_menu(items
);
2192 /* set non_ft working directory entries to %p for project */
2193 void build_set_non_ft_wd_to_proj(TableData table_data
)
2196 start
= build_groups_count
[GEANY_GBG_FT
];
2197 end
= start
+ build_groups_count
[GEANY_GBG_NON_FT
];
2198 for (i
= start
; i
< end
; ++i
)
2199 gtk_entry_set_text(GTK_ENTRY(table_data
->rows
[i
]->entries
[GEANY_BC_WORKING_DIR
]), "%p");
2203 /*----------------------------------------------------------
2205 * Load and store configuration
2207 * ---------------------------------------------------------*/
2208 static const gchar
*build_grp_name
= "build-menu";
2210 /* config format for build-menu group is prefix_gg_nn_xx=value
2211 * where gg = FT, NF, EX for the command group
2212 * nn = 2 digit command number
2213 * xx = LB for label, CM for command and WD for working dir */
2214 static const gchar
*groups
[GEANY_GBG_COUNT
] = { "FT", "NF", "EX" };
2215 static const gchar
*fixedkey
="xx_xx_xx";
2217 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2218 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2219 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2221 static void build_load_menu_grp(GKeyFile
*config
, GeanyBuildCommand
**dst
, gint grp
,
2222 gchar
*prefix
, gboolean loc
)
2224 gint cmd
, prefixlen
; /* NOTE prefixlen used in macros above */
2225 GeanyBuildCommand
*dstcmd
;
2227 static gchar cmdbuf
[3] = " ";
2230 *dst
= g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
2232 prefixlen
= prefix
== NULL
? 0 : strlen(prefix
);
2233 key
= g_strconcat(prefix
== NULL
? "" : prefix
, fixedkey
, NULL
);
2234 for (cmd
= 0; cmd
< build_groups_count
[grp
]; ++cmd
)
2237 if (cmd
< 0 || cmd
>= 100)
2238 return; /* ensure no buffer overflow */
2239 sprintf(cmdbuf
, "%02d", cmd
);
2240 set_key_grp(key
, groups
[grp
]);
2241 set_key_cmd(key
, cmdbuf
);
2242 set_key_fld(key
, "LB");
2244 label
= g_key_file_get_locale_string(config
, build_grp_name
, key
, NULL
, NULL
);
2246 label
= g_key_file_get_string(config
, build_grp_name
, key
, NULL
);
2249 dstcmd
[cmd
].exists
= TRUE
;
2250 setptr(dstcmd
[cmd
].entries
[GEANY_BC_LABEL
], label
);
2251 set_key_fld(key
,"CM");
2252 setptr(dstcmd
[cmd
].entries
[GEANY_BC_COMMAND
],
2253 g_key_file_get_string(config
, build_grp_name
, key
, NULL
));
2254 set_key_fld(key
,"WD");
2255 setptr(dstcmd
[cmd
].entries
[GEANY_BC_WORKING_DIR
],
2256 g_key_file_get_string(config
, build_grp_name
, key
, NULL
));
2258 else dstcmd
[cmd
].exists
= FALSE
;
2264 /* for the specified source load new format build menu items or try to make some sense of
2265 * old format setings, not done perfectly but better than ignoring them */
2266 void build_load_menu(GKeyFile
*config
, GeanyBuildSource src
, gpointer p
)
2272 gchar
*value
, *basedir
, *makebasedir
;
2273 gboolean bvalue
= FALSE
;
2275 if (g_key_file_has_group(config
, build_grp_name
))
2280 ft
= (GeanyFiletype
*)p
;
2283 build_load_menu_grp(config
, &(ft
->filecmds
), GEANY_GBG_FT
, NULL
, TRUE
);
2284 build_load_menu_grp(config
, &(ft
->ftdefcmds
), GEANY_GBG_NON_FT
, NULL
, TRUE
);
2285 build_load_menu_grp(config
, &(ft
->execcmds
), GEANY_GBG_EXEC
, NULL
, TRUE
);
2286 setptr(ft
->error_regex_string
,
2287 g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2289 case GEANY_BCS_HOME_FT
:
2290 ft
= (GeanyFiletype
*)p
;
2293 build_load_menu_grp(config
, &(ft
->homefilecmds
), GEANY_GBG_FT
, NULL
, FALSE
);
2294 build_load_menu_grp(config
, &(ft
->homeexeccmds
), GEANY_GBG_EXEC
, NULL
, FALSE
);
2295 setptr(ft
->homeerror_regex_string
,
2296 g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2298 case GEANY_BCS_PREF
:
2299 build_load_menu_grp(config
, &non_ft_pref
, GEANY_GBG_NON_FT
, NULL
, FALSE
);
2300 build_load_menu_grp(config
, &exec_pref
, GEANY_GBG_EXEC
, NULL
, FALSE
);
2301 setptr(regex_pref
, g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2303 case GEANY_BCS_PROJ
:
2304 build_load_menu_grp(config
, &non_ft_proj
, GEANY_GBG_NON_FT
, NULL
, FALSE
);
2305 build_load_menu_grp(config
, &exec_proj
, GEANY_GBG_EXEC
, NULL
, FALSE
);
2306 setptr(regex_proj
, g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2307 pj
= (GeanyProject
*)p
;
2310 ftlist
= g_key_file_get_string_list(config
, build_grp_name
, "filetypes", NULL
, NULL
);
2314 if (pj
->build_filetypes_list
== NULL
)
2315 pj
->build_filetypes_list
= g_ptr_array_new();
2316 g_ptr_array_set_size(pj
->build_filetypes_list
, 0);
2317 for (ftname
= ftlist
; *ftname
!= NULL
; ++ftname
)
2319 ft
= filetypes_lookup_by_name(*ftname
);
2322 gchar
*regkey
= g_strdup_printf("%serror_regex", *ftname
);
2323 g_ptr_array_add(pj
->build_filetypes_list
, ft
);
2324 setptr(ft
->projerror_regex_string
,
2325 g_key_file_get_string(config
, build_grp_name
, regkey
, NULL
));
2327 build_load_menu_grp(config
, &(ft
->projfilecmds
), GEANY_GBG_FT
, *ftname
, FALSE
);
2328 build_load_menu_grp(config
, &(ft
->projexeccmds
), GEANY_GBG_EXEC
, *ftname
, FALSE
);
2334 default: /* defaults don't load from config, see build_init */
2339 /* load old [build_settings] values if there is no value defined by [build-menu] */
2341 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2342 #define ASSIGNIF(type, id, string, value) \
2343 if (NZV(value) && ! type[GBO_TO_CMD(id)].exists) { \
2344 type[GBO_TO_CMD(id)].exists = TRUE; \
2345 setptr(type[GBO_TO_CMD(id)].entries[GEANY_BC_LABEL], g_strdup(_(string))); \
2346 setptr(type[GBO_TO_CMD(id)].entries[GEANY_BC_COMMAND], (value)); \
2347 setptr(type[GBO_TO_CMD(id)].entries[GEANY_BC_WORKING_DIR], NULL); \
2348 type[GBO_TO_CMD(id)].old = TRUE; \
2355 ft
= (GeanyFiletype
*)p
;
2356 value
= g_key_file_get_string(config
, "build_settings", "compiler", NULL
);
2359 if (ft
->filecmds
== NULL
)
2360 ft
->filecmds
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_FT
]);
2361 ASSIGNIF(ft
->filecmds
, GEANY_GBO_COMPILE
, "_Compile", value
);
2363 value
= g_key_file_get_string(config
, "build_settings", "linker", NULL
);
2366 if (ft
->filecmds
== NULL
)
2367 ft
->filecmds
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_FT
]);
2368 ASSIGNIF(ft
->filecmds
, GEANY_GBO_BUILD
, "_Build", value
);
2370 value
= g_key_file_get_string(config
, "build_settings", "run_cmd", NULL
);
2373 if (ft
->execcmds
== NULL
)
2374 ft
->execcmds
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_EXEC
]);
2375 ASSIGNIF(ft
->execcmds
, GEANY_GBO_EXEC
, "_Execute", value
);
2377 if (ft
->error_regex_string
== NULL
)
2378 ft
->error_regex_string
= g_key_file_get_string(config
, "build_settings", "error_regex", NULL
);
2380 case GEANY_BCS_PROJ
:
2381 if (non_ft_pref
== NULL
)
2382 non_ft_pref
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_NON_FT
]);
2383 basedir
= project_get_base_path();
2384 if (basedir
== NULL
)
2385 basedir
= g_strdup("%d");
2386 bvalue
= g_key_file_get_boolean(config
, "project", "make_in_base_path", NULL
);
2388 makebasedir
= g_strdup(basedir
);
2390 makebasedir
= g_strdup("%d");
2391 if (non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)].old
)
2392 setptr(non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)].entries
[GEANY_BC_WORKING_DIR
], g_strdup(makebasedir
));
2393 if (non_ft_pref
[GBO_TO_CMD(GEANY_GBO_CUSTOM
)].old
)
2394 setptr(non_ft_pref
[GBO_TO_CMD(GEANY_GBO_CUSTOM
)].entries
[GEANY_BC_WORKING_DIR
], g_strdup(makebasedir
));
2395 if (non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)].old
)
2396 setptr(non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)].entries
[GEANY_BC_WORKING_DIR
], g_strdup("%d"));
2397 value
= g_key_file_get_string(config
, "project", "run_cmd", NULL
);
2400 if (exec_proj
== NULL
)
2401 exec_proj
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_EXEC
]);
2402 if (! exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].exists
)
2404 exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].exists
= TRUE
;
2405 setptr(exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].entries
[GEANY_BC_LABEL
], g_strdup(_("_Execute")));
2406 setptr(exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].entries
[GEANY_BC_COMMAND
], value
);
2407 setptr(exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].entries
[GEANY_BC_WORKING_DIR
], g_strdup(basedir
));
2408 exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].old
= TRUE
;
2411 g_free(makebasedir
);
2414 case GEANY_BCS_PREF
:
2415 value
= g_key_file_get_string(config
, "tools", "make_cmd", NULL
);
2418 if (non_ft_pref
== NULL
)
2419 non_ft_pref
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_NON_FT
]);
2420 ASSIGNIF(non_ft_pref
, GEANY_GBO_CUSTOM
, "Make Custom _Target",
2421 g_strdup_printf("%s ", value
));
2422 ASSIGNIF(non_ft_pref
, GEANY_GBO_MAKE_OBJECT
, "Make _Object",
2423 g_strdup_printf("%s %%e.o",value
));
2424 ASSIGNIF(non_ft_pref
, GEANY_GBO_MAKE_ALL
, "_Make", value
);
2433 static gint
build_save_menu_grp(GKeyFile
*config
, GeanyBuildCommand
*src
, gint grp
, gchar
*prefix
)
2435 gint cmd
, prefixlen
; /* NOTE prefixlen used in macros above */
2438 enum GeanyBuildCmdEntries i
;
2442 prefixlen
= prefix
== NULL
? 0 : strlen(prefix
);
2443 key
= g_strconcat(prefix
== NULL
? "" : prefix
, fixedkey
, NULL
);
2444 for (cmd
= 0; cmd
< build_groups_count
[grp
]; ++cmd
)
2446 if (src
[cmd
].exists
) ++count
;
2447 if (src
[cmd
].changed
)
2449 static gchar cmdbuf
[4] = " ";
2450 if (cmd
< 0 || cmd
>= 100)
2451 return count
; /* ensure no buffer overflow */
2452 sprintf(cmdbuf
, "%02d", cmd
);
2453 set_key_grp(key
, groups
[grp
]);
2454 set_key_cmd(key
, cmdbuf
);
2455 if (src
[cmd
].exists
)
2457 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2459 set_key_fld(key
, config_keys
[i
]);
2460 g_key_file_set_string(config
, build_grp_name
, key
, src
[cmd
].entries
[i
]);
2465 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2467 set_key_fld(key
, config_keys
[i
]);
2468 g_key_file_remove_key(config
, build_grp_name
, key
, NULL
);
2478 typedef struct ForEachData
2481 GPtrArray
*ft_names
;
2485 static void foreach_project_filetype(gpointer data
, gpointer user_data
)
2487 GeanyFiletype
*ft
= (GeanyFiletype
*) data
;
2488 ForEachData
*d
= (ForEachData
*) user_data
;
2490 gchar
*regkey
= g_strdup_printf("%serror_regex", ft
->name
);
2492 i
+= build_save_menu_grp(d
->config
, ft
->projfilecmds
, GEANY_GBG_FT
, ft
->name
);
2493 i
+= build_save_menu_grp(d
->config
, ft
->projexeccmds
, GEANY_GBG_EXEC
, ft
->name
);
2494 if (NZV(ft
->projerror_regex_string
))
2496 g_key_file_set_string(d
->config
, build_grp_name
, regkey
, ft
->projerror_regex_string
);
2500 g_key_file_remove_key(d
->config
, build_grp_name
, regkey
, NULL
);
2503 g_ptr_array_add(d
->ft_names
, ft
->name
);
2507 void build_save_menu(GKeyFile
*config
, gpointer ptr
, GeanyBuildSource src
)
2516 case GEANY_BCS_HOME_FT
:
2517 ft
= (GeanyFiletype
*)ptr
;
2520 build_save_menu_grp(config
, ft
->homefilecmds
, GEANY_GBG_FT
, NULL
);
2521 build_save_menu_grp(config
, ft
->homeexeccmds
, GEANY_GBG_EXEC
, NULL
);
2522 regkey
= g_strdup_printf("%serror_regex", ft
->name
);
2523 if (NZV(ft
->homeerror_regex_string
))
2524 g_key_file_set_string(config
, build_grp_name
, regkey
, ft
->projerror_regex_string
);
2526 g_key_file_remove_key(config
, build_grp_name
, regkey
, NULL
);
2529 case GEANY_BCS_PREF
:
2530 build_save_menu_grp(config
, non_ft_pref
, GEANY_GBG_NON_FT
, NULL
);
2531 build_save_menu_grp(config
, exec_pref
, GEANY_GBG_EXEC
, NULL
);
2532 if (NZV(regex_pref
))
2533 g_key_file_set_string(config
, build_grp_name
, "error_regex", regex_pref
);
2535 g_key_file_remove_key(config
, build_grp_name
, "error_regex", NULL
);
2537 case GEANY_BCS_PROJ
:
2538 pj
= (GeanyProject
*)ptr
;
2539 build_save_menu_grp(config
, non_ft_proj
, GEANY_GBG_NON_FT
, NULL
);
2540 build_save_menu_grp(config
, exec_proj
, GEANY_GBG_EXEC
, NULL
);
2541 if (NZV(regex_proj
))
2542 g_key_file_set_string(config
, build_grp_name
, "error_regex", regex_proj
);
2544 g_key_file_remove_key(config
, build_grp_name
, "error_regex", NULL
);
2545 if (pj
->build_filetypes_list
!= NULL
)
2547 data
.config
= config
;
2548 data
.ft_names
= g_ptr_array_new();
2549 g_ptr_array_foreach(pj
->build_filetypes_list
, foreach_project_filetype
, (gpointer
)(&data
));
2550 if (data
.ft_names
->pdata
!= NULL
)
2551 g_key_file_set_string_list(config
, build_grp_name
, "filetypes",
2552 (const gchar
**)(data
.ft_names
->pdata
), data
.ft_names
->len
);
2554 g_key_file_remove_key(config
, build_grp_name
, "filetypes", NULL
);
2555 g_ptr_array_free(data
.ft_names
, TRUE
);
2558 default: /* defaults and GEANY_BCS_FT can't save */
2564 void build_set_group_count(GeanyBuildGroup grp
, gint count
)
2567 if (count
> build_groups_count
[grp
])
2568 build_groups_count
[grp
] = count
;
2569 for (i
= 0, sum
= 0; i
< GEANY_GBG_COUNT
; ++i
)
2570 sum
+= build_groups_count
[i
];
2571 build_items_count
= sum
;
2575 gint
build_get_group_count(GeanyBuildGroup grp
)
2577 return build_groups_count
[grp
];
2583 gchar
*entries
[GEANY_BC_CMDENTRIES_COUNT
];
2584 GeanyBuildCommand
**ptr
;
2586 } default_cmds
[] = {
2587 { {N_("_Make"), "make", NULL
}, &non_ft_def
, GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)},
2588 { {N_("Make Custom _Target"), "make ", NULL
}, &non_ft_def
, GBO_TO_CMD(GEANY_GBO_CUSTOM
)},
2589 { {N_("Make _Object"), "make %e.o", NULL
}, &non_ft_def
, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)},
2590 { {N_("_Execute"), "./%e", NULL
}, &exec_def
, GBO_TO_CMD(GEANY_GBO_EXEC
)},
2591 { {NULL
, NULL
, NULL
}, NULL
, 0 }
2595 void build_init(void)
2598 GtkWidget
*toolmenu
;
2601 ft_def
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_FT
]);
2602 non_ft_def
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_NON_FT
]);
2603 exec_def
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_EXEC
]);
2604 run_info
= g_new0(RunInfo
, build_groups_count
[GEANY_GBG_EXEC
]);
2605 for (cmdindex
= 0; default_cmds
[cmdindex
].entries
[GEANY_BC_COMMAND
] != NULL
; ++cmdindex
)
2607 enum GeanyBuildCmdEntries k
;
2608 GeanyBuildCommand
*cmd
= &((*(default_cmds
[cmdindex
].ptr
))[ default_cmds
[cmdindex
].index
]);
2610 for (k
= 0; k
< GEANY_BC_CMDENTRIES_COUNT
; k
++)
2612 cmd
->entries
[k
] = g_strdup(default_cmds
[cmdindex
].entries
[k
]);
2616 /* create the toolbar Build item sub menu */
2617 toolmenu
= gtk_menu_new();
2618 g_object_ref(toolmenu
);
2620 /* build the code */
2621 item
= ui_image_menu_item_new(GEANY_STOCK_BUILD
, _("_Build"));
2622 gtk_widget_show(item
);
2623 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2624 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_build_activate
),
2625 GBO_TO_POINTER(GEANY_GBO_BUILD
));
2626 widgets
.toolitem_build
= item
;
2628 item
= gtk_separator_menu_item_new();
2629 gtk_widget_show(item
);
2630 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2632 /* build the code with make all */
2633 item
= gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2634 gtk_widget_show(item
);
2635 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2636 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_make_activate
),
2637 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL
));
2638 widgets
.toolitem_make_all
= item
;
2640 /* build the code with make custom */
2641 item
= gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target"));
2642 gtk_widget_show(item
);
2643 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2644 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_make_activate
),
2645 GBO_TO_POINTER(GEANY_GBO_CUSTOM
));
2646 widgets
.toolitem_make_custom
= item
;
2648 /* build the code with make object */
2649 item
= gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2650 gtk_widget_show(item
);
2651 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2652 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_make_activate
),
2653 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT
));
2654 widgets
.toolitem_make_object
= item
;
2656 item
= gtk_separator_menu_item_new();
2657 gtk_widget_show(item
);
2658 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2661 item
= ui_image_menu_item_new(GTK_STOCK_PREFERENCES
, _("_Set Build Menu Commands"));
2662 gtk_widget_show(item
);
2663 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2664 g_signal_connect(item
, "activate", G_CALLBACK(on_set_build_commands_activate
), NULL
);
2665 widgets
.toolitem_set_args
= item
;
2668 /* get toolbar action pointers */
2669 widgets
.build_action
= toolbar_get_action_by_name("Build");
2670 widgets
.compile_action
= toolbar_get_action_by_name("Compile");
2671 widgets
.run_action
= toolbar_get_action_by_name("Run");
2672 widgets
.toolmenu
= toolmenu
;
2673 /* set the submenu to the toolbar item */
2674 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets
.build_action
), toolmenu
);