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"
25 #include "CiteEnginesList.h"
27 #include "ConverterCache.h"
28 #include "Converter.h"
29 #include "CutAndPaste.h"
30 #include "EnchantChecker.h"
32 #include "ErrorList.h"
34 #include "FuncStatus.h"
35 #include "HunspellChecker.h"
38 #include "LaTeXFonts.h"
39 #include "LayoutFile.h"
42 #include "LyXAction.h"
44 #include "ModuleList.h"
47 #include "ServerSocket.h"
51 #include "frontends/alert.h"
52 #include "frontends/Application.h"
54 #include "support/ConsoleApplication.h"
55 #include "support/convert.h"
56 #include "support/lassert.h"
57 #include "support/debug.h"
58 #include "support/environment.h"
59 #include "support/ExceptionMessage.h"
60 #include "support/filetools.h"
61 #include "support/gettext.h"
62 #include "support/lstrings.h"
63 #include "support/Messages.h"
64 #include "support/os.h"
65 #include "support/Package.h"
66 #include "support/unique_ptr.h"
77 #include <qglobal.h> // For QT_VERSION
80 using namespace lyx::support;
82 #if defined (USE_MACOSX_PACKAGING)
83 #include <crt_externs.h>
88 namespace Alert = frontend::Alert;
89 namespace os = support::os;
93 // Are we using the GUI at all? We default to true and this is changed
94 // to false when the export feature is used.
99 // Report on the terminal about spawned commands. The default is false
100 // and can be changed with the option -v (--verbose).
102 bool verbose = false;
105 // Do not treat the "missing glyphs" warning of fontspec as an error message.
106 // The default is false and can be changed with the option
107 // --ignore-error-message missing_glyphs
108 // This is used in automated testing.
109 bool ignore_missing_glyphs = false;
112 // We default to open documents in an already running instance, provided that
113 // the lyxpipe has been setup. This can be overridden either on the command
114 // line or through preference settings.
116 RunMode run_mode = PREFERRED;
119 // Tell what files can be silently overwritten during batch export.
120 // Possible values are: NO_FILES, MAIN_FILE, ALL_FILES, UNSPECIFIED.
121 // Unless specified on command line (through the -f switch) or through the
122 // environment variable LYX_FORCE_OVERWRITE, the default will be MAIN_FILE.
124 OverwriteFiles force_overwrite = UNSPECIFIED;
127 // Scale the GUI by this factor. This works whether we have a HiDpi screen
128 // or not and scales everything, also fonts. Can only be changed by setting
129 // the QT_SCALE_FACTOR environment variable before launching LyX and only
130 // works properly with Qt 5.6 or higher.
132 double qt_scale_factor = 1.0;
137 // Filled with the command line arguments "foo" of "-sysdir foo" or
139 string cl_system_support;
140 string cl_user_support;
144 LyX * singleton_ = 0;
146 void showFileError(string const & error)
148 Alert::warning(_("Could not read configuration file"),
149 bformat(_("Error while reading the configuration file\n%1$s.\n"
150 "Please check your installation."), from_utf8(error)));
155 /// The main application class private implementation.
158 : latexfonts_(0), spell_checker_(0), apple_spell_checker_(0), aspell_checker_(0), enchant_checker_(0), hunspell_checker_(0)
164 delete apple_spell_checker_;
165 delete aspell_checker_;
166 delete enchant_checker_;
167 delete hunspell_checker_;
171 BufferList buffer_list_;
173 KeyMap toplevel_keymap_;
175 CmdDef toplevel_cmddef_;
177 unique_ptr<Server> lyx_server_;
179 unique_ptr<ServerSocket> lyx_socket_;
181 unique_ptr<frontend::Application> application_;
182 /// lyx session, containing lastfiles, lastfilepos, and lastopened
183 unique_ptr<Session> session_;
185 /// Files to load at start.
186 vector<string> files_to_load_;
188 /// The messages translators.
189 map<string, Messages> messages_;
191 /// The file converters.
192 Converters converters_;
193 /// The system converters after reading lyxrc.defaults.
194 Converters system_converters_;
196 /// Global format information
198 /// The system formats after reading lyxrc.defaults.
199 Formats system_formats_;
205 Movers system_movers_;
207 /// the parsed command line batch command if any
208 vector<string> batch_commands;
211 LaTeXFonts * latexfonts_;
214 SpellChecker * spell_checker_;
216 SpellChecker * apple_spell_checker_;
218 SpellChecker * aspell_checker_;
220 SpellChecker * enchant_checker_;
222 SpellChecker * hunspell_checker_;
226 /// The main application class for console mode
227 class LyXConsoleApp : public ConsoleApplication
230 LyXConsoleApp(LyX * lyx, int & argc, char * argv[])
231 : ConsoleApplication(lyx_package, argc, argv), lyx_(lyx),
232 argc_(argc), argv_(argv)
237 int const exit_status = lyx_->execWithoutGui(argc_, argv_);
248 frontend::Application * theApp()
251 return singleton_->pimpl_->application_.get();
264 void lyx_exit(int exit_code)
267 // Something wrong happened so better save everything, just in
272 // Properly crash in debug mode in order to get a useful backtrace.
276 // In release mode, try to exit gracefully.
278 theApp()->exit(exit_code);
292 Messages & LyX::messages(string const & language)
294 map<string, Messages>::iterator it = pimpl_->messages_.find(language);
296 if (it != pimpl_->messages_.end())
299 pair<map<string, Messages>::iterator, bool> result =
300 pimpl_->messages_.insert(make_pair(language, Messages(language)));
302 LATTEST(result.second);
303 return result.first->second;
307 int LyX::exec(int & argc, char * argv[])
309 // Minimal setting of locale before parsing command line
311 init_package(os::utf8_argv(0), string(), string());
312 // we do not get to this point when init_package throws an exception
314 } catch (ExceptionMessage const & message) {
315 LYXERR(Debug::LOCALE, message.title_ + ", " + message.details_);
318 // Here we need to parse the command line. At least
319 // we need to parse for "-dbg" and "-help"
320 easyParse(argc, argv);
322 #if QT_VERSION >= 0x050600
323 // Check whether Qt will scale all GUI elements and accordingly
324 // set the scale factor so that to avoid blurred images and text
325 char const * const scale_factor = getenv("QT_SCALE_FACTOR");
327 qt_scale_factor = convert<double>(scale_factor);
328 if (qt_scale_factor < 1.0)
329 qt_scale_factor = 1.0;
334 init_package(os::utf8_argv(0), cl_system_support, cl_user_support);
335 } catch (ExceptionMessage const & message) {
336 if (message.type_ == ErrorException) {
337 Alert::error(message.title_, message.details_);
339 } else if (message.type_ == WarningException) {
340 Alert::warning(message.title_, message.details_);
344 // Reinit the messages machinery in case package() knows
345 // something interesting about the locale directory.
349 LyXConsoleApp app(this, argc, argv);
351 // Reestablish our defaults, as Qt overwrites them
352 // after creating app
358 // Let the frontend parse and remove all arguments that it knows
359 pimpl_->application_.reset(createApplication(argc, argv));
361 // Reestablish our defaults, as Qt overwrites them
362 // after createApplication()
365 // Parse and remove all known arguments in the LyX singleton
366 // Give an error for all remaining ones.
367 int exit_status = init(argc, argv);
369 // Kill the application object before exiting.
370 pimpl_->application_.reset();
376 // If not otherwise specified by a command line option or
377 // by preferences, we default to reuse a running instance.
378 if (run_mode == PREFERRED)
379 run_mode = USE_REMOTE;
382 /* Create a CoreApplication class that will provide the main event loop
383 * and the socket callback registering. With Qt, only QtCore
384 * library would be needed.
385 * When this is done, a server_mode could be created and the following two
386 * line would be moved out from here.
387 * However, note that the first of the two lines below triggers the
388 * "single instance" behavior, which should occur right at this point.
390 // Note: socket callback must be registered after init(argc, argv)
391 // such that package().temp_dir() is properly initialized.
392 pimpl_->lyx_server_.reset(new Server(lyxrc.lyxpipes));
393 pimpl_->lyx_socket_.reset(new ServerSocket(
394 FileName(package().temp_dir().absFileName() + "/lyxsocket")));
396 // Start the real execution loop.
397 if (!pimpl_->lyx_server_->deferredLoadingToOtherInstance())
398 exit_status = pimpl_->application_->exec();
399 else if (!pimpl_->files_to_load_.empty()) {
400 vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
401 vector<string>::const_iterator end = pimpl_->files_to_load_.end();
402 lyxerr << _("The following files could not be loaded:") << endl;
403 for (; it != end; ++it)
404 lyxerr << *it << endl;
413 void LyX::prepareExit()
415 // Clear the clipboard and selection stack:
416 cap::clearCutStack();
417 cap::clearSelection();
419 // Write the index file of the converter cache
420 ConverterCache::get().writeIndex();
422 // closing buffer may throw exceptions, but we ignore them since we
425 // close buffers first
426 pimpl_->buffer_list_.closeAll();
427 } catch (ExceptionMessage const &) {}
429 // register session changes and shutdown server and socket
431 if (pimpl_->session_)
432 pimpl_->session_->writeFile();
433 pimpl_->session_.reset();
434 pimpl_->lyx_server_.reset();
435 pimpl_->lyx_socket_.reset();
438 // do any other cleanup procedures now
439 if (package().temp_dir() != package().system_temp_dir()) {
440 string const abs_tmpdir = package().temp_dir().absFileName();
441 if (!contains(package().temp_dir().absFileName(), "lyx_tmpdir")) {
442 docstring const msg =
443 bformat(_("%1$s does not appear like a LyX created temporary directory."),
444 from_utf8(abs_tmpdir));
445 Alert::warning(_("Cannot remove temporary directory"), msg);
447 LYXERR(Debug::INFO, "Deleting tmp dir "
448 << package().temp_dir().absFileName());
449 if (!package().temp_dir().destroyDirectory()) {
450 LYXERR0(bformat(_("Unable to remove the temporary directory %1$s"),
451 from_utf8(package().temp_dir().absFileName())));
456 // Kill the application object before exiting. This avoids crashes
457 // when exiting on Linux.
458 pimpl_->application_.reset();
462 void LyX::earlyExit(int status)
464 LATTEST(pimpl_->application_.get());
465 // LyX::pimpl_::application_ is not initialised at this
466 // point so it's safe to just exit after some cleanup.
472 int LyX::init(int & argc, char * argv[])
474 // check for any spurious extra arguments
475 // other than documents
476 for (int argi = 1; argi < argc ; ++argi) {
477 if (argv[argi][0] == '-') {
479 bformat(_("Wrong command line option `%1$s'. Exiting."),
480 from_utf8(os::utf8_argv(argi)))) << endl;
485 // Initialization of LyX (reads lyxrc and more)
487 LYXERR(Debug::INIT, "Initializing LyX::init...");
488 bool success = init();
489 LYXERR(Debug::INIT, "Initializing LyX::init...done");
492 } catch (exception const &e) {
493 // This can happen _in_theory_ in replaceEnvironmentPath
494 lyxerr << "Caught exception `" << e.what() << "'." << endl;
497 // Remaining arguments are assumed to be files to load.
498 for (int argi = 1; argi < argc; ++argi)
499 pimpl_->files_to_load_.push_back(os::utf8_argv(argi));
501 if (!use_gui && pimpl_->files_to_load_.empty()) {
502 lyxerr << to_utf8(_("Missing filename for this operation.")) << endl;
507 pimpl_->files_to_load_.push_back(
508 i18nLibFileSearch("examples", "splash.lyx").absFileName());
515 int LyX::execWithoutGui(int & argc, char * argv[])
517 int exit_status = init(argc, argv);
523 // Used to keep track of which buffers were explicitly loaded by user request.
524 // This is necessary because master and child document buffers are loaded, even
525 // if they were not named on the command line. We do not want to dispatch to
527 vector<Buffer *> command_line_buffers;
529 // Load the files specified on the command line
530 vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
531 vector<string>::const_iterator end = pimpl_->files_to_load_.end();
532 for (; it != end; ++it) {
533 // get absolute path of file and add ".lyx" to the filename if necessary
534 FileName fname = fileSearch(string(), os::internal_path(*it), "lyx",
540 Buffer * buf = pimpl_->buffer_list_.newBuffer(fname.absFileName());
541 LYXERR(Debug::FILES, "Loading " << fname);
542 if (buf && buf->loadLyXFile() == Buffer::ReadSuccess) {
543 ErrorList const & el = buf->errorList("Parse");
544 for(ErrorItem const & e : el)
546 command_line_buffers.push_back(buf);
549 pimpl_->buffer_list_.release(buf);
550 docstring const error_message =
551 bformat(_("LyX failed to load the following file: %1$s"),
552 from_utf8(fname.absFileName()));
553 lyxerr << to_utf8(error_message) << endl;
554 exit_status = 1; // failed
558 if (exit_status || pimpl_->batch_commands.empty() || pimpl_->buffer_list_.empty()) {
563 // Iterate through the buffers that were specified on the command line
564 bool final_success = false;
565 vector<Buffer *>::iterator buf_it = command_line_buffers.begin();
566 for (; buf_it != command_line_buffers.end(); ++buf_it) {
567 Buffer * buf = *buf_it;
568 vector<string>::const_iterator bcit = pimpl_->batch_commands.begin();
569 vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
571 for (; bcit != bcend; ++bcit) {
572 LYXERR(Debug::ACTION, "Buffer::dispatch: cmd: " << *bcit);
573 buf->dispatch(*bcit, dr);
574 final_success |= !dr.error();
578 return !final_success;
582 void execBatchCommands()
585 singleton_->execCommands();
589 void LyX::execCommands()
591 // The advantage of doing this here is that the event loop
592 // is already started. So any need for interaction will be
595 // if reconfiguration is needed.
596 if (LayoutFileList::get().empty()) {
597 switch (Alert::prompt(
598 _("No textclass is found"),
599 _("LyX will only have minimal functionality because no textclasses "
600 "have been found. You can either try to reconfigure LyX normally, "
601 "try to reconfigure without checking your LaTeX installation, or continue."),
608 // regular reconfigure
609 lyx::dispatch(FuncRequest(LFUN_RECONFIGURE, ""));
612 // reconfigure --without-latex-config
613 lyx::dispatch(FuncRequest(LFUN_RECONFIGURE,
614 " --without-latex-config"));
621 // create the first main window
622 lyx::dispatch(FuncRequest(LFUN_WINDOW_NEW, geometryArg));
624 if (!pimpl_->files_to_load_.empty()) {
625 // if some files were specified at command-line we assume that the
626 // user wants to edit *these* files and not to restore the session.
627 for (size_t i = 0; i != pimpl_->files_to_load_.size(); ++i) {
629 FuncRequest(LFUN_FILE_OPEN, pimpl_->files_to_load_[i]));
631 // clear this list to save a few bytes of RAM
632 pimpl_->files_to_load_.clear();
634 pimpl_->application_->restoreGuiSession();
636 // Execute batch commands if available
637 if (pimpl_->batch_commands.empty())
640 vector<string>::const_iterator bcit = pimpl_->batch_commands.begin();
641 vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
642 for (; bcit != bcend; ++bcit) {
643 LYXERR(Debug::INIT, "About to handle -x '" << *bcit << '\'');
644 lyx::dispatch(lyxaction.lookupFunc(*bcit));
652 The SIGHUP signal does not exist on Windows and does not need to be handled.
654 Windows handles SIGFPE and SIGSEGV signals as expected.
656 Ctrl+C interrupts (mapped to SIGINT by Windows' POSIX compatability layer)
657 cause a new thread to be spawned. This may well result in unexpected
658 behaviour by the single-threaded LyX.
660 SIGTERM signals will come only from another process actually sending
661 that signal using 'raise' in Windows' POSIX compatability layer. It will
662 not come from the general "terminate process" methods that everyone
663 actually uses (and which can't be trapped). Killing an app 'politely' on
664 Windows involves first sending a WM_CLOSE message, something that is
665 caught already by the Qt frontend.
667 For more information see:
669 http://aspn.activestate.com/ASPN/Mail/Message/ActiveTcl/2034055
670 ...signals are mostly useless on Windows for a variety of reasons that are
673 'UNIX Application Migration Guide, Chapter 9'
674 http://msdn.microsoft.com/library/en-us/dnucmg/html/UCMGch09.asp
676 'How To Terminate an Application "Cleanly" in Win32'
677 http://support.microsoft.com/default.aspx?scid=kb;en-us;178893
681 static void error_handler(int err_sig)
683 // Throw away any signals other than the first one received.
684 static sig_atomic_t handling_error = false;
687 handling_error = true;
689 // We have received a signal indicating a fatal error, so
690 // try and save the data ASAP.
693 // These lyxerr calls may or may not work:
695 // Signals are asynchronous, so the main program may be in a very
696 // fragile state when a signal is processed and thus while a signal
697 // handler function executes.
698 // In general, therefore, we should avoid performing any
699 // I/O operations or calling most library and system functions from
702 // This shouldn't matter here, however, as we've already invoked
708 msg = _("SIGHUP signal caught!\nBye.");
712 msg = _("SIGFPE signal caught!\nBye.");
715 msg = _("SIGSEGV signal caught!\n"
716 "Sorry, you have found a bug in LyX, "
717 "hope you have not lost any data.\n"
718 "Please read the bug-reporting instructions "
719 "in 'Help->Introduction' and send us a bug report, "
720 "if necessary. Thanks!\nBye.");
729 lyxerr << "\nlyx: " << msg << endl;
730 // try to make a GUI message
731 Alert::error(_("LyX crashed!"), msg, true);
734 // Deinstall the signal handlers
736 signal(SIGHUP, SIG_DFL);
738 signal(SIGINT, SIG_DFL);
739 signal(SIGFPE, SIG_DFL);
740 signal(SIGSEGV, SIG_DFL);
741 signal(SIGTERM, SIG_DFL);
744 if (err_sig == SIGSEGV ||
745 (err_sig != SIGHUP && !getEnv("LYXDEBUG").empty())) {
747 if (err_sig == SIGSEGV || !getEnv("LYXDEBUG").empty()) {
750 // with abort() it crashes again.
763 void LyX::printError(ErrorItem const & ei)
765 docstring tmp = _("LyX: ") + ei.error + char_type(':')
767 cerr << to_utf8(tmp) << endl;
770 #if defined (USE_MACOSX_PACKAGING)
772 // Unexposed--extract an environment variable name from its NAME=VALUE
774 std::string varname(const char* line)
776 size_t nameLen = strcspn(line, "=");
777 if (nameLen == strlen(line)) {
778 return std::string();
780 return std::string(line, nameLen);
785 void cleanDuplicateEnvVars()
787 std::set<std::string> seen;
788 std::set<std::string> dupes;
790 // Create a list of the environment variables that appear more than once
791 for (char **read = *_NSGetEnviron(); *read; read++) {
792 std::string name = varname(*read);
793 if (name.size() == 0) {
796 if (seen.find(name) != seen.end()) {
803 // Loop over the list of duplicated variables
804 std::set<std::string>::iterator dupe = dupes.begin();
805 std::set<std::string>::iterator const dend = dupes.end();
806 for (; dupe != dend; ++dupe) {
807 const char *name = (*dupe).c_str();
808 char *val = getenv(name);
810 LYXERR(Debug::INIT, "Duplicate environment variable: " << name);
811 // unsetenv removes *all* instances of the variable from the environment
814 // replace with the value from getenv (in practice appears to be the
815 // first value in the list)
816 setenv(name, val, 0);
823 static void initTemplatePath()
825 FileName const package_template_path =
826 FileName(addName(package().system_support().absFileName(), "templates"));
828 if (lyxrc.template_path.empty()) {
829 lyxrc.template_path = package_template_path.absFileName();
831 #if defined (USE_MACOSX_PACKAGING)
832 FileName const user_template_path =
833 FileName(addName(package().user_support().absFileName(), "templates"));
835 if (package_template_path != FileName(lyxrc.template_path) &&
836 user_template_path != FileName(lyxrc.template_path))
840 FileName const user_template_link =
841 FileName(addName(user_template_path.absFileName(),"SystemTemplates"));
842 if (user_template_link.isSymLink() && !equivalent(user_template_link, package_template_path)) {
843 user_template_link.removeFile();
845 if (!user_template_link.exists()) {
846 if (!package_template_path.link(user_template_link)) {
847 FileName const user_support = package().user_support();
848 if (user_support.exists() && user_support.isDirectory()) {
849 LYXERR(Debug::INIT, "Cannot create symlink " + user_template_link.absFileName());
850 lyxrc.template_path = package_template_path.absFileName();
854 LYXERR(Debug::INIT, "Symlink \"" << user_template_link.absFileName() << "\" created.");
856 lyxrc.template_path = user_template_path.absFileName();
864 signal(SIGHUP, error_handler);
866 signal(SIGFPE, error_handler);
867 signal(SIGSEGV, error_handler);
868 signal(SIGINT, error_handler);
869 signal(SIGTERM, error_handler);
870 // SIGPIPE can be safely ignored.
872 #if defined (USE_MACOSX_PACKAGING)
873 cleanDuplicateEnvVars();
876 lyxrc.tempdir_path = package().temp_dir().absFileName();
877 lyxrc.document_path = package().document_dir().absFileName();
879 if (lyxrc.example_path.empty()) {
880 lyxrc.example_path = addPath(package().system_support().absFileName(),
885 // init LyXDir environment variable
886 string const lyx_dir = package().lyx_dir().absFileName();
887 LYXERR(Debug::INIT, "Setting LyXDir... to \"" << lyx_dir << "\"");
888 if (!setEnv("LyXDir", lyx_dir))
889 LYXERR(Debug::INIT, "\t... failed!");
891 if (package().explicit_user_support() && getEnv(LYX_USERDIR_VER).empty()) {
892 // -userdir was given on the command line.
893 // Make it available to child processes, otherwise tex2lyx
894 // would not find all layout files, and other converters might
896 string const user_dir = package().user_support().absFileName();
897 LYXERR(Debug::INIT, "Setting " LYX_USERDIR_VER "... to \""
899 if (!setEnv(LYX_USERDIR_VER, user_dir))
900 LYXERR(Debug::INIT, "\t... failed!");
904 // Read configuration files
907 // This one may have been distributed along with LyX.
908 if (!readRcFile("lyxrc.dist"))
911 // Set the PATH correctly.
912 #if !defined (USE_POSIX_PACKAGING) && !defined (USE_HAIKU_PACKAGING)
913 // Add the directory containing the LyX executable to the path
914 // so that LyX can find things like tex2lyx.
915 if (package().build_support().empty())
916 prependEnvPath("PATH", package().binary_dir().absFileName());
918 if (!lyxrc.path_prefix.empty())
919 prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
921 // Check that user LyX directory is ok.
923 string const lock_file = package().getConfigureLockName();
924 int fd = fileLock(lock_file.c_str());
926 if (queryUserLyXDir(package().explicit_user_support())) {
927 package().reconfigureUserLyXDir("");
928 // Now the user directory is present on first start.
931 fileUnlock(fd, lock_file.c_str());
935 // No need for a splash when there is no GUI
937 // Default is to overwrite the main file during export, unless
938 // the -f switch was specified or LYX_FORCE_OVERWRITE was set
939 if (force_overwrite == UNSPECIFIED) {
940 string const what = getEnv("LYX_FORCE_OVERWRITE");
942 force_overwrite = ALL_FILES;
943 else if (what == "none")
944 force_overwrite = NO_FILES;
946 force_overwrite = MAIN_FILE;
950 // This one is generated in user_support directory by lib/configure.py.
951 if (!readRcFile("lyxrc.defaults"))
954 // Query the OS to know what formats are viewed natively
955 theFormats().setAutoOpen();
957 // Read lyxrc.dist again to be able to override viewer auto-detection.
958 readRcFile("lyxrc.dist");
960 system_lyxrc = lyxrc;
961 theSystemFormats() = theFormats();
962 pimpl_->system_converters_ = pimpl_->converters_;
963 pimpl_->system_movers_ = pimpl_->movers_;
964 system_lcolor = lcolor;
966 // This one is edited through the preferences dialog.
967 if (!readRcFile("preferences", true))
970 // The language may have been set to someting useful through prefs
973 if (!readEncodingsFile("encodings", "unicodesymbols"))
975 if (!readLanguagesFile("languages"))
978 LYXERR(Debug::INIT, "Reading layouts...");
980 LayoutFileList::get().read();
982 theModuleList.read();
983 //... and the cite engines
984 theCiteEnginesList.read();
986 // read keymap and ui files in batch mode as well
987 // because InsetInfo needs to know these to produce
988 // the correct output
990 // Set up command definitions
991 pimpl_->toplevel_cmddef_.read(lyxrc.def_file);
995 pimpl_->toplevel_keymap_.read("site");
996 pimpl_->toplevel_keymap_.read(lyxrc.bind_file);
997 // load user bind file user.bind
998 pimpl_->toplevel_keymap_.read("user", 0, KeyMap::MissingOK);
1000 if (lyxerr.debugging(Debug::LYXRC))
1003 os::windows_style_tex_paths(lyxrc.windows_style_tex_paths);
1004 // Prepend path prefix a second time to take the user preferences into a account
1005 if (!lyxrc.path_prefix.empty())
1006 prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
1008 FileName const document_path(lyxrc.document_path);
1009 if (document_path.exists() && document_path.isDirectory())
1010 package().document_dir() = document_path;
1012 package().set_temp_dir(createLyXTmpDir(FileName(lyxrc.tempdir_path)));
1013 if (package().temp_dir().empty()) {
1014 Alert::error(_("Could not create temporary directory"),
1015 bformat(_("Could not create a temporary directory in\n"
1017 "Make sure that this path exists and is writable and try again."),
1018 from_utf8(lyxrc.tempdir_path)));
1019 // createLyXTmpDir() tries sufficiently hard to create a
1020 // usable temp dir, so the probability to come here is
1021 // close to zero. We therefore don't try to overcome this
1022 // problem with e.g. asking the user for a new path and
1023 // trying again but simply exit.
1027 LYXERR(Debug::INIT, "LyX tmp dir: `"
1028 << package().temp_dir().absFileName() << '\'');
1030 LYXERR(Debug::INIT, "Reading session information '.lyx/session'...");
1031 pimpl_->session_.reset(new Session(lyxrc.num_lastfiles));
1033 // This must happen after package initialization and after lyxrc is
1034 // read, therefore it can't be done by a static object.
1035 ConverterCache::init();
1041 void emergencyCleanup()
1043 // what to do about tmpfiles is non-obvious. we would
1044 // like to delete any we find, but our lyxdir might
1045 // contain documents etc. which might be helpful on
1048 singleton_->pimpl_->buffer_list_.emergencyWriteAll();
1050 if (singleton_->pimpl_->lyx_server_)
1051 singleton_->pimpl_->lyx_server_->emergencyCleanup();
1052 singleton_->pimpl_->lyx_server_.reset();
1053 singleton_->pimpl_->lyx_socket_.reset();
1058 bool LyX::queryUserLyXDir(bool explicit_userdir)
1060 // Does user directory exist?
1061 FileName const sup = package().user_support();
1062 if (sup.exists() && sup.isDirectory()) {
1063 first_start = false;
1065 return configFileNeedsUpdate("lyxrc.defaults")
1066 || configFileNeedsUpdate("lyxmodules.lst")
1067 || configFileNeedsUpdate("textclass.lst")
1068 || configFileNeedsUpdate("packages.lst")
1069 || configFileNeedsUpdate("lyxciteengines.lst")
1070 || configFileNeedsUpdate("xtemplates.lst");
1073 first_start = !explicit_userdir;
1075 // If the user specified explicitly a directory, ask whether
1076 // to create it. If the user says "no", then exit.
1077 if (explicit_userdir &&
1079 _("Missing user LyX directory"),
1080 bformat(_("You have specified a non-existent user "
1081 "LyX directory, %1$s.\n"
1082 "It is needed to keep your own configuration."),
1083 from_utf8(package().user_support().absFileName())),
1085 _("&Create directory"),
1087 lyxerr << to_utf8(_("No user LyX directory. Exiting.")) << endl;
1088 earlyExit(EXIT_FAILURE);
1091 lyxerr << to_utf8(bformat(_("LyX: Creating directory %1$s"),
1092 from_utf8(sup.absFileName()))) << endl;
1094 if (!sup.createDirectory(0755)) {
1095 // Failed, so let's exit.
1096 lyxerr << to_utf8(_("Failed to create directory. Exiting."))
1098 earlyExit(EXIT_FAILURE);
1105 bool LyX::readRcFile(string const & name, bool check_format)
1107 LYXERR(Debug::INIT, "About to read " << name << "... ");
1109 FileName const lyxrc_path = libFileSearch(string(), name);
1110 if (lyxrc_path.empty()) {
1111 LYXERR(Debug::INIT, "Not found." << lyxrc_path);
1113 // This was the previous logic, but can it be right??
1116 LYXERR(Debug::INIT, "Found in " << lyxrc_path);
1117 bool const success = lyxrc.read(lyxrc_path, check_format);
1119 showFileError(name);
1123 // Read the languages file `name'
1124 bool LyX::readLanguagesFile(string const & name)
1126 LYXERR(Debug::INIT, "About to read " << name << "...");
1128 FileName const lang_path = libFileSearch(string(), name);
1129 if (lang_path.empty()) {
1130 showFileError(name);
1133 languages.read(lang_path);
1138 // Read the encodings file `name'
1139 bool LyX::readEncodingsFile(string const & enc_name,
1140 string const & symbols_name)
1142 LYXERR(Debug::INIT, "About to read " << enc_name << " and "
1143 << symbols_name << "...");
1145 FileName const symbols_path = libFileSearch(string(), symbols_name);
1146 if (symbols_path.empty()) {
1147 showFileError(symbols_name);
1151 FileName const enc_path = libFileSearch(string(), enc_name);
1152 if (enc_path.empty()) {
1153 showFileError(enc_name);
1156 encodings.read(enc_path, symbols_path);
1163 /// return the the number of arguments consumed
1164 typedef function<int(string const &, string const &, string &)> cmd_helper;
1166 int parse_dbg(string const & arg, string const &, string &)
1169 cout << to_utf8(_("List of supported debug flags:")) << endl;
1170 Debug::showTags(cout);
1173 lyxerr << to_utf8(bformat(_("Setting debug level to %1$s"), from_utf8(arg))) << endl;
1175 lyxerr.setLevel(Debug::value(arg));
1176 Debug::showLevel(lyxerr, lyxerr.level());
1181 int parse_help(string const &, string const &, string &)
1184 to_utf8(_("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
1185 "Command line switches (case sensitive):\n"
1186 "\t-help summarize LyX usage\n"
1187 "\t-userdir dir set user directory to dir\n"
1188 "\t-sysdir dir set system directory to dir\n"
1189 "\t-geometry WxH+X+Y set geometry of the main window\n"
1190 "\t-dbg feature[,feature]...\n"
1191 " select the features to debug.\n"
1192 " Type `lyx -dbg' to see the list of features\n"
1193 "\t-x [--execute] command\n"
1194 " where command is a lyx command.\n"
1195 "\t-e [--export] fmt\n"
1196 " where fmt is the export format of choice. Look in\n"
1197 " Tools->Preferences->File Handling->File Formats->Short Name\n"
1198 " to see which parameter (which differs from the format name\n"
1199 " in the File->Export menu) should be passed. To export to\n"
1200 " the document's default output format, use 'default'.\n"
1201 " Note that the order of -e and -x switches matters.\n"
1202 "\t-E [--export-to] fmt filename\n"
1203 " where fmt is the export format of choice (see --export),\n"
1204 " and filename is the destination filename.\n"
1205 "\t-i [--import] fmt file.xxx\n"
1206 " where fmt is the import format of choice\n"
1207 " and file.xxx is the file to be imported.\n"
1208 "\t-f [--force-overwrite] what\n"
1209 " where what is either `all', `main' or `none',\n"
1210 " specifying whether all files, main file only, or no files,\n"
1211 " respectively, are to be overwritten during a batch export.\n"
1212 " Anything else is equivalent to `all', but is not consumed.\n"
1213 "\t--ignore-error-message which\n"
1214 " allows you to ignore specific LaTeX error messages.\n"
1215 " Do not use for final documents! Currently supported values:\n"
1216 " * missing_glyphs: Fontspec `missing glyphs' error.\n"
1217 "\t-n [--no-remote]\n"
1218 " open documents in a new instance\n"
1220 " open documents in an already running instance\n"
1221 " (a working lyxpipe is needed)\n"
1222 "\t-v [--verbose]\n"
1223 " report on terminal about spawned commands.\n"
1224 "\t-batch execute commands without launching GUI and exit.\n"
1225 "\t-version summarize version and build info\n"
1226 "Check the LyX man page for more details.")) << endl;
1232 int parse_version(string const &, string const &, string &)
1234 cout << "LyX " << lyx_version
1235 << " (" << lyx_release_date << ")" << endl;
1236 if (string(lyx_git_commit_hash) != "none")
1237 cout << to_utf8(_(" Git commit hash "))
1238 << string(lyx_git_commit_hash).substr(0,8) << endl;
1239 cout << lyx_version_info << endl;
1245 int parse_sysdir(string const & arg, string const &, string &)
1248 Alert::error(_("No system directory"),
1249 _("Missing directory for -sysdir switch"));
1252 cl_system_support = arg;
1257 int parse_userdir(string const & arg, string const &, string &)
1260 Alert::error(_("No user directory"),
1261 _("Missing directory for -userdir switch"));
1264 cl_user_support = arg;
1269 int parse_execute(string const & arg, string const &, string & batch)
1272 Alert::error(_("Incomplete command"),
1273 _("Missing command string after --execute switch"));
1281 int parse_export_to(string const & type, string const & output_file, string & batch)
1284 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1285 "--export-to switch")) << endl;
1288 if (output_file.empty()) {
1289 lyxerr << to_utf8(_("Missing destination filename after "
1290 "--export-to switch")) << endl;
1293 batch = "buffer-export " + type + " " + output_file;
1299 int parse_export(string const & type, string const &, string & batch)
1302 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1303 "--export switch")) << endl;
1306 batch = "buffer-export " + type;
1312 int parse_import(string const & type, string const & file, string & batch)
1315 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1316 "--import switch")) << endl;
1320 lyxerr << to_utf8(_("Missing filename for --import")) << endl;
1323 batch = "buffer-import " + type + ' ' + file;
1328 int parse_geometry(string const & arg1, string const &, string &)
1331 // don't remove "-geometry", it will be pruned out later in the
1332 // frontend if need be.
1337 int parse_batch(string const &, string const &, string &)
1344 int parse_noremote(string const &, string const &, string &)
1346 run_mode = NEW_INSTANCE;
1351 int parse_remote(string const &, string const &, string &)
1353 run_mode = USE_REMOTE;
1358 int parse_verbose(string const &, string const &, string &)
1365 int parse_ignore_error_message(string const & arg1, string const &, string &)
1367 if (arg1 == "missing_glyphs") {
1368 ignore_missing_glyphs = true;
1375 int parse_force(string const & arg, string const &, string &)
1378 force_overwrite = ALL_FILES;
1380 } else if (arg == "main") {
1381 force_overwrite = MAIN_FILE;
1383 } else if (arg == "none") {
1384 force_overwrite = NO_FILES;
1387 force_overwrite = ALL_FILES;
1395 void LyX::easyParse(int & argc, char * argv[])
1397 map<string, cmd_helper> cmdmap;
1399 cmdmap["-dbg"] = parse_dbg;
1400 cmdmap["-help"] = parse_help;
1401 cmdmap["--help"] = parse_help;
1402 cmdmap["-version"] = parse_version;
1403 cmdmap["--version"] = parse_version;
1404 cmdmap["-sysdir"] = parse_sysdir;
1405 cmdmap["-userdir"] = parse_userdir;
1406 cmdmap["-x"] = parse_execute;
1407 cmdmap["--execute"] = parse_execute;
1408 cmdmap["-e"] = parse_export;
1409 cmdmap["--export"] = parse_export;
1410 cmdmap["-E"] = parse_export_to;
1411 cmdmap["--export-to"] = parse_export_to;
1412 cmdmap["-i"] = parse_import;
1413 cmdmap["--import"] = parse_import;
1414 cmdmap["-geometry"] = parse_geometry;
1415 cmdmap["-batch"] = parse_batch;
1416 cmdmap["-f"] = parse_force;
1417 cmdmap["--force-overwrite"] = parse_force;
1418 cmdmap["-n"] = parse_noremote;
1419 cmdmap["--no-remote"] = parse_noremote;
1420 cmdmap["-r"] = parse_remote;
1421 cmdmap["--remote"] = parse_remote;
1422 cmdmap["-v"] = parse_verbose;
1423 cmdmap["--verbose"] = parse_verbose;
1424 cmdmap["--ignore-error-message"] = parse_ignore_error_message;
1426 for (int i = 1; i < argc; ++i) {
1427 map<string, cmd_helper>::const_iterator it
1428 = cmdmap.find(argv[i]);
1430 // don't complain if not found - may be parsed later
1431 if (it == cmdmap.end())
1435 (i + 1 < argc) ? os::utf8_argv(i + 1) : string();
1437 (i + 2 < argc) ? os::utf8_argv(i + 2) : string();
1440 int const remove = 1 + it->second(arg, arg2, batch);
1442 pimpl_->batch_commands.push_back(batch);
1444 // Now, remove used arguments by shifting
1445 // the following ones remove places down.
1447 os::remove_internal_args(i, remove);
1449 for (int j = i; j < argc; ++j)
1450 argv[j] = argv[j + remove];
1457 FuncStatus getStatus(FuncRequest const & action)
1460 return theApp()->getStatus(action);
1464 DispatchResult const & dispatch(FuncRequest const & action)
1467 return theApp()->dispatch(action);
1471 void dispatch(FuncRequest const & action, DispatchResult & dr)
1474 theApp()->dispatch(action, dr);
1478 vector<string> & theFilesToLoad()
1480 LAPPERR(singleton_);
1481 return singleton_->pimpl_->files_to_load_;
1485 BufferList & theBufferList()
1487 LAPPERR(singleton_);
1488 return singleton_->pimpl_->buffer_list_;
1492 Server & theServer()
1494 // FIXME: this should not be use_gui dependent
1496 LAPPERR(singleton_);
1497 return *singleton_->pimpl_->lyx_server_;
1501 ServerSocket & theServerSocket()
1503 // FIXME: this should not be use_gui dependent
1505 LAPPERR(singleton_);
1506 return *singleton_->pimpl_->lyx_socket_;
1510 KeyMap & theTopLevelKeymap()
1512 LAPPERR(singleton_);
1513 return singleton_->pimpl_->toplevel_keymap_;
1517 Formats & theFormats()
1519 LAPPERR(singleton_);
1520 return singleton_->pimpl_->formats_;
1524 Formats & theSystemFormats()
1526 LAPPERR(singleton_);
1527 return singleton_->pimpl_->system_formats_;
1531 Converters & theConverters()
1533 LAPPERR(singleton_);
1534 return singleton_->pimpl_->converters_;
1538 Converters & theSystemConverters()
1540 LAPPERR(singleton_);
1541 return singleton_->pimpl_->system_converters_;
1545 Movers & theMovers()
1547 LAPPERR(singleton_);
1548 return singleton_->pimpl_->movers_;
1552 Mover const & getMover(string const & fmt)
1554 LAPPERR(singleton_);
1555 return singleton_->pimpl_->movers_(fmt);
1559 void setMover(string const & fmt, string const & command)
1561 LAPPERR(singleton_);
1562 singleton_->pimpl_->movers_.set(fmt, command);
1566 Movers & theSystemMovers()
1568 LAPPERR(singleton_);
1569 return singleton_->pimpl_->system_movers_;
1573 Messages const & getMessages(string const & language)
1575 LAPPERR(singleton_);
1576 return singleton_->messages(language);
1580 Messages const & getGuiMessages()
1582 LAPPERR(singleton_);
1583 return singleton_->messages(Messages::guiLanguage());
1587 Session & theSession()
1589 LAPPERR(singleton_);
1590 return *singleton_->pimpl_->session_.get();
1594 LaTeXFonts & theLaTeXFonts()
1596 LAPPERR(singleton_);
1597 if (!singleton_->pimpl_->latexfonts_)
1598 singleton_->pimpl_->latexfonts_ = new LaTeXFonts;
1599 return *singleton_->pimpl_->latexfonts_;
1603 CmdDef & theTopLevelCmdDef()
1605 LAPPERR(singleton_);
1606 return singleton_->pimpl_->toplevel_cmddef_;
1610 SpellChecker * theSpellChecker()
1612 if (!singleton_->pimpl_->spell_checker_)
1614 return singleton_->pimpl_->spell_checker_;
1618 void setSpellChecker()
1620 SpellChecker::ChangeNumber speller_change_number =singleton_->pimpl_->spell_checker_ ?
1621 singleton_->pimpl_->spell_checker_->changeNumber() : 0;
1623 if (lyxrc.spellchecker == "native") {
1624 #if defined(USE_MACOSX_PACKAGING)
1625 if (!singleton_->pimpl_->apple_spell_checker_)
1626 singleton_->pimpl_->apple_spell_checker_ = new AppleSpellChecker;
1627 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->apple_spell_checker_;
1629 singleton_->pimpl_->spell_checker_ = 0;
1631 } else if (lyxrc.spellchecker == "aspell") {
1632 #if defined(USE_ASPELL)
1633 if (!singleton_->pimpl_->aspell_checker_)
1634 singleton_->pimpl_->aspell_checker_ = new AspellChecker;
1635 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->aspell_checker_;
1637 singleton_->pimpl_->spell_checker_ = 0;
1639 } else if (lyxrc.spellchecker == "enchant") {
1640 #if defined(USE_ENCHANT)
1641 if (!singleton_->pimpl_->enchant_checker_)
1642 singleton_->pimpl_->enchant_checker_ = new EnchantChecker;
1643 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->enchant_checker_;
1645 singleton_->pimpl_->spell_checker_ = 0;
1647 } else if (lyxrc.spellchecker == "hunspell") {
1648 #if defined(USE_HUNSPELL)
1649 if (!singleton_->pimpl_->hunspell_checker_)
1650 singleton_->pimpl_->hunspell_checker_ = new HunspellChecker;
1651 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->hunspell_checker_;
1653 singleton_->pimpl_->spell_checker_ = 0;
1656 singleton_->pimpl_->spell_checker_ = 0;
1658 if (singleton_->pimpl_->spell_checker_) {
1659 singleton_->pimpl_->spell_checker_->changeNumber(speller_change_number);
1660 singleton_->pimpl_->spell_checker_->advanceChangeNumber();