Support more folding icon styles: arrows, +/- and no lines
[geany-mirror.git] / src / main.c
blob9b4f47174e788dedf1de69fcd5832c7393521fe4
1 /*
2 * main.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
24 /**
25 * @file: main.h
26 * Main program-related commands.
27 * Handles program initialization and cleanup.
30 #include <signal.h>
31 #include <time.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdlib.h>
38 #include "geany.h"
39 #include <glib/gstdio.h>
41 #ifdef HAVE_LOCALE_H
42 # include <locale.h>
43 #endif
45 #include "main.h"
46 #include "prefix.h"
47 #include "prefs.h"
48 #include "interface.h"
49 #include "support.h"
50 #include "callbacks.h"
51 #include "log.h"
52 #include "ui_utils.h"
53 #include "utils.h"
54 #include "document.h"
55 #include "filetypes.h"
56 #include "keyfile.h"
57 #include "win32.h"
58 #include "msgwindow.h"
59 #include "dialogs.h"
60 #include "templates.h"
61 #include "encodings.h"
62 #include "sidebar.h"
63 #include "notebook.h"
64 #include "keybindings.h"
65 #include "editor.h"
66 #include "search.h"
67 #include "build.h"
68 #include "highlighting.h"
69 #include "symbols.h"
70 #include "project.h"
71 #include "tools.h"
72 #include "navqueue.h"
73 #include "plugins.h"
74 #include "printing.h"
75 #include "toolbar.h"
76 #include "geanyobject.h"
78 #ifdef HAVE_SOCKET
79 # include "socket.h"
80 #endif
82 #ifdef HAVE_VTE
83 # include "vte.h"
84 #endif
86 #ifndef N_
87 # define N_(String) (String)
88 #endif
91 GeanyApp *app;
92 gboolean ignore_callback; /* hack workaround for GTK+ toggle button callback problem */
94 GeanyStatus main_status;
95 CommandLineOptions cl_options; /* fields initialised in parse_command_line_options */
98 static gboolean want_plugins;
100 /* command-line options */
101 static gboolean verbose_mode = FALSE;
102 static gboolean ignore_global_tags = FALSE;
103 static gboolean no_msgwin = FALSE;
104 static gboolean show_version = FALSE;
105 static gchar *alternate_config = NULL;
106 #ifdef HAVE_VTE
107 static gboolean no_vte = FALSE;
108 static gchar *lib_vte = NULL;
109 #endif
110 static gboolean generate_tags = FALSE;
111 static gboolean no_preprocessing = FALSE;
112 static gboolean ft_names = FALSE;
113 static gboolean print_prefix = FALSE;
114 #ifdef HAVE_PLUGINS
115 static gboolean no_plugins = FALSE;
116 #endif
117 static gboolean dummy = FALSE;
119 /* in alphabetical order of short options */
120 static GOptionEntry entries[] =
122 { "column", 0, 0, G_OPTION_ARG_INT, &cl_options.goto_column, N_("Set initial column number for the first opened file (useful in conjunction with --line)"), NULL },
123 { "config", 'c', 0, G_OPTION_ARG_FILENAME, &alternate_config, N_("Use an alternate configuration directory"), NULL },
124 { "ft-names", 0, 0, G_OPTION_ARG_NONE, &ft_names, N_("Print internal filetype names"), NULL },
125 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE, &generate_tags, N_("Generate global tags file (see documentation)"), NULL },
126 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE, &no_preprocessing, N_("Don't preprocess C/C++ files when generating tags"), NULL },
127 #ifdef HAVE_SOCKET
128 { "new-instance", 'i', 0, G_OPTION_ARG_NONE, &cl_options.new_instance, N_("Don't open files in a running instance, force opening a new instance"), NULL },
129 { "socket-file", 0, 0, G_OPTION_ARG_FILENAME, &cl_options.socket_filename, N_("Use this socket filename for communication with a running Geany instance"), NULL },
130 { "list-documents", 0, 0, G_OPTION_ARG_NONE, &cl_options.list_documents, N_("Return a list of open documents in a running Geany instance"), NULL },
131 #endif
132 { "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL },
133 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
134 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE, &ignore_global_tags, N_("Don't load auto completion data (see documentation)"), NULL },
135 #ifdef HAVE_PLUGINS
136 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE, &no_plugins, N_("Don't load plugins"), NULL },
137 #endif
138 { "print-prefix", 0, 0, G_OPTION_ARG_NONE, &print_prefix, N_("Print Geany's installation prefix"), NULL },
139 { "no-session", 's', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &cl_options.load_session, N_("Don't load the previous session's files"), NULL },
140 #ifdef HAVE_VTE
141 { "no-terminal", 't', 0, G_OPTION_ARG_NONE, &no_vte, N_("Don't load terminal support"), NULL },
142 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME, &lib_vte, N_("Filename of libvte.so"), NULL },
143 #endif
144 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_mode, N_("Be verbose"), NULL },
145 { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Show version and exit"), NULL },
146 { "dummy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &dummy, NULL, NULL }, /* for +NNN line number arguments */
147 { NULL, 0, 0, 0, NULL, NULL, NULL }
151 static void setup_window_position(void)
153 /* interprets the saved window geometry */
154 if (prefs.save_winpos && ui_prefs.geometry[0] != -1)
156 gtk_window_move(GTK_WINDOW(main_widgets.window), ui_prefs.geometry[0], ui_prefs.geometry[1]);
157 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window), ui_prefs.geometry[2], ui_prefs.geometry[3]);
158 if (ui_prefs.geometry[4] == 1)
159 gtk_window_maximize(GTK_WINDOW(main_widgets.window));
164 /* special things for the initial setup of the checkboxes and related stuff
165 * an action on a setting is only performed if the setting is not equal to the program default
166 * (all the following code is not perfect but it works for the moment) */
167 static void apply_settings(void)
169 ui_update_fold_items();
171 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
172 toolbar_show_hide();
173 if (! ui_prefs.msgwindow_visible)
175 ignore_callback = TRUE;
176 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")), FALSE);
177 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"));
178 ignore_callback = FALSE;
180 if (! ui_prefs.sidebar_visible)
182 ignore_callback = TRUE;
183 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_sidebar1")), FALSE);
184 ignore_callback = FALSE;
186 ui_sidebar_show_hide();
188 toolbar_apply_settings();
189 toolbar_update_ui();
191 ui_update_view_editor_menu_items();
193 /* hide statusbar if desired */
194 if (! interface_prefs.statusbar_visible)
196 gtk_widget_hide(ui_widgets.statusbar);
199 /* set the tab placements of the notebooks */
200 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.tab_pos_editor);
201 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow.notebook), interface_prefs.tab_pos_msgwin);
202 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.sidebar_notebook), interface_prefs.tab_pos_sidebar);
204 /* whether to show notebook tabs or not */
205 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
207 #ifdef HAVE_VTE
208 if (! vte_info.load_vte)
209 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "send_selection_to_vte1"));
210 #else
211 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "send_selection_to_vte1"));
212 #endif
214 if (interface_prefs.sidebar_pos != GTK_POS_LEFT)
215 ui_swap_sidebar_pos();
219 static void main_init(void)
221 /* inits */
222 main_widgets.window = NULL;
223 app->project = NULL;
224 ui_widgets.open_fontsel = NULL;
225 ui_widgets.open_colorsel = NULL;
226 ui_widgets.open_filesel = NULL;
227 ui_widgets.save_filesel = NULL;
228 ui_widgets.prefs_dialog = NULL;
229 main_status.main_window_realized = FALSE;
230 file_prefs.tab_order_ltr = FALSE;
231 main_status.quitting = FALSE;
232 ignore_callback = FALSE;
233 app->tm_workspace = tm_get_workspace();
234 ui_prefs.recent_queue = g_queue_new();
235 ui_prefs.recent_projects_queue = g_queue_new();
236 main_status.opening_session_files = FALSE;
238 ui_init_stock_items();
240 main_widgets.window = create_window1();
242 /* add recent projects to the Project menu */
243 ui_widgets.recent_projects_menuitem = ui_lookup_widget(main_widgets.window, "recent_projects1");
244 ui_widgets.recent_projects_menu_menubar = gtk_menu_new();
245 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_projects_menuitem),
246 ui_widgets.recent_projects_menu_menubar);
248 /* store important pointers for later reference */
249 main_widgets.toolbar = toolbar_init();
250 main_widgets.sidebar_notebook = ui_lookup_widget(main_widgets.window, "notebook3");
251 main_widgets.notebook = ui_lookup_widget(main_widgets.window, "notebook1");
252 main_widgets.editor_menu = create_edit_menu1();
253 main_widgets.tools_menu = ui_lookup_widget(main_widgets.window, "tools1_menu");
255 ui_widgets.toolbar_menu = create_toolbar_popup_menu1();
256 ui_init();
258 /* set widget names for matching with .gtkrc-2.0 */
259 gtk_widget_set_name(main_widgets.window, "GeanyMainWindow");
260 gtk_widget_set_name(ui_widgets.toolbar_menu, "GeanyToolbarMenu");
261 gtk_widget_set_name(main_widgets.editor_menu, "GeanyEditMenu");
262 gtk_widget_set_name(ui_lookup_widget(main_widgets.window, "menubar1"), "GeanyMenubar");
263 gtk_widget_set_name(main_widgets.toolbar, "GeanyToolbar");
265 #if ! GTK_CHECK_VERSION(2, 10, 0)
266 /* hide Page setup menu item, it isn't supported with non-GTK printing */
267 gtk_widget_hide(ui_widgets.print_page_setup);
268 #endif
270 /* for some reason we need to set the initial size request,
271 * otherwise the main window gets crazy dimensions */
272 gtk_widget_set_size_request(main_widgets.window, 10, 10);
273 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
274 GEANY_WINDOW_DEFAULT_WIDTH, GEANY_WINDOW_DEFAULT_HEIGHT);
278 const gchar *main_get_version_string(void)
280 static gchar full[] = VERSION " (svn >= " REVISION ")";
282 if (utils_str_equal(REVISION, "-1"))
283 return VERSION;
284 else
285 return full;
289 /* get the full file path of a command-line argument
290 * N.B. the result should be freed and may contain '/../' or '/./ ' */
291 gchar *main_get_argv_filename(const gchar *filename)
293 gchar *result;
295 if (g_path_is_absolute(filename) || utils_is_uri(filename))
296 result = g_strdup(filename);
297 else
299 /* use current dir */
300 gchar *cur_dir = g_get_current_dir();
302 result = g_strjoin(
303 G_DIR_SEPARATOR_S, cur_dir, filename, NULL);
304 g_free(cur_dir);
306 return result;
310 /* get a :line:column specifier from the end of a filename (if present),
311 * return the line/column values, and remove the specifier from the string
312 * (Note that *line and *column must both be set to -1 initially) */
313 static void get_line_and_column_from_filename(gchar *filename, gint *line, gint *column)
315 gsize i;
316 gint colon_count = 0;
317 gboolean have_number = FALSE;
318 gsize len;
320 g_assert(*line == -1 && *column == -1);
322 if (! NZV(filename))
323 return;
325 /* allow to open files like "test:0" */
326 if (g_file_test(filename, G_FILE_TEST_EXISTS))
327 return;
329 len = strlen(filename);
330 for (i = len - 1; i >= 1; i--)
332 gboolean is_colon = filename[i] == ':';
333 gboolean is_digit = g_ascii_isdigit(filename[i]);
335 if (! is_colon && ! is_digit)
336 break;
338 if (is_colon)
340 if (++colon_count > 1)
341 break; /* bail on 2+ colons in a row */
343 else
344 colon_count = 0;
346 if (is_digit)
347 have_number = TRUE;
349 if (is_colon && have_number)
351 gint number = atoi(&filename[i + 1]);
353 filename[i] = '\0';
354 have_number = FALSE;
356 *column = *line;
357 *line = number;
360 if (*column >= 0)
361 break; /* line and column are set, so we're done */
366 static void setup_paths(void)
368 gchar *data_dir;
369 gchar *doc_dir;
371 /* set paths */
372 #ifdef G_OS_WIN32
373 /* use the installation directory(the one where geany.exe is located) as the base for the
374 * documentation and data files */
375 gchar *install_dir = win32_get_installation_dir();
377 data_dir = g_strconcat(install_dir, "\\data", NULL); /* e.g. C:\Program Files\geany\data */
378 doc_dir = g_strconcat(install_dir, "\\doc", NULL);
380 g_free(install_dir);
381 #else
382 data_dir = g_strconcat(GEANY_DATADIR, "/geany/", NULL); /* e.g. /usr/share/geany */
383 doc_dir = g_strconcat(GEANY_DOCDIR, "/html/", NULL);
384 #endif
386 /* convert path names to locale encoding */
387 app->datadir = utils_get_locale_from_utf8(data_dir);
388 app->docdir = utils_get_locale_from_utf8(doc_dir);
390 g_free(data_dir);
391 g_free(doc_dir);
396 * Checks whether the main window has been realized.
397 * This is an easy indicator whether Geany is right now starting up (main window is not
398 * yet realized) or whether it has finished the startup process (main window is realized).
399 * This is because the main window is realized (i.e. actually drawn on the screen) at the
400 * end of the startup process.
402 * @note Maybe you want to use the @ref geany-startup-complete signal to get notified about
403 * the completed startup process.
405 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
407 * @since 0.19
409 gboolean main_is_realized(void)
411 return main_status.main_window_realized;
416 * Initialises the gettext translation system.
417 * This is a convenience function to set up gettext for internationalisation support
418 * in external plugins. You should call this function early in @ref plugin_init().
419 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
420 * The codeset for the message translations is set to UTF-8.
422 * Note that this function only setups the gettext textdomain for you. You still have
423 * to adjust the build system of your plugin to get internationalisation support
424 * working properly.
426 * @param locale_dir The location where the translation files should be searched. This is
427 * usually the @c LOCALEDIR macro, defined by the build system.
428 * E.g. @c $prefix/share/locale.
429 * Only used on non-Windows systems. On Windows, the directory is determined
430 * by @c g_win32_get_package_installation_directory().
431 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
432 * defined by the build system.
434 * @since 0.16
436 void main_locale_init(const gchar *locale_dir, const gchar *package)
438 gchar *l_locale_dir = NULL;
440 #ifdef HAVE_LOCALE_H
441 setlocale(LC_ALL, "");
442 #endif
444 #ifdef G_OS_WIN32
446 gchar *install_dir = win32_get_installation_dir();
447 /* e.g. C:\Program Files\geany\lib\locale */
448 l_locale_dir = g_strconcat(install_dir, "\\share\\locale", NULL);
449 g_free(install_dir);
451 #else
452 l_locale_dir = g_strdup(locale_dir);
453 #endif
455 bindtextdomain(package, l_locale_dir);
456 bind_textdomain_codeset(package, "UTF-8");
457 textdomain(package);
458 g_free(l_locale_dir);
462 static void print_filetypes(void)
464 guint i;
466 filetypes_init_types();
467 printf("Geany's internal filetype names:\n");
469 for (i = 0; i < filetypes_array->len; i++)
471 printf("%s\n", filetypes[i]->name);
473 filetypes_free_types();
477 static void parse_command_line_options(gint *argc, gchar ***argv)
479 GError *error = NULL;
480 GOptionContext *context;
481 gint i;
482 CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE, FALSE};
484 /* first initialise cl_options fields with default values */
485 cl_options = def_clo;
487 /* the GLib option parser can't handle the +NNN (line number) option,
488 * so we grab that here and replace it with a no-op */
489 for (i = 1; i < (*argc); i++)
491 if ((*argv)[i][0] != '+')
492 continue;
494 cl_options.goto_line = atoi((*argv)[i] + 1);
495 (*argv)[i] = "--dummy";
498 context = g_option_context_new(_("[FILES...]"));
499 g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
500 g_option_group_set_translation_domain(g_option_context_get_main_group(context), GETTEXT_PACKAGE);
501 g_option_context_add_group(context, gtk_get_option_group(FALSE));
502 g_option_context_parse(context, argc, argv, &error);
503 g_option_context_free(context);
505 if (error != NULL)
507 g_printerr("Geany: %s\n", error->message);
508 g_error_free(error);
509 exit(1);
512 if (show_version)
514 printf(PACKAGE " %s ", main_get_version_string());
515 printf(_("(built on %s with GTK %d.%d.%d, GLib %d.%d.%d)"),
516 __DATE__, GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
517 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
518 printf("\n");
520 exit(0);
523 if (print_prefix)
525 printf("%s\n", GEANY_PREFIX);
526 printf("%s\n", GEANY_DATADIR);
527 printf("%s\n", GEANY_LIBDIR);
528 printf("%s\n", GEANY_LOCALEDIR);
529 exit(0);
532 app->debug_mode = verbose_mode;
534 #ifdef G_OS_WIN32
535 win32_init_debug_code();
536 #endif
538 if (alternate_config)
540 geany_debug("alternate config: %s", alternate_config);
541 app->configdir = alternate_config;
543 else
545 app->configdir = g_build_filename(g_get_user_config_dir(), "geany", NULL);
548 if (generate_tags)
550 gboolean ret;
552 filetypes_init_types();
553 filetypes_read_extensions(); /* needed for *.lang.tags filetype matching */
554 ret = symbols_generate_global_tags(*argc, *argv, ! no_preprocessing);
555 exit(ret);
558 if (ft_names)
560 print_filetypes();
561 exit(0);
564 #ifdef HAVE_SOCKET
565 socket_info.ignore_socket = cl_options.new_instance;
566 if (cl_options.socket_filename)
568 socket_info.file_name = cl_options.socket_filename;
570 #endif
572 #ifdef HAVE_VTE
573 vte_info.lib_vte = lib_vte;
574 #endif
575 cl_options.ignore_global_tags = ignore_global_tags;
577 if (! gtk_init_check(NULL, NULL))
578 { /* check whether we have a valid X display and exit if not */
579 g_printerr("Geany: cannot open display\n");
580 exit(1);
585 #ifdef G_OS_WIN32
586 # define DIR_SEP "\\" /* on Windows we need an additional dir separator */
587 #else
588 # define DIR_SEP ""
589 #endif
591 static gint create_config_dir(void)
593 gint saved_errno = 0;
594 gchar *conf_file = g_build_filename(app->configdir, "geany.conf", NULL);
595 gchar *filedefs_dir = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
597 gchar *templates_dir = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
599 if (! g_file_test(app->configdir, G_FILE_TEST_EXISTS))
601 #ifndef G_OS_WIN32
602 /* if we are *not* using an alternate config directory, we check whether the old one
603 * in ~/.geany still exists and try to move it */
604 if (alternate_config == NULL)
606 gchar *old_dir = g_build_filename(g_get_home_dir(), ".geany", NULL);
607 /* move the old config dir if it exists */
608 if (g_file_test(old_dir, G_FILE_TEST_EXISTS))
610 if (! dialogs_show_question_full(main_widgets.window,
611 GTK_STOCK_YES, GTK_STOCK_QUIT, _("Move it now?"),
612 "%s",
613 _("Geany needs to move your old configuration directory before starting.")))
614 exit(0);
616 if (! g_file_test(app->configdir, G_FILE_TEST_IS_DIR))
617 utils_mkdir(app->configdir, TRUE);
619 if (g_rename(old_dir, app->configdir) == 0)
621 dialogs_show_msgbox(GTK_MESSAGE_INFO,
622 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
623 old_dir, app->configdir);
624 g_free(old_dir);
625 return 0;
627 else
629 dialogs_show_msgbox(GTK_MESSAGE_WARNING,
630 /* for translators: the third %s in brackets is the error message which
631 * describes why moving the dir didn't work */
632 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
633 "Please move manually the directory to the new location."),
634 old_dir, app->configdir, g_strerror(errno));
637 g_free(old_dir);
639 #endif
640 geany_debug("creating config directory %s", app->configdir);
641 saved_errno = utils_mkdir(app->configdir, TRUE);
644 if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS))
645 { /* check whether geany.conf can be written */
646 saved_errno = utils_is_file_writeable(app->configdir);
649 /* make subdir for filetype definitions */
650 if (saved_errno == 0)
652 gchar *filedefs_readme = g_build_filename(app->configdir,
653 GEANY_FILEDEFS_SUBDIR, "filetypes.README", NULL);
655 if (! g_file_test(filedefs_dir, G_FILE_TEST_EXISTS))
657 saved_errno = utils_mkdir(filedefs_dir, FALSE);
659 if (saved_errno == 0 && ! g_file_test(filedefs_readme, G_FILE_TEST_EXISTS))
661 gchar *text = g_strconcat(
662 "Copy files from ", app->datadir, " to this directory to overwrite "
663 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
664 "the documentation (in ", app->docdir, DIR_SEP "index.html or visit " GEANY_HOMEPAGE ").", NULL);
665 utils_write_file(filedefs_readme, text);
666 g_free(text);
668 g_free(filedefs_readme);
671 /* make subdir for template files */
672 if (saved_errno == 0)
674 gchar *templates_readme = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR,
675 "templates.README", NULL);
677 if (! g_file_test(templates_dir, G_FILE_TEST_EXISTS))
679 saved_errno = utils_mkdir(templates_dir, FALSE);
681 if (saved_errno == 0 && ! g_file_test(templates_readme, G_FILE_TEST_EXISTS))
683 gchar *text = g_strconcat(
684 "There are several template files in this directory. For these templates you can use wildcards.\n\
685 For more information read the documentation (in ", app->docdir, DIR_SEP "index.html or visit " GEANY_HOMEPAGE ").",
686 NULL);
687 utils_write_file(templates_readme, text);
688 g_free(text);
690 g_free(templates_readme);
693 g_free(filedefs_dir);
694 g_free(templates_dir);
695 g_free(conf_file);
697 return saved_errno;
701 /* Returns 0 if config dir is OK. */
702 static gint setup_config_dir(void)
704 gint mkdir_result = 0;
706 /* convert configdir to locale encoding to avoid troubles */
707 setptr(app->configdir, utils_get_locale_from_utf8(app->configdir));
709 mkdir_result = create_config_dir();
710 if (mkdir_result != 0)
712 if (! dialogs_show_question(
713 _("Configuration directory could not be created (%s).\nThere could be some problems "
714 "using Geany without a configuration directory.\nStart Geany anyway?"),
715 g_strerror(mkdir_result)))
717 exit(0);
720 /* make configdir a real path */
721 if (g_file_test(app->configdir, G_FILE_TEST_EXISTS))
722 setptr(app->configdir, tm_get_real_path(app->configdir));
724 return mkdir_result;
728 static void signal_cb(gint sig)
730 if (sig == SIGTERM)
732 on_exit_clicked(NULL, NULL);
737 /* Used for command-line arguments at startup or from socket.
738 * this will strip any :line:col filename suffix from locale_filename */
739 gboolean main_handle_filename(const gchar *locale_filename)
741 GeanyDocument *doc;
742 gint line = -1, column = -1;
743 gchar *filename;
745 g_return_val_if_fail(locale_filename, FALSE);
747 /* check whether the passed filename is an URI */
748 filename = utils_get_path_from_uri(locale_filename);
749 if (filename == NULL)
750 return FALSE;
752 get_line_and_column_from_filename(filename, &line, &column);
753 if (line >= 0)
754 cl_options.goto_line = line;
755 if (column >= 0)
756 cl_options.goto_column = column;
758 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
760 doc = document_open_file(filename, FALSE, NULL, NULL);
761 /* add recent file manually if opening_session_files is set */
762 if (doc != NULL && main_status.opening_session_files)
763 ui_add_recent_file(doc->file_name);
764 g_free(filename);
765 return TRUE;
767 else if (file_prefs.cmdline_new_files)
768 { /* create new file with the given filename */
769 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
771 doc = document_new_file(utf8_filename, NULL, NULL);
772 if (doc != NULL)
773 ui_add_recent_file(doc->file_name);
774 g_free(utf8_filename);
775 g_free(filename);
776 return TRUE;
778 g_free(filename);
779 return FALSE;
783 /* open files from command line */
784 static gboolean open_cl_files(gint argc, gchar **argv)
786 gint i;
788 if (argc <= 1) return FALSE;
790 for (i = 1; i < argc; i++)
792 gchar *filename = main_get_argv_filename(argv[i]);
793 #ifdef G_OS_WIN32
794 /* It seems argv elements are encoded in CP1252 on a German Windows */
795 setptr(filename, g_locale_to_utf8(filename, -1, NULL, NULL, NULL));
796 #endif
797 if (filename && ! main_handle_filename(filename))
799 const gchar *msg = _("Could not find file '%s'.");
801 g_printerr(msg, filename); /* also print to the terminal */
802 g_printerr("\n");
803 ui_set_statusbar(TRUE, msg, filename);
805 g_free(filename);
807 return TRUE;
811 static void load_session_project_file(void)
813 gchar *locale_filename;
815 g_return_if_fail(project_prefs.session_file != NULL);
817 locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
819 if (NZV(locale_filename))
820 project_load_file(locale_filename);
822 g_free(locale_filename);
823 g_free(project_prefs.session_file); /* no longer needed */
827 static void load_settings(void)
829 configuration_load();
830 /* let cmdline options overwrite configuration settings */
831 #ifdef HAVE_VTE
832 vte_info.have_vte = (no_vte) ? FALSE : vte_info.load_vte;
833 #endif
834 if (no_msgwin)
835 ui_prefs.msgwindow_visible = FALSE;
837 #ifdef HAVE_PLUGINS
838 want_plugins = prefs.load_plugins && !no_plugins;
839 #endif
843 void main_load_project_from_command_line(const gchar *locale_filename, gboolean use_session)
845 gchar *pfile = NULL;
847 if (utils_is_uri(locale_filename))
848 pfile = utils_get_path_from_uri(locale_filename);
849 else
850 pfile = g_strdup(locale_filename);
852 if (pfile != NULL)
854 if (use_session)
855 project_load_file_with_session(pfile);
856 else
857 project_load_file(pfile);
859 g_free(pfile);
863 static void load_startup_files(gint argc, gchar **argv)
865 gboolean load_project_from_cl = FALSE;
867 /* ATM when opening a project file any other filenames are ignored */
868 load_project_from_cl = (argc > 1) && g_str_has_suffix(argv[1], ".geany");
869 if (load_project_from_cl && argc > 2)
870 g_print("Ignoring extra filenames after %s", argv[1]);
872 if (load_project_from_cl || ! open_cl_files(argc, argv))
874 if (prefs.load_session)
876 if (load_project_from_cl)
878 main_load_project_from_command_line(argv[1], FALSE);
880 else if (cl_options.load_session && !cl_options.new_instance)
881 load_session_project_file();
883 /* when we want a new instance, we still load project session files unless -s
884 * was passed */
885 if (!cl_options.load_session || (!load_project_from_cl && cl_options.new_instance))
886 return;
888 /* load session files into tabs, as they are found in the session_files variable */
889 configuration_open_files();
891 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
893 ui_update_popup_copy_items(NULL);
894 ui_update_popup_reundo_items(NULL);
901 static gboolean send_startup_complete(gpointer data)
903 g_signal_emit_by_name(geany_object, "geany-startup-complete");
904 return FALSE;
908 gint main(gint argc, gchar **argv)
910 GeanyDocument *doc;
911 gint config_dir_result;
913 log_handlers_init();
915 app = g_new0(GeanyApp, 1);
916 memset(&main_status, 0, sizeof(GeanyStatus));
917 memset(&prefs, 0, sizeof(GeanyPrefs));
918 memset(&interface_prefs, 0, sizeof(GeanyInterfacePrefs));
919 memset(&toolbar_prefs, 0, sizeof(GeanyToolbarPrefs));
920 memset(&file_prefs, 0, sizeof(GeanyFilePrefs));
921 memset(&search_prefs, 0, sizeof(GeanySearchPrefs));
922 memset(&tool_prefs, 0, sizeof(GeanyToolPrefs));
923 memset(&template_prefs, 0, sizeof(GeanyTemplatePrefs));
924 memset(&ui_prefs, 0, sizeof(UIPrefs));
925 memset(&ui_widgets, 0, sizeof(UIWidgets));
927 setup_paths();
928 #ifdef ENABLE_NLS
929 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
930 #endif
931 parse_command_line_options(&argc, &argv);
933 signal(SIGTERM, signal_cb);
934 #ifdef G_OS_UNIX
935 /* SIGQUIT is used to kill spawned children and we get also this signal, so ignore */
936 signal(SIGQUIT, SIG_IGN);
937 /* ignore SIGPIPE signal for preventing sudden death of program */
938 signal(SIGPIPE, SIG_IGN);
939 #endif
941 config_dir_result = setup_config_dir();
942 #ifdef HAVE_SOCKET
943 /* check and create (unix domain) socket for remote operation */
944 if (! socket_info.ignore_socket)
946 socket_info.lock_socket = -1;
947 socket_info.lock_socket_tag = 0;
948 socket_info.lock_socket = socket_init(argc, argv);
949 /* Socket exists */
950 if (socket_info.lock_socket == -2)
952 /* Quit if filenames were sent to first instance or the list of open
953 * documents has been sent */
954 if (argc > 1 || cl_options.list_documents)
956 gdk_notify_startup_complete();
957 g_free(app->configdir);
958 g_free(app->datadir);
959 g_free(app->docdir);
960 g_free(app);
961 return 0;
963 /* Start a new instance if no command line strings were passed */
964 socket_info.ignore_socket = TRUE;
965 cl_options.new_instance = TRUE;
968 #endif
970 geany_debug("Geany %s, GTK+ %u.%u.%u, GLib %u.%u.%u",
971 main_get_version_string(),
972 gtk_major_version, gtk_minor_version, gtk_micro_version,
973 glib_major_version, glib_minor_version, glib_micro_version);
975 /* create the object so Geany signals can be connected in init() functions */
976 geany_object = geany_object_new();
978 /* inits */
979 main_init();
981 encodings_init();
982 editor_init();
984 configuration_init();
985 /* init stash code before loading keyfile */
986 search_init();
987 project_init();
988 #ifdef HAVE_PLUGINS
989 plugins_init();
990 #endif
991 sidebar_init();
992 load_settings(); /* load keyfile */
994 highlighting_init();
995 msgwin_init();
996 build_init();
997 ui_create_insert_menu_items();
998 ui_create_insert_date_menu_items();
999 keybindings_init();
1000 tools_create_insert_custom_command_menu_items();
1001 notebook_init();
1002 filetypes_init();
1003 templates_init();
1004 navqueue_init();
1005 document_init_doclist();
1006 symbols_init();
1007 filetypes_read_extensions();
1008 editor_snippets_init();
1010 /* set window icon */
1012 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
1013 gtk_window_set_icon(GTK_WINDOW(main_widgets.window), pb);
1014 g_object_unref(pb); /* free our reference */
1017 /* registering some basic events */
1018 g_signal_connect(main_widgets.window, "delete-event", G_CALLBACK(on_exit_clicked), NULL);
1019 g_signal_connect(main_widgets.window, "window-state-event", G_CALLBACK(on_window_state_event), NULL);
1021 g_signal_connect(msgwindow.scribble, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
1023 #ifdef HAVE_VTE
1024 vte_init();
1025 #endif
1026 ui_create_recent_menus();
1028 ui_set_statusbar(TRUE, _("This is Geany %s."), main_get_version_string());
1029 if (config_dir_result != 0)
1030 ui_set_statusbar(TRUE, _("Configuration directory could not be created (%s)."),
1031 g_strerror(config_dir_result));
1033 /* apply all configuration options */
1034 apply_settings();
1036 #ifdef HAVE_PLUGINS
1037 /* load any enabled plugins before we open any documents */
1038 if (want_plugins)
1039 plugins_load_active();
1040 #endif
1042 /* set the active sidebar page after plugins have been loaded */
1043 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), ui_prefs.sidebar_page);
1045 /* load keybinding settings after plugins have added their groups */
1046 keybindings_load_keyfile();
1048 /* load any command line files or session files */
1049 main_status.opening_session_files = TRUE;
1050 load_startup_files(argc, argv);
1051 main_status.opening_session_files = FALSE;
1053 /* open a new file if no other file was opened */
1054 document_new_file_if_non_open();
1056 ui_document_buttons_update();
1057 ui_save_buttons_toggle(FALSE);
1059 doc = document_get_current();
1060 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1061 sidebar_select_openfiles_item(doc);
1062 build_menu_update(doc);
1063 sidebar_update_tag_list(doc, FALSE);
1065 #ifdef G_OS_WIN32
1066 /* Manually realise the main window to be able to set the position but don't show it.
1067 * We don't set the position after showing the window to avoid flickering. */
1068 gtk_widget_realize(main_widgets.window);
1069 #endif
1070 setup_window_position();
1072 /* finally show the window */
1073 gtk_widget_show(main_widgets.window);
1074 main_status.main_window_realized = TRUE;
1076 configuration_apply_settings();
1078 #ifdef HAVE_SOCKET
1079 /* register the callback of socket input */
1080 if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
1082 socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
1083 socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,
1084 G_IO_IN | G_IO_PRI | G_IO_ERR, socket_lock_input_cb, main_widgets.window);
1086 #endif
1088 #ifdef G_OS_WIN32
1090 gchar *dir;
1091 /* On Windows, change the working directory to the Geany installation path to not lock
1092 * the directory of a file passed as command line argument (see bug #2626124). */
1093 dir = win32_get_installation_dir();
1094 win32_set_working_directory(dir);
1095 g_free(dir);
1097 #endif
1099 /* when we are really done with setting everything up and the main event loop is running,
1100 * tell other components, mainly plugins, that startup is complete */
1101 g_idle_add_full(G_PRIORITY_LOW, send_startup_complete, NULL, NULL);
1103 gtk_main();
1104 return 0;
1108 static void queue_free(GQueue *queue)
1110 while (! g_queue_is_empty(queue))
1112 g_free(g_queue_pop_tail(queue));
1114 g_queue_free(queue);
1118 void main_quit()
1120 geany_debug("Quitting...");
1122 #ifdef HAVE_SOCKET
1123 socket_finalize();
1124 #endif
1126 #ifdef HAVE_PLUGINS
1127 plugins_finalize();
1128 #endif
1130 navqueue_free();
1131 keybindings_free();
1132 filetypes_save_commands();
1133 highlighting_free_styles();
1134 templates_free_templates();
1135 msgwin_finalize();
1136 search_finalize();
1137 build_finalize();
1138 document_finalize();
1139 symbols_finalize();
1140 project_finalize();
1141 editor_finalize();
1142 editor_snippets_free();
1143 encodings_finalize();
1144 toolbar_finalize();
1145 sidebar_finalize();
1146 configuration_finalize();
1147 filetypes_free_types();
1148 log_finalize();
1150 tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
1151 g_free(app->configdir);
1152 g_free(app->datadir);
1153 g_free(app->docdir);
1154 g_free(prefs.default_open_path);
1155 g_free(prefs.custom_plugin_path);
1156 g_free(ui_prefs.custom_date_format);
1157 g_free(interface_prefs.editor_font);
1158 g_free(interface_prefs.tagbar_font);
1159 g_free(interface_prefs.msgwin_font);
1160 g_free(editor_prefs.long_line_color);
1161 g_free(editor_prefs.comment_toggle_mark);
1162 g_free(editor_prefs.color_scheme);
1163 g_free(tool_prefs.context_action_cmd);
1164 g_free(template_prefs.developer);
1165 g_free(template_prefs.company);
1166 g_free(template_prefs.mail);
1167 g_free(template_prefs.initials);
1168 g_free(template_prefs.version);
1169 g_free(tool_prefs.term_cmd);
1170 g_free(tool_prefs.browser_cmd);
1171 g_free(tool_prefs.grep_cmd);
1172 g_free(printing_prefs.external_print_cmd);
1173 g_free(printing_prefs.page_header_datefmt);
1174 g_strfreev(ui_prefs.custom_commands);
1176 queue_free(ui_prefs.recent_queue);
1177 queue_free(ui_prefs.recent_projects_queue);
1179 if (ui_widgets.prefs_dialog && GTK_IS_WIDGET(ui_widgets.prefs_dialog)) gtk_widget_destroy(ui_widgets.prefs_dialog);
1180 if (ui_widgets.save_filesel && GTK_IS_WIDGET(ui_widgets.save_filesel)) gtk_widget_destroy(ui_widgets.save_filesel);
1181 if (ui_widgets.open_filesel && GTK_IS_WIDGET(ui_widgets.open_filesel)) gtk_widget_destroy(ui_widgets.open_filesel);
1182 if (ui_widgets.open_fontsel && GTK_IS_WIDGET(ui_widgets.open_fontsel)) gtk_widget_destroy(ui_widgets.open_fontsel);
1183 if (ui_widgets.open_colorsel && GTK_IS_WIDGET(ui_widgets.open_colorsel)) gtk_widget_destroy(ui_widgets.open_colorsel);
1184 #ifdef HAVE_VTE
1185 if (vte_info.have_vte) vte_close();
1186 g_free(vte_info.lib_vte);
1187 g_free(vte_info.dir);
1188 #endif
1189 gtk_widget_destroy(main_widgets.window);
1191 /* destroy popup menus */
1192 if (main_widgets.editor_menu && GTK_IS_WIDGET(main_widgets.editor_menu))
1193 gtk_widget_destroy(main_widgets.editor_menu);
1194 if (ui_widgets.toolbar_menu && GTK_IS_WIDGET(ui_widgets.toolbar_menu))
1195 gtk_widget_destroy(ui_widgets.toolbar_menu);
1196 if (msgwindow.popup_status_menu && GTK_IS_WIDGET(msgwindow.popup_status_menu))
1197 gtk_widget_destroy(msgwindow.popup_status_menu);
1198 if (msgwindow.popup_msg_menu && GTK_IS_WIDGET(msgwindow.popup_msg_menu))
1199 gtk_widget_destroy(msgwindow.popup_msg_menu);
1200 if (msgwindow.popup_compiler_menu && GTK_IS_WIDGET(msgwindow.popup_compiler_menu))
1201 gtk_widget_destroy(msgwindow.popup_compiler_menu);
1203 g_object_unref(geany_object);
1204 geany_object = NULL;
1206 g_free(app);
1208 gtk_main_quit();
1212 /* malloc compatibility code */
1213 #undef malloc
1214 void *malloc(size_t n);
1216 /* Allocate an N-byte block of memory from the heap. If N is zero, allocate a 1-byte block. */
1217 void *rpl_malloc(size_t n)
1219 if (n == 0)
1220 n = 1;
1221 return malloc(n);
1226 * Reloads most of Geany's configuration files without restarting. Currently the following
1227 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1228 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1229 * and 'settings' and 'build_settings' sections of the filetype definition files.
1231 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1232 * editor plugin).
1234 * @since 0.15
1236 void main_reload_configuration(void)
1238 /* reload templates */
1239 templates_free_templates();
1240 templates_init();
1242 /* reload snippets */
1243 editor_snippets_free();
1244 editor_snippets_init();
1246 /* reload filetype extensions */
1247 filetypes_read_extensions();
1249 filetypes_reload();
1251 /* C tag names to ignore */
1252 symbols_reload_config_files();
1254 ui_set_statusbar(TRUE, _("Configuration files reloaded."));