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.
24 * Main program-related commands.
25 * Handles program initialization and cleanup.
30 #include <sys/types.h>
37 #include <glib/gstdio.h>
47 #include "callbacks.h"
52 #include "filetypes.h"
55 #include "msgwindow.h"
57 #include "templates.h"
58 #include "encodings.h"
61 #include "keybindings.h"
65 #include "highlighting.h"
73 #include "geanyobject.h"
84 # define N_(String) (String)
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"
96 #ifdef USE_INCLUDED_REGEX
101 static gboolean want_plugins
;
103 /* command-line options */
104 static gboolean verbose_mode
= FALSE
;
105 static gboolean ignore_global_tags
= FALSE
;
106 static gboolean no_msgwin
= FALSE
;
107 static gboolean show_version
= FALSE
;
108 static gchar
*alternate_config
= NULL
;
110 static gboolean no_vte
= FALSE
;
111 static gchar
*lib_vte
= NULL
;
113 static gboolean generate_tags
= FALSE
;
114 static gboolean no_preprocessing
= FALSE
;
115 static gboolean ft_names
= FALSE
;
116 static gboolean print_prefix
= FALSE
;
118 static gboolean no_plugins
= FALSE
;
120 static gboolean dummy
= FALSE
;
122 /* in alphabetical order of short options */
123 static GOptionEntry entries
[] =
125 { "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
},
126 { "config", 'c', 0, G_OPTION_ARG_FILENAME
, &alternate_config
, N_("Use an alternate configuration directory"), NULL
},
127 { "ft-names", 0, 0, G_OPTION_ARG_NONE
, &ft_names
, N_("Print internal filetype names"), NULL
},
128 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE
, &generate_tags
, N_("Generate global tags file (see documentation)"), NULL
},
129 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE
, &no_preprocessing
, N_("Don't preprocess C/C++ files when generating tags"), NULL
},
131 { "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
},
132 { "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
},
133 { "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 { "line", 'l', 0, G_OPTION_ARG_INT
, &cl_options
.goto_line
, N_("Set initial line number for the first opened file"), NULL
},
136 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE
, &no_msgwin
, N_("Don't show message window at startup"), NULL
},
137 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE
, &ignore_global_tags
, N_("Don't load auto completion data (see documentation)"), NULL
},
139 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE
, &no_plugins
, N_("Don't load plugins"), NULL
},
141 { "print-prefix", 0, 0, G_OPTION_ARG_NONE
, &print_prefix
, N_("Print Geany's installation prefix"), NULL
},
142 { "read-only", 'r', 0, G_OPTION_ARG_NONE
, &cl_options
.readonly
, N_("Open all FILES in read-only mode (see documention)"), 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
},
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
},
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
)
161 if (ui_prefs
.geometry
[0] != -1 && ui_prefs
.geometry
[1] != -1)
162 gtk_window_move(GTK_WINDOW(main_widgets
.window
),
163 ui_prefs
.geometry
[0], ui_prefs
.geometry
[1]);
165 if (ui_prefs
.geometry
[2] != -1 && ui_prefs
.geometry
[3] != -1)
166 gtk_window_set_default_size(GTK_WINDOW(main_widgets
.window
),
167 ui_prefs
.geometry
[2], ui_prefs
.geometry
[3]);
169 if (ui_prefs
.geometry
[4] == 1)
170 gtk_window_maximize(GTK_WINDOW(main_widgets
.window
));
174 /* special things for the initial setup of the checkboxes and related stuff
175 * an action on a setting is only performed if the setting is not equal to the program default
176 * (all the following code is not perfect but it works for the moment) */
177 static void apply_settings(void)
179 ui_update_fold_items();
181 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
183 if (! ui_prefs
.msgwindow_visible
)
185 ignore_callback
= TRUE
;
186 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_messages_window1")), FALSE
);
187 gtk_widget_hide(ui_lookup_widget(main_widgets
.window
, "scrolledwindow1"));
188 ignore_callback
= FALSE
;
190 if (! ui_prefs
.sidebar_visible
)
192 ignore_callback
= TRUE
;
193 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_sidebar1")), FALSE
);
194 ignore_callback
= FALSE
;
197 toolbar_apply_settings();
200 ui_update_view_editor_menu_items();
202 /* hide statusbar if desired */
203 if (! interface_prefs
.statusbar_visible
)
205 gtk_widget_hide(ui_widgets
.statusbar
);
208 /* set the tab placements of the notebooks */
209 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets
.notebook
), interface_prefs
.tab_pos_editor
);
210 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow
.notebook
), interface_prefs
.tab_pos_msgwin
);
211 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), interface_prefs
.tab_pos_sidebar
);
213 /* whether to show notebook tabs or not */
214 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets
.notebook
), interface_prefs
.show_notebook_tabs
);
217 if (! vte_info
.have_vte
)
220 gtk_widget_set_sensitive(
221 ui_lookup_widget(main_widgets
.window
, "send_selection_to_vte1"), FALSE
);
224 if (interface_prefs
.sidebar_pos
!= GTK_POS_LEFT
)
225 ui_swap_sidebar_pos();
229 static void main_init(void)
234 main_widgets
.window
= NULL
;
236 ui_widgets
.open_fontsel
= NULL
;
237 ui_widgets
.open_colorsel
= NULL
;
238 ui_widgets
.prefs_dialog
= NULL
;
239 main_status
.main_window_realized
= FALSE
;
240 file_prefs
.tab_order_ltr
= FALSE
;
241 file_prefs
.tab_order_beside
= FALSE
;
242 main_status
.quitting
= FALSE
;
243 ignore_callback
= FALSE
;
244 app
->tm_workspace
= tm_get_workspace();
245 ui_prefs
.recent_queue
= g_queue_new();
246 ui_prefs
.recent_projects_queue
= g_queue_new();
247 main_status
.opening_session_files
= FALSE
;
249 ui_init_stock_items();
251 main_widgets
.window
= create_window1();
253 /* add recent projects to the Project menu */
254 ui_widgets
.recent_projects_menuitem
= ui_lookup_widget(main_widgets
.window
, "recent_projects1");
255 ui_widgets
.recent_projects_menu_menubar
= gtk_menu_new();
256 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets
.recent_projects_menuitem
),
257 ui_widgets
.recent_projects_menu_menubar
);
259 /* store important pointers for later reference */
260 main_widgets
.toolbar
= toolbar_init();
261 main_widgets
.sidebar_notebook
= ui_lookup_widget(main_widgets
.window
, "notebook3");
262 main_widgets
.notebook
= ui_lookup_widget(main_widgets
.window
, "notebook1");
263 main_widgets
.editor_menu
= create_edit_menu1();
264 main_widgets
.tools_menu
= ui_lookup_widget(main_widgets
.window
, "tools1_menu");
265 main_widgets
.message_window_notebook
= ui_lookup_widget(main_widgets
.window
, "notebook_info");
266 main_widgets
.project_menu
= ui_lookup_widget(main_widgets
.window
, "menu_project1_menu");
268 ui_widgets
.toolbar_menu
= create_toolbar_popup_menu1();
271 /* set widget names for matching with .gtkrc-2.0 */
272 gtk_widget_set_name(main_widgets
.window
, "GeanyMainWindow");
273 gtk_widget_set_name(ui_widgets
.toolbar_menu
, "GeanyToolbarMenu");
274 gtk_widget_set_name(main_widgets
.editor_menu
, "GeanyEditMenu");
275 gtk_widget_set_name(ui_lookup_widget(main_widgets
.window
, "menubar1"), "GeanyMenubar");
276 gtk_widget_set_name(main_widgets
.toolbar
, "GeanyToolbar");
278 /* for some reason we need to set the initial size request,
279 * otherwise the main window gets crazy dimensions */
280 gtk_widget_set_size_request(main_widgets
.window
,
281 GEANY_WINDOW_DEFAULT_WIDTH
, GEANY_WINDOW_DEFAULT_HEIGHT
);
282 gtk_window_set_default_size(GTK_WINDOW(main_widgets
.window
),
283 GEANY_WINDOW_DEFAULT_WIDTH
, GEANY_WINDOW_DEFAULT_HEIGHT
);
287 const gchar
*main_get_version_string(void)
289 static gchar full
[] = VERSION
" (git >= " REVISION
")";
291 if (utils_str_equal(REVISION
, "-1"))
298 /* get the full file path of a command-line argument
299 * N.B. the result should be freed and may contain '/../' or '/./ ' */
300 gchar
*main_get_argv_filename(const gchar
*filename
)
304 if (g_path_is_absolute(filename
) || utils_is_uri(filename
))
305 result
= g_strdup(filename
);
308 /* use current dir */
309 gchar
*cur_dir
= g_get_current_dir();
312 G_DIR_SEPARATOR_S
, cur_dir
, filename
, NULL
);
319 /* get a :line:column specifier from the end of a filename (if present),
320 * return the line/column values, and remove the specifier from the string
321 * (Note that *line and *column must both be set to -1 initially) */
322 static void get_line_and_column_from_filename(gchar
*filename
, gint
*line
, gint
*column
)
325 gint colon_count
= 0;
326 gboolean have_number
= FALSE
;
329 g_assert(*line
== -1 && *column
== -1);
331 if (G_UNLIKELY(! NZV(filename
)))
334 /* allow to open files like "test:0" */
335 if (g_file_test(filename
, G_FILE_TEST_EXISTS
))
338 len
= strlen(filename
);
339 for (i
= len
- 1; i
>= 1; i
--)
341 gboolean is_colon
= filename
[i
] == ':';
342 gboolean is_digit
= g_ascii_isdigit(filename
[i
]);
344 if (! is_colon
&& ! is_digit
)
349 if (++colon_count
> 1)
350 break; /* bail on 2+ colons in a row */
358 if (is_colon
&& have_number
)
360 gint number
= atoi(&filename
[i
+ 1]);
370 break; /* line and column are set, so we're done */
375 static void setup_paths(void)
382 /* use the installation directory(the one where geany.exe is located) as the base for the
383 * documentation and data files */
384 gchar
*install_dir
= win32_get_installation_dir();
386 data_dir
= g_strconcat(install_dir
, "\\data", NULL
); /* e.g. C:\Program Files\geany\data */
387 doc_dir
= g_strconcat(install_dir
, "\\doc", NULL
);
391 data_dir
= g_strconcat(GEANY_DATADIR
, "/geany", NULL
); /* e.g. /usr/share/geany */
392 doc_dir
= g_strconcat(GEANY_DOCDIR
, "/html", NULL
);
395 /* convert path names to locale encoding */
396 app
->datadir
= utils_get_locale_from_utf8(data_dir
);
397 app
->docdir
= utils_get_locale_from_utf8(doc_dir
);
405 * Checks whether the main window has been realized.
406 * This is an easy indicator whether Geany is right now starting up (main window is not
407 * yet realized) or whether it has finished the startup process (main window is realized).
408 * This is because the main window is realized (i.e. actually drawn on the screen) at the
409 * end of the startup process.
411 * @note Maybe you want to use the @link pluginsignals.c @c "geany-startup-complete" signal @endlink
412 * to get notified about the completed startup process.
414 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
418 gboolean
main_is_realized(void)
420 return main_status
.main_window_realized
;
425 * Initialises the gettext translation system.
426 * This is a convenience function to set up gettext for internationalisation support
427 * in external plugins. You should call this function early in @ref plugin_init().
428 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
429 * The codeset for the message translations is set to UTF-8.
431 * Note that this function only setups the gettext textdomain for you. You still have
432 * to adjust the build system of your plugin to get internationalisation support
435 * If you have already used @ref PLUGIN_SET_TRANSLATABLE_INFO() you
436 * don't need to call main_locale_init() again as it has already been done.
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.
448 void main_locale_init(const gchar
*locale_dir
, const gchar
*package
)
450 gchar
*l_locale_dir
= NULL
;
453 setlocale(LC_ALL
, "");
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
);
464 l_locale_dir
= g_strdup(locale_dir
);
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 const GSList
*list
, *node
;
477 filetypes_init_types();
478 printf("Geany's filetype names:\n");
480 list
= filetypes_get_sorted_by_name();
481 foreach_slist(node
, list
)
483 GeanyFiletype
*ft
= node
->data
;
485 printf("%s\n", ft
->name
);
487 filetypes_free_types();
491 static void wait_for_input_on_windows(void)
496 geany_debug("Press any key to continue");
503 static void parse_command_line_options(gint
*argc
, gchar
***argv
)
505 GError
*error
= NULL
;
506 GOptionContext
*context
;
508 CommandLineOptions def_clo
= {FALSE
, NULL
, TRUE
, -1, -1, FALSE
, FALSE
, FALSE
};
510 /* first initialise cl_options fields with default values */
511 cl_options
= def_clo
;
513 /* the GLib option parser can't handle the +NNN (line number) option,
514 * so we grab that here and replace it with a no-op */
515 for (i
= 1; i
< (*argc
); i
++)
517 if ((*argv
)[i
][0] != '+')
520 cl_options
.goto_line
= atoi((*argv
)[i
] + 1);
521 (*argv
)[i
] = "--dummy";
524 context
= g_option_context_new(_("[FILES...]"));
525 g_option_context_add_main_entries(context
, entries
, GETTEXT_PACKAGE
);
526 g_option_group_set_translation_domain(g_option_context_get_main_group(context
), GETTEXT_PACKAGE
);
527 g_option_context_add_group(context
, gtk_get_option_group(FALSE
));
528 g_option_context_parse(context
, argc
, argv
, &error
);
529 g_option_context_free(context
);
533 g_printerr("Geany: %s\n", error
->message
);
538 app
->debug_mode
= verbose_mode
;
541 win32_init_debug_code();
546 printf(PACKAGE
" %s (", main_get_version_string());
547 /* note for translators: library versions are printed after this */
548 printf(_("built on %s with "), __DATE__
);
549 printf(geany_lib_versions
,
550 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
,
551 GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
553 wait_for_input_on_windows();
559 printf("%s\n", GEANY_PREFIX
);
560 printf("%s\n", GEANY_DATADIR
);
561 printf("%s\n", GEANY_LIBDIR
);
562 printf("%s\n", GEANY_LOCALEDIR
);
563 wait_for_input_on_windows();
567 if (alternate_config
)
569 geany_debug("alternate config: %s", alternate_config
);
570 app
->configdir
= alternate_config
;
574 app
->configdir
= g_build_filename(g_get_user_config_dir(), "geany", NULL
);
581 filetypes_init_types();
582 ret
= symbols_generate_global_tags(*argc
, *argv
, ! no_preprocessing
);
583 filetypes_free_types();
584 wait_for_input_on_windows();
591 wait_for_input_on_windows();
596 socket_info
.ignore_socket
= cl_options
.new_instance
;
597 if (cl_options
.socket_filename
)
599 socket_info
.file_name
= cl_options
.socket_filename
;
604 vte_info
.lib_vte
= lib_vte
;
606 cl_options
.ignore_global_tags
= ignore_global_tags
;
608 if (! gtk_init_check(NULL
, NULL
))
609 { /* check whether we have a valid X display and exit if not */
610 g_printerr("Geany: cannot open display\n");
616 static gint
create_config_dir(void)
618 gint saved_errno
= 0;
619 gchar
*conf_file
= g_build_filename(app
->configdir
, "geany.conf", NULL
);
620 gchar
*filedefs_dir
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, NULL
);
622 gchar
*templates_dir
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
, NULL
);
624 if (! g_file_test(app
->configdir
, G_FILE_TEST_EXISTS
))
627 /* if we are *not* using an alternate config directory, we check whether the old one
628 * in ~/.geany still exists and try to move it */
629 if (alternate_config
== NULL
)
631 gchar
*old_dir
= g_build_filename(g_get_home_dir(), ".geany", NULL
);
632 /* move the old config dir if it exists */
633 if (g_file_test(old_dir
, G_FILE_TEST_EXISTS
))
635 if (! dialogs_show_question_full(main_widgets
.window
,
636 GTK_STOCK_YES
, GTK_STOCK_QUIT
, _("Move it now?"),
638 _("Geany needs to move your old configuration directory before starting.")))
641 if (! g_file_test(app
->configdir
, G_FILE_TEST_IS_DIR
))
642 utils_mkdir(app
->configdir
, TRUE
);
644 if (g_rename(old_dir
, app
->configdir
) == 0)
646 dialogs_show_msgbox(GTK_MESSAGE_INFO
,
647 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
648 old_dir
, app
->configdir
);
654 dialogs_show_msgbox(GTK_MESSAGE_WARNING
,
655 /* for translators: the third %s in brackets is the error message which
656 * describes why moving the dir didn't work */
657 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
658 "Please move manually the directory to the new location."),
659 old_dir
, app
->configdir
, g_strerror(errno
));
665 geany_debug("creating config directory %s", app
->configdir
);
666 saved_errno
= utils_mkdir(app
->configdir
, TRUE
);
669 if (saved_errno
== 0 && ! g_file_test(conf_file
, G_FILE_TEST_EXISTS
))
670 { /* check whether geany.conf can be written */
671 saved_errno
= utils_is_file_writable(app
->configdir
);
674 /* make subdir for filetype definitions */
675 if (saved_errno
== 0)
677 gchar
*filedefs_readme
= g_build_filename(app
->configdir
,
678 GEANY_FILEDEFS_SUBDIR
, "filetypes.README", NULL
);
680 if (! g_file_test(filedefs_dir
, G_FILE_TEST_EXISTS
))
682 saved_errno
= utils_mkdir(filedefs_dir
, FALSE
);
684 if (saved_errno
== 0 && ! g_file_test(filedefs_readme
, G_FILE_TEST_EXISTS
))
686 gchar
*text
= g_strconcat(
687 "Copy files from ", app
->datadir
, " to this directory to overwrite "
688 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
689 "the documentation (in ", app
->docdir
, G_DIR_SEPARATOR_S
"index.html or visit " GEANY_HOMEPAGE
").", NULL
);
690 utils_write_file(filedefs_readme
, text
);
693 g_free(filedefs_readme
);
696 /* make subdir for template files */
697 if (saved_errno
== 0)
699 gchar
*templates_readme
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
,
700 "templates.README", NULL
);
702 if (! g_file_test(templates_dir
, G_FILE_TEST_EXISTS
))
704 saved_errno
= utils_mkdir(templates_dir
, FALSE
);
706 if (saved_errno
== 0 && ! g_file_test(templates_readme
, G_FILE_TEST_EXISTS
))
708 gchar
*text
= g_strconcat(
709 "There are several template files in this directory. For these templates you can use wildcards.\n\
710 For more information read the documentation (in ", app
->docdir
, G_DIR_SEPARATOR_S
"index.html or visit " GEANY_HOMEPAGE
").",
712 utils_write_file(templates_readme
, text
);
715 g_free(templates_readme
);
718 g_free(filedefs_dir
);
719 g_free(templates_dir
);
726 /* Returns 0 if config dir is OK. */
727 static gint
setup_config_dir(void)
729 gint mkdir_result
= 0;
731 /* convert configdir to locale encoding to avoid troubles */
732 setptr(app
->configdir
, utils_get_locale_from_utf8(app
->configdir
));
734 mkdir_result
= create_config_dir();
735 if (mkdir_result
!= 0)
737 if (! dialogs_show_question(
738 _("Configuration directory could not be created (%s).\nThere could be some problems "
739 "using Geany without a configuration directory.\nStart Geany anyway?"),
740 g_strerror(mkdir_result
)))
745 /* make configdir a real path */
746 if (g_file_test(app
->configdir
, G_FILE_TEST_EXISTS
))
747 setptr(app
->configdir
, tm_get_real_path(app
->configdir
));
753 static void signal_cb(gint sig
)
757 on_exit_clicked(NULL
, NULL
);
762 /* Used for command-line arguments at startup or from socket.
763 * this will strip any :line:col filename suffix from locale_filename */
764 gboolean
main_handle_filename(const gchar
*locale_filename
)
767 gint line
= -1, column
= -1;
770 g_return_val_if_fail(locale_filename
, FALSE
);
772 /* check whether the passed filename is an URI */
773 filename
= utils_get_path_from_uri(locale_filename
);
774 if (filename
== NULL
)
777 get_line_and_column_from_filename(filename
, &line
, &column
);
779 cl_options
.goto_line
= line
;
781 cl_options
.goto_column
= column
;
783 if (g_file_test(filename
, G_FILE_TEST_IS_REGULAR
))
785 doc
= document_open_file(filename
, cl_options
.readonly
, NULL
, NULL
);
786 /* add recent file manually if opening_session_files is set */
787 if (doc
!= NULL
&& main_status
.opening_session_files
)
788 ui_add_recent_document(doc
);
792 else if (file_prefs
.cmdline_new_files
)
793 { /* create new file with the given filename */
794 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
796 doc
= document_new_file(utf8_filename
, NULL
, NULL
);
798 ui_add_recent_document(doc
);
799 g_free(utf8_filename
);
808 /* open files from command line */
809 static gboolean
open_cl_files(gint argc
, gchar
**argv
)
813 if (argc
<= 1) return FALSE
;
815 for (i
= 1; i
< argc
; i
++)
817 gchar
*filename
= main_get_argv_filename(argv
[i
]);
819 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
826 /* It seems argv elements are encoded in CP1252 on a German Windows */
827 setptr(filename
, g_locale_to_utf8(filename
, -1, NULL
, NULL
, NULL
));
829 if (filename
&& ! main_handle_filename(filename
))
831 const gchar
*msg
= _("Could not find file '%s'.");
833 g_printerr(msg
, filename
); /* also print to the terminal */
835 ui_set_statusbar(TRUE
, msg
, filename
);
843 static void load_session_project_file(void)
845 gchar
*locale_filename
;
847 g_return_if_fail(project_prefs
.session_file
!= NULL
);
849 locale_filename
= utils_get_locale_from_utf8(project_prefs
.session_file
);
851 if (G_LIKELY(NZV(locale_filename
)))
852 project_load_file(locale_filename
);
854 g_free(locale_filename
);
855 g_free(project_prefs
.session_file
); /* no longer needed */
859 static void load_settings(void)
861 configuration_load();
862 /* let cmdline options overwrite configuration settings */
864 vte_info
.have_vte
= (no_vte
) ? FALSE
: vte_info
.load_vte
;
867 ui_prefs
.msgwindow_visible
= FALSE
;
870 want_plugins
= prefs
.load_plugins
&& !no_plugins
;
875 void main_load_project_from_command_line(const gchar
*locale_filename
, gboolean use_session
)
879 if (utils_is_uri(locale_filename
))
880 pfile
= utils_get_path_from_uri(locale_filename
);
882 pfile
= g_strdup(locale_filename
);
887 project_load_file_with_session(pfile
);
889 project_load_file(pfile
);
895 static void load_startup_files(gint argc
, gchar
**argv
)
897 gboolean load_project_from_cl
= FALSE
;
899 /* ATM when opening a project file any other filenames are ignored */
900 load_project_from_cl
= (argc
> 1) && g_str_has_suffix(argv
[1], ".geany");
901 if (load_project_from_cl
&& argc
> 2)
902 g_print("Ignoring extra filenames after %s", argv
[1]);
904 if (load_project_from_cl
|| ! open_cl_files(argc
, argv
))
906 if (prefs
.load_session
)
908 if (load_project_from_cl
)
910 main_load_project_from_command_line(argv
[1], FALSE
);
912 else if (cl_options
.load_session
&& !cl_options
.new_instance
)
913 load_session_project_file();
915 /* when we want a new instance, we still load project session files unless -s
917 if (!cl_options
.load_session
|| (!load_project_from_cl
&& cl_options
.new_instance
))
920 /* load session files into tabs, as they are found in the session_files variable */
921 configuration_open_files();
923 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) == 0)
925 ui_update_popup_copy_items(NULL
);
926 ui_update_popup_reundo_items(NULL
);
933 static gboolean
send_startup_complete(gpointer data
)
935 g_signal_emit_by_name(geany_object
, "geany-startup-complete");
940 static const gchar
*get_locale(void)
942 const gchar
*locale
= "unknown";
944 locale
= setlocale(LC_CTYPE
, NULL
);
950 gint
main(gint argc
, gchar
**argv
)
953 gint config_dir_result
;
958 app
= g_new0(GeanyApp
, 1);
959 memset(&main_status
, 0, sizeof(GeanyStatus
));
960 memset(&prefs
, 0, sizeof(GeanyPrefs
));
961 memset(&interface_prefs
, 0, sizeof(GeanyInterfacePrefs
));
962 memset(&toolbar_prefs
, 0, sizeof(GeanyToolbarPrefs
));
963 memset(&file_prefs
, 0, sizeof(GeanyFilePrefs
));
964 memset(&search_prefs
, 0, sizeof(GeanySearchPrefs
));
965 memset(&tool_prefs
, 0, sizeof(GeanyToolPrefs
));
966 memset(&template_prefs
, 0, sizeof(GeanyTemplatePrefs
));
967 memset(&ui_prefs
, 0, sizeof(UIPrefs
));
968 memset(&ui_widgets
, 0, sizeof(UIWidgets
));
972 main_locale_init(GEANY_LOCALEDIR
, GETTEXT_PACKAGE
);
974 parse_command_line_options(&argc
, &argv
);
976 /* Initialize GLib's thread system in case any plugins want to use it or their
977 * dependencies (e.g. WebKit, Soup, ...) */
978 if (!g_thread_supported())
981 signal(SIGTERM
, signal_cb
);
983 /* SIGQUIT is used to kill spawned children and we get also this signal, so ignore */
984 signal(SIGQUIT
, SIG_IGN
);
985 /* ignore SIGPIPE signal for preventing sudden death of program */
986 signal(SIGPIPE
, SIG_IGN
);
989 config_dir_result
= setup_config_dir();
991 /* check and create (unix domain) socket for remote operation */
992 if (! socket_info
.ignore_socket
)
994 socket_info
.lock_socket
= -1;
995 socket_info
.lock_socket_tag
= 0;
996 socket_info
.lock_socket
= socket_init(argc
, argv
);
998 if (socket_info
.lock_socket
== -2)
1000 /* Quit if filenames were sent to first instance or the list of open
1001 * documents has been sent */
1002 if (argc
> 1 || cl_options
.list_documents
)
1004 gdk_notify_startup_complete();
1005 g_free(app
->configdir
);
1006 g_free(app
->datadir
);
1007 g_free(app
->docdir
);
1011 /* Start a new instance if no command line strings were passed */
1012 socket_info
.ignore_socket
= TRUE
;
1013 cl_options
.new_instance
= TRUE
;
1018 locale
= get_locale();
1019 geany_debug("Geany %s, %s",
1020 main_get_version_string(),
1022 geany_debug(geany_lib_versions
,
1023 gtk_major_version
, gtk_minor_version
, gtk_micro_version
,
1024 glib_major_version
, glib_minor_version
, glib_micro_version
);
1025 geany_debug("System data dir: %s", app
->datadir
);
1026 geany_debug("User config dir: %s", app
->configdir
);
1028 /* create the object so Geany signals can be connected in init() functions */
1029 geany_object
= geany_object_new();
1037 /* init stash groups before loading keyfile */
1038 configuration_init();
1046 load_settings(); /* load keyfile */
1048 highlighting_init();
1051 ui_create_insert_menu_items();
1052 ui_create_insert_date_menu_items();
1058 document_init_doclist();
1060 editor_snippets_init();
1062 /* set window icon */
1064 GdkPixbuf
*pb
= ui_new_pixbuf_from_inline(GEANY_IMAGE_LOGO
);
1065 gtk_window_set_icon(GTK_WINDOW(main_widgets
.window
), pb
);
1066 g_object_unref(pb
); /* free our reference */
1069 /* registering some basic events */
1070 g_signal_connect(main_widgets
.window
, "delete-event", G_CALLBACK(on_exit_clicked
), NULL
);
1071 g_signal_connect(main_widgets
.window
, "window-state-event", G_CALLBACK(on_window_state_event
), NULL
);
1073 g_signal_connect(msgwindow
.scribble
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
1078 ui_create_recent_menus();
1080 ui_set_statusbar(TRUE
, _("This is Geany %s."), main_get_version_string());
1081 if (config_dir_result
!= 0)
1082 ui_set_statusbar(TRUE
, _("Configuration directory could not be created (%s)."),
1083 g_strerror(config_dir_result
));
1085 /* apply all configuration options */
1089 /* load any enabled plugins before we open any documents */
1091 plugins_load_active();
1094 ui_sidebar_show_hide();
1096 /* set the active sidebar page after plugins have been loaded */
1097 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), ui_prefs
.sidebar_page
);
1099 /* load keybinding settings after plugins have added their groups */
1100 keybindings_load_keyfile();
1102 /* create the custom command menu after the keybindings have been loaded to have the proper
1103 * accelerator shown for the menu items */
1104 tools_create_insert_custom_command_menu_items();
1106 /* load any command line files or session files */
1107 main_status
.opening_session_files
= TRUE
;
1108 load_startup_files(argc
, argv
);
1109 main_status
.opening_session_files
= FALSE
;
1111 /* open a new file if no other file was opened */
1112 document_new_file_if_non_open();
1114 ui_document_buttons_update();
1115 ui_save_buttons_toggle(FALSE
);
1117 doc
= document_get_current();
1118 sidebar_select_openfiles_item(doc
);
1119 build_menu_update(doc
);
1120 sidebar_update_tag_list(doc
, FALSE
);
1123 /* Manually realise the main window to be able to set the position but don't show it.
1124 * We don't set the position after showing the window to avoid flickering. */
1125 gtk_widget_realize(main_widgets
.window
);
1127 setup_window_position();
1129 /* finally show the window */
1130 document_grab_focus(doc
);
1131 gtk_widget_show(main_widgets
.window
);
1132 main_status
.main_window_realized
= TRUE
;
1134 configuration_apply_settings();
1137 /* register the callback of socket input */
1138 if (! socket_info
.ignore_socket
&& socket_info
.lock_socket
> 0)
1140 socket_info
.read_ioc
= g_io_channel_unix_new(socket_info
.lock_socket
);
1141 socket_info
.lock_socket_tag
= g_io_add_watch(socket_info
.read_ioc
,
1142 G_IO_IN
| G_IO_PRI
| G_IO_ERR
, socket_lock_input_cb
, main_widgets
.window
);
1149 /* On Windows, change the working directory to the Geany installation path to not lock
1150 * the directory of a file passed as command line argument (see bug #2626124). */
1151 dir
= win32_get_installation_dir();
1152 win32_set_working_directory(dir
);
1157 /* when we are really done with setting everything up and the main event loop is running,
1158 * tell other components, mainly plugins, that startup is complete */
1159 g_idle_add_full(G_PRIORITY_LOW
, send_startup_complete
, NULL
, NULL
);
1166 static void queue_free(GQueue
*queue
)
1168 while (! g_queue_is_empty(queue
))
1170 g_free(g_queue_pop_tail(queue
));
1172 g_queue_free(queue
);
1178 geany_debug("Quitting...");
1190 highlighting_free_styles();
1191 templates_free_templates();
1195 document_finalize();
1199 editor_snippets_free();
1200 encodings_finalize();
1203 configuration_finalize();
1204 filetypes_free_types();
1208 tm_workspace_free(TM_WORK_OBJECT(app
->tm_workspace
));
1209 g_free(app
->configdir
);
1210 g_free(app
->datadir
);
1211 g_free(app
->docdir
);
1212 g_free(prefs
.default_open_path
);
1213 g_free(prefs
.custom_plugin_path
);
1214 g_free(ui_prefs
.custom_date_format
);
1215 g_free(interface_prefs
.editor_font
);
1216 g_free(interface_prefs
.tagbar_font
);
1217 g_free(interface_prefs
.msgwin_font
);
1218 g_free(editor_prefs
.long_line_color
);
1219 g_free(editor_prefs
.comment_toggle_mark
);
1220 g_free(editor_prefs
.color_scheme
);
1221 g_free(tool_prefs
.context_action_cmd
);
1222 g_free(template_prefs
.developer
);
1223 g_free(template_prefs
.company
);
1224 g_free(template_prefs
.mail
);
1225 g_free(template_prefs
.initials
);
1226 g_free(template_prefs
.version
);
1227 g_free(tool_prefs
.term_cmd
);
1228 g_free(tool_prefs
.browser_cmd
);
1229 g_free(tool_prefs
.grep_cmd
);
1230 g_free(printing_prefs
.external_print_cmd
);
1231 g_free(printing_prefs
.page_header_datefmt
);
1232 g_strfreev(ui_prefs
.custom_commands
);
1234 queue_free(ui_prefs
.recent_queue
);
1235 queue_free(ui_prefs
.recent_projects_queue
);
1237 if (ui_widgets
.prefs_dialog
&& GTK_IS_WIDGET(ui_widgets
.prefs_dialog
)) gtk_widget_destroy(ui_widgets
.prefs_dialog
);
1238 if (ui_widgets
.open_fontsel
&& GTK_IS_WIDGET(ui_widgets
.open_fontsel
)) gtk_widget_destroy(ui_widgets
.open_fontsel
);
1239 if (ui_widgets
.open_colorsel
&& GTK_IS_WIDGET(ui_widgets
.open_colorsel
)) gtk_widget_destroy(ui_widgets
.open_colorsel
);
1241 if (vte_info
.have_vte
) vte_close();
1242 g_free(vte_info
.lib_vte
);
1243 g_free(vte_info
.dir
);
1245 gtk_widget_destroy(main_widgets
.window
);
1247 /* destroy popup menus */
1248 if (main_widgets
.editor_menu
&& GTK_IS_WIDGET(main_widgets
.editor_menu
))
1249 gtk_widget_destroy(main_widgets
.editor_menu
);
1250 if (ui_widgets
.toolbar_menu
&& GTK_IS_WIDGET(ui_widgets
.toolbar_menu
))
1251 gtk_widget_destroy(ui_widgets
.toolbar_menu
);
1252 if (msgwindow
.popup_status_menu
&& GTK_IS_WIDGET(msgwindow
.popup_status_menu
))
1253 gtk_widget_destroy(msgwindow
.popup_status_menu
);
1254 if (msgwindow
.popup_msg_menu
&& GTK_IS_WIDGET(msgwindow
.popup_msg_menu
))
1255 gtk_widget_destroy(msgwindow
.popup_msg_menu
);
1256 if (msgwindow
.popup_compiler_menu
&& GTK_IS_WIDGET(msgwindow
.popup_compiler_menu
))
1257 gtk_widget_destroy(msgwindow
.popup_compiler_menu
);
1259 g_object_unref(geany_object
);
1260 geany_object
= NULL
;
1264 ui_finalize_builder();
1271 * Reloads most of Geany's configuration files without restarting. Currently the following
1272 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1273 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1274 * and 'settings' and 'build_settings' sections of the filetype definition files.
1276 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1281 void main_reload_configuration(void)
1283 /* reload templates */
1284 templates_free_templates();
1287 /* reload snippets */
1288 editor_snippets_free();
1289 editor_snippets_init();
1291 filetypes_reload_extensions();
1294 /* C tag names to ignore */
1295 symbols_reload_config_files();
1297 ui_set_statusbar(TRUE
, _("Configuration files reloaded."));