2 * libmain.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Main program-related commands.
25 * Handles program initialization and cleanup.
36 #include "callbacks.h"
39 #include "encodings.h"
40 #include "filetypes.h"
41 #include "geanyobject.h"
42 #include "highlighting.h"
43 #include "keybindings.h"
46 #include "msgwindow.h"
58 #include "templates.h"
67 #include "gtkcompat.h"
71 #include <sys/types.h>
77 #include <glib/gstdio.h>
85 gboolean ignore_callback
; /* hack workaround for GTK+ toggle button callback problem */
87 GeanyStatus main_status
;
88 CommandLineOptions cl_options
; /* fields initialised in parse_command_line_options */
90 static gchar
*original_cwd
= NULL
;
92 static const gchar geany_lib_versions
[] = "GTK %u.%u.%u, GLib %u.%u.%u";
94 static gboolean want_plugins
;
96 /* command-line options */
97 static gboolean verbose_mode
= FALSE
;
98 static gboolean ignore_global_tags
= FALSE
;
99 static gboolean no_msgwin
= FALSE
;
100 static gboolean show_version
= FALSE
;
101 static gchar
*alternate_config
= NULL
;
103 static gboolean no_vte
= FALSE
;
104 static gchar
*lib_vte
= NULL
;
106 static gboolean generate_tags
= FALSE
;
107 static gboolean no_preprocessing
= FALSE
;
108 static gboolean ft_names
= FALSE
;
109 static gboolean print_prefix
= FALSE
;
111 static gboolean no_plugins
= FALSE
;
113 static gboolean dummy
= FALSE
;
115 /* in alphabetical order of short options */
116 static GOptionEntry entries
[] =
118 { "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
},
119 { "config", 'c', 0, G_OPTION_ARG_FILENAME
, &alternate_config
, N_("Use an alternate configuration directory"), NULL
},
120 { "ft-names", 0, 0, G_OPTION_ARG_NONE
, &ft_names
, N_("Print internal filetype names"), NULL
},
121 { "generate-tags", 'g', 0, G_OPTION_ARG_NONE
, &generate_tags
, N_("Generate global tags file (see documentation)"), NULL
},
122 { "no-preprocessing", 'P', 0, G_OPTION_ARG_NONE
, &no_preprocessing
, N_("Don't preprocess C/C++ files when generating tags file"), NULL
},
124 { "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
},
125 { "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
},
126 { "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
},
128 { "line", 'l', 0, G_OPTION_ARG_INT
, &cl_options
.goto_line
, N_("Set initial line number for the first opened file"), NULL
},
129 { "no-msgwin", 'm', 0, G_OPTION_ARG_NONE
, &no_msgwin
, N_("Don't show message window at startup"), NULL
},
130 { "no-ctags", 'n', 0, G_OPTION_ARG_NONE
, &ignore_global_tags
, N_("Don't load auto completion data (see documentation)"), NULL
},
132 { "no-plugins", 'p', 0, G_OPTION_ARG_NONE
, &no_plugins
, N_("Don't load plugins"), NULL
},
134 { "print-prefix", 0, 0, G_OPTION_ARG_NONE
, &print_prefix
, N_("Print Geany's installation prefix"), NULL
},
135 { "read-only", 'r', 0, G_OPTION_ARG_NONE
, &cl_options
.readonly
, N_("Open all FILES in read-only mode (see documentation)"), NULL
},
136 { "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
},
138 { "no-terminal", 't', 0, G_OPTION_ARG_NONE
, &no_vte
, N_("Don't load terminal support"), NULL
},
139 { "vte-lib", 0, 0, G_OPTION_ARG_FILENAME
, &lib_vte
, N_("Filename of libvte.so"), NULL
},
141 { "verbose", 'v', 0, G_OPTION_ARG_NONE
, &verbose_mode
, N_("Be verbose"), NULL
},
142 { "version", 'V', 0, G_OPTION_ARG_NONE
, &show_version
, N_("Show version and exit"), NULL
},
143 { "dummy", 0, G_OPTION_FLAG_HIDDEN
, G_OPTION_ARG_NONE
, &dummy
, NULL
, NULL
}, /* for +NNN line number arguments */
144 { NULL
, 0, 0, 0, NULL
, NULL
, NULL
}
148 static void setup_window_position(void)
150 /* interprets the saved window geometry */
151 if (!prefs
.save_winpos
)
154 if (ui_prefs
.geometry
[0] != -1 && ui_prefs
.geometry
[1] != -1)
155 gtk_window_move(GTK_WINDOW(main_widgets
.window
),
156 ui_prefs
.geometry
[0], ui_prefs
.geometry
[1]);
158 if (ui_prefs
.geometry
[2] != -1 && ui_prefs
.geometry
[3] != -1)
159 gtk_window_set_default_size(GTK_WINDOW(main_widgets
.window
),
160 ui_prefs
.geometry
[2], ui_prefs
.geometry
[3]);
162 if (ui_prefs
.geometry
[4] == 1)
163 gtk_window_maximize(GTK_WINDOW(main_widgets
.window
));
167 /* special things for the initial setup of the checkboxes and related stuff
168 * an action on a setting is only performed if the setting is not equal to the program default
169 * (all the following code is not perfect but it works for the moment) */
170 static void apply_settings(void)
172 ui_update_fold_items();
174 /* toolbar, message window and sidebar are by default visible, so don't change it if it is true */
176 if (! ui_prefs
.msgwindow_visible
)
178 ignore_callback
= TRUE
;
179 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_messages_window1")), FALSE
);
180 gtk_widget_hide(main_widgets
.message_window_notebook
);
181 ignore_callback
= FALSE
;
183 if (! ui_prefs
.sidebar_visible
)
185 ignore_callback
= TRUE
;
186 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets
.window
, "menu_show_sidebar1")), FALSE
);
187 ignore_callback
= FALSE
;
190 toolbar_apply_settings();
193 ui_update_view_editor_menu_items();
195 /* hide statusbar if desired */
196 if (! interface_prefs
.statusbar_visible
)
198 gtk_widget_hide(ui_widgets
.statusbar
);
201 /* set the tab placements of the notebooks */
202 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets
.notebook
), interface_prefs
.tab_pos_editor
);
203 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(msgwindow
.notebook
), interface_prefs
.tab_pos_msgwin
);
204 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), interface_prefs
.tab_pos_sidebar
);
206 /* whether to show notebook tabs or not */
207 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(main_widgets
.notebook
), interface_prefs
.show_notebook_tabs
);
210 if (! vte_info
.have_vte
)
213 gtk_widget_set_sensitive(
214 ui_lookup_widget(main_widgets
.window
, "send_selection_to_vte1"), FALSE
);
217 if (interface_prefs
.sidebar_pos
!= GTK_POS_LEFT
)
218 ui_swap_sidebar_pos();
220 gtk_orientable_set_orientation(GTK_ORIENTABLE(ui_lookup_widget(main_widgets
.window
, "vpaned1")),
221 interface_prefs
.msgwin_orientation
);
225 static void main_init(void)
227 /* add our icon path in case we aren't installed in the system prefix */
228 gtk_icon_theme_append_search_path(gtk_icon_theme_get_default(), utils_resource_dir(RESOURCE_DIR_ICON
));
231 ui_init_stock_items();
235 main_widgets
.window
= NULL
;
237 ui_widgets
.open_fontsel
= NULL
;
238 ui_widgets
.open_colorsel
= NULL
;
239 ui_widgets
.prefs_dialog
= NULL
;
240 main_status
.main_window_realized
= FALSE
;
241 file_prefs
.tab_order_ltr
= FALSE
;
242 file_prefs
.tab_order_beside
= FALSE
;
243 main_status
.quitting
= FALSE
;
244 ignore_callback
= FALSE
;
245 app
->tm_workspace
= tm_get_workspace();
246 ui_prefs
.recent_queue
= g_queue_new();
247 ui_prefs
.recent_projects_queue
= g_queue_new();
248 main_status
.opening_session_files
= FALSE
;
250 main_widgets
.window
= create_window1();
252 /* add recent projects to the Project menu */
253 ui_widgets
.recent_projects_menuitem
= ui_lookup_widget(main_widgets
.window
, "recent_projects1");
254 ui_widgets
.recent_projects_menu_menubar
= gtk_menu_new();
255 gtk_menu_item_set_submenu(GTK_MENU_ITEM(ui_widgets
.recent_projects_menuitem
),
256 ui_widgets
.recent_projects_menu_menubar
);
258 /* store important pointers for later reference */
259 main_widgets
.toolbar
= toolbar_init();
260 main_widgets
.sidebar_notebook
= ui_lookup_widget(main_widgets
.window
, "notebook3");
261 main_widgets
.notebook
= ui_lookup_widget(main_widgets
.window
, "notebook1");
262 main_widgets
.editor_menu
= create_edit_menu1();
263 main_widgets
.tools_menu
= ui_lookup_widget(main_widgets
.window
, "tools1_menu");
264 main_widgets
.message_window_notebook
= ui_lookup_widget(main_widgets
.window
, "notebook_info");
265 main_widgets
.project_menu
= ui_lookup_widget(main_widgets
.window
, "menu_project1_menu");
267 ui_widgets
.toolbar_menu
= create_toolbar_popup_menu1();
269 #ifdef MAC_INTEGRATION
273 /* set widget names for matching with .gtkrc-2.0 */
274 gtk_widget_set_name(main_widgets
.window
, "GeanyMainWindow");
275 gtk_widget_set_name(ui_widgets
.toolbar_menu
, "GeanyToolbarMenu");
276 gtk_widget_set_name(main_widgets
.editor_menu
, "GeanyEditMenu");
277 gtk_widget_set_name(ui_lookup_widget(main_widgets
.window
, "menubar1"), "GeanyMenubar");
278 gtk_widget_set_name(main_widgets
.toolbar
, "GeanyToolbar");
280 gtk_window_set_default_size(GTK_WINDOW(main_widgets
.window
),
281 GEANY_WINDOW_DEFAULT_WIDTH
, GEANY_WINDOW_DEFAULT_HEIGHT
);
285 const gchar
*main_get_version_string(void)
287 static gchar full
[] = VERSION
" (git >= " REVISION
")";
289 if (utils_str_equal(REVISION
, "-1"))
296 /* get the full file path of a command-line argument
297 * N.B. the result should be freed and may contain '/../' or '/./ ' */
298 gchar
*main_get_argv_filename(const gchar
*filename
)
302 if (g_path_is_absolute(filename
) || utils_is_uri(filename
))
303 result
= g_strdup(filename
);
306 /* use current dir */
307 gchar
*cur_dir
= NULL
;
308 if (original_cwd
== NULL
)
309 cur_dir
= g_get_current_dir();
311 cur_dir
= g_strdup(original_cwd
);
314 G_DIR_SEPARATOR_S
, cur_dir
, filename
, NULL
);
321 /* get a :line:column specifier from the end of a filename (if present),
322 * return the line/column values, and remove the specifier from the string
323 * (Note that *line and *column must both be set to -1 initially) */
324 static void get_line_and_column_from_filename(gchar
*filename
, gint
*line
, gint
*column
)
327 gint colon_count
= 0;
328 gboolean have_number
= FALSE
;
331 g_assert(*line
== -1 && *column
== -1);
333 if (G_UNLIKELY(EMPTY(filename
)))
336 /* allow to open files like "test:0" */
337 if (g_file_test(filename
, G_FILE_TEST_EXISTS
))
340 len
= strlen(filename
);
341 for (i
= len
- 1; i
>= 1; i
--)
343 gboolean is_colon
= filename
[i
] == ':';
344 gboolean is_digit
= g_ascii_isdigit(filename
[i
]);
346 if (! is_colon
&& ! is_digit
)
351 if (++colon_count
> 1)
352 break; /* bail on 2+ colons in a row */
360 if (is_colon
&& have_number
)
362 gint number
= atoi(&filename
[i
+ 1]);
372 break; /* line and column are set, so we're done */
378 static void change_working_directory_on_windows(void)
380 gchar
*install_dir
= win32_get_installation_dir();
382 /* remember original working directory for use with opening files from the command line */
383 original_cwd
= g_get_current_dir();
385 /* On Windows, change the working directory to the Geany installation path to not lock
386 * the directory of a file passed as command line argument (see bug #2626124).
387 * This also helps if plugins or other code uses relative paths to load
388 * any additional resources (e.g. share/geany-plugins/...). */
389 win32_set_working_directory(install_dir
);
396 static void setup_paths(void)
398 /* convert path names to locale encoding */
399 app
->datadir
= utils_get_locale_from_utf8(utils_resource_dir(RESOURCE_DIR_DATA
));
400 app
->docdir
= utils_get_locale_from_utf8(utils_resource_dir(RESOURCE_DIR_DOC
));
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.
419 gboolean
main_is_realized(void)
421 return main_status
.main_window_realized
;
426 * Initialises the gettext translation system.
427 * This is a convenience function to set up gettext for internationalisation support
428 * in external plugins. You should call this function early in @ref plugin_init().
429 * If the macro HAVE_LOCALE_H is defined, @c setlocale(LC_ALL, "") is called.
430 * The codeset for the message translations is set to UTF-8.
432 * Note that this function only setups the gettext textdomain for you. You still have
433 * to adjust the build system of your plugin to get internationalisation support
436 * If you have already used @ref PLUGIN_SET_TRANSLATABLE_INFO() you
437 * don't need to call main_locale_init() again as it has already been done.
439 * @param locale_dir The location where the translation files should be searched. This is
440 * usually the @c LOCALEDIR macro, defined by the build system.
441 * E.g. @c $prefix/share/locale.
442 * Only used on non-Windows systems. On Windows, the directory is determined
443 * by @c g_win32_get_package_installation_directory().
444 * @param package The package name, usually this is the @c GETTEXT_PACKAGE macro,
445 * defined by the build system.
450 void main_locale_init(const gchar
*locale_dir
, const gchar
*package
)
453 setlocale(LC_ALL
, "");
457 locale_dir
= utils_resource_dir(RESOURCE_DIR_LOCALE
);
459 (void) bindtextdomain(package
, locale_dir
);
460 (void) bind_textdomain_codeset(package
, "UTF-8");
464 static void print_filetypes(void)
466 const GSList
*list
, *node
;
468 filetypes_init_types();
469 printf("Geany's filetype names:\n");
471 list
= filetypes_get_sorted_by_name();
472 foreach_slist(node
, list
)
474 GeanyFiletype
*ft
= node
->data
;
476 printf("%s\n", ft
->name
);
478 filetypes_free_types();
482 static void wait_for_input_on_windows(void)
487 geany_debug("Press any key to continue");
494 static void parse_command_line_options(gint
*argc
, gchar
***argv
)
496 GError
*error
= NULL
;
497 GOptionContext
*context
;
499 CommandLineOptions def_clo
= {FALSE
, NULL
, TRUE
, -1, -1, FALSE
, FALSE
, FALSE
};
501 /* first initialise cl_options fields with default values */
502 cl_options
= def_clo
;
504 /* the GLib option parser can't handle the +NNN (line number) option,
505 * so we grab that here and replace it with a no-op */
506 for (i
= 1; i
< (*argc
); i
++)
508 if ((*argv
)[i
][0] != '+')
511 cl_options
.goto_line
= atoi((*argv
)[i
] + 1);
512 (*argv
)[i
] = (gchar
*) "--dummy";
515 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
);
525 g_printerr("Geany: %s\n", error
->message
);
530 app
->debug_mode
= verbose_mode
;
533 /* Since GLib 2.32 messages logged with levels INFO and DEBUG aren't output by the
534 * default log handler unless the G_MESSAGES_DEBUG environment variable contains the
535 * domain of the message or is set to the special value "all" */
536 g_setenv("G_MESSAGES_DEBUG", "all", FALSE
);
540 win32_init_debug_code();
545 gchar
*build_date
= utils_parse_and_format_build_date(__DATE__
);
547 printf(PACKAGE
" %s (", main_get_version_string());
548 /* note for translators: library versions are printed after this */
549 printf(_("built on %s with "), build_date
);
550 printf(geany_lib_versions
,
551 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
,
552 GLIB_MAJOR_VERSION
, GLIB_MINOR_VERSION
, GLIB_MICRO_VERSION
);
555 wait_for_input_on_windows();
561 printf("%s\n", GEANY_PREFIX
);
562 printf("%s\n", GEANY_DATADIR
);
563 printf("%s\n", GEANY_LIBDIR
);
564 printf("%s\n", GEANY_LOCALEDIR
);
565 wait_for_input_on_windows();
569 if (alternate_config
)
571 geany_debug("alternate config: %s", alternate_config
);
572 app
->configdir
= alternate_config
;
576 app
->configdir
= utils_get_user_config_dir();
583 filetypes_init_types();
584 ret
= symbols_generate_global_tags(*argc
, *argv
, ! no_preprocessing
);
585 filetypes_free_types();
586 wait_for_input_on_windows();
593 wait_for_input_on_windows();
598 socket_info
.ignore_socket
= cl_options
.new_instance
;
599 if (cl_options
.socket_filename
)
601 socket_info
.file_name
= cl_options
.socket_filename
;
606 vte_info
.lib_vte
= lib_vte
;
608 cl_options
.ignore_global_tags
= ignore_global_tags
;
610 if (! gtk_init_check(NULL
, NULL
))
611 { /* check whether we have a valid X display and exit if not */
612 g_printerr("Geany: cannot open display\n");
616 #ifdef MAC_INTEGRATION
617 /* Create GtkosxApplication singleton - should be created shortly after gtk_init() */
618 gtkosx_application_get();
623 static gint
create_config_dir(void)
625 gint saved_errno
= 0;
626 gchar
*conf_file
= NULL
;
627 gchar
*filedefs_dir
= NULL
;
628 gchar
*templates_dir
= NULL
;
630 if (! g_file_test(app
->configdir
, G_FILE_TEST_EXISTS
))
633 /* if we are *not* using an alternate config directory, we check whether the old one
634 * in ~/.geany still exists and try to move it */
635 if (alternate_config
== NULL
)
637 gchar
*old_dir
= g_build_filename(g_get_home_dir(), ".geany", NULL
);
638 /* move the old config dir if it exists */
639 if (g_file_test(old_dir
, G_FILE_TEST_EXISTS
))
641 if (! dialogs_show_question_full(main_widgets
.window
,
642 GTK_STOCK_YES
, GTK_STOCK_QUIT
, _("Move it now?"),
644 _("Geany needs to move your old configuration directory before starting.")))
647 if (! g_file_test(app
->configdir
, G_FILE_TEST_IS_DIR
))
648 utils_mkdir(app
->configdir
, TRUE
);
650 if (g_rename(old_dir
, app
->configdir
) == 0)
652 dialogs_show_msgbox(GTK_MESSAGE_INFO
,
653 _("Your configuration directory has been successfully moved from \"%s\" to \"%s\"."),
654 old_dir
, app
->configdir
);
660 dialogs_show_msgbox(GTK_MESSAGE_WARNING
,
661 /* for translators: the third %s in brackets is the error message which
662 * describes why moving the dir didn't work */
663 _("Your old configuration directory \"%s\" could not be moved to \"%s\" (%s). "
664 "Please move manually the directory to the new location."),
665 old_dir
, app
->configdir
, g_strerror(errno
));
671 geany_debug("creating config directory %s", app
->configdir
);
672 saved_errno
= utils_mkdir(app
->configdir
, TRUE
);
675 conf_file
= g_build_filename(app
->configdir
, "geany.conf", NULL
);
676 filedefs_dir
= g_build_filename(app
->configdir
, GEANY_FILEDEFS_SUBDIR
, NULL
);
677 templates_dir
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
, NULL
);
679 if (saved_errno
== 0 && ! g_file_test(conf_file
, G_FILE_TEST_EXISTS
))
680 { /* check whether geany.conf can be written */
681 saved_errno
= utils_is_file_writable(app
->configdir
);
684 /* make subdir for filetype definitions */
685 if (saved_errno
== 0)
687 gchar
*filedefs_readme
= g_build_filename(app
->configdir
,
688 GEANY_FILEDEFS_SUBDIR
, "filetypes.README", NULL
);
690 if (! g_file_test(filedefs_dir
, G_FILE_TEST_EXISTS
))
692 saved_errno
= utils_mkdir(filedefs_dir
, FALSE
);
694 if (saved_errno
== 0 && ! g_file_test(filedefs_readme
, G_FILE_TEST_EXISTS
))
696 gchar
*text
= g_strconcat(
697 "Copy files from ", app
->datadir
, " to this directory to overwrite "
698 "them. To use the defaults, just delete the file in this directory.\nFor more information read "
699 "the documentation (in ", app
->docdir
, G_DIR_SEPARATOR_S
"index.html or visit " GEANY_HOMEPAGE
").", NULL
);
700 utils_write_file(filedefs_readme
, text
);
703 g_free(filedefs_readme
);
706 /* make subdir for template files */
707 if (saved_errno
== 0)
709 gchar
*templates_readme
= g_build_filename(app
->configdir
, GEANY_TEMPLATES_SUBDIR
,
710 "templates.README", NULL
);
712 if (! g_file_test(templates_dir
, G_FILE_TEST_EXISTS
))
714 saved_errno
= utils_mkdir(templates_dir
, FALSE
);
716 if (saved_errno
== 0 && ! g_file_test(templates_readme
, G_FILE_TEST_EXISTS
))
718 gchar
*text
= g_strconcat(
719 "There are several template files in this directory. For these templates you can use wildcards.\n\
720 For more information read the documentation (in ", app
->docdir
, G_DIR_SEPARATOR_S
"index.html or visit " GEANY_HOMEPAGE
").",
722 utils_write_file(templates_readme
, text
);
725 g_free(templates_readme
);
728 g_free(filedefs_dir
);
729 g_free(templates_dir
);
736 /* Returns 0 if config dir is OK. */
737 static gint
setup_config_dir(void)
739 gint mkdir_result
= 0;
741 /* convert configdir to locale encoding to avoid troubles */
742 SETPTR(app
->configdir
, utils_get_locale_from_utf8(app
->configdir
));
744 mkdir_result
= create_config_dir();
745 if (mkdir_result
!= 0)
747 if (! dialogs_show_question(
748 _("Configuration directory could not be created (%s).\nThere could be some problems "
749 "using Geany without a configuration directory.\nStart Geany anyway?"),
750 g_strerror(mkdir_result
)))
755 /* make configdir a real path */
756 if (g_file_test(app
->configdir
, G_FILE_TEST_EXISTS
))
757 SETPTR(app
->configdir
, tm_get_real_path(app
->configdir
));
762 /* Signal handling removed since main_quit() uses functions that are
763 * illegal in signal handlers
764 static void signal_cb(gint sig)
773 /* Used for command-line arguments at startup or from socket.
774 * this will strip any :line:col filename suffix from locale_filename */
775 gboolean
main_handle_filename(const gchar
*locale_filename
)
778 gint line
= -1, column
= -1;
781 g_return_val_if_fail(locale_filename
, FALSE
);
783 /* check whether the passed filename is an URI */
784 filename
= utils_get_path_from_uri(locale_filename
);
785 if (filename
== NULL
)
788 get_line_and_column_from_filename(filename
, &line
, &column
);
790 cl_options
.goto_line
= line
;
792 cl_options
.goto_column
= column
;
794 if (g_file_test(filename
, G_FILE_TEST_IS_REGULAR
))
796 doc
= document_open_file(filename
, cl_options
.readonly
, NULL
, NULL
);
797 /* add recent file manually if opening_session_files is set */
798 if (doc
!= NULL
&& main_status
.opening_session_files
)
799 ui_add_recent_document(doc
);
803 else if (file_prefs
.cmdline_new_files
)
804 { /* create new file with the given filename */
805 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
807 doc
= document_new_file(utf8_filename
, NULL
, NULL
);
809 ui_add_recent_document(doc
);
810 g_free(utf8_filename
);
819 /* open files from command line */
820 static void open_cl_files(gint argc
, gchar
**argv
)
824 for (i
= 1; i
< argc
; i
++)
826 gchar
*filename
= main_get_argv_filename(argv
[i
]);
828 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
835 /* It seems argv elements are encoded in CP1252 on a German Windows */
836 SETPTR(filename
, g_locale_to_utf8(filename
, -1, NULL
, NULL
, NULL
));
838 if (filename
&& ! main_handle_filename(filename
))
840 const gchar
*msg
= _("Could not find file '%s'.");
842 g_printerr(msg
, filename
); /* also print to the terminal */
844 ui_set_statusbar(TRUE
, msg
, filename
);
851 static void load_session_project_file(void)
853 gchar
*locale_filename
;
855 g_return_if_fail(project_prefs
.session_file
!= NULL
);
857 locale_filename
= utils_get_locale_from_utf8(project_prefs
.session_file
);
859 if (G_LIKELY(!EMPTY(locale_filename
)))
860 project_load_file(locale_filename
);
862 g_free(locale_filename
);
863 g_free(project_prefs
.session_file
); /* no longer needed */
867 static void load_settings(void)
869 configuration_load();
870 /* let cmdline options overwrite configuration settings */
872 vte_info
.have_vte
= (no_vte
) ? FALSE
: vte_info
.load_vte
;
875 ui_prefs
.msgwindow_visible
= FALSE
;
878 want_plugins
= prefs
.load_plugins
&& !no_plugins
;
883 void main_load_project_from_command_line(const gchar
*locale_filename
, gboolean use_session
)
887 pfile
= utils_get_path_from_uri(locale_filename
);
891 project_load_file_with_session(pfile
);
893 project_load_file(pfile
);
899 static void load_startup_files(gint argc
, gchar
**argv
)
901 gboolean load_session
= FALSE
;
903 if (argc
> 1 && g_str_has_suffix(argv
[1], ".geany"))
905 gchar
*filename
= main_get_argv_filename(argv
[1]);
907 /* project file specified: load it, but decide the session later */
908 main_load_project_from_command_line(filename
, FALSE
);
910 /* force session load if using project-based session files */
911 load_session
= project_prefs
.project_session
;
915 /* Load the default session if:
916 * 1. "Load files from the last session" is active.
917 * 2. --no-session is not specified.
918 * 3. We are a primary instance.
919 * Has no effect if a CL project is loaded and using project-based session files. */
920 if (prefs
.load_session
&& cl_options
.load_session
&& !cl_options
.new_instance
)
922 if (app
->project
== NULL
)
923 load_session_project_file();
929 /* load session files into tabs, as they are found in the session_files variable */
930 configuration_open_files();
932 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
)) == 0)
934 ui_update_popup_copy_items(NULL
);
935 ui_update_popup_reundo_items(NULL
);
939 open_cl_files(argc
, argv
);
943 static gboolean
send_startup_complete(gpointer data
)
945 g_signal_emit_by_name(geany_object
, "geany-startup-complete");
950 static const gchar
*get_locale(void)
952 const gchar
*locale
= "unknown";
954 locale
= setlocale(LC_CTYPE
, NULL
);
960 #if ! GTK_CHECK_VERSION(3, 0, 0)
961 /* This prepends our own gtkrc file to the list of RC files to be loaded by GTK at startup.
962 * This function *has* to be called before gtk_init().
964 * We have a custom RC file defining various styles we need, and we want the user to be
965 * able to override them (e.g. if they want -- or need -- other colors). Fair enough, one
966 * would simply call gtk_rc_parse() with the appropriate filename. However, the styling
967 * rules applies in the order they are loaded, then if we load our styles after GTK has
968 * loaded the user's ones we'd override them.
970 * There are 2 solutions to fix this:
971 * 1) set our styles' priority to something with lower than "user" (actually "theme"
972 * priority because rules precedence are first calculated depending on the priority
973 * no matter of how precise the rules is, so we need to override the theme).
974 * 2) prepend our custom style to GTK's list while keeping priority to user (which is the
975 * default), so it gets loaded before real user's ones and so gets overridden by them.
977 * One would normally go for 1 because it's ways simpler and requires less code: you just
978 * have to add the priorities to your styles, which is a matter of adding a few ":theme" in
979 * the RC file. However, KDE being a bitch it doesn't set the gtk-theme-name but rather
980 * directly includes the style to use in a user gtkrc file, which makes the theme have
981 * "user" priority, hence overriding our styles. So, we cannot set priorities in the RC
982 * file if we want to support running under KDE, which pretty much leave us with no choice
983 * but to go with solution 2, which unfortunately requires writing ugly code since GTK
984 * don't have a gtk_rc_prepend_default_file() function. Thank you very much KDE.
986 * Though, as a side benefit it also makes the code work with people using gtk-chtheme,
987 * which also found it funny to include the theme in the user RC file. */
988 static void setup_gtk2_styles(void)
990 gchar
**gtk_files
= gtk_rc_get_default_files();
991 gchar
**new_files
= g_malloc(sizeof *new_files
* (g_strv_length(gtk_files
) + 2));
994 new_files
[i
++] = g_build_filename(app
->datadir
, "geany.gtkrc", NULL
);
995 for (; *gtk_files
; gtk_files
++)
996 new_files
[i
++] = g_strdup(*gtk_files
);
999 gtk_rc_set_default_files(new_files
);
1001 g_strfreev(new_files
);
1007 gint
main_lib(gint argc
, gchar
**argv
)
1010 gint config_dir_result
;
1011 const gchar
*locale
;
1013 #if ! GLIB_CHECK_VERSION(2, 36, 0)
1017 log_handlers_init();
1019 app
= g_new0(GeanyApp
, 1);
1020 memset(&main_status
, 0, sizeof(GeanyStatus
));
1021 memset(&prefs
, 0, sizeof(GeanyPrefs
));
1022 memset(&interface_prefs
, 0, sizeof(GeanyInterfacePrefs
));
1023 memset(&toolbar_prefs
, 0, sizeof(GeanyToolbarPrefs
));
1024 memset(&file_prefs
, 0, sizeof(GeanyFilePrefs
));
1025 memset(&search_prefs
, 0, sizeof(GeanySearchPrefs
));
1026 memset(&tool_prefs
, 0, sizeof(GeanyToolPrefs
));
1027 memset(&template_prefs
, 0, sizeof(GeanyTemplatePrefs
));
1028 memset(&ui_prefs
, 0, sizeof(UIPrefs
));
1029 memset(&ui_widgets
, 0, sizeof(UIWidgets
));
1032 #if ! GTK_CHECK_VERSION(3, 0, 0)
1033 setup_gtk2_styles();
1036 main_locale_init(utils_resource_dir(RESOURCE_DIR_LOCALE
), GETTEXT_PACKAGE
);
1038 parse_command_line_options(&argc
, &argv
);
1040 #if ! GLIB_CHECK_VERSION(2, 32, 0)
1041 /* Initialize GLib's thread system in case any plugins want to use it or their
1042 * dependencies (e.g. WebKit, Soup, ...). Deprecated since GLIB 2.32. */
1043 if (!g_thread_supported())
1044 g_thread_init(NULL
);
1047 /* removed as signal handling was wrong, see signal_cb()
1048 signal(SIGTERM, signal_cb); */
1051 /* ignore SIGPIPE signal for preventing sudden death of program */
1052 signal(SIGPIPE
, SIG_IGN
);
1055 config_dir_result
= setup_config_dir();
1057 /* check and create (unix domain) socket for remote operation */
1058 if (! socket_info
.ignore_socket
)
1060 socket_info
.lock_socket
= -1;
1061 socket_info
.lock_socket_tag
= 0;
1062 socket_info
.lock_socket
= socket_init(argc
, argv
);
1063 /* Quit if filenames were sent to first instance or the list of open
1064 * documents has been printed */
1065 if ((socket_info
.lock_socket
== -2 /* socket exists */ && argc
> 1) ||
1066 cl_options
.list_documents
)
1069 gdk_notify_startup_complete();
1070 g_free(app
->configdir
);
1071 g_free(app
->datadir
);
1072 g_free(app
->docdir
);
1076 /* Start a new instance if no command line strings were passed,
1077 * even if the socket already exists */
1078 else if (socket_info
.lock_socket
== -2 /* socket already exists */)
1080 socket_info
.ignore_socket
= TRUE
;
1081 cl_options
.new_instance
= TRUE
;
1087 /* after we initialized the socket code and handled command line args,
1088 * let's change the working directory on Windows to not lock it */
1089 change_working_directory_on_windows();
1092 locale
= get_locale();
1093 geany_debug("Geany %s, %s",
1094 main_get_version_string(),
1096 geany_debug(geany_lib_versions
,
1097 gtk_major_version
, gtk_minor_version
, gtk_micro_version
,
1098 glib_major_version
, glib_minor_version
, glib_micro_version
);
1099 geany_debug("System data dir: %s", app
->datadir
);
1100 geany_debug("User config dir: %s", app
->configdir
);
1102 /* create the object so Geany signals can be connected in init() functions */
1103 geany_object
= geany_object_new();
1111 /* init stash groups before loading keyfile */
1112 configuration_init();
1120 load_settings(); /* load keyfile */
1124 ui_create_insert_menu_items();
1125 ui_create_insert_date_menu_items();
1131 document_init_doclist();
1133 editor_snippets_init();
1138 ui_create_recent_menus();
1140 ui_set_statusbar(TRUE
, _("This is Geany %s."), main_get_version_string());
1141 if (config_dir_result
!= 0)
1142 ui_set_statusbar(TRUE
, _("Configuration directory could not be created (%s)."),
1143 g_strerror(config_dir_result
));
1145 /* apply all configuration options */
1149 /* load any enabled plugins before we open any documents */
1151 plugins_load_active();
1154 ui_sidebar_show_hide();
1156 /* set the active sidebar page after plugins have been loaded */
1157 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), ui_prefs
.sidebar_page
);
1159 /* load keybinding settings after plugins have added their groups */
1160 keybindings_load_keyfile();
1162 /* create the custom command menu after the keybindings have been loaded to have the proper
1163 * accelerator shown for the menu items */
1164 tools_create_insert_custom_command_menu_items();
1166 /* load any command line files or session files */
1167 main_status
.opening_session_files
= TRUE
;
1168 load_startup_files(argc
, argv
);
1169 main_status
.opening_session_files
= FALSE
;
1171 /* open a new file if no other file was opened */
1172 document_new_file_if_non_open();
1174 ui_document_buttons_update();
1175 ui_save_buttons_toggle(FALSE
);
1177 doc
= document_get_current();
1178 sidebar_select_openfiles_item(doc
);
1179 build_menu_update(doc
);
1180 sidebar_update_tag_list(doc
, FALSE
);
1183 /* Manually realise the main window to be able to set the position but don't show it.
1184 * We don't set the position after showing the window to avoid flickering. */
1185 gtk_widget_realize(main_widgets
.window
);
1187 setup_window_position();
1189 /* finally show the window */
1190 document_grab_focus(doc
);
1191 gtk_widget_show(main_widgets
.window
);
1192 main_status
.main_window_realized
= TRUE
;
1194 configuration_apply_settings();
1197 /* register the callback of socket input */
1198 if (! socket_info
.ignore_socket
&& socket_info
.lock_socket
> 0)
1200 socket_info
.read_ioc
= g_io_channel_unix_new(socket_info
.lock_socket
);
1201 socket_info
.lock_socket_tag
= g_io_add_watch(socket_info
.read_ioc
,
1202 G_IO_IN
| G_IO_PRI
| G_IO_ERR
, socket_lock_input_cb
, main_widgets
.window
);
1206 /* when we are really done with setting everything up and the main event loop is running,
1207 * tell other components, mainly plugins, that startup is complete */
1208 g_idle_add_full(G_PRIORITY_LOW
, send_startup_complete
, NULL
, NULL
);
1210 #ifdef MAC_INTEGRATION
1211 /* OS X application ready - has to be called before entering main loop */
1212 gtkosx_application_ready(gtkosx_application_get());
1220 static void queue_free(GQueue
*queue
)
1222 while (! g_queue_is_empty(queue
))
1224 g_free(g_queue_pop_tail(queue
));
1226 g_queue_free(queue
);
1230 static void do_main_quit(void)
1232 geany_debug("Quitting...");
1234 configuration_save();
1236 if (app
->project
!= NULL
)
1237 project_close(FALSE
); /* save project session files */
1239 document_close_all();
1241 main_status
.quitting
= TRUE
;
1254 highlighting_free_styles();
1255 templates_free_templates();
1259 document_finalize();
1263 editor_snippets_free();
1264 encodings_finalize();
1267 configuration_finalize();
1268 filetypes_free_types();
1271 tm_workspace_free();
1272 g_free(app
->configdir
);
1273 g_free(app
->datadir
);
1274 g_free(app
->docdir
);
1275 g_free(prefs
.default_open_path
);
1276 g_free(prefs
.custom_plugin_path
);
1277 g_free(ui_prefs
.custom_date_format
);
1278 g_free(interface_prefs
.editor_font
);
1279 g_free(interface_prefs
.tagbar_font
);
1280 g_free(interface_prefs
.msgwin_font
);
1281 g_free(editor_prefs
.long_line_color
);
1282 g_free(editor_prefs
.comment_toggle_mark
);
1283 g_free(editor_prefs
.color_scheme
);
1284 g_free(tool_prefs
.context_action_cmd
);
1285 g_free(template_prefs
.developer
);
1286 g_free(template_prefs
.company
);
1287 g_free(template_prefs
.mail
);
1288 g_free(template_prefs
.initials
);
1289 g_free(template_prefs
.version
);
1290 g_free(tool_prefs
.term_cmd
);
1291 g_free(tool_prefs
.browser_cmd
);
1292 g_free(tool_prefs
.grep_cmd
);
1293 g_free(printing_prefs
.external_print_cmd
);
1294 g_free(printing_prefs
.page_header_datefmt
);
1295 g_strfreev(ui_prefs
.custom_commands
);
1296 g_strfreev(ui_prefs
.custom_commands_labels
);
1298 queue_free(ui_prefs
.recent_queue
);
1299 queue_free(ui_prefs
.recent_projects_queue
);
1301 if (ui_widgets
.prefs_dialog
&& GTK_IS_WIDGET(ui_widgets
.prefs_dialog
)) gtk_widget_destroy(ui_widgets
.prefs_dialog
);
1302 if (ui_widgets
.open_fontsel
&& GTK_IS_WIDGET(ui_widgets
.open_fontsel
)) gtk_widget_destroy(ui_widgets
.open_fontsel
);
1303 if (ui_widgets
.open_colorsel
&& GTK_IS_WIDGET(ui_widgets
.open_colorsel
)) gtk_widget_destroy(ui_widgets
.open_colorsel
);
1305 if (vte_info
.have_vte
) vte_close();
1306 g_free(vte_info
.lib_vte
);
1307 g_free(vte_info
.dir
);
1309 gtk_widget_destroy(main_widgets
.window
);
1311 /* destroy popup menus */
1312 if (main_widgets
.editor_menu
&& GTK_IS_WIDGET(main_widgets
.editor_menu
))
1313 gtk_widget_destroy(main_widgets
.editor_menu
);
1314 if (ui_widgets
.toolbar_menu
&& GTK_IS_WIDGET(ui_widgets
.toolbar_menu
))
1315 gtk_widget_destroy(ui_widgets
.toolbar_menu
);
1316 if (msgwindow
.popup_status_menu
&& GTK_IS_WIDGET(msgwindow
.popup_status_menu
))
1317 gtk_widget_destroy(msgwindow
.popup_status_menu
);
1318 if (msgwindow
.popup_msg_menu
&& GTK_IS_WIDGET(msgwindow
.popup_msg_menu
))
1319 gtk_widget_destroy(msgwindow
.popup_msg_menu
);
1320 if (msgwindow
.popup_compiler_menu
&& GTK_IS_WIDGET(msgwindow
.popup_compiler_menu
))
1321 gtk_widget_destroy(msgwindow
.popup_compiler_menu
);
1323 g_object_unref(geany_object
);
1324 geany_object
= NULL
;
1326 g_free(original_cwd
);
1329 ui_finalize_builder();
1335 static gboolean
check_no_unsaved(void)
1339 for (i
= 0; i
< documents_array
->len
; i
++)
1341 if (documents
[i
]->is_valid
&& documents
[i
]->changed
)
1346 return TRUE
; /* no unsaved edits */
1350 /* Returns false when quitting is aborted due to user cancellation */
1351 gboolean
main_quit(void)
1353 main_status
.quitting
= TRUE
;
1355 if (! check_no_unsaved())
1357 if (document_account_for_unsaved())
1364 if (! prefs
.confirm_exit
||
1365 dialogs_show_question_full(NULL
, GTK_STOCK_QUIT
, GTK_STOCK_CANCEL
, NULL
,
1366 _("Do you really want to quit?")))
1372 main_status
.quitting
= FALSE
;
1377 * Reloads most of Geany's configuration files without restarting. Currently the following
1378 * files are reloaded: all template files, also new file templates and the 'New (with template)'
1379 * menus will be updated, Snippets (snippets.conf), filetype extensions (filetype_extensions.conf),
1380 * and 'settings' and 'build_settings' sections of the filetype definition files.
1382 * Plugins may call this function if they changed any of these files (e.g. a configuration file
1388 void main_reload_configuration(void)
1390 /* reload templates */
1391 templates_free_templates();
1394 /* reload snippets */
1395 editor_snippets_free();
1396 editor_snippets_init();
1398 filetypes_reload_extensions();
1401 /* C tag names to ignore */
1402 symbols_reload_config_files();
1404 ui_set_statusbar(TRUE
, _("Configuration files reloaded."));