If only given an input filename and no match string, match all changelog entries.
[geany-mirror.git] / src / main.c
blob15484ac28c857350bb54371599f3fe61abdd405d
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 const gchar geany_lib_versions[] = "GTK %u.%u.%u, GLib %u.%u.%u"
99 #ifdef HAVE_GIO
100 ", GIO"
101 #endif
102 #ifdef USE_INCLUDED_REGEX
103 ", built-in regex"
104 #endif
107 static gboolean want_plugins;
109 /* command-line options */
110 static gboolean verbose_mode = FALSE;
111 static gboolean ignore_global_tags = FALSE;
112 static gboolean no_msgwin = FALSE;
113 static gboolean show_version = FALSE;
114 static gchar *alternate_config = NULL;
115 #ifdef HAVE_VTE
116 static gboolean no_vte = FALSE;
117 static gchar *lib_vte = NULL;
118 #endif
119 static gboolean generate_tags = FALSE;
120 static gboolean no_preprocessing = FALSE;
121 static gboolean ft_names = FALSE;
122 static gboolean print_prefix = FALSE;
123 #ifdef HAVE_PLUGINS
124 static gboolean no_plugins = FALSE;
125 #endif
126 static gboolean dummy = FALSE;
128 /* in alphabetical order of short options */
129 static GOptionEntry entries[] =
131 { "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 },
132 { "config", 'c', 0, G_OPTION_ARG_FILENAME, &alternate_config, N_("Use an alternate configuration directory"), NULL },
133 { "ft-names", 0, 0, G_OPTION_ARG_NONE, &ft_names, N_("Print internal filetype names"), NULL },
134 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE, &generate_tags, N_("Generate global tags file (see documentation)"), NULL },
135 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE, &no_preprocessing, N_("Don't preprocess C/C++ files when generating tags"), NULL },
136 #ifdef HAVE_SOCKET
137 { "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 },
138 { "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 },
139 { "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 },
140 #endif
141 { "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL },
142 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
143 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE, &ignore_global_tags, N_("Don't load auto completion data (see documentation)"), NULL },
144 #ifdef HAVE_PLUGINS
145 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE, &no_plugins, N_("Don't load plugins"), NULL },
146 #endif
147 { "print-prefix", 0, 0, G_OPTION_ARG_NONE, &print_prefix, N_("Print Geany's installation prefix"), NULL },
148 { "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 },
149 #ifdef HAVE_VTE
150 { "no-terminal", 't', 0, G_OPTION_ARG_NONE, &no_vte, N_("Don't load terminal support"), NULL },
151 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME, &lib_vte, N_("Filename of libvte.so"), NULL },
152 #endif
153 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_mode, N_("Be verbose"), NULL },
154 { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Show version and exit"), NULL },
155 { "dummy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &dummy, NULL, NULL }, /* for +NNN line number arguments */
156 { NULL, 0, 0, 0, NULL, NULL, NULL }
160 static void setup_window_position(void)
162 /* interprets the saved window geometry */
163 if (prefs.save_winpos && ui_prefs.geometry[0] != -1)
165 gtk_window_move(GTK_WINDOW(main_widgets.window), ui_prefs.geometry[0], ui_prefs.geometry[1]);
166 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window), ui_prefs.geometry[2], ui_prefs.geometry[3]);
167 if (ui_prefs.geometry[4] == 1)
168 gtk_window_maximize(GTK_WINDOW(main_widgets.window));
173 /* special things for the initial setup of the checkboxes and related stuff
174 * an action on a setting is only performed if the setting is not equal to the program default
175 * (all the following code is not perfect but it works for the moment) */
176 static void apply_settings(void)
178 ui_update_fold_items();
180 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
181 toolbar_show_hide();
182 if (! ui_prefs.msgwindow_visible)
184 ignore_callback = TRUE;
185 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")), FALSE);
186 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"));
187 ignore_callback = FALSE;
189 if (! ui_prefs.sidebar_visible)
191 ignore_callback = TRUE;
192 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_sidebar1")), FALSE);
193 ignore_callback = FALSE;
196 toolbar_apply_settings();
197 toolbar_update_ui();
199 ui_update_view_editor_menu_items();
201 /* hide statusbar if desired */
202 if (! interface_prefs.statusbar_visible)
204 gtk_widget_hide(ui_widgets.statusbar);
207 /* set the tab placements of the notebooks */
208 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.tab_pos_editor);
209 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow.notebook), interface_prefs.tab_pos_msgwin);
210 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.sidebar_notebook), interface_prefs.tab_pos_sidebar);
212 /* whether to show notebook tabs or not */
213 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
215 #ifdef HAVE_VTE
216 if (! vte_info.have_vte)
217 #endif
219 gtk_widget_set_sensitive(
220 ui_lookup_widget(main_widgets.window, "send_selection_to_vte1"), FALSE);
223 if (interface_prefs.sidebar_pos != GTK_POS_LEFT)
224 ui_swap_sidebar_pos();
228 static void main_init(void)
230 /* inits */
231 main_widgets.window = NULL;
232 app->project = NULL;
233 ui_widgets.open_fontsel = NULL;
234 ui_widgets.open_colorsel = NULL;
235 ui_widgets.open_filesel = NULL;
236 ui_widgets.save_filesel = NULL;
237 ui_widgets.prefs_dialog = NULL;
238 main_status.main_window_realized = FALSE;
239 file_prefs.tab_order_ltr = FALSE;
240 file_prefs.tab_order_beside = FALSE;
241 main_status.quitting = FALSE;
242 ignore_callback = FALSE;
243 app->tm_workspace = tm_get_workspace();
244 ui_prefs.recent_queue = g_queue_new();
245 ui_prefs.recent_projects_queue = g_queue_new();
246 main_status.opening_session_files = FALSE;
248 ui_init_stock_items();
250 main_widgets.window = create_window1();
252 /* add recent projects to the Project menu */
253 ui_widgets.recent_projects_menuitem = ui_lookup_widget(main_widgets.window, "recent_projects1");
254 ui_widgets.recent_projects_menu_menubar = gtk_menu_new();
255 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_projects_menuitem),
256 ui_widgets.recent_projects_menu_menubar);
258 /* store important pointers for later reference */
259 main_widgets.toolbar = toolbar_init();
260 main_widgets.sidebar_notebook = ui_lookup_widget(main_widgets.window, "notebook3");
261 main_widgets.notebook = ui_lookup_widget(main_widgets.window, "notebook1");
262 main_widgets.editor_menu = create_edit_menu1();
263 main_widgets.tools_menu = ui_lookup_widget(main_widgets.window, "tools1_menu");
264 main_widgets.message_window_notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
265 main_widgets.project_menu = ui_lookup_widget(main_widgets.window, "menu_project1_menu");
267 ui_widgets.toolbar_menu = create_toolbar_popup_menu1();
268 ui_init();
270 /* set widget names for matching with .gtkrc-2.0 */
271 gtk_widget_set_name(main_widgets.window, "GeanyMainWindow");
272 gtk_widget_set_name(ui_widgets.toolbar_menu, "GeanyToolbarMenu");
273 gtk_widget_set_name(main_widgets.editor_menu, "GeanyEditMenu");
274 gtk_widget_set_name(ui_lookup_widget(main_widgets.window, "menubar1"), "GeanyMenubar");
275 gtk_widget_set_name(main_widgets.toolbar, "GeanyToolbar");
277 #if ! GTK_CHECK_VERSION(2, 10, 0)
278 /* hide Page setup menu item, it isn't supported with non-GTK printing */
279 gtk_widget_hide(ui_widgets.print_page_setup);
280 #endif
282 /* for some reason we need to set the initial size request,
283 * otherwise the main window gets crazy dimensions */
284 gtk_widget_set_size_request(main_widgets.window, 10, 10);
285 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
286 GEANY_WINDOW_DEFAULT_WIDTH, GEANY_WINDOW_DEFAULT_HEIGHT);
290 const gchar *main_get_version_string(void)
292 static gchar full[] = VERSION " (svn >= " REVISION ")";
294 if (utils_str_equal(REVISION, "-1"))
295 return VERSION;
296 else
297 return full;
301 /* get the full file path of a command-line argument
302 * N.B. the result should be freed and may contain '/../' or '/./ ' */
303 gchar *main_get_argv_filename(const gchar *filename)
305 gchar *result;
307 if (g_path_is_absolute(filename) || utils_is_uri(filename))
308 result = g_strdup(filename);
309 else
311 /* use current dir */
312 gchar *cur_dir = g_get_current_dir();
314 result = g_strjoin(
315 G_DIR_SEPARATOR_S, cur_dir, filename, NULL);
316 g_free(cur_dir);
318 return result;
322 /* get a :line:column specifier from the end of a filename (if present),
323 * return the line/column values, and remove the specifier from the string
324 * (Note that *line and *column must both be set to -1 initially) */
325 static void get_line_and_column_from_filename(gchar *filename, gint *line, gint *column)
327 gsize i;
328 gint colon_count = 0;
329 gboolean have_number = FALSE;
330 gsize len;
332 g_assert(*line == -1 && *column == -1);
334 if (! NZV(filename))
335 return;
337 /* allow to open files like "test:0" */
338 if (g_file_test(filename, G_FILE_TEST_EXISTS))
339 return;
341 len = strlen(filename);
342 for (i = len - 1; i >= 1; i--)
344 gboolean is_colon = filename[i] == ':';
345 gboolean is_digit = g_ascii_isdigit(filename[i]);
347 if (! is_colon && ! is_digit)
348 break;
350 if (is_colon)
352 if (++colon_count > 1)
353 break; /* bail on 2+ colons in a row */
355 else
356 colon_count = 0;
358 if (is_digit)
359 have_number = TRUE;
361 if (is_colon && have_number)
363 gint number = atoi(&filename[i + 1]);
365 filename[i] = '\0';
366 have_number = FALSE;
368 *column = *line;
369 *line = number;
372 if (*column >= 0)
373 break; /* line and column are set, so we're done */
378 static void setup_paths(void)
380 gchar *data_dir;
381 gchar *doc_dir;
383 /* set paths */
384 #ifdef G_OS_WIN32
385 /* use the installation directory(the one where geany.exe is located) as the base for the
386 * documentation and data files */
387 gchar *install_dir = win32_get_installation_dir();
389 data_dir = g_strconcat(install_dir, "\\data", NULL); /* e.g. C:\Program Files\geany\data */
390 doc_dir = g_strconcat(install_dir, "\\doc", NULL);
392 g_free(install_dir);
393 #else
394 data_dir = g_strconcat(GEANY_DATADIR, "/geany", NULL); /* e.g. /usr/share/geany */
395 doc_dir = g_strconcat(GEANY_DOCDIR, "/html", NULL);
396 #endif
398 /* convert path names to locale encoding */
399 app->datadir = utils_get_locale_from_utf8(data_dir);
400 app->docdir = utils_get_locale_from_utf8(doc_dir);
402 g_free(data_dir);
403 g_free(doc_dir);
408 * Checks whether the main window has been realized.
409 * This is an easy indicator whether Geany is right now starting up (main window is not
410 * yet realized) or whether it has finished the startup process (main window is realized).
411 * This is because the main window is realized (i.e. actually drawn on the screen) at the
412 * end of the startup process.
414 * @note Maybe you want to use the @link pluginsignals.c @c "geany-startup-complete" signal @endlink
415 * to get notified about the completed startup process.
417 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
419 * @since 0.19
421 gboolean main_is_realized(void)
423 return main_status.main_window_realized;
428 * Initialises the gettext translation system.
429 * This is a convenience function to set up gettext for internationalisation support
430 * in external plugins. You should call this function early in @ref plugin_init().
431 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
432 * The codeset for the message translations is set to UTF-8.
434 * Note that this function only setups the gettext textdomain for you. You still have
435 * to adjust the build system of your plugin to get internationalisation support
436 * working properly.
438 * @param locale_dir The location where the translation files should be searched. This is
439 * usually the @c LOCALEDIR macro, defined by the build system.
440 * E.g. @c $prefix/share/locale.
441 * Only used on non-Windows systems. On Windows, the directory is determined
442 * by @c g_win32_get_package_installation_directory().
443 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
444 * defined by the build system.
446 * @since 0.16
448 void main_locale_init(const gchar *locale_dir, const gchar *package)
450 gchar *l_locale_dir = NULL;
452 #ifdef HAVE_LOCALE_H
453 setlocale(LC_ALL, "");
454 #endif
456 #ifdef G_OS_WIN32
458 gchar *install_dir = win32_get_installation_dir();
459 /* e.g. C:\Program Files\geany\lib\locale */
460 l_locale_dir = g_strconcat(install_dir, "\\share\\locale", NULL);
461 g_free(install_dir);
463 #else
464 l_locale_dir = g_strdup(locale_dir);
465 #endif
467 bindtextdomain(package, l_locale_dir);
468 bind_textdomain_codeset(package, "UTF-8");
469 g_free(l_locale_dir);
473 static void print_filetypes(void)
475 guint i;
477 filetypes_init_types();
478 printf("Geany's internal filetype names:\n");
480 for (i = 0; i < filetypes_array->len; i++)
482 printf("%s\n", filetypes[i]->name);
484 filetypes_free_types();
488 static void parse_command_line_options(gint *argc, gchar ***argv)
490 GError *error = NULL;
491 GOptionContext *context;
492 gint i;
493 CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE, FALSE};
495 /* first initialise cl_options fields with default values */
496 cl_options = def_clo;
498 /* the GLib option parser can't handle the +NNN (line number) option,
499 * so we grab that here and replace it with a no-op */
500 for (i = 1; i < (*argc); i++)
502 if ((*argv)[i][0] != '+')
503 continue;
505 cl_options.goto_line = atoi((*argv)[i] + 1);
506 (*argv)[i] = "--dummy";
509 context = g_option_context_new(_("[FILES...]"));
510 g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
511 g_option_group_set_translation_domain(g_option_context_get_main_group(context), GETTEXT_PACKAGE);
512 g_option_context_add_group(context, gtk_get_option_group(FALSE));
513 g_option_context_parse(context, argc, argv, &error);
514 g_option_context_free(context);
516 if (error != NULL)
518 g_printerr("Geany: %s\n", error->message);
519 g_error_free(error);
520 exit(1);
523 if (show_version)
525 printf(PACKAGE " %s (", main_get_version_string());
526 /* note for translators: library versions are printed after this */
527 printf(_("built on %s with "), __DATE__);
528 printf(geany_lib_versions,
529 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
530 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
531 printf(")\n");
532 exit(0);
535 if (print_prefix)
537 printf("%s\n", GEANY_PREFIX);
538 printf("%s\n", GEANY_DATADIR);
539 printf("%s\n", GEANY_LIBDIR);
540 printf("%s\n", GEANY_LOCALEDIR);
541 exit(0);
544 app->debug_mode = verbose_mode;
546 #ifdef G_OS_WIN32
547 win32_init_debug_code();
548 #endif
550 if (alternate_config)
552 geany_debug("alternate config: %s", alternate_config);
553 app->configdir = alternate_config;
555 else
557 app->configdir = g_build_filename(g_get_user_config_dir(), "geany", NULL);
560 if (generate_tags)
562 gboolean ret;
564 document_init_doclist();
565 filetypes_init_types();
566 filetypes_read_extensions(); /* needed for *.lang.tags filetype matching */
567 ret = symbols_generate_global_tags(*argc, *argv, ! no_preprocessing);
568 filetypes_free_types();
569 document_finalize();
570 exit(ret);
573 if (ft_names)
575 print_filetypes();
576 exit(0);
579 #ifdef HAVE_SOCKET
580 socket_info.ignore_socket = cl_options.new_instance;
581 if (cl_options.socket_filename)
583 socket_info.file_name = cl_options.socket_filename;
585 #endif
587 #ifdef HAVE_VTE
588 vte_info.lib_vte = lib_vte;
589 #endif
590 cl_options.ignore_global_tags = ignore_global_tags;
592 if (! gtk_init_check(NULL, NULL))
593 { /* check whether we have a valid X display and exit if not */
594 g_printerr("Geany: cannot open display\n");
595 exit(1);
600 static gint create_config_dir(void)
602 gint saved_errno = 0;
603 gchar *conf_file = g_build_filename(app->configdir, "geany.conf", NULL);
604 gchar *filedefs_dir = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
606 gchar *templates_dir = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
608 if (! g_file_test(app->configdir, G_FILE_TEST_EXISTS))
610 #ifndef G_OS_WIN32
611 /* if we are *not* using an alternate config directory, we check whether the old one
612 * in ~/.geany still exists and try to move it */
613 if (alternate_config == NULL)
615 gchar *old_dir = g_build_filename(g_get_home_dir(), ".geany", NULL);
616 /* move the old config dir if it exists */
617 if (g_file_test(old_dir, G_FILE_TEST_EXISTS))
619 if (! dialogs_show_question_full(main_widgets.window,
620 GTK_STOCK_YES, GTK_STOCK_QUIT, _("Move it now?"),
621 "%s",
622 _("Geany needs to move your old configuration directory before starting.")))
623 exit(0);
625 if (! g_file_test(app->configdir, G_FILE_TEST_IS_DIR))
626 utils_mkdir(app->configdir, TRUE);
628 if (g_rename(old_dir, app->configdir) == 0)
630 dialogs_show_msgbox(GTK_MESSAGE_INFO,
631 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
632 old_dir, app->configdir);
633 g_free(old_dir);
634 return 0;
636 else
638 dialogs_show_msgbox(GTK_MESSAGE_WARNING,
639 /* for translators: the third %s in brackets is the error message which
640 * describes why moving the dir didn't work */
641 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
642 "Please move manually the directory to the new location."),
643 old_dir, app->configdir, g_strerror(errno));
646 g_free(old_dir);
648 #endif
649 geany_debug("creating config directory %s", app->configdir);
650 saved_errno = utils_mkdir(app->configdir, TRUE);
653 if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS))
654 { /* check whether geany.conf can be written */
655 saved_errno = utils_is_file_writeable(app->configdir);
658 /* make subdir for filetype definitions */
659 if (saved_errno == 0)
661 gchar *filedefs_readme = g_build_filename(app->configdir,
662 GEANY_FILEDEFS_SUBDIR, "filetypes.README", NULL);
664 if (! g_file_test(filedefs_dir, G_FILE_TEST_EXISTS))
666 saved_errno = utils_mkdir(filedefs_dir, FALSE);
668 if (saved_errno == 0 && ! g_file_test(filedefs_readme, G_FILE_TEST_EXISTS))
670 gchar *text = g_strconcat(
671 "Copy files from ", app->datadir, " to this directory to overwrite "
672 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
673 "the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").", NULL);
674 utils_write_file(filedefs_readme, text);
675 g_free(text);
677 g_free(filedefs_readme);
680 /* make subdir for template files */
681 if (saved_errno == 0)
683 gchar *templates_readme = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR,
684 "templates.README", NULL);
686 if (! g_file_test(templates_dir, G_FILE_TEST_EXISTS))
688 saved_errno = utils_mkdir(templates_dir, FALSE);
690 if (saved_errno == 0 && ! g_file_test(templates_readme, G_FILE_TEST_EXISTS))
692 gchar *text = g_strconcat(
693 "There are several template files in this directory. For these templates you can use wildcards.\n\
694 For more information read the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").",
695 NULL);
696 utils_write_file(templates_readme, text);
697 g_free(text);
699 g_free(templates_readme);
702 g_free(filedefs_dir);
703 g_free(templates_dir);
704 g_free(conf_file);
706 return saved_errno;
710 /* Returns 0 if config dir is OK. */
711 static gint setup_config_dir(void)
713 gint mkdir_result = 0;
715 /* convert configdir to locale encoding to avoid troubles */
716 setptr(app->configdir, utils_get_locale_from_utf8(app->configdir));
718 mkdir_result = create_config_dir();
719 if (mkdir_result != 0)
721 if (! dialogs_show_question(
722 _("Configuration directory could not be created (%s).\nThere could be some problems "
723 "using Geany without a configuration directory.\nStart Geany anyway?"),
724 g_strerror(mkdir_result)))
726 exit(0);
729 /* make configdir a real path */
730 if (g_file_test(app->configdir, G_FILE_TEST_EXISTS))
731 setptr(app->configdir, tm_get_real_path(app->configdir));
733 return mkdir_result;
737 static void signal_cb(gint sig)
739 if (sig == SIGTERM)
741 on_exit_clicked(NULL, NULL);
746 /* Used for command-line arguments at startup or from socket.
747 * this will strip any :line:col filename suffix from locale_filename */
748 gboolean main_handle_filename(const gchar *locale_filename)
750 GeanyDocument *doc;
751 gint line = -1, column = -1;
752 gchar *filename;
754 g_return_val_if_fail(locale_filename, FALSE);
756 /* check whether the passed filename is an URI */
757 filename = utils_get_path_from_uri(locale_filename);
758 if (filename == NULL)
759 return FALSE;
761 get_line_and_column_from_filename(filename, &line, &column);
762 if (line >= 0)
763 cl_options.goto_line = line;
764 if (column >= 0)
765 cl_options.goto_column = column;
767 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
769 doc = document_open_file(filename, FALSE, NULL, NULL);
770 /* add recent file manually if opening_session_files is set */
771 if (doc != NULL && main_status.opening_session_files)
772 ui_add_recent_file(doc->file_name);
773 g_free(filename);
774 return TRUE;
776 else if (file_prefs.cmdline_new_files)
777 { /* create new file with the given filename */
778 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
780 doc = document_new_file(utf8_filename, NULL, NULL);
781 if (doc != NULL)
782 ui_add_recent_file(doc->file_name);
783 g_free(utf8_filename);
784 g_free(filename);
785 return TRUE;
787 g_free(filename);
788 return FALSE;
792 /* open files from command line */
793 static gboolean open_cl_files(gint argc, gchar **argv)
795 gint i;
797 if (argc <= 1) return FALSE;
799 for (i = 1; i < argc; i++)
801 gchar *filename = main_get_argv_filename(argv[i]);
803 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
805 g_free(filename);
806 continue;
809 #ifdef G_OS_WIN32
810 /* It seems argv elements are encoded in CP1252 on a German Windows */
811 setptr(filename, g_locale_to_utf8(filename, -1, NULL, NULL, NULL));
812 #endif
813 if (filename && ! main_handle_filename(filename))
815 const gchar *msg = _("Could not find file '%s'.");
817 g_printerr(msg, filename); /* also print to the terminal */
818 g_printerr("\n");
819 ui_set_statusbar(TRUE, msg, filename);
821 g_free(filename);
823 return TRUE;
827 static void load_session_project_file(void)
829 gchar *locale_filename;
831 g_return_if_fail(project_prefs.session_file != NULL);
833 locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
835 if (NZV(locale_filename))
836 project_load_file(locale_filename);
838 g_free(locale_filename);
839 g_free(project_prefs.session_file); /* no longer needed */
843 static void load_settings(void)
845 configuration_load();
846 /* let cmdline options overwrite configuration settings */
847 #ifdef HAVE_VTE
848 vte_info.have_vte = (no_vte) ? FALSE : vte_info.load_vte;
849 #endif
850 if (no_msgwin)
851 ui_prefs.msgwindow_visible = FALSE;
853 #ifdef HAVE_PLUGINS
854 want_plugins = prefs.load_plugins && !no_plugins;
855 #endif
859 void main_load_project_from_command_line(const gchar *locale_filename, gboolean use_session)
861 gchar *pfile = NULL;
863 if (utils_is_uri(locale_filename))
864 pfile = utils_get_path_from_uri(locale_filename);
865 else
866 pfile = g_strdup(locale_filename);
868 if (pfile != NULL)
870 if (use_session)
871 project_load_file_with_session(pfile);
872 else
873 project_load_file(pfile);
875 g_free(pfile);
879 static void load_startup_files(gint argc, gchar **argv)
881 gboolean load_project_from_cl = FALSE;
883 /* ATM when opening a project file any other filenames are ignored */
884 load_project_from_cl = (argc > 1) && g_str_has_suffix(argv[1], ".geany");
885 if (load_project_from_cl && argc > 2)
886 g_print("Ignoring extra filenames after %s", argv[1]);
888 if (load_project_from_cl || ! open_cl_files(argc, argv))
890 if (prefs.load_session)
892 if (load_project_from_cl)
894 main_load_project_from_command_line(argv[1], FALSE);
896 else if (cl_options.load_session && !cl_options.new_instance)
897 load_session_project_file();
899 /* when we want a new instance, we still load project session files unless -s
900 * was passed */
901 if (!cl_options.load_session || (!load_project_from_cl && cl_options.new_instance))
902 return;
904 /* load session files into tabs, as they are found in the session_files variable */
905 configuration_open_files();
907 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
909 ui_update_popup_copy_items(NULL);
910 ui_update_popup_reundo_items(NULL);
917 static gboolean send_startup_complete(gpointer data)
919 g_signal_emit_by_name(geany_object, "geany-startup-complete");
920 return FALSE;
924 static const gchar *get_locale(void)
926 const gchar *locale = "unknown";
927 #ifdef HAVE_LOCALE_H
928 locale = setlocale(LC_CTYPE, NULL);
929 #endif
930 return locale;
934 gint main(gint argc, gchar **argv)
936 GeanyDocument *doc;
937 gint config_dir_result;
938 const gchar *locale;
940 log_handlers_init();
942 app = g_new0(GeanyApp, 1);
943 memset(&main_status, 0, sizeof(GeanyStatus));
944 memset(&prefs, 0, sizeof(GeanyPrefs));
945 memset(&interface_prefs, 0, sizeof(GeanyInterfacePrefs));
946 memset(&toolbar_prefs, 0, sizeof(GeanyToolbarPrefs));
947 memset(&file_prefs, 0, sizeof(GeanyFilePrefs));
948 memset(&search_prefs, 0, sizeof(GeanySearchPrefs));
949 memset(&tool_prefs, 0, sizeof(GeanyToolPrefs));
950 memset(&template_prefs, 0, sizeof(GeanyTemplatePrefs));
951 memset(&ui_prefs, 0, sizeof(UIPrefs));
952 memset(&ui_widgets, 0, sizeof(UIWidgets));
954 setup_paths();
955 #ifdef ENABLE_NLS
956 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
957 #endif
958 parse_command_line_options(&argc, &argv);
960 signal(SIGTERM, signal_cb);
961 #ifdef G_OS_UNIX
962 /* SIGQUIT is used to kill spawned children and we get also this signal, so ignore */
963 signal(SIGQUIT, SIG_IGN);
964 /* ignore SIGPIPE signal for preventing sudden death of program */
965 signal(SIGPIPE, SIG_IGN);
966 #endif
968 config_dir_result = setup_config_dir();
969 #ifdef HAVE_SOCKET
970 /* check and create (unix domain) socket for remote operation */
971 if (! socket_info.ignore_socket)
973 socket_info.lock_socket = -1;
974 socket_info.lock_socket_tag = 0;
975 socket_info.lock_socket = socket_init(argc, argv);
976 /* Socket exists */
977 if (socket_info.lock_socket == -2)
979 /* Quit if filenames were sent to first instance or the list of open
980 * documents has been sent */
981 if (argc > 1 || cl_options.list_documents)
983 gdk_notify_startup_complete();
984 g_free(app->configdir);
985 g_free(app->datadir);
986 g_free(app->docdir);
987 g_free(app);
988 return 0;
990 /* Start a new instance if no command line strings were passed */
991 socket_info.ignore_socket = TRUE;
992 cl_options.new_instance = TRUE;
995 #endif
997 locale = get_locale();
998 geany_debug("Geany %s, %s",
999 main_get_version_string(),
1000 locale);
1001 geany_debug(geany_lib_versions,
1002 gtk_major_version, gtk_minor_version, gtk_micro_version,
1003 glib_major_version, glib_minor_version, glib_micro_version);
1004 geany_debug("System data dir: %s", app->datadir);
1005 geany_debug("User config dir: %s", app->configdir);
1007 /* create the object so Geany signals can be connected in init() functions */
1008 geany_object = geany_object_new();
1010 /* inits */
1011 main_init();
1013 encodings_init();
1014 editor_init();
1016 /* init stash groups before loading keyfile */
1017 configuration_init();
1018 ui_init_prefs();
1019 search_init();
1020 project_init();
1021 #ifdef HAVE_PLUGINS
1022 plugins_init();
1023 #endif
1024 sidebar_init();
1025 load_settings(); /* load keyfile */
1027 highlighting_init();
1028 msgwin_init();
1029 build_init();
1030 ui_create_insert_menu_items();
1031 ui_create_insert_date_menu_items();
1032 keybindings_init();
1033 tools_create_insert_custom_command_menu_items();
1034 notebook_init();
1035 filetypes_init();
1036 templates_init();
1037 navqueue_init();
1038 document_init_doclist();
1039 symbols_init();
1040 filetypes_read_extensions();
1041 editor_snippets_init();
1043 /* set window icon */
1045 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
1046 gtk_window_set_icon(GTK_WINDOW(main_widgets.window), pb);
1047 g_object_unref(pb); /* free our reference */
1050 /* registering some basic events */
1051 g_signal_connect(main_widgets.window, "delete-event", G_CALLBACK(on_exit_clicked), NULL);
1052 g_signal_connect(main_widgets.window, "window-state-event", G_CALLBACK(on_window_state_event), NULL);
1054 g_signal_connect(msgwindow.scribble, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
1056 #ifdef HAVE_VTE
1057 vte_init();
1058 #endif
1059 ui_create_recent_menus();
1061 ui_set_statusbar(TRUE, _("This is Geany %s."), main_get_version_string());
1062 if (config_dir_result != 0)
1063 ui_set_statusbar(TRUE, _("Configuration directory could not be created (%s)."),
1064 g_strerror(config_dir_result));
1066 /* apply all configuration options */
1067 apply_settings();
1069 #ifdef HAVE_PLUGINS
1070 /* load any enabled plugins before we open any documents */
1071 if (want_plugins)
1072 plugins_load_active();
1073 #endif
1075 ui_sidebar_show_hide();
1077 /* set the active sidebar page after plugins have been loaded */
1078 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), ui_prefs.sidebar_page);
1080 /* load keybinding settings after plugins have added their groups */
1081 keybindings_load_keyfile();
1083 /* load any command line files or session files */
1084 main_status.opening_session_files = TRUE;
1085 load_startup_files(argc, argv);
1086 main_status.opening_session_files = FALSE;
1088 /* open a new file if no other file was opened */
1089 document_new_file_if_non_open();
1091 ui_document_buttons_update();
1092 ui_save_buttons_toggle(FALSE);
1094 doc = document_get_current();
1095 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1096 sidebar_select_openfiles_item(doc);
1097 build_menu_update(doc);
1098 sidebar_update_tag_list(doc, FALSE);
1100 #ifdef G_OS_WIN32
1101 /* Manually realise the main window to be able to set the position but don't show it.
1102 * We don't set the position after showing the window to avoid flickering. */
1103 gtk_widget_realize(main_widgets.window);
1104 #endif
1105 setup_window_position();
1107 /* finally show the window */
1108 gtk_widget_show(main_widgets.window);
1109 main_status.main_window_realized = TRUE;
1111 configuration_apply_settings();
1113 #ifdef HAVE_SOCKET
1114 /* register the callback of socket input */
1115 if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
1117 socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
1118 socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,
1119 G_IO_IN | G_IO_PRI | G_IO_ERR, socket_lock_input_cb, main_widgets.window);
1121 #endif
1123 #ifdef G_OS_WIN32
1125 gchar *dir;
1126 /* On Windows, change the working directory to the Geany installation path to not lock
1127 * the directory of a file passed as command line argument (see bug #2626124). */
1128 dir = win32_get_installation_dir();
1129 win32_set_working_directory(dir);
1130 g_free(dir);
1132 #endif
1134 /* when we are really done with setting everything up and the main event loop is running,
1135 * tell other components, mainly plugins, that startup is complete */
1136 g_idle_add_full(G_PRIORITY_LOW, send_startup_complete, NULL, NULL);
1138 gtk_main();
1139 return 0;
1143 static void queue_free(GQueue *queue)
1145 while (! g_queue_is_empty(queue))
1147 g_free(g_queue_pop_tail(queue));
1149 g_queue_free(queue);
1153 void main_quit()
1155 geany_debug("Quitting...");
1157 #ifdef HAVE_SOCKET
1158 socket_finalize();
1159 #endif
1161 #ifdef HAVE_PLUGINS
1162 plugins_finalize();
1163 #endif
1165 navqueue_free();
1166 keybindings_free();
1167 filetypes_save_commands();
1168 highlighting_free_styles();
1169 templates_free_templates();
1170 msgwin_finalize();
1171 search_finalize();
1172 build_finalize();
1173 document_finalize();
1174 symbols_finalize();
1175 project_finalize();
1176 editor_finalize();
1177 editor_snippets_free();
1178 encodings_finalize();
1179 toolbar_finalize();
1180 sidebar_finalize();
1181 configuration_finalize();
1182 filetypes_free_types();
1183 ui_finalize();
1184 log_finalize();
1186 tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
1187 g_free(app->configdir);
1188 g_free(app->datadir);
1189 g_free(app->docdir);
1190 g_free(prefs.default_open_path);
1191 g_free(prefs.custom_plugin_path);
1192 g_free(ui_prefs.custom_date_format);
1193 g_free(interface_prefs.editor_font);
1194 g_free(interface_prefs.tagbar_font);
1195 g_free(interface_prefs.msgwin_font);
1196 g_free(editor_prefs.long_line_color);
1197 g_free(editor_prefs.comment_toggle_mark);
1198 g_free(editor_prefs.color_scheme);
1199 g_free(tool_prefs.context_action_cmd);
1200 g_free(template_prefs.developer);
1201 g_free(template_prefs.company);
1202 g_free(template_prefs.mail);
1203 g_free(template_prefs.initials);
1204 g_free(template_prefs.version);
1205 g_free(tool_prefs.term_cmd);
1206 g_free(tool_prefs.browser_cmd);
1207 g_free(tool_prefs.grep_cmd);
1208 g_free(printing_prefs.external_print_cmd);
1209 g_free(printing_prefs.page_header_datefmt);
1210 g_strfreev(ui_prefs.custom_commands);
1212 queue_free(ui_prefs.recent_queue);
1213 queue_free(ui_prefs.recent_projects_queue);
1215 if (ui_widgets.prefs_dialog && GTK_IS_WIDGET(ui_widgets.prefs_dialog)) gtk_widget_destroy(ui_widgets.prefs_dialog);
1216 if (ui_widgets.save_filesel && GTK_IS_WIDGET(ui_widgets.save_filesel)) gtk_widget_destroy(ui_widgets.save_filesel);
1217 if (ui_widgets.open_filesel && GTK_IS_WIDGET(ui_widgets.open_filesel)) gtk_widget_destroy(ui_widgets.open_filesel);
1218 if (ui_widgets.open_fontsel && GTK_IS_WIDGET(ui_widgets.open_fontsel)) gtk_widget_destroy(ui_widgets.open_fontsel);
1219 if (ui_widgets.open_colorsel && GTK_IS_WIDGET(ui_widgets.open_colorsel)) gtk_widget_destroy(ui_widgets.open_colorsel);
1220 #ifdef HAVE_VTE
1221 if (vte_info.have_vte) vte_close();
1222 g_free(vte_info.lib_vte);
1223 g_free(vte_info.dir);
1224 #endif
1225 gtk_widget_destroy(main_widgets.window);
1227 /* destroy popup menus */
1228 if (main_widgets.editor_menu && GTK_IS_WIDGET(main_widgets.editor_menu))
1229 gtk_widget_destroy(main_widgets.editor_menu);
1230 if (ui_widgets.toolbar_menu && GTK_IS_WIDGET(ui_widgets.toolbar_menu))
1231 gtk_widget_destroy(ui_widgets.toolbar_menu);
1232 if (msgwindow.popup_status_menu && GTK_IS_WIDGET(msgwindow.popup_status_menu))
1233 gtk_widget_destroy(msgwindow.popup_status_menu);
1234 if (msgwindow.popup_msg_menu && GTK_IS_WIDGET(msgwindow.popup_msg_menu))
1235 gtk_widget_destroy(msgwindow.popup_msg_menu);
1236 if (msgwindow.popup_compiler_menu && GTK_IS_WIDGET(msgwindow.popup_compiler_menu))
1237 gtk_widget_destroy(msgwindow.popup_compiler_menu);
1239 g_object_unref(geany_object);
1240 geany_object = NULL;
1242 g_free(app);
1244 gtk_main_quit();
1249 * Reloads most of Geany's configuration files without restarting. Currently the following
1250 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1251 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1252 * and 'settings' and 'build_settings' sections of the filetype definition files.
1254 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1255 * editor plugin).
1257 * @since 0.15
1259 void main_reload_configuration(void)
1261 /* reload templates */
1262 templates_free_templates();
1263 templates_init();
1265 /* reload snippets */
1266 editor_snippets_free();
1267 editor_snippets_init();
1269 /* reload filetype extensions */
1270 filetypes_read_extensions();
1272 filetypes_reload();
1274 /* C tag names to ignore */
1275 symbols_reload_config_files();
1277 ui_set_statusbar(TRUE, _("Configuration files reloaded."));