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