Add 'Refresh' popup menu item (part of geany-plugins #2999858).
[geany-mirror.git] / src / msgwindow.c
blob4587da66045422a07a0f437e5f837e93b56b3d17
1 /*
2 * msgwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * $Id$
25 * Message window functions (status, compiler, messages windows).
26 * Also compiler error message parsing and grep file and line parsing.
30 #include "geany.h"
32 #include "support.h"
33 #include "prefs.h"
34 #include "callbacks.h"
35 #include "ui_utils.h"
36 #include "utils.h"
37 #include "document.h"
38 #include "filetypes.h"
39 #include "build.h"
40 #include "main.h"
41 #include "vte.h"
42 #include "navqueue.h"
43 #include "editor.h"
44 #include "msgwindow.h"
45 #include "keybindings.h"
47 #include <string.h>
48 #include <stdlib.h>
49 #include <time.h>
51 #include <gdk/gdkkeysyms.h>
54 /* used for parse_file_line */
55 typedef struct
57 const gchar *string; /* line data */
58 const gchar *pattern; /* pattern to split the error message into some fields */
59 guint min_fields; /* used to detect errors after parsing */
60 guint line_idx; /* idx of the field where the line is */
61 gint file_idx; /* idx of the field where the filename is or -1 */
63 ParseData;
65 MessageWindow msgwindow;
68 static void prepare_msg_tree_view(void);
69 static void prepare_status_tree_view(void);
70 static void prepare_compiler_tree_view(void);
71 static GtkWidget *create_message_popup_menu(gint type);
72 static void msgwin_parse_grep_line(const gchar *string, gchar **filename, gint *line);
73 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
74 gpointer user_data);
75 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data);
78 void msgwin_show_hide_tabs(void)
80 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_status), interface_prefs.msgwin_status_visible);
81 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_compiler), interface_prefs.msgwin_compiler_visible);
82 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_msg), interface_prefs.msgwin_messages_visible);
83 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.scribble), interface_prefs.msgwin_scribble_visible);
87 void msgwin_init(void)
89 msgwindow.notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
90 msgwindow.tree_status = ui_lookup_widget(main_widgets.window, "treeview3");
91 msgwindow.tree_msg = ui_lookup_widget(main_widgets.window, "treeview4");
92 msgwindow.tree_compiler = ui_lookup_widget(main_widgets.window, "treeview5");
93 msgwindow.scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
94 msgwindow.find_in_files_dir = NULL;
96 prepare_status_tree_view();
97 prepare_msg_tree_view();
98 prepare_compiler_tree_view();
99 msgwindow.popup_status_menu = create_message_popup_menu(MSG_STATUS);
100 msgwindow.popup_msg_menu = create_message_popup_menu(MSG_MESSAGE);
101 msgwindow.popup_compiler_menu = create_message_popup_menu(MSG_COMPILER);
103 ui_widget_modify_font_from_string(msgwindow.scribble, interface_prefs.msgwin_font);
104 g_signal_connect(msgwindow.scribble, "populate-popup", G_CALLBACK(on_scribble_populate), NULL);
108 void msgwin_finalize(void)
110 g_free(msgwindow.find_in_files_dir);
114 static gboolean on_msgwin_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
116 if (ui_is_keyval_enter_or_return(event->keyval) || event->keyval == GDK_space)
118 switch (GPOINTER_TO_INT(data))
120 case MSG_COMPILER:
121 { /* single click in the compiler treeview */
122 msgwin_goto_compiler_file_line(event->keyval);
123 break;
125 case MSG_MESSAGE:
126 { /* single click in the message treeview (results of 'Find usage') */
127 msgwin_goto_messages_file_line(event->keyval);
128 break;
132 return FALSE;
136 /* does some preparing things to the status message list widget */
137 static void prepare_status_tree_view(void)
139 GtkCellRenderer *renderer;
140 GtkTreeViewColumn *column;
142 msgwindow.store_status = gtk_list_store_new(1, G_TYPE_STRING);
143 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_status), GTK_TREE_MODEL(msgwindow.store_status));
144 g_object_unref(msgwindow.store_status);
146 renderer = gtk_cell_renderer_text_new();
147 column = gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer, "text", 0, NULL);
148 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_status), column);
150 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_status), FALSE);
152 ui_widget_modify_font_from_string(msgwindow.tree_status, interface_prefs.msgwin_font);
154 g_signal_connect(msgwindow.tree_status, "button-press-event",
155 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_STATUS));
159 /* does some preparing things to the message list widget
160 * (currently used for showing results of 'Find usage') */
161 static void prepare_msg_tree_view(void)
163 GtkCellRenderer *renderer;
164 GtkTreeViewColumn *column;
165 GtkTreeSelection *selection;
167 /* line, doc, fg, str */
168 msgwindow.store_msg = gtk_list_store_new(4, G_TYPE_INT, G_TYPE_POINTER,
169 GDK_TYPE_COLOR, G_TYPE_STRING);
170 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_msg), GTK_TREE_MODEL(msgwindow.store_msg));
171 g_object_unref(msgwindow.store_msg);
173 renderer = gtk_cell_renderer_text_new();
174 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
175 "foreground-gdk", 2, "text", 3, NULL);
176 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_msg), column);
178 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_msg), FALSE);
180 ui_widget_modify_font_from_string(msgwindow.tree_msg, interface_prefs.msgwin_font);
182 /* use button-release-event so the selection has changed
183 * (connect_after button-press-event doesn't work) */
184 g_signal_connect(msgwindow.tree_msg, "button-release-event",
185 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
186 g_signal_connect(msgwindow.tree_msg, "key-press-event",
187 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_MESSAGE));
189 /* selection handling */
190 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
191 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
192 /*g_signal_connect(selection, "changed",G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
196 /* does some preparing things to the compiler list widget */
197 static void prepare_compiler_tree_view(void)
199 GtkCellRenderer *renderer;
200 GtkTreeViewColumn *column;
201 GtkTreeSelection *selection;
203 msgwindow.store_compiler = gtk_list_store_new(2, GDK_TYPE_COLOR, G_TYPE_STRING);
204 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_compiler), GTK_TREE_MODEL(msgwindow.store_compiler));
205 g_object_unref(msgwindow.store_compiler);
207 renderer = gtk_cell_renderer_text_new();
208 column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "foreground-gdk", 0, "text", 1, NULL);
209 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_compiler), column);
211 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_compiler), FALSE);
213 ui_widget_modify_font_from_string(msgwindow.tree_compiler, interface_prefs.msgwin_font);
215 /* use button-release-event so the selection has changed
216 * (connect_after button-press-event doesn't work) */
217 g_signal_connect(msgwindow.tree_compiler, "button-release-event",
218 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
219 g_signal_connect(msgwindow.tree_compiler, "key-press-event",
220 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_COMPILER));
222 /* selection handling */
223 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
224 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
225 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
229 static const GdkColor color_error = {0, 65535, 0, 0};
231 static const GdkColor *get_color(gint msg_color)
233 static const GdkColor dark_red = {0, 65535 / 2, 0, 0};
234 static const GdkColor blue = {0, 0, 0, 0xD000}; /* not too bright ;-) */
236 switch (msg_color)
238 case COLOR_RED: return &color_error;
239 case COLOR_DARK_RED: return &dark_red;
240 case COLOR_BLUE: return &blue;
241 default: return NULL;
247 * Adds a new message in the compiler tab treeview in the messages window.
249 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
250 * @param format @c printf()-style format string.
251 * @param ... Arguments for the @c format string.
253 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
255 gchar *string;
256 va_list args;
258 va_start(args, format);
259 string = g_strdup_vprintf(format, args);
260 va_end(args);
261 msgwin_compiler_add_string(msg_color, string);
262 g_free(string);
266 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
268 GtkTreeIter iter;
269 GtkTreePath *path;
270 const GdkColor *color = get_color(msg_color);
272 gtk_list_store_append(msgwindow.store_compiler, &iter);
273 gtk_list_store_set(msgwindow.store_compiler, &iter, 0, color, 1, msg, -1);
275 if (ui_prefs.msgwindow_visible)
277 path = gtk_tree_model_get_path(
278 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
279 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
280 gtk_tree_path_free(path);
283 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
284 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
285 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
289 void msgwin_show_hide(gboolean show)
291 ui_prefs.msgwindow_visible = show;
292 ignore_callback = TRUE;
293 gtk_check_menu_item_set_active(
294 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
295 show);
296 ignore_callback = FALSE;
297 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"), show);
298 /* set the input focus back to the editor */
299 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
304 * Adds a new message in the messages tab treeview in the messages window.
305 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
306 * by @a doc into the line specified with @a line.
308 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
309 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
310 * @param doc The document. Set to @c NULL to ignore.
311 * @param format @c printf()-style format string.
312 * @param ... Arguments for the @c format string.
314 * @since 0.15
316 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
318 gchar *string;
319 va_list args;
321 va_start(args, format);
322 string = g_strdup_vprintf(format, args);
323 va_end(args);
325 msgwin_msg_add_string(msg_color, line, doc, string);
326 g_free(string);
330 /* adds string to the msg treeview */
331 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
333 GtkTreeIter iter;
334 const GdkColor *color = get_color(msg_color);
335 gchar *tmp;
336 gsize len;
338 if (! ui_prefs.msgwindow_visible) msgwin_show_hide(TRUE);
340 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
341 * cut the string to a maximum of 1024 bytes and discard the rest */
342 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
343 len = strlen(string);
344 if (len > 1024)
345 tmp = g_strndup(string, 1024);
346 else
347 tmp = g_strdup(string);
349 gtk_list_store_append(msgwindow.store_msg, &iter);
350 gtk_list_store_set(msgwindow.store_msg, &iter, 0, line, 1, doc, 2, color, 3, tmp, -1);
352 g_free(tmp);
357 * Logs a status message *without* setting the status bar.
358 * (Use ui_set_statusbar() to display text on the statusbar)
360 * @param format @c printf()-style format string.
361 * @param ... Arguments for the @c format string.
363 void msgwin_status_add(const gchar *format, ...)
365 GtkTreeIter iter;
366 gchar *string;
367 gchar *statusmsg, *time_str;
368 va_list args;
370 va_start(args, format);
371 string = g_strdup_vprintf(format, args);
372 va_end(args);
374 /* add a timestamp to status messages */
375 time_str = utils_get_current_time_string();
376 statusmsg = g_strconcat(time_str, ": ", string, NULL);
377 g_free(time_str);
378 g_free(string);
380 /* add message to Status window */
381 gtk_list_store_append(msgwindow.store_status, &iter);
382 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
383 g_free(statusmsg);
385 if (G_LIKELY(main_status.main_window_realized))
387 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
389 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
390 if (prefs.switch_to_status)
391 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
392 gtk_tree_path_free(path);
397 static void
398 on_message_treeview_clear_activate (GtkMenuItem *menuitem,
399 gpointer user_data)
401 gint tabnum = GPOINTER_TO_INT(user_data);
403 msgwin_clear_tab(tabnum);
407 static void
408 on_compiler_treeview_copy_activate (GtkMenuItem *menuitem,
409 gpointer user_data)
411 GtkWidget *tv = NULL;
412 GtkTreeSelection *selection;
413 GtkTreeModel *model;
414 GtkTreeIter iter;
415 gint str_idx = 1;
417 switch (GPOINTER_TO_INT(user_data))
419 case MSG_STATUS:
420 tv = msgwindow.tree_status;
421 str_idx = 0;
422 break;
424 case MSG_COMPILER:
425 tv = msgwindow.tree_compiler;
426 break;
428 case MSG_MESSAGE:
429 tv = msgwindow.tree_msg;
430 str_idx = 3;
431 break;
433 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
435 if (gtk_tree_selection_get_selected(selection, &model, &iter))
437 gchar *string;
439 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
440 if (NZV(string))
442 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
443 string, -1);
445 g_free(string);
450 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
452 GtkListStore *store = msgwindow.store_compiler;
453 GtkTreeIter iter;
454 GString *str = g_string_new("");
455 gint str_idx = 1;
456 gboolean valid;
458 switch (GPOINTER_TO_INT(user_data))
460 case MSG_STATUS:
461 store = msgwindow.store_status;
462 str_idx = 0;
463 break;
465 case MSG_COMPILER:
466 /* default values */
467 break;
469 case MSG_MESSAGE:
470 store = msgwindow.store_msg;
471 str_idx = 3;
472 break;
475 /* walk through the list and copy every line into a string */
476 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
477 while (valid)
479 gchar *line;
481 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
482 if (NZV(line))
484 g_string_append(str, line);
485 g_string_append_c(str, '\n');
487 g_free(line);
489 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
492 /* copy the string into the clipboard */
493 if (str->len > 0)
495 gtk_clipboard_set_text(
496 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
497 str->str,
498 str->len);
500 g_string_free(str, TRUE);
504 static void
505 on_hide_message_window (GtkMenuItem *menuitem,
506 gpointer user_data)
508 msgwin_show_hide(FALSE);
512 static GtkWidget *create_message_popup_menu(gint type)
514 GtkWidget *message_popup_menu, *clear, *copy, *image;
516 message_popup_menu = gtk_menu_new();
518 clear = gtk_image_menu_item_new_from_stock("gtk-clear", NULL);
519 gtk_widget_show(clear);
520 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
521 g_signal_connect(clear, "activate",
522 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
524 copy = gtk_image_menu_item_new_from_stock("gtk-copy", NULL);
525 gtk_widget_show(copy);
526 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
527 g_signal_connect(copy, "activate",
528 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
530 copy = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
531 gtk_widget_show(copy);
532 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
533 image = gtk_image_new_from_stock("gtk-copy", GTK_ICON_SIZE_MENU);
534 gtk_widget_show(image);
535 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
536 g_signal_connect(copy, "activate",
537 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
539 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
541 return message_popup_menu;
545 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
547 msgwin_menu_add_common_items(arg1);
551 /* Menu items that should be on all message window popup menus */
552 void msgwin_menu_add_common_items(GtkMenu *menu)
554 GtkWidget *item;
556 item = gtk_separator_menu_item_new();
557 gtk_widget_show(item);
558 gtk_container_add(GTK_CONTAINER(menu), item);
560 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
561 gtk_widget_show(item);
562 gtk_container_add(GTK_CONTAINER(menu), item);
563 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
567 /* look back up from the current path and find the directory we came from */
568 static gboolean
569 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
571 GtkTreeIter iter;
572 *prefix = NULL;
574 while (gtk_tree_path_prev(cur))
576 if (gtk_tree_model_get_iter(model, &iter, cur))
578 gchar *string;
579 gtk_tree_model_get(model, &iter, 1, &string, -1);
580 if (string != NULL && build_parse_make_dir(string, prefix))
582 g_free(string);
583 return TRUE;
585 g_free(string);
589 return FALSE;
593 static gboolean goto_compiler_file_line(const gchar *filename, gint line, guint keyval)
595 if (!filename || line <= -1)
596 return FALSE;
598 /* If the path doesn't exist, try the current document.
599 * This happens when we receive build messages in the wrong order - after the
600 * 'Leaving directory' messages */
601 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
603 gchar *cur_dir = utils_get_current_file_dir_utf8();
604 gchar *name;
606 if (cur_dir)
608 /* we let the user know we couldn't find the parsed filename from the message window */
609 setptr(cur_dir, utils_get_locale_from_utf8(cur_dir));
610 name = g_path_get_basename(filename);
611 setptr(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
612 g_free(cur_dir);
614 if (g_file_test(name, G_FILE_TEST_EXISTS))
616 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
617 filename);
618 filename = name;
620 else
621 g_free(name);
626 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
627 GeanyDocument *doc = document_find_by_filename(utf8_filename);
628 GeanyDocument *old_doc = document_get_current();
630 g_free(utf8_filename);
632 if (doc == NULL) /* file not already open */
633 doc = document_open_file(filename, FALSE, NULL, NULL);
635 if (doc != NULL)
637 gboolean ret;
639 if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
640 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
642 ret = navqueue_goto_line(old_doc, doc, line);
643 if (ret && ui_is_keyval_enter_or_return(keyval))
644 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
646 return ret;
649 return FALSE;
653 gboolean msgwin_goto_compiler_file_line(guint keyval)
655 GtkTreeIter iter;
656 GtkTreeModel *model;
657 GtkTreeSelection *selection;
658 gchar *string;
659 GdkColor *color;
661 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
662 if (gtk_tree_selection_get_selected(selection, &model, &iter))
664 /* if the item is not coloured red, it's not an error line */
665 gtk_tree_model_get(model, &iter, 0, &color, -1);
666 if (color == NULL || ! gdk_color_equal(color, &color_error))
668 if (color != NULL)
669 gdk_color_free(color);
670 return FALSE;
672 gdk_color_free(color);
674 gtk_tree_model_get(model, &iter, 1, &string, -1);
675 if (string != NULL)
677 gint line;
678 gchar *filename, *dir;
679 GtkTreePath *path;
680 gboolean ret;
682 path = gtk_tree_model_get_path(model, &iter);
683 find_prev_build_dir(path, model, &dir);
684 gtk_tree_path_free(path);
685 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
686 g_free(string);
687 g_free(dir);
689 ret = goto_compiler_file_line(filename, line, keyval);
690 g_free(filename);
691 return ret;
694 return FALSE;
698 static void make_absolute(gchar **filename, const gchar *dir)
700 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
702 if (*filename == NULL)
703 return;
705 /* skip some characters at the beginning of the filename, at the moment only "./"
706 * can be extended if other "trash" is known */
707 if (strncmp(*filename, "./", 2) == 0)
708 skip_dot_slash = 2;
710 /* add directory */
711 if (! utils_is_absolute_path(*filename))
712 setptr(*filename, g_strconcat(dir, G_DIR_SEPARATOR_S,
713 *filename + skip_dot_slash, NULL));
717 /* try to parse the file and line number where the error occured described in line
718 * and when something useful is found, it stores the line number in *line and the
719 * relevant file with the error in *filename.
720 * *line will be -1 if no error was found in string.
721 * *filename must be freed unless it is NULL. */
722 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
724 gchar *end = NULL;
725 gchar **fields;
727 *filename = NULL;
728 *line = -1;
730 g_return_if_fail(data->string != NULL);
732 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
734 /* parse the line */
735 if (g_strv_length(fields) < data->min_fields)
737 g_strfreev(fields);
738 return;
741 *line = strtol(fields[data->line_idx], &end, 10);
743 /* if the line could not be read, line is 0 and an error occurred, so we leave */
744 if (fields[data->line_idx] == end)
746 g_strfreev(fields);
747 return;
750 /* let's stop here if there is no filename in the error message */
751 if (data->file_idx == -1)
753 /* we have no filename in the error message, so take the current one and hope it's correct */
754 GeanyDocument *doc = document_get_current();
755 if (doc != NULL)
756 *filename = g_strdup(doc->file_name);
757 g_strfreev(fields);
758 return;
761 *filename = g_strdup(fields[data->file_idx]);
762 g_strfreev(fields);
766 static void parse_compiler_error_line(const gchar *string,
767 gchar **filename, gint *line)
769 ParseData data = {NULL, NULL, 0, 0, 0};
771 data.string = string;
773 switch (build_info.file_type_id)
775 case GEANY_FILETYPES_PHP:
777 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
778 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
779 gchar *tmp = strstr(string, " in ");
781 if (tmp != NULL)
783 data.string = tmp;
784 data.pattern = " ";
785 data.min_fields = 6;
786 data.line_idx = 5;
787 data.file_idx = 2;
789 else
791 data.pattern = " ";
792 data.min_fields = 11;
793 data.line_idx = 10;
794 data.file_idx = 7;
796 break;
798 case GEANY_FILETYPES_PERL:
800 /* syntax error at test.pl line 7, near "{ */
801 data.pattern = " ";
802 data.min_fields = 6;
803 data.line_idx = 5;
804 data.file_idx = 3;
805 break;
807 /* the error output of python and tcl equals */
808 case GEANY_FILETYPES_TCL:
809 case GEANY_FILETYPES_PYTHON:
811 /* File "HyperArch.py", line 37, in ?
812 * (file "clrdial.tcl" line 12) */
813 data.pattern = " \"";
814 data.min_fields = 6;
815 data.line_idx = 5;
816 data.file_idx = 2;
817 break;
819 case GEANY_FILETYPES_BASIC:
820 case GEANY_FILETYPES_PASCAL:
821 case GEANY_FILETYPES_CS:
823 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
824 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
825 data.pattern = "(";
826 data.min_fields = 2;
827 data.line_idx = 1;
828 data.file_idx = 0;
829 break;
831 case GEANY_FILETYPES_D:
833 /* GNU D compiler front-end, gdc
834 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
835 * warning - gantry.d:20: statement is not reachable
836 * Digital Mars dmd compiler
837 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
838 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
839 if (strncmp(string, "warning - ", 10) == 0)
841 data.pattern = " (:";
842 data.min_fields = 4;
843 data.line_idx = 3;
844 data.file_idx = 2;
846 else
848 data.pattern = "(:";
849 data.min_fields = 2;
850 data.line_idx = 1;
851 data.file_idx = 0;
853 break;
855 case GEANY_FILETYPES_FERITE:
857 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
858 * Error: Compile Error: on line 24, in /test/class.fe */
859 if (strncmp(string, "Error: Compile Error", 20) == 0)
861 data.pattern = " ";
862 data.min_fields = 8;
863 data.line_idx = 5;
864 data.file_idx = 7;
866 else
868 data.pattern = " \"";
869 data.min_fields = 10;
870 data.line_idx = 5;
871 data.file_idx = 8;
873 break;
875 case GEANY_FILETYPES_HTML:
877 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
878 data.pattern = " ";
879 data.min_fields = 4;
880 data.line_idx = 1;
881 data.file_idx = -1;
882 break;
884 /* All GNU gcc-like error messages */
885 case GEANY_FILETYPES_C:
886 case GEANY_FILETYPES_CPP:
887 case GEANY_FILETYPES_RUBY:
888 case GEANY_FILETYPES_JAVA:
889 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
890 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
891 * empty.c:21: error: conflicting types for `foo'
892 * Only parse file and line, so that linker errors will also work (with -g) */
893 case GEANY_FILETYPES_F77:
894 case GEANY_FILETYPES_FORTRAN:
895 case GEANY_FILETYPES_LATEX:
896 /* ./kommtechnik_2b.tex:18: Emergency stop. */
897 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
898 case GEANY_FILETYPES_NONE:
899 default: /* The default is a GNU gcc type error */
901 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
902 strncmp(string, "[javac]", 7) == 0)
904 /* Java Apache Ant.
905 * [javac] <Full Path to File + extension>:<line n°>: <error> */
906 data.pattern = " :";
907 data.min_fields = 4;
908 data.line_idx = 2;
909 data.file_idx = 1;
910 break;
912 /* don't accidently find libtool versions x:y:x and think it is a file name */
913 if (strstr(string, "libtool --mode=link") == NULL)
915 data.pattern = ":";
916 data.min_fields = 3;
917 data.line_idx = 1;
918 data.file_idx = 0;
919 break;
924 if (data.pattern != NULL)
925 parse_file_line(&data, filename, line);
929 /* try to parse the file and line number where the error occured described in string
930 * and when something useful is found, it stores the line number in *line and the
931 * relevant file with the error in *filename.
932 * *line will be -1 if no error was found in string.
933 * *filename must be freed unless it is NULL. */
934 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
935 gchar **filename, gint *line)
937 GeanyFiletype *ft;
938 gchar *trimmed_string;
940 *filename = NULL;
941 *line = -1;
943 if (G_UNLIKELY(string == NULL))
944 return;
946 if (dir == NULL)
947 dir = build_info.dir;
948 g_return_if_fail(dir != NULL);
950 trimmed_string = g_strdup(string);
951 g_strchug(trimmed_string); /* remove possible leading whitespace */
953 ft = filetypes[build_info.file_type_id];
955 /* try parsing with a custom regex */
956 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
958 /* fallback to default old-style parsing */
959 parse_compiler_error_line(trimmed_string, filename, line);
961 make_absolute(filename, dir);
962 g_free(trimmed_string);
966 gboolean msgwin_goto_messages_file_line(guint keyval)
968 GtkTreeIter iter;
969 GtkTreeModel *model;
970 GtkTreeSelection *selection;
971 gboolean ret = FALSE;
973 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
974 if (gtk_tree_selection_get_selected(selection, &model, &iter))
976 gint line;
977 gchar *string;
978 GeanyDocument *doc;
979 GeanyDocument *old_doc = document_get_current();
981 gtk_tree_model_get(model, &iter, 0, &line, 1, &doc, 3, &string, -1);
982 /* doc may have been closed, so check doc->index: */
983 if (line >= 0 && DOC_VALID(doc))
985 ret = navqueue_goto_line(old_doc, doc, line);
986 if (ret && ui_is_keyval_enter_or_return(keyval))
987 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
989 else if (line < 0 && string != NULL)
991 gchar *filename;
992 msgwin_parse_grep_line(string, &filename, &line);
993 if (filename != NULL && line > -1)
995 /* use document_open_file to find an already open file, or open it in place */
996 doc = document_open_file(filename, FALSE, NULL, NULL);
997 if (doc != NULL)
999 ret = navqueue_goto_line(old_doc, doc, line);
1000 if (ret && ui_is_keyval_enter_or_return(keyval))
1001 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1004 g_free(filename);
1006 g_free(string);
1008 return ret;
1012 /* Try to parse the file and line number for string and when something useful is
1013 * found, store the line number in *line and the relevant file with the error in
1014 * *filename.
1015 * *line will be -1 if no error was found in string.
1016 * *filename must be freed unless NULL. */
1017 static void msgwin_parse_grep_line(const gchar *string, gchar **filename, gint *line)
1019 ParseData data;
1021 *filename = NULL;
1022 *line = -1;
1024 if (string == NULL || msgwindow.find_in_files_dir == NULL)
1025 return;
1027 /* conflict:3:conflicting types for `foo' */
1028 data.string = string;
1029 data.pattern = ":";
1030 data.min_fields = 3;
1031 data.line_idx = 1;
1032 data.file_idx = 0;
1034 parse_file_line(&data, filename, line);
1035 make_absolute(filename, msgwindow.find_in_files_dir);
1039 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1040 gpointer user_data)
1042 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1044 if (event->button == 1)
1046 switch (GPOINTER_TO_INT(user_data))
1048 case MSG_COMPILER:
1049 { /* single click in the compiler treeview */
1050 msgwin_goto_compiler_file_line(0);
1051 break;
1053 case MSG_MESSAGE:
1054 { /* single click in the message treeview (results of 'Find usage') */
1055 msgwin_goto_messages_file_line(0);
1056 break;
1061 if (event->button == 3)
1062 { /* popupmenu to hide or clear the active treeview */
1063 switch (GPOINTER_TO_INT(user_data))
1065 case MSG_STATUS:
1067 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1068 event->button, event->time);
1069 break;
1071 case MSG_MESSAGE:
1073 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1074 event->button, event->time);
1075 break;
1077 case MSG_COMPILER:
1079 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1080 event->button, event->time);
1081 break;
1085 return FALSE;
1090 * Switches to the given notebook tab of the messages window and shows the messages window
1091 * if it was previously hidden and @a show is set to @c TRUE.
1093 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1094 * #MessageWindowTabNum.
1095 * @param show Whether to show the messages window at all if it was hidden before.
1097 * @since 0.15
1099 void msgwin_switch_tab(gint tabnum, gboolean show)
1101 GtkWidget *widget = NULL; /* widget to focus */
1103 switch (tabnum)
1105 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1106 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1107 case MSG_STATUS: widget = msgwindow.tree_status; break;
1108 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1109 #ifdef HAVE_VTE
1110 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1111 #endif
1112 default: break;
1115 /* the msgwin must be visible before we switch to the VTE page so that
1116 * the font settings are applied on realization */
1117 if (show)
1118 msgwin_show_hide(TRUE);
1119 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1120 if (show && widget)
1121 gtk_widget_grab_focus(widget);
1126 * Removes all messages from a tab specified by @a tabnum in the messages window.
1128 * @param tabnum An index of a tab in the messages window which should be cleared.
1129 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1131 * @since 0.15
1133 void msgwin_clear_tab(gint tabnum)
1135 GtkListStore *store = NULL;
1137 switch (tabnum)
1139 case MSG_MESSAGE:
1140 store = msgwindow.store_msg;
1141 break;
1143 case MSG_COMPILER:
1144 gtk_list_store_clear(msgwindow.store_compiler);
1145 build_menu_update(NULL); /* update next error items */
1146 return;
1148 case MSG_STATUS: store = msgwindow.store_status; break;
1149 default: return;
1151 if (store == NULL)
1152 return;
1153 gtk_list_store_clear(store);