]> git.lyx.org Git - lyx.git/blob - src/lyx_main.C
Use the dispatch_result wrapper class DispatchResult to remove
[lyx.git] / src / lyx_main.C
1 /**
2  * \file lyx_main.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16 #include <version.h>
17
18 #include "lyx_main.h"
19
20 #include "buffer.h"
21 #include "buffer_funcs.h"
22 #include "bufferlist.h"
23 #include "converter.h"
24 #include "debug.h"
25 #include "encoding.h"
26 #include "errorlist.h"
27 #include "format.h"
28 #include "gettext.h"
29 #include "kbmap.h"
30 #include "language.h"
31 #include "lastfiles.h"
32 #include "LColor.h"
33 #include "lyxfunc.h"
34 #include "lyxlex.h"
35 #include "lyxrc.h"
36 #include "lyxtextclasslist.h"
37 #include "lyxserver.h"
38 #include "MenuBackend.h"
39 #include "ToolbarBackend.h"
40
41 #include "frontends/Alert.h"
42 #include "frontends/lyx_gui.h"
43
44 #include "support/FileInfo.h"
45 #include "support/filetools.h"
46 #include "support/lyxlib.h"
47 #include "support/os.h"
48 #include "support/path.h"
49 #include "support/path_defines.h"
50
51 #include <boost/bind.hpp>
52
53 #include <iostream>
54 #include <csignal>
55
56 using lyx::support::AddName;
57 using lyx::support::AddPath;
58 using lyx::support::bformat;
59 using lyx::support::createDirectory;
60 using lyx::support::CreateLyXTmpDir;
61 using lyx::support::FileInfo;
62 using lyx::support::FileSearch;
63 using lyx::support::GetEnv;
64 using lyx::support::GetEnvPath;
65 using lyx::support::i18nLibFileSearch;
66 using lyx::support::LibFileSearch;
67 using lyx::support::Path;
68 using lyx::support::rtrim;
69 using lyx::support::setLyxPaths;
70 using lyx::support::system_lyxdir;
71 using lyx::support::user_lyxdir;
72
73 namespace os = lyx::support::os;
74
75 using std::endl;
76
77 using std::vector;
78
79 #ifndef CXX_GLOBAL_CSTD
80 using std::exit;
81 using std::signal;
82 using std::system;
83 #endif
84
85
86 extern void QuitLyX();
87
88 extern LyXServer * lyxserver;
89
90 DebugStream lyxerr;
91
92 boost::scoped_ptr<LastFiles> lastfiles;
93
94 // This is the global bufferlist object
95 BufferList bufferlist;
96
97 // convenient to have it here.
98 boost::scoped_ptr<kb_keymap> toplevel_keymap;
99
100 namespace {
101
102 void showFileError(string const & error)
103 {
104         Alert::warning(_("Could not read configuration file"),
105                    bformat(_("Error while reading the configuration file\n%1$s.\n"
106                      "Please check your installation."), error));
107         exit(EXIT_FAILURE);
108 }
109
110 }
111
112 LyX::LyX(int & argc, char * argv[])
113 {
114         // Here we need to parse the command line. At least
115         // we need to parse for "-dbg" and "-help"
116         bool const want_gui = easyParse(argc, argv);
117
118         // set the DisplayTranslator only once; should that be done here??
119         // if this should not be in this file, please also remove
120         // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
121         lyx::graphics::setDisplayTranslator();
122
123         if (want_gui)
124                 lyx_gui::parse_init(argc, argv);
125
126         // check for any spurious extra arguments
127         // other than documents
128         for (int argi = 1; argi < argc ; ++argi) {
129                 if (argv[argi][0] == '-') {
130                         lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
131                                 argv[argi]) << endl;
132                         exit(1);
133                 }
134         }
135
136         // Initialization of LyX (reads lyxrc and more)
137         lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
138         init(want_gui);
139         lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
140
141         if (want_gui)
142                 lyx_gui::parse_lyxrc();
143
144         vector<string> files;
145
146         for (int argi = argc - 1; argi >= 1; --argi)
147                 files.push_back(argv[argi]);
148
149         if (first_start)
150                 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
151
152         // Execute batch commands if available
153         if (!batch_command.empty()) {
154
155                 lyxerr[Debug::INIT] << "About to handle -x '"
156                        << batch_command << '\'' << endl;
157
158                 Buffer * last_loaded = 0;
159
160                 vector<string>::const_iterator it = files.begin();
161                 vector<string>::const_iterator end = files.end();
162
163                 for (; it != end; ++it) {
164                         // get absolute path of file and add ".lyx" to
165                         // the filename if necessary
166                         string s = FileSearch(string(), *it, "lyx");
167                         if (s.empty()) {
168                                 last_loaded = newFile(*it, string(), true);
169                         } else {
170                                 Buffer * buf = bufferlist.newBuffer(s, false);
171                                 buf->error.connect(boost::bind(&LyX::printError, this, _1));
172                                 if (loadLyXFile(buf, s))
173                                         last_loaded = buf;
174                                 else
175                                         bufferlist.release(buf);
176                         }
177                 }
178
179                 // try to dispatch to last loaded buffer first
180                 if (last_loaded) {
181                         bool success = false;
182                         if (last_loaded->dispatch(batch_command, &success)) {
183                                 QuitLyX();
184                                 exit(!success);
185                         }
186                 }
187                 files.clear(); // the files are already loaded
188         }
189
190         lyx_gui::start(batch_command, files);
191 }
192
193
194 extern "C" {
195
196 static void error_handler(int err_sig)
197 {
198         switch (err_sig) {
199         case SIGHUP:
200                 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
201                 break;
202         case SIGINT:
203                 // no comments
204                 break;
205         case SIGFPE:
206                 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
207                 break;
208         case SIGSEGV:
209                 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
210                 lyxerr <<
211                         "Sorry, you have found a bug in LyX. "
212                         "Please read the bug-reporting instructions "
213                         "in Help->Introduction and send us a bug report, "
214                         "if necessary. Thanks !" << endl;
215                 break;
216         case SIGTERM:
217                 // no comments
218                 break;
219         }
220
221         // Deinstall the signal handlers
222         signal(SIGHUP, SIG_DFL);
223         signal(SIGINT, SIG_DFL);
224         signal(SIGFPE, SIG_DFL);
225         signal(SIGSEGV, SIG_DFL);
226         signal(SIGTERM, SIG_DFL);
227
228         LyX::emergencyCleanup();
229
230         lyxerr << "Bye." << endl;
231         if (err_sig!= SIGHUP &&
232            (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
233                 lyx::support::abort();
234         exit(0);
235 }
236
237 }
238
239
240 void LyX::printError(ErrorItem const & ei)
241 {
242         std::cerr << _("LyX: ") << ei.error
243                   << ':' << ei.description << std::endl;
244
245 }
246
247
248 void LyX::init(bool gui)
249 {
250         signal(SIGHUP, error_handler);
251         signal(SIGFPE, error_handler);
252         signal(SIGSEGV, error_handler);
253         signal(SIGINT, error_handler);
254         signal(SIGTERM, error_handler);
255
256         bool const explicit_userdir = setLyxPaths();
257
258         // Check that user LyX directory is ok. We don't do that if
259         // running in batch mode.
260         if (gui) {
261                 queryUserLyXDir(explicit_userdir);
262         } else {
263                 first_start = false;
264         }
265
266         // Disable gui when easyparse says so
267         lyx_gui::use_gui = gui;
268
269         if (lyxrc.template_path.empty()) {
270                 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
271         }
272
273         if (lyxrc.lastfiles.empty()) {
274                 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
275         }
276
277         if (lyxrc.roman_font_name.empty())
278                 lyxrc.roman_font_name = lyx_gui::roman_font_name();
279         if (lyxrc.sans_font_name.empty())
280                 lyxrc.sans_font_name = lyx_gui::sans_font_name();
281         if (lyxrc.typewriter_font_name.empty())
282                 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
283
284         //
285         // Read configuration files
286         //
287
288         readRcFile("lyxrc.defaults");
289         system_lyxrc = lyxrc;
290         system_formats = formats;
291         system_converters = converters;
292         system_lcolor = lcolor;
293
294         string prefsfile = "preferences";
295         // back compatibility to lyxs < 1.1.6
296         if (LibFileSearch(string(), prefsfile).empty())
297                 prefsfile = "lyxrc";
298         if (!LibFileSearch(string(), prefsfile).empty())
299                 readRcFile(prefsfile);
300
301         readEncodingsFile("encodings");
302         readLanguagesFile("languages");
303
304         // Load the layouts
305         lyxerr[Debug::INIT] << "Reading layouts..." << endl;
306         LyXSetStyle();
307
308         if (gui) {
309                 // Set up bindings
310                 toplevel_keymap.reset(new kb_keymap);
311                 defaultKeyBindings(toplevel_keymap.get());
312                 toplevel_keymap->read(lyxrc.bind_file);
313
314                 // Read menus
315                 readUIFile(lyxrc.ui_file);
316         }
317
318         if (lyxerr.debugging(Debug::LYXRC))
319                 lyxrc.print();
320
321         os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
322         if (lyxerr.debugging(Debug::INIT)) {
323                 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
324         }
325
326         lyxerr[Debug::INIT] << "Reading lastfiles `"
327                             << lyxrc.lastfiles << "'..." << endl;
328         lastfiles.reset(new LastFiles(lyxrc.lastfiles,
329                                       lyxrc.check_lastfiles,
330                                       lyxrc.num_lastfiles));
331 }
332
333
334 void LyX::defaultKeyBindings(kb_keymap  * kbmap)
335 {
336         kbmap->bind("Right", LFUN_RIGHT);
337         kbmap->bind("Left", LFUN_LEFT);
338         kbmap->bind("Up", LFUN_UP);
339         kbmap->bind("Down", LFUN_DOWN);
340
341         kbmap->bind("Tab", LFUN_CELL_FORWARD);
342         kbmap->bind("ISO_Left_Tab", LFUN_CELL_FORWARD); // jbl 2001-23-02
343
344         kbmap->bind("Home", LFUN_HOME);
345         kbmap->bind("End", LFUN_END);
346         kbmap->bind("Prior", LFUN_PRIOR);
347         kbmap->bind("Next", LFUN_NEXT);
348
349         kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
350         //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
351
352         kbmap->bind("Delete", LFUN_DELETE);
353         kbmap->bind("BackSpace", LFUN_BACKSPACE);
354
355         // sub- and superscript -MV
356         kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
357         kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
358
359         // kbmap->bindings to enable the use of the numeric keypad
360         // e.g. Num Lock set
361         //kbmap->bind("KP_0", LFUN_SELFINSERT);
362         //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
363         kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
364         //kbmap->bind("KP_1", LFUN_SELFINSERT);
365         //kbmap->bind("KP_2", LFUN_SELFINSERT);
366         //kbmap->bind("KP_3", LFUN_SELFINSERT);
367         //kbmap->bind("KP_4", LFUN_SELFINSERT);
368         //kbmap->bind("KP_5", LFUN_SELFINSERT);
369         //kbmap->bind("KP_6", LFUN_SELFINSERT);
370         //kbmap->bind("KP_Add", LFUN_SELFINSERT);
371         //kbmap->bind("KP_7", LFUN_SELFINSERT);
372         //kbmap->bind("KP_8", LFUN_SELFINSERT);
373         //kbmap->bind("KP_9", LFUN_SELFINSERT);
374         //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
375         //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
376         //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
377         kbmap->bind("KP_Right", LFUN_RIGHT);
378         kbmap->bind("KP_Left", LFUN_LEFT);
379         kbmap->bind("KP_Up", LFUN_UP);
380         kbmap->bind("KP_Down", LFUN_DOWN);
381         kbmap->bind("KP_Home", LFUN_HOME);
382         kbmap->bind("KP_End", LFUN_END);
383         kbmap->bind("KP_Prior", LFUN_PRIOR);
384         kbmap->bind("KP_Next", LFUN_NEXT);
385
386         kbmap->bind("C-Tab", LFUN_CELL_SPLIT);  // ale970515
387         kbmap->bind("S-Tab", LFUN_CELL_BACKWARD);  // jug20000522
388         kbmap->bind("S-ISO_Left_Tab", LFUN_CELL_BACKWARD); // jbl 2001-23-02
389 }
390
391
392 void LyX::emergencyCleanup()
393 {
394         // what to do about tmpfiles is non-obvious. we would
395         // like to delete any we find, but our lyxdir might
396         // contain documents etc. which might be helpful on
397         // a crash
398
399         bufferlist.emergencyWriteAll();
400         if (lyxserver)
401                 lyxserver->emergencyCleanup();
402 }
403
404
405 void LyX::deadKeyBindings(kb_keymap * kbmap)
406 {
407         // bindKeyings for transparent handling of deadkeys
408         // The keysyms are gotten from XFree86 X11R6
409         kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
410         kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
411         kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
412         kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
413         kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
414         kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
415         kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
416         kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
417         kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
418         kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
419         // nothing with this name
420         // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
421         kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
422         kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
423         // nothing with this name either...
424         //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
425         kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
426         kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
427         kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
428 }
429
430
431 void LyX::queryUserLyXDir(bool explicit_userdir)
432 {
433         string const configure_script = AddName(system_lyxdir(), "configure");
434
435         // Does user directory exist?
436         FileInfo fileInfo(user_lyxdir());
437         if (fileInfo.isOK() && fileInfo.isDir()) {
438                 first_start = false;
439                 FileInfo script(configure_script);
440                 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
441                 if (defaults.isOK() && script.isOK()
442                     && defaults.getModificationTime() < script.getModificationTime()) {
443                         lyxerr << _("LyX: reconfiguring user directory")
444                                << endl;
445                         Path p(user_lyxdir());
446                         ::system(configure_script.c_str());
447                         lyxerr << "LyX: " << _("Done!") << endl;
448                 }
449                 return;
450         }
451
452         first_start = !explicit_userdir;
453
454         lyxerr << bformat(_("LyX: Creating directory %1$s"
455                                   " and running configure..."), user_lyxdir()) << endl;
456
457         if (!createDirectory(user_lyxdir(), 0755)) {
458                 // Failed, let's use $HOME instead.
459                 user_lyxdir(GetEnvPath("HOME"));
460                 lyxerr << bformat(_("Failed. Will use %1$s instead."),
461                         user_lyxdir()) << endl;
462                 return;
463         }
464
465         // Run configure in user lyx directory
466         Path p(user_lyxdir());
467         ::system(configure_script.c_str());
468         lyxerr << "LyX: " << _("Done!") << endl;
469 }
470
471
472 void LyX::readRcFile(string const & name)
473 {
474         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
475
476         string const lyxrc_path = LibFileSearch(string(), name);
477         if (!lyxrc_path.empty()) {
478
479                 lyxerr[Debug::INIT] << "Found " << name
480                                     << " in " << lyxrc_path << endl;
481
482                 if (lyxrc.read(lyxrc_path) >= 0)
483                         return;
484         }
485
486         showFileError(name);
487 }
488
489
490 // Read the ui file `name'
491 void LyX::readUIFile(string const & name)
492 {
493         enum Uitags {
494                 ui_menuset = 1,
495                 ui_toolbar,
496                 ui_toolbars,
497                 ui_include,
498                 ui_last
499         };
500
501         struct keyword_item uitags[ui_last - 1] = {
502                 { "include", ui_include },
503                 { "menuset", ui_menuset },
504                 { "toolbar", ui_toolbar },
505                 { "toolbars", ui_toolbars }
506         };
507
508         // Ensure that a file is read only once (prevents include loops)
509         static std::list<string> uifiles;
510         std::list<string>::const_iterator it  = uifiles.begin();
511         std::list<string>::const_iterator end = uifiles.end();
512         it = std::find(it, end, name);
513         if (it != end) {
514                 lyxerr[Debug::INIT] << "UI file '" << name
515                                     << "' has been read already. "
516                                     << "Is this an include loop?"
517                                     << endl;
518                 return;
519         }
520
521         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
522
523         string const ui_path = LibFileSearch("ui", name, "ui");
524
525         if (ui_path.empty()) {
526                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
527                 showFileError(name);
528                 return;
529         }
530         uifiles.push_back(name);
531
532         lyxerr[Debug::INIT] << "Found " << name
533                             << " in " << ui_path << endl;
534         LyXLex lex(uitags, ui_last - 1);
535         lex.setFile(ui_path);
536         if (!lex.isOK()) {
537                 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
538                        << endl;
539         }
540
541         if (lyxerr.debugging(Debug::PARSER))
542                 lex.printTable(lyxerr);
543
544         while (lex.isOK()) {
545                 switch (lex.lex()) {
546                 case ui_include: {
547                         lex.next(true);
548                         string const file = lex.getString();
549                         readUIFile(file);
550                         break;
551                 }
552                 case ui_menuset:
553                         menubackend.read(lex);
554                         break;
555
556                 case ui_toolbar:
557                         toolbarbackend.read(lex);
558                         break;
559
560                 case ui_toolbars:
561                         toolbarbackend.readToolbars(lex);
562                         break;
563
564                 default:
565                         if (!rtrim(lex.getString()).empty())
566                                 lex.printError("LyX::ReadUIFile: "
567                                                "Unknown menu tag: `$$Token'");
568                         break;
569                 }
570         }
571 }
572
573
574 // Read the languages file `name'
575 void LyX::readLanguagesFile(string const & name)
576 {
577         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
578
579         string const lang_path = LibFileSearch(string(), name);
580         if (lang_path.empty()) {
581                 showFileError(name);
582                 return;
583         }
584         languages.read(lang_path);
585 }
586
587
588 // Read the encodings file `name'
589 void LyX::readEncodingsFile(string const & name)
590 {
591         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
592
593         string const enc_path = LibFileSearch(string(), name);
594         if (enc_path.empty()) {
595                 showFileError(name);
596                 return;
597         }
598         encodings.read(enc_path);
599 }
600
601
602 namespace {
603
604 bool is_gui = true;
605 string batch;
606
607 /// return the the number of arguments consumed
608 typedef boost::function<int(string const &, string const &)> cmd_helper;
609
610 int parse_dbg(string const & arg, string const &)
611 {
612         if (arg.empty()) {
613                 lyxerr << _("List of supported debug flags:") << endl;
614                 Debug::showTags(lyxerr);
615                 exit(0);
616         }
617         lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
618
619         lyxerr.level(Debug::value(arg));
620         Debug::showLevel(lyxerr, lyxerr.level());
621         return 1;
622 }
623
624
625 int parse_help(string const &, string const &)
626 {
627         lyxerr <<
628                 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
629                   "Command line switches (case sensitive):\n"
630                   "\t-help              summarize LyX usage\n"
631                   "\t-userdir dir       try to set user directory to dir\n"
632                   "\t-sysdir dir        try to set system directory to dir\n"
633                   "\t-geometry WxH+X+Y  set geometry of the main window\n"
634                   "\t-dbg feature[,feature]...\n"
635                   "                  select the features to debug.\n"
636                   "                  Type `lyx -dbg' to see the list of features\n"
637                   "\t-x [--execute] command\n"
638                   "                  where command is a lyx command.\n"
639                   "\t-e [--export] fmt\n"
640                   "                  where fmt is the export format of choice.\n"
641                   "\t-i [--import] fmt file.xxx\n"
642                   "                  where fmt is the import format of choice\n"
643                   "                  and file.xxx is the file to be imported.\n"
644                   "\t-version        summarize version and build info\n"
645                   "Check the LyX man page for more details.") << endl;
646         exit(0);
647         return 0;
648 }
649
650 int parse_version(string const &, string const &)
651 {
652         lyxerr << "LyX " << lyx_version
653                << " of " << lyx_release_date << endl;
654         lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
655
656         lyxerr << lyx_version_info << endl;
657         exit(0);
658         return 0;
659 }
660
661 int parse_sysdir(string const & arg, string const &)
662 {
663         if (arg.empty()) {
664                 lyxerr << _("Missing directory for -sysdir switch") << endl;
665                 exit(1);
666         }
667         system_lyxdir(arg);
668         return 1;
669 }
670
671 int parse_userdir(string const & arg, string const &)
672 {
673         if (arg.empty()) {
674                 lyxerr << _("Missing directory for -userdir switch") << endl;
675                 exit(1);
676         }
677         user_lyxdir(arg);
678         return 1;
679 }
680
681 int parse_execute(string const & arg, string const &)
682 {
683         if (arg.empty()) {
684                 lyxerr << _("Missing command string after --execute switch") << endl;
685                 exit(1);
686         }
687         batch = arg;
688         // Argh. Setting gui to false segfaults..
689         // FIXME: when ? how ?
690         // is_gui = false;
691         return 1;
692 }
693
694 int parse_export(string const & type, string const &)
695 {
696         if (type.empty()) {
697                 lyxerr << _("Missing file type [eg latex, ps...] after "
698                         "--export switch") << endl;
699                 exit(1);
700         }
701         batch = "buffer-export " + type;
702         is_gui = false;
703         return 1;
704 }
705
706 int parse_import(string const & type, string const & file)
707 {
708         if (type.empty()) {
709                 lyxerr << _("Missing file type [eg latex, ps...] after "
710                         "--import switch") << endl;
711                 exit(1);
712         }
713         if (file.empty()) {
714                 lyxerr << _("Missing filename for --import") << endl;
715                 exit(1);
716         }
717
718         batch = "buffer-import " + type + ' ' + file;
719         return 2;
720 }
721
722 } // namespace anon
723
724
725 bool LyX::easyParse(int & argc, char * argv[])
726 {
727         std::map<string, cmd_helper> cmdmap;
728
729         cmdmap["-dbg"] = parse_dbg;
730         cmdmap["-help"] = parse_help;
731         cmdmap["--help"] = parse_help;
732         cmdmap["-version"] = parse_version;
733         cmdmap["--version"] = parse_version;
734         cmdmap["-sysdir"] = parse_sysdir;
735         cmdmap["-userdir"] = parse_userdir;
736         cmdmap["-x"] = parse_execute;
737         cmdmap["--execute"] = parse_execute;
738         cmdmap["-e"] = parse_export;
739         cmdmap["--export"] = parse_export;
740         cmdmap["-i"] = parse_import;
741         cmdmap["--import"] = parse_import;
742
743         for (int i = 1; i < argc; ++i) {
744                 std::map<string, cmd_helper>::const_iterator it
745                         = cmdmap.find(argv[i]);
746
747                 // don't complain if not found - may be parsed later
748                 if (it == cmdmap.end())
749                         continue;
750
751                 string arg((i + 1 < argc) ? argv[i + 1] : "");
752                 string arg2((i + 2 < argc) ? argv[i + 2] : "");
753
754                 int const remove = 1 + it->second(arg, arg2);
755
756                 // Now, remove used arguments by shifting
757                 // the following ones remove places down.
758                 argc -= remove;
759                 for (int j = i; j < argc; ++j)
760                         argv[j] = argv[j + remove];
761                 --i;
762         }
763
764         batch_command = batch;
765
766         return is_gui;
767 }