3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
10 * \author André Pönitz
12 * Full author contact details are available in file CREDITS.
21 #include "buffer_funcs.h"
22 #include "bufferlist.h"
23 #include "converter.h"
26 #include "errorlist.h"
31 #include "lastfiles.h"
36 #include "lyxtextclasslist.h"
37 #include "lyxserver.h"
38 #include "MenuBackend.h"
39 #include "ToolbarBackend.h"
41 #include "frontends/Alert.h"
42 #include "frontends/lyx_gui.h"
44 #include "support/FileInfo.h"
45 #include "support/filetools.h"
46 #include "support/lyxlib.h"
47 #include "support/os.h"
48 #include "support/path.h"
49 #include "support/path_defines.h"
51 #include <boost/bind.hpp>
56 using lyx::support::AddName;
57 using lyx::support::AddPath;
58 using lyx::support::bformat;
59 using lyx::support::createDirectory;
60 using lyx::support::CreateLyXTmpDir;
61 using lyx::support::FileInfo;
62 using lyx::support::FileSearch;
63 using lyx::support::GetEnv;
64 using lyx::support::GetEnvPath;
65 using lyx::support::i18nLibFileSearch;
66 using lyx::support::LibFileSearch;
67 using lyx::support::Path;
68 using lyx::support::rtrim;
69 using lyx::support::setLyxPaths;
70 using lyx::support::system_lyxdir;
71 using lyx::support::user_lyxdir;
73 namespace os = lyx::support::os;
79 #ifndef CXX_GLOBAL_CSTD
86 extern void QuitLyX();
88 extern LyXServer * lyxserver;
90 boost::scoped_ptr<LastFiles> lastfiles;
92 // This is the global bufferlist object
93 BufferList bufferlist;
95 // convenient to have it here.
96 boost::scoped_ptr<kb_keymap> toplevel_keymap;
100 void showFileError(string const & error)
102 Alert::warning(_("Could not read configuration file"),
103 bformat(_("Error while reading the configuration file\n%1$s.\n"
104 "Please check your installation."), error));
110 LyX::LyX(int & argc, char * argv[])
112 // Here we need to parse the command line. At least
113 // we need to parse for "-dbg" and "-help"
114 bool const want_gui = easyParse(argc, argv);
116 // set the DisplayTranslator only once; should that be done here??
117 // if this should not be in this file, please also remove
118 // #include "graphics/GraphicsTypes.h" at the top -- Rob Lahaye.
119 lyx::graphics::setDisplayTranslator();
122 lyx_gui::parse_init(argc, argv);
124 // check for any spurious extra arguments
125 // other than documents
126 for (int argi = 1; argi < argc ; ++argi) {
127 if (argv[argi][0] == '-') {
128 lyxerr << bformat(_("Wrong command line option `%1$s'. Exiting."),
134 // Initialization of LyX (reads lyxrc and more)
135 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
137 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
140 lyx_gui::parse_lyxrc();
142 vector<string> files;
144 for (int argi = argc - 1; argi >= 1; --argi)
145 files.push_back(argv[argi]);
148 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
150 // Execute batch commands if available
151 if (!batch_command.empty()) {
153 lyxerr[Debug::INIT] << "About to handle -x '"
154 << batch_command << '\'' << endl;
156 Buffer * last_loaded = 0;
158 vector<string>::const_iterator it = files.begin();
159 vector<string>::const_iterator end = files.end();
161 for (; it != end; ++it) {
162 // get absolute path of file and add ".lyx" to
163 // the filename if necessary
164 string s = FileSearch(string(), *it, "lyx");
166 last_loaded = newFile(*it, string(), true);
168 Buffer * buf = bufferlist.newBuffer(s, false);
169 buf->error.connect(boost::bind(&LyX::printError, this, _1));
170 if (loadLyXFile(buf, s))
173 bufferlist.release(buf);
177 // try to dispatch to last loaded buffer first
179 bool success = false;
180 if (last_loaded->dispatch(batch_command, &success)) {
185 files.clear(); // the files are already loaded
188 lyx_gui::start(batch_command, files);
194 static void error_handler(int err_sig)
198 lyxerr << "\nlyx: SIGHUP signal caught" << endl;
204 lyxerr << "\nlyx: SIGFPE signal caught" << endl;
207 lyxerr << "\nlyx: SIGSEGV signal caught" << endl;
209 "Sorry, you have found a bug in LyX. "
210 "Please read the bug-reporting instructions "
211 "in Help->Introduction and send us a bug report, "
212 "if necessary. Thanks !" << endl;
218 // This will be received if lyx tries to write to a socket
219 // whose reading end was closed. It can safely be ignored,
220 // as in this case the ::write() system call will return -1
221 // and errno will be set to EPIPE
226 // Deinstall the signal handlers
227 signal(SIGHUP, SIG_DFL);
228 signal(SIGINT, SIG_DFL);
229 signal(SIGFPE, SIG_DFL);
230 signal(SIGSEGV, SIG_DFL);
231 signal(SIGTERM, SIG_DFL);
232 signal(SIGPIPE, SIG_DFL);
234 LyX::emergencyCleanup();
236 lyxerr << "Bye." << endl;
237 if (err_sig!= SIGHUP &&
238 (!GetEnv("LYXDEBUG").empty() || err_sig == SIGSEGV))
239 lyx::support::abort();
246 void LyX::printError(ErrorItem const & ei)
248 std::cerr << _("LyX: ") << ei.error
249 << ':' << ei.description << std::endl;
254 void LyX::init(bool gui)
256 signal(SIGHUP, error_handler);
257 signal(SIGFPE, error_handler);
258 signal(SIGSEGV, error_handler);
259 signal(SIGINT, error_handler);
260 signal(SIGTERM, error_handler);
261 signal(SIGPIPE, error_handler);
263 bool const explicit_userdir = setLyxPaths();
265 // Check that user LyX directory is ok. We don't do that if
266 // running in batch mode.
268 queryUserLyXDir(explicit_userdir);
273 // Disable gui when easyparse says so
274 lyx_gui::use_gui = gui;
276 if (lyxrc.template_path.empty()) {
277 lyxrc.template_path = AddPath(system_lyxdir(), "templates");
280 if (lyxrc.lastfiles.empty()) {
281 lyxrc.lastfiles = AddName(user_lyxdir(), "lastfiles");
284 if (lyxrc.roman_font_name.empty())
285 lyxrc.roman_font_name = lyx_gui::roman_font_name();
286 if (lyxrc.sans_font_name.empty())
287 lyxrc.sans_font_name = lyx_gui::sans_font_name();
288 if (lyxrc.typewriter_font_name.empty())
289 lyxrc.typewriter_font_name = lyx_gui::typewriter_font_name();
292 // Read configuration files
295 readRcFile("lyxrc.defaults");
296 system_lyxrc = lyxrc;
297 system_formats = formats;
298 system_converters = converters;
299 system_lcolor = lcolor;
301 string prefsfile = "preferences";
302 // back compatibility to lyxs < 1.1.6
303 if (LibFileSearch(string(), prefsfile).empty())
305 if (!LibFileSearch(string(), prefsfile).empty())
306 readRcFile(prefsfile);
308 readEncodingsFile("encodings");
309 readLanguagesFile("languages");
312 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
317 toplevel_keymap.reset(new kb_keymap);
318 defaultKeyBindings(toplevel_keymap.get());
319 toplevel_keymap->read(lyxrc.bind_file);
322 readUIFile(lyxrc.ui_file);
325 if (lyxerr.debugging(Debug::LYXRC))
328 os::setTmpDir(CreateLyXTmpDir(lyxrc.tempdir_path));
329 if (lyxerr.debugging(Debug::INIT)) {
330 lyxerr << "LyX tmp dir: `" << os::getTmpDir() << '\'' << endl;
333 lyxerr[Debug::INIT] << "Reading lastfiles `"
334 << lyxrc.lastfiles << "'..." << endl;
335 lastfiles.reset(new LastFiles(lyxrc.lastfiles,
336 lyxrc.check_lastfiles,
337 lyxrc.num_lastfiles));
341 void LyX::defaultKeyBindings(kb_keymap * kbmap)
343 kbmap->bind("Right", FuncRequest(LFUN_RIGHT));
344 kbmap->bind("Left", FuncRequest(LFUN_LEFT));
345 kbmap->bind("Up", FuncRequest(LFUN_UP));
346 kbmap->bind("Down", FuncRequest(LFUN_DOWN));
348 kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
349 kbmap->bind("ISO_Left_Tab", FuncRequest(LFUN_CELL_FORWARD));
351 kbmap->bind("Home", FuncRequest(LFUN_HOME));
352 kbmap->bind("End", FuncRequest(LFUN_END));
353 kbmap->bind("Prior", FuncRequest(LFUN_PRIOR));
354 kbmap->bind("Next", FuncRequest(LFUN_NEXT));
356 kbmap->bind("Return", FuncRequest(LFUN_BREAKPARAGRAPH));
357 //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
359 kbmap->bind("Delete", FuncRequest(LFUN_DELETE));
360 kbmap->bind("BackSpace", FuncRequest(LFUN_BACKSPACE));
362 // sub- and superscript -MV
363 kbmap->bind("~S-underscore", FuncRequest(LFUN_SUBSCRIPT));
364 kbmap->bind("~S-asciicircum", FuncRequest(LFUN_SUPERSCRIPT));
366 // kbmap->bindings to enable the use of the numeric keypad
368 //kbmap->bind("KP_0", FuncRequest(LFUN_SELFINSERT));
369 //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELFINSERT));
370 kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAKPARAGRAPH));
371 //kbmap->bind("KP_1", FuncRequest(LFUN_SELFINSERT));
372 //kbmap->bind("KP_2", FuncRequest(LFUN_SELFINSERT));
373 //kbmap->bind("KP_3", FuncRequest(LFUN_SELFINSERT));
374 //kbmap->bind("KP_4", FuncRequest(LFUN_SELFINSERT));
375 //kbmap->bind("KP_5", FuncRequest(LFUN_SELFINSERT));
376 //kbmap->bind("KP_6", FuncRequest(LFUN_SELFINSERT));
377 //kbmap->bind("KP_Add", FuncRequest(LFUN_SELFINSERT));
378 //kbmap->bind("KP_7", FuncRequest(LFUN_SELFINSERT));
379 //kbmap->bind("KP_8", FuncRequest(LFUN_SELFINSERT));
380 //kbmap->bind("KP_9", FuncRequest(LFUN_SELFINSERT));
381 //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELFINSERT));
382 //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELFINSERT));
383 //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELFINSERT));
384 kbmap->bind("KP_Right", FuncRequest(LFUN_RIGHT));
385 kbmap->bind("KP_Left", FuncRequest(LFUN_LEFT));
386 kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
387 kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
388 kbmap->bind("KP_Home", FuncRequest(LFUN_HOME));
389 kbmap->bind("KP_End", FuncRequest(LFUN_END));
390 kbmap->bind("KP_Prior", FuncRequest(LFUN_PRIOR));
391 kbmap->bind("KP_Next", FuncRequest(LFUN_NEXT));
393 kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
394 kbmap->bind("S-Tab", FuncRequest(LFUN_CELL_BACKWARD));
395 kbmap->bind("S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
399 void LyX::emergencyCleanup()
401 // what to do about tmpfiles is non-obvious. we would
402 // like to delete any we find, but our lyxdir might
403 // contain documents etc. which might be helpful on
406 bufferlist.emergencyWriteAll();
408 lyxserver->emergencyCleanup();
412 void LyX::deadKeyBindings(kb_keymap * kbmap)
414 // bindKeyings for transparent handling of deadkeys
415 // The keysyms are gotten from XFree86 X11R6
416 kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACUTE));
417 kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_BREVE));
418 kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_CARON));
419 kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_CEDILLA));
420 kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_CIRCLE));
421 kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_CIRCUMFLEX));
422 kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_DOT));
423 kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_GRAVE));
424 kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_HUNG_UMLAUT));
425 kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_MACRON));
426 // nothing with this name
427 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_SPECIAL_CARON);
428 kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_TILDE));
429 kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_UMLAUT));
430 // nothing with this name either...
431 //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_UNDERBAR));
432 kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_UNDERDOT));
433 kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_TIE));
434 kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_OGONEK));
438 void LyX::queryUserLyXDir(bool explicit_userdir)
440 string const configure_script = AddName(system_lyxdir(), "configure");
442 // Does user directory exist?
443 FileInfo fileInfo(user_lyxdir());
444 if (fileInfo.isOK() && fileInfo.isDir()) {
446 FileInfo script(configure_script);
447 FileInfo defaults(AddName(user_lyxdir(), "lyxrc.defaults"));
448 if (defaults.isOK() && script.isOK()
449 && defaults.getModificationTime() < script.getModificationTime()) {
450 lyxerr << _("LyX: reconfiguring user directory")
452 Path p(user_lyxdir());
453 ::system(configure_script.c_str());
454 lyxerr << "LyX: " << _("Done!") << endl;
459 first_start = !explicit_userdir;
461 lyxerr << bformat(_("LyX: Creating directory %1$s"
462 " and running configure..."), user_lyxdir()) << endl;
464 if (!createDirectory(user_lyxdir(), 0755)) {
465 // Failed, let's use $HOME instead.
466 user_lyxdir(GetEnvPath("HOME"));
467 lyxerr << bformat(_("Failed. Will use %1$s instead."),
468 user_lyxdir()) << endl;
472 // Run configure in user lyx directory
473 Path p(user_lyxdir());
474 ::system(configure_script.c_str());
475 lyxerr << "LyX: " << _("Done!") << endl;
479 void LyX::readRcFile(string const & name)
481 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
483 string const lyxrc_path = LibFileSearch(string(), name);
484 if (!lyxrc_path.empty()) {
486 lyxerr[Debug::INIT] << "Found " << name
487 << " in " << lyxrc_path << endl;
489 if (lyxrc.read(lyxrc_path) >= 0)
497 // Read the ui file `name'
498 void LyX::readUIFile(string const & name)
508 struct keyword_item uitags[ui_last - 1] = {
509 { "include", ui_include },
510 { "menuset", ui_menuset },
511 { "toolbar", ui_toolbar },
512 { "toolbars", ui_toolbars }
515 // Ensure that a file is read only once (prevents include loops)
516 static std::list<string> uifiles;
517 std::list<string>::const_iterator it = uifiles.begin();
518 std::list<string>::const_iterator end = uifiles.end();
519 it = std::find(it, end, name);
521 lyxerr[Debug::INIT] << "UI file '" << name
522 << "' has been read already. "
523 << "Is this an include loop?"
528 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
530 string const ui_path = LibFileSearch("ui", name, "ui");
532 if (ui_path.empty()) {
533 lyxerr[Debug::INIT] << "Could not find " << name << endl;
537 uifiles.push_back(name);
539 lyxerr[Debug::INIT] << "Found " << name
540 << " in " << ui_path << endl;
541 LyXLex lex(uitags, ui_last - 1);
542 lex.setFile(ui_path);
544 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
548 if (lyxerr.debugging(Debug::PARSER))
549 lex.printTable(lyxerr);
555 string const file = lex.getString();
560 menubackend.read(lex);
564 toolbarbackend.read(lex);
568 toolbarbackend.readToolbars(lex);
572 if (!rtrim(lex.getString()).empty())
573 lex.printError("LyX::ReadUIFile: "
574 "Unknown menu tag: `$$Token'");
581 // Read the languages file `name'
582 void LyX::readLanguagesFile(string const & name)
584 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
586 string const lang_path = LibFileSearch(string(), name);
587 if (lang_path.empty()) {
591 languages.read(lang_path);
595 // Read the encodings file `name'
596 void LyX::readEncodingsFile(string const & name)
598 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
600 string const enc_path = LibFileSearch(string(), name);
601 if (enc_path.empty()) {
605 encodings.read(enc_path);
614 /// return the the number of arguments consumed
615 typedef boost::function<int(string const &, string const &)> cmd_helper;
617 int parse_dbg(string const & arg, string const &)
620 lyxerr << _("List of supported debug flags:") << endl;
621 Debug::showTags(lyxerr);
624 lyxerr << bformat(_("Setting debug level to %1$s"), arg) << endl;
626 lyxerr.level(Debug::value(arg));
627 Debug::showLevel(lyxerr, lyxerr.level());
632 int parse_help(string const &, string const &)
635 _("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
636 "Command line switches (case sensitive):\n"
637 "\t-help summarize LyX usage\n"
638 "\t-userdir dir try to set user directory to dir\n"
639 "\t-sysdir dir try to set system directory to dir\n"
640 "\t-geometry WxH+X+Y set geometry of the main window\n"
641 "\t-dbg feature[,feature]...\n"
642 " select the features to debug.\n"
643 " Type `lyx -dbg' to see the list of features\n"
644 "\t-x [--execute] command\n"
645 " where command is a lyx command.\n"
646 "\t-e [--export] fmt\n"
647 " where fmt is the export format of choice.\n"
648 "\t-i [--import] fmt file.xxx\n"
649 " where fmt is the import format of choice\n"
650 " and file.xxx is the file to be imported.\n"
651 "\t-version summarize version and build info\n"
652 "Check the LyX man page for more details.") << endl;
657 int parse_version(string const &, string const &)
659 lyxerr << "LyX " << lyx_version
660 << " of " << lyx_release_date << endl;
661 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
663 lyxerr << lyx_version_info << endl;
668 int parse_sysdir(string const & arg, string const &)
671 lyxerr << _("Missing directory for -sysdir switch") << endl;
678 int parse_userdir(string const & arg, string const &)
681 lyxerr << _("Missing directory for -userdir switch") << endl;
688 int parse_execute(string const & arg, string const &)
691 lyxerr << _("Missing command string after --execute switch") << endl;
695 // Argh. Setting gui to false segfaults..
696 // FIXME: when ? how ?
701 int parse_export(string const & type, string const &)
704 lyxerr << _("Missing file type [eg latex, ps...] after "
705 "--export switch") << endl;
708 batch = "buffer-export " + type;
713 int parse_import(string const & type, string const & file)
716 lyxerr << _("Missing file type [eg latex, ps...] after "
717 "--import switch") << endl;
721 lyxerr << _("Missing filename for --import") << endl;
725 batch = "buffer-import " + type + ' ' + file;
732 bool LyX::easyParse(int & argc, char * argv[])
734 std::map<string, cmd_helper> cmdmap;
736 cmdmap["-dbg"] = parse_dbg;
737 cmdmap["-help"] = parse_help;
738 cmdmap["--help"] = parse_help;
739 cmdmap["-version"] = parse_version;
740 cmdmap["--version"] = parse_version;
741 cmdmap["-sysdir"] = parse_sysdir;
742 cmdmap["-userdir"] = parse_userdir;
743 cmdmap["-x"] = parse_execute;
744 cmdmap["--execute"] = parse_execute;
745 cmdmap["-e"] = parse_export;
746 cmdmap["--export"] = parse_export;
747 cmdmap["-i"] = parse_import;
748 cmdmap["--import"] = parse_import;
750 for (int i = 1; i < argc; ++i) {
751 std::map<string, cmd_helper>::const_iterator it
752 = cmdmap.find(argv[i]);
754 // don't complain if not found - may be parsed later
755 if (it == cmdmap.end())
758 string arg((i + 1 < argc) ? argv[i + 1] : "");
759 string arg2((i + 2 < argc) ? argv[i + 2] : "");
761 int const remove = 1 + it->second(arg, arg2);
763 // Now, remove used arguments by shifting
764 // the following ones remove places down.
766 for (int j = i; j < argc; ++j)
767 argv[j] = argv[j + remove];
771 batch_command = batch;