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