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 "BranchList.h"
25 #include "buffer_funcs.h"
27 #include "BufferList.h"
28 #include "BufferParams.h"
29 #include "BufferView.h"
32 #include "Converter.h"
34 #include "CutAndPaste.h"
35 #include "DispatchResult.h"
37 #include "ErrorList.h"
39 #include "FuncRequest.h"
40 #include "FuncStatus.h"
41 #include "InsetIterator.h"
46 #include "LyXAction.h"
51 #include "Paragraph.h"
52 #include "ParagraphParameters.h"
53 #include "ParIterator.h"
57 #include "TextClassList.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/InsetTabular.h"
70 #include "insets/InsetVSpace.h"
71 #include "insets/InsetWrap.h"
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"
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"
95 using namespace lyx::support;
99 using frontend::LyXView;
101 namespace Alert = frontend::Alert;
106 // This function runs "configure" and then rereads lyx.defaults to
107 // reconfigure the automatic settings.
108 void reconfigure(LyXView & lv, string const & option)
110 // emit message signal.
111 lv.message(_("Running configure..."));
113 // Run configure in user lyx directory
114 PathChanger p(package().user_support());
115 string configure_command = package().configure_command();
116 configure_command += option;
118 int ret = one.startscript(Systemcall::Wait, configure_command);
120 // emit message signal.
121 lv.message(_("Reloading configuration..."));
122 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
123 // Re-read packages.lst
124 LaTeXFeatures::getAvailable();
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."));
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."));
141 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
143 // Try to fix cursor in case it is broken.
144 cursor.fixIfBroken();
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;
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());
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)) {
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.
175 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
177 size_t const depth = dit.depth() - (outer ? 1 : 0);
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)
188 return Change::UNCHANGED;
195 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
200 void LyXFunc::initKeySequences(KeyMap * kb)
202 keyseq = KeySequence(kb, kb);
203 cancel_meta_seq = KeySequence(kb, kb);
207 void LyXFunc::setLyXView(LyXView * lv)
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());
217 void LyXFunc::handleKeyFunc(kb_action action)
219 char_type c = encoded_last_key;
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
230 // copied verbatim from do_accent_char
231 view()->cursor().resetAnchor();
232 view()->processUpdateFlags(Update::FitCursor);
236 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
238 BOOST_ASSERT(lyx_view_);
239 if (!LyX::ref().session().bookmarks().isValid(idx))
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)) {
247 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
251 // open may fail, so we need to test it again
252 if (!theBufferList().exists(file))
255 // if the current buffer is not that one, switch to it.
256 if (lyx_view_->buffer()->absFileName() != file) {
259 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
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))
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();
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);
282 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
284 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
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();
293 if (keysym.isModifier()) {
294 LYXERR(Debug::KEY, "isModifier true");
295 lyx_view_->restartCursor();
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();
305 // Do a one-deep top-level lookup for
306 // cancel and meta-fake keys. RVDK_PATCH_5
307 cancel_meta_seq.reset();
309 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
310 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
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 << ']');
321 // Dont remove this unless you know what you are doing.
322 meta_fake_bit = NoModifier;
324 // Can this happen now ?
325 if (func.action == LFUN_NOACTION)
326 func = FuncRequest(LFUN_COMMAND_PREFIX);
328 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
329 << keyseq.print(KeySequence::Portable) << ']');
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)
335 if (keyseq.length() > 1)
336 lyx_view_->message(keyseq.print(KeySequence::ForGui));
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);
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
351 if (keysym.isText() && keyseq.length() == 1) {
352 LYXERR(Debug::KEY, "isText() is true, inserting.");
353 func = FuncRequest(LFUN_SELF_INSERT,
354 FuncRequest::KEYBOARD);
356 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
357 lyx_view_->message(_("Unknown function."));
358 lyx_view_->restartCursor();
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) << "']");
374 lyx_view_->restartCursor();
378 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
380 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
383 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
385 if (cmd.action == LFUN_NOACTION) {
386 flag.message(from_utf8(N_("Nothing to do")));
391 switch (cmd.action) {
392 case LFUN_UNKNOWN_ACTION:
393 #ifndef HAVE_LIBAIKSAURUS
394 case LFUN_THESAURUS_ENTRY:
404 if (flag.unknown()) {
405 flag.message(from_utf8(N_("Unknown action")));
409 if (!flag.enabled()) {
410 if (flag.message().empty())
411 flag.message(from_utf8(N_("Command disabled")));
415 // Check whether we need a buffer
416 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
418 flag.message(from_utf8(N_("Command not allowed with"
419 "out any document open")));
424 // I would really like to avoid having this switch and rather try to
425 // encode this in the function itself.
426 // -- And I'd rather let an inset decide which LFUNs it is willing
427 // to handle (Andre')
429 switch (cmd.action) {
431 // FIXME: these cases should be hidden in GuiApplication::getStatus().
432 case LFUN_WINDOW_CLOSE:
434 return theApp()->getStatus(cmd);
438 // FIXME: these cases should be hidden in GuiView::getStatus().
439 case LFUN_DIALOG_TOGGLE:
440 case LFUN_DIALOG_SHOW:
441 case LFUN_DIALOG_UPDATE:
442 if (cmd.argument() == "prefs"
443 || cmd.argument() == "aboutlyx")
446 return lyx_view_->getStatus(cmd);
451 case LFUN_TOOLBAR_TOGGLE:
452 case LFUN_INSET_APPLY:
453 case LFUN_BUFFER_WRITE:
454 case LFUN_BUFFER_WRITE_AS:
455 case LFUN_SPLIT_VIEW:
456 case LFUN_CLOSE_TAB_GROUP:
458 return lyx_view_->getStatus(cmd);
462 case LFUN_BUFFER_TOGGLE_READ_ONLY:
463 flag.setOnOff(buf->isReadonly());
466 case LFUN_BUFFER_SWITCH:
467 // toggle on the current buffer, but do not toggle off
468 // the other ones (is that a good idea?)
469 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
473 case LFUN_BUFFER_EXPORT:
474 enable = cmd.argument() == "custom"
475 || buf->isExportable(to_utf8(cmd.argument()));
478 case LFUN_BUFFER_CHKTEX:
479 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
482 case LFUN_BUILD_PROGRAM:
483 enable = buf->isExportable("program");
486 case LFUN_VC_REGISTER:
487 enable = !buf->lyxvc().inUse();
489 case LFUN_VC_CHECK_IN:
490 enable = buf->lyxvc().inUse() && !buf->isReadonly();
492 case LFUN_VC_CHECK_OUT:
493 enable = buf->lyxvc().inUse() && buf->isReadonly();
496 case LFUN_VC_UNDO_LAST:
497 enable = buf->lyxvc().inUse();
499 case LFUN_BUFFER_RELOAD:
500 enable = !buf->isUnnamed() && buf->fileName().exists()
501 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
504 case LFUN_CITATION_INSERT: {
505 FuncRequest fr(LFUN_INSET_INSERT, "citation");
506 enable = getStatus(fr).enabled();
510 // This could be used for the no-GUI version. The GUI version is handled in
511 // LyXView::getStatus(). See above.
513 case LFUN_BUFFER_WRITE:
514 case LFUN_BUFFER_WRITE_AS: {
515 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
516 enable = b && (b->isUnnamed() || !b->isClean());
521 case LFUN_BUFFER_WRITE_ALL: {
522 // We enable the command only if there are some modified buffers
523 Buffer * first = theBufferList().first();
528 // We cannot use a for loop as the buffer list is a cycle.
534 b = theBufferList().next(b);
535 } while (b != first);
539 case LFUN_BOOKMARK_GOTO: {
540 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
541 enable = LyX::ref().session().bookmarks().isValid(num);
545 case LFUN_BOOKMARK_CLEAR:
546 enable = LyX::ref().session().bookmarks().size() > 0;
549 // this one is difficult to get right. As a half-baked
550 // solution, we consider only the first action of the sequence
551 case LFUN_COMMAND_SEQUENCE: {
552 // argument contains ';'-terminated commands
553 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
554 FuncRequest func(lyxaction.lookupFunc(firstcmd));
555 func.origin = cmd.origin;
556 flag = getStatus(func);
562 string name = to_utf8(cmd.argument());
563 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
564 func.origin = cmd.origin;
565 flag = getStatus(func);
566 LyX::ref().topLevelCmdDef().release(name);
568 // catch recursion or unknown command definiton
569 // all operations until the recursion or unknown command
570 // definiton occures are performed, so set the state to enabled
576 case LFUN_BUFFER_NEW:
577 case LFUN_BUFFER_NEW_TEMPLATE:
578 case LFUN_WORD_FIND_FORWARD:
579 case LFUN_WORD_FIND_BACKWARD:
580 case LFUN_COMMAND_PREFIX:
581 case LFUN_COMMAND_EXECUTE:
583 case LFUN_META_PREFIX:
584 case LFUN_BUFFER_CLOSE:
585 case LFUN_BUFFER_UPDATE:
586 case LFUN_BUFFER_VIEW:
587 case LFUN_MASTER_BUFFER_UPDATE:
588 case LFUN_MASTER_BUFFER_VIEW:
589 case LFUN_BUFFER_IMPORT:
590 case LFUN_BUFFER_AUTO_SAVE:
591 case LFUN_RECONFIGURE:
594 case LFUN_DROP_LAYOUTS_CHOICE:
596 case LFUN_SERVER_GET_NAME:
597 case LFUN_SERVER_NOTIFY:
598 case LFUN_SERVER_GOTO_FILE_ROW:
599 case LFUN_DIALOG_HIDE:
600 case LFUN_DIALOG_DISCONNECT_INSET:
601 case LFUN_BUFFER_CHILD_OPEN:
603 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
604 case LFUN_KEYMAP_OFF:
605 case LFUN_KEYMAP_PRIMARY:
606 case LFUN_KEYMAP_SECONDARY:
607 case LFUN_KEYMAP_TOGGLE:
609 case LFUN_BUFFER_EXPORT_CUSTOM:
610 case LFUN_BUFFER_PRINT:
611 case LFUN_PREFERENCES_SAVE:
612 case LFUN_SCREEN_FONT_UPDATE:
615 case LFUN_EXTERNAL_EDIT:
616 case LFUN_GRAPHICS_EDIT:
617 case LFUN_ALL_INSETS_TOGGLE:
618 case LFUN_BUFFER_LANGUAGE:
619 case LFUN_TEXTCLASS_APPLY:
620 case LFUN_TEXTCLASS_LOAD:
621 case LFUN_BUFFER_SAVE_AS_DEFAULT:
622 case LFUN_BUFFER_PARAMS_APPLY:
623 case LFUN_LAYOUT_MODULES_CLEAR:
624 case LFUN_LAYOUT_MODULE_ADD:
625 case LFUN_LAYOUT_RELOAD:
626 case LFUN_LYXRC_APPLY:
627 case LFUN_BUFFER_NEXT:
628 case LFUN_BUFFER_PREVIOUS:
629 case LFUN_WINDOW_NEW:
631 // these are handled in our dispatch()
639 if (!getLocalStatus(view()->cursor(), cmd, flag))
640 flag = view()->getStatus(cmd);
646 // Can we use a readonly buffer?
647 if (buf && buf->isReadonly()
648 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
649 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
650 flag.message(from_utf8(N_("Document is read-only")));
654 // Are we in a DELETED change-tracking region?
656 && lookupChangeType(view()->cursor(), true) == Change::DELETED
657 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
658 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
659 flag.message(from_utf8(N_("This portion of the document is deleted.")));
663 // the default error message if we disable the command
664 if (!flag.enabled() && flag.message().empty())
665 flag.message(from_utf8(N_("Command disabled")));
671 bool LyXFunc::ensureBufferClean(BufferView * bv)
673 Buffer & buf = bv->buffer();
677 docstring const file = buf.fileName().displayName(30);
678 docstring text = bformat(_("The document %1$s has unsaved "
679 "changes.\n\nDo you want to save "
680 "the document?"), file);
681 int const ret = Alert::prompt(_("Save changed document?"),
682 text, 0, 1, _("&Save"),
686 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
688 return buf.isClean();
694 void showPrintError(string const & name)
696 docstring str = bformat(_("Could not print the document %1$s.\n"
697 "Check that your printer is set up correctly."),
698 makeDisplayPath(name, 50));
699 Alert::error(_("Print document failed"), str);
703 void loadTextClass(string const & name)
705 pair<bool, textclass_type> const tc_pair =
706 textclasslist.numberOfClass(name);
708 if (!tc_pair.first) {
709 lyxerr << "Document class \"" << name
710 << "\" does not exist."
715 textclass_type const tc = tc_pair.second;
717 if (!textclasslist[tc].load()) {
718 docstring s = bformat(_("The document class %1$s."
719 "could not be loaded."),
720 from_utf8(textclasslist[tc].name()));
721 Alert::error(_("Could not load class"), s);
726 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
731 void LyXFunc::dispatch(FuncRequest const & cmd)
733 string const argument = to_utf8(cmd.argument());
734 kb_action const action = cmd.action;
736 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
737 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
739 // we have not done anything wrong yet.
741 dispatch_buffer.erase();
743 // redraw the screen at the end (first of the two drawing steps).
744 //This is done unless explicitely requested otherwise
745 Update::flags updateFlags = Update::FitCursor;
747 FuncStatus const flag = getStatus(cmd);
748 if (!flag.enabled()) {
749 // We cannot use this function here
750 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
751 << lyxaction.getActionName(action)
752 << " [" << action << "] is disabled at this location");
753 setErrorMessage(flag.message());
757 case LFUN_WORD_FIND_FORWARD:
758 case LFUN_WORD_FIND_BACKWARD: {
759 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
760 static docstring last_search;
761 docstring searched_string;
763 if (!cmd.argument().empty()) {
764 last_search = cmd.argument();
765 searched_string = cmd.argument();
767 searched_string = last_search;
770 if (searched_string.empty())
773 bool const fw = action == LFUN_WORD_FIND_FORWARD;
774 docstring const data =
775 find2string(searched_string, true, false, fw);
776 find(view(), FuncRequest(LFUN_WORD_FIND, data));
780 case LFUN_COMMAND_PREFIX:
781 BOOST_ASSERT(lyx_view_);
782 lyx_view_->message(keyseq.printOptions(true));
786 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
788 meta_fake_bit = NoModifier;
789 if (lyx_view_->buffer())
790 // cancel any selection
791 dispatch(FuncRequest(LFUN_MARK_OFF));
792 setMessage(from_ascii(N_("Cancel")));
795 case LFUN_META_PREFIX:
796 meta_fake_bit = AltModifier;
797 setMessage(keyseq.print(KeySequence::ForGui));
800 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
801 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
802 Buffer * buf = lyx_view_->buffer();
803 if (buf->lyxvc().inUse())
804 buf->lyxvc().toggleReadOnly();
806 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
810 // --- Menus -----------------------------------------------
811 case LFUN_BUFFER_NEW:
812 lyx_view_->newDocument(argument, false);
813 updateFlags = Update::None;
816 case LFUN_BUFFER_NEW_TEMPLATE:
817 lyx_view_->newDocument(argument, true);
818 updateFlags = Update::None;
821 case LFUN_BUFFER_CLOSE:
823 updateFlags = Update::None;
826 case LFUN_BUFFER_RELOAD: {
827 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
828 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
829 docstring text = bformat(_("Any changes will be lost. Are you sure "
830 "you want to revert to the saved version of the document %1$s?"), file);
831 int const ret = Alert::prompt(_("Revert to saved document?"),
832 text, 1, 1, _("&Revert"), _("&Cancel"));
839 case LFUN_BUFFER_UPDATE:
840 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
841 lyx_view_->buffer()->doExport(argument, true);
844 case LFUN_BUFFER_VIEW:
845 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
846 lyx_view_->buffer()->preview(argument);
849 case LFUN_MASTER_BUFFER_UPDATE:
850 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
851 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
854 case LFUN_MASTER_BUFFER_VIEW:
855 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
856 lyx_view_->buffer()->masterBuffer()->preview(argument);
859 case LFUN_BUILD_PROGRAM:
860 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
861 lyx_view_->buffer()->doExport("program", true);
864 case LFUN_BUFFER_CHKTEX:
865 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
866 lyx_view_->buffer()->runChktex();
869 case LFUN_BUFFER_EXPORT:
870 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
871 if (argument == "custom")
872 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
874 lyx_view_->buffer()->doExport(argument, false);
877 case LFUN_BUFFER_EXPORT_CUSTOM: {
878 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
880 string command = split(argument, format_name, ' ');
881 Format const * format = formats.getFormat(format_name);
883 lyxerr << "Format \"" << format_name
884 << "\" not recognized!"
889 Buffer * buffer = lyx_view_->buffer();
891 // The name of the file created by the conversion process
894 // Output to filename
895 if (format->name() == "lyx") {
896 string const latexname = buffer->latexName(false);
897 filename = changeExtension(latexname,
898 format->extension());
899 filename = addName(buffer->temppath(), filename);
901 if (!buffer->writeFile(FileName(filename)))
905 buffer->doExport(format_name, true, filename);
908 // Substitute $$FName for filename
909 if (!contains(command, "$$FName"))
910 command = "( " + command + " ) < $$FName";
911 command = subst(command, "$$FName", filename);
913 // Execute the command in the background
915 call.startscript(Systemcall::DontWait, command);
919 case LFUN_BUFFER_PRINT: {
920 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
921 // FIXME: cmd.getArg() might fail if one of the arguments
922 // contains double quotes
923 string target = cmd.getArg(0);
924 string target_name = cmd.getArg(1);
925 string command = cmd.getArg(2);
928 || target_name.empty()
929 || command.empty()) {
930 lyxerr << "Unable to parse \""
931 << argument << '"' << endl;
934 if (target != "printer" && target != "file") {
935 lyxerr << "Unrecognized target \""
936 << target << '"' << endl;
940 Buffer * buffer = lyx_view_->buffer();
942 if (!buffer->doExport("dvi", true)) {
943 showPrintError(buffer->absFileName());
947 // Push directory path.
948 string const path = buffer->temppath();
949 // Prevent the compiler from optimizing away p
953 // there are three cases here:
954 // 1. we print to a file
955 // 2. we print directly to a printer
956 // 3. we print using a spool command (print to file first)
959 string const dviname =
960 changeExtension(buffer->latexName(true), "dvi");
962 if (target == "printer") {
963 if (!lyxrc.print_spool_command.empty()) {
964 // case 3: print using a spool
965 string const psname =
966 changeExtension(dviname,".ps");
967 command += ' ' + lyxrc.print_to_file
970 + quoteName(dviname);
973 lyxrc.print_spool_command + ' ';
974 if (target_name != "default") {
975 command2 += lyxrc.print_spool_printerprefix
979 command2 += quoteName(psname);
981 // If successful, then spool command
982 res = one.startscript(
987 res = one.startscript(
988 Systemcall::DontWait,
991 // case 2: print directly to a printer
992 if (target_name != "default")
993 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
994 res = one.startscript(
995 Systemcall::DontWait,
996 command + quoteName(dviname));
1000 // case 1: print to a file
1001 FileName const filename(makeAbsPath(target_name,
1002 lyx_view_->buffer()->filePath()));
1003 FileName const dvifile(makeAbsPath(dviname, path));
1004 if (filename.exists()) {
1005 docstring text = bformat(
1006 _("The file %1$s already exists.\n\n"
1007 "Do you want to overwrite that file?"),
1008 makeDisplayPath(filename.absFilename()));
1009 if (Alert::prompt(_("Overwrite file?"),
1010 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1013 command += ' ' + lyxrc.print_to_file
1014 + quoteName(filename.toFilesystemEncoding())
1016 + quoteName(dvifile.toFilesystemEncoding());
1017 res = one.startscript(Systemcall::DontWait,
1022 showPrintError(buffer->absFileName());
1026 // FIXME: There is need for a command-line import.
1028 case LFUN_BUFFER_IMPORT:
1033 case LFUN_BUFFER_AUTO_SAVE:
1034 lyx_view_->buffer()->autoSave();
1037 case LFUN_RECONFIGURE:
1038 BOOST_ASSERT(lyx_view_);
1039 // argument is any additional parameter to the configure.py command
1040 reconfigure(*lyx_view_, argument);
1043 case LFUN_HELP_OPEN: {
1044 BOOST_ASSERT(lyx_view_);
1045 string const arg = argument;
1047 setErrorMessage(from_ascii(N_("Missing argument")));
1050 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1051 if (fname.empty()) {
1052 lyxerr << "LyX: unable to find documentation file `"
1053 << arg << "'. Bad installation?" << endl;
1056 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1057 makeDisplayPath(fname.absFilename())));
1058 Buffer * buf = lyx_view_->loadDocument(fname, false);
1061 lyx_view_->setBuffer(buf);
1062 buf->errors("Parse");
1064 updateFlags = Update::None;
1068 // --- version control -------------------------------
1069 case LFUN_VC_REGISTER:
1070 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1071 if (!ensureBufferClean(view()))
1073 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1074 lyx_view_->buffer()->lyxvc().registrer();
1077 updateFlags = Update::Force;
1080 case LFUN_VC_CHECK_IN:
1081 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1082 if (!ensureBufferClean(view()))
1084 if (lyx_view_->buffer()->lyxvc().inUse()
1085 && !lyx_view_->buffer()->isReadonly()) {
1086 lyx_view_->buffer()->lyxvc().checkIn();
1091 case LFUN_VC_CHECK_OUT:
1092 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1093 if (!ensureBufferClean(view()))
1095 if (lyx_view_->buffer()->lyxvc().inUse()
1096 && lyx_view_->buffer()->isReadonly()) {
1097 lyx_view_->buffer()->lyxvc().checkOut();
1102 case LFUN_VC_REVERT:
1103 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1104 lyx_view_->buffer()->lyxvc().revert();
1108 case LFUN_VC_UNDO_LAST:
1109 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1110 lyx_view_->buffer()->lyxvc().undoLast();
1114 // --- lyxserver commands ----------------------------
1115 case LFUN_SERVER_GET_NAME:
1116 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1117 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1118 LYXERR(Debug::INFO, "FNAME["
1119 << lyx_view_->buffer()->absFileName() << ']');
1122 case LFUN_SERVER_NOTIFY:
1123 dispatch_buffer = keyseq.print(KeySequence::Portable);
1124 theServer().notifyClient(to_utf8(dispatch_buffer));
1127 case LFUN_SERVER_GOTO_FILE_ROW: {
1128 BOOST_ASSERT(lyx_view_);
1131 istringstream is(argument);
1132 is >> file_name >> row;
1134 bool loaded = false;
1135 if (prefixIs(file_name, package().temp_dir().absFilename()))
1136 // Needed by inverse dvi search. If it is a file
1137 // in tmpdir, call the apropriated function
1138 buf = theBufferList().getBufferFromTmp(file_name);
1140 // Must replace extension of the file to be .lyx
1141 // and get full path
1142 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1143 // Either change buffer or load the file
1144 if (theBufferList().exists(s.absFilename()))
1145 buf = theBufferList().getBuffer(s.absFilename());
1147 buf = lyx_view_->loadDocument(s);
1153 updateFlags = Update::None;
1158 lyx_view_->setBuffer(buf);
1159 view()->setCursorFromRow(row);
1161 buf->errors("Parse");
1162 updateFlags = Update::FitCursor;
1167 case LFUN_DIALOG_SHOW_NEW_INSET: {
1168 BOOST_ASSERT(lyx_view_);
1169 string const name = cmd.getArg(0);
1170 InsetCode code = insetCode(name);
1171 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1172 bool insetCodeOK = true;
1181 case HYPERLINK_CODE: {
1182 InsetCommandParams p(code);
1183 data = InsetCommandMailer::params2string(name, p);
1186 case INCLUDE_CODE: {
1187 // data is the include type: one of "include",
1188 // "input", "verbatiminput" or "verbatiminput*"
1190 // default type is requested
1192 InsetCommandParams p(INCLUDE_CODE, data);
1193 data = InsetCommandMailer::params2string("include", p);
1197 // \c data == "Boxed" || "Frameless" etc
1198 InsetBoxParams p(data);
1199 data = InsetBoxMailer::params2string(p);
1203 InsetBranchParams p;
1204 data = InsetBranchMailer::params2string(p);
1208 InsetCommandParams p(CITE_CODE);
1209 data = InsetCommandMailer::params2string(name, p);
1213 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1216 case EXTERNAL_CODE: {
1217 InsetExternalParams p;
1218 Buffer const & buffer = *lyx_view_->buffer();
1219 data = InsetExternalMailer::params2string(p, buffer);
1224 data = InsetFloatMailer::params2string(p);
1227 case LISTINGS_CODE: {
1228 InsetListingsParams p;
1229 data = InsetListingsMailer::params2string(p);
1232 case GRAPHICS_CODE: {
1233 InsetGraphicsParams p;
1234 Buffer const & buffer = *lyx_view_->buffer();
1235 data = InsetGraphicsMailer::params2string(p, buffer);
1240 data = InsetNoteMailer::params2string(p);
1245 data = InsetVSpaceMailer::params2string(space);
1250 data = InsetWrapMailer::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 BOOST_ASSERT(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 = InsetCommandMailer::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 BOOST_ASSERT(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_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1324 BOOST_ASSERT(lyx_view_);
1325 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1328 case LFUN_KEYMAP_OFF:
1329 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1330 lyx_view_->view()->getIntl().keyMapOn(false);
1333 case LFUN_KEYMAP_PRIMARY:
1334 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1335 lyx_view_->view()->getIntl().keyMapPrim();
1338 case LFUN_KEYMAP_SECONDARY:
1339 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1340 lyx_view_->view()->getIntl().keyMapSec();
1343 case LFUN_KEYMAP_TOGGLE:
1344 BOOST_ASSERT(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 BOOST_ASSERT(lyx_view_);
1439 lyx_view_->message(from_utf8(argument));
1442 case LFUN_EXTERNAL_EDIT: {
1443 BOOST_ASSERT(lyx_view_);
1444 FuncRequest fr(action, argument);
1445 InsetExternal().dispatch(view()->cursor(), fr);
1449 case LFUN_GRAPHICS_EDIT: {
1450 FuncRequest fr(action, argument);
1451 InsetGraphics().dispatch(view()->cursor(), fr);
1455 case LFUN_ALL_INSETS_TOGGLE: {
1456 BOOST_ASSERT(lyx_view_);
1458 string const name = split(argument, action, ' ');
1459 InsetCode const inset_code = insetCode(name);
1461 Cursor & cur = view()->cursor();
1462 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1464 Inset & inset = lyx_view_->buffer()->inset();
1465 InsetIterator it = inset_iterator_begin(inset);
1466 InsetIterator const end = inset_iterator_end(inset);
1467 for (; it != end; ++it) {
1468 if (!it->asInsetMath()
1469 && (inset_code == NO_CODE
1470 || inset_code == it->lyxCode())) {
1471 Cursor tmpcur = cur;
1472 tmpcur.pushBackward(*it);
1473 it->dispatch(tmpcur, fr);
1476 updateFlags = Update::Force | Update::FitCursor;
1480 case LFUN_BUFFER_LANGUAGE: {
1481 BOOST_ASSERT(lyx_view_);
1482 Buffer & buffer = *lyx_view_->buffer();
1483 Language const * oldL = buffer.params().language;
1484 Language const * newL = languages.getLanguage(argument);
1485 if (!newL || oldL == newL)
1488 if (oldL->rightToLeft() == newL->rightToLeft()
1489 && !buffer.isMultiLingual())
1490 buffer.changeLanguage(oldL, newL);
1494 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1495 string const fname =
1496 addName(addPath(package().user_support().absFilename(), "templates/"),
1498 Buffer defaults(fname);
1500 istringstream ss(argument);
1503 int const unknown_tokens = defaults.readHeader(lex);
1505 if (unknown_tokens != 0) {
1506 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1507 << unknown_tokens << " unknown token"
1508 << (unknown_tokens == 1 ? "" : "s")
1512 if (defaults.writeFile(FileName(defaults.absFileName())))
1513 setMessage(bformat(_("Document defaults saved in %1$s"),
1514 makeDisplayPath(fname)));
1516 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1520 case LFUN_BUFFER_PARAMS_APPLY: {
1521 BOOST_ASSERT(lyx_view_);
1522 biblio::CiteEngine const oldEngine =
1523 lyx_view_->buffer()->params().getEngine();
1525 Buffer * buffer = lyx_view_->buffer();
1527 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1529 Cursor & cur = view()->cursor();
1530 cur.recordUndoFullDocument();
1532 istringstream ss(argument);
1535 int const unknown_tokens = buffer->readHeader(lex);
1537 if (unknown_tokens != 0) {
1538 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1539 << unknown_tokens << " unknown token"
1540 << (unknown_tokens == 1 ? "" : "s")
1544 updateLayout(oldClass, buffer);
1546 biblio::CiteEngine const newEngine =
1547 lyx_view_->buffer()->params().getEngine();
1549 if (oldEngine != newEngine) {
1550 FuncRequest fr(LFUN_INSET_REFRESH);
1552 Inset & inset = lyx_view_->buffer()->inset();
1553 InsetIterator it = inset_iterator_begin(inset);
1554 InsetIterator const end = inset_iterator_end(inset);
1555 for (; it != end; ++it)
1556 if (it->lyxCode() == CITE_CODE)
1557 it->dispatch(cur, fr);
1560 updateFlags = Update::Force | Update::FitCursor;
1561 // We are here most certainaly because of a change in the document
1562 // It is then better to make sure that all dialogs are in sync
1563 // with current document settings. LyXView::restartCursor() achieve this.
1564 lyx_view_->restartCursor();
1568 case LFUN_LAYOUT_MODULES_CLEAR: {
1569 BOOST_ASSERT(lyx_view_);
1570 Buffer * buffer = lyx_view_->buffer();
1571 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1572 view()->cursor().recordUndoFullDocument();
1573 buffer->params().clearLayoutModules();
1574 buffer->params().makeTextClass();
1575 updateLayout(oldClass, buffer);
1576 updateFlags = Update::Force | Update::FitCursor;
1580 case LFUN_LAYOUT_MODULE_ADD: {
1581 BOOST_ASSERT(lyx_view_);
1582 Buffer * buffer = lyx_view_->buffer();
1583 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1584 view()->cursor().recordUndoFullDocument();
1585 buffer->params().addLayoutModule(argument);
1586 buffer->params().makeTextClass();
1587 updateLayout(oldClass, buffer);
1588 updateFlags = Update::Force | Update::FitCursor;
1592 case LFUN_TEXTCLASS_APPLY: {
1593 BOOST_ASSERT(lyx_view_);
1594 Buffer * buffer = lyx_view_->buffer();
1596 loadTextClass(argument);
1598 pair<bool, textclass_type> const tc_pair =
1599 textclasslist.numberOfClass(argument);
1604 textclass_type const old_class = buffer->params().getBaseClass();
1605 textclass_type const new_class = tc_pair.second;
1607 if (old_class == new_class)
1611 //Save the old, possibly modular, layout for use in conversion.
1612 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1613 view()->cursor().recordUndoFullDocument();
1614 buffer->params().setBaseClass(new_class);
1615 buffer->params().makeTextClass();
1616 updateLayout(oldClass, buffer);
1617 updateFlags = Update::Force | Update::FitCursor;
1621 case LFUN_LAYOUT_RELOAD: {
1622 BOOST_ASSERT(lyx_view_);
1623 Buffer * buffer = lyx_view_->buffer();
1624 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1625 textclass_type const tc = buffer->params().getBaseClass();
1626 textclasslist.reset(tc);
1627 buffer->params().setBaseClass(tc);
1628 buffer->params().makeTextClass();
1629 updateLayout(oldClass, buffer);
1630 updateFlags = Update::Force | Update::FitCursor;
1634 case LFUN_TEXTCLASS_LOAD:
1635 loadTextClass(argument);
1638 case LFUN_LYXRC_APPLY: {
1639 LyXRC const lyxrc_orig = lyxrc;
1641 istringstream ss(argument);
1642 bool const success = lyxrc.read(ss) == 0;
1645 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1646 << "Unable to read lyxrc data"
1651 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1653 theApp()->resetGui();
1655 /// We force the redraw in any case because there might be
1656 /// some screen font changes.
1657 /// FIXME: only the current view will be updated. the Gui
1658 /// class is able to furnish the list of views.
1659 updateFlags = Update::Force;
1663 case LFUN_BOOKMARK_GOTO:
1664 // go to bookmark, open unopened file and switch to buffer if necessary
1665 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1666 updateFlags = Update::FitCursor;
1669 case LFUN_BOOKMARK_CLEAR:
1670 LyX::ref().session().bookmarks().clear();
1674 BOOST_ASSERT(theApp());
1675 // Let the frontend dispatch its own actions.
1676 if (theApp()->dispatch(cmd))
1677 // Nothing more to do.
1680 // Let the current LyXView dispatch its own actions.
1681 BOOST_ASSERT(lyx_view_);
1682 if (lyx_view_->dispatch(cmd)) {
1683 if (lyx_view_->view())
1684 updateFlags = lyx_view_->view()->cursor().result().update();
1688 BOOST_ASSERT(lyx_view_->view());
1689 // Let the current BufferView dispatch its own actions.
1690 if (view()->dispatch(cmd)) {
1691 // The BufferView took care of its own updates if needed.
1692 updateFlags = Update::None;
1696 // Let the current Cursor dispatch its own actions.
1697 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1698 cursorPosBeforeDispatchY_);
1699 view()->cursor().dispatch(cmd);
1700 updateFlags = view()->cursor().result().update();
1701 if (!view()->cursor().result().dispatched()) {
1702 // No update needed in this case (e.g. when reaching
1704 updateFlags = Update::None;
1708 if (lyx_view_ && lyx_view_->buffer()) {
1709 // BufferView::update() updates the ViewMetricsInfo and
1710 // also initializes the position cache for all insets in
1711 // (at least partially) visible top-level paragraphs.
1712 // We will redraw the screen only if needed.
1713 view()->processUpdateFlags(updateFlags);
1715 // if we executed a mutating lfun, mark the buffer as dirty
1717 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1718 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1719 lyx_view_->buffer()->markDirty();
1721 //Do we have a selection?
1722 theSelection().haveSelection(view()->cursor().selection());
1725 if (!quitting && lyx_view_) {
1726 // Some messages may already be translated, so we cannot use _()
1727 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1732 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1734 const bool verbose = (cmd.origin == FuncRequest::MENU
1735 || cmd.origin == FuncRequest::TOOLBAR
1736 || cmd.origin == FuncRequest::COMMANDBUFFER);
1738 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1739 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1741 lyx_view_->message(msg);
1745 docstring dispatch_msg = msg;
1746 if (!dispatch_msg.empty())
1747 dispatch_msg += ' ';
1749 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1751 bool argsadded = false;
1753 if (!cmd.argument().empty()) {
1754 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1755 comname += ' ' + cmd.argument();
1760 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1762 if (!shortcuts.empty())
1763 comname += ": " + shortcuts;
1764 else if (!argsadded && !cmd.argument().empty())
1765 comname += ' ' + cmd.argument();
1767 if (!comname.empty()) {
1768 comname = rtrim(comname);
1769 dispatch_msg += '(' + rtrim(comname) + ')';
1772 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1773 if (!dispatch_msg.empty())
1774 lyx_view_->message(dispatch_msg);
1778 void LyXFunc::closeBuffer()
1780 // goto bookmark to update bookmark pit.
1781 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1782 gotoBookmark(i+1, false, false);
1784 lyx_view_->closeBuffer();
1788 void LyXFunc::reloadBuffer()
1790 FileName filename = lyx_view_->buffer()->fileName();
1791 // The user has already confirmed that the changes, if any, should
1792 // be discarded. So we just release the Buffer and don't call closeBuffer();
1793 theBufferList().release(lyx_view_->buffer());
1794 Buffer * buf = lyx_view_->loadDocument(filename);
1795 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1799 lyx_view_->setBuffer(buf);
1800 buf->errors("Parse");
1801 str = bformat(_("Document %1$s reloaded."), disp_fn);
1803 str = bformat(_("Could not reload document %1$s"), disp_fn);
1805 lyx_view_->message(str);
1808 // Each "lyx_view_" should have it's own message method. lyxview and
1809 // the minibuffer would use the minibuffer, but lyxserver would
1810 // send an ERROR signal to its client. Alejandro 970603
1811 // This function is bit problematic when it comes to NLS, to make the
1812 // lyx servers client be language indepenent we must not translate
1813 // strings sent to this func.
1814 void LyXFunc::setErrorMessage(docstring const & m) const
1816 dispatch_buffer = m;
1821 void LyXFunc::setMessage(docstring const & m) const
1823 dispatch_buffer = m;
1827 docstring const LyXFunc::viewStatusMessage()
1829 // When meta-fake key is pressed, show the key sequence so far + "M-".
1831 return keyseq.print(KeySequence::ForGui) + "M-";
1833 // Else, when a non-complete key sequence is pressed,
1834 // show the available options.
1835 if (keyseq.length() > 0 && !keyseq.deleted())
1836 return keyseq.printOptions(true);
1838 BOOST_ASSERT(lyx_view_);
1839 if (!lyx_view_->buffer())
1840 return _("Welcome to LyX!");
1842 return view()->cursor().currentState();
1846 BufferView * LyXFunc::view() const
1848 BOOST_ASSERT(lyx_view_);
1849 return lyx_view_->view();
1853 bool LyXFunc::wasMetaKey() const
1855 return (meta_fake_bit != NoModifier);
1859 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1862 lyx_view_->message(_("Converting document to new document class..."));
1864 StableDocIterator backcur(view()->cursor());
1865 ErrorList & el = buffer->errorList("Class Switch");
1866 cap::switchBetweenClasses(
1867 oldlayout, buffer->params().getTextClassPtr(),
1868 static_cast<InsetText &>(buffer->inset()), el);
1870 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1872 buffer->errors("Class Switch");
1873 updateLabels(*buffer);
1879 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1881 // Why the switch you might ask. It is a trick to ensure that all
1882 // the elements in the LyXRCTags enum is handled. As you can see
1883 // there are no breaks at all. So it is just a huge fall-through.
1884 // The nice thing is that we will get a warning from the compiler
1885 // if we forget an element.
1886 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1888 case LyXRC::RC_ACCEPT_COMPOUND:
1889 case LyXRC::RC_ALT_LANG:
1890 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1891 case LyXRC::RC_PLAINTEXT_LINELEN:
1892 case LyXRC::RC_AUTOREGIONDELETE:
1893 case LyXRC::RC_AUTORESET_OPTIONS:
1894 case LyXRC::RC_AUTOSAVE:
1895 case LyXRC::RC_AUTO_NUMBER:
1896 case LyXRC::RC_BACKUPDIR_PATH:
1897 case LyXRC::RC_BIBTEX_COMMAND:
1898 case LyXRC::RC_BINDFILE:
1899 case LyXRC::RC_CHECKLASTFILES:
1900 case LyXRC::RC_USELASTFILEPOS:
1901 case LyXRC::RC_LOADSESSION:
1902 case LyXRC::RC_CHKTEX_COMMAND:
1903 case LyXRC::RC_CONVERTER:
1904 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1905 case LyXRC::RC_COPIER:
1906 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1907 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1908 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1909 case LyXRC::RC_DATE_INSERT_FORMAT:
1910 case LyXRC::RC_DEFAULT_LANGUAGE:
1911 case LyXRC::RC_DEFAULT_PAPERSIZE:
1912 case LyXRC::RC_DEFFILE:
1913 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1914 case LyXRC::RC_DISPLAY_GRAPHICS:
1915 case LyXRC::RC_DOCUMENTPATH:
1916 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1917 FileName path(lyxrc_new.document_path);
1918 if (path.exists() && path.isDirectory())
1919 package().document_dir() = FileName(lyxrc.document_path);
1921 case LyXRC::RC_ESC_CHARS:
1922 case LyXRC::RC_EXAMPLEPATH:
1923 case LyXRC::RC_FONT_ENCODING:
1924 case LyXRC::RC_FORMAT:
1925 case LyXRC::RC_INDEX_COMMAND:
1926 case LyXRC::RC_INPUT:
1927 case LyXRC::RC_KBMAP:
1928 case LyXRC::RC_KBMAP_PRIMARY:
1929 case LyXRC::RC_KBMAP_SECONDARY:
1930 case LyXRC::RC_LABEL_INIT_LENGTH:
1931 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1932 case LyXRC::RC_LANGUAGE_AUTO_END:
1933 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1934 case LyXRC::RC_LANGUAGE_COMMAND_END:
1935 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1936 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1937 case LyXRC::RC_LANGUAGE_PACKAGE:
1938 case LyXRC::RC_LANGUAGE_USE_BABEL:
1939 case LyXRC::RC_MACRO_EDIT_STYLE:
1940 case LyXRC::RC_MAKE_BACKUP:
1941 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1942 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1943 case LyXRC::RC_NUMLASTFILES:
1944 case LyXRC::RC_PATH_PREFIX:
1945 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1946 prependEnvPath("PATH", lyxrc.path_prefix);
1948 case LyXRC::RC_PERS_DICT:
1949 case LyXRC::RC_PREVIEW:
1950 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1951 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1952 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1953 case LyXRC::RC_PRINTCOPIESFLAG:
1954 case LyXRC::RC_PRINTER:
1955 case LyXRC::RC_PRINTEVENPAGEFLAG:
1956 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1957 case LyXRC::RC_PRINTFILEEXTENSION:
1958 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1959 case LyXRC::RC_PRINTODDPAGEFLAG:
1960 case LyXRC::RC_PRINTPAGERANGEFLAG:
1961 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1962 case LyXRC::RC_PRINTPAPERFLAG:
1963 case LyXRC::RC_PRINTREVERSEFLAG:
1964 case LyXRC::RC_PRINTSPOOL_COMMAND:
1965 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1966 case LyXRC::RC_PRINTTOFILE:
1967 case LyXRC::RC_PRINTTOPRINTER:
1968 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1969 case LyXRC::RC_PRINT_COMMAND:
1970 case LyXRC::RC_RTL_SUPPORT:
1971 case LyXRC::RC_SCREEN_DPI:
1972 case LyXRC::RC_SCREEN_FONT_ROMAN:
1973 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1974 case LyXRC::RC_SCREEN_FONT_SANS:
1975 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1976 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1977 case LyXRC::RC_SCREEN_FONT_SIZES:
1978 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1979 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1980 case LyXRC::RC_GEOMETRY_SESSION:
1981 case LyXRC::RC_SCREEN_ZOOM:
1982 case LyXRC::RC_SERVERPIPE:
1983 case LyXRC::RC_SET_COLOR:
1984 case LyXRC::RC_SHOW_BANNER:
1985 case LyXRC::RC_SPELL_COMMAND:
1986 case LyXRC::RC_TEMPDIRPATH:
1987 case LyXRC::RC_TEMPLATEPATH:
1988 case LyXRC::RC_TEX_ALLOWS_SPACES:
1989 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1990 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1991 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1993 case LyXRC::RC_UIFILE:
1994 case LyXRC::RC_USER_EMAIL:
1995 case LyXRC::RC_USER_NAME:
1996 case LyXRC::RC_USETEMPDIR:
1997 case LyXRC::RC_USE_ALT_LANG:
1998 case LyXRC::RC_USE_CONVERTER_CACHE:
1999 case LyXRC::RC_USE_ESC_CHARS:
2000 case LyXRC::RC_USE_INP_ENC:
2001 case LyXRC::RC_USE_PERS_DICT:
2002 case LyXRC::RC_USE_TOOLTIP:
2003 case LyXRC::RC_USE_PIXMAP_CACHE:
2004 case LyXRC::RC_USE_SPELL_LIB:
2005 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2006 case LyXRC::RC_SORT_LAYOUTS:
2007 case LyXRC::RC_FULL_SCREEN_LIMIT:
2008 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2009 case LyXRC::RC_FULL_SCREEN_TABBAR:
2010 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2011 case LyXRC::RC_FULL_SCREEN_WIDTH:
2012 case LyXRC::RC_VISUAL_CURSOR:
2013 case LyXRC::RC_VIEWER:
2014 case LyXRC::RC_LAST: