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