3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "LayoutFile.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetListings.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetNote.h"
69 #include "insets/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.
112 lv->message(_("Running configure..."));
114 // Run configure in user lyx directory
115 PathChanger p(package().user_support());
116 string configure_command = package().configure_command();
117 configure_command += option;
119 int ret = one.startscript(Systemcall::Wait, configure_command);
121 // emit message signal.
123 lv->message(_("Reloading configuration..."));
124 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
125 // Re-read packages.lst
126 LaTeXFeatures::getAvailable();
129 Alert::information(_("System reconfiguration failed"),
130 _("The system reconfiguration has failed.\n"
131 "Default textclass is used but LyX may "
132 "not be able to work properly.\n"
133 "Please reconfigure again if needed."));
136 Alert::information(_("System reconfigured"),
137 _("The system has been reconfigured.\n"
138 "You need to restart LyX to make use of any\n"
139 "updated document class specifications."));
143 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
145 // Try to fix cursor in case it is broken.
146 cursor.fixIfBroken();
148 // This is, of course, a mess. Better create a new doc iterator and use
149 // this in Inset::getStatus. This might require an additional
150 // BufferView * arg, though (which should be avoided)
151 //Cursor safe = *this;
153 for ( ; cursor.depth(); cursor.pop()) {
154 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
155 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
156 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
157 BOOST_ASSERT(cursor.pos() <= cursor.lastpos());
159 // The inset's getStatus() will return 'true' if it made
160 // a definitive decision on whether it want to handle the
161 // request or not. The result of this decision is put into
162 // the 'status' parameter.
163 if (cursor.inset().getStatus(cursor, cmd, status)) {
172 /** Return the change status at cursor position, taking in account the
173 * status at each level of the document iterator (a table in a deleted
174 * footnote is deleted).
175 * When \param outer is true, the top slice is not looked at.
177 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
179 size_t const depth = dit.depth() - (outer ? 1 : 0);
181 for (size_t i = 0 ; i < depth ; ++i) {
182 CursorSlice const & slice = dit[i];
183 if (!slice.inset().inMathed()
184 && slice.pos() < slice.paragraph().size()) {
185 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
186 if (ch != Change::UNCHANGED)
190 return Change::UNCHANGED;
197 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
202 void LyXFunc::initKeySequences(KeyMap * kb)
204 keyseq = KeySequence(kb, kb);
205 cancel_meta_seq = KeySequence(kb, kb);
209 void LyXFunc::setLyXView(LyXView * lv)
211 if (lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
212 // save current selection to the selection buffer to allow
213 // middle-button paste in another window
214 cap::saveSelection(lyx_view_->view()->cursor());
219 void LyXFunc::handleKeyFunc(kb_action action)
221 char_type c = encoded_last_key;
226 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
227 lyx_view_->view()->getIntl().getTransManager().deadkey(
228 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
229 // Need to clear, in case the minibuffer calls these
232 // copied verbatim from do_accent_char
233 view()->cursor().resetAnchor();
234 view()->processUpdateFlags(Update::FitCursor);
237 //FIXME: bookmark handling is a frontend issue. This code should be transferred
238 // to GuiView and be GuiView and be window dependent.
239 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
241 BOOST_ASSERT(lyx_view_);
242 if (!LyX::ref().session().bookmarks().isValid(idx))
244 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
245 BOOST_ASSERT(!bm.filename.empty());
246 string const file = bm.filename.absFilename();
247 // if the file is not opened, open it.
248 if (!theBufferList().exists(file)) {
250 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
254 // open may fail, so we need to test it again
255 if (!theBufferList().exists(file))
258 // if the current buffer is not that one, switch to it.
259 if (lyx_view_->buffer()->absFileName() != file) {
262 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
264 // moveToPosition try paragraph id first and then paragraph (pit, pos).
265 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
266 bm.top_id, bm.top_pos))
269 // Cursor jump succeeded!
270 Cursor const & cur = view()->cursor();
271 pit_type new_pit = cur.pit();
272 pos_type new_pos = cur.pos();
273 int new_id = cur.paragraph().id();
275 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
276 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
277 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
278 || bm.top_id != new_id) {
279 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
280 new_pit, new_pos, new_id);
285 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
287 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
289 // Do nothing if we have nothing (JMarc)
290 if (!keysym.isOK()) {
291 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
292 lyx_view_->restartCursor();
296 if (keysym.isModifier()) {
297 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) << "']");
381 lyx_view_->restartCursor();
385 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
387 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
390 Buffer * buf = lyx_view_ ? lyx_view_->buffer() : 0;
392 if (cmd.action == LFUN_NOACTION) {
393 flag.message(from_utf8(N_("Nothing to do")));
398 switch (cmd.action) {
399 case LFUN_UNKNOWN_ACTION:
400 #ifndef HAVE_LIBAIKSAURUS
401 case LFUN_THESAURUS_ENTRY:
411 if (flag.unknown()) {
412 flag.message(from_utf8(N_("Unknown action")));
416 if (!flag.enabled()) {
417 if (flag.message().empty())
418 flag.message(from_utf8(N_("Command disabled")));
422 // Check whether we need a buffer
423 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
425 flag.message(from_utf8(N_("Command not allowed with"
426 "out any document open")));
431 // I would really like to avoid having this switch and rather try to
432 // encode this in the function itself.
433 // -- And I'd rather let an inset decide which LFUNs it is willing
434 // to handle (Andre')
436 switch (cmd.action) {
438 // FIXME: these cases should be hidden in GuiApplication::getStatus().
439 case LFUN_WINDOW_CLOSE:
441 return theApp()->getStatus(cmd);
445 // FIXME: these cases should be hidden in GuiView::getStatus().
446 case LFUN_DIALOG_TOGGLE:
447 case LFUN_DIALOG_SHOW:
448 case LFUN_DIALOG_UPDATE:
449 if (cmd.argument() == "prefs"
450 || cmd.argument() == "aboutlyx")
453 return lyx_view_->getStatus(cmd);
458 case LFUN_TOOLBAR_TOGGLE:
459 case LFUN_INSET_APPLY:
460 case LFUN_BUFFER_WRITE:
461 case LFUN_BUFFER_WRITE_AS:
462 case LFUN_SPLIT_VIEW:
463 case LFUN_CLOSE_TAB_GROUP:
464 case LFUN_COMPLETION_POPUP:
465 case LFUN_COMPLETION_INLINE:
466 case LFUN_COMPLETION_COMPLETE:
468 return lyx_view_->getStatus(cmd);
472 case LFUN_BUFFER_TOGGLE_READ_ONLY:
473 flag.setOnOff(buf->isReadonly());
476 case LFUN_BUFFER_SWITCH:
477 // toggle on the current buffer, but do not toggle off
478 // the other ones (is that a good idea?)
479 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
483 case LFUN_BUFFER_EXPORT:
484 enable = cmd.argument() == "custom"
485 || buf->isExportable(to_utf8(cmd.argument()));
488 case LFUN_BUFFER_CHKTEX:
489 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
492 case LFUN_BUILD_PROGRAM:
493 enable = buf->isExportable("program");
496 case LFUN_VC_REGISTER:
497 enable = !buf->lyxvc().inUse();
499 case LFUN_VC_CHECK_IN:
500 enable = buf->lyxvc().inUse() && !buf->isReadonly();
502 case LFUN_VC_CHECK_OUT:
503 enable = buf->lyxvc().inUse() && buf->isReadonly();
506 case LFUN_VC_UNDO_LAST:
507 enable = buf->lyxvc().inUse();
509 case LFUN_BUFFER_RELOAD:
510 enable = !buf->isUnnamed() && buf->fileName().exists()
511 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
514 case LFUN_CITATION_INSERT: {
515 FuncRequest fr(LFUN_INSET_INSERT, "citation");
516 enable = getStatus(fr).enabled();
520 // This could be used for the no-GUI version. The GUI version is handled in
521 // LyXView::getStatus(). See above.
523 case LFUN_BUFFER_WRITE:
524 case LFUN_BUFFER_WRITE_AS: {
525 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
526 enable = b && (b->isUnnamed() || !b->isClean());
531 case LFUN_BUFFER_WRITE_ALL: {
532 // We enable the command only if there are some modified buffers
533 Buffer * first = theBufferList().first();
538 // We cannot use a for loop as the buffer list is a cycle.
544 b = theBufferList().next(b);
545 } while (b != first);
549 case LFUN_BOOKMARK_GOTO: {
550 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
551 enable = LyX::ref().session().bookmarks().isValid(num);
555 case LFUN_BOOKMARK_CLEAR:
556 enable = LyX::ref().session().bookmarks().size() > 0;
559 // this one is difficult to get right. As a half-baked
560 // solution, we consider only the first action of the sequence
561 case LFUN_COMMAND_SEQUENCE: {
562 // argument contains ';'-terminated commands
563 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
564 FuncRequest func(lyxaction.lookupFunc(firstcmd));
565 func.origin = cmd.origin;
566 flag = getStatus(func);
572 string name = to_utf8(cmd.argument());
573 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
574 func.origin = cmd.origin;
575 flag = getStatus(func);
576 LyX::ref().topLevelCmdDef().release(name);
578 // catch recursion or unknown command definiton
579 // all operations until the recursion or unknown command
580 // definiton occures are performed, so set the state to enabled
586 case LFUN_BUFFER_NEW:
587 case LFUN_BUFFER_NEW_TEMPLATE:
588 case LFUN_WORD_FIND_FORWARD:
589 case LFUN_WORD_FIND_BACKWARD:
590 case LFUN_COMMAND_PREFIX:
591 case LFUN_COMMAND_EXECUTE:
593 case LFUN_META_PREFIX:
594 case LFUN_BUFFER_CLOSE:
595 case LFUN_BUFFER_UPDATE:
596 case LFUN_BUFFER_VIEW:
597 case LFUN_MASTER_BUFFER_UPDATE:
598 case LFUN_MASTER_BUFFER_VIEW:
599 case LFUN_BUFFER_IMPORT:
600 case LFUN_BUFFER_AUTO_SAVE:
601 case LFUN_RECONFIGURE:
604 case LFUN_DROP_LAYOUTS_CHOICE:
606 case LFUN_SERVER_GET_NAME:
607 case LFUN_SERVER_NOTIFY:
608 case LFUN_SERVER_GOTO_FILE_ROW:
609 case LFUN_DIALOG_HIDE:
610 case LFUN_DIALOG_DISCONNECT_INSET:
611 case LFUN_BUFFER_CHILD_OPEN:
613 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
614 case LFUN_KEYMAP_OFF:
615 case LFUN_KEYMAP_PRIMARY:
616 case LFUN_KEYMAP_SECONDARY:
617 case LFUN_KEYMAP_TOGGLE:
619 case LFUN_BUFFER_EXPORT_CUSTOM:
620 case LFUN_BUFFER_PRINT:
621 case LFUN_PREFERENCES_SAVE:
622 case LFUN_SCREEN_FONT_UPDATE:
625 case LFUN_EXTERNAL_EDIT:
626 case LFUN_GRAPHICS_EDIT:
627 case LFUN_ALL_INSETS_TOGGLE:
628 case LFUN_BUFFER_LANGUAGE:
629 case LFUN_TEXTCLASS_APPLY:
630 case LFUN_TEXTCLASS_LOAD:
631 case LFUN_BUFFER_SAVE_AS_DEFAULT:
632 case LFUN_BUFFER_PARAMS_APPLY:
633 case LFUN_LAYOUT_MODULES_CLEAR:
634 case LFUN_LAYOUT_MODULE_ADD:
635 case LFUN_LAYOUT_RELOAD:
636 case LFUN_LYXRC_APPLY:
637 case LFUN_BUFFER_NEXT:
638 case LFUN_BUFFER_PREVIOUS:
639 case LFUN_WINDOW_NEW:
641 // these are handled in our dispatch()
649 if (!getLocalStatus(view()->cursor(), cmd, flag))
650 flag = view()->getStatus(cmd);
656 // Can we use a readonly buffer?
657 if (buf && buf->isReadonly()
658 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
659 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
660 flag.message(from_utf8(N_("Document is read-only")));
664 // Are we in a DELETED change-tracking region?
666 && lookupChangeType(view()->cursor(), true) == Change::DELETED
667 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
668 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
669 flag.message(from_utf8(N_("This portion of the document is deleted.")));
673 // the default error message if we disable the command
674 if (!flag.enabled() && flag.message().empty())
675 flag.message(from_utf8(N_("Command disabled")));
681 bool LyXFunc::ensureBufferClean(BufferView * bv)
683 Buffer & buf = bv->buffer();
687 docstring const file = buf.fileName().displayName(30);
688 docstring text = bformat(_("The document %1$s has unsaved "
689 "changes.\n\nDo you want to save "
690 "the document?"), file);
691 int const ret = Alert::prompt(_("Save changed document?"),
692 text, 0, 1, _("&Save"),
696 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
698 return buf.isClean();
704 void showPrintError(string const & name)
706 docstring str = bformat(_("Could not print the document %1$s.\n"
707 "Check that your printer is set up correctly."),
708 makeDisplayPath(name, 50));
709 Alert::error(_("Print document failed"), str);
713 bool loadLayoutFile(string const & name, string const & buf_path)
715 if (!LayoutFileList::get().haveClass(name)) {
716 lyxerr << "Document class \"" << name
717 << "\" does not exist."
722 LayoutFile & tc = LayoutFileList::get()[name];
723 if (!tc.load(buf_path)) {
724 docstring s = bformat(_("The document class %1$s."
725 "could not be loaded."), from_utf8(name));
726 Alert::error(_("Could not load class"), s);
733 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
738 void LyXFunc::dispatch(FuncRequest const & cmd)
740 string const argument = to_utf8(cmd.argument());
741 kb_action const action = cmd.action;
743 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
744 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
746 // we have not done anything wrong yet.
748 dispatch_buffer.erase();
750 // redraw the screen at the end (first of the two drawing steps).
751 //This is done unless explicitely requested otherwise
752 Update::flags updateFlags = Update::FitCursor;
754 FuncStatus const flag = getStatus(cmd);
755 if (!flag.enabled()) {
756 // We cannot use this function here
757 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
758 << lyxaction.getActionName(action)
759 << " [" << action << "] is disabled at this location");
760 setErrorMessage(flag.message());
764 case LFUN_WORD_FIND_FORWARD:
765 case LFUN_WORD_FIND_BACKWARD: {
766 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
767 static docstring last_search;
768 docstring searched_string;
770 if (!cmd.argument().empty()) {
771 last_search = cmd.argument();
772 searched_string = cmd.argument();
774 searched_string = last_search;
777 if (searched_string.empty())
780 bool const fw = action == LFUN_WORD_FIND_FORWARD;
781 docstring const data =
782 find2string(searched_string, true, false, fw);
783 find(view(), FuncRequest(LFUN_WORD_FIND, data));
787 case LFUN_COMMAND_PREFIX:
788 BOOST_ASSERT(lyx_view_);
789 lyx_view_->message(keyseq.printOptions(true));
793 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
795 meta_fake_bit = NoModifier;
796 if (lyx_view_->buffer())
797 // cancel any selection
798 dispatch(FuncRequest(LFUN_MARK_OFF));
799 setMessage(from_ascii(N_("Cancel")));
802 case LFUN_META_PREFIX:
803 meta_fake_bit = AltModifier;
804 setMessage(keyseq.print(KeySequence::ForGui));
807 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
808 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
809 Buffer * buf = lyx_view_->buffer();
810 if (buf->lyxvc().inUse())
811 buf->lyxvc().toggleReadOnly();
813 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
817 // --- Menus -----------------------------------------------
818 case LFUN_BUFFER_CLOSE:
819 lyx_view_->closeBuffer();
820 updateFlags = Update::None;
823 case LFUN_BUFFER_RELOAD: {
824 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
825 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
826 docstring text = bformat(_("Any changes will be lost. Are you sure "
827 "you want to revert to the saved version of the document %1$s?"), file);
828 int const ret = Alert::prompt(_("Revert to saved document?"),
829 text, 1, 1, _("&Revert"), _("&Cancel"));
836 case LFUN_BUFFER_UPDATE:
837 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
838 lyx_view_->buffer()->doExport(argument, true);
841 case LFUN_BUFFER_VIEW:
842 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
843 lyx_view_->buffer()->preview(argument);
846 case LFUN_MASTER_BUFFER_UPDATE:
847 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
848 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
851 case LFUN_MASTER_BUFFER_VIEW:
852 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
853 lyx_view_->buffer()->masterBuffer()->preview(argument);
856 case LFUN_BUILD_PROGRAM:
857 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
858 lyx_view_->buffer()->doExport("program", true);
861 case LFUN_BUFFER_CHKTEX:
862 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
863 lyx_view_->buffer()->runChktex();
866 case LFUN_BUFFER_EXPORT:
867 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
868 if (argument == "custom")
869 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
871 lyx_view_->buffer()->doExport(argument, false);
874 case LFUN_BUFFER_EXPORT_CUSTOM: {
875 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
877 string command = split(argument, format_name, ' ');
878 Format const * format = formats.getFormat(format_name);
880 lyxerr << "Format \"" << format_name
881 << "\" not recognized!"
886 Buffer * buffer = lyx_view_->buffer();
888 // The name of the file created by the conversion process
891 // Output to filename
892 if (format->name() == "lyx") {
893 string const latexname = buffer->latexName(false);
894 filename = changeExtension(latexname,
895 format->extension());
896 filename = addName(buffer->temppath(), filename);
898 if (!buffer->writeFile(FileName(filename)))
902 buffer->doExport(format_name, true, filename);
905 // Substitute $$FName for filename
906 if (!contains(command, "$$FName"))
907 command = "( " + command + " ) < $$FName";
908 command = subst(command, "$$FName", filename);
910 // Execute the command in the background
912 call.startscript(Systemcall::DontWait, command);
916 case LFUN_BUFFER_PRINT: {
917 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
918 // FIXME: cmd.getArg() might fail if one of the arguments
919 // contains double quotes
920 string target = cmd.getArg(0);
921 string target_name = cmd.getArg(1);
922 string command = cmd.getArg(2);
925 || target_name.empty()
926 || command.empty()) {
927 lyxerr << "Unable to parse \""
928 << argument << '"' << endl;
931 if (target != "printer" && target != "file") {
932 lyxerr << "Unrecognized target \""
933 << target << '"' << endl;
937 Buffer * buffer = lyx_view_->buffer();
939 if (!buffer->doExport("dvi", true)) {
940 showPrintError(buffer->absFileName());
944 // Push directory path.
945 string const path = buffer->temppath();
946 // Prevent the compiler from optimizing away p
950 // there are three cases here:
951 // 1. we print to a file
952 // 2. we print directly to a printer
953 // 3. we print using a spool command (print to file first)
956 string const dviname =
957 changeExtension(buffer->latexName(true), "dvi");
959 if (target == "printer") {
960 if (!lyxrc.print_spool_command.empty()) {
961 // case 3: print using a spool
962 string const psname =
963 changeExtension(dviname,".ps");
964 command += ' ' + lyxrc.print_to_file
967 + quoteName(dviname);
970 lyxrc.print_spool_command + ' ';
971 if (target_name != "default") {
972 command2 += lyxrc.print_spool_printerprefix
976 command2 += quoteName(psname);
978 // If successful, then spool command
979 res = one.startscript(
984 res = one.startscript(
985 Systemcall::DontWait,
988 // case 2: print directly to a printer
989 if (target_name != "default")
990 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
991 res = one.startscript(
992 Systemcall::DontWait,
993 command + quoteName(dviname));
997 // case 1: print to a file
998 FileName const filename(makeAbsPath(target_name,
999 lyx_view_->buffer()->filePath()));
1000 FileName const dvifile(makeAbsPath(dviname, path));
1001 if (filename.exists()) {
1002 docstring text = bformat(
1003 _("The file %1$s already exists.\n\n"
1004 "Do you want to overwrite that file?"),
1005 makeDisplayPath(filename.absFilename()));
1006 if (Alert::prompt(_("Overwrite file?"),
1007 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1010 command += ' ' + lyxrc.print_to_file
1011 + quoteName(filename.toFilesystemEncoding())
1013 + quoteName(dvifile.toFilesystemEncoding());
1014 res = one.startscript(Systemcall::DontWait,
1019 showPrintError(buffer->absFileName());
1023 // FIXME: There is need for a command-line import.
1025 case LFUN_BUFFER_IMPORT:
1030 case LFUN_BUFFER_AUTO_SAVE:
1031 lyx_view_->buffer()->autoSave();
1034 case LFUN_RECONFIGURE:
1035 // argument is any additional parameter to the configure.py command
1036 reconfigure(lyx_view_, argument);
1039 case LFUN_HELP_OPEN: {
1040 BOOST_ASSERT(lyx_view_);
1041 string const arg = argument;
1043 setErrorMessage(from_ascii(N_("Missing argument")));
1046 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1047 if (fname.empty()) {
1048 lyxerr << "LyX: unable to find documentation file `"
1049 << arg << "'. Bad installation?" << endl;
1052 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1053 makeDisplayPath(fname.absFilename())));
1054 Buffer * buf = lyx_view_->loadDocument(fname, false);
1057 lyx_view_->setBuffer(buf);
1058 buf->errors("Parse");
1060 updateFlags = Update::None;
1064 // --- version control -------------------------------
1065 case LFUN_VC_REGISTER:
1066 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1067 if (!ensureBufferClean(view()))
1069 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1070 lyx_view_->buffer()->lyxvc().registrer();
1073 updateFlags = Update::Force;
1076 case LFUN_VC_CHECK_IN:
1077 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1078 if (!ensureBufferClean(view()))
1080 if (lyx_view_->buffer()->lyxvc().inUse()
1081 && !lyx_view_->buffer()->isReadonly()) {
1082 lyx_view_->buffer()->lyxvc().checkIn();
1087 case LFUN_VC_CHECK_OUT:
1088 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1089 if (!ensureBufferClean(view()))
1091 if (lyx_view_->buffer()->lyxvc().inUse()
1092 && lyx_view_->buffer()->isReadonly()) {
1093 lyx_view_->buffer()->lyxvc().checkOut();
1098 case LFUN_VC_REVERT:
1099 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1100 lyx_view_->buffer()->lyxvc().revert();
1104 case LFUN_VC_UNDO_LAST:
1105 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1106 lyx_view_->buffer()->lyxvc().undoLast();
1110 // --- lyxserver commands ----------------------------
1111 case LFUN_SERVER_GET_NAME:
1112 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1113 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1114 LYXERR(Debug::INFO, "FNAME["
1115 << lyx_view_->buffer()->absFileName() << ']');
1118 case LFUN_SERVER_NOTIFY:
1119 dispatch_buffer = keyseq.print(KeySequence::Portable);
1120 theServer().notifyClient(to_utf8(dispatch_buffer));
1123 case LFUN_SERVER_GOTO_FILE_ROW: {
1124 BOOST_ASSERT(lyx_view_);
1127 istringstream is(argument);
1128 is >> file_name >> row;
1130 bool loaded = false;
1131 if (prefixIs(file_name, package().temp_dir().absFilename()))
1132 // Needed by inverse dvi search. If it is a file
1133 // in tmpdir, call the apropriated function
1134 buf = theBufferList().getBufferFromTmp(file_name);
1136 // Must replace extension of the file to be .lyx
1137 // and get full path
1138 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1139 // Either change buffer or load the file
1140 if (theBufferList().exists(s.absFilename()))
1141 buf = theBufferList().getBuffer(s.absFilename());
1143 buf = lyx_view_->loadDocument(s);
1149 updateFlags = Update::None;
1154 lyx_view_->setBuffer(buf);
1155 view()->setCursorFromRow(row);
1157 buf->errors("Parse");
1158 updateFlags = Update::FitCursor;
1163 case LFUN_DIALOG_SHOW_NEW_INSET: {
1164 BOOST_ASSERT(lyx_view_);
1165 string const name = cmd.getArg(0);
1166 InsetCode code = insetCode(name);
1167 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1168 bool insetCodeOK = true;
1177 case HYPERLINK_CODE: {
1178 InsetCommandParams p(code);
1179 data = InsetCommandMailer::params2string(name, p);
1182 case INCLUDE_CODE: {
1183 // data is the include type: one of "include",
1184 // "input", "verbatiminput" or "verbatiminput*"
1186 // default type is requested
1188 InsetCommandParams p(INCLUDE_CODE, data);
1189 data = InsetCommandMailer::params2string("include", p);
1193 // \c data == "Boxed" || "Frameless" etc
1194 InsetBoxParams p(data);
1195 data = InsetBoxMailer::params2string(p);
1199 InsetBranchParams p;
1200 data = InsetBranchMailer::params2string(p);
1204 InsetCommandParams p(CITE_CODE);
1205 data = InsetCommandMailer::params2string(name, p);
1209 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1212 case EXTERNAL_CODE: {
1213 InsetExternalParams p;
1214 Buffer const & buffer = *lyx_view_->buffer();
1215 data = InsetExternalMailer::params2string(p, buffer);
1220 data = InsetFloatMailer::params2string(p);
1223 case LISTINGS_CODE: {
1224 InsetListingsParams p;
1225 data = InsetListingsMailer::params2string(p);
1228 case GRAPHICS_CODE: {
1229 InsetGraphicsParams p;
1230 Buffer const & buffer = *lyx_view_->buffer();
1231 data = InsetGraphicsMailer::params2string(p, buffer);
1236 data = InsetNoteMailer::params2string(p);
1241 data = InsetVSpaceMailer::params2string(space);
1246 data = InsetWrapMailer::params2string(p);
1250 lyxerr << "Inset type '" << name <<
1251 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1252 insetCodeOK = false;
1254 } // end switch(code)
1256 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1260 case LFUN_CITATION_INSERT: {
1261 BOOST_ASSERT(lyx_view_);
1262 if (!argument.empty()) {
1263 // we can have one optional argument, delimited by '|'
1264 // citation-insert <key>|<text_before>
1265 // this should be enhanced to also support text_after
1266 // and citation style
1267 string arg = argument;
1269 if (contains(argument, "|")) {
1270 arg = token(argument, '|', 0);
1271 opt1 = token(argument, '|', 1);
1273 InsetCommandParams icp(CITE_CODE);
1274 icp["key"] = from_utf8(arg);
1276 icp["before"] = from_utf8(opt1);
1277 string icstr = InsetCommandMailer::params2string("citation", icp);
1278 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1281 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1285 case LFUN_BUFFER_CHILD_OPEN: {
1286 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1287 Buffer * parent = lyx_view_->buffer();
1288 FileName filename = makeAbsPath(argument, parent->filePath());
1289 view()->saveBookmark(false);
1291 bool parsed = false;
1292 if (theBufferList().exists(filename.absFilename())) {
1293 child = theBufferList().getBuffer(filename.absFilename());
1295 setMessage(bformat(_("Opening child document %1$s..."),
1296 makeDisplayPath(filename.absFilename())));
1297 child = lyx_view_->loadDocument(filename, false);
1301 // Set the parent name of the child document.
1302 // This makes insertion of citations and references in the child work,
1303 // when the target is in the parent or another child document.
1304 child->setParent(parent);
1305 updateLabels(*child->masterBuffer());
1306 lyx_view_->setBuffer(child);
1308 child->errors("Parse");
1311 // If a screen update is required (in case where auto_open is false),
1312 // setBuffer() would have taken care of it already. Otherwise we shall
1313 // reset the update flag because it can cause a circular problem.
1315 updateFlags = Update::None;
1319 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1320 BOOST_ASSERT(lyx_view_);
1321 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1324 case LFUN_KEYMAP_OFF:
1325 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1326 lyx_view_->view()->getIntl().keyMapOn(false);
1329 case LFUN_KEYMAP_PRIMARY:
1330 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1331 lyx_view_->view()->getIntl().keyMapPrim();
1334 case LFUN_KEYMAP_SECONDARY:
1335 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1336 lyx_view_->view()->getIntl().keyMapSec();
1339 case LFUN_KEYMAP_TOGGLE:
1340 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1341 lyx_view_->view()->getIntl().toggleKeyMap();
1347 string rest = split(argument, countstr, ' ');
1348 istringstream is(countstr);
1351 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1352 for (int i = 0; i < count; ++i)
1353 dispatch(lyxaction.lookupFunc(rest));
1357 case LFUN_COMMAND_SEQUENCE: {
1358 // argument contains ';'-terminated commands
1359 string arg = argument;
1360 while (!arg.empty()) {
1362 arg = split(arg, first, ';');
1363 FuncRequest func(lyxaction.lookupFunc(first));
1364 func.origin = cmd.origin;
1372 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1373 func.origin = cmd.origin;
1375 LyX::ref().topLevelCmdDef().release(argument);
1377 if (func.action == LFUN_UNKNOWN_ACTION) {
1378 // unknown command definition
1379 lyxerr << "Warning: unknown command definition `"
1383 // recursion detected
1384 lyxerr << "Warning: Recursion in the command definition `"
1385 << argument << "' detected"
1392 case LFUN_PREFERENCES_SAVE: {
1393 lyxrc.write(makeAbsPath("preferences",
1394 package().user_support().absFilename()),
1399 case LFUN_SET_COLOR: {
1401 string const x11_name = split(argument, lyx_name, ' ');
1402 if (lyx_name.empty() || x11_name.empty()) {
1403 setErrorMessage(from_ascii(N_(
1404 "Syntax: set-color <lyx_name>"
1409 bool const graphicsbg_changed =
1410 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1411 x11_name != lcolor.getX11Name(Color_graphicsbg));
1413 if (!lcolor.setColor(lyx_name, x11_name)) {
1415 bformat(_("Set-color \"%1$s\" failed "
1416 "- color is undefined or "
1417 "may not be redefined"),
1418 from_utf8(lyx_name)));
1422 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1424 if (graphicsbg_changed) {
1425 // FIXME: The graphics cache no longer has a changeDisplay method.
1427 graphics::GCache::get().changeDisplay(true);
1434 BOOST_ASSERT(lyx_view_);
1435 lyx_view_->message(from_utf8(argument));
1438 case LFUN_EXTERNAL_EDIT: {
1439 BOOST_ASSERT(lyx_view_);
1440 FuncRequest fr(action, argument);
1441 InsetExternal().dispatch(view()->cursor(), fr);
1445 case LFUN_GRAPHICS_EDIT: {
1446 FuncRequest fr(action, argument);
1447 InsetGraphics().dispatch(view()->cursor(), fr);
1451 case LFUN_ALL_INSETS_TOGGLE: {
1452 BOOST_ASSERT(lyx_view_);
1454 string const name = split(argument, action, ' ');
1455 InsetCode const inset_code = insetCode(name);
1457 Cursor & cur = view()->cursor();
1458 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1460 Inset & inset = lyx_view_->buffer()->inset();
1461 InsetIterator it = inset_iterator_begin(inset);
1462 InsetIterator const end = inset_iterator_end(inset);
1463 for (; it != end; ++it) {
1464 if (!it->asInsetMath()
1465 && (inset_code == NO_CODE
1466 || inset_code == it->lyxCode())) {
1467 Cursor tmpcur = cur;
1468 tmpcur.pushBackward(*it);
1469 it->dispatch(tmpcur, fr);
1472 updateFlags = Update::Force | Update::FitCursor;
1476 case LFUN_BUFFER_LANGUAGE: {
1477 BOOST_ASSERT(lyx_view_);
1478 Buffer & buffer = *lyx_view_->buffer();
1479 Language const * oldL = buffer.params().language;
1480 Language const * newL = languages.getLanguage(argument);
1481 if (!newL || oldL == newL)
1484 if (oldL->rightToLeft() == newL->rightToLeft()
1485 && !buffer.isMultiLingual())
1486 buffer.changeLanguage(oldL, newL);
1490 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1491 string const fname =
1492 addName(addPath(package().user_support().absFilename(), "templates/"),
1494 Buffer defaults(fname);
1496 istringstream ss(argument);
1499 int const unknown_tokens = defaults.readHeader(lex);
1501 if (unknown_tokens != 0) {
1502 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1503 << unknown_tokens << " unknown token"
1504 << (unknown_tokens == 1 ? "" : "s")
1508 if (defaults.writeFile(FileName(defaults.absFileName())))
1509 setMessage(bformat(_("Document defaults saved in %1$s"),
1510 makeDisplayPath(fname)));
1512 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1516 case LFUN_BUFFER_PARAMS_APPLY: {
1517 BOOST_ASSERT(lyx_view_);
1519 Buffer * buffer = lyx_view_->buffer();
1520 DocumentClass * oldClass = buffer->params().documentClassPtr();
1521 Cursor & cur = view()->cursor();
1522 cur.recordUndoFullDocument();
1524 istringstream ss(argument);
1527 int const unknown_tokens = buffer->readHeader(lex);
1529 if (unknown_tokens != 0) {
1530 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1531 << unknown_tokens << " unknown token"
1532 << (unknown_tokens == 1 ? "" : "s")
1536 updateLayout(oldClass, buffer);
1538 updateFlags = Update::Force | Update::FitCursor;
1539 // We are most certainly here because of a change in the document
1540 // It is then better to make sure that all dialogs are in sync with
1541 // current document settings. LyXView::restartCursor() achieve this.
1542 lyx_view_->restartCursor();
1546 case LFUN_LAYOUT_MODULES_CLEAR: {
1547 BOOST_ASSERT(lyx_view_);
1548 Buffer * buffer = lyx_view_->buffer();
1549 DocumentClass * oldClass = buffer->params().documentClassPtr();
1550 view()->cursor().recordUndoFullDocument();
1551 buffer->params().clearLayoutModules();
1552 buffer->params().makeDocumentClass();
1553 updateLayout(oldClass, buffer);
1554 updateFlags = Update::Force | Update::FitCursor;
1558 case LFUN_LAYOUT_MODULE_ADD: {
1559 BOOST_ASSERT(lyx_view_);
1560 Buffer * buffer = lyx_view_->buffer();
1561 DocumentClass * oldClass = buffer->params().documentClassPtr();
1562 view()->cursor().recordUndoFullDocument();
1563 buffer->params().addLayoutModule(argument);
1564 buffer->params().makeDocumentClass();
1565 updateLayout(oldClass, buffer);
1566 updateFlags = Update::Force | Update::FitCursor;
1570 case LFUN_TEXTCLASS_APPLY: {
1571 BOOST_ASSERT(lyx_view_);
1572 Buffer * buffer = lyx_view_->buffer();
1574 if (!loadLayoutFile(argument, buffer->temppath()) &&
1575 !loadLayoutFile(argument, buffer->filePath()))
1578 LayoutFile const * old_layout = buffer->params().baseClass();
1579 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1581 if (old_layout == new_layout)
1585 //Save the old, possibly modular, layout for use in conversion.
1586 DocumentClass * oldDocClass = buffer->params().documentClassPtr();
1587 view()->cursor().recordUndoFullDocument();
1588 buffer->params().setBaseClass(argument);
1589 buffer->params().makeDocumentClass();
1590 updateLayout(oldDocClass, buffer);
1591 updateFlags = Update::Force | Update::FitCursor;
1595 case LFUN_LAYOUT_RELOAD: {
1596 BOOST_ASSERT(lyx_view_);
1597 Buffer * buffer = lyx_view_->buffer();
1598 DocumentClass * oldClass = buffer->params().documentClassPtr();
1599 LayoutFileIndex bc = buffer->params().baseClassID();
1600 LayoutFileList::get().reset(bc);
1601 buffer->params().makeDocumentClass();
1602 updateLayout(oldClass, buffer);
1603 updateFlags = Update::Force | Update::FitCursor;
1607 case LFUN_TEXTCLASS_LOAD:
1608 loadLayoutFile(argument, lyx_view_->buffer()->temppath()) ||
1609 loadLayoutFile(argument, lyx_view_->buffer()->filePath());
1612 case LFUN_LYXRC_APPLY: {
1613 LyXRC const lyxrc_orig = lyxrc;
1615 istringstream ss(argument);
1616 bool const success = lyxrc.read(ss) == 0;
1619 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1620 << "Unable to read lyxrc data"
1625 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1627 theApp()->resetGui();
1629 /// We force the redraw in any case because there might be
1630 /// some screen font changes.
1631 /// FIXME: only the current view will be updated. the Gui
1632 /// class is able to furnish the list of views.
1633 updateFlags = Update::Force;
1637 case LFUN_BOOKMARK_GOTO:
1638 // go to bookmark, open unopened file and switch to buffer if necessary
1639 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1640 updateFlags = Update::FitCursor;
1643 case LFUN_BOOKMARK_CLEAR:
1644 LyX::ref().session().bookmarks().clear();
1648 BOOST_ASSERT(theApp());
1649 // Let the frontend dispatch its own actions.
1650 if (theApp()->dispatch(cmd))
1651 // Nothing more to do.
1654 // Everything below is only for active lyx_view_
1658 // Let the current LyXView dispatch its own actions.
1659 if (lyx_view_->dispatch(cmd)) {
1660 if (lyx_view_->view())
1661 updateFlags = lyx_view_->view()->cursor().result().update();
1665 BOOST_ASSERT(lyx_view_->view());
1666 // Let the current BufferView dispatch its own actions.
1667 if (view()->dispatch(cmd)) {
1668 // The BufferView took care of its own updates if needed.
1669 updateFlags = Update::None;
1673 // Let the current Cursor dispatch its own actions.
1674 Cursor old = view()->cursor();
1675 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1676 cursorPosBeforeDispatchY_);
1677 view()->cursor().dispatch(cmd);
1679 // notify insets we just left
1680 if (view()->cursor() != old) {
1682 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1684 view()->cursor().fixIfBroken();
1687 // update completion. We do it here and not in
1688 // processKeySym to avoid another redraw just for a
1689 // changed inline completion
1690 if (cmd.origin == FuncRequest::KEYBOARD) {
1691 if (cmd.action == LFUN_SELF_INSERT)
1692 lyx_view_->updateCompletion(view()->cursor(), true, true);
1693 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1694 lyx_view_->updateCompletion(view()->cursor(), false, true);
1696 lyx_view_->updateCompletion(view()->cursor(), false, false);
1699 updateFlags = view()->cursor().result().update();
1702 if (lyx_view_ && lyx_view_->buffer()) {
1703 // BufferView::update() updates the ViewMetricsInfo and
1704 // also initializes the position cache for all insets in
1705 // (at least partially) visible top-level paragraphs.
1706 // We will redraw the screen only if needed.
1707 view()->processUpdateFlags(updateFlags);
1709 // if we executed a mutating lfun, mark the buffer as dirty
1711 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1712 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1713 lyx_view_->buffer()->markDirty();
1715 // Do we have a selection?
1716 theSelection().haveSelection(view()->cursor().selection());
1719 lyx_view_->restartCursor();
1723 // Some messages may already be translated, so we cannot use _()
1724 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1729 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1731 const bool verbose = (cmd.origin == FuncRequest::MENU
1732 || cmd.origin == FuncRequest::TOOLBAR
1733 || cmd.origin == FuncRequest::COMMANDBUFFER);
1735 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1736 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1738 lyx_view_->message(msg);
1742 docstring dispatch_msg = msg;
1743 if (!dispatch_msg.empty())
1744 dispatch_msg += ' ';
1746 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1748 bool argsadded = false;
1750 if (!cmd.argument().empty()) {
1751 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1752 comname += ' ' + cmd.argument();
1757 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1759 if (!shortcuts.empty())
1760 comname += ": " + shortcuts;
1761 else if (!argsadded && !cmd.argument().empty())
1762 comname += ' ' + cmd.argument();
1764 if (!comname.empty()) {
1765 comname = rtrim(comname);
1766 dispatch_msg += '(' + rtrim(comname) + ')';
1769 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1770 if (!dispatch_msg.empty())
1771 lyx_view_->message(dispatch_msg);
1775 void LyXFunc::reloadBuffer()
1777 FileName filename = lyx_view_->buffer()->fileName();
1778 // The user has already confirmed that the changes, if any, should
1779 // be discarded. So we just release the Buffer and don't call closeBuffer();
1780 theBufferList().release(lyx_view_->buffer());
1781 Buffer * buf = lyx_view_->loadDocument(filename);
1782 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1786 lyx_view_->setBuffer(buf);
1787 buf->errors("Parse");
1788 str = bformat(_("Document %1$s reloaded."), disp_fn);
1790 str = bformat(_("Could not reload document %1$s"), disp_fn);
1792 lyx_view_->message(str);
1795 // Each "lyx_view_" should have it's own message method. lyxview and
1796 // the minibuffer would use the minibuffer, but lyxserver would
1797 // send an ERROR signal to its client. Alejandro 970603
1798 // This function is bit problematic when it comes to NLS, to make the
1799 // lyx servers client be language indepenent we must not translate
1800 // strings sent to this func.
1801 void LyXFunc::setErrorMessage(docstring const & m) const
1803 dispatch_buffer = m;
1808 void LyXFunc::setMessage(docstring const & m) const
1810 dispatch_buffer = m;
1814 docstring const LyXFunc::viewStatusMessage()
1816 // When meta-fake key is pressed, show the key sequence so far + "M-".
1818 return keyseq.print(KeySequence::ForGui) + "M-";
1820 // Else, when a non-complete key sequence is pressed,
1821 // show the available options.
1822 if (keyseq.length() > 0 && !keyseq.deleted())
1823 return keyseq.printOptions(true);
1825 BOOST_ASSERT(lyx_view_);
1826 if (!lyx_view_->buffer())
1827 return _("Welcome to LyX!");
1829 return view()->cursor().currentState();
1833 BufferView * LyXFunc::view() const
1835 BOOST_ASSERT(lyx_view_);
1836 return lyx_view_->view();
1840 bool LyXFunc::wasMetaKey() const
1842 return (meta_fake_bit != NoModifier);
1846 void LyXFunc::updateLayout(DocumentClass * oldlayout,Buffer * buffer)
1848 lyx_view_->message(_("Converting document to new document class..."));
1850 StableDocIterator backcur(view()->cursor());
1851 ErrorList & el = buffer->errorList("Class Switch");
1852 cap::switchBetweenClasses(
1853 oldlayout, buffer->params().documentClassPtr(),
1854 static_cast<InsetText &>(buffer->inset()), el);
1856 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1858 buffer->errors("Class Switch");
1859 updateLabels(*buffer);
1865 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1867 // Why the switch you might ask. It is a trick to ensure that all
1868 // the elements in the LyXRCTags enum is handled. As you can see
1869 // there are no breaks at all. So it is just a huge fall-through.
1870 // The nice thing is that we will get a warning from the compiler
1871 // if we forget an element.
1872 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1874 case LyXRC::RC_ACCEPT_COMPOUND:
1875 case LyXRC::RC_ALT_LANG:
1876 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1877 case LyXRC::RC_PLAINTEXT_LINELEN:
1878 case LyXRC::RC_AUTOREGIONDELETE:
1879 case LyXRC::RC_AUTORESET_OPTIONS:
1880 case LyXRC::RC_AUTOSAVE:
1881 case LyXRC::RC_AUTO_NUMBER:
1882 case LyXRC::RC_BACKUPDIR_PATH:
1883 case LyXRC::RC_BIBTEX_COMMAND:
1884 case LyXRC::RC_BINDFILE:
1885 case LyXRC::RC_CHECKLASTFILES:
1886 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1887 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1888 case LyXRC::RC_COMPLETION_INLINE_MATH:
1889 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1890 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1891 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1892 case LyXRC::RC_COMPLETION_POPUP_MATH:
1893 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1894 case LyXRC::RC_USELASTFILEPOS:
1895 case LyXRC::RC_LOADSESSION:
1896 case LyXRC::RC_CHKTEX_COMMAND:
1897 case LyXRC::RC_CONVERTER:
1898 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1899 case LyXRC::RC_COPIER:
1900 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1901 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1902 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1903 case LyXRC::RC_DATE_INSERT_FORMAT:
1904 case LyXRC::RC_DEFAULT_LANGUAGE:
1905 case LyXRC::RC_DEFAULT_PAPERSIZE:
1906 case LyXRC::RC_DEFFILE:
1907 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1908 case LyXRC::RC_DISPLAY_GRAPHICS:
1909 case LyXRC::RC_DOCUMENTPATH:
1910 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1911 FileName path(lyxrc_new.document_path);
1912 if (path.exists() && path.isDirectory())
1913 package().document_dir() = FileName(lyxrc.document_path);
1915 case LyXRC::RC_ESC_CHARS:
1916 case LyXRC::RC_EXAMPLEPATH:
1917 case LyXRC::RC_FONT_ENCODING:
1918 case LyXRC::RC_FORMAT:
1919 case LyXRC::RC_GROUP_LAYOUTS:
1920 case LyXRC::RC_INDEX_COMMAND:
1921 case LyXRC::RC_INPUT:
1922 case LyXRC::RC_KBMAP:
1923 case LyXRC::RC_KBMAP_PRIMARY:
1924 case LyXRC::RC_KBMAP_SECONDARY:
1925 case LyXRC::RC_LABEL_INIT_LENGTH:
1926 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1927 case LyXRC::RC_LANGUAGE_AUTO_END:
1928 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1929 case LyXRC::RC_LANGUAGE_COMMAND_END:
1930 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1931 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1932 case LyXRC::RC_LANGUAGE_PACKAGE:
1933 case LyXRC::RC_LANGUAGE_USE_BABEL:
1934 case LyXRC::RC_MACRO_EDIT_STYLE:
1935 case LyXRC::RC_MAKE_BACKUP:
1936 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1937 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1938 case LyXRC::RC_NUMLASTFILES:
1939 case LyXRC::RC_PATH_PREFIX:
1940 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1941 prependEnvPath("PATH", lyxrc.path_prefix);
1943 case LyXRC::RC_PERS_DICT:
1944 case LyXRC::RC_PREVIEW:
1945 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1946 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1947 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1948 case LyXRC::RC_PRINTCOPIESFLAG:
1949 case LyXRC::RC_PRINTER:
1950 case LyXRC::RC_PRINTEVENPAGEFLAG:
1951 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1952 case LyXRC::RC_PRINTFILEEXTENSION:
1953 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1954 case LyXRC::RC_PRINTODDPAGEFLAG:
1955 case LyXRC::RC_PRINTPAGERANGEFLAG:
1956 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1957 case LyXRC::RC_PRINTPAPERFLAG:
1958 case LyXRC::RC_PRINTREVERSEFLAG:
1959 case LyXRC::RC_PRINTSPOOL_COMMAND:
1960 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1961 case LyXRC::RC_PRINTTOFILE:
1962 case LyXRC::RC_PRINTTOPRINTER:
1963 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1964 case LyXRC::RC_PRINT_COMMAND:
1965 case LyXRC::RC_RTL_SUPPORT:
1966 case LyXRC::RC_SCREEN_DPI:
1967 case LyXRC::RC_SCREEN_FONT_ROMAN:
1968 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1969 case LyXRC::RC_SCREEN_FONT_SANS:
1970 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1971 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1972 case LyXRC::RC_SCREEN_FONT_SIZES:
1973 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1974 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1975 case LyXRC::RC_GEOMETRY_SESSION:
1976 case LyXRC::RC_SCREEN_ZOOM:
1977 case LyXRC::RC_SERVERPIPE:
1978 case LyXRC::RC_SET_COLOR:
1979 case LyXRC::RC_SHOW_BANNER:
1980 case LyXRC::RC_SINGLE_WINDOW:
1981 case LyXRC::RC_SPELL_COMMAND:
1982 case LyXRC::RC_TEMPDIRPATH:
1983 case LyXRC::RC_TEMPLATEPATH:
1984 case LyXRC::RC_TEX_ALLOWS_SPACES:
1985 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1986 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1987 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1989 case LyXRC::RC_UIFILE:
1990 case LyXRC::RC_USER_EMAIL:
1991 case LyXRC::RC_USER_NAME:
1992 case LyXRC::RC_USETEMPDIR:
1993 case LyXRC::RC_USE_ALT_LANG:
1994 case LyXRC::RC_USE_CONVERTER_CACHE:
1995 case LyXRC::RC_USE_ESC_CHARS:
1996 case LyXRC::RC_USE_INP_ENC:
1997 case LyXRC::RC_USE_PERS_DICT:
1998 case LyXRC::RC_USE_TOOLTIP:
1999 case LyXRC::RC_USE_PIXMAP_CACHE:
2000 case LyXRC::RC_USE_SPELL_LIB:
2001 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2002 case LyXRC::RC_SORT_LAYOUTS:
2003 case LyXRC::RC_FULL_SCREEN_LIMIT:
2004 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2005 case LyXRC::RC_FULL_SCREEN_TABBAR:
2006 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2007 case LyXRC::RC_FULL_SCREEN_WIDTH:
2008 case LyXRC::RC_VISUAL_CURSOR:
2009 case LyXRC::RC_VIEWER:
2010 case LyXRC::RC_LAST: