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