]> git.lyx.org Git - lyx.git/blob - src/LyXFunc.cpp
Buffer::resetChildDocuments() Check for Buffer emptiness before proceeding.
[lyx.git] / src / LyXFunc.cpp
1 /**
2  * \file LyXFunc.cpp
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 Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Allan Rae
13  * \author Dekel Tsur
14  * \author Martin Vermeer
15  * \author Jürgen Vigna
16  *
17  * Full author contact details are available in file CREDITS.
18  */
19
20 #include <config.h>
21 #include <vector>
22
23 #include "LyXFunc.h"
24
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
27 #include "Buffer.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
31 #include "CmdDef.h"
32 #include "Color.h"
33 #include "Converter.h"
34 #include "Cursor.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
37 #include "Encoding.h"
38 #include "ErrorList.h"
39 #include "Format.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
43 #include "Intl.h"
44 #include "KeyMap.h"
45 #include "Language.h"
46 #include "Lexer.h"
47 #include "LyXAction.h"
48 #include "lyxfind.h"
49 #include "LyX.h"
50 #include "LyXRC.h"
51 #include "LyXVC.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
55 #include "Row.h"
56 #include "Server.h"
57 #include "Session.h"
58 #include "TextClassList.h"
59 #include "ToolbarBackend.h"
60
61 #include "insets/InsetBox.h"
62 #include "insets/InsetBranch.h"
63 #include "insets/InsetCommand.h"
64 #include "insets/InsetERT.h"
65 #include "insets/InsetExternal.h"
66 #include "insets/InsetFloat.h"
67 #include "insets/InsetListings.h"
68 #include "insets/InsetGraphics.h"
69 #include "insets/InsetInclude.h"
70 #include "insets/InsetNote.h"
71 #include "insets/InsetTabular.h"
72 #include "insets/InsetVSpace.h"
73 #include "insets/InsetWrap.h"
74
75 #include "frontends/alert.h"
76 #include "frontends/Application.h"
77 #include "frontends/KeySymbol.h"
78 #include "frontends/LyXView.h"
79 #include "frontends/Selection.h"
80
81 #include "support/debug.h"
82 #include "support/environment.h"
83 #include "support/FileName.h"
84 #include "support/filetools.h"
85 #include "support/gettext.h"
86 #include "support/lstrings.h"
87 #include "support/Path.h"
88 #include "support/Package.h"
89 #include "support/Systemcall.h"
90 #include "support/convert.h"
91 #include "support/os.h"
92
93 #include <sstream>
94
95 using namespace std;
96 using namespace lyx::support;
97
98 namespace lyx {
99
100 using frontend::LyXView;
101
102 namespace Alert = frontend::Alert;
103
104 extern bool quitting;
105 extern bool use_gui;
106
107 namespace {
108
109
110 // This function runs "configure" and then rereads lyx.defaults to
111 // reconfigure the automatic settings.
112 void reconfigure(LyXView & lv, string const & option)
113 {
114         // emit message signal.
115         lv.message(_("Running configure..."));
116
117         // Run configure in user lyx directory
118         PathChanger p(package().user_support());
119         string configure_command = package().configure_command();
120         configure_command += option;
121         Systemcall one;
122         int ret = one.startscript(Systemcall::Wait, configure_command);
123         p.pop();
124         // emit message signal.
125         lv.message(_("Reloading configuration..."));
126         lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
127         // Re-read packages.lst
128         LaTeXFeatures::getAvailable();
129
130         if (ret)
131                 Alert::information(_("System reconfiguration failed"),
132                            _("The system reconfiguration has failed.\n"
133                                   "Default textclass is used but LyX may "
134                                   "not be able to work properly.\n"
135                                   "Please reconfigure again if needed."));
136         else
137
138                 Alert::information(_("System reconfigured"),
139                            _("The system has been reconfigured.\n"
140                              "You need to restart LyX to make use of any\n"
141                              "updated document class specifications."));
142 }
143
144
145 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
146 {
147         // Try to fix cursor in case it is broken.
148         cursor.fixIfBroken();
149
150         // This is, of course, a mess. Better create a new doc iterator and use
151         // this in Inset::getStatus. This might require an additional
152         // BufferView * arg, though (which should be avoided)
153         //Cursor safe = *this;
154         bool res = false;
155         for ( ; cursor.depth(); cursor.pop()) {
156                 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
157                 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
158                 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
159                 BOOST_ASSERT(cursor.pos() <= cursor.lastpos());
160
161                 // The inset's getStatus() will return 'true' if it made
162                 // a definitive decision on whether it want to handle the
163                 // request or not. The result of this decision is put into
164                 // the 'status' parameter.
165                 if (cursor.inset().getStatus(cursor, cmd, status)) {
166                         res = true;
167                         break;
168                 }
169         }
170         return res;
171 }
172
173
174 /** Return the change status at cursor position, taking in account the
175  * status at each level of the document iterator (a table in a deleted
176  * footnote is deleted).
177  * When \param outer is true, the top slice is not looked at.
178  */
179 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
180 {
181         size_t const depth = dit.depth() - (outer ? 1 : 0);
182
183         for (size_t i = 0 ; i < depth ; ++i) {
184                 CursorSlice const & slice = dit[i];
185                 if (!slice.inset().inMathed()
186                     && slice.pos() < slice.paragraph().size()) {
187                         Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
188                         if (ch != Change::UNCHANGED)
189                                 return ch;
190                 }
191         }
192         return Change::UNCHANGED;
193 }
194
195 }
196
197
198 LyXFunc::LyXFunc()
199         : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
200 {
201 }
202
203
204 void LyXFunc::initKeySequences(KeyMap * kb)
205 {
206         keyseq = KeySequence(kb, kb);
207         cancel_meta_seq = KeySequence(kb, kb);
208 }
209
210
211 void LyXFunc::setLyXView(LyXView * lv)
212 {
213         if (!quitting && lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
214                 // save current selection to the selection buffer to allow
215                 // middle-button paste in another window
216                 cap::saveSelection(lyx_view_->view()->cursor());
217         lyx_view_ = lv;
218 }
219
220
221 void LyXFunc::handleKeyFunc(kb_action action)
222 {
223         char_type c = encoded_last_key;
224
225         if (keyseq.length())
226                 c = 0;
227
228         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
229         lyx_view_->view()->getIntl().getTransManager().deadkey(
230                 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
231         // Need to clear, in case the minibuffer calls these
232         // actions
233         keyseq.clear();
234         // copied verbatim from do_accent_char
235         view()->cursor().resetAnchor();
236         view()->processUpdateFlags(Update::FitCursor);
237 }
238
239
240 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
241 {
242         BOOST_ASSERT(lyx_view_);
243         if (!LyX::ref().session().bookmarks().isValid(idx))
244                 return;
245         BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
246         BOOST_ASSERT(!bm.filename.empty());
247         string const file = bm.filename.absFilename();
248         // if the file is not opened, open it.
249         if (!theBufferList().exists(file)) {
250                 if (openFile)
251                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
252                 else
253                         return;
254         }
255         // open may fail, so we need to test it again
256         if (!theBufferList().exists(file))
257                 return;
258
259         // if the current buffer is not that one, switch to it.
260         if (lyx_view_->buffer()->absFileName() != file) {
261                 if (!switchToBuffer)
262                         return;
263                 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
264         }
265         // moveToPosition try paragraph id first and then paragraph (pit, pos).
266         if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
267                 bm.top_id, bm.top_pos))
268                 return;
269
270         // Cursor jump succeeded!
271         Cursor const & cur = view()->cursor();
272         pit_type new_pit = cur.pit();
273         pos_type new_pos = cur.pos();
274         int new_id = cur.paragraph().id();
275
276         // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
277         // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
278         if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos 
279                 || bm.top_id != new_id) {
280                 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
281                         new_pit, new_pos, new_id);
282         }
283 }
284
285
286 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
287 {
288         LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
289
290         // Do nothing if we have nothing (JMarc)
291         if (!keysym.isOK()) {
292                 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
293                 lyx_view_->restartCursor();
294                 return;
295         }
296
297         if (keysym.isModifier()) {
298                 LYXERR(Debug::KEY, "isModifier true");
299                 lyx_view_->restartCursor();
300                 return;
301         }
302
303         //Encoding const * encoding = view()->cursor().getEncoding();
304         //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
305         // FIXME: encoded_last_key shadows the member variable of the same
306         // name. Is that intended?
307         char_type encoded_last_key = keysym.getUCSEncoded();
308
309         // Do a one-deep top-level lookup for
310         // cancel and meta-fake keys. RVDK_PATCH_5
311         cancel_meta_seq.reset();
312
313         FuncRequest func = cancel_meta_seq.addkey(keysym, state);
314         LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
315
316         // When not cancel or meta-fake, do the normal lookup.
317         // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
318         // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
319         if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
320                 // remove Caps Lock and Mod2 as a modifiers
321                 func = keyseq.addkey(keysym, (state | meta_fake_bit));
322                 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
323         }
324
325         // Dont remove this unless you know what you are doing.
326         meta_fake_bit = NoModifier;
327
328         // Can this happen now ?
329         if (func.action == LFUN_NOACTION)
330                 func = FuncRequest(LFUN_COMMAND_PREFIX);
331
332         LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
333                 << keyseq.print(KeySequence::Portable) << ']');
334
335         // already here we know if it any point in going further
336         // why not return already here if action == -1 and
337         // num_bytes == 0? (Lgb)
338
339         if (keyseq.length() > 1)
340                 lyx_view_->message(keyseq.print(KeySequence::ForGui));
341
342
343         // Maybe user can only reach the key via holding down shift.
344         // Let's see. But only if shift is the only modifier
345         if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
346                 LYXERR(Debug::KEY, "Trying without shift");
347                 func = keyseq.addkey(keysym, NoModifier);
348                 LYXERR(Debug::KEY, "Action now " << func.action);
349         }
350
351         if (func.action == LFUN_UNKNOWN_ACTION) {
352                 // Hmm, we didn't match any of the keysequences. See
353                 // if it's normal insertable text not already covered
354                 // by a binding
355                 if (keysym.isText() && keyseq.length() == 1) {
356                         LYXERR(Debug::KEY, "isText() is true, inserting.");
357                         func = FuncRequest(LFUN_SELF_INSERT,
358                                            FuncRequest::KEYBOARD);
359                 } else {
360                         LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
361                         lyx_view_->message(_("Unknown function."));
362                         lyx_view_->restartCursor();
363                         return;
364                 }
365         }
366
367         if (func.action == LFUN_SELF_INSERT) {
368                 if (encoded_last_key != 0) {
369                         docstring const arg(1, encoded_last_key);
370                         dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
371                                              FuncRequest::KEYBOARD));
372                         LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
373                 }
374         } else {
375                 dispatch(func);
376         }
377
378         lyx_view_->restartCursor();
379 }
380
381
382 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
383 {
384         //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
385         FuncStatus flag;
386
387         Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
388
389         if (cmd.action == LFUN_NOACTION) {
390                 flag.message(from_utf8(N_("Nothing to do")));
391                 flag.enabled(false);
392                 return flag;
393         }
394
395         switch (cmd.action) {
396         case LFUN_UNKNOWN_ACTION:
397 #ifndef HAVE_LIBAIKSAURUS
398         case LFUN_THESAURUS_ENTRY:
399 #endif
400                 flag.unknown(true);
401                 flag.enabled(false);
402                 break;
403
404         default:
405                 break;
406         }
407
408         if (flag.unknown()) {
409                 flag.message(from_utf8(N_("Unknown action")));
410                 return flag;
411         }
412
413         if (!flag.enabled()) {
414                 if (flag.message().empty())
415                         flag.message(from_utf8(N_("Command disabled")));
416                 return flag;
417         }
418
419         // Check whether we need a buffer
420         if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
421                 // no, exit directly
422                 flag.message(from_utf8(N_("Command not allowed with"
423                                     "out any document open")));
424                 flag.enabled(false);
425                 return flag;
426         }
427
428         // I would really like to avoid having this switch and rather try to
429         // encode this in the function itself.
430         // -- And I'd rather let an inset decide which LFUNs it is willing
431         // to handle (Andre')
432         bool enable = true;
433         switch (cmd.action) {
434
435         // FIXME: these cases should be hidden in GuiApplication::getStatus().
436         case LFUN_WINDOW_CLOSE:
437                 if (theApp())
438                         return theApp()->getStatus(cmd);
439                 enable = false;
440                 break;
441
442         // FIXME: these cases should be hidden in GuiView::getStatus().
443         case LFUN_DIALOG_TOGGLE:
444         case LFUN_DIALOG_SHOW:
445         case LFUN_DIALOG_UPDATE:
446                 if (cmd.argument() == "prefs"
447                     || cmd.argument() == "aboutlyx")
448                         enable = true;
449                 else if (lyx_view_)
450                         return lyx_view_->getStatus(cmd);
451                 else
452                         enable = false;
453                 break;
454
455         case LFUN_TOOLBAR_TOGGLE:
456         case LFUN_INSET_APPLY:
457         case LFUN_BUFFER_WRITE:
458         case LFUN_BUFFER_WRITE_AS:
459                 if (lyx_view_)
460                         return lyx_view_->getStatus(cmd);
461                 enable = false;
462                 break;
463
464         case LFUN_BUFFER_TOGGLE_READ_ONLY:
465                 flag.setOnOff(buf->isReadonly());
466                 break;
467
468         case LFUN_BUFFER_SWITCH:
469                 // toggle on the current buffer, but do not toggle off
470                 // the other ones (is that a good idea?)
471                 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
472                         flag.setOnOff(true);
473                 break;
474
475         case LFUN_BUFFER_EXPORT:
476                 enable = cmd.argument() == "custom"
477                         || buf->isExportable(to_utf8(cmd.argument()));
478                 break;
479
480         case LFUN_BUFFER_CHKTEX:
481                 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
482                 break;
483
484         case LFUN_BUILD_PROGRAM:
485                 enable = buf->isExportable("program");
486                 break;
487
488         case LFUN_VC_REGISTER:
489                 enable = !buf->lyxvc().inUse();
490                 break;
491         case LFUN_VC_CHECK_IN:
492                 enable = buf->lyxvc().inUse() && !buf->isReadonly();
493                 break;
494         case LFUN_VC_CHECK_OUT:
495                 enable = buf->lyxvc().inUse() && buf->isReadonly();
496                 break;
497         case LFUN_VC_REVERT:
498         case LFUN_VC_UNDO_LAST:
499                 enable = buf->lyxvc().inUse();
500                 break;
501         case LFUN_BUFFER_RELOAD:
502                 enable = !buf->isUnnamed() && buf->fileName().exists()
503                         && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
504                 break;
505
506         case LFUN_CITATION_INSERT: {
507                 FuncRequest fr(LFUN_INSET_INSERT, "citation");
508                 enable = getStatus(fr).enabled();
509                 break;
510         }
511         
512         // This could be used for the no-GUI version. The GUI version is handled in
513         // LyXView::getStatus(). See above.
514         /*
515         case LFUN_BUFFER_WRITE:
516         case LFUN_BUFFER_WRITE_AS: {
517                 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
518                 enable = b && (b->isUnnamed() || !b->isClean());
519                 break;
520         }
521         */
522
523         case LFUN_BUFFER_WRITE_ALL: {
524                 // We enable the command only if there are some modified buffers
525                 Buffer * first = theBufferList().first();
526                 enable = false;
527                 if (!first)
528                         break;
529                 Buffer * b = first;
530                 // We cannot use a for loop as the buffer list is a cycle.
531                 do {
532                         if (!b->isClean()) {
533                                 enable = true;
534                                 break;
535                         }
536                         b = theBufferList().next(b);
537                 } while (b != first); 
538                 break;
539         }
540
541         case LFUN_BOOKMARK_GOTO: {
542                 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
543                 enable = LyX::ref().session().bookmarks().isValid(num);
544                 break;
545         }
546
547         case LFUN_BOOKMARK_CLEAR:
548                 enable = LyX::ref().session().bookmarks().size() > 0;
549                 break;
550
551         // this one is difficult to get right. As a half-baked
552         // solution, we consider only the first action of the sequence
553         case LFUN_COMMAND_SEQUENCE: {
554                 // argument contains ';'-terminated commands
555                 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
556                 FuncRequest func(lyxaction.lookupFunc(firstcmd));
557                 func.origin = cmd.origin;
558                 flag = getStatus(func);
559                 break;
560         }
561
562         case LFUN_CALL: {
563                 FuncRequest func;
564                 string name = to_utf8(cmd.argument());
565                 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
566                         func.origin = cmd.origin;
567                         flag = getStatus(func);
568                         LyX::ref().topLevelCmdDef().release(name);
569                 } else {
570                         // catch recursion or unknown command definiton
571                         // all operations until the recursion or unknown command 
572                         // definiton occures are performed, so set the state to enabled
573                         enable = true;
574                 }
575                 break;
576         }
577
578         case LFUN_BUFFER_NEW:
579         case LFUN_BUFFER_NEW_TEMPLATE:
580         case LFUN_WORD_FIND_FORWARD:
581         case LFUN_WORD_FIND_BACKWARD:
582         case LFUN_COMMAND_PREFIX:
583         case LFUN_COMMAND_EXECUTE:
584         case LFUN_CANCEL:
585         case LFUN_META_PREFIX:
586         case LFUN_BUFFER_CLOSE:
587         case LFUN_BUFFER_UPDATE:
588         case LFUN_BUFFER_VIEW:
589         case LFUN_MASTER_BUFFER_UPDATE:
590         case LFUN_MASTER_BUFFER_VIEW:
591         case LFUN_BUFFER_IMPORT:
592         case LFUN_BUFFER_AUTO_SAVE:
593         case LFUN_RECONFIGURE:
594         case LFUN_HELP_OPEN:
595         case LFUN_FILE_OPEN:
596         case LFUN_DROP_LAYOUTS_CHOICE:
597         case LFUN_MENU_OPEN:
598         case LFUN_SERVER_GET_NAME:
599         case LFUN_SERVER_NOTIFY:
600         case LFUN_SERVER_GOTO_FILE_ROW:
601         case LFUN_DIALOG_HIDE:
602         case LFUN_DIALOG_DISCONNECT_INSET:
603         case LFUN_BUFFER_CHILD_OPEN:
604         case LFUN_UI_TOGGLE:
605         case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
606         case LFUN_KEYMAP_OFF:
607         case LFUN_KEYMAP_PRIMARY:
608         case LFUN_KEYMAP_SECONDARY:
609         case LFUN_KEYMAP_TOGGLE:
610         case LFUN_REPEAT:
611         case LFUN_BUFFER_EXPORT_CUSTOM:
612         case LFUN_BUFFER_PRINT:
613         case LFUN_PREFERENCES_SAVE:
614         case LFUN_SCREEN_FONT_UPDATE:
615         case LFUN_SET_COLOR:
616         case LFUN_MESSAGE:
617         case LFUN_EXTERNAL_EDIT:
618         case LFUN_GRAPHICS_EDIT:
619         case LFUN_ALL_INSETS_TOGGLE:
620         case LFUN_BUFFER_LANGUAGE:
621         case LFUN_TEXTCLASS_APPLY:
622         case LFUN_TEXTCLASS_LOAD:
623         case LFUN_BUFFER_SAVE_AS_DEFAULT:
624         case LFUN_BUFFER_PARAMS_APPLY:
625         case LFUN_LAYOUT_MODULES_CLEAR:
626         case LFUN_LAYOUT_MODULE_ADD:
627         case LFUN_LAYOUT_RELOAD:
628         case LFUN_LYXRC_APPLY:
629         case LFUN_BUFFER_NEXT:
630         case LFUN_BUFFER_PREVIOUS:
631         case LFUN_WINDOW_NEW:
632         case LFUN_LYX_QUIT:
633                 // these are handled in our dispatch()
634                 break;
635
636         default:
637                 if (!view()) {
638                         enable = false;
639                         break;
640                 }
641                 if (!getLocalStatus(view()->cursor(), cmd, flag))
642                         flag = view()->getStatus(cmd);
643         }
644
645         if (!enable)
646                 flag.enabled(false);
647
648         // Can we use a readonly buffer?
649         if (buf && buf->isReadonly()
650             && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
651             && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
652                 flag.message(from_utf8(N_("Document is read-only")));
653                 flag.enabled(false);
654         }
655
656         // Are we in a DELETED change-tracking region?
657         if (buf && view() 
658                 && lookupChangeType(view()->cursor(), true) == Change::DELETED
659             && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
660             && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
661                 flag.message(from_utf8(N_("This portion of the document is deleted.")));
662                 flag.enabled(false);
663         }
664
665         // the default error message if we disable the command
666         if (!flag.enabled() && flag.message().empty())
667                 flag.message(from_utf8(N_("Command disabled")));
668
669         return flag;
670 }
671
672
673 bool LyXFunc::ensureBufferClean(BufferView * bv)
674 {
675         Buffer & buf = bv->buffer();
676         if (buf.isClean())
677                 return true;
678
679         docstring const file = buf.fileName().displayName(30);
680         docstring text = bformat(_("The document %1$s has unsaved "
681                                              "changes.\n\nDo you want to save "
682                                              "the document?"), file);
683         int const ret = Alert::prompt(_("Save changed document?"),
684                                       text, 0, 1, _("&Save"),
685                                       _("&Cancel"));
686
687         if (ret == 0)
688                 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
689
690         return buf.isClean();
691 }
692
693
694 namespace {
695
696 void showPrintError(string const & name)
697 {
698         docstring str = bformat(_("Could not print the document %1$s.\n"
699                                             "Check that your printer is set up correctly."),
700                              makeDisplayPath(name, 50));
701         Alert::error(_("Print document failed"), str);
702 }
703
704
705 void loadTextClass(string const & name)
706 {
707         pair<bool, textclass_type> const tc_pair =
708                 textclasslist.numberOfClass(name);
709
710         if (!tc_pair.first) {
711                 lyxerr << "Document class \"" << name
712                        << "\" does not exist."
713                        << endl;
714                 return;
715         }
716
717         textclass_type const tc = tc_pair.second;
718
719         if (!textclasslist[tc].load()) {
720                 docstring s = bformat(_("The document class %1$s."
721                                    "could not be loaded."),
722                                    from_utf8(textclasslist[tc].name()));
723                 Alert::error(_("Could not load class"), s);
724         }
725 }
726
727
728 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
729
730 } //namespace anon
731
732
733 void LyXFunc::dispatch(FuncRequest const & cmd)
734 {
735         string const argument = to_utf8(cmd.argument());
736         kb_action const action = cmd.action;
737
738         LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
739         //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
740
741         // we have not done anything wrong yet.
742         errorstat = false;
743         dispatch_buffer.erase();
744
745         // redraw the screen at the end (first of the two drawing steps).
746         //This is done unless explicitely requested otherwise
747         Update::flags updateFlags = Update::FitCursor;
748
749         FuncStatus const flag = getStatus(cmd);
750         if (!flag.enabled()) {
751                 // We cannot use this function here
752                 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
753                        << lyxaction.getActionName(action)
754                        << " [" << action << "] is disabled at this location");
755                 setErrorMessage(flag.message());
756         } else {
757                 switch (action) {
758
759                 case LFUN_WORD_FIND_FORWARD:
760                 case LFUN_WORD_FIND_BACKWARD: {
761                         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
762                         static docstring last_search;
763                         docstring searched_string;
764
765                         if (!cmd.argument().empty()) {
766                                 last_search = cmd.argument();
767                                 searched_string = cmd.argument();
768                         } else {
769                                 searched_string = last_search;
770                         }
771
772                         if (searched_string.empty())
773                                 break;
774
775                         bool const fw = action == LFUN_WORD_FIND_FORWARD;
776                         docstring const data =
777                                 find2string(searched_string, true, false, fw);
778                         find(view(), FuncRequest(LFUN_WORD_FIND, data));
779                         break;
780                 }
781
782                 case LFUN_COMMAND_PREFIX:
783                         BOOST_ASSERT(lyx_view_);
784                         lyx_view_->message(keyseq.printOptions(true));
785                         break;
786
787                 case LFUN_CANCEL:
788                         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
789                         keyseq.reset();
790                         meta_fake_bit = NoModifier;
791                         if (lyx_view_->buffer())
792                                 // cancel any selection
793                                 dispatch(FuncRequest(LFUN_MARK_OFF));
794                         setMessage(from_ascii(N_("Cancel")));
795                         break;
796
797                 case LFUN_META_PREFIX:
798                         meta_fake_bit = AltModifier;
799                         setMessage(keyseq.print(KeySequence::ForGui));
800                         break;
801
802                 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
803                         BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
804                         Buffer * buf = lyx_view_->buffer();
805                         if (buf->lyxvc().inUse())
806                                 buf->lyxvc().toggleReadOnly();
807                         else
808                                 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
809                         break;
810                 }
811
812                 // --- Menus -----------------------------------------------
813                 case LFUN_BUFFER_NEW:
814                         lyx_view_->newDocument(argument, false);
815                         updateFlags = Update::None;
816                         break;
817
818                 case LFUN_BUFFER_NEW_TEMPLATE:
819                         lyx_view_->newDocument(argument, true);
820                         updateFlags = Update::None;
821                         break;
822
823                 case LFUN_BUFFER_CLOSE:
824                         closeBuffer();
825                         updateFlags = Update::None;
826                         break;
827
828                 case LFUN_BUFFER_RELOAD: {
829                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
830                         docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
831                         docstring text = bformat(_("Any changes will be lost. Are you sure "
832                                                              "you want to revert to the saved version of the document %1$s?"), file);
833                         int const ret = Alert::prompt(_("Revert to saved document?"),
834                                 text, 1, 1, _("&Revert"), _("&Cancel"));
835
836                         if (ret == 0)
837                                 reloadBuffer();
838                         break;
839                 }
840
841                 case LFUN_BUFFER_UPDATE:
842                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
843                         lyx_view_->buffer()->doExport(argument, true);
844                         break;
845
846                 case LFUN_BUFFER_VIEW:
847                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
848                         lyx_view_->buffer()->preview(argument);
849                         break;
850
851                 case LFUN_MASTER_BUFFER_UPDATE:
852                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
853                         lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
854                         break;
855
856                 case LFUN_MASTER_BUFFER_VIEW:
857                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
858                         lyx_view_->buffer()->masterBuffer()->preview(argument);
859                         break;
860
861                 case LFUN_BUILD_PROGRAM:
862                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
863                         lyx_view_->buffer()->doExport("program", true);
864                         break;
865
866                 case LFUN_BUFFER_CHKTEX:
867                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
868                         lyx_view_->buffer()->runChktex();
869                         break;
870
871                 case LFUN_BUFFER_EXPORT:
872                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
873                         if (argument == "custom")
874                                 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
875                         else
876                                 lyx_view_->buffer()->doExport(argument, false);
877                         break;
878
879                 case LFUN_BUFFER_EXPORT_CUSTOM: {
880                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
881                         string format_name;
882                         string command = split(argument, format_name, ' ');
883                         Format const * format = formats.getFormat(format_name);
884                         if (!format) {
885                                 lyxerr << "Format \"" << format_name
886                                        << "\" not recognized!"
887                                        << endl;
888                                 break;
889                         }
890
891                         Buffer * buffer = lyx_view_->buffer();
892
893                         // The name of the file created by the conversion process
894                         string filename;
895
896                         // Output to filename
897                         if (format->name() == "lyx") {
898                                 string const latexname = buffer->latexName(false);
899                                 filename = changeExtension(latexname,
900                                                            format->extension());
901                                 filename = addName(buffer->temppath(), filename);
902
903                                 if (!buffer->writeFile(FileName(filename)))
904                                         break;
905
906                         } else {
907                                 buffer->doExport(format_name, true, filename);
908                         }
909
910                         // Substitute $$FName for filename
911                         if (!contains(command, "$$FName"))
912                                 command = "( " + command + " ) < $$FName";
913                         command = subst(command, "$$FName", filename);
914
915                         // Execute the command in the background
916                         Systemcall call;
917                         call.startscript(Systemcall::DontWait, command);
918                         break;
919                 }
920
921                 case LFUN_BUFFER_PRINT: {
922                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
923                         // FIXME: cmd.getArg() might fail if one of the arguments
924                         // contains double quotes
925                         string target = cmd.getArg(0);
926                         string target_name = cmd.getArg(1);
927                         string command = cmd.getArg(2);
928
929                         if (target.empty()
930                             || target_name.empty()
931                             || command.empty()) {
932                                 lyxerr << "Unable to parse \""
933                                        << argument << '"' << endl;
934                                 break;
935                         }
936                         if (target != "printer" && target != "file") {
937                                 lyxerr << "Unrecognized target \""
938                                        << target << '"' << endl;
939                                 break;
940                         }
941
942                         Buffer * buffer = lyx_view_->buffer();
943
944                         if (!buffer->doExport("dvi", true)) {
945                                 showPrintError(buffer->absFileName());
946                                 break;
947                         }
948
949                         // Push directory path.
950                         string const path = buffer->temppath();
951                         // Prevent the compiler from optimizing away p
952                         FileName pp(path);
953                         PathChanger p(pp);
954
955                         // there are three cases here:
956                         // 1. we print to a file
957                         // 2. we print directly to a printer
958                         // 3. we print using a spool command (print to file first)
959                         Systemcall one;
960                         int res = 0;
961                         string const dviname =
962                                 changeExtension(buffer->latexName(true), "dvi");
963
964                         if (target == "printer") {
965                                 if (!lyxrc.print_spool_command.empty()) {
966                                         // case 3: print using a spool
967                                         string const psname =
968                                                 changeExtension(dviname,".ps");
969                                         command += ' ' + lyxrc.print_to_file
970                                                 + quoteName(psname)
971                                                 + ' '
972                                                 + quoteName(dviname);
973
974                                         string command2 =
975                                                 lyxrc.print_spool_command + ' ';
976                                         if (target_name != "default") {
977                                                 command2 += lyxrc.print_spool_printerprefix
978                                                         + target_name
979                                                         + ' ';
980                                         }
981                                         command2 += quoteName(psname);
982                                         // First run dvips.
983                                         // If successful, then spool command
984                                         res = one.startscript(
985                                                 Systemcall::Wait,
986                                                 command);
987
988                                         if (res == 0)
989                                                 res = one.startscript(
990                                                         Systemcall::DontWait,
991                                                         command2);
992                                 } else {
993                                         // case 2: print directly to a printer
994                                         if (target_name != "default")
995                                                 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
996                                         res = one.startscript(
997                                                 Systemcall::DontWait,
998                                                 command + quoteName(dviname));
999                                 }
1000
1001                         } else {
1002                                 // case 1: print to a file
1003                                 FileName const filename(makeAbsPath(target_name,
1004                                                         lyx_view_->buffer()->filePath()));
1005                                 FileName const dvifile(makeAbsPath(dviname, path));
1006                                 if (filename.exists()) {
1007                                         docstring text = bformat(
1008                                                 _("The file %1$s already exists.\n\n"
1009                                                   "Do you want to overwrite that file?"),
1010                                                 makeDisplayPath(filename.absFilename()));
1011                                         if (Alert::prompt(_("Overwrite file?"),
1012                                             text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1013                                                 break;
1014                                 }
1015                                 command += ' ' + lyxrc.print_to_file
1016                                         + quoteName(filename.toFilesystemEncoding())
1017                                         + ' '
1018                                         + quoteName(dvifile.toFilesystemEncoding());
1019                                 res = one.startscript(Systemcall::DontWait,
1020                                                       command);
1021                         }
1022
1023                         if (res != 0)
1024                                 showPrintError(buffer->absFileName());
1025                         break;
1026                 }
1027
1028                 // FIXME: There is need for a command-line import.
1029                 /*
1030                 case LFUN_BUFFER_IMPORT:
1031                         doImport(argument);
1032                         break;
1033                 */
1034
1035                 case LFUN_BUFFER_AUTO_SAVE:
1036                         lyx_view_->buffer()->autoSave();
1037                         break;
1038
1039                 case LFUN_RECONFIGURE:
1040                         BOOST_ASSERT(lyx_view_);
1041                         // argument is any additional parameter to the configure.py command
1042                         reconfigure(*lyx_view_, argument);
1043                         break;
1044
1045                 case LFUN_HELP_OPEN: {
1046                         BOOST_ASSERT(lyx_view_);
1047                         string const arg = argument;
1048                         if (arg.empty()) {
1049                                 setErrorMessage(from_ascii(N_("Missing argument")));
1050                                 break;
1051                         }
1052                         FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1053                         if (fname.empty()) {
1054                                 lyxerr << "LyX: unable to find documentation file `"
1055                                                          << arg << "'. Bad installation?" << endl;
1056                                 break;
1057                         }
1058                         lyx_view_->message(bformat(_("Opening help file %1$s..."),
1059                                 makeDisplayPath(fname.absFilename())));
1060                         Buffer * buf = lyx_view_->loadDocument(fname, false);
1061                         if (buf) {
1062                                 updateLabels(*buf);
1063                                 lyx_view_->setBuffer(buf);
1064                                 buf->errors("Parse");
1065                         }
1066                         updateFlags = Update::None;
1067                         break;
1068                 }
1069
1070                 // --- version control -------------------------------
1071                 case LFUN_VC_REGISTER:
1072                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1073                         if (!ensureBufferClean(view()))
1074                                 break;
1075                         if (!lyx_view_->buffer()->lyxvc().inUse()) {
1076                                 lyx_view_->buffer()->lyxvc().registrer();
1077                                 reloadBuffer();
1078                         }
1079                         updateFlags = Update::Force;
1080                         break;
1081
1082                 case LFUN_VC_CHECK_IN:
1083                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1084                         if (!ensureBufferClean(view()))
1085                                 break;
1086                         if (lyx_view_->buffer()->lyxvc().inUse()
1087                                         && !lyx_view_->buffer()->isReadonly()) {
1088                                 lyx_view_->buffer()->lyxvc().checkIn();
1089                                 reloadBuffer();
1090                         }
1091                         break;
1092
1093                 case LFUN_VC_CHECK_OUT:
1094                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1095                         if (!ensureBufferClean(view()))
1096                                 break;
1097                         if (lyx_view_->buffer()->lyxvc().inUse()
1098                                         && lyx_view_->buffer()->isReadonly()) {
1099                                 lyx_view_->buffer()->lyxvc().checkOut();
1100                                 reloadBuffer();
1101                         }
1102                         break;
1103
1104                 case LFUN_VC_REVERT:
1105                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1106                         lyx_view_->buffer()->lyxvc().revert();
1107                         reloadBuffer();
1108                         break;
1109
1110                 case LFUN_VC_UNDO_LAST:
1111                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1112                         lyx_view_->buffer()->lyxvc().undoLast();
1113                         reloadBuffer();
1114                         break;
1115
1116                 // --- lyxserver commands ----------------------------
1117                 case LFUN_SERVER_GET_NAME:
1118                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1119                         setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1120                         LYXERR(Debug::INFO, "FNAME["
1121                                 << lyx_view_->buffer()->absFileName() << ']');
1122                         break;
1123
1124                 case LFUN_SERVER_NOTIFY:
1125                         dispatch_buffer = keyseq.print(KeySequence::Portable);
1126                         theServer().notifyClient(to_utf8(dispatch_buffer));
1127                         break;
1128
1129                 case LFUN_SERVER_GOTO_FILE_ROW: {
1130                         BOOST_ASSERT(lyx_view_);
1131                         string file_name;
1132                         int row;
1133                         istringstream is(argument);
1134                         is >> file_name >> row;
1135                         Buffer * buf = 0;
1136                         bool loaded = false;
1137                         if (prefixIs(file_name, package().temp_dir().absFilename()))
1138                                 // Needed by inverse dvi search. If it is a file
1139                                 // in tmpdir, call the apropriated function
1140                                 buf = theBufferList().getBufferFromTmp(file_name);
1141                         else {
1142                                 // Must replace extension of the file to be .lyx
1143                                 // and get full path
1144                                 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1145                                 // Either change buffer or load the file
1146                                 if (theBufferList().exists(s.absFilename()))
1147                                         buf = theBufferList().getBuffer(s.absFilename());
1148                                 else {
1149                                         buf = lyx_view_->loadDocument(s);
1150                                         loaded = true;
1151                                 }
1152                         }
1153
1154                         if (!buf) {
1155                                 updateFlags = Update::None;
1156                                 break;
1157                         }
1158
1159                         updateLabels(*buf);
1160                         lyx_view_->setBuffer(buf);
1161                         view()->setCursorFromRow(row);
1162                         if (loaded)
1163                                 buf->errors("Parse");
1164                         updateFlags = Update::FitCursor;
1165                         break;
1166                 }
1167
1168
1169                 case LFUN_DIALOG_SHOW_NEW_INSET: {
1170                         BOOST_ASSERT(lyx_view_);
1171                         string const name = cmd.getArg(0);
1172                         InsetCode code = insetCode(name);
1173                         string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1174                         bool insetCodeOK = true;
1175                         switch (code) {
1176                         case BIBITEM_CODE:
1177                         case BIBTEX_CODE:
1178                         case INDEX_CODE:
1179                         case LABEL_CODE:
1180                         case NOMENCL_CODE:
1181                         case REF_CODE:
1182                         case TOC_CODE:
1183                         case HYPERLINK_CODE: {
1184                                 InsetCommandParams p(code);
1185                                 data = InsetCommandMailer::params2string(name, p);
1186                                 break;
1187                         } 
1188                         case INCLUDE_CODE: {
1189                                 // data is the include type: one of "include",
1190                                 // "input", "verbatiminput" or "verbatiminput*"
1191                                 if (data.empty())
1192                                         // default type is requested
1193                                         data = "include";
1194                                 InsetCommandParams p(INCLUDE_CODE, data);
1195                                 data = InsetCommandMailer::params2string("include", p);
1196                                 break;
1197                         } 
1198                         case BOX_CODE: {
1199                                 // \c data == "Boxed" || "Frameless" etc
1200                                 InsetBoxParams p(data);
1201                                 data = InsetBoxMailer::params2string(p);
1202                                 break;
1203                         } 
1204                         case BRANCH_CODE: {
1205                                 InsetBranchParams p;
1206                                 data = InsetBranchMailer::params2string(p);
1207                                 break;
1208                         } 
1209                         case CITE_CODE: {
1210                                 InsetCommandParams p(CITE_CODE);
1211                                 data = InsetCommandMailer::params2string(name, p);
1212                                 break;
1213                         } 
1214                         case ERT_CODE: {
1215                                 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1216                                 break;
1217                         } 
1218                         case EXTERNAL_CODE: {
1219                                 InsetExternalParams p;
1220                                 Buffer const & buffer = *lyx_view_->buffer();
1221                                 data = InsetExternalMailer::params2string(p, buffer);
1222                                 break;
1223                         } 
1224                         case FLOAT_CODE:  {
1225                                 InsetFloatParams p;
1226                                 data = InsetFloatMailer::params2string(p);
1227                                 break;
1228                         } 
1229                         case LISTINGS_CODE: {
1230                                 InsetListingsParams p;
1231                                 data = InsetListingsMailer::params2string(p);
1232                                 break;
1233                         } 
1234                         case GRAPHICS_CODE: {
1235                                 InsetGraphicsParams p;
1236                                 Buffer const & buffer = *lyx_view_->buffer();
1237                                 data = InsetGraphicsMailer::params2string(p, buffer);
1238                                 break;
1239                         } 
1240                         case NOTE_CODE: {
1241                                 InsetNoteParams p;
1242                                 data = InsetNoteMailer::params2string(p);
1243                                 break;
1244                         } 
1245                         case VSPACE_CODE: {
1246                                 VSpace space;
1247                                 data = InsetVSpaceMailer::params2string(space);
1248                                 break;
1249                         } 
1250                         case WRAP_CODE: {
1251                                 InsetWrapParams p;
1252                                 data = InsetWrapMailer::params2string(p);
1253                                 break;
1254                         }
1255                         default:
1256                                 lyxerr << "Inset type '" << name << 
1257                                         "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" <<  endl;
1258                                 insetCodeOK = false;
1259                                 break;
1260                         } // end switch(code)
1261                         if (insetCodeOK)
1262                                 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1263                         break;
1264                 }
1265
1266                 case LFUN_CITATION_INSERT: {
1267                         BOOST_ASSERT(lyx_view_);
1268                         if (!argument.empty()) {
1269                                 // we can have one optional argument, delimited by '|'
1270                                 // citation-insert <key>|<text_before>
1271                                 // this should be enhanced to also support text_after
1272                                 // and citation style
1273                                 string arg = argument;
1274                                 string opt1;
1275                                 if (contains(argument, "|")) {
1276                                         arg = token(argument, '|', 0);
1277                                         opt1 = token(argument, '|', 1);
1278                                 }
1279                                 InsetCommandParams icp(CITE_CODE);
1280                                 icp["key"] = from_utf8(arg);
1281                                 if (!opt1.empty())
1282                                         icp["before"] = from_utf8(opt1);
1283                                 string icstr = InsetCommandMailer::params2string("citation", icp);
1284                                 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1285                                 dispatch(fr);
1286                         } else
1287                                 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1288                         break;
1289                 }
1290
1291                 case LFUN_BUFFER_CHILD_OPEN: {
1292                         BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1293                         Buffer * parent = lyx_view_->buffer();
1294                         FileName filename = makeAbsPath(argument, parent->filePath());
1295                         view()->saveBookmark(false);
1296                         Buffer * child = 0;
1297                         bool parsed = false;
1298                         if (theBufferList().exists(filename.absFilename())) {
1299                                 child = theBufferList().getBuffer(filename.absFilename());
1300                         } else {
1301                                 setMessage(bformat(_("Opening child document %1$s..."),
1302                                         makeDisplayPath(filename.absFilename())));
1303                                 child = lyx_view_->loadDocument(filename, false);
1304                                 parsed = true;
1305                         }
1306                         if (child) {
1307                                 // Set the parent name of the child document.
1308                                 // This makes insertion of citations and references in the child work,
1309                                 // when the target is in the parent or another child document.
1310                                 child->setParent(parent);
1311                                 updateLabels(*child->masterBuffer());
1312                                 lyx_view_->setBuffer(child);
1313                                 if (parsed)
1314                                         child->errors("Parse");
1315                         }
1316
1317                         // If a screen update is required (in case where auto_open is false), 
1318                         // setBuffer() would have taken care of it already. Otherwise we shall 
1319                         // reset the update flag because it can cause a circular problem.
1320                         // See bug 3970.
1321                         updateFlags = Update::None;
1322                         break;
1323                 }
1324
1325                 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1326                         BOOST_ASSERT(lyx_view_);
1327                         lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1328                         break;
1329
1330                 case LFUN_KEYMAP_OFF:
1331                         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1332                         lyx_view_->view()->getIntl().keyMapOn(false);
1333                         break;
1334
1335                 case LFUN_KEYMAP_PRIMARY:
1336                         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1337                         lyx_view_->view()->getIntl().keyMapPrim();
1338                         break;
1339
1340                 case LFUN_KEYMAP_SECONDARY:
1341                         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1342                         lyx_view_->view()->getIntl().keyMapSec();
1343                         break;
1344
1345                 case LFUN_KEYMAP_TOGGLE:
1346                         BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1347                         lyx_view_->view()->getIntl().toggleKeyMap();
1348                         break;
1349
1350                 case LFUN_REPEAT: {
1351                         // repeat command
1352                         string countstr;
1353                         string rest = split(argument, countstr, ' ');
1354                         istringstream is(countstr);
1355                         int count = 0;
1356                         is >> count;
1357                         lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1358                         for (int i = 0; i < count; ++i)
1359                                 dispatch(lyxaction.lookupFunc(rest));
1360                         break;
1361                 }
1362
1363                 case LFUN_COMMAND_SEQUENCE: {
1364                         // argument contains ';'-terminated commands
1365                         string arg = argument;
1366                         while (!arg.empty()) {
1367                                 string first;
1368                                 arg = split(arg, first, ';');
1369                                 FuncRequest func(lyxaction.lookupFunc(first));
1370                                 func.origin = cmd.origin;
1371                                 dispatch(func);
1372                         }
1373                         break;
1374                 }
1375
1376                 case LFUN_CALL: {
1377                         FuncRequest func;
1378                         if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1379                                 func.origin = cmd.origin;
1380                                 dispatch(func);
1381                                 LyX::ref().topLevelCmdDef().release(argument);
1382                         } else {
1383                                 if (func.action == LFUN_UNKNOWN_ACTION) {
1384                                         // unknown command definition
1385                                         lyxerr << "Warning: unknown command definition `"
1386                                                    << argument << "'"
1387                                                    << endl;
1388                                 } else {
1389                                         // recursion detected
1390                                         lyxerr << "Warning: Recursion in the command definition `"
1391                                                    << argument << "' detected"
1392                                                    << endl;
1393                                 }
1394                         }
1395                         break;
1396                 }
1397
1398                 case LFUN_PREFERENCES_SAVE: {
1399                         lyxrc.write(makeAbsPath("preferences",
1400                                                 package().user_support().absFilename()),
1401                                     false);
1402                         break;
1403                 }
1404
1405                 case LFUN_SET_COLOR: {
1406                         string lyx_name;
1407                         string const x11_name = split(argument, lyx_name, ' ');
1408                         if (lyx_name.empty() || x11_name.empty()) {
1409                                 setErrorMessage(from_ascii(N_(
1410                                                 "Syntax: set-color <lyx_name>"
1411                                                 " <x11_name>")));
1412                                 break;
1413                         }
1414
1415                         bool const graphicsbg_changed =
1416                                 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1417                                  x11_name != lcolor.getX11Name(Color_graphicsbg));
1418
1419                         if (!lcolor.setColor(lyx_name, x11_name)) {
1420                                 setErrorMessage(
1421                                                 bformat(_("Set-color \"%1$s\" failed "
1422                                                                        "- color is undefined or "
1423                                                                        "may not be redefined"),
1424                                                                            from_utf8(lyx_name)));
1425                                 break;
1426                         }
1427
1428                         theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1429
1430                         if (graphicsbg_changed) {
1431                                 // FIXME: The graphics cache no longer has a changeDisplay method.
1432 #if 0
1433                                 graphics::GCache::get().changeDisplay(true);
1434 #endif
1435                         }
1436                         break;
1437                 }
1438
1439                 case LFUN_MESSAGE:
1440                         BOOST_ASSERT(lyx_view_);
1441                         lyx_view_->message(from_utf8(argument));
1442                         break;
1443
1444                 case LFUN_EXTERNAL_EDIT: {
1445                         BOOST_ASSERT(lyx_view_);
1446                         FuncRequest fr(action, argument);
1447                         InsetExternal().dispatch(view()->cursor(), fr);
1448                         break;
1449                 }
1450
1451                 case LFUN_GRAPHICS_EDIT: {
1452                         FuncRequest fr(action, argument);
1453                         InsetGraphics().dispatch(view()->cursor(), fr);
1454                         break;
1455                 }
1456
1457                 case LFUN_ALL_INSETS_TOGGLE: {
1458                         BOOST_ASSERT(lyx_view_);
1459                         string action;
1460                         string const name = split(argument, action, ' ');
1461                         InsetCode const inset_code = insetCode(name);
1462
1463                         Cursor & cur = view()->cursor();
1464                         FuncRequest fr(LFUN_INSET_TOGGLE, action);
1465
1466                         Inset & inset = lyx_view_->buffer()->inset();
1467                         InsetIterator it  = inset_iterator_begin(inset);
1468                         InsetIterator const end = inset_iterator_end(inset);
1469                         for (; it != end; ++it) {
1470                                 if (!it->asInsetMath()
1471                                     && (inset_code == NO_CODE
1472                                     || inset_code == it->lyxCode())) {
1473                                         Cursor tmpcur = cur;
1474                                         tmpcur.pushBackward(*it);
1475                                         it->dispatch(tmpcur, fr);
1476                                 }
1477                         }
1478                         updateFlags = Update::Force | Update::FitCursor;
1479                         break;
1480                 }
1481
1482                 case LFUN_BUFFER_LANGUAGE: {
1483                         BOOST_ASSERT(lyx_view_);
1484                         Buffer & buffer = *lyx_view_->buffer();
1485                         Language const * oldL = buffer.params().language;
1486                         Language const * newL = languages.getLanguage(argument);
1487                         if (!newL || oldL == newL)
1488                                 break;
1489
1490                         if (oldL->rightToLeft() == newL->rightToLeft()
1491                             && !buffer.isMultiLingual())
1492                                 buffer.changeLanguage(oldL, newL);
1493                         break;
1494                 }
1495
1496                 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1497                         string const fname =
1498                                 addName(addPath(package().user_support().absFilename(), "templates/"),
1499                                         "defaults.lyx");
1500                         Buffer defaults(fname);
1501
1502                         istringstream ss(argument);
1503                         Lexer lex(0,0);
1504                         lex.setStream(ss);
1505                         int const unknown_tokens = defaults.readHeader(lex);
1506
1507                         if (unknown_tokens != 0) {
1508                                 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1509                                        << unknown_tokens << " unknown token"
1510                                        << (unknown_tokens == 1 ? "" : "s")
1511                                        << endl;
1512                         }
1513
1514                         if (defaults.writeFile(FileName(defaults.absFileName())))
1515                                 setMessage(bformat(_("Document defaults saved in %1$s"),
1516                                                    makeDisplayPath(fname)));
1517                         else
1518                                 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1519                         break;
1520                 }
1521
1522                 case LFUN_BUFFER_PARAMS_APPLY: {
1523                         BOOST_ASSERT(lyx_view_);
1524                         biblio::CiteEngine const oldEngine =
1525                                         lyx_view_->buffer()->params().getEngine();
1526                         
1527                         Buffer * buffer = lyx_view_->buffer();
1528
1529                         TextClassPtr oldClass = buffer->params().getTextClassPtr();
1530
1531                         Cursor & cur = view()->cursor();
1532                         cur.recordUndoFullDocument();
1533                         
1534                         istringstream ss(argument);
1535                         Lexer lex(0,0);
1536                         lex.setStream(ss);
1537                         int const unknown_tokens = buffer->readHeader(lex);
1538
1539                         if (unknown_tokens != 0) {
1540                                 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1541                                                 << unknown_tokens << " unknown token"
1542                                                 << (unknown_tokens == 1 ? "" : "s")
1543                                                 << endl;
1544                         }
1545                         
1546                         updateLayout(oldClass, buffer);
1547                         
1548                         biblio::CiteEngine const newEngine =
1549                                         lyx_view_->buffer()->params().getEngine();
1550                         
1551                         if (oldEngine != newEngine) {
1552                                 FuncRequest fr(LFUN_INSET_REFRESH);
1553         
1554                                 Inset & inset = lyx_view_->buffer()->inset();
1555                                 InsetIterator it  = inset_iterator_begin(inset);
1556                                 InsetIterator const end = inset_iterator_end(inset);
1557                                 for (; it != end; ++it)
1558                                         if (it->lyxCode() == CITE_CODE)
1559                                                 it->dispatch(cur, fr);
1560                         }
1561                         
1562                         updateFlags = Update::Force | Update::FitCursor;
1563                         // We are here most certainaly because of a change in the document
1564                         // It is then better to make sure that all dialogs are in sync
1565                         // with current document settings. LyXView::restartCursor() achieve this.
1566                         lyx_view_->restartCursor();
1567                         break;
1568                 }
1569                 
1570                 case LFUN_LAYOUT_MODULES_CLEAR: {
1571                         BOOST_ASSERT(lyx_view_);
1572                         Buffer * buffer = lyx_view_->buffer();
1573                         TextClassPtr oldClass = buffer->params().getTextClassPtr();
1574                         view()->cursor().recordUndoFullDocument();
1575                         buffer->params().clearLayoutModules();
1576                         buffer->params().makeTextClass();
1577                         updateLayout(oldClass, buffer);
1578                         updateFlags = Update::Force | Update::FitCursor;
1579                         break;
1580                 }
1581                 
1582                 case LFUN_LAYOUT_MODULE_ADD: {
1583                         BOOST_ASSERT(lyx_view_);
1584                         Buffer * buffer = lyx_view_->buffer();
1585                         TextClassPtr oldClass = buffer->params().getTextClassPtr();
1586                         view()->cursor().recordUndoFullDocument();
1587                         buffer->params().addLayoutModule(argument);
1588                         buffer->params().makeTextClass();
1589                         updateLayout(oldClass, buffer);
1590                         updateFlags = Update::Force | Update::FitCursor;
1591                         break;
1592                 }
1593
1594                 case LFUN_TEXTCLASS_APPLY: {
1595                         BOOST_ASSERT(lyx_view_);
1596                         Buffer * buffer = lyx_view_->buffer();
1597
1598                         loadTextClass(argument);
1599
1600                         pair<bool, textclass_type> const tc_pair =
1601                                 textclasslist.numberOfClass(argument);
1602
1603                         if (!tc_pair.first)
1604                                 break;
1605
1606                         textclass_type const old_class = buffer->params().getBaseClass();
1607                         textclass_type const new_class = tc_pair.second;
1608
1609                         if (old_class == new_class)
1610                                 // nothing to do
1611                                 break;
1612
1613                         //Save the old, possibly modular, layout for use in conversion.
1614                         TextClassPtr oldClass = buffer->params().getTextClassPtr();
1615                         view()->cursor().recordUndoFullDocument();
1616                         buffer->params().setBaseClass(new_class);
1617                         buffer->params().makeTextClass();
1618                         updateLayout(oldClass, buffer);
1619                         updateFlags = Update::Force | Update::FitCursor;
1620                         break;
1621                 }
1622                 
1623                 case LFUN_LAYOUT_RELOAD: {
1624                         BOOST_ASSERT(lyx_view_);
1625                         Buffer * buffer = lyx_view_->buffer();
1626                         TextClassPtr oldClass = buffer->params().getTextClassPtr();
1627                         textclass_type const tc = buffer->params().getBaseClass();
1628                         textclasslist.reset(tc);
1629                         buffer->params().setBaseClass(tc);
1630                         buffer->params().makeTextClass();
1631                         updateLayout(oldClass, buffer);
1632                         updateFlags = Update::Force | Update::FitCursor;
1633                         break;
1634                 }
1635
1636                 case LFUN_TEXTCLASS_LOAD:
1637                         loadTextClass(argument);
1638                         break;
1639
1640                 case LFUN_LYXRC_APPLY: {
1641                         LyXRC const lyxrc_orig = lyxrc;
1642
1643                         istringstream ss(argument);
1644                         bool const success = lyxrc.read(ss) == 0;
1645
1646                         if (!success) {
1647                                 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1648                                        << "Unable to read lyxrc data"
1649                                        << endl;
1650                                 break;
1651                         }
1652
1653                         actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1654
1655                         theApp()->resetGui();
1656
1657                         /// We force the redraw in any case because there might be
1658                         /// some screen font changes.
1659                         /// FIXME: only the current view will be updated. the Gui
1660                         /// class is able to furnish the list of views.
1661                         updateFlags = Update::Force;
1662                         break;
1663                 }
1664
1665                 case LFUN_BOOKMARK_GOTO:
1666                         // go to bookmark, open unopened file and switch to buffer if necessary
1667                         gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1668                         updateFlags = Update::FitCursor;
1669                         break;
1670
1671                 case LFUN_BOOKMARK_CLEAR:
1672                         LyX::ref().session().bookmarks().clear();
1673                         break;
1674
1675                 default:
1676                         BOOST_ASSERT(theApp());
1677                         // Let the frontend dispatch its own actions.
1678                         if (theApp()->dispatch(cmd))
1679                                 // Nothing more to do.
1680                                 return;
1681
1682                         // Let the current LyXView dispatch its own actions.
1683                         BOOST_ASSERT(lyx_view_);
1684                         if (lyx_view_->dispatch(cmd)) {
1685                                 if (lyx_view_->view())
1686                                         updateFlags = lyx_view_->view()->cursor().result().update();
1687                                 break;
1688                         }
1689
1690                         BOOST_ASSERT(lyx_view_->view());
1691                         // Let the current BufferView dispatch its own actions.
1692                         if (view()->dispatch(cmd)) {
1693                                 // The BufferView took care of its own updates if needed.
1694                                 updateFlags = Update::None;
1695                                 break;
1696                         }
1697
1698                         // Let the current Cursor dispatch its own actions.
1699                         view()->cursor().getPos(cursorPosBeforeDispatchX_,
1700                                                 cursorPosBeforeDispatchY_);
1701                         view()->cursor().dispatch(cmd);
1702                         updateFlags = view()->cursor().result().update();
1703                         if (!view()->cursor().result().dispatched()) {
1704                                 // No update needed in this case (e.g. when reaching
1705                                 // top of document.
1706                                 updateFlags = Update::None;
1707                         }
1708                 }
1709
1710                 if (lyx_view_ && lyx_view_->buffer()) {
1711                         // BufferView::update() updates the ViewMetricsInfo and
1712                         // also initializes the position cache for all insets in
1713                         // (at least partially) visible top-level paragraphs.
1714                         // We will redraw the screen only if needed.
1715                         view()->processUpdateFlags(updateFlags);
1716
1717                         // if we executed a mutating lfun, mark the buffer as dirty
1718                         if (flag.enabled()
1719                             && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1720                             && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1721                                 lyx_view_->buffer()->markDirty();                       
1722
1723                         //Do we have a selection?
1724                         theSelection().haveSelection(view()->cursor().selection());
1725                 }
1726         }
1727         if (!quitting && lyx_view_) {
1728                 // Some messages may already be translated, so we cannot use _()
1729                 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1730         }
1731 }
1732
1733
1734 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1735 {
1736         const bool verbose = (cmd.origin == FuncRequest::MENU
1737                               || cmd.origin == FuncRequest::TOOLBAR
1738                               || cmd.origin == FuncRequest::COMMANDBUFFER);
1739
1740         if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1741                 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1742                 if (!msg.empty())
1743                         lyx_view_->message(msg);
1744                 return;
1745         }
1746
1747         docstring dispatch_msg = msg;
1748         if (!dispatch_msg.empty())
1749                 dispatch_msg += ' ';
1750
1751         docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1752
1753         bool argsadded = false;
1754
1755         if (!cmd.argument().empty()) {
1756                 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1757                         comname += ' ' + cmd.argument();
1758                         argsadded = true;
1759                 }
1760         }
1761
1762         docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1763
1764         if (!shortcuts.empty())
1765                 comname += ": " + shortcuts;
1766         else if (!argsadded && !cmd.argument().empty())
1767                 comname += ' ' + cmd.argument();
1768
1769         if (!comname.empty()) {
1770                 comname = rtrim(comname);
1771                 dispatch_msg += '(' + rtrim(comname) + ')';
1772         }
1773
1774         LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1775         if (!dispatch_msg.empty())
1776                 lyx_view_->message(dispatch_msg);
1777 }
1778
1779
1780 void LyXFunc::closeBuffer()
1781 {
1782         // goto bookmark to update bookmark pit.
1783         for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1784                 gotoBookmark(i+1, false, false);
1785         
1786         lyx_view_->closeBuffer();
1787 }
1788
1789
1790 void LyXFunc::reloadBuffer()
1791 {
1792         FileName filename = lyx_view_->buffer()->fileName();
1793         // The user has already confirmed that the changes, if any, should
1794         // be discarded. So we just release the Buffer and don't call closeBuffer();
1795         theBufferList().release(lyx_view_->buffer());
1796         Buffer * buf = lyx_view_->loadDocument(filename);
1797         docstring const disp_fn = makeDisplayPath(filename.absFilename());
1798         docstring str;
1799         if (buf) {
1800                 updateLabels(*buf);
1801                 lyx_view_->setBuffer(buf);
1802                 buf->errors("Parse");
1803                 str = bformat(_("Document %1$s reloaded."), disp_fn);
1804         } else {
1805                 str = bformat(_("Could not reload document %1$s"), disp_fn);
1806         }
1807         lyx_view_->message(str);
1808 }
1809
1810 // Each "lyx_view_" should have it's own message method. lyxview and
1811 // the minibuffer would use the minibuffer, but lyxserver would
1812 // send an ERROR signal to its client.  Alejandro 970603
1813 // This function is bit problematic when it comes to NLS, to make the
1814 // lyx servers client be language indepenent we must not translate
1815 // strings sent to this func.
1816 void LyXFunc::setErrorMessage(docstring const & m) const
1817 {
1818         dispatch_buffer = m;
1819         errorstat = true;
1820 }
1821
1822
1823 void LyXFunc::setMessage(docstring const & m) const
1824 {
1825         dispatch_buffer = m;
1826 }
1827
1828
1829 docstring const LyXFunc::viewStatusMessage()
1830 {
1831         // When meta-fake key is pressed, show the key sequence so far + "M-".
1832         if (wasMetaKey())
1833                 return keyseq.print(KeySequence::ForGui) + "M-";
1834
1835         // Else, when a non-complete key sequence is pressed,
1836         // show the available options.
1837         if (keyseq.length() > 0 && !keyseq.deleted())
1838                 return keyseq.printOptions(true);
1839
1840         BOOST_ASSERT(lyx_view_);
1841         if (!lyx_view_->buffer())
1842                 return _("Welcome to LyX!");
1843
1844         return view()->cursor().currentState();
1845 }
1846
1847
1848 BufferView * LyXFunc::view() const
1849 {
1850         BOOST_ASSERT(lyx_view_);
1851         return lyx_view_->view();
1852 }
1853
1854
1855 bool LyXFunc::wasMetaKey() const
1856 {
1857         return (meta_fake_bit != NoModifier);
1858 }
1859
1860
1861 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1862                            Buffer * buffer)
1863 {
1864         lyx_view_->message(_("Converting document to new document class..."));
1865         
1866         StableDocIterator backcur(view()->cursor());
1867         ErrorList & el = buffer->errorList("Class Switch");
1868         cap::switchBetweenClasses(
1869                         oldlayout, buffer->params().getTextClassPtr(),
1870                         static_cast<InsetText &>(buffer->inset()), el);
1871
1872         view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1873
1874         buffer->errors("Class Switch");
1875         updateLabels(*buffer);
1876 }
1877
1878
1879 namespace {
1880
1881 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1882 {
1883         // Why the switch you might ask. It is a trick to ensure that all
1884         // the elements in the LyXRCTags enum is handled. As you can see
1885         // there are no breaks at all. So it is just a huge fall-through.
1886         // The nice thing is that we will get a warning from the compiler
1887         // if we forget an element.
1888         LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1889         switch (tag) {
1890         case LyXRC::RC_ACCEPT_COMPOUND:
1891         case LyXRC::RC_ALT_LANG:
1892         case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1893         case LyXRC::RC_PLAINTEXT_LINELEN:
1894         case LyXRC::RC_AUTOREGIONDELETE:
1895         case LyXRC::RC_AUTORESET_OPTIONS:
1896         case LyXRC::RC_AUTOSAVE:
1897         case LyXRC::RC_AUTO_NUMBER:
1898         case LyXRC::RC_BACKUPDIR_PATH:
1899         case LyXRC::RC_BIBTEX_COMMAND:
1900         case LyXRC::RC_BINDFILE:
1901         case LyXRC::RC_CHECKLASTFILES:
1902         case LyXRC::RC_USELASTFILEPOS:
1903         case LyXRC::RC_LOADSESSION:
1904         case LyXRC::RC_CHKTEX_COMMAND:
1905         case LyXRC::RC_CONVERTER:
1906         case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1907         case LyXRC::RC_COPIER:
1908         case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1909         case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1910         case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1911         case LyXRC::RC_DATE_INSERT_FORMAT:
1912         case LyXRC::RC_DEFAULT_LANGUAGE:
1913         case LyXRC::RC_DEFAULT_PAPERSIZE:
1914         case LyXRC::RC_DEFFILE:
1915         case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1916         case LyXRC::RC_DISPLAY_GRAPHICS:
1917         case LyXRC::RC_DOCUMENTPATH:
1918                 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1919                         FileName path(lyxrc_new.document_path);
1920                         if (path.exists() && path.isDirectory())
1921                                 package().document_dir() = FileName(lyxrc.document_path);
1922                 }
1923         case LyXRC::RC_ESC_CHARS:
1924         case LyXRC::RC_EXAMPLEPATH:
1925         case LyXRC::RC_FONT_ENCODING:
1926         case LyXRC::RC_FORMAT:
1927         case LyXRC::RC_INDEX_COMMAND:
1928         case LyXRC::RC_INPUT:
1929         case LyXRC::RC_KBMAP:
1930         case LyXRC::RC_KBMAP_PRIMARY:
1931         case LyXRC::RC_KBMAP_SECONDARY:
1932         case LyXRC::RC_LABEL_INIT_LENGTH:
1933         case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1934         case LyXRC::RC_LANGUAGE_AUTO_END:
1935         case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1936         case LyXRC::RC_LANGUAGE_COMMAND_END:
1937         case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1938         case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1939         case LyXRC::RC_LANGUAGE_PACKAGE:
1940         case LyXRC::RC_LANGUAGE_USE_BABEL:
1941         case LyXRC::RC_MACRO_EDIT_STYLE:
1942         case LyXRC::RC_MAKE_BACKUP:
1943         case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1944         case LyXRC::RC_MOUSE_WHEEL_SPEED:
1945         case LyXRC::RC_NUMLASTFILES:
1946         case LyXRC::RC_PATH_PREFIX:
1947                 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1948                         prependEnvPath("PATH", lyxrc.path_prefix);
1949                 }
1950         case LyXRC::RC_PERS_DICT:
1951         case LyXRC::RC_PREVIEW:
1952         case LyXRC::RC_PREVIEW_HASHED_LABELS:
1953         case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1954         case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1955         case LyXRC::RC_PRINTCOPIESFLAG:
1956         case LyXRC::RC_PRINTER:
1957         case LyXRC::RC_PRINTEVENPAGEFLAG:
1958         case LyXRC::RC_PRINTEXSTRAOPTIONS:
1959         case LyXRC::RC_PRINTFILEEXTENSION:
1960         case LyXRC::RC_PRINTLANDSCAPEFLAG:
1961         case LyXRC::RC_PRINTODDPAGEFLAG:
1962         case LyXRC::RC_PRINTPAGERANGEFLAG:
1963         case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1964         case LyXRC::RC_PRINTPAPERFLAG:
1965         case LyXRC::RC_PRINTREVERSEFLAG:
1966         case LyXRC::RC_PRINTSPOOL_COMMAND:
1967         case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1968         case LyXRC::RC_PRINTTOFILE:
1969         case LyXRC::RC_PRINTTOPRINTER:
1970         case LyXRC::RC_PRINT_ADAPTOUTPUT:
1971         case LyXRC::RC_PRINT_COMMAND:
1972         case LyXRC::RC_RTL_SUPPORT:
1973         case LyXRC::RC_SCREEN_DPI:
1974         case LyXRC::RC_SCREEN_FONT_ROMAN:
1975         case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1976         case LyXRC::RC_SCREEN_FONT_SANS:
1977         case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1978         case LyXRC::RC_SCREEN_FONT_SCALABLE:
1979         case LyXRC::RC_SCREEN_FONT_SIZES:
1980         case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1981         case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1982         case LyXRC::RC_GEOMETRY_SESSION:
1983         case LyXRC::RC_SCREEN_ZOOM:
1984         case LyXRC::RC_SERVERPIPE:
1985         case LyXRC::RC_SET_COLOR:
1986         case LyXRC::RC_SHOW_BANNER:
1987         case LyXRC::RC_SPELL_COMMAND:
1988         case LyXRC::RC_TEMPDIRPATH:
1989         case LyXRC::RC_TEMPLATEPATH:
1990         case LyXRC::RC_TEX_ALLOWS_SPACES:
1991         case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1992                 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1993                         os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1994                 }
1995         case LyXRC::RC_UIFILE:
1996         case LyXRC::RC_USER_EMAIL:
1997         case LyXRC::RC_USER_NAME:
1998         case LyXRC::RC_USETEMPDIR:
1999         case LyXRC::RC_USE_ALT_LANG:
2000         case LyXRC::RC_USE_CONVERTER_CACHE:
2001         case LyXRC::RC_USE_ESC_CHARS:
2002         case LyXRC::RC_USE_INP_ENC:
2003         case LyXRC::RC_USE_PERS_DICT:
2004         case LyXRC::RC_USE_TOOLTIP:
2005         case LyXRC::RC_USE_PIXMAP_CACHE:
2006         case LyXRC::RC_USE_SPELL_LIB:
2007         case LyXRC::RC_VIEWDVI_PAPEROPTION:
2008         case LyXRC::RC_SORT_LAYOUTS:
2009         case LyXRC::RC_VIEWER:
2010         case LyXRC::RC_LAST:
2011                 break;
2012         }
2013 }
2014
2015 } // namespace anon
2016
2017
2018 } // namespace lyx