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