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