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