Recognize #!/bin/dash as shebang for Shell files (closes #3470986)
[geany-mirror.git] / src / main.c
blob5ebad26aff35a447e4f2d118f40dbbae7a1bd0d1
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 "support.h"
47 #include "callbacks.h"
48 #include "log.h"
49 #include "ui_utils.h"
50 #include "utils.h"
51 #include "document.h"
52 #include "filetypes.h"
53 #include "keyfile.h"
54 #include "win32.h"
55 #include "msgwindow.h"
56 #include "dialogs.h"
57 #include "templates.h"
58 #include "encodings.h"
59 #include "sidebar.h"
60 #include "notebook.h"
61 #include "keybindings.h"
62 #include "editor.h"
63 #include "search.h"
64 #include "build.h"
65 #include "highlighting.h"
66 #include "symbols.h"
67 #include "project.h"
68 #include "tools.h"
69 #include "navqueue.h"
70 #include "plugins.h"
71 #include "printing.h"
72 #include "toolbar.h"
73 #include "geanyobject.h"
75 #ifdef HAVE_SOCKET
76 # include "socket.h"
77 #endif
79 #ifdef HAVE_VTE
80 # include "vte.h"
81 #endif
83 #ifndef N_
84 # define N_(String) (String)
85 #endif
88 GeanyApp *app;
89 gboolean ignore_callback; /* hack workaround for GTK+ toggle button callback problem */
91 GeanyStatus main_status;
92 CommandLineOptions cl_options; /* fields initialised in parse_command_line_options */
95 static const gchar geany_lib_versions[] = "GTK %u.%u.%u, GLib %u.%u.%u";
97 static gboolean want_plugins;
99 /* command-line options */
100 static gboolean verbose_mode = FALSE;
101 static gboolean ignore_global_tags = FALSE;
102 static gboolean no_msgwin = FALSE;
103 static gboolean show_version = FALSE;
104 static gchar *alternate_config = NULL;
105 #ifdef HAVE_VTE
106 static gboolean no_vte = FALSE;
107 static gchar *lib_vte = NULL;
108 #endif
109 static gboolean generate_tags = FALSE;
110 static gboolean no_preprocessing = FALSE;
111 static gboolean ft_names = FALSE;
112 static gboolean print_prefix = FALSE;
113 #ifdef HAVE_PLUGINS
114 static gboolean no_plugins = FALSE;
115 #endif
116 static gboolean dummy = FALSE;
118 /* in alphabetical order of short options */
119 static GOptionEntry entries[] =
121 { "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 },
122 { "config", 'c', 0, G_OPTION_ARG_FILENAME, &alternate_config, N_("Use an alternate configuration directory"), NULL },
123 { "ft-names", 0, 0, G_OPTION_ARG_NONE, &ft_names, N_("Print internal filetype names"), NULL },
124 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE, &generate_tags, N_("Generate global tags file (see documentation)"), NULL },
125 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE, &no_preprocessing, N_("Don't preprocess C/C++ files when generating tags"), NULL },
126 #ifdef HAVE_SOCKET
127 { "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 },
128 { "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 },
129 { "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 },
130 #endif
131 { "line", 'l', 0, G_OPTION_ARG_INT, &cl_options.goto_line, N_("Set initial line number for the first opened file"), NULL },
132 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE, &no_msgwin, N_("Don't show message window at startup"), NULL },
133 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE, &ignore_global_tags, N_("Don't load auto completion data (see documentation)"), NULL },
134 #ifdef HAVE_PLUGINS
135 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE, &no_plugins, N_("Don't load plugins"), NULL },
136 #endif
137 { "print-prefix", 0, 0, G_OPTION_ARG_NONE, &print_prefix, N_("Print Geany's installation prefix"), NULL },
138 { "read-only", 'r', 0, G_OPTION_ARG_NONE, &cl_options.readonly, N_("Open all FILES in read-only mode (see documention)"), NULL },
139 { "no-session", 's', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &cl_options.load_session, N_("Don't load the previous session's files"), NULL },
140 #ifdef HAVE_VTE
141 { "no-terminal", 't', 0, G_OPTION_ARG_NONE, &no_vte, N_("Don't load terminal support"), NULL },
142 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME, &lib_vte, N_("Filename of libvte.so"), NULL },
143 #endif
144 { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_mode, N_("Be verbose"), NULL },
145 { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Show version and exit"), NULL },
146 { "dummy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &dummy, NULL, NULL }, /* for +NNN line number arguments */
147 { NULL, 0, 0, 0, NULL, NULL, NULL }
151 static void setup_window_position(void)
153 /* interprets the saved window geometry */
154 if (!prefs.save_winpos)
155 return;
157 if (ui_prefs.geometry[0] != -1 && ui_prefs.geometry[1] != -1)
158 gtk_window_move(GTK_WINDOW(main_widgets.window),
159 ui_prefs.geometry[0], ui_prefs.geometry[1]);
161 if (ui_prefs.geometry[2] != -1 && ui_prefs.geometry[3] != -1)
162 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
163 ui_prefs.geometry[2], ui_prefs.geometry[3]);
165 if (ui_prefs.geometry[4] == 1)
166 gtk_window_maximize(GTK_WINDOW(main_widgets.window));
170 /* special things for the initial setup of the checkboxes and related stuff
171 * an action on a setting is only performed if the setting is not equal to the program default
172 * (all the following code is not perfect but it works for the moment) */
173 static void apply_settings(void)
175 ui_update_fold_items();
177 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
178 toolbar_show_hide();
179 if (! ui_prefs.msgwindow_visible)
181 ignore_callback = TRUE;
182 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_messages_window1")), FALSE);
183 gtk_widget_hide(ui_lookup_widget(main_widgets.window, "scrolledwindow1"));
184 ignore_callback = FALSE;
186 if (! ui_prefs.sidebar_visible)
188 ignore_callback = TRUE;
189 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_show_sidebar1")), FALSE);
190 ignore_callback = FALSE;
193 toolbar_apply_settings();
194 toolbar_update_ui();
196 ui_update_view_editor_menu_items();
198 /* hide statusbar if desired */
199 if (! interface_prefs.statusbar_visible)
201 gtk_widget_hide(ui_widgets.statusbar);
204 /* set the tab placements of the notebooks */
205 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.tab_pos_editor);
206 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow.notebook), interface_prefs.tab_pos_msgwin);
207 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets.sidebar_notebook), interface_prefs.tab_pos_sidebar);
209 /* whether to show notebook tabs or not */
210 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets.notebook), interface_prefs.show_notebook_tabs);
212 #ifdef HAVE_VTE
213 if (! vte_info.have_vte)
214 #endif
216 gtk_widget_set_sensitive(
217 ui_lookup_widget(main_widgets.window, "send_selection_to_vte1"), FALSE);
220 if (interface_prefs.sidebar_pos != GTK_POS_LEFT)
221 ui_swap_sidebar_pos();
225 static void main_init(void)
227 /* inits */
228 ui_init_builder();
230 main_widgets.window = NULL;
231 app->project = NULL;
232 ui_widgets.open_fontsel = NULL;
233 ui_widgets.open_colorsel = NULL;
234 ui_widgets.prefs_dialog = NULL;
235 main_status.main_window_realized = FALSE;
236 file_prefs.tab_order_ltr = FALSE;
237 file_prefs.tab_order_beside = FALSE;
238 main_status.quitting = FALSE;
239 ignore_callback = FALSE;
240 app->tm_workspace = tm_get_workspace();
241 ui_prefs.recent_queue = g_queue_new();
242 ui_prefs.recent_projects_queue = g_queue_new();
243 main_status.opening_session_files = FALSE;
245 ui_init_stock_items();
247 main_widgets.window = create_window1();
249 /* add recent projects to the Project menu */
250 ui_widgets.recent_projects_menuitem = ui_lookup_widget(main_widgets.window, "recent_projects1");
251 ui_widgets.recent_projects_menu_menubar = gtk_menu_new();
252 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets.recent_projects_menuitem),
253 ui_widgets.recent_projects_menu_menubar);
255 /* store important pointers for later reference */
256 main_widgets.toolbar = toolbar_init();
257 main_widgets.sidebar_notebook = ui_lookup_widget(main_widgets.window, "notebook3");
258 main_widgets.notebook = ui_lookup_widget(main_widgets.window, "notebook1");
259 main_widgets.editor_menu = create_edit_menu1();
260 main_widgets.tools_menu = ui_lookup_widget(main_widgets.window, "tools1_menu");
261 main_widgets.message_window_notebook = ui_lookup_widget(main_widgets.window, "notebook_info");
262 main_widgets.project_menu = ui_lookup_widget(main_widgets.window, "menu_project1_menu");
264 ui_widgets.toolbar_menu = create_toolbar_popup_menu1();
265 ui_init();
267 /* set widget names for matching with .gtkrc-2.0 */
268 gtk_widget_set_name(main_widgets.window, "GeanyMainWindow");
269 gtk_widget_set_name(ui_widgets.toolbar_menu, "GeanyToolbarMenu");
270 gtk_widget_set_name(main_widgets.editor_menu, "GeanyEditMenu");
271 gtk_widget_set_name(ui_lookup_widget(main_widgets.window, "menubar1"), "GeanyMenubar");
272 gtk_widget_set_name(main_widgets.toolbar, "GeanyToolbar");
274 gtk_window_set_default_size(GTK_WINDOW(main_widgets.window),
275 GEANY_WINDOW_DEFAULT_WIDTH, GEANY_WINDOW_DEFAULT_HEIGHT);
279 const gchar *main_get_version_string(void)
281 static gchar full[] = VERSION " (git >= " REVISION ")";
283 if (utils_str_equal(REVISION, "-1"))
284 return VERSION;
285 else
286 return full;
290 /* get the full file path of a command-line argument
291 * N.B. the result should be freed and may contain '/../' or '/./ ' */
292 gchar *main_get_argv_filename(const gchar *filename)
294 gchar *result;
296 if (g_path_is_absolute(filename) || utils_is_uri(filename))
297 result = g_strdup(filename);
298 else
300 /* use current dir */
301 gchar *cur_dir = g_get_current_dir();
303 result = g_strjoin(
304 G_DIR_SEPARATOR_S, cur_dir, filename, NULL);
305 g_free(cur_dir);
307 return result;
311 /* get a :line:column specifier from the end of a filename (if present),
312 * return the line/column values, and remove the specifier from the string
313 * (Note that *line and *column must both be set to -1 initially) */
314 static void get_line_and_column_from_filename(gchar *filename, gint *line, gint *column)
316 gsize i;
317 gint colon_count = 0;
318 gboolean have_number = FALSE;
319 gsize len;
321 g_assert(*line == -1 && *column == -1);
323 if (G_UNLIKELY(! NZV(filename)))
324 return;
326 /* allow to open files like "test:0" */
327 if (g_file_test(filename, G_FILE_TEST_EXISTS))
328 return;
330 len = strlen(filename);
331 for (i = len - 1; i >= 1; i--)
333 gboolean is_colon = filename[i] == ':';
334 gboolean is_digit = g_ascii_isdigit(filename[i]);
336 if (! is_colon && ! is_digit)
337 break;
339 if (is_colon)
341 if (++colon_count > 1)
342 break; /* bail on 2+ colons in a row */
344 else
345 colon_count = 0;
347 if (is_digit)
348 have_number = TRUE;
350 if (is_colon && have_number)
352 gint number = atoi(&filename[i + 1]);
354 filename[i] = '\0';
355 have_number = FALSE;
357 *column = *line;
358 *line = number;
361 if (*column >= 0)
362 break; /* line and column are set, so we're done */
367 static void setup_paths(void)
369 gchar *data_dir;
370 gchar *doc_dir;
372 /* set paths */
373 #ifdef G_OS_WIN32
374 /* use the installation directory(the one where geany.exe is located) as the base for the
375 * documentation and data files */
376 gchar *install_dir = win32_get_installation_dir();
378 data_dir = g_strconcat(install_dir, "\\data", NULL); /* e.g. C:\Program Files\geany\data */
379 doc_dir = g_strconcat(install_dir, "\\doc", NULL);
381 g_free(install_dir);
382 #else
383 data_dir = g_strconcat(GEANY_DATADIR, "/geany", NULL); /* e.g. /usr/share/geany */
384 doc_dir = g_strconcat(GEANY_DOCDIR, "/html", NULL);
385 #endif
387 /* convert path names to locale encoding */
388 app->datadir = utils_get_locale_from_utf8(data_dir);
389 app->docdir = utils_get_locale_from_utf8(doc_dir);
391 g_free(data_dir);
392 g_free(doc_dir);
397 * Checks whether the main window has been realized.
398 * This is an easy indicator whether Geany is right now starting up (main window is not
399 * yet realized) or whether it has finished the startup process (main window is realized).
400 * This is because the main window is realized (i.e. actually drawn on the screen) at the
401 * end of the startup process.
403 * @note Maybe you want to use the @link pluginsignals.c @c "geany-startup-complete" signal @endlink
404 * to get notified about the completed startup process.
406 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
408 * @since 0.19
410 gboolean main_is_realized(void)
412 return main_status.main_window_realized;
417 * Initialises the gettext translation system.
418 * This is a convenience function to set up gettext for internationalisation support
419 * in external plugins. You should call this function early in @ref plugin_init().
420 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
421 * The codeset for the message translations is set to UTF-8.
423 * Note that this function only setups the gettext textdomain for you. You still have
424 * to adjust the build system of your plugin to get internationalisation support
425 * working properly.
427 * If you have already used @ref PLUGIN_SET_TRANSLATABLE_INFO() you
428 * don't need to call main_locale_init() again as it has already been done.
430 * @param locale_dir The location where the translation files should be searched. This is
431 * usually the @c LOCALEDIR macro, defined by the build system.
432 * E.g. @c $prefix/share/locale.
433 * Only used on non-Windows systems. On Windows, the directory is determined
434 * by @c g_win32_get_package_installation_directory().
435 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
436 * defined by the build system.
438 * @since 0.16
440 void main_locale_init(const gchar *locale_dir, const gchar *package)
442 gchar *l_locale_dir = NULL;
444 #ifdef HAVE_LOCALE_H
445 setlocale(LC_ALL, "");
446 #endif
448 #ifdef G_OS_WIN32
450 gchar *install_dir = win32_get_installation_dir();
451 /* e.g. C:\Program Files\geany\lib\locale */
452 l_locale_dir = g_strconcat(install_dir, "\\share\\locale", NULL);
453 g_free(install_dir);
455 #else
456 l_locale_dir = g_strdup(locale_dir);
457 #endif
459 bindtextdomain(package, l_locale_dir);
460 bind_textdomain_codeset(package, "UTF-8");
461 g_free(l_locale_dir);
465 static void print_filetypes(void)
467 const GSList *list, *node;
469 filetypes_init_types();
470 printf("Geany's filetype names:\n");
472 list = filetypes_get_sorted_by_name();
473 foreach_slist(node, list)
475 GeanyFiletype *ft = node->data;
477 printf("%s\n", ft->name);
479 filetypes_free_types();
483 static void wait_for_input_on_windows(void)
485 #ifdef G_OS_WIN32
486 if (verbose_mode)
488 geany_debug("Press any key to continue");
489 getchar();
491 #endif
495 static void parse_command_line_options(gint *argc, gchar ***argv)
497 GError *error = NULL;
498 GOptionContext *context;
499 gint i;
500 CommandLineOptions def_clo = {FALSE, NULL, TRUE, -1, -1, FALSE, FALSE, FALSE};
502 /* first initialise cl_options fields with default values */
503 cl_options = def_clo;
505 /* the GLib option parser can't handle the +NNN (line number) option,
506 * so we grab that here and replace it with a no-op */
507 for (i = 1; i < (*argc); i++)
509 if ((*argv)[i][0] != '+')
510 continue;
512 cl_options.goto_line = atoi((*argv)[i] + 1);
513 (*argv)[i] = "--dummy";
516 context = g_option_context_new(_("[FILES...]"));
517 g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
518 g_option_group_set_translation_domain(g_option_context_get_main_group(context), GETTEXT_PACKAGE);
519 g_option_context_add_group(context, gtk_get_option_group(FALSE));
520 g_option_context_parse(context, argc, argv, &error);
521 g_option_context_free(context);
523 if (error != NULL)
525 g_printerr("Geany: %s\n", error->message);
526 g_error_free(error);
527 exit(1);
530 app->debug_mode = verbose_mode;
532 #ifdef G_OS_WIN32
533 win32_init_debug_code();
534 #endif
536 if (show_version)
538 printf(PACKAGE " %s (", main_get_version_string());
539 /* note for translators: library versions are printed after this */
540 printf(_("built on %s with "), __DATE__);
541 printf(geany_lib_versions,
542 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
543 GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
544 printf(")\n");
545 wait_for_input_on_windows();
546 exit(0);
549 if (print_prefix)
551 printf("%s\n", GEANY_PREFIX);
552 printf("%s\n", GEANY_DATADIR);
553 printf("%s\n", GEANY_LIBDIR);
554 printf("%s\n", GEANY_LOCALEDIR);
555 wait_for_input_on_windows();
556 exit(0);
559 if (alternate_config)
561 geany_debug("alternate config: %s", alternate_config);
562 app->configdir = alternate_config;
564 else
566 app->configdir = g_build_filename(g_get_user_config_dir(), "geany", NULL);
569 if (generate_tags)
571 gboolean ret;
573 filetypes_init_types();
574 ret = symbols_generate_global_tags(*argc, *argv, ! no_preprocessing);
575 filetypes_free_types();
576 wait_for_input_on_windows();
577 exit(ret);
580 if (ft_names)
582 print_filetypes();
583 wait_for_input_on_windows();
584 exit(0);
587 #ifdef HAVE_SOCKET
588 socket_info.ignore_socket = cl_options.new_instance;
589 if (cl_options.socket_filename)
591 socket_info.file_name = cl_options.socket_filename;
593 #endif
595 #ifdef HAVE_VTE
596 vte_info.lib_vte = lib_vte;
597 #endif
598 cl_options.ignore_global_tags = ignore_global_tags;
600 if (! gtk_init_check(NULL, NULL))
601 { /* check whether we have a valid X display and exit if not */
602 g_printerr("Geany: cannot open display\n");
603 exit(1);
608 static gint create_config_dir(void)
610 gint saved_errno = 0;
611 gchar *conf_file = g_build_filename(app->configdir, "geany.conf", NULL);
612 gchar *filedefs_dir = g_build_filename(app->configdir, GEANY_FILEDEFS_SUBDIR, NULL);
614 gchar *templates_dir = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR, NULL);
616 if (! g_file_test(app->configdir, G_FILE_TEST_EXISTS))
618 #ifndef G_OS_WIN32
619 /* if we are *not* using an alternate config directory, we check whether the old one
620 * in ~/.geany still exists and try to move it */
621 if (alternate_config == NULL)
623 gchar *old_dir = g_build_filename(g_get_home_dir(), ".geany", NULL);
624 /* move the old config dir if it exists */
625 if (g_file_test(old_dir, G_FILE_TEST_EXISTS))
627 if (! dialogs_show_question_full(main_widgets.window,
628 GTK_STOCK_YES, GTK_STOCK_QUIT, _("Move it now?"),
629 "%s",
630 _("Geany needs to move your old configuration directory before starting.")))
631 exit(0);
633 if (! g_file_test(app->configdir, G_FILE_TEST_IS_DIR))
634 utils_mkdir(app->configdir, TRUE);
636 if (g_rename(old_dir, app->configdir) == 0)
638 dialogs_show_msgbox(GTK_MESSAGE_INFO,
639 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
640 old_dir, app->configdir);
641 g_free(old_dir);
642 return 0;
644 else
646 dialogs_show_msgbox(GTK_MESSAGE_WARNING,
647 /* for translators: the third %s in brackets is the error message which
648 * describes why moving the dir didn't work */
649 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
650 "Please move manually the directory to the new location."),
651 old_dir, app->configdir, g_strerror(errno));
654 g_free(old_dir);
656 #endif
657 geany_debug("creating config directory %s", app->configdir);
658 saved_errno = utils_mkdir(app->configdir, TRUE);
661 if (saved_errno == 0 && ! g_file_test(conf_file, G_FILE_TEST_EXISTS))
662 { /* check whether geany.conf can be written */
663 saved_errno = utils_is_file_writable(app->configdir);
666 /* make subdir for filetype definitions */
667 if (saved_errno == 0)
669 gchar *filedefs_readme = g_build_filename(app->configdir,
670 GEANY_FILEDEFS_SUBDIR, "filetypes.README", NULL);
672 if (! g_file_test(filedefs_dir, G_FILE_TEST_EXISTS))
674 saved_errno = utils_mkdir(filedefs_dir, FALSE);
676 if (saved_errno == 0 && ! g_file_test(filedefs_readme, G_FILE_TEST_EXISTS))
678 gchar *text = g_strconcat(
679 "Copy files from ", app->datadir, " to this directory to overwrite "
680 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
681 "the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").", NULL);
682 utils_write_file(filedefs_readme, text);
683 g_free(text);
685 g_free(filedefs_readme);
688 /* make subdir for template files */
689 if (saved_errno == 0)
691 gchar *templates_readme = g_build_filename(app->configdir, GEANY_TEMPLATES_SUBDIR,
692 "templates.README", NULL);
694 if (! g_file_test(templates_dir, G_FILE_TEST_EXISTS))
696 saved_errno = utils_mkdir(templates_dir, FALSE);
698 if (saved_errno == 0 && ! g_file_test(templates_readme, G_FILE_TEST_EXISTS))
700 gchar *text = g_strconcat(
701 "There are several template files in this directory. For these templates you can use wildcards.\n\
702 For more information read the documentation (in ", app->docdir, G_DIR_SEPARATOR_S "index.html or visit " GEANY_HOMEPAGE ").",
703 NULL);
704 utils_write_file(templates_readme, text);
705 g_free(text);
707 g_free(templates_readme);
710 g_free(filedefs_dir);
711 g_free(templates_dir);
712 g_free(conf_file);
714 return saved_errno;
718 /* Returns 0 if config dir is OK. */
719 static gint setup_config_dir(void)
721 gint mkdir_result = 0;
723 /* convert configdir to locale encoding to avoid troubles */
724 setptr(app->configdir, utils_get_locale_from_utf8(app->configdir));
726 mkdir_result = create_config_dir();
727 if (mkdir_result != 0)
729 if (! dialogs_show_question(
730 _("Configuration directory could not be created (%s).\nThere could be some problems "
731 "using Geany without a configuration directory.\nStart Geany anyway?"),
732 g_strerror(mkdir_result)))
734 exit(0);
737 /* make configdir a real path */
738 if (g_file_test(app->configdir, G_FILE_TEST_EXISTS))
739 setptr(app->configdir, tm_get_real_path(app->configdir));
741 return mkdir_result;
745 static void signal_cb(gint sig)
747 if (sig == SIGTERM)
749 on_exit_clicked(NULL, NULL);
754 /* Used for command-line arguments at startup or from socket.
755 * this will strip any :line:col filename suffix from locale_filename */
756 gboolean main_handle_filename(const gchar *locale_filename)
758 GeanyDocument *doc;
759 gint line = -1, column = -1;
760 gchar *filename;
762 g_return_val_if_fail(locale_filename, FALSE);
764 /* check whether the passed filename is an URI */
765 filename = utils_get_path_from_uri(locale_filename);
766 if (filename == NULL)
767 return FALSE;
769 get_line_and_column_from_filename(filename, &line, &column);
770 if (line >= 0)
771 cl_options.goto_line = line;
772 if (column >= 0)
773 cl_options.goto_column = column;
775 if (g_file_test(filename, G_FILE_TEST_IS_REGULAR))
777 doc = document_open_file(filename, cl_options.readonly, NULL, NULL);
778 /* add recent file manually if opening_session_files is set */
779 if (doc != NULL && main_status.opening_session_files)
780 ui_add_recent_document(doc);
781 g_free(filename);
782 return TRUE;
784 else if (file_prefs.cmdline_new_files)
785 { /* create new file with the given filename */
786 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
788 doc = document_new_file(utf8_filename, NULL, NULL);
789 if (doc != NULL)
790 ui_add_recent_document(doc);
791 g_free(utf8_filename);
792 g_free(filename);
793 return TRUE;
795 g_free(filename);
796 return FALSE;
800 /* open files from command line */
801 static gboolean open_cl_files(gint argc, gchar **argv)
803 gint i;
805 if (argc <= 1) return FALSE;
807 for (i = 1; i < argc; i++)
809 gchar *filename = main_get_argv_filename(argv[i]);
811 if (g_file_test(filename, G_FILE_TEST_IS_DIR))
813 g_free(filename);
814 continue;
817 #ifdef G_OS_WIN32
818 /* It seems argv elements are encoded in CP1252 on a German Windows */
819 setptr(filename, g_locale_to_utf8(filename, -1, NULL, NULL, NULL));
820 #endif
821 if (filename && ! main_handle_filename(filename))
823 const gchar *msg = _("Could not find file '%s'.");
825 g_printerr(msg, filename); /* also print to the terminal */
826 g_printerr("\n");
827 ui_set_statusbar(TRUE, msg, filename);
829 g_free(filename);
831 return TRUE;
835 static void load_session_project_file(void)
837 gchar *locale_filename;
839 g_return_if_fail(project_prefs.session_file != NULL);
841 locale_filename = utils_get_locale_from_utf8(project_prefs.session_file);
843 if (G_LIKELY(NZV(locale_filename)))
844 project_load_file(locale_filename);
846 g_free(locale_filename);
847 g_free(project_prefs.session_file); /* no longer needed */
851 static void load_settings(void)
853 configuration_load();
854 /* let cmdline options overwrite configuration settings */
855 #ifdef HAVE_VTE
856 vte_info.have_vte = (no_vte) ? FALSE : vte_info.load_vte;
857 #endif
858 if (no_msgwin)
859 ui_prefs.msgwindow_visible = FALSE;
861 #ifdef HAVE_PLUGINS
862 want_plugins = prefs.load_plugins && !no_plugins;
863 #endif
867 void main_load_project_from_command_line(const gchar *locale_filename, gboolean use_session)
869 gchar *pfile = NULL;
871 if (utils_is_uri(locale_filename))
872 pfile = utils_get_path_from_uri(locale_filename);
873 else
874 pfile = g_strdup(locale_filename);
876 if (pfile != NULL)
878 if (use_session)
879 project_load_file_with_session(pfile);
880 else
881 project_load_file(pfile);
883 g_free(pfile);
887 static void load_startup_files(gint argc, gchar **argv)
889 gboolean load_project_from_cl = FALSE;
891 /* ATM when opening a project file any other filenames are ignored */
892 load_project_from_cl = (argc > 1) && g_str_has_suffix(argv[1], ".geany");
893 if (load_project_from_cl && argc > 2)
894 g_print("Ignoring extra filenames after %s", argv[1]);
896 if (load_project_from_cl || ! open_cl_files(argc, argv))
898 if (prefs.load_session)
900 if (load_project_from_cl)
902 main_load_project_from_command_line(argv[1], FALSE);
904 else if (cl_options.load_session && !cl_options.new_instance)
905 load_session_project_file();
907 /* when we want a new instance, we still load project session files unless -s
908 * was passed */
909 if (!cl_options.load_session || (!load_project_from_cl && cl_options.new_instance))
910 return;
912 /* load session files into tabs, as they are found in the session_files variable */
913 configuration_open_files();
915 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
917 ui_update_popup_copy_items(NULL);
918 ui_update_popup_reundo_items(NULL);
925 static gboolean send_startup_complete(gpointer data)
927 g_signal_emit_by_name(geany_object, "geany-startup-complete");
928 return FALSE;
932 static const gchar *get_locale(void)
934 const gchar *locale = "unknown";
935 #ifdef HAVE_LOCALE_H
936 locale = setlocale(LC_CTYPE, NULL);
937 #endif
938 return locale;
942 gint main(gint argc, gchar **argv)
944 GeanyDocument *doc;
945 gint config_dir_result;
946 const gchar *locale;
948 log_handlers_init();
950 app = g_new0(GeanyApp, 1);
951 memset(&main_status, 0, sizeof(GeanyStatus));
952 memset(&prefs, 0, sizeof(GeanyPrefs));
953 memset(&interface_prefs, 0, sizeof(GeanyInterfacePrefs));
954 memset(&toolbar_prefs, 0, sizeof(GeanyToolbarPrefs));
955 memset(&file_prefs, 0, sizeof(GeanyFilePrefs));
956 memset(&search_prefs, 0, sizeof(GeanySearchPrefs));
957 memset(&tool_prefs, 0, sizeof(GeanyToolPrefs));
958 memset(&template_prefs, 0, sizeof(GeanyTemplatePrefs));
959 memset(&ui_prefs, 0, sizeof(UIPrefs));
960 memset(&ui_widgets, 0, sizeof(UIWidgets));
962 setup_paths();
963 #ifdef ENABLE_NLS
964 main_locale_init(GEANY_LOCALEDIR, GETTEXT_PACKAGE);
965 #endif
966 parse_command_line_options(&argc, &argv);
968 /* Initialize GLib's thread system in case any plugins want to use it or their
969 * dependencies (e.g. WebKit, Soup, ...) */
970 if (!g_thread_supported())
971 g_thread_init(NULL);
973 signal(SIGTERM, signal_cb);
974 #ifdef G_OS_UNIX
975 /* SIGQUIT is used to kill spawned children and we get also this signal, so ignore */
976 signal(SIGQUIT, SIG_IGN);
977 /* ignore SIGPIPE signal for preventing sudden death of program */
978 signal(SIGPIPE, SIG_IGN);
979 #endif
981 config_dir_result = setup_config_dir();
982 #ifdef HAVE_SOCKET
983 /* check and create (unix domain) socket for remote operation */
984 if (! socket_info.ignore_socket)
986 socket_info.lock_socket = -1;
987 socket_info.lock_socket_tag = 0;
988 socket_info.lock_socket = socket_init(argc, argv);
989 /* Socket exists */
990 if (socket_info.lock_socket == -2)
992 /* Quit if filenames were sent to first instance or the list of open
993 * documents has been sent */
994 if (argc > 1 || cl_options.list_documents)
996 gdk_notify_startup_complete();
997 g_free(app->configdir);
998 g_free(app->datadir);
999 g_free(app->docdir);
1000 g_free(app);
1001 return 0;
1003 /* Start a new instance if no command line strings were passed */
1004 socket_info.ignore_socket = TRUE;
1005 cl_options.new_instance = TRUE;
1008 #endif
1010 locale = get_locale();
1011 geany_debug("Geany %s, %s",
1012 main_get_version_string(),
1013 locale);
1014 geany_debug(geany_lib_versions,
1015 gtk_major_version, gtk_minor_version, gtk_micro_version,
1016 glib_major_version, glib_minor_version, glib_micro_version);
1017 geany_debug("System data dir: %s", app->datadir);
1018 geany_debug("User config dir: %s", app->configdir);
1020 /* create the object so Geany signals can be connected in init() functions */
1021 geany_object = geany_object_new();
1023 /* inits */
1024 main_init();
1026 encodings_init();
1027 editor_init();
1029 /* init stash groups before loading keyfile */
1030 configuration_init();
1031 ui_init_prefs();
1032 search_init();
1033 project_init();
1034 #ifdef HAVE_PLUGINS
1035 plugins_init();
1036 #endif
1037 sidebar_init();
1038 load_settings(); /* load keyfile */
1040 highlighting_init();
1041 msgwin_init();
1042 build_init();
1043 ui_create_insert_menu_items();
1044 ui_create_insert_date_menu_items();
1045 keybindings_init();
1046 notebook_init();
1047 filetypes_init();
1048 templates_init();
1049 navqueue_init();
1050 document_init_doclist();
1051 symbols_init();
1052 editor_snippets_init();
1054 /* set window icon */
1056 GdkPixbuf *pb = ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO);
1057 gtk_window_set_icon(GTK_WINDOW(main_widgets.window), pb);
1058 g_object_unref(pb); /* free our reference */
1061 /* registering some basic events */
1062 g_signal_connect(main_widgets.window, "delete-event", G_CALLBACK(on_exit_clicked), NULL);
1063 g_signal_connect(main_widgets.window, "window-state-event", G_CALLBACK(on_window_state_event), NULL);
1065 g_signal_connect(msgwindow.scribble, "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
1067 #ifdef HAVE_VTE
1068 vte_init();
1069 #endif
1070 ui_create_recent_menus();
1072 ui_set_statusbar(TRUE, _("This is Geany %s."), main_get_version_string());
1073 if (config_dir_result != 0)
1074 ui_set_statusbar(TRUE, _("Configuration directory could not be created (%s)."),
1075 g_strerror(config_dir_result));
1077 /* apply all configuration options */
1078 apply_settings();
1080 #ifdef HAVE_PLUGINS
1081 /* load any enabled plugins before we open any documents */
1082 if (want_plugins)
1083 plugins_load_active();
1084 #endif
1086 ui_sidebar_show_hide();
1088 /* set the active sidebar page after plugins have been loaded */
1089 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), ui_prefs.sidebar_page);
1091 /* load keybinding settings after plugins have added their groups */
1092 keybindings_load_keyfile();
1094 /* create the custom command menu after the keybindings have been loaded to have the proper
1095 * accelerator shown for the menu items */
1096 tools_create_insert_custom_command_menu_items();
1098 /* load any command line files or session files */
1099 main_status.opening_session_files = TRUE;
1100 load_startup_files(argc, argv);
1101 main_status.opening_session_files = FALSE;
1103 /* open a new file if no other file was opened */
1104 document_new_file_if_non_open();
1106 ui_document_buttons_update();
1107 ui_save_buttons_toggle(FALSE);
1109 doc = document_get_current();
1110 sidebar_select_openfiles_item(doc);
1111 build_menu_update(doc);
1112 sidebar_update_tag_list(doc, FALSE);
1114 #ifdef G_OS_WIN32
1115 /* Manually realise the main window to be able to set the position but don't show it.
1116 * We don't set the position after showing the window to avoid flickering. */
1117 gtk_widget_realize(main_widgets.window);
1118 #endif
1119 setup_window_position();
1121 /* finally show the window */
1122 document_grab_focus(doc);
1123 gtk_widget_show(main_widgets.window);
1124 main_status.main_window_realized = TRUE;
1126 configuration_apply_settings();
1128 #ifdef HAVE_SOCKET
1129 /* register the callback of socket input */
1130 if (! socket_info.ignore_socket && socket_info.lock_socket > 0)
1132 socket_info.read_ioc = g_io_channel_unix_new(socket_info.lock_socket);
1133 socket_info.lock_socket_tag = g_io_add_watch(socket_info.read_ioc,
1134 G_IO_IN | G_IO_PRI | G_IO_ERR, socket_lock_input_cb, main_widgets.window);
1136 #endif
1138 #ifdef G_OS_WIN32
1140 gchar *dir;
1141 /* On Windows, change the working directory to the Geany installation path to not lock
1142 * the directory of a file passed as command line argument (see bug #2626124). */
1143 dir = win32_get_installation_dir();
1144 win32_set_working_directory(dir);
1145 g_free(dir);
1147 #endif
1149 /* when we are really done with setting everything up and the main event loop is running,
1150 * tell other components, mainly plugins, that startup is complete */
1151 g_idle_add_full(G_PRIORITY_LOW, send_startup_complete, NULL, NULL);
1153 gtk_main();
1154 return 0;
1158 static void queue_free(GQueue *queue)
1160 while (! g_queue_is_empty(queue))
1162 g_free(g_queue_pop_tail(queue));
1164 g_queue_free(queue);
1168 void main_quit()
1170 geany_debug("Quitting...");
1172 #ifdef HAVE_SOCKET
1173 socket_finalize();
1174 #endif
1176 #ifdef HAVE_PLUGINS
1177 plugins_finalize();
1178 #endif
1180 navqueue_free();
1181 keybindings_free();
1182 notebook_free();
1183 highlighting_free_styles();
1184 templates_free_templates();
1185 msgwin_finalize();
1186 search_finalize();
1187 build_finalize();
1188 document_finalize();
1189 symbols_finalize();
1190 project_finalize();
1191 editor_finalize();
1192 editor_snippets_free();
1193 encodings_finalize();
1194 toolbar_finalize();
1195 sidebar_finalize();
1196 configuration_finalize();
1197 filetypes_free_types();
1198 ui_finalize();
1199 log_finalize();
1201 tm_workspace_free(TM_WORK_OBJECT(app->tm_workspace));
1202 g_free(app->configdir);
1203 g_free(app->datadir);
1204 g_free(app->docdir);
1205 g_free(prefs.default_open_path);
1206 g_free(prefs.custom_plugin_path);
1207 g_free(ui_prefs.custom_date_format);
1208 g_free(interface_prefs.editor_font);
1209 g_free(interface_prefs.tagbar_font);
1210 g_free(interface_prefs.msgwin_font);
1211 g_free(editor_prefs.long_line_color);
1212 g_free(editor_prefs.comment_toggle_mark);
1213 g_free(editor_prefs.color_scheme);
1214 g_free(tool_prefs.context_action_cmd);
1215 g_free(template_prefs.developer);
1216 g_free(template_prefs.company);
1217 g_free(template_prefs.mail);
1218 g_free(template_prefs.initials);
1219 g_free(template_prefs.version);
1220 g_free(tool_prefs.term_cmd);
1221 g_free(tool_prefs.browser_cmd);
1222 g_free(tool_prefs.grep_cmd);
1223 g_free(printing_prefs.external_print_cmd);
1224 g_free(printing_prefs.page_header_datefmt);
1225 g_strfreev(ui_prefs.custom_commands);
1227 queue_free(ui_prefs.recent_queue);
1228 queue_free(ui_prefs.recent_projects_queue);
1230 if (ui_widgets.prefs_dialog && GTK_IS_WIDGET(ui_widgets.prefs_dialog)) gtk_widget_destroy(ui_widgets.prefs_dialog);
1231 if (ui_widgets.open_fontsel && GTK_IS_WIDGET(ui_widgets.open_fontsel)) gtk_widget_destroy(ui_widgets.open_fontsel);
1232 if (ui_widgets.open_colorsel && GTK_IS_WIDGET(ui_widgets.open_colorsel)) gtk_widget_destroy(ui_widgets.open_colorsel);
1233 #ifdef HAVE_VTE
1234 if (vte_info.have_vte) vte_close();
1235 g_free(vte_info.lib_vte);
1236 g_free(vte_info.dir);
1237 #endif
1238 gtk_widget_destroy(main_widgets.window);
1240 /* destroy popup menus */
1241 if (main_widgets.editor_menu && GTK_IS_WIDGET(main_widgets.editor_menu))
1242 gtk_widget_destroy(main_widgets.editor_menu);
1243 if (ui_widgets.toolbar_menu && GTK_IS_WIDGET(ui_widgets.toolbar_menu))
1244 gtk_widget_destroy(ui_widgets.toolbar_menu);
1245 if (msgwindow.popup_status_menu && GTK_IS_WIDGET(msgwindow.popup_status_menu))
1246 gtk_widget_destroy(msgwindow.popup_status_menu);
1247 if (msgwindow.popup_msg_menu && GTK_IS_WIDGET(msgwindow.popup_msg_menu))
1248 gtk_widget_destroy(msgwindow.popup_msg_menu);
1249 if (msgwindow.popup_compiler_menu && GTK_IS_WIDGET(msgwindow.popup_compiler_menu))
1250 gtk_widget_destroy(msgwindow.popup_compiler_menu);
1252 g_object_unref(geany_object);
1253 geany_object = NULL;
1255 g_free(app);
1257 ui_finalize_builder();
1259 gtk_main_quit();
1264 * Reloads most of Geany's configuration files without restarting. Currently the following
1265 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1266 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1267 * and 'settings' and 'build_settings' sections of the filetype definition files.
1269 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1270 * editor plugin).
1272 * @since 0.15
1274 void main_reload_configuration(void)
1276 /* reload templates */
1277 templates_free_templates();
1278 templates_init();
1280 /* reload snippets */
1281 editor_snippets_free();
1282 editor_snippets_init();
1284 filetypes_reload_extensions();
1285 filetypes_reload();
1287 /* C tag names to ignore */
1288 symbols_reload_config_files();
1290 ui_set_statusbar(TRUE, _("Configuration files reloaded."));