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