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