3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author Jean-Marc Lasgouttes
9 * \author Angus Leeming
11 * \author André Pönitz
14 * \author Martin Vermeer
15 * \author Jürgen Vigna
17 * Full author contact details are available in file CREDITS.
24 #include "BaseClassList.h"
25 #include "BranchList.h"
26 #include "buffer_funcs.h"
28 #include "BufferList.h"
29 #include "BufferParams.h"
30 #include "BufferView.h"
33 #include "Converter.h"
35 #include "CutAndPaste.h"
36 #include "DispatchResult.h"
38 #include "ErrorList.h"
40 #include "FuncRequest.h"
41 #include "FuncStatus.h"
42 #include "InsetIterator.h"
47 #include "LyXAction.h"
52 #include "Paragraph.h"
53 #include "ParagraphParameters.h"
54 #include "ParIterator.h"
59 #include "insets/InsetBox.h"
60 #include "insets/InsetBranch.h"
61 #include "insets/InsetCommand.h"
62 #include "insets/InsetERT.h"
63 #include "insets/InsetExternal.h"
64 #include "insets/InsetFloat.h"
65 #include "insets/InsetListings.h"
66 #include "insets/InsetGraphics.h"
67 #include "insets/InsetInclude.h"
68 #include "insets/InsetNote.h"
69 #include "insets/InsetTabular.h"
70 #include "insets/InsetVSpace.h"
71 #include "insets/InsetWrap.h"
73 #include "frontends/alert.h"
74 #include "frontends/Application.h"
75 #include "frontends/KeySymbol.h"
76 #include "frontends/LyXView.h"
77 #include "frontends/Selection.h"
79 #include "support/debug.h"
80 #include "support/environment.h"
81 #include "support/FileName.h"
82 #include "support/filetools.h"
83 #include "support/gettext.h"
84 #include "support/lstrings.h"
85 #include "support/Path.h"
86 #include "support/Package.h"
87 #include "support/Systemcall.h"
88 #include "support/convert.h"
89 #include "support/os.h"
95 using namespace lyx::support;
99 using frontend::LyXView;
101 namespace Alert = frontend::Alert;
106 // This function runs "configure" and then rereads lyx.defaults to
107 // reconfigure the automatic settings.
108 void reconfigure(LyXView & lv, string const & option)
110 // emit message signal.
111 lv.message(_("Running configure..."));
113 // Run configure in user lyx directory
114 PathChanger p(package().user_support());
115 string configure_command = package().configure_command();
116 configure_command += option;
118 int ret = one.startscript(Systemcall::Wait, configure_command);
120 // emit message signal.
121 lv.message(_("Reloading configuration..."));
122 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
123 // Re-read packages.lst
124 LaTeXFeatures::getAvailable();
127 Alert::information(_("System reconfiguration failed"),
128 _("The system reconfiguration has failed.\n"
129 "Default textclass is used but LyX may "
130 "not be able to work properly.\n"
131 "Please reconfigure again if needed."));
134 Alert::information(_("System reconfigured"),
135 _("The system has been reconfigured.\n"
136 "You need to restart LyX to make use of any\n"
137 "updated document class specifications."));
141 bool getLocalStatus(Cursor cursor, FuncRequest const & cmd, FuncStatus & status)
143 // Try to fix cursor in case it is broken.
144 cursor.fixIfBroken();
146 // This is, of course, a mess. Better create a new doc iterator and use
147 // this in Inset::getStatus. This might require an additional
148 // BufferView * arg, though (which should be avoided)
149 //Cursor safe = *this;
151 for ( ; cursor.depth(); cursor.pop()) {
152 //lyxerr << "\nCursor::getStatus: cmd: " << cmd << endl << *this << endl;
153 BOOST_ASSERT(cursor.idx() <= cursor.lastidx());
154 BOOST_ASSERT(cursor.pit() <= cursor.lastpit());
155 BOOST_ASSERT(cursor.pos() <= cursor.lastpos());
157 // The inset's getStatus() will return 'true' if it made
158 // a definitive decision on whether it want to handle the
159 // request or not. The result of this decision is put into
160 // the 'status' parameter.
161 if (cursor.inset().getStatus(cursor, cmd, status)) {
170 /** Return the change status at cursor position, taking in account the
171 * status at each level of the document iterator (a table in a deleted
172 * footnote is deleted).
173 * When \param outer is true, the top slice is not looked at.
175 Change::Type lookupChangeType(DocIterator const & dit, bool outer = false)
177 size_t const depth = dit.depth() - (outer ? 1 : 0);
179 for (size_t i = 0 ; i < depth ; ++i) {
180 CursorSlice const & slice = dit[i];
181 if (!slice.inset().inMathed()
182 && slice.pos() < slice.paragraph().size()) {
183 Change::Type const ch = slice.paragraph().lookupChange(slice.pos()).type;
184 if (ch != Change::UNCHANGED)
188 return Change::UNCHANGED;
195 : lyx_view_(0), encoded_last_key(0), meta_fake_bit(NoModifier)
200 void LyXFunc::initKeySequences(KeyMap * kb)
202 keyseq = KeySequence(kb, kb);
203 cancel_meta_seq = KeySequence(kb, kb);
207 void LyXFunc::setLyXView(LyXView * lv)
209 if (lyx_view_ && lyx_view_->view() && lyx_view_ != lv)
210 // save current selection to the selection buffer to allow
211 // middle-button paste in another window
212 cap::saveSelection(lyx_view_->view()->cursor());
217 void LyXFunc::handleKeyFunc(kb_action action)
219 char_type c = encoded_last_key;
224 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
225 lyx_view_->view()->getIntl().getTransManager().deadkey(
226 c, get_accent(action).accent, view()->cursor().innerText(), view()->cursor());
227 // Need to clear, in case the minibuffer calls these
230 // copied verbatim from do_accent_char
231 view()->cursor().resetAnchor();
232 view()->processUpdateFlags(Update::FitCursor);
235 //FIXME: bookmark handling is a frontend issue. This code should be transferred
236 // to GuiView and be GuiView and be window dependent.
237 void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
239 BOOST_ASSERT(lyx_view_);
240 if (!LyX::ref().session().bookmarks().isValid(idx))
242 BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(idx);
243 BOOST_ASSERT(!bm.filename.empty());
244 string const file = bm.filename.absFilename();
245 // if the file is not opened, open it.
246 if (!theBufferList().exists(file)) {
248 dispatch(FuncRequest(LFUN_FILE_OPEN, file));
252 // open may fail, so we need to test it again
253 if (!theBufferList().exists(file))
256 // if the current buffer is not that one, switch to it.
257 if (lyx_view_->buffer()->absFileName() != file) {
260 dispatch(FuncRequest(LFUN_BUFFER_SWITCH, file));
262 // moveToPosition try paragraph id first and then paragraph (pit, pos).
263 if (!view()->moveToPosition(bm.bottom_pit, bm.bottom_pos,
264 bm.top_id, bm.top_pos))
267 // Cursor jump succeeded!
268 Cursor const & cur = view()->cursor();
269 pit_type new_pit = cur.pit();
270 pos_type new_pos = cur.pos();
271 int new_id = cur.paragraph().id();
273 // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
274 // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
275 if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos
276 || bm.top_id != new_id) {
277 const_cast<BookmarksSection::Bookmark &>(bm).updatePos(
278 new_pit, new_pos, new_id);
283 void LyXFunc::processKeySym(KeySymbol const & keysym, KeyModifier state)
285 LYXERR(Debug::KEY, "KeySym is " << keysym.getSymbolName());
287 // Do nothing if we have nothing (JMarc)
288 if (!keysym.isOK()) {
289 LYXERR(Debug::KEY, "Empty kbd action (probably composing)");
290 lyx_view_->restartCursor();
294 if (keysym.isModifier()) {
295 LYXERR(Debug::KEY, "isModifier true");
296 lyx_view_->restartCursor();
300 //Encoding const * encoding = view()->cursor().getEncoding();
301 //encoded_last_key = keysym.getISOEncoded(encoding ? encoding->name() : "");
302 // FIXME: encoded_last_key shadows the member variable of the same
303 // name. Is that intended?
304 char_type encoded_last_key = keysym.getUCSEncoded();
306 // Do a one-deep top-level lookup for
307 // cancel and meta-fake keys. RVDK_PATCH_5
308 cancel_meta_seq.reset();
310 FuncRequest func = cancel_meta_seq.addkey(keysym, state);
311 LYXERR(Debug::KEY, "action first set to [" << func.action << ']');
313 // When not cancel or meta-fake, do the normal lookup.
314 // Note how the meta_fake Mod1 bit is OR-ed in and reset afterwards.
315 // Mostly, meta_fake_bit = NoModifier. RVDK_PATCH_5.
316 if ((func.action != LFUN_CANCEL) && (func.action != LFUN_META_PREFIX)) {
317 // remove Caps Lock and Mod2 as a modifiers
318 func = keyseq.addkey(keysym, (state | meta_fake_bit));
319 LYXERR(Debug::KEY, "action now set to [" << func.action << ']');
322 // Dont remove this unless you know what you are doing.
323 meta_fake_bit = NoModifier;
325 // Can this happen now ?
326 if (func.action == LFUN_NOACTION)
327 func = FuncRequest(LFUN_COMMAND_PREFIX);
329 LYXERR(Debug::KEY, " Key [action=" << func.action << "]["
330 << keyseq.print(KeySequence::Portable) << ']');
332 // already here we know if it any point in going further
333 // why not return already here if action == -1 and
334 // num_bytes == 0? (Lgb)
336 if (keyseq.length() > 1)
337 lyx_view_->message(keyseq.print(KeySequence::ForGui));
340 // Maybe user can only reach the key via holding down shift.
341 // Let's see. But only if shift is the only modifier
342 if (func.action == LFUN_UNKNOWN_ACTION && state == ShiftModifier) {
343 LYXERR(Debug::KEY, "Trying without shift");
344 func = keyseq.addkey(keysym, NoModifier);
345 LYXERR(Debug::KEY, "Action now " << func.action);
348 if (func.action == LFUN_UNKNOWN_ACTION) {
349 // Hmm, we didn't match any of the keysequences. See
350 // if it's normal insertable text not already covered
352 if (keysym.isText() && keyseq.length() == 1) {
353 LYXERR(Debug::KEY, "isText() is true, inserting.");
354 func = FuncRequest(LFUN_SELF_INSERT,
355 FuncRequest::KEYBOARD);
357 LYXERR(Debug::KEY, "Unknown, !isText() - giving up");
358 lyx_view_->message(_("Unknown function."));
359 lyx_view_->restartCursor();
364 if (func.action == LFUN_SELF_INSERT) {
365 if (encoded_last_key != 0) {
366 docstring const arg(1, encoded_last_key);
367 dispatch(FuncRequest(LFUN_SELF_INSERT, arg,
368 FuncRequest::KEYBOARD));
369 LYXERR(Debug::KEY, "SelfInsert arg[`" << to_utf8(arg) << "']");
370 lyx_view_->updateCompletion(true, true);
376 if (func.action == LFUN_CHAR_DELETE_BACKWARD)
377 // backspace is not a self-insertion. But it
378 // still should not hide the completion popup.
379 // FIXME: more clever way to detect those movements
380 lyx_view_->updateCompletion(false, true);
382 lyx_view_->updateCompletion(false, false);
386 lyx_view_->restartCursor();
390 FuncStatus LyXFunc::getStatus(FuncRequest const & cmd) const
392 //lyxerr << "LyXFunc::getStatus: cmd: " << cmd << endl;
395 Buffer * buf = lyx_view_? lyx_view_->buffer() : 0;
397 if (cmd.action == LFUN_NOACTION) {
398 flag.message(from_utf8(N_("Nothing to do")));
403 switch (cmd.action) {
404 case LFUN_UNKNOWN_ACTION:
405 #ifndef HAVE_LIBAIKSAURUS
406 case LFUN_THESAURUS_ENTRY:
416 if (flag.unknown()) {
417 flag.message(from_utf8(N_("Unknown action")));
421 if (!flag.enabled()) {
422 if (flag.message().empty())
423 flag.message(from_utf8(N_("Command disabled")));
427 // Check whether we need a buffer
428 if (!lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer) && !buf) {
430 flag.message(from_utf8(N_("Command not allowed with"
431 "out any document open")));
436 // I would really like to avoid having this switch and rather try to
437 // encode this in the function itself.
438 // -- And I'd rather let an inset decide which LFUNs it is willing
439 // to handle (Andre')
441 switch (cmd.action) {
443 // FIXME: these cases should be hidden in GuiApplication::getStatus().
444 case LFUN_WINDOW_CLOSE:
446 return theApp()->getStatus(cmd);
450 // FIXME: these cases should be hidden in GuiView::getStatus().
451 case LFUN_DIALOG_TOGGLE:
452 case LFUN_DIALOG_SHOW:
453 case LFUN_DIALOG_UPDATE:
454 if (cmd.argument() == "prefs"
455 || cmd.argument() == "aboutlyx")
458 return lyx_view_->getStatus(cmd);
463 case LFUN_TOOLBAR_TOGGLE:
464 case LFUN_INSET_APPLY:
465 case LFUN_BUFFER_WRITE:
466 case LFUN_BUFFER_WRITE_AS:
467 case LFUN_SPLIT_VIEW:
468 case LFUN_CLOSE_TAB_GROUP:
469 case LFUN_COMPLETION_POPUP:
470 case LFUN_COMPLETION_INLINE:
471 case LFUN_COMPLETION_COMPLETE:
473 return lyx_view_->getStatus(cmd);
477 case LFUN_BUFFER_TOGGLE_READ_ONLY:
478 flag.setOnOff(buf->isReadonly());
481 case LFUN_BUFFER_SWITCH:
482 // toggle on the current buffer, but do not toggle off
483 // the other ones (is that a good idea?)
484 if (buf && to_utf8(cmd.argument()) == buf->absFileName())
488 case LFUN_BUFFER_EXPORT:
489 enable = cmd.argument() == "custom"
490 || buf->isExportable(to_utf8(cmd.argument()));
493 case LFUN_BUFFER_CHKTEX:
494 enable = buf->isLatex() && !lyxrc.chktex_command.empty();
497 case LFUN_BUILD_PROGRAM:
498 enable = buf->isExportable("program");
501 case LFUN_VC_REGISTER:
502 enable = !buf->lyxvc().inUse();
504 case LFUN_VC_CHECK_IN:
505 enable = buf->lyxvc().inUse() && !buf->isReadonly();
507 case LFUN_VC_CHECK_OUT:
508 enable = buf->lyxvc().inUse() && buf->isReadonly();
511 case LFUN_VC_UNDO_LAST:
512 enable = buf->lyxvc().inUse();
514 case LFUN_BUFFER_RELOAD:
515 enable = !buf->isUnnamed() && buf->fileName().exists()
516 && (!buf->isClean() || buf->isExternallyModified(Buffer::timestamp_method));
519 case LFUN_CITATION_INSERT: {
520 FuncRequest fr(LFUN_INSET_INSERT, "citation");
521 enable = getStatus(fr).enabled();
525 // This could be used for the no-GUI version. The GUI version is handled in
526 // LyXView::getStatus(). See above.
528 case LFUN_BUFFER_WRITE:
529 case LFUN_BUFFER_WRITE_AS: {
530 Buffer * b = theBufferList().getBuffer(cmd.getArg(0));
531 enable = b && (b->isUnnamed() || !b->isClean());
536 case LFUN_BUFFER_WRITE_ALL: {
537 // We enable the command only if there are some modified buffers
538 Buffer * first = theBufferList().first();
543 // We cannot use a for loop as the buffer list is a cycle.
549 b = theBufferList().next(b);
550 } while (b != first);
554 case LFUN_BOOKMARK_GOTO: {
555 const unsigned int num = convert<unsigned int>(to_utf8(cmd.argument()));
556 enable = LyX::ref().session().bookmarks().isValid(num);
560 case LFUN_BOOKMARK_CLEAR:
561 enable = LyX::ref().session().bookmarks().size() > 0;
564 // this one is difficult to get right. As a half-baked
565 // solution, we consider only the first action of the sequence
566 case LFUN_COMMAND_SEQUENCE: {
567 // argument contains ';'-terminated commands
568 string const firstcmd = token(to_utf8(cmd.argument()), ';', 0);
569 FuncRequest func(lyxaction.lookupFunc(firstcmd));
570 func.origin = cmd.origin;
571 flag = getStatus(func);
577 string name = to_utf8(cmd.argument());
578 if (LyX::ref().topLevelCmdDef().lock(name, func)) {
579 func.origin = cmd.origin;
580 flag = getStatus(func);
581 LyX::ref().topLevelCmdDef().release(name);
583 // catch recursion or unknown command definiton
584 // all operations until the recursion or unknown command
585 // definiton occures are performed, so set the state to enabled
591 case LFUN_BUFFER_NEW:
592 case LFUN_BUFFER_NEW_TEMPLATE:
593 case LFUN_WORD_FIND_FORWARD:
594 case LFUN_WORD_FIND_BACKWARD:
595 case LFUN_COMMAND_PREFIX:
596 case LFUN_COMMAND_EXECUTE:
598 case LFUN_META_PREFIX:
599 case LFUN_BUFFER_CLOSE:
600 case LFUN_BUFFER_UPDATE:
601 case LFUN_BUFFER_VIEW:
602 case LFUN_MASTER_BUFFER_UPDATE:
603 case LFUN_MASTER_BUFFER_VIEW:
604 case LFUN_BUFFER_IMPORT:
605 case LFUN_BUFFER_AUTO_SAVE:
606 case LFUN_RECONFIGURE:
609 case LFUN_DROP_LAYOUTS_CHOICE:
611 case LFUN_SERVER_GET_NAME:
612 case LFUN_SERVER_NOTIFY:
613 case LFUN_SERVER_GOTO_FILE_ROW:
614 case LFUN_DIALOG_HIDE:
615 case LFUN_DIALOG_DISCONNECT_INSET:
616 case LFUN_BUFFER_CHILD_OPEN:
618 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
619 case LFUN_KEYMAP_OFF:
620 case LFUN_KEYMAP_PRIMARY:
621 case LFUN_KEYMAP_SECONDARY:
622 case LFUN_KEYMAP_TOGGLE:
624 case LFUN_BUFFER_EXPORT_CUSTOM:
625 case LFUN_BUFFER_PRINT:
626 case LFUN_PREFERENCES_SAVE:
627 case LFUN_SCREEN_FONT_UPDATE:
630 case LFUN_EXTERNAL_EDIT:
631 case LFUN_GRAPHICS_EDIT:
632 case LFUN_ALL_INSETS_TOGGLE:
633 case LFUN_BUFFER_LANGUAGE:
634 case LFUN_TEXTCLASS_APPLY:
635 case LFUN_TEXTCLASS_LOAD:
636 case LFUN_BUFFER_SAVE_AS_DEFAULT:
637 case LFUN_BUFFER_PARAMS_APPLY:
638 case LFUN_LAYOUT_MODULES_CLEAR:
639 case LFUN_LAYOUT_MODULE_ADD:
640 case LFUN_LAYOUT_RELOAD:
641 case LFUN_LYXRC_APPLY:
642 case LFUN_BUFFER_NEXT:
643 case LFUN_BUFFER_PREVIOUS:
644 case LFUN_WINDOW_NEW:
646 // these are handled in our dispatch()
654 if (!getLocalStatus(view()->cursor(), cmd, flag))
655 flag = view()->getStatus(cmd);
661 // Can we use a readonly buffer?
662 if (buf && buf->isReadonly()
663 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
664 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
665 flag.message(from_utf8(N_("Document is read-only")));
669 // Are we in a DELETED change-tracking region?
671 && lookupChangeType(view()->cursor(), true) == Change::DELETED
672 && !lyxaction.funcHasFlag(cmd.action, LyXAction::ReadOnly)
673 && !lyxaction.funcHasFlag(cmd.action, LyXAction::NoBuffer)) {
674 flag.message(from_utf8(N_("This portion of the document is deleted.")));
678 // the default error message if we disable the command
679 if (!flag.enabled() && flag.message().empty())
680 flag.message(from_utf8(N_("Command disabled")));
686 bool LyXFunc::ensureBufferClean(BufferView * bv)
688 Buffer & buf = bv->buffer();
692 docstring const file = buf.fileName().displayName(30);
693 docstring text = bformat(_("The document %1$s has unsaved "
694 "changes.\n\nDo you want to save "
695 "the document?"), file);
696 int const ret = Alert::prompt(_("Save changed document?"),
697 text, 0, 1, _("&Save"),
701 dispatch(FuncRequest(LFUN_BUFFER_WRITE));
703 return buf.isClean();
709 void showPrintError(string const & name)
711 docstring str = bformat(_("Could not print the document %1$s.\n"
712 "Check that your printer is set up correctly."),
713 makeDisplayPath(name, 50));
714 Alert::error(_("Print document failed"), str);
718 void loadTextClass(string const & name, string const & buf_path)
720 pair<bool, BaseClassIndex> const tc_pair =
721 baseclasslist.numberOfClass(name);
723 if (!tc_pair.first) {
724 lyxerr << "Document class \"" << name
725 << "\" does not exist."
730 BaseClassIndex const tc = tc_pair.second;
732 if (!baseclasslist[tc].load(buf_path)) {
733 docstring s = bformat(_("The document class %1$s."
734 "could not be loaded."),
735 from_utf8(baseclasslist[tc].name()));
736 Alert::error(_("Could not load class"), s);
741 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new);
746 void LyXFunc::dispatch(FuncRequest const & cmd)
748 string const argument = to_utf8(cmd.argument());
749 kb_action const action = cmd.action;
751 LYXERR(Debug::ACTION, "\nLyXFunc::dispatch: cmd: " << cmd);
752 //lyxerr << "LyXFunc::dispatch: cmd: " << cmd << endl;
754 // we have not done anything wrong yet.
756 dispatch_buffer.erase();
758 // redraw the screen at the end (first of the two drawing steps).
759 //This is done unless explicitely requested otherwise
760 Update::flags updateFlags = Update::FitCursor;
762 FuncStatus const flag = getStatus(cmd);
763 if (!flag.enabled()) {
764 // We cannot use this function here
765 LYXERR(Debug::ACTION, "LyXFunc::dispatch: "
766 << lyxaction.getActionName(action)
767 << " [" << action << "] is disabled at this location");
768 setErrorMessage(flag.message());
772 case LFUN_WORD_FIND_FORWARD:
773 case LFUN_WORD_FIND_BACKWARD: {
774 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
775 static docstring last_search;
776 docstring searched_string;
778 if (!cmd.argument().empty()) {
779 last_search = cmd.argument();
780 searched_string = cmd.argument();
782 searched_string = last_search;
785 if (searched_string.empty())
788 bool const fw = action == LFUN_WORD_FIND_FORWARD;
789 docstring const data =
790 find2string(searched_string, true, false, fw);
791 find(view(), FuncRequest(LFUN_WORD_FIND, data));
795 case LFUN_COMMAND_PREFIX:
796 BOOST_ASSERT(lyx_view_);
797 lyx_view_->message(keyseq.printOptions(true));
801 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
803 meta_fake_bit = NoModifier;
804 if (lyx_view_->buffer())
805 // cancel any selection
806 dispatch(FuncRequest(LFUN_MARK_OFF));
807 setMessage(from_ascii(N_("Cancel")));
810 case LFUN_META_PREFIX:
811 meta_fake_bit = AltModifier;
812 setMessage(keyseq.print(KeySequence::ForGui));
815 case LFUN_BUFFER_TOGGLE_READ_ONLY: {
816 BOOST_ASSERT(lyx_view_ && lyx_view_->view() && lyx_view_->buffer());
817 Buffer * buf = lyx_view_->buffer();
818 if (buf->lyxvc().inUse())
819 buf->lyxvc().toggleReadOnly();
821 buf->setReadonly(!lyx_view_->buffer()->isReadonly());
825 // --- Menus -----------------------------------------------
826 case LFUN_BUFFER_NEW:
827 lyx_view_->newDocument(argument, false);
828 updateFlags = Update::None;
831 case LFUN_BUFFER_NEW_TEMPLATE:
832 lyx_view_->newDocument(argument, true);
833 updateFlags = Update::None;
836 case LFUN_BUFFER_CLOSE:
837 lyx_view_->closeBuffer();
838 updateFlags = Update::None;
841 case LFUN_BUFFER_RELOAD: {
842 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
843 docstring const file = makeDisplayPath(lyx_view_->buffer()->absFileName(), 20);
844 docstring text = bformat(_("Any changes will be lost. Are you sure "
845 "you want to revert to the saved version of the document %1$s?"), file);
846 int const ret = Alert::prompt(_("Revert to saved document?"),
847 text, 1, 1, _("&Revert"), _("&Cancel"));
854 case LFUN_BUFFER_UPDATE:
855 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
856 lyx_view_->buffer()->doExport(argument, true);
859 case LFUN_BUFFER_VIEW:
860 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
861 lyx_view_->buffer()->preview(argument);
864 case LFUN_MASTER_BUFFER_UPDATE:
865 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
866 lyx_view_->buffer()->masterBuffer()->doExport(argument, true);
869 case LFUN_MASTER_BUFFER_VIEW:
870 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer() && lyx_view_->buffer()->masterBuffer());
871 lyx_view_->buffer()->masterBuffer()->preview(argument);
874 case LFUN_BUILD_PROGRAM:
875 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
876 lyx_view_->buffer()->doExport("program", true);
879 case LFUN_BUFFER_CHKTEX:
880 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
881 lyx_view_->buffer()->runChktex();
884 case LFUN_BUFFER_EXPORT:
885 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
886 if (argument == "custom")
887 dispatch(FuncRequest(LFUN_DIALOG_SHOW, "sendto"));
889 lyx_view_->buffer()->doExport(argument, false);
892 case LFUN_BUFFER_EXPORT_CUSTOM: {
893 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
895 string command = split(argument, format_name, ' ');
896 Format const * format = formats.getFormat(format_name);
898 lyxerr << "Format \"" << format_name
899 << "\" not recognized!"
904 Buffer * buffer = lyx_view_->buffer();
906 // The name of the file created by the conversion process
909 // Output to filename
910 if (format->name() == "lyx") {
911 string const latexname = buffer->latexName(false);
912 filename = changeExtension(latexname,
913 format->extension());
914 filename = addName(buffer->temppath(), filename);
916 if (!buffer->writeFile(FileName(filename)))
920 buffer->doExport(format_name, true, filename);
923 // Substitute $$FName for filename
924 if (!contains(command, "$$FName"))
925 command = "( " + command + " ) < $$FName";
926 command = subst(command, "$$FName", filename);
928 // Execute the command in the background
930 call.startscript(Systemcall::DontWait, command);
934 case LFUN_BUFFER_PRINT: {
935 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
936 // FIXME: cmd.getArg() might fail if one of the arguments
937 // contains double quotes
938 string target = cmd.getArg(0);
939 string target_name = cmd.getArg(1);
940 string command = cmd.getArg(2);
943 || target_name.empty()
944 || command.empty()) {
945 lyxerr << "Unable to parse \""
946 << argument << '"' << endl;
949 if (target != "printer" && target != "file") {
950 lyxerr << "Unrecognized target \""
951 << target << '"' << endl;
955 Buffer * buffer = lyx_view_->buffer();
957 if (!buffer->doExport("dvi", true)) {
958 showPrintError(buffer->absFileName());
962 // Push directory path.
963 string const path = buffer->temppath();
964 // Prevent the compiler from optimizing away p
968 // there are three cases here:
969 // 1. we print to a file
970 // 2. we print directly to a printer
971 // 3. we print using a spool command (print to file first)
974 string const dviname =
975 changeExtension(buffer->latexName(true), "dvi");
977 if (target == "printer") {
978 if (!lyxrc.print_spool_command.empty()) {
979 // case 3: print using a spool
980 string const psname =
981 changeExtension(dviname,".ps");
982 command += ' ' + lyxrc.print_to_file
985 + quoteName(dviname);
988 lyxrc.print_spool_command + ' ';
989 if (target_name != "default") {
990 command2 += lyxrc.print_spool_printerprefix
994 command2 += quoteName(psname);
996 // If successful, then spool command
997 res = one.startscript(
1002 res = one.startscript(
1003 Systemcall::DontWait,
1006 // case 2: print directly to a printer
1007 if (target_name != "default")
1008 command += ' ' + lyxrc.print_to_printer + target_name + ' ';
1009 res = one.startscript(
1010 Systemcall::DontWait,
1011 command + quoteName(dviname));
1015 // case 1: print to a file
1016 FileName const filename(makeAbsPath(target_name,
1017 lyx_view_->buffer()->filePath()));
1018 FileName const dvifile(makeAbsPath(dviname, path));
1019 if (filename.exists()) {
1020 docstring text = bformat(
1021 _("The file %1$s already exists.\n\n"
1022 "Do you want to overwrite that file?"),
1023 makeDisplayPath(filename.absFilename()));
1024 if (Alert::prompt(_("Overwrite file?"),
1025 text, 0, 1, _("&Overwrite"), _("&Cancel")) != 0)
1028 command += ' ' + lyxrc.print_to_file
1029 + quoteName(filename.toFilesystemEncoding())
1031 + quoteName(dvifile.toFilesystemEncoding());
1032 res = one.startscript(Systemcall::DontWait,
1037 showPrintError(buffer->absFileName());
1041 // FIXME: There is need for a command-line import.
1043 case LFUN_BUFFER_IMPORT:
1048 case LFUN_BUFFER_AUTO_SAVE:
1049 lyx_view_->buffer()->autoSave();
1052 case LFUN_RECONFIGURE:
1053 BOOST_ASSERT(lyx_view_);
1054 // argument is any additional parameter to the configure.py command
1055 reconfigure(*lyx_view_, argument);
1058 case LFUN_HELP_OPEN: {
1059 BOOST_ASSERT(lyx_view_);
1060 string const arg = argument;
1062 setErrorMessage(from_ascii(N_("Missing argument")));
1065 FileName const fname = i18nLibFileSearch("doc", arg, "lyx");
1066 if (fname.empty()) {
1067 lyxerr << "LyX: unable to find documentation file `"
1068 << arg << "'. Bad installation?" << endl;
1071 lyx_view_->message(bformat(_("Opening help file %1$s..."),
1072 makeDisplayPath(fname.absFilename())));
1073 Buffer * buf = lyx_view_->loadDocument(fname, false);
1076 lyx_view_->setBuffer(buf);
1077 buf->errors("Parse");
1079 updateFlags = Update::None;
1083 // --- version control -------------------------------
1084 case LFUN_VC_REGISTER:
1085 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1086 if (!ensureBufferClean(view()))
1088 if (!lyx_view_->buffer()->lyxvc().inUse()) {
1089 lyx_view_->buffer()->lyxvc().registrer();
1092 updateFlags = Update::Force;
1095 case LFUN_VC_CHECK_IN:
1096 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1097 if (!ensureBufferClean(view()))
1099 if (lyx_view_->buffer()->lyxvc().inUse()
1100 && !lyx_view_->buffer()->isReadonly()) {
1101 lyx_view_->buffer()->lyxvc().checkIn();
1106 case LFUN_VC_CHECK_OUT:
1107 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1108 if (!ensureBufferClean(view()))
1110 if (lyx_view_->buffer()->lyxvc().inUse()
1111 && lyx_view_->buffer()->isReadonly()) {
1112 lyx_view_->buffer()->lyxvc().checkOut();
1117 case LFUN_VC_REVERT:
1118 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1119 lyx_view_->buffer()->lyxvc().revert();
1123 case LFUN_VC_UNDO_LAST:
1124 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1125 lyx_view_->buffer()->lyxvc().undoLast();
1129 // --- lyxserver commands ----------------------------
1130 case LFUN_SERVER_GET_NAME:
1131 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1132 setMessage(from_utf8(lyx_view_->buffer()->absFileName()));
1133 LYXERR(Debug::INFO, "FNAME["
1134 << lyx_view_->buffer()->absFileName() << ']');
1137 case LFUN_SERVER_NOTIFY:
1138 dispatch_buffer = keyseq.print(KeySequence::Portable);
1139 theServer().notifyClient(to_utf8(dispatch_buffer));
1142 case LFUN_SERVER_GOTO_FILE_ROW: {
1143 BOOST_ASSERT(lyx_view_);
1146 istringstream is(argument);
1147 is >> file_name >> row;
1149 bool loaded = false;
1150 if (prefixIs(file_name, package().temp_dir().absFilename()))
1151 // Needed by inverse dvi search. If it is a file
1152 // in tmpdir, call the apropriated function
1153 buf = theBufferList().getBufferFromTmp(file_name);
1155 // Must replace extension of the file to be .lyx
1156 // and get full path
1157 FileName const s = fileSearch(string(), changeExtension(file_name, ".lyx"), "lyx");
1158 // Either change buffer or load the file
1159 if (theBufferList().exists(s.absFilename()))
1160 buf = theBufferList().getBuffer(s.absFilename());
1162 buf = lyx_view_->loadDocument(s);
1168 updateFlags = Update::None;
1173 lyx_view_->setBuffer(buf);
1174 view()->setCursorFromRow(row);
1176 buf->errors("Parse");
1177 updateFlags = Update::FitCursor;
1182 case LFUN_DIALOG_SHOW_NEW_INSET: {
1183 BOOST_ASSERT(lyx_view_);
1184 string const name = cmd.getArg(0);
1185 InsetCode code = insetCode(name);
1186 string data = trim(to_utf8(cmd.argument()).substr(name.size()));
1187 bool insetCodeOK = true;
1196 case HYPERLINK_CODE: {
1197 InsetCommandParams p(code);
1198 data = InsetCommandMailer::params2string(name, p);
1201 case INCLUDE_CODE: {
1202 // data is the include type: one of "include",
1203 // "input", "verbatiminput" or "verbatiminput*"
1205 // default type is requested
1207 InsetCommandParams p(INCLUDE_CODE, data);
1208 data = InsetCommandMailer::params2string("include", p);
1212 // \c data == "Boxed" || "Frameless" etc
1213 InsetBoxParams p(data);
1214 data = InsetBoxMailer::params2string(p);
1218 InsetBranchParams p;
1219 data = InsetBranchMailer::params2string(p);
1223 InsetCommandParams p(CITE_CODE);
1224 data = InsetCommandMailer::params2string(name, p);
1228 data = InsetERTMailer::params2string(InsetCollapsable::Open);
1231 case EXTERNAL_CODE: {
1232 InsetExternalParams p;
1233 Buffer const & buffer = *lyx_view_->buffer();
1234 data = InsetExternalMailer::params2string(p, buffer);
1239 data = InsetFloatMailer::params2string(p);
1242 case LISTINGS_CODE: {
1243 InsetListingsParams p;
1244 data = InsetListingsMailer::params2string(p);
1247 case GRAPHICS_CODE: {
1248 InsetGraphicsParams p;
1249 Buffer const & buffer = *lyx_view_->buffer();
1250 data = InsetGraphicsMailer::params2string(p, buffer);
1255 data = InsetNoteMailer::params2string(p);
1260 data = InsetVSpaceMailer::params2string(space);
1265 data = InsetWrapMailer::params2string(p);
1269 lyxerr << "Inset type '" << name <<
1270 "' not recognized in LFUN_DIALOG_SHOW_NEW_INSET" << endl;
1271 insetCodeOK = false;
1273 } // end switch(code)
1275 dispatch(FuncRequest(LFUN_DIALOG_SHOW, name + " " + data));
1279 case LFUN_CITATION_INSERT: {
1280 BOOST_ASSERT(lyx_view_);
1281 if (!argument.empty()) {
1282 // we can have one optional argument, delimited by '|'
1283 // citation-insert <key>|<text_before>
1284 // this should be enhanced to also support text_after
1285 // and citation style
1286 string arg = argument;
1288 if (contains(argument, "|")) {
1289 arg = token(argument, '|', 0);
1290 opt1 = token(argument, '|', 1);
1292 InsetCommandParams icp(CITE_CODE);
1293 icp["key"] = from_utf8(arg);
1295 icp["before"] = from_utf8(opt1);
1296 string icstr = InsetCommandMailer::params2string("citation", icp);
1297 FuncRequest fr(LFUN_INSET_INSERT, icstr);
1300 dispatch(FuncRequest(LFUN_DIALOG_SHOW_NEW_INSET, "citation"));
1304 case LFUN_BUFFER_CHILD_OPEN: {
1305 BOOST_ASSERT(lyx_view_ && lyx_view_->buffer());
1306 Buffer * parent = lyx_view_->buffer();
1307 FileName filename = makeAbsPath(argument, parent->filePath());
1308 view()->saveBookmark(false);
1310 bool parsed = false;
1311 if (theBufferList().exists(filename.absFilename())) {
1312 child = theBufferList().getBuffer(filename.absFilename());
1314 setMessage(bformat(_("Opening child document %1$s..."),
1315 makeDisplayPath(filename.absFilename())));
1316 child = lyx_view_->loadDocument(filename, false);
1320 // Set the parent name of the child document.
1321 // This makes insertion of citations and references in the child work,
1322 // when the target is in the parent or another child document.
1323 child->setParent(parent);
1324 updateLabels(*child->masterBuffer());
1325 lyx_view_->setBuffer(child);
1327 child->errors("Parse");
1330 // If a screen update is required (in case where auto_open is false),
1331 // setBuffer() would have taken care of it already. Otherwise we shall
1332 // reset the update flag because it can cause a circular problem.
1334 updateFlags = Update::None;
1338 case LFUN_TOGGLE_CURSOR_FOLLOWS_SCROLLBAR:
1339 BOOST_ASSERT(lyx_view_);
1340 lyxrc.cursor_follows_scrollbar = !lyxrc.cursor_follows_scrollbar;
1343 case LFUN_KEYMAP_OFF:
1344 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1345 lyx_view_->view()->getIntl().keyMapOn(false);
1348 case LFUN_KEYMAP_PRIMARY:
1349 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1350 lyx_view_->view()->getIntl().keyMapPrim();
1353 case LFUN_KEYMAP_SECONDARY:
1354 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1355 lyx_view_->view()->getIntl().keyMapSec();
1358 case LFUN_KEYMAP_TOGGLE:
1359 BOOST_ASSERT(lyx_view_ && lyx_view_->view());
1360 lyx_view_->view()->getIntl().toggleKeyMap();
1366 string rest = split(argument, countstr, ' ');
1367 istringstream is(countstr);
1370 lyxerr << "repeat: count: " << count << " cmd: " << rest << endl;
1371 for (int i = 0; i < count; ++i)
1372 dispatch(lyxaction.lookupFunc(rest));
1376 case LFUN_COMMAND_SEQUENCE: {
1377 // argument contains ';'-terminated commands
1378 string arg = argument;
1379 while (!arg.empty()) {
1381 arg = split(arg, first, ';');
1382 FuncRequest func(lyxaction.lookupFunc(first));
1383 func.origin = cmd.origin;
1391 if (LyX::ref().topLevelCmdDef().lock(argument, func)) {
1392 func.origin = cmd.origin;
1394 LyX::ref().topLevelCmdDef().release(argument);
1396 if (func.action == LFUN_UNKNOWN_ACTION) {
1397 // unknown command definition
1398 lyxerr << "Warning: unknown command definition `"
1402 // recursion detected
1403 lyxerr << "Warning: Recursion in the command definition `"
1404 << argument << "' detected"
1411 case LFUN_PREFERENCES_SAVE: {
1412 lyxrc.write(makeAbsPath("preferences",
1413 package().user_support().absFilename()),
1418 case LFUN_SET_COLOR: {
1420 string const x11_name = split(argument, lyx_name, ' ');
1421 if (lyx_name.empty() || x11_name.empty()) {
1422 setErrorMessage(from_ascii(N_(
1423 "Syntax: set-color <lyx_name>"
1428 bool const graphicsbg_changed =
1429 (lyx_name == lcolor.getLyXName(Color_graphicsbg) &&
1430 x11_name != lcolor.getX11Name(Color_graphicsbg));
1432 if (!lcolor.setColor(lyx_name, x11_name)) {
1434 bformat(_("Set-color \"%1$s\" failed "
1435 "- color is undefined or "
1436 "may not be redefined"),
1437 from_utf8(lyx_name)));
1441 theApp()->updateColor(lcolor.getFromLyXName(lyx_name));
1443 if (graphicsbg_changed) {
1444 // FIXME: The graphics cache no longer has a changeDisplay method.
1446 graphics::GCache::get().changeDisplay(true);
1453 BOOST_ASSERT(lyx_view_);
1454 lyx_view_->message(from_utf8(argument));
1457 case LFUN_EXTERNAL_EDIT: {
1458 BOOST_ASSERT(lyx_view_);
1459 FuncRequest fr(action, argument);
1460 InsetExternal().dispatch(view()->cursor(), fr);
1464 case LFUN_GRAPHICS_EDIT: {
1465 FuncRequest fr(action, argument);
1466 InsetGraphics().dispatch(view()->cursor(), fr);
1470 case LFUN_ALL_INSETS_TOGGLE: {
1471 BOOST_ASSERT(lyx_view_);
1473 string const name = split(argument, action, ' ');
1474 InsetCode const inset_code = insetCode(name);
1476 Cursor & cur = view()->cursor();
1477 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1479 Inset & inset = lyx_view_->buffer()->inset();
1480 InsetIterator it = inset_iterator_begin(inset);
1481 InsetIterator const end = inset_iterator_end(inset);
1482 for (; it != end; ++it) {
1483 if (!it->asInsetMath()
1484 && (inset_code == NO_CODE
1485 || inset_code == it->lyxCode())) {
1486 Cursor tmpcur = cur;
1487 tmpcur.pushBackward(*it);
1488 it->dispatch(tmpcur, fr);
1491 updateFlags = Update::Force | Update::FitCursor;
1495 case LFUN_BUFFER_LANGUAGE: {
1496 BOOST_ASSERT(lyx_view_);
1497 Buffer & buffer = *lyx_view_->buffer();
1498 Language const * oldL = buffer.params().language;
1499 Language const * newL = languages.getLanguage(argument);
1500 if (!newL || oldL == newL)
1503 if (oldL->rightToLeft() == newL->rightToLeft()
1504 && !buffer.isMultiLingual())
1505 buffer.changeLanguage(oldL, newL);
1509 case LFUN_BUFFER_SAVE_AS_DEFAULT: {
1510 string const fname =
1511 addName(addPath(package().user_support().absFilename(), "templates/"),
1513 Buffer defaults(fname);
1515 istringstream ss(argument);
1518 int const unknown_tokens = defaults.readHeader(lex);
1520 if (unknown_tokens != 0) {
1521 lyxerr << "Warning in LFUN_BUFFER_SAVE_AS_DEFAULT!\n"
1522 << unknown_tokens << " unknown token"
1523 << (unknown_tokens == 1 ? "" : "s")
1527 if (defaults.writeFile(FileName(defaults.absFileName())))
1528 setMessage(bformat(_("Document defaults saved in %1$s"),
1529 makeDisplayPath(fname)));
1531 setErrorMessage(from_ascii(N_("Unable to save document defaults")));
1535 case LFUN_BUFFER_PARAMS_APPLY: {
1536 BOOST_ASSERT(lyx_view_);
1537 biblio::CiteEngine const oldEngine =
1538 lyx_view_->buffer()->params().getEngine();
1540 Buffer * buffer = lyx_view_->buffer();
1542 TextClassPtr oldClass = buffer->params().textClassPtr();
1544 Cursor & cur = view()->cursor();
1545 cur.recordUndoFullDocument();
1547 istringstream ss(argument);
1550 int const unknown_tokens = buffer->readHeader(lex);
1552 if (unknown_tokens != 0) {
1553 lyxerr << "Warning in LFUN_BUFFER_PARAMS_APPLY!\n"
1554 << unknown_tokens << " unknown token"
1555 << (unknown_tokens == 1 ? "" : "s")
1559 updateLayout(oldClass, buffer);
1561 biblio::CiteEngine const newEngine =
1562 lyx_view_->buffer()->params().getEngine();
1564 if (oldEngine != newEngine) {
1565 FuncRequest fr(LFUN_INSET_REFRESH);
1567 Inset & inset = lyx_view_->buffer()->inset();
1568 InsetIterator it = inset_iterator_begin(inset);
1569 InsetIterator const end = inset_iterator_end(inset);
1570 for (; it != end; ++it)
1571 if (it->lyxCode() == CITE_CODE)
1572 it->dispatch(cur, fr);
1575 updateFlags = Update::Force | Update::FitCursor;
1576 // We are here most certainaly because of a change in the document
1577 // It is then better to make sure that all dialogs are in sync
1578 // with current document settings. LyXView::restartCursor() achieve this.
1579 lyx_view_->restartCursor();
1583 case LFUN_LAYOUT_MODULES_CLEAR: {
1584 BOOST_ASSERT(lyx_view_);
1585 Buffer * buffer = lyx_view_->buffer();
1586 TextClassPtr oldClass = buffer->params().textClassPtr();
1587 view()->cursor().recordUndoFullDocument();
1588 buffer->params().clearLayoutModules();
1589 buffer->params().makeTextClass();
1590 updateLayout(oldClass, buffer);
1591 updateFlags = Update::Force | Update::FitCursor;
1595 case LFUN_LAYOUT_MODULE_ADD: {
1596 BOOST_ASSERT(lyx_view_);
1597 Buffer * buffer = lyx_view_->buffer();
1598 TextClassPtr oldClass = buffer->params().textClassPtr();
1599 view()->cursor().recordUndoFullDocument();
1600 buffer->params().addLayoutModule(argument);
1601 buffer->params().makeTextClass();
1602 updateLayout(oldClass, buffer);
1603 updateFlags = Update::Force | Update::FitCursor;
1607 case LFUN_TEXTCLASS_APPLY: {
1608 BOOST_ASSERT(lyx_view_);
1609 Buffer * buffer = lyx_view_->buffer();
1611 loadTextClass(argument, buffer->filePath());
1613 pair<bool, BaseClassIndex> const tc_pair =
1614 baseclasslist.numberOfClass(argument);
1619 BaseClassIndex const old_class = buffer->params().baseClass();
1620 BaseClassIndex const new_class = tc_pair.second;
1622 if (old_class == new_class)
1626 //Save the old, possibly modular, layout for use in conversion.
1627 TextClassPtr oldClass = buffer->params().textClassPtr();
1628 view()->cursor().recordUndoFullDocument();
1629 buffer->params().setBaseClass(new_class);
1630 buffer->params().makeTextClass();
1631 updateLayout(oldClass, buffer);
1632 updateFlags = Update::Force | Update::FitCursor;
1636 case LFUN_LAYOUT_RELOAD: {
1637 BOOST_ASSERT(lyx_view_);
1638 Buffer * buffer = lyx_view_->buffer();
1639 TextClassPtr oldClass = buffer->params().textClassPtr();
1640 BaseClassIndex const tc = buffer->params().baseClass();
1641 baseclasslist.reset(tc);
1642 buffer->params().setBaseClass(tc);
1643 buffer->params().makeTextClass();
1644 updateLayout(oldClass, buffer);
1645 updateFlags = Update::Force | Update::FitCursor;
1649 case LFUN_TEXTCLASS_LOAD:
1650 loadTextClass(argument, lyx_view_->buffer()->filePath());
1653 case LFUN_LYXRC_APPLY: {
1654 LyXRC const lyxrc_orig = lyxrc;
1656 istringstream ss(argument);
1657 bool const success = lyxrc.read(ss) == 0;
1660 lyxerr << "Warning in LFUN_LYXRC_APPLY!\n"
1661 << "Unable to read lyxrc data"
1666 actOnUpdatedPrefs(lyxrc_orig, lyxrc);
1668 theApp()->resetGui();
1670 /// We force the redraw in any case because there might be
1671 /// some screen font changes.
1672 /// FIXME: only the current view will be updated. the Gui
1673 /// class is able to furnish the list of views.
1674 updateFlags = Update::Force;
1678 case LFUN_BOOKMARK_GOTO:
1679 // go to bookmark, open unopened file and switch to buffer if necessary
1680 gotoBookmark(convert<unsigned int>(to_utf8(cmd.argument())), true, true);
1681 updateFlags = Update::FitCursor;
1684 case LFUN_BOOKMARK_CLEAR:
1685 LyX::ref().session().bookmarks().clear();
1689 BOOST_ASSERT(theApp());
1690 // Let the frontend dispatch its own actions.
1691 if (theApp()->dispatch(cmd))
1692 // Nothing more to do.
1695 // Let the current LyXView dispatch its own actions.
1696 BOOST_ASSERT(lyx_view_);
1697 if (lyx_view_->dispatch(cmd)) {
1698 if (lyx_view_->view())
1699 updateFlags = lyx_view_->view()->cursor().result().update();
1703 BOOST_ASSERT(lyx_view_->view());
1704 // Let the current BufferView dispatch its own actions.
1705 if (view()->dispatch(cmd)) {
1706 // The BufferView took care of its own updates if needed.
1707 updateFlags = Update::None;
1711 // Let the current Cursor dispatch its own actions.
1712 Cursor old = view()->cursor();
1713 view()->cursor().getPos(cursorPosBeforeDispatchX_,
1714 cursorPosBeforeDispatchY_);
1715 view()->cursor().dispatch(cmd);
1717 // notify insets we just left
1718 if (view()->cursor() != old) {
1720 bool badcursor = notifyCursorLeaves(old, view()->cursor());
1722 view()->cursor().fixIfBroken();
1725 updateFlags = view()->cursor().result().update();
1728 if (lyx_view_ && lyx_view_->buffer()) {
1729 // BufferView::update() updates the ViewMetricsInfo and
1730 // also initializes the position cache for all insets in
1731 // (at least partially) visible top-level paragraphs.
1732 // We will redraw the screen only if needed.
1733 view()->processUpdateFlags(updateFlags);
1735 // if we executed a mutating lfun, mark the buffer as dirty
1737 && !lyxaction.funcHasFlag(action, LyXAction::NoBuffer)
1738 && !lyxaction.funcHasFlag(action, LyXAction::ReadOnly))
1739 lyx_view_->buffer()->markDirty();
1741 //Do we have a selection?
1742 theSelection().haveSelection(view()->cursor().selection());
1746 // Some messages may already be translated, so we cannot use _()
1747 sendDispatchMessage(translateIfPossible(getMessage()), cmd);
1752 void LyXFunc::sendDispatchMessage(docstring const & msg, FuncRequest const & cmd)
1754 const bool verbose = (cmd.origin == FuncRequest::MENU
1755 || cmd.origin == FuncRequest::TOOLBAR
1756 || cmd.origin == FuncRequest::COMMANDBUFFER);
1758 if (cmd.action == LFUN_SELF_INSERT || !verbose) {
1759 LYXERR(Debug::ACTION, "dispatch msg is " << to_utf8(msg));
1761 lyx_view_->message(msg);
1765 docstring dispatch_msg = msg;
1766 if (!dispatch_msg.empty())
1767 dispatch_msg += ' ';
1769 docstring comname = from_utf8(lyxaction.getActionName(cmd.action));
1771 bool argsadded = false;
1773 if (!cmd.argument().empty()) {
1774 if (cmd.action != LFUN_UNKNOWN_ACTION) {
1775 comname += ' ' + cmd.argument();
1780 docstring const shortcuts = theTopLevelKeymap().printBindings(cmd);
1782 if (!shortcuts.empty())
1783 comname += ": " + shortcuts;
1784 else if (!argsadded && !cmd.argument().empty())
1785 comname += ' ' + cmd.argument();
1787 if (!comname.empty()) {
1788 comname = rtrim(comname);
1789 dispatch_msg += '(' + rtrim(comname) + ')';
1792 LYXERR(Debug::ACTION, "verbose dispatch msg " << to_utf8(dispatch_msg));
1793 if (!dispatch_msg.empty())
1794 lyx_view_->message(dispatch_msg);
1798 void LyXFunc::reloadBuffer()
1800 FileName filename = lyx_view_->buffer()->fileName();
1801 // The user has already confirmed that the changes, if any, should
1802 // be discarded. So we just release the Buffer and don't call closeBuffer();
1803 theBufferList().release(lyx_view_->buffer());
1804 Buffer * buf = lyx_view_->loadDocument(filename);
1805 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1809 lyx_view_->setBuffer(buf);
1810 buf->errors("Parse");
1811 str = bformat(_("Document %1$s reloaded."), disp_fn);
1813 str = bformat(_("Could not reload document %1$s"), disp_fn);
1815 lyx_view_->message(str);
1818 // Each "lyx_view_" should have it's own message method. lyxview and
1819 // the minibuffer would use the minibuffer, but lyxserver would
1820 // send an ERROR signal to its client. Alejandro 970603
1821 // This function is bit problematic when it comes to NLS, to make the
1822 // lyx servers client be language indepenent we must not translate
1823 // strings sent to this func.
1824 void LyXFunc::setErrorMessage(docstring const & m) const
1826 dispatch_buffer = m;
1831 void LyXFunc::setMessage(docstring const & m) const
1833 dispatch_buffer = m;
1837 docstring const LyXFunc::viewStatusMessage()
1839 // When meta-fake key is pressed, show the key sequence so far + "M-".
1841 return keyseq.print(KeySequence::ForGui) + "M-";
1843 // Else, when a non-complete key sequence is pressed,
1844 // show the available options.
1845 if (keyseq.length() > 0 && !keyseq.deleted())
1846 return keyseq.printOptions(true);
1848 BOOST_ASSERT(lyx_view_);
1849 if (!lyx_view_->buffer())
1850 return _("Welcome to LyX!");
1852 return view()->cursor().currentState();
1856 BufferView * LyXFunc::view() const
1858 BOOST_ASSERT(lyx_view_);
1859 return lyx_view_->view();
1863 bool LyXFunc::wasMetaKey() const
1865 return (meta_fake_bit != NoModifier);
1869 void LyXFunc::updateLayout(TextClassPtr oldlayout,Buffer * buffer)
1871 lyx_view_->message(_("Converting document to new document class..."));
1873 StableDocIterator backcur(view()->cursor());
1874 ErrorList & el = buffer->errorList("Class Switch");
1875 cap::switchBetweenClasses(
1876 oldlayout, buffer->params().textClassPtr(),
1877 static_cast<InsetText &>(buffer->inset()), el);
1879 view()->setCursor(backcur.asDocIterator(&(buffer->inset())));
1881 buffer->errors("Class Switch");
1882 updateLabels(*buffer);
1888 void actOnUpdatedPrefs(LyXRC const & lyxrc_orig, LyXRC const & lyxrc_new)
1890 // Why the switch you might ask. It is a trick to ensure that all
1891 // the elements in the LyXRCTags enum is handled. As you can see
1892 // there are no breaks at all. So it is just a huge fall-through.
1893 // The nice thing is that we will get a warning from the compiler
1894 // if we forget an element.
1895 LyXRC::LyXRCTags tag = LyXRC::RC_LAST;
1897 case LyXRC::RC_ACCEPT_COMPOUND:
1898 case LyXRC::RC_ALT_LANG:
1899 case LyXRC::RC_PLAINTEXT_ROFF_COMMAND:
1900 case LyXRC::RC_PLAINTEXT_LINELEN:
1901 case LyXRC::RC_AUTOREGIONDELETE:
1902 case LyXRC::RC_AUTORESET_OPTIONS:
1903 case LyXRC::RC_AUTOSAVE:
1904 case LyXRC::RC_AUTO_NUMBER:
1905 case LyXRC::RC_BACKUPDIR_PATH:
1906 case LyXRC::RC_BIBTEX_COMMAND:
1907 case LyXRC::RC_BINDFILE:
1908 case LyXRC::RC_CHECKLASTFILES:
1909 case LyXRC::RC_COMPLETION_INLINE_DELAY:
1910 case LyXRC::RC_COMPLETION_INLINE_DOTS:
1911 case LyXRC::RC_COMPLETION_INLINE_MATH:
1912 case LyXRC::RC_COMPLETION_INLINE_TEXT:
1913 case LyXRC::RC_COMPLETION_POPUP_AFTER_COMPLETE:
1914 case LyXRC::RC_COMPLETION_POPUP_DELAY:
1915 case LyXRC::RC_COMPLETION_POPUP_MATH:
1916 case LyXRC::RC_COMPLETION_POPUP_TEXT:
1917 case LyXRC::RC_USELASTFILEPOS:
1918 case LyXRC::RC_LOADSESSION:
1919 case LyXRC::RC_CHKTEX_COMMAND:
1920 case LyXRC::RC_CONVERTER:
1921 case LyXRC::RC_CONVERTER_CACHE_MAXAGE:
1922 case LyXRC::RC_COPIER:
1923 case LyXRC::RC_CURSOR_FOLLOWS_SCROLLBAR:
1924 case LyXRC::RC_CUSTOM_EXPORT_COMMAND:
1925 case LyXRC::RC_CUSTOM_EXPORT_FORMAT:
1926 case LyXRC::RC_DATE_INSERT_FORMAT:
1927 case LyXRC::RC_DEFAULT_LANGUAGE:
1928 case LyXRC::RC_DEFAULT_PAPERSIZE:
1929 case LyXRC::RC_DEFFILE:
1930 case LyXRC::RC_DIALOGS_ICONIFY_WITH_MAIN:
1931 case LyXRC::RC_DISPLAY_GRAPHICS:
1932 case LyXRC::RC_DOCUMENTPATH:
1933 if (lyxrc_orig.document_path != lyxrc_new.document_path) {
1934 FileName path(lyxrc_new.document_path);
1935 if (path.exists() && path.isDirectory())
1936 package().document_dir() = FileName(lyxrc.document_path);
1938 case LyXRC::RC_ESC_CHARS:
1939 case LyXRC::RC_EXAMPLEPATH:
1940 case LyXRC::RC_FONT_ENCODING:
1941 case LyXRC::RC_FORMAT:
1942 case LyXRC::RC_INDEX_COMMAND:
1943 case LyXRC::RC_INPUT:
1944 case LyXRC::RC_KBMAP:
1945 case LyXRC::RC_KBMAP_PRIMARY:
1946 case LyXRC::RC_KBMAP_SECONDARY:
1947 case LyXRC::RC_LABEL_INIT_LENGTH:
1948 case LyXRC::RC_LANGUAGE_AUTO_BEGIN:
1949 case LyXRC::RC_LANGUAGE_AUTO_END:
1950 case LyXRC::RC_LANGUAGE_COMMAND_BEGIN:
1951 case LyXRC::RC_LANGUAGE_COMMAND_END:
1952 case LyXRC::RC_LANGUAGE_COMMAND_LOCAL:
1953 case LyXRC::RC_LANGUAGE_GLOBAL_OPTIONS:
1954 case LyXRC::RC_LANGUAGE_PACKAGE:
1955 case LyXRC::RC_LANGUAGE_USE_BABEL:
1956 case LyXRC::RC_MACRO_EDIT_STYLE:
1957 case LyXRC::RC_MAKE_BACKUP:
1958 case LyXRC::RC_MARK_FOREIGN_LANGUAGE:
1959 case LyXRC::RC_MOUSE_WHEEL_SPEED:
1960 case LyXRC::RC_NUMLASTFILES:
1961 case LyXRC::RC_PATH_PREFIX:
1962 if (lyxrc_orig.path_prefix != lyxrc_new.path_prefix) {
1963 prependEnvPath("PATH", lyxrc.path_prefix);
1965 case LyXRC::RC_PERS_DICT:
1966 case LyXRC::RC_PREVIEW:
1967 case LyXRC::RC_PREVIEW_HASHED_LABELS:
1968 case LyXRC::RC_PREVIEW_SCALE_FACTOR:
1969 case LyXRC::RC_PRINTCOLLCOPIESFLAG:
1970 case LyXRC::RC_PRINTCOPIESFLAG:
1971 case LyXRC::RC_PRINTER:
1972 case LyXRC::RC_PRINTEVENPAGEFLAG:
1973 case LyXRC::RC_PRINTEXSTRAOPTIONS:
1974 case LyXRC::RC_PRINTFILEEXTENSION:
1975 case LyXRC::RC_PRINTLANDSCAPEFLAG:
1976 case LyXRC::RC_PRINTODDPAGEFLAG:
1977 case LyXRC::RC_PRINTPAGERANGEFLAG:
1978 case LyXRC::RC_PRINTPAPERDIMENSIONFLAG:
1979 case LyXRC::RC_PRINTPAPERFLAG:
1980 case LyXRC::RC_PRINTREVERSEFLAG:
1981 case LyXRC::RC_PRINTSPOOL_COMMAND:
1982 case LyXRC::RC_PRINTSPOOL_PRINTERPREFIX:
1983 case LyXRC::RC_PRINTTOFILE:
1984 case LyXRC::RC_PRINTTOPRINTER:
1985 case LyXRC::RC_PRINT_ADAPTOUTPUT:
1986 case LyXRC::RC_PRINT_COMMAND:
1987 case LyXRC::RC_RTL_SUPPORT:
1988 case LyXRC::RC_SCREEN_DPI:
1989 case LyXRC::RC_SCREEN_FONT_ROMAN:
1990 case LyXRC::RC_SCREEN_FONT_ROMAN_FOUNDRY:
1991 case LyXRC::RC_SCREEN_FONT_SANS:
1992 case LyXRC::RC_SCREEN_FONT_SANS_FOUNDRY:
1993 case LyXRC::RC_SCREEN_FONT_SCALABLE:
1994 case LyXRC::RC_SCREEN_FONT_SIZES:
1995 case LyXRC::RC_SCREEN_FONT_TYPEWRITER:
1996 case LyXRC::RC_SCREEN_FONT_TYPEWRITER_FOUNDRY:
1997 case LyXRC::RC_GEOMETRY_SESSION:
1998 case LyXRC::RC_SCREEN_ZOOM:
1999 case LyXRC::RC_SERVERPIPE:
2000 case LyXRC::RC_SET_COLOR:
2001 case LyXRC::RC_SHOW_BANNER:
2002 case LyXRC::RC_SPELL_COMMAND:
2003 case LyXRC::RC_TEMPDIRPATH:
2004 case LyXRC::RC_TEMPLATEPATH:
2005 case LyXRC::RC_TEX_ALLOWS_SPACES:
2006 case LyXRC::RC_TEX_EXPECTS_WINDOWS_PATHS:
2007 if (lyxrc_orig.windows_style_tex_paths != lyxrc_new.windows_style_tex_paths) {
2008 os::windows_style_tex_paths(lyxrc_new.windows_style_tex_paths);
2010 case LyXRC::RC_UIFILE:
2011 case LyXRC::RC_USER_EMAIL:
2012 case LyXRC::RC_USER_NAME:
2013 case LyXRC::RC_USETEMPDIR:
2014 case LyXRC::RC_USE_ALT_LANG:
2015 case LyXRC::RC_USE_CONVERTER_CACHE:
2016 case LyXRC::RC_USE_ESC_CHARS:
2017 case LyXRC::RC_USE_INP_ENC:
2018 case LyXRC::RC_USE_PERS_DICT:
2019 case LyXRC::RC_USE_TOOLTIP:
2020 case LyXRC::RC_USE_PIXMAP_CACHE:
2021 case LyXRC::RC_USE_SPELL_LIB:
2022 case LyXRC::RC_VIEWDVI_PAPEROPTION:
2023 case LyXRC::RC_SORT_LAYOUTS:
2024 case LyXRC::RC_FULL_SCREEN_LIMIT:
2025 case LyXRC::RC_FULL_SCREEN_SCROLLBAR:
2026 case LyXRC::RC_FULL_SCREEN_TABBAR:
2027 case LyXRC::RC_FULL_SCREEN_TOOLBARS:
2028 case LyXRC::RC_FULL_SCREEN_WIDTH:
2029 case LyXRC::RC_VISUAL_CURSOR:
2030 case LyXRC::RC_VIEWER:
2031 case LyXRC::RC_LAST: