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