API: Rename document_reload_file -> document_reload_force
[geany-mirror.git] / src / msgwindow.c
blob22a1a480bc44fd268c72f785298b41074ff577e9
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
79 static void prepare_msg_tree_view(void);
80 static void prepare_status_tree_view(void);
81 static void prepare_compiler_tree_view(void);
82 static GtkWidget *create_message_popup_menu(gint type);
83 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
84 gpointer user_data);
85 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data);
88 void msgwin_show_hide_tabs(void)
90 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_status), interface_prefs.msgwin_status_visible);
91 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_compiler), interface_prefs.msgwin_compiler_visible);
92 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_msg), interface_prefs.msgwin_messages_visible);
93 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.scribble), interface_prefs.msgwin_scribble_visible);
97 /** Sets the Messages path for opening any parsed filenames without absolute path
98 * from message lines.
99 * @param messages_dir The directory. **/
100 void msgwin_set_messages_dir(const gchar *messages_dir)
102 g_free(msgwindow.messages_dir);
103 msgwindow.messages_dir = g_strdup(messages_dir);
107 void msgwin_init(void)
109 msgwindow.notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
110 msgwindow.tree_status = ui_lookup_widget(main_widgets.window, "treeview3");
111 msgwindow.tree_msg = ui_lookup_widget(main_widgets.window, "treeview4");
112 msgwindow.tree_compiler = ui_lookup_widget(main_widgets.window, "treeview5");
113 msgwindow.scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
114 msgwindow.messages_dir = NULL;
116 prepare_status_tree_view();
117 prepare_msg_tree_view();
118 prepare_compiler_tree_view();
119 msgwindow.popup_status_menu = create_message_popup_menu(MSG_STATUS);
120 msgwindow.popup_msg_menu = create_message_popup_menu(MSG_MESSAGE);
121 msgwindow.popup_compiler_menu = create_message_popup_menu(MSG_COMPILER);
123 ui_widget_modify_font_from_string(msgwindow.scribble, interface_prefs.msgwin_font);
124 g_signal_connect(msgwindow.scribble, "populate-popup", G_CALLBACK(on_scribble_populate), NULL);
128 void msgwin_finalize(void)
130 g_free(msgwindow.messages_dir);
134 static gboolean on_msgwin_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
136 gboolean enter_or_return = ui_is_keyval_enter_or_return(event->keyval);
138 if (enter_or_return || event->keyval == GDK_space)
140 switch (GPOINTER_TO_INT(data))
142 case MSG_COMPILER:
143 { /* key press in the compiler treeview */
144 msgwin_goto_compiler_file_line(enter_or_return);
145 break;
147 case MSG_MESSAGE:
148 { /* key press in the message treeview (results of 'Find usage') */
149 msgwin_goto_messages_file_line(enter_or_return);
150 break;
154 return FALSE;
158 /* does some preparing things to the status message list widget */
159 static void prepare_status_tree_view(void)
161 GtkCellRenderer *renderer;
162 GtkTreeViewColumn *column;
164 msgwindow.store_status = gtk_list_store_new(1, G_TYPE_STRING);
165 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_status), GTK_TREE_MODEL(msgwindow.store_status));
166 g_object_unref(msgwindow.store_status);
168 renderer = gtk_cell_renderer_text_new();
169 column = gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer, "text", 0, NULL);
170 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_status), column);
172 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_status), FALSE);
174 ui_widget_modify_font_from_string(msgwindow.tree_status, interface_prefs.msgwin_font);
176 g_signal_connect(msgwindow.tree_status, "button-press-event",
177 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_STATUS));
181 /* does some preparing things to the message list widget
182 * (currently used for showing results of 'Find usage') */
183 static void prepare_msg_tree_view(void)
185 GtkCellRenderer *renderer;
186 GtkTreeViewColumn *column;
187 GtkTreeSelection *selection;
189 /* line, doc id, fg, str */
190 msgwindow.store_msg = gtk_list_store_new(MSG_COL_COUNT, G_TYPE_INT, G_TYPE_UINT,
191 GDK_TYPE_COLOR, G_TYPE_STRING);
192 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_msg), GTK_TREE_MODEL(msgwindow.store_msg));
193 g_object_unref(msgwindow.store_msg);
195 renderer = gtk_cell_renderer_text_new();
196 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
197 "foreground-gdk", 2, "text", 3, NULL);
198 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_msg), column);
200 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_msg), FALSE);
202 ui_widget_modify_font_from_string(msgwindow.tree_msg, interface_prefs.msgwin_font);
204 /* use button-release-event so the selection has changed
205 * (connect_after button-press-event doesn't work) */
206 g_signal_connect(msgwindow.tree_msg, "button-release-event",
207 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
208 /* for double-clicking only, after the first release */
209 g_signal_connect(msgwindow.tree_msg, "button-press-event",
210 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
211 g_signal_connect(msgwindow.tree_msg, "key-press-event",
212 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_MESSAGE));
214 /* selection handling */
215 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
216 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
217 /*g_signal_connect(selection, "changed",G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
221 /* does some preparing things to the compiler list widget */
222 static void prepare_compiler_tree_view(void)
224 GtkCellRenderer *renderer;
225 GtkTreeViewColumn *column;
226 GtkTreeSelection *selection;
228 msgwindow.store_compiler = gtk_list_store_new(2, GDK_TYPE_COLOR, G_TYPE_STRING);
229 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_compiler), GTK_TREE_MODEL(msgwindow.store_compiler));
230 g_object_unref(msgwindow.store_compiler);
232 renderer = gtk_cell_renderer_text_new();
233 column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "foreground-gdk", 0, "text", 1, NULL);
234 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_compiler), column);
236 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_compiler), FALSE);
238 ui_widget_modify_font_from_string(msgwindow.tree_compiler, interface_prefs.msgwin_font);
240 /* use button-release-event so the selection has changed
241 * (connect_after button-press-event doesn't work) */
242 g_signal_connect(msgwindow.tree_compiler, "button-release-event",
243 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
244 /* for double-clicking only, after the first release */
245 g_signal_connect(msgwindow.tree_compiler, "button-press-event",
246 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
247 g_signal_connect(msgwindow.tree_compiler, "key-press-event",
248 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_COMPILER));
250 /* selection handling */
251 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
252 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
253 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
257 static const GdkColor color_error = {0, 65535, 0, 0};
259 static const GdkColor *get_color(gint msg_color)
261 static const GdkColor dark_red = {0, 65535 / 2, 0, 0};
262 static const GdkColor blue = {0, 0, 0, 0xD000}; /* not too bright ;-) */
264 switch (msg_color)
266 case COLOR_RED: return &color_error;
267 case COLOR_DARK_RED: return &dark_red;
268 case COLOR_BLUE: return &blue;
269 default: return NULL;
275 * Adds a new message in the compiler tab treeview in the messages window.
277 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
278 * @param format @c printf()-style format string.
279 * @param ... Arguments for the @c format string.
281 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
283 gchar *string;
284 va_list args;
286 va_start(args, format);
287 string = g_strdup_vprintf(format, args);
288 va_end(args);
289 msgwin_compiler_add_string(msg_color, string);
290 g_free(string);
294 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
296 GtkTreeIter iter;
297 GtkTreePath *path;
298 const GdkColor *color = get_color(msg_color);
299 gchar *utf8_msg;
301 if (! g_utf8_validate(msg, -1, NULL))
302 utf8_msg = utils_get_utf8_from_locale(msg);
303 else
304 utf8_msg = (gchar *) msg;
306 gtk_list_store_append(msgwindow.store_compiler, &iter);
307 gtk_list_store_set(msgwindow.store_compiler, &iter, 0, color, 1, utf8_msg, -1);
309 if (ui_prefs.msgwindow_visible && interface_prefs.compiler_tab_autoscroll)
311 path = gtk_tree_model_get_path(
312 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
313 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
314 gtk_tree_path_free(path);
317 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
318 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
319 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
321 if (utf8_msg != msg)
322 g_free(utf8_msg);
326 void msgwin_show_hide(gboolean show)
328 ui_prefs.msgwindow_visible = show;
329 ignore_callback = TRUE;
330 gtk_check_menu_item_set_active(
331 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
332 show);
333 ignore_callback = FALSE;
334 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"), show);
335 /* set the input focus back to the editor */
336 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
341 * Adds a new message in the messages tab treeview in the messages window.
342 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
343 * by @a doc into the line specified with @a line.
345 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
346 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
347 * @param doc The document. Set to @c NULL to ignore.
348 * @param format @c printf()-style format string.
349 * @param ... Arguments for the @c format string.
351 * @since 0.15
353 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
355 gchar *string;
356 va_list args;
358 va_start(args, format);
359 string = g_strdup_vprintf(format, args);
360 va_end(args);
362 msgwin_msg_add_string(msg_color, line, doc, string);
363 g_free(string);
367 /* adds string to the msg treeview */
368 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
370 GtkTreeIter iter;
371 const GdkColor *color = get_color(msg_color);
372 gchar *tmp;
373 gsize len;
374 gchar *utf8_msg;
376 if (! ui_prefs.msgwindow_visible)
377 msgwin_show_hide(TRUE);
379 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
380 * cut the string to a maximum of 1024 bytes and discard the rest */
381 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
382 len = strlen(string);
383 if (len > 1024)
384 tmp = g_strndup(string, 1024);
385 else
386 tmp = g_strdup(string);
388 if (! g_utf8_validate(tmp, -1, NULL))
389 utf8_msg = utils_get_utf8_from_locale(tmp);
390 else
391 utf8_msg = tmp;
393 gtk_list_store_append(msgwindow.store_msg, &iter);
394 gtk_list_store_set(msgwindow.store_msg, &iter,
395 MSG_COL_LINE, line, MSG_COL_DOC_ID, doc ? doc->id : 0, MSG_COL_COLOR,
396 color, MSG_COL_STRING, utf8_msg, -1);
398 g_free(tmp);
399 if (utf8_msg != tmp)
400 g_free(utf8_msg);
405 * Logs a status message *without* setting the status bar.
406 * (Use ui_set_statusbar() to display text on the statusbar)
408 * @param format @c printf()-style format string.
409 * @param ... Arguments for the @c format string.
411 void msgwin_status_add(const gchar *format, ...)
413 GtkTreeIter iter;
414 gchar *string;
415 gchar *statusmsg, *time_str;
416 va_list args;
418 va_start(args, format);
419 string = g_strdup_vprintf(format, args);
420 va_end(args);
422 /* add a timestamp to status messages */
423 time_str = utils_get_current_time_string();
424 statusmsg = g_strconcat(time_str, ": ", string, NULL);
425 g_free(time_str);
426 g_free(string);
428 /* add message to Status window */
429 gtk_list_store_append(msgwindow.store_status, &iter);
430 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
431 g_free(statusmsg);
433 if (G_LIKELY(main_status.main_window_realized))
435 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
437 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
438 if (prefs.switch_to_status)
439 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
440 gtk_tree_path_free(path);
445 static void
446 on_message_treeview_clear_activate(GtkMenuItem *menuitem, gpointer user_data)
448 gint tabnum = GPOINTER_TO_INT(user_data);
450 msgwin_clear_tab(tabnum);
454 static void
455 on_compiler_treeview_copy_activate(GtkMenuItem *menuitem, gpointer user_data)
457 GtkWidget *tv = NULL;
458 GtkTreeSelection *selection;
459 GtkTreeModel *model;
460 GtkTreeIter iter;
461 gint str_idx = 1;
463 switch (GPOINTER_TO_INT(user_data))
465 case MSG_STATUS:
466 tv = msgwindow.tree_status;
467 str_idx = 0;
468 break;
470 case MSG_COMPILER:
471 tv = msgwindow.tree_compiler;
472 break;
474 case MSG_MESSAGE:
475 tv = msgwindow.tree_msg;
476 str_idx = 3;
477 break;
479 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
481 if (gtk_tree_selection_get_selected(selection, &model, &iter))
483 gchar *string;
485 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
486 if (!EMPTY(string))
488 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
489 string, -1);
491 g_free(string);
496 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
498 GtkListStore *store = msgwindow.store_compiler;
499 GtkTreeIter iter;
500 GString *str = g_string_new("");
501 gint str_idx = 1;
502 gboolean valid;
504 switch (GPOINTER_TO_INT(user_data))
506 case MSG_STATUS:
507 store = msgwindow.store_status;
508 str_idx = 0;
509 break;
511 case MSG_COMPILER:
512 /* default values */
513 break;
515 case MSG_MESSAGE:
516 store = msgwindow.store_msg;
517 str_idx = 3;
518 break;
521 /* walk through the list and copy every line into a string */
522 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
523 while (valid)
525 gchar *line;
527 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
528 if (!EMPTY(line))
530 g_string_append(str, line);
531 g_string_append_c(str, '\n');
533 g_free(line);
535 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
538 /* copy the string into the clipboard */
539 if (str->len > 0)
541 gtk_clipboard_set_text(
542 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
543 str->str,
544 str->len);
546 g_string_free(str, TRUE);
550 static void
551 on_hide_message_window(GtkMenuItem *menuitem, gpointer user_data)
553 msgwin_show_hide(FALSE);
557 static GtkWidget *create_message_popup_menu(gint type)
559 GtkWidget *message_popup_menu, *clear, *copy, *copy_all, *image;
561 message_popup_menu = gtk_menu_new();
563 clear = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLEAR, NULL);
564 gtk_widget_show(clear);
565 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
566 g_signal_connect(clear, "activate",
567 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
569 copy = gtk_image_menu_item_new_with_mnemonic(_("C_opy"));
570 gtk_widget_show(copy);
571 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
572 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
573 gtk_widget_show(image);
574 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
575 g_signal_connect(copy, "activate",
576 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
578 copy_all = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
579 gtk_widget_show(copy_all);
580 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy_all);
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_all), image);
584 g_signal_connect(copy_all, "activate",
585 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
587 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
589 return message_popup_menu;
593 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
595 msgwin_menu_add_common_items(arg1);
599 /* Menu items that should be on all message window popup menus */
600 void msgwin_menu_add_common_items(GtkMenu *menu)
602 GtkWidget *item;
604 item = gtk_separator_menu_item_new();
605 gtk_widget_show(item);
606 gtk_container_add(GTK_CONTAINER(menu), item);
608 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
609 gtk_widget_show(item);
610 gtk_container_add(GTK_CONTAINER(menu), item);
611 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
615 /* look back up from the current path and find the directory we came from */
616 static gboolean
617 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
619 GtkTreeIter iter;
620 *prefix = NULL;
622 while (gtk_tree_path_prev(cur))
624 if (gtk_tree_model_get_iter(model, &iter, cur))
626 gchar *string;
627 gtk_tree_model_get(model, &iter, 1, &string, -1);
628 if (string != NULL && build_parse_make_dir(string, prefix))
630 g_free(string);
631 return TRUE;
633 g_free(string);
637 return FALSE;
641 static gboolean goto_compiler_file_line(const gchar *filename, gint line, gboolean focus_editor)
643 if (!filename || line <= -1)
644 return FALSE;
646 /* If the path doesn't exist, try the current document.
647 * This happens when we receive build messages in the wrong order - after the
648 * 'Leaving directory' messages */
649 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
651 gchar *cur_dir = utils_get_current_file_dir_utf8();
652 gchar *name;
654 if (cur_dir)
656 /* we let the user know we couldn't find the parsed filename from the message window */
657 SETPTR(cur_dir, utils_get_locale_from_utf8(cur_dir));
658 name = g_path_get_basename(filename);
659 SETPTR(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
660 g_free(cur_dir);
662 if (g_file_test(name, G_FILE_TEST_EXISTS))
664 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
665 filename);
666 filename = name;
668 else
669 g_free(name);
674 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
675 GeanyDocument *doc = document_find_by_filename(utf8_filename);
676 GeanyDocument *old_doc = document_get_current();
678 g_free(utf8_filename);
680 if (doc == NULL) /* file not already open */
681 doc = document_open_file(filename, FALSE, NULL, NULL);
683 if (doc != NULL)
685 gboolean ret;
687 if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
688 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
690 ret = navqueue_goto_line(old_doc, doc, line);
691 if (ret && focus_editor)
692 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
694 return ret;
697 return FALSE;
701 gboolean msgwin_goto_compiler_file_line(gboolean focus_editor)
703 GtkTreeIter iter;
704 GtkTreeModel *model;
705 GtkTreeSelection *selection;
706 gchar *string;
707 GdkColor *color;
709 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
710 if (gtk_tree_selection_get_selected(selection, &model, &iter))
712 /* if the item is not coloured red, it's not an error line */
713 gtk_tree_model_get(model, &iter, 0, &color, -1);
714 if (color == NULL || ! gdk_color_equal(color, &color_error))
716 if (color != NULL)
717 gdk_color_free(color);
718 return FALSE;
720 gdk_color_free(color);
722 gtk_tree_model_get(model, &iter, 1, &string, -1);
723 if (string != NULL)
725 gint line;
726 gchar *filename, *dir;
727 GtkTreePath *path;
728 gboolean ret;
730 path = gtk_tree_model_get_path(model, &iter);
731 find_prev_build_dir(path, model, &dir);
732 gtk_tree_path_free(path);
733 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
734 g_free(string);
735 g_free(dir);
737 ret = goto_compiler_file_line(filename, line, focus_editor);
738 g_free(filename);
739 return ret;
742 return FALSE;
746 static void make_absolute(gchar **filename, const gchar *dir)
748 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
750 if (*filename == NULL)
751 return;
753 /* skip some characters at the beginning of the filename, at the moment only "./"
754 * can be extended if other "trash" is known */
755 if (strncmp(*filename, "./", 2) == 0)
756 skip_dot_slash = 2;
758 /* add directory */
759 if (! utils_is_absolute_path(*filename))
760 SETPTR(*filename, g_build_filename(dir, *filename + skip_dot_slash, NULL));
764 /* try to parse the file and line number where the error occured described in line
765 * and when something useful is found, it stores the line number in *line and the
766 * relevant file with the error in *filename.
767 * *line will be -1 if no error was found in string.
768 * *filename must be freed unless it is NULL. */
769 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
771 gchar *end = NULL;
772 gchar **fields;
774 *filename = NULL;
775 *line = -1;
777 g_return_if_fail(data->string != NULL);
779 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
781 /* parse the line */
782 if (g_strv_length(fields) < data->min_fields)
784 g_strfreev(fields);
785 return;
788 *line = strtol(fields[data->line_idx], &end, 10);
790 /* if the line could not be read, line is 0 and an error occurred, so we leave */
791 if (fields[data->line_idx] == end)
793 g_strfreev(fields);
794 return;
797 /* let's stop here if there is no filename in the error message */
798 if (data->file_idx == -1)
800 /* we have no filename in the error message, so take the current one and hope it's correct */
801 GeanyDocument *doc = document_get_current();
802 if (doc != NULL)
803 *filename = g_strdup(doc->file_name);
804 g_strfreev(fields);
805 return;
808 *filename = g_strdup(fields[data->file_idx]);
809 g_strfreev(fields);
813 static void parse_compiler_error_line(const gchar *string,
814 gchar **filename, gint *line)
816 ParseData data = {NULL, NULL, 0, 0, 0};
818 data.string = string;
820 switch (build_info.file_type_id)
822 case GEANY_FILETYPES_PHP:
824 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
825 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
826 gchar *tmp = strstr(string, " in ");
828 if (tmp != NULL)
830 data.string = tmp;
831 data.pattern = " ";
832 data.min_fields = 6;
833 data.line_idx = 5;
834 data.file_idx = 2;
836 else
838 data.pattern = " ";
839 data.min_fields = 11;
840 data.line_idx = 10;
841 data.file_idx = 7;
843 break;
845 case GEANY_FILETYPES_PERL:
847 /* syntax error at test.pl line 7, near "{ */
848 data.pattern = " ";
849 data.min_fields = 6;
850 data.line_idx = 5;
851 data.file_idx = 3;
852 break;
854 /* the error output of python and tcl equals */
855 case GEANY_FILETYPES_TCL:
856 case GEANY_FILETYPES_PYTHON:
858 /* File "HyperArch.py", line 37, in ?
859 * (file "clrdial.tcl" line 12)
860 * */
861 if (strstr(string, " line ") != NULL)
863 /* Tcl and old Python format (<= Python 2.5) */
864 data.pattern = " \"";
865 data.min_fields = 6;
866 data.line_idx = 5;
867 data.file_idx = 2;
869 else
871 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
872 * (used since Python 2.6) */
873 data.pattern = ",'";
874 data.min_fields = 8;
875 data.line_idx = 6;
876 data.file_idx = 4;
878 break;
880 case GEANY_FILETYPES_BASIC:
881 case GEANY_FILETYPES_PASCAL:
882 case GEANY_FILETYPES_CS:
884 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
885 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
886 data.pattern = "(";
887 data.min_fields = 2;
888 data.line_idx = 1;
889 data.file_idx = 0;
890 break;
892 case GEANY_FILETYPES_D:
894 /* GNU D compiler front-end, gdc
895 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
896 * warning - gantry.d:20: statement is not reachable
897 * Digital Mars dmd compiler
898 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
899 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
900 if (strncmp(string, "warning - ", 10) == 0)
902 data.pattern = " (:";
903 data.min_fields = 4;
904 data.line_idx = 3;
905 data.file_idx = 2;
907 else
909 data.pattern = "(:";
910 data.min_fields = 2;
911 data.line_idx = 1;
912 data.file_idx = 0;
914 break;
916 case GEANY_FILETYPES_FERITE:
918 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
919 * Error: Compile Error: on line 24, in /test/class.fe */
920 if (strncmp(string, "Error: Compile Error", 20) == 0)
922 data.pattern = " ";
923 data.min_fields = 8;
924 data.line_idx = 5;
925 data.file_idx = 7;
927 else
929 data.pattern = " \"";
930 data.min_fields = 10;
931 data.line_idx = 5;
932 data.file_idx = 8;
934 break;
936 case GEANY_FILETYPES_HTML:
938 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
939 data.pattern = " ";
940 data.min_fields = 4;
941 data.line_idx = 1;
942 data.file_idx = -1;
943 break;
945 /* All GNU gcc-like error messages */
946 case GEANY_FILETYPES_C:
947 case GEANY_FILETYPES_CPP:
948 case GEANY_FILETYPES_RUBY:
949 case GEANY_FILETYPES_JAVA:
950 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
951 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
952 * empty.c:21: error: conflicting types for `foo'
953 * Only parse file and line, so that linker errors will also work (with -g) */
954 case GEANY_FILETYPES_F77:
955 case GEANY_FILETYPES_FORTRAN:
956 case GEANY_FILETYPES_LATEX:
957 /* ./kommtechnik_2b.tex:18: Emergency stop. */
958 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
959 case GEANY_FILETYPES_NONE:
960 default: /* The default is a GNU gcc type error */
962 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
963 strncmp(string, "[javac]", 7) == 0)
965 /* Java Apache Ant.
966 * [javac] <Full Path to File + extension>:<line n°>: <error> */
967 data.pattern = " :";
968 data.min_fields = 4;
969 data.line_idx = 2;
970 data.file_idx = 1;
971 break;
973 /* don't accidently find libtool versions x:y:x and think it is a file name */
974 if (strstr(string, "libtool --mode=link") == NULL)
976 data.pattern = ":";
977 data.min_fields = 3;
978 data.line_idx = 1;
979 data.file_idx = 0;
980 break;
985 if (data.pattern != NULL)
986 parse_file_line(&data, filename, line);
990 /* try to parse the file and line number where the error occured described in string
991 * and when something useful is found, it stores the line number in *line and the
992 * relevant file with the error in *filename.
993 * *line will be -1 if no error was found in string.
994 * *filename must be freed unless it is NULL. */
995 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
996 gchar **filename, gint *line)
998 GeanyFiletype *ft;
999 gchar *trimmed_string;
1001 *filename = NULL;
1002 *line = -1;
1004 if (G_UNLIKELY(string == NULL))
1005 return;
1007 if (dir == NULL)
1008 dir = build_info.dir;
1009 g_return_if_fail(dir != NULL);
1011 trimmed_string = g_strdup(string);
1012 g_strchug(trimmed_string); /* remove possible leading whitespace */
1014 ft = filetypes[build_info.file_type_id];
1016 /* try parsing with a custom regex */
1017 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
1019 /* fallback to default old-style parsing */
1020 parse_compiler_error_line(trimmed_string, filename, line);
1022 make_absolute(filename, dir);
1023 g_free(trimmed_string);
1027 /* Tries to parse strings of the file:line style, allowing line field to be missing
1028 * * filename is filled with the filename, should be freed
1029 * * line is filled with the line number or -1 */
1030 static void msgwin_parse_generic_line(const gchar *string, gchar **filename, gint *line)
1032 gchar **fields;
1033 gboolean incertain = TRUE; /* whether we're reasonably certain of the result */
1035 *filename = NULL;
1036 *line = -1;
1038 fields = g_strsplit(string, ":", 2);
1039 /* extract the filename */
1040 if (fields[0] != NULL)
1042 *filename = g_strdup(fields[0]);
1043 if (msgwindow.messages_dir != NULL)
1044 make_absolute(filename, msgwindow.messages_dir);
1046 /* now the line */
1047 if (fields[1] != NULL)
1049 gchar *end;
1051 *line = strtol(fields[1], &end, 10);
1052 if (end == fields[1])
1053 *line = -1;
1054 else if (*end == ':' || g_ascii_isspace(*end))
1055 { /* if we have a blank or a separator right after the number, assume we really got a
1056 * filename (it's a grep-like syntax) */
1057 incertain = FALSE;
1061 /* if we aren't sure we got a supposedly correct filename, check it */
1062 if (incertain && ! g_file_test(*filename, G_FILE_TEST_EXISTS))
1064 SETPTR(*filename, NULL);
1065 *line = -1;
1068 g_strfreev(fields);
1072 gboolean msgwin_goto_messages_file_line(gboolean focus_editor)
1074 GtkTreeIter iter;
1075 GtkTreeModel *model;
1076 GtkTreeSelection *selection;
1077 gboolean ret = FALSE;
1079 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
1080 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1082 gint line;
1083 guint id;
1084 gchar *string;
1085 GeanyDocument *doc;
1086 GeanyDocument *old_doc = document_get_current();
1088 gtk_tree_model_get(model, &iter,
1089 MSG_COL_LINE, &line, MSG_COL_DOC_ID, &id, MSG_COL_STRING, &string, -1);
1090 if (line >= 0 && id > 0)
1092 /* check doc is still open */
1093 doc = document_find_by_id(id);
1094 if (!doc)
1096 ui_set_statusbar(FALSE, _("The document has been closed."));
1097 utils_beep();
1099 else
1101 ret = navqueue_goto_line(old_doc, doc, line);
1102 if (ret && focus_editor)
1103 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1106 else if (line < 0 && string != NULL)
1108 gchar *filename;
1110 /* try with a file:line parsing */
1111 msgwin_parse_generic_line(string, &filename, &line);
1112 if (filename != NULL)
1114 /* use document_open_file to find an already open file, or open it in place */
1115 doc = document_open_file(filename, FALSE, NULL, NULL);
1116 if (doc != NULL)
1118 ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line);
1119 if (ret && focus_editor)
1120 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1123 g_free(filename);
1125 g_free(string);
1127 return ret;
1131 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1132 gpointer user_data)
1134 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1135 gboolean double_click = event->type == GDK_2BUTTON_PRESS;
1137 if (event->button == 1 && (event->type == GDK_BUTTON_RELEASE || double_click))
1139 switch (GPOINTER_TO_INT(user_data))
1141 case MSG_COMPILER:
1142 { /* mouse click in the compiler treeview */
1143 msgwin_goto_compiler_file_line(double_click);
1144 break;
1146 case MSG_MESSAGE:
1147 { /* mouse click in the message treeview (results of 'Find usage') */
1148 msgwin_goto_messages_file_line(double_click);
1149 break;
1152 return double_click; /* TRUE prevents message window re-focusing */
1155 if (event->button == 3)
1156 { /* popupmenu to hide or clear the active treeview */
1157 switch (GPOINTER_TO_INT(user_data))
1159 case MSG_STATUS:
1161 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1162 event->button, event->time);
1163 break;
1165 case MSG_MESSAGE:
1167 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1168 event->button, event->time);
1169 break;
1171 case MSG_COMPILER:
1173 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1174 event->button, event->time);
1175 break;
1179 return FALSE;
1184 * Switches to the given notebook tab of the messages window and shows the messages window
1185 * if it was previously hidden and @a show is set to @c TRUE.
1187 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1188 * #MessageWindowTabNum.
1189 * @param show Whether to show the messages window at all if it was hidden before.
1191 * @since 0.15
1193 void msgwin_switch_tab(gint tabnum, gboolean show)
1195 GtkWidget *widget = NULL; /* widget to focus */
1197 switch (tabnum)
1199 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1200 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1201 case MSG_STATUS: widget = msgwindow.tree_status; break;
1202 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1203 #ifdef HAVE_VTE
1204 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1205 #endif
1206 default: break;
1209 /* the msgwin must be visible before we switch to the VTE page so that
1210 * the font settings are applied on realization */
1211 if (show)
1212 msgwin_show_hide(TRUE);
1213 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1214 if (show && widget)
1215 gtk_widget_grab_focus(widget);
1220 * Removes all messages from a tab specified by @a tabnum in the messages window.
1222 * @param tabnum An index of a tab in the messages window which should be cleared.
1223 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1225 * @since 0.15
1227 void msgwin_clear_tab(gint tabnum)
1229 GtkListStore *store = NULL;
1231 switch (tabnum)
1233 case MSG_MESSAGE:
1234 store = msgwindow.store_msg;
1235 break;
1237 case MSG_COMPILER:
1238 gtk_list_store_clear(msgwindow.store_compiler);
1239 build_menu_update(NULL); /* update next error items */
1240 return;
1242 case MSG_STATUS: store = msgwindow.store_status; break;
1243 default: return;
1245 if (store == NULL)
1246 return;
1247 gtk_list_store_clear(store);