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 "BaseClassList.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetListings.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetTabular.h"
70 #include "insets/InsetVSpace.h"
71 #include "insets/InsetWrap.h"
73 #include "frontends/alert.h"
74 #include "frontends/Application.h"
75 #include "frontends/KeySymbol.h"
76 #include "frontends/LyXView.h"
77 #include "frontends/Selection.h"
79 #include "support/debug.h"
80 #include "support/environment.h"
81 #include "support/FileName.h"
82 #include "support/filetools.h"
83 #include "support/gettext.h"
84 #include "support/lstrings.h"
85 #include "support/Path.h"
86 #include "support/Package.h"
87 #include "support/Systemcall.h"
88 #include "support/convert.h"
89 #include "support/os.h"
95 using namespace lyx::support;
99 using frontend::LyXView;
101 namespace Alert = frontend::Alert;
106 // This function runs "configure" and then rereads lyx.defaults to
107 // reconfigure the automatic settings.
108 void reconfigure(LyXView & lv, string const & option)
110 // emit message signal.
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 (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);
235 //FIXME: bookmark handling is a frontend issue. This code should be transferred
236 // to GuiView and be GuiView and be window dependent.
237 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
239 BOOST_ASSERT(lyx_view_);
240 if (!LyX::ref().session().bookmarks().isValid(idx))
242 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
243 BOOST_ASSERT(!bm.filename.empty());
244 string const file = bm.filename.absFilename();
245 // if the file is not opened, open it.
246 if (!theBufferList().exists(file)) {
248 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
252 // open may fail, so we need to test it again
253 if (!theBufferList().exists(file))
256 // if the current buffer is not that one, switch to it.
257 if (lyx_view_->buffer()->absFileName() != file) {
260 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
262 // moveToPosition try paragraph id first and then paragraph (pit, pos).
263 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
264 bm.top_id, bm.top_pos))
267 // Cursor jump succeeded!
268 Cursor const & cur = view()->cursor();
269 pit_type new_pit = cur.pit();
270 pos_type new_pos = cur.pos();
271 int new_id = cur.paragraph().id();
273 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
274 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
275 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
276 || bm.top_id != new_id) {
277 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
278 new_pit, new_pos, new_id);
283 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
285 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
287 // Do nothing if we have nothing (JMarc)
288 if (!keysym.isOK()) {
289 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
290 lyx_view_->restartCursor();
294 if (keysym.isModifier()) {
295 LYXERR(Debug::KEY, "isModifier true");
296 lyx_view_->restartCursor();
300 //Encoding const * encoding = view()->cursor().getEncoding();
301 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
302 // FIXME: encoded_last_key shadows the member variable of the same
303 // name. Is that intended?
304 char_type encoded_last_key = keysym.getUCSEncoded();
306 // Do a one-deep top-level lookup for
307 // cancel and meta-fake keys. RVDK_PATCH_5
308 cancel_meta_seq.reset();
310 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
311 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
313 // When not cancel or meta-fake, do the normal lookup.
314 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
315 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
316 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
317 // remove Caps Lock and Mod2 as a modifiers
318 func = keyseq.addkey(keysym, (state | meta_fake_bit));
319 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
322 // Dont remove this unless you know what you are doing.
323 meta_fake_bit = NoModifier;
325 // Can this happen now ?
326 if (func.action == LFUN_NOACTION)
327 func = FuncRequest(LFUN_COMMAND_PREFIX);
329 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
330 << keyseq.print(KeySequence::Portable) << ']');
332 // already here we know if it any point in going further
333 // why not return already here if action == -1 and
334 // num_bytes == 0? (Lgb)
336 if (keyseq.length() > 1)
337 lyx_view_->message(keyseq.print(KeySequence::ForGui));
340 // Maybe user can only reach the key via holding down shift.
341 // Let's see. But only if shift is the only modifier
342 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
343 LYXERR(Debug::KEY, "Trying without shift");
344 func = keyseq.addkey(keysym, NoModifier);
345 LYXERR(Debug::KEY, "Action now " << func.action);
348 if (func.action == LFUN_UNKNOWN_ACTION) {
349 // Hmm, we didn't match any of the keysequences. See
350 // if it's normal insertable text not already covered
352 if (keysym.isText() && keyseq.length() == 1) {
353 LYXERR(Debug::KEY, "isText() is true, inserting.");
354 func = FuncRequest(LFUN_SELF_INSERT,
355 FuncRequest::KEYBOARD);
357 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
358 lyx_view_->message(_("Unknown function."));
359 lyx_view_->restartCursor();
364 if (func.action == LFUN_SELF_INSERT) {
365 if (encoded_last_key != 0) {
366 docstring const arg(1, encoded_last_key);
367 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
368 FuncRequest::KEYBOARD));
369 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
370 lyx_view_->updateCompletion(true, true);
376 if (func.action == LFUN_CHAR_DELETE_BACKWARD)
377 // backspace is not a self-insertion. But it
378 // still should not hide the completion popup.
379 // FIXME: more clever way to detect those movements
380 lyx_view_->updateCompletion(false, true);
382 lyx_view_->updateCompletion(false, false);
386 lyx_view_->restartCursor();
390 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
392 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
395 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
397 if (cmd.action == LFUN_NOACTION) {
398 flag.message(from_utf8(N_("Nothing to do")));
403 switch (cmd.action) {
404 case LFUN_UNKNOWN_ACTION:
405 #ifndef HAVE_LIBAIKSAURUS
406 case LFUN_THESAURUS_ENTRY:
416 if (flag.unknown()) {
417 flag.message(from_utf8(N_("Unknown action")));
421 if (!flag.enabled()) {
422 if (flag.message().empty())
423 flag.message(from_utf8(N_("Command disabled")));
427 // Check whether we need a buffer
428 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
430 flag.message(from_utf8(N_("Command not allowed with"
431 "out any document open")));
436 // I would really like to avoid having this switch and rather try to
437 // encode this in the function itself.
438 // -- And I'd rather let an inset decide which LFUNs it is willing
439 // to handle (Andre')
441 switch (cmd.action) {
443 // FIXME: these cases should be hidden in GuiApplication::getStatus().
444 case LFUN_WINDOW_CLOSE:
446 return theApp()->getStatus(cmd);
450 // FIXME: these cases should be hidden in GuiView::getStatus().
451 case LFUN_DIALOG_TOGGLE:
452 case LFUN_DIALOG_SHOW:
453 case LFUN_DIALOG_UPDATE:
454 if (cmd.argument() == "prefs"
455 || cmd.argument() == "aboutlyx")
458 return lyx_view_->getStatus(cmd);
463 case LFUN_TOOLBAR_TOGGLE:
464 case LFUN_INSET_APPLY:
465 case LFUN_BUFFER_WRITE:
466 case LFUN_BUFFER_WRITE_AS:
467 case LFUN_SPLIT_VIEW:
468 case LFUN_CLOSE_TAB_GROUP:
469 case LFUN_COMPLETION_POPUP:
470 case LFUN_COMPLETION_INLINE:
471 case LFUN_COMPLETION_COMPLETE:
473 return lyx_view_->getStatus(cmd);
477 case LFUN_BUFFER_TOGGLE_READ_ONLY:
478 flag.setOnOff(buf->isReadonly());
481 case LFUN_BUFFER_SWITCH:
482 // toggle on the current buffer, but do not toggle off
483 // the other ones (is that a good idea?)
484 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
488 case LFUN_BUFFER_EXPORT:
489 enable = cmd.argument() == "custom"
490 || buf->isExportable(to_utf8(cmd.argument()));
493 case LFUN_BUFFER_CHKTEX:
494 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
497 case LFUN_BUILD_PROGRAM:
498 enable = buf->isExportable("program");
501 case LFUN_VC_REGISTER:
502 enable = !buf->lyxvc().inUse();
504 case LFUN_VC_CHECK_IN:
505 enable = buf->lyxvc().inUse() && !buf->isReadonly();
507 case LFUN_VC_CHECK_OUT:
508 enable = buf->lyxvc().inUse() && buf->isReadonly();
511 case LFUN_VC_UNDO_LAST:
512 enable = buf->lyxvc().inUse();
514 case LFUN_BUFFER_RELOAD:
515 enable = !buf->isUnnamed() && buf->fileName().exists()
516 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
519 case LFUN_CITATION_INSERT: {
520 FuncRequest fr(LFUN_INSET_INSERT, "citation");
521 enable = getStatus(fr).enabled();
525 // This could be used for the no-GUI version. The GUI version is handled in
526 // LyXView::getStatus(). See above.
528 case LFUN_BUFFER_WRITE:
529 case LFUN_BUFFER_WRITE_AS: {
530 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
531 enable = b && (b->isUnnamed() || !b->isClean());
536 case LFUN_BUFFER_WRITE_ALL: {
537 // We enable the command only if there are some modified buffers
538 Buffer * first = theBufferList().first();
543 // We cannot use a for loop as the buffer list is a cycle.
549 b = theBufferList().next(b);
550 } while (b != first);
554 case LFUN_BOOKMARK_GOTO: {
555 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
556 enable = LyX::ref().session().bookmarks().isValid(num);
560 case LFUN_BOOKMARK_CLEAR:
561 enable = LyX::ref().session().bookmarks().size() > 0;
564 // this one is difficult to get right. As a half-baked
565 // solution, we consider only the first action of the sequence
566 case LFUN_COMMAND_SEQUENCE: {
567 // argument contains ';'-terminated commands
568 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
569 FuncRequest func(lyxaction.lookupFunc(firstcmd));
570 func.origin = cmd.origin;
571 flag = getStatus(func);
577 string name = to_utf8(cmd.argument());
578 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
579 func.origin = cmd.origin;
580 flag = getStatus(func);
581 LyX::ref().topLevelCmdDef().release(name);
583 // catch recursion or unknown command definiton
584 // all operations until the recursion or unknown command
585 // definiton occures are performed, so set the state to enabled
591 case LFUN_BUFFER_NEW:
592 case LFUN_BUFFER_NEW_TEMPLATE:
593 case LFUN_WORD_FIND_FORWARD:
594 case LFUN_WORD_FIND_BACKWARD:
595 case LFUN_COMMAND_PREFIX:
596 case LFUN_COMMAND_EXECUTE:
598 case LFUN_META_PREFIX:
599 case LFUN_BUFFER_CLOSE:
600 case LFUN_BUFFER_UPDATE:
601 case LFUN_BUFFER_VIEW:
602 case LFUN_MASTER_BUFFER_UPDATE:
603 case LFUN_MASTER_BUFFER_VIEW:
604 case LFUN_BUFFER_IMPORT:
605 case LFUN_BUFFER_AUTO_SAVE:
606 case LFUN_RECONFIGURE:
609 case LFUN_DROP_LAYOUTS_CHOICE:
611 case LFUN_SERVER_GET_NAME:
612 case LFUN_SERVER_NOTIFY:
613 case LFUN_SERVER_GOTO_FILE_ROW:
614 case LFUN_DIALOG_HIDE:
615 case LFUN_DIALOG_DISCONNECT_INSET:
616 case LFUN_BUFFER_CHILD_OPEN:
618 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
619 case LFUN_KEYMAP_OFF:
620 case LFUN_KEYMAP_PRIMARY:
621 case LFUN_KEYMAP_SECONDARY:
622 case LFUN_KEYMAP_TOGGLE:
624 case LFUN_BUFFER_EXPORT_CUSTOM:
625 case LFUN_BUFFER_PRINT:
626 case LFUN_PREFERENCES_SAVE:
627 case LFUN_SCREEN_FONT_UPDATE:
630 case LFUN_EXTERNAL_EDIT:
631 case LFUN_GRAPHICS_EDIT:
632 case LFUN_ALL_INSETS_TOGGLE:
633 case LFUN_BUFFER_LANGUAGE:
634 case LFUN_TEXTCLASS_APPLY:
635 case LFUN_TEXTCLASS_LOAD:
636 case LFUN_BUFFER_SAVE_AS_DEFAULT:
637 case LFUN_BUFFER_PARAMS_APPLY:
638 case LFUN_LAYOUT_MODULES_CLEAR:
639 case LFUN_LAYOUT_MODULE_ADD:
640 case LFUN_LAYOUT_RELOAD:
641 case LFUN_LYXRC_APPLY:
642 case LFUN_BUFFER_NEXT:
643 case LFUN_BUFFER_PREVIOUS:
644 case LFUN_WINDOW_NEW:
646 // these are handled in our dispatch()
654 if (!getLocalStatus(view()->cursor(), cmd, flag))
655 flag = view()->getStatus(cmd);
661 // Can we use a readonly buffer?
662 if (buf && buf->isReadonly()
663 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
664 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
665 flag.message(from_utf8(N_("Document is read-only")));
669 // Are we in a DELETED change-tracking region?
671 && lookupChangeType(view()->cursor(), true) == Change::DELETED
672 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
673 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
674 flag.message(from_utf8(N_("This portion of the document is deleted.")));
678 // the default error message if we disable the command
679 if (!flag.enabled() && flag.message().empty())
680 flag.message(from_utf8(N_("Command disabled")));
686 bool LyXFunc::ensureBufferClean(BufferView * bv)
688 Buffer & buf = bv->buffer();
692 docstring const file = buf.fileName().displayName(30);
693 docstring text = bformat(_("The document %1$s has unsaved "
694 "changes.\n\nDo you want to save "
695 "the document?"), file);
696 int const ret = Alert::prompt(_("Save changed document?"),
697 text, 0, 1, _("&Save"),
701 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
703 return buf.isClean();
709 void showPrintError(string const & name)
711 docstring str = bformat(_("Could not print the document %1$s.\n"
712 "Check that your printer is set up correctly."),
713 makeDisplayPath(name, 50));
714 Alert::error(_("Print document failed"), str);
718 bool loadTextClass(string const & name, string const & buf_path)
720 if (!BaseClassList::get().haveClass(name)) {
721 lyxerr << "Document class \"" << name
722 << "\" does not exist."
727 TextClass const & tc = BaseClassList::get()[name];
728 if (!tc.load(buf_path)) {
729 docstring s = bformat(_("The document class %1$s."
730 "could not be loaded."), from_utf8(name));
731 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:
834 lyx_view_->closeBuffer();
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 DocumentClass * oldClass = buffer->params().documentClassPtr();
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 DocumentClass * oldClass = buffer->params().documentClassPtr();
1584 view()->cursor().recordUndoFullDocument();
1585 buffer->params().clearLayoutModules();
1586 buffer->params().makeDocumentClass();
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 DocumentClass * oldClass = buffer->params().documentClassPtr();
1596 view()->cursor().recordUndoFullDocument();
1597 buffer->params().addLayoutModule(argument);
1598 buffer->params().makeDocumentClass();
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 if (!loadTextClass(argument, buffer->filePath()))
1611 TextClass const * old_class = buffer->params().baseClass();
1612 TextClass const * new_class = &(BaseClassList::get()[argument]);
1614 if (old_class == new_class)
1618 //Save the old, possibly modular, layout for use in conversion.
1619 DocumentClass * oldDocClass = buffer->params().documentClassPtr();
1620 view()->cursor().recordUndoFullDocument();
1621 buffer->params().setBaseClass(argument);
1622 buffer->params().makeDocumentClass();
1623 updateLayout(oldDocClass, buffer);
1624 updateFlags = Update::Force | Update::FitCursor;
1628 case LFUN_LAYOUT_RELOAD: {
1629 BOOST_ASSERT(lyx_view_);
1630 Buffer * buffer = lyx_view_->buffer();
1631 DocumentClass * oldClass = buffer->params().documentClassPtr();
1632 BaseClassIndex bc = buffer->params().baseClassID();
1633 BaseClassList::get().reset(bc);
1634 buffer->params().makeDocumentClass();
1635 updateLayout(oldClass, buffer);
1636 updateFlags = Update::Force | Update::FitCursor;
1640 case LFUN_TEXTCLASS_LOAD:
1641 loadTextClass(argument, lyx_view_->buffer()->filePath());
1644 case LFUN_LYXRC_APPLY: {
1645 LyXRC const lyxrc_orig = lyxrc;
1647 istringstream ss(argument);
1648 bool const success = lyxrc.read(ss) == 0;
1651 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1652 << "Unable to read lyxrc data"
1657 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1659 theApp()->resetGui();
1661 /// We force the redraw in any case because there might be
1662 /// some screen font changes.
1663 /// FIXME: only the current view will be updated. the Gui
1664 /// class is able to furnish the list of views.
1665 updateFlags = Update::Force;
1669 case LFUN_BOOKMARK_GOTO:
1670 // go to bookmark, open unopened file and switch to buffer if necessary
1671 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1672 updateFlags = Update::FitCursor;
1675 case LFUN_BOOKMARK_CLEAR:
1676 LyX::ref().session().bookmarks().clear();
1680 BOOST_ASSERT(theApp());
1681 // Let the frontend dispatch its own actions.
1682 if (theApp()->dispatch(cmd))
1683 // Nothing more to do.
1686 // Let the current LyXView dispatch its own actions.
1687 BOOST_ASSERT(lyx_view_);
1688 if (lyx_view_->dispatch(cmd)) {
1689 if (lyx_view_->view())
1690 updateFlags = lyx_view_->view()->cursor().result().update();
1694 BOOST_ASSERT(lyx_view_->view());
1695 // Let the current BufferView dispatch its own actions.
1696 if (view()->dispatch(cmd)) {
1697 // The BufferView took care of its own updates if needed.
1698 updateFlags = Update::None;
1702 // Let the current Cursor dispatch its own actions.
1703 Cursor old = view()->cursor();
1704 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1705 cursorPosBeforeDispatchY_);
1706 view()->cursor().dispatch(cmd);
1708 // notify insets we just left
1709 if (view()->cursor() != old) {
1711 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1713 view()->cursor().fixIfBroken();
1716 updateFlags = view()->cursor().result().update();
1719 if (lyx_view_ && lyx_view_->buffer()) {
1720 // BufferView::update() updates the ViewMetricsInfo and
1721 // also initializes the position cache for all insets in
1722 // (at least partially) visible top-level paragraphs.
1723 // We will redraw the screen only if needed.
1724 view()->processUpdateFlags(updateFlags);
1726 // if we executed a mutating lfun, mark the buffer as dirty
1728 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1729 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1730 lyx_view_->buffer()->markDirty();
1732 //Do we have a selection?
1733 theSelection().haveSelection(view()->cursor().selection());
1737 // Some messages may already be translated, so we cannot use _()
1738 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1743 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1745 const bool verbose = (cmd.origin == FuncRequest::MENU
1746 || cmd.origin == FuncRequest::TOOLBAR
1747 || cmd.origin == FuncRequest::COMMANDBUFFER);
1749 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1750 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1752 lyx_view_->message(msg);
1756 docstring dispatch_msg = msg;
1757 if (!dispatch_msg.empty())
1758 dispatch_msg += ' ';
1760 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1762 bool argsadded = false;
1764 if (!cmd.argument().empty()) {
1765 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1766 comname += ' ' + cmd.argument();
1771 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1773 if (!shortcuts.empty())
1774 comname += ": " + shortcuts;
1775 else if (!argsadded && !cmd.argument().empty())
1776 comname += ' ' + cmd.argument();
1778 if (!comname.empty()) {
1779 comname = rtrim(comname);
1780 dispatch_msg += '(' + rtrim(comname) + ')';
1783 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1784 if (!dispatch_msg.empty())
1785 lyx_view_->message(dispatch_msg);
1789 void LyXFunc::reloadBuffer()
1791 FileName filename = lyx_view_->buffer()->fileName();
1792 // The user has already confirmed that the changes, if any, should
1793 // be discarded. So we just release the Buffer and don't call closeBuffer();
1794 theBufferList().release(lyx_view_->buffer());
1795 Buffer * buf = lyx_view_->loadDocument(filename);
1796 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1800 lyx_view_->setBuffer(buf);
1801 buf->errors("Parse");
1802 str = bformat(_("Document %1$s reloaded."), disp_fn);
1804 str = bformat(_("Could not reload document %1$s"), disp_fn);
1806 lyx_view_->message(str);
1809 // Each "lyx_view_" should have it's own message method. lyxview and
1810 // the minibuffer would use the minibuffer, but lyxserver would
1811 // send an ERROR signal to its client. Alejandro 970603
1812 // This function is bit problematic when it comes to NLS, to make the
1813 // lyx servers client be language indepenent we must not translate
1814 // strings sent to this func.
1815 void LyXFunc::setErrorMessage(docstring const & m) const
1817 dispatch_buffer = m;
1822 void LyXFunc::setMessage(docstring const & m) const
1824 dispatch_buffer = m;
1828 docstring const LyXFunc::viewStatusMessage()
1830 // When meta-fake key is pressed, show the key sequence so far + "M-".
1832 return keyseq.print(KeySequence::ForGui) + "M-";
1834 // Else, when a non-complete key sequence is pressed,
1835 // show the available options.
1836 if (keyseq.length() > 0 && !keyseq.deleted())
1837 return keyseq.printOptions(true);
1839 BOOST_ASSERT(lyx_view_);
1840 if (!lyx_view_->buffer())
1841 return _("Welcome to LyX!");
1843 return view()->cursor().currentState();
1847 BufferView * LyXFunc::view() const
1849 BOOST_ASSERT(lyx_view_);
1850 return lyx_view_->view();
1854 bool LyXFunc::wasMetaKey() const
1856 return (meta_fake_bit != NoModifier);
1860 void LyXFunc::updateLayout(DocumentClass * oldlayout,Buffer * buffer)
1862 lyx_view_->message(_("Converting document to new document class..."));
1864 StableDocIterator backcur(view()->cursor());
1865 ErrorList & el = buffer->errorList("Class Switch");
1866 cap::switchBetweenClasses(
1867 oldlayout, buffer->params().documentClassPtr(),
1868 static_cast<InsetText &>(buffer->inset()), el);
1870 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1872 buffer->errors("Class Switch");
1873 updateLabels(*buffer);
1879 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1881 // Why the switch you might ask. It is a trick to ensure that all
1882 // the elements in the LyXRCTags enum is handled. As you can see
1883 // there are no breaks at all. So it is just a huge fall-through.
1884 // The nice thing is that we will get a warning from the compiler
1885 // if we forget an element.
1886 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1888 case LyXRC::RC_ACCEPT_COMPOUND:
1889 case LyXRC::RC_ALT_LANG:
1890 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1891 case LyXRC::RC_PLAINTEXT_LINELEN:
1892 case LyXRC::RC_AUTOREGIONDELETE:
1893 case LyXRC::RC_AUTORESET_OPTIONS:
1894 case LyXRC::RC_AUTOSAVE:
1895 case LyXRC::RC_AUTO_NUMBER:
1896 case LyXRC::RC_BACKUPDIR_PATH:
1897 case LyXRC::RC_BIBTEX_COMMAND:
1898 case LyXRC::RC_BINDFILE:
1899 case LyXRC::RC_CHECKLASTFILES:
1900 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1901 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1902 case LyXRC::RC_COMPLETION_INLINE_MATH:
1903 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1904 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1905 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1906 case LyXRC::RC_COMPLETION_POPUP_MATH:
1907 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1908 case LyXRC::RC_USELASTFILEPOS:
1909 case LyXRC::RC_LOADSESSION:
1910 case LyXRC::RC_CHKTEX_COMMAND:
1911 case LyXRC::RC_CONVERTER:
1912 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1913 case LyXRC::RC_COPIER:
1914 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1915 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1916 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1917 case LyXRC::RC_DATE_INSERT_FORMAT:
1918 case LyXRC::RC_DEFAULT_LANGUAGE:
1919 case LyXRC::RC_DEFAULT_PAPERSIZE:
1920 case LyXRC::RC_DEFFILE:
1921 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1922 case LyXRC::RC_DISPLAY_GRAPHICS:
1923 case LyXRC::RC_DOCUMENTPATH:
1924 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1925 FileName path(lyxrc_new.document_path);
1926 if (path.exists() && path.isDirectory())
1927 package().document_dir() = FileName(lyxrc.document_path);
1929 case LyXRC::RC_ESC_CHARS:
1930 case LyXRC::RC_EXAMPLEPATH:
1931 case LyXRC::RC_FONT_ENCODING:
1932 case LyXRC::RC_FORMAT:
1933 case LyXRC::RC_INDEX_COMMAND:
1934 case LyXRC::RC_INPUT:
1935 case LyXRC::RC_KBMAP:
1936 case LyXRC::RC_KBMAP_PRIMARY:
1937 case LyXRC::RC_KBMAP_SECONDARY:
1938 case LyXRC::RC_LABEL_INIT_LENGTH:
1939 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1940 case LyXRC::RC_LANGUAGE_AUTO_END:
1941 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1942 case LyXRC::RC_LANGUAGE_COMMAND_END:
1943 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1944 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1945 case LyXRC::RC_LANGUAGE_PACKAGE:
1946 case LyXRC::RC_LANGUAGE_USE_BABEL:
1947 case LyXRC::RC_MACRO_EDIT_STYLE:
1948 case LyXRC::RC_MAKE_BACKUP:
1949 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1950 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1951 case LyXRC::RC_NUMLASTFILES:
1952 case LyXRC::RC_PATH_PREFIX:
1953 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1954 prependEnvPath("PATH", lyxrc.path_prefix);
1956 case LyXRC::RC_PERS_DICT:
1957 case LyXRC::RC_PREVIEW:
1958 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1959 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1960 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1961 case LyXRC::RC_PRINTCOPIESFLAG:
1962 case LyXRC::RC_PRINTER:
1963 case LyXRC::RC_PRINTEVENPAGEFLAG:
1964 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1965 case LyXRC::RC_PRINTFILEEXTENSION:
1966 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1967 case LyXRC::RC_PRINTODDPAGEFLAG:
1968 case LyXRC::RC_PRINTPAGERANGEFLAG:
1969 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1970 case LyXRC::RC_PRINTPAPERFLAG:
1971 case LyXRC::RC_PRINTREVERSEFLAG:
1972 case LyXRC::RC_PRINTSPOOL_COMMAND:
1973 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1974 case LyXRC::RC_PRINTTOFILE:
1975 case LyXRC::RC_PRINTTOPRINTER:
1976 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1977 case LyXRC::RC_PRINT_COMMAND:
1978 case LyXRC::RC_RTL_SUPPORT:
1979 case LyXRC::RC_SCREEN_DPI:
1980 case LyXRC::RC_SCREEN_FONT_ROMAN:
1981 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1982 case LyXRC::RC_SCREEN_FONT_SANS:
1983 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1984 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1985 case LyXRC::RC_SCREEN_FONT_SIZES:
1986 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1987 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1988 case LyXRC::RC_GEOMETRY_SESSION:
1989 case LyXRC::RC_SCREEN_ZOOM:
1990 case LyXRC::RC_SERVERPIPE:
1991 case LyXRC::RC_SET_COLOR:
1992 case LyXRC::RC_SHOW_BANNER:
1993 case LyXRC::RC_SPELL_COMMAND:
1994 case LyXRC::RC_TEMPDIRPATH:
1995 case LyXRC::RC_TEMPLATEPATH:
1996 case LyXRC::RC_TEX_ALLOWS_SPACES:
1997 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1998 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1999 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2001 case LyXRC::RC_UIFILE:
2002 case LyXRC::RC_USER_EMAIL:
2003 case LyXRC::RC_USER_NAME:
2004 case LyXRC::RC_USETEMPDIR:
2005 case LyXRC::RC_USE_ALT_LANG:
2006 case LyXRC::RC_USE_CONVERTER_CACHE:
2007 case LyXRC::RC_USE_ESC_CHARS:
2008 case LyXRC::RC_USE_INP_ENC:
2009 case LyXRC::RC_USE_PERS_DICT:
2010 case LyXRC::RC_USE_TOOLTIP:
2011 case LyXRC::RC_USE_PIXMAP_CACHE:
2012 case LyXRC::RC_USE_SPELL_LIB:
2013 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2014 case LyXRC::RC_SORT_LAYOUTS:
2015 case LyXRC::RC_FULL_SCREEN_LIMIT:
2016 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2017 case LyXRC::RC_FULL_SCREEN_TABBAR:
2018 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2019 case LyXRC::RC_FULL_SCREEN_WIDTH:
2020 case LyXRC::RC_VISUAL_CURSOR:
2021 case LyXRC::RC_VIEWER:
2022 case LyXRC::RC_LAST: