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 (!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);
375 if (func.action == LFUN_CHAR_DELETE_BACKWARD)
376 // backspace is not a self-insertion. But it
377 // still should not hide the completion popup.
378 // FIXME: more clever way to detect those movements
379 lyx_view_->updateCompletion(false, true);
381 lyx_view_->updateCompletion(false, false);
385 lyx_view_->restartCursor();
389 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
391 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
394 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
396 if (cmd.action == LFUN_NOACTION) {
397 flag.message(from_utf8(N_("Nothing to do")));
402 switch (cmd.action) {
403 case LFUN_UNKNOWN_ACTION:
404 #ifndef HAVE_LIBAIKSAURUS
405 case LFUN_THESAURUS_ENTRY:
415 if (flag.unknown()) {
416 flag.message(from_utf8(N_("Unknown action")));
420 if (!flag.enabled()) {
421 if (flag.message().empty())
422 flag.message(from_utf8(N_("Command disabled")));
426 // Check whether we need a buffer
427 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
429 flag.message(from_utf8(N_("Command not allowed with"
430 "out any document open")));
435 // I would really like to avoid having this switch and rather try to
436 // encode this in the function itself.
437 // -- And I'd rather let an inset decide which LFUNs it is willing
438 // to handle (Andre')
440 switch (cmd.action) {
442 // FIXME: these cases should be hidden in GuiApplication::getStatus().
443 case LFUN_WINDOW_CLOSE:
445 return theApp()->getStatus(cmd);
449 // FIXME: these cases should be hidden in GuiView::getStatus().
450 case LFUN_DIALOG_TOGGLE:
451 case LFUN_DIALOG_SHOW:
452 case LFUN_DIALOG_UPDATE:
453 if (cmd.argument() == "prefs"
454 || cmd.argument() == "aboutlyx")
457 return lyx_view_->getStatus(cmd);
462 case LFUN_TOOLBAR_TOGGLE:
463 case LFUN_INSET_APPLY:
464 case LFUN_BUFFER_WRITE:
465 case LFUN_BUFFER_WRITE_AS:
466 case LFUN_SPLIT_VIEW:
467 case LFUN_CLOSE_TAB_GROUP:
468 case LFUN_COMPLETION_POPUP:
469 case LFUN_COMPLETION_INLINE:
470 case LFUN_COMPLETION_COMPLETE:
472 return lyx_view_->getStatus(cmd);
476 case LFUN_BUFFER_TOGGLE_READ_ONLY:
477 flag.setOnOff(buf->isReadonly());
480 case LFUN_BUFFER_SWITCH:
481 // toggle on the current buffer, but do not toggle off
482 // the other ones (is that a good idea?)
483 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
487 case LFUN_BUFFER_EXPORT:
488 enable = cmd.argument() == "custom"
489 || buf->isExportable(to_utf8(cmd.argument()));
492 case LFUN_BUFFER_CHKTEX:
493 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
496 case LFUN_BUILD_PROGRAM:
497 enable = buf->isExportable("program");
500 case LFUN_VC_REGISTER:
501 enable = !buf->lyxvc().inUse();
503 case LFUN_VC_CHECK_IN:
504 enable = buf->lyxvc().inUse() && !buf->isReadonly();
506 case LFUN_VC_CHECK_OUT:
507 enable = buf->lyxvc().inUse() && buf->isReadonly();
510 case LFUN_VC_UNDO_LAST:
511 enable = buf->lyxvc().inUse();
513 case LFUN_BUFFER_RELOAD:
514 enable = !buf->isUnnamed() && buf->fileName().exists()
515 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
518 case LFUN_CITATION_INSERT: {
519 FuncRequest fr(LFUN_INSET_INSERT, "citation");
520 enable = getStatus(fr).enabled();
524 // This could be used for the no-GUI version. The GUI version is handled in
525 // LyXView::getStatus(). See above.
527 case LFUN_BUFFER_WRITE:
528 case LFUN_BUFFER_WRITE_AS: {
529 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
530 enable = b && (b->isUnnamed() || !b->isClean());
535 case LFUN_BUFFER_WRITE_ALL: {
536 // We enable the command only if there are some modified buffers
537 Buffer * first = theBufferList().first();
542 // We cannot use a for loop as the buffer list is a cycle.
548 b = theBufferList().next(b);
549 } while (b != first);
553 case LFUN_BOOKMARK_GOTO: {
554 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
555 enable = LyX::ref().session().bookmarks().isValid(num);
559 case LFUN_BOOKMARK_CLEAR:
560 enable = LyX::ref().session().bookmarks().size() > 0;
563 // this one is difficult to get right. As a half-baked
564 // solution, we consider only the first action of the sequence
565 case LFUN_COMMAND_SEQUENCE: {
566 // argument contains ';'-terminated commands
567 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
568 FuncRequest func(lyxaction.lookupFunc(firstcmd));
569 func.origin = cmd.origin;
570 flag = getStatus(func);
576 string name = to_utf8(cmd.argument());
577 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
578 func.origin = cmd.origin;
579 flag = getStatus(func);
580 LyX::ref().topLevelCmdDef().release(name);
582 // catch recursion or unknown command definiton
583 // all operations until the recursion or unknown command
584 // definiton occures are performed, so set the state to enabled
590 case LFUN_BUFFER_NEW:
591 case LFUN_BUFFER_NEW_TEMPLATE:
592 case LFUN_WORD_FIND_FORWARD:
593 case LFUN_WORD_FIND_BACKWARD:
594 case LFUN_COMMAND_PREFIX:
595 case LFUN_COMMAND_EXECUTE:
597 case LFUN_META_PREFIX:
598 case LFUN_BUFFER_CLOSE:
599 case LFUN_BUFFER_UPDATE:
600 case LFUN_BUFFER_VIEW:
601 case LFUN_MASTER_BUFFER_UPDATE:
602 case LFUN_MASTER_BUFFER_VIEW:
603 case LFUN_BUFFER_IMPORT:
604 case LFUN_BUFFER_AUTO_SAVE:
605 case LFUN_RECONFIGURE:
608 case LFUN_DROP_LAYOUTS_CHOICE:
610 case LFUN_SERVER_GET_NAME:
611 case LFUN_SERVER_NOTIFY:
612 case LFUN_SERVER_GOTO_FILE_ROW:
613 case LFUN_DIALOG_HIDE:
614 case LFUN_DIALOG_DISCONNECT_INSET:
615 case LFUN_BUFFER_CHILD_OPEN:
617 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
618 case LFUN_KEYMAP_OFF:
619 case LFUN_KEYMAP_PRIMARY:
620 case LFUN_KEYMAP_SECONDARY:
621 case LFUN_KEYMAP_TOGGLE:
623 case LFUN_BUFFER_EXPORT_CUSTOM:
624 case LFUN_BUFFER_PRINT:
625 case LFUN_PREFERENCES_SAVE:
626 case LFUN_SCREEN_FONT_UPDATE:
629 case LFUN_EXTERNAL_EDIT:
630 case LFUN_GRAPHICS_EDIT:
631 case LFUN_ALL_INSETS_TOGGLE:
632 case LFUN_BUFFER_LANGUAGE:
633 case LFUN_TEXTCLASS_APPLY:
634 case LFUN_TEXTCLASS_LOAD:
635 case LFUN_BUFFER_SAVE_AS_DEFAULT:
636 case LFUN_BUFFER_PARAMS_APPLY:
637 case LFUN_LAYOUT_MODULES_CLEAR:
638 case LFUN_LAYOUT_MODULE_ADD:
639 case LFUN_LAYOUT_RELOAD:
640 case LFUN_LYXRC_APPLY:
641 case LFUN_BUFFER_NEXT:
642 case LFUN_BUFFER_PREVIOUS:
643 case LFUN_WINDOW_NEW:
645 // these are handled in our dispatch()
653 if (!getLocalStatus(view()->cursor(), cmd, flag))
654 flag = view()->getStatus(cmd);
660 // Can we use a readonly buffer?
661 if (buf && buf->isReadonly()
662 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
663 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
664 flag.message(from_utf8(N_("Document is read-only")));
668 // Are we in a DELETED change-tracking region?
670 && lookupChangeType(view()->cursor(), true) == Change::DELETED
671 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
672 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
673 flag.message(from_utf8(N_("This portion of the document is deleted.")));
677 // the default error message if we disable the command
678 if (!flag.enabled() && flag.message().empty())
679 flag.message(from_utf8(N_("Command disabled")));
685 bool LyXFunc::ensureBufferClean(BufferView * bv)
687 Buffer & buf = bv->buffer();
691 docstring const file = buf.fileName().displayName(30);
692 docstring text = bformat(_("The document %1$s has unsaved "
693 "changes.\n\nDo you want to save "
694 "the document?"), file);
695 int const ret = Alert::prompt(_("Save changed document?"),
696 text, 0, 1, _("&Save"),
700 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
702 return buf.isClean();
708 void showPrintError(string const & name)
710 docstring str = bformat(_("Could not print the document %1$s.\n"
711 "Check that your printer is set up correctly."),
712 makeDisplayPath(name, 50));
713 Alert::error(_("Print document failed"), str);
717 void loadTextClass(string const & name, string const & buf_path)
719 pair<bool, BaseClassIndex> const tc_pair =
720 baseclasslist.numberOfClass(name);
722 if (!tc_pair.first) {
723 lyxerr << "Document class \"" << name
724 << "\" does not exist."
729 BaseClassIndex const tc = tc_pair.second;
731 if (!baseclasslist[tc].load(buf_path)) {
732 docstring s = bformat(_("The document class %1$s."
733 "could not be loaded."),
734 from_utf8(baseclasslist[tc].name()));
735 Alert::error(_("Could not load class"), s);
740 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
745 void LyXFunc::dispatch(FuncRequest const & cmd)
747 string const argument = to_utf8(cmd.argument());
748 kb_action const action = cmd.action;
750 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
751 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
753 // we have not done anything wrong yet.
755 dispatch_buffer.erase();
757 // redraw the screen at the end (first of the two drawing steps).
758 //This is done unless explicitely requested otherwise
759 Update::flags updateFlags = Update::FitCursor;
761 FuncStatus const flag = getStatus(cmd);
762 if (!flag.enabled()) {
763 // We cannot use this function here
764 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
765 << lyxaction.getActionName(action)
766 << " [" << action << "] is disabled at this location");
767 setErrorMessage(flag.message());
771 case LFUN_WORD_FIND_FORWARD:
772 case LFUN_WORD_FIND_BACKWARD: {
773 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
774 static docstring last_search;
775 docstring searched_string;
777 if (!cmd.argument().empty()) {
778 last_search = cmd.argument();
779 searched_string = cmd.argument();
781 searched_string = last_search;
784 if (searched_string.empty())
787 bool const fw = action == LFUN_WORD_FIND_FORWARD;
788 docstring const data =
789 find2string(searched_string, true, false, fw);
790 find(view(), FuncRequest(LFUN_WORD_FIND, data));
794 case LFUN_COMMAND_PREFIX:
795 BOOST_ASSERT(lyx_view_);
796 lyx_view_->message(keyseq.printOptions(true));
800 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
802 meta_fake_bit = NoModifier;
803 if (lyx_view_->buffer())
804 // cancel any selection
805 dispatch(FuncRequest(LFUN_MARK_OFF));
806 setMessage(from_ascii(N_("Cancel")));
809 case LFUN_META_PREFIX:
810 meta_fake_bit = AltModifier;
811 setMessage(keyseq.print(KeySequence::ForGui));
814 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
815 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
816 Buffer * buf = lyx_view_->buffer();
817 if (buf->lyxvc().inUse())
818 buf->lyxvc().toggleReadOnly();
820 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
824 // --- Menus -----------------------------------------------
825 case LFUN_BUFFER_NEW:
826 lyx_view_->newDocument(argument, false);
827 updateFlags = Update::None;
830 case LFUN_BUFFER_NEW_TEMPLATE:
831 lyx_view_->newDocument(argument, true);
832 updateFlags = Update::None;
835 case LFUN_BUFFER_CLOSE:
837 updateFlags = Update::None;
840 case LFUN_BUFFER_RELOAD: {
841 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
842 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
843 docstring text = bformat(_("Any changes will be lost. Are you sure "
844 "you want to revert to the saved version of the document %1$s?"), file);
845 int const ret = Alert::prompt(_("Revert to saved document?"),
846 text, 1, 1, _("&Revert"), _("&Cancel"));
853 case LFUN_BUFFER_UPDATE:
854 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
855 lyx_view_->buffer()->doExport(argument, true);
858 case LFUN_BUFFER_VIEW:
859 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
860 lyx_view_->buffer()->preview(argument);
863 case LFUN_MASTER_BUFFER_UPDATE:
864 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
865 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
868 case LFUN_MASTER_BUFFER_VIEW:
869 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
870 lyx_view_->buffer()->masterBuffer()->preview(argument);
873 case LFUN_BUILD_PROGRAM:
874 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
875 lyx_view_->buffer()->doExport("program", true);
878 case LFUN_BUFFER_CHKTEX:
879 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
880 lyx_view_->buffer()->runChktex();
883 case LFUN_BUFFER_EXPORT:
884 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
885 if (argument == "custom")
886 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
888 lyx_view_->buffer()->doExport(argument, false);
891 case LFUN_BUFFER_EXPORT_CUSTOM: {
892 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
894 string command = split(argument, format_name, ' ');
895 Format const * format = formats.getFormat(format_name);
897 lyxerr << "Format \"" << format_name
898 << "\" not recognized!"
903 Buffer * buffer = lyx_view_->buffer();
905 // The name of the file created by the conversion process
908 // Output to filename
909 if (format->name() == "lyx") {
910 string const latexname = buffer->latexName(false);
911 filename = changeExtension(latexname,
912 format->extension());
913 filename = addName(buffer->temppath(), filename);
915 if (!buffer->writeFile(FileName(filename)))
919 buffer->doExport(format_name, true, filename);
922 // Substitute $$FName for filename
923 if (!contains(command, "$$FName"))
924 command = "( " + command + " ) < $$FName";
925 command = subst(command, "$$FName", filename);
927 // Execute the command in the background
929 call.startscript(Systemcall::DontWait, command);
933 case LFUN_BUFFER_PRINT: {
934 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
935 // FIXME: cmd.getArg() might fail if one of the arguments
936 // contains double quotes
937 string target = cmd.getArg(0);
938 string target_name = cmd.getArg(1);
939 string command = cmd.getArg(2);
942 || target_name.empty()
943 || command.empty()) {
944 lyxerr << "Unable to parse \""
945 << argument << '"' << endl;
948 if (target != "printer" && target != "file") {
949 lyxerr << "Unrecognized target \""
950 << target << '"' << endl;
954 Buffer * buffer = lyx_view_->buffer();
956 if (!buffer->doExport("dvi", true)) {
957 showPrintError(buffer->absFileName());
961 // Push directory path.
962 string const path = buffer->temppath();
963 // Prevent the compiler from optimizing away p
967 // there are three cases here:
968 // 1. we print to a file
969 // 2. we print directly to a printer
970 // 3. we print using a spool command (print to file first)
973 string const dviname =
974 changeExtension(buffer->latexName(true), "dvi");
976 if (target == "printer") {
977 if (!lyxrc.print_spool_command.empty()) {
978 // case 3: print using a spool
979 string const psname =
980 changeExtension(dviname,".ps");
981 command += ' ' + lyxrc.print_to_file
984 + quoteName(dviname);
987 lyxrc.print_spool_command + ' ';
988 if (target_name != "default") {
989 command2 += lyxrc.print_spool_printerprefix
993 command2 += quoteName(psname);
995 // If successful, then spool command
996 res = one.startscript(
1001 res = one.startscript(
1002 Systemcall::DontWait,
1005 // case 2: print directly to a printer
1006 if (target_name != "default")
1007 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
1008 res = one.startscript(
1009 Systemcall::DontWait,
1010 command + quoteName(dviname));
1014 // case 1: print to a file
1015 FileName const filename(makeAbsPath(target_name,
1016 lyx_view_->buffer()->filePath()));
1017 FileName const dvifile(makeAbsPath(dviname, path));
1018 if (filename.exists()) {
1019 docstring text = bformat(
1020 _("The file %1$s already exists.\n\n"
1021 "Do you want to overwrite that file?"),
1022 makeDisplayPath(filename.absFilename()));
1023 if (Alert::prompt(_("Overwrite file?"),
1024 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1027 command += ' ' + lyxrc.print_to_file
1028 + quoteName(filename.toFilesystemEncoding())
1030 + quoteName(dvifile.toFilesystemEncoding());
1031 res = one.startscript(Systemcall::DontWait,
1036 showPrintError(buffer->absFileName());
1040 // FIXME: There is need for a command-line import.
1042 case LFUN_BUFFER_IMPORT:
1047 case LFUN_BUFFER_AUTO_SAVE:
1048 lyx_view_->buffer()->autoSave();
1051 case LFUN_RECONFIGURE:
1052 BOOST_ASSERT(lyx_view_);
1053 // argument is any additional parameter to the configure.py command
1054 reconfigure(*lyx_view_, argument);
1057 case LFUN_HELP_OPEN: {
1058 BOOST_ASSERT(lyx_view_);
1059 string const arg = argument;
1061 setErrorMessage(from_ascii(N_("Missing argument")));
1064 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1065 if (fname.empty()) {
1066 lyxerr << "LyX: unable to find documentation file `"
1067 << arg << "'. Bad installation?" << endl;
1070 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1071 makeDisplayPath(fname.absFilename())));
1072 Buffer * buf = lyx_view_->loadDocument(fname, false);
1075 lyx_view_->setBuffer(buf);
1076 buf->errors("Parse");
1078 updateFlags = Update::None;
1082 // --- version control -------------------------------
1083 case LFUN_VC_REGISTER:
1084 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1085 if (!ensureBufferClean(view()))
1087 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1088 lyx_view_->buffer()->lyxvc().registrer();
1091 updateFlags = Update::Force;
1094 case LFUN_VC_CHECK_IN:
1095 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1096 if (!ensureBufferClean(view()))
1098 if (lyx_view_->buffer()->lyxvc().inUse()
1099 && !lyx_view_->buffer()->isReadonly()) {
1100 lyx_view_->buffer()->lyxvc().checkIn();
1105 case LFUN_VC_CHECK_OUT:
1106 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1107 if (!ensureBufferClean(view()))
1109 if (lyx_view_->buffer()->lyxvc().inUse()
1110 && lyx_view_->buffer()->isReadonly()) {
1111 lyx_view_->buffer()->lyxvc().checkOut();
1116 case LFUN_VC_REVERT:
1117 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1118 lyx_view_->buffer()->lyxvc().revert();
1122 case LFUN_VC_UNDO_LAST:
1123 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1124 lyx_view_->buffer()->lyxvc().undoLast();
1128 // --- lyxserver commands ----------------------------
1129 case LFUN_SERVER_GET_NAME:
1130 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1131 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1132 LYXERR(Debug::INFO, "FNAME["
1133 << lyx_view_->buffer()->absFileName() << ']');
1136 case LFUN_SERVER_NOTIFY:
1137 dispatch_buffer = keyseq.print(KeySequence::Portable);
1138 theServer().notifyClient(to_utf8(dispatch_buffer));
1141 case LFUN_SERVER_GOTO_FILE_ROW: {
1142 BOOST_ASSERT(lyx_view_);
1145 istringstream is(argument);
1146 is >> file_name >> row;
1148 bool loaded = false;
1149 if (prefixIs(file_name, package().temp_dir().absFilename()))
1150 // Needed by inverse dvi search. If it is a file
1151 // in tmpdir, call the apropriated function
1152 buf = theBufferList().getBufferFromTmp(file_name);
1154 // Must replace extension of the file to be .lyx
1155 // and get full path
1156 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1157 // Either change buffer or load the file
1158 if (theBufferList().exists(s.absFilename()))
1159 buf = theBufferList().getBuffer(s.absFilename());
1161 buf = lyx_view_->loadDocument(s);
1167 updateFlags = Update::None;
1172 lyx_view_->setBuffer(buf);
1173 view()->setCursorFromRow(row);
1175 buf->errors("Parse");
1176 updateFlags = Update::FitCursor;
1181 case LFUN_DIALOG_SHOW_NEW_INSET: {
1182 BOOST_ASSERT(lyx_view_);
1183 string const name = cmd.getArg(0);
1184 InsetCode code = insetCode(name);
1185 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1186 bool insetCodeOK = true;
1195 case HYPERLINK_CODE: {
1196 InsetCommandParams p(code);
1197 data = InsetCommandMailer::params2string(name, p);
1200 case INCLUDE_CODE: {
1201 // data is the include type: one of "include",
1202 // "input", "verbatiminput" or "verbatiminput*"
1204 // default type is requested
1206 InsetCommandParams p(INCLUDE_CODE, data);
1207 data = InsetCommandMailer::params2string("include", p);
1211 // \c data == "Boxed" || "Frameless" etc
1212 InsetBoxParams p(data);
1213 data = InsetBoxMailer::params2string(p);
1217 InsetBranchParams p;
1218 data = InsetBranchMailer::params2string(p);
1222 InsetCommandParams p(CITE_CODE);
1223 data = InsetCommandMailer::params2string(name, p);
1227 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1230 case EXTERNAL_CODE: {
1231 InsetExternalParams p;
1232 Buffer const & buffer = *lyx_view_->buffer();
1233 data = InsetExternalMailer::params2string(p, buffer);
1238 data = InsetFloatMailer::params2string(p);
1241 case LISTINGS_CODE: {
1242 InsetListingsParams p;
1243 data = InsetListingsMailer::params2string(p);
1246 case GRAPHICS_CODE: {
1247 InsetGraphicsParams p;
1248 Buffer const & buffer = *lyx_view_->buffer();
1249 data = InsetGraphicsMailer::params2string(p, buffer);
1254 data = InsetNoteMailer::params2string(p);
1259 data = InsetVSpaceMailer::params2string(space);
1264 data = InsetWrapMailer::params2string(p);
1268 lyxerr << "Inset type '" << name <<
1269 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1270 insetCodeOK = false;
1272 } // end switch(code)
1274 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1278 case LFUN_CITATION_INSERT: {
1279 BOOST_ASSERT(lyx_view_);
1280 if (!argument.empty()) {
1281 // we can have one optional argument, delimited by '|'
1282 // citation-insert <key>|<text_before>
1283 // this should be enhanced to also support text_after
1284 // and citation style
1285 string arg = argument;
1287 if (contains(argument, "|")) {
1288 arg = token(argument, '|', 0);
1289 opt1 = token(argument, '|', 1);
1291 InsetCommandParams icp(CITE_CODE);
1292 icp["key"] = from_utf8(arg);
1294 icp["before"] = from_utf8(opt1);
1295 string icstr = InsetCommandMailer::params2string("citation", icp);
1296 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1299 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1303 case LFUN_BUFFER_CHILD_OPEN: {
1304 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1305 Buffer * parent = lyx_view_->buffer();
1306 FileName filename = makeAbsPath(argument, parent->filePath());
1307 view()->saveBookmark(false);
1309 bool parsed = false;
1310 if (theBufferList().exists(filename.absFilename())) {
1311 child = theBufferList().getBuffer(filename.absFilename());
1313 setMessage(bformat(_("Opening child document %1$s..."),
1314 makeDisplayPath(filename.absFilename())));
1315 child = lyx_view_->loadDocument(filename, false);
1319 // Set the parent name of the child document.
1320 // This makes insertion of citations and references in the child work,
1321 // when the target is in the parent or another child document.
1322 child->setParent(parent);
1323 updateLabels(*child->masterBuffer());
1324 lyx_view_->setBuffer(child);
1326 child->errors("Parse");
1329 // If a screen update is required (in case where auto_open is false),
1330 // setBuffer() would have taken care of it already. Otherwise we shall
1331 // reset the update flag because it can cause a circular problem.
1333 updateFlags = Update::None;
1337 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1338 BOOST_ASSERT(lyx_view_);
1339 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1342 case LFUN_KEYMAP_OFF:
1343 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1344 lyx_view_->view()->getIntl().keyMapOn(false);
1347 case LFUN_KEYMAP_PRIMARY:
1348 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1349 lyx_view_->view()->getIntl().keyMapPrim();
1352 case LFUN_KEYMAP_SECONDARY:
1353 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1354 lyx_view_->view()->getIntl().keyMapSec();
1357 case LFUN_KEYMAP_TOGGLE:
1358 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1359 lyx_view_->view()->getIntl().toggleKeyMap();
1365 string rest = split(argument, countstr, ' ');
1366 istringstream is(countstr);
1369 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1370 for (int i = 0; i < count; ++i)
1371 dispatch(lyxaction.lookupFunc(rest));
1375 case LFUN_COMMAND_SEQUENCE: {
1376 // argument contains ';'-terminated commands
1377 string arg = argument;
1378 while (!arg.empty()) {
1380 arg = split(arg, first, ';');
1381 FuncRequest func(lyxaction.lookupFunc(first));
1382 func.origin = cmd.origin;
1390 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1391 func.origin = cmd.origin;
1393 LyX::ref().topLevelCmdDef().release(argument);
1395 if (func.action == LFUN_UNKNOWN_ACTION) {
1396 // unknown command definition
1397 lyxerr << "Warning: unknown command definition `"
1401 // recursion detected
1402 lyxerr << "Warning: Recursion in the command definition `"
1403 << argument << "' detected"
1410 case LFUN_PREFERENCES_SAVE: {
1411 lyxrc.write(makeAbsPath("preferences",
1412 package().user_support().absFilename()),
1417 case LFUN_SET_COLOR: {
1419 string const x11_name = split(argument, lyx_name, ' ');
1420 if (lyx_name.empty() || x11_name.empty()) {
1421 setErrorMessage(from_ascii(N_(
1422 "Syntax: set-color <lyx_name>"
1427 bool const graphicsbg_changed =
1428 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1429 x11_name != lcolor.getX11Name(Color_graphicsbg));
1431 if (!lcolor.setColor(lyx_name, x11_name)) {
1433 bformat(_("Set-color \"%1$s\" failed "
1434 "- color is undefined or "
1435 "may not be redefined"),
1436 from_utf8(lyx_name)));
1440 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1442 if (graphicsbg_changed) {
1443 // FIXME: The graphics cache no longer has a changeDisplay method.
1445 graphics::GCache::get().changeDisplay(true);
1452 BOOST_ASSERT(lyx_view_);
1453 lyx_view_->message(from_utf8(argument));
1456 case LFUN_EXTERNAL_EDIT: {
1457 BOOST_ASSERT(lyx_view_);
1458 FuncRequest fr(action, argument);
1459 InsetExternal().dispatch(view()->cursor(), fr);
1463 case LFUN_GRAPHICS_EDIT: {
1464 FuncRequest fr(action, argument);
1465 InsetGraphics().dispatch(view()->cursor(), fr);
1469 case LFUN_ALL_INSETS_TOGGLE: {
1470 BOOST_ASSERT(lyx_view_);
1472 string const name = split(argument, action, ' ');
1473 InsetCode const inset_code = insetCode(name);
1475 Cursor & cur = view()->cursor();
1476 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1478 Inset & inset = lyx_view_->buffer()->inset();
1479 InsetIterator it = inset_iterator_begin(inset);
1480 InsetIterator const end = inset_iterator_end(inset);
1481 for (; it != end; ++it) {
1482 if (!it->asInsetMath()
1483 && (inset_code == NO_CODE
1484 || inset_code == it->lyxCode())) {
1485 Cursor tmpcur = cur;
1486 tmpcur.pushBackward(*it);
1487 it->dispatch(tmpcur, fr);
1490 updateFlags = Update::Force | Update::FitCursor;
1494 case LFUN_BUFFER_LANGUAGE: {
1495 BOOST_ASSERT(lyx_view_);
1496 Buffer & buffer = *lyx_view_->buffer();
1497 Language const * oldL = buffer.params().language;
1498 Language const * newL = languages.getLanguage(argument);
1499 if (!newL || oldL == newL)
1502 if (oldL->rightToLeft() == newL->rightToLeft()
1503 && !buffer.isMultiLingual())
1504 buffer.changeLanguage(oldL, newL);
1508 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1509 string const fname =
1510 addName(addPath(package().user_support().absFilename(), "templates/"),
1512 Buffer defaults(fname);
1514 istringstream ss(argument);
1517 int const unknown_tokens = defaults.readHeader(lex);
1519 if (unknown_tokens != 0) {
1520 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1521 << unknown_tokens << " unknown token"
1522 << (unknown_tokens == 1 ? "" : "s")
1526 if (defaults.writeFile(FileName(defaults.absFileName())))
1527 setMessage(bformat(_("Document defaults saved in %1$s"),
1528 makeDisplayPath(fname)));
1530 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1534 case LFUN_BUFFER_PARAMS_APPLY: {
1535 BOOST_ASSERT(lyx_view_);
1536 biblio::CiteEngine const oldEngine =
1537 lyx_view_->buffer()->params().getEngine();
1539 Buffer * buffer = lyx_view_->buffer();
1541 TextClassPtr oldClass = buffer->params().textClassPtr();
1543 Cursor & cur = view()->cursor();
1544 cur.recordUndoFullDocument();
1546 istringstream ss(argument);
1549 int const unknown_tokens = buffer->readHeader(lex);
1551 if (unknown_tokens != 0) {
1552 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1553 << unknown_tokens << " unknown token"
1554 << (unknown_tokens == 1 ? "" : "s")
1558 updateLayout(oldClass, buffer);
1560 biblio::CiteEngine const newEngine =
1561 lyx_view_->buffer()->params().getEngine();
1563 if (oldEngine != newEngine) {
1564 FuncRequest fr(LFUN_INSET_REFRESH);
1566 Inset & inset = lyx_view_->buffer()->inset();
1567 InsetIterator it = inset_iterator_begin(inset);
1568 InsetIterator const end = inset_iterator_end(inset);
1569 for (; it != end; ++it)
1570 if (it->lyxCode() == CITE_CODE)
1571 it->dispatch(cur, fr);
1574 updateFlags = Update::Force | Update::FitCursor;
1575 // We are here most certainaly because of a change in the document
1576 // It is then better to make sure that all dialogs are in sync
1577 // with current document settings. LyXView::restartCursor() achieve this.
1578 lyx_view_->restartCursor();
1582 case LFUN_LAYOUT_MODULES_CLEAR: {
1583 BOOST_ASSERT(lyx_view_);
1584 Buffer * buffer = lyx_view_->buffer();
1585 TextClassPtr oldClass = buffer->params().textClassPtr();
1586 view()->cursor().recordUndoFullDocument();
1587 buffer->params().clearLayoutModules();
1588 buffer->params().makeTextClass();
1589 updateLayout(oldClass, buffer);
1590 updateFlags = Update::Force | Update::FitCursor;
1594 case LFUN_LAYOUT_MODULE_ADD: {
1595 BOOST_ASSERT(lyx_view_);
1596 Buffer * buffer = lyx_view_->buffer();
1597 TextClassPtr oldClass = buffer->params().textClassPtr();
1598 view()->cursor().recordUndoFullDocument();
1599 buffer->params().addLayoutModule(argument);
1600 buffer->params().makeTextClass();
1601 updateLayout(oldClass, buffer);
1602 updateFlags = Update::Force | Update::FitCursor;
1606 case LFUN_TEXTCLASS_APPLY: {
1607 BOOST_ASSERT(lyx_view_);
1608 Buffer * buffer = lyx_view_->buffer();
1610 loadTextClass(argument, buffer->filePath());
1612 pair<bool, BaseClassIndex> const tc_pair =
1613 baseclasslist.numberOfClass(argument);
1618 BaseClassIndex const old_class = buffer->params().baseClass();
1619 BaseClassIndex const new_class = tc_pair.second;
1621 if (old_class == new_class)
1625 //Save the old, possibly modular, layout for use in conversion.
1626 TextClassPtr oldClass = buffer->params().textClassPtr();
1627 view()->cursor().recordUndoFullDocument();
1628 buffer->params().setBaseClass(new_class);
1629 buffer->params().makeTextClass();
1630 updateLayout(oldClass, buffer);
1631 updateFlags = Update::Force | Update::FitCursor;
1635 case LFUN_LAYOUT_RELOAD: {
1636 BOOST_ASSERT(lyx_view_);
1637 Buffer * buffer = lyx_view_->buffer();
1638 TextClassPtr oldClass = buffer->params().textClassPtr();
1639 BaseClassIndex const tc = buffer->params().baseClass();
1640 baseclasslist.reset(tc);
1641 buffer->params().setBaseClass(tc);
1642 buffer->params().makeTextClass();
1643 updateLayout(oldClass, buffer);
1644 updateFlags = Update::Force | Update::FitCursor;
1648 case LFUN_TEXTCLASS_LOAD:
1649 loadTextClass(argument, lyx_view_->buffer()->filePath());
1652 case LFUN_LYXRC_APPLY: {
1653 LyXRC const lyxrc_orig = lyxrc;
1655 istringstream ss(argument);
1656 bool const success = lyxrc.read(ss) == 0;
1659 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1660 << "Unable to read lyxrc data"
1665 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1667 theApp()->resetGui();
1669 /// We force the redraw in any case because there might be
1670 /// some screen font changes.
1671 /// FIXME: only the current view will be updated. the Gui
1672 /// class is able to furnish the list of views.
1673 updateFlags = Update::Force;
1677 case LFUN_BOOKMARK_GOTO:
1678 // go to bookmark, open unopened file and switch to buffer if necessary
1679 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1680 updateFlags = Update::FitCursor;
1683 case LFUN_BOOKMARK_CLEAR:
1684 LyX::ref().session().bookmarks().clear();
1688 BOOST_ASSERT(theApp());
1689 // Let the frontend dispatch its own actions.
1690 if (theApp()->dispatch(cmd))
1691 // Nothing more to do.
1694 // Let the current LyXView dispatch its own actions.
1695 BOOST_ASSERT(lyx_view_);
1696 if (lyx_view_->dispatch(cmd)) {
1697 if (lyx_view_->view())
1698 updateFlags = lyx_view_->view()->cursor().result().update();
1702 BOOST_ASSERT(lyx_view_->view());
1703 // Let the current BufferView dispatch its own actions.
1704 if (view()->dispatch(cmd)) {
1705 // The BufferView took care of its own updates if needed.
1706 updateFlags = Update::None;
1710 // Let the current Cursor dispatch its own actions.
1711 Cursor old = view()->cursor();
1712 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1713 cursorPosBeforeDispatchY_);
1714 view()->cursor().dispatch(cmd);
1716 // notify insets we just left
1717 if (view()->cursor() != old) {
1719 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1721 view()->cursor().fixIfBroken();
1724 updateFlags = view()->cursor().result().update();
1727 if (lyx_view_ && lyx_view_->buffer()) {
1728 // BufferView::update() updates the ViewMetricsInfo and
1729 // also initializes the position cache for all insets in
1730 // (at least partially) visible top-level paragraphs.
1731 // We will redraw the screen only if needed.
1732 view()->processUpdateFlags(updateFlags);
1734 // if we executed a mutating lfun, mark the buffer as dirty
1736 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1737 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1738 lyx_view_->buffer()->markDirty();
1740 //Do we have a selection?
1741 theSelection().haveSelection(view()->cursor().selection());
1744 if (!quitting && lyx_view_) {
1745 // Some messages may already be translated, so we cannot use _()
1746 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1751 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1753 const bool verbose = (cmd.origin == FuncRequest::MENU
1754 || cmd.origin == FuncRequest::TOOLBAR
1755 || cmd.origin == FuncRequest::COMMANDBUFFER);
1757 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1758 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1760 lyx_view_->message(msg);
1764 docstring dispatch_msg = msg;
1765 if (!dispatch_msg.empty())
1766 dispatch_msg += ' ';
1768 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1770 bool argsadded = false;
1772 if (!cmd.argument().empty()) {
1773 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1774 comname += ' ' + cmd.argument();
1779 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1781 if (!shortcuts.empty())
1782 comname += ": " + shortcuts;
1783 else if (!argsadded && !cmd.argument().empty())
1784 comname += ' ' + cmd.argument();
1786 if (!comname.empty()) {
1787 comname = rtrim(comname);
1788 dispatch_msg += '(' + rtrim(comname) + ')';
1791 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1792 if (!dispatch_msg.empty())
1793 lyx_view_->message(dispatch_msg);
1797 void LyXFunc::closeBuffer()
1799 // goto bookmark to update bookmark pit.
1800 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1801 gotoBookmark(i+1, false, false);
1803 lyx_view_->closeBuffer();
1807 void LyXFunc::reloadBuffer()
1809 FileName filename = lyx_view_->buffer()->fileName();
1810 // The user has already confirmed that the changes, if any, should
1811 // be discarded. So we just release the Buffer and don't call closeBuffer();
1812 theBufferList().release(lyx_view_->buffer());
1813 Buffer * buf = lyx_view_->loadDocument(filename);
1814 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1818 lyx_view_->setBuffer(buf);
1819 buf->errors("Parse");
1820 str = bformat(_("Document %1$s reloaded."), disp_fn);
1822 str = bformat(_("Could not reload document %1$s"), disp_fn);
1824 lyx_view_->message(str);
1827 // Each "lyx_view_" should have it's own message method. lyxview and
1828 // the minibuffer would use the minibuffer, but lyxserver would
1829 // send an ERROR signal to its client. Alejandro 970603
1830 // This function is bit problematic when it comes to NLS, to make the
1831 // lyx servers client be language indepenent we must not translate
1832 // strings sent to this func.
1833 void LyXFunc::setErrorMessage(docstring const & m) const
1835 dispatch_buffer = m;
1840 void LyXFunc::setMessage(docstring const & m) const
1842 dispatch_buffer = m;
1846 docstring const LyXFunc::viewStatusMessage()
1848 // When meta-fake key is pressed, show the key sequence so far + "M-".
1850 return keyseq.print(KeySequence::ForGui) + "M-";
1852 // Else, when a non-complete key sequence is pressed,
1853 // show the available options.
1854 if (keyseq.length() > 0 && !keyseq.deleted())
1855 return keyseq.printOptions(true);
1857 BOOST_ASSERT(lyx_view_);
1858 if (!lyx_view_->buffer())
1859 return _("Welcome to LyX!");
1861 return view()->cursor().currentState();
1865 BufferView * LyXFunc::view() const
1867 BOOST_ASSERT(lyx_view_);
1868 return lyx_view_->view();
1872 bool LyXFunc::wasMetaKey() const
1874 return (meta_fake_bit != NoModifier);
1878 void LyXFunc::updateLayout(TextClassPtr oldlayout,Buffer * buffer)
1880 lyx_view_->message(_("Converting document to new document class..."));
1882 StableDocIterator backcur(view()->cursor());
1883 ErrorList & el = buffer->errorList("Class Switch");
1884 cap::switchBetweenClasses(
1885 oldlayout, buffer->params().textClassPtr(),
1886 static_cast<InsetText &>(buffer->inset()), el);
1888 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1890 buffer->errors("Class Switch");
1891 updateLabels(*buffer);
1897 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1899 // Why the switch you might ask. It is a trick to ensure that all
1900 // the elements in the LyXRCTags enum is handled. As you can see
1901 // there are no breaks at all. So it is just a huge fall-through.
1902 // The nice thing is that we will get a warning from the compiler
1903 // if we forget an element.
1904 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1906 case LyXRC::RC_ACCEPT_COMPOUND:
1907 case LyXRC::RC_ALT_LANG:
1908 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1909 case LyXRC::RC_PLAINTEXT_LINELEN:
1910 case LyXRC::RC_AUTOREGIONDELETE:
1911 case LyXRC::RC_AUTORESET_OPTIONS:
1912 case LyXRC::RC_AUTOSAVE:
1913 case LyXRC::RC_AUTO_NUMBER:
1914 case LyXRC::RC_BACKUPDIR_PATH:
1915 case LyXRC::RC_BIBTEX_COMMAND:
1916 case LyXRC::RC_BINDFILE:
1917 case LyXRC::RC_CHECKLASTFILES:
1918 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1919 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1920 case LyXRC::RC_COMPLETION_INLINE_MATH:
1921 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1922 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1923 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1924 case LyXRC::RC_COMPLETION_POPUP_MATH:
1925 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1926 case LyXRC::RC_USELASTFILEPOS:
1927 case LyXRC::RC_LOADSESSION:
1928 case LyXRC::RC_CHKTEX_COMMAND:
1929 case LyXRC::RC_CONVERTER:
1930 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1931 case LyXRC::RC_COPIER:
1932 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1933 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1934 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1935 case LyXRC::RC_DATE_INSERT_FORMAT:
1936 case LyXRC::RC_DEFAULT_LANGUAGE:
1937 case LyXRC::RC_DEFAULT_PAPERSIZE:
1938 case LyXRC::RC_DEFFILE:
1939 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1940 case LyXRC::RC_DISPLAY_GRAPHICS:
1941 case LyXRC::RC_DOCUMENTPATH:
1942 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1943 FileName path(lyxrc_new.document_path);
1944 if (path.exists() && path.isDirectory())
1945 package().document_dir() = FileName(lyxrc.document_path);
1947 case LyXRC::RC_ESC_CHARS:
1948 case LyXRC::RC_EXAMPLEPATH:
1949 case LyXRC::RC_FONT_ENCODING:
1950 case LyXRC::RC_FORMAT:
1951 case LyXRC::RC_INDEX_COMMAND:
1952 case LyXRC::RC_INPUT:
1953 case LyXRC::RC_KBMAP:
1954 case LyXRC::RC_KBMAP_PRIMARY:
1955 case LyXRC::RC_KBMAP_SECONDARY:
1956 case LyXRC::RC_LABEL_INIT_LENGTH:
1957 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1958 case LyXRC::RC_LANGUAGE_AUTO_END:
1959 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1960 case LyXRC::RC_LANGUAGE_COMMAND_END:
1961 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1962 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1963 case LyXRC::RC_LANGUAGE_PACKAGE:
1964 case LyXRC::RC_LANGUAGE_USE_BABEL:
1965 case LyXRC::RC_MACRO_EDIT_STYLE:
1966 case LyXRC::RC_MAKE_BACKUP:
1967 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1968 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1969 case LyXRC::RC_NUMLASTFILES:
1970 case LyXRC::RC_PATH_PREFIX:
1971 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1972 prependEnvPath("PATH", lyxrc.path_prefix);
1974 case LyXRC::RC_PERS_DICT:
1975 case LyXRC::RC_PREVIEW:
1976 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1977 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1978 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1979 case LyXRC::RC_PRINTCOPIESFLAG:
1980 case LyXRC::RC_PRINTER:
1981 case LyXRC::RC_PRINTEVENPAGEFLAG:
1982 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1983 case LyXRC::RC_PRINTFILEEXTENSION:
1984 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1985 case LyXRC::RC_PRINTODDPAGEFLAG:
1986 case LyXRC::RC_PRINTPAGERANGEFLAG:
1987 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1988 case LyXRC::RC_PRINTPAPERFLAG:
1989 case LyXRC::RC_PRINTREVERSEFLAG:
1990 case LyXRC::RC_PRINTSPOOL_COMMAND:
1991 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1992 case LyXRC::RC_PRINTTOFILE:
1993 case LyXRC::RC_PRINTTOPRINTER:
1994 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1995 case LyXRC::RC_PRINT_COMMAND:
1996 case LyXRC::RC_RTL_SUPPORT:
1997 case LyXRC::RC_SCREEN_DPI:
1998 case LyXRC::RC_SCREEN_FONT_ROMAN:
1999 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
2000 case LyXRC::RC_SCREEN_FONT_SANS:
2001 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
2002 case LyXRC::RC_SCREEN_FONT_SCALABLE:
2003 case LyXRC::RC_SCREEN_FONT_SIZES:
2004 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
2005 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
2006 case LyXRC::RC_GEOMETRY_SESSION:
2007 case LyXRC::RC_SCREEN_ZOOM:
2008 case LyXRC::RC_SERVERPIPE:
2009 case LyXRC::RC_SET_COLOR:
2010 case LyXRC::RC_SHOW_BANNER:
2011 case LyXRC::RC_SPELL_COMMAND:
2012 case LyXRC::RC_TEMPDIRPATH:
2013 case LyXRC::RC_TEMPLATEPATH:
2014 case LyXRC::RC_TEX_ALLOWS_SPACES:
2015 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2016 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2017 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2019 case LyXRC::RC_UIFILE:
2020 case LyXRC::RC_USER_EMAIL:
2021 case LyXRC::RC_USER_NAME:
2022 case LyXRC::RC_USETEMPDIR:
2023 case LyXRC::RC_USE_ALT_LANG:
2024 case LyXRC::RC_USE_CONVERTER_CACHE:
2025 case LyXRC::RC_USE_ESC_CHARS:
2026 case LyXRC::RC_USE_INP_ENC:
2027 case LyXRC::RC_USE_PERS_DICT:
2028 case LyXRC::RC_USE_TOOLTIP:
2029 case LyXRC::RC_USE_PIXMAP_CACHE:
2030 case LyXRC::RC_USE_SPELL_LIB:
2031 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2032 case LyXRC::RC_SORT_LAYOUTS:
2033 case LyXRC::RC_FULL_SCREEN_LIMIT:
2034 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2035 case LyXRC::RC_FULL_SCREEN_TABBAR:
2036 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2037 case LyXRC::RC_FULL_SCREEN_WIDTH:
2038 case LyXRC::RC_VISUAL_CURSOR:
2039 case LyXRC::RC_VIEWER:
2040 case LyXRC::RC_LAST: