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