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