]> git.lyx.org Git - lyx.git/blob - src/lyx_main.C
285606c2bf2e49a0e05d4986b8c84c89e8f2ccd3
[lyx.git] / src / lyx_main.C
1 /**
2  * \file lyx_main.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  */
8
9 #include <config.h>
10 #include <version.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "lyx_main.h"
17
18 #include "support/filetools.h"
19 #include "support/lyxlib.h"
20 #include "support/os.h"
21 #include "support/FileInfo.h"
22 #include "support/path.h"
23 #include "debug.h"
24 #include "gettext.h"
25 #include "lyxlex.h"
26
27 #include "bufferlist.h"
28 #include "buffer.h"
29 #include "lyxserver.h"
30 #include "kbmap.h"
31 #include "lyxfunc.h"
32 #include "ToolbarDefaults.h"
33 #include "MenuBackend.h"
34 #include "language.h"
35 #include "lastfiles.h"
36 #include "encoding.h"
37 #include "converter.h"
38 #include "lyxtextclasslist.h"
39
40 #include "frontends/Alert.h"
41 #include "frontends/lyx_gui.h"
42
43 #include <cstdlib>
44 #include <csignal>
45
46 using std::vector;
47 using std::endl;
48
49 #ifndef CXX_GLOBAL_CSTD
50 using std::exit;
51 using std::signal;
52 using std::system;
53 #endif
54
55 extern void LoadLyXFile(string const &);
56 extern void QuitLyX();
57
58 extern LyXServer * lyxserver;
59
60 string system_lyxdir;
61 string build_lyxdir;
62 string system_tempdir;
63 string user_lyxdir;
64
65 DebugStream lyxerr;
66
67 boost::scoped_ptr<LastFiles> lastfiles;
68
69 // This is the global bufferlist object
70 BufferList bufferlist;
71
72 // convenient to have it here.
73 boost::scoped_ptr<kb_keymap> toplevel_keymap;
74
75
76 LyX::LyX(int & argc, char * argv[])
77 {
78         // Here we need to parse the command line. At least
79         // we need to parse for "-dbg" and "-help"
80         bool const want_gui = easyParse(argc, argv);
81
82         // Global bindings (this must be done as early as possible.) (Lgb)
83         toplevel_keymap.reset(new kb_keymap);
84         defaultKeyBindings(toplevel_keymap.get());
85
86         if (want_gui) {
87                 lyx_gui::parse_init(argc, argv);
88         }
89
90         // check for any spurious extra arguments
91         // other than documents
92         for (int argi = 1; argi < argc ; ++argi) {
93                 if (argv[argi][0] == '-') {
94                         lyxerr << _("Wrong command line option `")
95                                << argv[argi]
96                                << _("'. Exiting.") << endl;
97                         exit(1);
98                 }
99         }
100
101         // Initialization of LyX (reads lyxrc and more)
102         lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
103         init(want_gui);
104         lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
105
106         if (want_gui) {
107                 lyx_gui::parse_lyxrc();
108         }
109
110         vector<string> files;
111
112         for (int argi = argc - 1; argi >= 1; --argi) {
113                 files.push_back(argv[argi]);
114         }
115
116         if (first_start) {
117                 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
118         }
119
120         // Execute batch commands if available
121         if (!batch_command.empty()) {
122                 lyxerr[Debug::INIT] << "About to handle -x '"
123                        << batch_command << "'" << endl;
124
125                 Buffer * last_loaded = 0;
126  
127                 vector<string>::iterator it = files.begin();
128                 vector<string>::iterator end = files.end();
129                 for (; it != end; ++it) {
130                         last_loaded = bufferlist.loadLyXFile(*it);
131                 }
132
133                 files.clear();
134
135                 // no buffer loaded, create one
136                 if (!last_loaded)
137                         last_loaded = bufferlist.newFile("tmpfile", string());
138
139                 bool success = false;
140
141                 // try to dispatch to last loaded buffer first
142                 bool const dispatched = last_loaded->dispatch(batch_command, &success);
143
144                 if (success) { 
145                         QuitLyX();
146                         exit(!success);
147                 }
148         }
149
150         lyx_gui::start(batch_command, files);
151 }
152
153
154 extern "C" {
155
156 static
157 void error_handler(int err_sig)
158 {
159         switch (err_sig) {
160         case SIGHUP:
161                 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
162                 break;
163         case SIGINT:
164                 // no comments
165                 break;
166         case SIGFPE:
167                 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
168                 break;
169         case SIGSEGV:
170                 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
171                 lyxerr <<
172                         "Sorry, you have found a bug in LyX. "
173                         "Please read the bug-reporting instructions "
174                         "in Help->Introduction and send us a bug report, "
175                         "if necessary. Thanks !" << endl;
176                 break;
177         case SIGTERM:
178                 // no comments
179                 break;
180         }
181
182         // Deinstall the signal handlers
183         signal(SIGHUP, SIG_DFL);
184         signal(SIGINT, SIG_DFL);
185         signal(SIGFPE, SIG_DFL);
186         signal(SIGSEGV, SIG_DFL);
187         signal(SIGTERM, SIG_DFL);
188
189         LyX::emergencyCleanup();
190
191         lyxerr << "Bye." << endl;
192         if (err_sig!= SIGHUP &&
193            (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
194                 lyx::abort();
195         exit(0);
196 }
197
198 }
199
200
201 void LyX::init(bool gui)
202 {
203         // Install the signal handlers
204         signal(SIGHUP, error_handler);
205         signal(SIGFPE, error_handler);
206         signal(SIGSEGV, error_handler);
207         signal(SIGINT, error_handler);
208         signal(SIGTERM, error_handler);
209
210         //
211         // Determine path of binary
212         //
213
214         string binpath = os::binpath();
215         string binname = os::binname();
216         string fullbinname = MakeAbsPath(binname, binpath);
217
218         if (binpath.empty()) {
219                 lyxerr << _("Warning: could not determine path of binary.")
220                        << "\n"
221                        << _("If you have problems, try starting LyX with an absolute path.")
222                        << endl;
223         }
224         lyxerr[Debug::INIT] << "Name of binary: " << binname << endl;
225         lyxerr[Debug::INIT] << "Path of binary: " << binpath << endl;
226
227         //
228         // Determine system directory.
229         //
230
231         // Directories are searched in this order:
232         // 1) -sysdir command line parameter
233         // 2) LYX_DIR_13x environment variable
234         // 3) Maybe <path of binary>/TOP_SRCDIR/lib
235         // 4) <path of binary>/../share/<name of binary>/
236         // 4a) repeat 4 after following the Symlink if <path of
237         //     binary> is a symbolic link.
238         // 5) hardcoded lyx_dir
239         // The directory is checked for the presence of the file
240         // "chkconfig.ltx", and if that is present, the directory
241         // is accepted as the system directory.
242         // If no chkconfig.ltx file is found, a warning is given,
243         // and the hardcoded lyx_dir is used.
244
245         // If we had a command line switch, system_lyxdir is already set
246         string searchpath;
247         if (!system_lyxdir.empty())
248                 searchpath = MakeAbsPath(system_lyxdir) + ';';
249
250         // LYX_DIR_13x environment variable
251         string const lyxdir = GetEnvPath("LYX_DIR_13x");
252
253         if (!lyxdir.empty()) {
254                 lyxerr[Debug::INIT] << "LYX_DIR_13x: " << lyxdir << endl;
255                 searchpath += lyxdir + ';';
256         }
257
258         string fullbinpath = binpath;
259         FileInfo file(fullbinname, true);
260         if (file.isLink()) {
261                 lyxerr[Debug::INIT] << "binary is a link" << endl;
262                 string link;
263                 if (LyXReadLink(fullbinname, link, true)) {
264                         // Path of binary/../share/name of binary/
265                         searchpath += NormalizePath(AddPath(binpath,
266                                                             "../share/")
267                                                     + OnlyFilename(binname));
268                         searchpath += ';';
269                         fullbinpath = link;
270                         binpath = MakeAbsPath(OnlyPath(fullbinpath));
271                 }
272         }
273
274         bool followlink;
275         do {
276                 // Path of binary/../share/name of binary/
277                 searchpath += NormalizePath(AddPath(binpath, "../share/") +
278                       OnlyFilename(binname)) + ';';
279
280                 // Follow Symlinks
281                 FileInfo file(fullbinpath, true);
282                 followlink = file.isLink();
283                 if (followlink) {
284                         lyxerr << " directory " << fullbinpath
285                                << " is a link" << endl;
286                         string link;
287                         if (LyXReadLink(fullbinpath, link, true)) {
288                                 fullbinpath = link;
289                                 binpath = MakeAbsPath(OnlyPath(fullbinpath));
290                         }
291                         else {
292                                 followlink = false;
293                         }
294                 }
295         } while (followlink);
296
297         // <path of binary>/TOP_SRCDIR/lib
298         build_lyxdir = MakeAbsPath("../lib", binpath);
299         if (!FileSearch(build_lyxdir, "lyxrc.defaults").empty()) {
300                 searchpath += MakeAbsPath(AddPath(TOP_SRCDIR, "lib"),
301                                           binpath) + ';';
302                 lyxerr[Debug::INIT] << "Checking whether LyX is run in "
303                         "place... yes" << endl;
304         } else {
305                 lyxerr[Debug::INIT]
306                         << "Checking whether LyX is run in place... no"
307                         << endl;
308                 build_lyxdir.erase();
309         }
310
311         // Hardcoded dir
312         searchpath += LYX_DIR;
313
314         // If debugging, show complete search path
315         lyxerr[Debug::INIT] << "System directory search path: "
316                             << searchpath << endl;
317
318         string const filename = "chkconfig.ltx";
319         string const temp = FileOpenSearch(searchpath, filename, string());
320         system_lyxdir = OnlyPath(temp);
321
322         // Reduce "path/../path" stuff out of system directory
323         system_lyxdir = NormalizePath(system_lyxdir);
324
325         bool path_shown = false;
326
327         // Warn if environment variable is set, but unusable
328         if (!lyxdir.empty()) {
329                 if (system_lyxdir != NormalizePath(lyxdir)) {
330                         lyxerr <<_("LYX_DIR_13x environment variable no good.")
331                                << '\n'
332                                << _("System directory set to: ")
333                                << system_lyxdir << endl;
334                         path_shown = true;
335                 }
336         }
337
338         // Warn the user if we couldn't find "chkconfig.ltx"
339         if (system_lyxdir == "./") {
340                 lyxerr <<_("LyX Warning! Couldn't determine system directory. ")
341                        <<_("Try the '-sysdir' command line parameter or ")
342                        <<_("set the environment variable LYX_DIR_13x to the "
343                            "LyX system directory ")
344                        << _("containing the file `chkconfig.ltx'.") << endl;
345                 if (!path_shown) {
346                         FileInfo fi(LYX_DIR);
347                         if (!fi.exist()) {
348                                 lyxerr << "Couldn't even find the default LYX_DIR." << endl
349                                         << "Giving up." << endl;
350                                 exit(1);
351                         }
352                         lyxerr << _("Using built-in default ")
353                                << LYX_DIR << _(" but expect problems.")
354                                << endl;
355                 } else {
356                         lyxerr << _("Expect problems.") << endl;
357                 }
358                 system_lyxdir = LYX_DIR;
359                 path_shown = true;
360         }
361         // Report the system directory if debugging is on
362         if (!path_shown)
363                 lyxerr[Debug::INIT] << "System directory: '"
364                                     << system_lyxdir << '\'' << endl;
365
366         //
367         // Determine user lyx-dir
368         //
369
370         // Directories are searched in this order:
371         // 1) -userdir command line parameter
372         // 2) LYX_USERDIR_13x environment variable
373         // 3) $HOME/.<name of binary>
374
375         // If we had a command line switch, user_lyxdir is already set
376         bool explicit_userdir = true;
377         if (user_lyxdir.empty()) {
378
379                 // LYX_USERDIR_13x environment variable
380                 user_lyxdir = GetEnvPath("LYX_USERDIR_13x");
381
382                 // default behaviour
383                 if (user_lyxdir.empty())
384                         user_lyxdir = AddPath(GetEnvPath("HOME"),
385                                                         string(".") + PACKAGE);
386                         explicit_userdir = false;
387         }
388
389         lyxerr[Debug::INIT] << "User LyX directory: '"
390                             <<  user_lyxdir << '\'' << endl;
391
392         // Check that user LyX directory is ok. We don't do that if
393         // running in batch mode.
394         if (gui) {
395                 queryUserLyXDir(explicit_userdir);
396         } else {
397                 first_start = false;
398         }
399
400         //
401         // Shine up lyxrc defaults
402         //
403
404         // Default template path: system_dir/templates
405         if (lyxrc.template_path.empty()) {
406                 lyxrc.template_path = AddPath(system_lyxdir, "templates");
407         }
408
409         // Default lastfiles file: $HOME/.lyx/lastfiles
410         if (lyxrc.lastfiles.empty()) {
411                 lyxrc.lastfiles = AddName(user_lyxdir, "lastfiles");
412         }
413
414         // Disable gui when either lyxrc or easyparse says so
415         if (!gui)
416                 lyxrc.use_gui = false;
417
418         //
419         // Read configuration files
420         //
421
422         readRcFile("lyxrc.defaults");
423         system_lyxrc = lyxrc;
424         system_formats = formats;
425         system_converters = converters;
426         system_lcolor = lcolor;
427
428         // If there is a preferences file we read that instead
429         // of the old lyxrc file.
430         if (!readRcFile("preferences"))
431             readRcFile("lyxrc");
432
433         // Read encodings
434         readEncodingsFile("encodings");
435         // Read languages
436         readLanguagesFile("languages");
437
438         // Load the layouts
439         lyxerr[Debug::INIT] << "Reading layouts..." << endl;
440         LyXSetStyle();
441
442         // Ensure that we have really read a bind file, so that LyX is
443         // usable.
444         lyxrc.readBindFileIfNeeded();
445
446         // Read menus
447         readUIFile(lyxrc.ui_file);
448
449         // Bind the X dead keys to the corresponding LyX functions if
450         // necessary.
451         if (lyxrc.override_x_deadkeys)
452                 deadKeyBindings(toplevel_keymap.get());
453
454         if (lyxerr.debugging(Debug::LYXRC)) {
455                 lyxrc.print();
456         }
457
458         // Create temp directory
459         os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
460         system_tempdir = os::getTmpDir();
461         if (lyxerr.debugging(Debug::INIT)) {
462                 lyxerr << "LyX tmp dir: `" << system_tempdir << '\'' << endl;
463         }
464
465         // load the lastfiles mini-database
466         lyxerr[Debug::INIT] << "Reading lastfiles `"
467                             << lyxrc.lastfiles << "'..." << endl;
468         lastfiles.reset(new LastFiles(lyxrc.lastfiles,
469                                       lyxrc.check_lastfiles,
470                                       lyxrc.num_lastfiles));
471 }
472
473
474 // These are the default bindings known to LyX
475 void LyX::defaultKeyBindings(kb_keymap  * kbmap)
476 {
477         kbmap->bind("Right", LFUN_RIGHT);
478         kbmap->bind("Left", LFUN_LEFT);
479         kbmap->bind("Up", LFUN_UP);
480         kbmap->bind("Down", LFUN_DOWN);
481
482         kbmap->bind("Tab", LFUN_TAB);
483         kbmap->bind("ISO_Left_Tab", LFUN_TAB); // jbl 2001-23-02
484
485         kbmap->bind("Home", LFUN_HOME);
486         kbmap->bind("End", LFUN_END);
487         kbmap->bind("Prior", LFUN_PRIOR);
488         kbmap->bind("Next", LFUN_NEXT);
489
490         kbmap->bind("Return", LFUN_BREAKPARAGRAPH);
491         //kbmap->bind("~C-~S-~M-nobreakspace", LFUN_PROTECTEDSPACE);
492
493         kbmap->bind("Delete", LFUN_DELETE);
494         kbmap->bind("BackSpace", LFUN_BACKSPACE);
495
496         // sub- and superscript -MV
497         kbmap->bind("~S-underscore", LFUN_SUBSCRIPT);
498         kbmap->bind("~S-asciicircum", LFUN_SUPERSCRIPT);
499
500         // kbmap->bindings to enable the use of the numeric keypad
501         // e.g. Num Lock set
502         //kbmap->bind("KP_0", LFUN_SELFINSERT);
503         //kbmap->bind("KP_Decimal", LFUN_SELFINSERT);
504         kbmap->bind("KP_Enter", LFUN_BREAKPARAGRAPH);
505         //kbmap->bind("KP_1", LFUN_SELFINSERT);
506         //kbmap->bind("KP_2", LFUN_SELFINSERT);
507         //kbmap->bind("KP_3", LFUN_SELFINSERT);
508         //kbmap->bind("KP_4", LFUN_SELFINSERT);
509         //kbmap->bind("KP_5", LFUN_SELFINSERT);
510         //kbmap->bind("KP_6", LFUN_SELFINSERT);
511         //kbmap->bind("KP_Add", LFUN_SELFINSERT);
512         //kbmap->bind("KP_7", LFUN_SELFINSERT);
513         //kbmap->bind("KP_8", LFUN_SELFINSERT);
514         //kbmap->bind("KP_9", LFUN_SELFINSERT);
515         //kbmap->bind("KP_Divide", LFUN_SELFINSERT);
516         //kbmap->bind("KP_Multiply", LFUN_SELFINSERT);
517         //kbmap->bind("KP_Subtract", LFUN_SELFINSERT);
518         kbmap->bind("KP_Right", LFUN_RIGHT);
519         kbmap->bind("KP_Left", LFUN_LEFT);
520         kbmap->bind("KP_Up", LFUN_UP);
521         kbmap->bind("KP_Down", LFUN_DOWN);
522         kbmap->bind("KP_Home", LFUN_HOME);
523         kbmap->bind("KP_End", LFUN_END);
524         kbmap->bind("KP_Prior", LFUN_PRIOR);
525         kbmap->bind("KP_Next", LFUN_NEXT);
526
527         kbmap->bind("C-Tab", LFUN_TABINSERT);  // ale970515
528         kbmap->bind("S-Tab", LFUN_SHIFT_TAB);  // jug20000522
529         kbmap->bind("S-ISO_Left_Tab", LFUN_SHIFT_TAB); // jbl 2001-23-02
530 }
531
532
533 void LyX::emergencyCleanup()
534 {
535         // what to do about tmpfiles is non-obvious. we would
536         // like to delete any we find, but our lyxdir might
537         // contain documents etc. which might be helpful on
538         // a crash
539
540         bufferlist.emergencyWriteAll();
541         if (lyxserver)
542                 lyxserver->emergencyCleanup();
543 }
544
545
546 // LyX can optionally take over the handling of deadkeys
547 void LyX::deadKeyBindings(kb_keymap * kbmap)
548 {
549         // bindKeyings for transparent handling of deadkeys
550         // The keysyms are gotten from XFree86 X11R6
551         kbmap->bind("~C-~S-~M-dead_acute", LFUN_ACUTE);
552         kbmap->bind("~C-~S-~M-dead_breve", LFUN_BREVE);
553         kbmap->bind("~C-~S-~M-dead_caron", LFUN_CARON);
554         kbmap->bind("~C-~S-~M-dead_cedilla", LFUN_CEDILLA);
555         kbmap->bind("~C-~S-~M-dead_abovering", LFUN_CIRCLE);
556         kbmap->bind("~C-~S-~M-dead_circumflex", LFUN_CIRCUMFLEX);
557         kbmap->bind("~C-~S-~M-dead_abovedot", LFUN_DOT);
558         kbmap->bind("~C-~S-~M-dead_grave", LFUN_GRAVE);
559         kbmap->bind("~C-~S-~M-dead_doubleacute", LFUN_HUNG_UMLAUT);
560         kbmap->bind("~C-~S-~M-dead_macron", LFUN_MACRON);
561         // nothing with this name
562         // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
563         kbmap->bind("~C-~S-~M-dead_tilde", LFUN_TILDE);
564         kbmap->bind("~C-~S-~M-dead_diaeresis", LFUN_UMLAUT);
565         // nothing with this name either...
566         //kbmap->bind("~C-~S-~M-dead_underbar", LFUN_UNDERBAR);
567         kbmap->bind("~C-~S-~M-dead_belowdot", LFUN_UNDERDOT);
568         kbmap->bind("~C-~S-~M-dead_tie", LFUN_TIE);
569         kbmap->bind("~C-~S-~M-dead_ogonek", LFUN_OGONEK);
570 }
571
572
573 // This one is not allowed to use anything on the main form, since that
574 // one does not exist yet. (Asger)
575 void LyX::queryUserLyXDir(bool explicit_userdir)
576 {
577         // Does user directory exist?
578         FileInfo fileInfo(user_lyxdir);
579         if (fileInfo.isOK() && fileInfo.isDir()) {
580                 first_start = false;
581                 return;
582         } else {
583                 first_start = !explicit_userdir;
584         }
585
586         // If the user specified explicitely a directory, ask whether
587         // to create it (otherwise, always create it)
588         if (explicit_userdir &&
589             !Alert::askQuestion(_("You have specified an invalid LyX directory."),
590                          _("It is needed to keep your own configuration."),
591                          _("Should I try to set it up for you (recommended)?"))) {
592                 lyxerr << _("Running without personal LyX directory.") << endl;
593                 // No, let's use $HOME instead.
594                 user_lyxdir = GetEnvPath("HOME");
595                 return;
596         }
597
598         // Tell the user what is going on
599         lyxerr << _("LyX: Creating directory ") << user_lyxdir
600                << _(" and running configure...") << endl;
601
602         // Create directory structure
603         if (!createDirectory(user_lyxdir, 0755)) {
604                 // Failed, let's use $HOME instead.
605                 user_lyxdir = GetEnvPath("HOME");
606                 lyxerr << _("Failed. Will use ") << user_lyxdir
607                        << _(" instead.") << endl;
608                 return;
609         }
610
611         // Run configure in user lyx directory
612         Path p(user_lyxdir);
613         ::system(AddName(system_lyxdir, "configure").c_str());
614         lyxerr << "LyX: " << _("Done!") << endl;
615 }
616
617
618 // Read the rc file `name'
619 bool LyX::readRcFile(string const & name)
620 {
621         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
622
623         string const lyxrc_path = LibFileSearch(string(), name);
624         if (!lyxrc_path.empty()) {
625                 lyxerr[Debug::INIT] << "Found " << name
626                                     << " in " << lyxrc_path << endl;
627                 if (lyxrc.read(lyxrc_path) < 0) {
628                         Alert::alert(_("LyX Warning!"),
629                                    _("Error while reading ") + lyxrc_path + ".",
630                                    _("Using built-in defaults."));
631                         return false;
632                 }
633                 return true;
634         } else {
635                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
636         }
637
638         return false;
639 }
640
641
642 // Read the ui file `name'
643 void LyX::readUIFile(string const & name)
644 {
645         enum Uitags {
646                 ui_menuset = 1,
647                 ui_toolbar,
648                 ui_last
649         };
650
651         struct keyword_item uitags[ui_last-1] = {
652                 { "menuset", ui_menuset },
653                 { "toolbar", ui_toolbar }
654         };
655
656         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
657
658         string const ui_path = LibFileSearch("ui", name, "ui");
659
660         if (ui_path.empty()) {
661                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
662                 menubackend.defaults();
663                 return;
664         }
665
666         lyxerr[Debug::INIT] << "Found " << name
667                             << " in " << ui_path << endl;
668         LyXLex lex(uitags, ui_last - 1);
669         lex.setFile(ui_path);
670         if (!lex.isOK()) {
671                 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
672                        << endl;
673         }
674
675         if (lyxerr.debugging(Debug::PARSER))
676                 lex.printTable(lyxerr);
677
678         while (lex.isOK()) {
679                 switch (lex.lex()) {
680                 case ui_menuset:
681                         menubackend.read(lex);
682                         break;
683
684                 case ui_toolbar:
685                         toolbardefaults.read(lex);
686                         break;
687
688                 default:
689                         if (!rtrim(lex.getString()).empty())
690                                 lex.printError("LyX::ReadUIFile: "
691                                                "Unknown menu tag: `$$Token'");
692                         break;
693                 }
694         }
695 }
696
697
698 // Read the languages file `name'
699 void LyX::readLanguagesFile(string const & name)
700 {
701         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
702
703         string const lang_path = LibFileSearch(string(), name);
704         if (lang_path.empty()) {
705                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
706                 languages.setDefaults();
707                 return;
708         }
709         languages.read(lang_path);
710 }
711
712
713 // Read the encodings file `name'
714 void LyX::readEncodingsFile(string const & name)
715 {
716         lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
717
718         string const enc_path = LibFileSearch(string(), name);
719         if (enc_path.empty()) {
720                 lyxerr[Debug::INIT] << "Could not find " << name << endl;
721                 return;
722         }
723         encodings.read(enc_path);
724 }
725
726
727 namespace {
728
729 // Set debugging level and report result to user
730 void setDebuggingLevel(string const & dbgLevel)
731 {
732         lyxerr << _("Setting debug level to ") <<  dbgLevel << endl;
733         lyxerr.level(Debug::value(dbgLevel));
734         Debug::showLevel(lyxerr, lyxerr.level());
735 }
736
737
738 // Give command line help
739 void commandLineHelp()
740 {
741         lyxerr <<
742                 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
743                   "Command line switches (case sensitive):\n"
744                   "\t-help              summarize LyX usage\n"
745                   "\t-userdir dir       try to set user directory to dir\n"
746                   "\t-sysdir dir        try to set system directory to dir\n"
747                   "\t-geometry WxH+X+Y  set geometry of the main window\n"
748                   "\t-dbg feature[,feature]...\n"
749                   "                  select the features to debug.\n"
750                   "                  Type `lyx -dbg' to see the list of features\n"
751                   "\t-x [--execute] command\n"
752                   "                  where command is a lyx command.\n"
753                   "\t-e [--export] fmt\n"
754                   "                  where fmt is the export format of choice.\n"
755                   "\t-i [--import] fmt file.xxx\n"
756                   "                  where fmt is the import format of choice\n"
757                   "                  and file.xxx is the file to be imported.\n"
758                   "\t-version        summarize version and build info\n"
759                   "Check the LyX man page for more details.") << endl;
760 }
761
762 // Give command line version information
763 void commandLineVersionInfo()
764 {
765         lyxerr << "LyX " << lyx_version
766                << " of " << lyx_release_date << endl;
767         lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
768
769         lyxerr << lyx_version_info << endl;
770 }
771
772
773 } // namespace anon
774
775
776 bool LyX::easyParse(int & argc, char * argv[])
777 {
778         bool gui = true;
779         int removeargs = 0; // used when options are read
780         for (int i = 1; i < argc; ++i) {
781                 string arg = argv[i];
782
783                 // Check for -dbg int
784                 if (arg == "-dbg") {
785                         if (i + 1 < argc) {
786                                 setDebuggingLevel(argv[i + 1]);
787                                 removeargs = 2;
788                         } else {
789                                 lyxerr << _("List of supported debug flags:")
790                                        << endl;
791                                 Debug::showTags(lyxerr);
792                                 exit(1);
793                         }
794                 }
795                 // Check for "-sysdir"
796                 else if (arg == "-sysdir") {
797                         if (i + 1 < argc) {
798                                 system_lyxdir = argv[i + 1];
799                                 removeargs = 2;
800                         } else {
801                                 lyxerr << _("Missing directory for -sysdir switch!")
802                                        << endl;
803                                 exit(1);
804                         }
805                 }
806                 // Check for "-userdir"
807                 else if (arg == "-userdir") {
808                         if (i + 1 < argc) {
809                                 user_lyxdir = argv[i + 1];
810                                 removeargs = 2;
811                         } else {
812                                 lyxerr << _("Missing directory for -userdir switch!")
813                                        << endl;
814                                 exit(1);
815                         }
816                 }
817                 // Check for --help or -help
818                 else if (arg == "--help" || arg == "-help") {
819                         commandLineHelp();
820                         exit(0);
821                 }
822                 // Check for --version or -version
823                 else if (arg == "--version" || arg == "-version") {
824                         commandLineVersionInfo();
825                         exit(0);
826                 }
827                 // FIXME: why is this commented out ?
828                 // Check for "-nw": No XWindows as for emacs this should
829                 // give a LyX that could be used in a terminal window.
830                 //else if (arg == "-nw") {
831                 //      gui = false;
832                 //}
833
834                 // Check for "-x": Execute commands
835                 else if (arg == "-x" || arg == "--execute") {
836                         if (i + 1 < argc) {
837                                 batch_command = string(argv[i + 1]);
838                                 removeargs = 2;
839                         }
840                         else
841                                 lyxerr << _("Missing command string after  -x switch!") << endl;
842
843                         // Argh. Setting gui to false segfaults..
844                         // FIXME: when ? how ?
845                         // gui = false;
846                 }
847
848                 else if (arg == "-e" || arg == "--export") {
849                         if (i + 1 < argc) {
850                                 string type(argv[i+1]);
851                                 removeargs = 2;
852                                 batch_command = "buffer-export " + type;
853                                 gui = false;
854                         } else {
855                                 lyxerr << _("Missing file type [eg latex, "
856                                             "ps...] after ")
857                                        << arg << _(" switch!") << endl;
858                                 exit(1);
859                         }
860                 }
861                 else if (arg == "-i" || arg == "--import") {
862                         if (i + 1 < argc) {
863                                 if (!argv[i+2]) {
864                                         lyxerr << _("Missing filename for --import") << endl;
865                                         exit(1);
866                                 }
867
868                                 string const file(argv[i+2]);
869                                 string const type(argv[i+1]);
870                                 removeargs = 3;
871
872                                 batch_command = "buffer-import " + type + " " + file;
873                                 lyxerr << "batch_command: "
874                                        << batch_command << endl;
875
876                         } else {
877                                 lyxerr << _("Missing type [eg latex, "
878                                             "ps...] after ")
879                                        << arg << _(" switch!") << endl;
880                                 exit(1);
881                         }
882                 }
883
884                 if (removeargs > 0) {
885                         // Now, remove used arguments by shifting
886                         // the following ones removeargs places down.
887                         argc -= removeargs;
888                         for (int j = i; j < argc; ++j)
889                                 argv[j] = argv[j + removeargs];
890                         --i; // After shift, check this number again.
891                         removeargs = 0;
892                 }
893
894         }
895
896         return gui;
897 }