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