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:
457 return lyx_view_->getStatus(cmd);
461 case LFUN_BUFFER_TOGGLE_READ_ONLY:
462 flag.setOnOff(buf->isReadonly());
465 case LFUN_BUFFER_SWITCH:
466 // toggle on the current buffer, but do not toggle off
467 // the other ones (is that a good idea?)
468 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
472 case LFUN_BUFFER_EXPORT:
473 enable = cmd.argument() == "custom"
474 || buf->isExportable(to_utf8(cmd.argument()));
477 case LFUN_BUFFER_CHKTEX:
478 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
481 case LFUN_BUILD_PROGRAM:
482 enable = buf->isExportable("program");
485 case LFUN_VC_REGISTER:
486 enable = !buf->lyxvc().inUse();
488 case LFUN_VC_CHECK_IN:
489 enable = buf->lyxvc().inUse() && !buf->isReadonly();
491 case LFUN_VC_CHECK_OUT:
492 enable = buf->lyxvc().inUse() && buf->isReadonly();
495 case LFUN_VC_UNDO_LAST:
496 enable = buf->lyxvc().inUse();
498 case LFUN_BUFFER_RELOAD:
499 enable = !buf->isUnnamed() && buf->fileName().exists()
500 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
503 case LFUN_CITATION_INSERT: {
504 FuncRequest fr(LFUN_INSET_INSERT, "citation");
505 enable = getStatus(fr).enabled();
509 // This could be used for the no-GUI version. The GUI version is handled in
510 // LyXView::getStatus(). See above.
512 case LFUN_BUFFER_WRITE:
513 case LFUN_BUFFER_WRITE_AS: {
514 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
515 enable = b && (b->isUnnamed() || !b->isClean());
520 case LFUN_BUFFER_WRITE_ALL: {
521 // We enable the command only if there are some modified buffers
522 Buffer * first = theBufferList().first();
527 // We cannot use a for loop as the buffer list is a cycle.
533 b = theBufferList().next(b);
534 } while (b != first);
538 case LFUN_BOOKMARK_GOTO: {
539 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
540 enable = LyX::ref().session().bookmarks().isValid(num);
544 case LFUN_BOOKMARK_CLEAR:
545 enable = LyX::ref().session().bookmarks().size() > 0;
548 // this one is difficult to get right. As a half-baked
549 // solution, we consider only the first action of the sequence
550 case LFUN_COMMAND_SEQUENCE: {
551 // argument contains ';'-terminated commands
552 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
553 FuncRequest func(lyxaction.lookupFunc(firstcmd));
554 func.origin = cmd.origin;
555 flag = getStatus(func);
561 string name = to_utf8(cmd.argument());
562 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
563 func.origin = cmd.origin;
564 flag = getStatus(func);
565 LyX::ref().topLevelCmdDef().release(name);
567 // catch recursion or unknown command definiton
568 // all operations until the recursion or unknown command
569 // definiton occures are performed, so set the state to enabled
575 case LFUN_BUFFER_NEW:
576 case LFUN_BUFFER_NEW_TEMPLATE:
577 case LFUN_WORD_FIND_FORWARD:
578 case LFUN_WORD_FIND_BACKWARD:
579 case LFUN_COMMAND_PREFIX:
580 case LFUN_COMMAND_EXECUTE:
582 case LFUN_META_PREFIX:
583 case LFUN_BUFFER_CLOSE:
584 case LFUN_BUFFER_UPDATE:
585 case LFUN_BUFFER_VIEW:
586 case LFUN_MASTER_BUFFER_UPDATE:
587 case LFUN_MASTER_BUFFER_VIEW:
588 case LFUN_BUFFER_IMPORT:
589 case LFUN_BUFFER_AUTO_SAVE:
590 case LFUN_RECONFIGURE:
593 case LFUN_DROP_LAYOUTS_CHOICE:
595 case LFUN_SERVER_GET_NAME:
596 case LFUN_SERVER_NOTIFY:
597 case LFUN_SERVER_GOTO_FILE_ROW:
598 case LFUN_DIALOG_HIDE:
599 case LFUN_DIALOG_DISCONNECT_INSET:
600 case LFUN_BUFFER_CHILD_OPEN:
602 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
603 case LFUN_KEYMAP_OFF:
604 case LFUN_KEYMAP_PRIMARY:
605 case LFUN_KEYMAP_SECONDARY:
606 case LFUN_KEYMAP_TOGGLE:
608 case LFUN_BUFFER_EXPORT_CUSTOM:
609 case LFUN_BUFFER_PRINT:
610 case LFUN_PREFERENCES_SAVE:
611 case LFUN_SCREEN_FONT_UPDATE:
614 case LFUN_EXTERNAL_EDIT:
615 case LFUN_GRAPHICS_EDIT:
616 case LFUN_ALL_INSETS_TOGGLE:
617 case LFUN_BUFFER_LANGUAGE:
618 case LFUN_TEXTCLASS_APPLY:
619 case LFUN_TEXTCLASS_LOAD:
620 case LFUN_BUFFER_SAVE_AS_DEFAULT:
621 case LFUN_BUFFER_PARAMS_APPLY:
622 case LFUN_LAYOUT_MODULES_CLEAR:
623 case LFUN_LAYOUT_MODULE_ADD:
624 case LFUN_LAYOUT_RELOAD:
625 case LFUN_LYXRC_APPLY:
626 case LFUN_BUFFER_NEXT:
627 case LFUN_BUFFER_PREVIOUS:
628 case LFUN_WINDOW_NEW:
630 // these are handled in our dispatch()
638 if (!getLocalStatus(view()->cursor(), cmd, flag))
639 flag = view()->getStatus(cmd);
645 // Can we use a readonly buffer?
646 if (buf && buf->isReadonly()
647 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
648 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
649 flag.message(from_utf8(N_("Document is read-only")));
653 // Are we in a DELETED change-tracking region?
655 && lookupChangeType(view()->cursor(), true) == Change::DELETED
656 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
657 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
658 flag.message(from_utf8(N_("This portion of the document is deleted.")));
662 // the default error message if we disable the command
663 if (!flag.enabled() && flag.message().empty())
664 flag.message(from_utf8(N_("Command disabled")));
670 bool LyXFunc::ensureBufferClean(BufferView * bv)
672 Buffer & buf = bv->buffer();
676 docstring const file = buf.fileName().displayName(30);
677 docstring text = bformat(_("The document %1$s has unsaved "
678 "changes.\n\nDo you want to save "
679 "the document?"), file);
680 int const ret = Alert::prompt(_("Save changed document?"),
681 text, 0, 1, _("&Save"),
685 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
687 return buf.isClean();
693 void showPrintError(string const & name)
695 docstring str = bformat(_("Could not print the document %1$s.\n"
696 "Check that your printer is set up correctly."),
697 makeDisplayPath(name, 50));
698 Alert::error(_("Print document failed"), str);
702 void loadTextClass(string const & name)
704 pair<bool, textclass_type> const tc_pair =
705 textclasslist.numberOfClass(name);
707 if (!tc_pair.first) {
708 lyxerr << "Document class \"" << name
709 << "\" does not exist."
714 textclass_type const tc = tc_pair.second;
716 if (!textclasslist[tc].load()) {
717 docstring s = bformat(_("The document class %1$s."
718 "could not be loaded."),
719 from_utf8(textclasslist[tc].name()));
720 Alert::error(_("Could not load class"), s);
725 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
730 void LyXFunc::dispatch(FuncRequest const & cmd)
732 string const argument = to_utf8(cmd.argument());
733 kb_action const action = cmd.action;
735 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
736 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
738 // we have not done anything wrong yet.
740 dispatch_buffer.erase();
742 // redraw the screen at the end (first of the two drawing steps).
743 //This is done unless explicitely requested otherwise
744 Update::flags updateFlags = Update::FitCursor;
746 FuncStatus const flag = getStatus(cmd);
747 if (!flag.enabled()) {
748 // We cannot use this function here
749 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
750 << lyxaction.getActionName(action)
751 << " [" << action << "] is disabled at this location");
752 setErrorMessage(flag.message());
756 case LFUN_WORD_FIND_FORWARD:
757 case LFUN_WORD_FIND_BACKWARD: {
758 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
759 static docstring last_search;
760 docstring searched_string;
762 if (!cmd.argument().empty()) {
763 last_search = cmd.argument();
764 searched_string = cmd.argument();
766 searched_string = last_search;
769 if (searched_string.empty())
772 bool const fw = action == LFUN_WORD_FIND_FORWARD;
773 docstring const data =
774 find2string(searched_string, true, false, fw);
775 find(view(), FuncRequest(LFUN_WORD_FIND, data));
779 case LFUN_COMMAND_PREFIX:
780 BOOST_ASSERT(lyx_view_);
781 lyx_view_->message(keyseq.printOptions(true));
785 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
787 meta_fake_bit = NoModifier;
788 if (lyx_view_->buffer())
789 // cancel any selection
790 dispatch(FuncRequest(LFUN_MARK_OFF));
791 setMessage(from_ascii(N_("Cancel")));
794 case LFUN_META_PREFIX:
795 meta_fake_bit = AltModifier;
796 setMessage(keyseq.print(KeySequence::ForGui));
799 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
800 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
801 Buffer * buf = lyx_view_->buffer();
802 if (buf->lyxvc().inUse())
803 buf->lyxvc().toggleReadOnly();
805 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
809 // --- Menus -----------------------------------------------
810 case LFUN_BUFFER_NEW:
811 lyx_view_->newDocument(argument, false);
812 updateFlags = Update::None;
815 case LFUN_BUFFER_NEW_TEMPLATE:
816 lyx_view_->newDocument(argument, true);
817 updateFlags = Update::None;
820 case LFUN_BUFFER_CLOSE:
822 updateFlags = Update::None;
825 case LFUN_BUFFER_RELOAD: {
826 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
827 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
828 docstring text = bformat(_("Any changes will be lost. Are you sure "
829 "you want to revert to the saved version of the document %1$s?"), file);
830 int const ret = Alert::prompt(_("Revert to saved document?"),
831 text, 1, 1, _("&Revert"), _("&Cancel"));
838 case LFUN_BUFFER_UPDATE:
839 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
840 lyx_view_->buffer()->doExport(argument, true);
843 case LFUN_BUFFER_VIEW:
844 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
845 lyx_view_->buffer()->preview(argument);
848 case LFUN_MASTER_BUFFER_UPDATE:
849 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
850 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
853 case LFUN_MASTER_BUFFER_VIEW:
854 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
855 lyx_view_->buffer()->masterBuffer()->preview(argument);
858 case LFUN_BUILD_PROGRAM:
859 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
860 lyx_view_->buffer()->doExport("program", true);
863 case LFUN_BUFFER_CHKTEX:
864 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
865 lyx_view_->buffer()->runChktex();
868 case LFUN_BUFFER_EXPORT:
869 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
870 if (argument == "custom")
871 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
873 lyx_view_->buffer()->doExport(argument, false);
876 case LFUN_BUFFER_EXPORT_CUSTOM: {
877 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
879 string command = split(argument, format_name, ' ');
880 Format const * format = formats.getFormat(format_name);
882 lyxerr << "Format \"" << format_name
883 << "\" not recognized!"
888 Buffer * buffer = lyx_view_->buffer();
890 // The name of the file created by the conversion process
893 // Output to filename
894 if (format->name() == "lyx") {
895 string const latexname = buffer->latexName(false);
896 filename = changeExtension(latexname,
897 format->extension());
898 filename = addName(buffer->temppath(), filename);
900 if (!buffer->writeFile(FileName(filename)))
904 buffer->doExport(format_name, true, filename);
907 // Substitute $$FName for filename
908 if (!contains(command, "$$FName"))
909 command = "( " + command + " ) < $$FName";
910 command = subst(command, "$$FName", filename);
912 // Execute the command in the background
914 call.startscript(Systemcall::DontWait, command);
918 case LFUN_BUFFER_PRINT: {
919 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
920 // FIXME: cmd.getArg() might fail if one of the arguments
921 // contains double quotes
922 string target = cmd.getArg(0);
923 string target_name = cmd.getArg(1);
924 string command = cmd.getArg(2);
927 || target_name.empty()
928 || command.empty()) {
929 lyxerr << "Unable to parse \""
930 << argument << '"' << endl;
933 if (target != "printer" && target != "file") {
934 lyxerr << "Unrecognized target \""
935 << target << '"' << endl;
939 Buffer * buffer = lyx_view_->buffer();
941 if (!buffer->doExport("dvi", true)) {
942 showPrintError(buffer->absFileName());
946 // Push directory path.
947 string const path = buffer->temppath();
948 // Prevent the compiler from optimizing away p
952 // there are three cases here:
953 // 1. we print to a file
954 // 2. we print directly to a printer
955 // 3. we print using a spool command (print to file first)
958 string const dviname =
959 changeExtension(buffer->latexName(true), "dvi");
961 if (target == "printer") {
962 if (!lyxrc.print_spool_command.empty()) {
963 // case 3: print using a spool
964 string const psname =
965 changeExtension(dviname,".ps");
966 command += ' ' + lyxrc.print_to_file
969 + quoteName(dviname);
972 lyxrc.print_spool_command + ' ';
973 if (target_name != "default") {
974 command2 += lyxrc.print_spool_printerprefix
978 command2 += quoteName(psname);
980 // If successful, then spool command
981 res = one.startscript(
986 res = one.startscript(
987 Systemcall::DontWait,
990 // case 2: print directly to a printer
991 if (target_name != "default")
992 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
993 res = one.startscript(
994 Systemcall::DontWait,
995 command + quoteName(dviname));
999 // case 1: print to a file
1000 FileName const filename(makeAbsPath(target_name,
1001 lyx_view_->buffer()->filePath()));
1002 FileName const dvifile(makeAbsPath(dviname, path));
1003 if (filename.exists()) {
1004 docstring text = bformat(
1005 _("The file %1$s already exists.\n\n"
1006 "Do you want to overwrite that file?"),
1007 makeDisplayPath(filename.absFilename()));
1008 if (Alert::prompt(_("Overwrite file?"),
1009 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1012 command += ' ' + lyxrc.print_to_file
1013 + quoteName(filename.toFilesystemEncoding())
1015 + quoteName(dvifile.toFilesystemEncoding());
1016 res = one.startscript(Systemcall::DontWait,
1021 showPrintError(buffer->absFileName());
1025 // FIXME: There is need for a command-line import.
1027 case LFUN_BUFFER_IMPORT:
1032 case LFUN_BUFFER_AUTO_SAVE:
1033 lyx_view_->buffer()->autoSave();
1036 case LFUN_RECONFIGURE:
1037 BOOST_ASSERT(lyx_view_);
1038 // argument is any additional parameter to the configure.py command
1039 reconfigure(*lyx_view_, argument);
1042 case LFUN_HELP_OPEN: {
1043 BOOST_ASSERT(lyx_view_);
1044 string const arg = argument;
1046 setErrorMessage(from_ascii(N_("Missing argument")));
1049 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1050 if (fname.empty()) {
1051 lyxerr << "LyX: unable to find documentation file `"
1052 << arg << "'. Bad installation?" << endl;
1055 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1056 makeDisplayPath(fname.absFilename())));
1057 Buffer * buf = lyx_view_->loadDocument(fname, false);
1060 lyx_view_->setBuffer(buf);
1061 buf->errors("Parse");
1063 updateFlags = Update::None;
1067 // --- version control -------------------------------
1068 case LFUN_VC_REGISTER:
1069 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1070 if (!ensureBufferClean(view()))
1072 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1073 lyx_view_->buffer()->lyxvc().registrer();
1076 updateFlags = Update::Force;
1079 case LFUN_VC_CHECK_IN:
1080 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1081 if (!ensureBufferClean(view()))
1083 if (lyx_view_->buffer()->lyxvc().inUse()
1084 && !lyx_view_->buffer()->isReadonly()) {
1085 lyx_view_->buffer()->lyxvc().checkIn();
1090 case LFUN_VC_CHECK_OUT:
1091 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1092 if (!ensureBufferClean(view()))
1094 if (lyx_view_->buffer()->lyxvc().inUse()
1095 && lyx_view_->buffer()->isReadonly()) {
1096 lyx_view_->buffer()->lyxvc().checkOut();
1101 case LFUN_VC_REVERT:
1102 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1103 lyx_view_->buffer()->lyxvc().revert();
1107 case LFUN_VC_UNDO_LAST:
1108 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1109 lyx_view_->buffer()->lyxvc().undoLast();
1113 // --- lyxserver commands ----------------------------
1114 case LFUN_SERVER_GET_NAME:
1115 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1116 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1117 LYXERR(Debug::INFO, "FNAME["
1118 << lyx_view_->buffer()->absFileName() << ']');
1121 case LFUN_SERVER_NOTIFY:
1122 dispatch_buffer = keyseq.print(KeySequence::Portable);
1123 theServer().notifyClient(to_utf8(dispatch_buffer));
1126 case LFUN_SERVER_GOTO_FILE_ROW: {
1127 BOOST_ASSERT(lyx_view_);
1130 istringstream is(argument);
1131 is >> file_name >> row;
1133 bool loaded = false;
1134 if (prefixIs(file_name, package().temp_dir().absFilename()))
1135 // Needed by inverse dvi search. If it is a file
1136 // in tmpdir, call the apropriated function
1137 buf = theBufferList().getBufferFromTmp(file_name);
1139 // Must replace extension of the file to be .lyx
1140 // and get full path
1141 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1142 // Either change buffer or load the file
1143 if (theBufferList().exists(s.absFilename()))
1144 buf = theBufferList().getBuffer(s.absFilename());
1146 buf = lyx_view_->loadDocument(s);
1152 updateFlags = Update::None;
1157 lyx_view_->setBuffer(buf);
1158 view()->setCursorFromRow(row);
1160 buf->errors("Parse");
1161 updateFlags = Update::FitCursor;
1166 case LFUN_DIALOG_SHOW_NEW_INSET: {
1167 BOOST_ASSERT(lyx_view_);
1168 string const name = cmd.getArg(0);
1169 InsetCode code = insetCode(name);
1170 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1171 bool insetCodeOK = true;
1180 case HYPERLINK_CODE: {
1181 InsetCommandParams p(code);
1182 data = InsetCommandMailer::params2string(name, p);
1185 case INCLUDE_CODE: {
1186 // data is the include type: one of "include",
1187 // "input", "verbatiminput" or "verbatiminput*"
1189 // default type is requested
1191 InsetCommandParams p(INCLUDE_CODE, data);
1192 data = InsetCommandMailer::params2string("include", p);
1196 // \c data == "Boxed" || "Frameless" etc
1197 InsetBoxParams p(data);
1198 data = InsetBoxMailer::params2string(p);
1202 InsetBranchParams p;
1203 data = InsetBranchMailer::params2string(p);
1207 InsetCommandParams p(CITE_CODE);
1208 data = InsetCommandMailer::params2string(name, p);
1212 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1215 case EXTERNAL_CODE: {
1216 InsetExternalParams p;
1217 Buffer const & buffer = *lyx_view_->buffer();
1218 data = InsetExternalMailer::params2string(p, buffer);
1223 data = InsetFloatMailer::params2string(p);
1226 case LISTINGS_CODE: {
1227 InsetListingsParams p;
1228 data = InsetListingsMailer::params2string(p);
1231 case GRAPHICS_CODE: {
1232 InsetGraphicsParams p;
1233 Buffer const & buffer = *lyx_view_->buffer();
1234 data = InsetGraphicsMailer::params2string(p, buffer);
1239 data = InsetNoteMailer::params2string(p);
1244 data = InsetVSpaceMailer::params2string(space);
1249 data = InsetWrapMailer::params2string(p);
1253 lyxerr << "Inset type '" << name <<
1254 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1255 insetCodeOK = false;
1257 } // end switch(code)
1259 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1263 case LFUN_CITATION_INSERT: {
1264 BOOST_ASSERT(lyx_view_);
1265 if (!argument.empty()) {
1266 // we can have one optional argument, delimited by '|'
1267 // citation-insert <key>|<text_before>
1268 // this should be enhanced to also support text_after
1269 // and citation style
1270 string arg = argument;
1272 if (contains(argument, "|")) {
1273 arg = token(argument, '|', 0);
1274 opt1 = token(argument, '|', 1);
1276 InsetCommandParams icp(CITE_CODE);
1277 icp["key"] = from_utf8(arg);
1279 icp["before"] = from_utf8(opt1);
1280 string icstr = InsetCommandMailer::params2string("citation", icp);
1281 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1284 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1288 case LFUN_BUFFER_CHILD_OPEN: {
1289 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1290 Buffer * parent = lyx_view_->buffer();
1291 FileName filename = makeAbsPath(argument, parent->filePath());
1292 view()->saveBookmark(false);
1294 bool parsed = false;
1295 if (theBufferList().exists(filename.absFilename())) {
1296 child = theBufferList().getBuffer(filename.absFilename());
1298 setMessage(bformat(_("Opening child document %1$s..."),
1299 makeDisplayPath(filename.absFilename())));
1300 child = lyx_view_->loadDocument(filename, false);
1304 // Set the parent name of the child document.
1305 // This makes insertion of citations and references in the child work,
1306 // when the target is in the parent or another child document.
1307 child->setParent(parent);
1308 updateLabels(*child->masterBuffer());
1309 lyx_view_->setBuffer(child);
1311 child->errors("Parse");
1314 // If a screen update is required (in case where auto_open is false),
1315 // setBuffer() would have taken care of it already. Otherwise we shall
1316 // reset the update flag because it can cause a circular problem.
1318 updateFlags = Update::None;
1322 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1323 BOOST_ASSERT(lyx_view_);
1324 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1327 case LFUN_KEYMAP_OFF:
1328 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1329 lyx_view_->view()->getIntl().keyMapOn(false);
1332 case LFUN_KEYMAP_PRIMARY:
1333 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1334 lyx_view_->view()->getIntl().keyMapPrim();
1337 case LFUN_KEYMAP_SECONDARY:
1338 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1339 lyx_view_->view()->getIntl().keyMapSec();
1342 case LFUN_KEYMAP_TOGGLE:
1343 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1344 lyx_view_->view()->getIntl().toggleKeyMap();
1350 string rest = split(argument, countstr, ' ');
1351 istringstream is(countstr);
1354 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1355 for (int i = 0; i < count; ++i)
1356 dispatch(lyxaction.lookupFunc(rest));
1360 case LFUN_COMMAND_SEQUENCE: {
1361 // argument contains ';'-terminated commands
1362 string arg = argument;
1363 while (!arg.empty()) {
1365 arg = split(arg, first, ';');
1366 FuncRequest func(lyxaction.lookupFunc(first));
1367 func.origin = cmd.origin;
1375 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1376 func.origin = cmd.origin;
1378 LyX::ref().topLevelCmdDef().release(argument);
1380 if (func.action == LFUN_UNKNOWN_ACTION) {
1381 // unknown command definition
1382 lyxerr << "Warning: unknown command definition `"
1386 // recursion detected
1387 lyxerr << "Warning: Recursion in the command definition `"
1388 << argument << "' detected"
1395 case LFUN_PREFERENCES_SAVE: {
1396 lyxrc.write(makeAbsPath("preferences",
1397 package().user_support().absFilename()),
1402 case LFUN_SET_COLOR: {
1404 string const x11_name = split(argument, lyx_name, ' ');
1405 if (lyx_name.empty() || x11_name.empty()) {
1406 setErrorMessage(from_ascii(N_(
1407 "Syntax: set-color <lyx_name>"
1412 bool const graphicsbg_changed =
1413 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1414 x11_name != lcolor.getX11Name(Color_graphicsbg));
1416 if (!lcolor.setColor(lyx_name, x11_name)) {
1418 bformat(_("Set-color \"%1$s\" failed "
1419 "- color is undefined or "
1420 "may not be redefined"),
1421 from_utf8(lyx_name)));
1425 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1427 if (graphicsbg_changed) {
1428 // FIXME: The graphics cache no longer has a changeDisplay method.
1430 graphics::GCache::get().changeDisplay(true);
1437 BOOST_ASSERT(lyx_view_);
1438 lyx_view_->message(from_utf8(argument));
1441 case LFUN_EXTERNAL_EDIT: {
1442 BOOST_ASSERT(lyx_view_);
1443 FuncRequest fr(action, argument);
1444 InsetExternal().dispatch(view()->cursor(), fr);
1448 case LFUN_GRAPHICS_EDIT: {
1449 FuncRequest fr(action, argument);
1450 InsetGraphics().dispatch(view()->cursor(), fr);
1454 case LFUN_ALL_INSETS_TOGGLE: {
1455 BOOST_ASSERT(lyx_view_);
1457 string const name = split(argument, action, ' ');
1458 InsetCode const inset_code = insetCode(name);
1460 Cursor & cur = view()->cursor();
1461 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1463 Inset & inset = lyx_view_->buffer()->inset();
1464 InsetIterator it = inset_iterator_begin(inset);
1465 InsetIterator const end = inset_iterator_end(inset);
1466 for (; it != end; ++it) {
1467 if (!it->asInsetMath()
1468 && (inset_code == NO_CODE
1469 || inset_code == it->lyxCode())) {
1470 Cursor tmpcur = cur;
1471 tmpcur.pushBackward(*it);
1472 it->dispatch(tmpcur, fr);
1475 updateFlags = Update::Force | Update::FitCursor;
1479 case LFUN_BUFFER_LANGUAGE: {
1480 BOOST_ASSERT(lyx_view_);
1481 Buffer & buffer = *lyx_view_->buffer();
1482 Language const * oldL = buffer.params().language;
1483 Language const * newL = languages.getLanguage(argument);
1484 if (!newL || oldL == newL)
1487 if (oldL->rightToLeft() == newL->rightToLeft()
1488 && !buffer.isMultiLingual())
1489 buffer.changeLanguage(oldL, newL);
1493 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1494 string const fname =
1495 addName(addPath(package().user_support().absFilename(), "templates/"),
1497 Buffer defaults(fname);
1499 istringstream ss(argument);
1502 int const unknown_tokens = defaults.readHeader(lex);
1504 if (unknown_tokens != 0) {
1505 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1506 << unknown_tokens << " unknown token"
1507 << (unknown_tokens == 1 ? "" : "s")
1511 if (defaults.writeFile(FileName(defaults.absFileName())))
1512 setMessage(bformat(_("Document defaults saved in %1$s"),
1513 makeDisplayPath(fname)));
1515 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1519 case LFUN_BUFFER_PARAMS_APPLY: {
1520 BOOST_ASSERT(lyx_view_);
1521 biblio::CiteEngine const oldEngine =
1522 lyx_view_->buffer()->params().getEngine();
1524 Buffer * buffer = lyx_view_->buffer();
1526 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1528 Cursor & cur = view()->cursor();
1529 cur.recordUndoFullDocument();
1531 istringstream ss(argument);
1534 int const unknown_tokens = buffer->readHeader(lex);
1536 if (unknown_tokens != 0) {
1537 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1538 << unknown_tokens << " unknown token"
1539 << (unknown_tokens == 1 ? "" : "s")
1543 updateLayout(oldClass, buffer);
1545 biblio::CiteEngine const newEngine =
1546 lyx_view_->buffer()->params().getEngine();
1548 if (oldEngine != newEngine) {
1549 FuncRequest fr(LFUN_INSET_REFRESH);
1551 Inset & inset = lyx_view_->buffer()->inset();
1552 InsetIterator it = inset_iterator_begin(inset);
1553 InsetIterator const end = inset_iterator_end(inset);
1554 for (; it != end; ++it)
1555 if (it->lyxCode() == CITE_CODE)
1556 it->dispatch(cur, fr);
1559 updateFlags = Update::Force | Update::FitCursor;
1560 // We are here most certainaly because of a change in the document
1561 // It is then better to make sure that all dialogs are in sync
1562 // with current document settings. LyXView::restartCursor() achieve this.
1563 lyx_view_->restartCursor();
1567 case LFUN_LAYOUT_MODULES_CLEAR: {
1568 BOOST_ASSERT(lyx_view_);
1569 Buffer * buffer = lyx_view_->buffer();
1570 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1571 view()->cursor().recordUndoFullDocument();
1572 buffer->params().clearLayoutModules();
1573 buffer->params().makeTextClass();
1574 updateLayout(oldClass, buffer);
1575 updateFlags = Update::Force | Update::FitCursor;
1579 case LFUN_LAYOUT_MODULE_ADD: {
1580 BOOST_ASSERT(lyx_view_);
1581 Buffer * buffer = lyx_view_->buffer();
1582 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1583 view()->cursor().recordUndoFullDocument();
1584 buffer->params().addLayoutModule(argument);
1585 buffer->params().makeTextClass();
1586 updateLayout(oldClass, buffer);
1587 updateFlags = Update::Force | Update::FitCursor;
1591 case LFUN_TEXTCLASS_APPLY: {
1592 BOOST_ASSERT(lyx_view_);
1593 Buffer * buffer = lyx_view_->buffer();
1595 loadTextClass(argument);
1597 pair<bool, textclass_type> const tc_pair =
1598 textclasslist.numberOfClass(argument);
1603 textclass_type const old_class = buffer->params().getBaseClass();
1604 textclass_type const new_class = tc_pair.second;
1606 if (old_class == new_class)
1610 //Save the old, possibly modular, layout for use in conversion.
1611 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1612 view()->cursor().recordUndoFullDocument();
1613 buffer->params().setBaseClass(new_class);
1614 buffer->params().makeTextClass();
1615 updateLayout(oldClass, buffer);
1616 updateFlags = Update::Force | Update::FitCursor;
1620 case LFUN_LAYOUT_RELOAD: {
1621 BOOST_ASSERT(lyx_view_);
1622 Buffer * buffer = lyx_view_->buffer();
1623 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1624 textclass_type const tc = buffer->params().getBaseClass();
1625 textclasslist.reset(tc);
1626 buffer->params().setBaseClass(tc);
1627 buffer->params().makeTextClass();
1628 updateLayout(oldClass, buffer);
1629 updateFlags = Update::Force | Update::FitCursor;
1633 case LFUN_TEXTCLASS_LOAD:
1634 loadTextClass(argument);
1637 case LFUN_LYXRC_APPLY: {
1638 LyXRC const lyxrc_orig = lyxrc;
1640 istringstream ss(argument);
1641 bool const success = lyxrc.read(ss) == 0;
1644 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1645 << "Unable to read lyxrc data"
1650 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1652 theApp()->resetGui();
1654 /// We force the redraw in any case because there might be
1655 /// some screen font changes.
1656 /// FIXME: only the current view will be updated. the Gui
1657 /// class is able to furnish the list of views.
1658 updateFlags = Update::Force;
1662 case LFUN_BOOKMARK_GOTO:
1663 // go to bookmark, open unopened file and switch to buffer if necessary
1664 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1665 updateFlags = Update::FitCursor;
1668 case LFUN_BOOKMARK_CLEAR:
1669 LyX::ref().session().bookmarks().clear();
1673 BOOST_ASSERT(theApp());
1674 // Let the frontend dispatch its own actions.
1675 if (theApp()->dispatch(cmd))
1676 // Nothing more to do.
1679 // Let the current LyXView dispatch its own actions.
1680 BOOST_ASSERT(lyx_view_);
1681 if (lyx_view_->dispatch(cmd)) {
1682 if (lyx_view_->view())
1683 updateFlags = lyx_view_->view()->cursor().result().update();
1687 BOOST_ASSERT(lyx_view_->view());
1688 // Let the current BufferView dispatch its own actions.
1689 if (view()->dispatch(cmd)) {
1690 // The BufferView took care of its own updates if needed.
1691 updateFlags = Update::None;
1695 // Let the current Cursor dispatch its own actions.
1696 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1697 cursorPosBeforeDispatchY_);
1698 view()->cursor().dispatch(cmd);
1699 updateFlags = view()->cursor().result().update();
1700 if (!view()->cursor().result().dispatched()) {
1701 // No update needed in this case (e.g. when reaching
1703 updateFlags = Update::None;
1707 if (lyx_view_ && lyx_view_->buffer()) {
1708 // BufferView::update() updates the ViewMetricsInfo and
1709 // also initializes the position cache for all insets in
1710 // (at least partially) visible top-level paragraphs.
1711 // We will redraw the screen only if needed.
1712 view()->processUpdateFlags(updateFlags);
1714 // if we executed a mutating lfun, mark the buffer as dirty
1716 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1717 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1718 lyx_view_->buffer()->markDirty();
1720 //Do we have a selection?
1721 theSelection().haveSelection(view()->cursor().selection());
1724 if (!quitting && lyx_view_) {
1725 // Some messages may already be translated, so we cannot use _()
1726 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1731 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1733 const bool verbose = (cmd.origin == FuncRequest::MENU
1734 || cmd.origin == FuncRequest::TOOLBAR
1735 || cmd.origin == FuncRequest::COMMANDBUFFER);
1737 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1738 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1740 lyx_view_->message(msg);
1744 docstring dispatch_msg = msg;
1745 if (!dispatch_msg.empty())
1746 dispatch_msg += ' ';
1748 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1750 bool argsadded = false;
1752 if (!cmd.argument().empty()) {
1753 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1754 comname += ' ' + cmd.argument();
1759 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1761 if (!shortcuts.empty())
1762 comname += ": " + shortcuts;
1763 else if (!argsadded && !cmd.argument().empty())
1764 comname += ' ' + cmd.argument();
1766 if (!comname.empty()) {
1767 comname = rtrim(comname);
1768 dispatch_msg += '(' + rtrim(comname) + ')';
1771 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1772 if (!dispatch_msg.empty())
1773 lyx_view_->message(dispatch_msg);
1777 void LyXFunc::closeBuffer()
1779 // goto bookmark to update bookmark pit.
1780 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1781 gotoBookmark(i+1, false, false);
1783 lyx_view_->closeBuffer();
1787 void LyXFunc::reloadBuffer()
1789 FileName filename = lyx_view_->buffer()->fileName();
1790 // The user has already confirmed that the changes, if any, should
1791 // be discarded. So we just release the Buffer and don't call closeBuffer();
1792 theBufferList().release(lyx_view_->buffer());
1793 Buffer * buf = lyx_view_->loadDocument(filename);
1794 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1798 lyx_view_->setBuffer(buf);
1799 buf->errors("Parse");
1800 str = bformat(_("Document %1$s reloaded."), disp_fn);
1802 str = bformat(_("Could not reload document %1$s"), disp_fn);
1804 lyx_view_->message(str);
1807 // Each "lyx_view_" should have it's own message method. lyxview and
1808 // the minibuffer would use the minibuffer, but lyxserver would
1809 // send an ERROR signal to its client. Alejandro 970603
1810 // This function is bit problematic when it comes to NLS, to make the
1811 // lyx servers client be language indepenent we must not translate
1812 // strings sent to this func.
1813 void LyXFunc::setErrorMessage(docstring const & m) const
1815 dispatch_buffer = m;
1820 void LyXFunc::setMessage(docstring const & m) const
1822 dispatch_buffer = m;
1826 docstring const LyXFunc::viewStatusMessage()
1828 // When meta-fake key is pressed, show the key sequence so far + "M-".
1830 return keyseq.print(KeySequence::ForGui) + "M-";
1832 // Else, when a non-complete key sequence is pressed,
1833 // show the available options.
1834 if (keyseq.length() > 0 && !keyseq.deleted())
1835 return keyseq.printOptions(true);
1837 BOOST_ASSERT(lyx_view_);
1838 if (!lyx_view_->buffer())
1839 return _("Welcome to LyX!");
1841 return view()->cursor().currentState();
1845 BufferView * LyXFunc::view() const
1847 BOOST_ASSERT(lyx_view_);
1848 return lyx_view_->view();
1852 bool LyXFunc::wasMetaKey() const
1854 return (meta_fake_bit != NoModifier);
1858 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1861 lyx_view_->message(_("Converting document to new document class..."));
1863 StableDocIterator backcur(view()->cursor());
1864 ErrorList & el = buffer->errorList("Class Switch");
1865 cap::switchBetweenClasses(
1866 oldlayout, buffer->params().getTextClassPtr(),
1867 static_cast<InsetText &>(buffer->inset()), el);
1869 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1871 buffer->errors("Class Switch");
1872 updateLabels(*buffer);
1878 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1880 // Why the switch you might ask. It is a trick to ensure that all
1881 // the elements in the LyXRCTags enum is handled. As you can see
1882 // there are no breaks at all. So it is just a huge fall-through.
1883 // The nice thing is that we will get a warning from the compiler
1884 // if we forget an element.
1885 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1887 case LyXRC::RC_ACCEPT_COMPOUND:
1888 case LyXRC::RC_ALT_LANG:
1889 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1890 case LyXRC::RC_PLAINTEXT_LINELEN:
1891 case LyXRC::RC_AUTOREGIONDELETE:
1892 case LyXRC::RC_AUTORESET_OPTIONS:
1893 case LyXRC::RC_AUTOSAVE:
1894 case LyXRC::RC_AUTO_NUMBER:
1895 case LyXRC::RC_BACKUPDIR_PATH:
1896 case LyXRC::RC_BIBTEX_COMMAND:
1897 case LyXRC::RC_BINDFILE:
1898 case LyXRC::RC_CHECKLASTFILES:
1899 case LyXRC::RC_USELASTFILEPOS:
1900 case LyXRC::RC_LOADSESSION:
1901 case LyXRC::RC_CHKTEX_COMMAND:
1902 case LyXRC::RC_CONVERTER:
1903 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1904 case LyXRC::RC_COPIER:
1905 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1906 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1907 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1908 case LyXRC::RC_DATE_INSERT_FORMAT:
1909 case LyXRC::RC_DEFAULT_LANGUAGE:
1910 case LyXRC::RC_DEFAULT_PAPERSIZE:
1911 case LyXRC::RC_DEFFILE:
1912 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1913 case LyXRC::RC_DISPLAY_GRAPHICS:
1914 case LyXRC::RC_DOCUMENTPATH:
1915 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1916 FileName path(lyxrc_new.document_path);
1917 if (path.exists() && path.isDirectory())
1918 package().document_dir() = FileName(lyxrc.document_path);
1920 case LyXRC::RC_ESC_CHARS:
1921 case LyXRC::RC_EXAMPLEPATH:
1922 case LyXRC::RC_FONT_ENCODING:
1923 case LyXRC::RC_FORMAT:
1924 case LyXRC::RC_INDEX_COMMAND:
1925 case LyXRC::RC_INPUT:
1926 case LyXRC::RC_KBMAP:
1927 case LyXRC::RC_KBMAP_PRIMARY:
1928 case LyXRC::RC_KBMAP_SECONDARY:
1929 case LyXRC::RC_LABEL_INIT_LENGTH:
1930 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1931 case LyXRC::RC_LANGUAGE_AUTO_END:
1932 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1933 case LyXRC::RC_LANGUAGE_COMMAND_END:
1934 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1935 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1936 case LyXRC::RC_LANGUAGE_PACKAGE:
1937 case LyXRC::RC_LANGUAGE_USE_BABEL:
1938 case LyXRC::RC_MACRO_EDIT_STYLE:
1939 case LyXRC::RC_MAKE_BACKUP:
1940 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1941 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1942 case LyXRC::RC_NUMLASTFILES:
1943 case LyXRC::RC_PATH_PREFIX:
1944 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1945 prependEnvPath("PATH", lyxrc.path_prefix);
1947 case LyXRC::RC_PERS_DICT:
1948 case LyXRC::RC_PREVIEW:
1949 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1950 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1951 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1952 case LyXRC::RC_PRINTCOPIESFLAG:
1953 case LyXRC::RC_PRINTER:
1954 case LyXRC::RC_PRINTEVENPAGEFLAG:
1955 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1956 case LyXRC::RC_PRINTFILEEXTENSION:
1957 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1958 case LyXRC::RC_PRINTODDPAGEFLAG:
1959 case LyXRC::RC_PRINTPAGERANGEFLAG:
1960 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1961 case LyXRC::RC_PRINTPAPERFLAG:
1962 case LyXRC::RC_PRINTREVERSEFLAG:
1963 case LyXRC::RC_PRINTSPOOL_COMMAND:
1964 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1965 case LyXRC::RC_PRINTTOFILE:
1966 case LyXRC::RC_PRINTTOPRINTER:
1967 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1968 case LyXRC::RC_PRINT_COMMAND:
1969 case LyXRC::RC_RTL_SUPPORT:
1970 case LyXRC::RC_SCREEN_DPI:
1971 case LyXRC::RC_SCREEN_FONT_ROMAN:
1972 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1973 case LyXRC::RC_SCREEN_FONT_SANS:
1974 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1975 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1976 case LyXRC::RC_SCREEN_FONT_SIZES:
1977 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1978 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1979 case LyXRC::RC_GEOMETRY_SESSION:
1980 case LyXRC::RC_SCREEN_ZOOM:
1981 case LyXRC::RC_SERVERPIPE:
1982 case LyXRC::RC_SET_COLOR:
1983 case LyXRC::RC_SHOW_BANNER:
1984 case LyXRC::RC_SPELL_COMMAND:
1985 case LyXRC::RC_TEMPDIRPATH:
1986 case LyXRC::RC_TEMPLATEPATH:
1987 case LyXRC::RC_TEX_ALLOWS_SPACES:
1988 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1989 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1990 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1992 case LyXRC::RC_UIFILE:
1993 case LyXRC::RC_USER_EMAIL:
1994 case LyXRC::RC_USER_NAME:
1995 case LyXRC::RC_USETEMPDIR:
1996 case LyXRC::RC_USE_ALT_LANG:
1997 case LyXRC::RC_USE_CONVERTER_CACHE:
1998 case LyXRC::RC_USE_ESC_CHARS:
1999 case LyXRC::RC_USE_INP_ENC:
2000 case LyXRC::RC_USE_PERS_DICT:
2001 case LyXRC::RC_USE_TOOLTIP:
2002 case LyXRC::RC_USE_PIXMAP_CACHE:
2003 case LyXRC::RC_USE_SPELL_LIB:
2004 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2005 case LyXRC::RC_SORT_LAYOUTS:
2006 case LyXRC::RC_VIEWER:
2007 case LyXRC::RC_LAST: