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()->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:
411 #if !defined(HAVE_LIBMYTHES) && !defined(HAVE_LIBAIKSAURUS)
412 case LFUN_THESAURUS_ENTRY:
415 flag.setEnabled(false);
422 if (flag.unknown()) {
423 flag.message(from_utf8(N_("Unknown action")));
427 if (!flag.enabled()) {
428 if (flag.message().empty())
429 flag.message(from_utf8(N_("Command disabled")));
433 // Check whether we need a buffer
434 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
436 flag.message(from_utf8(N_("Command not allowed with"
437 "out any document open")));
438 flag.setEnabled(false);
442 // I would really like to avoid having this switch and rather try to
443 // encode this in the function itself.
444 // -- And I'd rather let an inset decide which LFUNs it is willing
445 // to handle (Andre')
447 switch (cmd.action) {
449 case LFUN_BUFFER_TOGGLE_READ_ONLY:
450 flag.setOnOff(buf->isReadonly());
453 case LFUN_BUFFER_SWITCH:
454 // toggle on the current buffer, but do not toggle off
455 // the other ones (is that a good idea?)
456 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
460 case LFUN_BUFFER_EXPORT:
461 enable = cmd.argument() == "custom"
462 || buf->isExportable(to_utf8(cmd.argument()));
465 case LFUN_BUFFER_CHKTEX:
466 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
469 case LFUN_BUILD_PROGRAM:
470 enable = buf->isExportable("program");
473 case LFUN_VC_REGISTER:
474 enable = !buf->lyxvc().inUse();
476 case LFUN_VC_CHECK_IN:
477 enable = buf->lyxvc().checkInEnabled();
479 case LFUN_VC_CHECK_OUT:
480 enable = buf->lyxvc().checkOutEnabled();
483 enable = buf->lyxvc().inUse();
485 case LFUN_VC_UNDO_LAST:
486 enable = buf->lyxvc().undoLastEnabled();
488 case LFUN_BUFFER_RELOAD:
489 enable = !buf->isUnnamed() && buf->fileName().exists()
490 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
493 case LFUN_CITATION_INSERT: {
494 FuncRequest fr(LFUN_INSET_INSERT, "citation");
495 enable = getStatus(fr).enabled();
499 // This could be used for the no-GUI version. The GUI version is handled in
500 // LyXView::getStatus(). See above.
502 case LFUN_BUFFER_WRITE:
503 case LFUN_BUFFER_WRITE_AS: {
504 Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
505 enable = b && (b->isUnnamed() || !b->isClean());
510 case LFUN_BUFFER_WRITE_ALL: {
511 // We enable the command only if there are some modified buffers
512 Buffer * first = theBufferList().first();
517 // We cannot use a for loop as the buffer list is a cycle.
523 b = theBufferList().next(b);
524 } while (b != first);
528 case LFUN_BOOKMARK_GOTO: {
529 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
530 enable = theSession().bookmarks().isValid(num);
534 case LFUN_BOOKMARK_CLEAR:
535 enable = theSession().bookmarks().hasValid();
538 // this one is difficult to get right. As a half-baked
539 // solution, we consider only the first action of the sequence
540 case LFUN_COMMAND_SEQUENCE: {
541 // argument contains ';'-terminated commands
542 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
543 FuncRequest func(lyxaction.lookupFunc(firstcmd));
544 func.origin = cmd.origin;
545 flag = getStatus(func);
549 // we want to check if at least one of these is enabled
550 case LFUN_COMMAND_ALTERNATIVES: {
551 // argument contains ';'-terminated commands
552 string arg = to_utf8(cmd.argument());
553 while (!arg.empty()) {
555 arg = split(arg, first, ';');
556 FuncRequest func(lyxaction.lookupFunc(first));
557 func.origin = cmd.origin;
558 flag = getStatus(func);
559 // if this one is enabled, the whole thing is
568 string name = to_utf8(cmd.argument());
569 if (theTopLevelCmdDef().lock(name, func)) {
570 func.origin = cmd.origin;
571 flag = getStatus(func);
572 theTopLevelCmdDef().release(name);
574 // catch recursion or unknown command definiton
575 // all operations until the recursion or unknown command
576 // definiton occures are performed, so set the state to enabled
582 case LFUN_VC_COMMAND: {
583 if (cmd.argument().empty())
586 if (!buf && contains(cmd.getArg(0), 'D'))
591 case LFUN_WORD_FIND_FORWARD:
592 case LFUN_WORD_FIND_BACKWARD:
593 case LFUN_WORD_FINDADV:
594 case LFUN_COMMAND_PREFIX:
595 case LFUN_COMMAND_EXECUTE:
597 case LFUN_META_PREFIX:
598 case LFUN_BUFFER_CLOSE:
599 case LFUN_BUFFER_UPDATE:
600 case LFUN_BUFFER_VIEW:
601 case LFUN_MASTER_BUFFER_UPDATE:
602 case LFUN_MASTER_BUFFER_VIEW:
603 case LFUN_BUFFER_IMPORT:
604 case LFUN_BUFFER_AUTO_SAVE:
605 case LFUN_RECONFIGURE:
607 case LFUN_DROP_LAYOUTS_CHOICE:
609 case LFUN_SERVER_GET_FILENAME:
610 case LFUN_SERVER_NOTIFY:
611 case LFUN_SERVER_GOTO_FILE_ROW:
612 case LFUN_DIALOG_HIDE:
613 case LFUN_DIALOG_DISCONNECT_INSET:
614 case LFUN_BUFFER_CHILD_OPEN:
615 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
616 case LFUN_KEYMAP_OFF:
617 case LFUN_KEYMAP_PRIMARY:
618 case LFUN_KEYMAP_SECONDARY:
619 case LFUN_KEYMAP_TOGGLE:
621 case LFUN_BUFFER_EXPORT_CUSTOM:
622 case LFUN_BUFFER_PRINT:
623 case LFUN_PREFERENCES_SAVE:
625 case LFUN_INSET_EDIT:
626 case LFUN_BUFFER_LANGUAGE:
627 case LFUN_TEXTCLASS_APPLY:
628 case LFUN_TEXTCLASS_LOAD:
629 case LFUN_BUFFER_SAVE_AS_DEFAULT:
630 case LFUN_BUFFER_PARAMS_APPLY:
631 case LFUN_LAYOUT_MODULES_CLEAR:
632 case LFUN_LAYOUT_MODULE_ADD:
633 case LFUN_LAYOUT_RELOAD:
634 case LFUN_LYXRC_APPLY:
635 case LFUN_BUFFER_NEXT:
636 case LFUN_BUFFER_PREVIOUS:
637 // these are handled in our dispatch()
645 if (theApp()->getStatus(cmd, flag))
648 // Does the view know something?
653 if (lyx_view_->getStatus(cmd, flag))
656 // If we have a BufferView, try cursor position and
657 // then the BufferView.
663 // Is this a function that acts on inset at point?
664 Inset * inset = view()->cursor().nextInset();
665 if (lyxaction.funcHasFlag(cmd.action, LyXAction::AtPoint)
666 && inset && inset->getStatus(view()->cursor(), cmd, flag))
669 if (!getLocalStatus(view()->cursor(), cmd, flag))
670 flag = view()->getStatus(cmd);
674 flag.setEnabled(false);
676 // Can we use a readonly buffer?
677 if (buf && buf->isReadonly()
678 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
679 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
680 flag.message(from_utf8(N_("Document is read-only")));
681 flag.setEnabled(false);
684 // Are we in a DELETED change-tracking region?
686 && lookupChangeType(view()->cursor(), true) == Change::DELETED
687 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
688 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
689 flag.message(from_utf8(N_("This portion of the document is deleted.")));
690 flag.setEnabled(false);
693 // the default error message if we disable the command
694 if (!flag.enabled() && flag.message().empty())
695 flag.message(from_utf8(N_("Command disabled")));
701 bool LyXFunc::ensureBufferClean(BufferView * bv)
703 Buffer & buf = bv->buffer();
704 if (buf.isClean() && !buf.isUnnamed())
707 docstring const file = buf.fileName().displayName(30);
710 if (!buf.isUnnamed()) {
711 text = bformat(_("The document %1$s has unsaved "
712 "changes.\n\nDo you want to save "
713 "the document?"), file);
714 title = _("Save changed document?");
717 text = bformat(_("The document %1$s has not been "
718 "saved yet.\n\nDo you want to save "
719 "the document?"), file);
720 title = _("Save new document?");
722 int const ret = Alert::prompt(title, text, 0, 1, _("&Save"), _("&Cancel"));
725 lyx_view_->dispatch(FuncRequest(LFUN_BUFFER_WRITE));
727 return buf.isClean() && !buf.isUnnamed();
733 void showPrintError(string const & name)
735 docstring str = bformat(_("Could not print the document %1$s.\n"
736 "Check that your printer is set up correctly."),
737 makeDisplayPath(name, 50));
738 Alert::error(_("Print document failed"), str);
742 bool loadLayoutFile(string const & name, string const & buf_path)
744 if (!LayoutFileList::get().haveClass(name)) {
745 lyxerr << "Document class \"" << name
746 << "\" does not exist."
751 LayoutFile & tc = LayoutFileList::get()[name];
752 if (!tc.load(buf_path)) {
753 docstring s = bformat(_("The document class %1$s "
754 "could not be loaded."), from_utf8(name));
755 Alert::error(_("Could not load class"), s);
762 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
767 void LyXFunc::dispatch(FuncRequest const & cmd)
769 string const argument = to_utf8(cmd.argument());
770 FuncCode const action = cmd.action;
772 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
773 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
775 // we have not done anything wrong yet.
777 dispatch_buffer.erase();
779 // redraw the screen at the end (first of the two drawing steps).
780 //This is done unless explicitely requested otherwise
781 Update::flags updateFlags = Update::FitCursor;
783 FuncStatus const flag = getStatus(cmd);
784 if (!flag.enabled()) {
785 // We cannot use this function here
786 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
787 << lyxaction.getActionName(action)
788 << " [" << action << "] is disabled at this location");
789 setErrorMessage(flag.message());
791 lyx_view_->restartCursor();
793 Buffer * buffer = lyx_view_ ? lyx_view_->buffer() : 0;
796 case LFUN_WORD_FIND_FORWARD:
797 case LFUN_WORD_FIND_BACKWARD: {
798 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
799 static docstring last_search;
800 docstring searched_string;
802 if (!cmd.argument().empty()) {
803 last_search = cmd.argument();
804 searched_string = cmd.argument();
806 searched_string = last_search;
809 if (searched_string.empty())
812 bool const fw = action == LFUN_WORD_FIND_FORWARD;
813 docstring const data =
814 find2string(searched_string, true, false, fw);
815 find(view(), FuncRequest(LFUN_WORD_FIND, data));
819 case LFUN_COMMAND_PREFIX:
820 LASSERT(lyx_view_, /**/);
821 lyx_view_->message(keyseq.printOptions(true));
825 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
827 meta_fake_bit = NoModifier;
829 // cancel any selection
830 dispatch(FuncRequest(LFUN_MARK_OFF));
831 setMessage(from_ascii(N_("Cancel")));
834 case LFUN_META_PREFIX:
835 meta_fake_bit = AltModifier;
836 setMessage(keyseq.print(KeySequence::ForGui));
839 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
840 LASSERT(lyx_view_ && lyx_view_->view() && buffer, /**/);
841 if (buffer->lyxvc().inUse())
842 buffer->lyxvc().toggleReadOnly();
844 buffer->setReadonly(!buffer->isReadonly());
848 // --- Menus -----------------------------------------------
849 case LFUN_BUFFER_CLOSE:
850 lyx_view_->closeBuffer();
852 updateFlags = Update::None;
855 case LFUN_BUFFER_RELOAD: {
856 LASSERT(lyx_view_ && buffer, /**/);
857 docstring const file = makeDisplayPath(buffer->absFileName(), 20);
858 docstring text = bformat(_("Any changes will be lost. Are you sure "
859 "you want to revert to the saved version of the document %1$s?"), file);
860 int const ret = Alert::prompt(_("Revert to saved document?"),
861 text, 1, 1, _("&Revert"), _("&Cancel"));
868 case LFUN_BUFFER_UPDATE:
869 LASSERT(lyx_view_ && buffer, /**/);
870 buffer->doExport(argument, true);
873 case LFUN_BUFFER_VIEW:
874 LASSERT(lyx_view_ && buffer, /**/);
875 buffer->preview(argument);
878 case LFUN_MASTER_BUFFER_UPDATE:
879 LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
880 buffer->masterBuffer()->doExport(argument, true);
883 case LFUN_MASTER_BUFFER_VIEW:
884 LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
885 buffer->masterBuffer()->preview(argument);
888 case LFUN_BUILD_PROGRAM:
889 LASSERT(lyx_view_ && buffer, /**/);
890 buffer->doExport("program", true);
893 case LFUN_BUFFER_CHKTEX:
894 LASSERT(lyx_view_ && buffer, /**/);
898 case LFUN_BUFFER_EXPORT:
899 LASSERT(lyx_view_ && buffer, /**/);
900 if (argument == "custom")
901 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
903 buffer->doExport(argument, false);
906 case LFUN_BUFFER_EXPORT_CUSTOM: {
907 LASSERT(lyx_view_ && buffer, /**/);
909 string command = split(argument, format_name, ' ');
910 Format const * format = formats.getFormat(format_name);
912 lyxerr << "Format \"" << format_name
913 << "\" not recognized!"
918 // The name of the file created by the conversion process
921 // Output to filename
922 if (format->name() == "lyx") {
923 string const latexname = buffer->latexName(false);
924 filename = changeExtension(latexname,
925 format->extension());
926 filename = addName(buffer->temppath(), filename);
928 if (!buffer->writeFile(FileName(filename)))
932 buffer->doExport(format_name, true, filename);
935 // Substitute $$FName for filename
936 if (!contains(command, "$$FName"))
937 command = "( " + command + " ) < $$FName";
938 command = subst(command, "$$FName", filename);
940 // Execute the command in the background
942 call.startscript(Systemcall::DontWait, command);
946 case LFUN_BUFFER_PRINT: {
947 LASSERT(lyx_view_ && buffer, /**/);
948 // FIXME: cmd.getArg() might fail if one of the arguments
949 // contains double quotes
950 string target = cmd.getArg(0);
951 string target_name = cmd.getArg(1);
952 string command = cmd.getArg(2);
955 || target_name.empty()
956 || command.empty()) {
957 lyxerr << "Unable to parse \""
958 << argument << '"' << endl;
961 if (target != "printer" && target != "file") {
962 lyxerr << "Unrecognized target \""
963 << target << '"' << endl;
967 if (!buffer->doExport("dvi", true)) {
968 showPrintError(buffer->absFileName());
972 // Push directory path.
973 string const path = buffer->temppath();
974 // Prevent the compiler from optimizing away p
978 // there are three cases here:
979 // 1. we print to a file
980 // 2. we print directly to a printer
981 // 3. we print using a spool command (print to file first)
984 string const dviname =
985 changeExtension(buffer->latexName(true), "dvi");
987 if (target == "printer") {
988 if (!lyxrc.print_spool_command.empty()) {
989 // case 3: print using a spool
990 string const psname =
991 changeExtension(dviname,".ps");
992 command += ' ' + lyxrc.print_to_file
995 + quoteName(dviname);
998 lyxrc.print_spool_command + ' ';
999 if (target_name != "default") {
1000 command2 += lyxrc.print_spool_printerprefix
1004 command2 += quoteName(psname);
1006 // If successful, then spool command
1007 res = one.startscript(
1012 res = one.startscript(
1013 Systemcall::DontWait,
1016 // case 2: print directly to a printer
1017 if (target_name != "default")
1018 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
1019 res = one.startscript(
1020 Systemcall::DontWait,
1021 command + quoteName(dviname));
1025 // case 1: print to a file
1026 FileName const filename(makeAbsPath(target_name,
1027 buffer->filePath()));
1028 FileName const dvifile(makeAbsPath(dviname, path));
1029 if (filename.exists()) {
1030 docstring text = bformat(
1031 _("The file %1$s already exists.\n\n"
1032 "Do you want to overwrite that file?"),
1033 makeDisplayPath(filename.absFilename()));
1034 if (Alert::prompt(_("Overwrite file?"),
1035 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1038 command += ' ' + lyxrc.print_to_file
1039 + quoteName(filename.toFilesystemEncoding())
1041 + quoteName(dvifile.toFilesystemEncoding());
1042 res = one.startscript(Systemcall::DontWait,
1047 showPrintError(buffer->absFileName());
1051 // FIXME: There is need for a command-line import.
1053 case LFUN_BUFFER_IMPORT:
1058 case LFUN_BUFFER_AUTO_SAVE:
1062 case LFUN_RECONFIGURE:
1063 // argument is any additional parameter to the configure.py command
1064 reconfigure(lyx_view_, argument);
1067 case LFUN_HELP_OPEN: {
1069 theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
1070 string const arg = argument;
1072 setErrorMessage(from_utf8(N_("Missing argument")));
1075 FileName fname = i18nLibFileSearch("doc", arg, "lyx");
1077 fname = i18nLibFileSearch("examples", arg, "lyx");
1079 if (fname.empty()) {
1080 lyxerr << "LyX: unable to find documentation file `"
1081 << arg << "'. Bad installation?" << endl;
1084 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1085 makeDisplayPath(fname.absFilename())));
1086 Buffer * buf = lyx_view_->loadDocument(fname, false);
1088 buf->updateLabels();
1089 lyx_view_->setBuffer(buf);
1090 buf->errors("Parse");
1092 updateFlags = Update::None;
1096 // --- version control -------------------------------
1097 case LFUN_VC_REGISTER:
1098 LASSERT(lyx_view_ && buffer, /**/);
1099 if (!ensureBufferClean(view()))
1101 if (!buffer->lyxvc().inUse()) {
1102 if (buffer->lyxvc().registrer())
1105 updateFlags = Update::Force;
1108 case LFUN_VC_CHECK_IN:
1109 LASSERT(lyx_view_ && buffer, /**/);
1110 if (!ensureBufferClean(view()))
1112 if (buffer->lyxvc().inUse()
1113 && !buffer->isReadonly()) {
1114 setMessage(from_utf8(buffer->lyxvc().checkIn()));
1119 case LFUN_VC_CHECK_OUT:
1120 LASSERT(lyx_view_ && buffer, /**/);
1121 if (!ensureBufferClean(view()))
1123 if (buffer->lyxvc().inUse()) {
1124 setMessage(from_utf8(buffer->lyxvc().checkOut()));
1129 case LFUN_VC_REVERT:
1130 LASSERT(lyx_view_ && buffer, /**/);
1131 buffer->lyxvc().revert();
1135 case LFUN_VC_UNDO_LAST:
1136 LASSERT(lyx_view_ && buffer, /**/);
1137 buffer->lyxvc().undoLast();
1141 // --- lyxserver commands ----------------------------
1142 case LFUN_SERVER_GET_FILENAME:
1143 LASSERT(lyx_view_ && buffer, /**/);
1144 setMessage(from_utf8(buffer->absFileName()));
1145 LYXERR(Debug::INFO, "FNAME["
1146 << buffer->absFileName() << ']');
1149 case LFUN_SERVER_NOTIFY:
1150 dispatch_buffer = keyseq.print(KeySequence::Portable);
1151 theServer().notifyClient(to_utf8(dispatch_buffer));
1154 case LFUN_SERVER_GOTO_FILE_ROW: {
1155 LASSERT(lyx_view_, /**/);
1158 istringstream is(argument);
1159 is >> file_name >> row;
1161 bool loaded = false;
1162 if (prefixIs(file_name, package().temp_dir().absFilename()))
1163 // Needed by inverse dvi search. If it is a file
1164 // in tmpdir, call the apropriated function
1165 buf = theBufferList().getBufferFromTmp(file_name);
1167 // Must replace extension of the file to be .lyx
1168 // and get full path
1169 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1170 // Either change buffer or load the file
1171 if (theBufferList().exists(s))
1172 buf = theBufferList().getBuffer(s);
1174 buf = lyx_view_->loadDocument(s);
1180 updateFlags = Update::None;
1184 buf->updateLabels();
1185 lyx_view_->setBuffer(buf);
1186 view()->setCursorFromRow(row);
1188 buf->errors("Parse");
1189 updateFlags = Update::FitCursor;
1194 case LFUN_DIALOG_SHOW_NEW_INSET: {
1195 LASSERT(lyx_view_, /**/);
1196 string const name = cmd.getArg(0);
1197 InsetCode code = insetCode(name);
1198 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1199 bool insetCodeOK = true;
1208 case HYPERLINK_CODE: {
1209 InsetCommandParams p(code);
1210 data = InsetCommand::params2string(name, p);
1213 case INCLUDE_CODE: {
1214 // data is the include type: one of "include",
1215 // "input", "verbatiminput" or "verbatiminput*"
1217 // default type is requested
1219 InsetCommandParams p(INCLUDE_CODE, data);
1220 data = InsetCommand::params2string("include", p);
1224 // \c data == "Boxed" || "Frameless" etc
1225 InsetBoxParams p(data);
1226 data = InsetBox::params2string(p);
1230 InsetBranchParams p;
1231 data = InsetBranch::params2string(p);
1235 InsetCommandParams p(CITE_CODE);
1236 data = InsetCommand::params2string(name, p);
1240 data = InsetERT::params2string(InsetCollapsable::Open);
1243 case EXTERNAL_CODE: {
1244 InsetExternalParams p;
1245 data = InsetExternal::params2string(p, *buffer);
1250 data = InsetFloat::params2string(p);
1253 case LISTINGS_CODE: {
1254 InsetListingsParams p;
1255 data = InsetListings::params2string(p);
1258 case GRAPHICS_CODE: {
1259 InsetGraphicsParams p;
1260 data = InsetGraphics::params2string(p, *buffer);
1265 data = InsetNote::params2string(p);
1268 case PHANTOM_CODE: {
1269 InsetPhantomParams p;
1270 data = InsetPhantom::params2string(p);
1275 data = InsetSpace::params2string(p);
1280 data = InsetVSpace::params2string(space);
1285 data = InsetWrap::params2string(p);
1289 lyxerr << "Inset type '" << name <<
1290 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1291 insetCodeOK = false;
1293 } // end switch(code)
1295 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1299 case LFUN_CITATION_INSERT: {
1300 LASSERT(lyx_view_, /**/);
1301 if (!argument.empty()) {
1302 // we can have one optional argument, delimited by '|'
1303 // citation-insert <key>|<text_before>
1304 // this should be enhanced to also support text_after
1305 // and citation style
1306 string arg = argument;
1308 if (contains(argument, "|")) {
1309 arg = token(argument, '|', 0);
1310 opt1 = token(argument, '|', 1);
1312 InsetCommandParams icp(CITE_CODE);
1313 icp["key"] = from_utf8(arg);
1315 icp["before"] = from_utf8(opt1);
1316 string icstr = InsetCommand::params2string("citation", icp);
1317 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1320 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1324 case LFUN_BUFFER_CHILD_OPEN: {
1325 LASSERT(lyx_view_ && buffer, /**/);
1326 FileName filename = makeAbsPath(argument, buffer->filePath());
1327 view()->saveBookmark(false);
1329 bool parsed = false;
1330 if (theBufferList().exists(filename)) {
1331 child = theBufferList().getBuffer(filename);
1333 setMessage(bformat(_("Opening child document %1$s..."),
1334 makeDisplayPath(filename.absFilename())));
1335 child = lyx_view_->loadDocument(filename, false);
1339 // Set the parent name of the child document.
1340 // This makes insertion of citations and references in the child work,
1341 // when the target is in the parent or another child document.
1342 child->setParent(buffer);
1343 child->masterBuffer()->updateLabels();
1344 lyx_view_->setBuffer(child);
1346 child->errors("Parse");
1349 // If a screen update is required (in case where auto_open is false),
1350 // setBuffer() would have taken care of it already. Otherwise we shall
1351 // reset the update flag because it can cause a circular problem.
1353 updateFlags = Update::None;
1357 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1358 LASSERT(lyx_view_, /**/);
1359 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1362 case LFUN_KEYMAP_OFF:
1363 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1364 lyx_view_->view()->getIntl().keyMapOn(false);
1367 case LFUN_KEYMAP_PRIMARY:
1368 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1369 lyx_view_->view()->getIntl().keyMapPrim();
1372 case LFUN_KEYMAP_SECONDARY:
1373 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1374 lyx_view_->view()->getIntl().keyMapSec();
1377 case LFUN_KEYMAP_TOGGLE:
1378 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1379 lyx_view_->view()->getIntl().toggleKeyMap();
1385 string rest = split(argument, countstr, ' ');
1386 istringstream is(countstr);
1389 //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1390 for (int i = 0; i < count; ++i)
1391 dispatch(lyxaction.lookupFunc(rest));
1395 case LFUN_COMMAND_SEQUENCE: {
1396 // argument contains ';'-terminated commands
1397 string arg = argument;
1398 if (theBufferList().isLoaded(buffer))
1399 buffer->undo().beginUndoGroup();
1400 while (!arg.empty()) {
1402 arg = split(arg, first, ';');
1403 FuncRequest func(lyxaction.lookupFunc(first));
1404 func.origin = cmd.origin;
1407 if (theBufferList().isLoaded(buffer))
1408 buffer->undo().endUndoGroup();
1412 case LFUN_COMMAND_ALTERNATIVES: {
1413 // argument contains ';'-terminated commands
1414 string arg = argument;
1415 while (!arg.empty()) {
1417 arg = split(arg, first, ';');
1418 FuncRequest func(lyxaction.lookupFunc(first));
1419 func.origin = cmd.origin;
1420 FuncStatus stat = getStatus(func);
1421 if (stat.enabled()) {
1431 if (theTopLevelCmdDef().lock(argument, func)) {
1432 func.origin = cmd.origin;
1434 theTopLevelCmdDef().release(argument);
1436 if (func.action == LFUN_UNKNOWN_ACTION) {
1437 // unknown command definition
1438 lyxerr << "Warning: unknown command definition `"
1442 // recursion detected
1443 lyxerr << "Warning: Recursion in the command definition `"
1444 << argument << "' detected"
1451 case LFUN_PREFERENCES_SAVE: {
1452 lyxrc.write(makeAbsPath("preferences",
1453 package().user_support().absFilename()),
1459 LASSERT(lyx_view_, /**/);
1460 lyx_view_->message(from_utf8(argument));
1463 case LFUN_BUFFER_LANGUAGE: {
1464 LASSERT(lyx_view_, /**/);
1465 Language const * oldL = buffer->params().language;
1466 Language const * newL = languages.getLanguage(argument);
1467 if (!newL || oldL == newL)
1470 if (oldL->rightToLeft() == newL->rightToLeft()
1471 && !buffer->isMultiLingual())
1472 buffer->changeLanguage(oldL, newL);
1476 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1477 string const fname =
1478 addName(addPath(package().user_support().absFilename(), "templates/"),
1480 Buffer defaults(fname);
1482 istringstream ss(argument);
1485 int const unknown_tokens = defaults.readHeader(lex);
1487 if (unknown_tokens != 0) {
1488 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1489 << unknown_tokens << " unknown token"
1490 << (unknown_tokens == 1 ? "" : "s")
1494 if (defaults.writeFile(FileName(defaults.absFileName())))
1495 setMessage(bformat(_("Document defaults saved in %1$s"),
1496 makeDisplayPath(fname)));
1498 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1502 case LFUN_BUFFER_PARAMS_APPLY: {
1503 LASSERT(lyx_view_, /**/);
1505 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1506 Cursor & cur = view()->cursor();
1507 cur.recordUndoFullDocument();
1509 istringstream ss(argument);
1512 int const unknown_tokens = buffer->readHeader(lex);
1514 if (unknown_tokens != 0) {
1515 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1516 << unknown_tokens << " unknown token"
1517 << (unknown_tokens == 1 ? "" : "s")
1521 updateLayout(oldClass, buffer);
1523 updateFlags = Update::Force | Update::FitCursor;
1524 // We are most certainly here because of a change in the document
1525 // It is then better to make sure that all dialogs are in sync with
1526 // current document settings. LyXView::restartCursor() achieve this.
1527 lyx_view_->restartCursor();
1531 case LFUN_LAYOUT_MODULES_CLEAR: {
1532 LASSERT(lyx_view_, /**/);
1533 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1534 view()->cursor().recordUndoFullDocument();
1535 buffer->params().clearLayoutModules();
1536 buffer->params().makeDocumentClass();
1537 updateLayout(oldClass, buffer);
1538 updateFlags = Update::Force | Update::FitCursor;
1542 case LFUN_LAYOUT_MODULE_ADD: {
1543 LASSERT(lyx_view_, /**/);
1544 BufferParams const & params = buffer->params();
1545 if (!params.moduleCanBeAdded(argument)) {
1546 LYXERR0("Module `" << argument <<
1547 "' cannot be added due to failed requirements or "
1548 "conflicts with installed modules.");
1551 DocumentClass const * const oldClass = params.documentClassPtr();
1552 view()->cursor().recordUndoFullDocument();
1553 buffer->params().addLayoutModule(argument);
1554 buffer->params().makeDocumentClass();
1555 updateLayout(oldClass, buffer);
1556 updateFlags = Update::Force | Update::FitCursor;
1560 case LFUN_TEXTCLASS_APPLY: {
1561 LASSERT(lyx_view_, /**/);
1563 if (!loadLayoutFile(argument, buffer->temppath()) &&
1564 !loadLayoutFile(argument, buffer->filePath()))
1567 LayoutFile const * old_layout = buffer->params().baseClass();
1568 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1570 if (old_layout == new_layout)
1574 //Save the old, possibly modular, layout for use in conversion.
1575 DocumentClass const * const oldDocClass = buffer->params().documentClassPtr();
1576 view()->cursor().recordUndoFullDocument();
1577 buffer->params().setBaseClass(argument);
1578 buffer->params().makeDocumentClass();
1579 updateLayout(oldDocClass, buffer);
1580 updateFlags = Update::Force | Update::FitCursor;
1584 case LFUN_LAYOUT_RELOAD: {
1585 LASSERT(lyx_view_, /**/);
1586 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1587 LayoutFileIndex bc = buffer->params().baseClassID();
1588 LayoutFileList::get().reset(bc);
1589 buffer->params().setBaseClass(bc);
1590 buffer->params().makeDocumentClass();
1591 updateLayout(oldClass, buffer);
1592 updateFlags = Update::Force | Update::FitCursor;
1596 case LFUN_TEXTCLASS_LOAD:
1597 loadLayoutFile(argument, buffer->temppath()) ||
1598 loadLayoutFile(argument, buffer->filePath());
1601 case LFUN_LYXRC_APPLY: {
1602 LyXRC const lyxrc_orig = lyxrc;
1604 istringstream ss(argument);
1605 bool const success = lyxrc.read(ss) == 0;
1608 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1609 << "Unable to read lyxrc data"
1614 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1616 theApp()->resetGui();
1618 /// We force the redraw in any case because there might be
1619 /// some screen font changes.
1620 /// FIXME: only the current view will be updated. the Gui
1621 /// class is able to furnish the list of views.
1622 updateFlags = Update::Force;
1626 case LFUN_BOOKMARK_GOTO:
1627 // go to bookmark, open unopened file and switch to buffer if necessary
1628 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1629 updateFlags = Update::FitCursor;
1632 case LFUN_BOOKMARK_CLEAR:
1633 theSession().bookmarks().clear();
1636 case LFUN_VC_COMMAND: {
1637 string flag = cmd.getArg(0);
1638 if (buffer && contains(flag, 'R') && !ensureBufferClean(view()))
1641 if (contains(flag, 'M'))
1642 if (!Alert::askForText(message, _("LyX VC: Log Message")))
1645 string path = cmd.getArg(1);
1646 if (contains(path, "$$p") && buffer)
1647 path = subst(path, "$$p", buffer->filePath());
1648 LYXERR(Debug::LYXVC, "Directory: " << path);
1650 if (!pp.isReadableDirectory()) {
1651 lyxerr << _("Directory is not accessible.") << endl;
1654 support::PathChanger p(pp);
1656 string command = cmd.getArg(2);
1657 if (command.empty())
1660 command = subst(command, "$$i", buffer->absFileName());
1661 command = subst(command, "$$p", buffer->filePath());
1663 command = subst(command, "$$m", to_utf8(message));
1664 LYXERR(Debug::LYXVC, "Command: " << command);
1666 one.startscript(Systemcall::Wait, command);
1670 if (contains(flag, 'I'))
1671 buffer->markDirty();
1672 if (contains(flag, 'R'))
1679 LASSERT(theApp(), /**/);
1680 // Let the frontend dispatch its own actions.
1681 if (theApp()->dispatch(cmd))
1682 // Nothing more to do.
1685 // Everything below is only for active lyx_view_
1689 // Start an undo group. This may be needed for
1690 // some stuff like inset-apply on labels.
1691 if (theBufferList().isLoaded(buffer))
1692 buffer->undo().beginUndoGroup();
1694 // Let the current LyXView dispatch its own actions.
1695 if (lyx_view_->dispatch(cmd)) {
1696 if (lyx_view_->view()) {
1697 updateFlags = lyx_view_->view()->cursor().result().update();
1698 if (theBufferList().isLoaded(buffer))
1699 buffer->undo().endUndoGroup();
1704 LASSERT(lyx_view_->view(), /**/);
1706 // Let the current BufferView dispatch its own actions.
1707 if (view()->dispatch(cmd)) {
1708 // The BufferView took care of its own updates if needed.
1709 updateFlags = Update::None;
1710 if (theBufferList().isLoaded(buffer))
1711 buffer->undo().endUndoGroup();
1715 // Is this a function that acts on inset at point?
1716 Inset * inset = view()->cursor().nextInset();
1717 if (lyxaction.funcHasFlag(action, LyXAction::AtPoint)
1719 view()->cursor().result().dispatched(true);
1720 view()->cursor().result().update(Update::FitCursor | Update::Force);
1721 FuncRequest tmpcmd = cmd;
1722 inset->dispatch(view()->cursor(), tmpcmd);
1723 if (view()->cursor().result().dispatched()) {
1724 updateFlags = view()->cursor().result().update();
1729 // Let the current Cursor dispatch its own actions.
1730 Cursor old = view()->cursor();
1731 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1732 cursorPosBeforeDispatchY_);
1733 view()->cursor().dispatch(cmd);
1735 // notify insets we just left
1736 if (view()->cursor() != old) {
1738 bool badcursor = notifyCursorLeavesOrEnters(old, view()->cursor());
1740 view()->cursor().fixIfBroken();
1743 if (theBufferList().isLoaded(buffer))
1744 buffer->undo().endUndoGroup();
1746 // update completion. We do it here and not in
1747 // processKeySym to avoid another redraw just for a
1748 // changed inline completion
1749 if (cmd.origin == FuncRequest::KEYBOARD) {
1750 if (cmd.action == LFUN_SELF_INSERT)
1751 lyx_view_->updateCompletion(view()->cursor(), true, true);
1752 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1753 lyx_view_->updateCompletion(view()->cursor(), false, true);
1755 lyx_view_->updateCompletion(view()->cursor(), false, false);
1758 updateFlags = view()->cursor().result().update();
1761 // if we executed a mutating lfun, mark the buffer as dirty
1762 if (theBufferList().isLoaded(buffer) && flag.enabled()
1763 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1764 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1765 buffer->markDirty();
1767 if (lyx_view_ && lyx_view_->buffer()) {
1768 // BufferView::update() updates the ViewMetricsInfo and
1769 // also initializes the position cache for all insets in
1770 // (at least partially) visible top-level paragraphs.
1771 // We will redraw the screen only if needed.
1772 view()->processUpdateFlags(updateFlags);
1774 // Do we have a selection?
1775 theSelection().haveSelection(view()->cursor().selection());
1778 lyx_view_->restartCursor();
1782 // Some messages may already be translated, so we cannot use _()
1783 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1788 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1790 const bool verbose = (cmd.origin == FuncRequest::MENU
1791 || cmd.origin == FuncRequest::TOOLBAR
1792 || cmd.origin == FuncRequest::COMMANDBUFFER);
1794 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1795 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1797 lyx_view_->message(msg);
1801 docstring dispatch_msg = msg;
1802 if (!dispatch_msg.empty())
1803 dispatch_msg += ' ';
1805 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1807 bool argsadded = false;
1809 if (!cmd.argument().empty()) {
1810 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1811 comname += ' ' + cmd.argument();
1816 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd, KeySequence::ForGui);
1818 if (!shortcuts.empty())
1819 comname += ": " + shortcuts;
1820 else if (!argsadded && !cmd.argument().empty())
1821 comname += ' ' + cmd.argument();
1823 if (!comname.empty()) {
1824 comname = rtrim(comname);
1825 dispatch_msg += '(' + rtrim(comname) + ')';
1828 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1829 if (!dispatch_msg.empty())
1830 lyx_view_->message(dispatch_msg);
1834 void LyXFunc::reloadBuffer()
1836 FileName filename = lyx_view_->buffer()->fileName();
1837 // The user has already confirmed that the changes, if any, should
1838 // be discarded. So we just release the Buffer and don't call closeBuffer();
1839 theBufferList().release(lyx_view_->buffer());
1840 // if the lyx_view_ has been destroyed, create a new one
1842 theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
1843 Buffer * buf = lyx_view_->loadDocument(filename);
1844 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1847 buf->updateLabels();
1848 lyx_view_->setBuffer(buf);
1849 buf->errors("Parse");
1850 str = bformat(_("Document %1$s reloaded."), disp_fn);
1852 str = bformat(_("Could not reload document %1$s"), disp_fn);
1854 lyx_view_->message(str);
1857 // Each "lyx_view_" should have it's own message method. lyxview and
1858 // the minibuffer would use the minibuffer, but lyxserver would
1859 // send an ERROR signal to its client. Alejandro 970603
1860 // This function is bit problematic when it comes to NLS, to make the
1861 // lyx servers client be language indepenent we must not translate
1862 // strings sent to this func.
1863 void LyXFunc::setErrorMessage(docstring const & m) const
1865 dispatch_buffer = m;
1870 void LyXFunc::setMessage(docstring const & m) const
1872 dispatch_buffer = m;
1876 docstring LyXFunc::viewStatusMessage()
1878 // When meta-fake key is pressed, show the key sequence so far + "M-".
1880 return keyseq.print(KeySequence::ForGui) + "M-";
1882 // Else, when a non-complete key sequence is pressed,
1883 // show the available options.
1884 if (keyseq.length() > 0 && !keyseq.deleted())
1885 return keyseq.printOptions(true);
1887 LASSERT(lyx_view_, /**/);
1888 if (!lyx_view_->buffer())
1889 return _("Welcome to LyX!");
1891 return view()->cursor().currentState();
1895 BufferView * LyXFunc::view() const
1897 LASSERT(lyx_view_, /**/);
1898 return lyx_view_->view();
1902 bool LyXFunc::wasMetaKey() const
1904 return (meta_fake_bit != NoModifier);
1908 void LyXFunc::updateLayout(DocumentClass const * const oldlayout, Buffer * buf)
1910 lyx_view_->message(_("Converting document to new document class..."));
1912 StableDocIterator backcur(view()->cursor());
1913 ErrorList & el = buf->errorList("Class Switch");
1914 cap::switchBetweenClasses(
1915 oldlayout, buf->params().documentClassPtr(),
1916 static_cast<InsetText &>(buf->inset()), el);
1918 view()->setCursor(backcur.asDocIterator(buf));
1920 buf->errors("Class Switch");
1921 buf->updateLabels();
1927 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1929 // Why the switch you might ask. It is a trick to ensure that all
1930 // the elements in the LyXRCTags enum is handled. As you can see
1931 // there are no breaks at all. So it is just a huge fall-through.
1932 // The nice thing is that we will get a warning from the compiler
1933 // if we forget an element.
1934 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1936 case LyXRC::RC_ACCEPT_COMPOUND:
1937 case LyXRC::RC_ALT_LANG:
1938 case LyXRC::RC_PLAINTEXT_LINELEN:
1939 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1940 case LyXRC::RC_AUTOCORRECTION_MATH:
1941 case LyXRC::RC_AUTOREGIONDELETE:
1942 case LyXRC::RC_AUTORESET_OPTIONS:
1943 case LyXRC::RC_AUTOSAVE:
1944 case LyXRC::RC_AUTO_NUMBER:
1945 case LyXRC::RC_BACKUPDIR_PATH:
1946 case LyXRC::RC_BIBTEX_COMMAND:
1947 case LyXRC::RC_BINDFILE:
1948 case LyXRC::RC_CHECKLASTFILES:
1949 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1950 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1951 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1952 case LyXRC::RC_COMPLETION_INLINE_MATH:
1953 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1954 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1955 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1956 case LyXRC::RC_COMPLETION_POPUP_MATH:
1957 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1958 case LyXRC::RC_USELASTFILEPOS:
1959 case LyXRC::RC_LOADSESSION:
1960 case LyXRC::RC_CHKTEX_COMMAND:
1961 case LyXRC::RC_CONVERTER:
1962 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1963 case LyXRC::RC_COPIER:
1964 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1965 case LyXRC::RC_SCROLL_BELOW_DOCUMENT:
1966 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1967 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1968 case LyXRC::RC_DATE_INSERT_FORMAT:
1969 case LyXRC::RC_DEFAULT_LANGUAGE:
1970 case LyXRC::RC_GUI_LANGUAGE:
1971 case LyXRC::RC_DEFAULT_PAPERSIZE:
1972 case LyXRC::RC_DEFFILE:
1973 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1974 case LyXRC::RC_DISPLAY_GRAPHICS:
1975 case LyXRC::RC_DOCUMENTPATH:
1976 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1977 FileName path(lyxrc_new.document_path);
1978 if (path.exists() && path.isDirectory())
1979 package().document_dir() = FileName(lyxrc.document_path);
1981 case LyXRC::RC_ESC_CHARS:
1982 case LyXRC::RC_EXAMPLEPATH:
1983 case LyXRC::RC_FONT_ENCODING:
1984 case LyXRC::RC_FORMAT:
1985 case LyXRC::RC_GROUP_LAYOUTS:
1986 case LyXRC::RC_INDEX_COMMAND:
1987 case LyXRC::RC_NOMENCL_COMMAND:
1988 case LyXRC::RC_INPUT:
1989 case LyXRC::RC_KBMAP:
1990 case LyXRC::RC_KBMAP_PRIMARY:
1991 case LyXRC::RC_KBMAP_SECONDARY:
1992 case LyXRC::RC_LABEL_INIT_LENGTH:
1993 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1994 case LyXRC::RC_LANGUAGE_AUTO_END:
1995 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1996 case LyXRC::RC_LANGUAGE_COMMAND_END:
1997 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1998 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1999 case LyXRC::RC_LANGUAGE_PACKAGE:
2000 case LyXRC::RC_LANGUAGE_USE_BABEL:
2001 case LyXRC::RC_MAC_LIKE_WORD_MOVEMENT:
2002 case LyXRC::RC_MACRO_EDIT_STYLE:
2003 case LyXRC::RC_MAKE_BACKUP:
2004 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
2005 case LyXRC::RC_MOUSE_WHEEL_SPEED:
2006 case LyXRC::RC_NUMLASTFILES:
2007 case LyXRC::RC_PATH_PREFIX:
2008 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
2009 prependEnvPath("PATH", lyxrc.path_prefix);
2011 case LyXRC::RC_PERS_DICT:
2012 case LyXRC::RC_PREVIEW:
2013 case LyXRC::RC_PREVIEW_HASHED_LABELS:
2014 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
2015 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
2016 case LyXRC::RC_PRINTCOPIESFLAG:
2017 case LyXRC::RC_PRINTER:
2018 case LyXRC::RC_PRINTEVENPAGEFLAG:
2019 case LyXRC::RC_PRINTEXSTRAOPTIONS:
2020 case LyXRC::RC_PRINTFILEEXTENSION:
2021 case LyXRC::RC_PRINTLANDSCAPEFLAG:
2022 case LyXRC::RC_PRINTODDPAGEFLAG:
2023 case LyXRC::RC_PRINTPAGERANGEFLAG:
2024 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
2025 case LyXRC::RC_PRINTPAPERFLAG:
2026 case LyXRC::RC_PRINTREVERSEFLAG:
2027 case LyXRC::RC_PRINTSPOOL_COMMAND:
2028 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
2029 case LyXRC::RC_PRINTTOFILE:
2030 case LyXRC::RC_PRINTTOPRINTER:
2031 case LyXRC::RC_PRINT_ADAPTOUTPUT:
2032 case LyXRC::RC_PRINT_COMMAND:
2033 case LyXRC::RC_RTL_SUPPORT:
2034 case LyXRC::RC_SCREEN_DPI:
2035 case LyXRC::RC_SCREEN_FONT_ROMAN:
2036 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
2037 case LyXRC::RC_SCREEN_FONT_SANS:
2038 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
2039 case LyXRC::RC_SCREEN_FONT_SCALABLE:
2040 case LyXRC::RC_SCREEN_FONT_SIZES:
2041 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
2042 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
2043 case LyXRC::RC_GEOMETRY_SESSION:
2044 case LyXRC::RC_SCREEN_ZOOM:
2045 case LyXRC::RC_SERVERPIPE:
2046 case LyXRC::RC_SET_COLOR:
2047 case LyXRC::RC_SHOW_BANNER:
2048 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
2049 case LyXRC::RC_SPELL_COMMAND:
2050 case LyXRC::RC_SPELLCHECK_CONTINUOUSLY:
2051 case LyXRC::RC_TEMPDIRPATH:
2052 case LyXRC::RC_TEMPLATEPATH:
2053 case LyXRC::RC_TEX_ALLOWS_SPACES:
2054 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2055 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2056 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2058 case LyXRC::RC_THESAURUSDIRPATH:
2059 case LyXRC::RC_UIFILE:
2060 case LyXRC::RC_USER_EMAIL:
2061 case LyXRC::RC_USER_NAME:
2062 case LyXRC::RC_USETEMPDIR:
2063 case LyXRC::RC_USE_ALT_LANG:
2064 case LyXRC::RC_USE_CONVERTER_CACHE:
2065 case LyXRC::RC_USE_ESC_CHARS:
2066 case LyXRC::RC_USE_INP_ENC:
2067 case LyXRC::RC_USE_PERS_DICT:
2068 case LyXRC::RC_USE_TOOLTIP:
2069 case LyXRC::RC_USE_PIXMAP_CACHE:
2070 case LyXRC::RC_USE_SPELL_LIB:
2071 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2072 case LyXRC::RC_SORT_LAYOUTS:
2073 case LyXRC::RC_FULL_SCREEN_LIMIT:
2074 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2075 case LyXRC::RC_FULL_SCREEN_TABBAR:
2076 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2077 case LyXRC::RC_FULL_SCREEN_WIDTH:
2078 case LyXRC::RC_VISUAL_CURSOR:
2079 case LyXRC::RC_VIEWER:
2080 case LyXRC::RC_LAST: