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/FileFilterList.h"
85 #include "support/FileName.h"
86 #include "support/filetools.h"
87 #include "support/lstrings.h"
88 #include "support/Path.h"
89 #include "support/Package.h"
90 #include "support/Systemcall.h"
91 #include "support/convert.h"
92 #include "support/os.h"
97 using namespace lyx::support;
101 using frontend::LyXView;
103 namespace Alert = frontend::Alert;
105 extern bool quitting;
111 // This function runs "configure" and then rereads lyx.defaults to
112 // reconfigure the automatic settings.
113 void reconfigure(LyXView & lv, string const & option)
115 // emit message signal.
116 lv.message(_("Running configure..."));
118 // Run configure in user lyx directory
119 PathChanger p(package().user_support());
120 string configure_command = package().configure_command();
121 configure_command += option;
123 int ret = one.startscript(Systemcall::Wait, configure_command);
125 // emit message signal.
126 lv.message(_("Reloading configuration..."));
127 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
128 // Re-read packages.lst
129 LaTeXFeatures::getAvailable();
132 Alert::information(_("System reconfiguration failed"),
133 _("The system reconfiguration has failed.\n"
134 "Default textclass is used but LyX may "
135 "not be able to work properly.\n"
136 "Please reconfigure again if needed."));
139 Alert::information(_("System reconfigured"),
140 _("The system has been reconfigured.\n"
141 "You need to restart LyX to make use of any\n"
142 "updated document class specifications."));
146 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
148 // Try to fix cursor in case it is broken.
149 cursor.fixIfBroken();
151 // This is, of course, a mess. Better create a new doc iterator and use
152 // this in Inset::getStatus. This might require an additional
153 // BufferView * arg, though (which should be avoided)
154 //Cursor safe = *this;
156 for ( ; cursor.depth(); cursor.pop()) {
157 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
158 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
159 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
160 BOOST_ASSERT(cursor.pos() <= cursor.lastpos());
162 // The inset's getStatus() will return 'true' if it made
163 // a definitive decision on whether it want to handle the
164 // request or not. The result of this decision is put into
165 // the 'status' parameter.
166 if (cursor.inset().getStatus(cursor, cmd, status)) {
175 /** Return the change status at cursor position, taking in account the
176 * status at each level of the document iterator (a table in a deleted
177 * footnote is deleted).
178 * When \param outer is true, the top slice is not looked at.
180 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
182 size_t const depth = dit.depth() - (outer ? 1 : 0);
184 for (size_t i = 0 ; i < depth ; ++i) {
185 CursorSlice const & slice = dit[i];
186 if (!slice.inset().inMathed()
187 && slice.pos() < slice.paragraph().size()) {
188 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
189 if (ch != Change::UNCHANGED)
193 return Change::UNCHANGED;
200 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
205 void LyXFunc::initKeySequences(KeyMap * kb)
207 keyseq = KeySequence(kb, kb);
208 cancel_meta_seq = KeySequence(kb, kb);
212 void LyXFunc::setLyXView(LyXView * lv)
214 if (!quitting && lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
215 // save current selection to the selection buffer to allow
216 // middle-button paste in another window
217 cap::saveSelection(lyx_view_->view()->cursor());
222 void LyXFunc::handleKeyFunc(kb_action action)
224 char_type c = encoded_last_key;
229 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
230 lyx_view_->view()->getIntl().getTransManager().deadkey(
231 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
232 // Need to clear, in case the minibuffer calls these
235 // copied verbatim from do_accent_char
236 view()->cursor().resetAnchor();
237 view()->processUpdateFlags(Update::FitCursor);
241 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
243 BOOST_ASSERT(lyx_view_);
244 if (!LyX::ref().session().bookmarks().isValid(idx))
246 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
247 BOOST_ASSERT(!bm.filename.empty());
248 string const file = bm.filename.absFilename();
249 // if the file is not opened, open it.
250 if (!theBufferList().exists(file)) {
252 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
256 // open may fail, so we need to test it again
257 if (!theBufferList().exists(file))
260 // if the current buffer is not that one, switch to it.
261 if (lyx_view_->buffer()->absFileName() != file) {
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");
300 lyx_view_->restartCursor();
304 //Encoding const * encoding = view()->cursor().getEncoding();
305 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
306 // FIXME: encoded_last_key shadows the member variable of the same
307 // name. Is that intended?
308 char_type encoded_last_key = keysym.getUCSEncoded();
310 // Do a one-deep top-level lookup for
311 // cancel and meta-fake keys. RVDK_PATCH_5
312 cancel_meta_seq.reset();
314 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
315 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
317 // When not cancel or meta-fake, do the normal lookup.
318 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
319 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
320 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
321 // remove Caps Lock and Mod2 as a modifiers
322 func = keyseq.addkey(keysym, (state | meta_fake_bit));
323 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
326 // Dont remove this unless you know what you are doing.
327 meta_fake_bit = NoModifier;
329 // Can this happen now ?
330 if (func.action == LFUN_NOACTION)
331 func = FuncRequest(LFUN_COMMAND_PREFIX);
333 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
334 << keyseq.print(KeySequence::Portable) << ']');
336 // already here we know if it any point in going further
337 // why not return already here if action == -1 and
338 // num_bytes == 0? (Lgb)
340 if (keyseq.length() > 1)
341 lyx_view_->message(keyseq.print(KeySequence::ForGui));
344 // Maybe user can only reach the key via holding down shift.
345 // Let's see. But only if shift is the only modifier
346 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
347 LYXERR(Debug::KEY, "Trying without shift");
348 func = keyseq.addkey(keysym, NoModifier);
349 LYXERR(Debug::KEY, "Action now " << func.action);
352 if (func.action == LFUN_UNKNOWN_ACTION) {
353 // Hmm, we didn't match any of the keysequences. See
354 // if it's normal insertable text not already covered
356 if (keysym.isText() && keyseq.length() == 1) {
357 LYXERR(Debug::KEY, "isText() is true, inserting.");
358 func = FuncRequest(LFUN_SELF_INSERT,
359 FuncRequest::KEYBOARD);
361 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
362 lyx_view_->message(_("Unknown function."));
363 lyx_view_->restartCursor();
368 if (func.action == LFUN_SELF_INSERT) {
369 if (encoded_last_key != 0) {
370 docstring const arg(1, encoded_last_key);
371 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
372 FuncRequest::KEYBOARD));
373 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
379 lyx_view_->restartCursor();
383 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
385 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
388 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
390 if (cmd.action == LFUN_NOACTION) {
391 flag.message(from_utf8(N_("Nothing to do")));
396 switch (cmd.action) {
397 case LFUN_UNKNOWN_ACTION:
398 #ifndef HAVE_LIBAIKSAURUS
399 case LFUN_THESAURUS_ENTRY:
409 if (flag.unknown()) {
410 flag.message(from_utf8(N_("Unknown action")));
414 if (!flag.enabled()) {
415 if (flag.message().empty())
416 flag.message(from_utf8(N_("Command disabled")));
420 // Check whether we need a buffer
421 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
423 flag.message(from_utf8(N_("Command not allowed with"
424 "out any document open")));
429 // I would really like to avoid having this switch and rather try to
430 // encode this in the function itself.
431 // -- And I'd rather let an inset decide which LFUNs it is willing
432 // to handle (Andre')
434 switch (cmd.action) {
436 // FIXME: these cases should be hidden in GuiApplication::getStatus().
437 case LFUN_WINDOW_CLOSE:
439 return theApp()->getStatus(cmd);
443 // FIXME: these cases should be hidden in GuiView::getStatus().
444 case LFUN_DIALOG_TOGGLE:
445 case LFUN_DIALOG_SHOW:
446 case LFUN_DIALOG_UPDATE:
447 if (cmd.argument() == "prefs"
448 || cmd.argument() == "aboutlyx")
451 return lyx_view_->getStatus(cmd);
456 case LFUN_TOOLBAR_TOGGLE:
457 case LFUN_MENUBAR_TOGGLE:
458 case LFUN_STATUSBAR_TOGGLE:
459 case LFUN_INSET_APPLY:
460 case LFUN_BUFFER_WRITE:
461 case LFUN_BUFFER_WRITE_AS:
463 return lyx_view_->getStatus(cmd);
467 case LFUN_BUFFER_TOGGLE_READ_ONLY:
468 flag.setOnOff(buf->isReadonly());
471 case LFUN_BUFFER_SWITCH:
472 // toggle on the current buffer, but do not toggle off
473 // the other ones (is that a good idea?)
474 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
478 case LFUN_BUFFER_EXPORT:
479 enable = cmd.argument() == "custom"
480 || buf->isExportable(to_utf8(cmd.argument()));
483 case LFUN_BUFFER_CHKTEX:
484 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
487 case LFUN_BUILD_PROGRAM:
488 enable = buf->isExportable("program");
491 case LFUN_VC_REGISTER:
492 enable = !buf->lyxvc().inUse();
494 case LFUN_VC_CHECK_IN:
495 enable = buf->lyxvc().inUse() && !buf->isReadonly();
497 case LFUN_VC_CHECK_OUT:
498 enable = buf->lyxvc().inUse() && buf->isReadonly();
501 case LFUN_VC_UNDO_LAST:
502 enable = buf->lyxvc().inUse();
504 case LFUN_BUFFER_RELOAD:
505 enable = !buf->isUnnamed() && buf->fileName().exists()
506 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
509 case LFUN_CITATION_INSERT: {
510 FuncRequest fr(LFUN_INSET_INSERT, "citation");
511 enable = getStatus(fr).enabled();
515 // This could be used for the no-GUI version. The GUI version is handled in
516 // LyXView::getStatus(). See above.
518 case LFUN_BUFFER_WRITE:
519 case LFUN_BUFFER_WRITE_AS: {
520 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
521 enable = b && (b->isUnnamed() || !b->isClean());
526 case LFUN_BUFFER_WRITE_ALL: {
527 // We enable the command only if there are some modified buffers
528 Buffer * first = theBufferList().first();
533 // We cannot use a for loop as the buffer list is a cycle.
539 b = theBufferList().next(b);
540 } while (b != first);
544 case LFUN_BOOKMARK_GOTO: {
545 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
546 enable = LyX::ref().session().bookmarks().isValid(num);
550 case LFUN_BOOKMARK_CLEAR:
551 enable = LyX::ref().session().bookmarks().size() > 0;
554 // this one is difficult to get right. As a half-baked
555 // solution, we consider only the first action of the sequence
556 case LFUN_COMMAND_SEQUENCE: {
557 // argument contains ';'-terminated commands
558 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
559 FuncRequest func(lyxaction.lookupFunc(firstcmd));
560 func.origin = cmd.origin;
561 flag = getStatus(func);
567 string name = to_utf8(cmd.argument());
568 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
569 func.origin = cmd.origin;
570 flag = getStatus(func);
571 LyX::ref().topLevelCmdDef().release(name);
573 // catch recursion or unknown command definiton
574 // all operations until the recursion or unknown command
575 // definiton occures are performed, so set the state to enabled
581 case LFUN_BUFFER_NEW:
582 case LFUN_BUFFER_NEW_TEMPLATE:
583 case LFUN_WORD_FIND_FORWARD:
584 case LFUN_WORD_FIND_BACKWARD:
585 case LFUN_COMMAND_PREFIX:
586 case LFUN_COMMAND_EXECUTE:
588 case LFUN_META_PREFIX:
589 case LFUN_BUFFER_CLOSE:
590 case LFUN_BUFFER_UPDATE:
591 case LFUN_BUFFER_VIEW:
592 case LFUN_MASTER_BUFFER_UPDATE:
593 case LFUN_MASTER_BUFFER_VIEW:
594 case LFUN_BUFFER_IMPORT:
595 case LFUN_BUFFER_AUTO_SAVE:
596 case LFUN_RECONFIGURE:
600 case LFUN_DROP_LAYOUTS_CHOICE:
602 case LFUN_SERVER_GET_NAME:
603 case LFUN_SERVER_NOTIFY:
604 case LFUN_SERVER_GOTO_FILE_ROW:
605 case LFUN_DIALOG_HIDE:
606 case LFUN_DIALOG_DISCONNECT_INSET:
607 case LFUN_BUFFER_CHILD_OPEN:
608 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
609 case LFUN_KEYMAP_OFF:
610 case LFUN_KEYMAP_PRIMARY:
611 case LFUN_KEYMAP_SECONDARY:
612 case LFUN_KEYMAP_TOGGLE:
614 case LFUN_BUFFER_EXPORT_CUSTOM:
615 case LFUN_BUFFER_PRINT:
616 case LFUN_PREFERENCES_SAVE:
617 case LFUN_SCREEN_FONT_UPDATE:
620 case LFUN_EXTERNAL_EDIT:
621 case LFUN_GRAPHICS_EDIT:
622 case LFUN_ALL_INSETS_TOGGLE:
623 case LFUN_BUFFER_LANGUAGE:
624 case LFUN_TEXTCLASS_APPLY:
625 case LFUN_TEXTCLASS_LOAD:
626 case LFUN_BUFFER_SAVE_AS_DEFAULT:
627 case LFUN_BUFFER_PARAMS_APPLY:
628 case LFUN_LAYOUT_MODULES_CLEAR:
629 case LFUN_LAYOUT_MODULE_ADD:
630 case LFUN_LAYOUT_RELOAD:
631 case LFUN_LYXRC_APPLY:
632 case LFUN_BUFFER_NEXT:
633 case LFUN_BUFFER_PREVIOUS:
634 case LFUN_WINDOW_NEW:
636 // these are handled in our dispatch()
644 if (!getLocalStatus(view()->cursor(), cmd, flag))
645 flag = view()->getStatus(cmd);
651 // Can we use a readonly buffer?
652 if (buf && buf->isReadonly()
653 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
654 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
655 flag.message(from_utf8(N_("Document is read-only")));
659 // Are we in a DELETED change-tracking region?
661 && lookupChangeType(view()->cursor(), true) == Change::DELETED
662 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
663 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
664 flag.message(from_utf8(N_("This portion of the document is deleted.")));
668 // the default error message if we disable the command
669 if (!flag.enabled() && flag.message().empty())
670 flag.message(from_utf8(N_("Command disabled")));
676 bool LyXFunc::ensureBufferClean(BufferView * bv)
678 Buffer & buf = bv->buffer();
682 docstring const file = buf.fileName().displayName(30);
683 docstring text = bformat(_("The document %1$s has unsaved "
684 "changes.\n\nDo you want to save "
685 "the document?"), file);
686 int const ret = Alert::prompt(_("Save changed document?"),
687 text, 0, 1, _("&Save"),
691 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
693 return buf.isClean();
699 void showPrintError(string const & name)
701 docstring str = bformat(_("Could not print the document %1$s.\n"
702 "Check that your printer is set up correctly."),
703 makeDisplayPath(name, 50));
704 Alert::error(_("Print document failed"), str);
708 void loadTextClass(string const & name)
710 pair<bool, textclass_type> const tc_pair =
711 textclasslist.numberOfClass(name);
713 if (!tc_pair.first) {
714 lyxerr << "Document class \"" << name
715 << "\" does not exist."
720 textclass_type const tc = tc_pair.second;
722 if (!textclasslist[tc].load()) {
723 docstring s = bformat(_("The document class %1$s."
724 "could not be loaded."),
725 from_utf8(textclasslist[tc].name()));
726 Alert::error(_("Could not load class"), s);
731 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
736 void LyXFunc::dispatch(FuncRequest const & cmd)
738 string const argument = to_utf8(cmd.argument());
739 kb_action const action = cmd.action;
741 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
742 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
744 // we have not done anything wrong yet.
746 dispatch_buffer.erase();
748 // redraw the screen at the end (first of the two drawing steps).
749 //This is done unless explicitely requested otherwise
750 Update::flags updateFlags = Update::FitCursor;
752 FuncStatus const flag = getStatus(cmd);
753 if (!flag.enabled()) {
754 // We cannot use this function here
755 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
756 << lyxaction.getActionName(action)
757 << " [" << action << "] is disabled at this location");
758 setErrorMessage(flag.message());
762 case LFUN_WORD_FIND_FORWARD:
763 case LFUN_WORD_FIND_BACKWARD: {
764 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
765 static docstring last_search;
766 docstring searched_string;
768 if (!cmd.argument().empty()) {
769 last_search = cmd.argument();
770 searched_string = cmd.argument();
772 searched_string = last_search;
775 if (searched_string.empty())
778 bool const fw = action == LFUN_WORD_FIND_FORWARD;
779 docstring const data =
780 find2string(searched_string, true, false, fw);
781 find(view(), FuncRequest(LFUN_WORD_FIND, data));
785 case LFUN_COMMAND_PREFIX:
786 BOOST_ASSERT(lyx_view_);
787 lyx_view_->message(keyseq.printOptions(true));
791 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
793 meta_fake_bit = NoModifier;
794 if (lyx_view_->buffer())
795 // cancel any selection
796 dispatch(FuncRequest(LFUN_MARK_OFF));
797 setMessage(from_ascii(N_("Cancel")));
800 case LFUN_META_PREFIX:
801 meta_fake_bit = AltModifier;
802 setMessage(keyseq.print(KeySequence::ForGui));
805 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
806 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
807 Buffer * buf = lyx_view_->buffer();
808 if (buf->lyxvc().inUse())
809 buf->lyxvc().toggleReadOnly();
811 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
815 // --- Menus -----------------------------------------------
816 case LFUN_BUFFER_NEW:
817 lyx_view_->newDocument(argument, false);
818 updateFlags = Update::None;
821 case LFUN_BUFFER_NEW_TEMPLATE:
822 lyx_view_->newDocument(argument, true);
823 updateFlags = Update::None;
826 case LFUN_BUFFER_CLOSE:
828 updateFlags = Update::None;
831 case LFUN_BUFFER_RELOAD: {
832 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
833 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
834 docstring text = bformat(_("Any changes will be lost. Are you sure "
835 "you want to revert to the saved version of the document %1$s?"), file);
836 int const ret = Alert::prompt(_("Revert to saved document?"),
837 text, 1, 1, _("&Revert"), _("&Cancel"));
844 case LFUN_BUFFER_UPDATE:
845 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
846 lyx_view_->buffer()->doExport(argument, true);
849 case LFUN_BUFFER_VIEW:
850 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
851 lyx_view_->buffer()->preview(argument);
854 case LFUN_MASTER_BUFFER_UPDATE:
855 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
856 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
859 case LFUN_MASTER_BUFFER_VIEW:
860 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
861 lyx_view_->buffer()->masterBuffer()->preview(argument);
864 case LFUN_BUILD_PROGRAM:
865 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
866 lyx_view_->buffer()->doExport("program", true);
869 case LFUN_BUFFER_CHKTEX:
870 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
871 lyx_view_->buffer()->runChktex();
874 case LFUN_BUFFER_EXPORT:
875 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
876 if (argument == "custom")
877 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
879 lyx_view_->buffer()->doExport(argument, false);
882 case LFUN_BUFFER_EXPORT_CUSTOM: {
883 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
885 string command = split(argument, format_name, ' ');
886 Format const * format = formats.getFormat(format_name);
888 lyxerr << "Format \"" << format_name
889 << "\" not recognized!"
894 Buffer * buffer = lyx_view_->buffer();
896 // The name of the file created by the conversion process
899 // Output to filename
900 if (format->name() == "lyx") {
901 string const latexname = buffer->latexName(false);
902 filename = changeExtension(latexname,
903 format->extension());
904 filename = addName(buffer->temppath(), filename);
906 if (!buffer->writeFile(FileName(filename)))
910 buffer->doExport(format_name, true, filename);
913 // Substitute $$FName for filename
914 if (!contains(command, "$$FName"))
915 command = "( " + command + " ) < $$FName";
916 command = subst(command, "$$FName", filename);
918 // Execute the command in the background
920 call.startscript(Systemcall::DontWait, command);
924 case LFUN_BUFFER_PRINT: {
925 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
926 // FIXME: cmd.getArg() might fail if one of the arguments
927 // contains double quotes
928 string target = cmd.getArg(0);
929 string target_name = cmd.getArg(1);
930 string command = cmd.getArg(2);
933 || target_name.empty()
934 || command.empty()) {
935 lyxerr << "Unable to parse \""
936 << argument << '"' << endl;
939 if (target != "printer" && target != "file") {
940 lyxerr << "Unrecognized target \""
941 << target << '"' << endl;
945 Buffer * buffer = lyx_view_->buffer();
947 if (!buffer->doExport("dvi", true)) {
948 showPrintError(buffer->absFileName());
952 // Push directory path.
953 string const path = buffer->temppath();
954 // Prevent the compiler from optimizing away p
958 // there are three cases here:
959 // 1. we print to a file
960 // 2. we print directly to a printer
961 // 3. we print using a spool command (print to file first)
964 string const dviname =
965 changeExtension(buffer->latexName(true), "dvi");
967 if (target == "printer") {
968 if (!lyxrc.print_spool_command.empty()) {
969 // case 3: print using a spool
970 string const psname =
971 changeExtension(dviname,".ps");
972 command += ' ' + lyxrc.print_to_file
975 + quoteName(dviname);
978 lyxrc.print_spool_command + ' ';
979 if (target_name != "default") {
980 command2 += lyxrc.print_spool_printerprefix
984 command2 += quoteName(psname);
986 // If successful, then spool command
987 res = one.startscript(
992 res = one.startscript(
993 Systemcall::DontWait,
996 // case 2: print directly to a printer
997 if (target_name != "default")
998 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
999 res = one.startscript(
1000 Systemcall::DontWait,
1001 command + quoteName(dviname));
1005 // case 1: print to a file
1006 FileName const filename(makeAbsPath(target_name,
1007 lyx_view_->buffer()->filePath()));
1008 FileName const dvifile(makeAbsPath(dviname, path));
1009 if (filename.exists()) {
1010 docstring text = bformat(
1011 _("The file %1$s already exists.\n\n"
1012 "Do you want to overwrite that file?"),
1013 makeDisplayPath(filename.absFilename()));
1014 if (Alert::prompt(_("Overwrite file?"),
1015 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1018 command += ' ' + lyxrc.print_to_file
1019 + quoteName(filename.toFilesystemEncoding())
1021 + quoteName(dvifile.toFilesystemEncoding());
1022 res = one.startscript(Systemcall::DontWait,
1027 showPrintError(buffer->absFileName());
1031 // FIXME: There is need for a command-line import.
1033 case LFUN_BUFFER_IMPORT:
1038 case LFUN_BUFFER_AUTO_SAVE:
1039 lyx_view_->buffer()->autoSave();
1042 case LFUN_RECONFIGURE:
1043 BOOST_ASSERT(lyx_view_);
1044 // argument is any additional parameter to the configure.py command
1045 reconfigure(*lyx_view_, argument);
1048 case LFUN_HELP_OPEN: {
1049 BOOST_ASSERT(lyx_view_);
1050 string const arg = argument;
1052 setErrorMessage(from_ascii(N_("Missing argument")));
1055 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1056 if (fname.empty()) {
1057 lyxerr << "LyX: unable to find documentation file `"
1058 << arg << "'. Bad installation?" << endl;
1061 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1062 makeDisplayPath(fname.absFilename())));
1063 Buffer * buf = lyx_view_->loadDocument(fname, false);
1066 lyx_view_->setBuffer(buf);
1067 buf->errors("Parse");
1069 updateFlags = Update::None;
1073 // --- version control -------------------------------
1074 case LFUN_VC_REGISTER:
1075 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1076 if (!ensureBufferClean(view()))
1078 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1079 lyx_view_->buffer()->lyxvc().registrer();
1082 updateFlags = Update::Force;
1085 case LFUN_VC_CHECK_IN:
1086 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1087 if (!ensureBufferClean(view()))
1089 if (lyx_view_->buffer()->lyxvc().inUse()
1090 && !lyx_view_->buffer()->isReadonly()) {
1091 lyx_view_->buffer()->lyxvc().checkIn();
1096 case LFUN_VC_CHECK_OUT:
1097 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1098 if (!ensureBufferClean(view()))
1100 if (lyx_view_->buffer()->lyxvc().inUse()
1101 && lyx_view_->buffer()->isReadonly()) {
1102 lyx_view_->buffer()->lyxvc().checkOut();
1107 case LFUN_VC_REVERT:
1108 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1109 lyx_view_->buffer()->lyxvc().revert();
1113 case LFUN_VC_UNDO_LAST:
1114 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1115 lyx_view_->buffer()->lyxvc().undoLast();
1119 // --- buffers ----------------------------------------
1121 case LFUN_FILE_NEW: {
1122 BOOST_ASSERT(lyx_view_);
1124 string tmpname = split(argument, name, ':'); // Split filename
1125 Buffer * const b = newFile(name, tmpname);
1127 lyx_view_->setBuffer(b);
1128 updateFlags = Update::None;
1132 // --- lyxserver commands ----------------------------
1133 case LFUN_SERVER_GET_NAME:
1134 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1135 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1136 LYXERR(Debug::INFO, "FNAME["
1137 << lyx_view_->buffer()->absFileName() << ']');
1140 case LFUN_SERVER_NOTIFY:
1141 dispatch_buffer = keyseq.print(KeySequence::Portable);
1142 theServer().notifyClient(to_utf8(dispatch_buffer));
1145 case LFUN_SERVER_GOTO_FILE_ROW: {
1146 BOOST_ASSERT(lyx_view_);
1149 istringstream is(argument);
1150 is >> file_name >> row;
1152 bool loaded = false;
1153 if (prefixIs(file_name, package().temp_dir().absFilename()))
1154 // Needed by inverse dvi search. If it is a file
1155 // in tmpdir, call the apropriated function
1156 buf = theBufferList().getBufferFromTmp(file_name);
1158 // Must replace extension of the file to be .lyx
1159 // and get full path
1160 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1161 // Either change buffer or load the file
1162 if (theBufferList().exists(s.absFilename()))
1163 buf = theBufferList().getBuffer(s.absFilename());
1165 buf = lyx_view_->loadDocument(s);
1171 updateFlags = Update::None;
1176 lyx_view_->setBuffer(buf);
1177 view()->setCursorFromRow(row);
1179 buf->errors("Parse");
1180 updateFlags = Update::FitCursor;
1185 case LFUN_DIALOG_SHOW_NEW_INSET: {
1186 BOOST_ASSERT(lyx_view_);
1187 string const name = cmd.getArg(0);
1188 InsetCode code = insetCode(name);
1189 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1190 bool insetCodeOK = true;
1199 case HYPERLINK_CODE: {
1200 InsetCommandParams p(code);
1201 data = InsetCommandMailer::params2string(name, p);
1204 case INCLUDE_CODE: {
1205 // data is the include type: one of "include",
1206 // "input", "verbatiminput" or "verbatiminput*"
1208 // default type is requested
1210 InsetCommandParams p(INCLUDE_CODE, data);
1211 data = InsetCommandMailer::params2string("include", p);
1215 // \c data == "Boxed" || "Frameless" etc
1216 InsetBoxParams p(data);
1217 data = InsetBoxMailer::params2string(p);
1221 InsetBranchParams p;
1222 data = InsetBranchMailer::params2string(p);
1226 InsetCommandParams p(CITE_CODE);
1227 data = InsetCommandMailer::params2string(name, p);
1231 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1234 case EXTERNAL_CODE: {
1235 InsetExternalParams p;
1236 Buffer const & buffer = *lyx_view_->buffer();
1237 data = InsetExternalMailer::params2string(p, buffer);
1242 data = InsetFloatMailer::params2string(p);
1245 case LISTINGS_CODE: {
1246 InsetListingsParams p;
1247 data = InsetListingsMailer::params2string(p);
1250 case GRAPHICS_CODE: {
1251 InsetGraphicsParams p;
1252 Buffer const & buffer = *lyx_view_->buffer();
1253 data = InsetGraphicsMailer::params2string(p, buffer);
1258 data = InsetNoteMailer::params2string(p);
1263 data = InsetVSpaceMailer::params2string(space);
1268 data = InsetWrapMailer::params2string(p);
1272 lyxerr << "Inset type '" << name <<
1273 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1274 insetCodeOK = false;
1276 } // end switch(code)
1278 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1282 case LFUN_CITATION_INSERT: {
1283 BOOST_ASSERT(lyx_view_);
1284 if (!argument.empty()) {
1285 // we can have one optional argument, delimited by '|'
1286 // citation-insert <key>|<text_before>
1287 // this should be enhanced to also support text_after
1288 // and citation style
1289 string arg = argument;
1291 if (contains(argument, "|")) {
1292 arg = token(argument, '|', 0);
1293 opt1 = token(argument, '|', 1);
1295 InsetCommandParams icp(CITE_CODE);
1296 icp["key"] = from_utf8(arg);
1298 icp["before"] = from_utf8(opt1);
1299 string icstr = InsetCommandMailer::params2string("citation", icp);
1300 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1303 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1307 case LFUN_BUFFER_CHILD_OPEN: {
1308 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1309 Buffer * parent = lyx_view_->buffer();
1310 FileName filename = makeAbsPath(argument, parent->filePath());
1311 view()->saveBookmark(false);
1313 bool parsed = false;
1314 if (theBufferList().exists(filename.absFilename())) {
1315 child = theBufferList().getBuffer(filename.absFilename());
1317 setMessage(bformat(_("Opening child document %1$s..."),
1318 makeDisplayPath(filename.absFilename())));
1319 child = lyx_view_->loadDocument(filename, false);
1323 // Set the parent name of the child document.
1324 // This makes insertion of citations and references in the child work,
1325 // when the target is in the parent or another child document.
1326 child->setParent(parent);
1327 updateLabels(*child->masterBuffer());
1328 lyx_view_->setBuffer(child);
1330 child->errors("Parse");
1333 // If a screen update is required (in case where auto_open is false),
1334 // setBuffer() would have taken care of it already. Otherwise we shall
1335 // reset the update flag because it can cause a circular problem.
1337 updateFlags = Update::None;
1341 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1342 BOOST_ASSERT(lyx_view_);
1343 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1346 case LFUN_KEYMAP_OFF:
1347 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1348 lyx_view_->view()->getIntl().keyMapOn(false);
1351 case LFUN_KEYMAP_PRIMARY:
1352 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1353 lyx_view_->view()->getIntl().keyMapPrim();
1356 case LFUN_KEYMAP_SECONDARY:
1357 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1358 lyx_view_->view()->getIntl().keyMapSec();
1361 case LFUN_KEYMAP_TOGGLE:
1362 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1363 lyx_view_->view()->getIntl().toggleKeyMap();
1369 string rest = split(argument, countstr, ' ');
1370 istringstream is(countstr);
1373 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1374 for (int i = 0; i < count; ++i)
1375 dispatch(lyxaction.lookupFunc(rest));
1379 case LFUN_COMMAND_SEQUENCE: {
1380 // argument contains ';'-terminated commands
1381 string arg = argument;
1382 while (!arg.empty()) {
1384 arg = split(arg, first, ';');
1385 FuncRequest func(lyxaction.lookupFunc(first));
1386 func.origin = cmd.origin;
1394 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1395 func.origin = cmd.origin;
1397 LyX::ref().topLevelCmdDef().release(argument);
1399 if (func.action == LFUN_UNKNOWN_ACTION) {
1400 // unknown command definition
1401 lyxerr << "Warning: unknown command definition `"
1405 // recursion detected
1406 lyxerr << "Warning: Recursion in the command definition `"
1407 << argument << "' detected"
1414 case LFUN_PREFERENCES_SAVE: {
1415 lyxrc.write(makeAbsPath("preferences",
1416 package().user_support().absFilename()),
1421 case LFUN_SET_COLOR: {
1423 string const x11_name = split(argument, lyx_name, ' ');
1424 if (lyx_name.empty() || x11_name.empty()) {
1425 setErrorMessage(from_ascii(N_(
1426 "Syntax: set-color <lyx_name>"
1431 bool const graphicsbg_changed =
1432 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1433 x11_name != lcolor.getX11Name(Color_graphicsbg));
1435 if (!lcolor.setColor(lyx_name, x11_name)) {
1437 bformat(_("Set-color \"%1$s\" failed "
1438 "- color is undefined or "
1439 "may not be redefined"),
1440 from_utf8(lyx_name)));
1444 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1446 if (graphicsbg_changed) {
1447 // FIXME: The graphics cache no longer has a changeDisplay method.
1449 graphics::GCache::get().changeDisplay(true);
1456 BOOST_ASSERT(lyx_view_);
1457 lyx_view_->message(from_utf8(argument));
1460 case LFUN_EXTERNAL_EDIT: {
1461 BOOST_ASSERT(lyx_view_);
1462 FuncRequest fr(action, argument);
1463 InsetExternal().dispatch(view()->cursor(), fr);
1467 case LFUN_GRAPHICS_EDIT: {
1468 FuncRequest fr(action, argument);
1469 InsetGraphics().dispatch(view()->cursor(), fr);
1473 case LFUN_ALL_INSETS_TOGGLE: {
1474 BOOST_ASSERT(lyx_view_);
1476 string const name = split(argument, action, ' ');
1477 InsetCode const inset_code = insetCode(name);
1479 Cursor & cur = view()->cursor();
1480 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1482 Inset & inset = lyx_view_->buffer()->inset();
1483 InsetIterator it = inset_iterator_begin(inset);
1484 InsetIterator const end = inset_iterator_end(inset);
1485 for (; it != end; ++it) {
1486 if (!it->asInsetMath()
1487 && (inset_code == NO_CODE
1488 || inset_code == it->lyxCode())) {
1489 Cursor tmpcur = cur;
1490 tmpcur.pushBackward(*it);
1491 it->dispatch(tmpcur, fr);
1494 updateFlags = Update::Force | Update::FitCursor;
1498 case LFUN_BUFFER_LANGUAGE: {
1499 BOOST_ASSERT(lyx_view_);
1500 Buffer & buffer = *lyx_view_->buffer();
1501 Language const * oldL = buffer.params().language;
1502 Language const * newL = languages.getLanguage(argument);
1503 if (!newL || oldL == newL)
1506 if (oldL->rightToLeft() == newL->rightToLeft()
1507 && !buffer.isMultiLingual())
1508 buffer.changeLanguage(oldL, newL);
1512 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1513 string const fname =
1514 addName(addPath(package().user_support().absFilename(), "templates/"),
1516 Buffer defaults(fname);
1518 istringstream ss(argument);
1521 int const unknown_tokens = defaults.readHeader(lex);
1523 if (unknown_tokens != 0) {
1524 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1525 << unknown_tokens << " unknown token"
1526 << (unknown_tokens == 1 ? "" : "s")
1530 if (defaults.writeFile(FileName(defaults.absFileName())))
1531 setMessage(bformat(_("Document defaults saved in %1$s"),
1532 makeDisplayPath(fname)));
1534 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1538 case LFUN_BUFFER_PARAMS_APPLY: {
1539 BOOST_ASSERT(lyx_view_);
1540 biblio::CiteEngine const oldEngine =
1541 lyx_view_->buffer()->params().getEngine();
1543 Buffer * buffer = lyx_view_->buffer();
1545 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1547 Cursor & cur = view()->cursor();
1548 cur.recordUndoFullDocument();
1550 istringstream ss(argument);
1553 int const unknown_tokens = buffer->readHeader(lex);
1555 if (unknown_tokens != 0) {
1556 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1557 << unknown_tokens << " unknown token"
1558 << (unknown_tokens == 1 ? "" : "s")
1562 updateLayout(oldClass, buffer);
1564 biblio::CiteEngine const newEngine =
1565 lyx_view_->buffer()->params().getEngine();
1567 if (oldEngine != newEngine) {
1568 FuncRequest fr(LFUN_INSET_REFRESH);
1570 Inset & inset = lyx_view_->buffer()->inset();
1571 InsetIterator it = inset_iterator_begin(inset);
1572 InsetIterator const end = inset_iterator_end(inset);
1573 for (; it != end; ++it)
1574 if (it->lyxCode() == CITE_CODE)
1575 it->dispatch(cur, fr);
1578 updateFlags = Update::Force | Update::FitCursor;
1579 // We are here most certainaly because of a change in the document
1580 // It is then better to make sure that all dialogs are in sync
1581 // with current document settings. LyXView::restartCursor() achieve this.
1582 lyx_view_->restartCursor();
1586 case LFUN_LAYOUT_MODULES_CLEAR: {
1587 BOOST_ASSERT(lyx_view_);
1588 Buffer * buffer = lyx_view_->buffer();
1589 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1590 view()->cursor().recordUndoFullDocument();
1591 buffer->params().clearLayoutModules();
1592 buffer->params().makeTextClass();
1593 updateLayout(oldClass, buffer);
1594 updateFlags = Update::Force | Update::FitCursor;
1598 case LFUN_LAYOUT_MODULE_ADD: {
1599 BOOST_ASSERT(lyx_view_);
1600 Buffer * buffer = lyx_view_->buffer();
1601 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1602 view()->cursor().recordUndoFullDocument();
1603 buffer->params().addLayoutModule(argument);
1604 buffer->params().makeTextClass();
1605 updateLayout(oldClass, buffer);
1606 updateFlags = Update::Force | Update::FitCursor;
1610 case LFUN_TEXTCLASS_APPLY: {
1611 BOOST_ASSERT(lyx_view_);
1612 Buffer * buffer = lyx_view_->buffer();
1614 loadTextClass(argument);
1616 pair<bool, textclass_type> const tc_pair =
1617 textclasslist.numberOfClass(argument);
1622 textclass_type const old_class = buffer->params().getBaseClass();
1623 textclass_type const new_class = tc_pair.second;
1625 if (old_class == new_class)
1629 //Save the old, possibly modular, layout for use in conversion.
1630 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1631 view()->cursor().recordUndoFullDocument();
1632 buffer->params().setBaseClass(new_class);
1633 buffer->params().makeTextClass();
1634 updateLayout(oldClass, buffer);
1635 updateFlags = Update::Force | Update::FitCursor;
1639 case LFUN_LAYOUT_RELOAD: {
1640 BOOST_ASSERT(lyx_view_);
1641 Buffer * buffer = lyx_view_->buffer();
1642 TextClassPtr oldClass = buffer->params().getTextClassPtr();
1643 textclass_type const tc = buffer->params().getBaseClass();
1644 textclasslist.reset(tc);
1645 buffer->params().setBaseClass(tc);
1646 buffer->params().makeTextClass();
1647 updateLayout(oldClass, buffer);
1648 updateFlags = Update::Force | Update::FitCursor;
1652 case LFUN_TEXTCLASS_LOAD:
1653 loadTextClass(argument);
1656 case LFUN_LYXRC_APPLY: {
1657 LyXRC const lyxrc_orig = lyxrc;
1659 istringstream ss(argument);
1660 bool const success = lyxrc.read(ss) == 0;
1663 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1664 << "Unable to read lyxrc data"
1669 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1671 theApp()->resetGui();
1673 /// We force the redraw in any case because there might be
1674 /// some screen font changes.
1675 /// FIXME: only the current view will be updated. the Gui
1676 /// class is able to furnish the list of views.
1677 updateFlags = Update::Force;
1681 case LFUN_BOOKMARK_GOTO:
1682 // go to bookmark, open unopened file and switch to buffer if necessary
1683 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1684 updateFlags = Update::FitCursor;
1687 case LFUN_BOOKMARK_CLEAR:
1688 LyX::ref().session().bookmarks().clear();
1692 BOOST_ASSERT(theApp());
1693 // Let the frontend dispatch its own actions.
1694 if (theApp()->dispatch(cmd))
1695 // Nothing more to do.
1698 // Let the current LyXView dispatch its own actions.
1699 BOOST_ASSERT(lyx_view_);
1700 if (lyx_view_->dispatch(cmd)) {
1701 if (lyx_view_->view())
1702 updateFlags = lyx_view_->view()->cursor().result().update();
1706 BOOST_ASSERT(lyx_view_->view());
1707 // Let the current BufferView dispatch its own actions.
1708 if (view()->dispatch(cmd)) {
1709 // The BufferView took care of its own updates if needed.
1710 updateFlags = Update::None;
1714 // Let the current Cursor dispatch its own actions.
1715 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1716 cursorPosBeforeDispatchY_);
1717 view()->cursor().dispatch(cmd);
1718 updateFlags = view()->cursor().result().update();
1719 if (!view()->cursor().result().dispatched()) {
1720 // No update needed in this case (e.g. when reaching
1722 updateFlags = Update::None;
1726 if (lyx_view_ && lyx_view_->buffer()) {
1727 // BufferView::update() updates the ViewMetricsInfo and
1728 // also initializes the position cache for all insets in
1729 // (at least partially) visible top-level paragraphs.
1730 // We will redraw the screen only if needed.
1731 view()->processUpdateFlags(updateFlags);
1733 // if we executed a mutating lfun, mark the buffer as dirty
1735 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1736 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1737 lyx_view_->buffer()->markDirty();
1739 //Do we have a selection?
1740 theSelection().haveSelection(view()->cursor().selection());
1743 if (!quitting && lyx_view_) {
1744 // Some messages may already be translated, so we cannot use _()
1745 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1750 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1752 const bool verbose = (cmd.origin == FuncRequest::MENU
1753 || cmd.origin == FuncRequest::TOOLBAR
1754 || cmd.origin == FuncRequest::COMMANDBUFFER);
1756 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1757 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1759 lyx_view_->message(msg);
1763 docstring dispatch_msg = msg;
1764 if (!dispatch_msg.empty())
1765 dispatch_msg += ' ';
1767 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1769 bool argsadded = false;
1771 if (!cmd.argument().empty()) {
1772 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1773 comname += ' ' + cmd.argument();
1778 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1780 if (!shortcuts.empty())
1781 comname += ": " + shortcuts;
1782 else if (!argsadded && !cmd.argument().empty())
1783 comname += ' ' + cmd.argument();
1785 if (!comname.empty()) {
1786 comname = rtrim(comname);
1787 dispatch_msg += '(' + rtrim(comname) + ')';
1790 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1791 if (!dispatch_msg.empty())
1792 lyx_view_->message(dispatch_msg);
1796 void LyXFunc::closeBuffer()
1798 // goto bookmark to update bookmark pit.
1799 for (size_t i = 0; i < LyX::ref().session().bookmarks().size(); ++i)
1800 gotoBookmark(i+1, false, false);
1802 lyx_view_->closeBuffer();
1806 void LyXFunc::reloadBuffer()
1808 FileName filename = lyx_view_->buffer()->fileName();
1809 // The user has already confirmed that the changes, if any, should
1810 // be discarded. So we just release the Buffer and don't call closeBuffer();
1811 theBufferList().release(lyx_view_->buffer());
1812 Buffer * buf = lyx_view_->loadDocument(filename);
1813 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1817 lyx_view_->setBuffer(buf);
1818 buf->errors("Parse");
1819 str = bformat(_("Document %1$s reloaded."), disp_fn);
1821 str = bformat(_("Could not reload document %1$s"), disp_fn);
1823 lyx_view_->message(str);
1826 // Each "lyx_view_" should have it's own message method. lyxview and
1827 // the minibuffer would use the minibuffer, but lyxserver would
1828 // send an ERROR signal to its client. Alejandro 970603
1829 // This function is bit problematic when it comes to NLS, to make the
1830 // lyx servers client be language indepenent we must not translate
1831 // strings sent to this func.
1832 void LyXFunc::setErrorMessage(docstring const & m) const
1834 dispatch_buffer = m;
1839 void LyXFunc::setMessage(docstring const & m) const
1841 dispatch_buffer = m;
1845 docstring const LyXFunc::viewStatusMessage()
1847 // When meta-fake key is pressed, show the key sequence so far + "M-".
1849 return keyseq.print(KeySequence::ForGui) + "M-";
1851 // Else, when a non-complete key sequence is pressed,
1852 // show the available options.
1853 if (keyseq.length() > 0 && !keyseq.deleted())
1854 return keyseq.printOptions(true);
1856 BOOST_ASSERT(lyx_view_);
1857 if (!lyx_view_->buffer())
1858 return _("Welcome to LyX!");
1860 return view()->cursor().currentState();
1864 BufferView * LyXFunc::view() const
1866 BOOST_ASSERT(lyx_view_);
1867 return lyx_view_->view();
1871 bool LyXFunc::wasMetaKey() const
1873 return (meta_fake_bit != NoModifier);
1877 void LyXFunc::updateLayout(TextClassPtr const & oldlayout,
1880 lyx_view_->message(_("Converting document to new document class..."));
1882 StableDocIterator backcur(view()->cursor());
1883 ErrorList & el = buffer->errorList("Class Switch");
1884 cap::switchBetweenClasses(
1885 oldlayout, buffer->params().getTextClassPtr(),
1886 static_cast<InsetText &>(buffer->inset()), el);
1888 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1890 buffer->errors("Class Switch");
1891 updateLabels(*buffer);
1897 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1899 // Why the switch you might ask. It is a trick to ensure that all
1900 // the elements in the LyXRCTags enum is handled. As you can see
1901 // there are no breaks at all. So it is just a huge fall-through.
1902 // The nice thing is that we will get a warning from the compiler
1903 // if we forget an element.
1904 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1906 case LyXRC::RC_ACCEPT_COMPOUND:
1907 case LyXRC::RC_ALT_LANG:
1908 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1909 case LyXRC::RC_PLAINTEXT_LINELEN:
1910 case LyXRC::RC_AUTOREGIONDELETE:
1911 case LyXRC::RC_AUTORESET_OPTIONS:
1912 case LyXRC::RC_AUTOSAVE:
1913 case LyXRC::RC_AUTO_NUMBER:
1914 case LyXRC::RC_BACKUPDIR_PATH:
1915 case LyXRC::RC_BIBTEX_COMMAND:
1916 case LyXRC::RC_BINDFILE:
1917 case LyXRC::RC_CHECKLASTFILES:
1918 case LyXRC::RC_USELASTFILEPOS:
1919 case LyXRC::RC_LOADSESSION:
1920 case LyXRC::RC_CHKTEX_COMMAND:
1921 case LyXRC::RC_CONVERTER:
1922 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1923 case LyXRC::RC_COPIER:
1924 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1925 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1926 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1927 case LyXRC::RC_DATE_INSERT_FORMAT:
1928 case LyXRC::RC_DEFAULT_LANGUAGE:
1929 case LyXRC::RC_DEFAULT_PAPERSIZE:
1930 case LyXRC::RC_DEFFILE:
1931 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1932 case LyXRC::RC_DISPLAY_GRAPHICS:
1933 case LyXRC::RC_DOCUMENTPATH:
1934 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1935 FileName path(lyxrc_new.document_path);
1936 if (path.exists() && path.isDirectory())
1937 package().document_dir() = FileName(lyxrc.document_path);
1939 case LyXRC::RC_ESC_CHARS:
1940 case LyXRC::RC_EXAMPLEPATH:
1941 case LyXRC::RC_FONT_ENCODING:
1942 case LyXRC::RC_FORMAT:
1943 case LyXRC::RC_INDEX_COMMAND:
1944 case LyXRC::RC_INPUT:
1945 case LyXRC::RC_KBMAP:
1946 case LyXRC::RC_KBMAP_PRIMARY:
1947 case LyXRC::RC_KBMAP_SECONDARY:
1948 case LyXRC::RC_LABEL_INIT_LENGTH:
1949 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1950 case LyXRC::RC_LANGUAGE_AUTO_END:
1951 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1952 case LyXRC::RC_LANGUAGE_COMMAND_END:
1953 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1954 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1955 case LyXRC::RC_LANGUAGE_PACKAGE:
1956 case LyXRC::RC_LANGUAGE_USE_BABEL:
1957 case LyXRC::RC_MACRO_EDIT_STYLE:
1958 case LyXRC::RC_MAKE_BACKUP:
1959 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1960 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1961 case LyXRC::RC_NUMLASTFILES:
1962 case LyXRC::RC_PATH_PREFIX:
1963 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1964 prependEnvPath("PATH", lyxrc.path_prefix);
1966 case LyXRC::RC_PERS_DICT:
1967 case LyXRC::RC_PREVIEW:
1968 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1969 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1970 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1971 case LyXRC::RC_PRINTCOPIESFLAG:
1972 case LyXRC::RC_PRINTER:
1973 case LyXRC::RC_PRINTEVENPAGEFLAG:
1974 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1975 case LyXRC::RC_PRINTFILEEXTENSION:
1976 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1977 case LyXRC::RC_PRINTODDPAGEFLAG:
1978 case LyXRC::RC_PRINTPAGERANGEFLAG:
1979 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1980 case LyXRC::RC_PRINTPAPERFLAG:
1981 case LyXRC::RC_PRINTREVERSEFLAG:
1982 case LyXRC::RC_PRINTSPOOL_COMMAND:
1983 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1984 case LyXRC::RC_PRINTTOFILE:
1985 case LyXRC::RC_PRINTTOPRINTER:
1986 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1987 case LyXRC::RC_PRINT_COMMAND:
1988 case LyXRC::RC_RTL_SUPPORT:
1989 case LyXRC::RC_SCREEN_DPI:
1990 case LyXRC::RC_SCREEN_FONT_ROMAN:
1991 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1992 case LyXRC::RC_SCREEN_FONT_SANS:
1993 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1994 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1995 case LyXRC::RC_SCREEN_FONT_SIZES:
1996 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1997 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1998 case LyXRC::RC_GEOMETRY_SESSION:
1999 case LyXRC::RC_SCREEN_ZOOM:
2000 case LyXRC::RC_SERVERPIPE:
2001 case LyXRC::RC_SET_COLOR:
2002 case LyXRC::RC_SHOW_BANNER:
2003 case LyXRC::RC_SPELL_COMMAND:
2004 case LyXRC::RC_TEMPDIRPATH:
2005 case LyXRC::RC_TEMPLATEPATH:
2006 case LyXRC::RC_TEX_ALLOWS_SPACES:
2007 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2008 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2009 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2011 case LyXRC::RC_UIFILE:
2012 case LyXRC::RC_USER_EMAIL:
2013 case LyXRC::RC_USER_NAME:
2014 case LyXRC::RC_USETEMPDIR:
2015 case LyXRC::RC_USE_ALT_LANG:
2016 case LyXRC::RC_USE_CONVERTER_CACHE:
2017 case LyXRC::RC_USE_ESC_CHARS:
2018 case LyXRC::RC_USE_INP_ENC:
2019 case LyXRC::RC_USE_PERS_DICT:
2020 case LyXRC::RC_USE_TOOLTIP:
2021 case LyXRC::RC_USE_PIXMAP_CACHE:
2022 case LyXRC::RC_USE_SPELL_LIB:
2023 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2024 case LyXRC::RC_SORT_LAYOUTS:
2025 case LyXRC::RC_VIEWER:
2026 case LyXRC::RC_LAST: