2 * msgwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Message window functions (status, compiler, messages windows).
24 * Also compiler error message parsing and grep file and line parsing.
26 * @see GeanyMainWidgets::message_window_notebook to append a new notebook page.
33 #include "msgwindow.h"
37 #include "callbacks.h"
38 #include "filetypes.h"
39 #include "keybindings.h"
52 #include <gdk/gdkkeysyms.h>
55 /* used for parse_file_line */
58 const gchar
*string
; /* line data */
59 const gchar
*pattern
; /* pattern to split the error message into some fields */
60 guint min_fields
; /* used to detect errors after parsing */
61 guint line_idx
; /* idx of the field where the line is */
62 gint file_idx
; /* idx of the field where the filename is or -1 */
66 MessageWindow msgwindow
;
79 COMPILER_COL_COLOR
= 0,
85 static GdkColor color_error
= {0, 0xFFFF, 0, 0};
86 static GdkColor color_context
= {0, 0x7FFF, 0, 0};
87 static GdkColor color_message
= {0, 0, 0, 0xD000};
90 static void prepare_msg_tree_view(void);
91 static void prepare_status_tree_view(void);
92 static void prepare_compiler_tree_view(void);
93 static GtkWidget
*create_message_popup_menu(gint type
);
94 static gboolean
on_msgwin_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
96 static void on_scribble_populate(GtkTextView
*textview
, GtkMenu
*arg1
, gpointer user_data
);
99 void msgwin_show_hide_tabs(void)
101 ui_widget_show_hide(gtk_widget_get_parent(msgwindow
.tree_status
), interface_prefs
.msgwin_status_visible
);
102 ui_widget_show_hide(gtk_widget_get_parent(msgwindow
.tree_compiler
), interface_prefs
.msgwin_compiler_visible
);
103 ui_widget_show_hide(gtk_widget_get_parent(msgwindow
.tree_msg
), interface_prefs
.msgwin_messages_visible
);
104 ui_widget_show_hide(gtk_widget_get_parent(msgwindow
.scribble
), interface_prefs
.msgwin_scribble_visible
);
109 * Sets the Messages path for opening any parsed filenames without absolute path from message lines.
111 * @param messages_dir The directory.
114 void msgwin_set_messages_dir(const gchar
*messages_dir
)
116 g_free(msgwindow
.messages_dir
);
117 msgwindow
.messages_dir
= g_strdup(messages_dir
);
121 static void load_color(const gchar
*color_name
, GdkColor
*color
)
123 #if GTK_CHECK_VERSION(3, 0, 0)
125 GtkWidgetPath
*path
= gtk_widget_path_new();
126 GtkStyleContext
*ctx
= gtk_style_context_new();
128 gtk_widget_path_append_type(path
, GTK_TYPE_WINDOW
);
129 gtk_widget_path_iter_set_name(path
, -1, color_name
);
130 gtk_style_context_set_screen(ctx
, gdk_screen_get_default());
131 gtk_style_context_set_path(ctx
, path
);
132 gtk_style_context_get_color(ctx
, gtk_style_context_get_state(ctx
), &rgba_color
);
134 color
->red
= 0xffff * rgba_color
.red
;
135 color
->green
= 0xffff * rgba_color
.green
;
136 color
->blue
= 0xffff * rgba_color
.blue
;
138 gtk_widget_path_unref(path
);
141 gchar
*path
= g_strconcat("*.", color_name
, NULL
);
143 GtkSettings
*settings
= gtk_settings_get_default();
144 GtkStyle
*style
= gtk_rc_get_style_by_paths(settings
, path
, NULL
, GTK_TYPE_WIDGET
);
145 *color
= style
->fg
[GTK_STATE_NORMAL
];
152 void msgwin_init(void)
154 msgwindow
.notebook
= ui_lookup_widget(main_widgets
.window
, "notebook_info");
155 msgwindow
.tree_status
= ui_lookup_widget(main_widgets
.window
, "treeview3");
156 msgwindow
.tree_msg
= ui_lookup_widget(main_widgets
.window
, "treeview4");
157 msgwindow
.tree_compiler
= ui_lookup_widget(main_widgets
.window
, "treeview5");
158 msgwindow
.scribble
= ui_lookup_widget(main_widgets
.window
, "textview_scribble");
159 msgwindow
.messages_dir
= NULL
;
161 prepare_status_tree_view();
162 prepare_msg_tree_view();
163 prepare_compiler_tree_view();
164 msgwindow
.popup_status_menu
= create_message_popup_menu(MSG_STATUS
);
165 msgwindow
.popup_msg_menu
= create_message_popup_menu(MSG_MESSAGE
);
166 msgwindow
.popup_compiler_menu
= create_message_popup_menu(MSG_COMPILER
);
168 ui_widget_modify_font_from_string(msgwindow
.scribble
, interface_prefs
.msgwin_font
);
169 g_signal_connect(msgwindow
.scribble
, "populate-popup", G_CALLBACK(on_scribble_populate
), NULL
);
171 load_color("geany-compiler-error", &color_error
);
172 load_color("geany-compiler-context", &color_context
);
173 load_color("geany-compiler-message", &color_message
);
177 void msgwin_finalize(void)
179 g_free(msgwindow
.messages_dir
);
183 static gboolean
on_msgwin_key_press_event(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
185 gboolean enter_or_return
= ui_is_keyval_enter_or_return(event
->keyval
);
187 if (enter_or_return
|| event
->keyval
== GDK_space
)
189 switch (GPOINTER_TO_INT(data
))
192 { /* key press in the compiler treeview */
193 msgwin_goto_compiler_file_line(enter_or_return
);
197 { /* key press in the message treeview (results of 'Find usage') */
198 msgwin_goto_messages_file_line(enter_or_return
);
207 /* does some preparing things to the status message list widget */
208 static void prepare_status_tree_view(void)
210 GtkCellRenderer
*renderer
;
211 GtkTreeViewColumn
*column
;
213 msgwindow
.store_status
= gtk_list_store_new(1, G_TYPE_STRING
);
214 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow
.tree_status
), GTK_TREE_MODEL(msgwindow
.store_status
));
215 g_object_unref(msgwindow
.store_status
);
217 renderer
= gtk_cell_renderer_text_new();
218 column
= gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer
, "text", 0, NULL
);
219 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow
.tree_status
), column
);
221 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow
.tree_status
), FALSE
);
223 ui_widget_modify_font_from_string(msgwindow
.tree_status
, interface_prefs
.msgwin_font
);
225 g_signal_connect(msgwindow
.tree_status
, "button-press-event",
226 G_CALLBACK(on_msgwin_button_press_event
), GINT_TO_POINTER(MSG_STATUS
));
230 /* does some preparing things to the message list widget
231 * (currently used for showing results of 'Find usage') */
232 static void prepare_msg_tree_view(void)
234 GtkCellRenderer
*renderer
;
235 GtkTreeViewColumn
*column
;
236 GtkTreeSelection
*selection
;
238 /* line, doc id, fg, str */
239 msgwindow
.store_msg
= gtk_list_store_new(MSG_COL_COUNT
, G_TYPE_INT
, G_TYPE_UINT
,
240 GDK_TYPE_COLOR
, G_TYPE_STRING
);
241 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow
.tree_msg
), GTK_TREE_MODEL(msgwindow
.store_msg
));
242 g_object_unref(msgwindow
.store_msg
);
244 renderer
= gtk_cell_renderer_text_new();
245 column
= gtk_tree_view_column_new_with_attributes(NULL
, renderer
,
246 "foreground-gdk", MSG_COL_COLOR
, "text", MSG_COL_STRING
, NULL
);
247 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow
.tree_msg
), column
);
249 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow
.tree_msg
), FALSE
);
251 ui_widget_modify_font_from_string(msgwindow
.tree_msg
, interface_prefs
.msgwin_font
);
253 /* use button-release-event so the selection has changed
254 * (connect_after button-press-event doesn't work) */
255 g_signal_connect(msgwindow
.tree_msg
, "button-release-event",
256 G_CALLBACK(on_msgwin_button_press_event
), GINT_TO_POINTER(MSG_MESSAGE
));
257 /* for double-clicking only, after the first release */
258 g_signal_connect(msgwindow
.tree_msg
, "button-press-event",
259 G_CALLBACK(on_msgwin_button_press_event
), GINT_TO_POINTER(MSG_MESSAGE
));
260 g_signal_connect(msgwindow
.tree_msg
, "key-press-event",
261 G_CALLBACK(on_msgwin_key_press_event
), GINT_TO_POINTER(MSG_MESSAGE
));
263 /* selection handling */
264 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow
.tree_msg
));
265 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
266 /*g_signal_connect(selection, "changed",G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
270 /* does some preparing things to the compiler list widget */
271 static void prepare_compiler_tree_view(void)
273 GtkCellRenderer
*renderer
;
274 GtkTreeViewColumn
*column
;
275 GtkTreeSelection
*selection
;
277 msgwindow
.store_compiler
= gtk_list_store_new(COMPILER_COL_COUNT
, GDK_TYPE_COLOR
, G_TYPE_STRING
);
278 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow
.tree_compiler
), GTK_TREE_MODEL(msgwindow
.store_compiler
));
279 g_object_unref(msgwindow
.store_compiler
);
281 renderer
= gtk_cell_renderer_text_new();
282 column
= gtk_tree_view_column_new_with_attributes(NULL
, renderer
,
283 "foreground-gdk", COMPILER_COL_COLOR
, "text", COMPILER_COL_STRING
, NULL
);
284 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow
.tree_compiler
), column
);
286 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow
.tree_compiler
), FALSE
);
288 ui_widget_modify_font_from_string(msgwindow
.tree_compiler
, interface_prefs
.msgwin_font
);
290 /* use button-release-event so the selection has changed
291 * (connect_after button-press-event doesn't work) */
292 g_signal_connect(msgwindow
.tree_compiler
, "button-release-event",
293 G_CALLBACK(on_msgwin_button_press_event
), GINT_TO_POINTER(MSG_COMPILER
));
294 /* for double-clicking only, after the first release */
295 g_signal_connect(msgwindow
.tree_compiler
, "button-press-event",
296 G_CALLBACK(on_msgwin_button_press_event
), GINT_TO_POINTER(MSG_COMPILER
));
297 g_signal_connect(msgwindow
.tree_compiler
, "key-press-event",
298 G_CALLBACK(on_msgwin_key_press_event
), GINT_TO_POINTER(MSG_COMPILER
));
300 /* selection handling */
301 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow
.tree_compiler
));
302 gtk_tree_selection_set_mode(selection
, GTK_SELECTION_SINGLE
);
303 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
306 static const GdkColor
*get_color(gint msg_color
)
310 case COLOR_RED
: return &color_error
;
311 case COLOR_DARK_RED
: return &color_context
;
312 case COLOR_BLUE
: return &color_message
;
313 default: return NULL
;
319 * Adds a formatted message in the compiler tab treeview in the messages window.
321 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
322 * @param format @c printf()-style format string.
323 * @param ... Arguments for the @c format string.
325 * @see msgwin_compiler_add_string()
330 void msgwin_compiler_add(gint msg_color
, const gchar
*format
, ...)
335 va_start(args
, format
);
336 string
= g_strdup_vprintf(format
, args
);
338 msgwin_compiler_add_string(msg_color
, string
);
343 * Adds a new message in the compiler tab treeview in the messages window.
345 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
346 * @param msg Compiler message to be added.
348 * @see msgwin_compiler_add()
350 * @since 1.34 (API 236)
353 void msgwin_compiler_add_string(gint msg_color
, const gchar
*msg
)
356 const GdkColor
*color
= get_color(msg_color
);
359 if (! g_utf8_validate(msg
, -1, NULL
))
360 utf8_msg
= utils_get_utf8_from_locale(msg
);
362 utf8_msg
= (gchar
*) msg
;
364 gtk_list_store_append(msgwindow
.store_compiler
, &iter
);
365 gtk_list_store_set(msgwindow
.store_compiler
, &iter
,
366 COMPILER_COL_COLOR
, color
, COMPILER_COL_STRING
, utf8_msg
, -1);
368 if (ui_prefs
.msgwindow_visible
&& interface_prefs
.compiler_tab_autoscroll
)
370 GtkTreePath
*path
= gtk_tree_model_get_path(
371 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow
.tree_compiler
)), &iter
);
373 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow
.tree_compiler
), path
, NULL
, TRUE
, 0.5, 0.5);
374 gtk_tree_path_free(path
);
377 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
378 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item
[GBG_FIXED
][GBF_NEXT_ERROR
], TRUE
);
379 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item
[GBG_FIXED
][GBF_PREV_ERROR
], TRUE
);
386 void msgwin_show_hide(gboolean show
)
388 ui_prefs
.msgwindow_visible
= show
;
389 ignore_callback
= TRUE
;
390 gtk_check_menu_item_set_active(
391 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_messages_window1")),
393 ignore_callback
= FALSE
;
394 ui_widget_show_hide(main_widgets
.message_window_notebook
, show
);
395 /* set the input focus back to the editor */
396 keybindings_send_command(GEANY_KEY_GROUP_FOCUS
, GEANY_KEYS_FOCUS_EDITOR
);
401 * Adds a formatted message in the messages tab treeview in the messages window.
403 * If @a line and @a doc are set, clicking on this line jumps into the file
404 * which is specified by @a doc into the line specified with @a line.
406 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
407 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
408 * @param doc @nullable The document. Set to @c NULL to ignore.
409 * @param format @c printf()-style format string.
410 * @param ... Arguments for the @c format string.
412 * @see msgwin_msg_add_string()
417 void msgwin_msg_add(gint msg_color
, gint line
, GeanyDocument
*doc
, const gchar
*format
, ...)
422 va_start(args
, format
);
423 string
= g_strdup_vprintf(format
, args
);
426 msgwin_msg_add_string(msg_color
, line
, doc
, string
);
432 * Adds a new message in the messages tab treeview in the messages window.
434 * If @a line and @a doc are set, clicking on this line jumps into the
435 * file which is specified by @a doc into the line specified with @a line.
437 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
438 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
439 * @param doc @nullable The document. Set to @c NULL to ignore.
440 * @param string Message to be added.
442 * @see msgwin_msg_add()
444 * @since 1.34 (API 236)
447 void msgwin_msg_add_string(gint msg_color
, gint line
, GeanyDocument
*doc
, const gchar
*string
)
450 const GdkColor
*color
= get_color(msg_color
);
455 if (! ui_prefs
.msgwindow_visible
)
456 msgwin_show_hide(TRUE
);
458 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
459 * cut the string to a maximum of 1024 bytes and discard the rest */
460 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
461 len
= strlen(string
);
463 tmp
= g_strndup(string
, 1024);
465 tmp
= g_strdup(string
);
467 if (! g_utf8_validate(tmp
, -1, NULL
))
468 utf8_msg
= utils_get_utf8_from_locale(tmp
);
472 gtk_list_store_append(msgwindow
.store_msg
, &iter
);
473 gtk_list_store_set(msgwindow
.store_msg
, &iter
,
474 MSG_COL_LINE
, line
, MSG_COL_DOC_ID
, doc
? doc
->id
: 0, MSG_COL_COLOR
,
475 color
, MSG_COL_STRING
, utf8_msg
, -1);
484 * Logs a new status message *without* setting the status bar.
486 * Use @ref ui_set_statusbar() to display text on the statusbar.
488 * @param string Status message to be logged.
490 * @see msgwin_status_add()
492 * @since 1.34 (API 236)
495 void msgwin_status_add_string(const gchar
*string
)
498 gchar
*statusmsg
, *time_str
;
500 /* add a timestamp to status messages */
501 time_str
= utils_get_current_time_string();
502 statusmsg
= g_strconcat(time_str
, ": ", string
, NULL
);
505 /* add message to Status window */
506 gtk_list_store_append(msgwindow
.store_status
, &iter
);
507 gtk_list_store_set(msgwindow
.store_status
, &iter
, 0, statusmsg
, -1);
510 if (G_LIKELY(main_status
.main_window_realized
))
512 GtkTreePath
*path
= gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow
.tree_status
)), &iter
);
514 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow
.tree_status
), path
, NULL
, FALSE
, 0.0, 0.0);
515 if (prefs
.switch_to_status
)
516 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), MSG_STATUS
);
517 gtk_tree_path_free(path
);
522 * Logs a formatted status message *without* setting the status bar.
524 * Use @ref ui_set_statusbar() to display text on the statusbar.
526 * @param format @c printf()-style format string.
527 * @param ... Arguments for the @c format string.
529 * @see msgwin_status_add_string()
534 void msgwin_status_add(const gchar
*format
, ...)
539 va_start(args
, format
);
540 string
= g_strdup_vprintf(format
, args
);
543 msgwin_status_add_string(string
);
549 on_message_treeview_clear_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
551 gint tabnum
= GPOINTER_TO_INT(user_data
);
553 msgwin_clear_tab(tabnum
);
558 on_compiler_treeview_copy_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
560 GtkWidget
*tv
= NULL
;
561 GtkTreeSelection
*selection
;
564 gint str_idx
= COMPILER_COL_STRING
;
566 switch (GPOINTER_TO_INT(user_data
))
569 tv
= msgwindow
.tree_status
;
574 tv
= msgwindow
.tree_compiler
;
578 tv
= msgwindow
.tree_msg
;
579 str_idx
= MSG_COL_STRING
;
582 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tv
));
584 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
588 gtk_tree_model_get(model
, &iter
, str_idx
, &string
, -1);
591 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE
)),
599 static void on_compiler_treeview_copy_all_activate(GtkMenuItem
*menuitem
, gpointer user_data
)
601 GtkListStore
*store
= msgwindow
.store_compiler
;
603 GString
*str
= g_string_new("");
604 gint str_idx
= COMPILER_COL_STRING
;
607 switch (GPOINTER_TO_INT(user_data
))
610 store
= msgwindow
.store_status
;
619 store
= msgwindow
.store_msg
;
620 str_idx
= MSG_COL_STRING
;
624 /* walk through the list and copy every line into a string */
625 valid
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store
), &iter
);
630 gtk_tree_model_get(GTK_TREE_MODEL(store
), &iter
, str_idx
, &line
, -1);
633 g_string_append(str
, line
);
634 g_string_append_c(str
, '\n');
638 valid
= gtk_tree_model_iter_next(GTK_TREE_MODEL(store
), &iter
);
641 /* copy the string into the clipboard */
644 gtk_clipboard_set_text(
645 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE
)),
649 g_string_free(str
, TRUE
);
654 on_hide_message_window(GtkMenuItem
*menuitem
, gpointer user_data
)
656 msgwin_show_hide(FALSE
);
660 static GtkWidget
*create_message_popup_menu(gint type
)
662 GtkWidget
*message_popup_menu
, *clear
, *copy
, *copy_all
, *image
;
664 message_popup_menu
= gtk_menu_new();
666 clear
= gtk_image_menu_item_new_from_stock(GTK_STOCK_CLEAR
, NULL
);
667 gtk_widget_show(clear
);
668 gtk_container_add(GTK_CONTAINER(message_popup_menu
), clear
);
669 g_signal_connect(clear
, "activate",
670 G_CALLBACK(on_message_treeview_clear_activate
), GINT_TO_POINTER(type
));
672 copy
= gtk_image_menu_item_new_with_mnemonic(_("C_opy"));
673 gtk_widget_show(copy
);
674 gtk_container_add(GTK_CONTAINER(message_popup_menu
), copy
);
675 image
= gtk_image_new_from_stock(GTK_STOCK_COPY
, GTK_ICON_SIZE_MENU
);
676 gtk_widget_show(image
);
677 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy
), image
);
678 g_signal_connect(copy
, "activate",
679 G_CALLBACK(on_compiler_treeview_copy_activate
), GINT_TO_POINTER(type
));
681 copy_all
= gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
682 gtk_widget_show(copy_all
);
683 gtk_container_add(GTK_CONTAINER(message_popup_menu
), copy_all
);
684 image
= gtk_image_new_from_stock(GTK_STOCK_COPY
, GTK_ICON_SIZE_MENU
);
685 gtk_widget_show(image
);
686 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy_all
), image
);
687 g_signal_connect(copy_all
, "activate",
688 G_CALLBACK(on_compiler_treeview_copy_all_activate
), GINT_TO_POINTER(type
));
690 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu
));
692 return message_popup_menu
;
696 static void on_scribble_populate(GtkTextView
*textview
, GtkMenu
*arg1
, gpointer user_data
)
698 msgwin_menu_add_common_items(arg1
);
702 /* Menu items that should be on all message window popup menus */
703 void msgwin_menu_add_common_items(GtkMenu
*menu
)
707 item
= gtk_separator_menu_item_new();
708 gtk_widget_show(item
);
709 gtk_container_add(GTK_CONTAINER(menu
), item
);
711 item
= gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
712 gtk_widget_show(item
);
713 gtk_container_add(GTK_CONTAINER(menu
), item
);
714 g_signal_connect(item
, "activate", G_CALLBACK(on_hide_message_window
), NULL
);
718 /* look back up from the current path and find the directory we came from */
720 find_prev_build_dir(GtkTreePath
*cur
, GtkTreeModel
*model
, gchar
**prefix
)
725 while (gtk_tree_path_prev(cur
))
727 if (gtk_tree_model_get_iter(model
, &iter
, cur
))
730 gtk_tree_model_get(model
, &iter
, COMPILER_COL_STRING
, &string
, -1);
731 if (string
!= NULL
&& build_parse_make_dir(string
, prefix
))
744 static gboolean
goto_compiler_file_line(const gchar
*fname
, gint line
, gboolean focus_editor
)
746 gboolean ret
= FALSE
;
749 if (!fname
|| line
<= -1)
752 filename
= utils_get_locale_from_utf8(fname
);
754 /* If the path doesn't exist, try the current document.
755 * This happens when we receive build messages in the wrong order - after the
756 * 'Leaving directory' messages */
757 if (!g_file_test(filename
, G_FILE_TEST_EXISTS
))
759 gchar
*cur_dir
= utils_get_current_file_dir_utf8();
764 /* we let the user know we couldn't find the parsed filename from the message window */
765 SETPTR(cur_dir
, utils_get_locale_from_utf8(cur_dir
));
766 name
= g_path_get_basename(filename
);
767 SETPTR(name
, g_build_path(G_DIR_SEPARATOR_S
, cur_dir
, name
, NULL
));
770 if (g_file_test(name
, G_FILE_TEST_EXISTS
))
772 ui_set_statusbar(FALSE
, _("Could not find file '%s' - trying the current document path."),
774 SETPTR(filename
, name
);
782 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
783 GeanyDocument
*doc
= document_find_by_filename(utf8_filename
);
784 GeanyDocument
*old_doc
= document_get_current();
786 g_free(utf8_filename
);
788 if (doc
== NULL
) /* file not already open */
789 doc
= document_open_file(filename
, FALSE
, NULL
, NULL
);
793 if (! doc
->changed
&& editor_prefs
.use_indicators
) /* if modified, line may be wrong */
794 editor_indicator_set_on_line(doc
->editor
, GEANY_INDICATOR_ERROR
, line
- 1);
796 ret
= navqueue_goto_line(old_doc
, doc
, line
);
797 if (ret
&& focus_editor
)
798 gtk_widget_grab_focus(GTK_WIDGET(doc
->editor
->sci
));
810 gboolean
msgwin_goto_compiler_file_line(gboolean focus_editor
)
814 GtkTreeSelection
*selection
;
818 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow
.tree_compiler
));
819 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
821 /* if the item is not coloured red, it's not an error line */
822 gtk_tree_model_get(model
, &iter
, COMPILER_COL_COLOR
, &color
, -1);
823 if (color
== NULL
|| ! gdk_color_equal(color
, &color_error
))
826 gdk_color_free(color
);
829 gdk_color_free(color
);
831 gtk_tree_model_get(model
, &iter
, COMPILER_COL_STRING
, &string
, -1);
835 gchar
*filename
, *dir
;
839 path
= gtk_tree_model_get_path(model
, &iter
);
840 find_prev_build_dir(path
, model
, &dir
);
841 gtk_tree_path_free(path
);
842 msgwin_parse_compiler_error_line(string
, dir
, &filename
, &line
);
846 ret
= goto_compiler_file_line(filename
, line
, focus_editor
);
855 static void make_absolute(gchar
**filename
, const gchar
*dir
)
857 guint skip_dot_slash
= 0; /* number of characters to skip at the beginning of the filename */
859 if (*filename
== NULL
)
862 /* skip some characters at the beginning of the filename, at the moment only "./"
863 * can be extended if other "trash" is known */
864 if (strncmp(*filename
, "./", 2) == 0)
868 if (! utils_is_absolute_path(*filename
))
869 SETPTR(*filename
, g_build_filename(dir
, *filename
+ skip_dot_slash
, NULL
));
873 /* try to parse the file and line number where the error occurred described in line
874 * and when something useful is found, it stores the line number in *line and the
875 * relevant file with the error in *filename.
876 * *line will be -1 if no error was found in string.
877 * *filename must be freed unless it is NULL. */
878 static void parse_file_line(ParseData
*data
, gchar
**filename
, gint
*line
)
886 g_return_if_fail(data
->string
!= NULL
);
888 fields
= g_strsplit_set(data
->string
, data
->pattern
, data
->min_fields
);
891 if (g_strv_length(fields
) < data
->min_fields
)
897 *line
= strtol(fields
[data
->line_idx
], &end
, 10);
899 /* if the line could not be read, line is 0 and an error occurred, so we leave */
900 if (fields
[data
->line_idx
] == end
)
906 /* let's stop here if there is no filename in the error message */
907 if (data
->file_idx
== -1)
909 /* we have no filename in the error message, so take the current one and hope it's correct */
910 GeanyDocument
*doc
= document_get_current();
912 *filename
= g_strdup(doc
->file_name
);
917 *filename
= g_strdup(fields
[data
->file_idx
]);
922 static void parse_compiler_error_line(const gchar
*string
,
923 gchar
**filename
, gint
*line
)
925 ParseData data
= {NULL
, NULL
, 0, 0, 0};
927 data
.string
= string
;
929 switch (build_info
.file_type_id
)
931 case GEANY_FILETYPES_PHP
:
933 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
934 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
935 gchar
*tmp
= strstr(string
, " in ");
948 data
.min_fields
= 11;
954 case GEANY_FILETYPES_PERL
:
956 /* syntax error at test.pl line 7, near "{ */
963 /* the error output of python and tcl equals */
964 case GEANY_FILETYPES_TCL
:
965 case GEANY_FILETYPES_PYTHON
:
967 /* File "HyperArch.py", line 37, in ?
968 * (file "clrdial.tcl" line 12)
970 if (strstr(string
, " line ") != NULL
)
972 /* Tcl and old Python format (<= Python 2.5) */
973 data
.pattern
= " \"";
980 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
981 * (used since Python 2.6) */
989 case GEANY_FILETYPES_BASIC
:
990 case GEANY_FILETYPES_PASCAL
:
991 case GEANY_FILETYPES_CS
:
993 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
994 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
1001 case GEANY_FILETYPES_D
:
1003 /* GNU D compiler front-end, gdc
1004 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
1005 * warning - gantry.d:20: statement is not reachable
1006 * Digital Mars dmd compiler
1007 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
1008 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
1009 if (strncmp(string
, "warning - ", 10) == 0)
1011 data
.pattern
= " (:";
1012 data
.min_fields
= 4;
1018 data
.pattern
= "(:";
1019 data
.min_fields
= 2;
1025 case GEANY_FILETYPES_FERITE
:
1027 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
1028 * Error: Compile Error: on line 24, in /test/class.fe */
1029 if (strncmp(string
, "Error: Compile Error", 20) == 0)
1032 data
.min_fields
= 8;
1038 data
.pattern
= " \"";
1039 data
.min_fields
= 10;
1045 case GEANY_FILETYPES_HTML
:
1047 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
1049 data
.min_fields
= 4;
1054 /* All GNU gcc-like error messages */
1055 case GEANY_FILETYPES_C
:
1056 case GEANY_FILETYPES_CPP
:
1057 case GEANY_FILETYPES_RUBY
:
1058 case GEANY_FILETYPES_JAVA
:
1059 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
1060 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
1061 * empty.c:21: error: conflicting types for `foo'
1062 * Only parse file and line, so that linker errors will also work (with -g) */
1063 case GEANY_FILETYPES_F77
:
1064 case GEANY_FILETYPES_FORTRAN
:
1065 case GEANY_FILETYPES_LATEX
:
1066 /* ./kommtechnik_2b.tex:18: Emergency stop. */
1067 case GEANY_FILETYPES_MAKE
: /* Assume makefile is building with gcc */
1068 case GEANY_FILETYPES_NONE
:
1069 default: /* The default is a GNU gcc type error */
1071 if (build_info
.file_type_id
== GEANY_FILETYPES_JAVA
&&
1072 strncmp(string
, "[javac]", 7) == 0)
1075 * [javac] <Full Path to File + extension>:<line n°>: <error> */
1076 data
.pattern
= " :";
1077 data
.min_fields
= 4;
1082 /* don't accidentally find libtool versions x:y:x and think it is a file name */
1083 if (strstr(string
, "libtool --mode=link") == NULL
)
1086 data
.min_fields
= 3;
1094 if (data
.pattern
!= NULL
)
1095 parse_file_line(&data
, filename
, line
);
1099 /* try to parse the file and line number where the error occurred described in string
1100 * and when something useful is found, it stores the line number in *line and the
1101 * relevant file with the error in *filename.
1102 * *line will be -1 if no error was found in string.
1103 * *filename must be freed unless it is NULL. */
1104 void msgwin_parse_compiler_error_line(const gchar
*string
, const gchar
*dir
,
1105 gchar
**filename
, gint
*line
)
1108 gchar
*trimmed_string
, *utf8_dir
;
1113 if (G_UNLIKELY(string
== NULL
))
1117 utf8_dir
= utils_get_utf8_from_locale(build_info
.dir
);
1119 utf8_dir
= g_strdup(dir
);
1120 g_return_if_fail(utf8_dir
!= NULL
);
1122 trimmed_string
= g_strdup(string
);
1123 g_strchug(trimmed_string
); /* remove possible leading whitespace */
1125 ft
= filetypes
[build_info
.file_type_id
];
1127 /* try parsing with a custom regex */
1128 if (!filetypes_parse_error_message(ft
, trimmed_string
, filename
, line
))
1130 /* fallback to default old-style parsing */
1131 parse_compiler_error_line(trimmed_string
, filename
, line
);
1133 make_absolute(filename
, utf8_dir
);
1134 g_free(trimmed_string
);
1139 /* Tries to parse strings of the file:line style, allowing line field to be missing
1140 * * filename is filled with the filename, should be freed
1141 * * line is filled with the line number or -1 */
1142 static void msgwin_parse_generic_line(const gchar
*string
, gchar
**filename
, gint
*line
)
1145 gboolean incertain
= TRUE
; /* whether we're reasonably certain of the result */
1150 fields
= g_strsplit(string
, ":", 2);
1151 /* extract the filename */
1152 if (fields
[0] != NULL
)
1154 *filename
= utils_get_locale_from_utf8(fields
[0]);
1155 if (msgwindow
.messages_dir
!= NULL
)
1156 make_absolute(filename
, msgwindow
.messages_dir
);
1159 if (fields
[1] != NULL
)
1163 *line
= strtol(fields
[1], &end
, 10);
1164 if (end
== fields
[1])
1166 else if (*end
== ':' || g_ascii_isspace(*end
))
1167 { /* if we have a blank or a separator right after the number, assume we really got a
1168 * filename (it's a grep-like syntax) */
1173 /* if we aren't sure we got a supposedly correct filename, check it */
1174 if (incertain
&& ! g_file_test(*filename
, G_FILE_TEST_EXISTS
))
1176 SETPTR(*filename
, NULL
);
1184 gboolean
msgwin_goto_messages_file_line(gboolean focus_editor
)
1187 GtkTreeModel
*model
;
1188 GtkTreeSelection
*selection
;
1189 gboolean ret
= FALSE
;
1191 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow
.tree_msg
));
1192 if (gtk_tree_selection_get_selected(selection
, &model
, &iter
))
1198 GeanyDocument
*old_doc
= document_get_current();
1200 gtk_tree_model_get(model
, &iter
,
1201 MSG_COL_LINE
, &line
, MSG_COL_DOC_ID
, &id
, MSG_COL_STRING
, &string
, -1);
1202 if (line
>= 0 && id
> 0)
1204 /* check doc is still open */
1205 doc
= document_find_by_id(id
);
1208 ui_set_statusbar(FALSE
, _("The document has been closed."));
1213 ret
= navqueue_goto_line(old_doc
, doc
, line
);
1214 if (ret
&& focus_editor
)
1215 gtk_widget_grab_focus(GTK_WIDGET(doc
->editor
->sci
));
1218 else if (line
< 0 && string
!= NULL
)
1222 /* try with a file:line parsing */
1223 msgwin_parse_generic_line(string
, &filename
, &line
);
1224 if (filename
!= NULL
)
1226 /* use document_open_file to find an already open file, or open it in place */
1227 doc
= document_open_file(filename
, FALSE
, NULL
, NULL
);
1230 ret
= (line
< 0) ? TRUE
: navqueue_goto_line(old_doc
, doc
, line
);
1231 if (ret
&& focus_editor
)
1232 gtk_widget_grab_focus(GTK_WIDGET(doc
->editor
->sci
));
1243 static gboolean
on_msgwin_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
1246 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1247 gboolean double_click
= event
->type
== GDK_2BUTTON_PRESS
;
1249 if (event
->button
== 1 && (event
->type
== GDK_BUTTON_RELEASE
|| double_click
))
1251 switch (GPOINTER_TO_INT(user_data
))
1254 { /* mouse click in the compiler treeview */
1255 msgwin_goto_compiler_file_line(double_click
);
1259 { /* mouse click in the message treeview (results of 'Find usage') */
1260 msgwin_goto_messages_file_line(double_click
);
1264 return double_click
; /* TRUE prevents message window re-focusing */
1267 if (event
->button
== 3)
1268 { /* popupmenu to hide or clear the active treeview */
1269 switch (GPOINTER_TO_INT(user_data
))
1273 gtk_menu_popup(GTK_MENU(msgwindow
.popup_status_menu
), NULL
, NULL
, NULL
, NULL
,
1274 event
->button
, event
->time
);
1279 gtk_menu_popup(GTK_MENU(msgwindow
.popup_msg_menu
), NULL
, NULL
, NULL
, NULL
,
1280 event
->button
, event
->time
);
1285 gtk_menu_popup(GTK_MENU(msgwindow
.popup_compiler_menu
), NULL
, NULL
, NULL
, NULL
,
1286 event
->button
, event
->time
);
1296 * Switches to the given notebook tab of the messages window.
1298 * The messages window is shown if it was previously hidden and @a show is set to @c TRUE.
1300 * @param tabnum An index of a tab in the messages window. Valid values are
1301 * all elements of #MessageWindowTabNum.
1302 * @param show Whether to show the messages window at all if it was hidden before.
1307 void msgwin_switch_tab(gint tabnum
, gboolean show
)
1309 GtkWidget
*widget
= NULL
; /* widget to focus */
1313 case MSG_SCRATCH
: widget
= msgwindow
.scribble
; break;
1314 case MSG_COMPILER
: widget
= msgwindow
.tree_compiler
; break;
1315 case MSG_STATUS
: widget
= msgwindow
.tree_status
; break;
1316 case MSG_MESSAGE
: widget
= msgwindow
.tree_msg
; break;
1318 case MSG_VTE
: widget
= (vte_info
.have_vte
) ? vc
->vte
: NULL
; break;
1323 /* the msgwin must be visible before we switch to the VTE page so that
1324 * the font settings are applied on realization */
1326 msgwin_show_hide(TRUE
);
1327 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow
.notebook
), tabnum
);
1329 gtk_widget_grab_focus(widget
);
1334 * Removes all messages from a tab specified by @a tabnum in the messages window.
1336 * @param tabnum An index of a tab in the messages window which should be cleared.
1337 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1342 void msgwin_clear_tab(gint tabnum
)
1344 GtkListStore
*store
= NULL
;
1349 store
= msgwindow
.store_msg
;
1353 gtk_list_store_clear(msgwindow
.store_compiler
);
1354 build_menu_update(NULL
); /* update next error items */
1357 case MSG_STATUS
: store
= msgwindow
.store_status
; break;
1362 gtk_list_store_clear(store
);