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