]> git.lyx.org Git - lyx.git/blob - src/LyX.cpp
Natbib authoryear uses (Ref1; Ref2) by default.
[lyx.git] / src / LyX.cpp
1 /**
2  * \file LyX.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16 #include <version.h>
17
18 #include "LyX.h"
19
20 #include "AppleSpellChecker.h"
21 #include "AspellChecker.h"
22 #include "Buffer.h"
23 #include "BufferList.h"
24 #include "CmdDef.h"
25 #include "ColorSet.h"
26 #include "ConverterCache.h"
27 #include "Converter.h"
28 #include "CutAndPaste.h"
29 #include "EnchantChecker.h"
30 #include "Encoding.h"
31 #include "ErrorList.h"
32 #include "Format.h"
33 #include "FuncStatus.h"
34 #include "HunspellChecker.h"
35 #include "KeyMap.h"
36 #include "Language.h"
37 #include "LaTeXFonts.h"
38 #include "LayoutFile.h"
39 #include "Lexer.h"
40 #include "LyX.h"
41 #include "LyXAction.h"
42 #include "LyXRC.h"
43 #include "ModuleList.h"
44 #include "Mover.h"
45 #include "Server.h"
46 #include "ServerSocket.h"
47 #include "Session.h"
48 #include "WordList.h"
49
50 #include "frontends/alert.h"
51 #include "frontends/Application.h"
52
53 #include "support/lassert.h"
54 #include "support/debug.h"
55 #include "support/environment.h"
56 #include "support/ExceptionMessage.h"
57 #include "support/filetools.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
60 #include "support/Messages.h"
61 #include "support/os.h"
62 #include "support/Package.h"
63 #include "support/PathChanger.h"
64 #include "support/Systemcall.h"
65
66 #include "support/bind.h"
67 #include <boost/scoped_ptr.hpp>
68
69 #include <algorithm>
70 #include <iostream>
71 #include <csignal>
72 #include <map>
73 #include <stdlib.h>
74 #include <string>
75 #include <vector>
76
77 using namespace std;
78 using namespace lyx::support;
79
80 namespace lyx {
81
82 namespace Alert = frontend::Alert;
83 namespace os = support::os;
84
85
86
87 // Are we using the GUI at all?  We default to true and this is changed
88 // to false when the export feature is used.
89
90 bool use_gui = true;
91
92
93 // We default to open documents in an already running instance, provided that
94 // the lyxpipe has been setup. This can be overridden either on the command
95 // line or through preference settings.
96
97 RunMode run_mode = PREFERRED;
98
99
100 // Tell what files can be silently overwritten during batch export.
101 // Possible values are: NO_FILES, MAIN_FILE, ALL_FILES, UNSPECIFIED.
102 // Unless specified on command line (through the -f switch) or through the
103 // environment variable LYX_FORCE_OVERWRITE, the default will be MAIN_FILE.
104
105 OverwriteFiles force_overwrite = UNSPECIFIED;
106
107
108 namespace {
109
110 // Filled with the command line arguments "foo" of "-sysdir foo" or
111 // "-userdir foo".
112 string cl_system_support;
113 string cl_user_support;
114
115 string geometryArg;
116
117 LyX * singleton_ = 0;
118
119 void showFileError(string const & error)
120 {
121         Alert::warning(_("Could not read configuration file"),
122                        bformat(_("Error while reading the configuration file\n%1$s.\n"
123                            "Please check your installation."), from_utf8(error)));
124 }
125
126
127 void reconfigureUserLyXDir()
128 {
129         string const configure_command = package().configure_command();
130
131         lyxerr << to_utf8(_("LyX: reconfiguring user directory")) << endl;
132         PathChanger p(package().user_support());
133         Systemcall one;
134         one.startscript(Systemcall::Wait, configure_command);
135         lyxerr << "LyX: " << to_utf8(_("Done!")) << endl;
136 }
137
138 } // namespace anon
139
140 /// The main application class private implementation.
141 struct LyX::Impl {
142         Impl()
143                 : latexfonts_(0), spell_checker_(0), apple_spell_checker_(0), aspell_checker_(0), enchant_checker_(0), hunspell_checker_(0)
144         {}
145
146         ~Impl()
147         {
148                 delete latexfonts_;
149                 delete apple_spell_checker_;
150                 delete aspell_checker_;
151                 delete enchant_checker_;
152                 delete hunspell_checker_;
153         }
154
155         ///
156         BufferList buffer_list_;
157         ///
158         KeyMap toplevel_keymap_;
159         ///
160         CmdDef toplevel_cmddef_;
161         ///
162         boost::scoped_ptr<Server> lyx_server_;
163         ///
164         boost::scoped_ptr<ServerSocket> lyx_socket_;
165         ///
166         boost::scoped_ptr<frontend::Application> application_;
167         /// lyx session, containing lastfiles, lastfilepos, and lastopened
168         boost::scoped_ptr<Session> session_;
169
170         /// Files to load at start.
171         vector<string> files_to_load_;
172
173         /// The messages translators.
174         map<string, Messages> messages_;
175
176         /// The file converters.
177         Converters converters_;
178
179         // The system converters copy after reading lyxrc.defaults.
180         Converters system_converters_;
181
182         ///
183         Movers movers_;
184         ///
185         Movers system_movers_;
186
187         /// has this user started lyx for the first time?
188         bool first_start;
189         /// the parsed command line batch command if any
190         vector<string> batch_commands;
191         
192         ///
193         LaTeXFonts * latexfonts_;
194
195         ///
196         SpellChecker * spell_checker_;
197         ///
198         SpellChecker * apple_spell_checker_;
199         ///
200         SpellChecker * aspell_checker_;
201         ///
202         SpellChecker * enchant_checker_;
203         ///
204         SpellChecker * hunspell_checker_;
205 };
206
207
208 ///
209 frontend::Application * theApp()
210 {
211         if (singleton_)
212                 return singleton_->pimpl_->application_.get();
213         else
214                 return 0;
215 }
216
217
218 LyX::~LyX()
219 {
220         delete pimpl_;
221         singleton_ = 0;
222         WordList::cleanupWordLists();
223 }
224
225
226 void lyx_exit(int exit_code)
227 {
228         if (exit_code)
229                 // Something wrong happened so better save everything, just in
230                 // case.
231                 emergencyCleanup();
232
233 #ifndef NDEBUG
234         // Properly crash in debug mode in order to get a useful backtrace.
235         abort();
236 #endif
237
238         // In release mode, try to exit gracefully.
239         if (theApp())
240                 theApp()->exit(exit_code);
241         else
242                 exit(exit_code);
243 }
244
245
246 LyX::LyX()
247         : first_start(false)
248 {
249         singleton_ = this;
250         pimpl_ = new Impl;
251 }
252
253
254 Messages & LyX::messages(string const & language)
255 {
256         map<string, Messages>::iterator it = pimpl_->messages_.find(language);
257
258         if (it != pimpl_->messages_.end())
259                 return it->second;
260
261         pair<map<string, Messages>::iterator, bool> result =
262                         pimpl_->messages_.insert(make_pair(language, Messages(language)));
263
264         LATTEST(result.second);
265         return result.first->second;
266 }
267
268
269 int LyX::exec(int & argc, char * argv[])
270 {
271         // Minimal setting of locale before parsing command line
272         try {
273                 init_package(os::utf8_argv(0), string(), string());
274                 // we do not get to this point when init_package throws an exception
275                 locale_init();
276         } catch (ExceptionMessage const & message) {
277                 LYXERR(Debug::LOCALE, message.title_ + ", " + message.details_);
278         }
279
280         // Here we need to parse the command line. At least
281         // we need to parse for "-dbg" and "-help"
282         easyParse(argc, argv);
283
284         try {
285                 init_package(os::utf8_argv(0), cl_system_support, cl_user_support);
286         } catch (ExceptionMessage const & message) {
287                 if (message.type_ == ErrorException) {
288                         Alert::error(message.title_, message.details_);
289                         lyx_exit(1);
290                 } else if (message.type_ == WarningException) {
291                         Alert::warning(message.title_, message.details_);
292                 }
293         }
294
295         // Reinit the messages machinery in case package() knows
296         // something interesting about the locale directory.
297         Messages::init();
298
299         if (!use_gui) {
300                 // FIXME: create a ConsoleApplication
301                 int exit_status = init(argc, argv);
302                 if (exit_status) {
303                         prepareExit();
304                         return exit_status;
305                 }
306
307                 // this is correct, since return values are inverted.
308                 exit_status = !loadFiles();
309
310                 if (pimpl_->batch_commands.empty() || pimpl_->buffer_list_.empty()) {
311                         prepareExit();
312                         return exit_status;
313                 }
314
315                 BufferList::iterator begin = pimpl_->buffer_list_.begin();
316
317                 bool final_success = false;
318                 for (BufferList::iterator I = begin; I != pimpl_->buffer_list_.end(); ++I) {
319                         Buffer * buf = *I;
320                         if (buf != buf->masterBuffer())
321                                 continue;
322                         vector<string>::const_iterator bcit  = pimpl_->batch_commands.begin();
323                         vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
324                         DispatchResult dr;
325                         for (; bcit != bcend; ++bcit) {
326                                 LYXERR(Debug::ACTION, "Buffer::dispatch: cmd: " << *bcit);
327                                 buf->dispatch(*bcit, dr);
328                                 final_success |= !dr.error();
329                         }
330                 }
331                 prepareExit();
332                 return !final_success;
333         }
334
335         // Let the frontend parse and remove all arguments that it knows
336         pimpl_->application_.reset(createApplication(argc, argv));
337
338         // Reestablish our defaults, as Qt overwrites them
339         // after createApplication()
340         locale_init();
341
342         // Parse and remove all known arguments in the LyX singleton
343         // Give an error for all remaining ones.
344         int exit_status = init(argc, argv);
345         if (exit_status) {
346                 // Kill the application object before exiting.
347                 pimpl_->application_.reset();
348                 use_gui = false;
349                 prepareExit();
350                 return exit_status;
351         }
352
353         // If not otherwise specified by a command line option or
354         // by preferences, we default to reuse a running instance.
355         if (run_mode == PREFERRED)
356                 run_mode = USE_REMOTE;
357
358         // FIXME
359         /* Create a CoreApplication class that will provide the main event loop
360         * and the socket callback registering. With Qt4, only QtCore
361         * library would be needed.
362         * When this is done, a server_mode could be created and the following two
363         * line would be moved out from here.
364         * However, note that the first of the two lines below triggers the
365         * "single instance" behavior, which should occur right at this point.
366         */
367         // Note: socket callback must be registered after init(argc, argv)
368         // such that package().temp_dir() is properly initialized.
369         pimpl_->lyx_server_.reset(new Server(lyxrc.lyxpipes));
370         pimpl_->lyx_socket_.reset(new ServerSocket(
371                         FileName(package().temp_dir().absFileName() + "/lyxsocket")));
372
373         // Start the real execution loop.
374         if (!theServer().deferredLoadingToOtherInstance())
375                 exit_status = pimpl_->application_->exec();
376         else if (!pimpl_->files_to_load_.empty()) {
377                 vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
378                 vector<string>::const_iterator end = pimpl_->files_to_load_.end();
379                 lyxerr << _("The following files could not be loaded:") << endl;
380                 for (; it != end; ++it)
381                         lyxerr << *it << endl;
382         }
383
384         prepareExit();
385
386         return exit_status;
387 }
388
389
390 void LyX::prepareExit()
391 {
392         // Clear the clipboard and selection stack:
393         cap::clearCutStack();
394         cap::clearSelection();
395
396         // Write the index file of the converter cache
397         ConverterCache::get().writeIndex();
398
399         // close buffers first
400         pimpl_->buffer_list_.closeAll();
401
402         // register session changes and shutdown server and socket
403         if (use_gui) {
404                 if (pimpl_->session_)
405                         pimpl_->session_->writeFile();
406                 pimpl_->session_.reset();
407                 pimpl_->lyx_server_.reset();
408                 pimpl_->lyx_socket_.reset();
409         }
410
411         // do any other cleanup procedures now
412         if (package().temp_dir() != package().system_temp_dir()) {
413                 string const abs_tmpdir = package().temp_dir().absFileName();
414                 if (!contains(package().temp_dir().absFileName(), "lyx_tmpdir")) {
415                         docstring const msg =
416                                 bformat(_("%1$s does not appear like a LyX created temporary directory."),
417                                 from_utf8(abs_tmpdir));
418                         Alert::warning(_("Cannot remove temporary directory"), msg);
419                 } else {
420                         LYXERR(Debug::INFO, "Deleting tmp dir "
421                                 << package().temp_dir().absFileName());
422                         if (!package().temp_dir().destroyDirectory()) {
423                                 docstring const msg =
424                                         bformat(_("Unable to remove the temporary directory %1$s"),
425                                         from_utf8(package().temp_dir().absFileName()));
426                                 Alert::warning(_("Unable to remove temporary directory"), msg);
427                         }
428                 }
429         }
430
431         // Kill the application object before exiting. This avoids crashes
432         // when exiting on Linux.
433         pimpl_->application_.reset();
434 }
435
436
437 void LyX::earlyExit(int status)
438 {
439         LATTEST(pimpl_->application_.get());
440         // LyX::pimpl_::application_ is not initialised at this
441         // point so it's safe to just exit after some cleanup.
442         prepareExit();
443         exit(status);
444 }
445
446
447 int LyX::init(int & argc, char * argv[])
448 {
449         // check for any spurious extra arguments
450         // other than documents
451         for (int argi = 1; argi < argc ; ++argi) {
452                 if (argv[argi][0] == '-') {
453                         lyxerr << to_utf8(
454                                 bformat(_("Wrong command line option `%1$s'. Exiting."),
455                                 from_utf8(os::utf8_argv(argi)))) << endl;
456                         return EXIT_FAILURE;
457                 }
458         }
459
460         // Initialization of LyX (reads lyxrc and more)
461         LYXERR(Debug::INIT, "Initializing LyX::init...");
462         bool success = init();
463         LYXERR(Debug::INIT, "Initializing LyX::init...done");
464         if (!success)
465                 return EXIT_FAILURE;
466
467         // Remaining arguments are assumed to be files to load.
468         for (int argi = 1; argi < argc; ++argi)
469                 pimpl_->files_to_load_.push_back(os::utf8_argv(argi));
470
471         if (!use_gui && pimpl_->files_to_load_.empty()) {
472                 lyxerr << to_utf8(_("Missing filename for this operation.")) << endl;
473                 return EXIT_FAILURE;
474         }
475
476         if (first_start) {
477                 pimpl_->files_to_load_.push_back(
478                         i18nLibFileSearch("examples", "splash.lyx").absFileName());
479         }
480
481         return EXIT_SUCCESS;
482 }
483
484
485 bool LyX::loadFiles()
486 {
487         LATTEST(!use_gui);
488         bool success = true;
489         vector<string>::const_iterator it = pimpl_->files_to_load_.begin();
490         vector<string>::const_iterator end = pimpl_->files_to_load_.end();
491
492         for (; it != end; ++it) {
493                 // get absolute path of file and add ".lyx" to
494                 // the filename if necessary
495                 FileName fname = fileSearch(string(), os::internal_path(*it), "lyx",
496                         may_not_exist);
497
498                 if (fname.empty())
499                         continue;
500
501                 Buffer * buf = pimpl_->buffer_list_.newBuffer(fname.absFileName());
502                 if (buf->loadLyXFile() == Buffer::ReadSuccess) {
503                         ErrorList const & el = buf->errorList("Parse");
504                         if (!el.empty())
505                                 for_each(el.begin(), el.end(),
506                                 bind(&LyX::printError, this, _1));
507                 }
508                 else {
509                         pimpl_->buffer_list_.release(buf);
510                         docstring const error_message =
511                                 bformat(_("LyX failed to load the following file: %1$s"),
512                                 from_utf8(fname.absFileName()));
513                         lyxerr << to_utf8(error_message) << endl;
514                         success = false;
515                 }
516         }
517         return success;
518 }
519
520
521 void execBatchCommands()
522 {
523         LAPPERR(singleton_);
524         singleton_->execCommands();
525 }
526
527
528 void LyX::execCommands()
529 {
530         // The advantage of doing this here is that the event loop
531         // is already started. So any need for interaction will be
532         // aknowledged.
533
534         // if reconfiguration is needed.
535         if (LayoutFileList::get().empty()) {
536                 switch (Alert::prompt(
537                         _("No textclass is found"),
538                         _("LyX will only have minimal functionality because no textclasses "
539                                 "have been found. You can either try to reconfigure LyX normally, "
540                                 "try to reconfigure without checking your LaTeX installation, or continue."),
541                         0, 2,
542                         _("&Reconfigure"),
543                         _("&Without LaTeX"),
544                         _("&Continue")))
545                 {
546                 case 0:
547                         // regular reconfigure
548                         lyx::dispatch(FuncRequest(LFUN_RECONFIGURE, ""));
549                         break;
550                 case 1:
551                         // reconfigure --without-latex-config
552                         lyx::dispatch(FuncRequest(LFUN_RECONFIGURE,
553                                 " --without-latex-config"));
554                         break;
555                 default:
556                         break;
557                 }
558         }
559
560         // create the first main window
561         lyx::dispatch(FuncRequest(LFUN_WINDOW_NEW, geometryArg));
562
563         if (!pimpl_->files_to_load_.empty()) {
564                 // if some files were specified at command-line we assume that the
565                 // user wants to edit *these* files and not to restore the session.
566                 for (size_t i = 0; i != pimpl_->files_to_load_.size(); ++i) {
567                         lyx::dispatch(
568                                 FuncRequest(LFUN_FILE_OPEN, pimpl_->files_to_load_[i]));
569                 }
570                 // clear this list to save a few bytes of RAM
571                 pimpl_->files_to_load_.clear();
572         } else
573                 pimpl_->application_->restoreGuiSession();
574
575         // Execute batch commands if available
576         if (pimpl_->batch_commands.empty())
577                 return;
578
579         vector<string>::const_iterator bcit  = pimpl_->batch_commands.begin();
580         vector<string>::const_iterator bcend = pimpl_->batch_commands.end();
581         for (; bcit != bcend; ++bcit) {
582                 LYXERR(Debug::INIT, "About to handle -x '" << *bcit << '\'');
583                 lyx::dispatch(lyxaction.lookupFunc(*bcit));
584         }
585 }
586
587
588 /*
589 Signals and Windows
590 ===================
591 The SIGHUP signal does not exist on Windows and does not need to be handled.
592
593 Windows handles SIGFPE and SIGSEGV signals as expected.
594
595 Ctrl+C interrupts (mapped to SIGINT by Windows' POSIX compatability layer)
596 cause a new thread to be spawned. This may well result in unexpected
597 behaviour by the single-threaded LyX.
598
599 SIGTERM signals will come only from another process actually sending
600 that signal using 'raise' in Windows' POSIX compatability layer. It will
601 not come from the general "terminate process" methods that everyone
602 actually uses (and which can't be trapped). Killing an app 'politely' on
603 Windows involves first sending a WM_CLOSE message, something that is
604 caught already by the Qt frontend.
605
606 For more information see:
607
608 http://aspn.activestate.com/ASPN/Mail/Message/ActiveTcl/2034055
609 ...signals are mostly useless on Windows for a variety of reasons that are
610 Windows specific...
611
612 'UNIX Application Migration Guide, Chapter 9'
613 http://msdn.microsoft.com/library/en-us/dnucmg/html/UCMGch09.asp
614
615 'How To Terminate an Application "Cleanly" in Win32'
616 http://support.microsoft.com/default.aspx?scid=kb;en-us;178893
617 */
618 extern "C" {
619
620 static void error_handler(int err_sig)
621 {
622         // Throw away any signals other than the first one received.
623         static sig_atomic_t handling_error = false;
624         if (handling_error)
625                 return;
626         handling_error = true;
627
628         // We have received a signal indicating a fatal error, so
629         // try and save the data ASAP.
630         emergencyCleanup();
631
632         // These lyxerr calls may or may not work:
633
634         // Signals are asynchronous, so the main program may be in a very
635         // fragile state when a signal is processed and thus while a signal
636         // handler function executes.
637         // In general, therefore, we should avoid performing any
638         // I/O operations or calling most library and system functions from
639         // signal handlers.
640
641         // This shouldn't matter here, however, as we've already invoked
642         // emergencyCleanup.
643         docstring msg;
644         switch (err_sig) {
645 #ifdef SIGHUP
646         case SIGHUP:
647                 msg = _("SIGHUP signal caught!\nBye.");
648                 break;
649 #endif
650         case SIGFPE:
651                 msg = _("SIGFPE signal caught!\nBye.");
652                 break;
653         case SIGSEGV:
654                 msg = _("SIGSEGV signal caught!\n"
655                           "Sorry, you have found a bug in LyX, "
656                           "hope you have not lost any data.\n"
657                           "Please read the bug-reporting instructions "
658                           "in 'Help->Introduction' and send us a bug report, "
659                           "if necessary. Thanks!\nBye.");
660                 break;
661         case SIGINT:
662         case SIGTERM:
663                 // no comments
664                 break;
665         }
666
667         if (!msg.empty()) {
668                 lyxerr << "\nlyx: " << msg << endl;
669                 // try to make a GUI message
670                 Alert::error(_("LyX crashed!"), msg);
671         }
672
673         // Deinstall the signal handlers
674 #ifdef SIGHUP
675         signal(SIGHUP, SIG_DFL);
676 #endif
677         signal(SIGINT, SIG_DFL);
678         signal(SIGFPE, SIG_DFL);
679         signal(SIGSEGV, SIG_DFL);
680         signal(SIGTERM, SIG_DFL);
681
682 #ifdef SIGHUP
683         if (err_sig == SIGSEGV ||
684                 (err_sig != SIGHUP && !getEnv("LYXDEBUG").empty())) {
685 #else
686         if (err_sig == SIGSEGV || !getEnv("LYXDEBUG").empty()) {
687 #endif
688 #ifdef _MSC_VER
689                 // with abort() it crashes again.
690                 exit(err_sig);
691 #else
692                 abort();
693 #endif
694         }
695
696         exit(0);
697 }
698
699 }
700
701
702 void LyX::printError(ErrorItem const & ei)
703 {
704         docstring tmp = _("LyX: ") + ei.error + char_type(':')
705                 + ei.description;
706         cerr << to_utf8(tmp) << endl;
707 }
708
709
710 bool LyX::init()
711 {
712 #ifdef SIGHUP
713         signal(SIGHUP, error_handler);
714 #endif
715         signal(SIGFPE, error_handler);
716         signal(SIGSEGV, error_handler);
717         signal(SIGINT, error_handler);
718         signal(SIGTERM, error_handler);
719         // SIGPIPE can be safely ignored.
720
721         lyxrc.tempdir_path = package().temp_dir().absFileName();
722         lyxrc.document_path = package().document_dir().absFileName();
723
724         if (lyxrc.example_path.empty()) {
725                 lyxrc.example_path = addPath(package().system_support().absFileName(),
726                                               "examples");
727         }
728         if (lyxrc.template_path.empty()) {
729                 lyxrc.template_path = addPath(package().system_support().absFileName(),
730                                               "templates");
731         }
732
733         // init LyXDir environment variable
734         string const lyx_dir = package().lyx_dir().absFileName();
735         LYXERR(Debug::INIT, "Setting LyXDir... to \"" << lyx_dir << "\"");
736         if (!setEnv("LyXDir", lyx_dir))
737                 LYXERR(Debug::INIT, "\t... failed!");
738
739         //
740         // Read configuration files
741         //
742
743         // This one may have been distributed along with LyX.
744         if (!readRcFile("lyxrc.dist"))
745                 return false;
746
747         // Set the PATH correctly.
748 #if !defined (USE_POSIX_PACKAGING)
749         // Add the directory containing the LyX executable to the path
750         // so that LyX can find things like tex2lyx.
751         if (package().build_support().empty())
752                 prependEnvPath("PATH", package().binary_dir().absFileName());
753 #endif
754         if (!lyxrc.path_prefix.empty())
755                 prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
756
757         // Check that user LyX directory is ok.
758         if (queryUserLyXDir(package().explicit_user_support()))
759                 reconfigureUserLyXDir();
760
761         if (!use_gui) {
762                 // No need for a splash when there is no GUI
763                 first_start = false;
764                 // Default is to overwrite the main file during export, unless
765                 // the -f switch was specified or LYX_FORCE_OVERWRITE was set
766                 if (force_overwrite == UNSPECIFIED) {
767                         string const what = getEnv("LYX_FORCE_OVERWRITE");
768                         if (what == "all")
769                                 force_overwrite = ALL_FILES;
770                         else if (what == "none")
771                                 force_overwrite = NO_FILES;
772                         else
773                                 force_overwrite = MAIN_FILE;
774                 }
775         }
776
777         // This one is generated in user_support directory by lib/configure.py.
778         if (!readRcFile("lyxrc.defaults"))
779                 return false;
780
781         // Query the OS to know what formats are viewed natively
782         formats.setAutoOpen();
783
784         // Read lyxrc.dist again to be able to override viewer auto-detection.
785         readRcFile("lyxrc.dist");
786
787         system_lyxrc = lyxrc;
788         system_formats = formats;
789         pimpl_->system_converters_ = pimpl_->converters_;
790         pimpl_->system_movers_ = pimpl_->movers_;
791         system_lcolor = lcolor;
792
793         // This one is edited through the preferences dialog.
794         if (!readRcFile("preferences", true))
795                 return false;
796
797         if (!readEncodingsFile("encodings", "unicodesymbols"))
798                 return false;
799         if (!readLanguagesFile("languages"))
800                 return false;
801
802         LYXERR(Debug::INIT, "Reading layouts...");
803         // Load the layouts
804         LayoutFileList::get().read();
805         //...and the modules
806         theModuleList.read();
807
808         // read keymap and ui files in batch mode as well
809         // because InsetInfo needs to know these to produce
810         // the correct output
811
812         // Set up command definitions
813         pimpl_->toplevel_cmddef_.read(lyxrc.def_file);
814
815         // FIXME
816         // Set up bindings
817         pimpl_->toplevel_keymap_.read("site");
818         pimpl_->toplevel_keymap_.read(lyxrc.bind_file);
819         // load user bind file user.bind
820         pimpl_->toplevel_keymap_.read("user", 0, KeyMap::MissingOK);
821
822         if (lyxerr.debugging(Debug::LYXRC))
823                 lyxrc.print();
824
825         os::windows_style_tex_paths(lyxrc.windows_style_tex_paths);
826         if (!lyxrc.path_prefix.empty())
827                 prependEnvPath("PATH", replaceEnvironmentPath(lyxrc.path_prefix));
828
829         FileName const document_path(lyxrc.document_path);
830         if (document_path.exists() && document_path.isDirectory())
831                 package().document_dir() = document_path;
832
833         package().set_temp_dir(createLyXTmpDir(FileName(lyxrc.tempdir_path)));
834         if (package().temp_dir().empty()) {
835                 Alert::error(_("Could not create temporary directory"),
836                              bformat(_("Could not create a temporary directory in\n"
837                                                        "\"%1$s\"\n"
838                                                            "Make sure that this path exists and is writable and try again."),
839                                      from_utf8(lyxrc.tempdir_path)));
840                 // createLyXTmpDir() tries sufficiently hard to create a
841                 // usable temp dir, so the probability to come here is
842                 // close to zero. We therefore don't try to overcome this
843                 // problem with e.g. asking the user for a new path and
844                 // trying again but simply exit.
845                 return false;
846         }
847
848         LYXERR(Debug::INIT, "LyX tmp dir: `"
849                             << package().temp_dir().absFileName() << '\'');
850
851         LYXERR(Debug::INIT, "Reading session information '.lyx/session'...");
852         pimpl_->session_.reset(new Session(lyxrc.num_lastfiles));
853
854         // This must happen after package initialization and after lyxrc is
855         // read, therefore it can't be done by a static object.
856         ConverterCache::init();
857
858         return true;
859 }
860
861
862 void emergencyCleanup()
863 {
864         // what to do about tmpfiles is non-obvious. we would
865         // like to delete any we find, but our lyxdir might
866         // contain documents etc. which might be helpful on
867         // a crash
868
869         singleton_->pimpl_->buffer_list_.emergencyWriteAll();
870         if (use_gui) {
871                 if (singleton_->pimpl_->lyx_server_)
872                         singleton_->pimpl_->lyx_server_->emergencyCleanup();
873                 singleton_->pimpl_->lyx_server_.reset();
874                 singleton_->pimpl_->lyx_socket_.reset();
875         }
876 }
877
878
879 // return true if file does not exist or is older than configure.py.
880 static bool needsUpdate(string const & file)
881 {
882         // We cannot initialize configure_script directly because the package
883         // is not initialized yet when static objects are constructed.
884         static FileName configure_script;
885         static bool firstrun = true;
886         if (firstrun) {
887                 configure_script =
888                         FileName(addName(package().system_support().absFileName(),
889                                 "configure.py"));
890                 firstrun = false;
891         }
892
893         FileName absfile =
894                 FileName(addName(package().user_support().absFileName(), file));
895         return !absfile.exists()
896                 || configure_script.lastModified() > absfile.lastModified();
897 }
898
899
900 bool LyX::queryUserLyXDir(bool explicit_userdir)
901 {
902         // Does user directory exist?
903         FileName const sup = package().user_support();
904         if (sup.exists() && sup.isDirectory()) {
905                 first_start = false;
906
907                 return needsUpdate("lyxrc.defaults")
908                         || needsUpdate("lyxmodules.lst")
909                         || needsUpdate("textclass.lst")
910                         || needsUpdate("packages.lst");
911         }
912
913         first_start = !explicit_userdir;
914
915         // If the user specified explicitly a directory, ask whether
916         // to create it. If the user says "no", then exit.
917         if (explicit_userdir &&
918             Alert::prompt(
919                     _("Missing user LyX directory"),
920                     bformat(_("You have specified a non-existent user "
921                                            "LyX directory, %1$s.\n"
922                                            "It is needed to keep your own configuration."),
923                             from_utf8(package().user_support().absFileName())),
924                     1, 0,
925                     _("&Create directory"),
926                     _("&Exit LyX"))) {
927                 lyxerr << to_utf8(_("No user LyX directory. Exiting.")) << endl;
928                 earlyExit(EXIT_FAILURE);
929         }
930
931         lyxerr << to_utf8(bformat(_("LyX: Creating directory %1$s"),
932                           from_utf8(sup.absFileName()))) << endl;
933
934         if (!sup.createDirectory(0755)) {
935                 // Failed, so let's exit.
936                 lyxerr << to_utf8(_("Failed to create directory. Exiting."))
937                        << endl;
938                 earlyExit(EXIT_FAILURE);
939         }
940
941         return true;
942 }
943
944
945 bool LyX::readRcFile(string const & name, bool check_format)
946 {
947         LYXERR(Debug::INIT, "About to read " << name << "... ");
948
949         FileName const lyxrc_path = libFileSearch(string(), name);
950         if (lyxrc_path.empty()) {
951                 LYXERR(Debug::INIT, "Not found." << lyxrc_path);
952                 // FIXME
953                 // This was the previous logic, but can it be right??
954                 return true;
955         }
956         LYXERR(Debug::INIT, "Found in " << lyxrc_path);
957         bool const success = lyxrc.read(lyxrc_path, check_format);
958         if (!success)
959                 showFileError(name);
960         return success;
961 }
962
963 // Read the languages file `name'
964 bool LyX::readLanguagesFile(string const & name)
965 {
966         LYXERR(Debug::INIT, "About to read " << name << "...");
967
968         FileName const lang_path = libFileSearch(string(), name);
969         if (lang_path.empty()) {
970                 showFileError(name);
971                 return false;
972         }
973         languages.read(lang_path);
974         return true;
975 }
976
977
978 // Read the encodings file `name'
979 bool LyX::readEncodingsFile(string const & enc_name,
980                             string const & symbols_name)
981 {
982         LYXERR(Debug::INIT, "About to read " << enc_name << " and "
983                             << symbols_name << "...");
984
985         FileName const symbols_path = libFileSearch(string(), symbols_name);
986         if (symbols_path.empty()) {
987                 showFileError(symbols_name);
988                 return false;
989         }
990
991         FileName const enc_path = libFileSearch(string(), enc_name);
992         if (enc_path.empty()) {
993                 showFileError(enc_name);
994                 return false;
995         }
996         encodings.read(enc_path, symbols_path);
997         return true;
998 }
999
1000
1001 namespace {
1002
1003 /// return the the number of arguments consumed
1004 typedef boost::function<int(string const &, string const &, string &)> cmd_helper;
1005
1006 int parse_dbg(string const & arg, string const &, string &)
1007 {
1008         if (arg.empty()) {
1009                 cout << to_utf8(_("List of supported debug flags:")) << endl;
1010                 Debug::showTags(cout);
1011                 exit(0);
1012         }
1013         lyxerr << to_utf8(bformat(_("Setting debug level to %1$s"), from_utf8(arg))) << endl;
1014
1015         lyxerr.setLevel(Debug::value(arg));
1016         Debug::showLevel(lyxerr, lyxerr.level());
1017         return 1;
1018 }
1019
1020
1021 int parse_help(string const &, string const &, string &)
1022 {
1023         cout <<
1024                 to_utf8(_("Usage: lyx [ command line switches ] [ name.lyx ... ]\n"
1025                   "Command line switches (case sensitive):\n"
1026                   "\t-help              summarize LyX usage\n"
1027                   "\t-userdir dir       set user directory to dir\n"
1028                   "\t-sysdir dir        set system directory to dir\n"
1029                   "\t-geometry WxH+X+Y  set geometry of the main window\n"
1030                   "\t-dbg feature[,feature]...\n"
1031                   "                  select the features to debug.\n"
1032                   "                  Type `lyx -dbg' to see the list of features\n"
1033                   "\t-x [--execute] command\n"
1034                   "                  where command is a lyx command.\n"
1035                   "\t-e [--export] fmt\n"
1036                   "                  where fmt is the export format of choice. Look in\n"
1037                   "                  Tools->Preferences->File Handling->File Formats->Short Name\n"
1038                   "                  to see which parameter (which differs from the format name\n"
1039                   "                  in the File->Export menu) should be passed.\n"
1040                   "                  Note that the order of -e and -x switches matters.\n"
1041                   "\t-E [--export-to] fmt filename\n"
1042                   "                  where fmt is the export format of choice (see --export),\n"
1043                   "                  and filename is the destination filename.\n"
1044                   "\t-i [--import] fmt file.xxx\n"
1045                   "                  where fmt is the import format of choice\n"
1046                   "                  and file.xxx is the file to be imported.\n"
1047                   "\t-f [--force-overwrite] what\n"
1048                   "                  where what is either `all', `main' or `none',\n"
1049                   "                  specifying whether all files, main file only, or no files,\n"
1050                   "                  respectively, are to be overwritten during a batch export.\n"
1051                   "                  Anything else is equivalent to `all', but is not consumed.\n"
1052                   "\t-n [--no-remote]\n"
1053                   "                  open documents in a new instance\n"
1054                   "\t-r [--remote]\n"
1055                   "                  open documents in an already running instance\n"
1056                   "                  (a working lyxpipe is needed)\n"
1057                   "\t-batch    execute commands without launching GUI and exit.\n"
1058                   "\t-version  summarize version and build info\n"
1059                                "Check the LyX man page for more details.")) << endl;
1060         exit(0);
1061         return 0;
1062 }
1063
1064
1065 int parse_version(string const &, string const &, string &)
1066 {
1067         cout << "LyX " << lyx_version
1068                << " (" << lyx_release_date << ")" << endl;
1069         cout << to_utf8(bformat(_("Built on %1$s, %2$s"),
1070                 from_ascii(__DATE__), from_ascii(__TIME__))) << endl;
1071
1072         cout << lyx_version_info << endl;
1073         exit(0);
1074         return 0;
1075 }
1076
1077
1078 int parse_sysdir(string const & arg, string const &, string &)
1079 {
1080         if (arg.empty()) {
1081                 Alert::error(_("No system directory"),
1082                         _("Missing directory for -sysdir switch"));
1083                 exit(1);
1084         }
1085         cl_system_support = arg;
1086         return 1;
1087 }
1088
1089
1090 int parse_userdir(string const & arg, string const &, string &)
1091 {
1092         if (arg.empty()) {
1093                 Alert::error(_("No user directory"),
1094                         _("Missing directory for -userdir switch"));
1095                 exit(1);
1096         }
1097         cl_user_support = arg;
1098         return 1;
1099 }
1100
1101
1102 int parse_execute(string const & arg, string const &, string & batch)
1103 {
1104         if (arg.empty()) {
1105                 Alert::error(_("Incomplete command"),
1106                         _("Missing command string after --execute switch"));
1107                 exit(1);
1108         }
1109         batch = arg;
1110         return 1;
1111 }
1112
1113
1114 int parse_export_to(string const & type, string const & output_file, string & batch)
1115 {
1116         if (type.empty()) {
1117                 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1118                                          "--export-to switch")) << endl;
1119                 exit(1);
1120         }
1121         if (output_file.empty()) {
1122                 lyxerr << to_utf8(_("Missing destination filename after "
1123                                          "--export-to switch")) << endl;
1124                 exit(1);
1125         }
1126         batch = "buffer-export " + type + " " + output_file;
1127         use_gui = false;
1128         return 2;
1129 }
1130
1131
1132 int parse_export(string const & type, string const &, string & batch)
1133 {
1134         if (type.empty()) {
1135                 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1136                                          "--export switch")) << endl;
1137                 exit(1);
1138         }
1139         batch = "buffer-export " + type;
1140         use_gui = false;
1141         return 1;
1142 }
1143
1144
1145 int parse_import(string const & type, string const & file, string & batch)
1146 {
1147         if (type.empty()) {
1148                 lyxerr << to_utf8(_("Missing file type [eg latex, ps...] after "
1149                                          "--import switch")) << endl;
1150                 exit(1);
1151         }
1152         if (file.empty()) {
1153                 lyxerr << to_utf8(_("Missing filename for --import")) << endl;
1154                 exit(1);
1155         }
1156         batch = "buffer-import " + type + ' ' + file;
1157         return 2;
1158 }
1159
1160
1161 int parse_geometry(string const & arg1, string const &, string &)
1162 {
1163         geometryArg = arg1;
1164         // don't remove "-geometry", it will be pruned out later in the
1165         // frontend if need be.
1166         return -1;
1167 }
1168
1169
1170 int parse_batch(string const &, string const &, string &)
1171 {
1172         use_gui = false;
1173         return 0;
1174 }
1175
1176
1177 int parse_noremote(string const &, string const &, string &)
1178 {
1179         run_mode = NEW_INSTANCE;
1180         return 0;
1181 }
1182
1183
1184 int parse_remote(string const &, string const &, string &)
1185 {
1186         run_mode = USE_REMOTE;
1187         return 0;
1188 }
1189
1190
1191 int parse_force(string const & arg, string const &, string &)
1192 {
1193         if (arg == "all") {
1194                 force_overwrite = ALL_FILES;
1195                 return 1;
1196         } else if (arg == "main") {
1197                 force_overwrite = MAIN_FILE;
1198                 return 1;
1199         } else if (arg == "none") {
1200                 force_overwrite = NO_FILES;
1201                 return 1;
1202         }
1203         force_overwrite = ALL_FILES;
1204         return 0;
1205 }
1206
1207
1208 } // namespace anon
1209
1210
1211 void LyX::easyParse(int & argc, char * argv[])
1212 {
1213         map<string, cmd_helper> cmdmap;
1214
1215         cmdmap["-dbg"] = parse_dbg;
1216         cmdmap["-help"] = parse_help;
1217         cmdmap["--help"] = parse_help;
1218         cmdmap["-version"] = parse_version;
1219         cmdmap["--version"] = parse_version;
1220         cmdmap["-sysdir"] = parse_sysdir;
1221         cmdmap["-userdir"] = parse_userdir;
1222         cmdmap["-x"] = parse_execute;
1223         cmdmap["--execute"] = parse_execute;
1224         cmdmap["-e"] = parse_export;
1225         cmdmap["--export"] = parse_export;
1226         cmdmap["-E"] = parse_export_to;
1227         cmdmap["--export-to"] = parse_export_to;
1228         cmdmap["-i"] = parse_import;
1229         cmdmap["--import"] = parse_import;
1230         cmdmap["-geometry"] = parse_geometry;
1231         cmdmap["-batch"] = parse_batch;
1232         cmdmap["-f"] = parse_force;
1233         cmdmap["--force-overwrite"] = parse_force;
1234         cmdmap["-n"] = parse_noremote;
1235         cmdmap["--no-remote"] = parse_noremote;
1236         cmdmap["-r"] = parse_remote;
1237         cmdmap["--remote"] = parse_remote;
1238
1239         for (int i = 1; i < argc; ++i) {
1240                 map<string, cmd_helper>::const_iterator it
1241                         = cmdmap.find(argv[i]);
1242
1243                 // don't complain if not found - may be parsed later
1244                 if (it == cmdmap.end())
1245                         continue;
1246
1247                 string const arg =
1248                         (i + 1 < argc) ? os::utf8_argv(i + 1) : string();
1249                 string const arg2 =
1250                         (i + 2 < argc) ? os::utf8_argv(i + 2) : string();
1251
1252                 string batch;
1253                 int const remove = 1 + it->second(arg, arg2, batch);
1254                 if (!batch.empty())
1255                         pimpl_->batch_commands.push_back(batch);
1256
1257                 // Now, remove used arguments by shifting
1258                 // the following ones remove places down.
1259                 if (remove > 0) {
1260                         os::remove_internal_args(i, remove);
1261                         argc -= remove;
1262                         for (int j = i; j < argc; ++j)
1263                                 argv[j] = argv[j + remove];
1264                         --i;
1265                 }
1266         }
1267 }
1268
1269
1270 FuncStatus getStatus(FuncRequest const & action)
1271 {
1272         LAPPERR(theApp());
1273         return theApp()->getStatus(action);
1274 }
1275
1276
1277 void dispatch(FuncRequest const & action)
1278 {
1279         LAPPERR(theApp());
1280         return theApp()->dispatch(action);
1281 }
1282
1283
1284 void dispatch(FuncRequest const & action, DispatchResult & dr)
1285 {
1286         LAPPERR(theApp());
1287         return theApp()->dispatch(action, dr);
1288 }
1289
1290
1291 vector<string> & theFilesToLoad()
1292 {
1293         LAPPERR(singleton_);
1294         return singleton_->pimpl_->files_to_load_;
1295 }
1296
1297
1298 BufferList & theBufferList()
1299 {
1300         LAPPERR(singleton_);
1301         return singleton_->pimpl_->buffer_list_;
1302 }
1303
1304
1305 Server & theServer()
1306 {
1307         // FIXME: this should not be use_gui dependent
1308         LWARNIF(use_gui);
1309         LAPPERR(singleton_);
1310         return *singleton_->pimpl_->lyx_server_.get();
1311 }
1312
1313
1314 ServerSocket & theServerSocket()
1315 {
1316         // FIXME: this should not be use_gui dependent
1317         LWARNIF(use_gui);
1318         LAPPERR(singleton_);
1319         return *singleton_->pimpl_->lyx_socket_.get();
1320 }
1321
1322
1323 KeyMap & theTopLevelKeymap()
1324 {
1325         LAPPERR(singleton_);
1326         return singleton_->pimpl_->toplevel_keymap_;
1327 }
1328
1329
1330 Converters & theConverters()
1331 {
1332         LAPPERR(singleton_);
1333         return  singleton_->pimpl_->converters_;
1334 }
1335
1336
1337 Converters & theSystemConverters()
1338 {
1339         LAPPERR(singleton_);
1340         return  singleton_->pimpl_->system_converters_;
1341 }
1342
1343
1344 Movers & theMovers()
1345 {
1346         LAPPERR(singleton_);
1347         return singleton_->pimpl_->movers_;
1348 }
1349
1350
1351 Mover const & getMover(string  const & fmt)
1352 {
1353         LAPPERR(singleton_);
1354         return singleton_->pimpl_->movers_(fmt);
1355 }
1356
1357
1358 void setMover(string const & fmt, string const & command)
1359 {
1360         LAPPERR(singleton_);
1361         singleton_->pimpl_->movers_.set(fmt, command);
1362 }
1363
1364
1365 Movers & theSystemMovers()
1366 {
1367         LAPPERR(singleton_);
1368         return singleton_->pimpl_->system_movers_;
1369 }
1370
1371
1372 Messages const & getMessages(string const & language)
1373 {
1374         LAPPERR(singleton_);
1375         return singleton_->messages(language);
1376 }
1377
1378
1379 Messages const & getGuiMessages()
1380 {
1381         LAPPERR(singleton_);
1382         // A cache to translate full language name to language code
1383         static string last_language = "auto";
1384         static string code;
1385         if (lyxrc.gui_language != last_language) {
1386                 if (lyxrc.gui_language == "auto")
1387                         code.clear();
1388                 else {
1389                         Language const * l = languages.getLanguage(lyxrc.gui_language);
1390                         code = l ? l->code() : string();
1391                 }
1392                 last_language = lyxrc.gui_language;
1393         }
1394         return singleton_->messages(code);
1395 }
1396
1397
1398 Session & theSession()
1399 {
1400         LAPPERR(singleton_);
1401         return *singleton_->pimpl_->session_.get();
1402 }
1403
1404
1405 LaTeXFonts & theLaTeXFonts()
1406 {
1407         LAPPERR(singleton_);
1408         if (!singleton_->pimpl_->latexfonts_)
1409                 singleton_->pimpl_->latexfonts_ = new LaTeXFonts;
1410         return *singleton_->pimpl_->latexfonts_;
1411 }
1412
1413
1414 CmdDef & theTopLevelCmdDef()
1415 {
1416         LAPPERR(singleton_);
1417         return singleton_->pimpl_->toplevel_cmddef_;
1418 }
1419
1420
1421 SpellChecker * theSpellChecker()
1422 {
1423         if (!singleton_->pimpl_->spell_checker_)
1424                 setSpellChecker();
1425         return singleton_->pimpl_->spell_checker_;
1426 }
1427
1428
1429 void setSpellChecker()
1430 {
1431         SpellChecker::ChangeNumber speller_change_number =singleton_->pimpl_->spell_checker_ ?
1432                 singleton_->pimpl_->spell_checker_->changeNumber() : 0;
1433
1434         if (lyxrc.spellchecker == "native") {
1435 #if defined(USE_MACOSX_PACKAGING)
1436                 if (!singleton_->pimpl_->apple_spell_checker_)
1437                         singleton_->pimpl_->apple_spell_checker_ = new AppleSpellChecker;
1438                 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->apple_spell_checker_;
1439 #else
1440                 singleton_->pimpl_->spell_checker_ = 0;
1441 #endif
1442         } else if (lyxrc.spellchecker == "aspell") {
1443 #if defined(USE_ASPELL)
1444                 if (!singleton_->pimpl_->aspell_checker_)
1445                         singleton_->pimpl_->aspell_checker_ = new AspellChecker;
1446                 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->aspell_checker_;
1447 #else
1448                 singleton_->pimpl_->spell_checker_ = 0;
1449 #endif
1450         } else if (lyxrc.spellchecker == "enchant") {
1451 #if defined(USE_ENCHANT)
1452                 if (!singleton_->pimpl_->enchant_checker_)
1453                         singleton_->pimpl_->enchant_checker_ = new EnchantChecker;
1454                 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->enchant_checker_;
1455 #else
1456                 singleton_->pimpl_->spell_checker_ = 0;
1457 #endif
1458         } else if (lyxrc.spellchecker == "hunspell") {
1459 #if defined(USE_HUNSPELL)
1460                 if (!singleton_->pimpl_->hunspell_checker_)
1461                         singleton_->pimpl_->hunspell_checker_ = new HunspellChecker;
1462                 singleton_->pimpl_->spell_checker_ = singleton_->pimpl_->hunspell_checker_;
1463 #else
1464                 singleton_->pimpl_->spell_checker_ = 0;
1465 #endif
1466         } else {
1467                 singleton_->pimpl_->spell_checker_ = 0;
1468         }
1469         if (singleton_->pimpl_->spell_checker_) {
1470                 singleton_->pimpl_->spell_checker_->changeNumber(speller_change_number);
1471                 singleton_->pimpl_->spell_checker_->advanceChangeNumber();
1472         }
1473 }
1474
1475 } // namespace lyx