3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
35 #include "InsetList.h"
39 #include "LyXAction.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "SpellChecker.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "WordLangTuple.h"
51 #include "frontends/Application.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetCollapsable.h"
56 #include "insets/InsetCommand.h"
57 #include "insets/InsetExternal.h"
58 #include "insets/InsetFloat.h"
59 #include "insets/InsetFloatList.h"
60 #include "insets/InsetGraphics.h"
61 #include "insets/InsetGraphicsParams.h"
62 #include "insets/InsetNewline.h"
63 #include "insets/InsetQuotes.h"
64 #include "insets/InsetSpecialChar.h"
65 #include "insets/InsetText.h"
66 #include "insets/InsetWrap.h"
68 #include "support/convert.h"
69 #include "support/debug.h"
70 #include "support/gettext.h"
71 #include "support/lstrings.h"
72 #include "support/lyxtime.h"
73 #include "support/os.h"
75 #include "mathed/InsetMathHull.h"
76 #include "mathed/MathMacroTemplate.h"
78 #include <boost/next_prior.hpp>
84 using namespace lyx::support;
88 using cap::copySelection;
89 using cap::cutSelection;
90 using cap::pasteFromStack;
91 using cap::pasteClipboardText;
92 using cap::pasteClipboardGraphics;
93 using cap::replaceSelection;
94 using cap::grabAndEraseSelection;
95 using cap::selClearOrDel;
98 static Font freefont(ignore_font, ignore_language);
99 static bool toggleall = false;
101 static void toggleAndShow(Cursor & cur, Text * text,
102 Font const & font, bool toggleall = true)
104 text->toggleFree(cur, font, toggleall);
106 if (font.language() != ignore_language ||
107 font.fontInfo().number() != FONT_IGNORE) {
108 TextMetrics const & tm = cur.bv().textMetrics(text);
109 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
110 cur.real_current_font))
111 text->setCursor(cur, cur.pit(), cur.pos(),
112 false, !cur.boundary());
117 static void moveCursor(Cursor & cur, bool selecting)
119 if (selecting || cur.mark())
124 static void finishChange(Cursor & cur, bool selecting)
127 moveCursor(cur, selecting);
131 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
134 docstring sel = cur.selectionAsString(false);
136 // It may happen that sel is empty but there is a selection
137 replaceSelection(cur);
139 // Is this a valid formula?
143 #ifdef ENABLE_ASSERTIONS
144 const int old_pos = cur.pos();
146 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
147 #ifdef ENABLE_ASSERTIONS
148 LASSERT(old_pos == cur.pos(), /**/);
150 cur.nextInset()->edit(cur, true);
151 // don't do that also for LFUN_MATH_MODE
152 // unless you want end up with always changing
153 // to mathrm when opening an inlined inset --
154 // I really hate "LyXfunc overloading"...
156 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
157 // Avoid an unnecessary undo step if cmd.argument
159 if (!cmd.argument().empty())
160 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
163 // create a macro if we see "\\newcommand"
164 // somewhere, and an ordinary formula
166 if (sel.find(from_ascii("\\newcommand")) == string::npos
167 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
168 && sel.find(from_ascii("\\def")) == string::npos)
170 InsetMathHull * formula = new InsetMathHull(cur.buffer());
171 string const selstr = to_utf8(sel);
172 istringstream is(selstr);
175 if (!formula->readQuiet(lex)) {
176 // No valid formula, let's try with delims
177 is.str("$" + selstr + "$");
179 if (!formula->readQuiet(lex)) {
180 // Still not valid, leave it as is
189 cur.insert(new MathMacroTemplate(cur.buffer(), sel));
193 cur.message(from_utf8(N_("Math editor mode")));
195 cur.message(from_utf8(N_("No valid math formula")));
199 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
201 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
202 if (cur.inRegexped()) {
203 cur.message(_("Already in regular expression mode"));
207 docstring sel = cur.selectionAsString(false);
209 // It may happen that sel is empty but there is a selection
210 replaceSelection(cur);
212 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
213 cur.nextInset()->edit(cur, true);
216 cur.message(_("Regexp editor mode"));
220 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
223 cap::replaceSelection(cur);
224 cur.insert(new InsetSpecialChar(kind));
229 static bool doInsertInset(Cursor & cur, Text * text,
230 FuncRequest const & cmd, bool edit, bool pastesel)
232 Buffer & buffer = cur.bv().buffer();
233 BufferParams const & bparams = buffer.params();
234 Inset * inset = createInset(&buffer, cmd);
238 if (InsetCollapsable * ci = inset->asInsetCollapsable())
239 ci->setButtonLabel();
242 if (cmd.action() == LFUN_INDEX_INSERT) {
243 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
244 text->insertInset(cur, inset);
246 inset->edit(cur, true);
247 // Now put this into inset
248 cur.text()->insertStringAsLines(cur, ds, cur.current_font);
249 cur.leaveInset(*inset);
254 if (cur.selection()) {
255 cutSelection(cur, false, pastesel);
256 cur.clearSelection();
259 text->insertInset(cur, inset);
262 inset->edit(cur, true);
264 if (!gotsel || !pastesel)
267 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
268 cur.buffer()->errors("Paste");
269 cur.clearSelection(); // bug 393
271 InsetText * inset_text = inset->asInsetText();
273 inset_text->fixParagraphsFont();
274 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
275 // reset first par to default
276 cur.text()->paragraphs().begin()
277 ->setPlainOrDefaultLayout(bparams.documentClass());
280 // Merge multiple paragraphs -- hack
281 while (cur.lastpit() > 0)
282 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
283 cur.leaveInset(*inset);
286 cur.leaveInset(*inset);
287 // reset surrounding par to default
288 DocumentClass const & dc = bparams.documentClass();
289 docstring const layoutname = inset->usePlainLayout()
290 ? dc.plainLayoutName()
291 : dc.defaultLayoutName();
292 text->setLayout(cur, layoutname);
298 string const freefont2string()
300 return freefont.toString(toggleall);
304 /// the type of outline operation
306 OutlineUp, // Move this header with text down
307 OutlineDown, // Move this header with text up
308 OutlineIn, // Make this header deeper
309 OutlineOut // Make this header shallower
313 static void outline(OutlineOp mode, Cursor & cur)
315 Buffer & buf = *cur.buffer();
316 pit_type & pit = cur.pit();
317 ParagraphList & pars = buf.text().paragraphs();
318 ParagraphList::iterator const bgn = pars.begin();
319 // The first paragraph of the area to be copied:
320 ParagraphList::iterator start = boost::next(bgn, pit);
321 // The final paragraph of area to be copied:
322 ParagraphList::iterator finish = start;
323 ParagraphList::iterator const end = pars.end();
325 DocumentClass const & tc = buf.params().documentClass();
327 int const thistoclevel = start->layout().toclevel;
330 // Move out (down) from this section header
334 // Seek the one (on same level) below
335 for (; finish != end; ++finish) {
336 toclevel = finish->layout().toclevel;
337 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
343 if (start == pars.begin())
346 ParagraphList::iterator dest = start;
347 // Move out (up) from this header
350 // Search previous same-level header above
353 toclevel = dest->layout().toclevel;
355 && (toclevel == Layout::NOT_IN_TOC
356 || toclevel > thistoclevel));
357 // Not found; do nothing
358 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
360 pit_type const newpit = distance(bgn, dest);
361 pit_type const len = distance(start, finish);
362 pit_type const deletepit = pit + len;
363 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
364 pars.splice(dest, start, finish);
372 // Go one down from *this* header:
373 ParagraphList::iterator dest = boost::next(finish, 1);
374 // Go further down to find header to insert in front of:
375 for (; dest != end; ++dest) {
376 toclevel = dest->layout().toclevel;
377 if (toclevel != Layout::NOT_IN_TOC
378 && toclevel <= thistoclevel)
381 // One such was found:
382 pit_type newpit = distance(bgn, dest);
383 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
384 pit_type const len = distance(start, finish);
385 pars.splice(dest, start, finish);
386 cur.pit() = newpit - len;
390 pit_type const len = distance(start, finish);
391 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
392 for (; start != finish; ++start) {
393 toclevel = start->layout().toclevel;
394 if (toclevel == Layout::NOT_IN_TOC)
396 DocumentClass::const_iterator lit = tc.begin();
397 DocumentClass::const_iterator len = tc.end();
398 for (; lit != len; ++lit) {
399 if (lit->toclevel == toclevel + 1 &&
400 start->layout().labeltype == lit->labeltype) {
401 start->setLayout(*lit);
409 pit_type const len = distance(start, finish);
410 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
411 for (; start != finish; ++start) {
412 toclevel = start->layout().toclevel;
413 if (toclevel == Layout::NOT_IN_TOC)
415 DocumentClass::const_iterator lit = tc.begin();
416 DocumentClass::const_iterator len = tc.end();
417 for (; lit != len; ++lit) {
418 if (lit->toclevel == toclevel - 1 &&
419 start->layout().labeltype == lit->labeltype) {
420 start->setLayout(*lit);
431 void Text::number(Cursor & cur)
433 FontInfo font = ignore_font;
434 font.setNumber(FONT_TOGGLE);
435 toggleAndShow(cur, this, Font(font, ignore_language));
439 bool Text::isRTL(Paragraph const & par) const
441 Buffer const & buffer = owner_->buffer();
442 return par.isRTL(buffer.params());
446 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
448 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
450 // Dispatch if the cursor is inside the text. It is not the
451 // case for context menus (bug 5797).
452 if (cur.text() != this) {
457 BufferView * bv = &cur.bv();
458 TextMetrics * tm = &bv->textMetrics(this);
459 if (!tm->contains(cur.pit())) {
460 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
461 tm = &bv->textMetrics(this);
464 // FIXME: We use the update flag to indicates wether a singlePar or a
465 // full screen update is needed. We reset it here but shall we restore it
467 cur.noScreenUpdate();
469 LASSERT(cur.text() == this, /**/);
470 CursorSlice oldTopSlice = cur.top();
471 bool oldBoundary = cur.boundary();
472 bool sel = cur.selection();
473 // Signals that, even if needsUpdate == false, an update of the
474 // cursor paragraph is required
475 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
476 LyXAction::SingleParUpdate);
477 // Signals that a full-screen update is required
478 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
479 LyXAction::NoUpdate) || singleParUpdate);
481 FuncCode const act = cmd.action();
484 case LFUN_PARAGRAPH_MOVE_DOWN: {
485 pit_type const pit = cur.pit();
486 recUndo(cur, pit, pit + 1);
488 pars_.swap(pit, pit + 1);
490 cur.forceBufferUpdate();
495 case LFUN_PARAGRAPH_MOVE_UP: {
496 pit_type const pit = cur.pit();
497 recUndo(cur, pit - 1, pit);
499 pars_.swap(pit, pit - 1);
502 cur.forceBufferUpdate();
506 case LFUN_APPENDIX: {
507 Paragraph & par = cur.paragraph();
508 bool start = !par.params().startOfAppendix();
510 // FIXME: The code below only makes sense at top level.
511 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
512 // ensure that we have only one start_of_appendix in this document
513 // FIXME: this don't work for multipart document!
514 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
515 if (pars_[tmp].params().startOfAppendix()) {
517 pars_[tmp].params().startOfAppendix(false);
523 par.params().startOfAppendix(start);
525 // we can set the refreshing parameters now
526 cur.forceBufferUpdate();
530 case LFUN_WORD_DELETE_FORWARD:
532 cutSelection(cur, true, false);
534 deleteWordForward(cur);
535 finishChange(cur, false);
538 case LFUN_WORD_DELETE_BACKWARD:
540 cutSelection(cur, true, false);
542 deleteWordBackward(cur);
543 finishChange(cur, false);
546 case LFUN_LINE_DELETE:
548 cutSelection(cur, true, false);
550 tm->deleteLineForward(cur);
551 finishChange(cur, false);
554 case LFUN_BUFFER_BEGIN:
555 case LFUN_BUFFER_BEGIN_SELECT:
556 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
557 if (cur.depth() == 1)
558 needsUpdate |= cursorTop(cur);
561 cur.screenUpdateFlags(Update::FitCursor);
564 case LFUN_BUFFER_END:
565 case LFUN_BUFFER_END_SELECT:
566 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
567 if (cur.depth() == 1)
568 needsUpdate |= cursorBottom(cur);
571 cur.screenUpdateFlags(Update::FitCursor);
574 case LFUN_INSET_BEGIN:
575 case LFUN_INSET_BEGIN_SELECT:
576 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
577 if (cur.depth() == 1 || !cur.top().at_begin())
578 needsUpdate |= cursorTop(cur);
581 cur.screenUpdateFlags(Update::FitCursor);
585 case LFUN_INSET_END_SELECT:
586 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
587 if (cur.depth() == 1 || !cur.top().at_end())
588 needsUpdate |= cursorBottom(cur);
591 cur.screenUpdateFlags(Update::FitCursor);
594 case LFUN_INSET_SELECT_ALL:
595 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
596 || !cur.selEnd().at_end()) {
597 needsUpdate |= cur.selHandle(false);
598 needsUpdate |= cursorTop(cur);
599 needsUpdate |= cur.selHandle(true);
600 needsUpdate |= cursorBottom(cur);
603 cur.screenUpdateFlags(Update::FitCursor);
606 case LFUN_CHAR_FORWARD:
607 case LFUN_CHAR_FORWARD_SELECT:
608 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
609 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
610 needsUpdate |= cursorForward(cur);
612 if (!needsUpdate && oldTopSlice == cur.top()
613 && cur.boundary() == oldBoundary) {
615 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
617 // we will probably be moving out the inset, so we should execute
618 // the depm-mechanism, but only when the cursor has a place to
619 // go outside this inset, i.e. in a slice above.
620 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
621 && cur.pit() == cur.lastpit()) {
622 // The cursor hasn't changed yet. To give the
623 // DEPM the possibility of doing something we must
624 // provide it with two different cursors.
626 dummy.pos() = dummy.pit() = 0;
627 if (cur.bv().checkDepm(dummy, cur))
628 cur.forceBufferUpdate();;
633 case LFUN_CHAR_BACKWARD:
634 case LFUN_CHAR_BACKWARD_SELECT:
635 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
636 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
637 needsUpdate |= cursorBackward(cur);
639 if (!needsUpdate && oldTopSlice == cur.top()
640 && cur.boundary() == oldBoundary) {
642 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
644 // we will probably be moving out the inset, so we should execute
645 // the depm-mechanism, but only when the cursor has a place to
646 // go outside this inset, i.e. in a slice above.
647 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
648 // The cursor hasn't changed yet. To give the
649 // DEPM the possibility of doing something we must
650 // provide it with two different cursors.
652 dummy.pos() = cur.lastpos();
653 dummy.pit() = cur.lastpit();
654 if (cur.bv().checkDepm(dummy, cur))
655 cur.forceBufferUpdate();
661 case LFUN_CHAR_LEFT_SELECT:
662 if (lyxrc.visual_cursor) {
663 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
664 needsUpdate |= cursorVisLeft(cur);
665 if (!needsUpdate && oldTopSlice == cur.top()
666 && cur.boundary() == oldBoundary) {
668 cmd = FuncRequest(LFUN_FINISHED_LEFT);
671 if (reverseDirectionNeeded(cur)) {
672 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
673 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
675 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
676 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
683 case LFUN_CHAR_RIGHT:
684 case LFUN_CHAR_RIGHT_SELECT:
685 if (lyxrc.visual_cursor) {
686 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
687 needsUpdate |= cursorVisRight(cur);
688 if (!needsUpdate && oldTopSlice == cur.top()
689 && cur.boundary() == oldBoundary) {
691 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
694 if (reverseDirectionNeeded(cur)) {
695 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
696 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
698 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
699 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
708 case LFUN_DOWN_SELECT:
711 // stop/start the selection
712 bool select = cmd.action() == LFUN_DOWN_SELECT ||
713 cmd.action() == LFUN_UP_SELECT;
715 // move cursor up/down
716 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
717 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
719 if (!atFirstOrLastRow) {
720 needsUpdate |= cur.selHandle(select);
721 cur.selHandle(select);
722 cur.upDownInText(up, needsUpdate);
723 needsUpdate |= cur.beforeDispatchCursor().inMathed();
725 // if the cursor cannot be moved up or down do not remove
726 // the selection right now, but wait for the next dispatch.
728 needsUpdate |= cur.selHandle(select);
729 cur.upDownInText(up, needsUpdate);
736 case LFUN_PARAGRAPH_UP:
737 case LFUN_PARAGRAPH_UP_SELECT:
738 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
739 needsUpdate |= cursorUpParagraph(cur);
742 case LFUN_PARAGRAPH_DOWN:
743 case LFUN_PARAGRAPH_DOWN_SELECT:
744 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
745 needsUpdate |= cursorDownParagraph(cur);
748 case LFUN_LINE_BEGIN:
749 case LFUN_LINE_BEGIN_SELECT:
750 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
751 needsUpdate |= tm->cursorHome(cur);
755 case LFUN_LINE_END_SELECT:
756 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
757 needsUpdate |= tm->cursorEnd(cur);
760 case LFUN_SECTION_SELECT: {
761 Buffer const & buf = *cur.buffer();
762 pit_type const pit = cur.pit();
763 ParagraphList & pars = buf.text().paragraphs();
764 ParagraphList::iterator bgn = pars.begin();
765 // The first paragraph of the area to be selected:
766 ParagraphList::iterator start = boost::next(bgn, pit);
767 // The final paragraph of area to be selected:
768 ParagraphList::iterator finish = start;
769 ParagraphList::iterator end = pars.end();
771 int const thistoclevel = start->layout().toclevel;
772 if (thistoclevel == Layout::NOT_IN_TOC)
776 Cursor const old_cur = cur;
777 needsUpdate |= cur.selHandle(true);
779 // Move out (down) from this section header
783 // Seek the one (on same level) below
784 for (; finish != end; ++finish, ++cur.pit()) {
785 int const toclevel = finish->layout().toclevel;
786 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
789 cur.pos() = cur.lastpos();
791 needsUpdate |= cur != old_cur;
795 case LFUN_WORD_RIGHT:
796 case LFUN_WORD_RIGHT_SELECT:
797 if (lyxrc.visual_cursor) {
798 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
799 needsUpdate |= cursorVisRightOneWord(cur);
800 if (!needsUpdate && oldTopSlice == cur.top()
801 && cur.boundary() == oldBoundary) {
803 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
806 if (reverseDirectionNeeded(cur)) {
807 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
808 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
810 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
811 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
818 case LFUN_WORD_FORWARD:
819 case LFUN_WORD_FORWARD_SELECT:
820 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
821 needsUpdate |= cursorForwardOneWord(cur);
823 if (!needsUpdate && oldTopSlice == cur.top()
824 && cur.boundary() == oldBoundary) {
826 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
828 // we will probably be moving out the inset, so we should execute
829 // the depm-mechanism, but only when the cursor has a place to
830 // go outside this inset, i.e. in a slice above.
831 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
832 && cur.pit() == cur.lastpit()) {
833 // The cursor hasn't changed yet. To give the
834 // DEPM the possibility of doing something we must
835 // provide it with two different cursors.
837 dummy.pos() = dummy.pit() = 0;
838 if (cur.bv().checkDepm(dummy, cur))
839 cur.forceBufferUpdate();;
845 case LFUN_WORD_LEFT_SELECT:
846 if (lyxrc.visual_cursor) {
847 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
848 needsUpdate |= cursorVisLeftOneWord(cur);
849 if (!needsUpdate && oldTopSlice == cur.top()
850 && cur.boundary() == oldBoundary) {
852 cmd = FuncRequest(LFUN_FINISHED_LEFT);
855 if (reverseDirectionNeeded(cur)) {
856 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
857 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
859 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
860 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
867 case LFUN_WORD_BACKWARD:
868 case LFUN_WORD_BACKWARD_SELECT:
869 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
870 needsUpdate |= cursorBackwardOneWord(cur);
872 if (!needsUpdate && oldTopSlice == cur.top()
873 && cur.boundary() == oldBoundary) {
875 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
877 // we will probably be moving out the inset, so we should execute
878 // the depm-mechanism, but only when the cursor has a place to
879 // go outside this inset, i.e. in a slice above.
880 if (cur.depth() > 1 && cur.pos() == 0
882 // The cursor hasn't changed yet. To give the
883 // DEPM the possibility of doing something we must
884 // provide it with two different cursors.
886 dummy.pos() = cur.lastpos();
887 dummy.pit() = cur.lastpit();
888 if (cur.bv().checkDepm(dummy, cur))
889 cur.forceBufferUpdate();
894 case LFUN_WORD_SELECT: {
895 selectWord(cur, WHOLE_WORD);
896 finishChange(cur, true);
900 case LFUN_NEWLINE_INSERT: {
901 InsetNewlineParams inp;
902 docstring arg = cmd.argument();
903 // this avoids a double undo
904 // FIXME: should not be needed, ideally
905 if (!cur.selection())
907 cap::replaceSelection(cur);
908 if (arg == "linebreak")
909 inp.kind = InsetNewlineParams::LINEBREAK;
911 inp.kind = InsetNewlineParams::NEWLINE;
912 cur.insert(new InsetNewline(inp));
914 moveCursor(cur, false);
918 case LFUN_TAB_INSERT: {
919 bool const multi_par_selection = cur.selection() &&
920 cur.selBegin().pit() != cur.selEnd().pit();
921 if (multi_par_selection) {
922 // If there is a multi-paragraph selection, a tab is inserted
923 // at the beginning of each paragraph.
924 cur.recordUndoSelection();
925 pit_type const pit_end = cur.selEnd().pit();
926 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
927 pars_[pit].insertChar(0, '\t',
928 bv->buffer().params().trackChanges);
929 // Update the selection pos to make sure the selection does not
930 // change as the inserted tab will increase the logical pos.
931 if (cur.realAnchor().pit() == pit)
932 cur.realAnchor().forwardPos();
933 if (cur.pit() == pit)
938 // Maybe we shouldn't allow tabs within a line, because they
939 // are not (yet) aligned as one might do expect.
940 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
946 case LFUN_TAB_DELETE: {
947 bool const tc = bv->buffer().params().trackChanges;
948 if (cur.selection()) {
949 // If there is a selection, a tab (if present) is removed from
950 // the beginning of each paragraph.
951 cur.recordUndoSelection();
952 pit_type const pit_end = cur.selEnd().pit();
953 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
954 Paragraph & par = paragraphs()[pit];
957 char_type const c = par.getChar(0);
958 if (c == '\t' || c == ' ') {
959 // remove either 1 tab or 4 spaces.
960 int const n = (c == ' ' ? 4 : 1);
961 for (int i = 0; i < n
962 && !par.empty() && par.getChar(0) == c; ++i) {
963 if (cur.pit() == pit)
965 if (cur.realAnchor().pit() == pit
966 && cur.realAnchor().pos() > 0 )
967 cur.realAnchor().backwardPos();
968 par.eraseChar(0, tc);
974 // If there is no selection, try to remove a tab or some spaces
975 // before the position of the cursor.
976 Paragraph & par = paragraphs()[cur.pit()];
977 pos_type const pos = cur.pos();
982 char_type const c = par.getChar(pos - 1);
986 par.eraseChar(cur.pos(), tc);
988 for (int n_spaces = 0;
990 && par.getChar(cur.pos() - 1) == ' '
994 par.eraseChar(cur.pos(), tc);
1001 case LFUN_CHAR_DELETE_FORWARD:
1002 if (!cur.selection()) {
1003 if (cur.pos() == cur.paragraph().size())
1004 // Par boundary, force full-screen update
1005 singleParUpdate = false;
1006 needsUpdate |= erase(cur);
1008 // It is possible to make it a lot faster still
1009 // just comment out the line below...
1011 cutSelection(cur, true, false);
1012 singleParUpdate = false;
1014 moveCursor(cur, false);
1017 case LFUN_CHAR_DELETE_BACKWARD:
1018 if (!cur.selection()) {
1019 if (bv->getIntl().getTransManager().backspace()) {
1020 // Par boundary, full-screen update
1022 singleParUpdate = false;
1023 needsUpdate |= backspace(cur);
1025 // It is possible to make it a lot faster still
1026 // just comment out the line below...
1029 cutSelection(cur, true, false);
1030 singleParUpdate = false;
1034 case LFUN_BREAK_PARAGRAPH:
1035 cap::replaceSelection(cur);
1036 breakParagraph(cur, cmd.argument() == "inverse");
1040 case LFUN_INSET_INSERT: {
1043 // We have to avoid triggering InstantPreview loading
1044 // before inserting into the document. See bug #5626.
1045 bool loaded = bv->buffer().isFullyLoaded();
1046 bv->buffer().setFullyLoaded(false);
1047 Inset * inset = createInset(&bv->buffer(), cmd);
1048 bv->buffer().setFullyLoaded(loaded);
1051 // FIXME (Abdel 01/02/2006):
1052 // What follows would be a partial fix for bug 2154:
1053 // http://www.lyx.org/trac/ticket/2154
1054 // This automatically put the label inset _after_ a
1055 // numbered section. It should be possible to extend the mechanism
1056 // to any kind of LateX environement.
1057 // The correct way to fix that bug would be at LateX generation.
1058 // I'll let the code here for reference as it could be used for some
1059 // other feature like "automatic labelling".
1061 Paragraph & par = pars_[cur.pit()];
1062 if (inset->lyxCode() == LABEL_CODE
1063 && par.layout().labeltype == LABEL_COUNTER) {
1064 // Go to the end of the paragraph
1065 // Warning: Because of Change-Tracking, the last
1066 // position is 'size()' and not 'size()-1':
1067 cur.pos() = par.size();
1068 // Insert a new paragraph
1069 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1073 if (cur.selection())
1074 cutSelection(cur, true, false);
1076 if (inset->editable() && inset->asInsetText())
1077 inset->edit(cur, true);
1081 // trigger InstantPreview now
1082 if (inset->lyxCode() == EXTERNAL_CODE) {
1083 InsetExternal & ins =
1084 static_cast<InsetExternal &>(*inset);
1085 ins.updatePreview();
1092 case LFUN_INSET_DISSOLVE: {
1093 if (dissolveInset(cur)) {
1095 cur.forceBufferUpdate();
1100 case LFUN_SET_GRAPHICS_GROUP: {
1101 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1107 string id = to_utf8(cmd.argument());
1108 string grp = graphics::getGroupParams(bv->buffer(), id);
1109 InsetGraphicsParams tmp, inspar = ins->getParams();
1112 inspar.groupId = to_utf8(cmd.argument());
1114 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1115 tmp.filename = inspar.filename;
1119 ins->setParams(inspar);
1122 case LFUN_SPACE_INSERT:
1123 if (cur.paragraph().layout().free_spacing)
1124 insertChar(cur, ' ');
1126 doInsertInset(cur, this, cmd, false, false);
1129 moveCursor(cur, false);
1132 case LFUN_SPECIALCHAR_INSERT: {
1133 string const name = to_utf8(cmd.argument());
1134 if (name == "hyphenation")
1135 specialChar(cur, InsetSpecialChar::HYPHENATION);
1136 else if (name == "ligature-break")
1137 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1138 else if (name == "slash")
1139 specialChar(cur, InsetSpecialChar::SLASH);
1140 else if (name == "nobreakdash")
1141 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1142 else if (name == "dots")
1143 specialChar(cur, InsetSpecialChar::LDOTS);
1144 else if (name == "end-of-sentence")
1145 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1146 else if (name == "menu-separator")
1147 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1148 else if (name.empty())
1149 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1151 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1155 case LFUN_WORD_UPCASE:
1156 changeCase(cur, text_uppercase);
1159 case LFUN_WORD_LOWCASE:
1160 changeCase(cur, text_lowercase);
1163 case LFUN_WORD_CAPITALIZE:
1164 changeCase(cur, text_capitalization);
1167 case LFUN_CHARS_TRANSPOSE:
1168 charsTranspose(cur);
1172 cur.message(_("Paste"));
1173 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1174 cap::replaceSelection(cur);
1176 // without argument?
1177 string const arg = to_utf8(cmd.argument());
1179 if (theClipboard().isInternal())
1180 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1181 else if (theClipboard().hasGraphicsContents()
1182 && !theClipboard().hasTextContents())
1183 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1185 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1186 } else if (isStrUnsignedInt(arg)) {
1187 // we have a numerical argument
1188 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1189 convert<unsigned int>(arg));
1191 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1193 type = Clipboard::PdfGraphicsType;
1194 else if (arg == "png")
1195 type = Clipboard::PngGraphicsType;
1196 else if (arg == "jpeg")
1197 type = Clipboard::JpegGraphicsType;
1198 else if (arg == "linkback")
1199 type = Clipboard::LinkBackGraphicsType;
1200 else if (arg == "emf")
1201 type = Clipboard::EmfGraphicsType;
1202 else if (arg == "wmf")
1203 type = Clipboard::WmfGraphicsType;
1206 LASSERT(false, /**/);
1208 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1211 bv->buffer().errors("Paste");
1212 cur.clearSelection(); // bug 393
1218 cutSelection(cur, true, true);
1219 cur.message(_("Cut"));
1224 cur.message(_("Copy"));
1227 case LFUN_SERVER_GET_XY:
1228 cur.message(from_utf8(
1229 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1230 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1233 case LFUN_SERVER_SET_XY: {
1236 istringstream is(to_utf8(cmd.argument()));
1239 lyxerr << "SETXY: Could not parse coordinates in '"
1240 << to_utf8(cmd.argument()) << endl;
1242 tm->setCursorFromCoordinates(cur, x, y);
1246 case LFUN_SERVER_GET_LAYOUT:
1247 cur.message(cur.paragraph().layout().name());
1251 docstring layout = cmd.argument();
1252 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1254 Paragraph const & para = cur.paragraph();
1255 docstring const old_layout = para.layout().name();
1256 DocumentClass const & tclass = bv->buffer().params().documentClass();
1259 layout = tclass.defaultLayoutName();
1261 if (owner_->forcePlainLayout())
1262 // in this case only the empty layout is allowed
1263 layout = tclass.plainLayoutName();
1264 else if (para.usePlainLayout()) {
1265 // in this case, default layout maps to empty layout
1266 if (layout == tclass.defaultLayoutName())
1267 layout = tclass.plainLayoutName();
1269 // otherwise, the empty layout maps to the default
1270 if (layout == tclass.plainLayoutName())
1271 layout = tclass.defaultLayoutName();
1274 bool hasLayout = tclass.hasLayout(layout);
1276 // If the entry is obsolete, use the new one instead.
1278 docstring const & obs = tclass[layout].obsoleted_by();
1284 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1285 from_utf8(N_(" not known")));
1289 bool change_layout = (old_layout != layout);
1291 if (!change_layout && cur.selection() &&
1292 cur.selBegin().pit() != cur.selEnd().pit())
1294 pit_type spit = cur.selBegin().pit();
1295 pit_type epit = cur.selEnd().pit() + 1;
1296 while (spit != epit) {
1297 if (pars_[spit].layout().name() != old_layout) {
1298 change_layout = true;
1306 setLayout(cur, layout);
1311 case LFUN_CLIPBOARD_PASTE:
1312 cur.clearSelection();
1313 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1314 cmd.argument() == "paragraph");
1315 bv->buffer().errors("Paste");
1318 case LFUN_PRIMARY_SELECTION_PASTE:
1319 pasteString(cur, theSelection().get(),
1320 cmd.argument() == "paragraph");
1323 case LFUN_SELECTION_PASTE:
1324 // Copy the selection buffer to the clipboard stack,
1325 // because we want it to appear in the "Edit->Paste
1327 cap::copySelectionToStack();
1328 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1329 bv->buffer().errors("Paste");
1332 case LFUN_UNICODE_INSERT: {
1333 if (cmd.argument().empty())
1335 docstring hexstring = cmd.argument();
1336 if (isHex(hexstring)) {
1337 char_type c = hexToInt(hexstring);
1338 if (c >= 32 && c < 0x10ffff) {
1339 lyxerr << "Inserting c: " << c << endl;
1340 docstring s = docstring(1, c);
1341 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1347 case LFUN_QUOTE_INSERT: {
1348 Paragraph & par = cur.paragraph();
1349 pos_type pos = cur.pos();
1350 BufferParams const & bufparams = bv->buffer().params();
1351 Layout const & style = par.layout();
1352 InsetLayout const & ilayout = cur.inset().getLayout();
1353 if (!style.pass_thru && !ilayout.isPassThru()
1354 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1355 // this avoids a double undo
1356 // FIXME: should not be needed, ideally
1357 if (!cur.selection())
1359 cap::replaceSelection(cur);
1364 else if (cur.prevInset() && cur.prevInset()->isSpace())
1367 c = par.getChar(pos - 1);
1368 string arg = to_utf8(cmd.argument());
1369 cur.insert(new InsetQuotes(cur.buffer(), c, (arg == "single")
1370 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1374 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1378 case LFUN_DATE_INSERT: {
1379 string const format = cmd.argument().empty()
1380 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1381 string const time = formatted_time(current_time(), format);
1382 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1386 case LFUN_MOUSE_TRIPLE:
1387 if (cmd.button() == mouse_button::button1) {
1388 tm->cursorHome(cur);
1396 case LFUN_MOUSE_DOUBLE:
1397 if (cmd.button() == mouse_button::button1) {
1398 selectWord(cur, WHOLE_WORD_STRICT);
1403 // Single-click on work area
1404 case LFUN_MOUSE_PRESS: {
1405 // We are not marking a selection with the keyboard in any case.
1406 Cursor & bvcur = cur.bv().cursor();
1407 bvcur.setMark(false);
1408 switch (cmd.button()) {
1409 case mouse_button::button1:
1411 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1412 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1413 if (bvcur.wordSelection())
1414 selectWord(bvcur, WHOLE_WORD);
1417 case mouse_button::button2:
1418 // Middle mouse pasting.
1419 bv->mouseSetCursor(cur);
1421 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1422 "selection-paste ; primary-selection-paste paragraph"));
1423 cur.noScreenUpdate();
1426 case mouse_button::button3: {
1427 // Don't do anything if we right-click a
1428 // selection, a context menu will popup.
1429 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1430 && cur < bvcur.selectionEnd()) {
1431 cur.noScreenUpdate();
1434 if (!bv->mouseSetCursor(cur, false))
1435 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1441 } // switch (cmd.button())
1444 case LFUN_MOUSE_MOTION: {
1445 // Mouse motion with right or middle mouse do nothing for now.
1446 if (cmd.button() != mouse_button::button1) {
1447 cur.noScreenUpdate();
1450 // ignore motions deeper nested than the real anchor
1451 Cursor & bvcur = cur.bv().cursor();
1452 if (!bvcur.realAnchor().hasPart(cur)) {
1456 CursorSlice old = bvcur.top();
1458 int const wh = bv->workHeight();
1459 int const y = max(0, min(wh - 1, cmd.y()));
1461 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1462 cur.setTargetX(cmd.x());
1464 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1465 else if (cmd.y() < 0)
1466 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1467 // This is to allow jumping over large insets
1468 if (cur.top() == old) {
1470 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1471 else if (cmd.y() < 0)
1472 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1474 // We continue with our existing selection or start a new one, so don't
1475 // reset the anchor.
1476 bvcur.setCursor(cur);
1477 bvcur.setSelection(true);
1478 if (cur.top() == old) {
1479 // We didn't move one iota, so no need to update the screen.
1480 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1481 //cur.noScreenUpdate();
1487 case LFUN_MOUSE_RELEASE:
1488 switch (cmd.button()) {
1489 case mouse_button::button1:
1490 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1491 // If there is a new selection, update persistent selection;
1492 // otherwise, single click does not clear persistent selection
1494 if (cur.selection()) {
1495 // Finish selection. If double click,
1496 // cur is moved to the end of word by
1497 // selectWord but bvcur is current
1499 cur.bv().cursor().setSelection();
1500 // We might have removed an empty but drawn selection
1501 // (probably a margin)
1502 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1504 cur.noScreenUpdate();
1505 // FIXME: We could try to handle drag and drop of selection here.
1508 case mouse_button::button2:
1509 // Middle mouse pasting is handled at mouse press time,
1510 // see LFUN_MOUSE_PRESS.
1511 cur.noScreenUpdate();
1514 case mouse_button::button3:
1515 // Cursor was set at LFUN_MOUSE_PRESS time.
1516 // FIXME: If there is a selection we could try to handle a special
1517 // drag & drop context menu.
1518 cur.noScreenUpdate();
1521 case mouse_button::none:
1522 case mouse_button::button4:
1523 case mouse_button::button5:
1525 } // switch (cmd.button())
1529 case LFUN_SELF_INSERT: {
1530 if (cmd.argument().empty())
1533 // Automatically delete the currently selected
1534 // text and replace it with what is being
1535 // typed in now. Depends on lyxrc settings
1536 // "auto_region_delete", which defaults to
1539 if (lyxrc.auto_region_delete && cur.selection())
1540 cutSelection(cur, false, false);
1542 cur.clearSelection();
1544 docstring::const_iterator cit = cmd.argument().begin();
1545 docstring::const_iterator const end = cmd.argument().end();
1546 for (; cit != end; ++cit)
1547 bv->translateAndInsert(*cit, this, cur);
1550 moveCursor(cur, false);
1551 bv->bookmarkEditPosition();
1555 case LFUN_HYPERLINK_INSERT: {
1556 InsetCommandParams p(HYPERLINK_CODE);
1558 if (cur.selection()) {
1559 content = cur.selectionAsString(false);
1560 cutSelection(cur, true, false);
1562 p["target"] = (cmd.argument().empty()) ?
1563 content : cmd.argument();
1564 string const data = InsetCommand::params2string(p);
1565 if (p["target"].empty()) {
1566 bv->showDialog("href", data);
1568 FuncRequest fr(LFUN_INSET_INSERT, data);
1574 case LFUN_LABEL_INSERT: {
1575 InsetCommandParams p(LABEL_CODE);
1576 // Try to generate a valid label
1577 p["name"] = (cmd.argument().empty()) ?
1578 cur.getPossibleLabel() :
1580 string const data = InsetCommand::params2string(p);
1582 if (cmd.argument().empty()) {
1583 bv->showDialog("label", data);
1585 FuncRequest fr(LFUN_INSET_INSERT, data);
1591 case LFUN_INFO_INSERT: {
1593 if (cmd.argument().empty() && cur.selection()) {
1594 // if command argument is empty use current selection as parameter.
1595 docstring ds = cur.selectionAsString(false);
1596 cutSelection(cur, true, false);
1597 FuncRequest cmd0(cmd, ds);
1598 inset = createInset(cur.buffer(), cmd0);
1600 inset = createInset(cur.buffer(), cmd);
1605 insertInset(cur, inset);
1609 case LFUN_CAPTION_INSERT:
1610 case LFUN_FOOTNOTE_INSERT:
1611 case LFUN_NOTE_INSERT:
1612 case LFUN_FLEX_INSERT:
1613 case LFUN_BOX_INSERT:
1614 case LFUN_BRANCH_INSERT:
1615 case LFUN_PHANTOM_INSERT:
1616 case LFUN_ERT_INSERT:
1617 case LFUN_LISTING_INSERT:
1618 case LFUN_MARGINALNOTE_INSERT:
1619 case LFUN_ARGUMENT_INSERT:
1620 case LFUN_INDEX_INSERT:
1621 case LFUN_PREVIEW_INSERT:
1622 case LFUN_SCRIPT_INSERT:
1623 // Open the inset, and move the current selection
1625 doInsertInset(cur, this, cmd, true, true);
1627 // Some insets are numbered, others are shown in the outline pane so
1628 // let's update the labels and the toc backend.
1629 cur.forceBufferUpdate();
1632 case LFUN_TABULAR_INSERT:
1633 // if there were no arguments, just open the dialog
1634 if (doInsertInset(cur, this, cmd, false, true))
1637 bv->showDialog("tabularcreate");
1641 case LFUN_FLOAT_INSERT:
1642 case LFUN_FLOAT_WIDE_INSERT:
1643 case LFUN_WRAP_INSERT: {
1644 // will some text be moved into the inset?
1645 bool content = cur.selection();
1647 doInsertInset(cur, this, cmd, true, true);
1650 // If some text is moved into the inset, doInsertInset
1651 // puts the cursor outside the inset. To insert the
1652 // caption we put it back into the inset.
1656 ParagraphList & pars = cur.text()->paragraphs();
1658 DocumentClass const & tclass = bv->buffer().params().documentClass();
1660 // add a separate paragraph for the caption inset
1661 pars.push_back(Paragraph());
1662 pars.back().setInsetOwner(&cur.text()->inset());
1663 pars.back().setPlainOrDefaultLayout(tclass);
1664 int cap_pit = pars.size() - 1;
1666 // if an empty inset was created, we create an additional empty
1667 // paragraph at the bottom so that the user can choose where to put
1668 // the graphics (or table).
1670 pars.push_back(Paragraph());
1671 pars.back().setInsetOwner(&cur.text()->inset());
1672 pars.back().setPlainOrDefaultLayout(tclass);
1675 // reposition the cursor to the caption
1676 cur.pit() = cap_pit;
1678 // FIXME: This Text/Cursor dispatch handling is a mess!
1679 // We cannot use Cursor::dispatch here it needs access to up to
1681 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1682 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1683 cur.forceBufferUpdate();
1684 cur.screenUpdateFlags(Update::Force);
1685 // FIXME: When leaving the Float (or Wrap) inset we should
1686 // delete any empty paragraph left above or below the
1691 case LFUN_NOMENCL_INSERT: {
1692 InsetCommandParams p(NOMENCL_CODE);
1693 if (cmd.argument().empty())
1694 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1696 p["symbol"] = cmd.argument();
1697 string const data = InsetCommand::params2string(p);
1698 bv->showDialog("nomenclature", data);
1702 case LFUN_INDEX_PRINT: {
1703 InsetCommandParams p(INDEX_PRINT_CODE);
1704 if (cmd.argument().empty())
1705 p["type"] = from_ascii("idx");
1707 p["type"] = cmd.argument();
1708 string const data = InsetCommand::params2string(p);
1709 FuncRequest fr(LFUN_INSET_INSERT, data);
1714 case LFUN_NOMENCL_PRINT:
1715 case LFUN_NEWPAGE_INSERT:
1717 doInsertInset(cur, this, cmd, false, false);
1721 case LFUN_DEPTH_DECREMENT:
1722 changeDepth(cur, DEC_DEPTH);
1725 case LFUN_DEPTH_INCREMENT:
1726 changeDepth(cur, INC_DEPTH);
1729 case LFUN_MATH_DISPLAY:
1730 mathDispatch(cur, cmd, true);
1733 case LFUN_REGEXP_MODE:
1734 regexpDispatch(cur, cmd);
1737 case LFUN_MATH_MODE:
1738 if (cmd.argument() == "on")
1739 // don't pass "on" as argument
1740 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1742 mathDispatch(cur, cmd, false);
1745 case LFUN_MATH_MACRO:
1746 if (cmd.argument().empty())
1747 cur.errorMessage(from_utf8(N_("Missing argument")));
1749 string s = to_utf8(cmd.argument());
1750 string const s1 = token(s, ' ', 1);
1751 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1752 string const s2 = token(s, ' ', 2);
1753 MacroType type = MacroTypeNewcommand;
1755 type = MacroTypeDef;
1756 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1757 from_utf8(token(s, ' ', 0)), nargs, false, type);
1758 inset->setBuffer(bv->buffer());
1759 insertInset(cur, inset);
1761 // enter macro inset and select the name
1763 cur.top().pos() = cur.top().lastpos();
1765 cur.setSelection(true);
1766 cur.top().pos() = 0;
1770 // passthrough hat and underscore outside mathed:
1771 case LFUN_MATH_SUBSCRIPT:
1772 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1774 case LFUN_MATH_SUPERSCRIPT:
1775 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1778 case LFUN_MATH_INSERT:
1779 case LFUN_MATH_AMS_MATRIX:
1780 case LFUN_MATH_MATRIX:
1781 case LFUN_MATH_DELIM:
1782 case LFUN_MATH_BIGDELIM: {
1784 cap::replaceSelection(cur);
1785 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1786 checkAndActivateInset(cur, true);
1787 LASSERT(cur.inMathed(), /**/);
1792 case LFUN_FONT_EMPH: {
1793 Font font(ignore_font, ignore_language);
1794 font.fontInfo().setEmph(FONT_TOGGLE);
1795 toggleAndShow(cur, this, font);
1799 case LFUN_FONT_ITAL: {
1800 Font font(ignore_font, ignore_language);
1801 font.fontInfo().setShape(ITALIC_SHAPE);
1802 toggleAndShow(cur, this, font);
1806 case LFUN_FONT_BOLD:
1807 case LFUN_FONT_BOLDSYMBOL: {
1808 Font font(ignore_font, ignore_language);
1809 font.fontInfo().setSeries(BOLD_SERIES);
1810 toggleAndShow(cur, this, font);
1814 case LFUN_FONT_NOUN: {
1815 Font font(ignore_font, ignore_language);
1816 font.fontInfo().setNoun(FONT_TOGGLE);
1817 toggleAndShow(cur, this, font);
1821 case LFUN_FONT_TYPEWRITER: {
1822 Font font(ignore_font, ignore_language);
1823 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1824 toggleAndShow(cur, this, font);
1828 case LFUN_FONT_SANS: {
1829 Font font(ignore_font, ignore_language);
1830 font.fontInfo().setFamily(SANS_FAMILY);
1831 toggleAndShow(cur, this, font);
1835 case LFUN_FONT_ROMAN: {
1836 Font font(ignore_font, ignore_language);
1837 font.fontInfo().setFamily(ROMAN_FAMILY);
1838 toggleAndShow(cur, this, font);
1842 case LFUN_FONT_DEFAULT: {
1843 Font font(inherit_font, ignore_language);
1844 toggleAndShow(cur, this, font);
1848 case LFUN_FONT_STRIKEOUT: {
1849 Font font(ignore_font, ignore_language);
1850 font.fontInfo().setStrikeout(FONT_TOGGLE);
1851 toggleAndShow(cur, this, font);
1855 case LFUN_FONT_UULINE: {
1856 Font font(ignore_font, ignore_language);
1857 font.fontInfo().setUuline(FONT_TOGGLE);
1858 toggleAndShow(cur, this, font);
1862 case LFUN_FONT_UWAVE: {
1863 Font font(ignore_font, ignore_language);
1864 font.fontInfo().setUwave(FONT_TOGGLE);
1865 toggleAndShow(cur, this, font);
1869 case LFUN_FONT_UNDERLINE: {
1870 Font font(ignore_font, ignore_language);
1871 font.fontInfo().setUnderbar(FONT_TOGGLE);
1872 toggleAndShow(cur, this, font);
1876 case LFUN_FONT_SIZE: {
1877 Font font(ignore_font, ignore_language);
1878 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1879 toggleAndShow(cur, this, font);
1883 case LFUN_LANGUAGE: {
1884 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1887 Font font(ignore_font, lang);
1888 toggleAndShow(cur, this, font);
1892 case LFUN_TEXTSTYLE_APPLY:
1893 toggleAndShow(cur, this, freefont, toggleall);
1894 cur.message(_("Character set"));
1897 // Set the freefont using the contents of \param data dispatched from
1898 // the frontends and apply it at the current cursor location.
1899 case LFUN_TEXTSTYLE_UPDATE: {
1902 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1905 toggleAndShow(cur, this, freefont, toggleall);
1906 cur.message(_("Character set"));
1908 lyxerr << "Argument not ok";
1913 case LFUN_FINISHED_LEFT:
1914 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1915 // We're leaving an inset, going left. If the inset is LTR, we're
1916 // leaving from the front, so we should not move (remain at --- but
1917 // not in --- the inset). If the inset is RTL, move left, without
1918 // entering the inset itself; i.e., move to after the inset.
1919 if (cur.paragraph().getFontSettings(
1920 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1921 cursorVisLeft(cur, true);
1924 case LFUN_FINISHED_RIGHT:
1925 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1926 // We're leaving an inset, going right. If the inset is RTL, we're
1927 // leaving from the front, so we should not move (remain at --- but
1928 // not in --- the inset). If the inset is LTR, move right, without
1929 // entering the inset itself; i.e., move to after the inset.
1930 if (!cur.paragraph().getFontSettings(
1931 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1932 cursorVisRight(cur, true);
1935 case LFUN_FINISHED_BACKWARD:
1936 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1939 case LFUN_FINISHED_FORWARD:
1940 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1942 cur.setCurrentFont();
1945 case LFUN_LAYOUT_PARAGRAPH: {
1947 params2string(cur.paragraph(), data);
1948 data = "show\n" + data;
1949 bv->showDialog("paragraph", data);
1953 case LFUN_PARAGRAPH_UPDATE: {
1955 params2string(cur.paragraph(), data);
1957 // Will the paragraph accept changes from the dialog?
1959 cur.inset().allowParagraphCustomization(cur.idx());
1961 data = "update " + convert<string>(accept) + '\n' + data;
1962 bv->updateDialog("paragraph", data);
1966 case LFUN_ACCENT_UMLAUT:
1967 case LFUN_ACCENT_CIRCUMFLEX:
1968 case LFUN_ACCENT_GRAVE:
1969 case LFUN_ACCENT_ACUTE:
1970 case LFUN_ACCENT_TILDE:
1971 case LFUN_ACCENT_CEDILLA:
1972 case LFUN_ACCENT_MACRON:
1973 case LFUN_ACCENT_DOT:
1974 case LFUN_ACCENT_UNDERDOT:
1975 case LFUN_ACCENT_UNDERBAR:
1976 case LFUN_ACCENT_CARON:
1977 case LFUN_ACCENT_BREVE:
1978 case LFUN_ACCENT_TIE:
1979 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1980 case LFUN_ACCENT_CIRCLE:
1981 case LFUN_ACCENT_OGONEK:
1982 theApp()->handleKeyFunc(cmd.action());
1983 if (!cmd.argument().empty())
1984 // FIXME: Are all these characters encoded in one byte in utf8?
1985 bv->translateAndInsert(cmd.argument()[0], this, cur);
1986 cur.screenUpdateFlags(Update::FitCursor);
1989 case LFUN_FLOAT_LIST_INSERT: {
1990 DocumentClass const & tclass = bv->buffer().params().documentClass();
1991 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1993 if (cur.selection())
1994 cutSelection(cur, true, false);
1995 breakParagraph(cur);
1997 if (cur.lastpos() != 0) {
1998 cursorBackward(cur);
1999 breakParagraph(cur);
2002 docstring const laystr = cur.inset().usePlainLayout() ?
2003 tclass.plainLayoutName() :
2004 tclass.defaultLayoutName();
2005 setLayout(cur, laystr);
2006 ParagraphParameters p;
2007 // FIXME If this call were replaced with one to clearParagraphParams(),
2008 // then we could get rid of this method altogether.
2009 setParagraphs(cur, p);
2010 // FIXME This should be simplified when InsetFloatList takes a
2011 // Buffer in its constructor.
2012 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2013 ifl->setBuffer(bv->buffer());
2014 insertInset(cur, ifl);
2017 lyxerr << "Non-existent float type: "
2018 << to_utf8(cmd.argument()) << endl;
2023 case LFUN_CHANGE_ACCEPT: {
2024 acceptOrRejectChanges(cur, ACCEPT);
2028 case LFUN_CHANGE_REJECT: {
2029 acceptOrRejectChanges(cur, REJECT);
2033 case LFUN_THESAURUS_ENTRY: {
2034 docstring arg = cmd.argument();
2036 arg = cur.selectionAsString(false);
2038 if (arg.size() > 100 || arg.empty()) {
2039 // Get word or selection
2040 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2041 arg = cur.selectionAsString(false);
2042 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2045 bv->showDialog("thesaurus", to_utf8(arg));
2049 case LFUN_SPELLING_ADD: {
2050 docstring word = from_utf8(cmd.getArg(0));
2053 word = cur.selectionAsString(false);
2055 if (word.size() > 100 || word.empty()) {
2056 // Get word or selection
2057 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2058 word = cur.selectionAsString(false);
2060 lang = const_cast<Language *>(cur.getFont().language());
2062 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2063 WordLangTuple wl(word, lang);
2064 theSpellChecker()->insert(wl);
2068 case LFUN_SPELLING_IGNORE: {
2069 docstring word = from_utf8(cmd.getArg(0));
2072 word = cur.selectionAsString(false);
2074 if (word.size() > 100 || word.empty()) {
2075 // Get word or selection
2076 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2077 word = cur.selectionAsString(false);
2079 lang = const_cast<Language *>(cur.getFont().language());
2081 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2082 WordLangTuple wl(word, lang);
2083 theSpellChecker()->accept(wl);
2087 case LFUN_SPELLING_REMOVE: {
2088 docstring word = from_utf8(cmd.getArg(0));
2091 word = cur.selectionAsString(false);
2093 if (word.size() > 100 || word.empty()) {
2094 // Get word or selection
2095 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2096 word = cur.selectionAsString(false);
2098 lang = const_cast<Language *>(cur.getFont().language());
2100 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2101 WordLangTuple wl(word, lang);
2102 theSpellChecker()->remove(wl);
2106 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2107 // Given data, an encoding of the ParagraphParameters
2108 // generated in the Paragraph dialog, this function sets
2109 // the current paragraph, or currently selected paragraphs,
2111 // NOTE: This function overrides all existing settings.
2112 setParagraphs(cur, cmd.argument());
2113 cur.message(_("Paragraph layout set"));
2117 case LFUN_PARAGRAPH_PARAMS: {
2118 // Given data, an encoding of the ParagraphParameters as we'd
2119 // find them in a LyX file, this function modifies the current paragraph,
2120 // or currently selected paragraphs.
2121 // NOTE: This function only modifies, and does not override, existing
2123 setParagraphs(cur, cmd.argument(), true);
2124 cur.message(_("Paragraph layout set"));
2129 if (cur.selection()) {
2130 cur.setSelection(false);
2133 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2134 // correct, but I'm not 100% sure -- dov, 071019
2135 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2139 case LFUN_OUTLINE_UP:
2140 outline(OutlineUp, cur);
2141 setCursor(cur, cur.pit(), 0);
2142 cur.forceBufferUpdate();
2146 case LFUN_OUTLINE_DOWN:
2147 outline(OutlineDown, cur);
2148 setCursor(cur, cur.pit(), 0);
2149 cur.forceBufferUpdate();
2153 case LFUN_OUTLINE_IN:
2154 outline(OutlineIn, cur);
2155 cur.forceBufferUpdate();
2159 case LFUN_OUTLINE_OUT:
2160 outline(OutlineOut, cur);
2161 cur.forceBufferUpdate();
2166 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2171 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2173 // FIXME: The cursor flag is reset two lines below
2174 // so we need to check here if some of the LFUN did touch that.
2175 // for now only Text::erase() and Text::backspace() do that.
2176 // The plan is to verify all the LFUNs and then to remove this
2177 // singleParUpdate boolean altogether.
2178 if (cur.result().screenUpdate() & Update::Force) {
2179 singleParUpdate = false;
2183 // FIXME: the following code should go in favor of fine grained
2184 // update flag treatment.
2185 if (singleParUpdate) {
2186 // Inserting characters does not change par height in general. So, try
2187 // to update _only_ this paragraph. BufferView will detect if a full
2188 // metrics update is needed anyway.
2189 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2194 && &oldTopSlice.inset() == &cur.inset()
2195 && oldTopSlice.idx() == cur.idx()
2196 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2197 && !cur.selection())
2198 // FIXME: it would be better if we could just do this
2200 //if (cur.result().update() != Update::FitCursor)
2201 // cur.noScreenUpdate();
2203 // But some LFUNs do not set Update::FitCursor when needed, so we
2204 // do it for all. This is not very harmfull as FitCursor will provoke
2205 // a full redraw only if needed but still, a proper review of all LFUN
2206 // should be done and this needsUpdate boolean can then be removed.
2207 cur.screenUpdateFlags(Update::FitCursor);
2209 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2213 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2214 FuncStatus & flag) const
2216 LASSERT(cur.text() == this, /**/);
2218 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2220 InsetCode code = NO_CODE;
2222 switch (cmd.action()) {
2224 case LFUN_DEPTH_DECREMENT:
2225 enable = changeDepthAllowed(cur, DEC_DEPTH);
2228 case LFUN_DEPTH_INCREMENT:
2229 enable = changeDepthAllowed(cur, INC_DEPTH);
2233 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2236 case LFUN_DIALOG_SHOW_NEW_INSET:
2237 if (cmd.argument() == "bibitem")
2238 code = BIBITEM_CODE;
2239 else if (cmd.argument() == "bibtex") {
2241 // not allowed in description items
2242 enable = !inDescriptionItem(cur);
2244 else if (cmd.argument() == "box")
2246 else if (cmd.argument() == "branch")
2248 else if (cmd.argument() == "citation")
2250 else if (cmd.argument() == "ert")
2252 else if (cmd.argument() == "external")
2253 code = EXTERNAL_CODE;
2254 else if (cmd.argument() == "float")
2256 else if (cmd.argument() == "graphics")
2257 code = GRAPHICS_CODE;
2258 else if (cmd.argument() == "href")
2259 code = HYPERLINK_CODE;
2260 else if (cmd.argument() == "include")
2261 code = INCLUDE_CODE;
2262 else if (cmd.argument() == "index")
2264 else if (cmd.argument() == "index_print")
2265 code = INDEX_PRINT_CODE;
2266 else if (cmd.argument() == "nomenclature")
2267 code = NOMENCL_CODE;
2268 else if (cmd.argument() == "nomencl_print")
2269 code = NOMENCL_PRINT_CODE;
2270 else if (cmd.argument() == "label")
2272 else if (cmd.argument() == "line")
2274 else if (cmd.argument() == "note")
2276 else if (cmd.argument() == "phantom")
2277 code = PHANTOM_CODE;
2278 else if (cmd.argument() == "ref")
2280 else if (cmd.argument() == "space")
2282 else if (cmd.argument() == "toc")
2284 else if (cmd.argument() == "vspace")
2286 else if (cmd.argument() == "wrap")
2288 else if (cmd.argument() == "listings")
2289 code = LISTINGS_CODE;
2292 case LFUN_ERT_INSERT:
2295 case LFUN_LISTING_INSERT:
2296 code = LISTINGS_CODE;
2297 // not allowed in description items
2298 enable = !inDescriptionItem(cur);
2300 case LFUN_FOOTNOTE_INSERT:
2303 case LFUN_TABULAR_INSERT:
2304 code = TABULAR_CODE;
2306 case LFUN_MARGINALNOTE_INSERT:
2309 case LFUN_FLOAT_INSERT:
2310 case LFUN_FLOAT_WIDE_INSERT:
2311 // FIXME: If there is a selection, we should check whether there
2312 // are floats in the selection, but this has performance issues, see
2313 // LFUN_CHANGE_ACCEPT/REJECT.
2315 if (inDescriptionItem(cur))
2316 // not allowed in description items
2319 InsetCode const inset_code = cur.inset().lyxCode();
2321 // algorithm floats cannot be put in another float
2322 if (to_utf8(cmd.argument()) == "algorithm") {
2323 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2327 // for figures and tables: only allow in another
2328 // float or wrap if it is of the same type and
2329 // not a subfloat already
2330 if(cur.inset().lyxCode() == code) {
2331 InsetFloat const & ins =
2332 static_cast<InsetFloat const &>(cur.inset());
2333 enable = ins.params().type == to_utf8(cmd.argument())
2334 && !ins.params().subfloat;
2335 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2336 InsetWrap const & ins =
2337 static_cast<InsetWrap const &>(cur.inset());
2338 enable = ins.params().type == to_utf8(cmd.argument());
2342 case LFUN_WRAP_INSERT:
2344 // not allowed in description items
2345 enable = !inDescriptionItem(cur);
2347 case LFUN_FLOAT_LIST_INSERT: {
2348 code = FLOAT_LIST_CODE;
2349 // not allowed in description items
2350 enable = !inDescriptionItem(cur);
2352 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2353 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2354 // make sure we know about such floats
2355 if (cit == floats.end() ||
2356 // and that we know how to generate a list of them
2357 (!cit->second.needsFloatPkg() && cit->second.listCommand().empty())) {
2358 flag.setUnknown(true);
2359 // probably not necessary, but...
2365 case LFUN_CAPTION_INSERT:
2366 code = CAPTION_CODE;
2367 // not allowed in description items
2368 enable = !inDescriptionItem(cur);
2370 case LFUN_NOTE_INSERT:
2372 // in commands (sections etc.) and description items,
2373 // only Notes are allowed
2374 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2375 (!cur.paragraph().layout().isCommand()
2376 && !inDescriptionItem(cur)));
2378 case LFUN_FLEX_INSERT: {
2380 string s = cmd.getArg(0);
2382 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2383 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2384 il.lyxtype() != InsetLayout::CUSTOM &&
2385 il.lyxtype() != InsetLayout::ELEMENT &&
2386 il.lyxtype ()!= InsetLayout::STANDARD)
2390 case LFUN_BOX_INSERT:
2393 case LFUN_BRANCH_INSERT:
2395 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2396 && cur.buffer()->params().branchlist().empty())
2399 case LFUN_PHANTOM_INSERT:
2400 code = PHANTOM_CODE;
2402 case LFUN_LABEL_INSERT:
2405 case LFUN_INFO_INSERT:
2408 case LFUN_ARGUMENT_INSERT: {
2410 Layout const & lay = cur.paragraph().layout();
2411 int const numargs = lay.reqargs + lay.optargs;
2412 enable = cur.paragraph().insetList().count(ARG_CODE) < numargs;
2415 case LFUN_INDEX_INSERT:
2418 case LFUN_INDEX_PRINT:
2419 code = INDEX_PRINT_CODE;
2420 // not allowed in description items
2421 enable = !inDescriptionItem(cur);
2423 case LFUN_NOMENCL_INSERT:
2424 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2428 code = NOMENCL_CODE;
2430 case LFUN_NOMENCL_PRINT:
2431 code = NOMENCL_PRINT_CODE;
2432 // not allowed in description items
2433 enable = !inDescriptionItem(cur);
2435 case LFUN_HYPERLINK_INSERT:
2436 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2440 code = HYPERLINK_CODE;
2442 case LFUN_QUOTE_INSERT:
2443 // always allow this, since we will inset a raw quote
2444 // if an inset is not allowed.
2446 case LFUN_SPECIALCHAR_INSERT:
2447 code = SPECIALCHAR_CODE;
2449 case LFUN_SPACE_INSERT:
2450 // slight hack: we know this is allowed in math mode
2454 case LFUN_PREVIEW_INSERT:
2455 code = PREVIEW_CODE;
2457 case LFUN_SCRIPT_INSERT:
2461 case LFUN_MATH_INSERT:
2462 case LFUN_MATH_AMS_MATRIX:
2463 case LFUN_MATH_MATRIX:
2464 case LFUN_MATH_DELIM:
2465 case LFUN_MATH_BIGDELIM:
2466 case LFUN_MATH_DISPLAY:
2467 case LFUN_MATH_MODE:
2468 case LFUN_MATH_MACRO:
2469 case LFUN_MATH_SUBSCRIPT:
2470 case LFUN_MATH_SUPERSCRIPT:
2471 code = MATH_HULL_CODE;
2474 case LFUN_REGEXP_MODE:
2475 code = MATH_HULL_CODE;
2476 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2479 case LFUN_INSET_MODIFY:
2480 // We need to disable this, because we may get called for a
2482 // InsetTabular::getStatus() -> InsetText::getStatus()
2483 // and we don't handle LFUN_INSET_MODIFY.
2487 case LFUN_FONT_EMPH:
2488 flag.setOnOff(fontinfo.emph() == FONT_ON);
2489 enable = !cur.inset().getLayout().isPassThru();
2492 case LFUN_FONT_ITAL:
2493 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2494 enable = !cur.inset().getLayout().isPassThru();
2497 case LFUN_FONT_NOUN:
2498 flag.setOnOff(fontinfo.noun() == FONT_ON);
2499 enable = !cur.inset().getLayout().isPassThru();
2502 case LFUN_FONT_BOLD:
2503 case LFUN_FONT_BOLDSYMBOL:
2504 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2505 enable = !cur.inset().getLayout().isPassThru();
2508 case LFUN_FONT_SANS:
2509 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2510 enable = !cur.inset().getLayout().isPassThru();
2513 case LFUN_FONT_ROMAN:
2514 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2515 enable = !cur.inset().getLayout().isPassThru();
2518 case LFUN_FONT_TYPEWRITER:
2519 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2520 enable = !cur.inset().getLayout().isPassThru();
2525 enable = cur.selection();
2529 if (cmd.argument().empty()) {
2530 if (theClipboard().isInternal())
2531 enable = cap::numberOfSelections() > 0;
2533 enable = !theClipboard().empty();
2537 // we have an argument
2538 string const arg = to_utf8(cmd.argument());
2539 if (isStrUnsignedInt(arg)) {
2540 // it's a number and therefore means the internal stack
2541 unsigned int n = convert<unsigned int>(arg);
2542 enable = cap::numberOfSelections() > n;
2546 // explicit graphics type?
2547 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2548 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2549 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2550 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2551 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2552 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2553 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2554 enable = theClipboard().hasGraphicsContents(type);
2563 case LFUN_CLIPBOARD_PASTE:
2564 enable = !theClipboard().empty();
2567 case LFUN_PRIMARY_SELECTION_PASTE:
2568 enable = cur.selection() || !theSelection().empty();
2571 case LFUN_SELECTION_PASTE:
2572 enable = cap::selection();
2575 case LFUN_PARAGRAPH_MOVE_UP:
2576 enable = cur.pit() > 0 && !cur.selection();
2579 case LFUN_PARAGRAPH_MOVE_DOWN:
2580 enable = cur.pit() < cur.lastpit() && !cur.selection();
2583 case LFUN_CHANGE_ACCEPT:
2584 case LFUN_CHANGE_REJECT:
2585 // In principle, these LFUNs should only be enabled if there
2586 // is a change at the current position/in the current selection.
2587 // However, without proper optimizations, this will inevitably
2588 // result in unacceptable performance - just imagine a user who
2589 // wants to select the complete content of a long document.
2590 if (!cur.selection())
2591 enable = cur.paragraph().isChanged(cur.pos());
2593 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2598 case LFUN_OUTLINE_UP:
2599 case LFUN_OUTLINE_DOWN:
2600 case LFUN_OUTLINE_IN:
2601 case LFUN_OUTLINE_OUT:
2602 // FIXME: LyX is not ready for outlining within inset.
2603 enable = isMainText()
2604 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2607 case LFUN_NEWLINE_INSERT:
2608 // LaTeX restrictions (labels or empty par)
2609 enable = (cur.pos() > cur.paragraph().beginOfBody());
2612 case LFUN_TAB_INSERT:
2613 case LFUN_TAB_DELETE:
2614 enable = cur.inset().getLayout().isPassThru();
2617 case LFUN_SET_GRAPHICS_GROUP: {
2618 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2622 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2626 case LFUN_NEWPAGE_INSERT:
2627 // not allowed in description items
2628 code = NEWPAGE_CODE;
2629 enable = !inDescriptionItem(cur);
2632 case LFUN_DATE_INSERT: {
2633 string const format = cmd.argument().empty()
2634 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2635 enable = support::os::is_valid_strftime(format);
2640 enable = !cur.inset().getLayout().isPassThru();
2641 flag.setOnOff(to_utf8(cmd.argument()) == cur.real_current_font.language()->lang());
2644 case LFUN_BREAK_PARAGRAPH:
2645 enable = cur.inset().getLayout().isMultiPar();
2648 case LFUN_SPELLING_ADD:
2649 case LFUN_SPELLING_IGNORE:
2650 case LFUN_SPELLING_REMOVE:
2651 enable = theSpellChecker();
2655 enable = !cur.inset().forcePlainLayout();
2658 case LFUN_LAYOUT_PARAGRAPH:
2659 case LFUN_PARAGRAPH_PARAMS:
2660 case LFUN_PARAGRAPH_PARAMS_APPLY:
2661 case LFUN_PARAGRAPH_UPDATE:
2662 enable = cur.inset().allowParagraphCustomization();
2665 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2666 case LFUN_ACCENT_ACUTE:
2667 case LFUN_ACCENT_BREVE:
2668 case LFUN_ACCENT_CARON:
2669 case LFUN_ACCENT_CEDILLA:
2670 case LFUN_ACCENT_CIRCLE:
2671 case LFUN_ACCENT_CIRCUMFLEX:
2672 case LFUN_ACCENT_DOT:
2673 case LFUN_ACCENT_GRAVE:
2674 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2675 case LFUN_ACCENT_MACRON:
2676 case LFUN_ACCENT_OGONEK:
2677 case LFUN_ACCENT_TIE:
2678 case LFUN_ACCENT_TILDE:
2679 case LFUN_ACCENT_UMLAUT:
2680 case LFUN_ACCENT_UNDERBAR:
2681 case LFUN_ACCENT_UNDERDOT:
2682 case LFUN_FONT_DEFAULT:
2683 case LFUN_FONT_FRAK:
2684 case LFUN_FONT_SIZE:
2685 case LFUN_FONT_STATE:
2686 case LFUN_FONT_UNDERLINE:
2687 case LFUN_FONT_STRIKEOUT:
2688 case LFUN_FONT_UULINE:
2689 case LFUN_FONT_UWAVE:
2690 case LFUN_TEXTSTYLE_APPLY:
2691 case LFUN_TEXTSTYLE_UPDATE:
2692 enable = !cur.inset().getLayout().isPassThru();
2695 case LFUN_WORD_DELETE_FORWARD:
2696 case LFUN_WORD_DELETE_BACKWARD:
2697 case LFUN_LINE_DELETE:
2698 case LFUN_WORD_FORWARD:
2699 case LFUN_WORD_BACKWARD:
2700 case LFUN_WORD_RIGHT:
2701 case LFUN_WORD_LEFT:
2702 case LFUN_CHAR_FORWARD:
2703 case LFUN_CHAR_FORWARD_SELECT:
2704 case LFUN_CHAR_BACKWARD:
2705 case LFUN_CHAR_BACKWARD_SELECT:
2706 case LFUN_CHAR_LEFT:
2707 case LFUN_CHAR_LEFT_SELECT:
2708 case LFUN_CHAR_RIGHT:
2709 case LFUN_CHAR_RIGHT_SELECT:
2711 case LFUN_UP_SELECT:
2713 case LFUN_DOWN_SELECT:
2714 case LFUN_PARAGRAPH_UP_SELECT:
2715 case LFUN_PARAGRAPH_DOWN_SELECT:
2716 case LFUN_LINE_BEGIN_SELECT:
2717 case LFUN_LINE_END_SELECT:
2718 case LFUN_WORD_FORWARD_SELECT:
2719 case LFUN_WORD_BACKWARD_SELECT:
2720 case LFUN_WORD_RIGHT_SELECT:
2721 case LFUN_WORD_LEFT_SELECT:
2722 case LFUN_WORD_SELECT:
2723 case LFUN_SECTION_SELECT:
2724 case LFUN_BUFFER_BEGIN:
2725 case LFUN_BUFFER_END:
2726 case LFUN_BUFFER_BEGIN_SELECT:
2727 case LFUN_BUFFER_END_SELECT:
2728 case LFUN_INSET_BEGIN:
2729 case LFUN_INSET_END:
2730 case LFUN_INSET_BEGIN_SELECT:
2731 case LFUN_INSET_END_SELECT:
2732 case LFUN_INSET_SELECT_ALL:
2733 case LFUN_PARAGRAPH_UP:
2734 case LFUN_PARAGRAPH_DOWN:
2735 case LFUN_LINE_BEGIN:
2737 case LFUN_CHAR_DELETE_FORWARD:
2738 case LFUN_CHAR_DELETE_BACKWARD:
2739 case LFUN_WORD_UPCASE:
2740 case LFUN_WORD_LOWCASE:
2741 case LFUN_WORD_CAPITALIZE:
2742 case LFUN_CHARS_TRANSPOSE:
2743 case LFUN_SERVER_GET_XY:
2744 case LFUN_SERVER_SET_XY:
2745 case LFUN_SERVER_GET_LAYOUT:
2746 case LFUN_SELF_INSERT:
2747 case LFUN_UNICODE_INSERT:
2748 case LFUN_THESAURUS_ENTRY:
2750 // these are handled in our dispatch()
2754 case LFUN_INSET_INSERT: {
2755 string const type = cmd.getArg(0);
2756 if (type == "toc") {
2758 // not allowed in description items
2759 //FIXME: couldn't this be merged in Inset::insetAllowed()?
2760 enable = !inDescriptionItem(cur);
2773 || !cur.inset().insetAllowed(code)
2774 || cur.paragraph().layout().pass_thru))
2777 flag.setEnabled(enable);
2782 void Text::pasteString(Cursor & cur, docstring const & clip,
2785 cur.clearSelection();
2786 if (!clip.empty()) {
2789 insertStringAsParagraphs(cur, clip, cur.current_font);
2791 insertStringAsLines(cur, clip, cur.current_font);
2796 // FIXME: an item inset would make things much easier.
2797 bool Text::inDescriptionItem(Cursor & cur) const
2799 Paragraph & par = cur.paragraph();
2800 pos_type const pos = cur.pos();
2801 pos_type const body_pos = par.beginOfBody();
2803 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2804 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2805 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2808 return (pos < body_pos
2810 && (pos == 0 || par.getChar(pos - 1) != ' ')));