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