3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "LayoutFile.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetGraphics.h"
66 #include "insets/InsetInclude.h"
67 #include "insets/InsetListings.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetPhantom.h"
70 #include "insets/InsetSpace.h"
71 #include "insets/InsetTabular.h"
72 #include "insets/InsetVSpace.h"
73 #include "insets/InsetWrap.h"
75 #include "frontends/alert.h"
76 #include "frontends/Application.h"
77 #include "frontends/KeySymbol.h"
78 #include "frontends/LyXView.h"
79 #include "frontends/Selection.h"
81 #include "support/debug.h"
82 #include "support/environment.h"
83 #include "support/FileName.h"
84 #include "support/filetools.h"
85 #include "support/gettext.h"
86 #include "support/lstrings.h"
87 #include "support/Path.h"
88 #include "support/Package.h"
89 #include "support/Systemcall.h"
90 #include "support/convert.h"
91 #include "support/os.h"
97 using namespace lyx::support;
101 using frontend::LyXView;
103 namespace Alert = frontend::Alert;
108 // This function runs "configure" and then rereads lyx.defaults to
109 // reconfigure the automatic settings.
110 void reconfigure(LyXView * lv, string const & option)
112 // emit message signal.
114 lv->message(_("Running configure..."));
116 // Run configure in user lyx directory
117 PathChanger p(package().user_support());
118 string configure_command = package().configure_command();
119 configure_command += option;
121 int ret = one.startscript(Systemcall::Wait, configure_command);
123 // emit message signal.
125 lv->message(_("Reloading configuration..."));
126 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
127 // Re-read packages.lst
128 LaTeXFeatures::getAvailable();
131 Alert::information(_("System reconfiguration failed"),
132 _("The system reconfiguration has failed.\n"
133 "Default textclass is used but LyX may "
134 "not be able to work properly.\n"
135 "Please reconfigure again if needed."));
138 Alert::information(_("System reconfigured"),
139 _("The system has been reconfigured.\n"
140 "You need to restart LyX to make use of any\n"
141 "updated document class specifications."));
145 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
147 // Try to fix cursor in case it is broken.
148 cursor.fixIfBroken();
150 // This is, of course, a mess. Better create a new doc iterator and use
151 // this in Inset::getStatus. This might require an additional
152 // BufferView * arg, though (which should be avoided)
153 //Cursor safe = *this;
155 for ( ; cursor.depth(); cursor.pop()) {
156 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
157 LASSERT(cursor.idx() <= cursor.lastidx(), /**/);
158 LASSERT(cursor.pit() <= cursor.lastpit(), /**/);
159 LASSERT(cursor.pos() <= cursor.lastpos(), /**/);
161 // The inset's getStatus() will return 'true' if it made
162 // a definitive decision on whether it want to handle the
163 // request or not. The result of this decision is put into
164 // the 'status' parameter.
165 if (cursor.inset().getStatus(cursor, cmd, status)) {
174 /** Return the change status at cursor position, taking in account the
175 * status at each level of the document iterator (a table in a deleted
176 * footnote is deleted).
177 * When \param outer is true, the top slice is not looked at.
179 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
181 size_t const depth = dit.depth() - (outer ? 1 : 0);
183 for (size_t i = 0 ; i < depth ; ++i) {
184 CursorSlice const & slice = dit[i];
185 if (!slice.inset().inMathed()
186 && slice.pos() < slice.paragraph().size()) {
187 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
188 if (ch != Change::UNCHANGED)
192 return Change::UNCHANGED;
199 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
204 void LyXFunc::initKeySequences(KeyMap * kb)
206 keyseq = KeySequence(kb, kb);
207 cancel_meta_seq = KeySequence(kb, kb);
211 void LyXFunc::setLyXView(LyXView * lv)
213 if (lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
214 // save current selection to the selection buffer to allow
215 // middle-button paste in another window
216 cap::saveSelection(lyx_view_->view()->cursor());
221 void LyXFunc::handleKeyFunc(FuncCode action)
223 char_type c = encoded_last_key;
228 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
229 lyx_view_->view()->getIntl().getTransManager().deadkey(
230 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
231 // Need to clear, in case the minibuffer calls these
234 // copied verbatim from do_accent_char
235 view()->cursor().resetAnchor();
236 view()->processUpdateFlags(Update::FitCursor);
239 //FIXME: bookmark handling is a frontend issue. This code should be transferred
240 // to GuiView and be GuiView and be window dependent.
241 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
243 LASSERT(lyx_view_, /**/);
244 if (!theSession().bookmarks().isValid(idx))
246 BookmarksSection::Bookmark const & bm = theSession().bookmarks().bookmark(idx);
247 LASSERT(!bm.filename.empty(), /**/);
248 string const file = bm.filename.absFilename();
249 // if the file is not opened, open it.
250 if (!theBufferList().exists(bm.filename)) {
252 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
256 // open may fail, so we need to test it again
257 if (!theBufferList().exists(bm.filename))
260 // if the current buffer is not that one, switch to it.
261 if (lyx_view_->buffer()->fileName() != bm.filename) {
264 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
266 // moveToPosition try paragraph id first and then paragraph (pit, pos).
267 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
268 bm.top_id, bm.top_pos))
271 // Cursor jump succeeded!
272 Cursor const & cur = view()->cursor();
273 pit_type new_pit = cur.pit();
274 pos_type new_pos = cur.pos();
275 int new_id = cur.paragraph().id();
277 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
278 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
279 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
280 || bm.top_id != new_id) {
281 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
282 new_pit, new_pos, new_id);
287 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
289 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
291 // Do nothing if we have nothing (JMarc)
292 if (!keysym.isOK()) {
293 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
294 lyx_view_->restartCursor();
298 if (keysym.isModifier()) {
299 LYXERR(Debug::KEY, "isModifier true");
301 lyx_view_->restartCursor();
305 //Encoding const * encoding = view()->cursor().getEncoding();
306 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
307 // FIXME: encoded_last_key shadows the member variable of the same
308 // name. Is that intended?
309 char_type encoded_last_key = keysym.getUCSEncoded();
311 // Do a one-deep top-level lookup for
312 // cancel and meta-fake keys. RVDK_PATCH_5
313 cancel_meta_seq.reset();
315 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
316 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
318 // When not cancel or meta-fake, do the normal lookup.
319 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
320 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
321 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
322 // remove Caps Lock and Mod2 as a modifiers
323 func = keyseq.addkey(keysym, (state | meta_fake_bit));
324 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
327 // Dont remove this unless you know what you are doing.
328 meta_fake_bit = NoModifier;
330 // Can this happen now ?
331 if (func.action == LFUN_NOACTION)
332 func = FuncRequest(LFUN_COMMAND_PREFIX);
334 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
335 << keyseq.print(KeySequence::Portable) << ']');
337 // already here we know if it any point in going further
338 // why not return already here if action == -1 and
339 // num_bytes == 0? (Lgb)
341 if (keyseq.length() > 1)
342 lyx_view_->message(keyseq.print(KeySequence::ForGui));
345 // Maybe user can only reach the key via holding down shift.
346 // Let's see. But only if shift is the only modifier
347 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
348 LYXERR(Debug::KEY, "Trying without shift");
349 func = keyseq.addkey(keysym, NoModifier);
350 LYXERR(Debug::KEY, "Action now " << func.action);
353 if (func.action == LFUN_UNKNOWN_ACTION) {
354 // Hmm, we didn't match any of the keysequences. See
355 // if it's normal insertable text not already covered
357 if (keysym.isText() && keyseq.length() == 1) {
358 LYXERR(Debug::KEY, "isText() is true, inserting.");
359 func = FuncRequest(LFUN_SELF_INSERT,
360 FuncRequest::KEYBOARD);
362 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
363 lyx_view_->message(_("Unknown function."));
364 lyx_view_->restartCursor();
369 if (func.action == LFUN_SELF_INSERT) {
370 if (encoded_last_key != 0) {
371 docstring const arg(1, encoded_last_key);
372 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
373 FuncRequest::KEYBOARD));
374 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
384 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
386 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
389 Buffer * buf = lyx_view_ ? lyx_view_->buffer() : 0;
391 if (cmd.action == LFUN_NOACTION) {
392 flag.message(from_utf8(N_("Nothing to do")));
393 flag.setEnabled(false);
397 switch (cmd.action) {
398 case LFUN_UNKNOWN_ACTION:
399 #if !defined(HAVE_LIBMYTHES) && !defined(HAVE_LIBAIKSAURUS)
400 case LFUN_THESAURUS_ENTRY:
403 flag.setEnabled(false);
410 if (flag.unknown()) {
411 flag.message(from_utf8(N_("Unknown action")));
415 if (!flag.enabled()) {
416 if (flag.message().empty())
417 flag.message(from_utf8(N_("Command disabled")));
421 // Check whether we need a buffer
422 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
424 flag.message(from_utf8(N_("Command not allowed with"
425 "out any document open")));
426 flag.setEnabled(false);
430 // I would really like to avoid having this switch and rather try to
431 // encode this in the function itself.
432 // -- And I'd rather let an inset decide which LFUNs it is willing
433 // to handle (Andre')
435 switch (cmd.action) {
437 case LFUN_BUFFER_TOGGLE_READ_ONLY:
438 flag.setOnOff(buf->isReadonly());
441 case LFUN_BUFFER_SWITCH:
442 // toggle on the current buffer, but do not toggle off
443 // the other ones (is that a good idea?)
444 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
448 case LFUN_BUFFER_EXPORT:
449 enable = cmd.argument() == "custom"
450 || buf->isExportable(to_utf8(cmd.argument()));
453 case LFUN_BUFFER_CHKTEX:
454 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
457 case LFUN_BUILD_PROGRAM:
458 enable = buf->isExportable("program");
461 case LFUN_VC_REGISTER:
462 enable = !buf->lyxvc().inUse();
464 case LFUN_VC_CHECK_IN:
465 enable = buf->lyxvc().checkInEnabled();
467 case LFUN_VC_CHECK_OUT:
468 enable = buf->lyxvc().checkOutEnabled();
471 enable = buf->lyxvc().inUse();
473 case LFUN_VC_UNDO_LAST:
474 enable = buf->lyxvc().undoLastEnabled();
476 case LFUN_BUFFER_RELOAD:
477 enable = !buf->isUnnamed() && buf->fileName().exists()
478 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
481 case LFUN_CITATION_INSERT: {
482 FuncRequest fr(LFUN_INSET_INSERT, "citation");
483 enable = getStatus(fr).enabled();
487 // This could be used for the no-GUI version. The GUI version is handled in
488 // LyXView::getStatus(). See above.
490 case LFUN_BUFFER_WRITE:
491 case LFUN_BUFFER_WRITE_AS: {
492 Buffer * b = theBufferList().getBuffer(FileName(cmd.getArg(0)));
493 enable = b && (b->isUnnamed() || !b->isClean());
498 case LFUN_BUFFER_WRITE_ALL: {
499 // We enable the command only if there are some modified buffers
500 Buffer * first = theBufferList().first();
505 // We cannot use a for loop as the buffer list is a cycle.
511 b = theBufferList().next(b);
512 } while (b != first);
516 case LFUN_BOOKMARK_GOTO: {
517 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
518 enable = theSession().bookmarks().isValid(num);
522 case LFUN_BOOKMARK_CLEAR:
523 enable = theSession().bookmarks().hasValid();
526 // this one is difficult to get right. As a half-baked
527 // solution, we consider only the first action of the sequence
528 case LFUN_COMMAND_SEQUENCE: {
529 // argument contains ';'-terminated commands
530 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
531 FuncRequest func(lyxaction.lookupFunc(firstcmd));
532 func.origin = cmd.origin;
533 flag = getStatus(func);
537 // we want to check if at least one of these is enabled
538 case LFUN_COMMAND_ALTERNATIVES: {
539 // argument contains ';'-terminated commands
540 string arg = to_utf8(cmd.argument());
541 while (!arg.empty()) {
543 arg = split(arg, first, ';');
544 FuncRequest func(lyxaction.lookupFunc(first));
545 func.origin = cmd.origin;
546 flag = getStatus(func);
547 // if this one is enabled, the whole thing is
556 string name = to_utf8(cmd.argument());
557 if (theTopLevelCmdDef().lock(name, func)) {
558 func.origin = cmd.origin;
559 flag = getStatus(func);
560 theTopLevelCmdDef().release(name);
562 // catch recursion or unknown command definiton
563 // all operations until the recursion or unknown command
564 // definiton occures are performed, so set the state to enabled
570 case LFUN_VC_COMMAND: {
571 if (cmd.argument().empty())
574 if (!buf && contains(cmd.getArg(0), 'D'))
579 case LFUN_WORD_FIND_FORWARD:
580 case LFUN_WORD_FIND_BACKWARD:
581 case LFUN_WORD_FINDADV:
582 case LFUN_COMMAND_PREFIX:
583 case LFUN_COMMAND_EXECUTE:
585 case LFUN_META_PREFIX:
586 case LFUN_BUFFER_CLOSE:
587 case LFUN_BUFFER_UPDATE:
588 case LFUN_BUFFER_VIEW:
589 case LFUN_MASTER_BUFFER_UPDATE:
590 case LFUN_MASTER_BUFFER_VIEW:
591 case LFUN_BUFFER_IMPORT:
592 case LFUN_BUFFER_AUTO_SAVE:
593 case LFUN_RECONFIGURE:
595 case LFUN_DROP_LAYOUTS_CHOICE:
597 case LFUN_SERVER_GET_FILENAME:
598 case LFUN_SERVER_NOTIFY:
599 case LFUN_SERVER_GOTO_FILE_ROW:
600 case LFUN_DIALOG_HIDE:
601 case LFUN_DIALOG_DISCONNECT_INSET:
602 case LFUN_BUFFER_CHILD_OPEN:
603 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
604 case LFUN_KEYMAP_OFF:
605 case LFUN_KEYMAP_PRIMARY:
606 case LFUN_KEYMAP_SECONDARY:
607 case LFUN_KEYMAP_TOGGLE:
609 case LFUN_BUFFER_EXPORT_CUSTOM:
610 case LFUN_BUFFER_PRINT:
611 case LFUN_PREFERENCES_SAVE:
613 case LFUN_INSET_EDIT:
614 case LFUN_BUFFER_LANGUAGE:
615 case LFUN_TEXTCLASS_APPLY:
616 case LFUN_TEXTCLASS_LOAD:
617 case LFUN_BUFFER_SAVE_AS_DEFAULT:
618 case LFUN_BUFFER_PARAMS_APPLY:
619 case LFUN_LAYOUT_MODULES_CLEAR:
620 case LFUN_LAYOUT_MODULE_ADD:
621 case LFUN_LAYOUT_RELOAD:
622 case LFUN_LYXRC_APPLY:
623 case LFUN_BUFFER_NEXT:
624 case LFUN_BUFFER_PREVIOUS:
625 // these are handled in our dispatch()
633 if (theApp()->getStatus(cmd, flag))
636 // Does the view know something?
641 if (lyx_view_->getStatus(cmd, flag))
644 // If we have a BufferView, try cursor position and
645 // then the BufferView.
650 if (!getLocalStatus(view()->cursor(), cmd, flag))
651 flag = view()->getStatus(cmd);
655 flag.setEnabled(false);
657 // Can we use a readonly buffer?
658 if (buf && buf->isReadonly()
659 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
660 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
661 flag.message(from_utf8(N_("Document is read-only")));
662 flag.setEnabled(false);
665 // Are we in a DELETED change-tracking region?
667 && lookupChangeType(view()->cursor(), true) == Change::DELETED
668 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
669 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
670 flag.message(from_utf8(N_("This portion of the document is deleted.")));
671 flag.setEnabled(false);
674 // the default error message if we disable the command
675 if (!flag.enabled() && flag.message().empty())
676 flag.message(from_utf8(N_("Command disabled")));
682 bool LyXFunc::ensureBufferClean(BufferView * bv)
684 Buffer & buf = bv->buffer();
685 if (buf.isClean() && !buf.isUnnamed())
688 docstring const file = buf.fileName().displayName(30);
691 if (!buf.isUnnamed()) {
692 text = bformat(_("The document %1$s has unsaved "
693 "changes.\n\nDo you want to save "
694 "the document?"), file);
695 title = _("Save changed document?");
698 text = bformat(_("The document %1$s has not been "
699 "saved yet.\n\nDo you want to save "
700 "the document?"), file);
701 title = _("Save new document?");
703 int const ret = Alert::prompt(title, text, 0, 1, _("&Save"), _("&Cancel"));
706 lyx_view_->dispatch(FuncRequest(LFUN_BUFFER_WRITE));
708 return buf.isClean() && !buf.isUnnamed();
714 void showPrintError(string const & name)
716 docstring str = bformat(_("Could not print the document %1$s.\n"
717 "Check that your printer is set up correctly."),
718 makeDisplayPath(name, 50));
719 Alert::error(_("Print document failed"), str);
723 bool loadLayoutFile(string const & name, string const & buf_path)
725 if (!LayoutFileList::get().haveClass(name)) {
726 lyxerr << "Document class \"" << name
727 << "\" does not exist."
732 LayoutFile & tc = LayoutFileList::get()[name];
733 if (!tc.load(buf_path)) {
734 docstring s = bformat(_("The document class %1$s "
735 "could not be loaded."), from_utf8(name));
736 Alert::error(_("Could not load class"), s);
743 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
748 void LyXFunc::dispatch(FuncRequest const & cmd)
750 string const argument = to_utf8(cmd.argument());
751 FuncCode const action = cmd.action;
753 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
754 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
756 // we have not done anything wrong yet.
758 dispatch_buffer.erase();
760 // redraw the screen at the end (first of the two drawing steps).
761 //This is done unless explicitely requested otherwise
762 Update::flags updateFlags = Update::FitCursor;
764 FuncStatus const flag = getStatus(cmd);
765 if (!flag.enabled()) {
766 // We cannot use this function here
767 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
768 << lyxaction.getActionName(action)
769 << " [" << action << "] is disabled at this location");
770 setErrorMessage(flag.message());
772 lyx_view_->restartCursor();
774 Buffer * buffer = lyx_view_ ? lyx_view_->buffer() : 0;
777 case LFUN_WORD_FIND_FORWARD:
778 case LFUN_WORD_FIND_BACKWARD: {
779 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
780 static docstring last_search;
781 docstring searched_string;
783 if (!cmd.argument().empty()) {
784 last_search = cmd.argument();
785 searched_string = cmd.argument();
787 searched_string = last_search;
790 if (searched_string.empty())
793 bool const fw = action == LFUN_WORD_FIND_FORWARD;
794 docstring const data =
795 find2string(searched_string, true, false, fw);
796 find(view(), FuncRequest(LFUN_WORD_FIND, data));
800 case LFUN_COMMAND_PREFIX:
801 LASSERT(lyx_view_, /**/);
802 lyx_view_->message(keyseq.printOptions(true));
806 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
808 meta_fake_bit = NoModifier;
810 // cancel any selection
811 dispatch(FuncRequest(LFUN_MARK_OFF));
812 setMessage(from_ascii(N_("Cancel")));
815 case LFUN_META_PREFIX:
816 meta_fake_bit = AltModifier;
817 setMessage(keyseq.print(KeySequence::ForGui));
820 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
821 LASSERT(lyx_view_ && lyx_view_->view() && buffer, /**/);
822 if (buffer->lyxvc().inUse())
823 buffer->lyxvc().toggleReadOnly();
825 buffer->setReadonly(!buffer->isReadonly());
829 // --- Menus -----------------------------------------------
830 case LFUN_BUFFER_CLOSE:
831 lyx_view_->closeBuffer();
833 updateFlags = Update::None;
836 case LFUN_BUFFER_RELOAD: {
837 LASSERT(lyx_view_ && buffer, /**/);
838 docstring const file = makeDisplayPath(buffer->absFileName(), 20);
839 docstring text = bformat(_("Any changes will be lost. Are you sure "
840 "you want to revert to the saved version of the document %1$s?"), file);
841 int const ret = Alert::prompt(_("Revert to saved document?"),
842 text, 1, 1, _("&Revert"), _("&Cancel"));
849 case LFUN_BUFFER_UPDATE:
850 LASSERT(lyx_view_ && buffer, /**/);
851 buffer->doExport(argument, true);
854 case LFUN_BUFFER_VIEW:
855 LASSERT(lyx_view_ && buffer, /**/);
856 buffer->preview(argument);
859 case LFUN_MASTER_BUFFER_UPDATE:
860 LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
861 buffer->masterBuffer()->doExport(argument, true);
864 case LFUN_MASTER_BUFFER_VIEW:
865 LASSERT(lyx_view_ && buffer && buffer->masterBuffer(), /**/);
866 buffer->masterBuffer()->preview(argument);
869 case LFUN_BUILD_PROGRAM:
870 LASSERT(lyx_view_ && buffer, /**/);
871 buffer->doExport("program", true);
874 case LFUN_BUFFER_CHKTEX:
875 LASSERT(lyx_view_ && buffer, /**/);
879 case LFUN_BUFFER_EXPORT:
880 LASSERT(lyx_view_ && buffer, /**/);
881 if (argument == "custom")
882 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
884 buffer->doExport(argument, false);
887 case LFUN_BUFFER_EXPORT_CUSTOM: {
888 LASSERT(lyx_view_ && buffer, /**/);
890 string command = split(argument, format_name, ' ');
891 Format const * format = formats.getFormat(format_name);
893 lyxerr << "Format \"" << format_name
894 << "\" not recognized!"
899 // The name of the file created by the conversion process
902 // Output to filename
903 if (format->name() == "lyx") {
904 string const latexname = buffer->latexName(false);
905 filename = changeExtension(latexname,
906 format->extension());
907 filename = addName(buffer->temppath(), filename);
909 if (!buffer->writeFile(FileName(filename)))
913 buffer->doExport(format_name, true, filename);
916 // Substitute $$FName for filename
917 if (!contains(command, "$$FName"))
918 command = "( " + command + " ) < $$FName";
919 command = subst(command, "$$FName", filename);
921 // Execute the command in the background
923 call.startscript(Systemcall::DontWait, command);
927 case LFUN_BUFFER_PRINT: {
928 LASSERT(lyx_view_ && buffer, /**/);
929 // FIXME: cmd.getArg() might fail if one of the arguments
930 // contains double quotes
931 string target = cmd.getArg(0);
932 string target_name = cmd.getArg(1);
933 string command = cmd.getArg(2);
936 || target_name.empty()
937 || command.empty()) {
938 lyxerr << "Unable to parse \""
939 << argument << '"' << endl;
942 if (target != "printer" && target != "file") {
943 lyxerr << "Unrecognized target \""
944 << target << '"' << endl;
948 if (!buffer->doExport("dvi", true)) {
949 showPrintError(buffer->absFileName());
953 // Push directory path.
954 string const path = buffer->temppath();
955 // Prevent the compiler from optimizing away p
959 // there are three cases here:
960 // 1. we print to a file
961 // 2. we print directly to a printer
962 // 3. we print using a spool command (print to file first)
965 string const dviname =
966 changeExtension(buffer->latexName(true), "dvi");
968 if (target == "printer") {
969 if (!lyxrc.print_spool_command.empty()) {
970 // case 3: print using a spool
971 string const psname =
972 changeExtension(dviname,".ps");
973 command += ' ' + lyxrc.print_to_file
976 + quoteName(dviname);
979 lyxrc.print_spool_command + ' ';
980 if (target_name != "default") {
981 command2 += lyxrc.print_spool_printerprefix
985 command2 += quoteName(psname);
987 // If successful, then spool command
988 res = one.startscript(
993 res = one.startscript(
994 Systemcall::DontWait,
997 // case 2: print directly to a printer
998 if (target_name != "default")
999 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
1000 res = one.startscript(
1001 Systemcall::DontWait,
1002 command + quoteName(dviname));
1006 // case 1: print to a file
1007 FileName const filename(makeAbsPath(target_name,
1008 buffer->filePath()));
1009 FileName const dvifile(makeAbsPath(dviname, path));
1010 if (filename.exists()) {
1011 docstring text = bformat(
1012 _("The file %1$s already exists.\n\n"
1013 "Do you want to overwrite that file?"),
1014 makeDisplayPath(filename.absFilename()));
1015 if (Alert::prompt(_("Overwrite file?"),
1016 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1019 command += ' ' + lyxrc.print_to_file
1020 + quoteName(filename.toFilesystemEncoding())
1022 + quoteName(dvifile.toFilesystemEncoding());
1023 res = one.startscript(Systemcall::DontWait,
1028 showPrintError(buffer->absFileName());
1032 // FIXME: There is need for a command-line import.
1034 case LFUN_BUFFER_IMPORT:
1039 case LFUN_BUFFER_AUTO_SAVE:
1043 case LFUN_RECONFIGURE:
1044 // argument is any additional parameter to the configure.py command
1045 reconfigure(lyx_view_, argument);
1048 case LFUN_HELP_OPEN: {
1050 theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
1051 string const arg = argument;
1053 setErrorMessage(from_utf8(N_("Missing argument")));
1056 FileName fname = i18nLibFileSearch("doc", arg, "lyx");
1058 fname = i18nLibFileSearch("examples", arg, "lyx");
1060 if (fname.empty()) {
1061 lyxerr << "LyX: unable to find documentation file `"
1062 << arg << "'. Bad installation?" << endl;
1065 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1066 makeDisplayPath(fname.absFilename())));
1067 Buffer * buf = lyx_view_->loadDocument(fname, false);
1069 buf->updateLabels();
1070 lyx_view_->setBuffer(buf);
1071 buf->errors("Parse");
1073 updateFlags = Update::None;
1077 // --- version control -------------------------------
1078 case LFUN_VC_REGISTER:
1079 LASSERT(lyx_view_ && buffer, /**/);
1080 if (!ensureBufferClean(view()))
1082 if (!buffer->lyxvc().inUse()) {
1083 if (buffer->lyxvc().registrer())
1086 updateFlags = Update::Force;
1089 case LFUN_VC_CHECK_IN:
1090 LASSERT(lyx_view_ && buffer, /**/);
1091 if (!ensureBufferClean(view()))
1093 if (buffer->lyxvc().inUse()
1094 && !buffer->isReadonly()) {
1095 setMessage(from_utf8(buffer->lyxvc().checkIn()));
1100 case LFUN_VC_CHECK_OUT:
1101 LASSERT(lyx_view_ && buffer, /**/);
1102 if (!ensureBufferClean(view()))
1104 if (buffer->lyxvc().inUse()) {
1105 setMessage(from_utf8(buffer->lyxvc().checkOut()));
1110 case LFUN_VC_REVERT:
1111 LASSERT(lyx_view_ && buffer, /**/);
1112 buffer->lyxvc().revert();
1116 case LFUN_VC_UNDO_LAST:
1117 LASSERT(lyx_view_ && buffer, /**/);
1118 buffer->lyxvc().undoLast();
1122 // --- lyxserver commands ----------------------------
1123 case LFUN_SERVER_GET_FILENAME:
1124 LASSERT(lyx_view_ && buffer, /**/);
1125 setMessage(from_utf8(buffer->absFileName()));
1126 LYXERR(Debug::INFO, "FNAME["
1127 << buffer->absFileName() << ']');
1130 case LFUN_SERVER_NOTIFY:
1131 dispatch_buffer = keyseq.print(KeySequence::Portable);
1132 theServer().notifyClient(to_utf8(dispatch_buffer));
1135 case LFUN_SERVER_GOTO_FILE_ROW: {
1136 LASSERT(lyx_view_, /**/);
1139 istringstream is(argument);
1140 is >> file_name >> row;
1142 bool loaded = false;
1143 if (prefixIs(file_name, package().temp_dir().absFilename()))
1144 // Needed by inverse dvi search. If it is a file
1145 // in tmpdir, call the apropriated function
1146 buf = theBufferList().getBufferFromTmp(file_name);
1148 // Must replace extension of the file to be .lyx
1149 // and get full path
1150 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1151 // Either change buffer or load the file
1152 if (theBufferList().exists(s))
1153 buf = theBufferList().getBuffer(s);
1155 buf = lyx_view_->loadDocument(s);
1161 updateFlags = Update::None;
1165 buf->updateLabels();
1166 lyx_view_->setBuffer(buf);
1167 view()->setCursorFromRow(row);
1169 buf->errors("Parse");
1170 updateFlags = Update::FitCursor;
1175 case LFUN_DIALOG_SHOW_NEW_INSET: {
1176 LASSERT(lyx_view_, /**/);
1177 string const name = cmd.getArg(0);
1178 InsetCode code = insetCode(name);
1179 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1180 bool insetCodeOK = true;
1189 case HYPERLINK_CODE: {
1190 InsetCommandParams p(code);
1191 data = InsetCommand::params2string(name, p);
1194 case INCLUDE_CODE: {
1195 // data is the include type: one of "include",
1196 // "input", "verbatiminput" or "verbatiminput*"
1198 // default type is requested
1200 InsetCommandParams p(INCLUDE_CODE, data);
1201 data = InsetCommand::params2string("include", p);
1205 // \c data == "Boxed" || "Frameless" etc
1206 InsetBoxParams p(data);
1207 data = InsetBox::params2string(p);
1211 InsetBranchParams p;
1212 data = InsetBranch::params2string(p);
1216 InsetCommandParams p(CITE_CODE);
1217 data = InsetCommand::params2string(name, p);
1221 data = InsetERT::params2string(InsetCollapsable::Open);
1224 case EXTERNAL_CODE: {
1225 InsetExternalParams p;
1226 data = InsetExternal::params2string(p, *buffer);
1231 data = InsetFloat::params2string(p);
1234 case LISTINGS_CODE: {
1235 InsetListingsParams p;
1236 data = InsetListings::params2string(p);
1239 case GRAPHICS_CODE: {
1240 InsetGraphicsParams p;
1241 data = InsetGraphics::params2string(p, *buffer);
1246 data = InsetNote::params2string(p);
1249 case PHANTOM_CODE: {
1250 InsetPhantomParams p;
1251 data = InsetPhantom::params2string(p);
1256 data = InsetSpace::params2string(p);
1261 data = InsetVSpace::params2string(space);
1266 data = InsetWrap::params2string(p);
1270 lyxerr << "Inset type '" << name <<
1271 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1272 insetCodeOK = false;
1274 } // end switch(code)
1276 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1280 case LFUN_CITATION_INSERT: {
1281 LASSERT(lyx_view_, /**/);
1282 if (!argument.empty()) {
1283 // we can have one optional argument, delimited by '|'
1284 // citation-insert <key>|<text_before>
1285 // this should be enhanced to also support text_after
1286 // and citation style
1287 string arg = argument;
1289 if (contains(argument, "|")) {
1290 arg = token(argument, '|', 0);
1291 opt1 = token(argument, '|', 1);
1293 InsetCommandParams icp(CITE_CODE);
1294 icp["key"] = from_utf8(arg);
1296 icp["before"] = from_utf8(opt1);
1297 string icstr = InsetCommand::params2string("citation", icp);
1298 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1301 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1305 case LFUN_BUFFER_CHILD_OPEN: {
1306 LASSERT(lyx_view_ && buffer, /**/);
1307 FileName filename = makeAbsPath(argument, buffer->filePath());
1308 view()->saveBookmark(false);
1310 bool parsed = false;
1311 if (theBufferList().exists(filename)) {
1312 child = theBufferList().getBuffer(filename);
1314 setMessage(bformat(_("Opening child document %1$s..."),
1315 makeDisplayPath(filename.absFilename())));
1316 child = lyx_view_->loadDocument(filename, false);
1320 // Set the parent name of the child document.
1321 // This makes insertion of citations and references in the child work,
1322 // when the target is in the parent or another child document.
1323 child->setParent(buffer);
1324 child->masterBuffer()->updateLabels();
1325 lyx_view_->setBuffer(child);
1327 child->errors("Parse");
1330 // If a screen update is required (in case where auto_open is false),
1331 // setBuffer() would have taken care of it already. Otherwise we shall
1332 // reset the update flag because it can cause a circular problem.
1334 updateFlags = Update::None;
1338 case LFUN_CURSOR_FOLLOWS_SCROLLBAR_TOGGLE:
1339 LASSERT(lyx_view_, /**/);
1340 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1343 case LFUN_KEYMAP_OFF:
1344 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1345 lyx_view_->view()->getIntl().keyMapOn(false);
1348 case LFUN_KEYMAP_PRIMARY:
1349 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1350 lyx_view_->view()->getIntl().keyMapPrim();
1353 case LFUN_KEYMAP_SECONDARY:
1354 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1355 lyx_view_->view()->getIntl().keyMapSec();
1358 case LFUN_KEYMAP_TOGGLE:
1359 LASSERT(lyx_view_ && lyx_view_->view(), /**/);
1360 lyx_view_->view()->getIntl().toggleKeyMap();
1366 string rest = split(argument, countstr, ' ');
1367 istringstream is(countstr);
1370 //lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1371 for (int i = 0; i < count; ++i)
1372 dispatch(lyxaction.lookupFunc(rest));
1376 case LFUN_COMMAND_SEQUENCE: {
1377 // argument contains ';'-terminated commands
1378 string arg = argument;
1379 if (theBufferList().isLoaded(buffer))
1380 buffer->undo().beginUndoGroup();
1381 while (!arg.empty()) {
1383 arg = split(arg, first, ';');
1384 FuncRequest func(lyxaction.lookupFunc(first));
1385 func.origin = cmd.origin;
1388 if (theBufferList().isLoaded(buffer))
1389 buffer->undo().endUndoGroup();
1393 case LFUN_COMMAND_ALTERNATIVES: {
1394 // argument contains ';'-terminated commands
1395 string arg = argument;
1396 while (!arg.empty()) {
1398 arg = split(arg, first, ';');
1399 FuncRequest func(lyxaction.lookupFunc(first));
1400 func.origin = cmd.origin;
1401 FuncStatus stat = getStatus(func);
1402 if (stat.enabled()) {
1412 if (theTopLevelCmdDef().lock(argument, func)) {
1413 func.origin = cmd.origin;
1415 theTopLevelCmdDef().release(argument);
1417 if (func.action == LFUN_UNKNOWN_ACTION) {
1418 // unknown command definition
1419 lyxerr << "Warning: unknown command definition `"
1423 // recursion detected
1424 lyxerr << "Warning: Recursion in the command definition `"
1425 << argument << "' detected"
1432 case LFUN_PREFERENCES_SAVE: {
1433 lyxrc.write(makeAbsPath("preferences",
1434 package().user_support().absFilename()),
1440 LASSERT(lyx_view_, /**/);
1441 lyx_view_->message(from_utf8(argument));
1444 case LFUN_BUFFER_LANGUAGE: {
1445 LASSERT(lyx_view_, /**/);
1446 Language const * oldL = buffer->params().language;
1447 Language const * newL = languages.getLanguage(argument);
1448 if (!newL || oldL == newL)
1451 if (oldL->rightToLeft() == newL->rightToLeft()
1452 && !buffer->isMultiLingual())
1453 buffer->changeLanguage(oldL, newL);
1457 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1458 string const fname =
1459 addName(addPath(package().user_support().absFilename(), "templates/"),
1461 Buffer defaults(fname);
1463 istringstream ss(argument);
1466 int const unknown_tokens = defaults.readHeader(lex);
1468 if (unknown_tokens != 0) {
1469 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1470 << unknown_tokens << " unknown token"
1471 << (unknown_tokens == 1 ? "" : "s")
1475 if (defaults.writeFile(FileName(defaults.absFileName())))
1476 setMessage(bformat(_("Document defaults saved in %1$s"),
1477 makeDisplayPath(fname)));
1479 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1483 case LFUN_BUFFER_PARAMS_APPLY: {
1484 LASSERT(lyx_view_, /**/);
1486 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1487 Cursor & cur = view()->cursor();
1488 cur.recordUndoFullDocument();
1490 istringstream ss(argument);
1493 int const unknown_tokens = buffer->readHeader(lex);
1495 if (unknown_tokens != 0) {
1496 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1497 << unknown_tokens << " unknown token"
1498 << (unknown_tokens == 1 ? "" : "s")
1502 updateLayout(oldClass, buffer);
1504 updateFlags = Update::Force | Update::FitCursor;
1505 // We are most certainly here because of a change in the document
1506 // It is then better to make sure that all dialogs are in sync with
1507 // current document settings. LyXView::restartCursor() achieve this.
1508 lyx_view_->restartCursor();
1512 case LFUN_LAYOUT_MODULES_CLEAR: {
1513 LASSERT(lyx_view_, /**/);
1514 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1515 view()->cursor().recordUndoFullDocument();
1516 buffer->params().clearLayoutModules();
1517 buffer->params().makeDocumentClass();
1518 updateLayout(oldClass, buffer);
1519 updateFlags = Update::Force | Update::FitCursor;
1523 case LFUN_LAYOUT_MODULE_ADD: {
1524 LASSERT(lyx_view_, /**/);
1525 BufferParams const & params = buffer->params();
1526 if (!params.moduleCanBeAdded(argument)) {
1527 LYXERR0("Module `" << argument <<
1528 "' cannot be added due to failed requirements or "
1529 "conflicts with installed modules.");
1532 DocumentClass const * const oldClass = params.documentClassPtr();
1533 view()->cursor().recordUndoFullDocument();
1534 buffer->params().addLayoutModule(argument);
1535 buffer->params().makeDocumentClass();
1536 updateLayout(oldClass, buffer);
1537 updateFlags = Update::Force | Update::FitCursor;
1541 case LFUN_TEXTCLASS_APPLY: {
1542 LASSERT(lyx_view_, /**/);
1544 if (!loadLayoutFile(argument, buffer->temppath()) &&
1545 !loadLayoutFile(argument, buffer->filePath()))
1548 LayoutFile const * old_layout = buffer->params().baseClass();
1549 LayoutFile const * new_layout = &(LayoutFileList::get()[argument]);
1551 if (old_layout == new_layout)
1555 //Save the old, possibly modular, layout for use in conversion.
1556 DocumentClass const * const oldDocClass = buffer->params().documentClassPtr();
1557 view()->cursor().recordUndoFullDocument();
1558 buffer->params().setBaseClass(argument);
1559 buffer->params().makeDocumentClass();
1560 updateLayout(oldDocClass, buffer);
1561 updateFlags = Update::Force | Update::FitCursor;
1565 case LFUN_LAYOUT_RELOAD: {
1566 LASSERT(lyx_view_, /**/);
1567 DocumentClass const * const oldClass = buffer->params().documentClassPtr();
1568 LayoutFileIndex bc = buffer->params().baseClassID();
1569 LayoutFileList::get().reset(bc);
1570 buffer->params().setBaseClass(bc);
1571 buffer->params().makeDocumentClass();
1572 updateLayout(oldClass, buffer);
1573 updateFlags = Update::Force | Update::FitCursor;
1577 case LFUN_TEXTCLASS_LOAD:
1578 loadLayoutFile(argument, buffer->temppath()) ||
1579 loadLayoutFile(argument, buffer->filePath());
1582 case LFUN_LYXRC_APPLY: {
1583 LyXRC const lyxrc_orig = lyxrc;
1585 istringstream ss(argument);
1586 bool const success = lyxrc.read(ss) == 0;
1589 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1590 << "Unable to read lyxrc data"
1595 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1597 theApp()->resetGui();
1599 /// We force the redraw in any case because there might be
1600 /// some screen font changes.
1601 /// FIXME: only the current view will be updated. the Gui
1602 /// class is able to furnish the list of views.
1603 updateFlags = Update::Force;
1607 case LFUN_BOOKMARK_GOTO:
1608 // go to bookmark, open unopened file and switch to buffer if necessary
1609 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1610 updateFlags = Update::FitCursor;
1613 case LFUN_BOOKMARK_CLEAR:
1614 theSession().bookmarks().clear();
1617 case LFUN_VC_COMMAND: {
1618 string flag = cmd.getArg(0);
1619 if (buffer && contains(flag, 'R') && !ensureBufferClean(view()))
1622 if (contains(flag, 'M'))
1623 if (!Alert::askForText(message, _("LyX VC: Log Message")))
1626 string path = cmd.getArg(1);
1627 if (contains(path, "$$p") && buffer)
1628 path = subst(path, "$$p", buffer->filePath());
1629 LYXERR(Debug::LYXVC, "Directory: " << path);
1631 if (!pp.isReadableDirectory()) {
1632 lyxerr << _("Directory is not accessible.") << endl;
1635 support::PathChanger p(pp);
1637 string command = cmd.getArg(2);
1638 if (command.empty())
1641 command = subst(command, "$$i", buffer->absFileName());
1642 command = subst(command, "$$p", buffer->filePath());
1644 command = subst(command, "$$m", to_utf8(message));
1645 LYXERR(Debug::LYXVC, "Command: " << command);
1647 one.startscript(Systemcall::Wait, command);
1651 if (contains(flag, 'I'))
1652 buffer->markDirty();
1653 if (contains(flag, 'R'))
1660 LASSERT(theApp(), /**/);
1661 // Let the frontend dispatch its own actions.
1662 if (theApp()->dispatch(cmd))
1663 // Nothing more to do.
1666 // Everything below is only for active lyx_view_
1670 // Start an undo group. This may be needed for
1671 // some stuff like inset-apply on labels.
1672 if (theBufferList().isLoaded(buffer))
1673 buffer->undo().beginUndoGroup();
1675 // Let the current LyXView dispatch its own actions.
1676 if (lyx_view_->dispatch(cmd)) {
1677 if (lyx_view_->view()) {
1678 updateFlags = lyx_view_->view()->cursor().result().update();
1679 if (theBufferList().isLoaded(buffer))
1680 buffer->undo().endUndoGroup();
1685 LASSERT(lyx_view_->view(), /**/);
1687 // Let the current BufferView dispatch its own actions.
1688 if (view()->dispatch(cmd)) {
1689 // The BufferView took care of its own updates if needed.
1690 updateFlags = Update::None;
1691 if (theBufferList().isLoaded(buffer))
1692 buffer->undo().endUndoGroup();
1696 // Let the current Cursor dispatch its own actions.
1697 Cursor old = view()->cursor();
1698 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1699 cursorPosBeforeDispatchY_);
1700 view()->cursor().dispatch(cmd);
1702 // notify insets we just left
1703 if (view()->cursor() != old) {
1705 bool badcursor = notifyCursorLeavesOrEnters(old, view()->cursor());
1707 view()->cursor().fixIfBroken();
1710 if (theBufferList().isLoaded(buffer))
1711 buffer->undo().endUndoGroup();
1713 // update completion. We do it here and not in
1714 // processKeySym to avoid another redraw just for a
1715 // changed inline completion
1716 if (cmd.origin == FuncRequest::KEYBOARD) {
1717 if (cmd.action == LFUN_SELF_INSERT)
1718 lyx_view_->updateCompletion(view()->cursor(), true, true);
1719 else if (cmd.action == LFUN_CHAR_DELETE_BACKWARD)
1720 lyx_view_->updateCompletion(view()->cursor(), false, true);
1722 lyx_view_->updateCompletion(view()->cursor(), false, false);
1725 updateFlags = view()->cursor().result().update();
1728 // if we executed a mutating lfun, mark the buffer as dirty
1729 if (theBufferList().isLoaded(buffer) && flag.enabled()
1730 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1731 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1732 buffer->markDirty();
1734 if (lyx_view_ && lyx_view_->buffer()) {
1735 // BufferView::update() updates the ViewMetricsInfo and
1736 // also initializes the position cache for all insets in
1737 // (at least partially) visible top-level paragraphs.
1738 // We will redraw the screen only if needed.
1739 view()->processUpdateFlags(updateFlags);
1741 // Do we have a selection?
1742 theSelection().haveSelection(view()->cursor().selection());
1745 lyx_view_->restartCursor();
1749 // Some messages may already be translated, so we cannot use _()
1750 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1755 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1757 const bool verbose = (cmd.origin == FuncRequest::MENU
1758 || cmd.origin == FuncRequest::TOOLBAR
1759 || cmd.origin == FuncRequest::COMMANDBUFFER);
1761 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1762 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1764 lyx_view_->message(msg);
1768 docstring dispatch_msg = msg;
1769 if (!dispatch_msg.empty())
1770 dispatch_msg += ' ';
1772 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1774 bool argsadded = false;
1776 if (!cmd.argument().empty()) {
1777 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1778 comname += ' ' + cmd.argument();
1783 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd, KeySequence::ForGui);
1785 if (!shortcuts.empty())
1786 comname += ": " + shortcuts;
1787 else if (!argsadded && !cmd.argument().empty())
1788 comname += ' ' + cmd.argument();
1790 if (!comname.empty()) {
1791 comname = rtrim(comname);
1792 dispatch_msg += '(' + rtrim(comname) + ')';
1795 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1796 if (!dispatch_msg.empty())
1797 lyx_view_->message(dispatch_msg);
1801 void LyXFunc::reloadBuffer()
1803 FileName filename = lyx_view_->buffer()->fileName();
1804 // The user has already confirmed that the changes, if any, should
1805 // be discarded. So we just release the Buffer and don't call closeBuffer();
1806 theBufferList().release(lyx_view_->buffer());
1807 // if the lyx_view_ has been destroyed, create a new one
1809 theApp()->dispatch(FuncRequest(LFUN_WINDOW_NEW));
1810 Buffer * buf = lyx_view_->loadDocument(filename);
1811 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1814 buf->updateLabels();
1815 lyx_view_->setBuffer(buf);
1816 buf->errors("Parse");
1817 str = bformat(_("Document %1$s reloaded."), disp_fn);
1819 str = bformat(_("Could not reload document %1$s"), disp_fn);
1821 lyx_view_->message(str);
1824 // Each "lyx_view_" should have it's own message method. lyxview and
1825 // the minibuffer would use the minibuffer, but lyxserver would
1826 // send an ERROR signal to its client. Alejandro 970603
1827 // This function is bit problematic when it comes to NLS, to make the
1828 // lyx servers client be language indepenent we must not translate
1829 // strings sent to this func.
1830 void LyXFunc::setErrorMessage(docstring const & m) const
1832 dispatch_buffer = m;
1837 void LyXFunc::setMessage(docstring const & m) const
1839 dispatch_buffer = m;
1843 docstring LyXFunc::viewStatusMessage()
1845 // When meta-fake key is pressed, show the key sequence so far + "M-".
1847 return keyseq.print(KeySequence::ForGui) + "M-";
1849 // Else, when a non-complete key sequence is pressed,
1850 // show the available options.
1851 if (keyseq.length() > 0 && !keyseq.deleted())
1852 return keyseq.printOptions(true);
1854 LASSERT(lyx_view_, /**/);
1855 if (!lyx_view_->buffer())
1856 return _("Welcome to LyX!");
1858 return view()->cursor().currentState();
1862 BufferView * LyXFunc::view() const
1864 LASSERT(lyx_view_, /**/);
1865 return lyx_view_->view();
1869 bool LyXFunc::wasMetaKey() const
1871 return (meta_fake_bit != NoModifier);
1875 void LyXFunc::updateLayout(DocumentClass const * const oldlayout, Buffer * buf)
1877 lyx_view_->message(_("Converting document to new document class..."));
1879 StableDocIterator backcur(view()->cursor());
1880 ErrorList & el = buf->errorList("Class Switch");
1881 cap::switchBetweenClasses(
1882 oldlayout, buf->params().documentClassPtr(),
1883 static_cast<InsetText &>(buf->inset()), el);
1885 view()->setCursor(backcur.asDocIterator(buf));
1887 buf->errors("Class Switch");
1888 buf->updateLabels();
1894 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1896 // Why the switch you might ask. It is a trick to ensure that all
1897 // the elements in the LyXRCTags enum is handled. As you can see
1898 // there are no breaks at all. So it is just a huge fall-through.
1899 // The nice thing is that we will get a warning from the compiler
1900 // if we forget an element.
1901 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1903 case LyXRC::RC_ACCEPT_COMPOUND:
1904 case LyXRC::RC_ALT_LANG:
1905 case LyXRC::RC_PLAINTEXT_LINELEN:
1906 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1907 case LyXRC::RC_AUTOCORRECTION_MATH:
1908 case LyXRC::RC_AUTOREGIONDELETE:
1909 case LyXRC::RC_AUTORESET_OPTIONS:
1910 case LyXRC::RC_AUTOSAVE:
1911 case LyXRC::RC_AUTO_NUMBER:
1912 case LyXRC::RC_BACKUPDIR_PATH:
1913 case LyXRC::RC_BIBTEX_COMMAND:
1914 case LyXRC::RC_BINDFILE:
1915 case LyXRC::RC_CHECKLASTFILES:
1916 case LyXRC::RC_COMPLETION_CURSOR_TEXT:
1917 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1918 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1919 case LyXRC::RC_COMPLETION_INLINE_MATH:
1920 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1921 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1922 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1923 case LyXRC::RC_COMPLETION_POPUP_MATH:
1924 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1925 case LyXRC::RC_USELASTFILEPOS:
1926 case LyXRC::RC_LOADSESSION:
1927 case LyXRC::RC_CHKTEX_COMMAND:
1928 case LyXRC::RC_CONVERTER:
1929 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1930 case LyXRC::RC_COPIER:
1931 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1932 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1933 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1934 case LyXRC::RC_DATE_INSERT_FORMAT:
1935 case LyXRC::RC_DEFAULT_LANGUAGE:
1936 case LyXRC::RC_GUI_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_GROUP_LAYOUTS:
1952 case LyXRC::RC_INDEX_COMMAND:
1953 case LyXRC::RC_NOMENCL_COMMAND:
1954 case LyXRC::RC_INPUT:
1955 case LyXRC::RC_KBMAP:
1956 case LyXRC::RC_KBMAP_PRIMARY:
1957 case LyXRC::RC_KBMAP_SECONDARY:
1958 case LyXRC::RC_LABEL_INIT_LENGTH:
1959 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1960 case LyXRC::RC_LANGUAGE_AUTO_END:
1961 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1962 case LyXRC::RC_LANGUAGE_COMMAND_END:
1963 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1964 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1965 case LyXRC::RC_LANGUAGE_PACKAGE:
1966 case LyXRC::RC_LANGUAGE_USE_BABEL:
1967 case LyXRC::RC_MAC_LIKE_WORD_MOVEMENT:
1968 case LyXRC::RC_MACRO_EDIT_STYLE:
1969 case LyXRC::RC_MAKE_BACKUP:
1970 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1971 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1972 case LyXRC::RC_NUMLASTFILES:
1973 case LyXRC::RC_PATH_PREFIX:
1974 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1975 prependEnvPath("PATH", lyxrc.path_prefix);
1977 case LyXRC::RC_PERS_DICT:
1978 case LyXRC::RC_PREVIEW:
1979 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1980 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1981 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1982 case LyXRC::RC_PRINTCOPIESFLAG:
1983 case LyXRC::RC_PRINTER:
1984 case LyXRC::RC_PRINTEVENPAGEFLAG:
1985 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1986 case LyXRC::RC_PRINTFILEEXTENSION:
1987 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1988 case LyXRC::RC_PRINTODDPAGEFLAG:
1989 case LyXRC::RC_PRINTPAGERANGEFLAG:
1990 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1991 case LyXRC::RC_PRINTPAPERFLAG:
1992 case LyXRC::RC_PRINTREVERSEFLAG:
1993 case LyXRC::RC_PRINTSPOOL_COMMAND:
1994 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1995 case LyXRC::RC_PRINTTOFILE:
1996 case LyXRC::RC_PRINTTOPRINTER:
1997 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1998 case LyXRC::RC_PRINT_COMMAND:
1999 case LyXRC::RC_RTL_SUPPORT:
2000 case LyXRC::RC_SCREEN_DPI:
2001 case LyXRC::RC_SCREEN_FONT_ROMAN:
2002 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
2003 case LyXRC::RC_SCREEN_FONT_SANS:
2004 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
2005 case LyXRC::RC_SCREEN_FONT_SCALABLE:
2006 case LyXRC::RC_SCREEN_FONT_SIZES:
2007 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
2008 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
2009 case LyXRC::RC_GEOMETRY_SESSION:
2010 case LyXRC::RC_SCREEN_ZOOM:
2011 case LyXRC::RC_SERVERPIPE:
2012 case LyXRC::RC_SET_COLOR:
2013 case LyXRC::RC_SHOW_BANNER:
2014 case LyXRC::RC_OPEN_BUFFERS_IN_TABS:
2015 case LyXRC::RC_SPELL_COMMAND:
2016 case LyXRC::RC_TEMPDIRPATH:
2017 case LyXRC::RC_TEMPLATEPATH:
2018 case LyXRC::RC_TEX_ALLOWS_SPACES:
2019 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2020 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2021 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2023 case LyXRC::RC_THESAURUSDIRPATH:
2024 case LyXRC::RC_UIFILE:
2025 case LyXRC::RC_USER_EMAIL:
2026 case LyXRC::RC_USER_NAME:
2027 case LyXRC::RC_USETEMPDIR:
2028 case LyXRC::RC_USE_ALT_LANG:
2029 case LyXRC::RC_USE_CONVERTER_CACHE:
2030 case LyXRC::RC_USE_ESC_CHARS:
2031 case LyXRC::RC_USE_INP_ENC:
2032 case LyXRC::RC_USE_PERS_DICT:
2033 case LyXRC::RC_USE_TOOLTIP:
2034 case LyXRC::RC_USE_PIXMAP_CACHE:
2035 case LyXRC::RC_USE_SPELL_LIB:
2036 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2037 case LyXRC::RC_SORT_LAYOUTS:
2038 case LyXRC::RC_FULL_SCREEN_LIMIT:
2039 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2040 case LyXRC::RC_FULL_SCREEN_TABBAR:
2041 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2042 case LyXRC::RC_FULL_SCREEN_WIDTH:
2043 case LyXRC::RC_VISUAL_CURSOR:
2044 case LyXRC::RC_VIEWER:
2045 case LyXRC::RC_LAST: