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