medit/medit/medit-app.opag

328 lines
7.7 KiB
Plaintext

/*
* medit-app.c
*
* Copyright (C) 2004-2007 by Yevgen Muntyan <muntyan@math.tamu.edu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* See COPYING file that comes with this distribution.
*/
#line 15 "../../../medit/medit-app.opag"
#include "medit-credits.h"
#include <moo.h>
#include <mooutils/mooi18n.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mem-debug.h"
#if GTK_CHECK_VERSION(2,8,0) && defined(GDK_WINDOWING_X11)
#include <gdk/gdkx.h>
#define TIMESTAMP() (gdk_x11_display_get_user_time (gdk_display_get_default ()))
#else
#define TIMESTAMP() (0)
#endif
typedef enum {
MODE_SIMPLE,
MODE_PROJECT
} AppMode;
int _medit_parse_options (const char *const program_name,
const int argc,
char **const argv);
/********************************************************
* command line parsing code generated by Opag
* http://www.zero-based.org/software/opag/
*/
#line 122 "medit-app.c"
%%
n new-app "Run new instance of application"
s use-session "Load and save session"
pid "=PID Use existing instance with process id PID" reqarg
m mode "=[simple|project] Use specified mode" reqarg
p project "=PROJECT Open project file PROJECT" reqarg
l line "=LINE Open file and position cursor on line LINE" reqarg
log "[=FILE] Show debug output or write it to FILE" optarg
debug "Run in debug mode"
exec "=CODE Execute python code in an existing instance" reqarg
exec-file "=FILE Execute python file in an existing instance" reqarg
version "Display version information and exit" return
h help "Display this help text and exit" return
%%
#line 63 "../../../medit/medit-app.opag"
#undef STR_HELP
#define STR_HELP \
STR_HELP_NEW_APP \
STR_HELP_PID \
STR_HELP_LINE \
STR_HELP_LOG \
STR_HELP_DEBUG \
STR_HELP_VERSION \
STR_HELP_HELP
/* end of generated code
********************************************************/
static void
usage (void)
{
g_print ("Usage: %s [OPTIONS] [FILES]\n", g_get_prgname ());
g_print ("Options:\n%s", STR_HELP);
}
static void
version (void)
{
g_print ("medit %s\n", MOO_VERSION);
}
static void
check_args (int opt_remain)
{
if (_medit_opt_help)
{
usage ();
exit (0);
}
if (_medit_opt_version)
{
version ();
exit (0);
}
if (opt_remain < 0)
{
usage ();
exit (1);
}
if (_medit_opt_pid)
{
if (_medit_opt_new_app)
{
g_print ("--new-app can't be used together with --pid\n");
exit (1);
}
if (!_medit_arg_pid || !_medit_arg_pid[0])
{
usage ();
exit (1);
}
}
}
static void
push_appdir_to_path (void)
{
#ifdef __WIN32__
char *appdir;
const char *path;
char *new_path;
appdir = moo_win32_get_app_dir ();
g_return_if_fail (appdir != NULL);
path = g_getenv ("Path");
if (path)
new_path = g_strdup_printf ("%s;%s", appdir, path);
else
new_path = g_strdup (appdir);
g_setenv ("Path", new_path, TRUE);
g_free (new_path);
g_free (appdir);
#endif
}
static void
project_mode (void)
{
MooPlugin *plugin;
plugin = moo_plugin_lookup ("ProjectManager");
if (!plugin)
{
g_printerr ("Could not initialize project manager plugin\n");
exit (1);
}
if (_medit_arg_project && *_medit_arg_project)
{
char *project = moo_filename_from_locale (_medit_arg_project);
g_object_set (plugin, "project", _medit_arg_project, NULL);
g_free (project);
}
moo_plugin_set_enabled (plugin, TRUE);
}
int
main (int argc, char *argv[])
{
MooApp *app;
int opt_remain;
MooEditor *editor;
char **files;
int retval;
gboolean new_instance = FALSE;
gboolean run_input = TRUE;
AppMode mode = MODE_SIMPLE;
guint32 stamp;
guint32 line = 0;
const char *pid_string = NULL;
gboolean use_session = FALSE;
init_mem_stuff ();
g_thread_init (NULL);
gdk_threads_init ();
gdk_threads_enter ();
gtk_init (&argc, &argv);
stamp = TIMESTAMP ();
#if 0
gdk_window_set_debug_updates (TRUE);
#endif
opt_remain = _medit_parse_options (g_get_prgname (), argc, argv);
check_args (opt_remain);
if (_medit_opt_debug)
g_setenv ("MOO_DEBUG", "yes", FALSE);
if (_medit_opt_log)
{
if (_medit_arg_log)
moo_set_log_func_file (_medit_arg_log);
else
moo_set_log_func_window (TRUE);
}
push_appdir_to_path ();
if (_medit_opt_mode)
{
if (!strcmp (_medit_arg_mode, "simple"))
mode = MODE_SIMPLE;
else
mode = MODE_PROJECT;
}
if (_medit_opt_project)
mode = MODE_PROJECT;
if (_medit_opt_new_app || mode == MODE_PROJECT)
new_instance = TRUE;
run_input = !_medit_opt_new_app || _medit_opt_use_session || _medit_opt_project;
use_session = !_medit_opt_new_app || _medit_opt_use_session;
app = g_object_new (MOO_TYPE_APP,
"argv", argv,
"run-input", run_input,
"short-name", "medit",
"full-name", "medit",
"description", _("medit is a text editor"),
"website", "http://mooedit.sourceforge.net/",
"website-label", "http://mooedit.sourceforge.net/",
"quit-on-editor-close", TRUE,
"logo", "medit",
"credits", THANKS,
NULL);
if (_medit_arg_line)
line = strtol (_medit_arg_line, NULL, 10);
if (_medit_opt_pid)
pid_string = _medit_arg_pid;
else if (!_medit_opt_new_app)
pid_string = g_getenv ("MEDIT_PID");
if (_medit_opt_exec || _medit_opt_exec_file)
{
GString *msg;
msg = g_string_new (_medit_opt_exec ? "p" : "P");
g_string_append (msg, _medit_opt_exec ? _medit_arg_exec : _medit_arg_exec_file);
moo_app_send_msg (app, pid_string, msg->str, msg->len + 1);
exit (0);
}
files = moo_filenames_from_locale (argv + opt_remain);
if (pid_string)
{
if (moo_app_send_files (app, files, line, stamp, pid_string))
exit (0);
g_print ("Could not send files to pid %s\n", pid_string);
exit (1);
}
if ((!new_instance && moo_app_send_files (app, files, line, stamp, NULL)) ||
!moo_app_init (app))
{
gdk_notify_startup_complete ();
g_strfreev (files);
g_object_unref (app);
return 0;
}
if (mode == MODE_PROJECT)
project_mode ();
else if (use_session)
moo_app_load_session (app);
editor = moo_app_get_editor (app);
if (!moo_editor_get_active_window (editor))
moo_editor_new_window (editor);
if (files && *files)
moo_app_open_files (app, files, line, stamp);
g_strfreev (files);
retval = moo_app_run (app);
g_object_unref (app);
gdk_threads_leave ();
return retval;
}
#if defined(__WIN32__) && !defined(__GNUC__)
#include <windows.h>
int __stdcall
WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
char *lpszCmdLine,
int nCmdShow)
{
return main (__argc, __argv);
}
#endif