Cache G_TYPE_INSTANCE_GET_PRIVATE() result when initializing an
[geany-mirror.git] / src / msgwindow.c
blob3fbb0e7291d5731c39a032b6d5af244da62ad3e5
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[512];
256 va_list args;
258 va_start(args, format);
259 g_vsnprintf(string, 512, format, args);
260 va_end(args);
261 msgwin_compiler_add_string(msg_color, string);
265 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
267 GtkTreeIter iter;
268 GtkTreePath *path;
269 const GdkColor *color = get_color(msg_color);
271 gtk_list_store_append(msgwindow.store_compiler, &iter);
272 gtk_list_store_set(msgwindow.store_compiler, &iter, 0, color, 1, msg, -1);
274 if (ui_prefs.msgwindow_visible)
276 path = gtk_tree_model_get_path(
277 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
278 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
279 gtk_tree_path_free(path);
282 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
283 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
284 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
288 void msgwin_show_hide(gboolean show)
290 ui_prefs.msgwindow_visible = show;
291 ignore_callback = TRUE;
292 gtk_check_menu_item_set_active(
293 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
294 show);
295 ignore_callback = FALSE;
296 ui_widget_show_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"), show);
297 /* set the input focus back to the editor */
298 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
303 * Adds a new message in the messages tab treeview in the messages window.
304 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
305 * by @a doc into the line specified with @a line.
307 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
308 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
309 * @param doc The document. Set to @c NULL to ignore.
310 * @param format @c printf()-style format string.
311 * @param ... Arguments for the @c format string.
313 * @since 0.15
315 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
317 gchar string[512];
318 va_list args;
320 va_start(args, format);
321 g_vsnprintf(string, 512, format, args);
322 va_end(args);
324 msgwin_msg_add_string(msg_color, line, doc, string);
328 /* adds string to the msg treeview */
329 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
331 GtkTreeIter iter;
332 const GdkColor *color = get_color(msg_color);
333 gchar *tmp;
334 gsize len;
336 if (! ui_prefs.msgwindow_visible) msgwin_show_hide(TRUE);
338 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
339 * cut the string to a maximum of 1024 bytes and discard the rest */
340 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
341 len = strlen(string);
342 if (len > 1024)
343 tmp = g_strndup(string, 1024);
344 else
345 tmp = g_strdup(string);
347 gtk_list_store_append(msgwindow.store_msg, &iter);
348 gtk_list_store_set(msgwindow.store_msg, &iter, 0, line, 1, doc, 2, color, 3, tmp, -1);
350 g_free(tmp);
355 * Logs a status message *without* setting the status bar.
356 * (Use ui_set_statusbar() to display text on the statusbar)
358 * @param format @c printf()-style format string.
359 * @param ... Arguments for the @c format string.
361 void msgwin_status_add(const gchar *format, ...)
363 GtkTreeIter iter;
364 gchar string[512];
365 gchar *statusmsg, *time_str;
366 va_list args;
368 va_start(args, format);
369 g_vsnprintf(string, 512, format, args);
370 va_end(args);
372 /* add a timestamp to status messages */
373 time_str = utils_get_current_time_string();
374 statusmsg = g_strconcat(time_str, ": ", string, NULL);
375 g_free(time_str);
377 /* add message to Status window */
378 gtk_list_store_append(msgwindow.store_status, &iter);
379 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
380 g_free(statusmsg);
382 if (G_LIKELY(main_status.main_window_realized))
384 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
386 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
387 if (prefs.switch_to_status)
388 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
389 gtk_tree_path_free(path);
394 static void
395 on_message_treeview_clear_activate (GtkMenuItem *menuitem,
396 gpointer user_data)
398 gint tabnum = GPOINTER_TO_INT(user_data);
400 msgwin_clear_tab(tabnum);
404 static void
405 on_compiler_treeview_copy_activate (GtkMenuItem *menuitem,
406 gpointer user_data)
408 GtkWidget *tv = NULL;
409 GtkTreeSelection *selection;
410 GtkTreeModel *model;
411 GtkTreeIter iter;
412 gint str_idx = 1;
414 switch (GPOINTER_TO_INT(user_data))
416 case MSG_STATUS:
417 tv = msgwindow.tree_status;
418 str_idx = 0;
419 break;
421 case MSG_COMPILER:
422 tv = msgwindow.tree_compiler;
423 break;
425 case MSG_MESSAGE:
426 tv = msgwindow.tree_msg;
427 str_idx = 3;
428 break;
430 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
432 if (gtk_tree_selection_get_selected(selection, &model, &iter))
434 gchar *string;
436 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
437 if (NZV(string))
439 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
440 string, -1);
442 g_free(string);
447 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
449 GtkListStore *store = msgwindow.store_compiler;
450 GtkTreeIter iter;
451 GString *str = g_string_new("");
452 gint str_idx = 1;
453 gboolean valid;
455 switch (GPOINTER_TO_INT(user_data))
457 case MSG_STATUS:
458 store = msgwindow.store_status;
459 str_idx = 0;
460 break;
462 case MSG_COMPILER:
463 /* default values */
464 break;
466 case MSG_MESSAGE:
467 store = msgwindow.store_msg;
468 str_idx = 3;
469 break;
472 /* walk through the list and copy every line into a string */
473 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
474 while (valid)
476 gchar *line;
478 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
479 if (NZV(line))
481 g_string_append(str, line);
482 g_string_append_c(str, '\n');
484 g_free(line);
486 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
489 /* copy the string into the clipboard */
490 if (str->len > 0)
492 gtk_clipboard_set_text(
493 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
494 str->str,
495 str->len);
497 g_string_free(str, TRUE);
501 static void
502 on_hide_message_window (GtkMenuItem *menuitem,
503 gpointer user_data)
505 msgwin_show_hide(FALSE);
509 static GtkWidget *create_message_popup_menu(gint type)
511 GtkWidget *message_popup_menu, *clear, *copy, *image;
513 message_popup_menu = gtk_menu_new();
515 clear = gtk_image_menu_item_new_from_stock("gtk-clear", NULL);
516 gtk_widget_show(clear);
517 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
518 g_signal_connect(clear, "activate",
519 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
521 copy = gtk_image_menu_item_new_from_stock("gtk-copy", NULL);
522 gtk_widget_show(copy);
523 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
524 g_signal_connect(copy, "activate",
525 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
527 copy = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
528 gtk_widget_show(copy);
529 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
530 image = gtk_image_new_from_stock("gtk-copy", GTK_ICON_SIZE_MENU);
531 gtk_widget_show(image);
532 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
533 g_signal_connect(copy, "activate",
534 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
536 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
538 return message_popup_menu;
542 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
544 msgwin_menu_add_common_items(arg1);
548 /* Menu items that should be on all message window popup menus */
549 void msgwin_menu_add_common_items(GtkMenu *menu)
551 GtkWidget *item;
553 item = gtk_separator_menu_item_new();
554 gtk_widget_show(item);
555 gtk_container_add(GTK_CONTAINER(menu), item);
557 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
558 gtk_widget_show(item);
559 gtk_container_add(GTK_CONTAINER(menu), item);
560 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
564 /* look back up from the current path and find the directory we came from */
565 static gboolean
566 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
568 GtkTreeIter iter;
569 *prefix = NULL;
571 while (gtk_tree_path_prev(cur))
573 if (gtk_tree_model_get_iter(model, &iter, cur))
575 gchar *string;
576 gtk_tree_model_get(model, &iter, 1, &string, -1);
577 if (string != NULL && build_parse_make_dir(string, prefix))
579 g_free(string);
580 return TRUE;
582 g_free(string);
586 return FALSE;
590 static gboolean goto_compiler_file_line(const gchar *filename, gint line, guint keyval)
592 if (!filename || line <= -1)
593 return FALSE;
595 /* If the path doesn't exist, try the current document.
596 * This happens when we receive build messages in the wrong order - after the
597 * 'Leaving directory' messages */
598 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
600 gchar *cur_dir = utils_get_current_file_dir_utf8();
601 gchar *name;
603 if (cur_dir)
605 /* we let the user know we couldn't find the parsed filename from the message window */
606 setptr(cur_dir, utils_get_locale_from_utf8(cur_dir));
607 name = g_path_get_basename(filename);
608 setptr(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
609 g_free(cur_dir);
611 if (g_file_test(name, G_FILE_TEST_EXISTS))
613 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
614 filename);
615 filename = name;
617 else
618 g_free(name);
623 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
624 GeanyDocument *doc = document_find_by_filename(utf8_filename);
625 GeanyDocument *old_doc = document_get_current();
627 g_free(utf8_filename);
629 if (doc == NULL) /* file not already open */
630 doc = document_open_file(filename, FALSE, NULL, NULL);
632 if (doc != NULL)
634 gboolean ret;
636 if (! doc->changed) /* if modified, line may be wrong */
637 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
639 ret = navqueue_goto_line(old_doc, doc, line);
640 if (ret && ui_is_keyval_enter_or_return(keyval))
641 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
643 return ret;
646 return FALSE;
650 gboolean msgwin_goto_compiler_file_line(guint keyval)
652 GtkTreeIter iter;
653 GtkTreeModel *model;
654 GtkTreeSelection *selection;
655 gchar *string;
656 GdkColor *color;
658 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
659 if (gtk_tree_selection_get_selected(selection, &model, &iter))
661 /* if the item is not coloured red, it's not an error line */
662 gtk_tree_model_get(model, &iter, 0, &color, -1);
663 if (color == NULL || ! gdk_color_equal(color, &color_error))
665 if (color != NULL)
666 gdk_color_free(color);
667 return FALSE;
669 gdk_color_free(color);
671 gtk_tree_model_get(model, &iter, 1, &string, -1);
672 if (string != NULL)
674 gint line;
675 gchar *filename, *dir;
676 GtkTreePath *path;
677 gboolean ret;
679 path = gtk_tree_model_get_path(model, &iter);
680 find_prev_build_dir(path, model, &dir);
681 gtk_tree_path_free(path);
682 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
683 g_free(string);
684 g_free(dir);
686 ret = goto_compiler_file_line(filename, line, keyval);
687 g_free(filename);
688 return ret;
691 return FALSE;
695 static void make_absolute(gchar **filename, const gchar *dir)
697 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
699 if (*filename == NULL)
700 return;
702 /* skip some characters at the beginning of the filename, at the moment only "./"
703 * can be extended if other "trash" is known */
704 if (strncmp(*filename, "./", 2) == 0)
705 skip_dot_slash = 2;
707 /* add directory */
708 if (! utils_is_absolute_path(*filename))
709 setptr(*filename, g_strconcat(dir, G_DIR_SEPARATOR_S,
710 *filename + skip_dot_slash, NULL));
714 /* try to parse the file and line number where the error occured described in line
715 * and when something useful is found, it stores the line number in *line and the
716 * relevant file with the error in *filename.
717 * *line will be -1 if no error was found in string.
718 * *filename must be freed unless it is NULL. */
719 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
721 gchar *end = NULL;
722 gchar **fields;
724 *filename = NULL;
725 *line = -1;
727 g_return_if_fail(data->string != NULL);
729 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
731 /* parse the line */
732 if (g_strv_length(fields) < data->min_fields)
734 g_strfreev(fields);
735 return;
738 *line = strtol(fields[data->line_idx], &end, 10);
740 /* if the line could not be read, line is 0 and an error occurred, so we leave */
741 if (fields[data->line_idx] == end)
743 g_strfreev(fields);
744 return;
747 /* let's stop here if there is no filename in the error message */
748 if (data->file_idx == -1)
750 /* we have no filename in the error message, so take the current one and hope it's correct */
751 GeanyDocument *doc = document_get_current();
752 if (doc != NULL)
753 *filename = g_strdup(doc->file_name);
754 g_strfreev(fields);
755 return;
758 *filename = g_strdup(fields[data->file_idx]);
759 g_strfreev(fields);
763 static void parse_compiler_error_line(const gchar *string,
764 gchar **filename, gint *line)
766 ParseData data = {NULL, NULL, 0, 0, 0};
768 data.string = string;
770 switch (build_info.file_type_id)
772 case GEANY_FILETYPES_PHP:
774 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
775 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
776 gchar *tmp = strstr(string, " in ");
778 if (tmp != NULL)
780 data.string = tmp;
781 data.pattern = " ";
782 data.min_fields = 6;
783 data.line_idx = 5;
784 data.file_idx = 2;
786 else
788 data.pattern = " ";
789 data.min_fields = 11;
790 data.line_idx = 10;
791 data.file_idx = 7;
793 break;
795 case GEANY_FILETYPES_PERL:
797 /* syntax error at test.pl line 7, near "{ */
798 data.pattern = " ";
799 data.min_fields = 6;
800 data.line_idx = 5;
801 data.file_idx = 3;
802 break;
804 /* the error output of python and tcl equals */
805 case GEANY_FILETYPES_TCL:
806 case GEANY_FILETYPES_PYTHON:
808 /* File "HyperArch.py", line 37, in ?
809 * (file "clrdial.tcl" line 12) */
810 data.pattern = " \"";
811 data.min_fields = 6;
812 data.line_idx = 5;
813 data.file_idx = 2;
814 break;
816 case GEANY_FILETYPES_BASIC:
817 case GEANY_FILETYPES_PASCAL:
818 case GEANY_FILETYPES_CS:
820 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
821 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
822 data.pattern = "(";
823 data.min_fields = 2;
824 data.line_idx = 1;
825 data.file_idx = 0;
826 break;
828 case GEANY_FILETYPES_D:
830 /* GNU D compiler front-end, gdc
831 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
832 * warning - gantry.d:20: statement is not reachable
833 * Digital Mars dmd compiler
834 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
835 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
836 if (strncmp(string, "warning - ", 10) == 0)
838 data.pattern = " (:";
839 data.min_fields = 4;
840 data.line_idx = 3;
841 data.file_idx = 2;
843 else
845 data.pattern = "(:";
846 data.min_fields = 2;
847 data.line_idx = 1;
848 data.file_idx = 0;
850 break;
852 case GEANY_FILETYPES_FERITE:
854 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
855 * Error: Compile Error: on line 24, in /test/class.fe */
856 if (strncmp(string, "Error: Compile Error", 20) == 0)
858 data.pattern = " ";
859 data.min_fields = 8;
860 data.line_idx = 5;
861 data.file_idx = 7;
863 else
865 data.pattern = " \"";
866 data.min_fields = 10;
867 data.line_idx = 5;
868 data.file_idx = 8;
870 break;
872 case GEANY_FILETYPES_HTML:
874 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
875 data.pattern = " ";
876 data.min_fields = 4;
877 data.line_idx = 1;
878 data.file_idx = -1;
879 break;
881 /* All GNU gcc-like error messages */
882 case GEANY_FILETYPES_C:
883 case GEANY_FILETYPES_CPP:
884 case GEANY_FILETYPES_RUBY:
885 case GEANY_FILETYPES_JAVA:
886 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
887 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
888 * empty.c:21: error: conflicting types for `foo'
889 * Only parse file and line, so that linker errors will also work (with -g) */
890 case GEANY_FILETYPES_F77:
891 case GEANY_FILETYPES_FORTRAN:
892 case GEANY_FILETYPES_LATEX:
893 /* ./kommtechnik_2b.tex:18: Emergency stop. */
894 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
895 case GEANY_FILETYPES_NONE:
896 default: /* The default is a GNU gcc type error */
898 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
899 strncmp(string, "[javac]", 7) == 0)
901 /* Java Apache Ant.
902 * [javac] <Full Path to File + extension>:<line n°>: <error> */
903 data.pattern = " :";
904 data.min_fields = 4;
905 data.line_idx = 2;
906 data.file_idx = 1;
907 break;
909 /* don't accidently find libtool versions x:y:x and think it is a file name */
910 if (strstr(string, "libtool --mode=link") == NULL)
912 data.pattern = ":";
913 data.min_fields = 3;
914 data.line_idx = 1;
915 data.file_idx = 0;
916 break;
921 if (data.pattern != NULL)
922 parse_file_line(&data, filename, line);
926 /* try to parse the file and line number where the error occured described in string
927 * and when something useful is found, it stores the line number in *line and the
928 * relevant file with the error in *filename.
929 * *line will be -1 if no error was found in string.
930 * *filename must be freed unless it is NULL. */
931 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
932 gchar **filename, gint *line)
934 GeanyFiletype *ft;
935 gchar *trimmed_string;
937 *filename = NULL;
938 *line = -1;
940 if (G_UNLIKELY(string == NULL))
941 return;
943 if (dir == NULL)
944 dir = build_info.dir;
945 g_return_if_fail(dir != NULL);
947 trimmed_string = g_strdup(string);
948 g_strchug(trimmed_string); /* remove possible leading whitespace */
950 ft = filetypes[build_info.file_type_id];
952 /* try parsing with a custom regex */
953 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
955 /* fallback to default old-style parsing */
956 parse_compiler_error_line(trimmed_string, filename, line);
958 make_absolute(filename, dir);
959 g_free(trimmed_string);
963 gboolean msgwin_goto_messages_file_line(guint keyval)
965 GtkTreeIter iter;
966 GtkTreeModel *model;
967 GtkTreeSelection *selection;
968 gboolean ret = FALSE;
970 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
971 if (gtk_tree_selection_get_selected(selection, &model, &iter))
973 gint line;
974 gchar *string;
975 GeanyDocument *doc;
976 GeanyDocument *old_doc = document_get_current();
978 gtk_tree_model_get(model, &iter, 0, &line, 1, &doc, 3, &string, -1);
979 /* doc may have been closed, so check doc->index: */
980 if (line >= 0 && DOC_VALID(doc))
982 ret = navqueue_goto_line(old_doc, doc, line);
983 if (ret && ui_is_keyval_enter_or_return(keyval))
984 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
986 else if (line < 0 && string != NULL)
988 gchar *filename;
989 msgwin_parse_grep_line(string, &filename, &line);
990 if (filename != NULL && line > -1)
992 /* use document_open_file to find an already open file, or open it in place */
993 doc = document_open_file(filename, FALSE, NULL, NULL);
994 if (doc != NULL)
996 ret = navqueue_goto_line(old_doc, doc, line);
997 if (ret && ui_is_keyval_enter_or_return(keyval))
998 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1001 g_free(filename);
1003 g_free(string);
1005 return ret;
1009 /* Try to parse the file and line number for string and when something useful is
1010 * found, store the line number in *line and the relevant file with the error in
1011 * *filename.
1012 * *line will be -1 if no error was found in string.
1013 * *filename must be freed unless NULL. */
1014 static void msgwin_parse_grep_line(const gchar *string, gchar **filename, gint *line)
1016 ParseData data;
1018 *filename = NULL;
1019 *line = -1;
1021 if (string == NULL || msgwindow.find_in_files_dir == NULL)
1022 return;
1024 /* conflict:3:conflicting types for `foo' */
1025 data.string = string;
1026 data.pattern = ":";
1027 data.min_fields = 3;
1028 data.line_idx = 1;
1029 data.file_idx = 0;
1031 parse_file_line(&data, filename, line);
1032 make_absolute(filename, msgwindow.find_in_files_dir);
1036 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1037 gpointer user_data)
1039 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1041 if (event->button == 1)
1043 switch (GPOINTER_TO_INT(user_data))
1045 case MSG_COMPILER:
1046 { /* single click in the compiler treeview */
1047 msgwin_goto_compiler_file_line(0);
1048 break;
1050 case MSG_MESSAGE:
1051 { /* single click in the message treeview (results of 'Find usage') */
1052 msgwin_goto_messages_file_line(0);
1053 break;
1058 if (event->button == 3)
1059 { /* popupmenu to hide or clear the active treeview */
1060 switch (GPOINTER_TO_INT(user_data))
1062 case MSG_STATUS:
1064 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1065 event->button, event->time);
1066 break;
1068 case MSG_MESSAGE:
1070 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1071 event->button, event->time);
1072 break;
1074 case MSG_COMPILER:
1076 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1077 event->button, event->time);
1078 break;
1082 return FALSE;
1087 * Switches to the given notebook tab of the messages window and shows the messages window
1088 * if it was previously hidden and @a show is set to @c TRUE.
1090 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1091 * #MessageWindowTabNum.
1092 * @param show Whether to show the messages window at all if it was hidden before.
1094 * @since 0.15
1096 void msgwin_switch_tab(gint tabnum, gboolean show)
1098 GtkWidget *widget = NULL; /* widget to focus */
1100 switch (tabnum)
1102 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1103 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1104 case MSG_STATUS: widget = msgwindow.tree_status; break;
1105 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1106 #ifdef HAVE_VTE
1107 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1108 #endif
1109 default: break;
1112 /* the msgwin must be visible before we switch to the VTE page so that
1113 * the font settings are applied on realization */
1114 if (show)
1115 msgwin_show_hide(TRUE);
1116 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1117 if (show && widget)
1118 gtk_widget_grab_focus(widget);
1123 * Removes all messages from a tab specified by @a tabnum in the messages window.
1125 * @param tabnum An index of a tab in the messages window which should be cleared.
1126 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1128 * @since 0.15
1130 void msgwin_clear_tab(gint tabnum)
1132 GtkListStore *store = NULL;
1134 switch (tabnum)
1136 case MSG_MESSAGE:
1137 store = msgwindow.store_msg;
1138 break;
1140 case MSG_COMPILER:
1141 gtk_list_store_clear(msgwindow.store_compiler);
1142 build_menu_update(NULL); /* update next error items */
1143 return;
1145 case MSG_STATUS: store = msgwindow.store_status; break;
1146 default: return;
1148 if (store == NULL)
1149 return;
1150 gtk_list_store_clear(store);