2 * libmain.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Main program-related commands.
24 * Handles program initialization and cleanup.
35 #include "callbacks.h"
38 #include "encodingsprivate.h"
39 #include "filetypes.h"
40 #include "geanyobject.h"
41 #include "highlighting.h"
42 #include "keybindings.h"
45 #include "msgwindow.h"
57 #include "templates.h"
68 #include <sys/types.h>
75 #include <glib/gstdio.h>
78 # include <glib-unix.h>
87 gboolean ignore_callback
; /* hack workaround for GTK+ toggle button callback problem */
89 GeanyStatus main_status
;
90 CommandLineOptions cl_options
; /* fields initialised in parse_command_line_options */
92 static gchar
*original_cwd
= NULL
;
94 static const gchar geany_lib_versions
[] = "GTK %u.%u.%u, GLib %u.%u.%u";
96 static gboolean want_plugins
;
98 /* command-line options */
99 static gboolean verbose_mode
= FALSE
;
100 static gboolean ignore_global_tags
= FALSE
;
101 static gboolean no_msgwin
= FALSE
;
102 static gboolean show_version
= FALSE
;
103 static gchar
*alternate_config
= NULL
;
105 static gboolean no_vte
= FALSE
;
106 static gchar
*lib_vte
= NULL
;
108 static gboolean generate_tags
= FALSE
;
109 static gboolean no_preprocessing
= FALSE
;
110 static gboolean ft_names
= FALSE
;
111 static gboolean print_prefix
= FALSE
;
113 static gboolean no_plugins
= FALSE
;
115 static gboolean dummy
= FALSE
;
117 /* in alphabetical order of short options */
118 static GOptionEntry entries
[] =
120 { "column", 0, 0, G_OPTION_ARG_INT
, &cl_options
.goto_column
, N_("Set initial column number to COLUMN for the first opened file (useful in conjunction with --line)"), N_("COLUMN") },
121 { "config", 'c', 0, G_OPTION_ARG_FILENAME
, &alternate_config
, N_("Use alternate configuration directory DIR"), N_("DIR") },
122 { "ft-names", 0, 0, G_OPTION_ARG_NONE
, &ft_names
, N_("Print internal filetype names"), NULL
},
123 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE
, &generate_tags
, N_("Generate global tags file (see documentation)"), NULL
},
124 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE
, &no_preprocessing
, N_("Don't preprocess C/C++ files when generating tags file"), NULL
},
126 { "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
},
127 { "socket-file", 0, 0, G_OPTION_ARG_FILENAME
, &cl_options
.socket_filename
, N_("Use socket filename FILE for communication with a running Geany instance"), N_("FILE") },
128 { "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 { "line", 'l', 0, G_OPTION_ARG_INT
, &cl_options
.goto_line
, N_("Set initial line number to LINE for the first opened file"), N_("LINE") },
131 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE
, &no_msgwin
, N_("Don't show message window at startup"), NULL
},
132 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE
, &ignore_global_tags
, N_("Don't load auto completion data (see documentation)"), NULL
},
134 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE
, &no_plugins
, N_("Don't load plugins"), NULL
},
136 { "print-prefix", 0, 0, G_OPTION_ARG_NONE
, &print_prefix
, N_("Print Geany's installation prefix"), NULL
},
137 { "read-only", 'r', 0, G_OPTION_ARG_NONE
, &cl_options
.readonly
, N_("Open all FILES in read-only mode (see documentation)"), NULL
},
138 { "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 { "no-terminal", 't', 0, G_OPTION_ARG_NONE
, &no_vte
, N_("Don't load terminal support"), NULL
},
141 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME
, &lib_vte
, N_("Use FILE as the dynamically-linked VTE library"), N_("FILE") },
143 { "verbose", 'v', 0, G_OPTION_ARG_NONE
, &verbose_mode
, N_("Be verbose"), NULL
},
144 { "version", 'V', 0, G_OPTION_ARG_NONE
, &show_version
, N_("Show version and exit"), NULL
},
145 { "dummy", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, &dummy
, NULL
, NULL
}, /* for +NNN line number arguments */
146 { NULL
, 0, 0, 0, NULL
, NULL
, NULL
}
150 static void setup_window_position(void)
152 /* interprets the saved window geometry */
153 if (prefs
.save_wingeom
)
155 if (ui_prefs
.geometry
[2] != -1 && ui_prefs
.geometry
[3] != -1)
157 gtk_window_set_default_size(GTK_WINDOW(main_widgets
.window
),
158 ui_prefs
.geometry
[2], ui_prefs
.geometry
[3]);
162 if (prefs
.save_winpos
)
164 if (ui_prefs
.geometry
[0] != -1 && ui_prefs
.geometry
[1] != -1)
166 gtk_window_move(GTK_WINDOW(main_widgets
.window
),
167 ui_prefs
.geometry
[0], ui_prefs
.geometry
[1]);
169 if (ui_prefs
.geometry
[4] == 1)
171 gtk_window_maximize(GTK_WINDOW(main_widgets
.window
));
177 /* special things for the initial setup of the checkboxes and related stuff
178 * an action on a setting is only performed if the setting is not equal to the program default
179 * (all the following code is not perfect but it works for the moment) */
180 static void apply_settings(void)
182 ui_update_fold_items();
184 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
186 if (! ui_prefs
.msgwindow_visible
)
188 ignore_callback
= TRUE
;
189 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_messages_window1")), FALSE
);
190 gtk_widget_hide(main_widgets
.message_window_notebook
);
191 ignore_callback
= FALSE
;
193 if (! ui_prefs
.sidebar_visible
)
195 ignore_callback
= TRUE
;
196 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_sidebar1")), FALSE
);
197 ignore_callback
= FALSE
;
200 toolbar_apply_settings();
203 ui_update_view_editor_menu_items();
205 /* hide statusbar if desired */
206 if (! interface_prefs
.statusbar_visible
)
208 gtk_widget_hide(ui_widgets
.statusbar
);
211 /* set the tab placements of the notebooks */
212 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets
.notebook
), interface_prefs
.tab_pos_editor
);
213 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow
.notebook
), interface_prefs
.tab_pos_msgwin
);
214 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), interface_prefs
.tab_pos_sidebar
);
216 /* whether to show notebook tabs or not */
217 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets
.notebook
), interface_prefs
.show_notebook_tabs
);
220 if (! vte_info
.have_vte
)
223 gtk_widget_set_sensitive(
224 ui_lookup_widget(main_widgets
.window
, "send_selection_to_vte1"), FALSE
);
227 if (interface_prefs
.sidebar_pos
!= GTK_POS_LEFT
)
228 ui_swap_sidebar_pos();
230 gtk_orientable_set_orientation(GTK_ORIENTABLE(ui_lookup_widget(main_widgets
.window
, "vpaned1")),
231 interface_prefs
.msgwin_orientation
);
235 static void on_window_active_changed(GtkWindow
*window
, GParamSpec
*pspec
, gpointer data
)
237 GeanyDocument
*doc
= document_get_current();
239 if (doc
&& gtk_window_is_active(window
))
240 document_check_disk_status(doc
, TRUE
);
244 static void main_init(void)
246 /* add our icon path in case we aren't installed in the system prefix */
247 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), utils_resource_dir(RESOURCE_DIR_ICON
));
250 ui_init_stock_items();
254 main_widgets
.window
= NULL
;
256 ui_widgets
.open_fontsel
= NULL
;
257 ui_widgets
.open_colorsel
= NULL
;
258 ui_widgets
.prefs_dialog
= NULL
;
259 main_status
.main_window_realized
= FALSE
;
260 file_prefs
.tab_order_ltr
= FALSE
;
261 file_prefs
.tab_order_beside
= FALSE
;
262 main_status
.quitting
= FALSE
;
263 ignore_callback
= FALSE
;
264 ui_prefs
.recent_queue
= g_queue_new();
265 ui_prefs
.recent_projects_queue
= g_queue_new();
266 main_status
.opening_session_files
= FALSE
;
268 main_widgets
.window
= create_window1();
269 g_signal_connect(main_widgets
.window
, "notify::is-active", G_CALLBACK(on_window_active_changed
), NULL
);
271 /* add recent projects to the Project menu */
272 ui_widgets
.recent_projects_menuitem
= ui_lookup_widget(main_widgets
.window
, "recent_projects1");
273 ui_widgets
.recent_projects_menu_menubar
= gtk_menu_new();
274 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets
.recent_projects_menuitem
),
275 ui_widgets
.recent_projects_menu_menubar
);
277 /* store important pointers for later reference */
278 main_widgets
.toolbar
= toolbar_init();
279 main_widgets
.sidebar_notebook
= ui_lookup_widget(main_widgets
.window
, "notebook3");
280 main_widgets
.notebook
= ui_lookup_widget(main_widgets
.window
, "notebook1");
281 main_widgets
.editor_menu
= create_edit_menu1();
282 main_widgets
.tools_menu
= ui_lookup_widget(main_widgets
.window
, "tools1_menu");
283 main_widgets
.message_window_notebook
= ui_lookup_widget(main_widgets
.window
, "notebook_info");
284 main_widgets
.project_menu
= ui_lookup_widget(main_widgets
.window
, "menu_project1_menu");
286 ui_widgets
.toolbar_menu
= create_toolbar_popup_menu1();
288 #ifdef MAC_INTEGRATION
292 /* set widget names for matching with GTK CSS */
293 gtk_widget_set_name(main_widgets
.window
, "GeanyMainWindow");
294 gtk_widget_set_name(ui_widgets
.toolbar_menu
, "GeanyToolbarMenu");
295 gtk_widget_set_name(main_widgets
.editor_menu
, "GeanyEditMenu");
296 gtk_widget_set_name(ui_lookup_widget(main_widgets
.window
, "menubar1"), "GeanyMenubar");
297 gtk_widget_set_name(main_widgets
.toolbar
, "GeanyToolbar");
299 gtk_window_set_default_size(GTK_WINDOW(main_widgets
.window
),
300 GEANY_WINDOW_DEFAULT_WIDTH
, GEANY_WINDOW_DEFAULT_HEIGHT
);
304 const gchar
*main_get_version_string(void)
306 static gchar full
[] = VERSION
" (git >= " REVISION
")";
308 if (utils_str_equal(REVISION
, "-1"))
315 /* get the full file path of a command-line argument
316 * N.B. the result should be freed and may contain '/../' or '/./ ' */
317 gchar
*main_get_argv_filename(const gchar
*filename
)
321 if (g_path_is_absolute(filename
) || utils_is_uri(filename
))
322 result
= g_strdup(filename
);
325 /* use current dir */
326 gchar
*cur_dir
= NULL
;
327 if (original_cwd
== NULL
)
328 cur_dir
= g_get_current_dir();
330 cur_dir
= g_strdup(original_cwd
);
333 G_DIR_SEPARATOR_S
, cur_dir
, filename
, NULL
);
340 /* get a :line:column specifier from the end of a filename (if present),
341 * return the line/column values, and remove the specifier from the string
342 * (Note that *line and *column must both be set to -1 initially) */
343 static void get_line_and_column_from_filename(gchar
*filename
, gint
*line
, gint
*column
)
346 gint colon_count
= 0;
347 gboolean have_number
= FALSE
;
350 g_assert(*line
== -1 && *column
== -1);
352 if (G_UNLIKELY(EMPTY(filename
)))
355 /* allow to open files like "test:0" */
356 if (g_file_test(filename
, G_FILE_TEST_EXISTS
))
359 len
= strlen(filename
);
360 for (i
= len
- 1; i
>= 1; i
--)
362 gboolean is_colon
= filename
[i
] == ':';
363 gboolean is_digit
= g_ascii_isdigit(filename
[i
]);
365 if (! is_colon
&& ! is_digit
)
370 if (++colon_count
> 1)
371 break; /* bail on 2+ colons in a row */
379 if (is_colon
&& have_number
)
381 gint number
= atoi(&filename
[i
+ 1]);
391 break; /* line and column are set, so we're done */
397 static gint
get_windows_socket_port(void)
399 /* Read config file early to get TCP port number as we need it for IPC before all
400 * other settings are read in load_settings() */
401 gchar
*configfile
= g_build_filename(app
->configdir
, "geany.conf", NULL
);
402 GKeyFile
*config
= g_key_file_new();
405 if (! g_file_test(configfile
, G_FILE_TEST_IS_REGULAR
))
408 "No user config file found, use default TCP port (%s).",
409 SOCKET_WINDOWS_REMOTE_CMD_PORT
);
411 return SOCKET_WINDOWS_REMOTE_CMD_PORT
;
413 g_key_file_load_from_file(config
, configfile
, G_KEY_FILE_NONE
, NULL
);
414 port_number
= utils_get_setting_integer(config
, PACKAGE
, "socket_remote_cmd_port",
415 SOCKET_WINDOWS_REMOTE_CMD_PORT
);
416 geany_debug("Using TCP port number %d for IPC", port_number
);
418 g_key_file_free(config
);
419 g_return_val_if_fail(port_number
>= 1024 && port_number
<= (gint
)G_MAXUINT16
,
420 SOCKET_WINDOWS_REMOTE_CMD_PORT
);
425 static void change_working_directory_on_windows(void)
427 gchar
*install_dir
= win32_get_installation_dir();
429 /* remember original working directory for use with opening files from the command line */
430 original_cwd
= g_get_current_dir();
432 /* On Windows, change the working directory to the Geany installation path to not lock
433 * the directory of a file passed as command line argument (see bug #2626124).
434 * This also helps if plugins or other code uses relative paths to load
435 * any additional resources (e.g. share/geany-plugins/...). */
436 win32_set_working_directory(install_dir
);
443 static void setup_paths(void)
445 /* convert path names to locale encoding */
446 app
->datadir
= utils_get_locale_from_utf8(utils_resource_dir(RESOURCE_DIR_DATA
));
447 app
->docdir
= utils_get_locale_from_utf8(utils_resource_dir(RESOURCE_DIR_DOC
));
452 * Checks whether the main window has been realized.
453 * This is an easy indicator whether Geany is right now starting up (main window is not
454 * yet realized) or whether it has finished the startup process (main window is realized).
455 * This is because the main window is realized (i.e. actually drawn on the screen) at the
456 * end of the startup process.
458 * @note Maybe you want to use the @link pluginsignals.c @c "geany-startup-complete" signal @endlink
459 * to get notified about the completed startup process.
461 * @return @c TRUE if the Geany main window has been realized or @c FALSE otherwise.
466 gboolean
main_is_realized(void)
468 return main_status
.main_window_realized
;
473 * Initialises the gettext translation system.
474 * This is a convenience function to set up gettext for internationalisation support
475 * in external plugins. You should call this function early in @ref plugin_init().
476 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
477 * The codeset for the message translations is set to UTF-8.
479 * Note that this function only setups the gettext textdomain for you. You still have
480 * to adjust the build system of your plugin to get internationalisation support
483 * If you have already used @ref PLUGIN_SET_TRANSLATABLE_INFO() you
484 * don't need to call main_locale_init() again as it has already been done.
486 * @param locale_dir The location where the translation files should be searched. This is
487 * usually the @c LOCALEDIR macro, defined by the build system.
488 * E.g. @c $prefix/share/locale.
489 * Only used on non-Windows systems. On Windows, the directory is determined
490 * by @c g_win32_get_package_installation_directory().
491 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
492 * defined by the build system.
497 void main_locale_init(const gchar
*locale_dir
, const gchar
*package
)
500 setlocale(LC_ALL
, "");
504 locale_dir
= utils_resource_dir(RESOURCE_DIR_LOCALE
);
506 (void) bindtextdomain(package
, locale_dir
);
507 (void) bind_textdomain_codeset(package
, "UTF-8");
511 static void print_filetypes(void)
513 const GSList
*list
, *node
;
515 filetypes_init_types();
516 printf("Geany's filetype names:\n");
518 list
= filetypes_get_sorted_by_name();
519 foreach_slist(node
, list
)
521 GeanyFiletype
*ft
= node
->data
;
523 printf("%s\n", ft
->name
);
525 filetypes_free_types();
529 static void wait_for_input_on_windows(void)
534 geany_debug("Press any key to continue");
541 static void parse_command_line_options(gint
*argc
, gchar
***argv
)
543 GError
*error
= NULL
;
544 GOptionContext
*context
;
546 CommandLineOptions def_clo
= {FALSE
, NULL
, TRUE
, -1, -1, FALSE
, FALSE
, FALSE
};
548 /* first initialise cl_options fields with default values */
549 cl_options
= def_clo
;
551 /* the GLib option parser can't handle the +NNN (line number) option,
552 * so we grab that here and replace it with a no-op */
553 for (i
= 1; i
< (*argc
); i
++)
555 if ((*argv
)[i
][0] != '+')
558 cl_options
.goto_line
= atoi((*argv
)[i
] + 1);
559 (*argv
)[i
] = (gchar
*) "--dummy";
562 context
= g_option_context_new(_("[FILES...]"));
564 g_option_context_set_summary(context
, _("A fast and lightweight IDE."));
565 g_option_context_set_description(context
, _("Report bugs to https://github.com/geany/geany/issues."));
566 g_option_context_add_main_entries(context
, entries
, GETTEXT_PACKAGE
);
567 g_option_group_set_translation_domain(g_option_context_get_main_group(context
), GETTEXT_PACKAGE
);
568 g_option_context_add_group(context
, gtk_get_option_group(FALSE
));
569 g_option_context_parse(context
, argc
, argv
, &error
);
570 g_option_context_free(context
);
574 g_printerr("Geany: %s\n", error
->message
);
579 app
->debug_mode
= verbose_mode
;
582 /* Since GLib 2.32 messages logged with levels INFO and DEBUG aren't output by the
583 * default log handler unless the G_MESSAGES_DEBUG environment variable contains the
584 * domain of the message or is set to the special value "all" */
585 g_setenv("G_MESSAGES_DEBUG", "all", FALSE
);
589 win32_init_debug_code();
594 gchar
*build_date
= utils_parse_and_format_build_date(__DATE__
);
596 printf(PACKAGE
" %s (", main_get_version_string());
597 /* note for translators: library versions are printed after this */
598 printf(_("built on %s with "), build_date
);
599 printf(geany_lib_versions
,
600 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
,
601 GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
604 wait_for_input_on_windows();
610 printf("%s\n", GEANY_PREFIX
);
611 printf("%s\n", GEANY_DATADIR
);
612 printf("%s\n", GEANY_LIBDIR
);
613 printf("%s\n", GEANY_LOCALEDIR
);
614 wait_for_input_on_windows();
618 if (alternate_config
)
620 geany_debug("Using alternate configuration directory");
621 app
->configdir
= alternate_config
;
625 app
->configdir
= utils_get_user_config_dir();
632 filetypes_init_types();
633 ret
= symbols_generate_global_tags(*argc
, *argv
, ! no_preprocessing
);
634 filetypes_free_types();
635 wait_for_input_on_windows();
642 wait_for_input_on_windows();
647 socket_info
.ignore_socket
= cl_options
.new_instance
;
648 if (cl_options
.socket_filename
)
650 socket_info
.file_name
= cl_options
.socket_filename
;
655 vte_info
.lib_vte
= lib_vte
;
657 cl_options
.ignore_global_tags
= ignore_global_tags
;
659 if (! gtk_init_check(NULL
, NULL
))
660 { /* check whether we have a valid X display and exit if not */
661 g_printerr("Geany: cannot open display\n");
665 #ifdef MAC_INTEGRATION
666 /* Create GtkosxApplication singleton - should be created shortly after gtk_init() */
667 gtkosx_application_get();
672 static gint
create_config_dir(void)
674 gint saved_errno
= 0;
675 gchar
*conf_file
= NULL
;
676 gchar
*filedefs_dir
= NULL
;
677 gchar
*templates_dir
= NULL
;
679 if (! g_file_test(app
->configdir
, G_FILE_TEST_EXISTS
))
682 /* if we are *not* using an alternate config directory, we check whether the old one
683 * in ~/.geany still exists and try to move it */
684 if (alternate_config
== NULL
)
686 gchar
*old_dir
= g_build_filename(g_get_home_dir(), ".geany", NULL
);
687 /* move the old config dir if it exists */
688 if (g_file_test(old_dir
, G_FILE_TEST_EXISTS
))
690 if (! dialogs_show_question_full(main_widgets
.window
,
691 GTK_STOCK_YES
, GTK_STOCK_QUIT
, _("Move it now?"),
693 _("Geany needs to move your old configuration directory before starting.")))
696 if (! g_file_test(app
->configdir
, G_FILE_TEST_IS_DIR
))
697 utils_mkdir(app
->configdir
, TRUE
);
699 if (g_rename(old_dir
, app
->configdir
) == 0)
701 dialogs_show_msgbox(GTK_MESSAGE_INFO
,
702 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
703 old_dir
, app
->configdir
);
709 dialogs_show_msgbox(GTK_MESSAGE_WARNING
,
710 /* for translators: the third %s in brackets is the error message which
711 * describes why moving the dir didn't work */
712 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
713 "Please move manually the directory to the new location."),
714 old_dir
, app
->configdir
, g_strerror(errno
));
720 geany_debug("Creating configuration directory");
721 saved_errno
= utils_mkdir(app
->configdir
, TRUE
);
724 conf_file
= g_build_filename(app
->configdir
, "geany.conf", NULL
);
725 filedefs_dir
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, NULL
);
726 templates_dir
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
, NULL
);
728 if (saved_errno
== 0 && ! g_file_test(conf_file
, G_FILE_TEST_EXISTS
))
729 { /* check whether geany.conf can be written */
730 saved_errno
= utils_is_file_writable(app
->configdir
);
733 /* make subdir for filetype definitions */
734 if (saved_errno
== 0)
736 gchar
*filedefs_readme
= g_build_filename(app
->configdir
,
737 GEANY_FILEDEFS_SUBDIR
, "filetypes.README", NULL
);
739 if (! g_file_test(filedefs_dir
, G_FILE_TEST_EXISTS
))
741 saved_errno
= utils_mkdir(filedefs_dir
, FALSE
);
743 if (saved_errno
== 0 && ! g_file_test(filedefs_readme
, G_FILE_TEST_EXISTS
))
745 gchar
*text
= g_strconcat(
746 "Copy files from ", app
->datadir
, "/filedefs to this directory to overwrite "
747 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
748 "the documentation (in ", app
->docdir
, G_DIR_SEPARATOR_S
"index.html or visit " GEANY_HOMEPAGE
").", NULL
);
749 utils_write_file(filedefs_readme
, text
);
752 g_free(filedefs_readme
);
755 /* make subdir for template files */
756 if (saved_errno
== 0)
758 gchar
*templates_readme
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
,
759 "templates.README", NULL
);
761 if (! g_file_test(templates_dir
, G_FILE_TEST_EXISTS
))
763 saved_errno
= utils_mkdir(templates_dir
, FALSE
);
765 if (saved_errno
== 0 && ! g_file_test(templates_readme
, G_FILE_TEST_EXISTS
))
767 gchar
*text
= g_strconcat(
768 "There are several template files in this directory. For these templates you can use wildcards.\n\
769 For more information read the documentation (in ", app
->docdir
, G_DIR_SEPARATOR_S
"index.html or visit " GEANY_HOMEPAGE
").",
771 utils_write_file(templates_readme
, text
);
774 g_free(templates_readme
);
777 g_free(filedefs_dir
);
778 g_free(templates_dir
);
785 /* Returns 0 if config dir is OK. */
786 static gint
setup_config_dir(void)
788 gint mkdir_result
= 0;
790 mkdir_result
= create_config_dir();
791 if (mkdir_result
!= 0)
793 if (! dialogs_show_question(
794 _("Configuration directory could not be created (%s).\nThere could be some problems "
795 "using Geany without a configuration directory.\nStart Geany anyway?"),
796 g_strerror(mkdir_result
)))
801 /* make configdir a real path */
802 if (g_file_test(app
->configdir
, G_FILE_TEST_EXISTS
))
803 SETPTR(app
->configdir
, utils_get_real_path(app
->configdir
));
810 static gboolean
signal_cb(gpointer user_data
)
812 gint sig
= GPOINTER_TO_INT(user_data
);
815 geany_debug("Received SIGTERM signal");
818 return G_SOURCE_REMOVE
;
823 /* Used for command-line arguments at startup or from socket.
824 * this will strip any :line:col filename suffix from locale_filename */
825 gboolean
main_handle_filename(const gchar
*locale_filename
)
828 gint line
= -1, column
= -1;
831 g_return_val_if_fail(locale_filename
, FALSE
);
833 /* check whether the passed filename is an URI */
834 filename
= utils_get_path_from_uri(locale_filename
);
835 if (filename
== NULL
)
838 get_line_and_column_from_filename(filename
, &line
, &column
);
840 cl_options
.goto_line
= line
;
842 cl_options
.goto_column
= column
;
844 if (g_file_test(filename
, G_FILE_TEST_IS_REGULAR
))
846 doc
= document_open_file(filename
, cl_options
.readonly
, NULL
, NULL
);
847 /* add recent file manually if opening_session_files is set */
848 if (doc
!= NULL
&& main_status
.opening_session_files
)
849 ui_add_recent_document(doc
);
853 else if (file_prefs
.cmdline_new_files
)
854 { /* create new file with the given filename */
855 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
857 doc
= document_find_by_filename(utf8_filename
);
859 document_show_tab(doc
);
861 doc
= document_new_file(utf8_filename
, NULL
, NULL
);
863 ui_add_recent_document(doc
);
864 g_free(utf8_filename
);
873 /* open files from command line */
874 static void open_cl_files(gint argc
, gchar
**argv
)
878 for (i
= 1; i
< argc
; i
++)
880 gchar
*filename
= main_get_argv_filename(argv
[i
]);
882 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
889 /* It seems argv elements are encoded in CP1252 on a German Windows */
890 SETPTR(filename
, g_locale_to_utf8(filename
, -1, NULL
, NULL
, NULL
));
892 if (filename
&& ! main_handle_filename(filename
))
894 const gchar
*msg
= _("Could not find file '%s'.");
896 g_printerr(msg
, filename
); /* also print to the terminal */
898 ui_set_statusbar(TRUE
, msg
, filename
);
905 static void load_session_project_file(void)
907 gchar
*locale_filename
;
909 g_return_if_fail(project_prefs
.session_file
!= NULL
);
911 locale_filename
= utils_get_locale_from_utf8(project_prefs
.session_file
);
913 if (G_LIKELY(!EMPTY(locale_filename
)))
914 project_load_file(locale_filename
);
916 g_free(locale_filename
);
917 g_free(project_prefs
.session_file
); /* no longer needed */
921 static void load_settings(void)
924 vte_info
.load_vte_cmdline
= !no_vte
;
926 configuration_load();
927 /* let cmdline options overwrite configuration settings */
929 vte_info
.have_vte
= vte_info
.load_vte
&& vte_info
.load_vte_cmdline
;
932 ui_prefs
.msgwindow_visible
= FALSE
;
935 want_plugins
= prefs
.load_plugins
&& !no_plugins
;
940 void main_load_project_from_command_line(const gchar
*locale_filename
, gboolean use_session
)
944 pfile
= utils_get_path_from_uri(locale_filename
);
948 project_load_file_with_session(pfile
);
950 project_load_file(pfile
);
956 static void load_startup_files(gint argc
, gchar
**argv
)
958 gboolean load_session
= FALSE
;
960 if (argc
> 1 && g_str_has_suffix(argv
[1], ".geany"))
962 gchar
*filename
= main_get_argv_filename(argv
[1]);
964 /* project file specified: load it, but decide the session later */
965 main_load_project_from_command_line(filename
, FALSE
);
967 /* force session load if using project-based session files */
968 load_session
= project_prefs
.project_session
;
972 /* Load the default session if:
973 * 1. "Load files from the last session" is active.
974 * 2. --no-session is not specified.
975 * 3. We are a primary instance.
976 * Has no effect if a CL project is loaded and using project-based session files. */
977 if (prefs
.load_session
&& cl_options
.load_session
&& !cl_options
.new_instance
)
979 if (app
->project
== NULL
)
980 load_session_project_file();
986 /* load session files into tabs, as they are found in the session_files variable */
987 configuration_open_files();
989 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) == 0)
991 ui_update_popup_copy_items(NULL
);
992 ui_update_popup_reundo_items(NULL
);
996 open_cl_files(argc
, argv
);
1000 static gboolean
send_startup_complete(gpointer data
)
1002 g_signal_emit_by_name(geany_object
, "geany-startup-complete");
1007 static const gchar
*get_locale(void)
1009 const gchar
*locale
= "unknown";
1010 #ifdef HAVE_LOCALE_H
1011 locale
= setlocale(LC_CTYPE
, NULL
);
1018 gint
main_lib(gint argc
, gchar
**argv
)
1021 gint config_dir_result
;
1022 const gchar
*locale
;
1023 gchar
*utf8_configdir
;
1025 #if ! GLIB_CHECK_VERSION(2, 36, 0)
1029 log_handlers_init();
1031 app
= g_new0(GeanyApp
, 1);
1032 memset(&main_status
, 0, sizeof(GeanyStatus
));
1033 memset(&prefs
, 0, sizeof(GeanyPrefs
));
1034 memset(&interface_prefs
, 0, sizeof(GeanyInterfacePrefs
));
1035 memset(&toolbar_prefs
, 0, sizeof(GeanyToolbarPrefs
));
1036 memset(&file_prefs
, 0, sizeof(GeanyFilePrefs
));
1037 memset(&search_prefs
, 0, sizeof(GeanySearchPrefs
));
1038 memset(&tool_prefs
, 0, sizeof(GeanyToolPrefs
));
1039 memset(&template_prefs
, 0, sizeof(GeanyTemplatePrefs
));
1040 memset(&ui_prefs
, 0, sizeof(UIPrefs
));
1041 memset(&ui_widgets
, 0, sizeof(UIWidgets
));
1046 main_locale_init(utils_resource_dir(RESOURCE_DIR_LOCALE
), GETTEXT_PACKAGE
);
1048 /* initialize TM before parsing command-line - needed for tag file generation */
1049 app
->tm_workspace
= tm_get_workspace();
1050 parse_command_line_options(&argc
, &argv
);
1052 #if ! GLIB_CHECK_VERSION(2, 32, 0)
1053 /* Initialize GLib's thread system in case any plugins want to use it or their
1054 * dependencies (e.g. WebKit, Soup, ...). Deprecated since GLIB 2.32. */
1055 if (!g_thread_supported())
1056 g_thread_init(NULL
);
1060 g_unix_signal_add(SIGTERM
, signal_cb
, GINT_TO_POINTER(SIGTERM
));
1062 /* ignore SIGPIPE signal for preventing sudden death of program */
1063 signal(SIGPIPE
, SIG_IGN
);
1066 config_dir_result
= setup_config_dir();
1068 /* check and create (unix domain) socket for remote operation */
1069 if (! socket_info
.ignore_socket
)
1071 gushort socket_port
= 0;
1073 socket_port
= (gushort
) get_windows_socket_port();
1075 socket_info
.lock_socket
= -1;
1076 socket_info
.lock_socket_tag
= 0;
1077 socket_info
.lock_socket
= socket_init(argc
, argv
, socket_port
);
1078 /* Quit if filenames were sent to first instance or the list of open
1079 * documents has been printed */
1080 if ((socket_info
.lock_socket
== -2 /* socket exists */ && argc
> 1) ||
1081 cl_options
.list_documents
)
1084 gdk_notify_startup_complete();
1085 g_free(app
->configdir
);
1086 g_free(app
->datadir
);
1087 g_free(app
->docdir
);
1091 /* Start a new instance if no command line strings were passed,
1092 * even if the socket already exists */
1093 else if (socket_info
.lock_socket
== -2 /* socket already exists */)
1095 socket_info
.ignore_socket
= TRUE
;
1096 cl_options
.new_instance
= TRUE
;
1102 /* after we initialized the socket code and handled command line args,
1103 * let's change the working directory on Windows to not lock it */
1104 change_working_directory_on_windows();
1107 locale
= get_locale();
1108 geany_debug("Geany %s, %s",
1109 main_get_version_string(),
1111 geany_debug(geany_lib_versions
,
1112 gtk_major_version
, gtk_minor_version
, gtk_micro_version
,
1113 glib_major_version
, glib_minor_version
, glib_micro_version
);
1115 #if GLIB_CHECK_VERSION(2, 64, 0)
1116 gchar
*os_prettyname
= g_get_os_info(G_OS_INFO_KEY_PRETTY_NAME
);
1117 gchar
*os_codename
= g_get_os_info(G_OS_INFO_KEY_VERSION_CODENAME
);
1118 geany_debug("OS: %s (%s)",
1119 os_prettyname
? os_prettyname
: "Unknown",
1120 os_codename
? os_codename
: "Unknown");
1121 g_free(os_prettyname
);
1122 g_free(os_codename
);
1125 geany_debug("System data dir: %s", app
->datadir
);
1126 utf8_configdir
= utils_get_utf8_from_locale(app
->configdir
);
1127 geany_debug("User config dir: %s", utf8_configdir
);
1128 g_free(utf8_configdir
);
1130 /* create the object so Geany signals can be connected in init() functions */
1131 geany_object
= geany_object_new();
1139 /* init stash groups before loading keyfile */
1140 configuration_init();
1148 load_settings(); /* load keyfile */
1152 ui_create_insert_menu_items();
1153 ui_create_insert_date_menu_items();
1159 document_init_doclist();
1161 editor_snippets_init();
1166 ui_create_recent_menus();
1168 ui_set_statusbar(TRUE
, _("This is Geany %s."), main_get_version_string());
1169 if (config_dir_result
!= 0)
1171 const gchar
*message
= _("Configuration directory could not be created (%s).");
1172 ui_set_statusbar(TRUE
, message
, g_strerror(config_dir_result
));
1173 g_warning(message
, g_strerror(config_dir_result
));
1176 if (socket_info
.lock_socket
== -1)
1178 const gchar
*message
=
1179 _("IPC socket could not be created, see Help->Debug Messages for details.");
1180 ui_set_statusbar(TRUE
, "%s", message
);
1181 g_warning("%s", message
);
1185 /* apply all configuration options */
1189 /* load any enabled plugins before we open any documents */
1191 plugins_load_active();
1194 ui_sidebar_show_hide();
1196 /* set the active sidebar page after plugins have been loaded */
1197 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), ui_prefs
.sidebar_page
);
1199 /* load keybinding settings after plugins have added their groups */
1200 keybindings_load_keyfile();
1202 /* create the custom command menu after the keybindings have been loaded to have the proper
1203 * accelerator shown for the menu items */
1204 tools_create_insert_custom_command_menu_items();
1206 /* load any command line files or session files */
1207 main_status
.opening_session_files
= TRUE
;
1208 load_startup_files(argc
, argv
);
1209 main_status
.opening_session_files
= FALSE
;
1211 /* open a new file if no other file was opened */
1212 document_new_file_if_non_open();
1214 ui_document_buttons_update();
1215 ui_save_buttons_toggle(FALSE
);
1217 doc
= document_get_current();
1218 sidebar_select_openfiles_item(doc
);
1219 build_menu_update(doc
);
1220 sidebar_update_tag_list(doc
, FALSE
);
1223 /* Manually realise the main window to be able to set the position but don't show it.
1224 * We don't set the position after showing the window to avoid flickering. */
1225 gtk_widget_realize(main_widgets
.window
);
1227 setup_window_position();
1229 /* finally show the window */
1230 document_grab_focus(doc
);
1231 gtk_widget_show(main_widgets
.window
);
1232 main_status
.main_window_realized
= TRUE
;
1234 configuration_apply_settings();
1237 /* register the callback of socket input */
1238 if (! socket_info
.ignore_socket
&& socket_info
.lock_socket
> 0)
1240 socket_info
.read_ioc
= g_io_channel_unix_new(socket_info
.lock_socket
);
1241 socket_info
.lock_socket_tag
= g_io_add_watch(socket_info
.read_ioc
,
1242 G_IO_IN
| G_IO_PRI
| G_IO_ERR
, socket_lock_input_cb
, main_widgets
.window
);
1246 /* when we are really done with setting everything up and the main event loop is running,
1247 * tell other components, mainly plugins, that startup is complete */
1248 g_idle_add_full(G_PRIORITY_LOW
, send_startup_complete
, NULL
, NULL
);
1250 #ifdef MAC_INTEGRATION
1251 /* OS X application ready - has to be called before entering main loop */
1252 gtkosx_application_ready(gtkosx_application_get());
1260 static void queue_free(GQueue
*queue
)
1262 while (! g_queue_is_empty(queue
))
1264 g_free(g_queue_pop_tail(queue
));
1266 g_queue_free(queue
);
1270 static gboolean
do_main_quit(void)
1272 configuration_save();
1274 if (app
->project
!= NULL
)
1276 if (!project_close(FALSE
)) /* save project session files */
1280 if (!document_close_all())
1283 geany_debug("Quitting...");
1285 main_status
.quitting
= TRUE
;
1298 highlighting_free_styles();
1299 templates_free_templates();
1303 document_finalize();
1307 editor_snippets_free();
1308 encodings_finalize();
1311 configuration_finalize();
1312 filetypes_free_types();
1315 tm_workspace_free();
1316 g_free(app
->configdir
);
1317 g_free(app
->datadir
);
1318 g_free(app
->docdir
);
1319 g_free(prefs
.default_open_path
);
1320 g_free(prefs
.custom_plugin_path
);
1321 g_free(ui_prefs
.custom_date_format
);
1322 g_free(ui_prefs
.color_picker_palette
);
1323 g_free(interface_prefs
.editor_font
);
1324 g_free(interface_prefs
.tagbar_font
);
1325 g_free(interface_prefs
.msgwin_font
);
1326 g_free(editor_prefs
.long_line_color
);
1327 g_free(editor_prefs
.comment_toggle_mark
);
1328 g_free(editor_prefs
.color_scheme
);
1329 g_free(tool_prefs
.context_action_cmd
);
1330 g_free(template_prefs
.developer
);
1331 g_free(template_prefs
.company
);
1332 g_free(template_prefs
.mail
);
1333 g_free(template_prefs
.initials
);
1334 g_free(template_prefs
.version
);
1335 g_free(tool_prefs
.term_cmd
);
1336 g_free(tool_prefs
.browser_cmd
);
1337 g_free(tool_prefs
.grep_cmd
);
1338 g_free(printing_prefs
.external_print_cmd
);
1339 g_free(printing_prefs
.page_header_datefmt
);
1340 g_strfreev(ui_prefs
.custom_commands
);
1341 g_strfreev(ui_prefs
.custom_commands_labels
);
1343 queue_free(ui_prefs
.recent_queue
);
1344 queue_free(ui_prefs
.recent_projects_queue
);
1346 if (ui_widgets
.prefs_dialog
&& GTK_IS_WIDGET(ui_widgets
.prefs_dialog
)) gtk_widget_destroy(ui_widgets
.prefs_dialog
);
1347 if (ui_widgets
.open_fontsel
&& GTK_IS_WIDGET(ui_widgets
.open_fontsel
)) gtk_widget_destroy(ui_widgets
.open_fontsel
);
1348 if (ui_widgets
.open_colorsel
&& GTK_IS_WIDGET(ui_widgets
.open_colorsel
)) gtk_widget_destroy(ui_widgets
.open_colorsel
);
1350 if (vte_info
.have_vte
) vte_close();
1351 g_free(vte_info
.lib_vte
);
1352 g_free(vte_info
.dir
);
1354 gtk_widget_destroy(main_widgets
.window
);
1356 /* destroy popup menus */
1357 if (main_widgets
.editor_menu
&& GTK_IS_WIDGET(main_widgets
.editor_menu
))
1358 gtk_widget_destroy(main_widgets
.editor_menu
);
1359 if (ui_widgets
.toolbar_menu
&& GTK_IS_WIDGET(ui_widgets
.toolbar_menu
))
1360 gtk_widget_destroy(ui_widgets
.toolbar_menu
);
1361 if (msgwindow
.popup_status_menu
&& GTK_IS_WIDGET(msgwindow
.popup_status_menu
))
1362 gtk_widget_destroy(msgwindow
.popup_status_menu
);
1363 if (msgwindow
.popup_msg_menu
&& GTK_IS_WIDGET(msgwindow
.popup_msg_menu
))
1364 gtk_widget_destroy(msgwindow
.popup_msg_menu
);
1365 if (msgwindow
.popup_compiler_menu
&& GTK_IS_WIDGET(msgwindow
.popup_compiler_menu
))
1366 gtk_widget_destroy(msgwindow
.popup_compiler_menu
);
1368 g_object_unref(geany_object
);
1369 geany_object
= NULL
;
1371 g_free(original_cwd
);
1374 ui_finalize_builder();
1382 static gboolean
check_no_unsaved(void)
1386 for (i
= 0; i
< documents_array
->len
; i
++)
1388 if (documents
[i
]->is_valid
&& documents
[i
]->changed
)
1393 return TRUE
; /* no unsaved edits */
1397 /* Returns false when quitting is aborted due to user cancellation */
1398 gboolean
main_quit(void)
1400 main_status
.quitting
= TRUE
;
1402 if (! check_no_unsaved())
1408 if (! prefs
.confirm_exit
||
1409 dialogs_show_question_full(NULL
, GTK_STOCK_QUIT
, GTK_STOCK_CANCEL
, NULL
,
1410 _("Do you really want to quit?")))
1416 main_status
.quitting
= FALSE
;
1421 * Reloads most of Geany's configuration files without restarting. Currently the following
1422 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1423 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1424 * and 'settings' and 'build_settings' sections of the filetype definition files.
1426 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1432 void main_reload_configuration(void)
1434 /* reload templates */
1435 templates_free_templates();
1438 /* reload snippets */
1439 editor_snippets_free();
1440 editor_snippets_init();
1442 filetypes_reload_extensions();
1445 /* C tag names to ignore */
1446 symbols_reload_config_files();
1448 ui_set_statusbar(TRUE
, _("Configuration files reloaded."));