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