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/InsetGraphics.h"
66 #include "insets/InsetInclude.h"
67 #include "insets/InsetListings.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetPhantom.h"
70 #include "insets/InsetSpace.h"
71 #include "insets/InsetTabular.h"
72 #include "insets/InsetVSpace.h"
73 #include "insets/InsetWrap.h"
75 #include "frontends/alert.h"
76 #include "frontends/Application.h"
77 #include "frontends/KeySymbol.h"
78 #include "frontends/LyXView.h"
79 #include "frontends/Selection.h"
81 #include "support/debug.h"
82 #include "support/environment.h"
83 #include "support/FileName.h"
84 #include "support/filetools.h"
85 #include "support/gettext.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"
97 using namespace lyx::support;
101 using frontend::LyXView;
103 namespace Alert = frontend::Alert;
108 // This function runs "configure" and then rereads lyx.defaults to
109 // reconfigure the automatic settings.
110 void reconfigure(LyXView * lv, string const & option)
112 // emit message signal.
114 lv->message(_("Running configure..."));
116 // Run configure in user lyx directory
117 PathChanger p(package().user_support());
118 string configure_command = package().configure_command();
119 configure_command += option;
121 int ret = one.startscript(Systemcall::Wait, configure_command);
123 // 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 LASSERT(cursor.idx() <= cursor.lastidx(), /**/);
158 LASSERT(cursor.pit() <= cursor.lastpit(), /**/);
159 LASSERT(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 (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(FuncCode action)
223 char_type c = encoded_last_key;
228 LASSERT(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);
239 //FIXME: bookmark handling is a frontend issue. This code should be transferred
240 // to GuiView and be GuiView and be window dependent.
241 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
243 LASSERT(lyx_view_, /**/);
244 if (!theSession().bookmarks().isValid(idx))
246 BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
247 LASSERT(!bm.filename.empty(), /**/);
248 string const file = bm.filename.absFilename();
249 // if the file is not opened, open it.
250 if (!theBufferList().exists(bm.filename)) {
252 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
256 // open may fail, so we need to test it again
257 if (!theBufferList().exists(bm.filename))
260 // bm can be changed when saving
261 BookmarksSection::Bookmark tmp = bm;
263 // Special case idx == 0 used for back-from-back jump navigation
265 dispatch(FuncRequest(LFUN_BOOKMARK_SAVE, "0"));
267 // if the current buffer is not that one, switch to it.
268 if (!lyx_view_->buffer() || lyx_view_->buffer()->fileName() != tmp.filename) {
271 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
274 // moveToPosition try paragraph id first and then paragraph (pit, pos).
275 if (!view()->moveToPosition(tmp.bottom_pit, tmp.bottom_pos,
276 tmp.top_id, tmp.top_pos))
283 // Cursor jump succeeded!
284 Cursor const & cur = view()->cursor();
285 pit_type new_pit = cur.pit();
286 pos_type new_pos = cur.pos();
287 int new_id = cur.paragraph().id();
289 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
290 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
291 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
292 || bm.top_id != new_id) {
293 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
294 new_pit, new_pos, new_id);
299 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
301 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
303 // Do nothing if we have nothing (JMarc)
304 if (!keysym.isOK()) {
305 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
306 lyx_view_->restartCursor();
310 if (keysym.isModifier()) {
311 LYXERR(Debug::KEY, "isModifier true");
313 lyx_view_->restartCursor();
317 //Encoding const * encoding = view()->cursor().getEncoding();
318 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
319 // FIXME: encoded_last_key shadows the member variable of the same
320 // name. Is that intended?
321 char_type encoded_last_key = keysym.getUCSEncoded();
323 // Do a one-deep top-level lookup for
324 // cancel and meta-fake keys. RVDK_PATCH_5
325 cancel_meta_seq.reset();
327 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
328 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
330 // When not cancel or meta-fake, do the normal lookup.
331 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
332 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
333 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
334 // remove Caps Lock and Mod2 as a modifiers
335 func = keyseq.addkey(keysym, (state | meta_fake_bit));
336 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
339 // Dont remove this unless you know what you are doing.
340 meta_fake_bit = NoModifier;
342 // Can this happen now ?
343 if (func.action == LFUN_NOACTION)
344 func = FuncRequest(LFUN_COMMAND_PREFIX);
346 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
347 << keyseq.print(KeySequence::Portable) << ']');
349 // already here we know if it any point in going further
350 // why not return already here if action == -1 and
351 // num_bytes == 0? (Lgb)
353 if (keyseq.length() > 1)
354 lyx_view_->message(keyseq.print(KeySequence::ForGui));
357 // Maybe user can only reach the key via holding down shift.
358 // Let's see. But only if shift is the only modifier
359 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
360 LYXERR(Debug::KEY, "Trying without shift");
361 func = keyseq.addkey(keysym, NoModifier);
362 LYXERR(Debug::KEY, "Action now " << func.action);
365 if (func.action == LFUN_UNKNOWN_ACTION) {
366 // Hmm, we didn't match any of the keysequences. See
367 // if it's normal insertable text not already covered
369 if (keysym.isText() && keyseq.length() == 1) {
370 LYXERR(Debug::KEY, "isText() is true, inserting.");
371 func = FuncRequest(LFUN_SELF_INSERT,
372 FuncRequest::KEYBOARD);
374 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
375 lyx_view_->message(_("Unknown function."));
376 lyx_view_->restartCursor();
381 if (func.action == LFUN_SELF_INSERT) {
382 if (encoded_last_key != 0) {
383 docstring const arg(1, encoded_last_key);
384 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
385 FuncRequest::KEYBOARD));
386 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
396 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
398 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
401 Buffer * buf = lyx_view_ ? lyx_view_->buffer() : 0;
403 if (cmd.action == LFUN_NOACTION) {
404 flag.message(from_utf8(N_("Nothing to do")));
405 flag.setEnabled(false);
409 switch (cmd.action) {
410 case LFUN_UNKNOWN_ACTION:
412 flag.setEnabled(false);
419 if (flag.unknown()) {
420 flag.message(from_utf8(N_("Unknown action")));
424 if (!flag.enabled()) {
425 if (flag.message().empty())
426 flag.message(from_utf8(N_("Command disabled")));
430 // Check whether we need a buffer
431 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
433 flag.message(from_utf8(N_("Command not allowed with"
434 "out any document open")));
435 flag.setEnabled(false);
439 // I would really like to avoid having this switch and rather try to
440 // encode this in the function itself.
441 // -- And I'd rather let an inset decide which LFUNs it is willing
442 // to handle (Andre')
444 switch (cmd.action) {
446 case LFUN_BUFFER_TOGGLE_READ_ONLY:
447 flag.setOnOff(buf->isReadonly());
450 case LFUN_BUFFER_SWITCH:
451 // toggle on the current buffer, but do not toggle off
452 // the other ones (is that a good idea?)
453 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
457 case LFUN_BUFFER_CHKTEX:
458 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
461 case LFUN_BUILD_PROGRAM:
462 enable = buf->isExportable("program");
465 case LFUN_VC_REGISTER:
466 enable = !buf->lyxvc().inUse();
468 case LFUN_VC_CHECK_IN:
469 enable = buf->lyxvc().checkInEnabled();
471 case LFUN_VC_CHECK_OUT:
472 enable = buf->lyxvc().checkOutEnabled();
474 case LFUN_VC_LOCKING_TOGGLE:
475 enable = !buf->isReadonly() && buf->lyxvc().lockingToggleEnabled();
476 flag.setEnabled(enable);
478 flag.setOnOff(!buf->lyxvc().locker().empty());
481 enable = buf->lyxvc().inUse();
483 case LFUN_VC_UNDO_LAST:
484 enable = buf->lyxvc().undoLastEnabled();
486 case LFUN_BUFFER_RELOAD:
487 enable = !buf->isUnnamed() && buf->fileName().exists()
488 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
491 case LFUN_CITATION_INSERT: {
492 FuncRequest fr(LFUN_INSET_INSERT, "citation");
493 enable = getStatus(fr).enabled();
497 // This could be used for the no-GUI version. The GUI version is handled in
498 // LyXView::getStatus(). See above.
500 case LFUN_BUFFER_WRITE:
501 case LFUN_BUFFER_WRITE_AS: {
502 Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
503 enable = b && (b->isUnnamed() || !b->isClean());
508 case LFUN_BUFFER_WRITE_ALL: {
509 // We enable the command only if there are some modified buffers
510 Buffer * first = theBufferList().first();
515 // We cannot use a for loop as the buffer list is a cycle.
521 b = theBufferList().next(b);
522 } while (b != first);
526 case LFUN_BOOKMARK_GOTO: {
527 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
528 enable = theSession().bookmarks().isValid(num);
532 case LFUN_BOOKMARK_CLEAR:
533 enable = theSession().bookmarks().hasValid();
536 // this one is difficult to get right. As a half-baked
537 // solution, we consider only the first action of the sequence
538 case LFUN_COMMAND_SEQUENCE: {
539 // argument contains ';'-terminated commands
540 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
541 FuncRequest func(lyxaction.lookupFunc(firstcmd));
542 func.origin = cmd.origin;
543 flag = getStatus(func);
547 // we want to check if at least one of these is enabled
548 case LFUN_COMMAND_ALTERNATIVES: {
549 // argument contains ';'-terminated commands
550 string arg = to_utf8(cmd.argument());
551 while (!arg.empty()) {
553 arg = split(arg, first, ';');
554 FuncRequest func(lyxaction.lookupFunc(first));
555 func.origin = cmd.origin;
556 flag = getStatus(func);
557 // if this one is enabled, the whole thing is
566 string name = to_utf8(cmd.argument());
567 if (theTopLevelCmdDef().lock(name, func)) {
568 func.origin = cmd.origin;
569 flag = getStatus(func);
570 theTopLevelCmdDef().release(name);
572 // catch recursion or unknown command
573 // definition. all operations until the
574 // recursion or unknown command definition
575 // occurs are performed, so set the state to
582 case LFUN_VC_COMMAND: {
583 if (cmd.argument().empty())
586 if (!buf && contains(cmd.getArg(0), 'D'))
591 case LFUN_MASTER_BUFFER_UPDATE:
592 case LFUN_MASTER_BUFFER_VIEW:
593 if (!buf->parent()) {
597 case LFUN_BUFFER_UPDATE:
598 case LFUN_BUFFER_VIEW: {
599 string format = to_utf8(cmd.argument());
600 if (cmd.argument().empty())
601 format = buf->getDefaultOutputFormat();
602 typedef vector<Format const *> Formats;
604 formats = buf->exportableFormats(true);
605 Formats::const_iterator fit = formats.begin();
606 Formats::const_iterator end = formats.end();
608 for (; fit != end ; ++fit) {
609 if ((*fit)->name() == format)
615 case LFUN_WORD_FIND_FORWARD:
616 case LFUN_WORD_FIND_BACKWARD:
617 case LFUN_WORD_FINDADV:
618 case LFUN_COMMAND_PREFIX:
619 case LFUN_COMMAND_EXECUTE:
621 case LFUN_META_PREFIX:
622 case LFUN_BUFFER_CLOSE:
623 case LFUN_BUFFER_IMPORT:
624 case LFUN_BUFFER_AUTO_SAVE:
625 case LFUN_RECONFIGURE:
627 case LFUN_DROP_LAYOUTS_CHOICE:
629 case LFUN_SERVER_GET_FILENAME:
630 case LFUN_SERVER_NOTIFY:
631 case LFUN_SERVER_GOTO_FILE_ROW:
632 case LFUN_DIALOG_HIDE:
633 case LFUN_DIALOG_DISCONNECT_INSET:
634 case LFUN_BUFFER_CHILD_OPEN:
635 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
636 case LFUN_KEYMAP_OFF:
637 case LFUN_KEYMAP_PRIMARY:
638 case LFUN_KEYMAP_SECONDARY:
639 case LFUN_KEYMAP_TOGGLE:
641 case LFUN_BUFFER_EXPORT_CUSTOM:
642 case LFUN_PREFERENCES_SAVE:
644 case LFUN_INSET_EDIT:
645 case LFUN_BUFFER_LANGUAGE:
646 case LFUN_TEXTCLASS_APPLY:
647 case LFUN_TEXTCLASS_LOAD:
648 case LFUN_BUFFER_SAVE_AS_DEFAULT:
649 case LFUN_BUFFER_PARAMS_APPLY:
650 case LFUN_LAYOUT_MODULES_CLEAR:
651 case LFUN_LAYOUT_MODULE_ADD:
652 case LFUN_LAYOUT_RELOAD:
653 case LFUN_LYXRC_APPLY:
654 case LFUN_BUFFER_NEXT:
655 case LFUN_BUFFER_PREVIOUS:
656 // these are handled in our dispatch()
664 if (theApp()->getStatus(cmd, flag))
667 // Does the view know something?
672 if (lyx_view_->getStatus(cmd, flag))
675 // If we do not have a BufferView, then other functions are disabled
681 // Is this a function that acts on inset at point?
682 Inset * inset = view()->cursor().nextInset();
683 if (lyxaction.funcHasFlag(cmd.action, LyXAction::AtPoint)
684 && inset && inset->getStatus(view()->cursor(), cmd, flag))
687 bool decided = getLocalStatus(view()->cursor(), cmd, flag);
689 // try the BufferView
690 decided = view()->getStatus(cmd, flag);
693 view()->buffer().getStatus(cmd, flag);
697 flag.setEnabled(false);
699 // Can we use a readonly buffer?
700 if (buf && buf->isReadonly()
701 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
702 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
703 flag.message(from_utf8(N_("Document is read-only")));
704 flag.setEnabled(false);
707 // Are we in a DELETED change-tracking region?
709 && lookupChangeType(view()->cursor(), true) == Change::DELETED
710 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
711 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
712 flag.message(from_utf8(N_("This portion of the document is deleted.")));
713 flag.setEnabled(false);
716 // the default error message if we disable the command
717 if (!flag.enabled() && flag.message().empty())
718 flag.message(from_utf8(N_("Command disabled")));
724 bool LyXFunc::ensureBufferClean(BufferView * bv)
726 Buffer & buf = bv->buffer();
727 if (buf.isClean() && !buf.isUnnamed())
730 docstring const file = buf.fileName().displayName(30);
733 if (!buf.isUnnamed()) {
734 text = bformat(_("The document %1$s has unsaved "
735 "changes.\n\nDo you want to save "
736 "the document?"), file);
737 title = _("Save changed document?");
740 text = bformat(_("The document %1$s has not been "
741 "saved yet.\n\nDo you want to save "
742 "the document?"), file);
743 title = _("Save new document?");
745 int const ret = Alert::prompt(title, text, 0, 1, _("&Save"), _("&Cancel"));
748 lyx_view_->dispatch(FuncRequest(LFUN_BUFFER_WRITE));
750 return buf.isClean() && !buf.isUnnamed();
756 bool loadLayoutFile(string const & name, string const & buf_path)
758 if (!LayoutFileList::get().haveClass(name)) {
759 lyxerr << "Document class \"" << name
760 << "\" does not exist."
765 LayoutFile & tc = LayoutFileList::get()[name];
766 if (!tc.load(buf_path)) {
767 docstring s = bformat(_("The document class %1$s "
768 "could not be loaded."), from_utf8(name));
769 Alert::error(_("Could not load class"), s);
776 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
781 void LyXFunc::dispatch(FuncRequest const & cmd)
783 string const argument = to_utf8(cmd.argument());
784 FuncCode const action = cmd.action;
786 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
787 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
789 // we have not done anything wrong yet.
791 dispatch_buffer.erase();
793 // redraw the screen at the end (first of the two drawing steps).
794 //This is done unless explicitely requested otherwise
795 Update::flags updateFlags = Update::FitCursor;
797 FuncStatus const flag = getStatus(cmd);
798 if (!flag.enabled()) {
799 // We cannot use this function here
800 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
801 << lyxaction.getActionName(action)
802 << " [" << action << "] is disabled at this location");
803 setErrorMessage(flag.message());
805 lyx_view_->restartCursor();
807 Buffer * buffer = lyx_view_ ? lyx_view_->buffer() : 0;
810 case LFUN_WORD_FIND_FORWARD:
811 case LFUN_WORD_FIND_BACKWARD: {
812 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
813 static docstring last_search;
814 docstring searched_string;
816 if (!cmd.argument().empty()) {
817 last_search = cmd.argument();
818 searched_string = cmd.argument();
820 searched_string = last_search;
823 if (searched_string.empty())
826 bool const fw = action == LFUN_WORD_FIND_FORWARD;
827 docstring const data =
828 find2string(searched_string, true, false, fw);
829 find(view(), FuncRequest(LFUN_WORD_FIND, data));
833 case LFUN_COMMAND_PREFIX:
834 LASSERT(lyx_view_, /**/);
835 lyx_view_->message(keyseq.printOptions(true));
839 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
841 meta_fake_bit = NoModifier;
843 // cancel any selection
844 dispatch(FuncRequest(LFUN_MARK_OFF));
845 setMessage(from_ascii(N_("Cancel")));
848 case LFUN_META_PREFIX:
849 meta_fake_bit = AltModifier;
850 setMessage(keyseq.print(KeySequence::ForGui));
853 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
854 LASSERT(lyx_view_ && lyx_view_->view() && buffer, /**/);
855 if (buffer->lyxvc().inUse())
856 buffer->lyxvc().toggleReadOnly();
858 buffer->setReadonly(!buffer->isReadonly());
862 // --- Menus -----------------------------------------------
863 case LFUN_BUFFER_CLOSE:
864 lyx_view_->closeBuffer();
866 updateFlags = Update::None;
869 case LFUN_BUFFER_RELOAD: {
870 LASSERT(lyx_view_ && buffer, /**/);
871 docstring const file = makeDisplayPath(buffer->absFileName(), 20);
872 docstring text = bformat(_("Any changes will be lost. Are you sure "
873 "you want to revert to the saved version of the document %1$s?"), file);
874 int const ret = Alert::prompt(_("Revert to saved document?"),
875 text, 1, 1, _("&Revert"), _("&Cancel"));
882 case LFUN_BUFFER_UPDATE: {
883 LASSERT(lyx_view_ && buffer, /**/);
884 string format = argument;
885 if (argument.empty())
886 format = buffer->getDefaultOutputFormat();
887 buffer->doExport(format, true);
891 case LFUN_BUFFER_VIEW: {
892 LASSERT(lyx_view_ && buffer, /**/);
893 string format = argument;
894 if (argument.empty())
895 format = buffer->getDefaultOutputFormat();
896 buffer->preview(format);
900 case LFUN_MASTER_BUFFER_UPDATE: {
901 LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
902 string format = argument;
903 if (argument.empty())
904 format = buffer->masterBuffer()->getDefaultOutputFormat();
905 buffer->masterBuffer()->doExport(format, true);
909 case LFUN_MASTER_BUFFER_VIEW: {
910 LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
911 string format = argument;
912 if (argument.empty())
913 format = buffer->masterBuffer()->getDefaultOutputFormat();
914 buffer->masterBuffer()->preview(format);
918 case LFUN_BUILD_PROGRAM:
919 LASSERT(lyx_view_ && buffer, /**/);
920 buffer->doExport("program", true);
923 case LFUN_BUFFER_CHKTEX:
924 LASSERT(lyx_view_ && buffer, /**/);
928 case LFUN_BUFFER_EXPORT:
929 LASSERT(lyx_view_ && buffer, /**/);
930 if (argument == "custom")
931 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
933 buffer->doExport(argument, false);
936 case LFUN_BUFFER_EXPORT_CUSTOM: {
937 LASSERT(lyx_view_ && buffer, /**/);
939 string command = split(argument, format_name, ' ');
940 Format const * format = formats.getFormat(format_name);
942 lyxerr << "Format \"" << format_name
943 << "\" not recognized!"
948 // The name of the file created by the conversion process
951 // Output to filename
952 if (format->name() == "lyx") {
953 string const latexname = buffer->latexName(false);
954 filename = changeExtension(latexname,
955 format->extension());
956 filename = addName(buffer->temppath(), filename);
958 if (!buffer->writeFile(FileName(filename)))
962 buffer->doExport(format_name, true, filename);
965 // Substitute $$FName for filename
966 if (!contains(command, "$$FName"))
967 command = "( " + command + " ) < $$FName";
968 command = subst(command, "$$FName", filename);
970 // Execute the command in the background
972 call.startscript(Systemcall::DontWait, command);
976 // FIXME: There is need for a command-line import.
978 case LFUN_BUFFER_IMPORT:
983 case LFUN_BUFFER_AUTO_SAVE:
987 case LFUN_RECONFIGURE:
988 // argument is any additional parameter to the configure.py command
989 reconfigure(lyx_view_, argument);
992 case LFUN_HELP_OPEN: {
994 theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
995 string const arg = argument;
997 setErrorMessage(from_utf8(N_("Missing argument")));
1000 FileName fname = i18nLibFileSearch("doc", arg, "lyx");
1002 fname = i18nLibFileSearch("examples", arg, "lyx");
1004 if (fname.empty()) {
1005 lyxerr << "LyX: unable to find documentation file `"
1006 << arg << "'. Bad installation?" << endl;
1009 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1010 makeDisplayPath(fname.absFilename())));
1011 Buffer * buf = lyx_view_->loadDocument(fname, false);
1013 buf->updateLabels();
1014 lyx_view_->setBuffer(buf);
1015 buf->errors("Parse");
1017 updateFlags = Update::None;
1021 // --- version control -------------------------------
1022 case LFUN_VC_REGISTER:
1023 LASSERT(lyx_view_ && buffer, /**/);
1024 if (!ensureBufferClean(view()))
1026 if (!buffer->lyxvc().inUse()) {
1027 if (buffer->lyxvc().registrer())
1030 updateFlags = Update::Force;
1033 case LFUN_VC_CHECK_IN:
1034 LASSERT(lyx_view_ && buffer, /**/);
1035 if (!ensureBufferClean(view()))
1037 if (buffer->lyxvc().inUse()
1038 && !buffer->isReadonly()) {
1039 setMessage(from_utf8(buffer->lyxvc().checkIn()));
1044 case LFUN_VC_CHECK_OUT:
1045 LASSERT(lyx_view_ && buffer, /**/);
1046 if (!ensureBufferClean(view()))
1048 if (buffer->lyxvc().inUse()) {
1049 setMessage(from_utf8(buffer->lyxvc().checkOut()));
1054 case LFUN_VC_LOCKING_TOGGLE:
1055 LASSERT(lyx_view_ && buffer, /**/);
1056 if (!ensureBufferClean(view()) || buffer->isReadonly())
1058 if (buffer->lyxvc().inUse()) {
1059 string res = buffer->lyxvc().lockingToggle();
1061 frontend::Alert::error(_("Revision control error."),
1062 _("Error when setting the locking property."));
1064 setMessage(from_utf8(res));
1070 case LFUN_VC_REVERT:
1071 LASSERT(lyx_view_ && buffer, /**/);
1072 buffer->lyxvc().revert();
1076 case LFUN_VC_UNDO_LAST:
1077 LASSERT(lyx_view_ && buffer, /**/);
1078 buffer->lyxvc().undoLast();
1082 // --- lyxserver commands ----------------------------
1083 case LFUN_SERVER_GET_FILENAME:
1084 LASSERT(lyx_view_ && buffer, /**/);
1085 setMessage(from_utf8(buffer->absFileName()));
1086 LYXERR(Debug::INFO, "FNAME["
1087 << buffer->absFileName() << ']');
1090 case LFUN_SERVER_NOTIFY:
1091 dispatch_buffer = keyseq.print(KeySequence::Portable);
1092 theServer().notifyClient(to_utf8(dispatch_buffer));
1095 case LFUN_SERVER_GOTO_FILE_ROW: {
1096 LASSERT(lyx_view_, /**/);
1099 istringstream is(argument);
1100 is >> file_name >> row;
1102 bool loaded = false;
1103 string const abstmp = package().temp_dir().absFilename();
1104 string const realtmp = package().temp_dir().realPath();
1105 // We have to use os::path_prefix_is() here, instead of
1106 // simply prefixIs(), because the file name comes from
1107 // an external application and may need case adjustment.
1108 if (os::path_prefix_is(file_name, abstmp, os::CASE_ADJUSTED)
1109 || os::path_prefix_is(file_name, realtmp, os::CASE_ADJUSTED)) {
1110 // Needed by inverse dvi search. If it is a file
1111 // in tmpdir, call the apropriated function.
1112 // If tmpdir is a symlink, we may have the real
1113 // path passed back, so we correct for that.
1114 if (!prefixIs(file_name, abstmp))
1115 file_name = subst(file_name, realtmp, abstmp);
1116 buf = theBufferList().getBufferFromTmp(file_name);
1118 // Must replace extension of the file to be .lyx
1119 // and get full path
1120 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1121 // Either change buffer or load the file
1122 if (theBufferList().exists(s))
1123 buf = theBufferList().getBuffer(s);
1124 else if (s.exists()) {
1125 buf = lyx_view_->loadDocument(s);
1128 lyx_view_->message(bformat(
1129 _("File does not exist: %1$s"),
1130 makeDisplayPath(file_name)));
1134 updateFlags = Update::None;
1138 buf->updateLabels();
1139 lyx_view_->setBuffer(buf);
1140 view()->setCursorFromRow(row);
1142 buf->errors("Parse");
1143 updateFlags = Update::FitCursor;
1148 case LFUN_DIALOG_SHOW_NEW_INSET: {
1149 LASSERT(lyx_view_, /**/);
1150 string const name = cmd.getArg(0);
1151 InsetCode code = insetCode(name);
1152 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1153 bool insetCodeOK = true;
1160 case NOMENCL_PRINT_CODE:
1163 case HYPERLINK_CODE: {
1164 InsetCommandParams p(code);
1165 data = InsetCommand::params2string(name, p);
1168 case INCLUDE_CODE: {
1169 // data is the include type: one of "include",
1170 // "input", "verbatiminput" or "verbatiminput*"
1172 // default type is requested
1174 InsetCommandParams p(INCLUDE_CODE, data);
1175 data = InsetCommand::params2string("include", p);
1179 // \c data == "Boxed" || "Frameless" etc
1180 InsetBoxParams p(data);
1181 data = InsetBox::params2string(p);
1185 InsetBranchParams p;
1186 data = InsetBranch::params2string(p);
1190 InsetCommandParams p(CITE_CODE);
1191 data = InsetCommand::params2string(name, p);
1195 data = InsetERT::params2string(InsetCollapsable::Open);
1198 case EXTERNAL_CODE: {
1199 InsetExternalParams p;
1200 data = InsetExternal::params2string(p, *buffer);
1205 data = InsetFloat::params2string(p);
1208 case LISTINGS_CODE: {
1209 InsetListingsParams p;
1210 data = InsetListings::params2string(p);
1213 case GRAPHICS_CODE: {
1214 InsetGraphicsParams p;
1215 data = InsetGraphics::params2string(p, *buffer);
1220 data = InsetNote::params2string(p);
1223 case PHANTOM_CODE: {
1224 InsetPhantomParams p;
1225 data = InsetPhantom::params2string(p);
1230 data = InsetSpace::params2string(p);
1235 data = InsetVSpace::params2string(space);
1240 data = InsetWrap::params2string(p);
1244 lyxerr << "Inset type '" << name <<
1245 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1246 insetCodeOK = false;
1248 } // end switch(code)
1250 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1254 case LFUN_CITATION_INSERT: {
1255 LASSERT(lyx_view_, /**/);
1256 if (!argument.empty()) {
1257 // we can have one optional argument, delimited by '|'
1258 // citation-insert <key>|<text_before>
1259 // this should be enhanced to also support text_after
1260 // and citation style
1261 string arg = argument;
1263 if (contains(argument, "|")) {
1264 arg = token(argument, '|', 0);
1265 opt1 = token(argument, '|', 1);
1267 InsetCommandParams icp(CITE_CODE);
1268 icp["key"] = from_utf8(arg);
1270 icp["before"] = from_utf8(opt1);
1271 string icstr = InsetCommand::params2string("citation", icp);
1272 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1275 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1279 case LFUN_BUFFER_CHILD_OPEN: {
1280 LASSERT(lyx_view_ && buffer, /**/);
1281 FileName filename = makeAbsPath(argument, buffer->filePath());
1282 view()->saveBookmark(false);
1284 bool parsed = false;
1285 if (theBufferList().exists(filename)) {
1286 child = theBufferList().getBuffer(filename);
1288 setMessage(bformat(_("Opening child document %1$s..."),
1289 makeDisplayPath(filename.absFilename())));
1290 child = lyx_view_->loadDocument(filename, false);
1294 // Set the parent name of the child document.
1295 // This makes insertion of citations and references in the child work,
1296 // when the target is in the parent or another child document.
1297 child->setParent(buffer);
1298 child->masterBuffer()->updateLabels();
1299 lyx_view_->setBuffer(child);
1301 child->errors("Parse");
1304 // If a screen update is required (in case where auto_open is false),
1305 // setBuffer() would have taken care of it already. Otherwise we shall
1306 // reset the update flag because it can cause a circular problem.
1308 updateFlags = Update::None;
1312 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1313 LASSERT(lyx_view_, /**/);
1314 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1317 case LFUN_KEYMAP_OFF:
1318 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1319 lyx_view_->view()->getIntl().keyMapOn(false);
1322 case LFUN_KEYMAP_PRIMARY:
1323 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1324 lyx_view_->view()->getIntl().keyMapPrim();
1327 case LFUN_KEYMAP_SECONDARY:
1328 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1329 lyx_view_->view()->getIntl().keyMapSec();
1332 case LFUN_KEYMAP_TOGGLE:
1333 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1334 lyx_view_->view()->getIntl().toggleKeyMap();
1340 string rest = split(argument, countstr, ' ');
1341 istringstream is(countstr);
1344 //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1345 for (int i = 0; i < count; ++i)
1346 dispatch(lyxaction.lookupFunc(rest));
1350 case LFUN_COMMAND_SEQUENCE: {
1351 // argument contains ';'-terminated commands
1352 string arg = argument;
1353 if (theBufferList().isLoaded(buffer))
1354 buffer->undo().beginUndoGroup();
1355 while (!arg.empty()) {
1357 arg = split(arg, first, ';');
1358 FuncRequest func(lyxaction.lookupFunc(first));
1359 func.origin = cmd.origin;
1362 if (theBufferList().isLoaded(buffer))
1363 buffer->undo().endUndoGroup();
1367 case LFUN_COMMAND_ALTERNATIVES: {
1368 // argument contains ';'-terminated commands
1369 string arg = argument;
1370 while (!arg.empty()) {
1372 arg = split(arg, first, ';');
1373 FuncRequest func(lyxaction.lookupFunc(first));
1374 func.origin = cmd.origin;
1375 FuncStatus stat = getStatus(func);
1376 if (stat.enabled()) {
1386 if (theTopLevelCmdDef().lock(argument, func)) {
1387 func.origin = cmd.origin;
1389 theTopLevelCmdDef().release(argument);
1391 if (func.action == LFUN_UNKNOWN_ACTION) {
1392 // unknown command definition
1393 lyxerr << "Warning: unknown command definition `"
1397 // recursion detected
1398 lyxerr << "Warning: Recursion in the command definition `"
1399 << argument << "' detected"
1406 case LFUN_PREFERENCES_SAVE: {
1407 lyxrc.write(makeAbsPath("preferences",
1408 package().user_support().absFilename()),
1414 LASSERT(lyx_view_, /**/);
1415 lyx_view_->message(from_utf8(argument));
1418 case LFUN_BUFFER_LANGUAGE: {
1419 LASSERT(lyx_view_, /**/);
1420 Language const * oldL = buffer->params().language;
1421 Language const * newL = languages.getLanguage(argument);
1422 if (!newL || oldL == newL)
1425 if (oldL->rightToLeft() == newL->rightToLeft()
1426 && !buffer->isMultiLingual())
1427 buffer->changeLanguage(oldL, newL);
1431 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1432 string const fname =
1433 addName(addPath(package().user_support().absFilename(), "templates/"),
1435 Buffer defaults(fname);
1437 istringstream ss(argument);
1440 int const unknown_tokens = defaults.readHeader(lex);
1442 if (unknown_tokens != 0) {
1443 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1444 << unknown_tokens << " unknown token"
1445 << (unknown_tokens == 1 ? "" : "s")
1449 if (defaults.writeFile(FileName(defaults.absFileName())))
1450 setMessage(bformat(_("Document defaults saved in %1$s"),
1451 makeDisplayPath(fname)));
1453 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1457 case LFUN_BUFFER_PARAMS_APPLY: {
1458 LASSERT(lyx_view_, /**/);
1460 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1461 Cursor & cur = view()->cursor();
1462 cur.recordUndoFullDocument();
1464 istringstream ss(argument);
1467 int const unknown_tokens = buffer->readHeader(lex);
1469 if (unknown_tokens != 0) {
1470 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1471 << unknown_tokens << " unknown token"
1472 << (unknown_tokens == 1 ? "" : "s")
1476 updateLayout(oldClass, buffer);
1478 updateFlags = Update::Force | Update::FitCursor;
1479 // We are most certainly here because of a change in the document
1480 // It is then better to make sure that all dialogs are in sync with
1481 // current document settings. LyXView::restartCursor() achieve this.
1482 lyx_view_->restartCursor();
1486 case LFUN_LAYOUT_MODULES_CLEAR: {
1487 LASSERT(lyx_view_, /**/);
1488 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1489 view()->cursor().recordUndoFullDocument();
1490 buffer->params().clearLayoutModules();
1491 buffer->params().makeDocumentClass();
1492 updateLayout(oldClass, buffer);
1493 updateFlags = Update::Force | Update::FitCursor;
1497 case LFUN_LAYOUT_MODULE_ADD: {
1498 LASSERT(lyx_view_, /**/);
1499 BufferParams const & params = buffer->params();
1500 if (!params.moduleCanBeAdded(argument)) {
1501 LYXERR0("Module `" << argument <<
1502 "' cannot be added due to failed requirements or "
1503 "conflicts with installed modules.");
1506 DocumentClass const * const oldClass = params.documentClassPtr();
1507 view()->cursor().recordUndoFullDocument();
1508 buffer->params().addLayoutModule(argument);
1509 buffer->params().makeDocumentClass();
1510 updateLayout(oldClass, buffer);
1511 updateFlags = Update::Force | Update::FitCursor;
1515 case LFUN_TEXTCLASS_APPLY: {
1516 LASSERT(lyx_view_, /**/);
1518 if (!loadLayoutFile(argument, buffer->temppath()) &&
1519 !loadLayoutFile(argument, buffer->filePath()))
1522 LayoutFile const * old_layout = buffer->params().baseClass();
1523 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1525 if (old_layout == new_layout)
1529 //Save the old, possibly modular, layout for use in conversion.
1530 DocumentClass const * const oldDocClass = buffer->params().documentClassPtr();
1531 view()->cursor().recordUndoFullDocument();
1532 buffer->params().setBaseClass(argument);
1533 buffer->params().makeDocumentClass();
1534 updateLayout(oldDocClass, buffer);
1535 updateFlags = Update::Force | Update::FitCursor;
1539 case LFUN_LAYOUT_RELOAD: {
1540 LASSERT(lyx_view_, /**/);
1541 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1542 LayoutFileIndex bc = buffer->params().baseClassID();
1543 LayoutFileList::get().reset(bc);
1544 buffer->params().setBaseClass(bc);
1545 buffer->params().makeDocumentClass();
1546 updateLayout(oldClass, buffer);
1547 updateFlags = Update::Force | Update::FitCursor;
1551 case LFUN_TEXTCLASS_LOAD:
1552 loadLayoutFile(argument, buffer->temppath()) ||
1553 loadLayoutFile(argument, buffer->filePath());
1556 case LFUN_LYXRC_APPLY: {
1557 LyXRC const lyxrc_orig = lyxrc;
1559 istringstream ss(argument);
1560 bool const success = lyxrc.read(ss) == 0;
1563 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1564 << "Unable to read lyxrc data"
1569 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1571 theApp()->resetGui();
1573 /// We force the redraw in any case because there might be
1574 /// some screen font changes.
1575 /// FIXME: only the current view will be updated. the Gui
1576 /// class is able to furnish the list of views.
1577 updateFlags = Update::Force;
1581 case LFUN_BOOKMARK_GOTO:
1582 // go to bookmark, open unopened file and switch to buffer if necessary
1583 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1584 updateFlags = Update::FitCursor;
1587 case LFUN_BOOKMARK_CLEAR:
1588 theSession().bookmarks().clear();
1591 case LFUN_VC_COMMAND: {
1592 string flag = cmd.getArg(0);
1593 if (buffer && contains(flag, 'R') && !ensureBufferClean(view()))
1596 if (contains(flag, 'M'))
1597 if (!Alert::askForText(message, _("LyX VC: Log Message")))
1600 string path = cmd.getArg(1);
1601 if (contains(path, "$$p") && buffer)
1602 path = subst(path, "$$p", buffer->filePath());
1603 LYXERR(Debug::LYXVC, "Directory: " << path);
1605 if (!pp.isReadableDirectory()) {
1606 lyxerr << _("Directory is not accessible.") << endl;
1609 support::PathChanger p(pp);
1611 string command = cmd.getArg(2);
1612 if (command.empty())
1615 command = subst(command, "$$i", buffer->absFileName());
1616 command = subst(command, "$$p", buffer->filePath());
1618 command = subst(command, "$$m", to_utf8(message));
1619 LYXERR(Debug::LYXVC, "Command: " << command);
1621 one.startscript(Systemcall::Wait, command);
1625 if (contains(flag, 'I'))
1626 buffer->markDirty();
1627 if (contains(flag, 'R'))
1634 LASSERT(theApp(), /**/);
1635 // Let the frontend dispatch its own actions.
1636 if (theApp()->dispatch(cmd))
1637 // Nothing more to do.
1640 // Everything below is only for active lyx_view_
1644 // Start an undo group. This may be needed for
1645 // some stuff like inset-apply on labels.
1646 if (theBufferList().isLoaded(buffer))
1647 buffer->undo().beginUndoGroup();
1649 // Let the current LyXView dispatch its own actions.
1650 if (lyx_view_->dispatch(cmd)) {
1651 if (lyx_view_->view()) {
1652 updateFlags = lyx_view_->view()->cursor().result().update();
1653 if (theBufferList().isLoaded(buffer))
1654 buffer->undo().endUndoGroup();
1659 LASSERT(lyx_view_->view(), /**/);
1661 // Let the current BufferView dispatch its own actions.
1662 if (view()->dispatch(cmd)) {
1663 // The BufferView took care of its own updates if needed.
1664 updateFlags = Update::None;
1665 if (theBufferList().isLoaded(buffer))
1666 buffer->undo().endUndoGroup();
1670 // OK, so try the Buffer itself
1672 view()->buffer().dispatch(cmd, dr);
1673 if (dr.dispatched()) {
1674 updateFlags = dr.update();
1678 // Is this a function that acts on inset at point?
1679 Inset * inset = view()->cursor().nextInset();
1680 if (lyxaction.funcHasFlag(action, LyXAction::AtPoint)
1682 view()->cursor().result().dispatched(true);
1683 view()->cursor().result().update(Update::FitCursor | Update::Force);
1684 FuncRequest tmpcmd = cmd;
1685 inset->dispatch(view()->cursor(), tmpcmd);
1686 if (view()->cursor().result().dispatched()) {
1687 updateFlags = view()->cursor().result().update();
1692 // Let the current Cursor dispatch its own actions.
1693 Cursor old = view()->cursor();
1694 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1695 cursorPosBeforeDispatchY_);
1696 view()->cursor().dispatch(cmd);
1698 // notify insets we just left
1699 if (view()->cursor() != old) {
1701 bool badcursor = notifyCursorLeavesOrEnters(old, view()->cursor());
1703 view()->cursor().fixIfBroken();
1706 if (theBufferList().isLoaded(buffer))
1707 buffer->undo().endUndoGroup();
1709 // update completion. We do it here and not in
1710 // processKeySym to avoid another redraw just for a
1711 // changed inline completion
1712 if (cmd.origin == FuncRequest::KEYBOARD) {
1713 if (cmd.action == LFUN_SELF_INSERT)
1714 lyx_view_->updateCompletion(view()->cursor(), true, true);
1715 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1716 lyx_view_->updateCompletion(view()->cursor(), false, true);
1718 lyx_view_->updateCompletion(view()->cursor(), false, false);
1721 updateFlags = view()->cursor().result().update();
1724 // if we executed a mutating lfun, mark the buffer as dirty
1725 if (theBufferList().isLoaded(buffer) && flag.enabled()
1726 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1727 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1728 buffer->markDirty();
1730 if (lyx_view_ && lyx_view_->buffer()) {
1731 // BufferView::update() updates the ViewMetricsInfo and
1732 // also initializes the position cache for all insets in
1733 // (at least partially) visible top-level paragraphs.
1734 // We will redraw the screen only if needed.
1735 view()->processUpdateFlags(updateFlags);
1737 // Do we have a selection?
1738 theSelection().haveSelection(view()->cursor().selection());
1741 lyx_view_->restartCursor();
1745 // Some messages may already be translated, so we cannot use _()
1746 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1751 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1753 const bool verbose = (cmd.origin == FuncRequest::MENU
1754 || cmd.origin == FuncRequest::TOOLBAR
1755 || cmd.origin == FuncRequest::COMMANDBUFFER);
1757 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1758 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1760 lyx_view_->message(msg);
1764 docstring dispatch_msg = msg;
1765 if (!dispatch_msg.empty())
1766 dispatch_msg += ' ';
1768 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1770 bool argsadded = false;
1772 if (!cmd.argument().empty()) {
1773 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1774 comname += ' ' + cmd.argument();
1779 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd, KeySequence::ForGui);
1781 if (!shortcuts.empty())
1782 comname += ": " + shortcuts;
1783 else if (!argsadded && !cmd.argument().empty())
1784 comname += ' ' + cmd.argument();
1786 if (!comname.empty()) {
1787 comname = rtrim(comname);
1788 dispatch_msg += '(' + rtrim(comname) + ')';
1791 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1792 if (!dispatch_msg.empty())
1793 lyx_view_->message(dispatch_msg);
1797 void LyXFunc::reloadBuffer()
1799 FileName filename = lyx_view_->buffer()->fileName();
1800 // The user has already confirmed that the changes, if any, should
1801 // be discarded. So we just release the Buffer and don't call closeBuffer();
1802 theBufferList().release(lyx_view_->buffer());
1803 // if the lyx_view_ has been destroyed, create a new one
1805 theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
1806 Buffer * buf = lyx_view_->loadDocument(filename);
1807 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1810 buf->updateLabels();
1811 lyx_view_->setBuffer(buf);
1812 buf->errors("Parse");
1813 str = bformat(_("Document %1$s reloaded."), disp_fn);
1815 str = bformat(_("Could not reload document %1$s"), disp_fn);
1817 lyx_view_->message(str);
1820 // Each "lyx_view_" should have it's own message method. lyxview and
1821 // the minibuffer would use the minibuffer, but lyxserver would
1822 // send an ERROR signal to its client. Alejandro 970603
1823 // This function is bit problematic when it comes to NLS, to make the
1824 // lyx servers client be language indepenent we must not translate
1825 // strings sent to this func.
1826 void LyXFunc::setErrorMessage(docstring const & m) const
1828 dispatch_buffer = m;
1833 void LyXFunc::setMessage(docstring const & m) const
1835 dispatch_buffer = m;
1839 docstring LyXFunc::viewStatusMessage()
1841 // When meta-fake key is pressed, show the key sequence so far + "M-".
1843 return keyseq.print(KeySequence::ForGui) + "M-";
1845 // Else, when a non-complete key sequence is pressed,
1846 // show the available options.
1847 if (keyseq.length() > 0 && !keyseq.deleted())
1848 return keyseq.printOptions(true);
1850 LASSERT(lyx_view_, /**/);
1851 if (!lyx_view_->buffer())
1852 return _("Welcome to LyX!");
1854 return view()->cursor().currentState();
1858 BufferView * LyXFunc::view() const
1860 LASSERT(lyx_view_, /**/);
1861 return lyx_view_->view();
1865 bool LyXFunc::wasMetaKey() const
1867 return (meta_fake_bit != NoModifier);
1871 void LyXFunc::updateLayout(DocumentClass const * const oldlayout, Buffer * buf)
1873 lyx_view_->message(_("Converting document to new document class..."));
1875 StableDocIterator backcur(view()->cursor());
1876 ErrorList & el = buf->errorList("Class Switch");
1877 cap::switchBetweenClasses(
1878 oldlayout, buf->params().documentClassPtr(),
1879 static_cast<InsetText &>(buf->inset()), el);
1881 view()->setCursor(backcur.asDocIterator(buf));
1883 buf->errors("Class Switch");
1884 buf->updateLabels();
1890 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1892 // Why the switch you might ask. It is a trick to ensure that all
1893 // the elements in the LyXRCTags enum is handled. As you can see
1894 // there are no breaks at all. So it is just a huge fall-through.
1895 // The nice thing is that we will get a warning from the compiler
1896 // if we forget an element.
1897 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1899 case LyXRC::RC_ACCEPT_COMPOUND:
1900 case LyXRC::RC_ALT_LANG:
1901 case LyXRC::RC_PLAINTEXT_LINELEN:
1902 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1903 case LyXRC::RC_AUTOCORRECTION_MATH:
1904 case LyXRC::RC_AUTOREGIONDELETE:
1905 case LyXRC::RC_AUTORESET_OPTIONS:
1906 case LyXRC::RC_AUTOSAVE:
1907 case LyXRC::RC_AUTO_NUMBER:
1908 case LyXRC::RC_BACKUPDIR_PATH:
1909 case LyXRC::RC_BIBTEX_ALTERNATIVES:
1910 case LyXRC::RC_BIBTEX_COMMAND:
1911 case LyXRC::RC_BINDFILE:
1912 case LyXRC::RC_CHECKLASTFILES:
1913 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1914 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1915 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1916 case LyXRC::RC_COMPLETION_INLINE_MATH:
1917 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1918 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1919 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1920 case LyXRC::RC_COMPLETION_POPUP_MATH:
1921 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1922 case LyXRC::RC_USELASTFILEPOS:
1923 case LyXRC::RC_LOADSESSION:
1924 case LyXRC::RC_CHKTEX_COMMAND:
1925 case LyXRC::RC_CONVERTER:
1926 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1927 case LyXRC::RC_COPIER:
1928 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1929 case LyXRC::RC_SCROLL_BELOW_DOCUMENT:
1930 case LyXRC::RC_DATE_INSERT_FORMAT:
1931 case LyXRC::RC_DEFAULT_LANGUAGE:
1932 case LyXRC::RC_GUI_LANGUAGE:
1933 case LyXRC::RC_DEFAULT_PAPERSIZE:
1934 case LyXRC::RC_DEFAULT_VIEW_FORMAT:
1935 case LyXRC::RC_DEFFILE:
1936 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1937 case LyXRC::RC_DISPLAY_GRAPHICS:
1938 case LyXRC::RC_DOCUMENTPATH:
1939 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1940 FileName path(lyxrc_new.document_path);
1941 if (path.exists() && path.isDirectory())
1942 package().document_dir() = FileName(lyxrc.document_path);
1944 case LyXRC::RC_ESC_CHARS:
1945 case LyXRC::RC_EXAMPLEPATH:
1946 case LyXRC::RC_FONT_ENCODING:
1947 case LyXRC::RC_FORMAT:
1948 case LyXRC::RC_GROUP_LAYOUTS:
1949 case LyXRC::RC_INDEX_ALTERNATIVES:
1950 case LyXRC::RC_INDEX_COMMAND:
1951 case LyXRC::RC_JBIBTEX_COMMAND:
1952 case LyXRC::RC_JINDEX_COMMAND:
1953 case LyXRC::RC_NOMENCL_COMMAND:
1954 case LyXRC::RC_INPUT:
1955 case LyXRC::RC_KBMAP:
1956 case LyXRC::RC_KBMAP_PRIMARY:
1957 case LyXRC::RC_KBMAP_SECONDARY:
1958 case LyXRC::RC_LABEL_INIT_LENGTH:
1959 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1960 case LyXRC::RC_LANGUAGE_AUTO_END:
1961 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1962 case LyXRC::RC_LANGUAGE_COMMAND_END:
1963 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1964 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1965 case LyXRC::RC_LANGUAGE_PACKAGE:
1966 case LyXRC::RC_LANGUAGE_USE_BABEL:
1967 case LyXRC::RC_MAC_LIKE_WORD_MOVEMENT:
1968 case LyXRC::RC_MACRO_EDIT_STYLE:
1969 case LyXRC::RC_MAKE_BACKUP:
1970 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1971 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1972 case LyXRC::RC_NUMLASTFILES:
1973 case LyXRC::RC_PATH_PREFIX:
1974 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1975 prependEnvPath("PATH", lyxrc.path_prefix);
1977 case LyXRC::RC_PERS_DICT:
1978 case LyXRC::RC_PREVIEW:
1979 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1980 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1981 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1982 case LyXRC::RC_PRINTCOPIESFLAG:
1983 case LyXRC::RC_PRINTER:
1984 case LyXRC::RC_PRINTEVENPAGEFLAG:
1985 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1986 case LyXRC::RC_PRINTFILEEXTENSION:
1987 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1988 case LyXRC::RC_PRINTODDPAGEFLAG:
1989 case LyXRC::RC_PRINTPAGERANGEFLAG:
1990 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1991 case LyXRC::RC_PRINTPAPERFLAG:
1992 case LyXRC::RC_PRINTREVERSEFLAG:
1993 case LyXRC::RC_PRINTSPOOL_COMMAND:
1994 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1995 case LyXRC::RC_PRINTTOFILE:
1996 case LyXRC::RC_PRINTTOPRINTER:
1997 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1998 case LyXRC::RC_PRINT_COMMAND:
1999 case LyXRC::RC_RTL_SUPPORT:
2000 case LyXRC::RC_SCREEN_DPI:
2001 case LyXRC::RC_SCREEN_FONT_ROMAN:
2002 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
2003 case LyXRC::RC_SCREEN_FONT_SANS:
2004 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
2005 case LyXRC::RC_SCREEN_FONT_SCALABLE:
2006 case LyXRC::RC_SCREEN_FONT_SIZES:
2007 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
2008 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
2009 case LyXRC::RC_GEOMETRY_SESSION:
2010 case LyXRC::RC_SCREEN_ZOOM:
2011 case LyXRC::RC_SERVERPIPE:
2012 case LyXRC::RC_SET_COLOR:
2013 case LyXRC::RC_SHOW_BANNER:
2014 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
2015 case LyXRC::RC_SPELL_COMMAND:
2016 case LyXRC::RC_SPELLCHECK_CONTINUOUSLY:
2017 case LyXRC::RC_SPLITINDEX_COMMAND:
2018 case LyXRC::RC_TEMPDIRPATH:
2019 case LyXRC::RC_TEMPLATEPATH:
2020 case LyXRC::RC_TEX_ALLOWS_SPACES:
2021 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2022 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2023 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2025 case LyXRC::RC_THESAURUSDIRPATH:
2026 case LyXRC::RC_UIFILE:
2027 case LyXRC::RC_USER_EMAIL:
2028 case LyXRC::RC_USER_NAME:
2029 case LyXRC::RC_USETEMPDIR:
2030 case LyXRC::RC_USE_ALT_LANG:
2031 case LyXRC::RC_USE_CONVERTER_CACHE:
2032 case LyXRC::RC_USE_ESC_CHARS:
2033 case LyXRC::RC_USE_INP_ENC:
2034 case LyXRC::RC_USE_PERS_DICT:
2035 case LyXRC::RC_USE_TOOLTIP:
2036 case LyXRC::RC_USE_PIXMAP_CACHE:
2037 case LyXRC::RC_USE_SPELL_LIB:
2038 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2039 case LyXRC::RC_SORT_LAYOUTS:
2040 case LyXRC::RC_FULL_SCREEN_LIMIT:
2041 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2042 case LyXRC::RC_FULL_SCREEN_MENUBAR:
2043 case LyXRC::RC_FULL_SCREEN_TABBAR:
2044 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2045 case LyXRC::RC_FULL_SCREEN_WIDTH:
2046 case LyXRC::RC_VISUAL_CURSOR:
2047 case LyXRC::RC_VIEWER:
2048 case LyXRC::RC_LAST: