prefs: Remove some global state in keybinding-related code
[geany-mirror.git] / src / msgwindow.c
blobebe7354b54d755811318c7475c814c604a4204ff
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;
70 static void prepare_msg_tree_view(void);
71 static void prepare_status_tree_view(void);
72 static void prepare_compiler_tree_view(void);
73 static GtkWidget *create_message_popup_menu(gint type);
74 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
75 gpointer user_data);
76 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data);
79 void msgwin_show_hide_tabs(void)
81 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_status), interface_prefs.msgwin_status_visible);
82 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_compiler), interface_prefs.msgwin_compiler_visible);
83 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_msg), interface_prefs.msgwin_messages_visible);
84 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.scribble), interface_prefs.msgwin_scribble_visible);
88 /** Sets the Messages path for opening any parsed filenames without absolute path
89 * from message lines.
90 * @param messages_dir The directory. **/
91 void msgwin_set_messages_dir(const gchar *messages_dir)
93 g_free(msgwindow.messages_dir);
94 msgwindow.messages_dir = g_strdup(messages_dir);
98 void msgwin_init(void)
100 msgwindow.notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
101 msgwindow.tree_status = ui_lookup_widget(main_widgets.window, "treeview3");
102 msgwindow.tree_msg = ui_lookup_widget(main_widgets.window, "treeview4");
103 msgwindow.tree_compiler = ui_lookup_widget(main_widgets.window, "treeview5");
104 msgwindow.scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
105 msgwindow.messages_dir = NULL;
107 prepare_status_tree_view();
108 prepare_msg_tree_view();
109 prepare_compiler_tree_view();
110 msgwindow.popup_status_menu = create_message_popup_menu(MSG_STATUS);
111 msgwindow.popup_msg_menu = create_message_popup_menu(MSG_MESSAGE);
112 msgwindow.popup_compiler_menu = create_message_popup_menu(MSG_COMPILER);
114 ui_widget_modify_font_from_string(msgwindow.scribble, interface_prefs.msgwin_font);
115 g_signal_connect(msgwindow.scribble, "populate-popup", G_CALLBACK(on_scribble_populate), NULL);
119 void msgwin_finalize(void)
121 g_free(msgwindow.messages_dir);
125 static gboolean on_msgwin_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
127 gboolean enter_or_return = ui_is_keyval_enter_or_return(event->keyval);
129 if (enter_or_return || event->keyval == GDK_space)
131 switch (GPOINTER_TO_INT(data))
133 case MSG_COMPILER:
134 { /* key press in the compiler treeview */
135 msgwin_goto_compiler_file_line(enter_or_return);
136 break;
138 case MSG_MESSAGE:
139 { /* key press in the message treeview (results of 'Find usage') */
140 msgwin_goto_messages_file_line(enter_or_return);
141 break;
145 return FALSE;
149 /* does some preparing things to the status message list widget */
150 static void prepare_status_tree_view(void)
152 GtkCellRenderer *renderer;
153 GtkTreeViewColumn *column;
155 msgwindow.store_status = gtk_list_store_new(1, G_TYPE_STRING);
156 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_status), GTK_TREE_MODEL(msgwindow.store_status));
157 g_object_unref(msgwindow.store_status);
159 renderer = gtk_cell_renderer_text_new();
160 column = gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer, "text", 0, NULL);
161 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_status), column);
163 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_status), FALSE);
165 ui_widget_modify_font_from_string(msgwindow.tree_status, interface_prefs.msgwin_font);
167 g_signal_connect(msgwindow.tree_status, "button-press-event",
168 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_STATUS));
172 /* does some preparing things to the message list widget
173 * (currently used for showing results of 'Find usage') */
174 static void prepare_msg_tree_view(void)
176 GtkCellRenderer *renderer;
177 GtkTreeViewColumn *column;
178 GtkTreeSelection *selection;
180 /* line, doc, fg, str */
181 msgwindow.store_msg = gtk_list_store_new(4, G_TYPE_INT, G_TYPE_POINTER,
182 GDK_TYPE_COLOR, G_TYPE_STRING);
183 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_msg), GTK_TREE_MODEL(msgwindow.store_msg));
184 g_object_unref(msgwindow.store_msg);
186 renderer = gtk_cell_renderer_text_new();
187 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
188 "foreground-gdk", 2, "text", 3, NULL);
189 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_msg), column);
191 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_msg), FALSE);
193 ui_widget_modify_font_from_string(msgwindow.tree_msg, interface_prefs.msgwin_font);
195 /* use button-release-event so the selection has changed
196 * (connect_after button-press-event doesn't work) */
197 g_signal_connect(msgwindow.tree_msg, "button-release-event",
198 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
199 /* for double-clicking only, after the first release */
200 g_signal_connect(msgwindow.tree_msg, "button-press-event",
201 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
202 g_signal_connect(msgwindow.tree_msg, "key-press-event",
203 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_MESSAGE));
205 /* selection handling */
206 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
207 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
208 /*g_signal_connect(selection, "changed",G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
212 /* does some preparing things to the compiler list widget */
213 static void prepare_compiler_tree_view(void)
215 GtkCellRenderer *renderer;
216 GtkTreeViewColumn *column;
217 GtkTreeSelection *selection;
219 msgwindow.store_compiler = gtk_list_store_new(2, GDK_TYPE_COLOR, G_TYPE_STRING);
220 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_compiler), GTK_TREE_MODEL(msgwindow.store_compiler));
221 g_object_unref(msgwindow.store_compiler);
223 renderer = gtk_cell_renderer_text_new();
224 column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "foreground-gdk", 0, "text", 1, NULL);
225 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_compiler), column);
227 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_compiler), FALSE);
229 ui_widget_modify_font_from_string(msgwindow.tree_compiler, interface_prefs.msgwin_font);
231 /* use button-release-event so the selection has changed
232 * (connect_after button-press-event doesn't work) */
233 g_signal_connect(msgwindow.tree_compiler, "button-release-event",
234 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
235 /* for double-clicking only, after the first release */
236 g_signal_connect(msgwindow.tree_compiler, "button-press-event",
237 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
238 g_signal_connect(msgwindow.tree_compiler, "key-press-event",
239 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_COMPILER));
241 /* selection handling */
242 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
243 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
244 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
248 static const GdkColor color_error = {0, 65535, 0, 0};
250 static const GdkColor *get_color(gint msg_color)
252 static const GdkColor dark_red = {0, 65535 / 2, 0, 0};
253 static const GdkColor blue = {0, 0, 0, 0xD000}; /* not too bright ;-) */
255 switch (msg_color)
257 case COLOR_RED: return &color_error;
258 case COLOR_DARK_RED: return &dark_red;
259 case COLOR_BLUE: return &blue;
260 default: return NULL;
266 * Adds a new message in the compiler tab treeview in the messages window.
268 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
269 * @param format @c printf()-style format string.
270 * @param ... Arguments for the @c format string.
272 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
274 gchar *string;
275 va_list args;
277 va_start(args, format);
278 string = g_strdup_vprintf(format, args);
279 va_end(args);
280 msgwin_compiler_add_string(msg_color, string);
281 g_free(string);
285 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
287 GtkTreeIter iter;
288 GtkTreePath *path;
289 const GdkColor *color = get_color(msg_color);
290 gchar *utf8_msg;
292 if (! g_utf8_validate(msg, -1, NULL))
293 utf8_msg = utils_get_utf8_from_locale(msg);
294 else
295 utf8_msg = (gchar *) msg;
297 gtk_list_store_append(msgwindow.store_compiler, &iter);
298 gtk_list_store_set(msgwindow.store_compiler, &iter, 0, color, 1, utf8_msg, -1);
300 if (ui_prefs.msgwindow_visible && interface_prefs.compiler_tab_autoscroll)
302 path = gtk_tree_model_get_path(
303 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
304 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
305 gtk_tree_path_free(path);
308 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
309 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
310 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
312 if (utf8_msg != msg)
313 g_free(utf8_msg);
317 void msgwin_show_hide(gboolean show)
319 ui_prefs.msgwindow_visible = show;
320 ignore_callback = TRUE;
321 gtk_check_menu_item_set_active(
322 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
323 show);
324 ignore_callback = FALSE;
325 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"), show);
326 /* set the input focus back to the editor */
327 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
332 * Adds a new message in the messages tab treeview in the messages window.
333 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
334 * by @a doc into the line specified with @a line.
336 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
337 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
338 * @param doc The document. Set to @c NULL to ignore.
339 * @param format @c printf()-style format string.
340 * @param ... Arguments for the @c format string.
342 * @since 0.15
344 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
346 gchar *string;
347 va_list args;
349 va_start(args, format);
350 string = g_strdup_vprintf(format, args);
351 va_end(args);
353 msgwin_msg_add_string(msg_color, line, doc, string);
354 g_free(string);
358 /* adds string to the msg treeview */
359 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
361 GtkTreeIter iter;
362 const GdkColor *color = get_color(msg_color);
363 gchar *tmp;
364 gsize len;
365 gchar *utf8_msg;
367 if (! ui_prefs.msgwindow_visible)
368 msgwin_show_hide(TRUE);
370 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
371 * cut the string to a maximum of 1024 bytes and discard the rest */
372 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
373 len = strlen(string);
374 if (len > 1024)
375 tmp = g_strndup(string, 1024);
376 else
377 tmp = g_strdup(string);
379 if (! g_utf8_validate(tmp, -1, NULL))
380 utf8_msg = utils_get_utf8_from_locale(tmp);
381 else
382 utf8_msg = tmp;
384 gtk_list_store_append(msgwindow.store_msg, &iter);
385 gtk_list_store_set(msgwindow.store_msg, &iter, 0, line, 1, doc, 2, color, 3, utf8_msg, -1);
387 g_free(tmp);
388 if (utf8_msg != tmp)
389 g_free(utf8_msg);
394 * Logs a status message *without* setting the status bar.
395 * (Use ui_set_statusbar() to display text on the statusbar)
397 * @param format @c printf()-style format string.
398 * @param ... Arguments for the @c format string.
400 void msgwin_status_add(const gchar *format, ...)
402 GtkTreeIter iter;
403 gchar *string;
404 gchar *statusmsg, *time_str;
405 va_list args;
407 va_start(args, format);
408 string = g_strdup_vprintf(format, args);
409 va_end(args);
411 /* add a timestamp to status messages */
412 time_str = utils_get_current_time_string();
413 statusmsg = g_strconcat(time_str, ": ", string, NULL);
414 g_free(time_str);
415 g_free(string);
417 /* add message to Status window */
418 gtk_list_store_append(msgwindow.store_status, &iter);
419 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
420 g_free(statusmsg);
422 if (G_LIKELY(main_status.main_window_realized))
424 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
426 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
427 if (prefs.switch_to_status)
428 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
429 gtk_tree_path_free(path);
434 static void
435 on_message_treeview_clear_activate(GtkMenuItem *menuitem, gpointer user_data)
437 gint tabnum = GPOINTER_TO_INT(user_data);
439 msgwin_clear_tab(tabnum);
443 static void
444 on_compiler_treeview_copy_activate(GtkMenuItem *menuitem, gpointer user_data)
446 GtkWidget *tv = NULL;
447 GtkTreeSelection *selection;
448 GtkTreeModel *model;
449 GtkTreeIter iter;
450 gint str_idx = 1;
452 switch (GPOINTER_TO_INT(user_data))
454 case MSG_STATUS:
455 tv = msgwindow.tree_status;
456 str_idx = 0;
457 break;
459 case MSG_COMPILER:
460 tv = msgwindow.tree_compiler;
461 break;
463 case MSG_MESSAGE:
464 tv = msgwindow.tree_msg;
465 str_idx = 3;
466 break;
468 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
470 if (gtk_tree_selection_get_selected(selection, &model, &iter))
472 gchar *string;
474 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
475 if (!EMPTY(string))
477 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
478 string, -1);
480 g_free(string);
485 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
487 GtkListStore *store = msgwindow.store_compiler;
488 GtkTreeIter iter;
489 GString *str = g_string_new("");
490 gint str_idx = 1;
491 gboolean valid;
493 switch (GPOINTER_TO_INT(user_data))
495 case MSG_STATUS:
496 store = msgwindow.store_status;
497 str_idx = 0;
498 break;
500 case MSG_COMPILER:
501 /* default values */
502 break;
504 case MSG_MESSAGE:
505 store = msgwindow.store_msg;
506 str_idx = 3;
507 break;
510 /* walk through the list and copy every line into a string */
511 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
512 while (valid)
514 gchar *line;
516 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
517 if (!EMPTY(line))
519 g_string_append(str, line);
520 g_string_append_c(str, '\n');
522 g_free(line);
524 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
527 /* copy the string into the clipboard */
528 if (str->len > 0)
530 gtk_clipboard_set_text(
531 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
532 str->str,
533 str->len);
535 g_string_free(str, TRUE);
539 static void
540 on_hide_message_window(GtkMenuItem *menuitem, gpointer user_data)
542 msgwin_show_hide(FALSE);
546 static GtkWidget *create_message_popup_menu(gint type)
548 GtkWidget *message_popup_menu, *clear, *copy, *copy_all, *image;
550 message_popup_menu = gtk_menu_new();
552 clear = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLEAR, NULL);
553 gtk_widget_show(clear);
554 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
555 g_signal_connect(clear, "activate",
556 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
558 copy = gtk_image_menu_item_new_with_mnemonic(_("C_opy"));
559 gtk_widget_show(copy);
560 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
561 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
562 gtk_widget_show(image);
563 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
564 g_signal_connect(copy, "activate",
565 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
567 copy_all = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
568 gtk_widget_show(copy_all);
569 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy_all);
570 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
571 gtk_widget_show(image);
572 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy_all), image);
573 g_signal_connect(copy_all, "activate",
574 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
576 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
578 return message_popup_menu;
582 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
584 msgwin_menu_add_common_items(arg1);
588 /* Menu items that should be on all message window popup menus */
589 void msgwin_menu_add_common_items(GtkMenu *menu)
591 GtkWidget *item;
593 item = gtk_separator_menu_item_new();
594 gtk_widget_show(item);
595 gtk_container_add(GTK_CONTAINER(menu), item);
597 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
598 gtk_widget_show(item);
599 gtk_container_add(GTK_CONTAINER(menu), item);
600 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
604 /* look back up from the current path and find the directory we came from */
605 static gboolean
606 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
608 GtkTreeIter iter;
609 *prefix = NULL;
611 while (gtk_tree_path_prev(cur))
613 if (gtk_tree_model_get_iter(model, &iter, cur))
615 gchar *string;
616 gtk_tree_model_get(model, &iter, 1, &string, -1);
617 if (string != NULL && build_parse_make_dir(string, prefix))
619 g_free(string);
620 return TRUE;
622 g_free(string);
626 return FALSE;
630 static gboolean goto_compiler_file_line(const gchar *filename, gint line, gboolean focus_editor)
632 if (!filename || line <= -1)
633 return FALSE;
635 /* If the path doesn't exist, try the current document.
636 * This happens when we receive build messages in the wrong order - after the
637 * 'Leaving directory' messages */
638 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
640 gchar *cur_dir = utils_get_current_file_dir_utf8();
641 gchar *name;
643 if (cur_dir)
645 /* we let the user know we couldn't find the parsed filename from the message window */
646 SETPTR(cur_dir, utils_get_locale_from_utf8(cur_dir));
647 name = g_path_get_basename(filename);
648 SETPTR(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
649 g_free(cur_dir);
651 if (g_file_test(name, G_FILE_TEST_EXISTS))
653 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
654 filename);
655 filename = name;
657 else
658 g_free(name);
663 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
664 GeanyDocument *doc = document_find_by_filename(utf8_filename);
665 GeanyDocument *old_doc = document_get_current();
667 g_free(utf8_filename);
669 if (doc == NULL) /* file not already open */
670 doc = document_open_file(filename, FALSE, NULL, NULL);
672 if (doc != NULL)
674 gboolean ret;
676 if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
677 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
679 ret = navqueue_goto_line(old_doc, doc, line);
680 if (ret && focus_editor)
681 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
683 return ret;
686 return FALSE;
690 gboolean msgwin_goto_compiler_file_line(gboolean focus_editor)
692 GtkTreeIter iter;
693 GtkTreeModel *model;
694 GtkTreeSelection *selection;
695 gchar *string;
696 GdkColor *color;
698 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
699 if (gtk_tree_selection_get_selected(selection, &model, &iter))
701 /* if the item is not coloured red, it's not an error line */
702 gtk_tree_model_get(model, &iter, 0, &color, -1);
703 if (color == NULL || ! gdk_color_equal(color, &color_error))
705 if (color != NULL)
706 gdk_color_free(color);
707 return FALSE;
709 gdk_color_free(color);
711 gtk_tree_model_get(model, &iter, 1, &string, -1);
712 if (string != NULL)
714 gint line;
715 gchar *filename, *dir;
716 GtkTreePath *path;
717 gboolean ret;
719 path = gtk_tree_model_get_path(model, &iter);
720 find_prev_build_dir(path, model, &dir);
721 gtk_tree_path_free(path);
722 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
723 g_free(string);
724 g_free(dir);
726 ret = goto_compiler_file_line(filename, line, focus_editor);
727 g_free(filename);
728 return ret;
731 return FALSE;
735 static void make_absolute(gchar **filename, const gchar *dir)
737 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
739 if (*filename == NULL)
740 return;
742 /* skip some characters at the beginning of the filename, at the moment only "./"
743 * can be extended if other "trash" is known */
744 if (strncmp(*filename, "./", 2) == 0)
745 skip_dot_slash = 2;
747 /* add directory */
748 if (! utils_is_absolute_path(*filename))
749 SETPTR(*filename, g_build_filename(dir, *filename + skip_dot_slash, NULL));
753 /* try to parse the file and line number where the error occured described in line
754 * and when something useful is found, it stores the line number in *line and the
755 * relevant file with the error in *filename.
756 * *line will be -1 if no error was found in string.
757 * *filename must be freed unless it is NULL. */
758 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
760 gchar *end = NULL;
761 gchar **fields;
763 *filename = NULL;
764 *line = -1;
766 g_return_if_fail(data->string != NULL);
768 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
770 /* parse the line */
771 if (g_strv_length(fields) < data->min_fields)
773 g_strfreev(fields);
774 return;
777 *line = strtol(fields[data->line_idx], &end, 10);
779 /* if the line could not be read, line is 0 and an error occurred, so we leave */
780 if (fields[data->line_idx] == end)
782 g_strfreev(fields);
783 return;
786 /* let's stop here if there is no filename in the error message */
787 if (data->file_idx == -1)
789 /* we have no filename in the error message, so take the current one and hope it's correct */
790 GeanyDocument *doc = document_get_current();
791 if (doc != NULL)
792 *filename = g_strdup(doc->file_name);
793 g_strfreev(fields);
794 return;
797 *filename = g_strdup(fields[data->file_idx]);
798 g_strfreev(fields);
802 static void parse_compiler_error_line(const gchar *string,
803 gchar **filename, gint *line)
805 ParseData data = {NULL, NULL, 0, 0, 0};
807 data.string = string;
809 switch (build_info.file_type_id)
811 case GEANY_FILETYPES_PHP:
813 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
814 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
815 gchar *tmp = strstr(string, " in ");
817 if (tmp != NULL)
819 data.string = tmp;
820 data.pattern = " ";
821 data.min_fields = 6;
822 data.line_idx = 5;
823 data.file_idx = 2;
825 else
827 data.pattern = " ";
828 data.min_fields = 11;
829 data.line_idx = 10;
830 data.file_idx = 7;
832 break;
834 case GEANY_FILETYPES_PERL:
836 /* syntax error at test.pl line 7, near "{ */
837 data.pattern = " ";
838 data.min_fields = 6;
839 data.line_idx = 5;
840 data.file_idx = 3;
841 break;
843 /* the error output of python and tcl equals */
844 case GEANY_FILETYPES_TCL:
845 case GEANY_FILETYPES_PYTHON:
847 /* File "HyperArch.py", line 37, in ?
848 * (file "clrdial.tcl" line 12)
849 * */
850 if (strstr(string, " line ") != NULL)
852 /* Tcl and old Python format (<= Python 2.5) */
853 data.pattern = " \"";
854 data.min_fields = 6;
855 data.line_idx = 5;
856 data.file_idx = 2;
858 else
860 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
861 * (used since Python 2.6) */
862 data.pattern = ",'";
863 data.min_fields = 8;
864 data.line_idx = 6;
865 data.file_idx = 4;
867 break;
869 case GEANY_FILETYPES_BASIC:
870 case GEANY_FILETYPES_PASCAL:
871 case GEANY_FILETYPES_CS:
873 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
874 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
875 data.pattern = "(";
876 data.min_fields = 2;
877 data.line_idx = 1;
878 data.file_idx = 0;
879 break;
881 case GEANY_FILETYPES_D:
883 /* GNU D compiler front-end, gdc
884 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
885 * warning - gantry.d:20: statement is not reachable
886 * Digital Mars dmd compiler
887 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
888 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
889 if (strncmp(string, "warning - ", 10) == 0)
891 data.pattern = " (:";
892 data.min_fields = 4;
893 data.line_idx = 3;
894 data.file_idx = 2;
896 else
898 data.pattern = "(:";
899 data.min_fields = 2;
900 data.line_idx = 1;
901 data.file_idx = 0;
903 break;
905 case GEANY_FILETYPES_FERITE:
907 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
908 * Error: Compile Error: on line 24, in /test/class.fe */
909 if (strncmp(string, "Error: Compile Error", 20) == 0)
911 data.pattern = " ";
912 data.min_fields = 8;
913 data.line_idx = 5;
914 data.file_idx = 7;
916 else
918 data.pattern = " \"";
919 data.min_fields = 10;
920 data.line_idx = 5;
921 data.file_idx = 8;
923 break;
925 case GEANY_FILETYPES_HTML:
927 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
928 data.pattern = " ";
929 data.min_fields = 4;
930 data.line_idx = 1;
931 data.file_idx = -1;
932 break;
934 /* All GNU gcc-like error messages */
935 case GEANY_FILETYPES_C:
936 case GEANY_FILETYPES_CPP:
937 case GEANY_FILETYPES_RUBY:
938 case GEANY_FILETYPES_JAVA:
939 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
940 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
941 * empty.c:21: error: conflicting types for `foo'
942 * Only parse file and line, so that linker errors will also work (with -g) */
943 case GEANY_FILETYPES_F77:
944 case GEANY_FILETYPES_FORTRAN:
945 case GEANY_FILETYPES_LATEX:
946 /* ./kommtechnik_2b.tex:18: Emergency stop. */
947 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
948 case GEANY_FILETYPES_NONE:
949 default: /* The default is a GNU gcc type error */
951 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
952 strncmp(string, "[javac]", 7) == 0)
954 /* Java Apache Ant.
955 * [javac] <Full Path to File + extension>:<line n°>: <error> */
956 data.pattern = " :";
957 data.min_fields = 4;
958 data.line_idx = 2;
959 data.file_idx = 1;
960 break;
962 /* don't accidently find libtool versions x:y:x and think it is a file name */
963 if (strstr(string, "libtool --mode=link") == NULL)
965 data.pattern = ":";
966 data.min_fields = 3;
967 data.line_idx = 1;
968 data.file_idx = 0;
969 break;
974 if (data.pattern != NULL)
975 parse_file_line(&data, filename, line);
979 /* try to parse the file and line number where the error occured described in string
980 * and when something useful is found, it stores the line number in *line and the
981 * relevant file with the error in *filename.
982 * *line will be -1 if no error was found in string.
983 * *filename must be freed unless it is NULL. */
984 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
985 gchar **filename, gint *line)
987 GeanyFiletype *ft;
988 gchar *trimmed_string;
990 *filename = NULL;
991 *line = -1;
993 if (G_UNLIKELY(string == NULL))
994 return;
996 if (dir == NULL)
997 dir = build_info.dir;
998 g_return_if_fail(dir != NULL);
1000 trimmed_string = g_strdup(string);
1001 g_strchug(trimmed_string); /* remove possible leading whitespace */
1003 ft = filetypes[build_info.file_type_id];
1005 /* try parsing with a custom regex */
1006 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
1008 /* fallback to default old-style parsing */
1009 parse_compiler_error_line(trimmed_string, filename, line);
1011 make_absolute(filename, dir);
1012 g_free(trimmed_string);
1016 /* Tries to parse strings of the file:line style, allowing line field to be missing
1017 * * filename is filled with the filename, should be freed
1018 * * line is filled with the line number or -1 */
1019 static void msgwin_parse_generic_line(const gchar *string, gchar **filename, gint *line)
1021 gchar **fields;
1022 gboolean incertain = TRUE; /* whether we're reasonably certain of the result */
1024 *filename = NULL;
1025 *line = -1;
1027 fields = g_strsplit(string, ":", 2);
1028 /* extract the filename */
1029 if (fields[0] != NULL)
1031 *filename = g_strdup(fields[0]);
1032 if (msgwindow.messages_dir != NULL)
1033 make_absolute(filename, msgwindow.messages_dir);
1035 /* now the line */
1036 if (fields[1] != NULL)
1038 gchar *end;
1040 *line = strtol(fields[1], &end, 10);
1041 if (end == fields[1])
1042 *line = -1;
1043 else if (*end == ':' || g_ascii_isspace(*end))
1044 { /* if we have a blank or a separator right after the number, assume we really got a
1045 * filename (it's a grep-like syntax) */
1046 incertain = FALSE;
1050 /* if we aren't sure we got a supposedly correct filename, check it */
1051 if (incertain && ! g_file_test(*filename, G_FILE_TEST_EXISTS))
1053 SETPTR(*filename, NULL);
1054 *line = -1;
1057 g_strfreev(fields);
1061 gboolean msgwin_goto_messages_file_line(gboolean focus_editor)
1063 GtkTreeIter iter;
1064 GtkTreeModel *model;
1065 GtkTreeSelection *selection;
1066 gboolean ret = FALSE;
1068 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
1069 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1071 gint line;
1072 gchar *string;
1073 GeanyDocument *doc;
1074 GeanyDocument *old_doc = document_get_current();
1076 gtk_tree_model_get(model, &iter, 0, &line, 1, &doc, 3, &string, -1);
1077 /* doc may have been closed, so check doc->index: */
1078 if (line >= 0 && DOC_VALID(doc))
1080 ret = navqueue_goto_line(old_doc, doc, line);
1081 if (ret && focus_editor)
1082 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1084 else if (line < 0 && string != NULL)
1086 gchar *filename;
1088 /* try with a file:line parsing */
1089 msgwin_parse_generic_line(string, &filename, &line);
1090 if (filename != NULL)
1092 /* use document_open_file to find an already open file, or open it in place */
1093 doc = document_open_file(filename, FALSE, NULL, NULL);
1094 if (doc != NULL)
1096 ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line);
1097 if (ret && focus_editor)
1098 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1101 g_free(filename);
1103 g_free(string);
1105 return ret;
1109 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1110 gpointer user_data)
1112 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1113 gboolean double_click = event->type == GDK_2BUTTON_PRESS;
1115 if (event->button == 1 && (event->type == GDK_BUTTON_RELEASE || double_click))
1117 switch (GPOINTER_TO_INT(user_data))
1119 case MSG_COMPILER:
1120 { /* mouse click in the compiler treeview */
1121 msgwin_goto_compiler_file_line(double_click);
1122 break;
1124 case MSG_MESSAGE:
1125 { /* mouse click in the message treeview (results of 'Find usage') */
1126 msgwin_goto_messages_file_line(double_click);
1127 break;
1130 return double_click; /* TRUE prevents message window re-focusing */
1133 if (event->button == 3)
1134 { /* popupmenu to hide or clear the active treeview */
1135 switch (GPOINTER_TO_INT(user_data))
1137 case MSG_STATUS:
1139 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1140 event->button, event->time);
1141 break;
1143 case MSG_MESSAGE:
1145 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1146 event->button, event->time);
1147 break;
1149 case MSG_COMPILER:
1151 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1152 event->button, event->time);
1153 break;
1157 return FALSE;
1162 * Switches to the given notebook tab of the messages window and shows the messages window
1163 * if it was previously hidden and @a show is set to @c TRUE.
1165 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1166 * #MessageWindowTabNum.
1167 * @param show Whether to show the messages window at all if it was hidden before.
1169 * @since 0.15
1171 void msgwin_switch_tab(gint tabnum, gboolean show)
1173 GtkWidget *widget = NULL; /* widget to focus */
1175 switch (tabnum)
1177 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1178 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1179 case MSG_STATUS: widget = msgwindow.tree_status; break;
1180 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1181 #ifdef HAVE_VTE
1182 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1183 #endif
1184 default: break;
1187 /* the msgwin must be visible before we switch to the VTE page so that
1188 * the font settings are applied on realization */
1189 if (show)
1190 msgwin_show_hide(TRUE);
1191 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1192 if (show && widget)
1193 gtk_widget_grab_focus(widget);
1198 * Removes all messages from a tab specified by @a tabnum in the messages window.
1200 * @param tabnum An index of a tab in the messages window which should be cleared.
1201 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1203 * @since 0.15
1205 void msgwin_clear_tab(gint tabnum)
1207 GtkListStore *store = NULL;
1209 switch (tabnum)
1211 case MSG_MESSAGE:
1212 store = msgwindow.store_msg;
1213 break;
1215 case MSG_COMPILER:
1216 gtk_list_store_clear(msgwindow.store_compiler);
1217 build_menu_update(NULL); /* update next error items */
1218 return;
1220 case MSG_STATUS: store = msgwindow.store_status; break;
1221 default: return;
1223 if (store == NULL)
1224 return;
1225 gtk_list_store_clear(store);