]> git.lyx.org Git - lyx.git/blob - src/lyx_main.C
The setEnvPath 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 "mover.h"
40 #include "ToolbarBackend.h"
41
42 #include "mathed/math_inset.h"
43
44 #include "frontends/Alert.h"
45 #include "frontends/lyx_gui.h"
46 #include "frontends/LyXView.h"
47
48 #include "support/FileInfo.h"
49 #include "support/filetools.h"
50 #include "support/lyxlib.h"
51 #include "support/os.h"
52 #include "support/package.h"
53 #include "support/path.h"
54
55 #include <boost/bind.hpp>
56
57 #include <iostream>
58 #include <csignal>
59
60 using lyx::support::AddName;
61 using lyx::support::AddPath;
62 using lyx::support::bformat;
63 using lyx::support::createDirectory;
64 using lyx::support::createLyXTmpDir;
65 using lyx::support::FileInfo;
66 using lyx::support::FileSearch;
67 using lyx::support::GetEnv;
68 using lyx::support::i18nLibFileSearch;
69 using lyx::support::LibFileSearch;
70 using lyx::support::package;
71 using lyx::support::Path;
72 using lyx::support::QuoteName;
73 using lyx::support::rtrim;
74
75 namespace os = lyx::support::os;
76
77 using std::endl;
78 using std::string;
79 using std::vector;
80
81 #ifndef CXX_GLOBAL_CSTD
82 using std::exit;
83 using std::signal;
84 using std::system;
85 #endif
86
87
88 extern void QuitLyX();
89
90 extern LyXServer * lyxserver;
91
92 // This is the global bufferlist object
93 BufferList bufferlist;
94
95 // convenient to have it here.
96 boost::scoped_ptr<kb_keymap> toplevel_keymap;
97
98 namespace {
99
100 // Filled with the command line arguments "foo" of "-sysdir foo" or
101 // "-userdir foo".
102 string cl_system_support;
103 string cl_user_support;
104
105
106 void showFileError(string const & error)
107 {
108         Alert::warning(_("Could not read configuration file"),
109                    bformat(_("Error while reading the configuration file\n%1$s.\n"
110                      "Please check your installation."), error));
111         exit(EXIT_FAILURE);
112 }
113
114 } // namespace anon
115
116
117 boost::scoped_ptr<LyX> LyX::singleton_;
118
119 void LyX::exec(int & argc, char * argv[])
120 {
121         BOOST_ASSERT(!singleton_.get());
122         // We must return from this before launching the gui so that
123         // other parts of the code can access singleton_ through
124         // LyX::ref and LyX::cref.
125         singleton_.reset(new LyX);
126         // Start the real execution loop.
127         singleton_->priv_exec(argc, argv);
128 }
129
130
131 LyX & LyX::ref()
132 {
133         BOOST_ASSERT(singleton_.get());
134         return *singleton_.get();
135 }
136
137
138 LyX const & LyX::cref()
139 {
140         BOOST_ASSERT(singleton_.get());
141         return *singleton_.get();
142 }
143
144
145 LyX::LyX()
146         : first_start(false)
147 {}
148
149
150 LastFiles & LyX::lastfiles()
151 {
152         BOOST_ASSERT(lastfiles_.get());
153         return *lastfiles_.get();
154 }
155
156
157 LastFiles const & LyX::lastfiles() const
158 {
159         BOOST_ASSERT(lastfiles_.get());
160         return *lastfiles_.get();
161 }
162
163
164 void LyX::addLyXView(boost::shared_ptr<LyXView> const & lyxview)
165 {
166         views_.push_back(lyxview);
167 }
168
169
170 Buffer const * const LyX::updateInset(InsetBase const * inset) const
171 {
172         if (!inset)
173                 return 0;
174
175         Buffer const * buffer_ptr = 0;
176         ViewList::const_iterator it = views_.begin();
177         ViewList::const_iterator const end = views_.end();
178         for (; it != end; ++it) {
179                 Buffer const * ptr = (*it)->updateInset(inset);
180                 if (ptr)
181                         buffer_ptr = ptr;
182         }
183         return buffer_ptr;
184 }
185
186
187 void LyX::priv_exec(int & argc, char * argv[])
188 {
189         // Here we need to parse the command line. At least
190         // we need to parse for "-dbg" and "-help"
191         bool const want_gui = easyParse(argc, argv);
192
193         lyx::support::init_package(argv[0], cl_system_support, cl_user_support);
194
195         if (want_gui)
196                 lyx_gui::parse_init(argc, argv);
197
198         // check for any spurious extra arguments
199         // other than documents
200         for (int argi = 1; argi < argc ; ++argi) {
201                 if (argv[argi][0] == '-') {
202                         lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
203                                 argv[argi]) << endl;
204                         exit(1);
205                 }
206         }
207
208         // Initialization of LyX (reads lyxrc and more)
209         lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
210         init(want_gui);
211         lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
212
213         if (want_gui)
214                 lyx_gui::parse_lyxrc();
215
216         initMath();
217
218         vector<string> files;
219
220         for (int argi = argc - 1; argi >= 1; --argi)
221                 files.push_back(argv[argi]);
222
223         if (first_start)
224                 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
225
226         // Execute batch commands if available
227         if (!batch_command.empty()) {
228
229                 lyxerr[Debug::INIT] << "About to handle -x '"
230                        << batch_command << '\'' << endl;
231
232                 Buffer * last_loaded = 0;
233
234                 vector<string>::const_iterator it = files.begin();
235                 vector<string>::const_iterator end = files.end();
236
237                 for (; it != end; ++it) {
238                         // get absolute path of file and add ".lyx" to
239                         // the filename if necessary
240                         string s = FileSearch(string(), *it, "lyx");
241                         if (s.empty()) {
242                                 last_loaded = newFile(*it, string(), true);
243                         } else {
244                                 Buffer * buf = bufferlist.newBuffer(s, false);
245                                 buf->error.connect(boost::bind(&LyX::printError, this, _1));
246                                 if (loadLyXFile(buf, s))
247                                         last_loaded = buf;
248                                 else
249                                         bufferlist.release(buf);
250                         }
251                 }
252
253                 // try to dispatch to last loaded buffer first
254                 if (last_loaded) {
255                         bool success = false;
256                         if (last_loaded->dispatch(batch_command, &success)) {
257                                 QuitLyX();
258                                 exit(!success);
259                         }
260                 }
261                 files.clear(); // the files are already loaded
262         }
263
264         lyx_gui::start(batch_command, files);
265 }
266
267
268 extern "C" {
269
270 static void error_handler(int err_sig)
271 {
272         // Throw away any signals other than the first one received.
273         static sig_atomic_t handling_error = false;
274         if (handling_error)
275                 return;
276         handling_error = true;
277
278         // We have received a signal indicating a fatal error, so
279         // try and save the data ASAP.
280         LyX::cref().emergencyCleanup();
281
282         // These lyxerr calls may or may not work:
283
284         // Signals are asynchronous, so the main program may be in a very
285         // fragile state when a signal is processed and thus while a signal
286         // handler function executes.
287         // In general, therefore, we should avoid performing any
288         // I/O operations or calling most library and system functions from
289         // signal handlers.
290
291         // This shouldn't matter here, however, as we've already invoked
292         // emergencyCleanup.
293         switch (err_sig) {
294         case SIGHUP:
295                 lyxerr << "\nlyx: SIGHUP signal caught\nBye." << endl;
296                 break;
297         case SIGFPE:
298                 lyxerr << "\nlyx: SIGFPE signal caught\nBye." << endl;
299                 break;
300         case SIGSEGV:
301                 lyxerr << "\nlyx: SIGSEGV signal caught\n"
302                           "Sorry, you have found a bug in LyX. "
303                           "Please read the bug-reporting instructions "
304                           "in Help->Introduction and send us a bug report, "
305                           "if necessary. Thanks !\nBye." << endl;
306                 break;
307         case SIGINT:
308         case SIGTERM:
309                 // no comments
310                 break;
311         }
312
313         // Deinstall the signal handlers
314         signal(SIGHUP, SIG_DFL);
315         signal(SIGINT, SIG_DFL);
316         signal(SIGFPE, SIG_DFL);
317         signal(SIGSEGV, SIG_DFL);
318         signal(SIGTERM, SIG_DFL);
319
320         if (err_sig == SIGSEGV ||
321             (err_sig != SIGHUP && !GetEnv("LYXDEBUG").empty()))
322                 lyx::support::abort();
323         exit(0);
324 }
325
326 }
327
328
329 void LyX::printError(ErrorItem const & ei)
330 {
331         std::cerr << _("LyX: ") << ei.error
332                   << ':' << ei.description << std::endl;
333
334 }
335
336
337 void LyX::init(bool gui)
338 {
339         signal(SIGHUP, error_handler);
340         signal(SIGFPE, error_handler);
341         signal(SIGSEGV, error_handler);
342         signal(SIGINT, error_handler);
343         signal(SIGTERM, error_handler);
344         // SIGPIPE can be safely ignored.
345
346 #if !defined (USE_POSIX_PACKAGING)
347         // Add the directory containing the LyX executable to the path
348         // so that LyX can find things like reLyX.
349         if (package.build_support().empty()) {
350                 vector<string> path = getEnvPath("PATH");
351                 path.insert(path.begin(), package.binary_dir());
352                 setEnvPath("PATH", path);
353         }
354 #endif
355 #if defined (USE_MACOSX_PACKAGING)
356         // This hard-coded nastiness should be moved into a LyXRC variable.
357         vector<string> path = getEnvPath("PATH");
358         path.insert(path.begin(), "/usr/local/teTeX/bin/powerpc-apple-darwin-current");
359         path.insert(path.begin(), "/usr/local/bin");
360         path.insert(path.begin(), "/sw/bin");
361         lyxerr[Debug::INIT]  << "Running from LyX/Mac bundle. "
362                 "Setting PATH to: " << GetEnv("PATH") << endl;
363 #endif
364
365         // Check that user LyX directory is ok. We don't do that if
366         // running in batch mode.
367         if (gui) {
368                 queryUserLyXDir(package().explicit_user_support());
369         } else {
370                 first_start = false;
371         }
372
373         // Disable gui when easyparse says so
374         lyx_gui::use_gui = gui;
375
376         lyxrc.tempdir_path = package().temp_dir();
377         lyxrc.document_path = package().document_dir();
378
379         if (lyxrc.template_path.empty()) {
380                 lyxrc.template_path = AddPath(package().system_support(),
381                                               "templates");
382         }
383
384         if (lyxrc.lastfiles.empty()) {
385                 lyxrc.lastfiles = AddName(package().user_support(), "lastfiles");
386         }
387
388         if (lyxrc.roman_font_name.empty())
389                 lyxrc.roman_font_name = lyx_gui::roman_font_name();
390         if (lyxrc.sans_font_name.empty())
391                 lyxrc.sans_font_name = lyx_gui::sans_font_name();
392         if (lyxrc.typewriter_font_name.empty())
393                 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
394
395         //
396         // Read configuration files
397         //
398
399         readRcFile("lyxrc.defaults");
400         system_lyxrc = lyxrc;
401         system_formats = formats;
402         system_converters = converters;
403         system_movers = movers;
404         system_lcolor = lcolor;
405
406         string prefsfile = "preferences";
407         // back compatibility to lyxs < 1.1.6
408         if (LibFileSearch(string(), prefsfile).empty())
409                 prefsfile = "lyxrc";
410         if (!LibFileSearch(string(), prefsfile).empty())
411                 readRcFile(prefsfile);
412
413         readEncodingsFile("encodings");
414         readLanguagesFile("languages");
415
416         // Load the layouts
417         lyxerr[Debug::INIT] << "Reading layouts..." << endl;
418         LyXSetStyle();
419
420         if (gui) {
421                 // Set up bindings
422                 toplevel_keymap.reset(new kb_keymap);
423                 defaultKeyBindings(toplevel_keymap.get());
424                 toplevel_keymap->read(lyxrc.bind_file);
425
426                 // Read menus
427                 readUIFile(lyxrc.ui_file);
428         }
429
430         if (lyxerr.debugging(Debug::LYXRC))
431                 lyxrc.print();
432
433         package().document_dir() = lyxrc.document_path;
434
435         package().temp_dir() = createLyXTmpDir(lyxrc.tempdir_path);
436         if (package().temp_dir().empty()) {
437                 Alert::error(_("Could not create temporary directory"),
438                              bformat(_("Could not create a temporary directory in\n"
439                                        "%1$s. Make sure that this\n"
440                                        "path exists and is writable and try again."),
441                                      lyxrc.tempdir_path));
442                 // createLyXTmpDir() tries sufficiently hard to create a
443                 // usable temp dir, so the probability to come here is
444                 // close to zero. We therefore don't try to overcome this
445                 // problem with e.g. asking the user for a new path and
446                 // trying again but simply exit.
447                 exit(EXIT_FAILURE);
448         }
449
450         if (lyxerr.debugging(Debug::INIT)) {
451                 lyxerr << "LyX tmp dir: `" << package().temp_dir() << '\'' << endl;
452         }
453
454         lyxerr[Debug::INIT] << "Reading lastfiles `"
455                             << lyxrc.lastfiles << "'..." << endl;
456         lastfiles_.reset(new LastFiles(lyxrc.lastfiles,
457                                        lyxrc.check_lastfiles,
458                                        lyxrc.num_lastfiles));
459 }
460
461
462 void LyX::defaultKeyBindings(kb_keymap  * kbmap)
463 {
464         kbmap->bind("Right", FuncRequest(LFUN_RIGHT));
465         kbmap->bind("Left", FuncRequest(LFUN_LEFT));
466         kbmap->bind("Up", FuncRequest(LFUN_UP));
467         kbmap->bind("Down", FuncRequest(LFUN_DOWN));
468
469         kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
470         kbmap->bind("ISO_Left_Tab", FuncRequest(LFUN_CELL_FORWARD));
471
472         kbmap->bind("Home", FuncRequest(LFUN_HOME));
473         kbmap->bind("End", FuncRequest(LFUN_END));
474         kbmap->bind("Prior", FuncRequest(LFUN_PRIOR));
475         kbmap->bind("Next", FuncRequest(LFUN_NEXT));
476
477         kbmap->bind("Return", FuncRequest(LFUN_BREAKPARAGRAPH));
478         //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
479
480         kbmap->bind("Delete", FuncRequest(LFUN_DELETE));
481         kbmap->bind("BackSpace", FuncRequest(LFUN_BACKSPACE));
482
483         // kbmap->bindings to enable the use of the numeric keypad
484         // e.g. Num Lock set
485         //kbmap->bind("KP_0", FuncRequest(LFUN_SELFINSERT));
486         //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELFINSERT));
487         kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAKPARAGRAPH));
488         //kbmap->bind("KP_1", FuncRequest(LFUN_SELFINSERT));
489         //kbmap->bind("KP_2", FuncRequest(LFUN_SELFINSERT));
490         //kbmap->bind("KP_3", FuncRequest(LFUN_SELFINSERT));
491         //kbmap->bind("KP_4", FuncRequest(LFUN_SELFINSERT));
492         //kbmap->bind("KP_5", FuncRequest(LFUN_SELFINSERT));
493         //kbmap->bind("KP_6", FuncRequest(LFUN_SELFINSERT));
494         //kbmap->bind("KP_Add", FuncRequest(LFUN_SELFINSERT));
495         //kbmap->bind("KP_7", FuncRequest(LFUN_SELFINSERT));
496         //kbmap->bind("KP_8", FuncRequest(LFUN_SELFINSERT));
497         //kbmap->bind("KP_9", FuncRequest(LFUN_SELFINSERT));
498         //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELFINSERT));
499         //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELFINSERT));
500         //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELFINSERT));
501         kbmap->bind("KP_Right", FuncRequest(LFUN_RIGHT));
502         kbmap->bind("KP_Left", FuncRequest(LFUN_LEFT));
503         kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
504         kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
505         kbmap->bind("KP_Home", FuncRequest(LFUN_HOME));
506         kbmap->bind("KP_End", FuncRequest(LFUN_END));
507         kbmap->bind("KP_Prior", FuncRequest(LFUN_PRIOR));
508         kbmap->bind("KP_Next", FuncRequest(LFUN_NEXT));
509
510         kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
511         kbmap->bind("S-Tab", FuncRequest(LFUN_CELL_BACKWARD));
512         kbmap->bind("S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
513 }
514
515
516 void LyX::emergencyCleanup() const
517 {
518         // what to do about tmpfiles is non-obvious. we would
519         // like to delete any we find, but our lyxdir might
520         // contain documents etc. which might be helpful on
521         // a crash
522
523         bufferlist.emergencyWriteAll();
524         if (lyxserver)
525                 lyxserver->emergencyCleanup();
526 }
527
528
529 void LyX::deadKeyBindings(kb_keymap * kbmap)
530 {
531         // bindKeyings for transparent handling of deadkeys
532         // The keysyms are gotten from XFree86 X11R6
533         kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACUTE));
534         kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_BREVE));
535         kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_CARON));
536         kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_CEDILLA));
537         kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_CIRCLE));
538         kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_CIRCUMFLEX));
539         kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_DOT));
540         kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_GRAVE));
541         kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_HUNG_UMLAUT));
542         kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_MACRON));
543         // nothing with this name
544         // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
545         kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_TILDE));
546         kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_UMLAUT));
547         // nothing with this name either...
548         //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_UNDERBAR));
549         kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_UNDERDOT));
550         kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_TIE));
551         kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_OGONEK));
552 }
553
554
555 void LyX::queryUserLyXDir(bool explicit_userdir)
556 {
557         string const configure_script = AddName(package().system_support(), "configure");
558         string const configure_command = "sh " + QuoteName(configure_script);
559
560         // Does user directory exist?
561         FileInfo fileInfo(package().user_support());
562         if (fileInfo.isOK() && fileInfo.isDir()) {
563                 first_start = false;
564                 FileInfo script(configure_script);
565                 FileInfo defaults(AddName(package().user_support(), "lyxrc.defaults"));
566                 if (defaults.isOK() && script.isOK()
567                     && defaults.getModificationTime() < script.getModificationTime()) {
568                         lyxerr << _("LyX: reconfiguring user directory")
569                                << endl;
570                         Path p(package().user_support());
571                         ::system(configure_command.c_str());
572                         lyxerr << "LyX: " << _("Done!") << endl;
573                 }
574                 return;
575         }
576
577         first_start = !explicit_userdir;
578
579         // If the user specified explicitly a directory, ask whether
580         // to create it. If the user says "no", then exit.
581         if (explicit_userdir &&
582             !Alert::prompt(
583                     _("Missing LyX support directory"),
584                     bformat(_("You have specified a non-existent user "
585                               "LyX directory, %1$s.\n"
586                               "It is needed to keep your own configuration."),
587                             package().user_support()),
588                     1, 0,
589                     _("&Create directory."),
590                     _("&Exit LyX."))) {
591                 lyxerr << _("No user LyX directory. Exiting.") << endl;
592                 exit(1);
593         }
594
595         lyxerr << bformat(_("LyX: Creating directory %1$s"
596                             " and running configure..."), package().user_support()) << endl;
597
598         if (!createDirectory(package().user_support(), 0755)) {
599                 // Failed, so let's exit.
600                 lyxerr << _("Failed to create directory. Exiting.")
601                        << endl;
602                 exit(1);
603         }
604
605         // Run configure in user lyx directory
606         Path p(package().user_support());
607         ::system(configure_command.c_str());
608         lyxerr << "LyX: " << _("Done!") << endl;
609 }
610
611
612 void LyX::readRcFile(string const & name)
613 {
614         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
615
616         string const lyxrc_path = LibFileSearch(string(), name);
617         if (!lyxrc_path.empty()) {
618
619                 lyxerr[Debug::INIT] << "Found " << name
620                                     << " in " << lyxrc_path << endl;
621
622                 if (lyxrc.read(lyxrc_path) >= 0)
623                         return;
624         }
625
626         showFileError(name);
627 }
628
629
630 // Read the ui file `name'
631 void LyX::readUIFile(string const & name)
632 {
633         enum Uitags {
634                 ui_menuset = 1,
635                 ui_toolbar,
636                 ui_toolbars,
637                 ui_include,
638                 ui_last
639         };
640
641         struct keyword_item uitags[ui_last - 1] = {
642                 { "include", ui_include },
643                 { "menuset", ui_menuset },
644                 { "toolbar", ui_toolbar },
645                 { "toolbars", ui_toolbars }
646         };
647
648         // Ensure that a file is read only once (prevents include loops)
649         static std::list<string> uifiles;
650         std::list<string>::const_iterator it  = uifiles.begin();
651         std::list<string>::const_iterator end = uifiles.end();
652         it = std::find(it, end, name);
653         if (it != end) {
654                 lyxerr[Debug::INIT] << "UI file '" << name
655                                     << "' has been read already. "
656                                     << "Is this an include loop?"
657                                     << endl;
658                 return;
659         }
660
661         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
662
663         string const ui_path = LibFileSearch("ui", name, "ui");
664
665         if (ui_path.empty()) {
666                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
667                 showFileError(name);
668                 return;
669         }
670         uifiles.push_back(name);
671
672         lyxerr[Debug::INIT] << "Found " << name
673                             << " in " << ui_path << endl;
674         LyXLex lex(uitags, ui_last - 1);
675         lex.setFile(ui_path);
676         if (!lex.isOK()) {
677                 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
678                        << endl;
679         }
680
681         if (lyxerr.debugging(Debug::PARSER))
682                 lex.printTable(lyxerr);
683
684         while (lex.isOK()) {
685                 switch (lex.lex()) {
686                 case ui_include: {
687                         lex.next(true);
688                         string const file = lex.getString();
689                         readUIFile(file);
690                         break;
691                 }
692                 case ui_menuset:
693                         menubackend.read(lex);
694                         break;
695
696                 case ui_toolbar:
697                         toolbarbackend.read(lex);
698                         break;
699
700                 case ui_toolbars:
701                         toolbarbackend.readToolbars(lex);
702                         break;
703
704                 default:
705                         if (!rtrim(lex.getString()).empty())
706                                 lex.printError("LyX::ReadUIFile: "
707                                                "Unknown menu tag: `$$Token'");
708                         break;
709                 }
710         }
711 }
712
713
714 // Read the languages file `name'
715 void LyX::readLanguagesFile(string const & name)
716 {
717         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
718
719         string const lang_path = LibFileSearch(string(), name);
720         if (lang_path.empty()) {
721                 showFileError(name);
722                 return;
723         }
724         languages.read(lang_path);
725 }
726
727
728 // Read the encodings file `name'
729 void LyX::readEncodingsFile(string const & name)
730 {
731         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
732
733         string const enc_path = LibFileSearch(string(), name);
734         if (enc_path.empty()) {
735                 showFileError(name);
736                 return;
737         }
738         encodings.read(enc_path);
739 }
740
741
742 namespace {
743
744 bool is_gui = true;
745 string batch;
746
747 /// return the the number of arguments consumed
748 typedef boost::function<int(string const &, string const &)> cmd_helper;
749
750 int parse_dbg(string const & arg, string const &)
751 {
752         if (arg.empty()) {
753                 lyxerr << _("List of supported debug flags:") << endl;
754                 Debug::showTags(lyxerr);
755                 exit(0);
756         }
757         lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
758
759         lyxerr.level(Debug::value(arg));
760         Debug::showLevel(lyxerr, lyxerr.level());
761         return 1;
762 }
763
764
765 int parse_help(string const &, string const &)
766 {
767         lyxerr <<
768                 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
769                   "Command line switches (case sensitive):\n"
770                   "\t-help              summarize LyX usage\n"
771                   "\t-userdir dir       try to set user directory to dir\n"
772                   "\t-sysdir dir        try to set system directory to dir\n"
773                   "\t-geometry WxH+X+Y  set geometry of the main window\n"
774                   "\t-dbg feature[,feature]...\n"
775                   "                  select the features to debug.\n"
776                   "                  Type `lyx -dbg' to see the list of features\n"
777                   "\t-x [--execute] command\n"
778                   "                  where command is a lyx command.\n"
779                   "\t-e [--export] fmt\n"
780                   "                  where fmt is the export format of choice.\n"
781                   "\t-i [--import] fmt file.xxx\n"
782                   "                  where fmt is the import format of choice\n"
783                   "                  and file.xxx is the file to be imported.\n"
784                   "\t-version        summarize version and build info\n"
785                   "Check the LyX man page for more details.") << endl;
786         exit(0);
787         return 0;
788 }
789
790 int parse_version(string const &, string const &)
791 {
792         lyxerr << "LyX " << lyx_version
793                << " of " << lyx_release_date << endl;
794         lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
795
796         lyxerr << lyx_version_info << endl;
797         exit(0);
798         return 0;
799 }
800
801 int parse_sysdir(string const & arg, string const &)
802 {
803         if (arg.empty()) {
804                 lyxerr << _("Missing directory for -sysdir switch") << endl;
805                 exit(1);
806         }
807         cl_system_support = arg;
808         return 1;
809 }
810
811 int parse_userdir(string const & arg, string const &)
812 {
813         if (arg.empty()) {
814                 lyxerr << _("Missing directory for -userdir switch") << endl;
815                 exit(1);
816         }
817         cl_user_support = arg;
818         return 1;
819 }
820
821 int parse_execute(string const & arg, string const &)
822 {
823         if (arg.empty()) {
824                 lyxerr << _("Missing command string after --execute switch") << endl;
825                 exit(1);
826         }
827         batch = arg;
828         // Argh. Setting gui to false segfaults..
829         // FIXME: when ? how ?
830         // is_gui = false;
831         return 1;
832 }
833
834 int parse_export(string const & type, string const &)
835 {
836         if (type.empty()) {
837                 lyxerr << _("Missing file type [eg latex, ps...] after "
838                         "--export switch") << endl;
839                 exit(1);
840         }
841         batch = "buffer-export " + type;
842         is_gui = false;
843         return 1;
844 }
845
846 int parse_import(string const & type, string const & file)
847 {
848         if (type.empty()) {
849                 lyxerr << _("Missing file type [eg latex, ps...] after "
850                         "--import switch") << endl;
851                 exit(1);
852         }
853         if (file.empty()) {
854                 lyxerr << _("Missing filename for --import") << endl;
855                 exit(1);
856         }
857
858         batch = "buffer-import " + type + ' ' + file;
859         return 2;
860 }
861
862 } // namespace anon
863
864
865 bool LyX::easyParse(int & argc, char * argv[])
866 {
867         std::map<string, cmd_helper> cmdmap;
868
869         cmdmap["-dbg"] = parse_dbg;
870         cmdmap["-help"] = parse_help;
871         cmdmap["--help"] = parse_help;
872         cmdmap["-version"] = parse_version;
873         cmdmap["--version"] = parse_version;
874         cmdmap["-sysdir"] = parse_sysdir;
875         cmdmap["-userdir"] = parse_userdir;
876         cmdmap["-x"] = parse_execute;
877         cmdmap["--execute"] = parse_execute;
878         cmdmap["-e"] = parse_export;
879         cmdmap["--export"] = parse_export;
880         cmdmap["-i"] = parse_import;
881         cmdmap["--import"] = parse_import;
882
883         for (int i = 1; i < argc; ++i) {
884                 std::map<string, cmd_helper>::const_iterator it
885                         = cmdmap.find(argv[i]);
886
887                 // don't complain if not found - may be parsed later
888                 if (it == cmdmap.end())
889                         continue;
890
891                 string arg((i + 1 < argc) ? argv[i + 1] : "");
892                 string arg2((i + 2 < argc) ? argv[i + 2] : "");
893
894                 int const remove = 1 + it->second(arg, arg2);
895
896                 // Now, remove used arguments by shifting
897                 // the following ones remove places down.
898                 argc -= remove;
899                 for (int j = i; j < argc; ++j)
900                         argv[j] = argv[j + remove];
901                 --i;
902         }
903
904         batch_command = batch;
905
906         return is_gui;
907 }