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