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"
34 #include "LyXAction.h"
38 #include "lyxserver.h"
39 #include "lyxsocket.h"
40 #include "lyxtextclasslist.h"
41 #include "MenuBackend.h"
43 #include "ToolbarBackend.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Application.h"
47 #include "frontends/LyXView.h"
49 #include "support/environment.h"
50 #include "support/filetools.h"
51 #include "support/fontutils.h"
52 #include "support/lyxlib.h"
53 #include "support/convert.h"
54 #include "support/os.h"
55 #include "support/package.h"
56 #include "support/path.h"
57 #include "support/systemcall.h"
59 #include <boost/bind.hpp>
60 #include <boost/filesystem/operations.hpp>
68 using support::addName;
69 using support::addPath;
70 using support::bformat;
71 using support::createDirectory;
72 using support::createLyXTmpDir;
73 using support::destroyDir;
74 using support::fileSearch;
75 using support::getEnv;
76 using support::i18nLibFileSearch;
77 using support::libFileSearch;
78 using support::package;
79 using support::prependEnvPath;
81 using support::Systemcall;
83 namespace Alert = frontend::Alert;
84 namespace os = support::os;
85 namespace fs = boost::filesystem;
92 #ifndef CXX_GLOBAL_CSTD
99 frontend::Application * theApp = 0;
101 /// are we using the GUI at all?
103 * We default to true and this is changed to false when the export feature is used.
110 // Filled with the command line arguments "foo" of "-sysdir foo" or
112 string cl_system_support;
113 string cl_user_support;
116 void showFileError(string const & error)
118 Alert::warning(_("Could not read configuration file"),
119 bformat(_("Error while reading the configuration file\n%1$s.\n"
120 "Please check your installation."), from_utf8(error)));
124 void reconfigureUserLyXDir()
126 string const configure_command = package().configure_command();
128 lyxerr << to_utf8(_("LyX: reconfiguring user directory")) << endl;
129 support::Path p(package().user_support());
131 one.startscript(Systemcall::Wait, configure_command);
132 lyxerr << "LyX: " << to_utf8(_("Done!")) << endl;
138 /// The main application class private implementation.
139 struct LyX::Singletons
141 /// our function handler
144 BufferList buffer_list_;
146 boost::scoped_ptr<kb_keymap> toplevel_keymap_;
148 boost::scoped_ptr<LyXServer> lyx_server_;
150 boost::scoped_ptr<LyXServerSocket> lyx_socket_;
152 boost::scoped_ptr<frontend::Application> application_;
153 /// lyx session, containing lastfiles, lastfilepos, and lastopened
154 boost::scoped_ptr<Session> session_;
158 boost::scoped_ptr<LyX> LyX::singleton_;
161 int LyX::exec(int & argc, char * argv[])
163 BOOST_ASSERT(!singleton_.get());
164 // We must return from this before launching the gui so that
165 // other parts of the code can access singleton_ through
166 // LyX::ref and LyX::cref.
167 singleton_.reset(new LyX);
168 // Start the real execution loop.
169 return singleton_->priv_exec(argc, argv);
175 BOOST_ASSERT(singleton_.get());
176 return *singleton_.get();
180 LyX const & LyX::cref()
182 BOOST_ASSERT(singleton_.get());
183 return *singleton_.get();
188 : first_start(false), geometryOption_(false)
190 pimpl_.reset(new Singletons);
194 BufferList & LyX::bufferList()
196 return pimpl_->buffer_list_;
200 BufferList const & LyX::bufferList() const
202 return pimpl_->buffer_list_;
206 Session & LyX::session()
208 BOOST_ASSERT(pimpl_->session_.get());
209 return *pimpl_->session_.get();
213 Session const & LyX::session() const
215 BOOST_ASSERT(pimpl_->session_.get());
216 return *pimpl_->session_.get();
220 LyXFunc & LyX::lyxFunc()
222 return pimpl_->lyxfunc_;
226 LyXFunc const & LyX::lyxFunc() const
228 return pimpl_->lyxfunc_;
232 LyXServer & LyX::server()
234 BOOST_ASSERT(pimpl_->lyx_server_.get());
235 return *pimpl_->lyx_server_.get();
239 LyXServer const & LyX::server() const
241 BOOST_ASSERT(pimpl_->lyx_server_.get());
242 return *pimpl_->lyx_server_.get();
246 LyXServerSocket & LyX::socket()
248 BOOST_ASSERT(pimpl_->lyx_socket_.get());
249 return *pimpl_->lyx_socket_.get();
253 LyXServerSocket const & LyX::socket() const
255 BOOST_ASSERT(pimpl_->lyx_socket_.get());
256 return *pimpl_->lyx_socket_.get();
260 frontend::Application & LyX::application()
262 BOOST_ASSERT(pimpl_->application_.get());
263 return *pimpl_->application_.get();
267 frontend::Application const & LyX::application() const
269 BOOST_ASSERT(pimpl_->application_.get());
270 return *pimpl_->application_.get();
274 kb_keymap & LyX::topLevelKeymap()
276 BOOST_ASSERT(pimpl_->toplevel_keymap_.get());
277 return *pimpl_->toplevel_keymap_.get();
281 kb_keymap const & LyX::topLevelKeymap() const
283 BOOST_ASSERT(pimpl_->toplevel_keymap_.get());
284 return *pimpl_->toplevel_keymap_.get();
287 void LyX::addLyXView(LyXView * lyxview)
289 views_.push_back(lyxview);
293 Buffer const * const LyX::updateInset(InsetBase const * inset) const
298 Buffer const * buffer_ptr = 0;
299 ViewList::const_iterator it = views_.begin();
300 ViewList::const_iterator const end = views_.end();
301 for (; it != end; ++it) {
302 Buffer const * ptr = (*it)->updateInset(inset);
310 int LyX::priv_exec(int & argc, char * argv[])
312 // Here we need to parse the command line. At least
313 // we need to parse for "-dbg" and "-help"
314 easyParse(argc, argv);
316 support::init_package(argv[0], cl_system_support, cl_user_support,
317 support::top_build_dir_is_one_level_up);
319 vector<string> files;
320 int exit_status = execBatchCommands(argc, argv, files);
326 // Force adding of font path _before_ Application is initialized
327 support::addFontResources();
328 pimpl_->application_.reset(createApplication(argc, argv));
330 // FIXME: this global pointer should probably go.
331 theApp = pimpl_->application_.get();
332 restoreGuiSession(files);
333 // Start the real execution loop.
336 /* Create a CoreApplication class that will provide the main event loop and
337 * the socket callback registering. With Qt4, only QtCore library would be needed.
338 * When this done, a server_mode could be created and the following two
339 * line would be moved out from here.
341 pimpl_->lyx_server_.reset(new LyXServer(&pimpl_->lyxfunc_, lyxrc.lyxpipes));
342 pimpl_->lyx_socket_.reset(new LyXServerSocket(&pimpl_->lyxfunc_,
343 support::os::internal_path(package().temp_dir() + "/lyxsocket")));
345 // handle the batch commands the user asked for
346 if (!batch_command.empty()) {
347 pimpl_->lyxfunc_.dispatch(lyxaction.lookupFunc(batch_command));
350 exit_status = pimpl_->application_->start(batch_command);
351 // Kill the application object before exiting. This avoid crash
353 pimpl_->application_.reset();
354 // Restore original font resources after Application is destroyed.
355 support::restoreFontResources();
358 // FIXME: create a ConsoleApplication
366 void LyX::prepareExit()
368 // Set a flag that we do quitting from the program,
369 // so no refreshes are necessary.
372 // close buffers first
373 pimpl_->buffer_list_.closeAll();
375 // do any other cleanup procedures now
376 lyxerr[Debug::INFO] << "Deleting tmp dir " << package().temp_dir() << endl;
378 if (!destroyDir(package().temp_dir())) {
379 docstring const msg =
380 bformat(_("Unable to remove the temporary directory %1$s"),
381 from_utf8(package().temp_dir()));
382 Alert::warning(_("Unable to remove temporary directory"), msg);
387 void LyX::earlyExit(int status)
389 BOOST_ASSERT(pimpl_->application_.get());
390 // LyX::pimpl_::application_ is not initialised at this
391 // point so it's safe to just exit after some cleanup.
397 void LyX::quit(bool noask)
399 lyxerr[Debug::INFO] << "Running QuitLyX." << endl;
402 if (!noask && !pimpl_->buffer_list_.quitWriteAll())
405 pimpl_->session_->writeFile();
411 pimpl_->lyx_server_.reset();
412 pimpl_->lyx_socket_.reset();
413 pimpl_->application_->exit(0);
418 int LyX::execBatchCommands(int & argc, char * argv[],
419 vector<string> & files)
421 // check for any spurious extra arguments
422 // other than documents
423 for (int argi = 1; argi < argc ; ++argi) {
424 if (argv[argi][0] == '-') {
426 bformat(_("Wrong command line option `%1$s'. Exiting."),
427 from_utf8(argv[argi]))) << endl;
432 // Initialization of LyX (reads lyxrc and more)
433 lyxerr[Debug::INIT] << "Initializing LyX::init..." << endl;
434 bool success = init();
435 lyxerr[Debug::INIT] << "Initializing LyX::init...done" << endl;
439 for (int argi = argc - 1; argi >= 1; --argi)
440 files.push_back(os::internal_path(argv[argi]));
443 files.push_back(i18nLibFileSearch("examples", "splash.lyx"));
445 // Execute batch commands if available
446 if (!batch_command.empty()) {
448 lyxerr[Debug::INIT] << "About to handle -x '"
449 << batch_command << '\'' << endl;
451 Buffer * last_loaded = 0;
453 vector<string>::const_iterator it = files.begin();
454 vector<string>::const_iterator end = files.end();
456 for (; it != end; ++it) {
457 // get absolute path of file and add ".lyx" to
458 // the filename if necessary
459 string s = fileSearch(string(), *it, "lyx");
461 Buffer * const b = newFile(*it, string(), true);
465 Buffer * buf = pimpl_->buffer_list_.newBuffer(s, false);
466 if (loadLyXFile(buf, s)) {
468 ErrorList const & el = buf->errorList("Parse");
470 for_each(el.begin(), el.end(),
471 boost::bind(&LyX::printError, this, _1));
474 pimpl_->buffer_list_.release(buf);
478 // try to dispatch to last loaded buffer first
481 if (last_loaded->dispatch(batch_command, &success)) {
486 files.clear(); // the files are already loaded
493 void LyX::restoreGuiSession(vector<string> const & files)
495 // determine windows size and position, from lyxrc and/or session
497 unsigned int width = 690;
498 unsigned int height = 510;
499 bool maximize = false;
501 if (lyxrc.geometry_width != 0 && lyxrc.geometry_height != 0 ) {
502 width = lyxrc.geometry_width;
503 height = lyxrc.geometry_height;
505 // if lyxrc returns (0,0), then use session info
507 string val = session().loadSessionInfo("WindowWidth");
509 width = convert<unsigned int>(val);
510 val = session().loadSessionInfo("WindowHeight");
512 height = convert<unsigned int>(val);
513 if (session().loadSessionInfo("WindowIsMaximized") == "yes")
516 // if user wants to restore window position
519 if (lyxrc.geometry_xysaved) {
520 string val = session().loadSessionInfo("WindowPosX");
522 posx = convert<int>(val);
523 val = session().loadSessionInfo("WindowPosY");
525 posy = convert<int>(val);
528 if (geometryOption_) {
532 // create the main window
533 LyXView * view = &pimpl_->application_->createView(width, height, posx, posy, maximize);
534 ref().addLyXView(view);
537 for_each(files.begin(), files.end(),
538 bind(&LyXView::loadLyXFile, view, _1, true));
540 // if a file is specified, I assume that user wants to edit *that* file
541 if (files.empty() && lyxrc.load_session) {
542 vector<string> const & lastopened = pimpl_->session_->lastOpenedFiles();
543 // do not add to the lastfile list since these files are restored from
544 // last seesion, and should be already there (regular files), or should
545 // not be added at all (help files).
546 for_each(lastopened.begin(), lastopened.end(),
547 bind(&LyXView::loadLyXFile, view, _1, false));
549 // clear this list to save a few bytes of RAM
550 pimpl_->session_->clearLastOpenedFiles();
557 The SIGHUP signal does not exist on Windows and does not need to be handled.
559 Windows handles SIGFPE and SIGSEGV signals as expected.
561 Cntl+C interrupts (mapped to SIGINT by Windows' POSIX compatability layer)
562 cause a new thread to be spawned. This may well result in unexpected
563 behaviour by the single-threaded LyX.
565 SIGTERM signals will come only from another process actually sending
566 that signal using 'raise' in Windows' POSIX compatability layer. It will
567 not come from the general "terminate process" methods that everyone
568 actually uses (and which can't be trapped). Killing an app 'politely' on
569 Windows involves first sending a WM_CLOSE message, something that is
570 caught already by the Qt frontend.
572 For more information see:
574 http://aspn.activestate.com/ASPN/Mail/Message/ActiveTcl/2034055
575 ...signals are mostly useless on Windows for a variety of reasons that are
578 'UNIX Application Migration Guide, Chapter 9'
579 http://msdn.microsoft.com/library/en-us/dnucmg/html/UCMGch09.asp
581 'How To Terminate an Application "Cleanly" in Win32'
582 http://support.microsoft.com/default.aspx?scid=kb;en-us;178893
586 static void error_handler(int err_sig)
588 // Throw away any signals other than the first one received.
589 static sig_atomic_t handling_error = false;
592 handling_error = true;
594 // We have received a signal indicating a fatal error, so
595 // try and save the data ASAP.
596 LyX::cref().emergencyCleanup();
598 // These lyxerr calls may or may not work:
600 // Signals are asynchronous, so the main program may be in a very
601 // fragile state when a signal is processed and thus while a signal
602 // handler function executes.
603 // In general, therefore, we should avoid performing any
604 // I/O operations or calling most library and system functions from
607 // This shouldn't matter here, however, as we've already invoked
612 lyxerr << "\nlyx: SIGHUP signal caught\nBye." << endl;
616 lyxerr << "\nlyx: SIGFPE signal caught\nBye." << endl;
619 lyxerr << "\nlyx: SIGSEGV signal caught\n"
620 "Sorry, you have found a bug in LyX. "
621 "Please read the bug-reporting instructions "
622 "in Help->Introduction and send us a bug report, "
623 "if necessary. Thanks !\nBye." << endl;
631 // Deinstall the signal handlers
633 signal(SIGHUP, SIG_DFL);
635 signal(SIGINT, SIG_DFL);
636 signal(SIGFPE, SIG_DFL);
637 signal(SIGSEGV, SIG_DFL);
638 signal(SIGTERM, SIG_DFL);
641 if (err_sig == SIGSEGV ||
642 (err_sig != SIGHUP && !getEnv("LYXDEBUG").empty()))
644 if (err_sig == SIGSEGV || !getEnv("LYXDEBUG").empty())
653 void LyX::printError(ErrorItem const & ei)
655 docstring tmp = _("LyX: ") + ei.error + char_type(':')
657 std::cerr << to_utf8(tmp) << std::endl;
661 void LyX::initGuiFont()
663 if (lyxrc.roman_font_name.empty())
664 lyxrc.roman_font_name = pimpl_->application_->romanFontName();
666 if (lyxrc.sans_font_name.empty())
667 lyxrc.sans_font_name = pimpl_->application_->sansFontName();
669 if (lyxrc.typewriter_font_name.empty())
670 lyxrc.typewriter_font_name
671 = pimpl_->application_->typewriterFontName();
678 signal(SIGHUP, error_handler);
680 signal(SIGFPE, error_handler);
681 signal(SIGSEGV, error_handler);
682 signal(SIGINT, error_handler);
683 signal(SIGTERM, error_handler);
684 // SIGPIPE can be safely ignored.
686 lyxrc.tempdir_path = package().temp_dir();
687 lyxrc.document_path = package().document_dir();
689 if (lyxrc.template_path.empty()) {
690 lyxrc.template_path = addPath(package().system_support(),
695 // Read configuration files
698 // This one may have been distributed along with LyX.
699 if (!readRcFile("lyxrc.dist"))
702 // Set the PATH correctly.
703 #if !defined (USE_POSIX_PACKAGING)
704 // Add the directory containing the LyX executable to the path
705 // so that LyX can find things like tex2lyx.
706 if (package().build_support().empty())
707 prependEnvPath("PATH", package().binary_dir());
709 if (!lyxrc.path_prefix.empty())
710 prependEnvPath("PATH", lyxrc.path_prefix);
712 // Check that user LyX directory is ok.
713 if (queryUserLyXDir(package().explicit_user_support()))
714 reconfigureUserLyXDir();
716 // no need for a splash when there is no GUI
721 // This one is generated in user_support directory by lib/configure.py.
722 if (!readRcFile("lyxrc.defaults"))
725 // Query the OS to know what formats are viewed natively
726 formats.setAutoOpen();
728 system_lyxrc = lyxrc;
729 system_formats = formats;
730 system_converters = converters;
731 system_movers = movers;
732 system_lcolor = lcolor;
734 // This one is edited through the preferences dialog.
735 if (!readRcFile("preferences"))
738 if (!readEncodingsFile("encodings"))
740 if (!readLanguagesFile("languages"))
744 lyxerr[Debug::INIT] << "Reading layouts..." << endl;
750 pimpl_->toplevel_keymap_.reset(new kb_keymap);
751 defaultKeyBindings(pimpl_->toplevel_keymap_.get());
752 pimpl_->toplevel_keymap_->read(lyxrc.bind_file);
754 pimpl_->lyxfunc_.initKeySequences(pimpl_->toplevel_keymap_.get());
757 if (!readUIFile(lyxrc.ui_file))
761 if (lyxerr.debugging(Debug::LYXRC))
764 os::windows_style_tex_paths(lyxrc.windows_style_tex_paths);
765 if (!lyxrc.path_prefix.empty())
766 prependEnvPath("PATH", lyxrc.path_prefix);
768 if (fs::exists(lyxrc.document_path) &&
769 fs::is_directory(lyxrc.document_path))
770 package().document_dir() = lyxrc.document_path;
772 package().temp_dir() = createLyXTmpDir(lyxrc.tempdir_path);
773 if (package().temp_dir().empty()) {
774 Alert::error(_("Could not create temporary directory"),
775 bformat(_("Could not create a temporary directory in\n"
776 "%1$s. Make sure that this\n"
777 "path exists and is writable and try again."),
778 from_utf8(lyxrc.tempdir_path)));
779 // createLyXTmpDir() tries sufficiently hard to create a
780 // usable temp dir, so the probability to come here is
781 // close to zero. We therefore don't try to overcome this
782 // problem with e.g. asking the user for a new path and
783 // trying again but simply exit.
787 if (lyxerr.debugging(Debug::INIT)) {
788 lyxerr << "LyX tmp dir: `" << package().temp_dir() << '\'' << endl;
791 lyxerr[Debug::INIT] << "Reading session information '.lyx/session'..." << endl;
792 pimpl_->session_.reset(new Session(lyxrc.num_lastfiles));
797 void LyX::defaultKeyBindings(kb_keymap * kbmap)
799 kbmap->bind("Right", FuncRequest(LFUN_CHAR_FORWARD));
800 kbmap->bind("Left", FuncRequest(LFUN_CHAR_BACKWARD));
801 kbmap->bind("Up", FuncRequest(LFUN_UP));
802 kbmap->bind("Down", FuncRequest(LFUN_DOWN));
804 kbmap->bind("Tab", FuncRequest(LFUN_CELL_FORWARD));
805 kbmap->bind("C-Tab", FuncRequest(LFUN_CELL_SPLIT));
806 kbmap->bind("~S-ISO_Left_Tab", FuncRequest(LFUN_CELL_BACKWARD));
807 kbmap->bind("~S-BackTab", FuncRequest(LFUN_CELL_BACKWARD));
809 kbmap->bind("Home", FuncRequest(LFUN_LINE_BEGIN));
810 kbmap->bind("End", FuncRequest(LFUN_LINE_END));
811 kbmap->bind("Prior", FuncRequest(LFUN_SCREEN_UP));
812 kbmap->bind("Next", FuncRequest(LFUN_SCREEN_DOWN));
814 kbmap->bind("Return", FuncRequest(LFUN_BREAK_PARAGRAPH));
815 //kbmap->bind("~C-~S-~M-nobreakspace", FuncRequest(LFUN_PROTECTEDSPACE));
817 kbmap->bind("Delete", FuncRequest(LFUN_CHAR_DELETE_FORWARD));
818 kbmap->bind("BackSpace", FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
820 // kbmap->bindings to enable the use of the numeric keypad
822 //kbmap->bind("KP_0", FuncRequest(LFUN_SELF_INSERT));
823 //kbmap->bind("KP_Decimal", FuncRequest(LFUN_SELF_INSERT));
824 kbmap->bind("KP_Enter", FuncRequest(LFUN_BREAK_PARAGRAPH));
825 //kbmap->bind("KP_1", FuncRequest(LFUN_SELF_INSERT));
826 //kbmap->bind("KP_2", FuncRequest(LFUN_SELF_INSERT));
827 //kbmap->bind("KP_3", FuncRequest(LFUN_SELF_INSERT));
828 //kbmap->bind("KP_4", FuncRequest(LFUN_SELF_INSERT));
829 //kbmap->bind("KP_5", FuncRequest(LFUN_SELF_INSERT));
830 //kbmap->bind("KP_6", FuncRequest(LFUN_SELF_INSERT));
831 //kbmap->bind("KP_Add", FuncRequest(LFUN_SELF_INSERT));
832 //kbmap->bind("KP_7", FuncRequest(LFUN_SELF_INSERT));
833 //kbmap->bind("KP_8", FuncRequest(LFUN_SELF_INSERT));
834 //kbmap->bind("KP_9", FuncRequest(LFUN_SELF_INSERT));
835 //kbmap->bind("KP_Divide", FuncRequest(LFUN_SELF_INSERT));
836 //kbmap->bind("KP_Multiply", FuncRequest(LFUN_SELF_INSERT));
837 //kbmap->bind("KP_Subtract", FuncRequest(LFUN_SELF_INSERT));
838 kbmap->bind("KP_Right", FuncRequest(LFUN_CHAR_FORWARD));
839 kbmap->bind("KP_Left", FuncRequest(LFUN_CHAR_BACKWARD));
840 kbmap->bind("KP_Up", FuncRequest(LFUN_UP));
841 kbmap->bind("KP_Down", FuncRequest(LFUN_DOWN));
842 kbmap->bind("KP_Home", FuncRequest(LFUN_LINE_BEGIN));
843 kbmap->bind("KP_End", FuncRequest(LFUN_LINE_END));
844 kbmap->bind("KP_Prior", FuncRequest(LFUN_SCREEN_UP));
845 kbmap->bind("KP_Next", FuncRequest(LFUN_SCREEN_DOWN));
849 void LyX::emergencyCleanup() const
851 // what to do about tmpfiles is non-obvious. we would
852 // like to delete any we find, but our lyxdir might
853 // contain documents etc. which might be helpful on
856 pimpl_->buffer_list_.emergencyWriteAll();
858 pimpl_->lyx_server_->emergencyCleanup();
859 pimpl_->lyx_server_.reset();
860 pimpl_->lyx_socket_.reset();
865 void LyX::deadKeyBindings(kb_keymap * kbmap)
867 // bindKeyings for transparent handling of deadkeys
868 // The keysyms are gotten from XFree86 X11R6
869 kbmap->bind("~C-~S-~M-dead_acute", FuncRequest(LFUN_ACCENT_ACUTE));
870 kbmap->bind("~C-~S-~M-dead_breve", FuncRequest(LFUN_ACCENT_BREVE));
871 kbmap->bind("~C-~S-~M-dead_caron", FuncRequest(LFUN_ACCENT_CARON));
872 kbmap->bind("~C-~S-~M-dead_cedilla", FuncRequest(LFUN_ACCENT_CEDILLA));
873 kbmap->bind("~C-~S-~M-dead_abovering", FuncRequest(LFUN_ACCENT_CIRCLE));
874 kbmap->bind("~C-~S-~M-dead_circumflex", FuncRequest(LFUN_ACCENT_CIRCUMFLEX));
875 kbmap->bind("~C-~S-~M-dead_abovedot", FuncRequest(LFUN_ACCENT_DOT));
876 kbmap->bind("~C-~S-~M-dead_grave", FuncRequest(LFUN_ACCENT_GRAVE));
877 kbmap->bind("~C-~S-~M-dead_doubleacute", FuncRequest(LFUN_ACCENT_HUNGARIAN_UMLAUT));
878 kbmap->bind("~C-~S-~M-dead_macron", FuncRequest(LFUN_ACCENT_MACRON));
879 // nothing with this name
880 // kbmap->bind("~C-~S-~M-dead_special_caron", LFUN_ACCENT_SPECIAL_CARON);
881 kbmap->bind("~C-~S-~M-dead_tilde", FuncRequest(LFUN_ACCENT_TILDE));
882 kbmap->bind("~C-~S-~M-dead_diaeresis", FuncRequest(LFUN_ACCENT_UMLAUT));
883 // nothing with this name either...
884 //kbmap->bind("~C-~S-~M-dead_underbar", FuncRequest(LFUN_ACCENT_UNDERBAR));
885 kbmap->bind("~C-~S-~M-dead_belowdot", FuncRequest(LFUN_ACCENT_UNDERDOT));
886 kbmap->bind("~C-~S-~M-dead_tie", FuncRequest(LFUN_ACCENT_TIE));
887 kbmap->bind("~C-~S-~M-dead_ogonek",FuncRequest(LFUN_ACCENT_OGONEK));
893 // return true if file does not exist or is older than configure.py.
894 bool needsUpdate(string const & file)
896 static string const configure_script =
897 addName(package().system_support(), "configure.py");
898 string const absfile =
899 addName(package().user_support(), file);
901 return (! fs::exists(absfile))
902 || (fs::last_write_time(configure_script)
903 > fs::last_write_time(absfile));
909 bool LyX::queryUserLyXDir(bool explicit_userdir)
911 // Does user directory exist?
912 if (fs::exists(package().user_support()) &&
913 fs::is_directory(package().user_support())) {
916 return needsUpdate("lyxrc.defaults")
917 || needsUpdate("textclass.lst")
918 || needsUpdate("packages.lst");
921 first_start = !explicit_userdir;
923 // If the user specified explicitly a directory, ask whether
924 // to create it. If the user says "no", then exit.
925 if (explicit_userdir &&
927 _("Missing user LyX directory"),
928 bformat(_("You have specified a non-existent user "
929 "LyX directory, %1$s.\n"
930 "It is needed to keep your own configuration."),
931 from_utf8(package().user_support())),
933 _("&Create directory"),
935 lyxerr << to_utf8(_("No user LyX directory. Exiting.")) << endl;
936 earlyExit(EXIT_FAILURE);
939 lyxerr << to_utf8(bformat(_("LyX: Creating directory %1$s"),
940 from_utf8(package().user_support())))
943 if (!createDirectory(package().user_support(), 0755)) {
944 // Failed, so let's exit.
945 lyxerr << to_utf8(_("Failed to create directory. Exiting."))
947 earlyExit(EXIT_FAILURE);
954 bool LyX::readRcFile(string const & name)
956 lyxerr[Debug::INIT] << "About to read " << name << "... ";
958 string const lyxrc_path = libFileSearch(string(), name);
959 if (!lyxrc_path.empty()) {
961 lyxerr[Debug::INIT] << "Found in " << lyxrc_path << endl;
963 if (lyxrc.read(lyxrc_path) < 0) {
968 lyxerr[Debug::INIT] << "Not found." << lyxrc_path << endl;
974 // Read the ui file `name'
975 bool LyX::readUIFile(string const & name)
985 struct keyword_item uitags[ui_last - 1] = {
986 { "include", ui_include },
987 { "menuset", ui_menuset },
988 { "toolbar", ui_toolbar },
989 { "toolbars", ui_toolbars }
992 // Ensure that a file is read only once (prevents include loops)
993 static std::list<string> uifiles;
994 std::list<string>::const_iterator it = uifiles.begin();
995 std::list<string>::const_iterator end = uifiles.end();
996 it = std::find(it, end, name);
998 lyxerr[Debug::INIT] << "UI file '" << name
999 << "' has been read already. "
1000 << "Is this an include loop?"
1005 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
1007 string const ui_path = libFileSearch("ui", name, "ui");
1009 if (ui_path.empty()) {
1010 lyxerr[Debug::INIT] << "Could not find " << name << endl;
1011 showFileError(name);
1014 uifiles.push_back(name);
1016 lyxerr[Debug::INIT] << "Found " << name
1017 << " in " << ui_path << endl;
1018 LyXLex lex(uitags, ui_last - 1);
1019 lex.setFile(ui_path);
1021 lyxerr << "Unable to set LyXLeX for ui file: " << ui_path
1025 if (lyxerr.debugging(Debug::PARSER))
1026 lex.printTable(lyxerr);
1028 while (lex.isOK()) {
1029 switch (lex.lex()) {
1032 string const file = lex.getString();
1033 if (!readUIFile(file))
1038 menubackend.read(lex);
1042 toolbarbackend.read(lex);
1046 toolbarbackend.readToolbars(lex);
1050 if (!rtrim(lex.getString()).empty())
1051 lex.printError("LyX::ReadUIFile: "
1052 "Unknown menu tag: `$$Token'");
1060 // Read the languages file `name'
1061 bool LyX::readLanguagesFile(string const & name)
1063 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
1065 string const lang_path = libFileSearch(string(), name);
1066 if (lang_path.empty()) {
1067 showFileError(name);
1070 languages.read(lang_path);
1075 // Read the encodings file `name'
1076 bool LyX::readEncodingsFile(string const & name)
1078 lyxerr[Debug::INIT] << "About to read " << name << "..." << endl;
1080 string const enc_path = libFileSearch(string(), name);
1081 if (enc_path.empty()) {
1082 showFileError(name);
1085 encodings.read(enc_path);
1094 /// return the the number of arguments consumed
1095 typedef boost::function<int(string const &, string const &)> cmd_helper;
1097 int parse_dbg(string const & arg, string const &)
1100 lyxerr << to_utf8(_("List of supported debug flags:")) << endl;
1101 Debug::showTags(lyxerr);
1104 lyxerr << to_utf8(bformat(_("Setting debug level to %1$s"), from_utf8(arg))) << endl;
1106 lyxerr.level(Debug::value(arg));
1107 Debug::showLevel(lyxerr, lyxerr.level());
1112 int parse_help(string const &, string const &)
1115 to_utf8(_("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
1116 "Command line switches (case sensitive):\n"
1117 "\t-help summarize LyX usage\n"
1118 "\t-userdir dir set user directory to dir\n"
1119 "\t-sysdir dir set system directory to dir\n"
1120 "\t-geometry WxH+X+Y set geometry of the main window\n"
1121 "\t-dbg feature[,feature]...\n"
1122 " select the features to debug.\n"
1123 " Type `lyx -dbg' to see the list of features\n"
1124 "\t-x [--execute] command\n"
1125 " where command is a lyx command.\n"
1126 "\t-e [--export] fmt\n"
1127 " where fmt is the export format of choice.\n"
1128 "\t-i [--import] fmt file.xxx\n"
1129 " where fmt is the import format of choice\n"
1130 " and file.xxx is the file to be imported.\n"
1131 "\t-version summarize version and build info\n"
1132 "Check the LyX man page for more details.")) << endl;
1137 int parse_version(string const &, string const &)
1139 lyxerr << "LyX " << lyx_version
1140 << " (" << lyx_release_date << ")" << endl;
1141 lyxerr << "Built on " << __DATE__ << ", " << __TIME__ << endl;
1143 lyxerr << lyx_version_info << endl;
1148 int parse_sysdir(string const & arg, string const &)
1151 lyxerr << to_utf8(_("Missing directory for -sysdir switch")) << endl;
1154 cl_system_support = arg;
1158 int parse_userdir(string const & arg, string const &)
1161 lyxerr << to_utf8(_("Missing directory for -userdir switch")) << endl;
1164 cl_user_support = arg;
1168 int parse_execute(string const & arg, string const &)
1171 lyxerr << to_utf8(_("Missing command string after --execute switch")) << endl;
1178 int parse_export(string const & type, string const &)
1181 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1182 "--export switch")) << endl;
1185 batch = "buffer-export " + type;
1190 int parse_import(string const & type, string const & file)
1193 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1194 "--import switch")) << endl;
1198 lyxerr << to_utf8(_("Missing filename for --import")) << endl;
1202 batch = "buffer-import " + type + ' ' + file;
1209 void LyX::easyParse(int & argc, char * argv[])
1211 std::map<string, cmd_helper> cmdmap;
1213 cmdmap["-dbg"] = parse_dbg;
1214 cmdmap["-help"] = parse_help;
1215 cmdmap["--help"] = parse_help;
1216 cmdmap["-version"] = parse_version;
1217 cmdmap["--version"] = parse_version;
1218 cmdmap["-sysdir"] = parse_sysdir;
1219 cmdmap["-userdir"] = parse_userdir;
1220 cmdmap["-x"] = parse_execute;
1221 cmdmap["--execute"] = parse_execute;
1222 cmdmap["-e"] = parse_export;
1223 cmdmap["--export"] = parse_export;
1224 cmdmap["-i"] = parse_import;
1225 cmdmap["--import"] = parse_import;
1227 for (int i = 1; i < argc; ++i) {
1228 std::map<string, cmd_helper>::const_iterator it
1229 = cmdmap.find(argv[i]);
1231 // check for X11 -geometry option
1232 if (support::compare(argv[i], "-geometry") == 0)
1233 geometryOption_ = true;
1235 // don't complain if not found - may be parsed later
1236 if (it == cmdmap.end())
1239 string arg((i + 1 < argc) ? argv[i + 1] : "");
1240 string arg2((i + 2 < argc) ? argv[i + 2] : "");
1242 int const remove = 1 + it->second(arg, arg2);
1244 // Now, remove used arguments by shifting
1245 // the following ones remove places down.
1247 for (int j = i; j < argc; ++j)
1248 argv[j] = argv[j + remove];
1252 batch_command = batch;
1256 FuncStatus getStatus(FuncRequest const & action)
1258 return LyX::ref().lyxFunc().getStatus(action);
1262 void dispatch(FuncRequest const & action)
1264 LyX::ref().lyxFunc().dispatch(action);
1268 BufferList & theBufferList()
1270 return LyX::ref().bufferList();
1274 LyXFunc & theLyXFunc()
1276 return LyX::ref().lyxFunc();
1280 LyXServer & theLyXServer()
1282 // FIXME: this should not be use_gui dependent
1283 BOOST_ASSERT(use_gui);
1284 return LyX::ref().server();
1288 LyXServerSocket & theLyXServerSocket()
1290 // FIXME: this should not be use_gui dependent
1291 BOOST_ASSERT(use_gui);
1292 return LyX::ref().socket();
1296 kb_keymap & theTopLevelKeymap()
1298 BOOST_ASSERT(use_gui);
1299 return LyX::ref().topLevelKeymap();
1307 void assertion_failed(char const* a, char const* b, char const* c, long d)
1309 lyx::lyxerr << "Assertion failed: " << a << ' ' << b << ' ' << c << ' '