Return GdkColor via out parameter rather than return value
[geany-mirror.git] / src / msgwindow.c
blob02e45234231112f590ade8efb2cffc080bb2a240
1 /*
2 * msgwindow.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>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /**
23 * @file msgwindow.h
24 * Message window functions (status, compiler, messages windows).
25 * Also compiler error message parsing and grep file and line parsing.
27 * @see GeanyMainWidgets::message_window_notebook to append a new notebook page.
28 **/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "msgwindow.h"
36 #include "build.h"
37 #include "document.h"
38 #include "callbacks.h"
39 #include "filetypes.h"
40 #include "keybindings.h"
41 #include "main.h"
42 #include "navqueue.h"
43 #include "prefs.h"
44 #include "support.h"
45 #include "ui_utils.h"
46 #include "utils.h"
47 #include "vte.h"
49 #include <string.h>
50 #include <stdlib.h>
51 #include <time.h>
53 #include <gdk/gdkkeysyms.h>
56 /* used for parse_file_line */
57 typedef struct
59 const gchar *string; /* line data */
60 const gchar *pattern; /* pattern to split the error message into some fields */
61 guint min_fields; /* used to detect errors after parsing */
62 guint line_idx; /* idx of the field where the line is */
63 gint file_idx; /* idx of the field where the filename is or -1 */
65 ParseData;
67 MessageWindow msgwindow;
69 enum
71 MSG_COL_LINE = 0,
72 MSG_COL_DOC_ID,
73 MSG_COL_COLOR,
74 MSG_COL_STRING,
75 MSG_COL_COUNT
78 enum
80 COMPILER_COL_COLOR = 0,
81 COMPILER_COL_STRING,
82 COMPILER_COL_COUNT
86 static GdkColor color_error = {0, 0xFFFF, 0, 0};
87 static GdkColor color_context = {0, 0x7FFF, 0, 0};
88 static GdkColor color_message = {0, 0, 0, 0xD000};
91 static void prepare_msg_tree_view(void);
92 static void prepare_status_tree_view(void);
93 static void prepare_compiler_tree_view(void);
94 static GtkWidget *create_message_popup_menu(gint type);
95 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
96 gpointer user_data);
97 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data);
100 void msgwin_show_hide_tabs(void)
102 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_status), interface_prefs.msgwin_status_visible);
103 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_compiler), interface_prefs.msgwin_compiler_visible);
104 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_msg), interface_prefs.msgwin_messages_visible);
105 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
110 * from message lines.
111 * @param messages_dir The directory. **/
112 GEANY_API_SYMBOL
113 void msgwin_set_messages_dir(const gchar *messages_dir)
115 g_free(msgwindow.messages_dir);
116 msgwindow.messages_dir = g_strdup(messages_dir);
120 void load_color(const gchar *color_name, GdkColor *color)
122 #if GTK_CHECK_VERSION(3, 0, 0)
123 GdkRGBA rgba_color;
124 GtkWidgetPath *path = gtk_widget_path_new();
125 GtkStyleContext *ctx = gtk_style_context_new();
127 gtk_widget_path_append_type(path, GTK_TYPE_WINDOW);
128 gtk_widget_path_iter_set_name(path, -1, color_name);
129 gtk_style_context_set_screen(ctx, gdk_screen_get_default());
130 gtk_style_context_set_path(ctx, path);
131 gtk_style_context_get_color(ctx, gtk_style_context_get_state(ctx), &rgba_color);
133 color->red = 0xffff * rgba_color.red;
134 color->green = 0xffff * rgba_color.green;
135 color->blue = 0xffff * rgba_color.blue;
137 gtk_widget_path_unref(path);
138 g_object_unref(ctx);
139 #else
140 gchar *path = g_strconcat("*.", color_name, NULL);
142 GtkSettings *settings = gtk_settings_get_default();
143 GtkStyle *style = gtk_rc_get_style_by_paths(settings, path, NULL, GTK_TYPE_WIDGET);
144 *color = style->fg[GTK_STATE_NORMAL];
146 g_free(path);
147 #endif
151 void msgwin_init(void)
153 msgwindow.notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
154 msgwindow.tree_status = ui_lookup_widget(main_widgets.window, "treeview3");
155 msgwindow.tree_msg = ui_lookup_widget(main_widgets.window, "treeview4");
156 msgwindow.tree_compiler = ui_lookup_widget(main_widgets.window, "treeview5");
157 msgwindow.scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
158 msgwindow.messages_dir = NULL;
160 prepare_status_tree_view();
161 prepare_msg_tree_view();
162 prepare_compiler_tree_view();
163 msgwindow.popup_status_menu = create_message_popup_menu(MSG_STATUS);
164 msgwindow.popup_msg_menu = create_message_popup_menu(MSG_MESSAGE);
165 msgwindow.popup_compiler_menu = create_message_popup_menu(MSG_COMPILER);
167 ui_widget_modify_font_from_string(msgwindow.scribble, interface_prefs.msgwin_font);
168 g_signal_connect(msgwindow.scribble, "populate-popup", G_CALLBACK(on_scribble_populate), NULL);
170 load_color("geany-compiler-error", &color_error);
171 load_color("geany-compiler-context", &color_context);
172 load_color("geany-compiler-message", &color_message);
176 void msgwin_finalize(void)
178 g_free(msgwindow.messages_dir);
182 static gboolean on_msgwin_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
184 gboolean enter_or_return = ui_is_keyval_enter_or_return(event->keyval);
186 if (enter_or_return || event->keyval == GDK_space)
188 switch (GPOINTER_TO_INT(data))
190 case MSG_COMPILER:
191 { /* key press in the compiler treeview */
192 msgwin_goto_compiler_file_line(enter_or_return);
193 break;
195 case MSG_MESSAGE:
196 { /* key press in the message treeview (results of 'Find usage') */
197 msgwin_goto_messages_file_line(enter_or_return);
198 break;
202 return FALSE;
206 /* does some preparing things to the status message list widget */
207 static void prepare_status_tree_view(void)
209 GtkCellRenderer *renderer;
210 GtkTreeViewColumn *column;
212 msgwindow.store_status = gtk_list_store_new(1, G_TYPE_STRING);
213 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_status), GTK_TREE_MODEL(msgwindow.store_status));
214 g_object_unref(msgwindow.store_status);
216 renderer = gtk_cell_renderer_text_new();
217 column = gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer, "text", 0, NULL);
218 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_status), column);
220 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_status), FALSE);
222 ui_widget_modify_font_from_string(msgwindow.tree_status, interface_prefs.msgwin_font);
224 g_signal_connect(msgwindow.tree_status, "button-press-event",
225 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_STATUS));
229 /* does some preparing things to the message list widget
230 * (currently used for showing results of 'Find usage') */
231 static void prepare_msg_tree_view(void)
233 GtkCellRenderer *renderer;
234 GtkTreeViewColumn *column;
235 GtkTreeSelection *selection;
237 /* line, doc id, fg, str */
238 msgwindow.store_msg = gtk_list_store_new(MSG_COL_COUNT, G_TYPE_INT, G_TYPE_UINT,
239 GDK_TYPE_COLOR, G_TYPE_STRING);
240 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_msg), GTK_TREE_MODEL(msgwindow.store_msg));
241 g_object_unref(msgwindow.store_msg);
243 renderer = gtk_cell_renderer_text_new();
244 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
245 "foreground-gdk", MSG_COL_COLOR, "text", MSG_COL_STRING, NULL);
246 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_msg), column);
248 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_msg), FALSE);
250 ui_widget_modify_font_from_string(msgwindow.tree_msg, interface_prefs.msgwin_font);
252 /* use button-release-event so the selection has changed
253 * (connect_after button-press-event doesn't work) */
254 g_signal_connect(msgwindow.tree_msg, "button-release-event",
255 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
256 /* for double-clicking only, after the first release */
257 g_signal_connect(msgwindow.tree_msg, "button-press-event",
258 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
259 g_signal_connect(msgwindow.tree_msg, "key-press-event",
260 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_MESSAGE));
262 /* selection handling */
263 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
264 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
265 /*g_signal_connect(selection, "changed",G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
269 /* does some preparing things to the compiler list widget */
270 static void prepare_compiler_tree_view(void)
272 GtkCellRenderer *renderer;
273 GtkTreeViewColumn *column;
274 GtkTreeSelection *selection;
276 msgwindow.store_compiler = gtk_list_store_new(COMPILER_COL_COUNT, GDK_TYPE_COLOR, G_TYPE_STRING);
277 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_compiler), GTK_TREE_MODEL(msgwindow.store_compiler));
278 g_object_unref(msgwindow.store_compiler);
280 renderer = gtk_cell_renderer_text_new();
281 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
282 "foreground-gdk", COMPILER_COL_COLOR, "text", COMPILER_COL_STRING, NULL);
283 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_compiler), column);
285 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_compiler), FALSE);
287 ui_widget_modify_font_from_string(msgwindow.tree_compiler, interface_prefs.msgwin_font);
289 /* use button-release-event so the selection has changed
290 * (connect_after button-press-event doesn't work) */
291 g_signal_connect(msgwindow.tree_compiler, "button-release-event",
292 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
293 /* for double-clicking only, after the first release */
294 g_signal_connect(msgwindow.tree_compiler, "button-press-event",
295 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
296 g_signal_connect(msgwindow.tree_compiler, "key-press-event",
297 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_COMPILER));
299 /* selection handling */
300 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
301 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
302 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
305 static const GdkColor *get_color(gint msg_color)
307 switch (msg_color)
309 case COLOR_RED: return &color_error;
310 case COLOR_DARK_RED: return &color_context;
311 case COLOR_BLUE: return &color_message;
312 default: return NULL;
318 * Adds a new message in the compiler tab treeview in the messages window.
320 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
321 * @param format @c printf()-style format string.
322 * @param ... Arguments for the @c format string.
324 GEANY_API_SYMBOL
325 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
327 gchar *string;
328 va_list args;
330 va_start(args, format);
331 string = g_strdup_vprintf(format, args);
332 va_end(args);
333 msgwin_compiler_add_string(msg_color, string);
334 g_free(string);
338 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
340 GtkTreeIter iter;
341 const GdkColor *color = get_color(msg_color);
342 gchar *utf8_msg;
344 if (! g_utf8_validate(msg, -1, NULL))
345 utf8_msg = utils_get_utf8_from_locale(msg);
346 else
347 utf8_msg = (gchar *) msg;
349 gtk_list_store_append(msgwindow.store_compiler, &iter);
350 gtk_list_store_set(msgwindow.store_compiler, &iter,
351 COMPILER_COL_COLOR, color, COMPILER_COL_STRING, utf8_msg, -1);
353 if (ui_prefs.msgwindow_visible && interface_prefs.compiler_tab_autoscroll)
355 GtkTreePath *path = gtk_tree_model_get_path(
356 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
358 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
359 gtk_tree_path_free(path);
362 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
363 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
364 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
366 if (utf8_msg != msg)
367 g_free(utf8_msg);
371 void msgwin_show_hide(gboolean show)
373 ui_prefs.msgwindow_visible = show;
374 ignore_callback = TRUE;
375 gtk_check_menu_item_set_active(
376 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
377 show);
378 ignore_callback = FALSE;
379 ui_widget_show_hide(main_widgets.message_window_notebook, show);
380 /* set the input focus back to the editor */
381 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
386 * Adds a new message in the messages tab treeview in the messages window.
387 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
388 * by @a doc into the line specified with @a line.
390 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
391 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
392 * @param doc The document. Set to @c NULL to ignore.
393 * @param format @c printf()-style format string.
394 * @param ... Arguments for the @c format string.
396 * @since 0.15
398 GEANY_API_SYMBOL
399 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
401 gchar *string;
402 va_list args;
404 va_start(args, format);
405 string = g_strdup_vprintf(format, args);
406 va_end(args);
408 msgwin_msg_add_string(msg_color, line, doc, string);
409 g_free(string);
413 /* adds string to the msg treeview */
414 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
416 GtkTreeIter iter;
417 const GdkColor *color = get_color(msg_color);
418 gchar *tmp;
419 gsize len;
420 gchar *utf8_msg;
422 if (! ui_prefs.msgwindow_visible)
423 msgwin_show_hide(TRUE);
425 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
426 * cut the string to a maximum of 1024 bytes and discard the rest */
427 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
428 len = strlen(string);
429 if (len > 1024)
430 tmp = g_strndup(string, 1024);
431 else
432 tmp = g_strdup(string);
434 if (! g_utf8_validate(tmp, -1, NULL))
435 utf8_msg = utils_get_utf8_from_locale(tmp);
436 else
437 utf8_msg = tmp;
439 gtk_list_store_append(msgwindow.store_msg, &iter);
440 gtk_list_store_set(msgwindow.store_msg, &iter,
441 MSG_COL_LINE, line, MSG_COL_DOC_ID, doc ? doc->id : 0, MSG_COL_COLOR,
442 color, MSG_COL_STRING, utf8_msg, -1);
444 g_free(tmp);
445 if (utf8_msg != tmp)
446 g_free(utf8_msg);
451 * Logs a status message *without* setting the status bar.
452 * (Use ui_set_statusbar() to display text on the statusbar)
454 * @param format @c printf()-style format string.
455 * @param ... Arguments for the @c format string.
457 GEANY_API_SYMBOL
458 void msgwin_status_add(const gchar *format, ...)
460 GtkTreeIter iter;
461 gchar *string;
462 gchar *statusmsg, *time_str;
463 va_list args;
465 va_start(args, format);
466 string = g_strdup_vprintf(format, args);
467 va_end(args);
469 /* add a timestamp to status messages */
470 time_str = utils_get_current_time_string();
471 statusmsg = g_strconcat(time_str, ": ", string, NULL);
472 g_free(time_str);
473 g_free(string);
475 /* add message to Status window */
476 gtk_list_store_append(msgwindow.store_status, &iter);
477 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
478 g_free(statusmsg);
480 if (G_LIKELY(main_status.main_window_realized))
482 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
484 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
485 if (prefs.switch_to_status)
486 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
487 gtk_tree_path_free(path);
492 static void
493 on_message_treeview_clear_activate(GtkMenuItem *menuitem, gpointer user_data)
495 gint tabnum = GPOINTER_TO_INT(user_data);
497 msgwin_clear_tab(tabnum);
501 static void
502 on_compiler_treeview_copy_activate(GtkMenuItem *menuitem, gpointer user_data)
504 GtkWidget *tv = NULL;
505 GtkTreeSelection *selection;
506 GtkTreeModel *model;
507 GtkTreeIter iter;
508 gint str_idx = COMPILER_COL_STRING;
510 switch (GPOINTER_TO_INT(user_data))
512 case MSG_STATUS:
513 tv = msgwindow.tree_status;
514 str_idx = 0;
515 break;
517 case MSG_COMPILER:
518 tv = msgwindow.tree_compiler;
519 break;
521 case MSG_MESSAGE:
522 tv = msgwindow.tree_msg;
523 str_idx = MSG_COL_STRING;
524 break;
526 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
528 if (gtk_tree_selection_get_selected(selection, &model, &iter))
530 gchar *string;
532 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
533 if (!EMPTY(string))
535 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
536 string, -1);
538 g_free(string);
543 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
545 GtkListStore *store = msgwindow.store_compiler;
546 GtkTreeIter iter;
547 GString *str = g_string_new("");
548 gint str_idx = COMPILER_COL_STRING;
549 gboolean valid;
551 switch (GPOINTER_TO_INT(user_data))
553 case MSG_STATUS:
554 store = msgwindow.store_status;
555 str_idx = 0;
556 break;
558 case MSG_COMPILER:
559 /* default values */
560 break;
562 case MSG_MESSAGE:
563 store = msgwindow.store_msg;
564 str_idx = MSG_COL_STRING;
565 break;
568 /* walk through the list and copy every line into a string */
569 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
570 while (valid)
572 gchar *line;
574 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
575 if (!EMPTY(line))
577 g_string_append(str, line);
578 g_string_append_c(str, '\n');
580 g_free(line);
582 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
585 /* copy the string into the clipboard */
586 if (str->len > 0)
588 gtk_clipboard_set_text(
589 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
590 str->str,
591 str->len);
593 g_string_free(str, TRUE);
597 static void
598 on_hide_message_window(GtkMenuItem *menuitem, gpointer user_data)
600 msgwin_show_hide(FALSE);
604 static GtkWidget *create_message_popup_menu(gint type)
606 GtkWidget *message_popup_menu, *clear, *copy, *copy_all, *image;
608 message_popup_menu = gtk_menu_new();
610 clear = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLEAR, NULL);
611 gtk_widget_show(clear);
612 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
613 g_signal_connect(clear, "activate",
614 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
616 copy = gtk_image_menu_item_new_with_mnemonic(_("C_opy"));
617 gtk_widget_show(copy);
618 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
619 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
620 gtk_widget_show(image);
621 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
622 g_signal_connect(copy, "activate",
623 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
625 copy_all = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
626 gtk_widget_show(copy_all);
627 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy_all);
628 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
629 gtk_widget_show(image);
630 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy_all), image);
631 g_signal_connect(copy_all, "activate",
632 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
634 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
636 return message_popup_menu;
640 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
642 msgwin_menu_add_common_items(arg1);
646 /* Menu items that should be on all message window popup menus */
647 void msgwin_menu_add_common_items(GtkMenu *menu)
649 GtkWidget *item;
651 item = gtk_separator_menu_item_new();
652 gtk_widget_show(item);
653 gtk_container_add(GTK_CONTAINER(menu), item);
655 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
656 gtk_widget_show(item);
657 gtk_container_add(GTK_CONTAINER(menu), item);
658 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
662 /* look back up from the current path and find the directory we came from */
663 static gboolean
664 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
666 GtkTreeIter iter;
667 *prefix = NULL;
669 while (gtk_tree_path_prev(cur))
671 if (gtk_tree_model_get_iter(model, &iter, cur))
673 gchar *string;
674 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
675 if (string != NULL && build_parse_make_dir(string, prefix))
677 g_free(string);
678 return TRUE;
680 g_free(string);
684 return FALSE;
688 static gboolean goto_compiler_file_line(const gchar *fname, gint line, gboolean focus_editor)
690 gboolean ret = FALSE;
691 gchar *filename;
693 if (!fname || line <= -1)
694 return FALSE;
696 filename = utils_get_locale_from_utf8(fname);
698 /* If the path doesn't exist, try the current document.
699 * This happens when we receive build messages in the wrong order - after the
700 * 'Leaving directory' messages */
701 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
703 gchar *cur_dir = utils_get_current_file_dir_utf8();
704 gchar *name;
706 if (cur_dir)
708 /* we let the user know we couldn't find the parsed filename from the message window */
709 SETPTR(cur_dir, utils_get_locale_from_utf8(cur_dir));
710 name = g_path_get_basename(filename);
711 SETPTR(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
712 g_free(cur_dir);
714 if (g_file_test(name, G_FILE_TEST_EXISTS))
716 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
717 fname);
718 SETPTR(filename, name);
720 else
721 g_free(name);
726 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
727 GeanyDocument *doc = document_find_by_filename(utf8_filename);
728 GeanyDocument *old_doc = document_get_current();
730 g_free(utf8_filename);
732 if (doc == NULL) /* file not already open */
733 doc = document_open_file(filename, FALSE, NULL, NULL);
735 if (doc != NULL)
737 if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
738 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
740 ret = navqueue_goto_line(old_doc, doc, line);
741 if (ret && focus_editor)
742 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
744 ret = TRUE;
748 g_free(filename);
750 return ret;
754 gboolean msgwin_goto_compiler_file_line(gboolean focus_editor)
756 GtkTreeIter iter;
757 GtkTreeModel *model;
758 GtkTreeSelection *selection;
759 gchar *string;
760 GdkColor *color;
762 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
763 if (gtk_tree_selection_get_selected(selection, &model, &iter))
765 /* if the item is not coloured red, it's not an error line */
766 gtk_tree_model_get(model, &iter, COMPILER_COL_COLOR, &color, -1);
767 if (color == NULL || ! gdk_color_equal(color, &color_error))
769 if (color != NULL)
770 gdk_color_free(color);
771 return FALSE;
773 gdk_color_free(color);
775 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
776 if (string != NULL)
778 gint line;
779 gchar *filename, *dir;
780 GtkTreePath *path;
781 gboolean ret;
783 path = gtk_tree_model_get_path(model, &iter);
784 find_prev_build_dir(path, model, &dir);
785 gtk_tree_path_free(path);
786 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
787 g_free(string);
788 g_free(dir);
790 ret = goto_compiler_file_line(filename, line, focus_editor);
791 g_free(filename);
792 return ret;
795 return FALSE;
799 static void make_absolute(gchar **filename, const gchar *dir)
801 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
803 if (*filename == NULL)
804 return;
806 /* skip some characters at the beginning of the filename, at the moment only "./"
807 * can be extended if other "trash" is known */
808 if (strncmp(*filename, "./", 2) == 0)
809 skip_dot_slash = 2;
811 /* add directory */
812 if (! utils_is_absolute_path(*filename))
813 SETPTR(*filename, g_build_filename(dir, *filename + skip_dot_slash, NULL));
817 /* try to parse the file and line number where the error occurred described in line
818 * and when something useful is found, it stores the line number in *line and the
819 * relevant file with the error in *filename.
820 * *line will be -1 if no error was found in string.
821 * *filename must be freed unless it is NULL. */
822 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
824 gchar *end = NULL;
825 gchar **fields;
827 *filename = NULL;
828 *line = -1;
830 g_return_if_fail(data->string != NULL);
832 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
834 /* parse the line */
835 if (g_strv_length(fields) < data->min_fields)
837 g_strfreev(fields);
838 return;
841 *line = strtol(fields[data->line_idx], &end, 10);
843 /* if the line could not be read, line is 0 and an error occurred, so we leave */
844 if (fields[data->line_idx] == end)
846 g_strfreev(fields);
847 return;
850 /* let's stop here if there is no filename in the error message */
851 if (data->file_idx == -1)
853 /* we have no filename in the error message, so take the current one and hope it's correct */
854 GeanyDocument *doc = document_get_current();
855 if (doc != NULL)
856 *filename = g_strdup(doc->file_name);
857 g_strfreev(fields);
858 return;
861 *filename = g_strdup(fields[data->file_idx]);
862 g_strfreev(fields);
866 static void parse_compiler_error_line(const gchar *string,
867 gchar **filename, gint *line)
869 ParseData data = {NULL, NULL, 0, 0, 0};
871 data.string = string;
873 switch (build_info.file_type_id)
875 case GEANY_FILETYPES_PHP:
877 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
878 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
879 gchar *tmp = strstr(string, " in ");
881 if (tmp != NULL)
883 data.string = tmp;
884 data.pattern = " ";
885 data.min_fields = 6;
886 data.line_idx = 5;
887 data.file_idx = 2;
889 else
891 data.pattern = " ";
892 data.min_fields = 11;
893 data.line_idx = 10;
894 data.file_idx = 7;
896 break;
898 case GEANY_FILETYPES_PERL:
900 /* syntax error at test.pl line 7, near "{ */
901 data.pattern = " ";
902 data.min_fields = 6;
903 data.line_idx = 5;
904 data.file_idx = 3;
905 break;
907 /* the error output of python and tcl equals */
908 case GEANY_FILETYPES_TCL:
909 case GEANY_FILETYPES_PYTHON:
911 /* File "HyperArch.py", line 37, in ?
912 * (file "clrdial.tcl" line 12)
913 * */
914 if (strstr(string, " line ") != NULL)
916 /* Tcl and old Python format (<= Python 2.5) */
917 data.pattern = " \"";
918 data.min_fields = 6;
919 data.line_idx = 5;
920 data.file_idx = 2;
922 else
924 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
925 * (used since Python 2.6) */
926 data.pattern = ",'";
927 data.min_fields = 8;
928 data.line_idx = 6;
929 data.file_idx = 4;
931 break;
933 case GEANY_FILETYPES_BASIC:
934 case GEANY_FILETYPES_PASCAL:
935 case GEANY_FILETYPES_CS:
937 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
938 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
939 data.pattern = "(";
940 data.min_fields = 2;
941 data.line_idx = 1;
942 data.file_idx = 0;
943 break;
945 case GEANY_FILETYPES_D:
947 /* GNU D compiler front-end, gdc
948 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
949 * warning - gantry.d:20: statement is not reachable
950 * Digital Mars dmd compiler
951 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
952 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
953 if (strncmp(string, "warning - ", 10) == 0)
955 data.pattern = " (:";
956 data.min_fields = 4;
957 data.line_idx = 3;
958 data.file_idx = 2;
960 else
962 data.pattern = "(:";
963 data.min_fields = 2;
964 data.line_idx = 1;
965 data.file_idx = 0;
967 break;
969 case GEANY_FILETYPES_FERITE:
971 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
972 * Error: Compile Error: on line 24, in /test/class.fe */
973 if (strncmp(string, "Error: Compile Error", 20) == 0)
975 data.pattern = " ";
976 data.min_fields = 8;
977 data.line_idx = 5;
978 data.file_idx = 7;
980 else
982 data.pattern = " \"";
983 data.min_fields = 10;
984 data.line_idx = 5;
985 data.file_idx = 8;
987 break;
989 case GEANY_FILETYPES_HTML:
991 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
992 data.pattern = " ";
993 data.min_fields = 4;
994 data.line_idx = 1;
995 data.file_idx = -1;
996 break;
998 /* All GNU gcc-like error messages */
999 case GEANY_FILETYPES_C:
1000 case GEANY_FILETYPES_CPP:
1001 case GEANY_FILETYPES_RUBY:
1002 case GEANY_FILETYPES_JAVA:
1003 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
1004 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
1005 * empty.c:21: error: conflicting types for `foo'
1006 * Only parse file and line, so that linker errors will also work (with -g) */
1007 case GEANY_FILETYPES_F77:
1008 case GEANY_FILETYPES_FORTRAN:
1009 case GEANY_FILETYPES_LATEX:
1010 /* ./kommtechnik_2b.tex:18: Emergency stop. */
1011 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
1012 case GEANY_FILETYPES_NONE:
1013 default: /* The default is a GNU gcc type error */
1015 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
1016 strncmp(string, "[javac]", 7) == 0)
1018 /* Java Apache Ant.
1019 * [javac] <Full Path to File + extension>:<line n°>: <error> */
1020 data.pattern = " :";
1021 data.min_fields = 4;
1022 data.line_idx = 2;
1023 data.file_idx = 1;
1024 break;
1026 /* don't accidentally find libtool versions x:y:x and think it is a file name */
1027 if (strstr(string, "libtool --mode=link") == NULL)
1029 data.pattern = ":";
1030 data.min_fields = 3;
1031 data.line_idx = 1;
1032 data.file_idx = 0;
1033 break;
1038 if (data.pattern != NULL)
1039 parse_file_line(&data, filename, line);
1043 /* try to parse the file and line number where the error occurred described in string
1044 * and when something useful is found, it stores the line number in *line and the
1045 * relevant file with the error in *filename.
1046 * *line will be -1 if no error was found in string.
1047 * *filename must be freed unless it is NULL. */
1048 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
1049 gchar **filename, gint *line)
1051 GeanyFiletype *ft;
1052 gchar *trimmed_string, *utf8_dir;
1054 *filename = NULL;
1055 *line = -1;
1057 if (G_UNLIKELY(string == NULL))
1058 return;
1060 if (dir == NULL)
1061 utf8_dir = utils_get_utf8_from_locale(build_info.dir);
1062 else
1063 utf8_dir = g_strdup(dir);
1064 g_return_if_fail(utf8_dir != NULL);
1066 trimmed_string = g_strdup(string);
1067 g_strchug(trimmed_string); /* remove possible leading whitespace */
1069 ft = filetypes[build_info.file_type_id];
1071 /* try parsing with a custom regex */
1072 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
1074 /* fallback to default old-style parsing */
1075 parse_compiler_error_line(trimmed_string, filename, line);
1077 make_absolute(filename, utf8_dir);
1078 g_free(trimmed_string);
1079 g_free(utf8_dir);
1083 /* Tries to parse strings of the file:line style, allowing line field to be missing
1084 * * filename is filled with the filename, should be freed
1085 * * line is filled with the line number or -1 */
1086 static void msgwin_parse_generic_line(const gchar *string, gchar **filename, gint *line)
1088 gchar **fields;
1089 gboolean incertain = TRUE; /* whether we're reasonably certain of the result */
1091 *filename = NULL;
1092 *line = -1;
1094 fields = g_strsplit(string, ":", 2);
1095 /* extract the filename */
1096 if (fields[0] != NULL)
1098 *filename = utils_get_locale_from_utf8(fields[0]);
1099 if (msgwindow.messages_dir != NULL)
1100 make_absolute(filename, msgwindow.messages_dir);
1102 /* now the line */
1103 if (fields[1] != NULL)
1105 gchar *end;
1107 *line = strtol(fields[1], &end, 10);
1108 if (end == fields[1])
1109 *line = -1;
1110 else if (*end == ':' || g_ascii_isspace(*end))
1111 { /* if we have a blank or a separator right after the number, assume we really got a
1112 * filename (it's a grep-like syntax) */
1113 incertain = FALSE;
1117 /* if we aren't sure we got a supposedly correct filename, check it */
1118 if (incertain && ! g_file_test(*filename, G_FILE_TEST_EXISTS))
1120 SETPTR(*filename, NULL);
1121 *line = -1;
1124 g_strfreev(fields);
1128 gboolean msgwin_goto_messages_file_line(gboolean focus_editor)
1130 GtkTreeIter iter;
1131 GtkTreeModel *model;
1132 GtkTreeSelection *selection;
1133 gboolean ret = FALSE;
1135 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
1136 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1138 gint line;
1139 guint id;
1140 gchar *string;
1141 GeanyDocument *doc;
1142 GeanyDocument *old_doc = document_get_current();
1144 gtk_tree_model_get(model, &iter,
1145 MSG_COL_LINE, &line, MSG_COL_DOC_ID, &id, MSG_COL_STRING, &string, -1);
1146 if (line >= 0 && id > 0)
1148 /* check doc is still open */
1149 doc = document_find_by_id(id);
1150 if (!doc)
1152 ui_set_statusbar(FALSE, _("The document has been closed."));
1153 utils_beep();
1155 else
1157 ret = navqueue_goto_line(old_doc, doc, line);
1158 if (ret && focus_editor)
1159 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1162 else if (line < 0 && string != NULL)
1164 gchar *filename;
1166 /* try with a file:line parsing */
1167 msgwin_parse_generic_line(string, &filename, &line);
1168 if (filename != NULL)
1170 /* use document_open_file to find an already open file, or open it in place */
1171 doc = document_open_file(filename, FALSE, NULL, NULL);
1172 if (doc != NULL)
1174 ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line);
1175 if (ret && focus_editor)
1176 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1179 g_free(filename);
1181 g_free(string);
1183 return ret;
1187 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1188 gpointer user_data)
1190 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1191 gboolean double_click = event->type == GDK_2BUTTON_PRESS;
1193 if (event->button == 1 && (event->type == GDK_BUTTON_RELEASE || double_click))
1195 switch (GPOINTER_TO_INT(user_data))
1197 case MSG_COMPILER:
1198 { /* mouse click in the compiler treeview */
1199 msgwin_goto_compiler_file_line(double_click);
1200 break;
1202 case MSG_MESSAGE:
1203 { /* mouse click in the message treeview (results of 'Find usage') */
1204 msgwin_goto_messages_file_line(double_click);
1205 break;
1208 return double_click; /* TRUE prevents message window re-focusing */
1211 if (event->button == 3)
1212 { /* popupmenu to hide or clear the active treeview */
1213 switch (GPOINTER_TO_INT(user_data))
1215 case MSG_STATUS:
1217 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1218 event->button, event->time);
1219 break;
1221 case MSG_MESSAGE:
1223 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1224 event->button, event->time);
1225 break;
1227 case MSG_COMPILER:
1229 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1230 event->button, event->time);
1231 break;
1235 return FALSE;
1240 * Switches to the given notebook tab of the messages window and shows the messages window
1241 * if it was previously hidden and @a show is set to @c TRUE.
1243 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1244 * #MessageWindowTabNum.
1245 * @param show Whether to show the messages window at all if it was hidden before.
1247 * @since 0.15
1249 GEANY_API_SYMBOL
1250 void msgwin_switch_tab(gint tabnum, gboolean show)
1252 GtkWidget *widget = NULL; /* widget to focus */
1254 switch (tabnum)
1256 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1257 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1258 case MSG_STATUS: widget = msgwindow.tree_status; break;
1259 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1260 #ifdef HAVE_VTE
1261 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1262 #endif
1263 default: break;
1266 /* the msgwin must be visible before we switch to the VTE page so that
1267 * the font settings are applied on realization */
1268 if (show)
1269 msgwin_show_hide(TRUE);
1270 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1271 if (show && widget)
1272 gtk_widget_grab_focus(widget);
1277 * Removes all messages from a tab specified by @a tabnum in the messages window.
1279 * @param tabnum An index of a tab in the messages window which should be cleared.
1280 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1282 * @since 0.15
1284 GEANY_API_SYMBOL
1285 void msgwin_clear_tab(gint tabnum)
1287 GtkListStore *store = NULL;
1289 switch (tabnum)
1291 case MSG_MESSAGE:
1292 store = msgwindow.store_msg;
1293 break;
1295 case MSG_COMPILER:
1296 gtk_list_store_clear(msgwindow.store_compiler);
1297 build_menu_update(NULL); /* update next error items */
1298 return;
1300 case MSG_STATUS: store = msgwindow.store_status; break;
1301 default: return;
1303 if (store == NULL)
1304 return;
1305 gtk_list_store_clear(store);