javascript: Stop using longjmp() and friends
[geany-mirror.git] / src / msgwindow.c
blob1aed38decd35142a81881987fab9b85aedb2e299
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 void prepare_msg_tree_view(void);
87 static void prepare_status_tree_view(void);
88 static void prepare_compiler_tree_view(void);
89 static GtkWidget *create_message_popup_menu(gint type);
90 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
91 gpointer user_data);
92 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data);
95 void msgwin_show_hide_tabs(void)
97 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_status), interface_prefs.msgwin_status_visible);
98 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_compiler), interface_prefs.msgwin_compiler_visible);
99 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_msg), interface_prefs.msgwin_messages_visible);
100 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.scribble), interface_prefs.msgwin_scribble_visible);
104 /** Sets the Messages path for opening any parsed filenames without absolute path
105 * from message lines.
106 * @param messages_dir The directory. **/
107 void msgwin_set_messages_dir(const gchar *messages_dir)
109 g_free(msgwindow.messages_dir);
110 msgwindow.messages_dir = g_strdup(messages_dir);
114 void msgwin_init(void)
116 msgwindow.notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
117 msgwindow.tree_status = ui_lookup_widget(main_widgets.window, "treeview3");
118 msgwindow.tree_msg = ui_lookup_widget(main_widgets.window, "treeview4");
119 msgwindow.tree_compiler = ui_lookup_widget(main_widgets.window, "treeview5");
120 msgwindow.scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
121 msgwindow.messages_dir = NULL;
123 prepare_status_tree_view();
124 prepare_msg_tree_view();
125 prepare_compiler_tree_view();
126 msgwindow.popup_status_menu = create_message_popup_menu(MSG_STATUS);
127 msgwindow.popup_msg_menu = create_message_popup_menu(MSG_MESSAGE);
128 msgwindow.popup_compiler_menu = create_message_popup_menu(MSG_COMPILER);
130 ui_widget_modify_font_from_string(msgwindow.scribble, interface_prefs.msgwin_font);
131 g_signal_connect(msgwindow.scribble, "populate-popup", G_CALLBACK(on_scribble_populate), NULL);
135 void msgwin_finalize(void)
137 g_free(msgwindow.messages_dir);
141 static gboolean on_msgwin_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
143 gboolean enter_or_return = ui_is_keyval_enter_or_return(event->keyval);
145 if (enter_or_return || event->keyval == GDK_space)
147 switch (GPOINTER_TO_INT(data))
149 case MSG_COMPILER:
150 { /* key press in the compiler treeview */
151 msgwin_goto_compiler_file_line(enter_or_return);
152 break;
154 case MSG_MESSAGE:
155 { /* key press in the message treeview (results of 'Find usage') */
156 msgwin_goto_messages_file_line(enter_or_return);
157 break;
161 return FALSE;
165 /* does some preparing things to the status message list widget */
166 static void prepare_status_tree_view(void)
168 GtkCellRenderer *renderer;
169 GtkTreeViewColumn *column;
171 msgwindow.store_status = gtk_list_store_new(1, G_TYPE_STRING);
172 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_status), GTK_TREE_MODEL(msgwindow.store_status));
173 g_object_unref(msgwindow.store_status);
175 renderer = gtk_cell_renderer_text_new();
176 column = gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer, "text", 0, NULL);
177 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_status), column);
179 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_status), FALSE);
181 ui_widget_modify_font_from_string(msgwindow.tree_status, interface_prefs.msgwin_font);
183 g_signal_connect(msgwindow.tree_status, "button-press-event",
184 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_STATUS));
188 /* does some preparing things to the message list widget
189 * (currently used for showing results of 'Find usage') */
190 static void prepare_msg_tree_view(void)
192 GtkCellRenderer *renderer;
193 GtkTreeViewColumn *column;
194 GtkTreeSelection *selection;
196 /* line, doc id, fg, str */
197 msgwindow.store_msg = gtk_list_store_new(MSG_COL_COUNT, G_TYPE_INT, G_TYPE_UINT,
198 GDK_TYPE_COLOR, G_TYPE_STRING);
199 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_msg), GTK_TREE_MODEL(msgwindow.store_msg));
200 g_object_unref(msgwindow.store_msg);
202 renderer = gtk_cell_renderer_text_new();
203 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
204 "foreground-gdk", MSG_COL_COLOR, "text", MSG_COL_STRING, NULL);
205 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_msg), column);
207 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_msg), FALSE);
209 ui_widget_modify_font_from_string(msgwindow.tree_msg, interface_prefs.msgwin_font);
211 /* use button-release-event so the selection has changed
212 * (connect_after button-press-event doesn't work) */
213 g_signal_connect(msgwindow.tree_msg, "button-release-event",
214 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
215 /* for double-clicking only, after the first release */
216 g_signal_connect(msgwindow.tree_msg, "button-press-event",
217 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
218 g_signal_connect(msgwindow.tree_msg, "key-press-event",
219 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_MESSAGE));
221 /* selection handling */
222 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
223 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
224 /*g_signal_connect(selection, "changed",G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
228 /* does some preparing things to the compiler list widget */
229 static void prepare_compiler_tree_view(void)
231 GtkCellRenderer *renderer;
232 GtkTreeViewColumn *column;
233 GtkTreeSelection *selection;
235 msgwindow.store_compiler = gtk_list_store_new(COMPILER_COL_COUNT, GDK_TYPE_COLOR, G_TYPE_STRING);
236 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_compiler), GTK_TREE_MODEL(msgwindow.store_compiler));
237 g_object_unref(msgwindow.store_compiler);
239 renderer = gtk_cell_renderer_text_new();
240 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
241 "foreground-gdk", COMPILER_COL_COLOR, "text", COMPILER_COL_STRING, NULL);
242 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_compiler), column);
244 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_compiler), FALSE);
246 ui_widget_modify_font_from_string(msgwindow.tree_compiler, interface_prefs.msgwin_font);
248 /* use button-release-event so the selection has changed
249 * (connect_after button-press-event doesn't work) */
250 g_signal_connect(msgwindow.tree_compiler, "button-release-event",
251 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
252 /* for double-clicking only, after the first release */
253 g_signal_connect(msgwindow.tree_compiler, "button-press-event",
254 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
255 g_signal_connect(msgwindow.tree_compiler, "key-press-event",
256 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_COMPILER));
258 /* selection handling */
259 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
260 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
261 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
265 static const GdkColor color_error = {0, 65535, 0, 0};
267 static const GdkColor *get_color(gint msg_color)
269 static const GdkColor dark_red = {0, 65535 / 2, 0, 0};
270 static const GdkColor blue = {0, 0, 0, 0xD000}; /* not too bright ;-) */
272 switch (msg_color)
274 case COLOR_RED: return &color_error;
275 case COLOR_DARK_RED: return &dark_red;
276 case COLOR_BLUE: return &blue;
277 default: return NULL;
283 * Adds a new message in the compiler tab treeview in the messages window.
285 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
286 * @param format @c printf()-style format string.
287 * @param ... Arguments for the @c format string.
289 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
291 gchar *string;
292 va_list args;
294 va_start(args, format);
295 string = g_strdup_vprintf(format, args);
296 va_end(args);
297 msgwin_compiler_add_string(msg_color, string);
298 g_free(string);
302 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
304 GtkTreeIter iter;
305 GtkTreePath *path;
306 const GdkColor *color = get_color(msg_color);
307 gchar *utf8_msg;
309 if (! g_utf8_validate(msg, -1, NULL))
310 utf8_msg = utils_get_utf8_from_locale(msg);
311 else
312 utf8_msg = (gchar *) msg;
314 gtk_list_store_append(msgwindow.store_compiler, &iter);
315 gtk_list_store_set(msgwindow.store_compiler, &iter,
316 COMPILER_COL_COLOR, color, COMPILER_COL_STRING, utf8_msg, -1);
318 if (ui_prefs.msgwindow_visible && interface_prefs.compiler_tab_autoscroll)
320 path = gtk_tree_model_get_path(
321 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
322 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
323 gtk_tree_path_free(path);
326 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
327 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
328 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
330 if (utf8_msg != msg)
331 g_free(utf8_msg);
335 void msgwin_show_hide(gboolean show)
337 ui_prefs.msgwindow_visible = show;
338 ignore_callback = TRUE;
339 gtk_check_menu_item_set_active(
340 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
341 show);
342 ignore_callback = FALSE;
343 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"), show);
344 /* set the input focus back to the editor */
345 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
350 * Adds a new message in the messages tab treeview in the messages window.
351 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
352 * by @a doc into the line specified with @a line.
354 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
355 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
356 * @param doc The document. Set to @c NULL to ignore.
357 * @param format @c printf()-style format string.
358 * @param ... Arguments for the @c format string.
360 * @since 0.15
362 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
364 gchar *string;
365 va_list args;
367 va_start(args, format);
368 string = g_strdup_vprintf(format, args);
369 va_end(args);
371 msgwin_msg_add_string(msg_color, line, doc, string);
372 g_free(string);
376 /* adds string to the msg treeview */
377 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
379 GtkTreeIter iter;
380 const GdkColor *color = get_color(msg_color);
381 gchar *tmp;
382 gsize len;
383 gchar *utf8_msg;
385 if (! ui_prefs.msgwindow_visible)
386 msgwin_show_hide(TRUE);
388 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
389 * cut the string to a maximum of 1024 bytes and discard the rest */
390 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
391 len = strlen(string);
392 if (len > 1024)
393 tmp = g_strndup(string, 1024);
394 else
395 tmp = g_strdup(string);
397 if (! g_utf8_validate(tmp, -1, NULL))
398 utf8_msg = utils_get_utf8_from_locale(tmp);
399 else
400 utf8_msg = tmp;
402 gtk_list_store_append(msgwindow.store_msg, &iter);
403 gtk_list_store_set(msgwindow.store_msg, &iter,
404 MSG_COL_LINE, line, MSG_COL_DOC_ID, doc ? doc->id : 0, MSG_COL_COLOR,
405 color, MSG_COL_STRING, utf8_msg, -1);
407 g_free(tmp);
408 if (utf8_msg != tmp)
409 g_free(utf8_msg);
414 * Logs a status message *without* setting the status bar.
415 * (Use ui_set_statusbar() to display text on the statusbar)
417 * @param format @c printf()-style format string.
418 * @param ... Arguments for the @c format string.
420 void msgwin_status_add(const gchar *format, ...)
422 GtkTreeIter iter;
423 gchar *string;
424 gchar *statusmsg, *time_str;
425 va_list args;
427 va_start(args, format);
428 string = g_strdup_vprintf(format, args);
429 va_end(args);
431 /* add a timestamp to status messages */
432 time_str = utils_get_current_time_string();
433 statusmsg = g_strconcat(time_str, ": ", string, NULL);
434 g_free(time_str);
435 g_free(string);
437 /* add message to Status window */
438 gtk_list_store_append(msgwindow.store_status, &iter);
439 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
440 g_free(statusmsg);
442 if (G_LIKELY(main_status.main_window_realized))
444 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
446 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
447 if (prefs.switch_to_status)
448 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
449 gtk_tree_path_free(path);
454 static void
455 on_message_treeview_clear_activate(GtkMenuItem *menuitem, gpointer user_data)
457 gint tabnum = GPOINTER_TO_INT(user_data);
459 msgwin_clear_tab(tabnum);
463 static void
464 on_compiler_treeview_copy_activate(GtkMenuItem *menuitem, gpointer user_data)
466 GtkWidget *tv = NULL;
467 GtkTreeSelection *selection;
468 GtkTreeModel *model;
469 GtkTreeIter iter;
470 gint str_idx = COMPILER_COL_STRING;
472 switch (GPOINTER_TO_INT(user_data))
474 case MSG_STATUS:
475 tv = msgwindow.tree_status;
476 str_idx = 0;
477 break;
479 case MSG_COMPILER:
480 tv = msgwindow.tree_compiler;
481 break;
483 case MSG_MESSAGE:
484 tv = msgwindow.tree_msg;
485 str_idx = MSG_COL_STRING;
486 break;
488 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
490 if (gtk_tree_selection_get_selected(selection, &model, &iter))
492 gchar *string;
494 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
495 if (!EMPTY(string))
497 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
498 string, -1);
500 g_free(string);
505 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
507 GtkListStore *store = msgwindow.store_compiler;
508 GtkTreeIter iter;
509 GString *str = g_string_new("");
510 gint str_idx = COMPILER_COL_STRING;
511 gboolean valid;
513 switch (GPOINTER_TO_INT(user_data))
515 case MSG_STATUS:
516 store = msgwindow.store_status;
517 str_idx = 0;
518 break;
520 case MSG_COMPILER:
521 /* default values */
522 break;
524 case MSG_MESSAGE:
525 store = msgwindow.store_msg;
526 str_idx = MSG_COL_STRING;
527 break;
530 /* walk through the list and copy every line into a string */
531 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
532 while (valid)
534 gchar *line;
536 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
537 if (!EMPTY(line))
539 g_string_append(str, line);
540 g_string_append_c(str, '\n');
542 g_free(line);
544 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
547 /* copy the string into the clipboard */
548 if (str->len > 0)
550 gtk_clipboard_set_text(
551 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
552 str->str,
553 str->len);
555 g_string_free(str, TRUE);
559 static void
560 on_hide_message_window(GtkMenuItem *menuitem, gpointer user_data)
562 msgwin_show_hide(FALSE);
566 static GtkWidget *create_message_popup_menu(gint type)
568 GtkWidget *message_popup_menu, *clear, *copy, *copy_all, *image;
570 message_popup_menu = gtk_menu_new();
572 clear = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLEAR, NULL);
573 gtk_widget_show(clear);
574 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
575 g_signal_connect(clear, "activate",
576 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
578 copy = gtk_image_menu_item_new_with_mnemonic(_("C_opy"));
579 gtk_widget_show(copy);
580 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
581 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
582 gtk_widget_show(image);
583 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
584 g_signal_connect(copy, "activate",
585 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
587 copy_all = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
588 gtk_widget_show(copy_all);
589 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy_all);
590 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
591 gtk_widget_show(image);
592 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy_all), image);
593 g_signal_connect(copy_all, "activate",
594 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
596 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
598 return message_popup_menu;
602 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
604 msgwin_menu_add_common_items(arg1);
608 /* Menu items that should be on all message window popup menus */
609 void msgwin_menu_add_common_items(GtkMenu *menu)
611 GtkWidget *item;
613 item = gtk_separator_menu_item_new();
614 gtk_widget_show(item);
615 gtk_container_add(GTK_CONTAINER(menu), item);
617 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
618 gtk_widget_show(item);
619 gtk_container_add(GTK_CONTAINER(menu), item);
620 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
624 /* look back up from the current path and find the directory we came from */
625 static gboolean
626 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
628 GtkTreeIter iter;
629 *prefix = NULL;
631 while (gtk_tree_path_prev(cur))
633 if (gtk_tree_model_get_iter(model, &iter, cur))
635 gchar *string;
636 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
637 if (string != NULL && build_parse_make_dir(string, prefix))
639 g_free(string);
640 return TRUE;
642 g_free(string);
646 return FALSE;
650 static gboolean goto_compiler_file_line(const gchar *filename, gint line, gboolean focus_editor)
652 if (!filename || line <= -1)
653 return FALSE;
655 /* If the path doesn't exist, try the current document.
656 * This happens when we receive build messages in the wrong order - after the
657 * 'Leaving directory' messages */
658 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
660 gchar *cur_dir = utils_get_current_file_dir_utf8();
661 gchar *name;
663 if (cur_dir)
665 /* we let the user know we couldn't find the parsed filename from the message window */
666 SETPTR(cur_dir, utils_get_locale_from_utf8(cur_dir));
667 name = g_path_get_basename(filename);
668 SETPTR(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
669 g_free(cur_dir);
671 if (g_file_test(name, G_FILE_TEST_EXISTS))
673 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
674 filename);
675 filename = name;
677 else
678 g_free(name);
683 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
684 GeanyDocument *doc = document_find_by_filename(utf8_filename);
685 GeanyDocument *old_doc = document_get_current();
687 g_free(utf8_filename);
689 if (doc == NULL) /* file not already open */
690 doc = document_open_file(filename, FALSE, NULL, NULL);
692 if (doc != NULL)
694 gboolean ret;
696 if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
697 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
699 ret = navqueue_goto_line(old_doc, doc, line);
700 if (ret && focus_editor)
701 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
703 return ret;
706 return FALSE;
710 gboolean msgwin_goto_compiler_file_line(gboolean focus_editor)
712 GtkTreeIter iter;
713 GtkTreeModel *model;
714 GtkTreeSelection *selection;
715 gchar *string;
716 GdkColor *color;
718 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
719 if (gtk_tree_selection_get_selected(selection, &model, &iter))
721 /* if the item is not coloured red, it's not an error line */
722 gtk_tree_model_get(model, &iter, COMPILER_COL_COLOR, &color, -1);
723 if (color == NULL || ! gdk_color_equal(color, &color_error))
725 if (color != NULL)
726 gdk_color_free(color);
727 return FALSE;
729 gdk_color_free(color);
731 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
732 if (string != NULL)
734 gint line;
735 gchar *filename, *dir;
736 GtkTreePath *path;
737 gboolean ret;
739 path = gtk_tree_model_get_path(model, &iter);
740 find_prev_build_dir(path, model, &dir);
741 gtk_tree_path_free(path);
742 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
743 g_free(string);
744 g_free(dir);
746 ret = goto_compiler_file_line(filename, line, focus_editor);
747 g_free(filename);
748 return ret;
751 return FALSE;
755 static void make_absolute(gchar **filename, const gchar *dir)
757 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
759 if (*filename == NULL)
760 return;
762 /* skip some characters at the beginning of the filename, at the moment only "./"
763 * can be extended if other "trash" is known */
764 if (strncmp(*filename, "./", 2) == 0)
765 skip_dot_slash = 2;
767 /* add directory */
768 if (! utils_is_absolute_path(*filename))
769 SETPTR(*filename, g_build_filename(dir, *filename + skip_dot_slash, NULL));
773 /* try to parse the file and line number where the error occured described in line
774 * and when something useful is found, it stores the line number in *line and the
775 * relevant file with the error in *filename.
776 * *line will be -1 if no error was found in string.
777 * *filename must be freed unless it is NULL. */
778 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
780 gchar *end = NULL;
781 gchar **fields;
783 *filename = NULL;
784 *line = -1;
786 g_return_if_fail(data->string != NULL);
788 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
790 /* parse the line */
791 if (g_strv_length(fields) < data->min_fields)
793 g_strfreev(fields);
794 return;
797 *line = strtol(fields[data->line_idx], &end, 10);
799 /* if the line could not be read, line is 0 and an error occurred, so we leave */
800 if (fields[data->line_idx] == end)
802 g_strfreev(fields);
803 return;
806 /* let's stop here if there is no filename in the error message */
807 if (data->file_idx == -1)
809 /* we have no filename in the error message, so take the current one and hope it's correct */
810 GeanyDocument *doc = document_get_current();
811 if (doc != NULL)
812 *filename = g_strdup(doc->file_name);
813 g_strfreev(fields);
814 return;
817 *filename = g_strdup(fields[data->file_idx]);
818 g_strfreev(fields);
822 static void parse_compiler_error_line(const gchar *string,
823 gchar **filename, gint *line)
825 ParseData data = {NULL, NULL, 0, 0, 0};
827 data.string = string;
829 switch (build_info.file_type_id)
831 case GEANY_FILETYPES_PHP:
833 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
834 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
835 gchar *tmp = strstr(string, " in ");
837 if (tmp != NULL)
839 data.string = tmp;
840 data.pattern = " ";
841 data.min_fields = 6;
842 data.line_idx = 5;
843 data.file_idx = 2;
845 else
847 data.pattern = " ";
848 data.min_fields = 11;
849 data.line_idx = 10;
850 data.file_idx = 7;
852 break;
854 case GEANY_FILETYPES_PERL:
856 /* syntax error at test.pl line 7, near "{ */
857 data.pattern = " ";
858 data.min_fields = 6;
859 data.line_idx = 5;
860 data.file_idx = 3;
861 break;
863 /* the error output of python and tcl equals */
864 case GEANY_FILETYPES_TCL:
865 case GEANY_FILETYPES_PYTHON:
867 /* File "HyperArch.py", line 37, in ?
868 * (file "clrdial.tcl" line 12)
869 * */
870 if (strstr(string, " line ") != NULL)
872 /* Tcl and old Python format (<= Python 2.5) */
873 data.pattern = " \"";
874 data.min_fields = 6;
875 data.line_idx = 5;
876 data.file_idx = 2;
878 else
880 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
881 * (used since Python 2.6) */
882 data.pattern = ",'";
883 data.min_fields = 8;
884 data.line_idx = 6;
885 data.file_idx = 4;
887 break;
889 case GEANY_FILETYPES_BASIC:
890 case GEANY_FILETYPES_PASCAL:
891 case GEANY_FILETYPES_CS:
893 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
894 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
895 data.pattern = "(";
896 data.min_fields = 2;
897 data.line_idx = 1;
898 data.file_idx = 0;
899 break;
901 case GEANY_FILETYPES_D:
903 /* GNU D compiler front-end, gdc
904 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
905 * warning - gantry.d:20: statement is not reachable
906 * Digital Mars dmd compiler
907 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
908 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
909 if (strncmp(string, "warning - ", 10) == 0)
911 data.pattern = " (:";
912 data.min_fields = 4;
913 data.line_idx = 3;
914 data.file_idx = 2;
916 else
918 data.pattern = "(:";
919 data.min_fields = 2;
920 data.line_idx = 1;
921 data.file_idx = 0;
923 break;
925 case GEANY_FILETYPES_FERITE:
927 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
928 * Error: Compile Error: on line 24, in /test/class.fe */
929 if (strncmp(string, "Error: Compile Error", 20) == 0)
931 data.pattern = " ";
932 data.min_fields = 8;
933 data.line_idx = 5;
934 data.file_idx = 7;
936 else
938 data.pattern = " \"";
939 data.min_fields = 10;
940 data.line_idx = 5;
941 data.file_idx = 8;
943 break;
945 case GEANY_FILETYPES_HTML:
947 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
948 data.pattern = " ";
949 data.min_fields = 4;
950 data.line_idx = 1;
951 data.file_idx = -1;
952 break;
954 /* All GNU gcc-like error messages */
955 case GEANY_FILETYPES_C:
956 case GEANY_FILETYPES_CPP:
957 case GEANY_FILETYPES_RUBY:
958 case GEANY_FILETYPES_JAVA:
959 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
960 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
961 * empty.c:21: error: conflicting types for `foo'
962 * Only parse file and line, so that linker errors will also work (with -g) */
963 case GEANY_FILETYPES_F77:
964 case GEANY_FILETYPES_FORTRAN:
965 case GEANY_FILETYPES_LATEX:
966 /* ./kommtechnik_2b.tex:18: Emergency stop. */
967 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
968 case GEANY_FILETYPES_NONE:
969 default: /* The default is a GNU gcc type error */
971 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
972 strncmp(string, "[javac]", 7) == 0)
974 /* Java Apache Ant.
975 * [javac] <Full Path to File + extension>:<line n°>: <error> */
976 data.pattern = " :";
977 data.min_fields = 4;
978 data.line_idx = 2;
979 data.file_idx = 1;
980 break;
982 /* don't accidently find libtool versions x:y:x and think it is a file name */
983 if (strstr(string, "libtool --mode=link") == NULL)
985 data.pattern = ":";
986 data.min_fields = 3;
987 data.line_idx = 1;
988 data.file_idx = 0;
989 break;
994 if (data.pattern != NULL)
995 parse_file_line(&data, filename, line);
999 /* try to parse the file and line number where the error occured described in string
1000 * and when something useful is found, it stores the line number in *line and the
1001 * relevant file with the error in *filename.
1002 * *line will be -1 if no error was found in string.
1003 * *filename must be freed unless it is NULL. */
1004 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
1005 gchar **filename, gint *line)
1007 GeanyFiletype *ft;
1008 gchar *trimmed_string;
1010 *filename = NULL;
1011 *line = -1;
1013 if (G_UNLIKELY(string == NULL))
1014 return;
1016 if (dir == NULL)
1017 dir = build_info.dir;
1018 g_return_if_fail(dir != NULL);
1020 trimmed_string = g_strdup(string);
1021 g_strchug(trimmed_string); /* remove possible leading whitespace */
1023 ft = filetypes[build_info.file_type_id];
1025 /* try parsing with a custom regex */
1026 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
1028 /* fallback to default old-style parsing */
1029 parse_compiler_error_line(trimmed_string, filename, line);
1031 make_absolute(filename, dir);
1032 g_free(trimmed_string);
1036 /* Tries to parse strings of the file:line style, allowing line field to be missing
1037 * * filename is filled with the filename, should be freed
1038 * * line is filled with the line number or -1 */
1039 static void msgwin_parse_generic_line(const gchar *string, gchar **filename, gint *line)
1041 gchar **fields;
1042 gboolean incertain = TRUE; /* whether we're reasonably certain of the result */
1044 *filename = NULL;
1045 *line = -1;
1047 fields = g_strsplit(string, ":", 2);
1048 /* extract the filename */
1049 if (fields[0] != NULL)
1051 *filename = g_strdup(fields[0]);
1052 if (msgwindow.messages_dir != NULL)
1053 make_absolute(filename, msgwindow.messages_dir);
1055 /* now the line */
1056 if (fields[1] != NULL)
1058 gchar *end;
1060 *line = strtol(fields[1], &end, 10);
1061 if (end == fields[1])
1062 *line = -1;
1063 else if (*end == ':' || g_ascii_isspace(*end))
1064 { /* if we have a blank or a separator right after the number, assume we really got a
1065 * filename (it's a grep-like syntax) */
1066 incertain = FALSE;
1070 /* if we aren't sure we got a supposedly correct filename, check it */
1071 if (incertain && ! g_file_test(*filename, G_FILE_TEST_EXISTS))
1073 SETPTR(*filename, NULL);
1074 *line = -1;
1077 g_strfreev(fields);
1081 gboolean msgwin_goto_messages_file_line(gboolean focus_editor)
1083 GtkTreeIter iter;
1084 GtkTreeModel *model;
1085 GtkTreeSelection *selection;
1086 gboolean ret = FALSE;
1088 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
1089 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1091 gint line;
1092 guint id;
1093 gchar *string;
1094 GeanyDocument *doc;
1095 GeanyDocument *old_doc = document_get_current();
1097 gtk_tree_model_get(model, &iter,
1098 MSG_COL_LINE, &line, MSG_COL_DOC_ID, &id, MSG_COL_STRING, &string, -1);
1099 if (line >= 0 && id > 0)
1101 /* check doc is still open */
1102 doc = document_find_by_id(id);
1103 if (!doc)
1105 ui_set_statusbar(FALSE, _("The document has been closed."));
1106 utils_beep();
1108 else
1110 ret = navqueue_goto_line(old_doc, doc, line);
1111 if (ret && focus_editor)
1112 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1115 else if (line < 0 && string != NULL)
1117 gchar *filename;
1119 /* try with a file:line parsing */
1120 msgwin_parse_generic_line(string, &filename, &line);
1121 if (filename != NULL)
1123 /* use document_open_file to find an already open file, or open it in place */
1124 doc = document_open_file(filename, FALSE, NULL, NULL);
1125 if (doc != NULL)
1127 ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line);
1128 if (ret && focus_editor)
1129 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1132 g_free(filename);
1134 g_free(string);
1136 return ret;
1140 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1141 gpointer user_data)
1143 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1144 gboolean double_click = event->type == GDK_2BUTTON_PRESS;
1146 if (event->button == 1 && (event->type == GDK_BUTTON_RELEASE || double_click))
1148 switch (GPOINTER_TO_INT(user_data))
1150 case MSG_COMPILER:
1151 { /* mouse click in the compiler treeview */
1152 msgwin_goto_compiler_file_line(double_click);
1153 break;
1155 case MSG_MESSAGE:
1156 { /* mouse click in the message treeview (results of 'Find usage') */
1157 msgwin_goto_messages_file_line(double_click);
1158 break;
1161 return double_click; /* TRUE prevents message window re-focusing */
1164 if (event->button == 3)
1165 { /* popupmenu to hide or clear the active treeview */
1166 switch (GPOINTER_TO_INT(user_data))
1168 case MSG_STATUS:
1170 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1171 event->button, event->time);
1172 break;
1174 case MSG_MESSAGE:
1176 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1177 event->button, event->time);
1178 break;
1180 case MSG_COMPILER:
1182 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1183 event->button, event->time);
1184 break;
1188 return FALSE;
1193 * Switches to the given notebook tab of the messages window and shows the messages window
1194 * if it was previously hidden and @a show is set to @c TRUE.
1196 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1197 * #MessageWindowTabNum.
1198 * @param show Whether to show the messages window at all if it was hidden before.
1200 * @since 0.15
1202 void msgwin_switch_tab(gint tabnum, gboolean show)
1204 GtkWidget *widget = NULL; /* widget to focus */
1206 switch (tabnum)
1208 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1209 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1210 case MSG_STATUS: widget = msgwindow.tree_status; break;
1211 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1212 #ifdef HAVE_VTE
1213 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1214 #endif
1215 default: break;
1218 /* the msgwin must be visible before we switch to the VTE page so that
1219 * the font settings are applied on realization */
1220 if (show)
1221 msgwin_show_hide(TRUE);
1222 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1223 if (show && widget)
1224 gtk_widget_grab_focus(widget);
1229 * Removes all messages from a tab specified by @a tabnum in the messages window.
1231 * @param tabnum An index of a tab in the messages window which should be cleared.
1232 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1234 * @since 0.15
1236 void msgwin_clear_tab(gint tabnum)
1238 GtkListStore *store = NULL;
1240 switch (tabnum)
1242 case MSG_MESSAGE:
1243 store = msgwindow.store_msg;
1244 break;
1246 case MSG_COMPILER:
1247 gtk_list_store_clear(msgwindow.store_compiler);
1248 build_menu_update(NULL); /* update next error items */
1249 return;
1251 case MSG_STATUS: store = msgwindow.store_status; break;
1252 default: return;
1254 if (store == NULL)
1255 return;
1256 gtk_list_store_clear(store);