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