3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "LayoutFile.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetListings.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetSpace.h"
70 #include "insets/InsetTabular.h"
71 #include "insets/InsetVSpace.h"
72 #include "insets/InsetWrap.h"
74 #include "frontends/alert.h"
75 #include "frontends/Application.h"
76 #include "frontends/KeySymbol.h"
77 #include "frontends/LyXView.h"
78 #include "frontends/Selection.h"
80 #include "support/debug.h"
81 #include "support/environment.h"
82 #include "support/FileName.h"
83 #include "support/filetools.h"
84 #include "support/gettext.h"
85 #include "support/lstrings.h"
86 #include "support/Path.h"
87 #include "support/Package.h"
88 #include "support/Systemcall.h"
89 #include "support/convert.h"
90 #include "support/os.h"
96 using namespace lyx::support;
100 using frontend::LyXView;
102 namespace Alert = frontend::Alert;
107 // This function runs "configure" and then rereads lyx.defaults to
108 // reconfigure the automatic settings.
109 void reconfigure(LyXView * lv, string const & option)
111 // emit message signal.
113 lv->message(_("Running configure..."));
115 // Run configure in user lyx directory
116 PathChanger p(package().user_support());
117 string configure_command = package().configure_command();
118 configure_command += option;
120 int ret = one.startscript(Systemcall::Wait, configure_command);
122 // emit message signal.
124 lv->message(_("Reloading configuration..."));
125 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
126 // Re-read packages.lst
127 LaTeXFeatures::getAvailable();
130 Alert::information(_("System reconfiguration failed"),
131 _("The system reconfiguration has failed.\n"
132 "Default textclass is used but LyX may "
133 "not be able to work properly.\n"
134 "Please reconfigure again if needed."));
137 Alert::information(_("System reconfigured"),
138 _("The system has been reconfigured.\n"
139 "You need to restart LyX to make use of any\n"
140 "updated document class specifications."));
144 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
146 // Try to fix cursor in case it is broken.
147 cursor.fixIfBroken();
149 // This is, of course, a mess. Better create a new doc iterator and use
150 // this in Inset::getStatus. This might require an additional
151 // BufferView * arg, though (which should be avoided)
152 //Cursor safe = *this;
154 for ( ; cursor.depth(); cursor.pop()) {
155 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
156 LASSERT(cursor.idx() <= cursor.lastidx(), /**/);
157 LASSERT(cursor.pit() <= cursor.lastpit(), /**/);
158 LASSERT(cursor.pos() <= cursor.lastpos(), /**/);
160 // The inset's getStatus() will return 'true' if it made
161 // a definitive decision on whether it want to handle the
162 // request or not. The result of this decision is put into
163 // the 'status' parameter.
164 if (cursor.inset().getStatus(cursor, cmd, status)) {
173 /** Return the change status at cursor position, taking in account the
174 * status at each level of the document iterator (a table in a deleted
175 * footnote is deleted).
176 * When \param outer is true, the top slice is not looked at.
178 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
180 size_t const depth = dit.depth() - (outer ? 1 : 0);
182 for (size_t i = 0 ; i < depth ; ++i) {
183 CursorSlice const & slice = dit[i];
184 if (!slice.inset().inMathed()
185 && slice.pos() < slice.paragraph().size()) {
186 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
187 if (ch != Change::UNCHANGED)
191 return Change::UNCHANGED;
198 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
203 void LyXFunc::initKeySequences(KeyMap * kb)
205 keyseq = KeySequence(kb, kb);
206 cancel_meta_seq = KeySequence(kb, kb);
210 void LyXFunc::setLyXView(LyXView * lv)
212 if (lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
213 // save current selection to the selection buffer to allow
214 // middle-button paste in another window
215 cap::saveSelection(lyx_view_->view()->cursor());
220 void LyXFunc::handleKeyFunc(FuncCode action)
222 char_type c = encoded_last_key;
227 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
228 lyx_view_->view()->getIntl().getTransManager().deadkey(
229 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
230 // Need to clear, in case the minibuffer calls these
233 // copied verbatim from do_accent_char
234 view()->cursor().resetAnchor();
235 view()->processUpdateFlags(Update::FitCursor);
238 //FIXME: bookmark handling is a frontend issue. This code should be transferred
239 // to GuiView and be GuiView and be window dependent.
240 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
242 LASSERT(lyx_view_, /**/);
243 if (!LyX::ref().session().bookmarks().isValid(idx))
245 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
246 LASSERT(!bm.filename.empty(), /**/);
247 string const file = bm.filename.absFilename();
248 // if the file is not opened, open it.
249 if (!theBufferList().exists(file)) {
251 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
255 // open may fail, so we need to test it again
256 if (!theBufferList().exists(file))
259 // if the current buffer is not that one, switch to it.
260 if (lyx_view_->buffer()->absFileName() != file) {
263 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
265 // moveToPosition try paragraph id first and then paragraph (pit, pos).
266 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
267 bm.top_id, bm.top_pos))
270 // Cursor jump succeeded!
271 Cursor const & cur = view()->cursor();
272 pit_type new_pit = cur.pit();
273 pos_type new_pos = cur.pos();
274 int new_id = cur.paragraph().id();
276 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
277 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
278 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
279 || bm.top_id != new_id) {
280 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
281 new_pit, new_pos, new_id);
286 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
288 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
290 // Do nothing if we have nothing (JMarc)
291 if (!keysym.isOK()) {
292 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
293 lyx_view_->restartCursor();
297 if (keysym.isModifier()) {
298 LYXERR(Debug::KEY, "isModifier true");
300 lyx_view_->restartCursor();
304 //Encoding const * encoding = view()->cursor().getEncoding();
305 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
306 // FIXME: encoded_last_key shadows the member variable of the same
307 // name. Is that intended?
308 char_type encoded_last_key = keysym.getUCSEncoded();
310 // Do a one-deep top-level lookup for
311 // cancel and meta-fake keys. RVDK_PATCH_5
312 cancel_meta_seq.reset();
314 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
315 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
317 // When not cancel or meta-fake, do the normal lookup.
318 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
319 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
320 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
321 // remove Caps Lock and Mod2 as a modifiers
322 func = keyseq.addkey(keysym, (state | meta_fake_bit));
323 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
326 // Dont remove this unless you know what you are doing.
327 meta_fake_bit = NoModifier;
329 // Can this happen now ?
330 if (func.action == LFUN_NOACTION)
331 func = FuncRequest(LFUN_COMMAND_PREFIX);
333 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
334 << keyseq.print(KeySequence::Portable) << ']');
336 // already here we know if it any point in going further
337 // why not return already here if action == -1 and
338 // num_bytes == 0? (Lgb)
340 if (keyseq.length() > 1)
341 lyx_view_->message(keyseq.print(KeySequence::ForGui));
344 // Maybe user can only reach the key via holding down shift.
345 // Let's see. But only if shift is the only modifier
346 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
347 LYXERR(Debug::KEY, "Trying without shift");
348 func = keyseq.addkey(keysym, NoModifier);
349 LYXERR(Debug::KEY, "Action now " << func.action);
352 if (func.action == LFUN_UNKNOWN_ACTION) {
353 // Hmm, we didn't match any of the keysequences. See
354 // if it's normal insertable text not already covered
356 if (keysym.isText() && keyseq.length() == 1) {
357 LYXERR(Debug::KEY, "isText() is true, inserting.");
358 func = FuncRequest(LFUN_SELF_INSERT,
359 FuncRequest::KEYBOARD);
361 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
362 lyx_view_->message(_("Unknown function."));
363 lyx_view_->restartCursor();
368 if (func.action == LFUN_SELF_INSERT) {
369 if (encoded_last_key != 0) {
370 docstring const arg(1, encoded_last_key);
371 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
372 FuncRequest::KEYBOARD));
373 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
383 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
385 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
388 Buffer * buf = lyx_view_ ? lyx_view_->buffer() : 0;
390 if (cmd.action == LFUN_NOACTION) {
391 flag.message(from_utf8(N_("Nothing to do")));
396 switch (cmd.action) {
397 case LFUN_UNKNOWN_ACTION:
398 #ifndef HAVE_LIBAIKSAURUS
399 case LFUN_THESAURUS_ENTRY:
409 if (flag.unknown()) {
410 flag.message(from_utf8(N_("Unknown action")));
414 if (!flag.enabled()) {
415 if (flag.message().empty())
416 flag.message(from_utf8(N_("Command disabled")));
420 // Check whether we need a buffer
421 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
423 flag.message(from_utf8(N_("Command not allowed with"
424 "out any document open")));
429 // I would really like to avoid having this switch and rather try to
430 // encode this in the function itself.
431 // -- And I'd rather let an inset decide which LFUNs it is willing
432 // to handle (Andre')
434 switch (cmd.action) {
436 // FIXME: these cases should be hidden in GuiApplication::getStatus().
437 case LFUN_WINDOW_CLOSE:
439 return theApp()->getStatus(cmd);
443 // FIXME: these cases should be hidden in GuiView::getStatus().
444 case LFUN_DIALOG_TOGGLE:
445 case LFUN_DIALOG_SHOW:
447 case LFUN_DIALOG_UPDATE:
448 // FIXME: add special handling for about and prefs dialogs here
449 // which do not depend on GuiView.
451 return lyx_view_->getStatus(cmd);
456 case LFUN_TOOLBAR_TOGGLE:
457 case LFUN_INSET_APPLY:
458 case LFUN_BUFFER_WRITE:
459 case LFUN_BUFFER_WRITE_AS:
460 case LFUN_SPLIT_VIEW:
461 case LFUN_CLOSE_TAB_GROUP:
462 case LFUN_COMPLETION_POPUP:
463 case LFUN_COMPLETION_INLINE:
464 case LFUN_COMPLETION_COMPLETE:
466 return lyx_view_->getStatus(cmd);
470 case LFUN_BUFFER_TOGGLE_READ_ONLY:
471 flag.setOnOff(buf->isReadonly());
474 case LFUN_BUFFER_SWITCH:
475 // toggle on the current buffer, but do not toggle off
476 // the other ones (is that a good idea?)
477 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
481 case LFUN_BUFFER_EXPORT:
482 enable = cmd.argument() == "custom"
483 || buf->isExportable(to_utf8(cmd.argument()));
486 case LFUN_BUFFER_CHKTEX:
487 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
490 case LFUN_BUILD_PROGRAM:
491 enable = buf->isExportable("program");
494 case LFUN_VC_REGISTER:
495 enable = !buf->lyxvc().inUse();
497 case LFUN_VC_CHECK_IN:
498 enable = buf->lyxvc().inUse() && !buf->isReadonly();
500 case LFUN_VC_CHECK_OUT:
501 enable = buf->lyxvc().inUse() && buf->isReadonly();
504 case LFUN_VC_UNDO_LAST:
505 enable = buf->lyxvc().inUse();
507 case LFUN_BUFFER_RELOAD:
508 enable = !buf->isUnnamed() && buf->fileName().exists()
509 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
512 case LFUN_CITATION_INSERT: {
513 FuncRequest fr(LFUN_INSET_INSERT, "citation");
514 enable = getStatus(fr).enabled();
518 // This could be used for the no-GUI version. The GUI version is handled in
519 // LyXView::getStatus(). See above.
521 case LFUN_BUFFER_WRITE:
522 case LFUN_BUFFER_WRITE_AS: {
523 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
524 enable = b && (b->isUnnamed() || !b->isClean());
529 case LFUN_BUFFER_WRITE_ALL: {
530 // We enable the command only if there are some modified buffers
531 Buffer * first = theBufferList().first();
536 // We cannot use a for loop as the buffer list is a cycle.
542 b = theBufferList().next(b);
543 } while (b != first);
547 case LFUN_BOOKMARK_GOTO: {
548 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
549 enable = LyX::ref().session().bookmarks().isValid(num);
553 case LFUN_BOOKMARK_CLEAR:
554 enable = LyX::ref().session().bookmarks().size() > 0;
557 // this one is difficult to get right. As a half-baked
558 // solution, we consider only the first action of the sequence
559 case LFUN_COMMAND_SEQUENCE: {
560 // argument contains ';'-terminated commands
561 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
562 FuncRequest func(lyxaction.lookupFunc(firstcmd));
563 func.origin = cmd.origin;
564 flag = getStatus(func);
570 string name = to_utf8(cmd.argument());
571 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
572 func.origin = cmd.origin;
573 flag = getStatus(func);
574 LyX::ref().topLevelCmdDef().release(name);
576 // catch recursion or unknown command definiton
577 // all operations until the recursion or unknown command
578 // definiton occures are performed, so set the state to enabled
584 case LFUN_BUFFER_NEW:
585 case LFUN_BUFFER_NEW_TEMPLATE:
586 case LFUN_WORD_FIND_FORWARD:
587 case LFUN_WORD_FIND_BACKWARD:
588 case LFUN_COMMAND_PREFIX:
589 case LFUN_COMMAND_EXECUTE:
591 case LFUN_META_PREFIX:
592 case LFUN_BUFFER_CLOSE:
593 case LFUN_BUFFER_UPDATE:
594 case LFUN_BUFFER_VIEW:
595 case LFUN_MASTER_BUFFER_UPDATE:
596 case LFUN_MASTER_BUFFER_VIEW:
597 case LFUN_BUFFER_IMPORT:
598 case LFUN_BUFFER_AUTO_SAVE:
599 case LFUN_RECONFIGURE:
602 case LFUN_DROP_LAYOUTS_CHOICE:
604 case LFUN_SERVER_GET_NAME:
605 case LFUN_SERVER_NOTIFY:
606 case LFUN_SERVER_GOTO_FILE_ROW:
607 case LFUN_DIALOG_HIDE:
608 case LFUN_DIALOG_DISCONNECT_INSET:
609 case LFUN_BUFFER_CHILD_OPEN:
610 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
611 case LFUN_KEYMAP_OFF:
612 case LFUN_KEYMAP_PRIMARY:
613 case LFUN_KEYMAP_SECONDARY:
614 case LFUN_KEYMAP_TOGGLE:
616 case LFUN_BUFFER_EXPORT_CUSTOM:
617 case LFUN_BUFFER_PRINT:
618 case LFUN_PREFERENCES_SAVE:
619 case LFUN_SCREEN_FONT_UPDATE:
622 case LFUN_INSET_EDIT:
623 case LFUN_ALL_INSETS_TOGGLE:
624 case LFUN_BUFFER_LANGUAGE:
625 case LFUN_TEXTCLASS_APPLY:
626 case LFUN_TEXTCLASS_LOAD:
627 case LFUN_BUFFER_SAVE_AS_DEFAULT:
628 case LFUN_BUFFER_PARAMS_APPLY:
629 case LFUN_LAYOUT_MODULES_CLEAR:
630 case LFUN_LAYOUT_MODULE_ADD:
631 case LFUN_LAYOUT_RELOAD:
632 case LFUN_LYXRC_APPLY:
633 case LFUN_BUFFER_NEXT:
634 case LFUN_BUFFER_PREVIOUS:
635 case LFUN_WINDOW_NEW:
637 // these are handled in our dispatch()
645 if (!getLocalStatus(view()->cursor(), cmd, flag))
646 flag = view()->getStatus(cmd);
652 // Can we use a readonly buffer?
653 if (buf && buf->isReadonly()
654 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
655 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
656 flag.message(from_utf8(N_("Document is read-only")));
660 // Are we in a DELETED change-tracking region?
662 && lookupChangeType(view()->cursor(), true) == Change::DELETED
663 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
664 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
665 flag.message(from_utf8(N_("This portion of the document is deleted.")));
669 // the default error message if we disable the command
670 if (!flag.enabled() && flag.message().empty())
671 flag.message(from_utf8(N_("Command disabled")));
677 bool LyXFunc::ensureBufferClean(BufferView * bv)
679 Buffer & buf = bv->buffer();
683 docstring const file = buf.fileName().displayName(30);
684 docstring text = bformat(_("The document %1$s has unsaved "
685 "changes.\n\nDo you want to save "
686 "the document?"), file);
687 int const ret = Alert::prompt(_("Save changed document?"),
688 text, 0, 1, _("&Save"),
692 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
694 return buf.isClean();
700 void showPrintError(string const & name)
702 docstring str = bformat(_("Could not print the document %1$s.\n"
703 "Check that your printer is set up correctly."),
704 makeDisplayPath(name, 50));
705 Alert::error(_("Print document failed"), str);
709 bool loadLayoutFile(string const & name, string const & buf_path)
711 if (!LayoutFileList::get().haveClass(name)) {
712 lyxerr << "Document class \"" << name
713 << "\" does not exist."
718 LayoutFile & tc = LayoutFileList::get()[name];
719 if (!tc.load(buf_path)) {
720 docstring s = bformat(_("The document class %1$s "
721 "could not be loaded."), from_utf8(name));
722 Alert::error(_("Could not load class"), s);
729 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
734 void LyXFunc::dispatch(FuncRequest const & cmd)
736 string const argument = to_utf8(cmd.argument());
737 FuncCode const action = cmd.action;
739 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
740 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
742 // we have not done anything wrong yet.
744 dispatch_buffer.erase();
746 // redraw the screen at the end (first of the two drawing steps).
747 //This is done unless explicitely requested otherwise
748 Update::flags updateFlags = Update::FitCursor;
750 FuncStatus const flag = getStatus(cmd);
751 if (!flag.enabled()) {
752 // We cannot use this function here
753 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
754 << lyxaction.getActionName(action)
755 << " [" << action << "] is disabled at this location");
756 setErrorMessage(flag.message());
760 case LFUN_WORD_FIND_FORWARD:
761 case LFUN_WORD_FIND_BACKWARD: {
762 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
763 static docstring last_search;
764 docstring searched_string;
766 if (!cmd.argument().empty()) {
767 last_search = cmd.argument();
768 searched_string = cmd.argument();
770 searched_string = last_search;
773 if (searched_string.empty())
776 bool const fw = action == LFUN_WORD_FIND_FORWARD;
777 docstring const data =
778 find2string(searched_string, true, false, fw);
779 find(view(), FuncRequest(LFUN_WORD_FIND, data));
783 case LFUN_COMMAND_PREFIX:
784 LASSERT(lyx_view_, /**/);
785 lyx_view_->message(keyseq.printOptions(true));
789 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
791 meta_fake_bit = NoModifier;
792 if (lyx_view_->buffer())
793 // cancel any selection
794 dispatch(FuncRequest(LFUN_MARK_OFF));
795 setMessage(from_ascii(N_("Cancel")));
798 case LFUN_META_PREFIX:
799 meta_fake_bit = AltModifier;
800 setMessage(keyseq.print(KeySequence::ForGui));
803 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
804 LASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer(), /**/);
805 Buffer * buf = lyx_view_->buffer();
806 if (buf->lyxvc().inUse())
807 buf->lyxvc().toggleReadOnly();
809 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
813 // --- Menus -----------------------------------------------
814 case LFUN_BUFFER_CLOSE:
815 lyx_view_->closeBuffer();
816 updateFlags = Update::None;
819 case LFUN_BUFFER_RELOAD: {
820 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
821 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
822 docstring text = bformat(_("Any changes will be lost. Are you sure "
823 "you want to revert to the saved version of the document %1$s?"), file);
824 int const ret = Alert::prompt(_("Revert to saved document?"),
825 text, 1, 1, _("&Revert"), _("&Cancel"));
832 case LFUN_BUFFER_UPDATE:
833 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
834 lyx_view_->buffer()->doExport(argument, true);
837 case LFUN_BUFFER_VIEW:
838 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
839 lyx_view_->buffer()->preview(argument);
842 case LFUN_MASTER_BUFFER_UPDATE:
843 LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
844 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
847 case LFUN_MASTER_BUFFER_VIEW:
848 LASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer(), /**/);
849 lyx_view_->buffer()->masterBuffer()->preview(argument);
852 case LFUN_BUILD_PROGRAM:
853 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
854 lyx_view_->buffer()->doExport("program", true);
857 case LFUN_BUFFER_CHKTEX:
858 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
859 lyx_view_->buffer()->runChktex();
862 case LFUN_BUFFER_EXPORT:
863 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
864 if (argument == "custom")
865 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
867 lyx_view_->buffer()->doExport(argument, false);
870 case LFUN_BUFFER_EXPORT_CUSTOM: {
871 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
873 string command = split(argument, format_name, ' ');
874 Format const * format = formats.getFormat(format_name);
876 lyxerr << "Format \"" << format_name
877 << "\" not recognized!"
882 Buffer * buffer = lyx_view_->buffer();
884 // The name of the file created by the conversion process
887 // Output to filename
888 if (format->name() == "lyx") {
889 string const latexname = buffer->latexName(false);
890 filename = changeExtension(latexname,
891 format->extension());
892 filename = addName(buffer->temppath(), filename);
894 if (!buffer->writeFile(FileName(filename)))
898 buffer->doExport(format_name, true, filename);
901 // Substitute $$FName for filename
902 if (!contains(command, "$$FName"))
903 command = "( " + command + " ) < $$FName";
904 command = subst(command, "$$FName", filename);
906 // Execute the command in the background
908 call.startscript(Systemcall::DontWait, command);
912 case LFUN_BUFFER_PRINT: {
913 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
914 // FIXME: cmd.getArg() might fail if one of the arguments
915 // contains double quotes
916 string target = cmd.getArg(0);
917 string target_name = cmd.getArg(1);
918 string command = cmd.getArg(2);
921 || target_name.empty()
922 || command.empty()) {
923 lyxerr << "Unable to parse \""
924 << argument << '"' << endl;
927 if (target != "printer" && target != "file") {
928 lyxerr << "Unrecognized target \""
929 << target << '"' << endl;
933 Buffer * buffer = lyx_view_->buffer();
935 if (!buffer->doExport("dvi", true)) {
936 showPrintError(buffer->absFileName());
940 // Push directory path.
941 string const path = buffer->temppath();
942 // Prevent the compiler from optimizing away p
946 // there are three cases here:
947 // 1. we print to a file
948 // 2. we print directly to a printer
949 // 3. we print using a spool command (print to file first)
952 string const dviname =
953 changeExtension(buffer->latexName(true), "dvi");
955 if (target == "printer") {
956 if (!lyxrc.print_spool_command.empty()) {
957 // case 3: print using a spool
958 string const psname =
959 changeExtension(dviname,".ps");
960 command += ' ' + lyxrc.print_to_file
963 + quoteName(dviname);
966 lyxrc.print_spool_command + ' ';
967 if (target_name != "default") {
968 command2 += lyxrc.print_spool_printerprefix
972 command2 += quoteName(psname);
974 // If successful, then spool command
975 res = one.startscript(
980 res = one.startscript(
981 Systemcall::DontWait,
984 // case 2: print directly to a printer
985 if (target_name != "default")
986 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
987 res = one.startscript(
988 Systemcall::DontWait,
989 command + quoteName(dviname));
993 // case 1: print to a file
994 FileName const filename(makeAbsPath(target_name,
995 lyx_view_->buffer()->filePath()));
996 FileName const dvifile(makeAbsPath(dviname, path));
997 if (filename.exists()) {
998 docstring text = bformat(
999 _("The file %1$s already exists.\n\n"
1000 "Do you want to overwrite that file?"),
1001 makeDisplayPath(filename.absFilename()));
1002 if (Alert::prompt(_("Overwrite file?"),
1003 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1006 command += ' ' + lyxrc.print_to_file
1007 + quoteName(filename.toFilesystemEncoding())
1009 + quoteName(dvifile.toFilesystemEncoding());
1010 res = one.startscript(Systemcall::DontWait,
1015 showPrintError(buffer->absFileName());
1019 // FIXME: There is need for a command-line import.
1021 case LFUN_BUFFER_IMPORT:
1026 case LFUN_BUFFER_AUTO_SAVE:
1027 lyx_view_->buffer()->autoSave();
1030 case LFUN_RECONFIGURE:
1031 // argument is any additional parameter to the configure.py command
1032 reconfigure(lyx_view_, argument);
1035 case LFUN_HELP_OPEN: {
1036 LASSERT(lyx_view_, /**/);
1037 string const arg = argument;
1039 setErrorMessage(from_ascii(N_("Missing argument")));
1042 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1043 if (fname.empty()) {
1044 lyxerr << "LyX: unable to find documentation file `"
1045 << arg << "'. Bad installation?" << endl;
1048 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1049 makeDisplayPath(fname.absFilename())));
1050 Buffer * buf = lyx_view_->loadDocument(fname, false);
1053 lyx_view_->setBuffer(buf);
1054 buf->errors("Parse");
1056 updateFlags = Update::None;
1060 // --- version control -------------------------------
1061 case LFUN_VC_REGISTER:
1062 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1063 if (!ensureBufferClean(view()))
1065 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1066 lyx_view_->buffer()->lyxvc().registrer();
1069 updateFlags = Update::Force;
1072 case LFUN_VC_CHECK_IN:
1073 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1074 if (!ensureBufferClean(view()))
1076 if (lyx_view_->buffer()->lyxvc().inUse()
1077 && !lyx_view_->buffer()->isReadonly()) {
1078 lyx_view_->buffer()->lyxvc().checkIn();
1083 case LFUN_VC_CHECK_OUT:
1084 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1085 if (!ensureBufferClean(view()))
1087 if (lyx_view_->buffer()->lyxvc().inUse()
1088 && lyx_view_->buffer()->isReadonly()) {
1089 lyx_view_->buffer()->lyxvc().checkOut();
1094 case LFUN_VC_REVERT:
1095 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1096 lyx_view_->buffer()->lyxvc().revert();
1100 case LFUN_VC_UNDO_LAST:
1101 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1102 lyx_view_->buffer()->lyxvc().undoLast();
1106 // --- lyxserver commands ----------------------------
1107 case LFUN_SERVER_GET_NAME:
1108 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1109 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1110 LYXERR(Debug::INFO, "FNAME["
1111 << lyx_view_->buffer()->absFileName() << ']');
1114 case LFUN_SERVER_NOTIFY:
1115 dispatch_buffer = keyseq.print(KeySequence::Portable);
1116 theServer().notifyClient(to_utf8(dispatch_buffer));
1119 case LFUN_SERVER_GOTO_FILE_ROW: {
1120 LASSERT(lyx_view_, /**/);
1123 istringstream is(argument);
1124 is >> file_name >> row;
1126 bool loaded = false;
1127 if (prefixIs(file_name, package().temp_dir().absFilename()))
1128 // Needed by inverse dvi search. If it is a file
1129 // in tmpdir, call the apropriated function
1130 buf = theBufferList().getBufferFromTmp(file_name);
1132 // Must replace extension of the file to be .lyx
1133 // and get full path
1134 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1135 // Either change buffer or load the file
1136 if (theBufferList().exists(s.absFilename()))
1137 buf = theBufferList().getBuffer(s.absFilename());
1139 buf = lyx_view_->loadDocument(s);
1145 updateFlags = Update::None;
1150 lyx_view_->setBuffer(buf);
1151 view()->setCursorFromRow(row);
1153 buf->errors("Parse");
1154 updateFlags = Update::FitCursor;
1159 case LFUN_DIALOG_SHOW_NEW_INSET: {
1160 LASSERT(lyx_view_, /**/);
1161 string const name = cmd.getArg(0);
1162 InsetCode code = insetCode(name);
1163 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1164 bool insetCodeOK = true;
1173 case HYPERLINK_CODE: {
1174 InsetCommandParams p(code);
1175 data = InsetCommand::params2string(name, p);
1178 case INCLUDE_CODE: {
1179 // data is the include type: one of "include",
1180 // "input", "verbatiminput" or "verbatiminput*"
1182 // default type is requested
1184 InsetCommandParams p(INCLUDE_CODE, data);
1185 data = InsetCommand::params2string("include", p);
1189 // \c data == "Boxed" || "Frameless" etc
1190 InsetBoxParams p(data);
1191 data = InsetBox::params2string(p);
1195 InsetBranchParams p;
1196 data = InsetBranch::params2string(p);
1200 InsetCommandParams p(CITE_CODE);
1201 data = InsetCommand::params2string(name, p);
1205 data = InsetERT::params2string(InsetCollapsable::Open);
1208 case EXTERNAL_CODE: {
1209 InsetExternalParams p;
1210 Buffer const & buffer = *lyx_view_->buffer();
1211 data = InsetExternal::params2string(p, buffer);
1216 data = InsetFloat::params2string(p);
1219 case LISTINGS_CODE: {
1220 InsetListingsParams p;
1221 data = InsetListings::params2string(p);
1224 case GRAPHICS_CODE: {
1225 InsetGraphicsParams p;
1226 Buffer const & buffer = *lyx_view_->buffer();
1227 data = InsetGraphics::params2string(p, buffer);
1232 data = InsetNote::params2string(p);
1237 data = InsetSpace::params2string(p);
1242 data = InsetVSpace::params2string(space);
1247 data = InsetWrap::params2string(p);
1251 lyxerr << "Inset type '" << name <<
1252 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1253 insetCodeOK = false;
1255 } // end switch(code)
1257 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1261 case LFUN_CITATION_INSERT: {
1262 LASSERT(lyx_view_, /**/);
1263 if (!argument.empty()) {
1264 // we can have one optional argument, delimited by '|'
1265 // citation-insert <key>|<text_before>
1266 // this should be enhanced to also support text_after
1267 // and citation style
1268 string arg = argument;
1270 if (contains(argument, "|")) {
1271 arg = token(argument, '|', 0);
1272 opt1 = token(argument, '|', 1);
1274 InsetCommandParams icp(CITE_CODE);
1275 icp["key"] = from_utf8(arg);
1277 icp["before"] = from_utf8(opt1);
1278 string icstr = InsetCommand::params2string("citation", icp);
1279 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1282 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1286 case LFUN_BUFFER_CHILD_OPEN: {
1287 LASSERT(lyx_view_ && lyx_view_->buffer(), /**/);
1288 Buffer * parent = lyx_view_->buffer();
1289 FileName filename = makeAbsPath(argument, parent->filePath());
1290 view()->saveBookmark(false);
1292 bool parsed = false;
1293 if (theBufferList().exists(filename.absFilename())) {
1294 child = theBufferList().getBuffer(filename.absFilename());
1296 setMessage(bformat(_("Opening child document %1$s..."),
1297 makeDisplayPath(filename.absFilename())));
1298 child = lyx_view_->loadDocument(filename, false);
1302 // Set the parent name of the child document.
1303 // This makes insertion of citations and references in the child work,
1304 // when the target is in the parent or another child document.
1305 child->setParent(parent);
1306 updateLabels(*child->masterBuffer());
1307 lyx_view_->setBuffer(child);
1309 child->errors("Parse");
1312 // If a screen update is required (in case where auto_open is false),
1313 // setBuffer() would have taken care of it already. Otherwise we shall
1314 // reset the update flag because it can cause a circular problem.
1316 updateFlags = Update::None;
1320 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1321 LASSERT(lyx_view_, /**/);
1322 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1325 case LFUN_KEYMAP_OFF:
1326 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1327 lyx_view_->view()->getIntl().keyMapOn(false);
1330 case LFUN_KEYMAP_PRIMARY:
1331 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1332 lyx_view_->view()->getIntl().keyMapPrim();
1335 case LFUN_KEYMAP_SECONDARY:
1336 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1337 lyx_view_->view()->getIntl().keyMapSec();
1340 case LFUN_KEYMAP_TOGGLE:
1341 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1342 lyx_view_->view()->getIntl().toggleKeyMap();
1348 string rest = split(argument, countstr, ' ');
1349 istringstream is(countstr);
1352 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1353 for (int i = 0; i < count; ++i)
1354 dispatch(lyxaction.lookupFunc(rest));
1358 case LFUN_COMMAND_SEQUENCE: {
1359 // argument contains ';'-terminated commands
1360 string arg = argument;
1361 while (!arg.empty()) {
1363 arg = split(arg, first, ';');
1364 FuncRequest func(lyxaction.lookupFunc(first));
1365 func.origin = cmd.origin;
1373 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1374 func.origin = cmd.origin;
1376 LyX::ref().topLevelCmdDef().release(argument);
1378 if (func.action == LFUN_UNKNOWN_ACTION) {
1379 // unknown command definition
1380 lyxerr << "Warning: unknown command definition `"
1384 // recursion detected
1385 lyxerr << "Warning: Recursion in the command definition `"
1386 << argument << "' detected"
1393 case LFUN_PREFERENCES_SAVE: {
1394 lyxrc.write(makeAbsPath("preferences",
1395 package().user_support().absFilename()),
1400 case LFUN_SET_COLOR: {
1402 string const x11_name = split(argument, lyx_name, ' ');
1403 if (lyx_name.empty() || x11_name.empty()) {
1404 setErrorMessage(from_ascii(N_(
1405 "Syntax: set-color <lyx_name>"
1410 bool const graphicsbg_changed =
1411 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1412 x11_name != lcolor.getX11Name(Color_graphicsbg));
1414 if (!lcolor.setColor(lyx_name, x11_name)) {
1416 bformat(_("Set-color \"%1$s\" failed "
1417 "- color is undefined or "
1418 "may not be redefined"),
1419 from_utf8(lyx_name)));
1423 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1425 if (graphicsbg_changed) {
1426 // FIXME: The graphics cache no longer has a changeDisplay method.
1428 graphics::GCache::get().changeDisplay(true);
1435 LASSERT(lyx_view_, /**/);
1436 lyx_view_->message(from_utf8(argument));
1440 case LFUN_ALL_INSETS_TOGGLE: {
1441 LASSERT(lyx_view_, /**/);
1443 string const name = split(argument, action, ' ');
1444 InsetCode const inset_code = insetCode(name);
1446 Cursor & cur = view()->cursor();
1447 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1449 Inset & inset = lyx_view_->buffer()->inset();
1450 InsetIterator it = inset_iterator_begin(inset);
1451 InsetIterator const end = inset_iterator_end(inset);
1452 for (; it != end; ++it) {
1453 if (!it->asInsetMath()
1454 && (inset_code == NO_CODE
1455 || inset_code == it->lyxCode())) {
1456 Cursor tmpcur = cur;
1457 tmpcur.pushBackward(*it);
1458 it->dispatch(tmpcur, fr);
1461 updateFlags = Update::Force | Update::FitCursor;
1465 case LFUN_BUFFER_LANGUAGE: {
1466 LASSERT(lyx_view_, /**/);
1467 Buffer & buffer = *lyx_view_->buffer();
1468 Language const * oldL = buffer.params().language;
1469 Language const * newL = languages.getLanguage(argument);
1470 if (!newL || oldL == newL)
1473 if (oldL->rightToLeft() == newL->rightToLeft()
1474 && !buffer.isMultiLingual())
1475 buffer.changeLanguage(oldL, newL);
1479 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1480 string const fname =
1481 addName(addPath(package().user_support().absFilename(), "templates/"),
1483 Buffer defaults(fname);
1485 istringstream ss(argument);
1488 int const unknown_tokens = defaults.readHeader(lex);
1490 if (unknown_tokens != 0) {
1491 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1492 << unknown_tokens << " unknown token"
1493 << (unknown_tokens == 1 ? "" : "s")
1497 if (defaults.writeFile(FileName(defaults.absFileName())))
1498 setMessage(bformat(_("Document defaults saved in %1$s"),
1499 makeDisplayPath(fname)));
1501 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1505 case LFUN_BUFFER_PARAMS_APPLY: {
1506 LASSERT(lyx_view_, /**/);
1508 Buffer * buffer = lyx_view_->buffer();
1509 DocumentClass * oldClass = buffer->params().documentClassPtr();
1510 Cursor & cur = view()->cursor();
1511 cur.recordUndoFullDocument();
1513 istringstream ss(argument);
1516 int const unknown_tokens = buffer->readHeader(lex);
1518 if (unknown_tokens != 0) {
1519 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1520 << unknown_tokens << " unknown token"
1521 << (unknown_tokens == 1 ? "" : "s")
1525 updateLayout(oldClass, buffer);
1527 updateFlags = Update::Force | Update::FitCursor;
1528 // We are most certainly here because of a change in the document
1529 // It is then better to make sure that all dialogs are in sync with
1530 // current document settings. LyXView::restartCursor() achieve this.
1531 lyx_view_->restartCursor();
1535 case LFUN_LAYOUT_MODULES_CLEAR: {
1536 LASSERT(lyx_view_, /**/);
1537 Buffer * buffer = lyx_view_->buffer();
1538 DocumentClass * oldClass = buffer->params().documentClassPtr();
1539 view()->cursor().recordUndoFullDocument();
1540 buffer->params().clearLayoutModules();
1541 buffer->params().makeDocumentClass();
1542 updateLayout(oldClass, buffer);
1543 updateFlags = Update::Force | Update::FitCursor;
1547 case LFUN_LAYOUT_MODULE_ADD: {
1548 LASSERT(lyx_view_, /**/);
1549 Buffer * buffer = lyx_view_->buffer();
1550 DocumentClass * oldClass = buffer->params().documentClassPtr();
1551 view()->cursor().recordUndoFullDocument();
1552 buffer->params().addLayoutModule(argument);
1553 buffer->params().makeDocumentClass();
1554 updateLayout(oldClass, buffer);
1555 updateFlags = Update::Force | Update::FitCursor;
1559 case LFUN_TEXTCLASS_APPLY: {
1560 LASSERT(lyx_view_, /**/);
1561 Buffer * buffer = lyx_view_->buffer();
1563 if (!loadLayoutFile(argument, buffer->temppath()) &&
1564 !loadLayoutFile(argument, buffer->filePath()))
1567 LayoutFile const * old_layout = buffer->params().baseClass();
1568 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1570 if (old_layout == new_layout)
1574 //Save the old, possibly modular, layout for use in conversion.
1575 DocumentClass * oldDocClass = buffer->params().documentClassPtr();
1576 view()->cursor().recordUndoFullDocument();
1577 buffer->params().setBaseClass(argument);
1578 buffer->params().makeDocumentClass();
1579 updateLayout(oldDocClass, buffer);
1580 updateFlags = Update::Force | Update::FitCursor;
1584 case LFUN_LAYOUT_RELOAD: {
1585 LASSERT(lyx_view_, /**/);
1586 Buffer * buffer = lyx_view_->buffer();
1587 DocumentClass * oldClass = buffer->params().documentClassPtr();
1588 LayoutFileIndex bc = buffer->params().baseClassID();
1589 LayoutFileList::get().reset(bc);
1590 buffer->params().setBaseClass(bc);
1591 buffer->params().makeDocumentClass();
1592 updateLayout(oldClass, buffer);
1593 updateFlags = Update::Force | Update::FitCursor;
1597 case LFUN_TEXTCLASS_LOAD:
1598 loadLayoutFile(argument, lyx_view_->buffer()->temppath()) ||
1599 loadLayoutFile(argument, lyx_view_->buffer()->filePath());
1602 case LFUN_LYXRC_APPLY: {
1603 LyXRC const lyxrc_orig = lyxrc;
1605 istringstream ss(argument);
1606 bool const success = lyxrc.read(ss) == 0;
1609 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1610 << "Unable to read lyxrc data"
1615 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1617 theApp()->resetGui();
1619 /// We force the redraw in any case because there might be
1620 /// some screen font changes.
1621 /// FIXME: only the current view will be updated. the Gui
1622 /// class is able to furnish the list of views.
1623 updateFlags = Update::Force;
1627 case LFUN_BOOKMARK_GOTO:
1628 // go to bookmark, open unopened file and switch to buffer if necessary
1629 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1630 updateFlags = Update::FitCursor;
1633 case LFUN_BOOKMARK_CLEAR:
1634 LyX::ref().session().bookmarks().clear();
1638 LASSERT(theApp(), /**/);
1639 // Let the frontend dispatch its own actions.
1640 if (theApp()->dispatch(cmd))
1641 // Nothing more to do.
1644 // Everything below is only for active lyx_view_
1648 // Let the current LyXView dispatch its own actions.
1649 if (lyx_view_->dispatch(cmd)) {
1650 if (lyx_view_->view())
1651 updateFlags = lyx_view_->view()->cursor().result().update();
1655 LASSERT(lyx_view_->view(), /**/);
1656 // Let the current BufferView dispatch its own actions.
1657 if (view()->dispatch(cmd)) {
1658 // The BufferView took care of its own updates if needed.
1659 updateFlags = Update::None;
1663 // Let the current Cursor dispatch its own actions.
1664 Cursor old = view()->cursor();
1665 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1666 cursorPosBeforeDispatchY_);
1667 view()->cursor().dispatch(cmd);
1669 // notify insets we just left
1670 if (view()->cursor() != old) {
1672 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1674 view()->cursor().fixIfBroken();
1677 // update completion. We do it here and not in
1678 // processKeySym to avoid another redraw just for a
1679 // changed inline completion
1680 if (cmd.origin == FuncRequest::KEYBOARD) {
1681 if (cmd.action == LFUN_SELF_INSERT)
1682 lyx_view_->updateCompletion(view()->cursor(), true, true);
1683 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1684 lyx_view_->updateCompletion(view()->cursor(), false, true);
1686 lyx_view_->updateCompletion(view()->cursor(), false, false);
1689 updateFlags = view()->cursor().result().update();
1692 if (lyx_view_ && lyx_view_->buffer()) {
1693 // BufferView::update() updates the ViewMetricsInfo and
1694 // also initializes the position cache for all insets in
1695 // (at least partially) visible top-level paragraphs.
1696 // We will redraw the screen only if needed.
1697 view()->processUpdateFlags(updateFlags);
1699 // if we executed a mutating lfun, mark the buffer as dirty
1701 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1702 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1703 lyx_view_->buffer()->markDirty();
1705 // Do we have a selection?
1706 theSelection().haveSelection(view()->cursor().selection());
1709 lyx_view_->restartCursor();
1713 // Some messages may already be translated, so we cannot use _()
1714 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1719 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1721 const bool verbose = (cmd.origin == FuncRequest::MENU
1722 || cmd.origin == FuncRequest::TOOLBAR
1723 || cmd.origin == FuncRequest::COMMANDBUFFER);
1725 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1726 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1728 lyx_view_->message(msg);
1732 docstring dispatch_msg = msg;
1733 if (!dispatch_msg.empty())
1734 dispatch_msg += ' ';
1736 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1738 bool argsadded = false;
1740 if (!cmd.argument().empty()) {
1741 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1742 comname += ' ' + cmd.argument();
1747 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1749 if (!shortcuts.empty())
1750 comname += ": " + shortcuts;
1751 else if (!argsadded && !cmd.argument().empty())
1752 comname += ' ' + cmd.argument();
1754 if (!comname.empty()) {
1755 comname = rtrim(comname);
1756 dispatch_msg += '(' + rtrim(comname) + ')';
1759 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1760 if (!dispatch_msg.empty())
1761 lyx_view_->message(dispatch_msg);
1765 void LyXFunc::reloadBuffer()
1767 FileName filename = lyx_view_->buffer()->fileName();
1768 // The user has already confirmed that the changes, if any, should
1769 // be discarded. So we just release the Buffer and don't call closeBuffer();
1770 theBufferList().release(lyx_view_->buffer());
1771 Buffer * buf = lyx_view_->loadDocument(filename);
1772 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1776 lyx_view_->setBuffer(buf);
1777 buf->errors("Parse");
1778 str = bformat(_("Document %1$s reloaded."), disp_fn);
1780 str = bformat(_("Could not reload document %1$s"), disp_fn);
1782 lyx_view_->message(str);
1785 // Each "lyx_view_" should have it's own message method. lyxview and
1786 // the minibuffer would use the minibuffer, but lyxserver would
1787 // send an ERROR signal to its client. Alejandro 970603
1788 // This function is bit problematic when it comes to NLS, to make the
1789 // lyx servers client be language indepenent we must not translate
1790 // strings sent to this func.
1791 void LyXFunc::setErrorMessage(docstring const & m) const
1793 dispatch_buffer = m;
1798 void LyXFunc::setMessage(docstring const & m) const
1800 dispatch_buffer = m;
1804 docstring LyXFunc::viewStatusMessage()
1806 // When meta-fake key is pressed, show the key sequence so far + "M-".
1808 return keyseq.print(KeySequence::ForGui) + "M-";
1810 // Else, when a non-complete key sequence is pressed,
1811 // show the available options.
1812 if (keyseq.length() > 0 && !keyseq.deleted())
1813 return keyseq.printOptions(true);
1815 LASSERT(lyx_view_, /**/);
1816 if (!lyx_view_->buffer())
1817 return _("Welcome to LyX!");
1819 return view()->cursor().currentState();
1823 BufferView * LyXFunc::view() const
1825 LASSERT(lyx_view_, /**/);
1826 return lyx_view_->view();
1830 bool LyXFunc::wasMetaKey() const
1832 return (meta_fake_bit != NoModifier);
1836 void LyXFunc::updateLayout(DocumentClass * oldlayout,Buffer * buffer)
1838 lyx_view_->message(_("Converting document to new document class..."));
1840 StableDocIterator backcur(view()->cursor());
1841 ErrorList & el = buffer->errorList("Class Switch");
1842 cap::switchBetweenClasses(
1843 oldlayout, buffer->params().documentClassPtr(),
1844 static_cast<InsetText &>(buffer->inset()), el);
1846 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1848 buffer->errors("Class Switch");
1849 updateLabels(*buffer);
1855 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1857 // Why the switch you might ask. It is a trick to ensure that all
1858 // the elements in the LyXRCTags enum is handled. As you can see
1859 // there are no breaks at all. So it is just a huge fall-through.
1860 // The nice thing is that we will get a warning from the compiler
1861 // if we forget an element.
1862 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1864 case LyXRC::RC_ACCEPT_COMPOUND:
1865 case LyXRC::RC_ALT_LANG:
1866 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1867 case LyXRC::RC_PLAINTEXT_LINELEN:
1868 case LyXRC::RC_AUTOREGIONDELETE:
1869 case LyXRC::RC_AUTORESET_OPTIONS:
1870 case LyXRC::RC_AUTOSAVE:
1871 case LyXRC::RC_AUTO_NUMBER:
1872 case LyXRC::RC_BACKUPDIR_PATH:
1873 case LyXRC::RC_BIBTEX_COMMAND:
1874 case LyXRC::RC_BINDFILE:
1875 case LyXRC::RC_CHECKLASTFILES:
1876 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1877 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1878 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1879 case LyXRC::RC_COMPLETION_INLINE_MATH:
1880 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1881 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1882 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1883 case LyXRC::RC_COMPLETION_POPUP_MATH:
1884 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1885 case LyXRC::RC_USELASTFILEPOS:
1886 case LyXRC::RC_LOADSESSION:
1887 case LyXRC::RC_CHKTEX_COMMAND:
1888 case LyXRC::RC_CONVERTER:
1889 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1890 case LyXRC::RC_COPIER:
1891 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1892 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1893 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1894 case LyXRC::RC_DATE_INSERT_FORMAT:
1895 case LyXRC::RC_DEFAULT_LANGUAGE:
1896 case LyXRC::RC_DEFAULT_PAPERSIZE:
1897 case LyXRC::RC_DEFFILE:
1898 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1899 case LyXRC::RC_DISPLAY_GRAPHICS:
1900 case LyXRC::RC_DOCUMENTPATH:
1901 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1902 FileName path(lyxrc_new.document_path);
1903 if (path.exists() && path.isDirectory())
1904 package().document_dir() = FileName(lyxrc.document_path);
1906 case LyXRC::RC_ESC_CHARS:
1907 case LyXRC::RC_EXAMPLEPATH:
1908 case LyXRC::RC_FONT_ENCODING:
1909 case LyXRC::RC_FORMAT:
1910 case LyXRC::RC_GROUP_LAYOUTS:
1911 case LyXRC::RC_INDEX_COMMAND:
1912 case LyXRC::RC_INPUT:
1913 case LyXRC::RC_KBMAP:
1914 case LyXRC::RC_KBMAP_PRIMARY:
1915 case LyXRC::RC_KBMAP_SECONDARY:
1916 case LyXRC::RC_LABEL_INIT_LENGTH:
1917 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1918 case LyXRC::RC_LANGUAGE_AUTO_END:
1919 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1920 case LyXRC::RC_LANGUAGE_COMMAND_END:
1921 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1922 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1923 case LyXRC::RC_LANGUAGE_PACKAGE:
1924 case LyXRC::RC_LANGUAGE_USE_BABEL:
1925 case LyXRC::RC_MACRO_EDIT_STYLE:
1926 case LyXRC::RC_MAKE_BACKUP:
1927 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1928 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1929 case LyXRC::RC_NUMLASTFILES:
1930 case LyXRC::RC_PATH_PREFIX:
1931 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1932 prependEnvPath("PATH", lyxrc.path_prefix);
1934 case LyXRC::RC_PERS_DICT:
1935 case LyXRC::RC_PREVIEW:
1936 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1937 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1938 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1939 case LyXRC::RC_PRINTCOPIESFLAG:
1940 case LyXRC::RC_PRINTER:
1941 case LyXRC::RC_PRINTEVENPAGEFLAG:
1942 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1943 case LyXRC::RC_PRINTFILEEXTENSION:
1944 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1945 case LyXRC::RC_PRINTODDPAGEFLAG:
1946 case LyXRC::RC_PRINTPAGERANGEFLAG:
1947 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1948 case LyXRC::RC_PRINTPAPERFLAG:
1949 case LyXRC::RC_PRINTREVERSEFLAG:
1950 case LyXRC::RC_PRINTSPOOL_COMMAND:
1951 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1952 case LyXRC::RC_PRINTTOFILE:
1953 case LyXRC::RC_PRINTTOPRINTER:
1954 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1955 case LyXRC::RC_PRINT_COMMAND:
1956 case LyXRC::RC_RTL_SUPPORT:
1957 case LyXRC::RC_SCREEN_DPI:
1958 case LyXRC::RC_SCREEN_FONT_ROMAN:
1959 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1960 case LyXRC::RC_SCREEN_FONT_SANS:
1961 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1962 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1963 case LyXRC::RC_SCREEN_FONT_SIZES:
1964 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1965 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1966 case LyXRC::RC_GEOMETRY_SESSION:
1967 case LyXRC::RC_SCREEN_ZOOM:
1968 case LyXRC::RC_SERVERPIPE:
1969 case LyXRC::RC_SET_COLOR:
1970 case LyXRC::RC_SHOW_BANNER:
1971 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
1972 case LyXRC::RC_SPELL_COMMAND:
1973 case LyXRC::RC_TEMPDIRPATH:
1974 case LyXRC::RC_TEMPLATEPATH:
1975 case LyXRC::RC_TEX_ALLOWS_SPACES:
1976 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
1977 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
1978 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
1980 case LyXRC::RC_UIFILE:
1981 case LyXRC::RC_USER_EMAIL:
1982 case LyXRC::RC_USER_NAME:
1983 case LyXRC::RC_USETEMPDIR:
1984 case LyXRC::RC_USE_ALT_LANG:
1985 case LyXRC::RC_USE_CONVERTER_CACHE:
1986 case LyXRC::RC_USE_ESC_CHARS:
1987 case LyXRC::RC_USE_INP_ENC:
1988 case LyXRC::RC_USE_PERS_DICT:
1989 case LyXRC::RC_USE_TOOLTIP:
1990 case LyXRC::RC_USE_PIXMAP_CACHE:
1991 case LyXRC::RC_USE_SPELL_LIB:
1992 case LyXRC::RC_VIEWDVI_PAPEROPTION:
1993 case LyXRC::RC_SORT_LAYOUTS:
1994 case LyXRC::RC_FULL_SCREEN_LIMIT:
1995 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
1996 case LyXRC::RC_FULL_SCREEN_TABBAR:
1997 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
1998 case LyXRC::RC_FULL_SCREEN_WIDTH:
1999 case LyXRC::RC_VISUAL_CURSOR:
2000 case LyXRC::RC_VIEWER:
2001 case LyXRC::RC_LAST: