Opening recent projects: Don't ask whether to close current project
[geany-mirror.git] / src / msgwindow.c
blobadb14474027434bebfad9124eeb41888aa9ae1b4
1 /*
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.
21 /**
22 * @file msgwindow.h
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.
27 **/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
33 #include "msgwindow.h"
35 #include "build.h"
36 #include "document.h"
37 #include "callbacks.h"
38 #include "filetypes.h"
39 #include "keybindings.h"
40 #include "main.h"
41 #include "navqueue.h"
42 #include "prefs.h"
43 #include "support.h"
44 #include "ui_utils.h"
45 #include "utils.h"
46 #include "vte.h"
48 #include <string.h>
49 #include <stdlib.h>
50 #include <time.h>
52 #include <gdk/gdkkeysyms.h>
55 /* used for parse_file_line */
56 typedef struct
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 */
64 ParseData;
66 MessageWindow msgwindow;
68 enum
70 MSG_COL_LINE = 0,
71 MSG_COL_DOC_ID,
72 MSG_COL_COLOR,
73 MSG_COL_STRING,
74 MSG_COL_COUNT
77 enum
79 COMPILER_COL_COLOR = 0,
80 COMPILER_COL_STRING,
81 COMPILER_COL_COUNT
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,
95 gpointer user_data);
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.
113 GEANY_API_SYMBOL
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)
124 GdkRGBA rgba_color;
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);
139 g_object_unref(ctx);
140 #else
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];
147 g_free(path);
148 #endif
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))
191 case MSG_COMPILER:
192 { /* key press in the compiler treeview */
193 msgwin_goto_compiler_file_line(enter_or_return);
194 break;
196 case MSG_MESSAGE:
197 { /* key press in the message treeview (results of 'Find usage') */
198 msgwin_goto_messages_file_line(enter_or_return);
199 break;
203 return FALSE;
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)
308 switch (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()
327 * @since 0.16
329 GEANY_API_SYMBOL
330 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
332 gchar *string;
333 va_list args;
335 va_start(args, format);
336 string = g_strdup_vprintf(format, args);
337 va_end(args);
338 msgwin_compiler_add_string(msg_color, string);
339 g_free(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)
352 GEANY_API_SYMBOL
353 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
355 GtkTreeIter iter;
356 const GdkColor *color = get_color(msg_color);
357 gchar *utf8_msg;
359 if (! g_utf8_validate(msg, -1, NULL))
360 utf8_msg = utils_get_utf8_from_locale(msg);
361 else
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);
381 if (utf8_msg != msg)
382 g_free(utf8_msg);
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")),
392 show);
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()
414 * @since 0.16
416 GEANY_API_SYMBOL
417 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
419 gchar *string;
420 va_list args;
422 va_start(args, format);
423 string = g_strdup_vprintf(format, args);
424 va_end(args);
426 msgwin_msg_add_string(msg_color, line, doc, string);
427 g_free(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)
446 GEANY_API_SYMBOL
447 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
449 GtkTreeIter iter;
450 const GdkColor *color = get_color(msg_color);
451 gchar *tmp;
452 gsize len;
453 gchar *utf8_msg;
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);
462 if (len > 1024)
463 tmp = g_strndup(string, 1024);
464 else
465 tmp = g_strdup(string);
467 if (! g_utf8_validate(tmp, -1, NULL))
468 utf8_msg = utils_get_utf8_from_locale(tmp);
469 else
470 utf8_msg = 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);
477 g_free(tmp);
478 if (utf8_msg != tmp)
479 g_free(utf8_msg);
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)
494 GEANY_API_SYMBOL
495 void msgwin_status_add_string(const gchar *string)
497 GtkTreeIter iter;
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);
503 g_free(time_str);
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);
508 g_free(statusmsg);
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()
531 * @since 0.12
533 GEANY_API_SYMBOL
534 void msgwin_status_add(const gchar *format, ...)
536 gchar *string;
537 va_list args;
539 va_start(args, format);
540 string = g_strdup_vprintf(format, args);
541 va_end(args);
543 msgwin_status_add_string(string);
544 g_free(string);
548 static void
549 on_message_treeview_clear_activate(GtkMenuItem *menuitem, gpointer user_data)
551 gint tabnum = GPOINTER_TO_INT(user_data);
553 msgwin_clear_tab(tabnum);
557 static void
558 on_compiler_treeview_copy_activate(GtkMenuItem *menuitem, gpointer user_data)
560 GtkWidget *tv = NULL;
561 GtkTreeSelection *selection;
562 GtkTreeModel *model;
563 GtkTreeIter iter;
564 gint str_idx = COMPILER_COL_STRING;
566 switch (GPOINTER_TO_INT(user_data))
568 case MSG_STATUS:
569 tv = msgwindow.tree_status;
570 str_idx = 0;
571 break;
573 case MSG_COMPILER:
574 tv = msgwindow.tree_compiler;
575 break;
577 case MSG_MESSAGE:
578 tv = msgwindow.tree_msg;
579 str_idx = MSG_COL_STRING;
580 break;
582 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
584 if (gtk_tree_selection_get_selected(selection, &model, &iter))
586 gchar *string;
588 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
589 if (!EMPTY(string))
591 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
592 string, -1);
594 g_free(string);
599 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
601 GtkListStore *store = msgwindow.store_compiler;
602 GtkTreeIter iter;
603 GString *str = g_string_new("");
604 gint str_idx = COMPILER_COL_STRING;
605 gboolean valid;
607 switch (GPOINTER_TO_INT(user_data))
609 case MSG_STATUS:
610 store = msgwindow.store_status;
611 str_idx = 0;
612 break;
614 case MSG_COMPILER:
615 /* default values */
616 break;
618 case MSG_MESSAGE:
619 store = msgwindow.store_msg;
620 str_idx = MSG_COL_STRING;
621 break;
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);
626 while (valid)
628 gchar *line;
630 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
631 if (!EMPTY(line))
633 g_string_append(str, line);
634 g_string_append_c(str, '\n');
636 g_free(line);
638 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
641 /* copy the string into the clipboard */
642 if (str->len > 0)
644 gtk_clipboard_set_text(
645 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
646 str->str,
647 str->len);
649 g_string_free(str, TRUE);
653 static void
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)
705 GtkWidget *item;
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 */
719 static gboolean
720 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
722 GtkTreeIter iter;
723 *prefix = NULL;
725 while (gtk_tree_path_prev(cur))
727 if (gtk_tree_model_get_iter(model, &iter, cur))
729 gchar *string;
730 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
731 if (string != NULL && build_parse_make_dir(string, prefix))
733 g_free(string);
734 return TRUE;
736 g_free(string);
740 return FALSE;
744 static gboolean goto_compiler_file_line(const gchar *fname, gint line, gboolean focus_editor)
746 gboolean ret = FALSE;
747 gchar *filename;
749 if (!fname || line <= -1)
750 return FALSE;
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();
760 gchar *name;
762 if (cur_dir)
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));
768 g_free(cur_dir);
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."),
773 fname);
774 SETPTR(filename, name);
776 else
777 g_free(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);
791 if (doc != 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));
800 ret = TRUE;
804 g_free(filename);
806 return ret;
810 gboolean msgwin_goto_compiler_file_line(gboolean focus_editor)
812 GtkTreeIter iter;
813 GtkTreeModel *model;
814 GtkTreeSelection *selection;
815 gchar *string;
816 GdkColor *color;
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))
825 if (color != NULL)
826 gdk_color_free(color);
827 return FALSE;
829 gdk_color_free(color);
831 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
832 if (string != NULL)
834 gint line;
835 gchar *filename, *dir;
836 GtkTreePath *path;
837 gboolean ret;
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);
843 g_free(string);
844 g_free(dir);
846 ret = goto_compiler_file_line(filename, line, focus_editor);
847 g_free(filename);
848 return ret;
851 return FALSE;
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)
860 return;
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)
865 skip_dot_slash = 2;
867 /* add directory */
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)
880 gchar *end = NULL;
881 gchar **fields;
883 *filename = NULL;
884 *line = -1;
886 g_return_if_fail(data->string != NULL);
888 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
890 /* parse the line */
891 if (g_strv_length(fields) < data->min_fields)
893 g_strfreev(fields);
894 return;
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)
902 g_strfreev(fields);
903 return;
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();
911 if (doc != NULL)
912 *filename = g_strdup(doc->file_name);
913 g_strfreev(fields);
914 return;
917 *filename = g_strdup(fields[data->file_idx]);
918 g_strfreev(fields);
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 ");
937 if (tmp != NULL)
939 data.string = tmp;
940 data.pattern = " ";
941 data.min_fields = 6;
942 data.line_idx = 5;
943 data.file_idx = 2;
945 else
947 data.pattern = " ";
948 data.min_fields = 11;
949 data.line_idx = 10;
950 data.file_idx = 7;
952 break;
954 case GEANY_FILETYPES_PERL:
956 /* syntax error at test.pl line 7, near "{ */
957 data.pattern = " ";
958 data.min_fields = 6;
959 data.line_idx = 5;
960 data.file_idx = 3;
961 break;
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)
969 * */
970 if (strstr(string, " line ") != NULL)
972 /* Tcl and old Python format (<= Python 2.5) */
973 data.pattern = " \"";
974 data.min_fields = 6;
975 data.line_idx = 5;
976 data.file_idx = 2;
978 else
980 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
981 * (used since Python 2.6) */
982 data.pattern = ",'";
983 data.min_fields = 8;
984 data.line_idx = 6;
985 data.file_idx = 4;
987 break;
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 */
995 data.pattern = "(";
996 data.min_fields = 2;
997 data.line_idx = 1;
998 data.file_idx = 0;
999 break;
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;
1013 data.line_idx = 3;
1014 data.file_idx = 2;
1016 else
1018 data.pattern = "(:";
1019 data.min_fields = 2;
1020 data.line_idx = 1;
1021 data.file_idx = 0;
1023 break;
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)
1031 data.pattern = " ";
1032 data.min_fields = 8;
1033 data.line_idx = 5;
1034 data.file_idx = 7;
1036 else
1038 data.pattern = " \"";
1039 data.min_fields = 10;
1040 data.line_idx = 5;
1041 data.file_idx = 8;
1043 break;
1045 case GEANY_FILETYPES_HTML:
1047 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
1048 data.pattern = " ";
1049 data.min_fields = 4;
1050 data.line_idx = 1;
1051 data.file_idx = -1;
1052 break;
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)
1074 /* Java Apache Ant.
1075 * [javac] <Full Path to File + extension>:<line n°>: <error> */
1076 data.pattern = " :";
1077 data.min_fields = 4;
1078 data.line_idx = 2;
1079 data.file_idx = 1;
1080 break;
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)
1085 data.pattern = ":";
1086 data.min_fields = 3;
1087 data.line_idx = 1;
1088 data.file_idx = 0;
1089 break;
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)
1107 GeanyFiletype *ft;
1108 gchar *trimmed_string, *utf8_dir;
1110 *filename = NULL;
1111 *line = -1;
1113 if (G_UNLIKELY(string == NULL))
1114 return;
1116 if (dir == NULL)
1117 utf8_dir = utils_get_utf8_from_locale(build_info.dir);
1118 else
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);
1135 g_free(utf8_dir);
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)
1144 gchar **fields;
1145 gboolean incertain = TRUE; /* whether we're reasonably certain of the result */
1147 *filename = NULL;
1148 *line = -1;
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);
1158 /* now the line */
1159 if (fields[1] != NULL)
1161 gchar *end;
1163 *line = strtol(fields[1], &end, 10);
1164 if (end == fields[1])
1165 *line = -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) */
1169 incertain = FALSE;
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);
1177 *line = -1;
1180 g_strfreev(fields);
1184 gboolean msgwin_goto_messages_file_line(gboolean focus_editor)
1186 GtkTreeIter iter;
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))
1194 gint line;
1195 guint id;
1196 gchar *string;
1197 GeanyDocument *doc;
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);
1206 if (!doc)
1208 ui_set_statusbar(FALSE, _("The document has been closed."));
1209 utils_beep();
1211 else
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)
1220 gchar *filename;
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);
1228 if (doc != 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));
1235 g_free(filename);
1237 g_free(string);
1239 return ret;
1243 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1244 gpointer user_data)
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))
1253 case MSG_COMPILER:
1254 { /* mouse click in the compiler treeview */
1255 msgwin_goto_compiler_file_line(double_click);
1256 break;
1258 case MSG_MESSAGE:
1259 { /* mouse click in the message treeview (results of 'Find usage') */
1260 msgwin_goto_messages_file_line(double_click);
1261 break;
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))
1271 case MSG_STATUS:
1273 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1274 event->button, event->time);
1275 break;
1277 case MSG_MESSAGE:
1279 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1280 event->button, event->time);
1281 break;
1283 case MSG_COMPILER:
1285 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1286 event->button, event->time);
1287 break;
1291 return FALSE;
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.
1304 * @since 0.15
1306 GEANY_API_SYMBOL
1307 void msgwin_switch_tab(gint tabnum, gboolean show)
1309 GtkWidget *widget = NULL; /* widget to focus */
1311 switch (tabnum)
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;
1317 #ifdef HAVE_VTE
1318 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1319 #endif
1320 default: break;
1323 /* the msgwin must be visible before we switch to the VTE page so that
1324 * the font settings are applied on realization */
1325 if (show)
1326 msgwin_show_hide(TRUE);
1327 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1328 if (show && widget)
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.
1339 * @since 0.15
1341 GEANY_API_SYMBOL
1342 void msgwin_clear_tab(gint tabnum)
1344 GtkListStore *store = NULL;
1346 switch (tabnum)
1348 case MSG_MESSAGE:
1349 store = msgwindow.store_msg;
1350 break;
1352 case MSG_COMPILER:
1353 gtk_list_store_clear(msgwindow.store_compiler);
1354 build_menu_update(NULL); /* update next error items */
1355 return;
1357 case MSG_STATUS: store = msgwindow.store_status; break;
1358 default: return;
1360 if (store == NULL)
1361 return;
1362 gtk_list_store_clear(store);