3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "BranchList.h"
25 #include "buffer_funcs.h"
27 #include "BufferList.h"
28 #include "BufferParams.h"
29 #include "BufferView.h"
32 #include "Converter.h"
34 #include "CutAndPaste.h"
35 #include "DispatchResult.h"
37 #include "ErrorList.h"
39 #include "FuncRequest.h"
40 #include "FuncStatus.h"
41 #include "InsetIterator.h"
46 #include "LyXAction.h"
51 #include "Paragraph.h"
52 #include "ParagraphParameters.h"
53 #include "ParIterator.h"
57 #include "TextClassList.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetListings.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetTabular.h"
70 #include "insets/InsetVSpace.h"
71 #include "insets/InsetWrap.h"
73 #include "frontends/alert.h"
74 #include "frontends/Application.h"
75 #include "frontends/KeySymbol.h"
76 #include "frontends/LyXView.h"
77 #include "frontends/Selection.h"
79 #include "support/debug.h"
80 #include "support/environment.h"
81 #include "support/FileName.h"
82 #include "support/filetools.h"
83 #include "support/gettext.h"
84 #include "support/lstrings.h"
85 #include "support/Path.h"
86 #include "support/Package.h"
87 #include "support/Systemcall.h"
88 #include "support/convert.h"
89 #include "support/os.h"
95 using namespace lyx::support;
99 using frontend::LyXView;
101 namespace Alert = frontend::Alert;
106 // This function runs "configure" and then rereads lyx.defaults to
107 // reconfigure the automatic settings.
108 void reconfigure(LyXView & lv, string const & option)
110 // emit message signal.
111 lv.message(_("Running configure..."));
113 // Run configure in user lyx directory
114 PathChanger p(package().user_support());
115 string configure_command = package().configure_command();
116 configure_command += option;
118 int ret = one.startscript(Systemcall::Wait, configure_command);
120 // emit message signal.
121 lv.message(_("Reloading configuration..."));
122 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
123 // Re-read packages.lst
124 LaTeXFeatures::getAvailable();
127 Alert::information(_("System reconfiguration failed"),
128 _("The system reconfiguration has failed.\n"
129 "Default textclass is used but LyX may "
130 "not be able to work properly.\n"
131 "Please reconfigure again if needed."));
134 Alert::information(_("System reconfigured"),
135 _("The system has been reconfigured.\n"
136 "You need to restart LyX to make use of any\n"
137 "updated document class specifications."));
141 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
143 // Try to fix cursor in case it is broken.
144 cursor.fixIfBroken();
146 // This is, of course, a mess. Better create a new doc iterator and use
147 // this in Inset::getStatus. This might require an additional
148 // BufferView * arg, though (which should be avoided)
149 //Cursor safe = *this;
151 for ( ; cursor.depth(); cursor.pop()) {
152 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
153 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
154 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
155 BOOST_ASSERT(cursor.pos() <= cursor.lastpos());
157 // The inset's getStatus() will return 'true' if it made
158 // a definitive decision on whether it want to handle the
159 // request or not. The result of this decision is put into
160 // the 'status' parameter.
161 if (cursor.inset().getStatus(cursor, cmd, status)) {
170 /** Return the change status at cursor position, taking in account the
171 * status at each level of the document iterator (a table in a deleted
172 * footnote is deleted).
173 * When \param outer is true, the top slice is not looked at.
175 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
177 size_t const depth = dit.depth() - (outer ? 1 : 0);
179 for (size_t i = 0 ; i < depth ; ++i) {
180 CursorSlice const & slice = dit[i];
181 if (!slice.inset().inMathed()
182 && slice.pos() < slice.paragraph().size()) {
183 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
184 if (ch != Change::UNCHANGED)
188 return Change::UNCHANGED;
195 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
200 void LyXFunc::initKeySequences(KeyMap * kb)
202 keyseq = KeySequence(kb, kb);
203 cancel_meta_seq = KeySequence(kb, kb);
207 void LyXFunc::setLyXView(LyXView * lv)
209 if (!quitting && lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
210 // save current selection to the selection buffer to allow
211 // middle-button paste in another window
212 cap::saveSelection(lyx_view_->view()->cursor());
217 void LyXFunc::handleKeyFunc(kb_action action)
219 char_type c = encoded_last_key;
224 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
225 lyx_view_->view()->getIntl().getTransManager().deadkey(
226 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
227 // Need to clear, in case the minibuffer calls these
230 // copied verbatim from do_accent_char
231 view()->cursor().resetAnchor();
232 view()->processUpdateFlags(Update::FitCursor);
236 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
238 BOOST_ASSERT(lyx_view_);
239 if (!LyX::ref().session().bookmarks().isValid(idx))
241 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
242 BOOST_ASSERT(!bm.filename.empty());
243 string const file = bm.filename.absFilename();
244 // if the file is not opened, open it.
245 if (!theBufferList().exists(file)) {
247 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
251 // open may fail, so we need to test it again
252 if (!theBufferList().exists(file))
255 // if the current buffer is not that one, switch to it.
256 if (lyx_view_->buffer()->absFileName() != file) {
259 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
261 // moveToPosition try paragraph id first and then paragraph (pit, pos).
262 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
263 bm.top_id, bm.top_pos))
266 // Cursor jump succeeded!
267 Cursor const & cur = view()->cursor();
268 pit_type new_pit = cur.pit();
269 pos_type new_pos = cur.pos();
270 int new_id = cur.paragraph().id();
272 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
273 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
274 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
275 || bm.top_id != new_id) {
276 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
277 new_pit, new_pos, new_id);
282 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
284 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
286 // Do nothing if we have nothing (JMarc)
287 if (!keysym.isOK()) {
288 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
289 lyx_view_->restartCursor();
293 if (keysym.isModifier()) {
294 LYXERR(Debug::KEY, "isModifier true");
295 lyx_view_->restartCursor();
299 //Encoding const * encoding = view()->cursor().getEncoding();
300 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
301 // FIXME: encoded_last_key shadows the member variable of the same
302 // name. Is that intended?
303 char_type encoded_last_key = keysym.getUCSEncoded();
305 // Do a one-deep top-level lookup for
306 // cancel and meta-fake keys. RVDK_PATCH_5
307 cancel_meta_seq.reset();
309 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
310 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
312 // When not cancel or meta-fake, do the normal lookup.
313 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
314 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
315 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
316 // remove Caps Lock and Mod2 as a modifiers
317 func = keyseq.addkey(keysym, (state | meta_fake_bit));
318 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
321 // Dont remove this unless you know what you are doing.
322 meta_fake_bit = NoModifier;
324 // Can this happen now ?
325 if (func.action == LFUN_NOACTION)
326 func = FuncRequest(LFUN_COMMAND_PREFIX);
328 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
329 << keyseq.print(KeySequence::Portable) << ']');
331 // already here we know if it any point in going further
332 // why not return already here if action == -1 and
333 // num_bytes == 0? (Lgb)
335 if (keyseq.length() > 1)
336 lyx_view_->message(keyseq.print(KeySequence::ForGui));
339 // Maybe user can only reach the key via holding down shift.
340 // Let's see. But only if shift is the only modifier
341 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
342 LYXERR(Debug::KEY, "Trying without shift");
343 func = keyseq.addkey(keysym, NoModifier);
344 LYXERR(Debug::KEY, "Action now " << func.action);
347 if (func.action == LFUN_UNKNOWN_ACTION) {
348 // Hmm, we didn't match any of the keysequences. See
349 // if it's normal insertable text not already covered
351 if (keysym.isText() && keyseq.length() == 1) {
352 LYXERR(Debug::KEY, "isText() is true, inserting.");
353 func = FuncRequest(LFUN_SELF_INSERT,
354 FuncRequest::KEYBOARD);
356 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
357 lyx_view_->message(_("Unknown function."));
358 lyx_view_->restartCursor();
363 if (func.action == LFUN_SELF_INSERT) {
364 if (encoded_last_key != 0) {
365 docstring const arg(1, encoded_last_key);
366 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
367 FuncRequest::KEYBOARD));
368 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
369 lyx_view_->updateCompletion(true, true);
373 if (func.action == LFUN_CHAR_DELETE_BACKWARD)
374 // backspace is not a self-insertion. But it
375 // still should not hide the completion popup.
376 // FIXME: more clever way to detect those movements
377 lyx_view_->updateCompletion(false, true);
379 lyx_view_->updateCompletion(false, false);
383 lyx_view_->restartCursor();
387 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
389 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
392 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
394 if (cmd.action == LFUN_NOACTION) {
395 flag.message(from_utf8(N_("Nothing to do")));
400 switch (cmd.action) {
401 case LFUN_UNKNOWN_ACTION:
402 #ifndef HAVE_LIBAIKSAURUS
403 case LFUN_THESAURUS_ENTRY:
413 if (flag.unknown()) {
414 flag.message(from_utf8(N_("Unknown action")));
418 if (!flag.enabled()) {
419 if (flag.message().empty())
420 flag.message(from_utf8(N_("Command disabled")));
424 // Check whether we need a buffer
425 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
427 flag.message(from_utf8(N_("Command not allowed with"
428 "out any document open")));
433 // I would really like to avoid having this switch and rather try to
434 // encode this in the function itself.
435 // -- And I'd rather let an inset decide which LFUNs it is willing
436 // to handle (Andre')
438 switch (cmd.action) {
440 // FIXME: these cases should be hidden in GuiApplication::getStatus().
441 case LFUN_WINDOW_CLOSE:
443 return theApp()->getStatus(cmd);
447 // FIXME: these cases should be hidden in GuiView::getStatus().
448 case LFUN_DIALOG_TOGGLE:
449 case LFUN_DIALOG_SHOW:
450 case LFUN_DIALOG_UPDATE:
451 if (cmd.argument() == "prefs"
452 || cmd.argument() == "aboutlyx")
455 return lyx_view_->getStatus(cmd);
460 case LFUN_TOOLBAR_TOGGLE:
461 case LFUN_INSET_APPLY:
462 case LFUN_BUFFER_WRITE:
463 case LFUN_BUFFER_WRITE_AS:
464 case LFUN_SPLIT_VIEW:
465 case LFUN_CLOSE_TAB_GROUP:
466 case LFUN_COMPLETION_POPUP:
467 case LFUN_COMPLETION_INLINE:
468 case LFUN_COMPLETION_COMPLETE:
470 return lyx_view_->getStatus(cmd);
474 case LFUN_BUFFER_TOGGLE_READ_ONLY:
475 flag.setOnOff(buf->isReadonly());
478 case LFUN_BUFFER_SWITCH:
479 // toggle on the current buffer, but do not toggle off
480 // the other ones (is that a good idea?)
481 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
485 case LFUN_BUFFER_EXPORT:
486 enable = cmd.argument() == "custom"
487 || buf->isExportable(to_utf8(cmd.argument()));
490 case LFUN_BUFFER_CHKTEX:
491 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
494 case LFUN_BUILD_PROGRAM:
495 enable = buf->isExportable("program");
498 case LFUN_VC_REGISTER:
499 enable = !buf->lyxvc().inUse();
501 case LFUN_VC_CHECK_IN:
502 enable = buf->lyxvc().inUse() && !buf->isReadonly();
504 case LFUN_VC_CHECK_OUT:
505 enable = buf->lyxvc().inUse() && buf->isReadonly();
508 case LFUN_VC_UNDO_LAST:
509 enable = buf->lyxvc().inUse();
511 case LFUN_BUFFER_RELOAD:
512 enable = !buf->isUnnamed() && buf->fileName().exists()
513 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
516 case LFUN_CITATION_INSERT: {
517 FuncRequest fr(LFUN_INSET_INSERT, "citation");
518 enable = getStatus(fr).enabled();
522 // This could be used for the no-GUI version. The GUI version is handled in
523 // LyXView::getStatus(). See above.
525 case LFUN_BUFFER_WRITE:
526 case LFUN_BUFFER_WRITE_AS: {
527 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
528 enable = b && (b->isUnnamed() || !b->isClean());
533 case LFUN_BUFFER_WRITE_ALL: {
534 // We enable the command only if there are some modified buffers
535 Buffer * first = theBufferList().first();
540 // We cannot use a for loop as the buffer list is a cycle.
546 b = theBufferList().next(b);
547 } while (b != first);
551 case LFUN_BOOKMARK_GOTO: {
552 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
553 enable = LyX::ref().session().bookmarks().isValid(num);
557 case LFUN_BOOKMARK_CLEAR:
558 enable = LyX::ref().session().bookmarks().size() > 0;
561 // this one is difficult to get right. As a half-baked
562 // solution, we consider only the first action of the sequence
563 case LFUN_COMMAND_SEQUENCE: {
564 // argument contains ';'-terminated commands
565 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
566 FuncRequest func(lyxaction.lookupFunc(firstcmd));
567 func.origin = cmd.origin;
568 flag = getStatus(func);
574 string name = to_utf8(cmd.argument());
575 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
576 func.origin = cmd.origin;
577 flag = getStatus(func);
578 LyX::ref().topLevelCmdDef().release(name);
580 // catch recursion or unknown command definiton
581 // all operations until the recursion or unknown command
582 // definiton occures are performed, so set the state to enabled
588 case LFUN_BUFFER_NEW:
589 case LFUN_BUFFER_NEW_TEMPLATE:
590 case LFUN_WORD_FIND_FORWARD:
591 case LFUN_WORD_FIND_BACKWARD:
592 case LFUN_COMMAND_PREFIX:
593 case LFUN_COMMAND_EXECUTE:
595 case LFUN_META_PREFIX:
596 case LFUN_BUFFER_CLOSE:
597 case LFUN_BUFFER_UPDATE:
598 case LFUN_BUFFER_VIEW:
599 case LFUN_MASTER_BUFFER_UPDATE:
600 case LFUN_MASTER_BUFFER_VIEW:
601 case LFUN_BUFFER_IMPORT:
602 case LFUN_BUFFER_AUTO_SAVE:
603 case LFUN_RECONFIGURE:
606 case LFUN_DROP_LAYOUTS_CHOICE:
608 case LFUN_SERVER_GET_NAME:
609 case LFUN_SERVER_NOTIFY:
610 case LFUN_SERVER_GOTO_FILE_ROW:
611 case LFUN_DIALOG_HIDE:
612 case LFUN_DIALOG_DISCONNECT_INSET:
613 case LFUN_BUFFER_CHILD_OPEN:
615 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
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:
624 case LFUN_SCREEN_FONT_UPDATE:
627 case LFUN_EXTERNAL_EDIT:
628 case LFUN_GRAPHICS_EDIT:
629 case LFUN_ALL_INSETS_TOGGLE:
630 case LFUN_BUFFER_LANGUAGE:
631 case LFUN_TEXTCLASS_APPLY:
632 case LFUN_TEXTCLASS_LOAD:
633 case LFUN_BUFFER_SAVE_AS_DEFAULT:
634 case LFUN_BUFFER_PARAMS_APPLY:
635 case LFUN_LAYOUT_MODULES_CLEAR:
636 case LFUN_LAYOUT_MODULE_ADD:
637 case LFUN_LAYOUT_RELOAD:
638 case LFUN_LYXRC_APPLY:
639 case LFUN_BUFFER_NEXT:
640 case LFUN_BUFFER_PREVIOUS:
641 case LFUN_WINDOW_NEW:
643 // these are handled in our dispatch()
651 if (!getLocalStatus(view()->cursor(), cmd, flag))
652 flag = view()->getStatus(cmd);
658 // Can we use a readonly buffer?
659 if (buf && buf->isReadonly()
660 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
661 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
662 flag.message(from_utf8(N_("Document is read-only")));
666 // Are we in a DELETED change-tracking region?
668 && lookupChangeType(view()->cursor(), true) == Change::DELETED
669 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
670 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
671 flag.message(from_utf8(N_("This portion of the document is deleted.")));
675 // the default error message if we disable the command
676 if (!flag.enabled() && flag.message().empty())
677 flag.message(from_utf8(N_("Command disabled")));
683 bool LyXFunc::ensureBufferClean(BufferView * bv)
685 Buffer & buf = bv->buffer();
689 docstring const file = buf.fileName().displayName(30);
690 docstring text = bformat(_("The document %1$s has unsaved "
691 "changes.\n\nDo you want to save "
692 "the document?"), file);
693 int const ret = Alert::prompt(_("Save changed document?"),
694 text, 0, 1, _("&Save"),
698 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
700 return buf.isClean();
706 void showPrintError(string const & name)
708 docstring str = bformat(_("Could not print the document %1$s.\n"
709 "Check that your printer is set up correctly."),
710 makeDisplayPath(name, 50));
711 Alert::error(_("Print document failed"), str);
715 void loadTextClass(string const & name, string const & buf_path)
717 pair<bool, BaseClassIndex> const tc_pair =
718 textclasslist.numberOfClass(name);
720 if (!tc_pair.first) {
721 lyxerr << "Document class \"" << name
722 << "\" does not exist."
727 BaseClassIndex const tc = tc_pair.second;
729 if (!textclasslist[tc].load(buf_path)) {
730 docstring s = bformat(_("The document class %1$s."
731 "could not be loaded."),
732 from_utf8(textclasslist[tc].name()));
733 Alert::error(_("Could not load class"), s);
738 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
743 void LyXFunc::dispatch(FuncRequest const & cmd)
745 string const argument = to_utf8(cmd.argument());
746 kb_action const action = cmd.action;
748 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
749 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
751 // we have not done anything wrong yet.
753 dispatch_buffer.erase();
755 // redraw the screen at the end (first of the two drawing steps).
756 //This is done unless explicitely requested otherwise
757 Update::flags updateFlags = Update::FitCursor;
759 FuncStatus const flag = getStatus(cmd);
760 if (!flag.enabled()) {
761 // We cannot use this function here
762 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
763 << lyxaction.getActionName(action)
764 << " [" << action << "] is disabled at this location");
765 setErrorMessage(flag.message());
769 case LFUN_WORD_FIND_FORWARD:
770 case LFUN_WORD_FIND_BACKWARD: {
771 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
772 static docstring last_search;
773 docstring searched_string;
775 if (!cmd.argument().empty()) {
776 last_search = cmd.argument();
777 searched_string = cmd.argument();
779 searched_string = last_search;
782 if (searched_string.empty())
785 bool const fw = action == LFUN_WORD_FIND_FORWARD;
786 docstring const data =
787 find2string(searched_string, true, false, fw);
788 find(view(), FuncRequest(LFUN_WORD_FIND, data));
792 case LFUN_COMMAND_PREFIX:
793 BOOST_ASSERT(lyx_view_);
794 lyx_view_->message(keyseq.printOptions(true));
798 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
800 meta_fake_bit = NoModifier;
801 if (lyx_view_->buffer())
802 // cancel any selection
803 dispatch(FuncRequest(LFUN_MARK_OFF));
804 setMessage(from_ascii(N_("Cancel")));
807 case LFUN_META_PREFIX:
808 meta_fake_bit = AltModifier;
809 setMessage(keyseq.print(KeySequence::ForGui));
812 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
813 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
814 Buffer * buf = lyx_view_->buffer();
815 if (buf->lyxvc().inUse())
816 buf->lyxvc().toggleReadOnly();
818 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
822 // --- Menus -----------------------------------------------
823 case LFUN_BUFFER_NEW:
824 lyx_view_->newDocument(argument, false);
825 updateFlags = Update::None;
828 case LFUN_BUFFER_NEW_TEMPLATE:
829 lyx_view_->newDocument(argument, true);
830 updateFlags = Update::None;
833 case LFUN_BUFFER_CLOSE:
835 updateFlags = Update::None;
838 case LFUN_BUFFER_RELOAD: {
839 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
840 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
841 docstring text = bformat(_("Any changes will be lost. Are you sure "
842 "you want to revert to the saved version of the document %1$s?"), file);
843 int const ret = Alert::prompt(_("Revert to saved document?"),
844 text, 1, 1, _("&Revert"), _("&Cancel"));
851 case LFUN_BUFFER_UPDATE:
852 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
853 lyx_view_->buffer()->doExport(argument, true);
856 case LFUN_BUFFER_VIEW:
857 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
858 lyx_view_->buffer()->preview(argument);
861 case LFUN_MASTER_BUFFER_UPDATE:
862 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
863 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
866 case LFUN_MASTER_BUFFER_VIEW:
867 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
868 lyx_view_->buffer()->masterBuffer()->preview(argument);
871 case LFUN_BUILD_PROGRAM:
872 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
873 lyx_view_->buffer()->doExport("program", true);
876 case LFUN_BUFFER_CHKTEX:
877 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
878 lyx_view_->buffer()->runChktex();
881 case LFUN_BUFFER_EXPORT:
882 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
883 if (argument == "custom")
884 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
886 lyx_view_->buffer()->doExport(argument, false);
889 case LFUN_BUFFER_EXPORT_CUSTOM: {
890 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
892 string command = split(argument, format_name, ' ');
893 Format const * format = formats.getFormat(format_name);
895 lyxerr << "Format \"" << format_name
896 << "\" not recognized!"
901 Buffer * buffer = lyx_view_->buffer();
903 // The name of the file created by the conversion process
906 // Output to filename
907 if (format->name() == "lyx") {
908 string const latexname = buffer->latexName(false);
909 filename = changeExtension(latexname,
910 format->extension());
911 filename = addName(buffer->temppath(), filename);
913 if (!buffer->writeFile(FileName(filename)))
917 buffer->doExport(format_name, true, filename);
920 // Substitute $$FName for filename
921 if (!contains(command, "$$FName"))
922 command = "( " + command + " ) < $$FName";
923 command = subst(command, "$$FName", filename);
925 // Execute the command in the background
927 call.startscript(Systemcall::DontWait, command);
931 case LFUN_BUFFER_PRINT: {
932 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
933 // FIXME: cmd.getArg() might fail if one of the arguments
934 // contains double quotes
935 string target = cmd.getArg(0);
936 string target_name = cmd.getArg(1);
937 string command = cmd.getArg(2);
940 || target_name.empty()
941 || command.empty()) {
942 lyxerr << "Unable to parse \""
943 << argument << '"' << endl;
946 if (target != "printer" && target != "file") {
947 lyxerr << "Unrecognized target \""
948 << target << '"' << endl;
952 Buffer * buffer = lyx_view_->buffer();
954 if (!buffer->doExport("dvi", true)) {
955 showPrintError(buffer->absFileName());
959 // Push directory path.
960 string const path = buffer->temppath();
961 // Prevent the compiler from optimizing away p
965 // there are three cases here:
966 // 1. we print to a file
967 // 2. we print directly to a printer
968 // 3. we print using a spool command (print to file first)
971 string const dviname =
972 changeExtension(buffer->latexName(true), "dvi");
974 if (target == "printer") {
975 if (!lyxrc.print_spool_command.empty()) {
976 // case 3: print using a spool
977 string const psname =
978 changeExtension(dviname,".ps");
979 command += ' ' + lyxrc.print_to_file
982 + quoteName(dviname);
985 lyxrc.print_spool_command + ' ';
986 if (target_name != "default") {
987 command2 += lyxrc.print_spool_printerprefix
991 command2 += quoteName(psname);
993 // If successful, then spool command
994 res = one.startscript(
999 res = one.startscript(
1000 Systemcall::DontWait,
1003 // case 2: print directly to a printer
1004 if (target_name != "default")
1005 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
1006 res = one.startscript(
1007 Systemcall::DontWait,
1008 command + quoteName(dviname));
1012 // case 1: print to a file
1013 FileName const filename(makeAbsPath(target_name,
1014 lyx_view_->buffer()->filePath()));
1015 FileName const dvifile(makeAbsPath(dviname, path));
1016 if (filename.exists()) {
1017 docstring text = bformat(
1018 _("The file %1$s already exists.\n\n"
1019 "Do you want to overwrite that file?"),
1020 makeDisplayPath(filename.absFilename()));
1021 if (Alert::prompt(_("Overwrite file?"),
1022 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1025 command += ' ' + lyxrc.print_to_file
1026 + quoteName(filename.toFilesystemEncoding())
1028 + quoteName(dvifile.toFilesystemEncoding());
1029 res = one.startscript(Systemcall::DontWait,
1034 showPrintError(buffer->absFileName());
1038 // FIXME: There is need for a command-line import.
1040 case LFUN_BUFFER_IMPORT:
1045 case LFUN_BUFFER_AUTO_SAVE:
1046 lyx_view_->buffer()->autoSave();
1049 case LFUN_RECONFIGURE:
1050 BOOST_ASSERT(lyx_view_);
1051 // argument is any additional parameter to the configure.py command
1052 reconfigure(*lyx_view_, argument);
1055 case LFUN_HELP_OPEN: {
1056 BOOST_ASSERT(lyx_view_);
1057 string const arg = argument;
1059 setErrorMessage(from_ascii(N_("Missing argument")));
1062 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1063 if (fname.empty()) {
1064 lyxerr << "LyX: unable to find documentation file `"
1065 << arg << "'. Bad installation?" << endl;
1068 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1069 makeDisplayPath(fname.absFilename())));
1070 Buffer * buf = lyx_view_->loadDocument(fname, false);
1073 lyx_view_->setBuffer(buf);
1074 buf->errors("Parse");
1076 updateFlags = Update::None;
1080 // --- version control -------------------------------
1081 case LFUN_VC_REGISTER:
1082 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1083 if (!ensureBufferClean(view()))
1085 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1086 lyx_view_->buffer()->lyxvc().registrer();
1089 updateFlags = Update::Force;
1092 case LFUN_VC_CHECK_IN:
1093 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1094 if (!ensureBufferClean(view()))
1096 if (lyx_view_->buffer()->lyxvc().inUse()
1097 && !lyx_view_->buffer()->isReadonly()) {
1098 lyx_view_->buffer()->lyxvc().checkIn();
1103 case LFUN_VC_CHECK_OUT:
1104 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1105 if (!ensureBufferClean(view()))
1107 if (lyx_view_->buffer()->lyxvc().inUse()
1108 && lyx_view_->buffer()->isReadonly()) {
1109 lyx_view_->buffer()->lyxvc().checkOut();
1114 case LFUN_VC_REVERT:
1115 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1116 lyx_view_->buffer()->lyxvc().revert();
1120 case LFUN_VC_UNDO_LAST:
1121 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1122 lyx_view_->buffer()->lyxvc().undoLast();
1126 // --- lyxserver commands ----------------------------
1127 case LFUN_SERVER_GET_NAME:
1128 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1129 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1130 LYXERR(Debug::INFO, "FNAME["
1131 << lyx_view_->buffer()->absFileName() << ']');
1134 case LFUN_SERVER_NOTIFY:
1135 dispatch_buffer = keyseq.print(KeySequence::Portable);
1136 theServer().notifyClient(to_utf8(dispatch_buffer));
1139 case LFUN_SERVER_GOTO_FILE_ROW: {
1140 BOOST_ASSERT(lyx_view_);
1143 istringstream is(argument);
1144 is >> file_name >> row;
1146 bool loaded = false;
1147 if (prefixIs(file_name, package().temp_dir().absFilename()))
1148 // Needed by inverse dvi search. If it is a file
1149 // in tmpdir, call the apropriated function
1150 buf = theBufferList().getBufferFromTmp(file_name);
1152 // Must replace extension of the file to be .lyx
1153 // and get full path
1154 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1155 // Either change buffer or load the file
1156 if (theBufferList().exists(s.absFilename()))
1157 buf = theBufferList().getBuffer(s.absFilename());
1159 buf = lyx_view_->loadDocument(s);
1165 updateFlags = Update::None;
1170 lyx_view_->setBuffer(buf);
1171 view()->setCursorFromRow(row);
1173 buf->errors("Parse");
1174 updateFlags = Update::FitCursor;
1179 case LFUN_DIALOG_SHOW_NEW_INSET: {
1180 BOOST_ASSERT(lyx_view_);
1181 string const name = cmd.getArg(0);
1182 InsetCode code = insetCode(name);
1183 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1184 bool insetCodeOK = true;
1193 case HYPERLINK_CODE: {
1194 InsetCommandParams p(code);
1195 data = InsetCommandMailer::params2string(name, p);
1198 case INCLUDE_CODE: {
1199 // data is the include type: one of "include",
1200 // "input", "verbatiminput" or "verbatiminput*"
1202 // default type is requested
1204 InsetCommandParams p(INCLUDE_CODE, data);
1205 data = InsetCommandMailer::params2string("include", p);
1209 // \c data == "Boxed" || "Frameless" etc
1210 InsetBoxParams p(data);
1211 data = InsetBoxMailer::params2string(p);
1215 InsetBranchParams p;
1216 data = InsetBranchMailer::params2string(p);
1220 InsetCommandParams p(CITE_CODE);
1221 data = InsetCommandMailer::params2string(name, p);
1225 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1228 case EXTERNAL_CODE: {
1229 InsetExternalParams p;
1230 Buffer const & buffer = *lyx_view_->buffer();
1231 data = InsetExternalMailer::params2string(p, buffer);
1236 data = InsetFloatMailer::params2string(p);
1239 case LISTINGS_CODE: {
1240 InsetListingsParams p;
1241 data = InsetListingsMailer::params2string(p);
1244 case GRAPHICS_CODE: {
1245 InsetGraphicsParams p;
1246 Buffer const & buffer = *lyx_view_->buffer();
1247 data = InsetGraphicsMailer::params2string(p, buffer);
1252 data = InsetNoteMailer::params2string(p);
1257 data = InsetVSpaceMailer::params2string(space);
1262 data = InsetWrapMailer::params2string(p);
1266 lyxerr << "Inset type '" << name <<
1267 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1268 insetCodeOK = false;
1270 } // end switch(code)
1272 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1276 case LFUN_CITATION_INSERT: {
1277 BOOST_ASSERT(lyx_view_);
1278 if (!argument.empty()) {
1279 // we can have one optional argument, delimited by '|'
1280 // citation-insert <key>|<text_before>
1281 // this should be enhanced to also support text_after
1282 // and citation style
1283 string arg = argument;
1285 if (contains(argument, "|")) {
1286 arg = token(argument, '|', 0);
1287 opt1 = token(argument, '|', 1);
1289 InsetCommandParams icp(CITE_CODE);
1290 icp["key"] = from_utf8(arg);
1292 icp["before"] = from_utf8(opt1);
1293 string icstr = InsetCommandMailer::params2string("citation", icp);
1294 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1297 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1301 case LFUN_BUFFER_CHILD_OPEN: {
1302 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1303 Buffer * parent = lyx_view_->buffer();
1304 FileName filename = makeAbsPath(argument, parent->filePath());
1305 view()->saveBookmark(false);
1307 bool parsed = false;
1308 if (theBufferList().exists(filename.absFilename())) {
1309 child = theBufferList().getBuffer(filename.absFilename());
1311 setMessage(bformat(_("Opening child document %1$s..."),
1312 makeDisplayPath(filename.absFilename())));
1313 child = lyx_view_->loadDocument(filename, false);
1317 // Set the parent name of the child document.
1318 // This makes insertion of citations and references in the child work,
1319 // when the target is in the parent or another child document.
1320 child->setParent(parent);
1321 updateLabels(*child->masterBuffer());
1322 lyx_view_->setBuffer(child);
1324 child->errors("Parse");
1327 // If a screen update is required (in case where auto_open is false),
1328 // setBuffer() would have taken care of it already. Otherwise we shall
1329 // reset the update flag because it can cause a circular problem.
1331 updateFlags = Update::None;
1335 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1336 BOOST_ASSERT(lyx_view_);
1337 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1340 case LFUN_KEYMAP_OFF:
1341 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1342 lyx_view_->view()->getIntl().keyMapOn(false);
1345 case LFUN_KEYMAP_PRIMARY:
1346 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1347 lyx_view_->view()->getIntl().keyMapPrim();
1350 case LFUN_KEYMAP_SECONDARY:
1351 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1352 lyx_view_->view()->getIntl().keyMapSec();
1355 case LFUN_KEYMAP_TOGGLE:
1356 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1357 lyx_view_->view()->getIntl().toggleKeyMap();
1363 string rest = split(argument, countstr, ' ');
1364 istringstream is(countstr);
1367 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1368 for (int i = 0; i < count; ++i)
1369 dispatch(lyxaction.lookupFunc(rest));
1373 case LFUN_COMMAND_SEQUENCE: {
1374 // argument contains ';'-terminated commands
1375 string arg = argument;
1376 while (!arg.empty()) {
1378 arg = split(arg, first, ';');
1379 FuncRequest func(lyxaction.lookupFunc(first));
1380 func.origin = cmd.origin;
1388 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1389 func.origin = cmd.origin;
1391 LyX::ref().topLevelCmdDef().release(argument);
1393 if (func.action == LFUN_UNKNOWN_ACTION) {
1394 // unknown command definition
1395 lyxerr << "Warning: unknown command definition `"
1399 // recursion detected
1400 lyxerr << "Warning: Recursion in the command definition `"
1401 << argument << "' detected"
1408 case LFUN_PREFERENCES_SAVE: {
1409 lyxrc.write(makeAbsPath("preferences",
1410 package().user_support().absFilename()),
1415 case LFUN_SET_COLOR: {
1417 string const x11_name = split(argument, lyx_name, ' ');
1418 if (lyx_name.empty() || x11_name.empty()) {
1419 setErrorMessage(from_ascii(N_(
1420 "Syntax: set-color <lyx_name>"
1425 bool const graphicsbg_changed =
1426 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1427 x11_name != lcolor.getX11Name(Color_graphicsbg));
1429 if (!lcolor.setColor(lyx_name, x11_name)) {
1431 bformat(_("Set-color \"%1$s\" failed "
1432 "- color is undefined or "
1433 "may not be redefined"),
1434 from_utf8(lyx_name)));
1438 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1440 if (graphicsbg_changed) {
1441 // FIXME: The graphics cache no longer has a changeDisplay method.
1443 graphics::GCache::get().changeDisplay(true);
1450 BOOST_ASSERT(lyx_view_);
1451 lyx_view_->message(from_utf8(argument));
1454 case LFUN_EXTERNAL_EDIT: {
1455 BOOST_ASSERT(lyx_view_);
1456 FuncRequest fr(action, argument);
1457 InsetExternal().dispatch(view()->cursor(), fr);
1461 case LFUN_GRAPHICS_EDIT: {
1462 FuncRequest fr(action, argument);
1463 InsetGraphics().dispatch(view()->cursor(), fr);
1467 case LFUN_ALL_INSETS_TOGGLE: {
1468 BOOST_ASSERT(lyx_view_);
1470 string const name = split(argument, action, ' ');
1471 InsetCode const inset_code = insetCode(name);
1473 Cursor & cur = view()->cursor();
1474 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1476 Inset & inset = lyx_view_->buffer()->inset();
1477 InsetIterator it = inset_iterator_begin(inset);
1478 InsetIterator const end = inset_iterator_end(inset);
1479 for (; it != end; ++it) {
1480 if (!it->asInsetMath()
1481 && (inset_code == NO_CODE
1482 || inset_code == it->lyxCode())) {
1483 Cursor tmpcur = cur;
1484 tmpcur.pushBackward(*it);
1485 it->dispatch(tmpcur, fr);
1488 updateFlags = Update::Force | Update::FitCursor;
1492 case LFUN_BUFFER_LANGUAGE: {
1493 BOOST_ASSERT(lyx_view_);
1494 Buffer & buffer = *lyx_view_->buffer();
1495 Language const * oldL = buffer.params().language;
1496 Language const * newL = languages.getLanguage(argument);
1497 if (!newL || oldL == newL)
1500 if (oldL->rightToLeft() == newL->rightToLeft()
1501 && !buffer.isMultiLingual())
1502 buffer.changeLanguage(oldL, newL);
1506 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1507 string const fname =
1508 addName(addPath(package().user_support().absFilename(), "templates/"),
1510 Buffer defaults(fname);
1512 istringstream ss(argument);
1515 int const unknown_tokens = defaults.readHeader(lex);
1517 if (unknown_tokens != 0) {
1518 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1519 << unknown_tokens << " unknown token"
1520 << (unknown_tokens == 1 ? "" : "s")
1524 if (defaults.writeFile(FileName(defaults.absFileName())))
1525 setMessage(bformat(_("Document defaults saved in %1$s"),
1526 makeDisplayPath(fname)));
1528 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1532 case LFUN_BUFFER_PARAMS_APPLY: {
1533 BOOST_ASSERT(lyx_view_);
1534 biblio::CiteEngine const oldEngine =
1535 lyx_view_->buffer()->params().getEngine();
1537 Buffer * buffer = lyx_view_->buffer();
1539 TextClassPtr oldClass = buffer->params().textClassPtr();
1541 Cursor & cur = view()->cursor();
1542 cur.recordUndoFullDocument();
1544 istringstream ss(argument);
1547 int const unknown_tokens = buffer->readHeader(lex);
1549 if (unknown_tokens != 0) {
1550 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1551 << unknown_tokens << " unknown token"
1552 << (unknown_tokens == 1 ? "" : "s")
1556 updateLayout(oldClass, buffer);
1558 biblio::CiteEngine const newEngine =
1559 lyx_view_->buffer()->params().getEngine();
1561 if (oldEngine != newEngine) {
1562 FuncRequest fr(LFUN_INSET_REFRESH);
1564 Inset & inset = lyx_view_->buffer()->inset();
1565 InsetIterator it = inset_iterator_begin(inset);
1566 InsetIterator const end = inset_iterator_end(inset);
1567 for (; it != end; ++it)
1568 if (it->lyxCode() == CITE_CODE)
1569 it->dispatch(cur, fr);
1572 updateFlags = Update::Force | Update::FitCursor;
1573 // We are here most certainaly because of a change in the document
1574 // It is then better to make sure that all dialogs are in sync
1575 // with current document settings. LyXView::restartCursor() achieve this.
1576 lyx_view_->restartCursor();
1580 case LFUN_LAYOUT_MODULES_CLEAR: {
1581 BOOST_ASSERT(lyx_view_);
1582 Buffer * buffer = lyx_view_->buffer();
1583 TextClassPtr oldClass = buffer->params().textClassPtr();
1584 view()->cursor().recordUndoFullDocument();
1585 buffer->params().clearLayoutModules();
1586 buffer->params().makeTextClass();
1587 updateLayout(oldClass, buffer);
1588 updateFlags = Update::Force | Update::FitCursor;
1592 case LFUN_LAYOUT_MODULE_ADD: {
1593 BOOST_ASSERT(lyx_view_);
1594 Buffer * buffer = lyx_view_->buffer();
1595 TextClassPtr oldClass = buffer->params().textClassPtr();
1596 view()->cursor().recordUndoFullDocument();
1597 buffer->params().addLayoutModule(argument);
1598 buffer->params().makeTextClass();
1599 updateLayout(oldClass, buffer);
1600 updateFlags = Update::Force | Update::FitCursor;
1604 case LFUN_TEXTCLASS_APPLY: {
1605 BOOST_ASSERT(lyx_view_);
1606 Buffer * buffer = lyx_view_->buffer();
1608 loadTextClass(argument, buffer->filePath());
1610 pair<bool, BaseClassIndex> const tc_pair =
1611 textclasslist.numberOfClass(argument);
1616 BaseClassIndex const old_class = buffer->params().baseClass();
1617 BaseClassIndex const new_class = tc_pair.second;
1619 if (old_class == new_class)
1623 //Save the old, possibly modular, layout for use in conversion.
1624 TextClassPtr oldClass = buffer->params().textClassPtr();
1625 view()->cursor().recordUndoFullDocument();
1626 buffer->params().setBaseClass(new_class);
1627 buffer->params().makeTextClass();
1628 updateLayout(oldClass, buffer);
1629 updateFlags = Update::Force | Update::FitCursor;
1633 case LFUN_LAYOUT_RELOAD: {
1634 BOOST_ASSERT(lyx_view_);
1635 Buffer * buffer = lyx_view_->buffer();
1636 TextClassPtr oldClass = buffer->params().textClassPtr();
1637 BaseClassIndex const tc = buffer->params().baseClass();
1638 textclasslist.reset(tc);
1639 buffer->params().setBaseClass(tc);
1640 buffer->params().makeTextClass();
1641 updateLayout(oldClass, buffer);
1642 updateFlags = Update::Force | Update::FitCursor;
1646 case LFUN_TEXTCLASS_LOAD:
1647 loadTextClass(argument, lyx_view_->buffer()->filePath());
1650 case LFUN_LYXRC_APPLY: {
1651 LyXRC const lyxrc_orig = lyxrc;
1653 istringstream ss(argument);
1654 bool const success = lyxrc.read(ss) == 0;
1657 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1658 << "Unable to read lyxrc data"
1663 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1665 theApp()->resetGui();
1667 /// We force the redraw in any case because there might be
1668 /// some screen font changes.
1669 /// FIXME: only the current view will be updated. the Gui
1670 /// class is able to furnish the list of views.
1671 updateFlags = Update::Force;
1675 case LFUN_BOOKMARK_GOTO:
1676 // go to bookmark, open unopened file and switch to buffer if necessary
1677 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1678 updateFlags = Update::FitCursor;
1681 case LFUN_BOOKMARK_CLEAR:
1682 LyX::ref().session().bookmarks().clear();
1686 BOOST_ASSERT(theApp());
1687 // Let the frontend dispatch its own actions.
1688 if (theApp()->dispatch(cmd))
1689 // Nothing more to do.
1692 // Let the current LyXView dispatch its own actions.
1693 BOOST_ASSERT(lyx_view_);
1694 if (lyx_view_->dispatch(cmd)) {
1695 if (lyx_view_->view())
1696 updateFlags = lyx_view_->view()->cursor().result().update();
1700 BOOST_ASSERT(lyx_view_->view());
1701 // Let the current BufferView dispatch its own actions.
1702 if (view()->dispatch(cmd)) {
1703 // The BufferView took care of its own updates if needed.
1704 updateFlags = Update::None;
1708 // Let the current Cursor dispatch its own actions.
1709 Cursor old = view()->cursor();
1710 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1711 cursorPosBeforeDispatchY_);
1712 view()->cursor().dispatch(cmd);
1714 // notify insets we just left
1715 if (view()->cursor() != old) {
1717 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1719 view()->cursor().fixIfBroken();
1722 updateFlags = view()->cursor().result().update();
1725 if (lyx_view_ && lyx_view_->buffer()) {
1726 // BufferView::update() updates the ViewMetricsInfo and
1727 // also initializes the position cache for all insets in
1728 // (at least partially) visible top-level paragraphs.
1729 // We will redraw the screen only if needed.
1730 view()->processUpdateFlags(updateFlags);
1732 // if we executed a mutating lfun, mark the buffer as dirty
1734 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1735 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1736 lyx_view_->buffer()->markDirty();
1738 //Do we have a selection?
1739 theSelection().haveSelection(view()->cursor().selection());
1742 if (!quitting && lyx_view_) {
1743 // Some messages may already be translated, so we cannot use _()
1744 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1749 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1751 const bool verbose = (cmd.origin == FuncRequest::MENU
1752 || cmd.origin == FuncRequest::TOOLBAR
1753 || cmd.origin == FuncRequest::COMMANDBUFFER);
1755 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1756 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1758 lyx_view_->message(msg);
1762 docstring dispatch_msg = msg;
1763 if (!dispatch_msg.empty())
1764 dispatch_msg += ' ';
1766 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1768 bool argsadded = false;
1770 if (!cmd.argument().empty()) {
1771 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1772 comname += ' ' + cmd.argument();
1777 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1779 if (!shortcuts.empty())
1780 comname += ": " + shortcuts;
1781 else if (!argsadded && !cmd.argument().empty())
1782 comname += ' ' + cmd.argument();
1784 if (!comname.empty()) {
1785 comname = rtrim(comname);
1786 dispatch_msg += '(' + rtrim(comname) + ')';
1789 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1790 if (!dispatch_msg.empty())
1791 lyx_view_->message(dispatch_msg);
1795 void LyXFunc::closeBuffer()
1797 // goto bookmark to update bookmark pit.
1798 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1799 gotoBookmark(i+1, false, false);
1801 lyx_view_->closeBuffer();
1805 void LyXFunc::reloadBuffer()
1807 FileName filename = lyx_view_->buffer()->fileName();
1808 // The user has already confirmed that the changes, if any, should
1809 // be discarded. So we just release the Buffer and don't call closeBuffer();
1810 theBufferList().release(lyx_view_->buffer());
1811 Buffer * buf = lyx_view_->loadDocument(filename);
1812 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1816 lyx_view_->setBuffer(buf);
1817 buf->errors("Parse");
1818 str = bformat(_("Document %1$s reloaded."), disp_fn);
1820 str = bformat(_("Could not reload document %1$s"), disp_fn);
1822 lyx_view_->message(str);
1825 // Each "lyx_view_" should have it's own message method. lyxview and
1826 // the minibuffer would use the minibuffer, but lyxserver would
1827 // send an ERROR signal to its client. Alejandro 970603
1828 // This function is bit problematic when it comes to NLS, to make the
1829 // lyx servers client be language indepenent we must not translate
1830 // strings sent to this func.
1831 void LyXFunc::setErrorMessage(docstring const & m) const
1833 dispatch_buffer = m;
1838 void LyXFunc::setMessage(docstring const & m) const
1840 dispatch_buffer = m;
1844 docstring const LyXFunc::viewStatusMessage()
1846 // When meta-fake key is pressed, show the key sequence so far + "M-".
1848 return keyseq.print(KeySequence::ForGui) + "M-";
1850 // Else, when a non-complete key sequence is pressed,
1851 // show the available options.
1852 if (keyseq.length() > 0 && !keyseq.deleted())
1853 return keyseq.printOptions(true);
1855 BOOST_ASSERT(lyx_view_);
1856 if (!lyx_view_->buffer())
1857 return _("Welcome to LyX!");
1859 return view()->cursor().currentState();
1863 BufferView * LyXFunc::view() const
1865 BOOST_ASSERT(lyx_view_);
1866 return lyx_view_->view();
1870 bool LyXFunc::wasMetaKey() const
1872 return (meta_fake_bit != NoModifier);
1876 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1879 lyx_view_->message(_("Converting document to new document class..."));
1881 StableDocIterator backcur(view()->cursor());
1882 ErrorList & el = buffer->errorList("Class Switch");
1883 cap::switchBetweenClasses(
1884 oldlayout, buffer->params().textClassPtr(),
1885 static_cast<InsetText &>(buffer->inset()), el);
1887 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1889 buffer->errors("Class Switch");
1890 updateLabels(*buffer);
1896 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1898 // Why the switch you might ask. It is a trick to ensure that all
1899 // the elements in the LyXRCTags enum is handled. As you can see
1900 // there are no breaks at all. So it is just a huge fall-through.
1901 // The nice thing is that we will get a warning from the compiler
1902 // if we forget an element.
1903 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1905 case LyXRC::RC_ACCEPT_COMPOUND:
1906 case LyXRC::RC_ALT_LANG:
1907 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1908 case LyXRC::RC_PLAINTEXT_LINELEN:
1909 case LyXRC::RC_AUTOREGIONDELETE:
1910 case LyXRC::RC_AUTORESET_OPTIONS:
1911 case LyXRC::RC_AUTOSAVE:
1912 case LyXRC::RC_AUTO_NUMBER:
1913 case LyXRC::RC_BACKUPDIR_PATH:
1914 case LyXRC::RC_BIBTEX_COMMAND:
1915 case LyXRC::RC_BINDFILE:
1916 case LyXRC::RC_CHECKLASTFILES:
1917 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1918 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1919 case LyXRC::RC_COMPLETION_INLINE_MATH:
1920 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1921 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1922 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1923 case LyXRC::RC_COMPLETION_POPUP_MATH:
1924 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1925 case LyXRC::RC_USELASTFILEPOS:
1926 case LyXRC::RC_LOADSESSION:
1927 case LyXRC::RC_CHKTEX_COMMAND:
1928 case LyXRC::RC_CONVERTER:
1929 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1930 case LyXRC::RC_COPIER:
1931 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1932 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1933 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1934 case LyXRC::RC_DATE_INSERT_FORMAT:
1935 case LyXRC::RC_DEFAULT_LANGUAGE:
1936 case LyXRC::RC_DEFAULT_PAPERSIZE:
1937 case LyXRC::RC_DEFFILE:
1938 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1939 case LyXRC::RC_DISPLAY_GRAPHICS:
1940 case LyXRC::RC_DOCUMENTPATH:
1941 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1942 FileName path(lyxrc_new.document_path);
1943 if (path.exists() && path.isDirectory())
1944 package().document_dir() = FileName(lyxrc.document_path);
1946 case LyXRC::RC_ESC_CHARS:
1947 case LyXRC::RC_EXAMPLEPATH:
1948 case LyXRC::RC_FONT_ENCODING:
1949 case LyXRC::RC_FORMAT:
1950 case LyXRC::RC_INDEX_COMMAND:
1951 case LyXRC::RC_INPUT:
1952 case LyXRC::RC_KBMAP:
1953 case LyXRC::RC_KBMAP_PRIMARY:
1954 case LyXRC::RC_KBMAP_SECONDARY:
1955 case LyXRC::RC_LABEL_INIT_LENGTH:
1956 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1957 case LyXRC::RC_LANGUAGE_AUTO_END:
1958 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1959 case LyXRC::RC_LANGUAGE_COMMAND_END:
1960 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1961 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1962 case LyXRC::RC_LANGUAGE_PACKAGE:
1963 case LyXRC::RC_LANGUAGE_USE_BABEL:
1964 case LyXRC::RC_MACRO_EDIT_STYLE:
1965 case LyXRC::RC_MAKE_BACKUP:
1966 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1967 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1968 case LyXRC::RC_NUMLASTFILES:
1969 case LyXRC::RC_PATH_PREFIX:
1970 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1971 prependEnvPath("PATH", lyxrc.path_prefix);
1973 case LyXRC::RC_PERS_DICT:
1974 case LyXRC::RC_PREVIEW:
1975 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1976 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1977 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1978 case LyXRC::RC_PRINTCOPIESFLAG:
1979 case LyXRC::RC_PRINTER:
1980 case LyXRC::RC_PRINTEVENPAGEFLAG:
1981 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1982 case LyXRC::RC_PRINTFILEEXTENSION:
1983 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1984 case LyXRC::RC_PRINTODDPAGEFLAG:
1985 case LyXRC::RC_PRINTPAGERANGEFLAG:
1986 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1987 case LyXRC::RC_PRINTPAPERFLAG:
1988 case LyXRC::RC_PRINTREVERSEFLAG:
1989 case LyXRC::RC_PRINTSPOOL_COMMAND:
1990 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1991 case LyXRC::RC_PRINTTOFILE:
1992 case LyXRC::RC_PRINTTOPRINTER:
1993 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1994 case LyXRC::RC_PRINT_COMMAND:
1995 case LyXRC::RC_RTL_SUPPORT:
1996 case LyXRC::RC_SCREEN_DPI:
1997 case LyXRC::RC_SCREEN_FONT_ROMAN:
1998 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1999 case LyXRC::RC_SCREEN_FONT_SANS:
2000 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
2001 case LyXRC::RC_SCREEN_FONT_SCALABLE:
2002 case LyXRC::RC_SCREEN_FONT_SIZES:
2003 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
2004 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
2005 case LyXRC::RC_GEOMETRY_SESSION:
2006 case LyXRC::RC_SCREEN_ZOOM:
2007 case LyXRC::RC_SERVERPIPE:
2008 case LyXRC::RC_SET_COLOR:
2009 case LyXRC::RC_SHOW_BANNER:
2010 case LyXRC::RC_SPELL_COMMAND:
2011 case LyXRC::RC_TEMPDIRPATH:
2012 case LyXRC::RC_TEMPLATEPATH:
2013 case LyXRC::RC_TEX_ALLOWS_SPACES:
2014 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2015 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2016 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2018 case LyXRC::RC_UIFILE:
2019 case LyXRC::RC_USER_EMAIL:
2020 case LyXRC::RC_USER_NAME:
2021 case LyXRC::RC_USETEMPDIR:
2022 case LyXRC::RC_USE_ALT_LANG:
2023 case LyXRC::RC_USE_CONVERTER_CACHE:
2024 case LyXRC::RC_USE_ESC_CHARS:
2025 case LyXRC::RC_USE_INP_ENC:
2026 case LyXRC::RC_USE_PERS_DICT:
2027 case LyXRC::RC_USE_TOOLTIP:
2028 case LyXRC::RC_USE_PIXMAP_CACHE:
2029 case LyXRC::RC_USE_SPELL_LIB:
2030 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2031 case LyXRC::RC_SORT_LAYOUTS:
2032 case LyXRC::RC_FULL_SCREEN_LIMIT:
2033 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2034 case LyXRC::RC_FULL_SCREEN_TABBAR:
2035 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2036 case LyXRC::RC_FULL_SCREEN_WIDTH:
2037 case LyXRC::RC_VISUAL_CURSOR:
2038 case LyXRC::RC_VIEWER:
2039 case LyXRC::RC_LAST: