Merge pull request #1133 from techee/readme_rst
[geany-mirror.git] / src / msgwindow.c
blob3a5e2c4000aa5f3ac499e067fcdaabfc07e134cd
1 /*
2 * msgwindow.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /**
23 * @file msgwindow.h
24 * Message window functions (status, compiler, messages windows).
25 * Also compiler error message parsing and grep file and line parsing.
27 * @see GeanyMainWidgets::message_window_notebook to append a new notebook page.
28 **/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "msgwindow.h"
36 #include "build.h"
37 #include "document.h"
38 #include "callbacks.h"
39 #include "filetypes.h"
40 #include "keybindings.h"
41 #include "main.h"
42 #include "navqueue.h"
43 #include "prefs.h"
44 #include "support.h"
45 #include "ui_utils.h"
46 #include "utils.h"
47 #include "vte.h"
49 #include <string.h>
50 #include <stdlib.h>
51 #include <time.h>
53 #include <gdk/gdkkeysyms.h>
56 /* used for parse_file_line */
57 typedef struct
59 const gchar *string; /* line data */
60 const gchar *pattern; /* pattern to split the error message into some fields */
61 guint min_fields; /* used to detect errors after parsing */
62 guint line_idx; /* idx of the field where the line is */
63 gint file_idx; /* idx of the field where the filename is or -1 */
65 ParseData;
67 MessageWindow msgwindow;
69 enum
71 MSG_COL_LINE = 0,
72 MSG_COL_DOC_ID,
73 MSG_COL_COLOR,
74 MSG_COL_STRING,
75 MSG_COL_COUNT
78 enum
80 COMPILER_COL_COLOR = 0,
81 COMPILER_COL_STRING,
82 COMPILER_COL_COUNT
86 static void prepare_msg_tree_view(void);
87 static void prepare_status_tree_view(void);
88 static void prepare_compiler_tree_view(void);
89 static GtkWidget *create_message_popup_menu(gint type);
90 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
91 gpointer user_data);
92 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data);
95 void msgwin_show_hide_tabs(void)
97 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_status), interface_prefs.msgwin_status_visible);
98 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_compiler), interface_prefs.msgwin_compiler_visible);
99 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.tree_msg), interface_prefs.msgwin_messages_visible);
100 ui_widget_show_hide(gtk_widget_get_parent(msgwindow.scribble), interface_prefs.msgwin_scribble_visible);
104 /** Sets the Messages path for opening any parsed filenames without absolute path
105 * from message lines.
106 * @param messages_dir The directory. **/
107 GEANY_API_SYMBOL
108 void msgwin_set_messages_dir(const gchar *messages_dir)
110 g_free(msgwindow.messages_dir);
111 msgwindow.messages_dir = g_strdup(messages_dir);
115 void msgwin_init(void)
117 msgwindow.notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
118 msgwindow.tree_status = ui_lookup_widget(main_widgets.window, "treeview3");
119 msgwindow.tree_msg = ui_lookup_widget(main_widgets.window, "treeview4");
120 msgwindow.tree_compiler = ui_lookup_widget(main_widgets.window, "treeview5");
121 msgwindow.scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
122 msgwindow.messages_dir = NULL;
124 prepare_status_tree_view();
125 prepare_msg_tree_view();
126 prepare_compiler_tree_view();
127 msgwindow.popup_status_menu = create_message_popup_menu(MSG_STATUS);
128 msgwindow.popup_msg_menu = create_message_popup_menu(MSG_MESSAGE);
129 msgwindow.popup_compiler_menu = create_message_popup_menu(MSG_COMPILER);
131 ui_widget_modify_font_from_string(msgwindow.scribble, interface_prefs.msgwin_font);
132 g_signal_connect(msgwindow.scribble, "populate-popup", G_CALLBACK(on_scribble_populate), NULL);
136 void msgwin_finalize(void)
138 g_free(msgwindow.messages_dir);
142 static gboolean on_msgwin_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer data)
144 gboolean enter_or_return = ui_is_keyval_enter_or_return(event->keyval);
146 if (enter_or_return || event->keyval == GDK_space)
148 switch (GPOINTER_TO_INT(data))
150 case MSG_COMPILER:
151 { /* key press in the compiler treeview */
152 msgwin_goto_compiler_file_line(enter_or_return);
153 break;
155 case MSG_MESSAGE:
156 { /* key press in the message treeview (results of 'Find usage') */
157 msgwin_goto_messages_file_line(enter_or_return);
158 break;
162 return FALSE;
166 /* does some preparing things to the status message list widget */
167 static void prepare_status_tree_view(void)
169 GtkCellRenderer *renderer;
170 GtkTreeViewColumn *column;
172 msgwindow.store_status = gtk_list_store_new(1, G_TYPE_STRING);
173 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_status), GTK_TREE_MODEL(msgwindow.store_status));
174 g_object_unref(msgwindow.store_status);
176 renderer = gtk_cell_renderer_text_new();
177 column = gtk_tree_view_column_new_with_attributes(_("Status messages"), renderer, "text", 0, NULL);
178 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_status), column);
180 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_status), FALSE);
182 ui_widget_modify_font_from_string(msgwindow.tree_status, interface_prefs.msgwin_font);
184 g_signal_connect(msgwindow.tree_status, "button-press-event",
185 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_STATUS));
189 /* does some preparing things to the message list widget
190 * (currently used for showing results of 'Find usage') */
191 static void prepare_msg_tree_view(void)
193 GtkCellRenderer *renderer;
194 GtkTreeViewColumn *column;
195 GtkTreeSelection *selection;
197 /* line, doc id, fg, str */
198 msgwindow.store_msg = gtk_list_store_new(MSG_COL_COUNT, G_TYPE_INT, G_TYPE_UINT,
199 GDK_TYPE_COLOR, G_TYPE_STRING);
200 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_msg), GTK_TREE_MODEL(msgwindow.store_msg));
201 g_object_unref(msgwindow.store_msg);
203 renderer = gtk_cell_renderer_text_new();
204 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
205 "foreground-gdk", MSG_COL_COLOR, "text", MSG_COL_STRING, NULL);
206 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_msg), column);
208 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_msg), FALSE);
210 ui_widget_modify_font_from_string(msgwindow.tree_msg, interface_prefs.msgwin_font);
212 /* use button-release-event so the selection has changed
213 * (connect_after button-press-event doesn't work) */
214 g_signal_connect(msgwindow.tree_msg, "button-release-event",
215 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
216 /* for double-clicking only, after the first release */
217 g_signal_connect(msgwindow.tree_msg, "button-press-event",
218 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_MESSAGE));
219 g_signal_connect(msgwindow.tree_msg, "key-press-event",
220 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_MESSAGE));
222 /* selection handling */
223 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
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 /* does some preparing things to the compiler list widget */
230 static void prepare_compiler_tree_view(void)
232 GtkCellRenderer *renderer;
233 GtkTreeViewColumn *column;
234 GtkTreeSelection *selection;
236 msgwindow.store_compiler = gtk_list_store_new(COMPILER_COL_COUNT, GDK_TYPE_COLOR, G_TYPE_STRING);
237 gtk_tree_view_set_model(GTK_TREE_VIEW(msgwindow.tree_compiler), GTK_TREE_MODEL(msgwindow.store_compiler));
238 g_object_unref(msgwindow.store_compiler);
240 renderer = gtk_cell_renderer_text_new();
241 column = gtk_tree_view_column_new_with_attributes(NULL, renderer,
242 "foreground-gdk", COMPILER_COL_COLOR, "text", COMPILER_COL_STRING, NULL);
243 gtk_tree_view_append_column(GTK_TREE_VIEW(msgwindow.tree_compiler), column);
245 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(msgwindow.tree_compiler), FALSE);
247 ui_widget_modify_font_from_string(msgwindow.tree_compiler, interface_prefs.msgwin_font);
249 /* use button-release-event so the selection has changed
250 * (connect_after button-press-event doesn't work) */
251 g_signal_connect(msgwindow.tree_compiler, "button-release-event",
252 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
253 /* for double-clicking only, after the first release */
254 g_signal_connect(msgwindow.tree_compiler, "button-press-event",
255 G_CALLBACK(on_msgwin_button_press_event), GINT_TO_POINTER(MSG_COMPILER));
256 g_signal_connect(msgwindow.tree_compiler, "key-press-event",
257 G_CALLBACK(on_msgwin_key_press_event), GINT_TO_POINTER(MSG_COMPILER));
259 /* selection handling */
260 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
261 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
262 /*g_signal_connect(selection, "changed", G_CALLBACK(on_msg_tree_selection_changed), NULL);*/
266 static const GdkColor color_error = {0, 65535, 0, 0};
268 static const GdkColor *get_color(gint msg_color)
270 static const GdkColor dark_red = {0, 65535 / 2, 0, 0};
271 static const GdkColor blue = {0, 0, 0, 0xD000}; /* not too bright ;-) */
273 switch (msg_color)
275 case COLOR_RED: return &color_error;
276 case COLOR_DARK_RED: return &dark_red;
277 case COLOR_BLUE: return &blue;
278 default: return NULL;
284 * Adds a new message in the compiler tab treeview in the messages window.
286 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
287 * @param format @c printf()-style format string.
288 * @param ... Arguments for the @c format string.
290 GEANY_API_SYMBOL
291 void msgwin_compiler_add(gint msg_color, const gchar *format, ...)
293 gchar *string;
294 va_list args;
296 va_start(args, format);
297 string = g_strdup_vprintf(format, args);
298 va_end(args);
299 msgwin_compiler_add_string(msg_color, string);
300 g_free(string);
304 void msgwin_compiler_add_string(gint msg_color, const gchar *msg)
306 GtkTreeIter iter;
307 const GdkColor *color = get_color(msg_color);
308 gchar *utf8_msg;
310 if (! g_utf8_validate(msg, -1, NULL))
311 utf8_msg = utils_get_utf8_from_locale(msg);
312 else
313 utf8_msg = (gchar *) msg;
315 gtk_list_store_append(msgwindow.store_compiler, &iter);
316 gtk_list_store_set(msgwindow.store_compiler, &iter,
317 COMPILER_COL_COLOR, color, COMPILER_COL_STRING, utf8_msg, -1);
319 if (ui_prefs.msgwindow_visible && interface_prefs.compiler_tab_autoscroll)
321 GtkTreePath *path = gtk_tree_model_get_path(
322 gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_compiler)), &iter);
324 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_compiler), path, NULL, TRUE, 0.5, 0.5);
325 gtk_tree_path_free(path);
328 /* calling build_menu_update for every build message would be overkill, TODO really should call it once when all done */
329 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_NEXT_ERROR], TRUE);
330 gtk_widget_set_sensitive(build_get_menu_items(-1)->menu_item[GBG_FIXED][GBF_PREV_ERROR], TRUE);
332 if (utf8_msg != msg)
333 g_free(utf8_msg);
337 void msgwin_show_hide(gboolean show)
339 ui_prefs.msgwindow_visible = show;
340 ignore_callback = TRUE;
341 gtk_check_menu_item_set_active(
342 GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")),
343 show);
344 ignore_callback = FALSE;
345 ui_widget_show_hide(main_widgets.message_window_notebook, show);
346 /* set the input focus back to the editor */
347 keybindings_send_command(GEANY_KEY_GROUP_FOCUS, GEANY_KEYS_FOCUS_EDITOR);
352 * Adds a new message in the messages tab treeview in the messages window.
353 * If @a line and @a doc are set, clicking on this line jumps into the file which is specified
354 * by @a doc into the line specified with @a line.
356 * @param msg_color A color to be used for the text. It must be an element of #MsgColors.
357 * @param line The document's line where the message belongs to. Set to @c -1 to ignore.
358 * @param doc The document. Set to @c NULL to ignore.
359 * @param format @c printf()-style format string.
360 * @param ... Arguments for the @c format string.
362 * @since 0.15
364 GEANY_API_SYMBOL
365 void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...)
367 gchar *string;
368 va_list args;
370 va_start(args, format);
371 string = g_strdup_vprintf(format, args);
372 va_end(args);
374 msgwin_msg_add_string(msg_color, line, doc, string);
375 g_free(string);
379 /* adds string to the msg treeview */
380 void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const gchar *string)
382 GtkTreeIter iter;
383 const GdkColor *color = get_color(msg_color);
384 gchar *tmp;
385 gsize len;
386 gchar *utf8_msg;
388 if (! ui_prefs.msgwindow_visible)
389 msgwin_show_hide(TRUE);
391 /* work around a strange problem when adding very long lines(greater than 4000 bytes)
392 * cut the string to a maximum of 1024 bytes and discard the rest */
393 /* TODO: find the real cause for the display problem / if it is GtkTreeView file a bug report */
394 len = strlen(string);
395 if (len > 1024)
396 tmp = g_strndup(string, 1024);
397 else
398 tmp = g_strdup(string);
400 if (! g_utf8_validate(tmp, -1, NULL))
401 utf8_msg = utils_get_utf8_from_locale(tmp);
402 else
403 utf8_msg = tmp;
405 gtk_list_store_append(msgwindow.store_msg, &iter);
406 gtk_list_store_set(msgwindow.store_msg, &iter,
407 MSG_COL_LINE, line, MSG_COL_DOC_ID, doc ? doc->id : 0, MSG_COL_COLOR,
408 color, MSG_COL_STRING, utf8_msg, -1);
410 g_free(tmp);
411 if (utf8_msg != tmp)
412 g_free(utf8_msg);
417 * Logs a status message *without* setting the status bar.
418 * (Use ui_set_statusbar() to display text on the statusbar)
420 * @param format @c printf()-style format string.
421 * @param ... Arguments for the @c format string.
423 GEANY_API_SYMBOL
424 void msgwin_status_add(const gchar *format, ...)
426 GtkTreeIter iter;
427 gchar *string;
428 gchar *statusmsg, *time_str;
429 va_list args;
431 va_start(args, format);
432 string = g_strdup_vprintf(format, args);
433 va_end(args);
435 /* add a timestamp to status messages */
436 time_str = utils_get_current_time_string();
437 statusmsg = g_strconcat(time_str, ": ", string, NULL);
438 g_free(time_str);
439 g_free(string);
441 /* add message to Status window */
442 gtk_list_store_append(msgwindow.store_status, &iter);
443 gtk_list_store_set(msgwindow.store_status, &iter, 0, statusmsg, -1);
444 g_free(statusmsg);
446 if (G_LIKELY(main_status.main_window_realized))
448 GtkTreePath *path = gtk_tree_model_get_path(gtk_tree_view_get_model(GTK_TREE_VIEW(msgwindow.tree_status)), &iter);
450 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(msgwindow.tree_status), path, NULL, FALSE, 0.0, 0.0);
451 if (prefs.switch_to_status)
452 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), MSG_STATUS);
453 gtk_tree_path_free(path);
458 static void
459 on_message_treeview_clear_activate(GtkMenuItem *menuitem, gpointer user_data)
461 gint tabnum = GPOINTER_TO_INT(user_data);
463 msgwin_clear_tab(tabnum);
467 static void
468 on_compiler_treeview_copy_activate(GtkMenuItem *menuitem, gpointer user_data)
470 GtkWidget *tv = NULL;
471 GtkTreeSelection *selection;
472 GtkTreeModel *model;
473 GtkTreeIter iter;
474 gint str_idx = COMPILER_COL_STRING;
476 switch (GPOINTER_TO_INT(user_data))
478 case MSG_STATUS:
479 tv = msgwindow.tree_status;
480 str_idx = 0;
481 break;
483 case MSG_COMPILER:
484 tv = msgwindow.tree_compiler;
485 break;
487 case MSG_MESSAGE:
488 tv = msgwindow.tree_msg;
489 str_idx = MSG_COL_STRING;
490 break;
492 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv));
494 if (gtk_tree_selection_get_selected(selection, &model, &iter))
496 gchar *string;
498 gtk_tree_model_get(model, &iter, str_idx, &string, -1);
499 if (!EMPTY(string))
501 gtk_clipboard_set_text(gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
502 string, -1);
504 g_free(string);
509 static void on_compiler_treeview_copy_all_activate(GtkMenuItem *menuitem, gpointer user_data)
511 GtkListStore *store = msgwindow.store_compiler;
512 GtkTreeIter iter;
513 GString *str = g_string_new("");
514 gint str_idx = COMPILER_COL_STRING;
515 gboolean valid;
517 switch (GPOINTER_TO_INT(user_data))
519 case MSG_STATUS:
520 store = msgwindow.store_status;
521 str_idx = 0;
522 break;
524 case MSG_COMPILER:
525 /* default values */
526 break;
528 case MSG_MESSAGE:
529 store = msgwindow.store_msg;
530 str_idx = MSG_COL_STRING;
531 break;
534 /* walk through the list and copy every line into a string */
535 valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter);
536 while (valid)
538 gchar *line;
540 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, str_idx, &line, -1);
541 if (!EMPTY(line))
543 g_string_append(str, line);
544 g_string_append_c(str, '\n');
546 g_free(line);
548 valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
551 /* copy the string into the clipboard */
552 if (str->len > 0)
554 gtk_clipboard_set_text(
555 gtk_clipboard_get(gdk_atom_intern("CLIPBOARD", FALSE)),
556 str->str,
557 str->len);
559 g_string_free(str, TRUE);
563 static void
564 on_hide_message_window(GtkMenuItem *menuitem, gpointer user_data)
566 msgwin_show_hide(FALSE);
570 static GtkWidget *create_message_popup_menu(gint type)
572 GtkWidget *message_popup_menu, *clear, *copy, *copy_all, *image;
574 message_popup_menu = gtk_menu_new();
576 clear = gtk_image_menu_item_new_from_stock(GTK_STOCK_CLEAR, NULL);
577 gtk_widget_show(clear);
578 gtk_container_add(GTK_CONTAINER(message_popup_menu), clear);
579 g_signal_connect(clear, "activate",
580 G_CALLBACK(on_message_treeview_clear_activate), GINT_TO_POINTER(type));
582 copy = gtk_image_menu_item_new_with_mnemonic(_("C_opy"));
583 gtk_widget_show(copy);
584 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy);
585 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
586 gtk_widget_show(image);
587 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy), image);
588 g_signal_connect(copy, "activate",
589 G_CALLBACK(on_compiler_treeview_copy_activate), GINT_TO_POINTER(type));
591 copy_all = gtk_image_menu_item_new_with_mnemonic(_("Copy _All"));
592 gtk_widget_show(copy_all);
593 gtk_container_add(GTK_CONTAINER(message_popup_menu), copy_all);
594 image = gtk_image_new_from_stock(GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
595 gtk_widget_show(image);
596 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(copy_all), image);
597 g_signal_connect(copy_all, "activate",
598 G_CALLBACK(on_compiler_treeview_copy_all_activate), GINT_TO_POINTER(type));
600 msgwin_menu_add_common_items(GTK_MENU(message_popup_menu));
602 return message_popup_menu;
606 static void on_scribble_populate(GtkTextView *textview, GtkMenu *arg1, gpointer user_data)
608 msgwin_menu_add_common_items(arg1);
612 /* Menu items that should be on all message window popup menus */
613 void msgwin_menu_add_common_items(GtkMenu *menu)
615 GtkWidget *item;
617 item = gtk_separator_menu_item_new();
618 gtk_widget_show(item);
619 gtk_container_add(GTK_CONTAINER(menu), item);
621 item = gtk_menu_item_new_with_mnemonic(_("_Hide Message Window"));
622 gtk_widget_show(item);
623 gtk_container_add(GTK_CONTAINER(menu), item);
624 g_signal_connect(item, "activate", G_CALLBACK(on_hide_message_window), NULL);
628 /* look back up from the current path and find the directory we came from */
629 static gboolean
630 find_prev_build_dir(GtkTreePath *cur, GtkTreeModel *model, gchar **prefix)
632 GtkTreeIter iter;
633 *prefix = NULL;
635 while (gtk_tree_path_prev(cur))
637 if (gtk_tree_model_get_iter(model, &iter, cur))
639 gchar *string;
640 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
641 if (string != NULL && build_parse_make_dir(string, prefix))
643 g_free(string);
644 return TRUE;
646 g_free(string);
650 return FALSE;
654 static gboolean goto_compiler_file_line(const gchar *fname, gint line, gboolean focus_editor)
656 gboolean ret = FALSE;
657 gchar *filename;
659 if (!fname || line <= -1)
660 return FALSE;
662 filename = utils_get_locale_from_utf8(fname);
664 /* If the path doesn't exist, try the current document.
665 * This happens when we receive build messages in the wrong order - after the
666 * 'Leaving directory' messages */
667 if (!g_file_test(filename, G_FILE_TEST_EXISTS))
669 gchar *cur_dir = utils_get_current_file_dir_utf8();
670 gchar *name;
672 if (cur_dir)
674 /* we let the user know we couldn't find the parsed filename from the message window */
675 SETPTR(cur_dir, utils_get_locale_from_utf8(cur_dir));
676 name = g_path_get_basename(filename);
677 SETPTR(name, g_build_path(G_DIR_SEPARATOR_S, cur_dir, name, NULL));
678 g_free(cur_dir);
680 if (g_file_test(name, G_FILE_TEST_EXISTS))
682 ui_set_statusbar(FALSE, _("Could not find file '%s' - trying the current document path."),
683 fname);
684 SETPTR(filename, name);
686 else
687 g_free(name);
692 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
693 GeanyDocument *doc = document_find_by_filename(utf8_filename);
694 GeanyDocument *old_doc = document_get_current();
696 g_free(utf8_filename);
698 if (doc == NULL) /* file not already open */
699 doc = document_open_file(filename, FALSE, NULL, NULL);
701 if (doc != NULL)
703 if (! doc->changed && editor_prefs.use_indicators) /* if modified, line may be wrong */
704 editor_indicator_set_on_line(doc->editor, GEANY_INDICATOR_ERROR, line - 1);
706 ret = navqueue_goto_line(old_doc, doc, line);
707 if (ret && focus_editor)
708 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
710 ret = TRUE;
714 g_free(filename);
716 return ret;
720 gboolean msgwin_goto_compiler_file_line(gboolean focus_editor)
722 GtkTreeIter iter;
723 GtkTreeModel *model;
724 GtkTreeSelection *selection;
725 gchar *string;
726 GdkColor *color;
728 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_compiler));
729 if (gtk_tree_selection_get_selected(selection, &model, &iter))
731 /* if the item is not coloured red, it's not an error line */
732 gtk_tree_model_get(model, &iter, COMPILER_COL_COLOR, &color, -1);
733 if (color == NULL || ! gdk_color_equal(color, &color_error))
735 if (color != NULL)
736 gdk_color_free(color);
737 return FALSE;
739 gdk_color_free(color);
741 gtk_tree_model_get(model, &iter, COMPILER_COL_STRING, &string, -1);
742 if (string != NULL)
744 gint line;
745 gchar *filename, *dir;
746 GtkTreePath *path;
747 gboolean ret;
749 path = gtk_tree_model_get_path(model, &iter);
750 find_prev_build_dir(path, model, &dir);
751 gtk_tree_path_free(path);
752 msgwin_parse_compiler_error_line(string, dir, &filename, &line);
753 g_free(string);
754 g_free(dir);
756 ret = goto_compiler_file_line(filename, line, focus_editor);
757 g_free(filename);
758 return ret;
761 return FALSE;
765 static void make_absolute(gchar **filename, const gchar *dir)
767 guint skip_dot_slash = 0; /* number of characters to skip at the beginning of the filename */
769 if (*filename == NULL)
770 return;
772 /* skip some characters at the beginning of the filename, at the moment only "./"
773 * can be extended if other "trash" is known */
774 if (strncmp(*filename, "./", 2) == 0)
775 skip_dot_slash = 2;
777 /* add directory */
778 if (! utils_is_absolute_path(*filename))
779 SETPTR(*filename, g_build_filename(dir, *filename + skip_dot_slash, NULL));
783 /* try to parse the file and line number where the error occurred described in line
784 * and when something useful is found, it stores the line number in *line and the
785 * relevant file with the error in *filename.
786 * *line will be -1 if no error was found in string.
787 * *filename must be freed unless it is NULL. */
788 static void parse_file_line(ParseData *data, gchar **filename, gint *line)
790 gchar *end = NULL;
791 gchar **fields;
793 *filename = NULL;
794 *line = -1;
796 g_return_if_fail(data->string != NULL);
798 fields = g_strsplit_set(data->string, data->pattern, data->min_fields);
800 /* parse the line */
801 if (g_strv_length(fields) < data->min_fields)
803 g_strfreev(fields);
804 return;
807 *line = strtol(fields[data->line_idx], &end, 10);
809 /* if the line could not be read, line is 0 and an error occurred, so we leave */
810 if (fields[data->line_idx] == end)
812 g_strfreev(fields);
813 return;
816 /* let's stop here if there is no filename in the error message */
817 if (data->file_idx == -1)
819 /* we have no filename in the error message, so take the current one and hope it's correct */
820 GeanyDocument *doc = document_get_current();
821 if (doc != NULL)
822 *filename = g_strdup(doc->file_name);
823 g_strfreev(fields);
824 return;
827 *filename = g_strdup(fields[data->file_idx]);
828 g_strfreev(fields);
832 static void parse_compiler_error_line(const gchar *string,
833 gchar **filename, gint *line)
835 ParseData data = {NULL, NULL, 0, 0, 0};
837 data.string = string;
839 switch (build_info.file_type_id)
841 case GEANY_FILETYPES_PHP:
843 /* Parse error: parse error, unexpected T_CASE in brace_bug.php on line 3
844 * Parse error: syntax error, unexpected T_LNUMBER, expecting T_FUNCTION in bob.php on line 16 */
845 gchar *tmp = strstr(string, " in ");
847 if (tmp != NULL)
849 data.string = tmp;
850 data.pattern = " ";
851 data.min_fields = 6;
852 data.line_idx = 5;
853 data.file_idx = 2;
855 else
857 data.pattern = " ";
858 data.min_fields = 11;
859 data.line_idx = 10;
860 data.file_idx = 7;
862 break;
864 case GEANY_FILETYPES_PERL:
866 /* syntax error at test.pl line 7, near "{ */
867 data.pattern = " ";
868 data.min_fields = 6;
869 data.line_idx = 5;
870 data.file_idx = 3;
871 break;
873 /* the error output of python and tcl equals */
874 case GEANY_FILETYPES_TCL:
875 case GEANY_FILETYPES_PYTHON:
877 /* File "HyperArch.py", line 37, in ?
878 * (file "clrdial.tcl" line 12)
879 * */
880 if (strstr(string, " line ") != NULL)
882 /* Tcl and old Python format (<= Python 2.5) */
883 data.pattern = " \"";
884 data.min_fields = 6;
885 data.line_idx = 5;
886 data.file_idx = 2;
888 else
890 /* SyntaxError: ('invalid syntax', ('sender.py', 149, 20, ' ...'))
891 * (used since Python 2.6) */
892 data.pattern = ",'";
893 data.min_fields = 8;
894 data.line_idx = 6;
895 data.file_idx = 4;
897 break;
899 case GEANY_FILETYPES_BASIC:
900 case GEANY_FILETYPES_PASCAL:
901 case GEANY_FILETYPES_CS:
903 /* getdrive.bas(52) error 18: Syntax error in '? GetAllDrives'
904 * bandit.pas(149,3) Fatal: Syntax error, ";" expected but "ELSE" found */
905 data.pattern = "(";
906 data.min_fields = 2;
907 data.line_idx = 1;
908 data.file_idx = 0;
909 break;
911 case GEANY_FILETYPES_D:
913 /* GNU D compiler front-end, gdc
914 * gantry.d:18: variable gantry.main.c reference to auto class must be auto
915 * warning - gantry.d:20: statement is not reachable
916 * Digital Mars dmd compiler
917 * warning - pi.d(118): implicit conversion of expression (digit) of type int ...
918 * gantry.d(18): variable gantry.main.c reference to auto class must be auto */
919 if (strncmp(string, "warning - ", 10) == 0)
921 data.pattern = " (:";
922 data.min_fields = 4;
923 data.line_idx = 3;
924 data.file_idx = 2;
926 else
928 data.pattern = "(:";
929 data.min_fields = 2;
930 data.line_idx = 1;
931 data.file_idx = 0;
933 break;
935 case GEANY_FILETYPES_FERITE:
937 /* Error: Parse Error: on line 5 in "/tmp/hello.fe"
938 * Error: Compile Error: on line 24, in /test/class.fe */
939 if (strncmp(string, "Error: Compile Error", 20) == 0)
941 data.pattern = " ";
942 data.min_fields = 8;
943 data.line_idx = 5;
944 data.file_idx = 7;
946 else
948 data.pattern = " \"";
949 data.min_fields = 10;
950 data.line_idx = 5;
951 data.file_idx = 8;
953 break;
955 case GEANY_FILETYPES_HTML:
957 /* line 78 column 7 - Warning: <table> missing '>' for end of tag */
958 data.pattern = " ";
959 data.min_fields = 4;
960 data.line_idx = 1;
961 data.file_idx = -1;
962 break;
964 /* All GNU gcc-like error messages */
965 case GEANY_FILETYPES_C:
966 case GEANY_FILETYPES_CPP:
967 case GEANY_FILETYPES_RUBY:
968 case GEANY_FILETYPES_JAVA:
969 /* only gcc is supported, I don't know any other C(++) compilers and their error messages
970 * empty.h:4: Warnung: type defaults to `int' in declaration of `foo'
971 * empty.c:21: error: conflicting types for `foo'
972 * Only parse file and line, so that linker errors will also work (with -g) */
973 case GEANY_FILETYPES_F77:
974 case GEANY_FILETYPES_FORTRAN:
975 case GEANY_FILETYPES_LATEX:
976 /* ./kommtechnik_2b.tex:18: Emergency stop. */
977 case GEANY_FILETYPES_MAKE: /* Assume makefile is building with gcc */
978 case GEANY_FILETYPES_NONE:
979 default: /* The default is a GNU gcc type error */
981 if (build_info.file_type_id == GEANY_FILETYPES_JAVA &&
982 strncmp(string, "[javac]", 7) == 0)
984 /* Java Apache Ant.
985 * [javac] <Full Path to File + extension>:<line n°>: <error> */
986 data.pattern = " :";
987 data.min_fields = 4;
988 data.line_idx = 2;
989 data.file_idx = 1;
990 break;
992 /* don't accidentally find libtool versions x:y:x and think it is a file name */
993 if (strstr(string, "libtool --mode=link") == NULL)
995 data.pattern = ":";
996 data.min_fields = 3;
997 data.line_idx = 1;
998 data.file_idx = 0;
999 break;
1004 if (data.pattern != NULL)
1005 parse_file_line(&data, filename, line);
1009 /* try to parse the file and line number where the error occurred described in string
1010 * and when something useful is found, it stores the line number in *line and the
1011 * relevant file with the error in *filename.
1012 * *line will be -1 if no error was found in string.
1013 * *filename must be freed unless it is NULL. */
1014 void msgwin_parse_compiler_error_line(const gchar *string, const gchar *dir,
1015 gchar **filename, gint *line)
1017 GeanyFiletype *ft;
1018 gchar *trimmed_string, *utf8_dir;
1020 *filename = NULL;
1021 *line = -1;
1023 if (G_UNLIKELY(string == NULL))
1024 return;
1026 if (dir == NULL)
1027 utf8_dir = utils_get_utf8_from_locale(build_info.dir);
1028 else
1029 utf8_dir = g_strdup(dir);
1030 g_return_if_fail(utf8_dir != NULL);
1032 trimmed_string = g_strdup(string);
1033 g_strchug(trimmed_string); /* remove possible leading whitespace */
1035 ft = filetypes[build_info.file_type_id];
1037 /* try parsing with a custom regex */
1038 if (!filetypes_parse_error_message(ft, trimmed_string, filename, line))
1040 /* fallback to default old-style parsing */
1041 parse_compiler_error_line(trimmed_string, filename, line);
1043 make_absolute(filename, utf8_dir);
1044 g_free(trimmed_string);
1045 g_free(utf8_dir);
1049 /* Tries to parse strings of the file:line style, allowing line field to be missing
1050 * * filename is filled with the filename, should be freed
1051 * * line is filled with the line number or -1 */
1052 static void msgwin_parse_generic_line(const gchar *string, gchar **filename, gint *line)
1054 gchar **fields;
1055 gboolean incertain = TRUE; /* whether we're reasonably certain of the result */
1057 *filename = NULL;
1058 *line = -1;
1060 fields = g_strsplit(string, ":", 2);
1061 /* extract the filename */
1062 if (fields[0] != NULL)
1064 *filename = utils_get_locale_from_utf8(fields[0]);
1065 if (msgwindow.messages_dir != NULL)
1066 make_absolute(filename, msgwindow.messages_dir);
1068 /* now the line */
1069 if (fields[1] != NULL)
1071 gchar *end;
1073 *line = strtol(fields[1], &end, 10);
1074 if (end == fields[1])
1075 *line = -1;
1076 else if (*end == ':' || g_ascii_isspace(*end))
1077 { /* if we have a blank or a separator right after the number, assume we really got a
1078 * filename (it's a grep-like syntax) */
1079 incertain = FALSE;
1083 /* if we aren't sure we got a supposedly correct filename, check it */
1084 if (incertain && ! g_file_test(*filename, G_FILE_TEST_EXISTS))
1086 SETPTR(*filename, NULL);
1087 *line = -1;
1090 g_strfreev(fields);
1094 gboolean msgwin_goto_messages_file_line(gboolean focus_editor)
1096 GtkTreeIter iter;
1097 GtkTreeModel *model;
1098 GtkTreeSelection *selection;
1099 gboolean ret = FALSE;
1101 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(msgwindow.tree_msg));
1102 if (gtk_tree_selection_get_selected(selection, &model, &iter))
1104 gint line;
1105 guint id;
1106 gchar *string;
1107 GeanyDocument *doc;
1108 GeanyDocument *old_doc = document_get_current();
1110 gtk_tree_model_get(model, &iter,
1111 MSG_COL_LINE, &line, MSG_COL_DOC_ID, &id, MSG_COL_STRING, &string, -1);
1112 if (line >= 0 && id > 0)
1114 /* check doc is still open */
1115 doc = document_find_by_id(id);
1116 if (!doc)
1118 ui_set_statusbar(FALSE, _("The document has been closed."));
1119 utils_beep();
1121 else
1123 ret = navqueue_goto_line(old_doc, doc, line);
1124 if (ret && focus_editor)
1125 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1128 else if (line < 0 && string != NULL)
1130 gchar *filename;
1132 /* try with a file:line parsing */
1133 msgwin_parse_generic_line(string, &filename, &line);
1134 if (filename != NULL)
1136 /* use document_open_file to find an already open file, or open it in place */
1137 doc = document_open_file(filename, FALSE, NULL, NULL);
1138 if (doc != NULL)
1140 ret = (line < 0) ? TRUE : navqueue_goto_line(old_doc, doc, line);
1141 if (ret && focus_editor)
1142 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1145 g_free(filename);
1147 g_free(string);
1149 return ret;
1153 static gboolean on_msgwin_button_press_event(GtkWidget *widget, GdkEventButton *event,
1154 gpointer user_data)
1156 /* user_data might be NULL, GPOINTER_TO_INT returns 0 if called with NULL */
1157 gboolean double_click = event->type == GDK_2BUTTON_PRESS;
1159 if (event->button == 1 && (event->type == GDK_BUTTON_RELEASE || double_click))
1161 switch (GPOINTER_TO_INT(user_data))
1163 case MSG_COMPILER:
1164 { /* mouse click in the compiler treeview */
1165 msgwin_goto_compiler_file_line(double_click);
1166 break;
1168 case MSG_MESSAGE:
1169 { /* mouse click in the message treeview (results of 'Find usage') */
1170 msgwin_goto_messages_file_line(double_click);
1171 break;
1174 return double_click; /* TRUE prevents message window re-focusing */
1177 if (event->button == 3)
1178 { /* popupmenu to hide or clear the active treeview */
1179 switch (GPOINTER_TO_INT(user_data))
1181 case MSG_STATUS:
1183 gtk_menu_popup(GTK_MENU(msgwindow.popup_status_menu), NULL, NULL, NULL, NULL,
1184 event->button, event->time);
1185 break;
1187 case MSG_MESSAGE:
1189 gtk_menu_popup(GTK_MENU(msgwindow.popup_msg_menu), NULL, NULL, NULL, NULL,
1190 event->button, event->time);
1191 break;
1193 case MSG_COMPILER:
1195 gtk_menu_popup(GTK_MENU(msgwindow.popup_compiler_menu), NULL, NULL, NULL, NULL,
1196 event->button, event->time);
1197 break;
1201 return FALSE;
1206 * Switches to the given notebook tab of the messages window and shows the messages window
1207 * if it was previously hidden and @a show is set to @c TRUE.
1209 * @param tabnum An index of a tab in the messages window. Valid values are all elements of
1210 * #MessageWindowTabNum.
1211 * @param show Whether to show the messages window at all if it was hidden before.
1213 * @since 0.15
1215 GEANY_API_SYMBOL
1216 void msgwin_switch_tab(gint tabnum, gboolean show)
1218 GtkWidget *widget = NULL; /* widget to focus */
1220 switch (tabnum)
1222 case MSG_SCRATCH: widget = msgwindow.scribble; break;
1223 case MSG_COMPILER: widget = msgwindow.tree_compiler; break;
1224 case MSG_STATUS: widget = msgwindow.tree_status; break;
1225 case MSG_MESSAGE: widget = msgwindow.tree_msg; break;
1226 #ifdef HAVE_VTE
1227 case MSG_VTE: widget = (vte_info.have_vte) ? vc->vte : NULL; break;
1228 #endif
1229 default: break;
1232 /* the msgwin must be visible before we switch to the VTE page so that
1233 * the font settings are applied on realization */
1234 if (show)
1235 msgwin_show_hide(TRUE);
1236 gtk_notebook_set_current_page(GTK_NOTEBOOK(msgwindow.notebook), tabnum);
1237 if (show && widget)
1238 gtk_widget_grab_focus(widget);
1243 * Removes all messages from a tab specified by @a tabnum in the messages window.
1245 * @param tabnum An index of a tab in the messages window which should be cleared.
1246 * Valid values are @c MSG_STATUS, @c MSG_COMPILER and @c MSG_MESSAGE.
1248 * @since 0.15
1250 GEANY_API_SYMBOL
1251 void msgwin_clear_tab(gint tabnum)
1253 GtkListStore *store = NULL;
1255 switch (tabnum)
1257 case MSG_MESSAGE:
1258 store = msgwindow.store_msg;
1259 break;
1261 case MSG_COMPILER:
1262 gtk_list_store_clear(msgwindow.store_compiler);
1263 build_menu_update(NULL); /* update next error items */
1264 return;
1266 case MSG_STATUS: store = msgwindow.store_status; break;
1267 default: return;
1269 if (store == NULL)
1270 return;
1271 gtk_list_store_clear(store);