Version bump.
[geany-mirror.git] / src / main.c
blobaa7cf07feccc6c2b49cd16259eea3fdb88c7e444
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 file_prefs.tab_order_beside = FALSE;
232 main_status.quitting = FALSE;
233 ignore_callback = FALSE;
234 app->tm_workspace = tm_get_workspace();
235 ui_prefs.recent_queue = g_queue_new();
236 ui_prefs.recent_projects_queue = g_queue_new();
237 main_status.opening_session_files = FALSE;
239 ui_init_stock_items();
241 main_widgets.window = create_window1();
243 /* add recent projects to the Project menu */
244 ui_widgets.recent_projects_menuitem = ui_lookup_widget(main_widgets.window, "recent_projects1");
245 ui_widgets.recent_projects_menu_menubar = gtk_menu_new();
246 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_projects_menuitem),
247 ui_widgets.recent_projects_menu_menubar);
249 /* store important pointers for later reference */
250 main_widgets.toolbar = toolbar_init();
251 main_widgets.sidebar_notebook = ui_lookup_widget(main_widgets.window, "notebook3");
252 main_widgets.notebook = ui_lookup_widget(main_widgets.window, "notebook1");
253 main_widgets.editor_menu = create_edit_menu1();
254 main_widgets.tools_menu = ui_lookup_widget(main_widgets.window, "tools1_menu");
256 ui_widgets.toolbar_menu = create_toolbar_popup_menu1();
257 ui_init();
259 /* set widget names for matching with .gtkrc-2.0 */
260 gtk_widget_set_name(main_widgets.window, "GeanyMainWindow");
261 gtk_widget_set_name(ui_widgets.toolbar_menu, "GeanyToolbarMenu");
262 gtk_widget_set_name(main_widgets.editor_menu, "GeanyEditMenu");
263 gtk_widget_set_name(ui_lookup_widget(main_widgets.window, "menubar1"), "GeanyMenubar");
264 gtk_widget_set_name(main_widgets.toolbar, "GeanyToolbar");
266 #if ! GTK_CHECK_VERSION(2, 10, 0)
267 /* hide Page setup menu item, it isn't supported with non-GTK printing */
268 gtk_widget_hide(ui_widgets.print_page_setup);
269 #endif
271 /* for some reason we need to set the initial size request,
272 * otherwise the main window gets crazy dimensions */
273 gtk_widget_set_size_request(main_widgets.window, 10, 10);
274 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
275 GEANY_WINDOW_DEFAULT_WIDTH, GEANY_WINDOW_DEFAULT_HEIGHT);
279 const gchar *main_get_version_string(void)
281 static gchar full[] = VERSION " (svn >= " REVISION ")";
283 if (utils_str_equal(REVISION, "-1"))
284 return VERSION;
285 else
286 return full;
290 /* get the full file path of a command-line argument
291 * N.B. the result should be freed and may contain '/../' or '/./ ' */
292 gchar *main_get_argv_filename(const gchar *filename)
294 gchar *result;
296 if (g_path_is_absolute(filename) || utils_is_uri(filename))
297 result = g_strdup(filename);
298 else
300 /* use current dir */
301 gchar *cur_dir = g_get_current_dir();
303 result = g_strjoin(
304 G_DIR_SEPARATOR_S, cur_dir, filename, NULL);
305 g_free(cur_dir);
307 return result;
311 /* get a :line:column specifier from the end of a filename (if present),
312 * return the line/column values, and remove the specifier from the string
313 * (Note that *line and *column must both be set to -1 initially) */
314 static void get_line_and_column_from_filename(gchar *filename, gint *line, gint *column)
316 gsize i;
317 gint colon_count = 0;
318 gboolean have_number = FALSE;
319 gsize len;
321 g_assert(*line == -1 && *column == -1);
323 if (! NZV(filename))
324 return;
326 /* allow to open files like "test:0" */
327 if (g_file_test(filename, G_FILE_TEST_EXISTS))
328 return;
330 len = strlen(filename);
331 for (i = len - 1; i >= 1; i--)
333 gboolean is_colon = filename[i] == ':';
334 gboolean is_digit = g_ascii_isdigit(filename[i]);
336 if (! is_colon && ! is_digit)
337 break;
339 if (is_colon)
341 if (++colon_count > 1)
342 break; /* bail on 2+ colons in a row */
344 else
345 colon_count = 0;
347 if (is_digit)
348 have_number = TRUE;
350 if (is_colon && have_number)
352 gint number = atoi(&filename[i + 1]);
354 filename[i] = '\0';
355 have_number = FALSE;
357 *column = *line;
358 *line = number;
361 if (*column >= 0)
362 break; /* line and column are set, so we're done */
367 static void setup_paths(void)
369 gchar *data_dir;
370 gchar *doc_dir;
372 /* set paths */
373 #ifdef G_OS_WIN32
374 /* use the installation directory(the one where geany.exe is located) as the base for the
375 * documentation and data files */
376 gchar *install_dir = win32_get_installation_dir();
378 data_dir = g_strconcat(install_dir, "\\data", NULL); /* e.g. C:\Program Files\geany\data */
379 doc_dir = g_strconcat(install_dir, "\\doc", NULL);
381 g_free(install_dir);
382 #else
383 data_dir = g_strconcat(GEANY_DATADIR, "/geany/", NULL); /* e.g. /usr/share/geany */
384 doc_dir = g_strconcat(GEANY_DOCDIR, "/html/", NULL);
385 #endif
387 /* convert path names to locale encoding */
388 app->datadir = utils_get_locale_from_utf8(data_dir);
389 app->docdir = utils_get_locale_from_utf8(doc_dir);
391 g_free(data_dir);
392 g_free(doc_dir);
397 * Checks whether the main window has been realized.
398 * This is an easy indicator whether Geany is right now starting up (main window is not
399 * yet realized) or whether it has finished the startup process (main window is realized).
400 * This is because the main window is realized (i.e. actually drawn on the screen) at the
401 * end of the startup process.
403 * @note Maybe you want to use the @ref geany-startup-complete signal to get notified about
404 * the completed startup process.
406 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
408 * @since 0.19
410 gboolean main_is_realized(void)
412 return main_status.main_window_realized;
417 * Initialises the gettext translation system.
418 * This is a convenience function to set up gettext for internationalisation support
419 * in external plugins. You should call this function early in @ref plugin_init().
420 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
421 * The codeset for the message translations is set to UTF-8.
423 * Note that this function only setups the gettext textdomain for you. You still have
424 * to adjust the build system of your plugin to get internationalisation support
425 * working properly.
427 * @param locale_dir The location where the translation files should be searched. This is
428 * usually the @c LOCALEDIR macro, defined by the build system.
429 * E.g. @c $prefix/share/locale.
430 * Only used on non-Windows systems. On Windows, the directory is determined
431 * by @c g_win32_get_package_installation_directory().
432 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
433 * defined by the build system.
435 * @since 0.16
437 void main_locale_init(const gchar *locale_dir, const gchar *package)
439 gchar *l_locale_dir = NULL;
441 #ifdef HAVE_LOCALE_H
442 setlocale(LC_ALL, "");
443 #endif
445 #ifdef G_OS_WIN32
447 gchar *install_dir = win32_get_installation_dir();
448 /* e.g. C:\Program Files\geany\lib\locale */
449 l_locale_dir = g_strconcat(install_dir, "\\share\\locale", NULL);
450 g_free(install_dir);
452 #else
453 l_locale_dir = g_strdup(locale_dir);
454 #endif
456 bindtextdomain(package, l_locale_dir);
457 bind_textdomain_codeset(package, "UTF-8");
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 document_init_doclist();
553 filetypes_init_types();
554 filetypes_read_extensions(); /* needed for *.lang.tags filetype matching */
555 ret = symbols_generate_global_tags(*argc, *argv, ! no_preprocessing);
556 filetypes_free_types();
557 document_finalize();
558 exit(ret);
561 if (ft_names)
563 print_filetypes();
564 exit(0);
567 #ifdef HAVE_SOCKET
568 socket_info.ignore_socket = cl_options.new_instance;
569 if (cl_options.socket_filename)
571 socket_info.file_name = cl_options.socket_filename;
573 #endif
575 #ifdef HAVE_VTE
576 vte_info.lib_vte = lib_vte;
577 #endif
578 cl_options.ignore_global_tags = ignore_global_tags;
580 if (! gtk_init_check(NULL, NULL))
581 { /* check whether we have a valid X display and exit if not */
582 g_printerr("Geany: cannot open display\n");
583 exit(1);
588 #ifdef G_OS_WIN32
589 # define DIR_SEP "\\" /* on Windows we need an additional dir separator */
590 #else
591 # define DIR_SEP ""
592 #endif
594 static gint create_config_dir(void)
596 gint saved_errno = 0;
597 gchar *conf_file = g_build_filename(app->configdir, "geany.conf", NULL);
598 gchar *filedefs_dir = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
600 gchar *templates_dir = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
602 if (! g_file_test(app->configdir, G_FILE_TEST_EXISTS))
604 #ifndef G_OS_WIN32
605 /* if we are *not* using an alternate config directory, we check whether the old one
606 * in ~/.geany still exists and try to move it */
607 if (alternate_config == NULL)
609 gchar *old_dir = g_build_filename(g_get_home_dir(), ".geany", NULL);
610 /* move the old config dir if it exists */
611 if (g_file_test(old_dir, G_FILE_TEST_EXISTS))
613 if (! dialogs_show_question_full(main_widgets.window,
614 GTK_STOCK_YES, GTK_STOCK_QUIT, _("Move it now?"),
615 "%s",
616 _("Geany needs to move your old configuration directory before starting.")))
617 exit(0);
619 if (! g_file_test(app->configdir, G_FILE_TEST_IS_DIR))
620 utils_mkdir(app->configdir, TRUE);
622 if (g_rename(old_dir, app->configdir) == 0)
624 dialogs_show_msgbox(GTK_MESSAGE_INFO,
625 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
626 old_dir, app->configdir);
627 g_free(old_dir);
628 return 0;
630 else
632 dialogs_show_msgbox(GTK_MESSAGE_WARNING,
633 /* for translators: the third %s in brackets is the error message which
634 * describes why moving the dir didn't work */
635 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
636 "Please move manually the directory to the new location."),
637 old_dir, app->configdir, g_strerror(errno));
640 g_free(old_dir);
642 #endif
643 geany_debug("creating config directory %s", app->configdir);
644 saved_errno = utils_mkdir(app->configdir, TRUE);
647 if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS))
648 { /* check whether geany.conf can be written */
649 saved_errno = utils_is_file_writeable(app->configdir);
652 /* make subdir for filetype definitions */
653 if (saved_errno == 0)
655 gchar *filedefs_readme = g_build_filename(app->configdir,
656 GEANY_FILEDEFS_SUBDIR, "filetypes.README", NULL);
658 if (! g_file_test(filedefs_dir, G_FILE_TEST_EXISTS))
660 saved_errno = utils_mkdir(filedefs_dir, FALSE);
662 if (saved_errno == 0 && ! g_file_test(filedefs_readme, G_FILE_TEST_EXISTS))
664 gchar *text = g_strconcat(
665 "Copy files from ", app->datadir, " to this directory to overwrite "
666 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
667 "the documentation (in ", app->docdir, DIR_SEP "index.html or visit " GEANY_HOMEPAGE ").", NULL);
668 utils_write_file(filedefs_readme, text);
669 g_free(text);
671 g_free(filedefs_readme);
674 /* make subdir for template files */
675 if (saved_errno == 0)
677 gchar *templates_readme = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR,
678 "templates.README", NULL);
680 if (! g_file_test(templates_dir, G_FILE_TEST_EXISTS))
682 saved_errno = utils_mkdir(templates_dir, FALSE);
684 if (saved_errno == 0 && ! g_file_test(templates_readme, G_FILE_TEST_EXISTS))
686 gchar *text = g_strconcat(
687 "There are several template files in this directory. For these templates you can use wildcards.\n\
688 For more information read the documentation (in ", app->docdir, DIR_SEP "index.html or visit " GEANY_HOMEPAGE ").",
689 NULL);
690 utils_write_file(templates_readme, text);
691 g_free(text);
693 g_free(templates_readme);
696 g_free(filedefs_dir);
697 g_free(templates_dir);
698 g_free(conf_file);
700 return saved_errno;
704 /* Returns 0 if config dir is OK. */
705 static gint setup_config_dir(void)
707 gint mkdir_result = 0;
709 /* convert configdir to locale encoding to avoid troubles */
710 setptr(app->configdir, utils_get_locale_from_utf8(app->configdir));
712 mkdir_result = create_config_dir();
713 if (mkdir_result != 0)
715 if (! dialogs_show_question(
716 _("Configuration directory could not be created (%s).\nThere could be some problems "
717 "using Geany without a configuration directory.\nStart Geany anyway?"),
718 g_strerror(mkdir_result)))
720 exit(0);
723 /* make configdir a real path */
724 if (g_file_test(app->configdir, G_FILE_TEST_EXISTS))
725 setptr(app->configdir, tm_get_real_path(app->configdir));
727 return mkdir_result;
731 static void signal_cb(gint sig)
733 if (sig == SIGTERM)
735 on_exit_clicked(NULL, NULL);
740 /* Used for command-line arguments at startup or from socket.
741 * this will strip any :line:col filename suffix from locale_filename */
742 gboolean main_handle_filename(const gchar *locale_filename)
744 GeanyDocument *doc;
745 gint line = -1, column = -1;
746 gchar *filename;
748 g_return_val_if_fail(locale_filename, FALSE);
750 /* check whether the passed filename is an URI */
751 filename = utils_get_path_from_uri(locale_filename);
752 if (filename == NULL)
753 return FALSE;
755 get_line_and_column_from_filename(filename, &line, &column);
756 if (line >= 0)
757 cl_options.goto_line = line;
758 if (column >= 0)
759 cl_options.goto_column = column;
761 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
763 doc = document_open_file(filename, FALSE, NULL, NULL);
764 /* add recent file manually if opening_session_files is set */
765 if (doc != NULL && main_status.opening_session_files)
766 ui_add_recent_file(doc->file_name);
767 g_free(filename);
768 return TRUE;
770 else if (file_prefs.cmdline_new_files)
771 { /* create new file with the given filename */
772 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
774 doc = document_new_file(utf8_filename, NULL, NULL);
775 if (doc != NULL)
776 ui_add_recent_file(doc->file_name);
777 g_free(utf8_filename);
778 g_free(filename);
779 return TRUE;
781 g_free(filename);
782 return FALSE;
786 /* open files from command line */
787 static gboolean open_cl_files(gint argc, gchar **argv)
789 gint i;
791 if (argc <= 1) return FALSE;
793 for (i = 1; i < argc; i++)
795 gchar *filename = main_get_argv_filename(argv[i]);
796 #ifdef G_OS_WIN32
797 /* It seems argv elements are encoded in CP1252 on a German Windows */
798 setptr(filename, g_locale_to_utf8(filename, -1, NULL, NULL, NULL));
799 #endif
800 if (filename && ! main_handle_filename(filename))
802 const gchar *msg = _("Could not find file '%s'.");
804 g_printerr(msg, filename); /* also print to the terminal */
805 g_printerr("\n");
806 ui_set_statusbar(TRUE, msg, filename);
808 g_free(filename);
810 return TRUE;
814 static void load_session_project_file(void)
816 gchar *locale_filename;
818 g_return_if_fail(project_prefs.session_file != NULL);
820 locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
822 if (NZV(locale_filename))
823 project_load_file(locale_filename);
825 g_free(locale_filename);
826 g_free(project_prefs.session_file); /* no longer needed */
830 static void load_settings(void)
832 configuration_load();
833 /* let cmdline options overwrite configuration settings */
834 #ifdef HAVE_VTE
835 vte_info.have_vte = (no_vte) ? FALSE : vte_info.load_vte;
836 #endif
837 if (no_msgwin)
838 ui_prefs.msgwindow_visible = FALSE;
840 #ifdef HAVE_PLUGINS
841 want_plugins = prefs.load_plugins && !no_plugins;
842 #endif
846 void main_load_project_from_command_line(const gchar *locale_filename, gboolean use_session)
848 gchar *pfile = NULL;
850 if (utils_is_uri(locale_filename))
851 pfile = utils_get_path_from_uri(locale_filename);
852 else
853 pfile = g_strdup(locale_filename);
855 if (pfile != NULL)
857 if (use_session)
858 project_load_file_with_session(pfile);
859 else
860 project_load_file(pfile);
862 g_free(pfile);
866 static void load_startup_files(gint argc, gchar **argv)
868 gboolean load_project_from_cl = FALSE;
870 /* ATM when opening a project file any other filenames are ignored */
871 load_project_from_cl = (argc > 1) && g_str_has_suffix(argv[1], ".geany");
872 if (load_project_from_cl && argc > 2)
873 g_print("Ignoring extra filenames after %s", argv[1]);
875 if (load_project_from_cl || ! open_cl_files(argc, argv))
877 if (prefs.load_session)
879 if (load_project_from_cl)
881 main_load_project_from_command_line(argv[1], FALSE);
883 else if (cl_options.load_session && !cl_options.new_instance)
884 load_session_project_file();
886 /* when we want a new instance, we still load project session files unless -s
887 * was passed */
888 if (!cl_options.load_session || (!load_project_from_cl && cl_options.new_instance))
889 return;
891 /* load session files into tabs, as they are found in the session_files variable */
892 configuration_open_files();
894 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
896 ui_update_popup_copy_items(NULL);
897 ui_update_popup_reundo_items(NULL);
904 static gboolean send_startup_complete(gpointer data)
906 g_signal_emit_by_name(geany_object, "geany-startup-complete");
907 return FALSE;
911 static const gchar *get_locale(void)
913 const gchar *locale = "unknown";
914 #ifdef HAVE_LOCALE_H
915 locale = setlocale(LC_CTYPE, NULL);
916 #endif
917 return locale;
921 gint main(gint argc, gchar **argv)
923 GeanyDocument *doc;
924 gint config_dir_result;
925 const gchar *locale;
927 log_handlers_init();
929 app = g_new0(GeanyApp, 1);
930 memset(&main_status, 0, sizeof(GeanyStatus));
931 memset(&prefs, 0, sizeof(GeanyPrefs));
932 memset(&interface_prefs, 0, sizeof(GeanyInterfacePrefs));
933 memset(&toolbar_prefs, 0, sizeof(GeanyToolbarPrefs));
934 memset(&file_prefs, 0, sizeof(GeanyFilePrefs));
935 memset(&search_prefs, 0, sizeof(GeanySearchPrefs));
936 memset(&tool_prefs, 0, sizeof(GeanyToolPrefs));
937 memset(&template_prefs, 0, sizeof(GeanyTemplatePrefs));
938 memset(&ui_prefs, 0, sizeof(UIPrefs));
939 memset(&ui_widgets, 0, sizeof(UIWidgets));
941 setup_paths();
942 #ifdef ENABLE_NLS
943 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
944 #endif
945 parse_command_line_options(&argc, &argv);
947 signal(SIGTERM, signal_cb);
948 #ifdef G_OS_UNIX
949 /* SIGQUIT is used to kill spawned children and we get also this signal, so ignore */
950 signal(SIGQUIT, SIG_IGN);
951 /* ignore SIGPIPE signal for preventing sudden death of program */
952 signal(SIGPIPE, SIG_IGN);
953 #endif
955 config_dir_result = setup_config_dir();
956 #ifdef HAVE_SOCKET
957 /* check and create (unix domain) socket for remote operation */
958 if (! socket_info.ignore_socket)
960 socket_info.lock_socket = -1;
961 socket_info.lock_socket_tag = 0;
962 socket_info.lock_socket = socket_init(argc, argv);
963 /* Socket exists */
964 if (socket_info.lock_socket == -2)
966 /* Quit if filenames were sent to first instance or the list of open
967 * documents has been sent */
968 if (argc > 1 || cl_options.list_documents)
970 gdk_notify_startup_complete();
971 g_free(app->configdir);
972 g_free(app->datadir);
973 g_free(app->docdir);
974 g_free(app);
975 return 0;
977 /* Start a new instance if no command line strings were passed */
978 socket_info.ignore_socket = TRUE;
979 cl_options.new_instance = TRUE;
982 #endif
984 locale = get_locale();
985 geany_debug("Geany %s, GTK+ %u.%u.%u, GLib %u.%u.%u (%s)",
986 main_get_version_string(),
987 gtk_major_version, gtk_minor_version, gtk_micro_version,
988 glib_major_version, glib_minor_version, glib_micro_version,
989 locale);
991 /* create the object so Geany signals can be connected in init() functions */
992 geany_object = geany_object_new();
994 /* inits */
995 main_init();
997 encodings_init();
998 editor_init();
1000 configuration_init();
1001 /* init stash code before loading keyfile */
1002 search_init();
1003 project_init();
1004 #ifdef HAVE_PLUGINS
1005 plugins_init();
1006 #endif
1007 sidebar_init();
1008 load_settings(); /* load keyfile */
1010 highlighting_init();
1011 msgwin_init();
1012 build_init();
1013 ui_create_insert_menu_items();
1014 ui_create_insert_date_menu_items();
1015 keybindings_init();
1016 tools_create_insert_custom_command_menu_items();
1017 notebook_init();
1018 filetypes_init();
1019 templates_init();
1020 navqueue_init();
1021 document_init_doclist();
1022 symbols_init();
1023 filetypes_read_extensions();
1024 editor_snippets_init();
1026 /* set window icon */
1028 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
1029 gtk_window_set_icon(GTK_WINDOW(main_widgets.window), pb);
1030 g_object_unref(pb); /* free our reference */
1033 /* registering some basic events */
1034 g_signal_connect(main_widgets.window, "delete-event", G_CALLBACK(on_exit_clicked), NULL);
1035 g_signal_connect(main_widgets.window, "window-state-event", G_CALLBACK(on_window_state_event), NULL);
1037 g_signal_connect(msgwindow.scribble, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
1039 #ifdef HAVE_VTE
1040 vte_init();
1041 #endif
1042 ui_create_recent_menus();
1044 ui_set_statusbar(TRUE, _("This is Geany %s."), main_get_version_string());
1045 if (config_dir_result != 0)
1046 ui_set_statusbar(TRUE, _("Configuration directory could not be created (%s)."),
1047 g_strerror(config_dir_result));
1049 /* apply all configuration options */
1050 apply_settings();
1052 #ifdef HAVE_PLUGINS
1053 /* load any enabled plugins before we open any documents */
1054 if (want_plugins)
1055 plugins_load_active();
1056 #endif
1058 /* set the active sidebar page after plugins have been loaded */
1059 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), ui_prefs.sidebar_page);
1061 /* load keybinding settings after plugins have added their groups */
1062 keybindings_load_keyfile();
1064 /* load any command line files or session files */
1065 main_status.opening_session_files = TRUE;
1066 load_startup_files(argc, argv);
1067 main_status.opening_session_files = FALSE;
1069 /* open a new file if no other file was opened */
1070 document_new_file_if_non_open();
1072 ui_document_buttons_update();
1073 ui_save_buttons_toggle(FALSE);
1075 doc = document_get_current();
1076 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1077 sidebar_select_openfiles_item(doc);
1078 build_menu_update(doc);
1079 sidebar_update_tag_list(doc, FALSE);
1081 #ifdef G_OS_WIN32
1082 /* Manually realise the main window to be able to set the position but don't show it.
1083 * We don't set the position after showing the window to avoid flickering. */
1084 gtk_widget_realize(main_widgets.window);
1085 #endif
1086 setup_window_position();
1088 /* finally show the window */
1089 gtk_widget_show(main_widgets.window);
1090 main_status.main_window_realized = TRUE;
1092 configuration_apply_settings();
1094 #ifdef HAVE_SOCKET
1095 /* register the callback of socket input */
1096 if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
1098 socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
1099 socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,
1100 G_IO_IN | G_IO_PRI | G_IO_ERR, socket_lock_input_cb, main_widgets.window);
1102 #endif
1104 #ifdef G_OS_WIN32
1106 gchar *dir;
1107 /* On Windows, change the working directory to the Geany installation path to not lock
1108 * the directory of a file passed as command line argument (see bug #2626124). */
1109 dir = win32_get_installation_dir();
1110 win32_set_working_directory(dir);
1111 g_free(dir);
1113 #endif
1115 /* when we are really done with setting everything up and the main event loop is running,
1116 * tell other components, mainly plugins, that startup is complete */
1117 g_idle_add_full(G_PRIORITY_LOW, send_startup_complete, NULL, NULL);
1119 gtk_main();
1120 return 0;
1124 static void queue_free(GQueue *queue)
1126 while (! g_queue_is_empty(queue))
1128 g_free(g_queue_pop_tail(queue));
1130 g_queue_free(queue);
1134 void main_quit()
1136 geany_debug("Quitting...");
1138 #ifdef HAVE_SOCKET
1139 socket_finalize();
1140 #endif
1142 #ifdef HAVE_PLUGINS
1143 plugins_finalize();
1144 #endif
1146 navqueue_free();
1147 keybindings_free();
1148 filetypes_save_commands();
1149 highlighting_free_styles();
1150 templates_free_templates();
1151 msgwin_finalize();
1152 search_finalize();
1153 build_finalize();
1154 document_finalize();
1155 symbols_finalize();
1156 project_finalize();
1157 editor_finalize();
1158 editor_snippets_free();
1159 encodings_finalize();
1160 toolbar_finalize();
1161 sidebar_finalize();
1162 configuration_finalize();
1163 filetypes_free_types();
1164 log_finalize();
1166 tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
1167 g_free(app->configdir);
1168 g_free(app->datadir);
1169 g_free(app->docdir);
1170 g_free(prefs.default_open_path);
1171 g_free(prefs.custom_plugin_path);
1172 g_free(ui_prefs.custom_date_format);
1173 g_free(interface_prefs.editor_font);
1174 g_free(interface_prefs.tagbar_font);
1175 g_free(interface_prefs.msgwin_font);
1176 g_free(editor_prefs.long_line_color);
1177 g_free(editor_prefs.comment_toggle_mark);
1178 g_free(editor_prefs.color_scheme);
1179 g_free(tool_prefs.context_action_cmd);
1180 g_free(template_prefs.developer);
1181 g_free(template_prefs.company);
1182 g_free(template_prefs.mail);
1183 g_free(template_prefs.initials);
1184 g_free(template_prefs.version);
1185 g_free(tool_prefs.term_cmd);
1186 g_free(tool_prefs.browser_cmd);
1187 g_free(tool_prefs.grep_cmd);
1188 g_free(printing_prefs.external_print_cmd);
1189 g_free(printing_prefs.page_header_datefmt);
1190 g_strfreev(ui_prefs.custom_commands);
1192 queue_free(ui_prefs.recent_queue);
1193 queue_free(ui_prefs.recent_projects_queue);
1195 if (ui_widgets.prefs_dialog && GTK_IS_WIDGET(ui_widgets.prefs_dialog)) gtk_widget_destroy(ui_widgets.prefs_dialog);
1196 if (ui_widgets.save_filesel && GTK_IS_WIDGET(ui_widgets.save_filesel)) gtk_widget_destroy(ui_widgets.save_filesel);
1197 if (ui_widgets.open_filesel && GTK_IS_WIDGET(ui_widgets.open_filesel)) gtk_widget_destroy(ui_widgets.open_filesel);
1198 if (ui_widgets.open_fontsel && GTK_IS_WIDGET(ui_widgets.open_fontsel)) gtk_widget_destroy(ui_widgets.open_fontsel);
1199 if (ui_widgets.open_colorsel && GTK_IS_WIDGET(ui_widgets.open_colorsel)) gtk_widget_destroy(ui_widgets.open_colorsel);
1200 #ifdef HAVE_VTE
1201 if (vte_info.have_vte) vte_close();
1202 g_free(vte_info.lib_vte);
1203 g_free(vte_info.dir);
1204 #endif
1205 gtk_widget_destroy(main_widgets.window);
1207 /* destroy popup menus */
1208 if (main_widgets.editor_menu && GTK_IS_WIDGET(main_widgets.editor_menu))
1209 gtk_widget_destroy(main_widgets.editor_menu);
1210 if (ui_widgets.toolbar_menu && GTK_IS_WIDGET(ui_widgets.toolbar_menu))
1211 gtk_widget_destroy(ui_widgets.toolbar_menu);
1212 if (msgwindow.popup_status_menu && GTK_IS_WIDGET(msgwindow.popup_status_menu))
1213 gtk_widget_destroy(msgwindow.popup_status_menu);
1214 if (msgwindow.popup_msg_menu && GTK_IS_WIDGET(msgwindow.popup_msg_menu))
1215 gtk_widget_destroy(msgwindow.popup_msg_menu);
1216 if (msgwindow.popup_compiler_menu && GTK_IS_WIDGET(msgwindow.popup_compiler_menu))
1217 gtk_widget_destroy(msgwindow.popup_compiler_menu);
1219 g_object_unref(geany_object);
1220 geany_object = NULL;
1222 g_free(app);
1224 gtk_main_quit();
1229 * Reloads most of Geany's configuration files without restarting. Currently the following
1230 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1231 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1232 * and 'settings' and 'build_settings' sections of the filetype definition files.
1234 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1235 * editor plugin).
1237 * @since 0.15
1239 void main_reload_configuration(void)
1241 /* reload templates */
1242 templates_free_templates();
1243 templates_init();
1245 /* reload snippets */
1246 editor_snippets_free();
1247 editor_snippets_init();
1249 /* reload filetype extensions */
1250 filetypes_read_extensions();
1252 filetypes_reload();
1254 /* C tag names to ignore */
1255 symbols_reload_config_files();
1257 ui_set_statusbar(TRUE, _("Configuration files reloaded."));