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