3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "LayoutFile.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetListings.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetSpace.h"
70 #include "insets/InsetTabular.h"
71 #include "insets/InsetVSpace.h"
72 #include "insets/InsetWrap.h"
74 #include "frontends/alert.h"
75 #include "frontends/Application.h"
76 #include "frontends/KeySymbol.h"
77 #include "frontends/LyXView.h"
78 #include "frontends/Selection.h"
80 #include "support/debug.h"
81 #include "support/environment.h"
82 #include "support/FileName.h"
83 #include "support/filetools.h"
84 #include "support/gettext.h"
85 #include "support/lstrings.h"
86 #include "support/Path.h"
87 #include "support/Package.h"
88 #include "support/Systemcall.h"
89 #include "support/convert.h"
90 #include "support/os.h"
96 using namespace lyx::support;
100 using frontend::LyXView;
102 namespace Alert = frontend::Alert;
107 // This function runs "configure" and then rereads lyx.defaults to
108 // reconfigure the automatic settings.
109 void reconfigure(LyXView * lv, string const & option)
111 // emit message signal.
113 lv->message(_("Running configure..."));
115 // Run configure in user lyx directory
116 PathChanger p(package().user_support());
117 string configure_command = package().configure_command();
118 configure_command += option;
120 int ret = one.startscript(Systemcall::Wait, configure_command);
122 // emit message signal.
124 lv->message(_("Reloading configuration..."));
125 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
126 // Re-read packages.lst
127 LaTeXFeatures::getAvailable();
130 Alert::information(_("System reconfiguration failed"),
131 _("The system reconfiguration has failed.\n"
132 "Default textclass is used but LyX may "
133 "not be able to work properly.\n"
134 "Please reconfigure again if needed."));
137 Alert::information(_("System reconfigured"),
138 _("The system has been reconfigured.\n"
139 "You need to restart LyX to make use of any\n"
140 "updated document class specifications."));
144 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
146 // Try to fix cursor in case it is broken.
147 cursor.fixIfBroken();
149 // This is, of course, a mess. Better create a new doc iterator and use
150 // this in Inset::getStatus. This might require an additional
151 // BufferView * arg, though (which should be avoided)
152 //Cursor safe = *this;
154 for ( ; cursor.depth(); cursor.pop()) {
155 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
156 LASSERT(cursor.idx() <= cursor.lastidx(), /**/);
157 LASSERT(cursor.pit() <= cursor.lastpit(), /**/);
158 LASSERT(cursor.pos() <= cursor.lastpos(), /**/);
160 // The inset's getStatus() will return 'true' if it made
161 // a definitive decision on whether it want to handle the
162 // request or not. The result of this decision is put into
163 // the 'status' parameter.
164 if (cursor.inset().getStatus(cursor, cmd, status)) {
173 /** Return the change status at cursor position, taking in account the
174 * status at each level of the document iterator (a table in a deleted
175 * footnote is deleted).
176 * When \param outer is true, the top slice is not looked at.
178 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
180 size_t const depth = dit.depth() - (outer ? 1 : 0);
182 for (size_t i = 0 ; i < depth ; ++i) {
183 CursorSlice const & slice = dit[i];
184 if (!slice.inset().inMathed()
185 && slice.pos() < slice.paragraph().size()) {
186 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
187 if (ch != Change::UNCHANGED)
191 return Change::UNCHANGED;
198 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
203 void LyXFunc::initKeySequences(KeyMap * kb)
205 keyseq = KeySequence(kb, kb);
206 cancel_meta_seq = KeySequence(kb, kb);
210 void LyXFunc::setLyXView(LyXView * lv)
212 if (lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
213 // save current selection to the selection buffer to allow
214 // middle-button paste in another window
215 cap::saveSelection(lyx_view_->view()->cursor());
220 void LyXFunc::handleKeyFunc(FuncCode action)
222 char_type c = encoded_last_key;
227 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
228 lyx_view_->view()->getIntl().getTransManager().deadkey(
229 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
230 // Need to clear, in case the minibuffer calls these
233 // copied verbatim from do_accent_char
234 view()->cursor().resetAnchor();
235 view()->processUpdateFlags(Update::FitCursor);
238 //FIXME: bookmark handling is a frontend issue. This code should be transferred
239 // to GuiView and be GuiView and be window dependent.
240 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
242 LASSERT(lyx_view_, /**/);
243 if (!LyX::ref().session().bookmarks().isValid(idx))
245 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
246 LASSERT(!bm.filename.empty(), /**/);
247 string const file = bm.filename.absFilename();
248 // if the file is not opened, open it.
249 if (!theBufferList().exists(file)) {
251 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
255 // open may fail, so we need to test it again
256 if (!theBufferList().exists(file))
259 // if the current buffer is not that one, switch to it.
260 if (lyx_view_->buffer()->absFileName() != file) {
263 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
265 // moveToPosition try paragraph id first and then paragraph (pit, pos).
266 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
267 bm.top_id, bm.top_pos))
270 // Cursor jump succeeded!
271 Cursor const & cur = view()->cursor();
272 pit_type new_pit = cur.pit();
273 pos_type new_pos = cur.pos();
274 int new_id = cur.paragraph().id();
276 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
277 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
278 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
279 || bm.top_id != new_id) {
280 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
281 new_pit, new_pos, new_id);
286 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
288 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
290 // Do nothing if we have nothing (JMarc)
291 if (!keysym.isOK()) {
292 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
293 lyx_view_->restartCursor();
297 if (keysym.isModifier()) {
298 LYXERR(Debug::KEY, "isModifier true");
300 lyx_view_->restartCursor();
304 //Encoding const * encoding = view()->cursor().getEncoding();
305 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
306 // FIXME: encoded_last_key shadows the member variable of the same
307 // name. Is that intended?
308 char_type encoded_last_key = keysym.getUCSEncoded();
310 // Do a one-deep top-level lookup for
311 // cancel and meta-fake keys. RVDK_PATCH_5
312 cancel_meta_seq.reset();
314 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
315 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
317 // When not cancel or meta-fake, do the normal lookup.
318 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
319 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
320 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
321 // remove Caps Lock and Mod2 as a modifiers
322 func = keyseq.addkey(keysym, (state | meta_fake_bit));
323 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
326 // Dont remove this unless you know what you are doing.
327 meta_fake_bit = NoModifier;
329 // Can this happen now ?
330 if (func.action == LFUN_NOACTION)
331 func = FuncRequest(LFUN_COMMAND_PREFIX);
333 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
334 << keyseq.print(KeySequence::Portable) << ']');
336 // already here we know if it any point in going further
337 // why not return already here if action == -1 and
338 // num_bytes == 0? (Lgb)
340 if (keyseq.length() > 1)
341 lyx_view_->message(keyseq.print(KeySequence::ForGui));
344 // Maybe user can only reach the key via holding down shift.
345 // Let's see. But only if shift is the only modifier
346 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
347 LYXERR(Debug::KEY, "Trying without shift");
348 func = keyseq.addkey(keysym, NoModifier);
349 LYXERR(Debug::KEY, "Action now " << func.action);
352 if (func.action == LFUN_UNKNOWN_ACTION) {
353 // Hmm, we didn't match any of the keysequences. See
354 // if it's normal insertable text not already covered
356 if (keysym.isText() && keyseq.length() == 1) {
357 LYXERR(Debug::KEY, "isText() is true, inserting.");
358 func = FuncRequest(LFUN_SELF_INSERT,
359 FuncRequest::KEYBOARD);
361 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
362 lyx_view_->message(_("Unknown function."));
363 lyx_view_->restartCursor();
368 if (func.action == LFUN_SELF_INSERT) {
369 if (encoded_last_key != 0) {
370 docstring const arg(1, encoded_last_key);
371 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
372 FuncRequest::KEYBOARD));
373 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
382 lyx_view_->restartCursor();
386 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
388 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
391 Buffer * buf = lyx_view_ ? lyx_view_->buffer() : 0;
393 if (cmd.action == LFUN_NOACTION) {
394 flag.message(from_utf8(N_("Nothing to do")));
399 switch (cmd.action) {
400 case LFUN_UNKNOWN_ACTION:
401 #ifndef HAVE_LIBAIKSAURUS
402 case LFUN_THESAURUS_ENTRY:
412 if (flag.unknown()) {
413 flag.message(from_utf8(N_("Unknown action")));
417 if (!flag.enabled()) {
418 if (flag.message().empty())
419 flag.message(from_utf8(N_("Command disabled")));
423 // Check whether we need a buffer
424 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
426 flag.message(from_utf8(N_("Command not allowed with"
427 "out any document open")));
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')
437 switch (cmd.action) {
439 // FIXME: these cases should be hidden in GuiApplication::getStatus().
440 case LFUN_WINDOW_CLOSE:
442 return theApp()->getStatus(cmd);
446 // FIXME: these cases should be hidden in GuiView::getStatus().
447 case LFUN_DIALOG_TOGGLE:
448 case LFUN_DIALOG_SHOW:
450 case LFUN_DIALOG_UPDATE:
451 // FIXME: add special handling for about and prefs dialogs here
452 // which do not depend on GuiView.
454 return lyx_view_->getStatus(cmd);
459 case LFUN_TOOLBAR_TOGGLE:
460 case LFUN_INSET_APPLY:
461 case LFUN_BUFFER_WRITE:
462 case LFUN_BUFFER_WRITE_AS:
463 case LFUN_SPLIT_VIEW:
464 case LFUN_CLOSE_TAB_GROUP:
465 case LFUN_COMPLETION_POPUP:
466 case LFUN_COMPLETION_INLINE:
467 case LFUN_COMPLETION_COMPLETE:
469 return lyx_view_->getStatus(cmd);
473 case LFUN_BUFFER_TOGGLE_READ_ONLY:
474 flag.setOnOff(buf->isReadonly());
477 case LFUN_BUFFER_SWITCH:
478 // toggle on the current buffer, but do not toggle off
479 // the other ones (is that a good idea?)
480 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
484 case LFUN_BUFFER_EXPORT:
485 enable = cmd.argument() == "custom"
486 || buf->isExportable(to_utf8(cmd.argument()));
489 case LFUN_BUFFER_CHKTEX:
490 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
493 case LFUN_BUILD_PROGRAM:
494 enable = buf->isExportable("program");
497 case LFUN_VC_REGISTER:
498 enable = !buf->lyxvc().inUse();
500 case LFUN_VC_CHECK_IN:
501 enable = buf->lyxvc().inUse() && !buf->isReadonly();
503 case LFUN_VC_CHECK_OUT:
504 enable = buf->lyxvc().inUse() && buf->isReadonly();
507 case LFUN_VC_UNDO_LAST:
508 enable = buf->lyxvc().inUse();
510 case LFUN_BUFFER_RELOAD:
511 enable = !buf->isUnnamed() && buf->fileName().exists()
512 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
515 case LFUN_CITATION_INSERT: {
516 FuncRequest fr(LFUN_INSET_INSERT, "citation");
517 enable = getStatus(fr).enabled();
521 // This could be used for the no-GUI version. The GUI version is handled in
522 // LyXView::getStatus(). See above.
524 case LFUN_BUFFER_WRITE:
525 case LFUN_BUFFER_WRITE_AS: {
526 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
527 enable = b && (b->isUnnamed() || !b->isClean());
532 case LFUN_BUFFER_WRITE_ALL: {
533 // We enable the command only if there are some modified buffers
534 Buffer * first = theBufferList().first();
539 // We cannot use a for loop as the buffer list is a cycle.
545 b = theBufferList().next(b);
546 } while (b != first);
550 case LFUN_BOOKMARK_GOTO: {
551 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
552 enable = LyX::ref().session().bookmarks().isValid(num);
556 case LFUN_BOOKMARK_CLEAR:
557 enable = LyX::ref().session().bookmarks().size() > 0;
560 // this one is difficult to get right. As a half-baked
561 // solution, we consider only the first action of the sequence
562 case LFUN_COMMAND_SEQUENCE: {
563 // argument contains ';'-terminated commands
564 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
565 FuncRequest func(lyxaction.lookupFunc(firstcmd));
566 func.origin = cmd.origin;
567 flag = getStatus(func);
573 string name = to_utf8(cmd.argument());
574 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
575 func.origin = cmd.origin;
576 flag = getStatus(func);
577 LyX::ref().topLevelCmdDef().release(name);
579 // catch recursion or unknown command definiton
580 // all operations until the recursion or unknown command
581 // definiton occures are performed, so set the state to enabled
587 case LFUN_BUFFER_NEW:
588 case LFUN_BUFFER_NEW_TEMPLATE:
589 case LFUN_WORD_FIND_FORWARD:
590 case LFUN_WORD_FIND_BACKWARD:
591 case LFUN_COMMAND_PREFIX:
592 case LFUN_COMMAND_EXECUTE:
594 case LFUN_META_PREFIX:
595 case LFUN_BUFFER_CLOSE:
596 case LFUN_BUFFER_UPDATE:
597 case LFUN_BUFFER_VIEW:
598 case LFUN_MASTER_BUFFER_UPDATE:
599 case LFUN_MASTER_BUFFER_VIEW:
600 case LFUN_BUFFER_IMPORT:
601 case LFUN_BUFFER_AUTO_SAVE:
602 case LFUN_RECONFIGURE:
605 case LFUN_DROP_LAYOUTS_CHOICE:
607 case LFUN_SERVER_GET_NAME:
608 case LFUN_SERVER_NOTIFY:
609 case LFUN_SERVER_GOTO_FILE_ROW:
610 case LFUN_DIALOG_HIDE:
611 case LFUN_DIALOG_DISCONNECT_INSET:
612 case LFUN_BUFFER_CHILD_OPEN:
613 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
614 case LFUN_KEYMAP_OFF:
615 case LFUN_KEYMAP_PRIMARY:
616 case LFUN_KEYMAP_SECONDARY:
617 case LFUN_KEYMAP_TOGGLE:
619 case LFUN_BUFFER_EXPORT_CUSTOM:
620 case LFUN_BUFFER_PRINT:
621 case LFUN_PREFERENCES_SAVE:
622 case LFUN_SCREEN_FONT_UPDATE:
625 case LFUN_INSET_EDIT:
626 case LFUN_ALL_INSETS_TOGGLE:
627 case LFUN_BUFFER_LANGUAGE:
628 case LFUN_TEXTCLASS_APPLY:
629 case LFUN_TEXTCLASS_LOAD:
630 case LFUN_BUFFER_SAVE_AS_DEFAULT:
631 case LFUN_BUFFER_PARAMS_APPLY:
632 case LFUN_LAYOUT_MODULES_CLEAR:
633 case LFUN_LAYOUT_MODULE_ADD:
634 case LFUN_LAYOUT_RELOAD:
635 case LFUN_LYXRC_APPLY:
636 case LFUN_BUFFER_NEXT:
637 case LFUN_BUFFER_PREVIOUS:
638 case LFUN_WINDOW_NEW:
640 // these are handled in our dispatch()
648 if (!getLocalStatus(view()->cursor(), cmd, flag))
649 flag = view()->getStatus(cmd);
655 // Can we use a readonly buffer?
656 if (buf && buf->isReadonly()
657 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
658 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
659 flag.message(from_utf8(N_("Document is read-only")));
663 // Are we in a DELETED change-tracking region?
665 && lookupChangeType(view()->cursor(), true) == Change::DELETED
666 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
667 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
668 flag.message(from_utf8(N_("This portion of the document is deleted.")));
672 // the default error message if we disable the command
673 if (!flag.enabled() && flag.message().empty())
674 flag.message(from_utf8(N_("Command disabled")));
680 bool LyXFunc::ensureBufferClean(BufferView * bv)
682 Buffer & buf = bv->buffer();
686 docstring const file = buf.fileName().displayName(30);
687 docstring text = bformat(_("The document %1$s has unsaved "
688 "changes.\n\nDo you want to save "
689 "the document?"), file);
690 int const ret = Alert::prompt(_("Save changed document?"),
691 text, 0, 1, _("&Save"),
695 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
697 return buf.isClean();
703 void showPrintError(string const & name)
705 docstring str = bformat(_("Could not print the document %1$s.\n"
706 "Check that your printer is set up correctly."),
707 makeDisplayPath(name, 50));
708 Alert::error(_("Print document failed"), str);
712 bool loadLayoutFile(string const & name, string const & buf_path)
714 if (!LayoutFileList::get().haveClass(name)) {
715 lyxerr << "Document class \"" << name
716 << "\" does not exist."
721 LayoutFile & tc = LayoutFileList::get()[name];
722 if (!tc.load(buf_path)) {
723 docstring s = bformat(_("The document class %1$s "
724 "could not be loaded."), from_utf8(name));
725 Alert::error(_("Could not load class"), s);
732 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
737 void LyXFunc::dispatch(FuncRequest const & cmd)
739 string const argument = to_utf8(cmd.argument());
740 FuncCode const action = cmd.action;
742 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
743 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
745 // we have not done anything wrong yet.
747 dispatch_buffer.erase();
749 // redraw the screen at the end (first of the two drawing steps).
750 //This is done unless explicitely requested otherwise
751 Update::flags updateFlags = Update::FitCursor;
753 FuncStatus const flag = getStatus(cmd);
754 if (!flag.enabled()) {
755 // We cannot use this function here
756 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
757 << lyxaction.getActionName(action)
758 << " [" << action << "] is disabled at this location");
759 setErrorMessage(flag.message());
763 case LFUN_WORD_FIND_FORWARD:
764 case LFUN_WORD_FIND_BACKWARD: {
765 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
766 static docstring last_search;
767 docstring searched_string;
769 if (!cmd.argument().empty()) {
770 last_search = cmd.argument();
771 searched_string = cmd.argument();
773 searched_string = last_search;
776 if (searched_string.empty())
779 bool const fw = action == LFUN_WORD_FIND_FORWARD;
780 docstring const data =
781 find2string(searched_string, true, false, fw);
782 find(view(), FuncRequest(LFUN_WORD_FIND, data));
786 case LFUN_COMMAND_PREFIX:
787 LASSERT(lyx_view_, /**/);
788 lyx_view_->message(keyseq.printOptions(true));
792 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
794 meta_fake_bit = NoModifier;
795 if (lyx_view_->buffer())
796 // cancel any selection
797 dispatch(FuncRequest(LFUN_MARK_OFF));
798 setMessage(from_ascii(N_("Cancel")));
801 case LFUN_META_PREFIX:
802 meta_fake_bit = AltModifier;
803 setMessage(keyseq.print(KeySequence::ForGui));
806 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
807 LASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer(), /**/);
808 Buffer * buf = lyx_view_->buffer();
809 if (buf->lyxvc().inUse())
810 buf->lyxvc().toggleReadOnly();
812 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
816 // --- Menus -----------------------------------------------
817 case LFUN_BUFFER_CLOSE:
818 lyx_view_->closeBuffer();
819 updateFlags = Update::None;
822 case LFUN_BUFFER_RELOAD: {
823 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
824 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
825 docstring text = bformat(_("Any changes will be lost. Are you sure "
826 "you want to revert to the saved version of the document %1$s?"), file);
827 int const ret = Alert::prompt(_("Revert to saved document?"),
828 text, 1, 1, _("&Revert"), _("&Cancel"));
835 case LFUN_BUFFER_UPDATE:
836 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
837 lyx_view_->buffer()->doExport(argument, true);
840 case LFUN_BUFFER_VIEW:
841 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
842 lyx_view_->buffer()->preview(argument);
845 case LFUN_MASTER_BUFFER_UPDATE:
846 LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
847 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
850 case LFUN_MASTER_BUFFER_VIEW:
851 LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
852 lyx_view_->buffer()->masterBuffer()->preview(argument);
855 case LFUN_BUILD_PROGRAM:
856 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
857 lyx_view_->buffer()->doExport("program", true);
860 case LFUN_BUFFER_CHKTEX:
861 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
862 lyx_view_->buffer()->runChktex();
865 case LFUN_BUFFER_EXPORT:
866 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
867 if (argument == "custom")
868 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
870 lyx_view_->buffer()->doExport(argument, false);
873 case LFUN_BUFFER_EXPORT_CUSTOM: {
874 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
876 string command = split(argument, format_name, ' ');
877 Format const * format = formats.getFormat(format_name);
879 lyxerr << "Format \"" << format_name
880 << "\" not recognized!"
885 Buffer * buffer = lyx_view_->buffer();
887 // The name of the file created by the conversion process
890 // Output to filename
891 if (format->name() == "lyx") {
892 string const latexname = buffer->latexName(false);
893 filename = changeExtension(latexname,
894 format->extension());
895 filename = addName(buffer->temppath(), filename);
897 if (!buffer->writeFile(FileName(filename)))
901 buffer->doExport(format_name, true, filename);
904 // Substitute $$FName for filename
905 if (!contains(command, "$$FName"))
906 command = "( " + command + " ) < $$FName";
907 command = subst(command, "$$FName", filename);
909 // Execute the command in the background
911 call.startscript(Systemcall::DontWait, command);
915 case LFUN_BUFFER_PRINT: {
916 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
917 // FIXME: cmd.getArg() might fail if one of the arguments
918 // contains double quotes
919 string target = cmd.getArg(0);
920 string target_name = cmd.getArg(1);
921 string command = cmd.getArg(2);
924 || target_name.empty()
925 || command.empty()) {
926 lyxerr << "Unable to parse \""
927 << argument << '"' << endl;
930 if (target != "printer" && target != "file") {
931 lyxerr << "Unrecognized target \""
932 << target << '"' << endl;
936 Buffer * buffer = lyx_view_->buffer();
938 if (!buffer->doExport("dvi", true)) {
939 showPrintError(buffer->absFileName());
943 // Push directory path.
944 string const path = buffer->temppath();
945 // Prevent the compiler from optimizing away p
949 // there are three cases here:
950 // 1. we print to a file
951 // 2. we print directly to a printer
952 // 3. we print using a spool command (print to file first)
955 string const dviname =
956 changeExtension(buffer->latexName(true), "dvi");
958 if (target == "printer") {
959 if (!lyxrc.print_spool_command.empty()) {
960 // case 3: print using a spool
961 string const psname =
962 changeExtension(dviname,".ps");
963 command += ' ' + lyxrc.print_to_file
966 + quoteName(dviname);
969 lyxrc.print_spool_command + ' ';
970 if (target_name != "default") {
971 command2 += lyxrc.print_spool_printerprefix
975 command2 += quoteName(psname);
977 // If successful, then spool command
978 res = one.startscript(
983 res = one.startscript(
984 Systemcall::DontWait,
987 // case 2: print directly to a printer
988 if (target_name != "default")
989 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
990 res = one.startscript(
991 Systemcall::DontWait,
992 command + quoteName(dviname));
996 // case 1: print to a file
997 FileName const filename(makeAbsPath(target_name,
998 lyx_view_->buffer()->filePath()));
999 FileName const dvifile(makeAbsPath(dviname, path));
1000 if (filename.exists()) {
1001 docstring text = bformat(
1002 _("The file %1$s already exists.\n\n"
1003 "Do you want to overwrite that file?"),
1004 makeDisplayPath(filename.absFilename()));
1005 if (Alert::prompt(_("Overwrite file?"),
1006 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1009 command += ' ' + lyxrc.print_to_file
1010 + quoteName(filename.toFilesystemEncoding())
1012 + quoteName(dvifile.toFilesystemEncoding());
1013 res = one.startscript(Systemcall::DontWait,
1018 showPrintError(buffer->absFileName());
1022 // FIXME: There is need for a command-line import.
1024 case LFUN_BUFFER_IMPORT:
1029 case LFUN_BUFFER_AUTO_SAVE:
1030 lyx_view_->buffer()->autoSave();
1033 case LFUN_RECONFIGURE:
1034 // argument is any additional parameter to the configure.py command
1035 reconfigure(lyx_view_, argument);
1038 case LFUN_HELP_OPEN: {
1039 LASSERT(lyx_view_, /**/);
1040 string const arg = argument;
1042 setErrorMessage(from_ascii(N_("Missing argument")));
1045 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1046 if (fname.empty()) {
1047 lyxerr << "LyX: unable to find documentation file `"
1048 << arg << "'. Bad installation?" << endl;
1051 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1052 makeDisplayPath(fname.absFilename())));
1053 Buffer * buf = lyx_view_->loadDocument(fname, false);
1056 lyx_view_->setBuffer(buf);
1057 buf->errors("Parse");
1059 updateFlags = Update::None;
1063 // --- version control -------------------------------
1064 case LFUN_VC_REGISTER:
1065 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1066 if (!ensureBufferClean(view()))
1068 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1069 lyx_view_->buffer()->lyxvc().registrer();
1072 updateFlags = Update::Force;
1075 case LFUN_VC_CHECK_IN:
1076 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1077 if (!ensureBufferClean(view()))
1079 if (lyx_view_->buffer()->lyxvc().inUse()
1080 && !lyx_view_->buffer()->isReadonly()) {
1081 lyx_view_->buffer()->lyxvc().checkIn();
1086 case LFUN_VC_CHECK_OUT:
1087 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1088 if (!ensureBufferClean(view()))
1090 if (lyx_view_->buffer()->lyxvc().inUse()
1091 && lyx_view_->buffer()->isReadonly()) {
1092 lyx_view_->buffer()->lyxvc().checkOut();
1097 case LFUN_VC_REVERT:
1098 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1099 lyx_view_->buffer()->lyxvc().revert();
1103 case LFUN_VC_UNDO_LAST:
1104 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1105 lyx_view_->buffer()->lyxvc().undoLast();
1109 // --- lyxserver commands ----------------------------
1110 case LFUN_SERVER_GET_NAME:
1111 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1112 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1113 LYXERR(Debug::INFO, "FNAME["
1114 << lyx_view_->buffer()->absFileName() << ']');
1117 case LFUN_SERVER_NOTIFY:
1118 dispatch_buffer = keyseq.print(KeySequence::Portable);
1119 theServer().notifyClient(to_utf8(dispatch_buffer));
1122 case LFUN_SERVER_GOTO_FILE_ROW: {
1123 LASSERT(lyx_view_, /**/);
1126 istringstream is(argument);
1127 is >> file_name >> row;
1129 bool loaded = false;
1130 if (prefixIs(file_name, package().temp_dir().absFilename()))
1131 // Needed by inverse dvi search. If it is a file
1132 // in tmpdir, call the apropriated function
1133 buf = theBufferList().getBufferFromTmp(file_name);
1135 // Must replace extension of the file to be .lyx
1136 // and get full path
1137 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1138 // Either change buffer or load the file
1139 if (theBufferList().exists(s.absFilename()))
1140 buf = theBufferList().getBuffer(s.absFilename());
1142 buf = lyx_view_->loadDocument(s);
1148 updateFlags = Update::None;
1153 lyx_view_->setBuffer(buf);
1154 view()->setCursorFromRow(row);
1156 buf->errors("Parse");
1157 updateFlags = Update::FitCursor;
1162 case LFUN_DIALOG_SHOW_NEW_INSET: {
1163 LASSERT(lyx_view_, /**/);
1164 string const name = cmd.getArg(0);
1165 InsetCode code = insetCode(name);
1166 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1167 bool insetCodeOK = true;
1176 case HYPERLINK_CODE: {
1177 InsetCommandParams p(code);
1178 data = InsetCommand::params2string(name, p);
1181 case INCLUDE_CODE: {
1182 // data is the include type: one of "include",
1183 // "input", "verbatiminput" or "verbatiminput*"
1185 // default type is requested
1187 InsetCommandParams p(INCLUDE_CODE, data);
1188 data = InsetCommand::params2string("include", p);
1192 // \c data == "Boxed" || "Frameless" etc
1193 InsetBoxParams p(data);
1194 data = InsetBox::params2string(p);
1198 InsetBranchParams p;
1199 data = InsetBranch::params2string(p);
1203 InsetCommandParams p(CITE_CODE);
1204 data = InsetCommand::params2string(name, p);
1208 data = InsetERT::params2string(InsetCollapsable::Open);
1211 case EXTERNAL_CODE: {
1212 InsetExternalParams p;
1213 Buffer const & buffer = *lyx_view_->buffer();
1214 data = InsetExternal::params2string(p, buffer);
1219 data = InsetFloat::params2string(p);
1222 case LISTINGS_CODE: {
1223 InsetListingsParams p;
1224 data = InsetListings::params2string(p);
1227 case GRAPHICS_CODE: {
1228 InsetGraphicsParams p;
1229 Buffer const & buffer = *lyx_view_->buffer();
1230 data = InsetGraphics::params2string(p, buffer);
1235 data = InsetNote::params2string(p);
1240 data = InsetSpace::params2string(p);
1245 data = InsetVSpace::params2string(space);
1250 data = InsetWrap::params2string(p);
1254 lyxerr << "Inset type '" << name <<
1255 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1256 insetCodeOK = false;
1258 } // end switch(code)
1260 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1264 case LFUN_CITATION_INSERT: {
1265 LASSERT(lyx_view_, /**/);
1266 if (!argument.empty()) {
1267 // we can have one optional argument, delimited by '|'
1268 // citation-insert <key>|<text_before>
1269 // this should be enhanced to also support text_after
1270 // and citation style
1271 string arg = argument;
1273 if (contains(argument, "|")) {
1274 arg = token(argument, '|', 0);
1275 opt1 = token(argument, '|', 1);
1277 InsetCommandParams icp(CITE_CODE);
1278 icp["key"] = from_utf8(arg);
1280 icp["before"] = from_utf8(opt1);
1281 string icstr = InsetCommand::params2string("citation", icp);
1282 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1285 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1289 case LFUN_BUFFER_CHILD_OPEN: {
1290 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1291 Buffer * parent = lyx_view_->buffer();
1292 FileName filename = makeAbsPath(argument, parent->filePath());
1293 view()->saveBookmark(false);
1295 bool parsed = false;
1296 if (theBufferList().exists(filename.absFilename())) {
1297 child = theBufferList().getBuffer(filename.absFilename());
1299 setMessage(bformat(_("Opening child document %1$s..."),
1300 makeDisplayPath(filename.absFilename())));
1301 child = lyx_view_->loadDocument(filename, false);
1305 // Set the parent name of the child document.
1306 // This makes insertion of citations and references in the child work,
1307 // when the target is in the parent or another child document.
1308 child->setParent(parent);
1309 updateLabels(*child->masterBuffer());
1310 lyx_view_->setBuffer(child);
1312 child->errors("Parse");
1315 // If a screen update is required (in case where auto_open is false),
1316 // setBuffer() would have taken care of it already. Otherwise we shall
1317 // reset the update flag because it can cause a circular problem.
1319 updateFlags = Update::None;
1323 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1324 LASSERT(lyx_view_, /**/);
1325 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1328 case LFUN_KEYMAP_OFF:
1329 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1330 lyx_view_->view()->getIntl().keyMapOn(false);
1333 case LFUN_KEYMAP_PRIMARY:
1334 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1335 lyx_view_->view()->getIntl().keyMapPrim();
1338 case LFUN_KEYMAP_SECONDARY:
1339 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1340 lyx_view_->view()->getIntl().keyMapSec();
1343 case LFUN_KEYMAP_TOGGLE:
1344 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1345 lyx_view_->view()->getIntl().toggleKeyMap();
1351 string rest = split(argument, countstr, ' ');
1352 istringstream is(countstr);
1355 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1356 for (int i = 0; i < count; ++i)
1357 dispatch(lyxaction.lookupFunc(rest));
1361 case LFUN_COMMAND_SEQUENCE: {
1362 // argument contains ';'-terminated commands
1363 string arg = argument;
1364 while (!arg.empty()) {
1366 arg = split(arg, first, ';');
1367 FuncRequest func(lyxaction.lookupFunc(first));
1368 func.origin = cmd.origin;
1376 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1377 func.origin = cmd.origin;
1379 LyX::ref().topLevelCmdDef().release(argument);
1381 if (func.action == LFUN_UNKNOWN_ACTION) {
1382 // unknown command definition
1383 lyxerr << "Warning: unknown command definition `"
1387 // recursion detected
1388 lyxerr << "Warning: Recursion in the command definition `"
1389 << argument << "' detected"
1396 case LFUN_PREFERENCES_SAVE: {
1397 lyxrc.write(makeAbsPath("preferences",
1398 package().user_support().absFilename()),
1403 case LFUN_SET_COLOR: {
1405 string const x11_name = split(argument, lyx_name, ' ');
1406 if (lyx_name.empty() || x11_name.empty()) {
1407 setErrorMessage(from_ascii(N_(
1408 "Syntax: set-color <lyx_name>"
1413 bool const graphicsbg_changed =
1414 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1415 x11_name != lcolor.getX11Name(Color_graphicsbg));
1417 if (!lcolor.setColor(lyx_name, x11_name)) {
1419 bformat(_("Set-color \"%1$s\" failed "
1420 "- color is undefined or "
1421 "may not be redefined"),
1422 from_utf8(lyx_name)));
1426 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1428 if (graphicsbg_changed) {
1429 // FIXME: The graphics cache no longer has a changeDisplay method.
1431 graphics::GCache::get().changeDisplay(true);
1438 LASSERT(lyx_view_, /**/);
1439 lyx_view_->message(from_utf8(argument));
1443 case LFUN_ALL_INSETS_TOGGLE: {
1444 LASSERT(lyx_view_, /**/);
1446 string const name = split(argument, action, ' ');
1447 InsetCode const inset_code = insetCode(name);
1449 Cursor & cur = view()->cursor();
1450 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1452 Inset & inset = lyx_view_->buffer()->inset();
1453 InsetIterator it = inset_iterator_begin(inset);
1454 InsetIterator const end = inset_iterator_end(inset);
1455 for (; it != end; ++it) {
1456 if (!it->asInsetMath()
1457 && (inset_code == NO_CODE
1458 || inset_code == it->lyxCode())) {
1459 Cursor tmpcur = cur;
1460 tmpcur.pushBackward(*it);
1461 it->dispatch(tmpcur, fr);
1464 updateFlags = Update::Force | Update::FitCursor;
1468 case LFUN_BUFFER_LANGUAGE: {
1469 LASSERT(lyx_view_, /**/);
1470 Buffer & buffer = *lyx_view_->buffer();
1471 Language const * oldL = buffer.params().language;
1472 Language const * newL = languages.getLanguage(argument);
1473 if (!newL || oldL == newL)
1476 if (oldL->rightToLeft() == newL->rightToLeft()
1477 && !buffer.isMultiLingual())
1478 buffer.changeLanguage(oldL, newL);
1482 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1483 string const fname =
1484 addName(addPath(package().user_support().absFilename(), "templates/"),
1486 Buffer defaults(fname);
1488 istringstream ss(argument);
1491 int const unknown_tokens = defaults.readHeader(lex);
1493 if (unknown_tokens != 0) {
1494 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1495 << unknown_tokens << " unknown token"
1496 << (unknown_tokens == 1 ? "" : "s")
1500 if (defaults.writeFile(FileName(defaults.absFileName())))
1501 setMessage(bformat(_("Document defaults saved in %1$s"),
1502 makeDisplayPath(fname)));
1504 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1508 case LFUN_BUFFER_PARAMS_APPLY: {
1509 LASSERT(lyx_view_, /**/);
1511 Buffer * buffer = lyx_view_->buffer();
1512 DocumentClass * oldClass = buffer->params().documentClassPtr();
1513 Cursor & cur = view()->cursor();
1514 cur.recordUndoFullDocument();
1516 istringstream ss(argument);
1519 int const unknown_tokens = buffer->readHeader(lex);
1521 if (unknown_tokens != 0) {
1522 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1523 << unknown_tokens << " unknown token"
1524 << (unknown_tokens == 1 ? "" : "s")
1528 updateLayout(oldClass, buffer);
1530 updateFlags = Update::Force | Update::FitCursor;
1531 // We are most certainly here because of a change in the document
1532 // It is then better to make sure that all dialogs are in sync with
1533 // current document settings. LyXView::restartCursor() achieve this.
1534 lyx_view_->restartCursor();
1538 case LFUN_LAYOUT_MODULES_CLEAR: {
1539 LASSERT(lyx_view_, /**/);
1540 Buffer * buffer = lyx_view_->buffer();
1541 DocumentClass * oldClass = buffer->params().documentClassPtr();
1542 view()->cursor().recordUndoFullDocument();
1543 buffer->params().clearLayoutModules();
1544 buffer->params().makeDocumentClass();
1545 updateLayout(oldClass, buffer);
1546 updateFlags = Update::Force | Update::FitCursor;
1550 case LFUN_LAYOUT_MODULE_ADD: {
1551 LASSERT(lyx_view_, /**/);
1552 Buffer * buffer = lyx_view_->buffer();
1553 DocumentClass * oldClass = buffer->params().documentClassPtr();
1554 view()->cursor().recordUndoFullDocument();
1555 buffer->params().addLayoutModule(argument);
1556 buffer->params().makeDocumentClass();
1557 updateLayout(oldClass, buffer);
1558 updateFlags = Update::Force | Update::FitCursor;
1562 case LFUN_TEXTCLASS_APPLY: {
1563 LASSERT(lyx_view_, /**/);
1564 Buffer * buffer = lyx_view_->buffer();
1566 if (!loadLayoutFile(argument, buffer->temppath()) &&
1567 !loadLayoutFile(argument, buffer->filePath()))
1570 LayoutFile const * old_layout = buffer->params().baseClass();
1571 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1573 if (old_layout == new_layout)
1577 //Save the old, possibly modular, layout for use in conversion.
1578 DocumentClass * oldDocClass = buffer->params().documentClassPtr();
1579 view()->cursor().recordUndoFullDocument();
1580 buffer->params().setBaseClass(argument);
1581 buffer->params().makeDocumentClass();
1582 updateLayout(oldDocClass, buffer);
1583 updateFlags = Update::Force | Update::FitCursor;
1587 case LFUN_LAYOUT_RELOAD: {
1588 LASSERT(lyx_view_, /**/);
1589 Buffer * buffer = lyx_view_->buffer();
1590 DocumentClass * oldClass = buffer->params().documentClassPtr();
1591 LayoutFileIndex bc = buffer->params().baseClassID();
1592 LayoutFileList::get().reset(bc);
1593 buffer->params().setBaseClass(bc);
1594 buffer->params().makeDocumentClass();
1595 updateLayout(oldClass, buffer);
1596 updateFlags = Update::Force | Update::FitCursor;
1600 case LFUN_TEXTCLASS_LOAD:
1601 loadLayoutFile(argument, lyx_view_->buffer()->temppath()) ||
1602 loadLayoutFile(argument, lyx_view_->buffer()->filePath());
1605 case LFUN_LYXRC_APPLY: {
1606 LyXRC const lyxrc_orig = lyxrc;
1608 istringstream ss(argument);
1609 bool const success = lyxrc.read(ss) == 0;
1612 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1613 << "Unable to read lyxrc data"
1618 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1620 theApp()->resetGui();
1622 /// We force the redraw in any case because there might be
1623 /// some screen font changes.
1624 /// FIXME: only the current view will be updated. the Gui
1625 /// class is able to furnish the list of views.
1626 updateFlags = Update::Force;
1630 case LFUN_BOOKMARK_GOTO:
1631 // go to bookmark, open unopened file and switch to buffer if necessary
1632 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1633 updateFlags = Update::FitCursor;
1636 case LFUN_BOOKMARK_CLEAR:
1637 LyX::ref().session().bookmarks().clear();
1641 LASSERT(theApp(), /**/);
1642 // Let the frontend dispatch its own actions.
1643 if (theApp()->dispatch(cmd))
1644 // Nothing more to do.
1647 // Everything below is only for active lyx_view_
1651 // Let the current LyXView dispatch its own actions.
1652 if (lyx_view_->dispatch(cmd)) {
1653 if (lyx_view_->view())
1654 updateFlags = lyx_view_->view()->cursor().result().update();
1658 LASSERT(lyx_view_->view(), /**/);
1659 // Let the current BufferView dispatch its own actions.
1660 if (view()->dispatch(cmd)) {
1661 // The BufferView took care of its own updates if needed.
1662 updateFlags = Update::None;
1666 // Let the current Cursor dispatch its own actions.
1667 Cursor old = view()->cursor();
1668 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1669 cursorPosBeforeDispatchY_);
1670 view()->cursor().dispatch(cmd);
1672 // notify insets we just left
1673 if (view()->cursor() != old) {
1675 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1677 view()->cursor().fixIfBroken();
1680 // update completion. We do it here and not in
1681 // processKeySym to avoid another redraw just for a
1682 // changed inline completion
1683 if (cmd.origin == FuncRequest::KEYBOARD) {
1684 if (cmd.action == LFUN_SELF_INSERT)
1685 lyx_view_->updateCompletion(view()->cursor(), true, true);
1686 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1687 lyx_view_->updateCompletion(view()->cursor(), false, true);
1689 lyx_view_->updateCompletion(view()->cursor(), false, false);
1692 updateFlags = view()->cursor().result().update();
1695 if (lyx_view_ && lyx_view_->buffer()) {
1696 // BufferView::update() updates the ViewMetricsInfo and
1697 // also initializes the position cache for all insets in
1698 // (at least partially) visible top-level paragraphs.
1699 // We will redraw the screen only if needed.
1700 view()->processUpdateFlags(updateFlags);
1702 // if we executed a mutating lfun, mark the buffer as dirty
1704 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1705 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1706 lyx_view_->buffer()->markDirty();
1708 // Do we have a selection?
1709 theSelection().haveSelection(view()->cursor().selection());
1712 lyx_view_->restartCursor();
1716 // Some messages may already be translated, so we cannot use _()
1717 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1722 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1724 const bool verbose = (cmd.origin == FuncRequest::MENU
1725 || cmd.origin == FuncRequest::TOOLBAR
1726 || cmd.origin == FuncRequest::COMMANDBUFFER);
1728 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1729 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1731 lyx_view_->message(msg);
1735 docstring dispatch_msg = msg;
1736 if (!dispatch_msg.empty())
1737 dispatch_msg += ' ';
1739 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1741 bool argsadded = false;
1743 if (!cmd.argument().empty()) {
1744 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1745 comname += ' ' + cmd.argument();
1750 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1752 if (!shortcuts.empty())
1753 comname += ": " + shortcuts;
1754 else if (!argsadded && !cmd.argument().empty())
1755 comname += ' ' + cmd.argument();
1757 if (!comname.empty()) {
1758 comname = rtrim(comname);
1759 dispatch_msg += '(' + rtrim(comname) + ')';
1762 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1763 if (!dispatch_msg.empty())
1764 lyx_view_->message(dispatch_msg);
1768 void LyXFunc::reloadBuffer()
1770 FileName filename = lyx_view_->buffer()->fileName();
1771 // The user has already confirmed that the changes, if any, should
1772 // be discarded. So we just release the Buffer and don't call closeBuffer();
1773 theBufferList().release(lyx_view_->buffer());
1774 Buffer * buf = lyx_view_->loadDocument(filename);
1775 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1779 lyx_view_->setBuffer(buf);
1780 buf->errors("Parse");
1781 str = bformat(_("Document %1$s reloaded."), disp_fn);
1783 str = bformat(_("Could not reload document %1$s"), disp_fn);
1785 lyx_view_->message(str);
1788 // Each "lyx_view_" should have it's own message method. lyxview and
1789 // the minibuffer would use the minibuffer, but lyxserver would
1790 // send an ERROR signal to its client. Alejandro 970603
1791 // This function is bit problematic when it comes to NLS, to make the
1792 // lyx servers client be language indepenent we must not translate
1793 // strings sent to this func.
1794 void LyXFunc::setErrorMessage(docstring const & m) const
1796 dispatch_buffer = m;
1801 void LyXFunc::setMessage(docstring const & m) const
1803 dispatch_buffer = m;
1807 docstring LyXFunc::viewStatusMessage()
1809 // When meta-fake key is pressed, show the key sequence so far + "M-".
1811 return keyseq.print(KeySequence::ForGui) + "M-";
1813 // Else, when a non-complete key sequence is pressed,
1814 // show the available options.
1815 if (keyseq.length() > 0 && !keyseq.deleted())
1816 return keyseq.printOptions(true);
1818 LASSERT(lyx_view_, /**/);
1819 if (!lyx_view_->buffer())
1820 return _("Welcome to LyX!");
1822 return view()->cursor().currentState();
1826 BufferView * LyXFunc::view() const
1828 LASSERT(lyx_view_, /**/);
1829 return lyx_view_->view();
1833 bool LyXFunc::wasMetaKey() const
1835 return (meta_fake_bit != NoModifier);
1839 void LyXFunc::updateLayout(DocumentClass * oldlayout,Buffer * buffer)
1841 lyx_view_->message(_("Converting document to new document class..."));
1843 StableDocIterator backcur(view()->cursor());
1844 ErrorList & el = buffer->errorList("Class Switch");
1845 cap::switchBetweenClasses(
1846 oldlayout, buffer->params().documentClassPtr(),
1847 static_cast<InsetText &>(buffer->inset()), el);
1849 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1851 buffer->errors("Class Switch");
1852 updateLabels(*buffer);
1858 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1860 // Why the switch you might ask. It is a trick to ensure that all
1861 // the elements in the LyXRCTags enum is handled. As you can see
1862 // there are no breaks at all. So it is just a huge fall-through.
1863 // The nice thing is that we will get a warning from the compiler
1864 // if we forget an element.
1865 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1867 case LyXRC::RC_ACCEPT_COMPOUND:
1868 case LyXRC::RC_ALT_LANG:
1869 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1870 case LyXRC::RC_PLAINTEXT_LINELEN:
1871 case LyXRC::RC_AUTOREGIONDELETE:
1872 case LyXRC::RC_AUTORESET_OPTIONS:
1873 case LyXRC::RC_AUTOSAVE:
1874 case LyXRC::RC_AUTO_NUMBER:
1875 case LyXRC::RC_BACKUPDIR_PATH:
1876 case LyXRC::RC_BIBTEX_COMMAND:
1877 case LyXRC::RC_BINDFILE:
1878 case LyXRC::RC_CHECKLASTFILES:
1879 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1880 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1881 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1882 case LyXRC::RC_COMPLETION_INLINE_MATH:
1883 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1884 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1885 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1886 case LyXRC::RC_COMPLETION_POPUP_MATH:
1887 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1888 case LyXRC::RC_USELASTFILEPOS:
1889 case LyXRC::RC_LOADSESSION:
1890 case LyXRC::RC_CHKTEX_COMMAND:
1891 case LyXRC::RC_CONVERTER:
1892 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1893 case LyXRC::RC_COPIER:
1894 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1895 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1896 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1897 case LyXRC::RC_DATE_INSERT_FORMAT:
1898 case LyXRC::RC_DEFAULT_LANGUAGE:
1899 case LyXRC::RC_DEFAULT_PAPERSIZE:
1900 case LyXRC::RC_DEFFILE:
1901 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1902 case LyXRC::RC_DISPLAY_GRAPHICS:
1903 case LyXRC::RC_DOCUMENTPATH:
1904 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1905 FileName path(lyxrc_new.document_path);
1906 if (path.exists() && path.isDirectory())
1907 package().document_dir() = FileName(lyxrc.document_path);
1909 case LyXRC::RC_ESC_CHARS:
1910 case LyXRC::RC_EXAMPLEPATH:
1911 case LyXRC::RC_FONT_ENCODING:
1912 case LyXRC::RC_FORMAT:
1913 case LyXRC::RC_GROUP_LAYOUTS:
1914 case LyXRC::RC_INDEX_COMMAND:
1915 case LyXRC::RC_INPUT:
1916 case LyXRC::RC_KBMAP:
1917 case LyXRC::RC_KBMAP_PRIMARY:
1918 case LyXRC::RC_KBMAP_SECONDARY:
1919 case LyXRC::RC_LABEL_INIT_LENGTH:
1920 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1921 case LyXRC::RC_LANGUAGE_AUTO_END:
1922 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1923 case LyXRC::RC_LANGUAGE_COMMAND_END:
1924 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1925 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1926 case LyXRC::RC_LANGUAGE_PACKAGE:
1927 case LyXRC::RC_LANGUAGE_USE_BABEL:
1928 case LyXRC::RC_MACRO_EDIT_STYLE:
1929 case LyXRC::RC_MAKE_BACKUP:
1930 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1931 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1932 case LyXRC::RC_NUMLASTFILES:
1933 case LyXRC::RC_PATH_PREFIX:
1934 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1935 prependEnvPath("PATH", lyxrc.path_prefix);
1937 case LyXRC::RC_PERS_DICT:
1938 case LyXRC::RC_PREVIEW:
1939 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1940 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1941 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1942 case LyXRC::RC_PRINTCOPIESFLAG:
1943 case LyXRC::RC_PRINTER:
1944 case LyXRC::RC_PRINTEVENPAGEFLAG:
1945 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1946 case LyXRC::RC_PRINTFILEEXTENSION:
1947 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1948 case LyXRC::RC_PRINTODDPAGEFLAG:
1949 case LyXRC::RC_PRINTPAGERANGEFLAG:
1950 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1951 case LyXRC::RC_PRINTPAPERFLAG:
1952 case LyXRC::RC_PRINTREVERSEFLAG:
1953 case LyXRC::RC_PRINTSPOOL_COMMAND:
1954 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1955 case LyXRC::RC_PRINTTOFILE:
1956 case LyXRC::RC_PRINTTOPRINTER:
1957 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1958 case LyXRC::RC_PRINT_COMMAND:
1959 case LyXRC::RC_RTL_SUPPORT:
1960 case LyXRC::RC_SCREEN_DPI:
1961 case LyXRC::RC_SCREEN_FONT_ROMAN:
1962 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1963 case LyXRC::RC_SCREEN_FONT_SANS:
1964 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1965 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1966 case LyXRC::RC_SCREEN_FONT_SIZES:
1967 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1968 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1969 case LyXRC::RC_GEOMETRY_SESSION:
1970 case LyXRC::RC_SCREEN_ZOOM:
1971 case LyXRC::RC_SERVERPIPE:
1972 case LyXRC::RC_SET_COLOR:
1973 case LyXRC::RC_SHOW_BANNER:
1974 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
1975 case LyXRC::RC_SPELL_COMMAND:
1976 case LyXRC::RC_TEMPDIRPATH:
1977 case LyXRC::RC_TEMPLATEPATH:
1978 case LyXRC::RC_TEX_ALLOWS_SPACES:
1979 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1980 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1981 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1983 case LyXRC::RC_UIFILE:
1984 case LyXRC::RC_USER_EMAIL:
1985 case LyXRC::RC_USER_NAME:
1986 case LyXRC::RC_USETEMPDIR:
1987 case LyXRC::RC_USE_ALT_LANG:
1988 case LyXRC::RC_USE_CONVERTER_CACHE:
1989 case LyXRC::RC_USE_ESC_CHARS:
1990 case LyXRC::RC_USE_INP_ENC:
1991 case LyXRC::RC_USE_PERS_DICT:
1992 case LyXRC::RC_USE_TOOLTIP:
1993 case LyXRC::RC_USE_PIXMAP_CACHE:
1994 case LyXRC::RC_USE_SPELL_LIB:
1995 case LyXRC::RC_VIEWDVI_PAPEROPTION:
1996 case LyXRC::RC_SORT_LAYOUTS:
1997 case LyXRC::RC_FULL_SCREEN_LIMIT:
1998 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
1999 case LyXRC::RC_FULL_SCREEN_TABBAR:
2000 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2001 case LyXRC::RC_FULL_SCREEN_WIDTH:
2002 case LyXRC::RC_VISUAL_CURSOR:
2003 case LyXRC::RC_VIEWER:
2004 case LyXRC::RC_LAST: