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