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.
20 #include "AppleSpellChecker.h"
21 #include "AspellChecker.h"
23 #include "BufferList.h"
26 #include "ConverterCache.h"
27 #include "Converter.h"
28 #include "CutAndPaste.h"
29 #include "EnchantChecker.h"
31 #include "ErrorList.h"
33 #include "FuncStatus.h"
34 #include "HunspellChecker.h"
36 #include "LaTeXFonts.h"
37 #include "LayoutFile.h"
40 #include "LyXAction.h"
42 #include "ModuleList.h"
45 #include "ServerSocket.h"
49 #include "frontends/alert.h"
50 #include "frontends/Application.h"
52 #include "support/lassert.h"
53 #include "support/debug.h"
54 #include "support/environment.h"
55 #include "support/ExceptionMessage.h"
56 #include "support/filetools.h"
57 #include "support/gettext.h"
58 #include "support/lstrings.h"
59 #include "support/Messages.h"
60 #include "support/os.h"
61 #include "support/Package.h"
62 #include "support/PathChanger.h"
63 #include "support/Systemcall.h"
65 #include "support/bind.h"
66 #include <boost/scoped_ptr.hpp>
77 using namespace lyx::support;
81 namespace Alert = frontend::Alert;
82 namespace os = support::os;
86 // Are we using the GUI at all? We default to true and this is changed
87 // to false when the export feature is used.
92 // We default to open documents in an already running instance, provided that
93 // the lyxpipe has been setup. This can be overridden either on the command
94 // line or through preference settings.
96 RunMode run_mode = PREFERRED;
99 // Tell what files can be silently overwritten during batch export.
100 // Possible values are: NO_FILES, MAIN_FILE, ALL_FILES, UNSPECIFIED.
101 // Unless specified on command line (through the -f switch) or through the
102 // environment variable LYX_FORCE_OVERWRITE, the default will be MAIN_FILE.
104 OverwriteFiles force_overwrite = UNSPECIFIED;
109 // Filled with the command line arguments "foo" of "-sysdir foo" or
111 string cl_system_support;
112 string cl_user_support;
116 LyX * singleton_ = 0;
118 void showFileError(string const & error)
120 Alert::warning(_("Could not read configuration file"),
121 bformat(_("Error while reading the configuration file\n%1$s.\n"
122 "Please check your installation."), from_utf8(error)));
126 void reconfigureUserLyXDir()
128 string const configure_command = package().configure_command();
130 lyxerr << to_utf8(_("LyX: reconfiguring user directory")) << endl;
131 PathChanger p(package().user_support());
133 one.startscript(Systemcall::Wait, configure_command);
134 lyxerr << "LyX: " << to_utf8(_("Done!")) << endl;
139 /// The main application class private implementation.
142 : latexfonts_(0), spell_checker_(0), apple_spell_checker_(0), aspell_checker_(0), enchant_checker_(0), hunspell_checker_(0)
148 delete apple_spell_checker_;
149 delete aspell_checker_;
150 delete enchant_checker_;
151 delete hunspell_checker_;
155 BufferList buffer_list_;
157 KeyMap toplevel_keymap_;
159 CmdDef toplevel_cmddef_;
161 boost::scoped_ptr<Server> lyx_server_;
163 boost::scoped_ptr<ServerSocket> lyx_socket_;
165 boost::scoped_ptr<frontend::Application> application_;
166 /// lyx session, containing lastfiles, lastfilepos, and lastopened
167 boost::scoped_ptr<Session> session_;
169 /// Files to load at start.
170 vector<string> files_to_load_;
172 /// The messages translators.
173 map<string, Messages> messages_;
175 /// The file converters.
176 Converters converters_;
178 // The system converters copy after reading lyxrc.defaults.
179 Converters system_converters_;
184 Movers system_movers_;
186 /// has this user started lyx for the first time?
188 /// the parsed command line batch command if any
189 vector<string> batch_commands;
192 LaTeXFonts * latexfonts_;
195 SpellChecker * spell_checker_;
197 SpellChecker * apple_spell_checker_;
199 SpellChecker * aspell_checker_;
201 SpellChecker * enchant_checker_;
203 SpellChecker * hunspell_checker_;
208 frontend::Application * theApp()
211 return singleton_->pimpl_->application_.get();
221 WordList::cleanupWordLists();
225 void lyx_exit(int exit_code)
228 // Something wrong happened so better save everything, just in
233 // Properly crash in debug mode in order to get a useful backtrace.
237 // In release mode, try to exit gracefully.
239 theApp()->exit(exit_code);
253 Messages & LyX::messages(string const & language)
255 map<string, Messages>::iterator it = pimpl_->messages_.find(language);
257 if (it != pimpl_->messages_.end())
260 pair<map<string, Messages>::iterator, bool> result =
261 pimpl_->messages_.insert(make_pair(language, Messages(language)));
263 LATTEST(result.second);
264 return result.first->second;
268 int LyX::exec(int & argc, char * argv[])
270 // Minimal setting of locale before parsing command line
272 init_package(os::utf8_argv(0), string(), string());
273 // we do not get to this point when init_package throws an exception
275 } catch (ExceptionMessage const & message) {
276 LYXERR(Debug::LOCALE, message.title_ + ", " + message.details_);
279 // Here we need to parse the command line. At least
280 // we need to parse for "-dbg" and "-help"
281 easyParse(argc, argv);
284 init_package(os::utf8_argv(0), cl_system_support, cl_user_support);
285 } catch (ExceptionMessage const & message) {
286 if (message.type_ == ErrorException) {
287 Alert::error(message.title_, message.details_);
289 } else if (message.type_ == WarningException) {
290 Alert::warning(message.title_, message.details_);
294 // Reinit the messages machinery in case package() knows
295 // something interesting about the locale directory.
299 // FIXME: create a ConsoleApplication
300 int exit_status = init(argc, argv);
306 // this is correct, since return values are inverted.
307 exit_status = !loadFiles();
309 if (pimpl_->batch_commands.empty() || pimpl_->buffer_list_.empty()) {
314 BufferList::iterator begin = pimpl_->buffer_list_.begin();
316 bool final_success = false;
317 for (BufferList::iterator I = begin; I != pimpl_->buffer_list_.end(); ++I) {
319 if (buf != buf->masterBuffer())
321 vector<string>::const_iterator bcit = pimpl_->batch_commands.begin();
322 vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
324 for (; bcit != bcend; ++bcit) {
325 LYXERR(Debug::ACTION, "Buffer::dispatch: cmd: " << *bcit);
326 buf->dispatch(*bcit, dr);
327 final_success |= !dr.error();
331 return !final_success;
334 // Let the frontend parse and remove all arguments that it knows
335 pimpl_->application_.reset(createApplication(argc, argv));
337 // Reestablish our defaults, as Qt overwrites them
338 // after createApplication()
341 // Parse and remove all known arguments in the LyX singleton
342 // Give an error for all remaining ones.
343 int exit_status = init(argc, argv);
345 // Kill the application object before exiting.
346 pimpl_->application_.reset();
352 // If not otherwise specified by a command line option or
353 // by preferences, we default to reuse a running instance.
354 if (run_mode == PREFERRED)
355 run_mode = USE_REMOTE;
358 /* Create a CoreApplication class that will provide the main event loop
359 * and the socket callback registering. With Qt4, only QtCore
360 * library would be needed.
361 * When this is done, a server_mode could be created and the following two
362 * line would be moved out from here.
363 * However, note that the first of the two lines below triggers the
364 * "single instance" behavior, which should occur right at this point.
366 // Note: socket callback must be registered after init(argc, argv)
367 // such that package().temp_dir() is properly initialized.
368 pimpl_->lyx_server_.reset(new Server(lyxrc.lyxpipes));
369 pimpl_->lyx_socket_.reset(new ServerSocket(
370 FileName(package().temp_dir().absFileName() + "/lyxsocket")));
372 // Start the real execution loop.
373 if (!theServer().deferredLoadingToOtherInstance())
374 exit_status = pimpl_->application_->exec();
375 else if (!pimpl_->files_to_load_.empty()) {
376 vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
377 vector<string>::const_iterator end = pimpl_->files_to_load_.end();
378 lyxerr << _("The following files could not be loaded:") << endl;
379 for (; it != end; ++it)
380 lyxerr << *it << endl;
389 void LyX::prepareExit()
391 // Clear the clipboard and selection stack:
392 cap::clearCutStack();
393 cap::clearSelection();
395 // Write the index file of the converter cache
396 ConverterCache::get().writeIndex();
398 // close buffers first
399 pimpl_->buffer_list_.closeAll();
401 // register session changes and shutdown server and socket
403 if (pimpl_->session_)
404 pimpl_->session_->writeFile();
405 pimpl_->session_.reset();
406 pimpl_->lyx_server_.reset();
407 pimpl_->lyx_socket_.reset();
410 // do any other cleanup procedures now
411 if (package().temp_dir() != package().system_temp_dir()) {
412 string const abs_tmpdir = package().temp_dir().absFileName();
413 if (!contains(package().temp_dir().absFileName(), "lyx_tmpdir")) {
414 docstring const msg =
415 bformat(_("%1$s does not appear like a LyX created temporary directory."),
416 from_utf8(abs_tmpdir));
417 Alert::warning(_("Cannot remove temporary directory"), msg);
419 LYXERR(Debug::INFO, "Deleting tmp dir "
420 << package().temp_dir().absFileName());
421 if (!package().temp_dir().destroyDirectory()) {
422 docstring const msg =
423 bformat(_("Unable to remove the temporary directory %1$s"),
424 from_utf8(package().temp_dir().absFileName()));
425 Alert::warning(_("Unable to remove temporary directory"), msg);
430 // Kill the application object before exiting. This avoids crashes
431 // when exiting on Linux.
432 pimpl_->application_.reset();
436 void LyX::earlyExit(int status)
438 LATTEST(pimpl_->application_.get());
439 // LyX::pimpl_::application_ is not initialised at this
440 // point so it's safe to just exit after some cleanup.
446 int LyX::init(int & argc, char * argv[])
448 // check for any spurious extra arguments
449 // other than documents
450 for (int argi = 1; argi < argc ; ++argi) {
451 if (argv[argi][0] == '-') {
453 bformat(_("Wrong command line option `%1$s'. Exiting."),
454 from_utf8(os::utf8_argv(argi)))) << endl;
459 // Initialization of LyX (reads lyxrc and more)
460 LYXERR(Debug::INIT, "Initializing LyX::init...");
461 bool success = init();
462 LYXERR(Debug::INIT, "Initializing LyX::init...done");
466 // Remaining arguments are assumed to be files to load.
467 for (int argi = 1; argi < argc; ++argi)
468 pimpl_->files_to_load_.push_back(os::utf8_argv(argi));
470 if (!use_gui && pimpl_->files_to_load_.empty()) {
471 lyxerr << to_utf8(_("Missing filename for this operation.")) << endl;
476 pimpl_->files_to_load_.push_back(
477 i18nLibFileSearch("examples", "splash.lyx").absFileName());
484 bool LyX::loadFiles()
488 vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
489 vector<string>::const_iterator end = pimpl_->files_to_load_.end();
491 for (; it != end; ++it) {
492 // get absolute path of file and add ".lyx" to
493 // the filename if necessary
494 FileName fname = fileSearch(string(), os::internal_path(*it), "lyx",
500 Buffer * buf = pimpl_->buffer_list_.newBuffer(fname.absFileName());
501 if (buf->loadLyXFile() == Buffer::ReadSuccess) {
502 ErrorList const & el = buf->errorList("Parse");
504 for_each(el.begin(), el.end(),
505 bind(&LyX::printError, this, _1));
508 pimpl_->buffer_list_.release(buf);
509 docstring const error_message =
510 bformat(_("LyX failed to load the following file: %1$s"),
511 from_utf8(fname.absFileName()));
512 lyxerr << to_utf8(error_message) << endl;
520 void execBatchCommands()
523 singleton_->execCommands();
527 void LyX::execCommands()
529 // The advantage of doing this here is that the event loop
530 // is already started. So any need for interaction will be
533 // if reconfiguration is needed.
534 if (LayoutFileList::get().empty()) {
535 switch (Alert::prompt(
536 _("No textclass is found"),
537 _("LyX will only have minimal functionality because no textclasses "
538 "have been found. You can either try to reconfigure LyX normally, "
539 "try to reconfigure without checking your LaTeX installation, or continue."),
546 // regular reconfigure
547 lyx::dispatch(FuncRequest(LFUN_RECONFIGURE, ""));
550 // reconfigure --without-latex-config
551 lyx::dispatch(FuncRequest(LFUN_RECONFIGURE,
552 " --without-latex-config"));
559 // create the first main window
560 lyx::dispatch(FuncRequest(LFUN_WINDOW_NEW, geometryArg));
562 if (!pimpl_->files_to_load_.empty()) {
563 // if some files were specified at command-line we assume that the
564 // user wants to edit *these* files and not to restore the session.
565 for (size_t i = 0; i != pimpl_->files_to_load_.size(); ++i) {
567 FuncRequest(LFUN_FILE_OPEN, pimpl_->files_to_load_[i]));
569 // clear this list to save a few bytes of RAM
570 pimpl_->files_to_load_.clear();
572 pimpl_->application_->restoreGuiSession();
574 // Execute batch commands if available
575 if (pimpl_->batch_commands.empty())
578 vector<string>::const_iterator bcit = pimpl_->batch_commands.begin();
579 vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
580 for (; bcit != bcend; ++bcit) {
581 LYXERR(Debug::INIT, "About to handle -x '" << *bcit << '\'');
582 lyx::dispatch(lyxaction.lookupFunc(*bcit));
590 The SIGHUP signal does not exist on Windows and does not need to be handled.
592 Windows handles SIGFPE and SIGSEGV signals as expected.
594 Ctrl+C interrupts (mapped to SIGINT by Windows' POSIX compatability layer)
595 cause a new thread to be spawned. This may well result in unexpected
596 behaviour by the single-threaded LyX.
598 SIGTERM signals will come only from another process actually sending
599 that signal using 'raise' in Windows' POSIX compatability layer. It will
600 not come from the general "terminate process" methods that everyone
601 actually uses (and which can't be trapped). Killing an app 'politely' on
602 Windows involves first sending a WM_CLOSE message, something that is
603 caught already by the Qt frontend.
605 For more information see:
607 http://aspn.activestate.com/ASPN/Mail/Message/ActiveTcl/2034055
608 ...signals are mostly useless on Windows for a variety of reasons that are
611 'UNIX Application Migration Guide, Chapter 9'
612 http://msdn.microsoft.com/library/en-us/dnucmg/html/UCMGch09.asp
614 'How To Terminate an Application "Cleanly" in Win32'
615 http://support.microsoft.com/default.aspx?scid=kb;en-us;178893
619 static void error_handler(int err_sig)
621 // Throw away any signals other than the first one received.
622 static sig_atomic_t handling_error = false;
625 handling_error = true;
627 // We have received a signal indicating a fatal error, so
628 // try and save the data ASAP.
631 // These lyxerr calls may or may not work:
633 // Signals are asynchronous, so the main program may be in a very
634 // fragile state when a signal is processed and thus while a signal
635 // handler function executes.
636 // In general, therefore, we should avoid performing any
637 // I/O operations or calling most library and system functions from
640 // This shouldn't matter here, however, as we've already invoked
646 msg = _("SIGHUP signal caught!\nBye.");
650 msg = _("SIGFPE signal caught!\nBye.");
653 msg = _("SIGSEGV signal caught!\n"
654 "Sorry, you have found a bug in LyX, "
655 "hope you have not lost any data.\n"
656 "Please read the bug-reporting instructions "
657 "in 'Help->Introduction' and send us a bug report, "
658 "if necessary. Thanks!\nBye.");
667 lyxerr << "\nlyx: " << msg << endl;
668 // try to make a GUI message
669 Alert::error(_("LyX crashed!"), msg);
672 // Deinstall the signal handlers
674 signal(SIGHUP, SIG_DFL);
676 signal(SIGINT, SIG_DFL);
677 signal(SIGFPE, SIG_DFL);
678 signal(SIGSEGV, SIG_DFL);
679 signal(SIGTERM, SIG_DFL);
682 if (err_sig == SIGSEGV ||
683 (err_sig != SIGHUP && !getEnv("LYXDEBUG").empty())) {
685 if (err_sig == SIGSEGV || !getEnv("LYXDEBUG").empty()) {
688 // with abort() it crashes again.
701 void LyX::printError(ErrorItem const & ei)
703 docstring tmp = _("LyX: ") + ei.error + char_type(':')
705 cerr << to_utf8(tmp) << endl;
712 signal(SIGHUP, error_handler);
714 signal(SIGFPE, error_handler);
715 signal(SIGSEGV, error_handler);
716 signal(SIGINT, error_handler);
717 signal(SIGTERM, error_handler);
718 // SIGPIPE can be safely ignored.
720 lyxrc.tempdir_path = package().temp_dir().absFileName();
721 lyxrc.document_path = package().document_dir().absFileName();
723 if (lyxrc.example_path.empty()) {
724 lyxrc.example_path = addPath(package().system_support().absFileName(),
727 if (lyxrc.template_path.empty()) {
728 lyxrc.template_path = addPath(package().system_support().absFileName(),
732 // init LyXDir environment variable
733 string const lyx_dir = package().lyx_dir().absFileName();
734 LYXERR(Debug::INIT, "Setting LyXDir... to \"" << lyx_dir << "\"");
735 if (!setEnv("LyXDir", lyx_dir))
736 LYXERR(Debug::INIT, "\t... failed!");
738 if (package().explicit_user_support() && getEnv(LYX_USERDIR_VER).empty()) {
739 // -userdir was given on the command line.
740 // Make it available to child processes, otherwise tex2lyx
741 // would not find all layout files, and other converters might
743 string const user_dir = package().user_support().absFileName();
744 LYXERR(Debug::INIT, "Setting " LYX_USERDIR_VER "... to \""
746 if (!setEnv(LYX_USERDIR_VER, user_dir))
747 LYXERR(Debug::INIT, "\t... failed!");
751 // Read configuration files
754 // This one may have been distributed along with LyX.
755 if (!readRcFile("lyxrc.dist"))
758 // Set the PATH correctly.
759 #if !defined (USE_POSIX_PACKAGING)
760 // Add the directory containing the LyX executable to the path
761 // so that LyX can find things like tex2lyx.
762 if (package().build_support().empty())
763 prependEnvPath("PATH", package().binary_dir().absFileName());
765 if (!lyxrc.path_prefix.empty())
766 prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
768 // Check that user LyX directory is ok.
770 string const lock_file = package().user_support().absFileName() + ".lyx_configure_lock";
771 int fd = fileLock(lock_file.c_str());
773 if (queryUserLyXDir(package().explicit_user_support())) {
774 reconfigureUserLyXDir();
776 fileUnlock(fd, lock_file.c_str());
780 // No need for a splash when there is no GUI
782 // Default is to overwrite the main file during export, unless
783 // the -f switch was specified or LYX_FORCE_OVERWRITE was set
784 if (force_overwrite == UNSPECIFIED) {
785 string const what = getEnv("LYX_FORCE_OVERWRITE");
787 force_overwrite = ALL_FILES;
788 else if (what == "none")
789 force_overwrite = NO_FILES;
791 force_overwrite = MAIN_FILE;
795 // This one is generated in user_support directory by lib/configure.py.
796 if (!readRcFile("lyxrc.defaults"))
799 // Query the OS to know what formats are viewed natively
800 formats.setAutoOpen();
802 // Read lyxrc.dist again to be able to override viewer auto-detection.
803 readRcFile("lyxrc.dist");
805 system_lyxrc = lyxrc;
806 system_formats = formats;
807 pimpl_->system_converters_ = pimpl_->converters_;
808 pimpl_->system_movers_ = pimpl_->movers_;
809 system_lcolor = lcolor;
811 // This one is edited through the preferences dialog.
812 if (!readRcFile("preferences", true))
815 // The language may have been set to someting useful through prefs
818 if (!readEncodingsFile("encodings", "unicodesymbols"))
820 if (!readLanguagesFile("languages"))
823 LYXERR(Debug::INIT, "Reading layouts...");
825 LayoutFileList::get().read();
827 theModuleList.read();
829 // read keymap and ui files in batch mode as well
830 // because InsetInfo needs to know these to produce
831 // the correct output
833 // Set up command definitions
834 pimpl_->toplevel_cmddef_.read(lyxrc.def_file);
838 pimpl_->toplevel_keymap_.read("site");
839 pimpl_->toplevel_keymap_.read(lyxrc.bind_file);
840 // load user bind file user.bind
841 pimpl_->toplevel_keymap_.read("user", 0, KeyMap::MissingOK);
843 if (lyxerr.debugging(Debug::LYXRC))
846 os::windows_style_tex_paths(lyxrc.windows_style_tex_paths);
847 // Prepend path prefix a second time to take the user preferences into a account
848 if (!lyxrc.path_prefix.empty())
849 prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
851 FileName const document_path(lyxrc.document_path);
852 if (document_path.exists() && document_path.isDirectory())
853 package().document_dir() = document_path;
855 package().set_temp_dir(createLyXTmpDir(FileName(lyxrc.tempdir_path)));
856 if (package().temp_dir().empty()) {
857 Alert::error(_("Could not create temporary directory"),
858 bformat(_("Could not create a temporary directory in\n"
860 "Make sure that this path exists and is writable and try again."),
861 from_utf8(lyxrc.tempdir_path)));
862 // createLyXTmpDir() tries sufficiently hard to create a
863 // usable temp dir, so the probability to come here is
864 // close to zero. We therefore don't try to overcome this
865 // problem with e.g. asking the user for a new path and
866 // trying again but simply exit.
870 LYXERR(Debug::INIT, "LyX tmp dir: `"
871 << package().temp_dir().absFileName() << '\'');
873 LYXERR(Debug::INIT, "Reading session information '.lyx/session'...");
874 pimpl_->session_.reset(new Session(lyxrc.num_lastfiles));
876 // This must happen after package initialization and after lyxrc is
877 // read, therefore it can't be done by a static object.
878 ConverterCache::init();
884 void emergencyCleanup()
886 // what to do about tmpfiles is non-obvious. we would
887 // like to delete any we find, but our lyxdir might
888 // contain documents etc. which might be helpful on
891 singleton_->pimpl_->buffer_list_.emergencyWriteAll();
893 if (singleton_->pimpl_->lyx_server_)
894 singleton_->pimpl_->lyx_server_->emergencyCleanup();
895 singleton_->pimpl_->lyx_server_.reset();
896 singleton_->pimpl_->lyx_socket_.reset();
901 // return true if file does not exist or is older than configure.py.
902 static bool needsUpdate(string const & file)
904 // We cannot initialize configure_script directly because the package
905 // is not initialized yet when static objects are constructed.
906 static FileName configure_script;
907 static bool firstrun = true;
910 FileName(addName(package().system_support().absFileName(),
916 FileName(addName(package().user_support().absFileName(), file));
917 return !absfile.exists()
918 || configure_script.lastModified() > absfile.lastModified();
922 bool LyX::queryUserLyXDir(bool explicit_userdir)
924 // Does user directory exist?
925 FileName const sup = package().user_support();
926 if (sup.exists() && sup.isDirectory()) {
929 return needsUpdate("lyxrc.defaults")
930 || needsUpdate("lyxmodules.lst")
931 || needsUpdate("textclass.lst")
932 || needsUpdate("packages.lst");
935 first_start = !explicit_userdir;
937 // If the user specified explicitly a directory, ask whether
938 // to create it. If the user says "no", then exit.
939 if (explicit_userdir &&
941 _("Missing user LyX directory"),
942 bformat(_("You have specified a non-existent user "
943 "LyX directory, %1$s.\n"
944 "It is needed to keep your own configuration."),
945 from_utf8(package().user_support().absFileName())),
947 _("&Create directory"),
949 lyxerr << to_utf8(_("No user LyX directory. Exiting.")) << endl;
950 earlyExit(EXIT_FAILURE);
953 lyxerr << to_utf8(bformat(_("LyX: Creating directory %1$s"),
954 from_utf8(sup.absFileName()))) << endl;
956 if (!sup.createDirectory(0755)) {
957 // Failed, so let's exit.
958 lyxerr << to_utf8(_("Failed to create directory. Exiting."))
960 earlyExit(EXIT_FAILURE);
967 bool LyX::readRcFile(string const & name, bool check_format)
969 LYXERR(Debug::INIT, "About to read " << name << "... ");
971 FileName const lyxrc_path = libFileSearch(string(), name);
972 if (lyxrc_path.empty()) {
973 LYXERR(Debug::INIT, "Not found." << lyxrc_path);
975 // This was the previous logic, but can it be right??
978 LYXERR(Debug::INIT, "Found in " << lyxrc_path);
979 bool const success = lyxrc.read(lyxrc_path, check_format);
985 // Read the languages file `name'
986 bool LyX::readLanguagesFile(string const & name)
988 LYXERR(Debug::INIT, "About to read " << name << "...");
990 FileName const lang_path = libFileSearch(string(), name);
991 if (lang_path.empty()) {
995 languages.read(lang_path);
1000 // Read the encodings file `name'
1001 bool LyX::readEncodingsFile(string const & enc_name,
1002 string const & symbols_name)
1004 LYXERR(Debug::INIT, "About to read " << enc_name << " and "
1005 << symbols_name << "...");
1007 FileName const symbols_path = libFileSearch(string(), symbols_name);
1008 if (symbols_path.empty()) {
1009 showFileError(symbols_name);
1013 FileName const enc_path = libFileSearch(string(), enc_name);
1014 if (enc_path.empty()) {
1015 showFileError(enc_name);
1018 encodings.read(enc_path, symbols_path);
1025 /// return the the number of arguments consumed
1026 typedef boost::function<int(string const &, string const &, string &)> cmd_helper;
1028 int parse_dbg(string const & arg, string const &, string &)
1031 cout << to_utf8(_("List of supported debug flags:")) << endl;
1032 Debug::showTags(cout);
1035 lyxerr << to_utf8(bformat(_("Setting debug level to %1$s"), from_utf8(arg))) << endl;
1037 lyxerr.setLevel(Debug::value(arg));
1038 Debug::showLevel(lyxerr, lyxerr.level());
1043 int parse_help(string const &, string const &, string &)
1046 to_utf8(_("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
1047 "Command line switches (case sensitive):\n"
1048 "\t-help summarize LyX usage\n"
1049 "\t-userdir dir set user directory to dir\n"
1050 "\t-sysdir dir set system directory to dir\n"
1051 "\t-geometry WxH+X+Y set geometry of the main window\n"
1052 "\t-dbg feature[,feature]...\n"
1053 " select the features to debug.\n"
1054 " Type `lyx -dbg' to see the list of features\n"
1055 "\t-x [--execute] command\n"
1056 " where command is a lyx command.\n"
1057 "\t-e [--export] fmt\n"
1058 " where fmt is the export format of choice. Look in\n"
1059 " Tools->Preferences->File Handling->File Formats->Short Name\n"
1060 " to see which parameter (which differs from the format name\n"
1061 " in the File->Export menu) should be passed.\n"
1062 " Note that the order of -e and -x switches matters.\n"
1063 "\t-E [--export-to] fmt filename\n"
1064 " where fmt is the export format of choice (see --export),\n"
1065 " and filename is the destination filename.\n"
1066 "\t-i [--import] fmt file.xxx\n"
1067 " where fmt is the import format of choice\n"
1068 " and file.xxx is the file to be imported.\n"
1069 "\t-f [--force-overwrite] what\n"
1070 " where what is either `all', `main' or `none',\n"
1071 " specifying whether all files, main file only, or no files,\n"
1072 " respectively, are to be overwritten during a batch export.\n"
1073 " Anything else is equivalent to `all', but is not consumed.\n"
1074 "\t-n [--no-remote]\n"
1075 " open documents in a new instance\n"
1077 " open documents in an already running instance\n"
1078 " (a working lyxpipe is needed)\n"
1079 "\t-batch execute commands without launching GUI and exit.\n"
1080 "\t-version summarize version and build info\n"
1081 "Check the LyX man page for more details.")) << endl;
1087 int parse_version(string const &, string const &, string &)
1089 cout << "LyX " << lyx_version
1090 << " (" << lyx_release_date << ")" << endl;
1091 cout << to_utf8(bformat(_("Built on %1$s[[date]], %2$s[[time]]"),
1092 from_ascii(__DATE__), from_ascii(__TIME__))) << endl;
1094 cout << lyx_version_info << endl;
1100 int parse_sysdir(string const & arg, string const &, string &)
1103 Alert::error(_("No system directory"),
1104 _("Missing directory for -sysdir switch"));
1107 cl_system_support = arg;
1112 int parse_userdir(string const & arg, string const &, string &)
1115 Alert::error(_("No user directory"),
1116 _("Missing directory for -userdir switch"));
1119 cl_user_support = arg;
1124 int parse_execute(string const & arg, string const &, string & batch)
1127 Alert::error(_("Incomplete command"),
1128 _("Missing command string after --execute switch"));
1136 int parse_export_to(string const & type, string const & output_file, string & batch)
1139 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1140 "--export-to switch")) << endl;
1143 if (output_file.empty()) {
1144 lyxerr << to_utf8(_("Missing destination filename after "
1145 "--export-to switch")) << endl;
1148 batch = "buffer-export " + type + " " + output_file;
1154 int parse_export(string const & type, string const &, string & batch)
1157 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1158 "--export switch")) << endl;
1161 batch = "buffer-export " + type;
1167 int parse_import(string const & type, string const & file, string & batch)
1170 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1171 "--import switch")) << endl;
1175 lyxerr << to_utf8(_("Missing filename for --import")) << endl;
1178 batch = "buffer-import " + type + ' ' + file;
1183 int parse_geometry(string const & arg1, string const &, string &)
1186 // don't remove "-geometry", it will be pruned out later in the
1187 // frontend if need be.
1192 int parse_batch(string const &, string const &, string &)
1199 int parse_noremote(string const &, string const &, string &)
1201 run_mode = NEW_INSTANCE;
1206 int parse_remote(string const &, string const &, string &)
1208 run_mode = USE_REMOTE;
1213 int parse_force(string const & arg, string const &, string &)
1216 force_overwrite = ALL_FILES;
1218 } else if (arg == "main") {
1219 force_overwrite = MAIN_FILE;
1221 } else if (arg == "none") {
1222 force_overwrite = NO_FILES;
1225 force_overwrite = ALL_FILES;
1233 void LyX::easyParse(int & argc, char * argv[])
1235 map<string, cmd_helper> cmdmap;
1237 cmdmap["-dbg"] = parse_dbg;
1238 cmdmap["-help"] = parse_help;
1239 cmdmap["--help"] = parse_help;
1240 cmdmap["-version"] = parse_version;
1241 cmdmap["--version"] = parse_version;
1242 cmdmap["-sysdir"] = parse_sysdir;
1243 cmdmap["-userdir"] = parse_userdir;
1244 cmdmap["-x"] = parse_execute;
1245 cmdmap["--execute"] = parse_execute;
1246 cmdmap["-e"] = parse_export;
1247 cmdmap["--export"] = parse_export;
1248 cmdmap["-E"] = parse_export_to;
1249 cmdmap["--export-to"] = parse_export_to;
1250 cmdmap["-i"] = parse_import;
1251 cmdmap["--import"] = parse_import;
1252 cmdmap["-geometry"] = parse_geometry;
1253 cmdmap["-batch"] = parse_batch;
1254 cmdmap["-f"] = parse_force;
1255 cmdmap["--force-overwrite"] = parse_force;
1256 cmdmap["-n"] = parse_noremote;
1257 cmdmap["--no-remote"] = parse_noremote;
1258 cmdmap["-r"] = parse_remote;
1259 cmdmap["--remote"] = parse_remote;
1261 for (int i = 1; i < argc; ++i) {
1262 map<string, cmd_helper>::const_iterator it
1263 = cmdmap.find(argv[i]);
1265 // don't complain if not found - may be parsed later
1266 if (it == cmdmap.end())
1270 (i + 1 < argc) ? os::utf8_argv(i + 1) : string();
1272 (i + 2 < argc) ? os::utf8_argv(i + 2) : string();
1275 int const remove = 1 + it->second(arg, arg2, batch);
1277 pimpl_->batch_commands.push_back(batch);
1279 // Now, remove used arguments by shifting
1280 // the following ones remove places down.
1282 os::remove_internal_args(i, remove);
1284 for (int j = i; j < argc; ++j)
1285 argv[j] = argv[j + remove];
1292 FuncStatus getStatus(FuncRequest const & action)
1295 return theApp()->getStatus(action);
1299 void dispatch(FuncRequest const & action)
1302 return theApp()->dispatch(action);
1306 void dispatch(FuncRequest const & action, DispatchResult & dr)
1309 return theApp()->dispatch(action, dr);
1313 vector<string> & theFilesToLoad()
1315 LAPPERR(singleton_);
1316 return singleton_->pimpl_->files_to_load_;
1320 BufferList & theBufferList()
1322 LAPPERR(singleton_);
1323 return singleton_->pimpl_->buffer_list_;
1327 Server & theServer()
1329 // FIXME: this should not be use_gui dependent
1331 LAPPERR(singleton_);
1332 return *singleton_->pimpl_->lyx_server_.get();
1336 ServerSocket & theServerSocket()
1338 // FIXME: this should not be use_gui dependent
1340 LAPPERR(singleton_);
1341 return *singleton_->pimpl_->lyx_socket_.get();
1345 KeyMap & theTopLevelKeymap()
1347 LAPPERR(singleton_);
1348 return singleton_->pimpl_->toplevel_keymap_;
1352 Converters & theConverters()
1354 LAPPERR(singleton_);
1355 return singleton_->pimpl_->converters_;
1359 Converters & theSystemConverters()
1361 LAPPERR(singleton_);
1362 return singleton_->pimpl_->system_converters_;
1366 Movers & theMovers()
1368 LAPPERR(singleton_);
1369 return singleton_->pimpl_->movers_;
1373 Mover const & getMover(string const & fmt)
1375 LAPPERR(singleton_);
1376 return singleton_->pimpl_->movers_(fmt);
1380 void setMover(string const & fmt, string const & command)
1382 LAPPERR(singleton_);
1383 singleton_->pimpl_->movers_.set(fmt, command);
1387 Movers & theSystemMovers()
1389 LAPPERR(singleton_);
1390 return singleton_->pimpl_->system_movers_;
1394 Messages const & getMessages(string const & language)
1396 LAPPERR(singleton_);
1397 return singleton_->messages(language);
1401 Messages const & getGuiMessages()
1403 LAPPERR(singleton_);
1404 return singleton_->messages(Messages::guiLanguage());
1408 Session & theSession()
1410 LAPPERR(singleton_);
1411 return *singleton_->pimpl_->session_.get();
1415 LaTeXFonts & theLaTeXFonts()
1417 LAPPERR(singleton_);
1418 if (!singleton_->pimpl_->latexfonts_)
1419 singleton_->pimpl_->latexfonts_ = new LaTeXFonts;
1420 return *singleton_->pimpl_->latexfonts_;
1424 CmdDef & theTopLevelCmdDef()
1426 LAPPERR(singleton_);
1427 return singleton_->pimpl_->toplevel_cmddef_;
1431 SpellChecker * theSpellChecker()
1433 if (!singleton_->pimpl_->spell_checker_)
1435 return singleton_->pimpl_->spell_checker_;
1439 void setSpellChecker()
1441 SpellChecker::ChangeNumber speller_change_number =singleton_->pimpl_->spell_checker_ ?
1442 singleton_->pimpl_->spell_checker_->changeNumber() : 0;
1444 if (lyxrc.spellchecker == "native") {
1445 #if defined(USE_MACOSX_PACKAGING)
1446 if (!singleton_->pimpl_->apple_spell_checker_)
1447 singleton_->pimpl_->apple_spell_checker_ = new AppleSpellChecker;
1448 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->apple_spell_checker_;
1450 singleton_->pimpl_->spell_checker_ = 0;
1452 } else if (lyxrc.spellchecker == "aspell") {
1453 #if defined(USE_ASPELL)
1454 if (!singleton_->pimpl_->aspell_checker_)
1455 singleton_->pimpl_->aspell_checker_ = new AspellChecker;
1456 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->aspell_checker_;
1458 singleton_->pimpl_->spell_checker_ = 0;
1460 } else if (lyxrc.spellchecker == "enchant") {
1461 #if defined(USE_ENCHANT)
1462 if (!singleton_->pimpl_->enchant_checker_)
1463 singleton_->pimpl_->enchant_checker_ = new EnchantChecker;
1464 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->enchant_checker_;
1466 singleton_->pimpl_->spell_checker_ = 0;
1468 } else if (lyxrc.spellchecker == "hunspell") {
1469 #if defined(USE_HUNSPELL)
1470 if (!singleton_->pimpl_->hunspell_checker_)
1471 singleton_->pimpl_->hunspell_checker_ = new HunspellChecker;
1472 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->hunspell_checker_;
1474 singleton_->pimpl_->spell_checker_ = 0;
1477 singleton_->pimpl_->spell_checker_ = 0;
1479 if (singleton_->pimpl_->spell_checker_) {
1480 singleton_->pimpl_->spell_checker_->changeNumber(speller_change_number);
1481 singleton_->pimpl_->spell_checker_->advanceChangeNumber();