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