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.
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 "support/debug.h"
37 #include "DispatchResult.h"
39 #include "ErrorList.h"
41 #include "FuncRequest.h"
42 #include "FuncStatus.h"
43 #include "support/gettext.h"
44 #include "InsetIterator.h"
49 #include "LyXAction.h"
54 #include "Paragraph.h"
55 #include "ParagraphParameters.h"
56 #include "ParIterator.h"
60 #include "TextClassList.h"
61 #include "ToolbarBackend.h"
63 #include "insets/InsetBox.h"
64 #include "insets/InsetBranch.h"
65 #include "insets/InsetCommand.h"
66 #include "insets/InsetERT.h"
67 #include "insets/InsetExternal.h"
68 #include "insets/InsetFloat.h"
69 #include "insets/InsetListings.h"
70 #include "insets/InsetGraphics.h"
71 #include "insets/InsetInclude.h"
72 #include "insets/InsetNote.h"
73 #include "insets/InsetTabular.h"
74 #include "insets/InsetVSpace.h"
75 #include "insets/InsetWrap.h"
77 #include "frontends/alert.h"
78 #include "frontends/Application.h"
79 #include "frontends/KeySymbol.h"
80 #include "frontends/LyXView.h"
81 #include "frontends/Selection.h"
83 #include "support/environment.h"
84 #include "support/FileName.h"
85 #include "support/filetools.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"
96 using namespace lyx::support;
100 using frontend::LyXView;
102 namespace Alert = frontend::Alert;
104 extern bool quitting;
110 // This function runs "configure" and then rereads lyx.defaults to
111 // reconfigure the automatic settings.
112 void reconfigure(LyXView & lv, string const & option)
114 // emit message signal.
115 lv.message(_("Running configure..."));
117 // Run configure in user lyx directory
118 PathChanger p(package().user_support());
119 string configure_command = package().configure_command();
120 configure_command += option;
122 int ret = one.startscript(Systemcall::Wait, configure_command);
124 // 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 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
158 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
159 BOOST_ASSERT(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 (!quitting && 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(kb_action action)
223 char_type c = encoded_last_key;
228 BOOST_ASSERT(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);
240 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
242 BOOST_ASSERT(lyx_view_);
243 if (!LyX::ref().session().bookmarks().isValid(idx))
245 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
246 BOOST_ASSERT(!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");
299 lyx_view_->restartCursor();
303 //Encoding const * encoding = view()->cursor().getEncoding();
304 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
305 // FIXME: encoded_last_key shadows the member variable of the same
306 // name. Is that intended?
307 char_type encoded_last_key = keysym.getUCSEncoded();
309 // Do a one-deep top-level lookup for
310 // cancel and meta-fake keys. RVDK_PATCH_5
311 cancel_meta_seq.reset();
313 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
314 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
316 // When not cancel or meta-fake, do the normal lookup.
317 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
318 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
319 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
320 // remove Caps Lock and Mod2 as a modifiers
321 func = keyseq.addkey(keysym, (state | meta_fake_bit));
322 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
325 // Dont remove this unless you know what you are doing.
326 meta_fake_bit = NoModifier;
328 // Can this happen now ?
329 if (func.action == LFUN_NOACTION)
330 func = FuncRequest(LFUN_COMMAND_PREFIX);
332 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
333 << keyseq.print(KeySequence::Portable) << ']');
335 // already here we know if it any point in going further
336 // why not return already here if action == -1 and
337 // num_bytes == 0? (Lgb)
339 if (keyseq.length() > 1)
340 lyx_view_->message(keyseq.print(KeySequence::ForGui));
343 // Maybe user can only reach the key via holding down shift.
344 // Let's see. But only if shift is the only modifier
345 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
346 LYXERR(Debug::KEY, "Trying without shift");
347 func = keyseq.addkey(keysym, NoModifier);
348 LYXERR(Debug::KEY, "Action now " << func.action);
351 if (func.action == LFUN_UNKNOWN_ACTION) {
352 // Hmm, we didn't match any of the keysequences. See
353 // if it's normal insertable text not already covered
355 if (keysym.isText() && keyseq.length() == 1) {
356 LYXERR(Debug::KEY, "isText() is true, inserting.");
357 func = FuncRequest(LFUN_SELF_INSERT,
358 FuncRequest::KEYBOARD);
360 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
361 lyx_view_->message(_("Unknown function."));
362 lyx_view_->restartCursor();
367 if (func.action == LFUN_SELF_INSERT) {
368 if (encoded_last_key != 0) {
369 docstring const arg(1, encoded_last_key);
370 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
371 FuncRequest::KEYBOARD));
372 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
378 lyx_view_->restartCursor();
382 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
384 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
387 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
389 if (cmd.action == LFUN_NOACTION) {
390 flag.message(from_utf8(N_("Nothing to do")));
395 switch (cmd.action) {
396 case LFUN_UNKNOWN_ACTION:
397 #ifndef HAVE_LIBAIKSAURUS
398 case LFUN_THESAURUS_ENTRY:
408 if (flag.unknown()) {
409 flag.message(from_utf8(N_("Unknown action")));
413 if (!flag.enabled()) {
414 if (flag.message().empty())
415 flag.message(from_utf8(N_("Command disabled")));
419 // Check whether we need a buffer
420 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
422 flag.message(from_utf8(N_("Command not allowed with"
423 "out any document open")));
428 // I would really like to avoid having this switch and rather try to
429 // encode this in the function itself.
430 // -- And I'd rather let an inset decide which LFUNs it is willing
431 // to handle (Andre')
433 switch (cmd.action) {
435 // FIXME: these cases should be hidden in GuiApplication::getStatus().
436 case LFUN_WINDOW_CLOSE:
438 return theApp()->getStatus(cmd);
442 // FIXME: these cases should be hidden in GuiView::getStatus().
443 case LFUN_DIALOG_TOGGLE:
444 case LFUN_DIALOG_SHOW:
445 case LFUN_DIALOG_UPDATE:
446 if (cmd.argument() == "prefs"
447 || cmd.argument() == "aboutlyx")
450 return lyx_view_->getStatus(cmd);
455 case LFUN_TOOLBAR_TOGGLE:
457 case LFUN_INSET_APPLY:
458 case LFUN_BUFFER_WRITE:
459 case LFUN_BUFFER_WRITE_AS:
461 return lyx_view_->getStatus(cmd);
465 case LFUN_BUFFER_TOGGLE_READ_ONLY:
466 flag.setOnOff(buf->isReadonly());
469 case LFUN_BUFFER_SWITCH:
470 // toggle on the current buffer, but do not toggle off
471 // the other ones (is that a good idea?)
472 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
476 case LFUN_BUFFER_EXPORT:
477 enable = cmd.argument() == "custom"
478 || buf->isExportable(to_utf8(cmd.argument()));
481 case LFUN_BUFFER_CHKTEX:
482 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
485 case LFUN_BUILD_PROGRAM:
486 enable = buf->isExportable("program");
489 case LFUN_VC_REGISTER:
490 enable = !buf->lyxvc().inUse();
492 case LFUN_VC_CHECK_IN:
493 enable = buf->lyxvc().inUse() && !buf->isReadonly();
495 case LFUN_VC_CHECK_OUT:
496 enable = buf->lyxvc().inUse() && buf->isReadonly();
499 case LFUN_VC_UNDO_LAST:
500 enable = buf->lyxvc().inUse();
502 case LFUN_BUFFER_RELOAD:
503 enable = !buf->isUnnamed() && buf->fileName().exists()
504 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
507 case LFUN_CITATION_INSERT: {
508 FuncRequest fr(LFUN_INSET_INSERT, "citation");
509 enable = getStatus(fr).enabled();
513 // This could be used for the no-GUI version. The GUI version is handled in
514 // LyXView::getStatus(). See above.
516 case LFUN_BUFFER_WRITE:
517 case LFUN_BUFFER_WRITE_AS: {
518 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
519 enable = b && (b->isUnnamed() || !b->isClean());
524 case LFUN_BUFFER_WRITE_ALL: {
525 // We enable the command only if there are some modified buffers
526 Buffer * first = theBufferList().first();
531 // We cannot use a for loop as the buffer list is a cycle.
537 b = theBufferList().next(b);
538 } while (b != first);
542 case LFUN_BOOKMARK_GOTO: {
543 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
544 enable = LyX::ref().session().bookmarks().isValid(num);
548 case LFUN_BOOKMARK_CLEAR:
549 enable = LyX::ref().session().bookmarks().size() > 0;
552 // this one is difficult to get right. As a half-baked
553 // solution, we consider only the first action of the sequence
554 case LFUN_COMMAND_SEQUENCE: {
555 // argument contains ';'-terminated commands
556 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
557 FuncRequest func(lyxaction.lookupFunc(firstcmd));
558 func.origin = cmd.origin;
559 flag = getStatus(func);
565 string name = to_utf8(cmd.argument());
566 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
567 func.origin = cmd.origin;
568 flag = getStatus(func);
569 LyX::ref().topLevelCmdDef().release(name);
571 // catch recursion or unknown command definiton
572 // all operations until the recursion or unknown command
573 // definiton occures are performed, so set the state to enabled
579 case LFUN_BUFFER_NEW:
580 case LFUN_BUFFER_NEW_TEMPLATE:
581 case LFUN_WORD_FIND_FORWARD:
582 case LFUN_WORD_FIND_BACKWARD:
583 case LFUN_COMMAND_PREFIX:
584 case LFUN_COMMAND_EXECUTE:
586 case LFUN_META_PREFIX:
587 case LFUN_BUFFER_CLOSE:
588 case LFUN_BUFFER_UPDATE:
589 case LFUN_BUFFER_VIEW:
590 case LFUN_MASTER_BUFFER_UPDATE:
591 case LFUN_MASTER_BUFFER_VIEW:
592 case LFUN_BUFFER_IMPORT:
593 case LFUN_BUFFER_AUTO_SAVE:
594 case LFUN_RECONFIGURE:
598 case LFUN_DROP_LAYOUTS_CHOICE:
600 case LFUN_SERVER_GET_NAME:
601 case LFUN_SERVER_NOTIFY:
602 case LFUN_SERVER_GOTO_FILE_ROW:
603 case LFUN_DIALOG_HIDE:
604 case LFUN_DIALOG_DISCONNECT_INSET:
605 case LFUN_BUFFER_CHILD_OPEN:
606 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
607 case LFUN_KEYMAP_OFF:
608 case LFUN_KEYMAP_PRIMARY:
609 case LFUN_KEYMAP_SECONDARY:
610 case LFUN_KEYMAP_TOGGLE:
612 case LFUN_BUFFER_EXPORT_CUSTOM:
613 case LFUN_BUFFER_PRINT:
614 case LFUN_PREFERENCES_SAVE:
615 case LFUN_SCREEN_FONT_UPDATE:
618 case LFUN_EXTERNAL_EDIT:
619 case LFUN_GRAPHICS_EDIT:
620 case LFUN_ALL_INSETS_TOGGLE:
621 case LFUN_BUFFER_LANGUAGE:
622 case LFUN_TEXTCLASS_APPLY:
623 case LFUN_TEXTCLASS_LOAD:
624 case LFUN_BUFFER_SAVE_AS_DEFAULT:
625 case LFUN_BUFFER_PARAMS_APPLY:
626 case LFUN_LAYOUT_MODULES_CLEAR:
627 case LFUN_LAYOUT_MODULE_ADD:
628 case LFUN_LAYOUT_RELOAD:
629 case LFUN_LYXRC_APPLY:
630 case LFUN_BUFFER_NEXT:
631 case LFUN_BUFFER_PREVIOUS:
632 case LFUN_WINDOW_NEW:
634 // these are handled in our dispatch()
642 if (!getLocalStatus(view()->cursor(), cmd, flag))
643 flag = view()->getStatus(cmd);
649 // Can we use a readonly buffer?
650 if (buf && buf->isReadonly()
651 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
652 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
653 flag.message(from_utf8(N_("Document is read-only")));
657 // Are we in a DELETED change-tracking region?
659 && lookupChangeType(view()->cursor(), true) == Change::DELETED
660 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
661 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
662 flag.message(from_utf8(N_("This portion of the document is deleted.")));
666 // the default error message if we disable the command
667 if (!flag.enabled() && flag.message().empty())
668 flag.message(from_utf8(N_("Command disabled")));
674 bool LyXFunc::ensureBufferClean(BufferView * bv)
676 Buffer & buf = bv->buffer();
680 docstring const file = buf.fileName().displayName(30);
681 docstring text = bformat(_("The document %1$s has unsaved "
682 "changes.\n\nDo you want to save "
683 "the document?"), file);
684 int const ret = Alert::prompt(_("Save changed document?"),
685 text, 0, 1, _("&Save"),
689 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
691 return buf.isClean();
697 void showPrintError(string const & name)
699 docstring str = bformat(_("Could not print the document %1$s.\n"
700 "Check that your printer is set up correctly."),
701 makeDisplayPath(name, 50));
702 Alert::error(_("Print document failed"), str);
706 void loadTextClass(string const & name)
708 pair<bool, textclass_type> const tc_pair =
709 textclasslist.numberOfClass(name);
711 if (!tc_pair.first) {
712 lyxerr << "Document class \"" << name
713 << "\" does not exist."
718 textclass_type const tc = tc_pair.second;
720 if (!textclasslist[tc].load()) {
721 docstring s = bformat(_("The document class %1$s."
722 "could not be loaded."),
723 from_utf8(textclasslist[tc].name()));
724 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 kb_action 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 BOOST_ASSERT(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 BOOST_ASSERT(lyx_view_);
785 lyx_view_->message(keyseq.printOptions(true));
789 BOOST_ASSERT(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 BOOST_ASSERT(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_NEW:
815 lyx_view_->newDocument(argument, false);
816 updateFlags = Update::None;
819 case LFUN_BUFFER_NEW_TEMPLATE:
820 lyx_view_->newDocument(argument, true);
821 updateFlags = Update::None;
824 case LFUN_BUFFER_CLOSE:
826 updateFlags = Update::None;
829 case LFUN_BUFFER_RELOAD: {
830 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
831 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
832 docstring text = bformat(_("Any changes will be lost. Are you sure "
833 "you want to revert to the saved version of the document %1$s?"), file);
834 int const ret = Alert::prompt(_("Revert to saved document?"),
835 text, 1, 1, _("&Revert"), _("&Cancel"));
842 case LFUN_BUFFER_UPDATE:
843 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
844 lyx_view_->buffer()->doExport(argument, true);
847 case LFUN_BUFFER_VIEW:
848 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
849 lyx_view_->buffer()->preview(argument);
852 case LFUN_MASTER_BUFFER_UPDATE:
853 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
854 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
857 case LFUN_MASTER_BUFFER_VIEW:
858 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
859 lyx_view_->buffer()->masterBuffer()->preview(argument);
862 case LFUN_BUILD_PROGRAM:
863 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
864 lyx_view_->buffer()->doExport("program", true);
867 case LFUN_BUFFER_CHKTEX:
868 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
869 lyx_view_->buffer()->runChktex();
872 case LFUN_BUFFER_EXPORT:
873 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
874 if (argument == "custom")
875 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
877 lyx_view_->buffer()->doExport(argument, false);
880 case LFUN_BUFFER_EXPORT_CUSTOM: {
881 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
883 string command = split(argument, format_name, ' ');
884 Format const * format = formats.getFormat(format_name);
886 lyxerr << "Format \"" << format_name
887 << "\" not recognized!"
892 Buffer * buffer = lyx_view_->buffer();
894 // The name of the file created by the conversion process
897 // Output to filename
898 if (format->name() == "lyx") {
899 string const latexname = buffer->latexName(false);
900 filename = changeExtension(latexname,
901 format->extension());
902 filename = addName(buffer->temppath(), filename);
904 if (!buffer->writeFile(FileName(filename)))
908 buffer->doExport(format_name, true, filename);
911 // Substitute $$FName for filename
912 if (!contains(command, "$$FName"))
913 command = "( " + command + " ) < $$FName";
914 command = subst(command, "$$FName", filename);
916 // Execute the command in the background
918 call.startscript(Systemcall::DontWait, command);
922 case LFUN_BUFFER_PRINT: {
923 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
924 // FIXME: cmd.getArg() might fail if one of the arguments
925 // contains double quotes
926 string target = cmd.getArg(0);
927 string target_name = cmd.getArg(1);
928 string command = cmd.getArg(2);
931 || target_name.empty()
932 || command.empty()) {
933 lyxerr << "Unable to parse \""
934 << argument << '"' << endl;
937 if (target != "printer" && target != "file") {
938 lyxerr << "Unrecognized target \""
939 << target << '"' << endl;
943 Buffer * buffer = lyx_view_->buffer();
945 if (!buffer->doExport("dvi", true)) {
946 showPrintError(buffer->absFileName());
950 // Push directory path.
951 string const path = buffer->temppath();
952 // Prevent the compiler from optimizing away p
956 // there are three cases here:
957 // 1. we print to a file
958 // 2. we print directly to a printer
959 // 3. we print using a spool command (print to file first)
962 string const dviname =
963 changeExtension(buffer->latexName(true), "dvi");
965 if (target == "printer") {
966 if (!lyxrc.print_spool_command.empty()) {
967 // case 3: print using a spool
968 string const psname =
969 changeExtension(dviname,".ps");
970 command += ' ' + lyxrc.print_to_file
973 + quoteName(dviname);
976 lyxrc.print_spool_command + ' ';
977 if (target_name != "default") {
978 command2 += lyxrc.print_spool_printerprefix
982 command2 += quoteName(psname);
984 // If successful, then spool command
985 res = one.startscript(
990 res = one.startscript(
991 Systemcall::DontWait,
994 // case 2: print directly to a printer
995 if (target_name != "default")
996 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
997 res = one.startscript(
998 Systemcall::DontWait,
999 command + quoteName(dviname));
1003 // case 1: print to a file
1004 FileName const filename(makeAbsPath(target_name,
1005 lyx_view_->buffer()->filePath()));
1006 FileName const dvifile(makeAbsPath(dviname, path));
1007 if (filename.exists()) {
1008 docstring text = bformat(
1009 _("The file %1$s already exists.\n\n"
1010 "Do you want to overwrite that file?"),
1011 makeDisplayPath(filename.absFilename()));
1012 if (Alert::prompt(_("Overwrite file?"),
1013 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1016 command += ' ' + lyxrc.print_to_file
1017 + quoteName(filename.toFilesystemEncoding())
1019 + quoteName(dvifile.toFilesystemEncoding());
1020 res = one.startscript(Systemcall::DontWait,
1025 showPrintError(buffer->absFileName());
1029 // FIXME: There is need for a command-line import.
1031 case LFUN_BUFFER_IMPORT:
1036 case LFUN_BUFFER_AUTO_SAVE:
1037 lyx_view_->buffer()->autoSave();
1040 case LFUN_RECONFIGURE:
1041 BOOST_ASSERT(lyx_view_);
1042 // argument is any additional parameter to the configure.py command
1043 reconfigure(*lyx_view_, argument);
1046 case LFUN_HELP_OPEN: {
1047 BOOST_ASSERT(lyx_view_);
1048 string const arg = argument;
1050 setErrorMessage(from_ascii(N_("Missing argument")));
1053 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1054 if (fname.empty()) {
1055 lyxerr << "LyX: unable to find documentation file `"
1056 << arg << "'. Bad installation?" << endl;
1059 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1060 makeDisplayPath(fname.absFilename())));
1061 Buffer * buf = lyx_view_->loadDocument(fname, false);
1064 lyx_view_->setBuffer(buf);
1065 buf->errors("Parse");
1067 updateFlags = Update::None;
1071 // --- version control -------------------------------
1072 case LFUN_VC_REGISTER:
1073 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1074 if (!ensureBufferClean(view()))
1076 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1077 lyx_view_->buffer()->lyxvc().registrer();
1080 updateFlags = Update::Force;
1083 case LFUN_VC_CHECK_IN:
1084 BOOST_ASSERT(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().checkIn();
1094 case LFUN_VC_CHECK_OUT:
1095 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1096 if (!ensureBufferClean(view()))
1098 if (lyx_view_->buffer()->lyxvc().inUse()
1099 && lyx_view_->buffer()->isReadonly()) {
1100 lyx_view_->buffer()->lyxvc().checkOut();
1105 case LFUN_VC_REVERT:
1106 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1107 lyx_view_->buffer()->lyxvc().revert();
1111 case LFUN_VC_UNDO_LAST:
1112 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1113 lyx_view_->buffer()->lyxvc().undoLast();
1117 // --- buffers ----------------------------------------
1119 case LFUN_FILE_NEW: {
1120 BOOST_ASSERT(lyx_view_);
1122 string tmpname = split(argument, name, ':'); // Split filename
1123 Buffer * const b = newFile(name, tmpname);
1125 lyx_view_->setBuffer(b);
1126 updateFlags = Update::None;
1130 // --- lyxserver commands ----------------------------
1131 case LFUN_SERVER_GET_NAME:
1132 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1133 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1134 LYXERR(Debug::INFO, "FNAME["
1135 << lyx_view_->buffer()->absFileName() << ']');
1138 case LFUN_SERVER_NOTIFY:
1139 dispatch_buffer = keyseq.print(KeySequence::Portable);
1140 theServer().notifyClient(to_utf8(dispatch_buffer));
1143 case LFUN_SERVER_GOTO_FILE_ROW: {
1144 BOOST_ASSERT(lyx_view_);
1147 istringstream is(argument);
1148 is >> file_name >> row;
1150 bool loaded = false;
1151 if (prefixIs(file_name, package().temp_dir().absFilename()))
1152 // Needed by inverse dvi search. If it is a file
1153 // in tmpdir, call the apropriated function
1154 buf = theBufferList().getBufferFromTmp(file_name);
1156 // Must replace extension of the file to be .lyx
1157 // and get full path
1158 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1159 // Either change buffer or load the file
1160 if (theBufferList().exists(s.absFilename()))
1161 buf = theBufferList().getBuffer(s.absFilename());
1163 buf = lyx_view_->loadDocument(s);
1169 updateFlags = Update::None;
1174 lyx_view_->setBuffer(buf);
1175 view()->setCursorFromRow(row);
1177 buf->errors("Parse");
1178 updateFlags = Update::FitCursor;
1183 case LFUN_DIALOG_SHOW_NEW_INSET: {
1184 BOOST_ASSERT(lyx_view_);
1185 string const name = cmd.getArg(0);
1186 InsetCode code = insetCode(name);
1187 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1188 bool insetCodeOK = true;
1197 case HYPERLINK_CODE: {
1198 InsetCommandParams p(code);
1199 data = InsetCommandMailer::params2string(name, p);
1202 case INCLUDE_CODE: {
1203 // data is the include type: one of "include",
1204 // "input", "verbatiminput" or "verbatiminput*"
1206 // default type is requested
1208 InsetCommandParams p(INCLUDE_CODE, data);
1209 data = InsetCommandMailer::params2string("include", p);
1213 // \c data == "Boxed" || "Frameless" etc
1214 InsetBoxParams p(data);
1215 data = InsetBoxMailer::params2string(p);
1219 InsetBranchParams p;
1220 data = InsetBranchMailer::params2string(p);
1224 InsetCommandParams p(CITE_CODE);
1225 data = InsetCommandMailer::params2string(name, p);
1229 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1232 case EXTERNAL_CODE: {
1233 InsetExternalParams p;
1234 Buffer const & buffer = *lyx_view_->buffer();
1235 data = InsetExternalMailer::params2string(p, buffer);
1240 data = InsetFloatMailer::params2string(p);
1243 case LISTINGS_CODE: {
1244 InsetListingsParams p;
1245 data = InsetListingsMailer::params2string(p);
1248 case GRAPHICS_CODE: {
1249 InsetGraphicsParams p;
1250 Buffer const & buffer = *lyx_view_->buffer();
1251 data = InsetGraphicsMailer::params2string(p, buffer);
1256 data = InsetNoteMailer::params2string(p);
1261 data = InsetVSpaceMailer::params2string(space);
1266 data = InsetWrapMailer::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 BOOST_ASSERT(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 = InsetCommandMailer::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 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1307 Buffer * parent = lyx_view_->buffer();
1308 FileName filename = makeAbsPath(argument, parent->filePath());
1309 view()->saveBookmark(false);
1311 bool parsed = false;
1312 if (theBufferList().exists(filename.absFilename())) {
1313 child = theBufferList().getBuffer(filename.absFilename());
1315 setMessage(bformat(_("Opening child document %1$s..."),
1316 makeDisplayPath(filename.absFilename())));
1317 child = lyx_view_->loadDocument(filename, false);
1321 // Set the parent name of the child document.
1322 // This makes insertion of citations and references in the child work,
1323 // when the target is in the parent or another child document.
1324 child->setParent(parent);
1325 updateLabels(*child->masterBuffer());
1326 lyx_view_->setBuffer(child);
1328 child->errors("Parse");
1331 // If a screen update is required (in case where auto_open is false),
1332 // setBuffer() would have taken care of it already. Otherwise we shall
1333 // reset the update flag because it can cause a circular problem.
1335 updateFlags = Update::None;
1339 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1340 BOOST_ASSERT(lyx_view_);
1341 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1344 case LFUN_KEYMAP_OFF:
1345 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1346 lyx_view_->view()->getIntl().keyMapOn(false);
1349 case LFUN_KEYMAP_PRIMARY:
1350 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1351 lyx_view_->view()->getIntl().keyMapPrim();
1354 case LFUN_KEYMAP_SECONDARY:
1355 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1356 lyx_view_->view()->getIntl().keyMapSec();
1359 case LFUN_KEYMAP_TOGGLE:
1360 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1361 lyx_view_->view()->getIntl().toggleKeyMap();
1367 string rest = split(argument, countstr, ' ');
1368 istringstream is(countstr);
1371 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1372 for (int i = 0; i < count; ++i)
1373 dispatch(lyxaction.lookupFunc(rest));
1377 case LFUN_COMMAND_SEQUENCE: {
1378 // argument contains ';'-terminated commands
1379 string arg = argument;
1380 while (!arg.empty()) {
1382 arg = split(arg, first, ';');
1383 FuncRequest func(lyxaction.lookupFunc(first));
1384 func.origin = cmd.origin;
1392 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1393 func.origin = cmd.origin;
1395 LyX::ref().topLevelCmdDef().release(argument);
1397 if (func.action == LFUN_UNKNOWN_ACTION) {
1398 // unknown command definition
1399 lyxerr << "Warning: unknown command definition `"
1403 // recursion detected
1404 lyxerr << "Warning: Recursion in the command definition `"
1405 << argument << "' detected"
1412 case LFUN_PREFERENCES_SAVE: {
1413 lyxrc.write(makeAbsPath("preferences",
1414 package().user_support().absFilename()),
1419 case LFUN_SET_COLOR: {
1421 string const x11_name = split(argument, lyx_name, ' ');
1422 if (lyx_name.empty() || x11_name.empty()) {
1423 setErrorMessage(from_ascii(N_(
1424 "Syntax: set-color <lyx_name>"
1429 bool const graphicsbg_changed =
1430 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1431 x11_name != lcolor.getX11Name(Color_graphicsbg));
1433 if (!lcolor.setColor(lyx_name, x11_name)) {
1435 bformat(_("Set-color \"%1$s\" failed "
1436 "- color is undefined or "
1437 "may not be redefined"),
1438 from_utf8(lyx_name)));
1442 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1444 if (graphicsbg_changed) {
1445 // FIXME: The graphics cache no longer has a changeDisplay method.
1447 graphics::GCache::get().changeDisplay(true);
1454 BOOST_ASSERT(lyx_view_);
1455 lyx_view_->message(from_utf8(argument));
1458 case LFUN_EXTERNAL_EDIT: {
1459 BOOST_ASSERT(lyx_view_);
1460 FuncRequest fr(action, argument);
1461 InsetExternal().dispatch(view()->cursor(), fr);
1465 case LFUN_GRAPHICS_EDIT: {
1466 FuncRequest fr(action, argument);
1467 InsetGraphics().dispatch(view()->cursor(), fr);
1471 case LFUN_ALL_INSETS_TOGGLE: {
1472 BOOST_ASSERT(lyx_view_);
1474 string const name = split(argument, action, ' ');
1475 InsetCode const inset_code = insetCode(name);
1477 Cursor & cur = view()->cursor();
1478 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1480 Inset & inset = lyx_view_->buffer()->inset();
1481 InsetIterator it = inset_iterator_begin(inset);
1482 InsetIterator const end = inset_iterator_end(inset);
1483 for (; it != end; ++it) {
1484 if (!it->asInsetMath()
1485 && (inset_code == NO_CODE
1486 || inset_code == it->lyxCode())) {
1487 Cursor tmpcur = cur;
1488 tmpcur.pushBackward(*it);
1489 it->dispatch(tmpcur, fr);
1492 updateFlags = Update::Force | Update::FitCursor;
1496 case LFUN_BUFFER_LANGUAGE: {
1497 BOOST_ASSERT(lyx_view_);
1498 Buffer & buffer = *lyx_view_->buffer();
1499 Language const * oldL = buffer.params().language;
1500 Language const * newL = languages.getLanguage(argument);
1501 if (!newL || oldL == newL)
1504 if (oldL->rightToLeft() == newL->rightToLeft()
1505 && !buffer.isMultiLingual())
1506 buffer.changeLanguage(oldL, newL);
1510 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1511 string const fname =
1512 addName(addPath(package().user_support().absFilename(), "templates/"),
1514 Buffer defaults(fname);
1516 istringstream ss(argument);
1519 int const unknown_tokens = defaults.readHeader(lex);
1521 if (unknown_tokens != 0) {
1522 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1523 << unknown_tokens << " unknown token"
1524 << (unknown_tokens == 1 ? "" : "s")
1528 if (defaults.writeFile(FileName(defaults.absFileName())))
1529 setMessage(bformat(_("Document defaults saved in %1$s"),
1530 makeDisplayPath(fname)));
1532 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1536 case LFUN_BUFFER_PARAMS_APPLY: {
1537 BOOST_ASSERT(lyx_view_);
1538 biblio::CiteEngine const oldEngine =
1539 lyx_view_->buffer()->params().getEngine();
1541 Buffer * buffer = lyx_view_->buffer();
1543 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1545 Cursor & cur = view()->cursor();
1546 cur.recordUndoFullDocument();
1548 istringstream ss(argument);
1551 int const unknown_tokens = buffer->readHeader(lex);
1553 if (unknown_tokens != 0) {
1554 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1555 << unknown_tokens << " unknown token"
1556 << (unknown_tokens == 1 ? "" : "s")
1560 updateLayout(oldClass, buffer);
1562 biblio::CiteEngine const newEngine =
1563 lyx_view_->buffer()->params().getEngine();
1565 if (oldEngine != newEngine) {
1566 FuncRequest fr(LFUN_INSET_REFRESH);
1568 Inset & inset = lyx_view_->buffer()->inset();
1569 InsetIterator it = inset_iterator_begin(inset);
1570 InsetIterator const end = inset_iterator_end(inset);
1571 for (; it != end; ++it)
1572 if (it->lyxCode() == CITE_CODE)
1573 it->dispatch(cur, fr);
1576 updateFlags = Update::Force | Update::FitCursor;
1577 // We are here most certainaly because of a change in the document
1578 // It is then better to make sure that all dialogs are in sync
1579 // with current document settings. LyXView::restartCursor() achieve this.
1580 lyx_view_->restartCursor();
1584 case LFUN_LAYOUT_MODULES_CLEAR: {
1585 BOOST_ASSERT(lyx_view_);
1586 Buffer * buffer = lyx_view_->buffer();
1587 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1588 view()->cursor().recordUndoFullDocument();
1589 buffer->params().clearLayoutModules();
1590 buffer->params().makeTextClass();
1591 updateLayout(oldClass, buffer);
1592 updateFlags = Update::Force | Update::FitCursor;
1596 case LFUN_LAYOUT_MODULE_ADD: {
1597 BOOST_ASSERT(lyx_view_);
1598 Buffer * buffer = lyx_view_->buffer();
1599 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1600 view()->cursor().recordUndoFullDocument();
1601 buffer->params().addLayoutModule(argument);
1602 buffer->params().makeTextClass();
1603 updateLayout(oldClass, buffer);
1604 updateFlags = Update::Force | Update::FitCursor;
1608 case LFUN_TEXTCLASS_APPLY: {
1609 BOOST_ASSERT(lyx_view_);
1610 Buffer * buffer = lyx_view_->buffer();
1612 loadTextClass(argument);
1614 pair<bool, textclass_type> const tc_pair =
1615 textclasslist.numberOfClass(argument);
1620 textclass_type const old_class = buffer->params().getBaseClass();
1621 textclass_type const new_class = tc_pair.second;
1623 if (old_class == new_class)
1627 //Save the old, possibly modular, layout for use in conversion.
1628 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1629 view()->cursor().recordUndoFullDocument();
1630 buffer->params().setBaseClass(new_class);
1631 buffer->params().makeTextClass();
1632 updateLayout(oldClass, buffer);
1633 updateFlags = Update::Force | Update::FitCursor;
1637 case LFUN_LAYOUT_RELOAD: {
1638 BOOST_ASSERT(lyx_view_);
1639 Buffer * buffer = lyx_view_->buffer();
1640 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1641 textclass_type const tc = buffer->params().getBaseClass();
1642 textclasslist.reset(tc);
1643 buffer->params().setBaseClass(tc);
1644 buffer->params().makeTextClass();
1645 updateLayout(oldClass, buffer);
1646 updateFlags = Update::Force | Update::FitCursor;
1650 case LFUN_TEXTCLASS_LOAD:
1651 loadTextClass(argument);
1654 case LFUN_LYXRC_APPLY: {
1655 LyXRC const lyxrc_orig = lyxrc;
1657 istringstream ss(argument);
1658 bool const success = lyxrc.read(ss) == 0;
1661 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1662 << "Unable to read lyxrc data"
1667 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1669 theApp()->resetGui();
1671 /// We force the redraw in any case because there might be
1672 /// some screen font changes.
1673 /// FIXME: only the current view will be updated. the Gui
1674 /// class is able to furnish the list of views.
1675 updateFlags = Update::Force;
1679 case LFUN_BOOKMARK_GOTO:
1680 // go to bookmark, open unopened file and switch to buffer if necessary
1681 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1682 updateFlags = Update::FitCursor;
1685 case LFUN_BOOKMARK_CLEAR:
1686 LyX::ref().session().bookmarks().clear();
1690 BOOST_ASSERT(theApp());
1691 // Let the frontend dispatch its own actions.
1692 if (theApp()->dispatch(cmd))
1693 // Nothing more to do.
1696 // Let the current LyXView dispatch its own actions.
1697 BOOST_ASSERT(lyx_view_);
1698 if (lyx_view_->dispatch(cmd)) {
1699 if (lyx_view_->view())
1700 updateFlags = lyx_view_->view()->cursor().result().update();
1704 BOOST_ASSERT(lyx_view_->view());
1705 // Let the current BufferView dispatch its own actions.
1706 if (view()->dispatch(cmd)) {
1707 // The BufferView took care of its own updates if needed.
1708 updateFlags = Update::None;
1712 // Let the current Cursor dispatch its own actions.
1713 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1714 cursorPosBeforeDispatchY_);
1715 view()->cursor().dispatch(cmd);
1716 updateFlags = view()->cursor().result().update();
1717 if (!view()->cursor().result().dispatched()) {
1718 // No update needed in this case (e.g. when reaching
1720 updateFlags = Update::None;
1724 if (lyx_view_ && lyx_view_->buffer()) {
1725 // BufferView::update() updates the ViewMetricsInfo and
1726 // also initializes the position cache for all insets in
1727 // (at least partially) visible top-level paragraphs.
1728 // We will redraw the screen only if needed.
1729 view()->processUpdateFlags(updateFlags);
1731 // if we executed a mutating lfun, mark the buffer as dirty
1733 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1734 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1735 lyx_view_->buffer()->markDirty();
1737 //Do we have a selection?
1738 theSelection().haveSelection(view()->cursor().selection());
1741 if (!quitting && lyx_view_) {
1742 // Some messages may already be translated, so we cannot use _()
1743 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1748 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1750 const bool verbose = (cmd.origin == FuncRequest::MENU
1751 || cmd.origin == FuncRequest::TOOLBAR
1752 || cmd.origin == FuncRequest::COMMANDBUFFER);
1754 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1755 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1757 lyx_view_->message(msg);
1761 docstring dispatch_msg = msg;
1762 if (!dispatch_msg.empty())
1763 dispatch_msg += ' ';
1765 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1767 bool argsadded = false;
1769 if (!cmd.argument().empty()) {
1770 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1771 comname += ' ' + cmd.argument();
1776 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1778 if (!shortcuts.empty())
1779 comname += ": " + shortcuts;
1780 else if (!argsadded && !cmd.argument().empty())
1781 comname += ' ' + cmd.argument();
1783 if (!comname.empty()) {
1784 comname = rtrim(comname);
1785 dispatch_msg += '(' + rtrim(comname) + ')';
1788 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1789 if (!dispatch_msg.empty())
1790 lyx_view_->message(dispatch_msg);
1794 void LyXFunc::closeBuffer()
1796 // goto bookmark to update bookmark pit.
1797 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1798 gotoBookmark(i+1, false, false);
1800 lyx_view_->closeBuffer();
1804 void LyXFunc::reloadBuffer()
1806 FileName filename = lyx_view_->buffer()->fileName();
1807 // The user has already confirmed that the changes, if any, should
1808 // be discarded. So we just release the Buffer and don't call closeBuffer();
1809 theBufferList().release(lyx_view_->buffer());
1810 Buffer * buf = lyx_view_->loadDocument(filename);
1811 docstring const disp_fn = makeDisplayPath(filename.absFilename());
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 const 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 BOOST_ASSERT(lyx_view_);
1855 if (!lyx_view_->buffer())
1856 return _("Welcome to LyX!");
1858 return view()->cursor().currentState();
1862 BufferView * LyXFunc::view() const
1864 BOOST_ASSERT(lyx_view_);
1865 return lyx_view_->view();
1869 bool LyXFunc::wasMetaKey() const
1871 return (meta_fake_bit != NoModifier);
1875 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1878 lyx_view_->message(_("Converting document to new document class..."));
1880 StableDocIterator backcur(view()->cursor());
1881 ErrorList & el = buffer->errorList("Class Switch");
1882 cap::switchBetweenClasses(
1883 oldlayout, buffer->params().getTextClassPtr(),
1884 static_cast<InsetText &>(buffer->inset()), el);
1886 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1888 buffer->errors("Class Switch");
1889 updateLabels(*buffer);
1895 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1897 // Why the switch you might ask. It is a trick to ensure that all
1898 // the elements in the LyXRCTags enum is handled. As you can see
1899 // there are no breaks at all. So it is just a huge fall-through.
1900 // The nice thing is that we will get a warning from the compiler
1901 // if we forget an element.
1902 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1904 case LyXRC::RC_ACCEPT_COMPOUND:
1905 case LyXRC::RC_ALT_LANG:
1906 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1907 case LyXRC::RC_PLAINTEXT_LINELEN:
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_USELASTFILEPOS:
1917 case LyXRC::RC_LOADSESSION:
1918 case LyXRC::RC_CHKTEX_COMMAND:
1919 case LyXRC::RC_CONVERTER:
1920 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1921 case LyXRC::RC_COPIER:
1922 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1923 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1924 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1925 case LyXRC::RC_DATE_INSERT_FORMAT:
1926 case LyXRC::RC_DEFAULT_LANGUAGE:
1927 case LyXRC::RC_DEFAULT_PAPERSIZE:
1928 case LyXRC::RC_DEFFILE:
1929 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1930 case LyXRC::RC_DISPLAY_GRAPHICS:
1931 case LyXRC::RC_DOCUMENTPATH:
1932 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1933 FileName path(lyxrc_new.document_path);
1934 if (path.exists() && path.isDirectory())
1935 package().document_dir() = FileName(lyxrc.document_path);
1937 case LyXRC::RC_ESC_CHARS:
1938 case LyXRC::RC_EXAMPLEPATH:
1939 case LyXRC::RC_FONT_ENCODING:
1940 case LyXRC::RC_FORMAT:
1941 case LyXRC::RC_INDEX_COMMAND:
1942 case LyXRC::RC_INPUT:
1943 case LyXRC::RC_KBMAP:
1944 case LyXRC::RC_KBMAP_PRIMARY:
1945 case LyXRC::RC_KBMAP_SECONDARY:
1946 case LyXRC::RC_LABEL_INIT_LENGTH:
1947 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1948 case LyXRC::RC_LANGUAGE_AUTO_END:
1949 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1950 case LyXRC::RC_LANGUAGE_COMMAND_END:
1951 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1952 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1953 case LyXRC::RC_LANGUAGE_PACKAGE:
1954 case LyXRC::RC_LANGUAGE_USE_BABEL:
1955 case LyXRC::RC_MACRO_EDIT_STYLE:
1956 case LyXRC::RC_MAKE_BACKUP:
1957 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1958 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1959 case LyXRC::RC_NUMLASTFILES:
1960 case LyXRC::RC_PATH_PREFIX:
1961 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1962 prependEnvPath("PATH", lyxrc.path_prefix);
1964 case LyXRC::RC_PERS_DICT:
1965 case LyXRC::RC_PREVIEW:
1966 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1967 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1968 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1969 case LyXRC::RC_PRINTCOPIESFLAG:
1970 case LyXRC::RC_PRINTER:
1971 case LyXRC::RC_PRINTEVENPAGEFLAG:
1972 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1973 case LyXRC::RC_PRINTFILEEXTENSION:
1974 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1975 case LyXRC::RC_PRINTODDPAGEFLAG:
1976 case LyXRC::RC_PRINTPAGERANGEFLAG:
1977 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1978 case LyXRC::RC_PRINTPAPERFLAG:
1979 case LyXRC::RC_PRINTREVERSEFLAG:
1980 case LyXRC::RC_PRINTSPOOL_COMMAND:
1981 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1982 case LyXRC::RC_PRINTTOFILE:
1983 case LyXRC::RC_PRINTTOPRINTER:
1984 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1985 case LyXRC::RC_PRINT_COMMAND:
1986 case LyXRC::RC_RTL_SUPPORT:
1987 case LyXRC::RC_SCREEN_DPI:
1988 case LyXRC::RC_SCREEN_FONT_ROMAN:
1989 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1990 case LyXRC::RC_SCREEN_FONT_SANS:
1991 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1992 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1993 case LyXRC::RC_SCREEN_FONT_SIZES:
1994 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1995 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1996 case LyXRC::RC_GEOMETRY_SESSION:
1997 case LyXRC::RC_SCREEN_ZOOM:
1998 case LyXRC::RC_SERVERPIPE:
1999 case LyXRC::RC_SET_COLOR:
2000 case LyXRC::RC_SHOW_BANNER:
2001 case LyXRC::RC_SPELL_COMMAND:
2002 case LyXRC::RC_TEMPDIRPATH:
2003 case LyXRC::RC_TEMPLATEPATH:
2004 case LyXRC::RC_TEX_ALLOWS_SPACES:
2005 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2006 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2007 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2009 case LyXRC::RC_UIFILE:
2010 case LyXRC::RC_USER_EMAIL:
2011 case LyXRC::RC_USER_NAME:
2012 case LyXRC::RC_USETEMPDIR:
2013 case LyXRC::RC_USE_ALT_LANG:
2014 case LyXRC::RC_USE_CONVERTER_CACHE:
2015 case LyXRC::RC_USE_ESC_CHARS:
2016 case LyXRC::RC_USE_INP_ENC:
2017 case LyXRC::RC_USE_PERS_DICT:
2018 case LyXRC::RC_USE_TOOLTIP:
2019 case LyXRC::RC_USE_PIXMAP_CACHE:
2020 case LyXRC::RC_USE_SPELL_LIB:
2021 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2022 case LyXRC::RC_SORT_LAYOUTS:
2023 case LyXRC::RC_VIEWER:
2024 case LyXRC::RC_LAST: