Remove prototypes of non-existing callbacks
[geany-mirror.git] / src / main.c
blobfef03a560a18eefe26eed8ea6d976f723f130c3f
1 /*
2 * main.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: main.h
24 * Main program-related commands.
25 * Handles program initialization and cleanup.
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "main.h"
34 #include "app.h"
35 #include "build.h"
36 #include "callbacks.h"
37 #include "dialogs.h"
38 #include "document.h"
39 #include "encodings.h"
40 #include "filetypes.h"
41 #include "geanyobject.h"
42 #include "highlighting.h"
43 #include "keybindings.h"
44 #include "keyfile.h"
45 #include "log.h"
46 #include "msgwindow.h"
47 #include "navqueue.h"
48 #include "notebook.h"
49 #include "plugins.h"
50 #include "prefs.h"
51 #include "printing.h"
52 #include "sidebar.h"
53 #ifdef HAVE_SOCKET
54 # include "socket.h"
55 #endif
56 #include "support.h"
57 #include "symbols.h"
58 #include "templates.h"
59 #include "toolbar.h"
60 #include "tools.h"
61 #include "ui_utils.h"
62 #include "utils.h"
63 #include "vte.h"
65 #include "gtkcompat.h"
67 #include <signal.h>
68 #include <time.h>
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #include <errno.h>
72 #include <string.h>
73 #include <stdlib.h>
75 #include <glib/gstdio.h>
77 #ifdef HAVE_LOCALE_H
78 # include <locale.h>
79 #endif
82 GeanyApp *app;
83 gboolean ignore_callback; /* hack workaround for GTK+ toggle button callback problem */
85 GeanyStatus main_status;
86 CommandLineOptions cl_options; /* fields initialised in parse_command_line_options */
88 static gchar *original_cwd = NULL;
90 static const gchar geany_lib_versions[] = "GTK %u.%u.%u, GLib %u.%u.%u";
92 static gboolean want_plugins;
94 /* command-line options */
95 static gboolean verbose_mode = FALSE;
96 static gboolean ignore_global_tags = FALSE;
97 static gboolean no_msgwin = FALSE;
98 static gboolean show_version = FALSE;
99 static gchar *alternate_config = NULL;
100 #ifdef HAVE_VTE
101 static gboolean no_vte = FALSE;
102 static gchar *lib_vte = NULL;
103 #endif
104 static gboolean generate_tags = FALSE;
105 static gboolean no_preprocessing = FALSE;
106 static gboolean ft_names = FALSE;
107 static gboolean print_prefix = FALSE;
108 #ifdef HAVE_PLUGINS
109 static gboolean no_plugins = FALSE;
110 #endif
111 static gboolean dummy = FALSE;
113 /* in alphabetical order of short options */
114 static GOptionEntry entries[] =
116 { "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 },
117 { "config", 'c', 0, G_OPTION_ARG_FILENAME, &alternate_config, N_("Use an alternate configuration directory"), NULL },
118 { "ft-names", 0, 0, G_OPTION_ARG_NONE, &ft_names, N_("Print internal filetype names"), NULL },
119 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE, &generate_tags, N_("Generate global tags file (see documentation)"), NULL },
120 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE, &no_preprocessing, N_("Don't preprocess C/C++ files when generating tags"), NULL },
121 #ifdef HAVE_SOCKET
122 { "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 },
123 { "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 },
124 { "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 },
125 #endif
126 { "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL },
127 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
128 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE, &ignore_global_tags, N_("Don't load auto completion data (see documentation)"), NULL },
129 #ifdef HAVE_PLUGINS
130 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE, &no_plugins, N_("Don't load plugins"), NULL },
131 #endif
132 { "print-prefix", 0, 0, G_OPTION_ARG_NONE, &print_prefix, N_("Print Geany's installation prefix"), NULL },
133 { "read-only", 'r', 0, G_OPTION_ARG_NONE, &cl_options.readonly, N_("Open all FILES in read-only mode (see documention)"), NULL },
134 { "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 },
135 #ifdef HAVE_VTE
136 { "no-terminal", 't', 0, G_OPTION_ARG_NONE, &no_vte, N_("Don't load terminal support"), NULL },
137 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME, &lib_vte, N_("Filename of libvte.so"), NULL },
138 #endif
139 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_mode, N_("Be verbose"), NULL },
140 { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Show version and exit"), NULL },
141 { "dummy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &dummy, NULL, NULL }, /* for +NNN line number arguments */
142 { NULL, 0, 0, 0, NULL, NULL, NULL }
146 static void setup_window_position(void)
148 /* interprets the saved window geometry */
149 if (!prefs.save_winpos)
150 return;
152 if (ui_prefs.geometry[0] != -1 && ui_prefs.geometry[1] != -1)
153 gtk_window_move(GTK_WINDOW(main_widgets.window),
154 ui_prefs.geometry[0], ui_prefs.geometry[1]);
156 if (ui_prefs.geometry[2] != -1 && ui_prefs.geometry[3] != -1)
157 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
158 ui_prefs.geometry[2], ui_prefs.geometry[3]);
160 if (ui_prefs.geometry[4] == 1)
161 gtk_window_maximize(GTK_WINDOW(main_widgets.window));
165 /* special things for the initial setup of the checkboxes and related stuff
166 * an action on a setting is only performed if the setting is not equal to the program default
167 * (all the following code is not perfect but it works for the moment) */
168 static void apply_settings(void)
170 ui_update_fold_items();
172 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
173 toolbar_show_hide();
174 if (! ui_prefs.msgwindow_visible)
176 ignore_callback = TRUE;
177 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")), FALSE);
178 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"));
179 ignore_callback = FALSE;
181 if (! ui_prefs.sidebar_visible)
183 ignore_callback = TRUE;
184 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_sidebar1")), FALSE);
185 ignore_callback = FALSE;
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.have_vte)
209 #endif
211 gtk_widget_set_sensitive(
212 ui_lookup_widget(main_widgets.window, "send_selection_to_vte1"), FALSE);
215 if (interface_prefs.sidebar_pos != GTK_POS_LEFT)
216 ui_swap_sidebar_pos();
218 gtk_orientable_set_orientation(GTK_ORIENTABLE(ui_lookup_widget(main_widgets.window, "vpaned1")),
219 interface_prefs.msgwin_orientation);
223 static void main_init(void)
225 /* add our icon path in case we aren't installed in the system prefix */
226 gchar *path;
227 #ifdef G_OS_WIN32
228 gchar *install_dir = win32_get_installation_dir();
229 path = g_build_filename(install_dir, "share", "icons", NULL);
230 g_free(install_dir);
231 #else
232 path = g_build_filename(GEANY_DATADIR, "icons", NULL);
233 #endif
234 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), path);
235 g_free(path);
237 /* inits */
238 ui_init_stock_items();
240 ui_init_builder();
242 main_widgets.window = NULL;
243 app->project = NULL;
244 ui_widgets.open_fontsel = NULL;
245 ui_widgets.open_colorsel = NULL;
246 ui_widgets.prefs_dialog = NULL;
247 main_status.main_window_realized = FALSE;
248 file_prefs.tab_order_ltr = FALSE;
249 file_prefs.tab_order_beside = FALSE;
250 main_status.quitting = FALSE;
251 ignore_callback = FALSE;
252 app->tm_workspace = tm_get_workspace();
253 ui_prefs.recent_queue = g_queue_new();
254 ui_prefs.recent_projects_queue = g_queue_new();
255 main_status.opening_session_files = FALSE;
257 main_widgets.window = create_window1();
259 /* add recent projects to the Project menu */
260 ui_widgets.recent_projects_menuitem = ui_lookup_widget(main_widgets.window, "recent_projects1");
261 ui_widgets.recent_projects_menu_menubar = gtk_menu_new();
262 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_projects_menuitem),
263 ui_widgets.recent_projects_menu_menubar);
265 /* store important pointers for later reference */
266 main_widgets.toolbar = toolbar_init();
267 main_widgets.sidebar_notebook = ui_lookup_widget(main_widgets.window, "notebook3");
268 main_widgets.notebook = ui_lookup_widget(main_widgets.window, "notebook1");
269 main_widgets.editor_menu = create_edit_menu1();
270 main_widgets.tools_menu = ui_lookup_widget(main_widgets.window, "tools1_menu");
271 main_widgets.message_window_notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
272 main_widgets.project_menu = ui_lookup_widget(main_widgets.window, "menu_project1_menu");
274 ui_widgets.toolbar_menu = create_toolbar_popup_menu1();
275 ui_init();
277 /* set widget names for matching with .gtkrc-2.0 */
278 gtk_widget_set_name(main_widgets.window, "GeanyMainWindow");
279 gtk_widget_set_name(ui_widgets.toolbar_menu, "GeanyToolbarMenu");
280 gtk_widget_set_name(main_widgets.editor_menu, "GeanyEditMenu");
281 gtk_widget_set_name(ui_lookup_widget(main_widgets.window, "menubar1"), "GeanyMenubar");
282 gtk_widget_set_name(main_widgets.toolbar, "GeanyToolbar");
284 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
285 GEANY_WINDOW_DEFAULT_WIDTH, GEANY_WINDOW_DEFAULT_HEIGHT);
289 const gchar *main_get_version_string(void)
291 static gchar full[] = VERSION " (git >= " REVISION ")";
293 if (utils_str_equal(REVISION, "-1"))
294 return VERSION;
295 else
296 return full;
300 /* get the full file path of a command-line argument
301 * N.B. the result should be freed and may contain '/../' or '/./ ' */
302 gchar *main_get_argv_filename(const gchar *filename)
304 gchar *result;
306 if (g_path_is_absolute(filename) || utils_is_uri(filename))
307 result = g_strdup(filename);
308 else
310 /* use current dir */
311 gchar *cur_dir = NULL;
312 if (original_cwd == NULL)
313 cur_dir = g_get_current_dir();
314 else
315 cur_dir = g_strdup(original_cwd);
317 result = g_strjoin(
318 G_DIR_SEPARATOR_S, cur_dir, filename, NULL);
319 g_free(cur_dir);
321 return result;
325 /* get a :line:column specifier from the end of a filename (if present),
326 * return the line/column values, and remove the specifier from the string
327 * (Note that *line and *column must both be set to -1 initially) */
328 static void get_line_and_column_from_filename(gchar *filename, gint *line, gint *column)
330 gsize i;
331 gint colon_count = 0;
332 gboolean have_number = FALSE;
333 gsize len;
335 g_assert(*line == -1 && *column == -1);
337 if (G_UNLIKELY(EMPTY(filename)))
338 return;
340 /* allow to open files like "test:0" */
341 if (g_file_test(filename, G_FILE_TEST_EXISTS))
342 return;
344 len = strlen(filename);
345 for (i = len - 1; i >= 1; i--)
347 gboolean is_colon = filename[i] == ':';
348 gboolean is_digit = g_ascii_isdigit(filename[i]);
350 if (! is_colon && ! is_digit)
351 break;
353 if (is_colon)
355 if (++colon_count > 1)
356 break; /* bail on 2+ colons in a row */
358 else
359 colon_count = 0;
361 if (is_digit)
362 have_number = TRUE;
364 if (is_colon && have_number)
366 gint number = atoi(&filename[i + 1]);
368 filename[i] = '\0';
369 have_number = FALSE;
371 *column = *line;
372 *line = number;
375 if (*column >= 0)
376 break; /* line and column are set, so we're done */
381 #ifdef G_OS_WIN32
382 static void change_working_directory_on_windows(void)
384 gchar *install_dir = win32_get_installation_dir();
386 /* remember original working directory for use with opening files from the command line */
387 original_cwd = g_get_current_dir();
389 /* On Windows, change the working directory to the Geany installation path to not lock
390 * the directory of a file passed as command line argument (see bug #2626124).
391 * This also helps if plugins or other code uses relative paths to load
392 * any additional resources (e.g. share/geany-plugins/...). */
393 win32_set_working_directory(install_dir);
395 g_free(install_dir);
397 #endif
400 static void setup_paths(void)
402 gchar *data_dir;
403 gchar *doc_dir;
405 /* set paths */
406 #ifdef G_OS_WIN32
407 /* use the installation directory(the one where geany.exe is located) as the base for the
408 * documentation and data files */
409 gchar *install_dir = win32_get_installation_dir();
411 data_dir = g_build_filename(install_dir, "data", NULL); /* e.g. C:\Program Files\geany\data */
412 doc_dir = g_build_filename(install_dir, "doc", NULL);
414 g_free(install_dir);
415 #else
416 data_dir = g_build_filename(GEANY_DATADIR, "geany", NULL); /* e.g. /usr/share/geany */
417 doc_dir = g_build_filename(GEANY_DOCDIR, "html", NULL);
418 #endif
420 /* convert path names to locale encoding */
421 app->datadir = utils_get_locale_from_utf8(data_dir);
422 app->docdir = utils_get_locale_from_utf8(doc_dir);
424 g_free(data_dir);
425 g_free(doc_dir);
430 * Checks whether the main window has been realized.
431 * This is an easy indicator whether Geany is right now starting up (main window is not
432 * yet realized) or whether it has finished the startup process (main window is realized).
433 * This is because the main window is realized (i.e. actually drawn on the screen) at the
434 * end of the startup process.
436 * @note Maybe you want to use the @link pluginsignals.c @c "geany-startup-complete" signal @endlink
437 * to get notified about the completed startup process.
439 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
441 * @since 0.19
443 gboolean main_is_realized(void)
445 return main_status.main_window_realized;
450 * Initialises the gettext translation system.
451 * This is a convenience function to set up gettext for internationalisation support
452 * in external plugins. You should call this function early in @ref plugin_init().
453 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
454 * The codeset for the message translations is set to UTF-8.
456 * Note that this function only setups the gettext textdomain for you. You still have
457 * to adjust the build system of your plugin to get internationalisation support
458 * working properly.
460 * If you have already used @ref PLUGIN_SET_TRANSLATABLE_INFO() you
461 * don't need to call main_locale_init() again as it has already been done.
463 * @param locale_dir The location where the translation files should be searched. This is
464 * usually the @c LOCALEDIR macro, defined by the build system.
465 * E.g. @c $prefix/share/locale.
466 * Only used on non-Windows systems. On Windows, the directory is determined
467 * by @c g_win32_get_package_installation_directory().
468 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
469 * defined by the build system.
471 * @since 0.16
473 void main_locale_init(const gchar *locale_dir, const gchar *package)
475 gchar *l_locale_dir = NULL;
477 #ifdef HAVE_LOCALE_H
478 setlocale(LC_ALL, "");
479 #endif
481 #ifdef G_OS_WIN32
483 gchar *install_dir = win32_get_installation_dir();
484 /* e.g. C:\Program Files\geany\lib\locale */
485 l_locale_dir = g_build_filename(install_dir, "share", "locale", NULL);
486 g_free(install_dir);
488 #else
489 l_locale_dir = g_strdup(locale_dir);
490 #endif
492 (void) bindtextdomain(package, l_locale_dir);
493 (void) bind_textdomain_codeset(package, "UTF-8");
494 g_free(l_locale_dir);
498 static void print_filetypes(void)
500 const GSList *list, *node;
502 filetypes_init_types();
503 printf("Geany's filetype names:\n");
505 list = filetypes_get_sorted_by_name();
506 foreach_slist(node, list)
508 GeanyFiletype *ft = node->data;
510 printf("%s\n", ft->name);
512 filetypes_free_types();
516 static void wait_for_input_on_windows(void)
518 #ifdef G_OS_WIN32
519 if (verbose_mode)
521 geany_debug("Press any key to continue");
522 getchar();
524 #endif
528 static void parse_command_line_options(gint *argc, gchar ***argv)
530 GError *error = NULL;
531 GOptionContext *context;
532 gint i;
533 CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE, FALSE, FALSE};
535 /* first initialise cl_options fields with default values */
536 cl_options = def_clo;
538 /* the GLib option parser can't handle the +NNN (line number) option,
539 * so we grab that here and replace it with a no-op */
540 for (i = 1; i < (*argc); i++)
542 if ((*argv)[i][0] != '+')
543 continue;
545 cl_options.goto_line = atoi((*argv)[i] + 1);
546 (*argv)[i] = (gchar *) "--dummy";
549 context = g_option_context_new(_("[FILES...]"));
551 g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
552 g_option_group_set_translation_domain(g_option_context_get_main_group(context), GETTEXT_PACKAGE);
553 g_option_context_add_group(context, gtk_get_option_group(FALSE));
554 g_option_context_parse(context, argc, argv, &error);
555 g_option_context_free(context);
557 if (error != NULL)
559 g_printerr("Geany: %s\n", error->message);
560 g_error_free(error);
561 exit(1);
564 app->debug_mode = verbose_mode;
565 if (app->debug_mode)
567 /* Since GLib 2.32 messages logged with levels INFO and DEBUG aren't output by the
568 * default log handler unless the G_MESSAGES_DEBUG environment variable contains the
569 * domain of the message or is set to the special value "all" */
570 g_setenv("G_MESSAGES_DEBUG", "all", FALSE);
573 #ifdef G_OS_WIN32
574 win32_init_debug_code();
575 #endif
577 if (show_version)
579 gchar *build_date = utils_parse_and_format_build_date(__DATE__);
581 printf(PACKAGE " %s (", main_get_version_string());
582 /* note for translators: library versions are printed after this */
583 printf(_("built on %s with "), build_date);
584 printf(geany_lib_versions,
585 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
586 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
587 printf(")\n");
588 g_free(build_date);
589 wait_for_input_on_windows();
590 exit(0);
593 if (print_prefix)
595 printf("%s\n", GEANY_PREFIX);
596 printf("%s\n", GEANY_DATADIR);
597 printf("%s\n", GEANY_LIBDIR);
598 printf("%s\n", GEANY_LOCALEDIR);
599 wait_for_input_on_windows();
600 exit(0);
603 if (alternate_config)
605 geany_debug("alternate config: %s", alternate_config);
606 app->configdir = alternate_config;
608 else
610 app->configdir = g_build_filename(g_get_user_config_dir(), "geany", NULL);
613 if (generate_tags)
615 gboolean ret;
617 filetypes_init_types();
618 ret = symbols_generate_global_tags(*argc, *argv, ! no_preprocessing);
619 filetypes_free_types();
620 wait_for_input_on_windows();
621 exit(ret);
624 if (ft_names)
626 print_filetypes();
627 wait_for_input_on_windows();
628 exit(0);
631 #ifdef HAVE_SOCKET
632 socket_info.ignore_socket = cl_options.new_instance;
633 if (cl_options.socket_filename)
635 socket_info.file_name = cl_options.socket_filename;
637 #endif
639 #ifdef HAVE_VTE
640 vte_info.lib_vte = lib_vte;
641 #endif
642 cl_options.ignore_global_tags = ignore_global_tags;
644 if (! gtk_init_check(NULL, NULL))
645 { /* check whether we have a valid X display and exit if not */
646 g_printerr("Geany: cannot open display\n");
647 exit(1);
652 static gint create_config_dir(void)
654 gint saved_errno = 0;
655 gchar *conf_file = NULL;
656 gchar *filedefs_dir = NULL;
657 gchar *templates_dir = NULL;
659 if (! g_file_test(app->configdir, G_FILE_TEST_EXISTS))
661 #ifndef G_OS_WIN32
662 /* if we are *not* using an alternate config directory, we check whether the old one
663 * in ~/.geany still exists and try to move it */
664 if (alternate_config == NULL)
666 gchar *old_dir = g_build_filename(g_get_home_dir(), ".geany", NULL);
667 /* move the old config dir if it exists */
668 if (g_file_test(old_dir, G_FILE_TEST_EXISTS))
670 if (! dialogs_show_question_full(main_widgets.window,
671 GTK_STOCK_YES, GTK_STOCK_QUIT, _("Move it now?"),
672 "%s",
673 _("Geany needs to move your old configuration directory before starting.")))
674 exit(0);
676 if (! g_file_test(app->configdir, G_FILE_TEST_IS_DIR))
677 utils_mkdir(app->configdir, TRUE);
679 if (g_rename(old_dir, app->configdir) == 0)
681 dialogs_show_msgbox(GTK_MESSAGE_INFO,
682 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
683 old_dir, app->configdir);
684 g_free(old_dir);
685 return 0;
687 else
689 dialogs_show_msgbox(GTK_MESSAGE_WARNING,
690 /* for translators: the third %s in brackets is the error message which
691 * describes why moving the dir didn't work */
692 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
693 "Please move manually the directory to the new location."),
694 old_dir, app->configdir, g_strerror(errno));
697 g_free(old_dir);
699 #endif
700 geany_debug("creating config directory %s", app->configdir);
701 saved_errno = utils_mkdir(app->configdir, TRUE);
704 conf_file = g_build_filename(app->configdir, "geany.conf", NULL);
705 filedefs_dir = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
706 templates_dir = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
708 if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS))
709 { /* check whether geany.conf can be written */
710 saved_errno = utils_is_file_writable(app->configdir);
713 /* make subdir for filetype definitions */
714 if (saved_errno == 0)
716 gchar *filedefs_readme = g_build_filename(app->configdir,
717 GEANY_FILEDEFS_SUBDIR, "filetypes.README", NULL);
719 if (! g_file_test(filedefs_dir, G_FILE_TEST_EXISTS))
721 saved_errno = utils_mkdir(filedefs_dir, FALSE);
723 if (saved_errno == 0 && ! g_file_test(filedefs_readme, G_FILE_TEST_EXISTS))
725 gchar *text = g_strconcat(
726 "Copy files from ", app->datadir, " to this directory to overwrite "
727 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
728 "the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").", NULL);
729 utils_write_file(filedefs_readme, text);
730 g_free(text);
732 g_free(filedefs_readme);
735 /* make subdir for template files */
736 if (saved_errno == 0)
738 gchar *templates_readme = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR,
739 "templates.README", NULL);
741 if (! g_file_test(templates_dir, G_FILE_TEST_EXISTS))
743 saved_errno = utils_mkdir(templates_dir, FALSE);
745 if (saved_errno == 0 && ! g_file_test(templates_readme, G_FILE_TEST_EXISTS))
747 gchar *text = g_strconcat(
748 "There are several template files in this directory. For these templates you can use wildcards.\n\
749 For more information read the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").",
750 NULL);
751 utils_write_file(templates_readme, text);
752 g_free(text);
754 g_free(templates_readme);
757 g_free(filedefs_dir);
758 g_free(templates_dir);
759 g_free(conf_file);
761 return saved_errno;
765 /* Returns 0 if config dir is OK. */
766 static gint setup_config_dir(void)
768 gint mkdir_result = 0;
770 /* convert configdir to locale encoding to avoid troubles */
771 SETPTR(app->configdir, utils_get_locale_from_utf8(app->configdir));
773 mkdir_result = create_config_dir();
774 if (mkdir_result != 0)
776 if (! dialogs_show_question(
777 _("Configuration directory could not be created (%s).\nThere could be some problems "
778 "using Geany without a configuration directory.\nStart Geany anyway?"),
779 g_strerror(mkdir_result)))
781 exit(0);
784 /* make configdir a real path */
785 if (g_file_test(app->configdir, G_FILE_TEST_EXISTS))
786 SETPTR(app->configdir, tm_get_real_path(app->configdir));
788 return mkdir_result;
791 /* Signal handling removed since on_exit_clicked() uses functions that are
792 * illegal in signal handlers
793 static void signal_cb(gint sig)
795 if (sig == SIGTERM)
797 on_exit_clicked(NULL, NULL);
802 /* Used for command-line arguments at startup or from socket.
803 * this will strip any :line:col filename suffix from locale_filename */
804 gboolean main_handle_filename(const gchar *locale_filename)
806 GeanyDocument *doc;
807 gint line = -1, column = -1;
808 gchar *filename;
810 g_return_val_if_fail(locale_filename, FALSE);
812 /* check whether the passed filename is an URI */
813 filename = utils_get_path_from_uri(locale_filename);
814 if (filename == NULL)
815 return FALSE;
817 get_line_and_column_from_filename(filename, &line, &column);
818 if (line >= 0)
819 cl_options.goto_line = line;
820 if (column >= 0)
821 cl_options.goto_column = column;
823 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
825 doc = document_open_file(filename, cl_options.readonly, NULL, NULL);
826 /* add recent file manually if opening_session_files is set */
827 if (doc != NULL && main_status.opening_session_files)
828 ui_add_recent_document(doc);
829 g_free(filename);
830 return TRUE;
832 else if (file_prefs.cmdline_new_files)
833 { /* create new file with the given filename */
834 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
836 doc = document_new_file(utf8_filename, NULL, NULL);
837 if (doc != NULL)
838 ui_add_recent_document(doc);
839 g_free(utf8_filename);
840 g_free(filename);
841 return TRUE;
843 g_free(filename);
844 return FALSE;
848 /* open files from command line */
849 static void open_cl_files(gint argc, gchar **argv)
851 gint i;
853 for (i = 1; i < argc; i++)
855 gchar *filename = main_get_argv_filename(argv[i]);
857 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
859 g_free(filename);
860 continue;
863 #ifdef G_OS_WIN32
864 /* It seems argv elements are encoded in CP1252 on a German Windows */
865 SETPTR(filename, g_locale_to_utf8(filename, -1, NULL, NULL, NULL));
866 #endif
867 if (filename && ! main_handle_filename(filename))
869 const gchar *msg = _("Could not find file '%s'.");
871 g_printerr(msg, filename); /* also print to the terminal */
872 g_printerr("\n");
873 ui_set_statusbar(TRUE, msg, filename);
875 g_free(filename);
880 static void load_session_project_file(void)
882 gchar *locale_filename;
884 g_return_if_fail(project_prefs.session_file != NULL);
886 locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
888 if (G_LIKELY(!EMPTY(locale_filename)))
889 project_load_file(locale_filename);
891 g_free(locale_filename);
892 g_free(project_prefs.session_file); /* no longer needed */
896 static void load_settings(void)
898 configuration_load();
899 /* let cmdline options overwrite configuration settings */
900 #ifdef HAVE_VTE
901 vte_info.have_vte = (no_vte) ? FALSE : vte_info.load_vte;
902 #endif
903 if (no_msgwin)
904 ui_prefs.msgwindow_visible = FALSE;
906 #ifdef HAVE_PLUGINS
907 want_plugins = prefs.load_plugins && !no_plugins;
908 #endif
912 void main_load_project_from_command_line(const gchar *locale_filename, gboolean use_session)
914 gchar *pfile;
916 pfile = utils_get_path_from_uri(locale_filename);
917 if (pfile != NULL)
919 if (use_session)
920 project_load_file_with_session(pfile);
921 else
922 project_load_file(pfile);
924 g_free(pfile);
928 static void load_startup_files(gint argc, gchar **argv)
930 gboolean load_session = FALSE;
932 if (argc > 1 && g_str_has_suffix(argv[1], ".geany"))
934 /* project file specified: load it, but decide the session later */
935 main_load_project_from_command_line(argv[1], FALSE);
936 argc--, argv++;
937 /* force session load if using project-based session files */
938 load_session = project_prefs.project_session;
941 /* Load the default session if:
942 * 1. "Load files from the last session" is active.
943 * 2. --no-session is not specified.
944 * 3. We are a primary instance.
945 * Has no effect if a CL project is loaded and using project-based session files. */
946 if (prefs.load_session && cl_options.load_session && !cl_options.new_instance)
948 if (app->project == NULL)
949 load_session_project_file();
950 load_session = TRUE;
953 if (load_session)
955 /* load session files into tabs, as they are found in the session_files variable */
956 configuration_open_files();
958 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
960 ui_update_popup_copy_items(NULL);
961 ui_update_popup_reundo_items(NULL);
965 open_cl_files(argc, argv);
969 static gboolean send_startup_complete(gpointer data)
971 g_signal_emit_by_name(geany_object, "geany-startup-complete");
972 return FALSE;
976 static const gchar *get_locale(void)
978 const gchar *locale = "unknown";
979 #ifdef HAVE_LOCALE_H
980 locale = setlocale(LC_CTYPE, NULL);
981 #endif
982 return locale;
986 #if ! GTK_CHECK_VERSION(3, 0, 0)
987 /* This prepends our own gtkrc file to the list of RC files to be loaded by GTK at startup.
988 * This function *has* to be called before gtk_init().
990 * We have a custom RC file defining various styles we need, and we want the user to be
991 * able to override them (e.g. if they want -- or need -- other colors). Fair enough, one
992 * would simply call gtk_rc_parse() with the appropriate filename. However, the styling
993 * rules applies in the order they are loaded, then if we load our styles after GTK has
994 * loaded the user's ones we'd override them.
996 * There are 2 solutions to fix this:
997 * 1) set our styles' priority to something with lower than "user" (actually "theme"
998 * priority because rules precedence are first calculated depending on the priority
999 * no matter of how precise the rules is, so we need to override the theme).
1000 * 2) prepend our custom style to GTK's list while keeping priority to user (which is the
1001 * default), so it gets loaded before real user's ones and so gets overridden by them.
1003 * One would normally go for 1 because it's ways simpler and requires less code: you just
1004 * have to add the priorities to your styles, which is a matter of adding a few ":theme" in
1005 * the RC file. However, KDE being a bitch it doesn't set the gtk-theme-name but rather
1006 * directly includes the style to use in a user gtkrc file, which makes the theme have
1007 * "user" priority, hence overriding our styles. So, we cannot set priorities in the RC
1008 * file if we want to support running under KDE, which pretty much leave us with no choice
1009 * but to go with solution 2, which unfortunately requires writing ugly code since GTK
1010 * don't have a gtk_rc_prepend_default_file() function. Thank you very much KDE.
1012 * Though, as a side benefit it also makes the code work with people using gtk-chtheme,
1013 * which also found it funny to include the theme in the user RC file. */
1014 static void setup_gtk2_styles(void)
1016 gchar **gtk_files = gtk_rc_get_default_files();
1017 gchar **new_files = g_malloc(sizeof *new_files * (g_strv_length(gtk_files) + 2));
1018 guint i = 0;
1020 new_files[i++] = g_build_filename(app->datadir, "geany.gtkrc", NULL);
1021 for (; *gtk_files; gtk_files++)
1022 new_files[i++] = g_strdup(*gtk_files);
1023 new_files[i] = NULL;
1025 gtk_rc_set_default_files(new_files);
1027 g_strfreev(new_files);
1029 #endif
1032 gint main(gint argc, gchar **argv)
1034 GeanyDocument *doc;
1035 gint config_dir_result;
1036 const gchar *locale;
1038 #if ! GLIB_CHECK_VERSION(2, 36, 0)
1039 g_type_init();
1040 #endif
1042 log_handlers_init();
1044 app = g_new0(GeanyApp, 1);
1045 memset(&main_status, 0, sizeof(GeanyStatus));
1046 memset(&prefs, 0, sizeof(GeanyPrefs));
1047 memset(&interface_prefs, 0, sizeof(GeanyInterfacePrefs));
1048 memset(&toolbar_prefs, 0, sizeof(GeanyToolbarPrefs));
1049 memset(&file_prefs, 0, sizeof(GeanyFilePrefs));
1050 memset(&search_prefs, 0, sizeof(GeanySearchPrefs));
1051 memset(&tool_prefs, 0, sizeof(GeanyToolPrefs));
1052 memset(&template_prefs, 0, sizeof(GeanyTemplatePrefs));
1053 memset(&ui_prefs, 0, sizeof(UIPrefs));
1054 memset(&ui_widgets, 0, sizeof(UIWidgets));
1056 setup_paths();
1057 #if ! GTK_CHECK_VERSION(3, 0, 0)
1058 setup_gtk2_styles();
1059 #endif
1060 #ifdef ENABLE_NLS
1061 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
1062 #endif
1063 parse_command_line_options(&argc, &argv);
1065 #if ! GLIB_CHECK_VERSION(2, 32, 0)
1066 /* Initialize GLib's thread system in case any plugins want to use it or their
1067 * dependencies (e.g. WebKit, Soup, ...). Deprecated since GLIB 2.32. */
1068 if (!g_thread_supported())
1069 g_thread_init(NULL);
1070 #endif
1072 /* removed as signal handling was wrong, see signal_cb()
1073 signal(SIGTERM, signal_cb); */
1075 #ifdef G_OS_UNIX
1076 /* ignore SIGPIPE signal for preventing sudden death of program */
1077 signal(SIGPIPE, SIG_IGN);
1078 #endif
1080 config_dir_result = setup_config_dir();
1081 #ifdef HAVE_SOCKET
1082 /* check and create (unix domain) socket for remote operation */
1083 if (! socket_info.ignore_socket)
1085 socket_info.lock_socket = -1;
1086 socket_info.lock_socket_tag = 0;
1087 socket_info.lock_socket = socket_init(argc, argv);
1088 /* Quit if filenames were sent to first instance or the list of open
1089 * documents has been printed */
1090 if ((socket_info.lock_socket == -2 /* socket exists */ && argc > 1) ||
1091 cl_options.list_documents)
1093 socket_finalize();
1094 gdk_notify_startup_complete();
1095 g_free(app->configdir);
1096 g_free(app->datadir);
1097 g_free(app->docdir);
1098 g_free(app);
1099 return 0;
1101 /* Start a new instance if no command line strings were passed,
1102 * even if the socket already exists */
1103 else if (socket_info.lock_socket == -2 /* socket already exists */)
1105 socket_info.ignore_socket = TRUE;
1106 cl_options.new_instance = TRUE;
1109 #endif
1111 #ifdef G_OS_WIN32
1112 /* after we initialized the socket code and handled command line args,
1113 * let's change the working directory on Windows to not lock it */
1114 change_working_directory_on_windows();
1115 #endif
1117 locale = get_locale();
1118 geany_debug("Geany %s, %s",
1119 main_get_version_string(),
1120 locale);
1121 geany_debug(geany_lib_versions,
1122 gtk_major_version, gtk_minor_version, gtk_micro_version,
1123 glib_major_version, glib_minor_version, glib_micro_version);
1124 geany_debug("System data dir: %s", app->datadir);
1125 geany_debug("User config dir: %s", app->configdir);
1127 /* create the object so Geany signals can be connected in init() functions */
1128 geany_object = geany_object_new();
1130 /* inits */
1131 main_init();
1133 encodings_init();
1134 editor_init();
1136 /* init stash groups before loading keyfile */
1137 configuration_init();
1138 ui_init_prefs();
1139 search_init();
1140 project_init();
1141 #ifdef HAVE_PLUGINS
1142 plugins_init();
1143 #endif
1144 sidebar_init();
1145 load_settings(); /* load keyfile */
1147 msgwin_init();
1148 build_init();
1149 ui_create_insert_menu_items();
1150 ui_create_insert_date_menu_items();
1151 keybindings_init();
1152 notebook_init();
1153 filetypes_init();
1154 templates_init();
1155 navqueue_init();
1156 document_init_doclist();
1157 symbols_init();
1158 editor_snippets_init();
1160 /* registering some basic events */
1161 g_signal_connect(main_widgets.window, "delete-event", G_CALLBACK(on_exit_clicked), NULL);
1162 g_signal_connect(main_widgets.window, "window-state-event", G_CALLBACK(on_window_state_event), NULL);
1164 g_signal_connect(msgwindow.scribble, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
1166 #ifdef HAVE_VTE
1167 vte_init();
1168 #endif
1169 ui_create_recent_menus();
1171 ui_set_statusbar(TRUE, _("This is Geany %s."), main_get_version_string());
1172 if (config_dir_result != 0)
1173 ui_set_statusbar(TRUE, _("Configuration directory could not be created (%s)."),
1174 g_strerror(config_dir_result));
1176 /* apply all configuration options */
1177 apply_settings();
1179 #ifdef HAVE_PLUGINS
1180 /* load any enabled plugins before we open any documents */
1181 if (want_plugins)
1182 plugins_load_active();
1183 #endif
1185 ui_sidebar_show_hide();
1187 /* set the active sidebar page after plugins have been loaded */
1188 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), ui_prefs.sidebar_page);
1190 /* load keybinding settings after plugins have added their groups */
1191 keybindings_load_keyfile();
1193 /* create the custom command menu after the keybindings have been loaded to have the proper
1194 * accelerator shown for the menu items */
1195 tools_create_insert_custom_command_menu_items();
1197 /* load any command line files or session files */
1198 main_status.opening_session_files = TRUE;
1199 load_startup_files(argc, argv);
1200 main_status.opening_session_files = FALSE;
1202 /* open a new file if no other file was opened */
1203 document_new_file_if_non_open();
1205 ui_document_buttons_update();
1206 ui_save_buttons_toggle(FALSE);
1208 doc = document_get_current();
1209 sidebar_select_openfiles_item(doc);
1210 build_menu_update(doc);
1211 sidebar_update_tag_list(doc, FALSE);
1213 #ifdef G_OS_WIN32
1214 /* Manually realise the main window to be able to set the position but don't show it.
1215 * We don't set the position after showing the window to avoid flickering. */
1216 gtk_widget_realize(main_widgets.window);
1217 #endif
1218 setup_window_position();
1220 /* finally show the window */
1221 document_grab_focus(doc);
1222 gtk_widget_show(main_widgets.window);
1223 main_status.main_window_realized = TRUE;
1225 configuration_apply_settings();
1227 #ifdef HAVE_SOCKET
1228 /* register the callback of socket input */
1229 if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
1231 socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
1232 socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,
1233 G_IO_IN | G_IO_PRI | G_IO_ERR, socket_lock_input_cb, main_widgets.window);
1235 #endif
1237 /* when we are really done with setting everything up and the main event loop is running,
1238 * tell other components, mainly plugins, that startup is complete */
1239 g_idle_add_full(G_PRIORITY_LOW, send_startup_complete, NULL, NULL);
1241 gtk_main();
1242 return 0;
1246 static void queue_free(GQueue *queue)
1248 while (! g_queue_is_empty(queue))
1250 g_free(g_queue_pop_tail(queue));
1252 g_queue_free(queue);
1256 void main_quit(void)
1258 geany_debug("Quitting...");
1260 #ifdef HAVE_SOCKET
1261 socket_finalize();
1262 #endif
1264 #ifdef HAVE_PLUGINS
1265 plugins_finalize();
1266 #endif
1268 navqueue_free();
1269 keybindings_free();
1270 notebook_free();
1271 highlighting_free_styles();
1272 templates_free_templates();
1273 msgwin_finalize();
1274 search_finalize();
1275 build_finalize();
1276 document_finalize();
1277 symbols_finalize();
1278 project_finalize();
1279 editor_finalize();
1280 editor_snippets_free();
1281 encodings_finalize();
1282 toolbar_finalize();
1283 sidebar_finalize();
1284 configuration_finalize();
1285 filetypes_free_types();
1286 log_finalize();
1288 tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
1289 g_free(app->configdir);
1290 g_free(app->datadir);
1291 g_free(app->docdir);
1292 g_free(prefs.default_open_path);
1293 g_free(prefs.custom_plugin_path);
1294 g_free(ui_prefs.custom_date_format);
1295 g_free(interface_prefs.editor_font);
1296 g_free(interface_prefs.tagbar_font);
1297 g_free(interface_prefs.msgwin_font);
1298 g_free(editor_prefs.long_line_color);
1299 g_free(editor_prefs.comment_toggle_mark);
1300 g_free(editor_prefs.color_scheme);
1301 g_free(tool_prefs.context_action_cmd);
1302 g_free(template_prefs.developer);
1303 g_free(template_prefs.company);
1304 g_free(template_prefs.mail);
1305 g_free(template_prefs.initials);
1306 g_free(template_prefs.version);
1307 g_free(tool_prefs.term_cmd);
1308 g_free(tool_prefs.browser_cmd);
1309 g_free(tool_prefs.grep_cmd);
1310 g_free(printing_prefs.external_print_cmd);
1311 g_free(printing_prefs.page_header_datefmt);
1312 g_strfreev(ui_prefs.custom_commands);
1313 g_strfreev(ui_prefs.custom_commands_labels);
1315 queue_free(ui_prefs.recent_queue);
1316 queue_free(ui_prefs.recent_projects_queue);
1318 if (ui_widgets.prefs_dialog && GTK_IS_WIDGET(ui_widgets.prefs_dialog)) gtk_widget_destroy(ui_widgets.prefs_dialog);
1319 if (ui_widgets.open_fontsel && GTK_IS_WIDGET(ui_widgets.open_fontsel)) gtk_widget_destroy(ui_widgets.open_fontsel);
1320 if (ui_widgets.open_colorsel && GTK_IS_WIDGET(ui_widgets.open_colorsel)) gtk_widget_destroy(ui_widgets.open_colorsel);
1321 #ifdef HAVE_VTE
1322 if (vte_info.have_vte) vte_close();
1323 g_free(vte_info.lib_vte);
1324 g_free(vte_info.dir);
1325 #endif
1326 gtk_widget_destroy(main_widgets.window);
1328 /* destroy popup menus */
1329 if (main_widgets.editor_menu && GTK_IS_WIDGET(main_widgets.editor_menu))
1330 gtk_widget_destroy(main_widgets.editor_menu);
1331 if (ui_widgets.toolbar_menu && GTK_IS_WIDGET(ui_widgets.toolbar_menu))
1332 gtk_widget_destroy(ui_widgets.toolbar_menu);
1333 if (msgwindow.popup_status_menu && GTK_IS_WIDGET(msgwindow.popup_status_menu))
1334 gtk_widget_destroy(msgwindow.popup_status_menu);
1335 if (msgwindow.popup_msg_menu && GTK_IS_WIDGET(msgwindow.popup_msg_menu))
1336 gtk_widget_destroy(msgwindow.popup_msg_menu);
1337 if (msgwindow.popup_compiler_menu && GTK_IS_WIDGET(msgwindow.popup_compiler_menu))
1338 gtk_widget_destroy(msgwindow.popup_compiler_menu);
1340 g_object_unref(geany_object);
1341 geany_object = NULL;
1343 g_free(original_cwd);
1344 g_free(app);
1346 ui_finalize_builder();
1348 gtk_main_quit();
1353 * Reloads most of Geany's configuration files without restarting. Currently the following
1354 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1355 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1356 * and 'settings' and 'build_settings' sections of the filetype definition files.
1358 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1359 * editor plugin).
1361 * @since 0.15
1363 void main_reload_configuration(void)
1365 /* reload templates */
1366 templates_free_templates();
1367 templates_init();
1369 /* reload snippets */
1370 editor_snippets_free();
1371 editor_snippets_init();
1373 filetypes_reload_extensions();
1374 filetypes_reload();
1376 /* C tag names to ignore */
1377 symbols_reload_config_files();
1379 ui_set_statusbar(TRUE, _("Configuration files reloaded."));