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