2 * build.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2009 Lex Trotman <elextr(at)gmail(dot)com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Build commands and menu items.
27 * Use intermediate pointers for common subexpressions.
28 * Replace defines with enums.
29 * Other TODOs in code. */
39 #include "filetypesprivate.h"
40 #include "geanymenubuttonaction.h"
41 #include "geanyobject.h"
42 #include "keybindingsprivate.h"
43 #include "msgwindow.h"
45 #include "projectprivate.h"
46 #include "sciwrappers.h"
55 #include "gtkcompat.h"
62 #include <glib/gstdio.h>
66 /* Number of editor indicators to draw - limited as this can affect performance */
67 #define GEANY_BUILD_ERR_HIGHLIGHT_MAX 50
70 GeanyBuildInfo build_info
= {GEANY_GBG_FT
, 0, 0, NULL
, GEANY_FILETYPES_NONE
, NULL
, 0};
72 static gchar
*current_dir_entered
= NULL
;
74 typedef struct RunInfo
80 static RunInfo
*run_info
;
83 static const gchar RUN_SCRIPT_CMD
[] = "geany_run_script_XXXXXX.bat";
85 static const gchar RUN_SCRIPT_CMD
[] = "geany_run_script_XXXXXX.sh";
88 /* pack group (<8) and command (<32) into a user_data pointer */
89 #define GRP_CMD_TO_POINTER(grp, cmd) GUINT_TO_POINTER((((grp)&7) << 5) | ((cmd)&0x1f))
90 #define GBO_TO_POINTER(gbo) (GRP_CMD_TO_POINTER(GBO_TO_GBG(gbo), GBO_TO_CMD(gbo)))
91 #define GPOINTER_TO_CMD(gptr) (GPOINTER_TO_UINT(gptr)&0x1f)
92 #define GPOINTER_TO_GRP(gptr) ((GPOINTER_TO_UINT(gptr)&0xe0) >> 5)
94 static gpointer last_toolbutton_action
= GBO_TO_POINTER(GEANY_GBO_BUILD
);
96 static BuildMenuItems menu_items
= {NULL
, {NULL
, NULL
, NULL
, NULL
}};
100 GtkAction
*run_action
;
101 GtkAction
*compile_action
;
102 GtkAction
*build_action
;
105 GtkWidget
*toolitem_build
;
106 GtkWidget
*toolitem_make_all
;
107 GtkWidget
*toolitem_make_custom
;
108 GtkWidget
*toolitem_make_object
;
109 GtkWidget
*toolitem_set_args
;
113 static guint build_groups_count
[GEANY_GBG_COUNT
] = { 3, 4, 2 };
114 static guint build_items_count
= 9;
116 static void build_exit_cb(GPid pid
, gint status
, gpointer user_data
);
117 static void build_iofunc(GString
*string
, GIOCondition condition
, gpointer data
);
118 static gchar
*build_create_shellscript(const gchar
*working_dir
, const gchar
*cmd
, gboolean autoclose
, GError
**error
);
119 static void build_spawn_cmd(GeanyDocument
*doc
, const gchar
*cmd
, const gchar
*dir
);
120 static void set_stop_button(gboolean stop
);
121 static void run_exit_cb(GPid child_pid
, gint status
, gpointer user_data
);
122 static void on_set_build_commands_activate(GtkWidget
*w
, gpointer u
);
123 static void on_build_next_error(GtkWidget
*menuitem
, gpointer user_data
);
124 static void on_build_previous_error(GtkWidget
*menuitem
, gpointer user_data
);
125 static void kill_process(GPid
*pid
);
126 static void show_build_result_message(gboolean failure
);
127 static void process_build_output_line(gchar
*msg
, gint color
);
128 static void show_build_commands_dialog(void);
129 static void on_build_menu_item(GtkWidget
*w
, gpointer user_data
);
131 void build_finalize(void)
133 g_free(build_info
.dir
);
134 g_free(build_info
.custom_target
);
136 if (menu_items
.menu
!= NULL
&& GTK_IS_WIDGET(menu_items
.menu
))
137 gtk_widget_destroy(menu_items
.menu
);
141 /* note: copied from keybindings.c, may be able to go away */
142 static void add_menu_accel(GeanyKeyGroup
*group
, guint kb_id
,
143 GtkAccelGroup
*accel_group
, GtkWidget
*menuitem
)
145 GeanyKeyBinding
*kb
= keybindings_get_item(group
, kb_id
);
148 gtk_widget_add_accelerator(menuitem
, "activate", accel_group
,
149 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
153 /* convenience routines to access parts of GeanyBuildCommand */
154 static gchar
*id_to_str(GeanyBuildCommand
*bc
, gint id
)
160 case GEANY_BC_COMMAND
:
162 case GEANY_BC_WORKING_DIR
:
163 return bc
->working_dir
;
170 static void set_command(GeanyBuildCommand
*bc
, gint id
, gchar
*str
)
175 SETPTR(bc
->label
, str
);
177 case GEANY_BC_COMMAND
:
178 SETPTR(bc
->command
, str
);
180 case GEANY_BC_WORKING_DIR
:
181 SETPTR(bc
->working_dir
, str
);
189 static const gchar
*config_keys
[GEANY_BC_CMDENTRIES_COUNT
] = {
192 "WD" /* working directory */
195 /*-----------------------------------------------------
197 * Execute commands and handle results
199 *-----------------------------------------------------*/
201 /* the various groups of commands not in the filetype struct */
202 static GeanyBuildCommand
*ft_def
= NULL
;
203 static GeanyBuildCommand
*non_ft_proj
= NULL
;
204 static GeanyBuildCommand
*non_ft_pref
= NULL
;
205 static GeanyBuildCommand
*non_ft_def
= NULL
;
206 static GeanyBuildCommand
*exec_proj
= NULL
;
207 static GeanyBuildCommand
*exec_pref
= NULL
;
208 static GeanyBuildCommand
*exec_def
= NULL
;
209 /* and the regexen not in the filetype structure */
210 static gchar
*regex_pref
= NULL
;
211 /* project non-fileregex string */
212 static gchar
*regex_proj
= NULL
;
214 /* control if build commands are printed by get_build_cmd, for debug purposes only*/
215 #ifndef PRINTBUILDCMDS
216 #define PRINTBUILDCMDS FALSE
218 static gboolean printbuildcmds
= PRINTBUILDCMDS
;
221 /* for debug only, print the commands structures in priority order */
222 static void printfcmds(void)
225 GeanyBuildCommand
**cl
[GEANY_GBG_COUNT
][GEANY_BCS_COUNT
] = {
226 /* GEANY_BCS_DEF, GEANY_BCS_FT, GEANY_BCS_HOME_FT, GEANY_BCS_PREF,
227 * GEANY_BCS_FT_PROJ, GEANY_BCS_PROJ */
228 { &ft_def
, NULL
, NULL
, NULL
, NULL
, NULL
},
229 { &non_ft_def
, NULL
, NULL
, &non_ft_pref
, NULL
, &non_ft_proj
},
230 { &exec_def
, NULL
, NULL
, &exec_pref
, NULL
, &exec_proj
}
232 GeanyFiletype
*ft
= NULL
;
235 enum GeanyBuildCmdEntries n
;
236 gint cc
[GEANY_BCS_COUNT
];
239 doc
= document_get_current();
244 printf("filetype %s\n",ft
->name
);
245 cl
[GEANY_GBG_FT
][GEANY_BCS_FT
] = &(ft
->priv
->filecmds
);
246 cl
[GEANY_GBG_FT
][GEANY_BCS_HOME_FT
] = &(ft
->priv
->homefilecmds
);
247 cl
[GEANY_GBG_FT
][GEANY_BCS_PROJ
] = &(ft
->priv
->projfilecmds
);
248 cl
[GEANY_GBG_NON_FT
][GEANY_BCS_FT
] = &(ft
->priv
->ftdefcmds
);
249 cl
[GEANY_GBG_EXEC
][GEANY_BCS_FT
] = &(ft
->priv
->execcmds
);
250 cl
[GEANY_GBG_EXEC
][GEANY_BCS_HOME_FT
] = &(ft
->priv
->homeexeccmds
);
251 cl
[GEANY_GBG_EXEC
][GEANY_BCS_PROJ_FT
] = &(ft
->priv
->projexeccmds
);
253 for (i
= 0; i
< GEANY_BCS_COUNT
; ++i
)
256 for (j
= 0; j
< GEANY_GBG_COUNT
; ++j
)
258 for (k
= 0; k
< build_groups_count
[j
]; ++k
)
259 if (cl
[j
][i
] != NULL
&& *(cl
[j
][i
]) != NULL
&& (*(cl
[j
][i
]))[k
].exists
)
261 for (n
= 0; n
< GEANY_BC_CMDENTRIES_COUNT
; n
++)
263 if ((*(cl
[j
][i
]))[k
].entries
[n
] != NULL
&&
264 (l
= strlen((*(cl
[j
][i
]))[k
].entries
[n
])) > m
)
273 for (i
= 0; i
< GEANY_GBG_COUNT
; ++i
)
275 for (k
= 0; k
< build_groups_count
[i
]; ++k
)
277 for (l
= 0; l
< 2; ++l
)
280 for (j
= 0; j
< GEANY_BCS_COUNT
; ++j
)
282 if (cl
[i
][j
] != NULL
&& *(cl
[i
][j
]) != NULL
&& (*(cl
[i
][j
]))[k
].exists
)
284 for (n
= 0; n
< GEANY_BC_CMDENTRIES_COUNT
; n
++)
286 if ((*(cl
[i
][j
]))[k
].entries
[i
] != NULL
)
287 printf("%c %*.*s",c
,cc
[j
],cc
[j
],(*(cl
[i
][j
]))[k
].entries
[i
]);
289 printf("%c %*.*s",c
,cc
[j
],cc
[j
]," ");
293 printf("%c %*.*s",c
,cc
[j
],cc
[j
]," ");
305 /* macros to save typing and make the logic visible */
306 #define return_cmd_if(src, cmds)\
307 if (cmds != NULL && cmds[cmdindex].exists && below>src)\
310 if (printbuildcmds) \
311 printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
312 return &(cmds[cmdindex]); \
315 #define return_ft_cmd_if(src, cmds)\
316 if (ft != NULL && ft->priv->cmds != NULL \
317 && ft->priv->cmds[cmdindex].exists && below>src)\
320 if (printbuildcmds) \
321 printf("cmd[%u,%u]=%u\n",cmdgrp,cmdindex,src); \
322 return &(ft->priv->cmds[cmdindex]); \
326 /* get the next lowest command taking priority into account */
327 static GeanyBuildCommand
*get_next_build_cmd(GeanyDocument
*doc
, guint cmdgrp
, guint cmdindex
,
328 guint below
, guint
*from
)
330 /* Note: parameter below used in macros above */
331 GeanyFiletype
*ft
= NULL
;
332 guint sink
, *fr
= &sink
;
334 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, NULL
);
338 if (cmdgrp
>= GEANY_GBG_COUNT
)
343 doc
= document_get_current();
349 case GEANY_GBG_FT
: /* order proj ft, home ft, ft, defft */
352 return_ft_cmd_if(GEANY_BCS_PROJ
, projfilecmds
);
353 return_ft_cmd_if(GEANY_BCS_PREF
, homefilecmds
);
354 return_ft_cmd_if(GEANY_BCS_FT
, filecmds
);
356 return_cmd_if(GEANY_BCS_DEF
, ft_def
);
358 case GEANY_GBG_NON_FT
: /* order proj, pref, def */
359 return_cmd_if(GEANY_BCS_PROJ
, non_ft_proj
);
360 return_cmd_if(GEANY_BCS_PREF
, non_ft_pref
);
361 return_ft_cmd_if(GEANY_BCS_FT
, ftdefcmds
);
362 return_cmd_if(GEANY_BCS_DEF
, non_ft_def
);
364 case GEANY_GBG_EXEC
: /* order proj, proj ft, pref, home ft, ft, def */
365 return_cmd_if(GEANY_BCS_PROJ
, exec_proj
);
366 return_ft_cmd_if(GEANY_BCS_PROJ_FT
, projexeccmds
);
367 return_cmd_if(GEANY_BCS_PREF
, exec_pref
);
368 return_ft_cmd_if(GEANY_BCS_FT
, homeexeccmds
);
369 return_ft_cmd_if(GEANY_BCS_FT
, execcmds
);
370 return_cmd_if(GEANY_BCS_DEF
, exec_def
);
379 /* shortcut to start looking at the top */
380 static GeanyBuildCommand
*get_build_cmd(GeanyDocument
*doc
, guint grp
, guint cmdindex
, guint
*from
)
382 return get_next_build_cmd(doc
, grp
, cmdindex
, GEANY_BCS_COUNT
, from
);
386 #define return_nonblank_regex(src, ptr)\
388 { *fr = (src); return &(ptr); }
391 /* like get_build_cmd, but for regexen, used by filetypes */
392 gchar
**build_get_regex(GeanyBuildGroup grp
, GeanyFiletype
*ft
, guint
*from
)
394 guint sink
, *fr
= &sink
;
398 if (grp
== GEANY_GBG_FT
)
402 GeanyDocument
*doc
= document_get_current();
408 return_nonblank_regex(GEANY_BCS_PROJ
, ft
->priv
->projerror_regex_string
);
409 return_nonblank_regex(GEANY_BCS_HOME_FT
, ft
->priv
->homeerror_regex_string
);
410 return_nonblank_regex(GEANY_BCS_FT
, ft
->error_regex_string
);
412 else if (grp
== GEANY_GBG_NON_FT
)
414 return_nonblank_regex(GEANY_BCS_PROJ
, regex_proj
);
415 return_nonblank_regex(GEANY_BCS_PREF
, regex_pref
);
421 static GeanyBuildCommand
**get_build_group_pointer(const GeanyBuildSource src
, const GeanyBuildGroup grp
)
424 GeanyFiletype
*ft
= NULL
;
429 if ((doc
= document_get_current()) == NULL
)
431 if ((ft
= doc
->file_type
) == NULL
)
435 case GEANY_BCS_DEF
: return &(ft
->priv
->ftdefcmds
);
436 case GEANY_BCS_FT
: return &(ft
->priv
->filecmds
);
437 case GEANY_BCS_HOME_FT
: return &(ft
->priv
->homefilecmds
);
438 case GEANY_BCS_PREF
: return &(ft
->priv
->homefilecmds
);
439 case GEANY_BCS_PROJ
: return &(ft
->priv
->projfilecmds
);
440 default: return NULL
;
443 case GEANY_GBG_NON_FT
:
446 case GEANY_BCS_DEF
: return &(non_ft_def
);
447 case GEANY_BCS_PREF
: return &(non_ft_pref
);
448 case GEANY_BCS_PROJ
: return &(non_ft_proj
);
449 default: return NULL
;
453 if ((doc
= document_get_current()) != NULL
)
457 case GEANY_BCS_DEF
: return &(exec_def
);
458 case GEANY_BCS_FT
: return ft
? &(ft
->priv
->execcmds
): NULL
;
459 case GEANY_BCS_HOME_FT
: return ft
? &(ft
->priv
->homeexeccmds
): NULL
;
460 case GEANY_BCS_PROJ_FT
: return ft
? &(ft
->priv
->projexeccmds
): NULL
;
461 case GEANY_BCS_PREF
: return &(exec_pref
);
462 case GEANY_BCS_PROJ
: return &(exec_proj
);
463 default: return NULL
;
472 /* get pointer to the command group array */
473 static GeanyBuildCommand
*get_build_group(const GeanyBuildSource src
, const GeanyBuildGroup grp
)
475 GeanyBuildCommand
**g
= get_build_group_pointer(src
, grp
);
476 if (g
== NULL
) return NULL
;
481 /** Remove the specified Build menu item.
483 * Makes the specified menu item configuration no longer exist. This
484 * is different to setting fields to blank because the menu item
485 * will be deleted from the configuration file on saving
486 * (except the system filetypes settings @see Build Menu Configuration
487 * section of the Manual).
489 * @param src the source of the menu item to remove.
490 * @param grp the group of the command to remove.
491 * @param cmd the index (from 0) of the command within the group. A negative
492 * value will remove the whole group.
494 * If any parameter is out of range does nothing.
500 void build_remove_menu_item(const GeanyBuildSource src
, const GeanyBuildGroup grp
, const gint cmd
)
502 GeanyBuildCommand
*bc
;
505 bc
= get_build_group(src
, grp
);
510 for (i
= 0; i
< build_groups_count
[grp
]; ++i
)
511 bc
[i
].exists
= FALSE
;
513 else if ((guint
) cmd
< build_groups_count
[grp
])
514 bc
[cmd
].exists
= FALSE
;
518 /* * Get the @a GeanyBuildCommand structure for the specified Build menu item.
520 * Get the command for any menu item specified by @a src, @a grp and @a cmd even if it is
521 * hidden by higher priority commands.
523 * @param src the source of the specified menu item.
524 * @param grp the group of the specified menu item.
525 * @param cmd the index of the command within the group.
527 * @return a pointer to the @a GeanyBuildCommand structure or @c NULL if it doesn't exist.
528 * This is a pointer to an internal structure and must not be freed.
530 * @see build_menu_update
532 GeanyBuildCommand
*build_get_menu_item(GeanyBuildSource src
, GeanyBuildGroup grp
, guint cmd
)
534 GeanyBuildCommand
*bc
;
536 g_return_val_if_fail(src
< GEANY_BCS_COUNT
, NULL
);
537 g_return_val_if_fail(grp
< GEANY_GBG_COUNT
, NULL
);
538 g_return_val_if_fail(cmd
< build_groups_count
[grp
], NULL
);
540 bc
= get_build_group(src
, grp
);
547 /** Get the string for the menu item field.
549 * Get the current highest priority command specified by @a grp and @a cmd. This is the one
550 * that the menu item will use if activated.
552 * @param grp the group of the specified menu item.
553 * @param cmd the index of the command within the group.
554 * @param fld the field to return
556 * @return @nullable a pointer to the constant string or @c NULL if it doesn't exist.
557 * This is a pointer to an internal structure and must not be freed.
561 const gchar
*build_get_current_menu_item(const GeanyBuildGroup grp
, const guint cmd
,
562 const GeanyBuildCmdEntries fld
)
564 GeanyBuildCommand
*c
;
567 g_return_val_if_fail(grp
< GEANY_GBG_COUNT
, NULL
);
568 g_return_val_if_fail(fld
< GEANY_BC_CMDENTRIES_COUNT
, NULL
);
569 g_return_val_if_fail(cmd
< build_groups_count
[grp
], NULL
);
571 c
= get_build_cmd(NULL
, grp
, cmd
, NULL
);
572 if (c
== NULL
) return NULL
;
575 case GEANY_BC_COMMAND
:
581 case GEANY_BC_WORKING_DIR
:
582 str
= c
->working_dir
;
590 /** Set the string for the menu item field.
592 * Set the specified field of the command specified by @a src, @a grp and @a cmd.
594 * @param src the source of the menu item
595 * @param grp the group of the specified menu item.
596 * @param cmd the index of the menu item within the group.
597 * @param fld the field in the menu item command to set
598 * @param val the value to set the field to, is copied
602 void build_set_menu_item(const GeanyBuildSource src
, const GeanyBuildGroup grp
,
603 const guint cmd
, const GeanyBuildCmdEntries fld
, const gchar
*val
)
605 GeanyBuildCommand
**g
;
607 g_return_if_fail(src
< GEANY_BCS_COUNT
);
608 g_return_if_fail(grp
< GEANY_GBG_COUNT
);
609 g_return_if_fail(fld
< GEANY_BC_CMDENTRIES_COUNT
);
610 g_return_if_fail(cmd
< build_groups_count
[grp
]);
612 g
= get_build_group_pointer(src
, grp
);
613 if (g
== NULL
) return;
616 *g
= g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
620 case GEANY_BC_COMMAND
:
621 SETPTR((*g
)[cmd
].command
, g_strdup(val
));
622 (*g
)[cmd
].exists
= TRUE
;
625 SETPTR((*g
)[cmd
].label
, g_strdup(val
));
626 (*g
)[cmd
].exists
= TRUE
;
628 case GEANY_BC_WORKING_DIR
:
629 SETPTR((*g
)[cmd
].working_dir
, g_strdup(val
));
630 (*g
)[cmd
].exists
= TRUE
;
635 build_menu_update(NULL
);
638 /** Activate the menu item.
640 * Activate the menu item specified by @a grp and @a cmd.
642 * @param grp the group of the specified menu item.
643 * @param cmd the index of the command within the group.
647 void build_activate_menu_item(const GeanyBuildGroup grp
, const guint cmd
)
649 on_build_menu_item(NULL
, GRP_CMD_TO_POINTER(grp
, cmd
));
653 /* Clear all error indicators in all documents. */
654 static void clear_all_errors(void)
660 editor_indicator_clear_errors(documents
[i
]->editor
);
665 /* Replaces occurrences of %e and %p with the appropriate filenames and
666 * %l with current line number. %d and %p replacements should be in UTF8 */
667 static gchar
*build_replace_placeholder(const GeanyDocument
*doc
, const gchar
*src
)
671 gchar
*executable
= NULL
;
674 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, NULL
);
676 stack
= g_string_new(src
);
677 if (doc
!= NULL
&& doc
->file_name
!= NULL
)
679 /* replace %f with the filename (including extension) */
680 replacement
= g_path_get_basename(doc
->file_name
);
681 utils_string_replace_all(stack
, "%f", replacement
);
684 /* replace %d with the absolute path of the dir of the current file */
685 replacement
= g_path_get_dirname(doc
->file_name
);
686 utils_string_replace_all(stack
, "%d", replacement
);
689 /* replace %e with the filename (excluding extension) */
690 executable
= utils_remove_ext_from_filename(doc
->file_name
);
691 replacement
= g_path_get_basename(executable
);
692 utils_string_replace_all(stack
, "%e", replacement
);
695 /* replace %l with the current 1-based line number */
696 line_num
= sci_get_current_line(doc
->editor
->sci
) + 1;
697 replacement
= g_strdup_printf("%i", line_num
);
698 utils_string_replace_all(stack
, "%l", replacement
);
702 /* replace %p with the current project's (absolute) base directory */
703 replacement
= NULL
; /* prevent double free if no replacement found */
706 replacement
= project_get_base_path();
708 else if (strstr(stack
->str
, "%p"))
709 { /* fall back to %d */
710 ui_set_statusbar(FALSE
, _("failed to substitute %%p, no project active"));
711 if (doc
!= NULL
&& doc
->file_name
!= NULL
)
712 replacement
= g_path_get_dirname(doc
->file_name
);
715 utils_string_replace_all(stack
, "%p", replacement
);
719 return g_string_free(stack
, FALSE
); /* don't forget to free src also if needed */
723 /* dir is the UTF-8 working directory to run cmd in. It can be NULL to use the
724 * idx document directory */
725 static void build_spawn_cmd(GeanyDocument
*doc
, const gchar
*cmd
, const gchar
*dir
)
727 GError
*error
= NULL
;
728 gchar
*argv
[] = { "/bin/sh", "-c", NULL
, NULL
};
730 gchar
*utf8_working_dir
;
733 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
735 if ((doc
== NULL
|| EMPTY(doc
->file_name
)) && EMPTY(dir
))
737 geany_debug("Failed to run command with no working directory");
738 ui_set_statusbar(TRUE
, _("Process failed, no working directory"));
743 SETPTR(current_dir_entered
, NULL
);
745 utf8_working_dir
= !EMPTY(dir
) ? g_strdup(dir
) : g_path_get_dirname(doc
->file_name
);
746 working_dir
= utils_get_locale_from_utf8(utf8_working_dir
);
748 gtk_list_store_clear(msgwindow
.store_compiler
);
749 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
750 msgwin_compiler_add(COLOR_BLUE
, _("%s (in directory: %s)"), cmd
, utf8_working_dir
);
751 g_free(utf8_working_dir
);
753 cmd_string
= utils_get_locale_from_utf8(cmd
);
754 argv
[2] = cmd_string
;
757 cmd
= NULL
; /* under Unix, use argv to start cmd via sh for compatibility */
759 argv
[0] = NULL
; /* under Windows, run cmd directly */
762 /* set the build info for the message window */
763 g_free(build_info
.dir
);
764 build_info
.dir
= g_strdup(working_dir
);
765 build_info
.file_type_id
= (doc
== NULL
) ? GEANY_FILETYPES_NONE
: doc
->file_type
->id
;
766 build_info
.message_count
= 0;
768 if (!spawn_with_callbacks(working_dir
, cmd
, argv
, NULL
, 0, NULL
, NULL
, build_iofunc
,
769 GINT_TO_POINTER(0), 0, build_iofunc
, GINT_TO_POINTER(1), 0, build_exit_cb
, NULL
,
770 &build_info
.pid
, &error
))
772 geany_debug("build command spawning failed: %s", error
->message
);
773 ui_set_statusbar(TRUE
, _("Process failed (%s)"), error
->message
);
782 /* Returns: NULL if there was an error, or the command to be executed. If Geany is
783 * set to use a run script, the returned value is a path to the script that runs
784 * the command; otherwise the command itself is returned. working_dir is a pointer
785 * to the working directory from which the command is executed. Both strings are
786 * in the locale encoding. */
787 static gchar
*prepare_run_cmd(GeanyDocument
*doc
, gchar
**working_dir
, guint cmdindex
)
789 GeanyBuildCommand
*cmd
= NULL
;
790 const gchar
*cmd_working_dir
;
791 gboolean autoclose
= FALSE
;
792 gchar
*cmd_string_utf8
, *working_dir_utf8
, *run_cmd
, *cmd_string
;
793 GError
*error
= NULL
;
795 cmd
= get_build_cmd(doc
, GEANY_GBG_EXEC
, cmdindex
, NULL
);
797 cmd_string_utf8
= build_replace_placeholder(doc
, cmd
->command
);
798 cmd_working_dir
= cmd
->working_dir
;
799 if (EMPTY(cmd_working_dir
))
800 cmd_working_dir
= "%d";
801 working_dir_utf8
= build_replace_placeholder(doc
, cmd_working_dir
);
802 *working_dir
= utils_get_locale_from_utf8(working_dir_utf8
);
804 if (EMPTY(*working_dir
) || ! g_file_test(*working_dir
, G_FILE_TEST_EXISTS
) ||
805 ! g_file_test(*working_dir
, G_FILE_TEST_IS_DIR
))
807 ui_set_statusbar(TRUE
, _("Invalid working directory \"%s\""),
808 !EMPTY(working_dir_utf8
) ? working_dir_utf8
: "<NULL>" );
809 utils_free_pointers(3, cmd_string_utf8
, working_dir_utf8
, *working_dir
, NULL
);
813 cmd_string
= utils_get_locale_from_utf8(cmd_string_utf8
);
816 if (vte_info
.have_vte
&& vc
->run_in_vte
)
818 if (vc
->skip_run_script
)
820 utils_free_pointers(2, cmd_string_utf8
, working_dir_utf8
, NULL
);
824 /* don't wait for user input at the end of script when we are running in VTE */
829 run_cmd
= build_create_shellscript(*working_dir
, cmd_string
, autoclose
, &error
);
832 ui_set_statusbar(TRUE
, _("Failed to execute \"%s\" (start-script could not be created: %s)"),
833 !EMPTY(cmd_string_utf8
) ? cmd_string_utf8
: NULL
, error
->message
);
835 g_free(*working_dir
);
838 utils_free_pointers(3, cmd_string_utf8
, working_dir_utf8
, cmd_string
, NULL
);
843 static void build_run_cmd(GeanyDocument
*doc
, guint cmdindex
)
846 gchar
*run_cmd
= NULL
;
848 if (! DOC_VALID(doc
) || doc
->file_name
== NULL
)
851 run_cmd
= prepare_run_cmd(doc
, &working_dir
, cmdindex
);
855 run_info
[cmdindex
].file_type_id
= doc
->file_type
->id
;
858 if (vte_info
.have_vte
&& vc
->run_in_vte
)
862 /* VTE expects commands in UTF-8 */
863 SETPTR(run_cmd
, utils_get_utf8_from_locale(run_cmd
));
864 SETPTR(working_dir
, utils_get_utf8_from_locale(working_dir
));
866 if (vc
->skip_run_script
)
867 vte_cmd
= g_strconcat(run_cmd
, "\n", NULL
);
869 vte_cmd
= g_strconcat("\n/bin/sh ", run_cmd
, "\n", NULL
);
871 vte_cwd(working_dir
, TRUE
);
872 if (! vte_send_cmd(vte_cmd
))
874 const gchar
*msg
= _("File not executed because the terminal may contain some input (press Ctrl+C or Enter to clear it).");
875 ui_set_statusbar(FALSE
, "%s", msg
);
876 geany_debug("%s", msg
);
877 if (!vc
->skip_run_script
)
882 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_VTE
);
883 gtk_widget_grab_focus(vc
->vte
);
884 msgwin_show_hide(TRUE
);
886 run_info
[cmdindex
].pid
= 1;
892 gchar
*locale_term_cmd
= utils_get_locale_from_utf8(tool_prefs
.term_cmd
);
893 GError
*error
= NULL
;
895 utils_str_replace_all(&locale_term_cmd
, "%c", run_cmd
);
897 if (spawn_async(working_dir
, locale_term_cmd
, NULL
, NULL
, &(run_info
[cmdindex
].pid
),
900 g_child_watch_add(run_info
[cmdindex
].pid
, (GChildWatchFunc
) run_exit_cb
,
901 (gpointer
) &(run_info
[cmdindex
]));
902 build_menu_update(doc
);
906 ui_set_statusbar(TRUE
, _("Cannot execute terminal command \"%s\": %s. "
907 "Check the path setting in Preferences."), tool_prefs
.term_cmd
, error
->message
);
910 run_info
[cmdindex
].pid
= (GPid
) 0;
919 static void process_build_output_line(gchar
*msg
, gint color
)
930 if (build_parse_make_dir(msg
, &tmp
))
932 SETPTR(current_dir_entered
, tmp
);
934 msgwin_parse_compiler_error_line(msg
, current_dir_entered
, &filename
, &line
);
936 if (line
!= -1 && filename
!= NULL
)
938 GeanyDocument
*doc
= document_find_by_filename(filename
);
940 /* limit number of indicators */
941 if (doc
&& editor_prefs
.use_indicators
&&
942 build_info
.message_count
< GEANY_BUILD_ERR_HIGHLIGHT_MAX
)
944 if (line
> 0) /* some compilers, like pdflatex report errors on line 0 */
945 line
--; /* so only adjust the line number if it is greater than 0 */
946 editor_indicator_set_on_line(doc
->editor
, GEANY_INDICATOR_ERROR
, line
);
948 build_info
.message_count
++;
949 color
= COLOR_RED
; /* error message parsed on the line */
953 msgwin_compiler_add_string(color
, msg
);
957 static void build_iofunc(GString
*string
, GIOCondition condition
, gpointer data
)
959 if (condition
& (G_IO_IN
| G_IO_PRI
))
961 process_build_output_line(string
->str
,
962 (GPOINTER_TO_INT(data
)) ? COLOR_DARK_RED
: COLOR_BLACK
);
967 gboolean
build_parse_make_dir(const gchar
*string
, gchar
**prefix
)
976 if ((pos
= strstr(string
, "Entering directory")) != NULL
)
981 /* get the start of the path */
982 pos
= strstr(string
, "/");
987 input
= g_strdup(pos
);
989 /* kill the ' at the end of the path */
991 input
[len
- 1] = '\0';
992 input
= g_realloc(input
, len
); /* shorten by 1 */
998 if (strstr(string
, "Leaving directory") != NULL
)
1008 static void show_build_result_message(gboolean failure
)
1014 msg
= _("Compilation failed.");
1015 msgwin_compiler_add_string(COLOR_BLUE
, msg
);
1016 /* If msgwindow is hidden, user will want to display it to see the error */
1017 if (! ui_prefs
.msgwindow_visible
)
1019 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
1020 msgwin_show_hide(TRUE
);
1023 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow
.notebook
)) != MSG_COMPILER
)
1024 ui_set_statusbar(FALSE
, "%s", msg
);
1028 msg
= _("Compilation finished successfully.");
1029 msgwin_compiler_add_string(COLOR_BLUE
, msg
);
1030 if (! ui_prefs
.msgwindow_visible
||
1031 gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow
.notebook
)) != MSG_COMPILER
)
1032 ui_set_statusbar(FALSE
, "%s", msg
);
1037 static void build_exit_cb(GPid child_pid
, gint status
, gpointer user_data
)
1039 show_build_result_message(!SPAWN_WIFEXITED(status
) || SPAWN_WEXITSTATUS(status
) != EXIT_SUCCESS
);
1043 /* enable build items again */
1044 build_menu_update(NULL
);
1045 ui_progress_bar_stop();
1049 static void run_exit_cb(GPid child_pid
, gint status
, gpointer user_data
)
1051 RunInfo
*run_info_data
= user_data
;
1053 g_spawn_close_pid(child_pid
);
1055 run_info_data
->pid
= 0;
1056 /* reset the stop button and menu item to the original meaning */
1057 build_menu_update(NULL
);
1061 /* write a little shellscript to call the executable (similar to anjuta_launcher but "internal")
1062 * working_dir and cmd are both in the locale encoding
1063 * it returns the full file name (including path) of the created script in the locale encoding */
1064 static gchar
*build_create_shellscript(const gchar
*working_dir
, const gchar
*cmd
, gboolean autoclose
, GError
**error
)
1068 gboolean success
= TRUE
;
1070 gchar
*expanded_cmd
;
1074 fd
= g_file_open_tmp (RUN_SCRIPT_CMD
, &fname
, error
);
1080 /* Expand environment variables like %blah%. */
1081 expanded_cmd
= win32_expand_environment_variables(cmd
);
1082 str
= g_strdup_printf("cd \"%s\"\n\n%s\n\n%s\ndel \"%%0\"\n\npause\n", working_dir
, expanded_cmd
, (autoclose
) ? "" : "pause");
1083 g_free(expanded_cmd
);
1085 escaped_dir
= g_shell_quote(working_dir
);
1086 str
= g_strdup_printf(
1087 "#!/bin/sh\n\nrm $0\n\ncd %s\n\n%s\n\necho \"\n\n------------------\n(program exited with code: $?)\" \
1088 \n\n%s\n", escaped_dir
, cmd
, (autoclose
) ? "" :
1089 "\necho \"Press return to continue\"\n#to be more compatible with shells like "
1090 "dash\ndummy_var=\"\"\nread dummy_var");
1091 g_free(escaped_dir
);
1094 if (!g_file_set_contents(fname
, str
, -1, error
))
1098 if (success
&& g_chmod(fname
, 0777) != 0)
1104 g_set_error(error
, G_FILE_ERROR
, g_file_error_from_errno(errsv
),
1105 "Failed to make file executable: %s", g_strerror(errsv
));
1122 typedef void Callback(GtkWidget
*w
, gpointer u
);
1124 /* run the command catenating cmd_cat if present */
1125 static void build_command(GeanyDocument
*doc
, GeanyBuildGroup grp
, guint cmd
, gchar
*cmd_cat
)
1128 gchar
*full_command
, *subs_command
;
1129 GeanyBuildCommand
*buildcmd
= get_build_cmd(doc
, grp
, cmd
, NULL
);
1132 if (buildcmd
== NULL
)
1135 cmdstr
= buildcmd
->command
;
1137 if (cmd_cat
!= NULL
)
1140 full_command
= g_strconcat(cmdstr
, cmd_cat
, NULL
);
1142 full_command
= g_strdup(cmd_cat
);
1145 full_command
= cmdstr
;
1147 dir
= build_replace_placeholder(doc
, buildcmd
->working_dir
);
1148 subs_command
= build_replace_placeholder(doc
, full_command
);
1149 build_info
.grp
= grp
;
1150 build_info
.cmd
= cmd
;
1151 build_spawn_cmd(doc
, subs_command
, dir
);
1152 g_free(subs_command
);
1154 if (cmd_cat
!= NULL
)
1155 g_free(full_command
);
1156 build_menu_update(doc
);
1158 ui_progress_bar_start(NULL
);
1162 /*----------------------------------------------------------------
1164 * Create build menu and handle callbacks (&toolbar callbacks)
1166 *----------------------------------------------------------------*/
1167 static void on_make_custom_input_response(const gchar
*input
, gpointer data
)
1169 GeanyDocument
*doc
= document_get_current();
1171 SETPTR(build_info
.custom_target
, g_strdup(input
));
1172 build_command(doc
, GBO_TO_GBG(GEANY_GBO_CUSTOM
), GBO_TO_CMD(GEANY_GBO_CUSTOM
),
1173 build_info
.custom_target
);
1177 static void on_build_menu_item(GtkWidget
*w
, gpointer user_data
)
1179 GeanyDocument
*doc
= document_get_current();
1180 GeanyBuildCommand
*bc
;
1181 guint grp
= GPOINTER_TO_GRP(user_data
);
1182 guint cmd
= GPOINTER_TO_CMD(user_data
);
1184 if (doc
&& doc
->changed
)
1186 if (!document_save_file(doc
, FALSE
))
1189 g_signal_emit_by_name(geany_object
, "build-start");
1191 if (grp
== GEANY_GBG_NON_FT
&& cmd
== GBO_TO_CMD(GEANY_GBO_CUSTOM
))
1193 static GtkWidget
*dialog
= NULL
; /* keep dialog for combo history */
1197 dialog
= dialogs_show_input_persistent(_("Custom Text"), GTK_WINDOW(main_widgets
.window
),
1198 _("Enter custom text here, all entered text is appended to the command."),
1199 build_info
.custom_target
, &on_make_custom_input_response
, NULL
);
1203 gtk_widget_show(dialog
);
1207 else if (grp
== GEANY_GBG_EXEC
)
1209 if (run_info
[cmd
].pid
> (GPid
) 1)
1211 kill_process(&run_info
[cmd
].pid
);
1214 bc
= get_build_cmd(doc
, grp
, cmd
, NULL
);
1215 if (bc
!= NULL
&& strcmp(bc
->command
, "builtin") == 0)
1220 uri
= g_strconcat("file:///", g_path_skip_root(doc
->file_name
), NULL
);
1221 utils_open_browser(uri
);
1226 build_run_cmd(doc
, cmd
);
1229 build_command(doc
, grp
, cmd
, NULL
);
1233 /* group codes for menu items other than the known commands
1234 * value order is important, see the following table for use */
1236 /* the rest in each group */
1237 #define MENU_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_FT)
1238 #define MENU_NON_FT_REST (GEANY_GBG_COUNT + GEANY_GBG_NON_FT)
1239 #define MENU_EXEC_REST (GEANY_GBG_COUNT + GEANY_GBG_EXEC)
1241 #define MENU_SEPARATOR (2*GEANY_GBG_COUNT)
1242 /* the fixed items */
1243 #define MENU_NEXT_ERROR (MENU_SEPARATOR + 1)
1244 #define MENU_PREV_ERROR (MENU_NEXT_ERROR + 1)
1245 #define MENU_COMMANDS (MENU_PREV_ERROR + 1)
1246 #define MENU_DONE (MENU_COMMANDS + 1)
1249 static struct BuildMenuItemSpec
{
1250 const gchar
*stock_id
;
1251 const gint key_binding
;
1252 const guint build_grp
;
1253 const guint build_cmd
;
1254 const gchar
*fix_label
;
1256 } build_menu_specs
[] = {
1257 {GTK_STOCK_CONVERT
, GEANY_KEYS_BUILD_COMPILE
, GBO_TO_GBG(GEANY_GBO_COMPILE
),
1258 GBO_TO_CMD(GEANY_GBO_COMPILE
), NULL
, on_build_menu_item
},
1259 {GEANY_STOCK_BUILD
, GEANY_KEYS_BUILD_LINK
, GBO_TO_GBG(GEANY_GBO_BUILD
),
1260 GBO_TO_CMD(GEANY_GBO_BUILD
), NULL
, on_build_menu_item
},
1261 {NULL
, -1, MENU_FT_REST
,
1262 GBO_TO_CMD(GEANY_GBO_BUILD
) + 1, NULL
, on_build_menu_item
},
1263 {NULL
, -1, MENU_SEPARATOR
,
1264 GBF_SEP_1
, NULL
, NULL
},
1265 {NULL
, GEANY_KEYS_BUILD_MAKE
, GBO_TO_GBG(GEANY_GBO_MAKE_ALL
),
1266 GBO_TO_CMD(GEANY_GBO_MAKE_ALL
), NULL
, on_build_menu_item
},
1267 {NULL
, GEANY_KEYS_BUILD_MAKEOWNTARGET
, GBO_TO_GBG(GEANY_GBO_CUSTOM
),
1268 GBO_TO_CMD(GEANY_GBO_CUSTOM
), NULL
, on_build_menu_item
},
1269 {NULL
, GEANY_KEYS_BUILD_MAKEOBJECT
, GBO_TO_GBG(GEANY_GBO_MAKE_OBJECT
),
1270 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
), NULL
, on_build_menu_item
},
1271 {NULL
, -1, MENU_NON_FT_REST
,
1272 GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
) + 1, NULL
, on_build_menu_item
},
1273 {NULL
, -1, MENU_SEPARATOR
,
1274 GBF_SEP_2
, NULL
, NULL
},
1275 {GTK_STOCK_GO_DOWN
, GEANY_KEYS_BUILD_NEXTERROR
, MENU_NEXT_ERROR
,
1276 GBF_NEXT_ERROR
, N_("_Next Error"), on_build_next_error
},
1277 {GTK_STOCK_GO_UP
, GEANY_KEYS_BUILD_PREVIOUSERROR
, MENU_PREV_ERROR
,
1278 GBF_PREV_ERROR
, N_("_Previous Error"), on_build_previous_error
},
1279 {NULL
, -1, MENU_SEPARATOR
,
1280 GBF_SEP_3
, NULL
, NULL
},
1281 {GTK_STOCK_EXECUTE
, GEANY_KEYS_BUILD_RUN
, GBO_TO_GBG(GEANY_GBO_EXEC
),
1282 GBO_TO_CMD(GEANY_GBO_EXEC
), NULL
, on_build_menu_item
},
1283 {NULL
, -1, MENU_EXEC_REST
,
1284 GBO_TO_CMD(GEANY_GBO_EXEC
) + 1, NULL
, on_build_menu_item
},
1285 {NULL
, -1, MENU_SEPARATOR
,
1286 GBF_SEP_4
, NULL
, NULL
},
1287 {GTK_STOCK_PREFERENCES
, GEANY_KEYS_BUILD_OPTIONS
, MENU_COMMANDS
,
1288 GBF_COMMANDS
, N_("_Set Build Commands"), on_set_build_commands_activate
},
1289 {NULL
, -1, MENU_DONE
,
1294 static void create_build_menu_item(GtkWidget
*menu
, GeanyKeyGroup
*group
, GtkAccelGroup
*ag
,
1295 struct BuildMenuItemSpec
*bs
, const gchar
*lbl
, guint grp
, guint cmd
)
1297 GtkWidget
*item
= gtk_image_menu_item_new_with_mnemonic(lbl
);
1299 if (bs
->stock_id
!= NULL
)
1301 GtkWidget
*image
= gtk_image_new_from_stock(bs
->stock_id
, GTK_ICON_SIZE_MENU
);
1302 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item
), image
);
1304 gtk_widget_show(item
);
1305 if (bs
->key_binding
>= 0)
1306 add_menu_accel(group
, (guint
) bs
->key_binding
, ag
, item
);
1307 gtk_container_add(GTK_CONTAINER(menu
), item
);
1310 g_signal_connect(item
, "activate", G_CALLBACK(bs
->cb
), GRP_CMD_TO_POINTER(grp
,cmd
));
1312 menu_items
.menu_item
[grp
][cmd
] = item
;
1316 static void create_build_menu(BuildMenuItems
*build_menu_items
)
1319 GtkAccelGroup
*accel_group
= gtk_accel_group_new();
1320 GeanyKeyGroup
*keygroup
= keybindings_get_core_group(GEANY_KEY_GROUP_BUILD
);
1323 menu
= gtk_menu_new();
1324 build_menu_items
->menu_item
[GEANY_GBG_FT
] = g_new0(GtkWidget
*, build_groups_count
[GEANY_GBG_FT
]);
1325 build_menu_items
->menu_item
[GEANY_GBG_NON_FT
] = g_new0(GtkWidget
*, build_groups_count
[GEANY_GBG_NON_FT
]);
1326 build_menu_items
->menu_item
[GEANY_GBG_EXEC
] = g_new0(GtkWidget
*, build_groups_count
[GEANY_GBG_EXEC
]);
1327 build_menu_items
->menu_item
[GBG_FIXED
] = g_new0(GtkWidget
*, GBF_COUNT
);
1329 for (i
= 0; build_menu_specs
[i
].build_grp
!= MENU_DONE
; ++i
)
1331 struct BuildMenuItemSpec
*bs
= &(build_menu_specs
[i
]);
1332 if (bs
->build_grp
== MENU_SEPARATOR
)
1334 GtkWidget
*item
= gtk_separator_menu_item_new();
1335 gtk_widget_show(item
);
1336 gtk_container_add(GTK_CONTAINER(menu
), item
);
1337 build_menu_items
->menu_item
[GBG_FIXED
][bs
->build_cmd
] = item
;
1339 else if (bs
->fix_label
!= NULL
)
1341 create_build_menu_item(menu
, keygroup
, accel_group
, bs
, _(bs
->fix_label
),
1342 GBG_FIXED
, bs
->build_cmd
);
1344 else if (bs
->build_grp
>= MENU_FT_REST
&& bs
->build_grp
<= MENU_SEPARATOR
)
1346 guint grp
= bs
->build_grp
- GEANY_GBG_COUNT
;
1347 for (j
= bs
->build_cmd
; j
< build_groups_count
[grp
]; ++j
)
1349 GeanyBuildCommand
*bc
= get_build_cmd(NULL
, grp
, j
, NULL
);
1350 const gchar
*lbl
= (bc
== NULL
) ? "" : bc
->label
;
1351 create_build_menu_item(menu
, keygroup
, accel_group
, bs
, lbl
, grp
, j
);
1356 GeanyBuildCommand
*bc
= get_build_cmd(NULL
, bs
->build_grp
, bs
->build_cmd
, NULL
);
1357 const gchar
*lbl
= (bc
== NULL
) ? "" : bc
->label
;
1358 create_build_menu_item(menu
, keygroup
, accel_group
, bs
, lbl
, bs
->build_grp
, bs
->build_cmd
);
1361 build_menu_items
->menu
= menu
;
1362 gtk_widget_show(menu
);
1363 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_build1")), menu
);
1367 /* portability to various GTK versions needs checking
1368 * conforms to description of gtk_accel_label as child of menu item
1369 * NB 2.16 adds set_label but not yet set_label_mnemonic */
1370 static void geany_menu_item_set_label(GtkWidget
*w
, const gchar
*label
)
1372 GtkWidget
*c
= gtk_bin_get_child(GTK_BIN(w
));
1374 gtk_label_set_text_with_mnemonic(GTK_LABEL(c
), label
);
1378 /* * Update the build menu to reflect changes in configuration or status.
1380 * Sets the labels and number of visible items to match the highest
1381 * priority configured commands. Also sets sensitivity if build commands are
1382 * running and switches executes to stop when commands are running.
1384 * @param doc The current document, if available, to save looking it up.
1385 * If @c NULL it will be looked up.
1387 * Call this after modifying any fields of a GeanyBuildCommand structure.
1389 * @see Build Menu Configuration section of the Manual.
1392 void build_menu_update(GeanyDocument
*doc
)
1394 guint i
, cmdcount
, cmd
, grp
;
1395 gboolean vis
= FALSE
;
1396 gboolean have_path
, build_running
, exec_running
, have_errors
, cmd_sensitivity
;
1397 gboolean can_compile
, can_build
, can_make
, run_sensitivity
= FALSE
, run_running
= FALSE
;
1398 GeanyBuildCommand
*bc
;
1400 g_return_if_fail(doc
== NULL
|| doc
->is_valid
);
1402 if (menu_items
.menu
== NULL
)
1403 create_build_menu(&menu_items
);
1405 doc
= document_get_current();
1406 have_path
= doc
!= NULL
&& doc
->file_name
!= NULL
;
1407 build_running
= build_info
.pid
> (GPid
) 1;
1408 have_errors
= gtk_tree_model_iter_n_children(GTK_TREE_MODEL(msgwindow
.store_compiler
), NULL
) > 0;
1409 for (i
= 0; build_menu_specs
[i
].build_grp
!= MENU_DONE
; ++i
)
1411 struct BuildMenuItemSpec
*bs
= &(build_menu_specs
[i
]);
1412 switch (bs
->build_grp
)
1414 case MENU_SEPARATOR
:
1417 gtk_widget_show_all(menu_items
.menu_item
[GBG_FIXED
][bs
->build_cmd
]);
1421 gtk_widget_hide(menu_items
.menu_item
[GBG_FIXED
][bs
->build_cmd
]);
1423 case MENU_NEXT_ERROR
:
1424 case MENU_PREV_ERROR
:
1425 gtk_widget_set_sensitive(menu_items
.menu_item
[GBG_FIXED
][bs
->build_cmd
], have_errors
);
1431 default: /* all configurable commands */
1432 if (bs
->build_grp
>= GEANY_GBG_COUNT
)
1434 grp
= bs
->build_grp
- GEANY_GBG_COUNT
;
1435 cmdcount
= build_groups_count
[grp
];
1439 grp
= bs
->build_grp
;
1440 cmdcount
= bs
->build_cmd
+ 1;
1442 for (cmd
= bs
->build_cmd
; cmd
< cmdcount
; ++cmd
)
1444 GtkWidget
*menu_item
= menu_items
.menu_item
[grp
][cmd
];
1446 bc
= get_build_cmd(doc
, grp
, cmd
, NULL
);
1452 if (grp
< GEANY_GBG_EXEC
)
1455 (grp
== GEANY_GBG_FT
&& bc
!= NULL
&& have_path
&& ! build_running
) ||
1456 (grp
== GEANY_GBG_NON_FT
&& bc
!= NULL
&& ! build_running
);
1457 gtk_widget_set_sensitive(menu_item
, cmd_sensitivity
);
1458 if (bc
!= NULL
&& !EMPTY(label
))
1460 geany_menu_item_set_label(menu_item
, label
);
1461 gtk_widget_show_all(menu_item
);
1465 gtk_widget_hide(menu_item
);
1470 exec_running
= run_info
[cmd
].pid
> (GPid
) 1;
1471 cmd_sensitivity
= (bc
!= NULL
) || exec_running
;
1472 gtk_widget_set_sensitive(menu_item
, cmd_sensitivity
);
1473 if (cmd
== GBO_TO_CMD(GEANY_GBO_EXEC
))
1474 run_sensitivity
= cmd_sensitivity
;
1477 image
= gtk_image_new_from_stock(bs
->stock_id
, GTK_ICON_SIZE_MENU
);
1481 image
= gtk_image_new_from_stock(GTK_STOCK_STOP
, GTK_ICON_SIZE_MENU
);
1483 if (cmd
== GBO_TO_CMD(GEANY_GBO_EXEC
))
1484 run_running
= exec_running
;
1485 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item
), image
);
1486 if (bc
!= NULL
&& !EMPTY(label
))
1488 geany_menu_item_set_label(menu_item
, label
);
1489 gtk_widget_show_all(menu_item
);
1493 gtk_widget_hide(menu_item
);
1499 run_sensitivity
&= (doc
!= NULL
);
1500 can_build
= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_BUILD
), NULL
) != NULL
1501 && have_path
&& ! build_running
;
1502 if (widgets
.toolitem_build
!= NULL
)
1503 gtk_widget_set_sensitive(widgets
.toolitem_build
, can_build
);
1505 if (widgets
.toolitem_make_all
!= NULL
)
1506 gtk_widget_set_sensitive(widgets
.toolitem_make_all
,
1507 (can_make
|= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_MAKE_ALL
), NULL
) != NULL
1508 && ! build_running
));
1509 if (widgets
.toolitem_make_custom
!= NULL
)
1510 gtk_widget_set_sensitive(widgets
.toolitem_make_custom
,
1511 (can_make
|= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_CUSTOM
), NULL
) != NULL
1512 && ! build_running
));
1513 if (widgets
.toolitem_make_object
!= NULL
)
1514 gtk_widget_set_sensitive(widgets
.toolitem_make_object
,
1515 (can_make
|= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
), NULL
) != NULL
1516 && ! build_running
));
1517 if (widgets
.toolitem_set_args
!= NULL
)
1518 gtk_widget_set_sensitive(widgets
.toolitem_set_args
, TRUE
);
1520 can_compile
= get_build_cmd(doc
, GEANY_GBG_FT
, GBO_TO_CMD(GEANY_GBO_COMPILE
), NULL
) != NULL
1521 && have_path
&& ! build_running
;
1522 gtk_action_set_sensitive(widgets
.compile_action
, can_compile
);
1523 gtk_action_set_sensitive(widgets
.build_action
, can_make
);
1524 gtk_action_set_sensitive(widgets
.run_action
, run_sensitivity
);
1526 /* show the stop command if a program is running from execute 0 , otherwise show run command */
1527 set_stop_button(run_running
);
1532 /* Call build_menu_update() instead of calling this directly. */
1533 static void set_stop_button(gboolean stop
)
1535 const gchar
*button_stock_id
= NULL
;
1536 GtkToolButton
*run_button
;
1538 run_button
= GTK_TOOL_BUTTON(toolbar_get_widget_by_name("Run"));
1539 if (run_button
!= NULL
)
1540 button_stock_id
= gtk_tool_button_get_stock_id(run_button
);
1542 if (stop
&& utils_str_equal(button_stock_id
, GTK_STOCK_STOP
))
1544 if (! stop
&& utils_str_equal(button_stock_id
, GTK_STOCK_EXECUTE
))
1547 /* use the run button also as stop button */
1550 if (run_button
!= NULL
)
1551 gtk_tool_button_set_stock_id(run_button
, GTK_STOCK_STOP
);
1555 if (run_button
!= NULL
)
1556 gtk_tool_button_set_stock_id(run_button
, GTK_STOCK_EXECUTE
);
1561 static void on_set_build_commands_activate(GtkWidget
*w
, gpointer u
)
1563 /* For now, just show the project dialog */
1565 project_build_properties();
1567 show_build_commands_dialog();
1571 static void on_toolbutton_build_activate(GtkWidget
*menuitem
, gpointer user_data
)
1573 last_toolbutton_action
= user_data
;
1574 g_object_set(widgets
.build_action
, "tooltip", _("Build the current file"), NULL
);
1575 on_build_menu_item(menuitem
, user_data
);
1579 static void on_toolbutton_make_activate(GtkWidget
*menuitem
, gpointer user_data
)
1583 last_toolbutton_action
= user_data
;
1584 if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_MAKE_ALL
))
1585 msg
= _("Build the current file with Make and the default target");
1586 else if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_CUSTOM
))
1587 msg
= _("Build the current file with Make and the specified target");
1588 else if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT
))
1589 msg
= _("Compile the current file with Make");
1592 g_object_set(widgets
.build_action
, "tooltip", msg
, NULL
);
1593 on_build_menu_item(menuitem
, user_data
);
1597 static void kill_process(GPid
*pid
)
1599 GError
*error
= NULL
;
1601 if (spawn_kill_process(*pid
, &error
))
1604 build_menu_update(NULL
);
1608 ui_set_statusbar(TRUE
, _("Process could not be stopped (%s)."), error
->message
);
1609 g_error_free(error
);
1614 static void on_build_next_error(GtkWidget
*menuitem
, gpointer user_data
)
1616 if (ui_tree_view_find_next(GTK_TREE_VIEW(msgwindow
.tree_compiler
),
1617 msgwin_goto_compiler_file_line
))
1619 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
1622 ui_set_statusbar(FALSE
, _("No more build errors."));
1626 static void on_build_previous_error(GtkWidget
*menuitem
, gpointer user_data
)
1628 if (ui_tree_view_find_previous(GTK_TREE_VIEW(msgwindow
.tree_compiler
),
1629 msgwin_goto_compiler_file_line
))
1631 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_COMPILER
);
1634 ui_set_statusbar(FALSE
, _("No more build errors."));
1638 void build_toolbutton_build_clicked(GtkAction
*action
, gpointer unused
)
1640 if (last_toolbutton_action
== GBO_TO_POINTER(GEANY_GBO_BUILD
))
1642 on_build_menu_item(NULL
, GBO_TO_POINTER(GEANY_GBO_BUILD
));
1646 on_build_menu_item(NULL
, last_toolbutton_action
);
1651 /*------------------------------------------------------
1653 * Create and handle the build menu configuration dialog
1655 *-------------------------------------------------------*/
1656 typedef struct RowWidgets
1658 GtkWidget
*entries
[GEANY_BC_CMDENTRIES_COUNT
];
1659 GeanyBuildSource src
;
1660 GeanyBuildSource dst
;
1661 GeanyBuildCommand
*cmdsrc
;
1668 #if GTK_CHECK_VERSION(3,0,0)
1669 typedef GdkRGBA InsensitiveColor
;
1671 typedef GdkColor InsensitiveColor
;
1673 static InsensitiveColor insensitive_color
;
1675 static void set_row_color(RowWidgets
*r
, InsensitiveColor
*color
)
1677 enum GeanyBuildCmdEntries i
;
1679 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1681 if (i
== GEANY_BC_LABEL
)
1684 #if GTK_CHECK_VERSION(3,0,0)
1685 gtk_widget_override_color(r
->entries
[i
], GTK_STATE_FLAG_NORMAL
, color
);
1687 gtk_widget_modify_text(r
->entries
[i
], GTK_STATE_NORMAL
, color
);
1693 static void set_build_command_entry_text(GtkWidget
*wid
, const gchar
*text
)
1695 if (GTK_IS_BUTTON(wid
))
1696 gtk_button_set_label(GTK_BUTTON(wid
), text
);
1698 gtk_entry_set_text(GTK_ENTRY(wid
), text
);
1702 static void on_clear_dialog_row(GtkWidget
*unused
, gpointer user_data
)
1704 RowWidgets
*r
= user_data
;
1706 enum GeanyBuildCmdEntries i
;
1707 GeanyBuildCommand
*bc
= get_next_build_cmd(NULL
, r
->grp
, r
->cmd
, r
->dst
, &src
);
1713 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1715 set_build_command_entry_text(r
->entries
[i
],
1716 id_to_str(bc
,i
) != NULL
? id_to_str(bc
,i
) : "");
1722 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1724 set_build_command_entry_text(r
->entries
[i
], "");
1727 r
->used_dst
= FALSE
;
1728 set_row_color(r
, &insensitive_color
);
1733 static void on_clear_dialog_regex_row(GtkEntry
*regex
, gpointer unused
)
1735 gtk_entry_set_text(regex
,"");
1739 static void on_label_button_clicked(GtkWidget
*wid
, gpointer user_data
)
1741 RowWidgets
*r
= user_data
;
1742 GtkWidget
*top_level
= gtk_widget_get_toplevel(wid
);
1743 const gchar
*old
= gtk_button_get_label(GTK_BUTTON(wid
));
1746 if (gtk_widget_is_toplevel(top_level
) && GTK_IS_WINDOW(top_level
))
1747 str
= dialogs_show_input(_("Set menu item label"), GTK_WINDOW(top_level
), NULL
, old
);
1749 str
= dialogs_show_input(_("Set menu item label"), NULL
, NULL
, old
);
1754 gtk_button_set_label(GTK_BUTTON(wid
), str
);
1757 set_row_color(r
, NULL
);
1761 static void on_entry_focus(GtkWidget
*wid
, GdkEventFocus
*unused
, gpointer user_data
)
1763 RowWidgets
*r
= user_data
;
1766 set_row_color(r
, NULL
);
1770 /* Column headings, array NULL-terminated */
1771 static const gchar
*colheads
[] =
1776 N_("Working directory"),
1783 #define DC_ENTRIES 1
1787 static const guint entry_x_padding
= 3;
1788 static const guint entry_y_padding
= 0;
1791 static RowWidgets
*build_add_dialog_row(GeanyDocument
*doc
, GtkTable
*table
, guint row
,
1792 GeanyBuildSource dst
, guint grp
, guint cmd
, gboolean dir
)
1794 GtkWidget
*label
, *clear
, *clearicon
;
1796 GeanyBuildCommand
*bc
;
1798 enum GeanyBuildCmdEntries i
;
1802 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, NULL
);
1804 text
= g_strdup_printf("%d.", cmd
+ 1);
1805 label
= gtk_label_new(text
);
1807 #if GTK_CHECK_VERSION(3,0,0)
1808 gtk_style_context_get_color(gtk_widget_get_style_context(label
), GTK_STATE_FLAG_INSENSITIVE
, &insensitive_color
);
1810 insensitive_color
= gtk_widget_get_style(label
)->text
[GTK_STATE_INSENSITIVE
];
1812 gtk_table_attach(table
, label
, column
, column
+ 1, row
, row
+ 1, GTK_FILL
,
1813 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1814 roww
= g_new0(RowWidgets
, 1);
1815 roww
->src
= GEANY_BCS_COUNT
;
1819 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1821 gint xflags
= (i
== GEANY_BC_COMMAND
) ? GTK_FILL
| GTK_EXPAND
: GTK_FILL
;
1824 if (i
== GEANY_BC_LABEL
)
1826 GtkWidget
*wid
= roww
->entries
[i
] = gtk_button_new();
1827 gtk_button_set_use_underline(GTK_BUTTON(wid
), TRUE
);
1828 gtk_widget_set_tooltip_text(wid
, _("Click to set menu item label"));
1829 g_signal_connect(wid
, "clicked", G_CALLBACK(on_label_button_clicked
), roww
);
1833 roww
->entries
[i
] = gtk_entry_new();
1834 g_signal_connect(roww
->entries
[i
], "focus-in-event", G_CALLBACK(on_entry_focus
), roww
);
1836 gtk_table_attach(table
, roww
->entries
[i
], column
, column
+ 1, row
, row
+ 1, xflags
,
1837 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1840 clearicon
= gtk_image_new_from_stock(GTK_STOCK_CLEAR
, GTK_ICON_SIZE_MENU
);
1841 clear
= gtk_button_new();
1842 gtk_button_set_image(GTK_BUTTON(clear
), clearicon
);
1843 g_signal_connect(clear
, "clicked", G_CALLBACK(on_clear_dialog_row
), roww
);
1844 gtk_table_attach(table
, clear
, column
, column
+ 1, row
, row
+ 1, GTK_FILL
,
1845 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1846 roww
->cmdsrc
= bc
= get_build_cmd(doc
, grp
, cmd
, &src
);
1850 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1852 const gchar
*str
= "";
1856 if ((str
= id_to_str(bc
, i
)) == NULL
)
1858 else if (dst
== src
)
1859 roww
->used_dst
= TRUE
;
1861 set_build_command_entry_text(roww
->entries
[i
], str
);
1863 if (bc
!= NULL
&& (dst
> src
))
1864 set_row_color(roww
, &insensitive_color
);
1865 if (bc
!= NULL
&& (src
> dst
|| (grp
== GEANY_GBG_FT
&& (doc
== NULL
|| doc
->file_type
== NULL
))))
1867 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
1868 gtk_widget_set_sensitive(roww
->entries
[i
], FALSE
);
1869 gtk_widget_set_sensitive(clear
, FALSE
);
1875 typedef struct BuildTableFields
1878 GtkWidget
*fileregex
;
1879 GtkWidget
*nonfileregex
;
1880 gchar
**fileregexstring
;
1881 gchar
**nonfileregexstring
;
1885 GtkWidget
*build_commands_table(GeanyDocument
*doc
, GeanyBuildSource dst
, BuildTableData
*table_data
,
1888 GtkWidget
*label
, *sep
, *clearicon
, *clear
;
1889 BuildTableFields
*fields
;
1893 guint col
, row
, cmdindex
;
1896 gboolean sensitivity
;
1897 guint sep_padding
= entry_y_padding
+ 3;
1899 table
= GTK_TABLE(gtk_table_new(build_items_count
+ 12, 5, FALSE
));
1900 fields
= g_new0(BuildTableFields
, 1);
1901 fields
->rows
= g_new0(RowWidgets
*, build_items_count
);
1902 for (ch
= colheads
, col
= 0; *ch
!= NULL
; ch
++, col
++)
1904 label
= gtk_label_new(_(*ch
));
1905 gtk_table_attach(table
, label
, col
, col
+ 1, 0, 1,
1906 GTK_FILL
, GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1908 sep
= gtk_hseparator_new();
1909 gtk_table_attach(table
, sep
, 0, DC_N_COL
, 1, 2, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1910 entry_x_padding
, sep_padding
);
1911 if (ft
!= NULL
&& ft
->id
!= GEANY_FILETYPES_NONE
)
1912 txt
= g_strdup_printf(_("%s commands"), ft
->name
);
1914 txt
= g_strdup_printf(_("%s commands"), _("No filetype"));
1916 label
= ui_label_new_bold(txt
);
1918 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1919 gtk_table_attach(table
, label
, 0, DC_N_COL
, 2, 3, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1920 entry_x_padding
, entry_y_padding
);
1921 for (row
= 3, cmdindex
= 0, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_FT
]; ++row
, ++cmdindex
, ++cmd
)
1922 fields
->rows
[cmdindex
] = build_add_dialog_row(doc
, table
, row
, dst
, GEANY_GBG_FT
, cmd
, FALSE
);
1923 label
= gtk_label_new(_("Error regular expression:"));
1924 gtk_table_attach(table
, label
, 0, DC_ENTRIES
+ 1, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1925 entry_x_padding
, entry_y_padding
);
1926 fields
->fileregex
= gtk_entry_new();
1927 fields
->fileregexstring
= build_get_regex(GEANY_GBG_FT
, NULL
, &src
);
1928 sensitivity
= (ft
== NULL
) ? FALSE
: TRUE
;
1929 if (fields
->fileregexstring
!= NULL
&& *(fields
->fileregexstring
) != NULL
)
1931 gtk_entry_set_text(GTK_ENTRY(fields
->fileregex
), *(fields
->fileregexstring
));
1933 sensitivity
= FALSE
;
1935 gtk_table_attach(table
, fields
->fileregex
, DC_ENTRIES
+ 1, DC_CLEAR
, row
, row
+ 1, GTK_FILL
,
1936 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1937 clearicon
= gtk_image_new_from_stock(GTK_STOCK_CLEAR
, GTK_ICON_SIZE_MENU
);
1938 clear
= gtk_button_new();
1939 gtk_button_set_image(GTK_BUTTON(clear
), clearicon
);
1940 g_signal_connect_swapped(clear
, "clicked",
1941 G_CALLBACK(on_clear_dialog_regex_row
), (fields
->fileregex
));
1942 gtk_table_attach(table
, clear
, DC_CLEAR
, DC_CLEAR
+ 1, row
, row
+ 1, GTK_FILL
,
1943 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1944 gtk_widget_set_sensitive(fields
->fileregex
, sensitivity
);
1945 gtk_widget_set_sensitive(clear
, sensitivity
);
1947 sep
= gtk_hseparator_new();
1948 gtk_table_attach(table
, sep
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1949 entry_x_padding
, sep_padding
);
1951 label
= ui_label_new_bold(_("Independent commands"));
1952 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1953 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1954 entry_x_padding
, entry_y_padding
);
1955 for (++row
, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_NON_FT
]; ++row
, ++cmdindex
, ++cmd
)
1956 fields
->rows
[cmdindex
] = build_add_dialog_row(
1957 doc
, table
, row
, dst
, GEANY_GBG_NON_FT
, cmd
, TRUE
);
1958 label
= gtk_label_new(_("Error regular expression:"));
1959 gtk_table_attach(table
, label
, 0, DC_ENTRIES
+ 1, row
, row
+ 1, GTK_FILL
,
1960 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1961 fields
->nonfileregex
= gtk_entry_new();
1962 fields
->nonfileregexstring
= build_get_regex(GEANY_GBG_NON_FT
, NULL
, &src
);
1964 if (fields
->nonfileregexstring
!= NULL
&& *(fields
->nonfileregexstring
) != NULL
)
1966 gtk_entry_set_text(GTK_ENTRY(fields
->nonfileregex
), *(fields
->nonfileregexstring
));
1967 sensitivity
= src
> dst
? FALSE
: TRUE
;
1969 gtk_table_attach(table
, fields
->nonfileregex
, DC_ENTRIES
+ 1, DC_CLEAR
, row
, row
+ 1, GTK_FILL
,
1970 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1971 clearicon
= gtk_image_new_from_stock(GTK_STOCK_CLEAR
, GTK_ICON_SIZE_MENU
);
1972 clear
= gtk_button_new();
1973 gtk_button_set_image(GTK_BUTTON(clear
), clearicon
);
1974 g_signal_connect_swapped(clear
, "clicked",
1975 G_CALLBACK(on_clear_dialog_regex_row
), (fields
->nonfileregex
));
1976 gtk_table_attach(table
, clear
, DC_CLEAR
, DC_CLEAR
+ 1, row
, row
+ 1, GTK_FILL
,
1977 GTK_FILL
| GTK_EXPAND
, entry_x_padding
, entry_y_padding
);
1978 gtk_widget_set_sensitive(fields
->nonfileregex
, sensitivity
);
1979 gtk_widget_set_sensitive(clear
, sensitivity
);
1981 label
= gtk_label_new(NULL
);
1982 ui_label_set_markup(GTK_LABEL(label
), "<i>%s</i>",
1983 _("Note: Item 2 opens a dialog and appends the response to the command."));
1984 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1985 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1986 entry_x_padding
, entry_y_padding
);
1988 sep
= gtk_hseparator_new();
1989 gtk_table_attach(table
, sep
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1990 entry_x_padding
, sep_padding
);
1992 label
= ui_label_new_bold(_("Execute commands"));
1993 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
1994 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
1995 entry_x_padding
, entry_y_padding
);
1996 for (++row
, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_EXEC
]; ++row
, ++cmdindex
, ++cmd
)
1997 fields
->rows
[cmdindex
] = build_add_dialog_row(doc
, table
, row
, dst
, GEANY_GBG_EXEC
, cmd
, TRUE
);
1998 sep
= gtk_hseparator_new();
1999 gtk_table_attach(table
, sep
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
2000 entry_x_padding
, sep_padding
);
2002 label
= gtk_label_new(NULL
);
2003 ui_label_set_markup(GTK_LABEL(label
), "<i>%s</i>",
2004 _("%d, %e, %f, %p, %l are substituted in command and directory fields, see manual for details."));
2005 gtk_misc_set_alignment(GTK_MISC(label
), 0.0, 0.5);
2006 gtk_table_attach(table
, label
, 0, DC_N_COL
, row
, row
+ 1, GTK_FILL
, GTK_FILL
| GTK_EXPAND
,
2007 entry_x_padding
, entry_y_padding
);
2008 /*printf("%d extra rows in dialog\n", row-build_items_count);*/
2010 *table_data
= fields
;
2011 return GTK_WIDGET(table
);
2015 void build_free_fields(BuildTableData table_data
)
2019 for (cmdindex
= 0; cmdindex
< build_items_count
; ++cmdindex
)
2020 g_free(table_data
->rows
[cmdindex
]);
2021 g_free(table_data
->rows
);
2026 /* string compare where null pointers match null or 0 length strings */
2028 static gint
stcmp(const gchar
*a
, const gchar
*b
)
2030 if (a
== NULL
&& b
== NULL
)
2032 if (a
== NULL
&& b
!= NULL
)
2034 if (a
!= NULL
&& b
== NULL
)
2036 return strcmp(a
, b
);
2041 static const gchar
*get_build_command_entry_text(GtkWidget
*wid
)
2043 if (GTK_IS_BUTTON(wid
))
2044 return gtk_button_get_label(GTK_BUTTON(wid
));
2046 return gtk_entry_get_text(GTK_ENTRY(wid
));
2050 static gboolean
read_row(BuildDestination
*dst
, BuildTableData table_data
, guint drow
, guint grp
, guint cmd
)
2052 gchar
*entries
[GEANY_BC_CMDENTRIES_COUNT
];
2053 gboolean changed
= FALSE
;
2054 enum GeanyBuildCmdEntries i
;
2056 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2058 entries
[i
] = g_strdup(get_build_command_entry_text(table_data
->rows
[drow
]->entries
[i
]));
2060 if (table_data
->rows
[drow
]->cleared
)
2062 if (dst
->dst
[grp
] != NULL
)
2064 if (*(dst
->dst
[grp
]) == NULL
)
2065 *(dst
->dst
[grp
]) = g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
2066 (*(dst
->dst
[grp
]))[cmd
].exists
= FALSE
;
2067 (*(dst
->dst
[grp
]))[cmd
].changed
= TRUE
;
2071 if (table_data
->rows
[drow
]->used_dst
== TRUE
)
2073 if (dst
->dst
[grp
] != NULL
)
2075 if (*(dst
->dst
[grp
]) == NULL
)
2076 *(dst
->dst
[grp
]) = g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
2077 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2078 set_command(&(*(dst
->dst
[grp
]))[cmd
], i
, entries
[i
]);
2079 (*(dst
->dst
[grp
]))[cmd
].exists
= TRUE
;
2080 (*(dst
->dst
[grp
]))[cmd
].changed
= TRUE
;
2086 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2093 static gboolean
read_regex(GtkWidget
*regexentry
, gchar
**src
, gchar
**dst
)
2095 gboolean changed
= FALSE
;
2096 const gchar
*reg
= gtk_entry_get_text(GTK_ENTRY(regexentry
));
2098 if (((src
== NULL
/* originally there was no regex */
2099 || *src
== NULL
) /* or it was NULL*/
2100 && !EMPTY(reg
)) /* and something was typed */
2101 || (src
!= NULL
/* originally there was a regex*/
2102 && (*src
== NULL
/* and either it was NULL */
2103 || strcmp(*src
, reg
) != 0))) /* or it has been changed */
2107 SETPTR(*dst
, g_strdup(reg
));
2115 static gboolean
build_read_commands(BuildDestination
*dst
, BuildTableData table_data
, gint response
)
2117 guint cmdindex
, cmd
;
2118 gboolean changed
= FALSE
;
2120 if (response
== GTK_RESPONSE_ACCEPT
)
2122 for (cmdindex
= 0, cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_FT
]; ++cmdindex
, ++cmd
)
2123 changed
|= read_row(dst
, table_data
, cmdindex
, GEANY_GBG_FT
, cmd
);
2124 for (cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_NON_FT
]; ++cmdindex
, ++cmd
)
2125 changed
|= read_row(dst
, table_data
, cmdindex
, GEANY_GBG_NON_FT
, cmd
);
2126 for (cmd
= 0; cmd
< build_groups_count
[GEANY_GBG_EXEC
]; ++cmdindex
, ++cmd
)
2127 changed
|= read_row(dst
, table_data
, cmdindex
, GEANY_GBG_EXEC
, cmd
);
2128 changed
|= read_regex(table_data
->fileregex
, table_data
->fileregexstring
, dst
->fileregexstr
);
2129 changed
|= read_regex(table_data
->nonfileregex
, table_data
->nonfileregexstring
, dst
->nonfileregexstr
);
2135 void build_read_project(GeanyFiletype
*ft
, BuildTableData build_properties
)
2137 BuildDestination menu_dst
;
2141 menu_dst
.dst
[GEANY_GBG_FT
] = &(ft
->priv
->projfilecmds
);
2142 menu_dst
.fileregexstr
= &(ft
->priv
->projerror_regex_string
);
2146 menu_dst
.dst
[GEANY_GBG_FT
] = NULL
;
2147 menu_dst
.fileregexstr
= NULL
;
2149 menu_dst
.dst
[GEANY_GBG_NON_FT
] = &non_ft_proj
;
2150 menu_dst
.dst
[GEANY_GBG_EXEC
] = &exec_proj
;
2151 menu_dst
.nonfileregexstr
= ®ex_proj
;
2153 build_read_commands(&menu_dst
, build_properties
, GTK_RESPONSE_ACCEPT
);
2157 static void show_build_commands_dialog(void)
2159 GtkWidget
*dialog
, *table
, *vbox
;
2160 GeanyDocument
*doc
= document_get_current();
2161 GeanyFiletype
*ft
= NULL
;
2162 const gchar
*title
= _("Set Build Commands");
2164 BuildTableData table_data
;
2165 BuildDestination prefdsts
;
2168 ft
= doc
->file_type
;
2169 dialog
= gtk_dialog_new_with_buttons(title
, GTK_WINDOW(main_widgets
.window
),
2170 GTK_DIALOG_DESTROY_WITH_PARENT
,
2171 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
2172 GTK_STOCK_OK
, GTK_RESPONSE_ACCEPT
, NULL
);
2173 table
= build_commands_table(doc
, GEANY_BCS_PREF
, &table_data
, ft
);
2174 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
2175 gtk_box_pack_start(GTK_BOX(vbox
), table
, TRUE
, TRUE
, 0);
2176 gtk_widget_show_all(dialog
);
2177 /* run modally to prevent user changing idx filetype */
2178 response
= gtk_dialog_run(GTK_DIALOG(dialog
));
2180 prefdsts
.dst
[GEANY_GBG_NON_FT
] = &non_ft_pref
;
2183 prefdsts
.dst
[GEANY_GBG_FT
] = &(ft
->priv
->homefilecmds
);
2184 prefdsts
.fileregexstr
= &(ft
->priv
->homeerror_regex_string
);
2185 prefdsts
.dst
[GEANY_GBG_EXEC
] = &(ft
->priv
->homeexeccmds
);
2189 prefdsts
.dst
[GEANY_GBG_FT
] = NULL
;
2190 prefdsts
.fileregexstr
= NULL
;
2191 prefdsts
.dst
[GEANY_GBG_EXEC
] = NULL
;
2193 prefdsts
.nonfileregexstr
= ®ex_pref
;
2194 if (build_read_commands(&prefdsts
, table_data
, response
) && ft
!= NULL
)
2195 filetypes_save_commands(ft
);
2196 build_free_fields(table_data
);
2198 build_menu_update(doc
);
2199 gtk_widget_destroy(dialog
);
2203 /* Creates the relevant build menu if necessary. */
2204 BuildMenuItems
*build_get_menu_items(gint filetype_idx
)
2206 BuildMenuItems
*items
;
2208 items
= &menu_items
;
2209 if (items
->menu
== NULL
)
2210 create_build_menu(items
);
2215 /*----------------------------------------------------------
2217 * Load and store configuration
2219 * ---------------------------------------------------------*/
2220 static const gchar
*build_grp_name
= "build-menu";
2222 /* config format for build-menu group is prefix_gg_nn_xx=value
2223 * where gg = FT, NF, EX for the command group
2224 * nn = 2 digit command number
2225 * xx = LB for label, CM for command and WD for working dir */
2226 static const gchar
*groups
[GEANY_GBG_COUNT
] = { "FT", "NF", "EX" };
2227 static const gchar
*fixedkey
="xx_xx_xx";
2229 #define set_key_grp(key, grp) (key[prefixlen + 0] = grp[0], key[prefixlen + 1] = grp[1])
2230 #define set_key_cmd(key, cmd) (key[prefixlen + 3] = cmd[0], key[prefixlen + 4] = cmd[1])
2231 #define set_key_fld(key, fld) (key[prefixlen + 6] = fld[0], key[prefixlen + 7] = fld[1])
2233 static void build_load_menu_grp(GKeyFile
*config
, GeanyBuildCommand
**dst
, gint grp
,
2234 gchar
*prefix
, gboolean loc
)
2237 gsize prefixlen
; /* NOTE prefixlen used in macros above */
2238 GeanyBuildCommand
*dstcmd
;
2240 static gchar cmdbuf
[3] = " ";
2243 *dst
= g_new0(GeanyBuildCommand
, build_groups_count
[grp
]);
2245 prefixlen
= prefix
== NULL
? 0 : strlen(prefix
);
2246 key
= g_strconcat(prefix
== NULL
? "" : prefix
, fixedkey
, NULL
);
2247 for (cmd
= 0; cmd
< build_groups_count
[grp
]; ++cmd
)
2251 break; /* ensure no buffer overflow */
2252 sprintf(cmdbuf
, "%02u", cmd
);
2253 set_key_grp(key
, groups
[grp
]);
2254 set_key_cmd(key
, cmdbuf
);
2255 set_key_fld(key
, "LB");
2257 label
= g_key_file_get_locale_string(config
, build_grp_name
, key
, NULL
, NULL
);
2259 label
= g_key_file_get_string(config
, build_grp_name
, key
, NULL
);
2262 dstcmd
[cmd
].exists
= TRUE
;
2263 SETPTR(dstcmd
[cmd
].label
, label
);
2264 set_key_fld(key
,"CM");
2265 SETPTR(dstcmd
[cmd
].command
,
2266 g_key_file_get_string(config
, build_grp_name
, key
, NULL
));
2267 set_key_fld(key
,"WD");
2268 SETPTR(dstcmd
[cmd
].working_dir
,
2269 g_key_file_get_string(config
, build_grp_name
, key
, NULL
));
2271 else dstcmd
[cmd
].exists
= FALSE
;
2277 /* for the specified source load new format build menu items or try to make some sense of
2278 * old format setings, not done perfectly but better than ignoring them */
2279 void build_load_menu(GKeyFile
*config
, GeanyBuildSource src
, gpointer p
)
2284 gchar
*value
, *basedir
, *makebasedir
;
2285 gboolean bvalue
= FALSE
;
2287 if (g_key_file_has_group(config
, build_grp_name
))
2292 ft
= (GeanyFiletype
*)p
;
2295 build_load_menu_grp(config
, &(ft
->priv
->filecmds
), GEANY_GBG_FT
, NULL
, TRUE
);
2296 build_load_menu_grp(config
, &(ft
->priv
->ftdefcmds
), GEANY_GBG_NON_FT
, NULL
, TRUE
);
2297 build_load_menu_grp(config
, &(ft
->priv
->execcmds
), GEANY_GBG_EXEC
, NULL
, TRUE
);
2298 SETPTR(ft
->error_regex_string
,
2299 g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2301 case GEANY_BCS_HOME_FT
:
2302 ft
= (GeanyFiletype
*)p
;
2305 build_load_menu_grp(config
, &(ft
->priv
->homefilecmds
), GEANY_GBG_FT
, NULL
, FALSE
);
2306 build_load_menu_grp(config
, &(ft
->priv
->homeexeccmds
), GEANY_GBG_EXEC
, NULL
, FALSE
);
2307 SETPTR(ft
->priv
->homeerror_regex_string
,
2308 g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2310 case GEANY_BCS_PREF
:
2311 build_load_menu_grp(config
, &non_ft_pref
, GEANY_GBG_NON_FT
, NULL
, FALSE
);
2312 build_load_menu_grp(config
, &exec_pref
, GEANY_GBG_EXEC
, NULL
, FALSE
);
2313 SETPTR(regex_pref
, g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2315 case GEANY_BCS_PROJ
:
2316 build_load_menu_grp(config
, &non_ft_proj
, GEANY_GBG_NON_FT
, NULL
, FALSE
);
2317 build_load_menu_grp(config
, &exec_proj
, GEANY_GBG_EXEC
, NULL
, FALSE
);
2318 SETPTR(regex_proj
, g_key_file_get_string(config
, build_grp_name
, "error_regex", NULL
));
2319 pj
= (GeanyProject
*)p
;
2322 ftlist
= g_key_file_get_string_list(config
, build_grp_name
, "filetypes", NULL
, NULL
);
2326 if (pj
->priv
->build_filetypes_list
== NULL
)
2327 pj
->priv
->build_filetypes_list
= g_ptr_array_new();
2328 g_ptr_array_set_size(pj
->priv
->build_filetypes_list
, 0);
2329 for (ftname
= ftlist
; *ftname
!= NULL
; ++ftname
)
2331 ft
= filetypes_lookup_by_name(*ftname
);
2334 gchar
*regkey
= g_strdup_printf("%serror_regex", *ftname
);
2335 g_ptr_array_add(pj
->priv
->build_filetypes_list
, ft
);
2336 SETPTR(ft
->priv
->projerror_regex_string
,
2337 g_key_file_get_string(config
, build_grp_name
, regkey
, NULL
));
2339 build_load_menu_grp(config
, &(ft
->priv
->projfilecmds
), GEANY_GBG_FT
, *ftname
, FALSE
);
2340 build_load_menu_grp(config
, &(ft
->priv
->projexeccmds
), GEANY_GBG_EXEC
, *ftname
, FALSE
);
2346 default: /* defaults don't load from config, see build_init */
2351 /* load old [build_settings] values if there is no value defined by [build-menu] */
2353 /* set GeanyBuildCommand if it doesn't already exist and there is a command */
2354 /* TODO: rewrite as function */
2355 #define ASSIGNIF(type, id, string, value) \
2357 gchar *ASSIGNF__value = (value); \
2358 if (!EMPTY(ASSIGNF__value) && ! type[GBO_TO_CMD(id)].exists) { \
2359 type[GBO_TO_CMD(id)].exists = TRUE; \
2360 SETPTR(type[GBO_TO_CMD(id)].label, g_strdup(string)); \
2361 SETPTR(type[GBO_TO_CMD(id)].command, ASSIGNF__value); \
2362 SETPTR(type[GBO_TO_CMD(id)].working_dir, NULL); \
2363 type[GBO_TO_CMD(id)].old = TRUE; \
2365 g_free(ASSIGNF__value); \
2371 ft
= (GeanyFiletype
*)p
;
2372 value
= g_key_file_get_string(config
, "build_settings", "compiler", NULL
);
2375 if (ft
->priv
->filecmds
== NULL
)
2376 ft
->priv
->filecmds
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_FT
]);
2377 ASSIGNIF(ft
->priv
->filecmds
, GEANY_GBO_COMPILE
, _("_Compile"), value
);
2379 value
= g_key_file_get_string(config
, "build_settings", "linker", NULL
);
2382 if (ft
->priv
->filecmds
== NULL
)
2383 ft
->priv
->filecmds
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_FT
]);
2384 ASSIGNIF(ft
->priv
->filecmds
, GEANY_GBO_BUILD
, _("_Build"), value
);
2386 value
= g_key_file_get_string(config
, "build_settings", "run_cmd", NULL
);
2389 if (ft
->priv
->execcmds
== NULL
)
2390 ft
->priv
->execcmds
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_EXEC
]);
2391 ASSIGNIF(ft
->priv
->execcmds
, GEANY_GBO_EXEC
, _("_Execute"), value
);
2393 if (ft
->error_regex_string
== NULL
)
2394 ft
->error_regex_string
= g_key_file_get_string(config
, "build_settings", "error_regex", NULL
);
2396 case GEANY_BCS_PROJ
:
2397 if (non_ft_pref
== NULL
)
2398 non_ft_pref
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_NON_FT
]);
2399 basedir
= project_get_base_path();
2400 if (basedir
== NULL
)
2401 basedir
= g_strdup("%d");
2402 bvalue
= g_key_file_get_boolean(config
, "project", "make_in_base_path", NULL
);
2404 makebasedir
= g_strdup(basedir
);
2406 makebasedir
= g_strdup("%d");
2407 if (non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)].old
)
2408 SETPTR(non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)].working_dir
, g_strdup(makebasedir
));
2409 if (non_ft_pref
[GBO_TO_CMD(GEANY_GBO_CUSTOM
)].old
)
2410 SETPTR(non_ft_pref
[GBO_TO_CMD(GEANY_GBO_CUSTOM
)].working_dir
, g_strdup(makebasedir
));
2411 if (non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)].old
)
2412 SETPTR(non_ft_pref
[GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)].working_dir
, g_strdup("%d"));
2413 value
= g_key_file_get_string(config
, "project", "run_cmd", NULL
);
2416 if (exec_proj
== NULL
)
2417 exec_proj
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_EXEC
]);
2418 if (! exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].exists
)
2420 exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].exists
= TRUE
;
2421 SETPTR(exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].label
, g_strdup(_("_Execute")));
2422 SETPTR(exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].command
, value
);
2423 SETPTR(exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].working_dir
, g_strdup(basedir
));
2424 exec_proj
[GBO_TO_CMD(GEANY_GBO_EXEC
)].old
= TRUE
;
2427 g_free(makebasedir
);
2430 case GEANY_BCS_PREF
:
2431 value
= g_key_file_get_string(config
, "tools", "make_cmd", NULL
);
2434 if (non_ft_pref
== NULL
)
2435 non_ft_pref
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_NON_FT
]);
2436 ASSIGNIF(non_ft_pref
, GEANY_GBO_CUSTOM
, _("Make Custom _Target..."),
2437 g_strdup_printf("%s ", value
));
2438 ASSIGNIF(non_ft_pref
, GEANY_GBO_MAKE_OBJECT
, _("Make _Object"),
2439 g_strdup_printf("%s %%e.o",value
));
2440 ASSIGNIF(non_ft_pref
, GEANY_GBO_MAKE_ALL
, _("_Make"), value
);
2449 static guint
build_save_menu_grp(GKeyFile
*config
, GeanyBuildCommand
*src
, gint grp
, gchar
*prefix
)
2452 gsize prefixlen
; /* NOTE prefixlen used in macros above */
2455 enum GeanyBuildCmdEntries i
;
2459 prefixlen
= prefix
== NULL
? 0 : strlen(prefix
);
2460 key
= g_strconcat(prefix
== NULL
? "" : prefix
, fixedkey
, NULL
);
2461 for (cmd
= 0; cmd
< build_groups_count
[grp
]; ++cmd
)
2463 if (src
[cmd
].exists
) ++count
;
2464 if (src
[cmd
].changed
)
2466 static gchar cmdbuf
[4] = " ";
2468 break; /* ensure no buffer overflow */
2469 sprintf(cmdbuf
, "%02u", cmd
);
2470 set_key_grp(key
, groups
[grp
]);
2471 set_key_cmd(key
, cmdbuf
);
2472 if (src
[cmd
].exists
)
2474 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2476 set_key_fld(key
, config_keys
[i
]);
2477 g_key_file_set_string(config
, build_grp_name
, key
, id_to_str(&src
[cmd
], i
));
2482 for (i
= 0; i
< GEANY_BC_CMDENTRIES_COUNT
; i
++)
2484 set_key_fld(key
, config_keys
[i
]);
2485 g_key_file_remove_key(config
, build_grp_name
, key
, NULL
);
2495 typedef struct ForEachData
2498 GPtrArray
*ft_names
;
2502 static void foreach_project_filetype(gpointer data
, gpointer user_data
)
2504 GeanyFiletype
*ft
= data
;
2505 ForEachData
*d
= user_data
;
2507 gchar
*regkey
= g_strdup_printf("%serror_regex", ft
->name
);
2509 i
+= build_save_menu_grp(d
->config
, ft
->priv
->projfilecmds
, GEANY_GBG_FT
, ft
->name
);
2510 i
+= build_save_menu_grp(d
->config
, ft
->priv
->projexeccmds
, GEANY_GBG_EXEC
, ft
->name
);
2511 if (!EMPTY(ft
->priv
->projerror_regex_string
))
2513 g_key_file_set_string(d
->config
, build_grp_name
, regkey
, ft
->priv
->projerror_regex_string
);
2517 g_key_file_remove_key(d
->config
, build_grp_name
, regkey
, NULL
);
2520 g_ptr_array_add(d
->ft_names
, ft
->name
);
2524 /* TODO: untyped ptr is too ugly (also for build_load_menu) */
2525 void build_save_menu(GKeyFile
*config
, gpointer ptr
, GeanyBuildSource src
)
2533 case GEANY_BCS_HOME_FT
:
2534 ft
= (GeanyFiletype
*)ptr
;
2537 build_save_menu_grp(config
, ft
->priv
->homefilecmds
, GEANY_GBG_FT
, NULL
);
2538 build_save_menu_grp(config
, ft
->priv
->homeexeccmds
, GEANY_GBG_EXEC
, NULL
);
2539 if (!EMPTY(ft
->priv
->homeerror_regex_string
))
2540 g_key_file_set_string(config
, build_grp_name
, "error_regex", ft
->priv
->homeerror_regex_string
);
2542 g_key_file_remove_key(config
, build_grp_name
, "error_regex", NULL
);
2544 case GEANY_BCS_PREF
:
2545 build_save_menu_grp(config
, non_ft_pref
, GEANY_GBG_NON_FT
, NULL
);
2546 build_save_menu_grp(config
, exec_pref
, GEANY_GBG_EXEC
, NULL
);
2547 if (!EMPTY(regex_pref
))
2548 g_key_file_set_string(config
, build_grp_name
, "error_regex", regex_pref
);
2550 g_key_file_remove_key(config
, build_grp_name
, "error_regex", NULL
);
2552 case GEANY_BCS_PROJ
:
2553 pj
= (GeanyProject
*)ptr
;
2554 build_save_menu_grp(config
, non_ft_proj
, GEANY_GBG_NON_FT
, NULL
);
2555 build_save_menu_grp(config
, exec_proj
, GEANY_GBG_EXEC
, NULL
);
2556 if (!EMPTY(regex_proj
))
2557 g_key_file_set_string(config
, build_grp_name
, "error_regex", regex_proj
);
2559 g_key_file_remove_key(config
, build_grp_name
, "error_regex", NULL
);
2560 if (pj
->priv
->build_filetypes_list
!= NULL
)
2562 data
.config
= config
;
2563 data
.ft_names
= g_ptr_array_new();
2564 g_ptr_array_foreach(pj
->priv
->build_filetypes_list
, foreach_project_filetype
, (gpointer
)(&data
));
2565 if (data
.ft_names
->pdata
!= NULL
)
2566 g_key_file_set_string_list(config
, build_grp_name
, "filetypes",
2567 (const gchar
**)(data
.ft_names
->pdata
), data
.ft_names
->len
);
2569 g_key_file_remove_key(config
, build_grp_name
, "filetypes", NULL
);
2570 g_ptr_array_free(data
.ft_names
, TRUE
);
2573 default: /* defaults and GEANY_BCS_FT can't save */
2579 /* FIXME: count is int only because calling code doesn't handle checking its value itself */
2580 void build_set_group_count(GeanyBuildGroup grp
, gint count
)
2584 g_return_if_fail(count
>= 0);
2586 if ((guint
) count
> build_groups_count
[grp
])
2587 build_groups_count
[grp
] = (guint
) count
;
2588 for (i
= 0, sum
= 0; i
< GEANY_GBG_COUNT
; ++i
)
2589 sum
+= build_groups_count
[i
];
2590 build_items_count
= sum
;
2594 /** Get the count of commands for the group
2596 * Get the number of commands in the group specified by @a grp.
2598 * @param grp the group of the specified menu item.
2600 * @return a count of the number of commands in the group
2604 guint
build_get_group_count(const GeanyBuildGroup grp
)
2606 g_return_val_if_fail(grp
< GEANY_GBG_COUNT
, 0);
2607 return build_groups_count
[grp
];
2611 static void on_project_close(void)
2613 /* remove project regexen */
2614 SETPTR(regex_proj
, NULL
);
2621 const gchar
*command
;
2622 const gchar
*working_dir
;
2623 GeanyBuildCommand
**ptr
;
2625 } default_cmds
[] = {
2626 { N_("_Make"), "make", NULL
, &non_ft_def
, GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)},
2627 { N_("Make Custom _Target..."), "make ", NULL
, &non_ft_def
, GBO_TO_CMD(GEANY_GBO_CUSTOM
)},
2628 { N_("Make _Object"), "make %e.o", NULL
, &non_ft_def
, GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)},
2629 { N_("_Execute"), "./%e", NULL
, &exec_def
, GBO_TO_CMD(GEANY_GBO_EXEC
)},
2630 { NULL
, NULL
, NULL
, NULL
, 0 }
2634 void build_init(void)
2637 GtkWidget
*toolmenu
;
2640 g_signal_connect(geany_object
, "project-close", on_project_close
, NULL
);
2642 ft_def
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_FT
]);
2643 non_ft_def
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_NON_FT
]);
2644 exec_def
= g_new0(GeanyBuildCommand
, build_groups_count
[GEANY_GBG_EXEC
]);
2645 run_info
= g_new0(RunInfo
, build_groups_count
[GEANY_GBG_EXEC
]);
2647 for (cmdindex
= 0; default_cmds
[cmdindex
].command
!= NULL
; ++cmdindex
)
2649 GeanyBuildCommand
*cmd
= &((*(default_cmds
[cmdindex
].ptr
))[ default_cmds
[cmdindex
].index
]);
2651 cmd
->label
= g_strdup(_(default_cmds
[cmdindex
].label
));
2652 cmd
->command
= g_strdup(default_cmds
[cmdindex
].command
);
2653 cmd
->working_dir
= g_strdup(default_cmds
[cmdindex
].working_dir
);
2656 /* create the toolbar Build item sub menu */
2657 toolmenu
= gtk_menu_new();
2658 g_object_ref(toolmenu
);
2660 /* build the code */
2661 item
= ui_image_menu_item_new(GEANY_STOCK_BUILD
, _("_Build"));
2662 gtk_widget_show(item
);
2663 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2664 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_build_activate
),
2665 GBO_TO_POINTER(GEANY_GBO_BUILD
));
2666 widgets
.toolitem_build
= item
;
2668 item
= gtk_separator_menu_item_new();
2669 gtk_widget_show(item
);
2670 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2672 /* build the code with make all */
2673 item
= gtk_image_menu_item_new_with_mnemonic(_("_Make All"));
2674 gtk_widget_show(item
);
2675 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2676 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_make_activate
),
2677 GBO_TO_POINTER(GEANY_GBO_MAKE_ALL
));
2678 widgets
.toolitem_make_all
= item
;
2680 /* build the code with make custom */
2681 item
= gtk_image_menu_item_new_with_mnemonic(_("Make Custom _Target..."));
2682 gtk_widget_show(item
);
2683 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2684 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_make_activate
),
2685 GBO_TO_POINTER(GEANY_GBO_CUSTOM
));
2686 widgets
.toolitem_make_custom
= item
;
2688 /* build the code with make object */
2689 item
= gtk_image_menu_item_new_with_mnemonic(_("Make _Object"));
2690 gtk_widget_show(item
);
2691 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2692 g_signal_connect(item
, "activate", G_CALLBACK(on_toolbutton_make_activate
),
2693 GBO_TO_POINTER(GEANY_GBO_MAKE_OBJECT
));
2694 widgets
.toolitem_make_object
= item
;
2696 item
= gtk_separator_menu_item_new();
2697 gtk_widget_show(item
);
2698 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2701 item
= ui_image_menu_item_new(GTK_STOCK_PREFERENCES
, _("_Set Build Commands"));
2702 gtk_widget_show(item
);
2703 gtk_container_add(GTK_CONTAINER(toolmenu
), item
);
2704 g_signal_connect(item
, "activate", G_CALLBACK(on_set_build_commands_activate
), NULL
);
2705 widgets
.toolitem_set_args
= item
;
2707 /* get toolbar action pointers */
2708 widgets
.build_action
= toolbar_get_action_by_name("Build");
2709 widgets
.compile_action
= toolbar_get_action_by_name("Compile");
2710 widgets
.run_action
= toolbar_get_action_by_name("Run");
2711 widgets
.toolmenu
= toolmenu
;
2712 /* set the submenu to the toolbar item */
2713 geany_menu_button_action_set_menu(GEANY_MENU_BUTTON_ACTION(widgets
.build_action
), toolmenu
);