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.
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "support/debug.h"
37 #include "DispatchResult.h"
39 #include "ErrorList.h"
41 #include "FuncRequest.h"
42 #include "FuncStatus.h"
43 #include "support/gettext.h"
44 #include "InsetIterator.h"
49 #include "LyXAction.h"
54 #include "Paragraph.h"
55 #include "ParagraphParameters.h"
56 #include "ParIterator.h"
60 #include "TextClassList.h"
61 #include "ToolbarBackend.h"
63 #include "insets/InsetBox.h"
64 #include "insets/InsetBranch.h"
65 #include "insets/InsetCommand.h"
66 #include "insets/InsetERT.h"
67 #include "insets/InsetExternal.h"
68 #include "insets/InsetFloat.h"
69 #include "insets/InsetListings.h"
70 #include "insets/InsetGraphics.h"
71 #include "insets/InsetInclude.h"
72 #include "insets/InsetNote.h"
73 #include "insets/InsetTabular.h"
74 #include "insets/InsetVSpace.h"
75 #include "insets/InsetWrap.h"
77 #include "frontends/alert.h"
78 #include "frontends/Application.h"
79 #include "frontends/KeySymbol.h"
80 #include "frontends/LyXView.h"
81 #include "frontends/Selection.h"
83 #include "support/environment.h"
84 #include "support/FileName.h"
85 #include "support/filetools.h"
86 #include "support/lstrings.h"
87 #include "support/Path.h"
88 #include "support/Package.h"
89 #include "support/Systemcall.h"
90 #include "support/convert.h"
91 #include "support/os.h"
96 using namespace lyx::support;
100 using frontend::LyXView;
102 namespace Alert = frontend::Alert;
104 extern bool quitting;
110 // This function runs "configure" and then rereads lyx.defaults to
111 // reconfigure the automatic settings.
112 void reconfigure(LyXView & lv, string const & option)
114 // emit message signal.
115 lv.message(_("Running configure..."));
117 // Run configure in user lyx directory
118 PathChanger p(package().user_support());
119 string configure_command = package().configure_command();
120 configure_command += option;
122 int ret = one.startscript(Systemcall::Wait, configure_command);
124 // emit message signal.
125 lv.message(_("Reloading configuration..."));
126 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
127 // Re-read packages.lst
128 LaTeXFeatures::getAvailable();
131 Alert::information(_("System reconfiguration failed"),
132 _("The system reconfiguration has failed.\n"
133 "Default textclass is used but LyX may "
134 "not be able to work properly.\n"
135 "Please reconfigure again if needed."));
138 Alert::information(_("System reconfigured"),
139 _("The system has been reconfigured.\n"
140 "You need to restart LyX to make use of any\n"
141 "updated document class specifications."));
145 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
147 // Try to fix cursor in case it is broken.
148 cursor.fixIfBroken();
150 // This is, of course, a mess. Better create a new doc iterator and use
151 // this in Inset::getStatus. This might require an additional
152 // BufferView * arg, though (which should be avoided)
153 //Cursor safe = *this;
155 for ( ; cursor.depth(); cursor.pop()) {
156 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
157 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
158 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
159 BOOST_ASSERT(cursor.pos() <= cursor.lastpos());
161 // The inset's getStatus() will return 'true' if it made
162 // a definitive decision on whether it want to handle the
163 // request or not. The result of this decision is put into
164 // the 'status' parameter.
165 if (cursor.inset().getStatus(cursor, cmd, status)) {
174 /** Return the change status at cursor position, taking in account the
175 * status at each level of the document iterator (a table in a deleted
176 * footnote is deleted).
177 * When \param outer is true, the top slice is not looked at.
179 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
181 size_t const depth = dit.depth() - (outer ? 1 : 0);
183 for (size_t i = 0 ; i < depth ; ++i) {
184 CursorSlice const & slice = dit[i];
185 if (!slice.inset().inMathed()
186 && slice.pos() < slice.paragraph().size()) {
187 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
188 if (ch != Change::UNCHANGED)
192 return Change::UNCHANGED;
199 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
204 void LyXFunc::initKeySequences(KeyMap * kb)
206 keyseq = KeySequence(kb, kb);
207 cancel_meta_seq = KeySequence(kb, kb);
211 void LyXFunc::setLyXView(LyXView * lv)
213 if (!quitting && lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
214 // save current selection to the selection buffer to allow
215 // middle-button paste in another window
216 cap::saveSelection(lyx_view_->view()->cursor());
221 void LyXFunc::handleKeyFunc(kb_action action)
223 char_type c = encoded_last_key;
228 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
229 lyx_view_->view()->getIntl().getTransManager().deadkey(
230 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
231 // Need to clear, in case the minibuffer calls these
234 // copied verbatim from do_accent_char
235 view()->cursor().resetAnchor();
236 view()->processUpdateFlags(Update::FitCursor);
240 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
242 BOOST_ASSERT(lyx_view_);
243 if (!LyX::ref().session().bookmarks().isValid(idx))
245 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
246 BOOST_ASSERT(!bm.filename.empty());
247 string const file = bm.filename.absFilename();
248 // if the file is not opened, open it.
249 if (!theBufferList().exists(file)) {
251 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
255 // open may fail, so we need to test it again
256 if (!theBufferList().exists(file))
259 // if the current buffer is not that one, switch to it.
260 if (lyx_view_->buffer()->absFileName() != file) {
263 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
265 // moveToPosition try paragraph id first and then paragraph (pit, pos).
266 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
267 bm.top_id, bm.top_pos))
270 // Cursor jump succeeded!
271 Cursor const & cur = view()->cursor();
272 pit_type new_pit = cur.pit();
273 pos_type new_pos = cur.pos();
274 int new_id = cur.paragraph().id();
276 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
277 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
278 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
279 || bm.top_id != new_id) {
280 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
281 new_pit, new_pos, new_id);
286 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
288 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
290 // Do nothing if we have nothing (JMarc)
291 if (!keysym.isOK()) {
292 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
293 lyx_view_->restartCursor();
297 if (keysym.isModifier()) {
298 LYXERR(Debug::KEY, "isModifier true");
299 lyx_view_->restartCursor();
303 //Encoding const * encoding = view()->cursor().getEncoding();
304 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
305 // FIXME: encoded_last_key shadows the member variable of the same
306 // name. Is that intended?
307 char_type encoded_last_key = keysym.getUCSEncoded();
309 // Do a one-deep top-level lookup for
310 // cancel and meta-fake keys. RVDK_PATCH_5
311 cancel_meta_seq.reset();
313 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
314 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
316 // When not cancel or meta-fake, do the normal lookup.
317 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
318 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
319 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
320 // remove Caps Lock and Mod2 as a modifiers
321 func = keyseq.addkey(keysym, (state | meta_fake_bit));
322 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
325 // Dont remove this unless you know what you are doing.
326 meta_fake_bit = NoModifier;
328 // Can this happen now ?
329 if (func.action == LFUN_NOACTION)
330 func = FuncRequest(LFUN_COMMAND_PREFIX);
332 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
333 << keyseq.print(KeySequence::Portable) << ']');
335 // already here we know if it any point in going further
336 // why not return already here if action == -1 and
337 // num_bytes == 0? (Lgb)
339 if (keyseq.length() > 1)
340 lyx_view_->message(keyseq.print(KeySequence::ForGui));
343 // Maybe user can only reach the key via holding down shift.
344 // Let's see. But only if shift is the only modifier
345 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
346 LYXERR(Debug::KEY, "Trying without shift");
347 func = keyseq.addkey(keysym, NoModifier);
348 LYXERR(Debug::KEY, "Action now " << func.action);
351 if (func.action == LFUN_UNKNOWN_ACTION) {
352 // Hmm, we didn't match any of the keysequences. See
353 // if it's normal insertable text not already covered
355 if (keysym.isText() && keyseq.length() == 1) {
356 LYXERR(Debug::KEY, "isText() is true, inserting.");
357 func = FuncRequest(LFUN_SELF_INSERT,
358 FuncRequest::KEYBOARD);
360 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
361 lyx_view_->message(_("Unknown function."));
362 lyx_view_->restartCursor();
367 if (func.action == LFUN_SELF_INSERT) {
368 if (encoded_last_key != 0) {
369 docstring const arg(1, encoded_last_key);
370 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
371 FuncRequest::KEYBOARD));
372 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
378 lyx_view_->restartCursor();
382 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
384 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
387 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
389 if (cmd.action == LFUN_NOACTION) {
390 flag.message(from_utf8(N_("Nothing to do")));
395 switch (cmd.action) {
396 case LFUN_UNKNOWN_ACTION:
397 #ifndef HAVE_LIBAIKSAURUS
398 case LFUN_THESAURUS_ENTRY:
408 if (flag.unknown()) {
409 flag.message(from_utf8(N_("Unknown action")));
413 if (!flag.enabled()) {
414 if (flag.message().empty())
415 flag.message(from_utf8(N_("Command disabled")));
419 // Check whether we need a buffer
420 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
422 flag.message(from_utf8(N_("Command not allowed with"
423 "out any document open")));
428 // I would really like to avoid having this switch and rather try to
429 // encode this in the function itself.
430 // -- And I'd rather let an inset decide which LFUNs it is willing
431 // to handle (Andre')
433 switch (cmd.action) {
435 // FIXME: these cases should be hidden in GuiApplication::getStatus().
436 case LFUN_WINDOW_CLOSE:
438 return theApp()->getStatus(cmd);
442 // FIXME: these cases should be hidden in GuiView::getStatus().
443 case LFUN_DIALOG_TOGGLE:
444 case LFUN_DIALOG_SHOW:
445 case LFUN_DIALOG_UPDATE:
446 if (cmd.argument() == "prefs"
447 || cmd.argument() == "aboutlyx")
450 return lyx_view_->getStatus(cmd);
455 case LFUN_TOOLBAR_TOGGLE:
456 case LFUN_INSET_APPLY:
457 case LFUN_BUFFER_WRITE:
458 case LFUN_BUFFER_WRITE_AS:
460 return lyx_view_->getStatus(cmd);
464 case LFUN_BUFFER_TOGGLE_READ_ONLY:
465 flag.setOnOff(buf->isReadonly());
468 case LFUN_BUFFER_SWITCH:
469 // toggle on the current buffer, but do not toggle off
470 // the other ones (is that a good idea?)
471 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
475 case LFUN_BUFFER_EXPORT:
476 enable = cmd.argument() == "custom"
477 || buf->isExportable(to_utf8(cmd.argument()));
480 case LFUN_BUFFER_CHKTEX:
481 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
484 case LFUN_BUILD_PROGRAM:
485 enable = buf->isExportable("program");
488 case LFUN_VC_REGISTER:
489 enable = !buf->lyxvc().inUse();
491 case LFUN_VC_CHECK_IN:
492 enable = buf->lyxvc().inUse() && !buf->isReadonly();
494 case LFUN_VC_CHECK_OUT:
495 enable = buf->lyxvc().inUse() && buf->isReadonly();
498 case LFUN_VC_UNDO_LAST:
499 enable = buf->lyxvc().inUse();
501 case LFUN_BUFFER_RELOAD:
502 enable = !buf->isUnnamed() && buf->fileName().exists()
503 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
506 case LFUN_CITATION_INSERT: {
507 FuncRequest fr(LFUN_INSET_INSERT, "citation");
508 enable = getStatus(fr).enabled();
512 // This could be used for the no-GUI version. The GUI version is handled in
513 // LyXView::getStatus(). See above.
515 case LFUN_BUFFER_WRITE:
516 case LFUN_BUFFER_WRITE_AS: {
517 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
518 enable = b && (b->isUnnamed() || !b->isClean());
523 case LFUN_BUFFER_WRITE_ALL: {
524 // We enable the command only if there are some modified buffers
525 Buffer * first = theBufferList().first();
530 // We cannot use a for loop as the buffer list is a cycle.
536 b = theBufferList().next(b);
537 } while (b != first);
541 case LFUN_BOOKMARK_GOTO: {
542 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
543 enable = LyX::ref().session().bookmarks().isValid(num);
547 case LFUN_BOOKMARK_CLEAR:
548 enable = LyX::ref().session().bookmarks().size() > 0;
551 // this one is difficult to get right. As a half-baked
552 // solution, we consider only the first action of the sequence
553 case LFUN_COMMAND_SEQUENCE: {
554 // argument contains ';'-terminated commands
555 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
556 FuncRequest func(lyxaction.lookupFunc(firstcmd));
557 func.origin = cmd.origin;
558 flag = getStatus(func);
564 string name = to_utf8(cmd.argument());
565 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
566 func.origin = cmd.origin;
567 flag = getStatus(func);
568 LyX::ref().topLevelCmdDef().release(name);
570 // catch recursion or unknown command definiton
571 // all operations until the recursion or unknown command
572 // definiton occures are performed, so set the state to enabled
578 case LFUN_BUFFER_NEW:
579 case LFUN_BUFFER_NEW_TEMPLATE:
580 case LFUN_WORD_FIND_FORWARD:
581 case LFUN_WORD_FIND_BACKWARD:
582 case LFUN_COMMAND_PREFIX:
583 case LFUN_COMMAND_EXECUTE:
585 case LFUN_META_PREFIX:
586 case LFUN_BUFFER_CLOSE:
587 case LFUN_BUFFER_UPDATE:
588 case LFUN_BUFFER_VIEW:
589 case LFUN_MASTER_BUFFER_UPDATE:
590 case LFUN_MASTER_BUFFER_VIEW:
591 case LFUN_BUFFER_IMPORT:
592 case LFUN_BUFFER_AUTO_SAVE:
593 case LFUN_RECONFIGURE:
596 case LFUN_DROP_LAYOUTS_CHOICE:
598 case LFUN_SERVER_GET_NAME:
599 case LFUN_SERVER_NOTIFY:
600 case LFUN_SERVER_GOTO_FILE_ROW:
601 case LFUN_DIALOG_HIDE:
602 case LFUN_DIALOG_DISCONNECT_INSET:
603 case LFUN_BUFFER_CHILD_OPEN:
605 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
606 case LFUN_KEYMAP_OFF:
607 case LFUN_KEYMAP_PRIMARY:
608 case LFUN_KEYMAP_SECONDARY:
609 case LFUN_KEYMAP_TOGGLE:
611 case LFUN_BUFFER_EXPORT_CUSTOM:
612 case LFUN_BUFFER_PRINT:
613 case LFUN_PREFERENCES_SAVE:
614 case LFUN_SCREEN_FONT_UPDATE:
617 case LFUN_EXTERNAL_EDIT:
618 case LFUN_GRAPHICS_EDIT:
619 case LFUN_ALL_INSETS_TOGGLE:
620 case LFUN_BUFFER_LANGUAGE:
621 case LFUN_TEXTCLASS_APPLY:
622 case LFUN_TEXTCLASS_LOAD:
623 case LFUN_BUFFER_SAVE_AS_DEFAULT:
624 case LFUN_BUFFER_PARAMS_APPLY:
625 case LFUN_LAYOUT_MODULES_CLEAR:
626 case LFUN_LAYOUT_MODULE_ADD:
627 case LFUN_LAYOUT_RELOAD:
628 case LFUN_LYXRC_APPLY:
629 case LFUN_BUFFER_NEXT:
630 case LFUN_BUFFER_PREVIOUS:
631 case LFUN_WINDOW_NEW:
633 // these are handled in our dispatch()
641 if (!getLocalStatus(view()->cursor(), cmd, flag))
642 flag = view()->getStatus(cmd);
648 // Can we use a readonly buffer?
649 if (buf && buf->isReadonly()
650 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
651 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
652 flag.message(from_utf8(N_("Document is read-only")));
656 // Are we in a DELETED change-tracking region?
658 && lookupChangeType(view()->cursor(), true) == Change::DELETED
659 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
660 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
661 flag.message(from_utf8(N_("This portion of the document is deleted.")));
665 // the default error message if we disable the command
666 if (!flag.enabled() && flag.message().empty())
667 flag.message(from_utf8(N_("Command disabled")));
673 bool LyXFunc::ensureBufferClean(BufferView * bv)
675 Buffer & buf = bv->buffer();
679 docstring const file = buf.fileName().displayName(30);
680 docstring text = bformat(_("The document %1$s has unsaved "
681 "changes.\n\nDo you want to save "
682 "the document?"), file);
683 int const ret = Alert::prompt(_("Save changed document?"),
684 text, 0, 1, _("&Save"),
688 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
690 return buf.isClean();
696 void showPrintError(string const & name)
698 docstring str = bformat(_("Could not print the document %1$s.\n"
699 "Check that your printer is set up correctly."),
700 makeDisplayPath(name, 50));
701 Alert::error(_("Print document failed"), str);
705 void loadTextClass(string const & name)
707 pair<bool, textclass_type> const tc_pair =
708 textclasslist.numberOfClass(name);
710 if (!tc_pair.first) {
711 lyxerr << "Document class \"" << name
712 << "\" does not exist."
717 textclass_type const tc = tc_pair.second;
719 if (!textclasslist[tc].load()) {
720 docstring s = bformat(_("The document class %1$s."
721 "could not be loaded."),
722 from_utf8(textclasslist[tc].name()));
723 Alert::error(_("Could not load class"), s);
728 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
733 void LyXFunc::dispatch(FuncRequest const & cmd)
735 string const argument = to_utf8(cmd.argument());
736 kb_action const action = cmd.action;
738 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
739 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
741 // we have not done anything wrong yet.
743 dispatch_buffer.erase();
745 // redraw the screen at the end (first of the two drawing steps).
746 //This is done unless explicitely requested otherwise
747 Update::flags updateFlags = Update::FitCursor;
749 FuncStatus const flag = getStatus(cmd);
750 if (!flag.enabled()) {
751 // We cannot use this function here
752 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
753 << lyxaction.getActionName(action)
754 << " [" << action << "] is disabled at this location");
755 setErrorMessage(flag.message());
759 case LFUN_WORD_FIND_FORWARD:
760 case LFUN_WORD_FIND_BACKWARD: {
761 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
762 static docstring last_search;
763 docstring searched_string;
765 if (!cmd.argument().empty()) {
766 last_search = cmd.argument();
767 searched_string = cmd.argument();
769 searched_string = last_search;
772 if (searched_string.empty())
775 bool const fw = action == LFUN_WORD_FIND_FORWARD;
776 docstring const data =
777 find2string(searched_string, true, false, fw);
778 find(view(), FuncRequest(LFUN_WORD_FIND, data));
782 case LFUN_COMMAND_PREFIX:
783 BOOST_ASSERT(lyx_view_);
784 lyx_view_->message(keyseq.printOptions(true));
788 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
790 meta_fake_bit = NoModifier;
791 if (lyx_view_->buffer())
792 // cancel any selection
793 dispatch(FuncRequest(LFUN_MARK_OFF));
794 setMessage(from_ascii(N_("Cancel")));
797 case LFUN_META_PREFIX:
798 meta_fake_bit = AltModifier;
799 setMessage(keyseq.print(KeySequence::ForGui));
802 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
803 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
804 Buffer * buf = lyx_view_->buffer();
805 if (buf->lyxvc().inUse())
806 buf->lyxvc().toggleReadOnly();
808 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
812 // --- Menus -----------------------------------------------
813 case LFUN_BUFFER_NEW:
814 lyx_view_->newDocument(argument, false);
815 updateFlags = Update::None;
818 case LFUN_BUFFER_NEW_TEMPLATE:
819 lyx_view_->newDocument(argument, true);
820 updateFlags = Update::None;
823 case LFUN_BUFFER_CLOSE:
825 updateFlags = Update::None;
828 case LFUN_BUFFER_RELOAD: {
829 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
830 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
831 docstring text = bformat(_("Any changes will be lost. Are you sure "
832 "you want to revert to the saved version of the document %1$s?"), file);
833 int const ret = Alert::prompt(_("Revert to saved document?"),
834 text, 1, 1, _("&Revert"), _("&Cancel"));
841 case LFUN_BUFFER_UPDATE:
842 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
843 lyx_view_->buffer()->doExport(argument, true);
846 case LFUN_BUFFER_VIEW:
847 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
848 lyx_view_->buffer()->preview(argument);
851 case LFUN_MASTER_BUFFER_UPDATE:
852 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
853 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
856 case LFUN_MASTER_BUFFER_VIEW:
857 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
858 lyx_view_->buffer()->masterBuffer()->preview(argument);
861 case LFUN_BUILD_PROGRAM:
862 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
863 lyx_view_->buffer()->doExport("program", true);
866 case LFUN_BUFFER_CHKTEX:
867 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
868 lyx_view_->buffer()->runChktex();
871 case LFUN_BUFFER_EXPORT:
872 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
873 if (argument == "custom")
874 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
876 lyx_view_->buffer()->doExport(argument, false);
879 case LFUN_BUFFER_EXPORT_CUSTOM: {
880 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
882 string command = split(argument, format_name, ' ');
883 Format const * format = formats.getFormat(format_name);
885 lyxerr << "Format \"" << format_name
886 << "\" not recognized!"
891 Buffer * buffer = lyx_view_->buffer();
893 // The name of the file created by the conversion process
896 // Output to filename
897 if (format->name() == "lyx") {
898 string const latexname = buffer->latexName(false);
899 filename = changeExtension(latexname,
900 format->extension());
901 filename = addName(buffer->temppath(), filename);
903 if (!buffer->writeFile(FileName(filename)))
907 buffer->doExport(format_name, true, filename);
910 // Substitute $$FName for filename
911 if (!contains(command, "$$FName"))
912 command = "( " + command + " ) < $$FName";
913 command = subst(command, "$$FName", filename);
915 // Execute the command in the background
917 call.startscript(Systemcall::DontWait, command);
921 case LFUN_BUFFER_PRINT: {
922 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
923 // FIXME: cmd.getArg() might fail if one of the arguments
924 // contains double quotes
925 string target = cmd.getArg(0);
926 string target_name = cmd.getArg(1);
927 string command = cmd.getArg(2);
930 || target_name.empty()
931 || command.empty()) {
932 lyxerr << "Unable to parse \""
933 << argument << '"' << endl;
936 if (target != "printer" && target != "file") {
937 lyxerr << "Unrecognized target \""
938 << target << '"' << endl;
942 Buffer * buffer = lyx_view_->buffer();
944 if (!buffer->doExport("dvi", true)) {
945 showPrintError(buffer->absFileName());
949 // Push directory path.
950 string const path = buffer->temppath();
951 // Prevent the compiler from optimizing away p
955 // there are three cases here:
956 // 1. we print to a file
957 // 2. we print directly to a printer
958 // 3. we print using a spool command (print to file first)
961 string const dviname =
962 changeExtension(buffer->latexName(true), "dvi");
964 if (target == "printer") {
965 if (!lyxrc.print_spool_command.empty()) {
966 // case 3: print using a spool
967 string const psname =
968 changeExtension(dviname,".ps");
969 command += ' ' + lyxrc.print_to_file
972 + quoteName(dviname);
975 lyxrc.print_spool_command + ' ';
976 if (target_name != "default") {
977 command2 += lyxrc.print_spool_printerprefix
981 command2 += quoteName(psname);
983 // If successful, then spool command
984 res = one.startscript(
989 res = one.startscript(
990 Systemcall::DontWait,
993 // case 2: print directly to a printer
994 if (target_name != "default")
995 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
996 res = one.startscript(
997 Systemcall::DontWait,
998 command + quoteName(dviname));
1002 // case 1: print to a file
1003 FileName const filename(makeAbsPath(target_name,
1004 lyx_view_->buffer()->filePath()));
1005 FileName const dvifile(makeAbsPath(dviname, path));
1006 if (filename.exists()) {
1007 docstring text = bformat(
1008 _("The file %1$s already exists.\n\n"
1009 "Do you want to overwrite that file?"),
1010 makeDisplayPath(filename.absFilename()));
1011 if (Alert::prompt(_("Overwrite file?"),
1012 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1015 command += ' ' + lyxrc.print_to_file
1016 + quoteName(filename.toFilesystemEncoding())
1018 + quoteName(dvifile.toFilesystemEncoding());
1019 res = one.startscript(Systemcall::DontWait,
1024 showPrintError(buffer->absFileName());
1028 // FIXME: There is need for a command-line import.
1030 case LFUN_BUFFER_IMPORT:
1035 case LFUN_BUFFER_AUTO_SAVE:
1036 lyx_view_->buffer()->autoSave();
1039 case LFUN_RECONFIGURE:
1040 BOOST_ASSERT(lyx_view_);
1041 // argument is any additional parameter to the configure.py command
1042 reconfigure(*lyx_view_, argument);
1045 case LFUN_HELP_OPEN: {
1046 BOOST_ASSERT(lyx_view_);
1047 string const arg = argument;
1049 setErrorMessage(from_ascii(N_("Missing argument")));
1052 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1053 if (fname.empty()) {
1054 lyxerr << "LyX: unable to find documentation file `"
1055 << arg << "'. Bad installation?" << endl;
1058 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1059 makeDisplayPath(fname.absFilename())));
1060 Buffer * buf = lyx_view_->loadDocument(fname, false);
1063 lyx_view_->setBuffer(buf);
1064 buf->errors("Parse");
1066 updateFlags = Update::None;
1070 // --- version control -------------------------------
1071 case LFUN_VC_REGISTER:
1072 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1073 if (!ensureBufferClean(view()))
1075 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1076 lyx_view_->buffer()->lyxvc().registrer();
1079 updateFlags = Update::Force;
1082 case LFUN_VC_CHECK_IN:
1083 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1084 if (!ensureBufferClean(view()))
1086 if (lyx_view_->buffer()->lyxvc().inUse()
1087 && !lyx_view_->buffer()->isReadonly()) {
1088 lyx_view_->buffer()->lyxvc().checkIn();
1093 case LFUN_VC_CHECK_OUT:
1094 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1095 if (!ensureBufferClean(view()))
1097 if (lyx_view_->buffer()->lyxvc().inUse()
1098 && lyx_view_->buffer()->isReadonly()) {
1099 lyx_view_->buffer()->lyxvc().checkOut();
1104 case LFUN_VC_REVERT:
1105 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1106 lyx_view_->buffer()->lyxvc().revert();
1110 case LFUN_VC_UNDO_LAST:
1111 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1112 lyx_view_->buffer()->lyxvc().undoLast();
1116 // --- lyxserver commands ----------------------------
1117 case LFUN_SERVER_GET_NAME:
1118 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1119 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1120 LYXERR(Debug::INFO, "FNAME["
1121 << lyx_view_->buffer()->absFileName() << ']');
1124 case LFUN_SERVER_NOTIFY:
1125 dispatch_buffer = keyseq.print(KeySequence::Portable);
1126 theServer().notifyClient(to_utf8(dispatch_buffer));
1129 case LFUN_SERVER_GOTO_FILE_ROW: {
1130 BOOST_ASSERT(lyx_view_);
1133 istringstream is(argument);
1134 is >> file_name >> row;
1136 bool loaded = false;
1137 if (prefixIs(file_name, package().temp_dir().absFilename()))
1138 // Needed by inverse dvi search. If it is a file
1139 // in tmpdir, call the apropriated function
1140 buf = theBufferList().getBufferFromTmp(file_name);
1142 // Must replace extension of the file to be .lyx
1143 // and get full path
1144 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1145 // Either change buffer or load the file
1146 if (theBufferList().exists(s.absFilename()))
1147 buf = theBufferList().getBuffer(s.absFilename());
1149 buf = lyx_view_->loadDocument(s);
1155 updateFlags = Update::None;
1160 lyx_view_->setBuffer(buf);
1161 view()->setCursorFromRow(row);
1163 buf->errors("Parse");
1164 updateFlags = Update::FitCursor;
1169 case LFUN_DIALOG_SHOW_NEW_INSET: {
1170 BOOST_ASSERT(lyx_view_);
1171 string const name = cmd.getArg(0);
1172 InsetCode code = insetCode(name);
1173 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1174 bool insetCodeOK = true;
1183 case HYPERLINK_CODE: {
1184 InsetCommandParams p(code);
1185 data = InsetCommandMailer::params2string(name, p);
1188 case INCLUDE_CODE: {
1189 // data is the include type: one of "include",
1190 // "input", "verbatiminput" or "verbatiminput*"
1192 // default type is requested
1194 InsetCommandParams p(INCLUDE_CODE, data);
1195 data = InsetCommandMailer::params2string("include", p);
1199 // \c data == "Boxed" || "Frameless" etc
1200 InsetBoxParams p(data);
1201 data = InsetBoxMailer::params2string(p);
1205 InsetBranchParams p;
1206 data = InsetBranchMailer::params2string(p);
1210 InsetCommandParams p(CITE_CODE);
1211 data = InsetCommandMailer::params2string(name, p);
1215 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1218 case EXTERNAL_CODE: {
1219 InsetExternalParams p;
1220 Buffer const & buffer = *lyx_view_->buffer();
1221 data = InsetExternalMailer::params2string(p, buffer);
1226 data = InsetFloatMailer::params2string(p);
1229 case LISTINGS_CODE: {
1230 InsetListingsParams p;
1231 data = InsetListingsMailer::params2string(p);
1234 case GRAPHICS_CODE: {
1235 InsetGraphicsParams p;
1236 Buffer const & buffer = *lyx_view_->buffer();
1237 data = InsetGraphicsMailer::params2string(p, buffer);
1242 data = InsetNoteMailer::params2string(p);
1247 data = InsetVSpaceMailer::params2string(space);
1252 data = InsetWrapMailer::params2string(p);
1256 lyxerr << "Inset type '" << name <<
1257 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1258 insetCodeOK = false;
1260 } // end switch(code)
1262 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1266 case LFUN_CITATION_INSERT: {
1267 BOOST_ASSERT(lyx_view_);
1268 if (!argument.empty()) {
1269 // we can have one optional argument, delimited by '|'
1270 // citation-insert <key>|<text_before>
1271 // this should be enhanced to also support text_after
1272 // and citation style
1273 string arg = argument;
1275 if (contains(argument, "|")) {
1276 arg = token(argument, '|', 0);
1277 opt1 = token(argument, '|', 1);
1279 InsetCommandParams icp(CITE_CODE);
1280 icp["key"] = from_utf8(arg);
1282 icp["before"] = from_utf8(opt1);
1283 string icstr = InsetCommandMailer::params2string("citation", icp);
1284 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1287 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1291 case LFUN_BUFFER_CHILD_OPEN: {
1292 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1293 Buffer * parent = lyx_view_->buffer();
1294 FileName filename = makeAbsPath(argument, parent->filePath());
1295 view()->saveBookmark(false);
1297 bool parsed = false;
1298 if (theBufferList().exists(filename.absFilename())) {
1299 child = theBufferList().getBuffer(filename.absFilename());
1301 setMessage(bformat(_("Opening child document %1$s..."),
1302 makeDisplayPath(filename.absFilename())));
1303 child = lyx_view_->loadDocument(filename, false);
1307 // Set the parent name of the child document.
1308 // This makes insertion of citations and references in the child work,
1309 // when the target is in the parent or another child document.
1310 child->setParent(parent);
1311 updateLabels(*child->masterBuffer());
1312 lyx_view_->setBuffer(child);
1314 child->errors("Parse");
1317 // If a screen update is required (in case where auto_open is false),
1318 // setBuffer() would have taken care of it already. Otherwise we shall
1319 // reset the update flag because it can cause a circular problem.
1321 updateFlags = Update::None;
1325 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1326 BOOST_ASSERT(lyx_view_);
1327 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1330 case LFUN_KEYMAP_OFF:
1331 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1332 lyx_view_->view()->getIntl().keyMapOn(false);
1335 case LFUN_KEYMAP_PRIMARY:
1336 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1337 lyx_view_->view()->getIntl().keyMapPrim();
1340 case LFUN_KEYMAP_SECONDARY:
1341 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1342 lyx_view_->view()->getIntl().keyMapSec();
1345 case LFUN_KEYMAP_TOGGLE:
1346 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1347 lyx_view_->view()->getIntl().toggleKeyMap();
1353 string rest = split(argument, countstr, ' ');
1354 istringstream is(countstr);
1357 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1358 for (int i = 0; i < count; ++i)
1359 dispatch(lyxaction.lookupFunc(rest));
1363 case LFUN_COMMAND_SEQUENCE: {
1364 // argument contains ';'-terminated commands
1365 string arg = argument;
1366 while (!arg.empty()) {
1368 arg = split(arg, first, ';');
1369 FuncRequest func(lyxaction.lookupFunc(first));
1370 func.origin = cmd.origin;
1378 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1379 func.origin = cmd.origin;
1381 LyX::ref().topLevelCmdDef().release(argument);
1383 if (func.action == LFUN_UNKNOWN_ACTION) {
1384 // unknown command definition
1385 lyxerr << "Warning: unknown command definition `"
1389 // recursion detected
1390 lyxerr << "Warning: Recursion in the command definition `"
1391 << argument << "' detected"
1398 case LFUN_PREFERENCES_SAVE: {
1399 lyxrc.write(makeAbsPath("preferences",
1400 package().user_support().absFilename()),
1405 case LFUN_SET_COLOR: {
1407 string const x11_name = split(argument, lyx_name, ' ');
1408 if (lyx_name.empty() || x11_name.empty()) {
1409 setErrorMessage(from_ascii(N_(
1410 "Syntax: set-color <lyx_name>"
1415 bool const graphicsbg_changed =
1416 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1417 x11_name != lcolor.getX11Name(Color_graphicsbg));
1419 if (!lcolor.setColor(lyx_name, x11_name)) {
1421 bformat(_("Set-color \"%1$s\" failed "
1422 "- color is undefined or "
1423 "may not be redefined"),
1424 from_utf8(lyx_name)));
1428 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1430 if (graphicsbg_changed) {
1431 // FIXME: The graphics cache no longer has a changeDisplay method.
1433 graphics::GCache::get().changeDisplay(true);
1440 BOOST_ASSERT(lyx_view_);
1441 lyx_view_->message(from_utf8(argument));
1444 case LFUN_EXTERNAL_EDIT: {
1445 BOOST_ASSERT(lyx_view_);
1446 FuncRequest fr(action, argument);
1447 InsetExternal().dispatch(view()->cursor(), fr);
1451 case LFUN_GRAPHICS_EDIT: {
1452 FuncRequest fr(action, argument);
1453 InsetGraphics().dispatch(view()->cursor(), fr);
1457 case LFUN_ALL_INSETS_TOGGLE: {
1458 BOOST_ASSERT(lyx_view_);
1460 string const name = split(argument, action, ' ');
1461 InsetCode const inset_code = insetCode(name);
1463 Cursor & cur = view()->cursor();
1464 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1466 Inset & inset = lyx_view_->buffer()->inset();
1467 InsetIterator it = inset_iterator_begin(inset);
1468 InsetIterator const end = inset_iterator_end(inset);
1469 for (; it != end; ++it) {
1470 if (!it->asInsetMath()
1471 && (inset_code == NO_CODE
1472 || inset_code == it->lyxCode())) {
1473 Cursor tmpcur = cur;
1474 tmpcur.pushBackward(*it);
1475 it->dispatch(tmpcur, fr);
1478 updateFlags = Update::Force | Update::FitCursor;
1482 case LFUN_BUFFER_LANGUAGE: {
1483 BOOST_ASSERT(lyx_view_);
1484 Buffer & buffer = *lyx_view_->buffer();
1485 Language const * oldL = buffer.params().language;
1486 Language const * newL = languages.getLanguage(argument);
1487 if (!newL || oldL == newL)
1490 if (oldL->rightToLeft() == newL->rightToLeft()
1491 && !buffer.isMultiLingual())
1492 buffer.changeLanguage(oldL, newL);
1496 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1497 string const fname =
1498 addName(addPath(package().user_support().absFilename(), "templates/"),
1500 Buffer defaults(fname);
1502 istringstream ss(argument);
1505 int const unknown_tokens = defaults.readHeader(lex);
1507 if (unknown_tokens != 0) {
1508 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1509 << unknown_tokens << " unknown token"
1510 << (unknown_tokens == 1 ? "" : "s")
1514 if (defaults.writeFile(FileName(defaults.absFileName())))
1515 setMessage(bformat(_("Document defaults saved in %1$s"),
1516 makeDisplayPath(fname)));
1518 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1522 case LFUN_BUFFER_PARAMS_APPLY: {
1523 BOOST_ASSERT(lyx_view_);
1524 biblio::CiteEngine const oldEngine =
1525 lyx_view_->buffer()->params().getEngine();
1527 Buffer * buffer = lyx_view_->buffer();
1529 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1531 Cursor & cur = view()->cursor();
1532 cur.recordUndoFullDocument();
1534 istringstream ss(argument);
1537 int const unknown_tokens = buffer->readHeader(lex);
1539 if (unknown_tokens != 0) {
1540 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1541 << unknown_tokens << " unknown token"
1542 << (unknown_tokens == 1 ? "" : "s")
1546 updateLayout(oldClass, buffer);
1548 biblio::CiteEngine const newEngine =
1549 lyx_view_->buffer()->params().getEngine();
1551 if (oldEngine != newEngine) {
1552 FuncRequest fr(LFUN_INSET_REFRESH);
1554 Inset & inset = lyx_view_->buffer()->inset();
1555 InsetIterator it = inset_iterator_begin(inset);
1556 InsetIterator const end = inset_iterator_end(inset);
1557 for (; it != end; ++it)
1558 if (it->lyxCode() == CITE_CODE)
1559 it->dispatch(cur, fr);
1562 updateFlags = Update::Force | Update::FitCursor;
1563 // We are here most certainaly because of a change in the document
1564 // It is then better to make sure that all dialogs are in sync
1565 // with current document settings. LyXView::restartCursor() achieve this.
1566 lyx_view_->restartCursor();
1570 case LFUN_LAYOUT_MODULES_CLEAR: {
1571 BOOST_ASSERT(lyx_view_);
1572 Buffer * buffer = lyx_view_->buffer();
1573 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1574 view()->cursor().recordUndoFullDocument();
1575 buffer->params().clearLayoutModules();
1576 buffer->params().makeTextClass();
1577 updateLayout(oldClass, buffer);
1578 updateFlags = Update::Force | Update::FitCursor;
1582 case LFUN_LAYOUT_MODULE_ADD: {
1583 BOOST_ASSERT(lyx_view_);
1584 Buffer * buffer = lyx_view_->buffer();
1585 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1586 view()->cursor().recordUndoFullDocument();
1587 buffer->params().addLayoutModule(argument);
1588 buffer->params().makeTextClass();
1589 updateLayout(oldClass, buffer);
1590 updateFlags = Update::Force | Update::FitCursor;
1594 case LFUN_TEXTCLASS_APPLY: {
1595 BOOST_ASSERT(lyx_view_);
1596 Buffer * buffer = lyx_view_->buffer();
1598 loadTextClass(argument);
1600 pair<bool, textclass_type> const tc_pair =
1601 textclasslist.numberOfClass(argument);
1606 textclass_type const old_class = buffer->params().getBaseClass();
1607 textclass_type const new_class = tc_pair.second;
1609 if (old_class == new_class)
1613 //Save the old, possibly modular, layout for use in conversion.
1614 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1615 view()->cursor().recordUndoFullDocument();
1616 buffer->params().setBaseClass(new_class);
1617 buffer->params().makeTextClass();
1618 updateLayout(oldClass, buffer);
1619 updateFlags = Update::Force | Update::FitCursor;
1623 case LFUN_LAYOUT_RELOAD: {
1624 BOOST_ASSERT(lyx_view_);
1625 Buffer * buffer = lyx_view_->buffer();
1626 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1627 textclass_type const tc = buffer->params().getBaseClass();
1628 textclasslist.reset(tc);
1629 buffer->params().setBaseClass(tc);
1630 buffer->params().makeTextClass();
1631 updateLayout(oldClass, buffer);
1632 updateFlags = Update::Force | Update::FitCursor;
1636 case LFUN_TEXTCLASS_LOAD:
1637 loadTextClass(argument);
1640 case LFUN_LYXRC_APPLY: {
1641 LyXRC const lyxrc_orig = lyxrc;
1643 istringstream ss(argument);
1644 bool const success = lyxrc.read(ss) == 0;
1647 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1648 << "Unable to read lyxrc data"
1653 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1655 theApp()->resetGui();
1657 /// We force the redraw in any case because there might be
1658 /// some screen font changes.
1659 /// FIXME: only the current view will be updated. the Gui
1660 /// class is able to furnish the list of views.
1661 updateFlags = Update::Force;
1665 case LFUN_BOOKMARK_GOTO:
1666 // go to bookmark, open unopened file and switch to buffer if necessary
1667 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1668 updateFlags = Update::FitCursor;
1671 case LFUN_BOOKMARK_CLEAR:
1672 LyX::ref().session().bookmarks().clear();
1676 BOOST_ASSERT(theApp());
1677 // Let the frontend dispatch its own actions.
1678 if (theApp()->dispatch(cmd))
1679 // Nothing more to do.
1682 // Let the current LyXView dispatch its own actions.
1683 BOOST_ASSERT(lyx_view_);
1684 if (lyx_view_->dispatch(cmd)) {
1685 if (lyx_view_->view())
1686 updateFlags = lyx_view_->view()->cursor().result().update();
1690 BOOST_ASSERT(lyx_view_->view());
1691 // Let the current BufferView dispatch its own actions.
1692 if (view()->dispatch(cmd)) {
1693 // The BufferView took care of its own updates if needed.
1694 updateFlags = Update::None;
1698 // Let the current Cursor dispatch its own actions.
1699 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1700 cursorPosBeforeDispatchY_);
1701 view()->cursor().dispatch(cmd);
1702 updateFlags = view()->cursor().result().update();
1703 if (!view()->cursor().result().dispatched()) {
1704 // No update needed in this case (e.g. when reaching
1706 updateFlags = Update::None;
1710 if (lyx_view_ && lyx_view_->buffer()) {
1711 // BufferView::update() updates the ViewMetricsInfo and
1712 // also initializes the position cache for all insets in
1713 // (at least partially) visible top-level paragraphs.
1714 // We will redraw the screen only if needed.
1715 view()->processUpdateFlags(updateFlags);
1717 // if we executed a mutating lfun, mark the buffer as dirty
1719 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1720 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1721 lyx_view_->buffer()->markDirty();
1723 //Do we have a selection?
1724 theSelection().haveSelection(view()->cursor().selection());
1727 if (!quitting && lyx_view_) {
1728 // Some messages may already be translated, so we cannot use _()
1729 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1734 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1736 const bool verbose = (cmd.origin == FuncRequest::MENU
1737 || cmd.origin == FuncRequest::TOOLBAR
1738 || cmd.origin == FuncRequest::COMMANDBUFFER);
1740 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1741 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1743 lyx_view_->message(msg);
1747 docstring dispatch_msg = msg;
1748 if (!dispatch_msg.empty())
1749 dispatch_msg += ' ';
1751 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1753 bool argsadded = false;
1755 if (!cmd.argument().empty()) {
1756 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1757 comname += ' ' + cmd.argument();
1762 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1764 if (!shortcuts.empty())
1765 comname += ": " + shortcuts;
1766 else if (!argsadded && !cmd.argument().empty())
1767 comname += ' ' + cmd.argument();
1769 if (!comname.empty()) {
1770 comname = rtrim(comname);
1771 dispatch_msg += '(' + rtrim(comname) + ')';
1774 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1775 if (!dispatch_msg.empty())
1776 lyx_view_->message(dispatch_msg);
1780 void LyXFunc::closeBuffer()
1782 // goto bookmark to update bookmark pit.
1783 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1784 gotoBookmark(i+1, false, false);
1786 lyx_view_->closeBuffer();
1790 void LyXFunc::reloadBuffer()
1792 FileName filename = lyx_view_->buffer()->fileName();
1793 // The user has already confirmed that the changes, if any, should
1794 // be discarded. So we just release the Buffer and don't call closeBuffer();
1795 theBufferList().release(lyx_view_->buffer());
1796 Buffer * buf = lyx_view_->loadDocument(filename);
1797 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1801 lyx_view_->setBuffer(buf);
1802 buf->errors("Parse");
1803 str = bformat(_("Document %1$s reloaded."), disp_fn);
1805 str = bformat(_("Could not reload document %1$s"), disp_fn);
1807 lyx_view_->message(str);
1810 // Each "lyx_view_" should have it's own message method. lyxview and
1811 // the minibuffer would use the minibuffer, but lyxserver would
1812 // send an ERROR signal to its client. Alejandro 970603
1813 // This function is bit problematic when it comes to NLS, to make the
1814 // lyx servers client be language indepenent we must not translate
1815 // strings sent to this func.
1816 void LyXFunc::setErrorMessage(docstring const & m) const
1818 dispatch_buffer = m;
1823 void LyXFunc::setMessage(docstring const & m) const
1825 dispatch_buffer = m;
1829 docstring const LyXFunc::viewStatusMessage()
1831 // When meta-fake key is pressed, show the key sequence so far + "M-".
1833 return keyseq.print(KeySequence::ForGui) + "M-";
1835 // Else, when a non-complete key sequence is pressed,
1836 // show the available options.
1837 if (keyseq.length() > 0 && !keyseq.deleted())
1838 return keyseq.printOptions(true);
1840 BOOST_ASSERT(lyx_view_);
1841 if (!lyx_view_->buffer())
1842 return _("Welcome to LyX!");
1844 return view()->cursor().currentState();
1848 BufferView * LyXFunc::view() const
1850 BOOST_ASSERT(lyx_view_);
1851 return lyx_view_->view();
1855 bool LyXFunc::wasMetaKey() const
1857 return (meta_fake_bit != NoModifier);
1861 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1864 lyx_view_->message(_("Converting document to new document class..."));
1866 StableDocIterator backcur(view()->cursor());
1867 ErrorList & el = buffer->errorList("Class Switch");
1868 cap::switchBetweenClasses(
1869 oldlayout, buffer->params().getTextClassPtr(),
1870 static_cast<InsetText &>(buffer->inset()), el);
1872 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1874 buffer->errors("Class Switch");
1875 updateLabels(*buffer);
1881 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1883 // Why the switch you might ask. It is a trick to ensure that all
1884 // the elements in the LyXRCTags enum is handled. As you can see
1885 // there are no breaks at all. So it is just a huge fall-through.
1886 // The nice thing is that we will get a warning from the compiler
1887 // if we forget an element.
1888 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1890 case LyXRC::RC_ACCEPT_COMPOUND:
1891 case LyXRC::RC_ALT_LANG:
1892 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1893 case LyXRC::RC_PLAINTEXT_LINELEN:
1894 case LyXRC::RC_AUTOREGIONDELETE:
1895 case LyXRC::RC_AUTORESET_OPTIONS:
1896 case LyXRC::RC_AUTOSAVE:
1897 case LyXRC::RC_AUTO_NUMBER:
1898 case LyXRC::RC_BACKUPDIR_PATH:
1899 case LyXRC::RC_BIBTEX_COMMAND:
1900 case LyXRC::RC_BINDFILE:
1901 case LyXRC::RC_CHECKLASTFILES:
1902 case LyXRC::RC_USELASTFILEPOS:
1903 case LyXRC::RC_LOADSESSION:
1904 case LyXRC::RC_CHKTEX_COMMAND:
1905 case LyXRC::RC_CONVERTER:
1906 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1907 case LyXRC::RC_COPIER:
1908 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1909 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1910 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1911 case LyXRC::RC_DATE_INSERT_FORMAT:
1912 case LyXRC::RC_DEFAULT_LANGUAGE:
1913 case LyXRC::RC_DEFAULT_PAPERSIZE:
1914 case LyXRC::RC_DEFFILE:
1915 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1916 case LyXRC::RC_DISPLAY_GRAPHICS:
1917 case LyXRC::RC_DOCUMENTPATH:
1918 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1919 FileName path(lyxrc_new.document_path);
1920 if (path.exists() && path.isDirectory())
1921 package().document_dir() = FileName(lyxrc.document_path);
1923 case LyXRC::RC_ESC_CHARS:
1924 case LyXRC::RC_EXAMPLEPATH:
1925 case LyXRC::RC_FONT_ENCODING:
1926 case LyXRC::RC_FORMAT:
1927 case LyXRC::RC_INDEX_COMMAND:
1928 case LyXRC::RC_INPUT:
1929 case LyXRC::RC_KBMAP:
1930 case LyXRC::RC_KBMAP_PRIMARY:
1931 case LyXRC::RC_KBMAP_SECONDARY:
1932 case LyXRC::RC_LABEL_INIT_LENGTH:
1933 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1934 case LyXRC::RC_LANGUAGE_AUTO_END:
1935 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1936 case LyXRC::RC_LANGUAGE_COMMAND_END:
1937 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1938 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1939 case LyXRC::RC_LANGUAGE_PACKAGE:
1940 case LyXRC::RC_LANGUAGE_USE_BABEL:
1941 case LyXRC::RC_MACRO_EDIT_STYLE:
1942 case LyXRC::RC_MAKE_BACKUP:
1943 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1944 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1945 case LyXRC::RC_NUMLASTFILES:
1946 case LyXRC::RC_PATH_PREFIX:
1947 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1948 prependEnvPath("PATH", lyxrc.path_prefix);
1950 case LyXRC::RC_PERS_DICT:
1951 case LyXRC::RC_PREVIEW:
1952 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1953 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1954 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1955 case LyXRC::RC_PRINTCOPIESFLAG:
1956 case LyXRC::RC_PRINTER:
1957 case LyXRC::RC_PRINTEVENPAGEFLAG:
1958 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1959 case LyXRC::RC_PRINTFILEEXTENSION:
1960 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1961 case LyXRC::RC_PRINTODDPAGEFLAG:
1962 case LyXRC::RC_PRINTPAGERANGEFLAG:
1963 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1964 case LyXRC::RC_PRINTPAPERFLAG:
1965 case LyXRC::RC_PRINTREVERSEFLAG:
1966 case LyXRC::RC_PRINTSPOOL_COMMAND:
1967 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1968 case LyXRC::RC_PRINTTOFILE:
1969 case LyXRC::RC_PRINTTOPRINTER:
1970 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1971 case LyXRC::RC_PRINT_COMMAND:
1972 case LyXRC::RC_RTL_SUPPORT:
1973 case LyXRC::RC_SCREEN_DPI:
1974 case LyXRC::RC_SCREEN_FONT_ROMAN:
1975 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1976 case LyXRC::RC_SCREEN_FONT_SANS:
1977 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1978 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1979 case LyXRC::RC_SCREEN_FONT_SIZES:
1980 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1981 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1982 case LyXRC::RC_GEOMETRY_SESSION:
1983 case LyXRC::RC_SCREEN_ZOOM:
1984 case LyXRC::RC_SERVERPIPE:
1985 case LyXRC::RC_SET_COLOR:
1986 case LyXRC::RC_SHOW_BANNER:
1987 case LyXRC::RC_SPELL_COMMAND:
1988 case LyXRC::RC_TEMPDIRPATH:
1989 case LyXRC::RC_TEMPLATEPATH:
1990 case LyXRC::RC_TEX_ALLOWS_SPACES:
1991 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1992 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1993 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1995 case LyXRC::RC_UIFILE:
1996 case LyXRC::RC_USER_EMAIL:
1997 case LyXRC::RC_USER_NAME:
1998 case LyXRC::RC_USETEMPDIR:
1999 case LyXRC::RC_USE_ALT_LANG:
2000 case LyXRC::RC_USE_CONVERTER_CACHE:
2001 case LyXRC::RC_USE_ESC_CHARS:
2002 case LyXRC::RC_USE_INP_ENC:
2003 case LyXRC::RC_USE_PERS_DICT:
2004 case LyXRC::RC_USE_TOOLTIP:
2005 case LyXRC::RC_USE_PIXMAP_CACHE:
2006 case LyXRC::RC_USE_SPELL_LIB:
2007 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2008 case LyXRC::RC_SORT_LAYOUTS:
2009 case LyXRC::RC_VIEWER:
2010 case LyXRC::RC_LAST: