Fixup various filedefs mappings
[geany-mirror.git] / src / main.c
blob0fd6e10f9f252a75c8d63fbd5fa5fc7b808ac706
1 /*
2 * main.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 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.
22 /**
23 * @file: main.h
24 * Main program-related commands.
25 * Handles program initialization and cleanup.
28 #include <signal.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <stdlib.h>
36 #include "geany.h"
37 #include <glib/gstdio.h>
39 #ifdef HAVE_LOCALE_H
40 # include <locale.h>
41 #endif
43 #include "main.h"
44 #include "prefix.h"
45 #include "prefs.h"
46 #include "interface.h"
47 #include "support.h"
48 #include "callbacks.h"
49 #include "log.h"
50 #include "ui_utils.h"
51 #include "utils.h"
52 #include "document.h"
53 #include "filetypes.h"
54 #include "keyfile.h"
55 #include "win32.h"
56 #include "msgwindow.h"
57 #include "dialogs.h"
58 #include "templates.h"
59 #include "encodings.h"
60 #include "sidebar.h"
61 #include "notebook.h"
62 #include "keybindings.h"
63 #include "editor.h"
64 #include "search.h"
65 #include "build.h"
66 #include "highlighting.h"
67 #include "symbols.h"
68 #include "project.h"
69 #include "tools.h"
70 #include "navqueue.h"
71 #include "plugins.h"
72 #include "printing.h"
73 #include "toolbar.h"
74 #include "geanyobject.h"
76 #ifdef HAVE_SOCKET
77 # include "socket.h"
78 #endif
80 #ifdef HAVE_VTE
81 # include "vte.h"
82 #endif
84 #ifndef N_
85 # define N_(String) (String)
86 #endif
89 GeanyApp *app;
90 gboolean ignore_callback; /* hack workaround for GTK+ toggle button callback problem */
92 GeanyStatus main_status;
93 CommandLineOptions cl_options; /* fields initialised in parse_command_line_options */
96 static const gchar geany_lib_versions[] = "GTK %u.%u.%u, GLib %u.%u.%u"
97 #ifdef USE_INCLUDED_REGEX
98 ", built-in regex"
99 #endif
102 static gboolean want_plugins;
104 /* command-line options */
105 static gboolean verbose_mode = FALSE;
106 static gboolean ignore_global_tags = FALSE;
107 static gboolean no_msgwin = FALSE;
108 static gboolean show_version = FALSE;
109 static gchar *alternate_config = NULL;
110 #ifdef HAVE_VTE
111 static gboolean no_vte = FALSE;
112 static gchar *lib_vte = NULL;
113 #endif
114 static gboolean generate_tags = FALSE;
115 static gboolean no_preprocessing = FALSE;
116 static gboolean ft_names = FALSE;
117 static gboolean print_prefix = FALSE;
118 #ifdef HAVE_PLUGINS
119 static gboolean no_plugins = FALSE;
120 #endif
121 static gboolean dummy = FALSE;
123 /* in alphabetical order of short options */
124 static GOptionEntry entries[] =
126 { "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 },
127 { "config", 'c', 0, G_OPTION_ARG_FILENAME, &alternate_config, N_("Use an alternate configuration directory"), NULL },
128 { "ft-names", 0, 0, G_OPTION_ARG_NONE, &ft_names, N_("Print internal filetype names"), NULL },
129 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE, &generate_tags, N_("Generate global tags file (see documentation)"), NULL },
130 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE, &no_preprocessing, N_("Don't preprocess C/C++ files when generating tags"), NULL },
131 #ifdef HAVE_SOCKET
132 { "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 },
133 { "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 },
134 { "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 },
135 #endif
136 { "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL },
137 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
138 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE, &ignore_global_tags, N_("Don't load auto completion data (see documentation)"), NULL },
139 #ifdef HAVE_PLUGINS
140 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE, &no_plugins, N_("Don't load plugins"), NULL },
141 #endif
142 { "print-prefix", 0, 0, G_OPTION_ARG_NONE, &print_prefix, N_("Print Geany's installation prefix"), NULL },
143 { "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 },
144 #ifdef HAVE_VTE
145 { "no-terminal", 't', 0, G_OPTION_ARG_NONE, &no_vte, N_("Don't load terminal support"), NULL },
146 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME, &lib_vte, N_("Filename of libvte.so"), NULL },
147 #endif
148 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_mode, N_("Be verbose"), NULL },
149 { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Show version and exit"), NULL },
150 { "dummy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &dummy, NULL, NULL }, /* for +NNN line number arguments */
151 { NULL, 0, 0, 0, NULL, NULL, NULL }
155 static void setup_window_position(void)
157 /* interprets the saved window geometry */
158 if (prefs.save_winpos && ui_prefs.geometry[0] != -1)
160 gtk_window_move(GTK_WINDOW(main_widgets.window), ui_prefs.geometry[0], ui_prefs.geometry[1]);
161 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window), ui_prefs.geometry[2], ui_prefs.geometry[3]);
162 if (ui_prefs.geometry[4] == 1)
163 gtk_window_maximize(GTK_WINDOW(main_widgets.window));
168 /* special things for the initial setup of the checkboxes and related stuff
169 * an action on a setting is only performed if the setting is not equal to the program default
170 * (all the following code is not perfect but it works for the moment) */
171 static void apply_settings(void)
173 ui_update_fold_items();
175 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
176 toolbar_show_hide();
177 if (! ui_prefs.msgwindow_visible)
179 ignore_callback = TRUE;
180 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")), FALSE);
181 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"));
182 ignore_callback = FALSE;
184 if (! ui_prefs.sidebar_visible)
186 ignore_callback = TRUE;
187 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_sidebar1")), FALSE);
188 ignore_callback = FALSE;
191 toolbar_apply_settings();
192 toolbar_update_ui();
194 ui_update_view_editor_menu_items();
196 /* hide statusbar if desired */
197 if (! interface_prefs.statusbar_visible)
199 gtk_widget_hide(ui_widgets.statusbar);
202 /* set the tab placements of the notebooks */
203 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.tab_pos_editor);
204 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow.notebook), interface_prefs.tab_pos_msgwin);
205 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.sidebar_notebook), interface_prefs.tab_pos_sidebar);
207 /* whether to show notebook tabs or not */
208 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
210 #ifdef HAVE_VTE
211 if (! vte_info.have_vte)
212 #endif
214 gtk_widget_set_sensitive(
215 ui_lookup_widget(main_widgets.window, "send_selection_to_vte1"), FALSE);
218 if (interface_prefs.sidebar_pos != GTK_POS_LEFT)
219 ui_swap_sidebar_pos();
223 static void main_init(void)
225 /* inits */
226 main_widgets.window = NULL;
227 app->project = NULL;
228 ui_widgets.open_fontsel = NULL;
229 ui_widgets.open_colorsel = NULL;
230 ui_widgets.prefs_dialog = NULL;
231 main_status.main_window_realized = FALSE;
232 file_prefs.tab_order_ltr = FALSE;
233 file_prefs.tab_order_beside = FALSE;
234 main_status.quitting = FALSE;
235 ignore_callback = FALSE;
236 app->tm_workspace = tm_get_workspace();
237 ui_prefs.recent_queue = g_queue_new();
238 ui_prefs.recent_projects_queue = g_queue_new();
239 main_status.opening_session_files = FALSE;
241 ui_init_stock_items();
243 main_widgets.window = create_window1();
245 /* add recent projects to the Project menu */
246 ui_widgets.recent_projects_menuitem = ui_lookup_widget(main_widgets.window, "recent_projects1");
247 ui_widgets.recent_projects_menu_menubar = gtk_menu_new();
248 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_projects_menuitem),
249 ui_widgets.recent_projects_menu_menubar);
251 /* store important pointers for later reference */
252 main_widgets.toolbar = toolbar_init();
253 main_widgets.sidebar_notebook = ui_lookup_widget(main_widgets.window, "notebook3");
254 main_widgets.notebook = ui_lookup_widget(main_widgets.window, "notebook1");
255 main_widgets.editor_menu = create_edit_menu1();
256 main_widgets.tools_menu = ui_lookup_widget(main_widgets.window, "tools1_menu");
257 main_widgets.message_window_notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
258 main_widgets.project_menu = ui_lookup_widget(main_widgets.window, "menu_project1_menu");
260 ui_widgets.toolbar_menu = create_toolbar_popup_menu1();
261 ui_init();
263 /* set widget names for matching with .gtkrc-2.0 */
264 gtk_widget_set_name(main_widgets.window, "GeanyMainWindow");
265 gtk_widget_set_name(ui_widgets.toolbar_menu, "GeanyToolbarMenu");
266 gtk_widget_set_name(main_widgets.editor_menu, "GeanyEditMenu");
267 gtk_widget_set_name(ui_lookup_widget(main_widgets.window, "menubar1"), "GeanyMenubar");
268 gtk_widget_set_name(main_widgets.toolbar, "GeanyToolbar");
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 " (git >= " 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 (G_UNLIKELY(! 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 @link pluginsignals.c @c "geany-startup-complete" signal @endlink
403 * to get notified about 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 * If you have already used @ref PLUGIN_SET_TRANSLATABLE_INFO() you
427 * don't need to call main_locale_init() again as it has already been done.
429 * @param locale_dir The location where the translation files should be searched. This is
430 * usually the @c LOCALEDIR macro, defined by the build system.
431 * E.g. @c $prefix/share/locale.
432 * Only used on non-Windows systems. On Windows, the directory is determined
433 * by @c g_win32_get_package_installation_directory().
434 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
435 * defined by the build system.
437 * @since 0.16
439 void main_locale_init(const gchar *locale_dir, const gchar *package)
441 gchar *l_locale_dir = NULL;
443 #ifdef HAVE_LOCALE_H
444 setlocale(LC_ALL, "");
445 #endif
447 #ifdef G_OS_WIN32
449 gchar *install_dir = win32_get_installation_dir();
450 /* e.g. C:\Program Files\geany\lib\locale */
451 l_locale_dir = g_strconcat(install_dir, "\\share\\locale", NULL);
452 g_free(install_dir);
454 #else
455 l_locale_dir = g_strdup(locale_dir);
456 #endif
458 bindtextdomain(package, l_locale_dir);
459 bind_textdomain_codeset(package, "UTF-8");
460 g_free(l_locale_dir);
464 static void print_filetypes(void)
466 const GSList *list, *node;
468 filetypes_init_types();
469 printf("Geany's filetype names:\n");
471 list = filetypes_get_sorted_by_name();
472 foreach_slist(node, list)
474 GeanyFiletype *ft = node->data;
476 printf("%s\n", ft->name);
478 filetypes_free_types();
482 static void wait_for_input_on_windows(void)
484 #ifdef G_OS_WIN32
485 if (verbose_mode)
487 geany_debug("Press any key to continue");
488 getchar();
490 #endif
494 static void parse_command_line_options(gint *argc, gchar ***argv)
496 GError *error = NULL;
497 GOptionContext *context;
498 gint i;
499 CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE, FALSE};
501 /* first initialise cl_options fields with default values */
502 cl_options = def_clo;
504 /* the GLib option parser can't handle the +NNN (line number) option,
505 * so we grab that here and replace it with a no-op */
506 for (i = 1; i < (*argc); i++)
508 if ((*argv)[i][0] != '+')
509 continue;
511 cl_options.goto_line = atoi((*argv)[i] + 1);
512 (*argv)[i] = "--dummy";
515 context = g_option_context_new(_("[FILES...]"));
516 g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
517 g_option_group_set_translation_domain(g_option_context_get_main_group(context), GETTEXT_PACKAGE);
518 g_option_context_add_group(context, gtk_get_option_group(FALSE));
519 g_option_context_parse(context, argc, argv, &error);
520 g_option_context_free(context);
522 if (error != NULL)
524 g_printerr("Geany: %s\n", error->message);
525 g_error_free(error);
526 exit(1);
529 app->debug_mode = verbose_mode;
531 #ifdef G_OS_WIN32
532 win32_init_debug_code();
533 #endif
535 if (show_version)
537 printf(PACKAGE " %s (", main_get_version_string());
538 /* note for translators: library versions are printed after this */
539 printf(_("built on %s with "), __DATE__);
540 printf(geany_lib_versions,
541 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
542 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
543 printf(")\n");
544 wait_for_input_on_windows();
545 exit(0);
548 if (print_prefix)
550 printf("%s\n", GEANY_PREFIX);
551 printf("%s\n", GEANY_DATADIR);
552 printf("%s\n", GEANY_LIBDIR);
553 printf("%s\n", GEANY_LOCALEDIR);
554 wait_for_input_on_windows();
555 exit(0);
558 if (alternate_config)
560 geany_debug("alternate config: %s", alternate_config);
561 app->configdir = alternate_config;
563 else
565 app->configdir = g_build_filename(g_get_user_config_dir(), "geany", NULL);
568 if (generate_tags)
570 gboolean ret;
572 filetypes_init_types();
573 ret = symbols_generate_global_tags(*argc, *argv, ! no_preprocessing);
574 filetypes_free_types();
575 wait_for_input_on_windows();
576 exit(ret);
579 if (ft_names)
581 print_filetypes();
582 wait_for_input_on_windows();
583 exit(0);
586 #ifdef HAVE_SOCKET
587 socket_info.ignore_socket = cl_options.new_instance;
588 if (cl_options.socket_filename)
590 socket_info.file_name = cl_options.socket_filename;
592 #endif
594 #ifdef HAVE_VTE
595 vte_info.lib_vte = lib_vte;
596 #endif
597 cl_options.ignore_global_tags = ignore_global_tags;
599 if (! gtk_init_check(NULL, NULL))
600 { /* check whether we have a valid X display and exit if not */
601 g_printerr("Geany: cannot open display\n");
602 exit(1);
607 static gint create_config_dir(void)
609 gint saved_errno = 0;
610 gchar *conf_file = g_build_filename(app->configdir, "geany.conf", NULL);
611 gchar *filedefs_dir = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
613 gchar *templates_dir = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
615 if (! g_file_test(app->configdir, G_FILE_TEST_EXISTS))
617 #ifndef G_OS_WIN32
618 /* if we are *not* using an alternate config directory, we check whether the old one
619 * in ~/.geany still exists and try to move it */
620 if (alternate_config == NULL)
622 gchar *old_dir = g_build_filename(g_get_home_dir(), ".geany", NULL);
623 /* move the old config dir if it exists */
624 if (g_file_test(old_dir, G_FILE_TEST_EXISTS))
626 if (! dialogs_show_question_full(main_widgets.window,
627 GTK_STOCK_YES, GTK_STOCK_QUIT, _("Move it now?"),
628 "%s",
629 _("Geany needs to move your old configuration directory before starting.")))
630 exit(0);
632 if (! g_file_test(app->configdir, G_FILE_TEST_IS_DIR))
633 utils_mkdir(app->configdir, TRUE);
635 if (g_rename(old_dir, app->configdir) == 0)
637 dialogs_show_msgbox(GTK_MESSAGE_INFO,
638 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
639 old_dir, app->configdir);
640 g_free(old_dir);
641 return 0;
643 else
645 dialogs_show_msgbox(GTK_MESSAGE_WARNING,
646 /* for translators: the third %s in brackets is the error message which
647 * describes why moving the dir didn't work */
648 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
649 "Please move manually the directory to the new location."),
650 old_dir, app->configdir, g_strerror(errno));
653 g_free(old_dir);
655 #endif
656 geany_debug("creating config directory %s", app->configdir);
657 saved_errno = utils_mkdir(app->configdir, TRUE);
660 if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS))
661 { /* check whether geany.conf can be written */
662 saved_errno = utils_is_file_writable(app->configdir);
665 /* make subdir for filetype definitions */
666 if (saved_errno == 0)
668 gchar *filedefs_readme = g_build_filename(app->configdir,
669 GEANY_FILEDEFS_SUBDIR, "filetypes.README", NULL);
671 if (! g_file_test(filedefs_dir, G_FILE_TEST_EXISTS))
673 saved_errno = utils_mkdir(filedefs_dir, FALSE);
675 if (saved_errno == 0 && ! g_file_test(filedefs_readme, G_FILE_TEST_EXISTS))
677 gchar *text = g_strconcat(
678 "Copy files from ", app->datadir, " to this directory to overwrite "
679 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
680 "the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").", NULL);
681 utils_write_file(filedefs_readme, text);
682 g_free(text);
684 g_free(filedefs_readme);
687 /* make subdir for template files */
688 if (saved_errno == 0)
690 gchar *templates_readme = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR,
691 "templates.README", NULL);
693 if (! g_file_test(templates_dir, G_FILE_TEST_EXISTS))
695 saved_errno = utils_mkdir(templates_dir, FALSE);
697 if (saved_errno == 0 && ! g_file_test(templates_readme, G_FILE_TEST_EXISTS))
699 gchar *text = g_strconcat(
700 "There are several template files in this directory. For these templates you can use wildcards.\n\
701 For more information read the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").",
702 NULL);
703 utils_write_file(templates_readme, text);
704 g_free(text);
706 g_free(templates_readme);
709 g_free(filedefs_dir);
710 g_free(templates_dir);
711 g_free(conf_file);
713 return saved_errno;
717 /* Returns 0 if config dir is OK. */
718 static gint setup_config_dir(void)
720 gint mkdir_result = 0;
722 /* convert configdir to locale encoding to avoid troubles */
723 setptr(app->configdir, utils_get_locale_from_utf8(app->configdir));
725 mkdir_result = create_config_dir();
726 if (mkdir_result != 0)
728 if (! dialogs_show_question(
729 _("Configuration directory could not be created (%s).\nThere could be some problems "
730 "using Geany without a configuration directory.\nStart Geany anyway?"),
731 g_strerror(mkdir_result)))
733 exit(0);
736 /* make configdir a real path */
737 if (g_file_test(app->configdir, G_FILE_TEST_EXISTS))
738 setptr(app->configdir, tm_get_real_path(app->configdir));
740 return mkdir_result;
744 static void signal_cb(gint sig)
746 if (sig == SIGTERM)
748 on_exit_clicked(NULL, NULL);
753 /* Used for command-line arguments at startup or from socket.
754 * this will strip any :line:col filename suffix from locale_filename */
755 gboolean main_handle_filename(const gchar *locale_filename)
757 GeanyDocument *doc;
758 gint line = -1, column = -1;
759 gchar *filename;
761 g_return_val_if_fail(locale_filename, FALSE);
763 /* check whether the passed filename is an URI */
764 filename = utils_get_path_from_uri(locale_filename);
765 if (filename == NULL)
766 return FALSE;
768 get_line_and_column_from_filename(filename, &line, &column);
769 if (line >= 0)
770 cl_options.goto_line = line;
771 if (column >= 0)
772 cl_options.goto_column = column;
774 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
776 doc = document_open_file(filename, FALSE, NULL, NULL);
777 /* add recent file manually if opening_session_files is set */
778 if (doc != NULL && main_status.opening_session_files)
779 ui_add_recent_file(doc->file_name);
780 g_free(filename);
781 return TRUE;
783 else if (file_prefs.cmdline_new_files)
784 { /* create new file with the given filename */
785 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
787 doc = document_new_file(utf8_filename, NULL, NULL);
788 if (doc != NULL)
789 ui_add_recent_file(doc->file_name);
790 g_free(utf8_filename);
791 g_free(filename);
792 return TRUE;
794 g_free(filename);
795 return FALSE;
799 /* open files from command line */
800 static gboolean open_cl_files(gint argc, gchar **argv)
802 gint i;
804 if (argc <= 1) return FALSE;
806 for (i = 1; i < argc; i++)
808 gchar *filename = main_get_argv_filename(argv[i]);
810 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
812 g_free(filename);
813 continue;
816 #ifdef G_OS_WIN32
817 /* It seems argv elements are encoded in CP1252 on a German Windows */
818 setptr(filename, g_locale_to_utf8(filename, -1, NULL, NULL, NULL));
819 #endif
820 if (filename && ! main_handle_filename(filename))
822 const gchar *msg = _("Could not find file '%s'.");
824 g_printerr(msg, filename); /* also print to the terminal */
825 g_printerr("\n");
826 ui_set_statusbar(TRUE, msg, filename);
828 g_free(filename);
830 return TRUE;
834 static void load_session_project_file(void)
836 gchar *locale_filename;
838 g_return_if_fail(project_prefs.session_file != NULL);
840 locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
842 if (G_LIKELY(NZV(locale_filename)))
843 project_load_file(locale_filename);
845 g_free(locale_filename);
846 g_free(project_prefs.session_file); /* no longer needed */
850 static void load_settings(void)
852 configuration_load();
853 /* let cmdline options overwrite configuration settings */
854 #ifdef HAVE_VTE
855 vte_info.have_vte = (no_vte) ? FALSE : vte_info.load_vte;
856 #endif
857 if (no_msgwin)
858 ui_prefs.msgwindow_visible = FALSE;
860 #ifdef HAVE_PLUGINS
861 want_plugins = prefs.load_plugins && !no_plugins;
862 #endif
866 void main_load_project_from_command_line(const gchar *locale_filename, gboolean use_session)
868 gchar *pfile = NULL;
870 if (utils_is_uri(locale_filename))
871 pfile = utils_get_path_from_uri(locale_filename);
872 else
873 pfile = g_strdup(locale_filename);
875 if (pfile != NULL)
877 if (use_session)
878 project_load_file_with_session(pfile);
879 else
880 project_load_file(pfile);
882 g_free(pfile);
886 static void load_startup_files(gint argc, gchar **argv)
888 gboolean load_project_from_cl = FALSE;
890 /* ATM when opening a project file any other filenames are ignored */
891 load_project_from_cl = (argc > 1) && g_str_has_suffix(argv[1], ".geany");
892 if (load_project_from_cl && argc > 2)
893 g_print("Ignoring extra filenames after %s", argv[1]);
895 if (load_project_from_cl || ! open_cl_files(argc, argv))
897 if (prefs.load_session)
899 if (load_project_from_cl)
901 main_load_project_from_command_line(argv[1], FALSE);
903 else if (cl_options.load_session && !cl_options.new_instance)
904 load_session_project_file();
906 /* when we want a new instance, we still load project session files unless -s
907 * was passed */
908 if (!cl_options.load_session || (!load_project_from_cl && cl_options.new_instance))
909 return;
911 /* load session files into tabs, as they are found in the session_files variable */
912 configuration_open_files();
914 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
916 ui_update_popup_copy_items(NULL);
917 ui_update_popup_reundo_items(NULL);
924 static gboolean send_startup_complete(gpointer data)
926 g_signal_emit_by_name(geany_object, "geany-startup-complete");
927 return FALSE;
931 static const gchar *get_locale(void)
933 const gchar *locale = "unknown";
934 #ifdef HAVE_LOCALE_H
935 locale = setlocale(LC_CTYPE, NULL);
936 #endif
937 return locale;
941 gint main(gint argc, gchar **argv)
943 GeanyDocument *doc;
944 gint config_dir_result;
945 const gchar *locale;
947 log_handlers_init();
949 app = g_new0(GeanyApp, 1);
950 memset(&main_status, 0, sizeof(GeanyStatus));
951 memset(&prefs, 0, sizeof(GeanyPrefs));
952 memset(&interface_prefs, 0, sizeof(GeanyInterfacePrefs));
953 memset(&toolbar_prefs, 0, sizeof(GeanyToolbarPrefs));
954 memset(&file_prefs, 0, sizeof(GeanyFilePrefs));
955 memset(&search_prefs, 0, sizeof(GeanySearchPrefs));
956 memset(&tool_prefs, 0, sizeof(GeanyToolPrefs));
957 memset(&template_prefs, 0, sizeof(GeanyTemplatePrefs));
958 memset(&ui_prefs, 0, sizeof(UIPrefs));
959 memset(&ui_widgets, 0, sizeof(UIWidgets));
961 setup_paths();
962 #ifdef ENABLE_NLS
963 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
964 #endif
965 parse_command_line_options(&argc, &argv);
967 /* Initialize GLib's thread system in case any plugins want to use it or their
968 * dependencies (e.g. WebKit, Soup, ...) */
969 if (!g_thread_supported())
970 g_thread_init(NULL);
972 signal(SIGTERM, signal_cb);
973 #ifdef G_OS_UNIX
974 /* SIGQUIT is used to kill spawned children and we get also this signal, so ignore */
975 signal(SIGQUIT, SIG_IGN);
976 /* ignore SIGPIPE signal for preventing sudden death of program */
977 signal(SIGPIPE, SIG_IGN);
978 #endif
980 config_dir_result = setup_config_dir();
981 #ifdef HAVE_SOCKET
982 /* check and create (unix domain) socket for remote operation */
983 if (! socket_info.ignore_socket)
985 socket_info.lock_socket = -1;
986 socket_info.lock_socket_tag = 0;
987 socket_info.lock_socket = socket_init(argc, argv);
988 /* Socket exists */
989 if (socket_info.lock_socket == -2)
991 /* Quit if filenames were sent to first instance or the list of open
992 * documents has been sent */
993 if (argc > 1 || cl_options.list_documents)
995 gdk_notify_startup_complete();
996 g_free(app->configdir);
997 g_free(app->datadir);
998 g_free(app->docdir);
999 g_free(app);
1000 return 0;
1002 /* Start a new instance if no command line strings were passed */
1003 socket_info.ignore_socket = TRUE;
1004 cl_options.new_instance = TRUE;
1007 #endif
1009 locale = get_locale();
1010 geany_debug("Geany %s, %s",
1011 main_get_version_string(),
1012 locale);
1013 geany_debug(geany_lib_versions,
1014 gtk_major_version, gtk_minor_version, gtk_micro_version,
1015 glib_major_version, glib_minor_version, glib_micro_version);
1016 geany_debug("System data dir: %s", app->datadir);
1017 geany_debug("User config dir: %s", app->configdir);
1019 /* create the object so Geany signals can be connected in init() functions */
1020 geany_object = geany_object_new();
1022 /* inits */
1023 main_init();
1025 encodings_init();
1026 editor_init();
1028 /* init stash groups before loading keyfile */
1029 configuration_init();
1030 ui_init_prefs();
1031 search_init();
1032 project_init();
1033 #ifdef HAVE_PLUGINS
1034 plugins_init();
1035 #endif
1036 sidebar_init();
1037 load_settings(); /* load keyfile */
1039 highlighting_init();
1040 msgwin_init();
1041 build_init();
1042 ui_create_insert_menu_items();
1043 ui_create_insert_date_menu_items();
1044 keybindings_init();
1045 notebook_init();
1046 filetypes_init();
1047 templates_init();
1048 navqueue_init();
1049 document_init_doclist();
1050 symbols_init();
1051 editor_snippets_init();
1053 /* set window icon */
1055 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
1056 gtk_window_set_icon(GTK_WINDOW(main_widgets.window), pb);
1057 g_object_unref(pb); /* free our reference */
1060 /* registering some basic events */
1061 g_signal_connect(main_widgets.window, "delete-event", G_CALLBACK(on_exit_clicked), NULL);
1062 g_signal_connect(main_widgets.window, "window-state-event", G_CALLBACK(on_window_state_event), NULL);
1064 g_signal_connect(msgwindow.scribble, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
1066 #ifdef HAVE_VTE
1067 vte_init();
1068 #endif
1069 ui_create_recent_menus();
1071 ui_set_statusbar(TRUE, _("This is Geany %s."), main_get_version_string());
1072 if (config_dir_result != 0)
1073 ui_set_statusbar(TRUE, _("Configuration directory could not be created (%s)."),
1074 g_strerror(config_dir_result));
1076 /* apply all configuration options */
1077 apply_settings();
1079 #ifdef HAVE_PLUGINS
1080 /* load any enabled plugins before we open any documents */
1081 if (want_plugins)
1082 plugins_load_active();
1083 #endif
1085 ui_sidebar_show_hide();
1087 /* set the active sidebar page after plugins have been loaded */
1088 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), ui_prefs.sidebar_page);
1090 /* load keybinding settings after plugins have added their groups */
1091 keybindings_load_keyfile();
1093 /* create the custom command menu after the keybindings have been loaded to have the proper
1094 * accelerator shown for the menu items */
1095 tools_create_insert_custom_command_menu_items();
1097 /* load any command line files or session files */
1098 main_status.opening_session_files = TRUE;
1099 load_startup_files(argc, argv);
1100 main_status.opening_session_files = FALSE;
1102 /* open a new file if no other file was opened */
1103 document_new_file_if_non_open();
1105 ui_document_buttons_update();
1106 ui_save_buttons_toggle(FALSE);
1108 doc = document_get_current();
1109 sidebar_select_openfiles_item(doc);
1110 build_menu_update(doc);
1111 sidebar_update_tag_list(doc, FALSE);
1113 #ifdef G_OS_WIN32
1114 /* Manually realise the main window to be able to set the position but don't show it.
1115 * We don't set the position after showing the window to avoid flickering. */
1116 gtk_widget_realize(main_widgets.window);
1117 #endif
1118 setup_window_position();
1120 /* finally show the window */
1121 document_grab_focus(doc);
1122 gtk_widget_show(main_widgets.window);
1123 main_status.main_window_realized = TRUE;
1125 configuration_apply_settings();
1127 #ifdef HAVE_SOCKET
1128 /* register the callback of socket input */
1129 if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
1131 socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
1132 socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,
1133 G_IO_IN | G_IO_PRI | G_IO_ERR, socket_lock_input_cb, main_widgets.window);
1135 #endif
1137 #ifdef G_OS_WIN32
1139 gchar *dir;
1140 /* On Windows, change the working directory to the Geany installation path to not lock
1141 * the directory of a file passed as command line argument (see bug #2626124). */
1142 dir = win32_get_installation_dir();
1143 win32_set_working_directory(dir);
1144 g_free(dir);
1146 #endif
1148 /* when we are really done with setting everything up and the main event loop is running,
1149 * tell other components, mainly plugins, that startup is complete */
1150 g_idle_add_full(G_PRIORITY_LOW, send_startup_complete, NULL, NULL);
1152 gtk_main();
1153 return 0;
1157 static void queue_free(GQueue *queue)
1159 while (! g_queue_is_empty(queue))
1161 g_free(g_queue_pop_tail(queue));
1163 g_queue_free(queue);
1167 void main_quit()
1169 geany_debug("Quitting...");
1171 #ifdef HAVE_SOCKET
1172 socket_finalize();
1173 #endif
1175 #ifdef HAVE_PLUGINS
1176 plugins_finalize();
1177 #endif
1179 navqueue_free();
1180 keybindings_free();
1181 highlighting_free_styles();
1182 templates_free_templates();
1183 msgwin_finalize();
1184 search_finalize();
1185 build_finalize();
1186 document_finalize();
1187 symbols_finalize();
1188 project_finalize();
1189 editor_finalize();
1190 editor_snippets_free();
1191 encodings_finalize();
1192 toolbar_finalize();
1193 sidebar_finalize();
1194 configuration_finalize();
1195 filetypes_free_types();
1196 ui_finalize();
1197 log_finalize();
1199 tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
1200 g_free(app->configdir);
1201 g_free(app->datadir);
1202 g_free(app->docdir);
1203 g_free(prefs.default_open_path);
1204 g_free(prefs.custom_plugin_path);
1205 g_free(ui_prefs.custom_date_format);
1206 g_free(interface_prefs.editor_font);
1207 g_free(interface_prefs.tagbar_font);
1208 g_free(interface_prefs.msgwin_font);
1209 g_free(editor_prefs.long_line_color);
1210 g_free(editor_prefs.comment_toggle_mark);
1211 g_free(editor_prefs.color_scheme);
1212 g_free(tool_prefs.context_action_cmd);
1213 g_free(template_prefs.developer);
1214 g_free(template_prefs.company);
1215 g_free(template_prefs.mail);
1216 g_free(template_prefs.initials);
1217 g_free(template_prefs.version);
1218 g_free(tool_prefs.term_cmd);
1219 g_free(tool_prefs.browser_cmd);
1220 g_free(tool_prefs.grep_cmd);
1221 g_free(printing_prefs.external_print_cmd);
1222 g_free(printing_prefs.page_header_datefmt);
1223 g_strfreev(ui_prefs.custom_commands);
1225 queue_free(ui_prefs.recent_queue);
1226 queue_free(ui_prefs.recent_projects_queue);
1228 if (ui_widgets.prefs_dialog && GTK_IS_WIDGET(ui_widgets.prefs_dialog)) gtk_widget_destroy(ui_widgets.prefs_dialog);
1229 if (ui_widgets.open_fontsel && GTK_IS_WIDGET(ui_widgets.open_fontsel)) gtk_widget_destroy(ui_widgets.open_fontsel);
1230 if (ui_widgets.open_colorsel && GTK_IS_WIDGET(ui_widgets.open_colorsel)) gtk_widget_destroy(ui_widgets.open_colorsel);
1231 #ifdef HAVE_VTE
1232 if (vte_info.have_vte) vte_close();
1233 g_free(vte_info.lib_vte);
1234 g_free(vte_info.dir);
1235 #endif
1236 gtk_widget_destroy(main_widgets.window);
1238 /* destroy popup menus */
1239 if (main_widgets.editor_menu && GTK_IS_WIDGET(main_widgets.editor_menu))
1240 gtk_widget_destroy(main_widgets.editor_menu);
1241 if (ui_widgets.toolbar_menu && GTK_IS_WIDGET(ui_widgets.toolbar_menu))
1242 gtk_widget_destroy(ui_widgets.toolbar_menu);
1243 if (msgwindow.popup_status_menu && GTK_IS_WIDGET(msgwindow.popup_status_menu))
1244 gtk_widget_destroy(msgwindow.popup_status_menu);
1245 if (msgwindow.popup_msg_menu && GTK_IS_WIDGET(msgwindow.popup_msg_menu))
1246 gtk_widget_destroy(msgwindow.popup_msg_menu);
1247 if (msgwindow.popup_compiler_menu && GTK_IS_WIDGET(msgwindow.popup_compiler_menu))
1248 gtk_widget_destroy(msgwindow.popup_compiler_menu);
1250 g_object_unref(geany_object);
1251 geany_object = NULL;
1253 g_free(app);
1255 gtk_main_quit();
1260 * Reloads most of Geany's configuration files without restarting. Currently the following
1261 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1262 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1263 * and 'settings' and 'build_settings' sections of the filetype definition files.
1265 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1266 * editor plugin).
1268 * @since 0.15
1270 void main_reload_configuration(void)
1272 /* reload templates */
1273 templates_free_templates();
1274 templates_init();
1276 /* reload snippets */
1277 editor_snippets_free();
1278 editor_snippets_init();
1280 filetypes_reload_extensions();
1281 filetypes_reload();
1283 /* C tag names to ignore */
1284 symbols_reload_config_files();
1286 ui_set_statusbar(TRUE, _("Configuration files reloaded."));