[gedit/wip/gtkapp-actions: 5/10] Handle basic command line options in main()
- From: SÃbastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/wip/gtkapp-actions: 5/10] Handle basic command line options in main()
- Date: Fri, 3 Aug 2012 08:04:36 +0000 (UTC)
commit f2d200f0dec587002c0537c467ff35443848b600
Author: SÃbastien Wilmet <swilmet gnome org>
Date: Fri Aug 3 07:35:22 2012 +0200
Handle basic command line options in main()
The i18n must be setup in the main() too.
setlocale (LC_ALL, ""); is not needed, GTK+ does that for us.
gedit/gedit-app.c | 11 ----
gedit/gedit.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 159 insertions(+), 11 deletions(-)
---
diff --git a/gedit/gedit-app.c b/gedit/gedit-app.c
index 778de0f..491070b 100644
--- a/gedit/gedit-app.c
+++ b/gedit/gedit-app.c
@@ -306,17 +306,6 @@ gedit_app_startup (GApplication *application)
gedit_debug_init ();
gedit_debug_message (DEBUG_APP, "Startup");
- /* Setup locale/gettext */
- setlocale (LC_ALL, "");
-
- dir = gedit_dirs_get_gedit_locale_dir ();
- bindtextdomain (GETTEXT_PACKAGE, dir);
-
- bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
- textdomain (GETTEXT_PACKAGE);
-
- gedit_dirs_init ();
-
gedit_debug_message (DEBUG_APP, "Set icon");
dir = gedit_dirs_get_gedit_data_dir ();
diff --git a/gedit/gedit.c b/gedit/gedit.c
index ac07a65..87add02 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -32,9 +32,119 @@
#include <config.h>
#endif
+#include <stdlib.h>
#include <glib.h>
+#include <glib/gi18n.h>
+
+#ifdef ENABLE_INTROSPECTION
+#include <girepository.h>
+#endif
#include "gedit-app.h"
+#include "gedit-dirs.h"
+
+static gboolean version = FALSE;
+static gboolean list_encodings = FALSE;
+static gchar *encoding_charset = NULL;
+static gboolean new_window = FALSE;
+static gboolean new_document = FALSE;
+static gchar *geometry = NULL;
+static gboolean wait = FALSE;
+static gboolean background = FALSE;
+static gboolean standalone = FALSE;
+static gchar **remaining_args = NULL;
+
+static const GOptionEntry options[] =
+{
+ /* Version */
+ {
+ "version", 'V', 0, G_OPTION_ARG_NONE, &version,
+ N_("Show the application's version"), NULL
+ },
+
+ /* List available encodings */
+ {
+ "list-encodings", '\0', 0, G_OPTION_ARG_NONE, &list_encodings,
+ N_("Display list of possible values for the encoding option"),
+ NULL
+ },
+
+ /* Encoding */
+ {
+ "encoding", '\0', 0, G_OPTION_ARG_STRING,
+ &encoding_charset,
+ N_("Set the character encoding to be used to open the files listed on the command line"),
+ N_("ENCODING")
+ },
+
+ /* Open a new window */
+ {
+ "new-window", '\0', 0, G_OPTION_ARG_NONE,
+ &new_window,
+ N_("Create a new top-level window in an existing instance of gedit"),
+ NULL
+ },
+
+ /* Create a new empty document */
+ {
+ "new-document", '\0', 0, G_OPTION_ARG_NONE,
+ &new_document,
+ N_("Create a new document in an existing instance of gedit"),
+ NULL
+ },
+
+ /* Window geometry */
+ {
+ "geometry", 'g', 0, G_OPTION_ARG_STRING,
+ &geometry,
+ N_("Set the size and position of the window (WIDTHxHEIGHT+X+Y)"),
+ N_("GEOMETRY")
+ },
+
+ /* Wait for closing documents */
+ /* TODO implement it */
+ {
+ "wait", 'w', 0, G_OPTION_ARG_NONE,
+ &wait,
+ N_("Open files and block process until files are closed"),
+ NULL
+ },
+
+ /* Run in the background */
+ /* TODO implement it */
+ {
+ "background", 'b', 0, G_OPTION_ARG_NONE,
+ &background,
+ N_("Run gedit in the background"),
+ NULL
+ },
+
+ /* New instance */
+ {
+ "standalone", 's', 0, G_OPTION_ARG_NONE,
+ &standalone,
+ N_("Run gedit in standalone mode"),
+ NULL
+ },
+
+ /* collects file arguments */
+ {
+ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY,
+ &remaining_args,
+ NULL,
+ N_("[FILE...] [+LINE[:COLUMN]]")
+ },
+
+ {NULL}
+};
+
+static void
+setup_i18n (void)
+{
+ bindtextdomain (GETTEXT_PACKAGE, gedit_dirs_get_gedit_locale_dir ());
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+ textdomain (GETTEXT_PACKAGE);
+}
static void
get_line_column_position (const gchar *arg,
@@ -61,6 +171,50 @@ get_line_column_position (const gchar *arg,
g_strfreev (split);
}
+static void
+parse_command_line (int argc, char **argv)
+{
+ GOptionContext *context;
+ GError *error = NULL;
+
+ context = g_option_context_new (_("- Edit text files"));
+ g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
+ g_option_context_add_group (context, gtk_get_option_group (TRUE));
+
+#ifdef ENABLE_INTROSPECTION
+ g_option_context_add_group (context, g_irepository_get_option_group ());
+#endif
+
+ if (!g_option_context_parse (context, &argc, &argv, &error))
+ {
+ g_error (_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
+ error->message, argv[0]);
+
+ exit (EXIT_FAILURE);
+ }
+
+ if (version)
+ {
+ g_print ("%s - Version %s\n", g_get_application_name (), VERSION);
+ exit (EXIT_SUCCESS);
+ }
+
+ if (list_encodings)
+ {
+ gint i = 0;
+ const GeditEncoding *enc;
+
+ while ((enc = gedit_encoding_get_from_index (i)) != NULL)
+ {
+ g_print ("%s\n", gedit_encoding_get_charset (enc));
+
+ ++i;
+ }
+
+ exit (EXIT_SUCCESS);
+ }
+}
+
int
main (int argc, char *argv[])
{
@@ -69,6 +223,11 @@ main (int argc, char *argv[])
g_type_init ();
+ gedit_dirs_init ();
+ setup_i18n ();
+
+ parse_command_line (argc, argv);
+
app = gedit_app_get_default ();
status = g_application_run (G_APPLICATION (app), argc, argv);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]