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 "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
50 #include "frontends/Application.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/LyXView.h"
53 #include "frontends/Selection.h"
54 #include "frontends/WorkArea.h"
56 #include "insets/InsetCollapsable.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetFloat.h"
60 #include "insets/InsetFloatList.h"
61 #include "insets/InsetGraphics.h"
62 #include "insets/InsetGraphicsParams.h"
63 #include "insets/InsetNewline.h"
64 #include "insets/InsetQuotes.h"
65 #include "insets/InsetSpecialChar.h"
66 #include "insets/InsetText.h"
67 #include "insets/InsetWrap.h"
69 #include "support/convert.h"
70 #include "support/debug.h"
71 #include "support/gettext.h"
72 #include "support/lstrings.h"
73 #include "support/lyxtime.h"
74 #include "support/os.h"
76 #include "mathed/InsetMathHull.h"
77 #include "mathed/MathMacroTemplate.h"
79 #include <boost/next_prior.hpp>
85 using namespace lyx::support;
89 using cap::copySelection;
90 using cap::cutSelection;
91 using cap::pasteFromStack;
92 using cap::pasteClipboardText;
93 using cap::pasteClipboardGraphics;
94 using cap::replaceSelection;
95 using cap::grabAndEraseSelection;
96 using cap::selClearOrDel;
99 static Font freefont(ignore_font, ignore_language);
100 static bool toggleall = false;
102 static void toggleAndShow(Cursor & cur, Text * text,
103 Font const & font, bool toggleall = true)
105 text->toggleFree(cur, font, toggleall);
107 if (font.language() != ignore_language ||
108 font.fontInfo().number() != FONT_IGNORE) {
109 TextMetrics const & tm = cur.bv().textMetrics(text);
110 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
111 cur.real_current_font))
112 text->setCursor(cur, cur.pit(), cur.pos(),
113 false, !cur.boundary());
118 static void moveCursor(Cursor & cur, bool selecting)
120 if (selecting || cur.mark())
125 static void finishChange(Cursor & cur, bool selecting)
128 moveCursor(cur, selecting);
132 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
135 docstring sel = cur.selectionAsString(false);
137 // It may happen that sel is empty but there is a selection
138 replaceSelection(cur);
140 // Is this a valid formula?
144 #ifdef ENABLE_ASSERTIONS
145 const int old_pos = cur.pos();
147 cur.insert(new InsetMathHull(hullSimple));
148 #ifdef ENABLE_ASSERTIONS
149 LASSERT(old_pos == cur.pos(), /**/);
151 cur.nextInset()->edit(cur, true);
152 // don't do that also for LFUN_MATH_MODE
153 // unless you want end up with always changing
154 // to mathrm when opening an inlined inset --
155 // I really hate "LyXfunc overloading"...
157 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
158 // Avoid an unnecessary undo step if cmd.argument
160 if (!cmd.argument().empty())
161 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
164 // create a macro if we see "\\newcommand"
165 // somewhere, and an ordinary formula
167 if (sel.find(from_ascii("\\newcommand")) == string::npos
168 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
169 && sel.find(from_ascii("\\def")) == string::npos)
171 InsetMathHull * formula = new InsetMathHull;
172 string const selstr = to_utf8(sel);
173 istringstream is(selstr);
176 if (!formula->readQuiet(lex)) {
177 // No valid formula, let's try with delims
178 is.str("$" + selstr + "$");
180 if (!formula->readQuiet(lex)) {
181 // Still not valid, leave it as is
190 cur.insert(new MathMacroTemplate(sel));
194 cur.message(from_utf8(N_("Math editor mode")));
196 cur.message(from_utf8(N_("No valid math formula")));
200 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
202 BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
203 if (cur.inRegexped()) {
204 cur.message(_("Already in regexp mode"));
208 docstring const save_selection = grabAndEraseSelection(cur);
210 // replaceSelection(cur);
212 cur.insert(new InsetMathHull(hullRegexp));
213 cur.nextInset()->edit(cur, true);
214 cur.niceInsert(save_selection);
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 * insetText = dynamic_cast<InsetText *>(inset);
272 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
273 // reset first par to default
274 cur.text()->paragraphs().begin()
275 ->setPlainOrDefaultLayout(bparams.documentClass());
278 // Merge multiple paragraphs -- hack
279 while (cur.lastpit() > 0)
280 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
281 cur.leaveInset(*inset);
283 cur.leaveInset(*inset);
284 // reset surrounding par to default
285 DocumentClass const & dc = bparams.documentClass();
286 docstring const layoutname = inset->usePlainLayout()
287 ? dc.plainLayoutName()
288 : dc.defaultLayoutName();
289 text->setLayout(cur, layoutname);
296 string const freefont2string()
298 return freefont.toString(toggleall);
302 static void dragMove(Cursor & cur, int moveid, int movetoid)
304 // Create pointers to buffers
305 Buffer & buf_move = *cur.buffer();
306 DocIterator dit_move = buf_move.getParFromID(moveid);
307 DocIterator dit_dest = buf_move.getParFromID(movetoid);
309 pit_type & pit_move = dit_move.pit();
310 pit_type & pit_dest = dit_dest.pit();
311 ParagraphList & pars = dit_move.text()->paragraphs();
313 // Create references to the paragraphs to be moved
314 ParagraphList::iterator const bgn = pars.begin();
315 ParagraphList::iterator dest_start = boost::next(bgn, pit_dest);
317 // The first paragraph of the area to be copied:
318 ParagraphList::iterator start = boost::next(bgn, pit_move);
319 // The final paragraph of area to be copied:
320 ParagraphList::iterator finish = start;
321 ParagraphList::iterator const end = pars.end();
323 // Move out (down) from this section header
327 // Seek the one (on same level) below
328 int const thistoclevel = start->layout().toclevel;
329 for (; finish != end; ++finish) {
330 int const toclevel = finish->layout().toclevel;
331 if (toclevel != Layout::NOT_IN_TOC
332 && toclevel <= thistoclevel)
336 if (start == pars.begin() || start == dest_start)
340 pars.insert(dest_start, start, finish);
341 pars.erase(start, finish);
343 // FIXME: This only really needs doing for the newly
344 // introduced paragraphs. Something like:
345 // pit_type const numpars = distance(start, finish);
346 // start = boost::next(bgn, pit);
347 // finish = boost::next(start, numpars);
348 // for (; start != finish; ++start)
349 // start->setBuffer(buf);
350 // But while this seems to work, it is kind of fragile.
351 buf_move.inset().setBuffer(buf_move);
355 /// the type of outline operation
357 OutlineUp, // Move this header with text down
358 OutlineDown, // Move this header with text up
359 OutlineIn, // Make this header deeper
360 OutlineOut // Make this header shallower
364 static void outline(OutlineOp mode, Cursor & cur)
366 Buffer & buf = *cur.buffer();
367 pit_type & pit = cur.pit();
368 ParagraphList & pars = buf.text().paragraphs();
369 ParagraphList::iterator const bgn = pars.begin();
370 // The first paragraph of the area to be copied:
371 ParagraphList::iterator start = boost::next(bgn, pit);
372 // The final paragraph of area to be copied:
373 ParagraphList::iterator finish = start;
374 ParagraphList::iterator const end = pars.end();
376 DocumentClass const & tc = buf.params().documentClass();
378 int const thistoclevel = start->layout().toclevel;
381 // Move out (down) from this section header
385 // Seek the one (on same level) below
386 for (; finish != end; ++finish) {
387 toclevel = finish->layout().toclevel;
388 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
392 // Do we need to set insets' buffer_ members, because we copied
393 // some stuff? We'll assume we do and reset it otherwise.
394 bool set_buffers = true;
398 if (start == pars.begin())
401 ParagraphList::iterator dest = start;
402 // Move out (up) from this header
405 // Search previous same-level header above
408 toclevel = dest->layout().toclevel;
410 && (toclevel == Layout::NOT_IN_TOC
411 || toclevel > thistoclevel));
412 // Not found; do nothing
413 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
415 pit_type const newpit = distance(bgn, dest);
416 pit_type const len = distance(start, finish);
417 pit_type const deletepit = pit + len;
418 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
419 pars.insert(dest, start, finish);
420 start = boost::next(pars.begin(), deletepit);
422 pars.erase(start, finish);
429 // Go one down from *this* header:
430 ParagraphList::iterator dest = boost::next(finish, 1);
431 // Go further down to find header to insert in front of:
432 for (; dest != end; ++dest) {
433 toclevel = dest->layout().toclevel;
434 if (toclevel != Layout::NOT_IN_TOC
435 && toclevel <= thistoclevel)
438 // One such was found:
439 pit_type newpit = distance(bgn, dest);
440 pit_type const len = distance(start, finish);
441 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
442 pars.insert(dest, start, finish);
443 start = boost::next(bgn, pit);
445 pars.erase(start, finish);
449 pit_type const len = distance(start, finish);
450 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
451 for (; start != finish; ++start) {
452 toclevel = start->layout().toclevel;
453 if (toclevel == Layout::NOT_IN_TOC)
455 DocumentClass::const_iterator lit = tc.begin();
456 DocumentClass::const_iterator len = tc.end();
457 for (; lit != len; ++lit) {
458 if (lit->toclevel == toclevel + 1 &&
459 start->layout().labeltype == lit->labeltype) {
460 start->setLayout(*lit);
469 pit_type const len = distance(start, finish);
470 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
471 for (; start != finish; ++start) {
472 toclevel = start->layout().toclevel;
473 if (toclevel == Layout::NOT_IN_TOC)
475 DocumentClass::const_iterator lit = tc.begin();
476 DocumentClass::const_iterator len = tc.end();
477 for (; lit != len; ++lit) {
478 if (lit->toclevel == toclevel - 1 &&
479 start->layout().labeltype == lit->labeltype) {
480 start->setLayout(*lit);
490 // FIXME This only really needs doing for the newly introduced
491 // paragraphs. Something like:
492 // pit_type const numpars = distance(start, finish);
493 // start = boost::next(bgn, pit);
494 // finish = boost::next(start, numpars);
495 // for (; start != finish; ++start)
496 // start->setBuffer(buf);
497 // But while this seems to work, it is kind of fragile.
498 buf.inset().setBuffer(buf);
502 void Text::number(Cursor & cur)
504 FontInfo font = ignore_font;
505 font.setNumber(FONT_TOGGLE);
506 toggleAndShow(cur, this, Font(font, ignore_language));
510 bool Text::isRTL(Paragraph const & par) const
512 Buffer const & buffer = owner_->buffer();
513 return par.isRTL(buffer.params());
517 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
519 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
521 // Dispatch if the cursor is inside the text. It is not the
522 // case for context menus (bug 5797).
523 if (cur.text() != this) {
528 BufferView * bv = &cur.bv();
529 TextMetrics * tm = &bv->textMetrics(this);
530 if (!tm->contains(cur.pit())) {
531 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
532 tm = &bv->textMetrics(this);
535 // FIXME: We use the update flag to indicates wether a singlePar or a
536 // full screen update is needed. We reset it here but shall we restore it
540 LASSERT(cur.text() == this, /**/);
541 CursorSlice oldTopSlice = cur.top();
542 bool oldBoundary = cur.boundary();
543 bool sel = cur.selection();
544 // Signals that, even if needsUpdate == false, an update of the
545 // cursor paragraph is required
546 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
547 LyXAction::SingleParUpdate);
548 // Signals that a full-screen update is required
549 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
550 LyXAction::NoUpdate) || singleParUpdate);
552 switch (cmd.action) {
554 case LFUN_PARAGRAPH_MOVE_DOWN: {
555 pit_type const pit = cur.pit();
556 recUndo(cur, pit, pit + 1);
558 swap(pars_[pit], pars_[pit + 1]);
559 cur.buffer()->updateLabels();
565 case LFUN_PARAGRAPH_MOVE_UP: {
566 pit_type const pit = cur.pit();
567 recUndo(cur, pit - 1, pit);
569 swap(pars_[pit], pars_[pit - 1]);
570 cur.buffer()->updateLabels();
576 case LFUN_APPENDIX: {
577 Paragraph & par = cur.paragraph();
578 bool start = !par.params().startOfAppendix();
580 // FIXME: The code below only makes sense at top level.
581 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
582 // ensure that we have only one start_of_appendix in this document
583 // FIXME: this don't work for multipart document!
584 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
585 if (pars_[tmp].params().startOfAppendix()) {
587 pars_[tmp].params().startOfAppendix(false);
593 par.params().startOfAppendix(start);
595 // we can set the refreshing parameters now
596 cur.buffer()->updateLabels();
600 case LFUN_WORD_DELETE_FORWARD:
602 cutSelection(cur, true, false);
604 deleteWordForward(cur);
605 finishChange(cur, false);
608 case LFUN_WORD_DELETE_BACKWARD:
610 cutSelection(cur, true, false);
612 deleteWordBackward(cur);
613 finishChange(cur, false);
616 case LFUN_LINE_DELETE:
618 cutSelection(cur, true, false);
620 tm->deleteLineForward(cur);
621 finishChange(cur, false);
624 case LFUN_BUFFER_BEGIN:
625 case LFUN_BUFFER_BEGIN_SELECT:
626 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
627 if (cur.depth() == 1)
628 needsUpdate |= cursorTop(cur);
631 cur.updateFlags(Update::FitCursor);
634 case LFUN_BUFFER_END:
635 case LFUN_BUFFER_END_SELECT:
636 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
637 if (cur.depth() == 1)
638 needsUpdate |= cursorBottom(cur);
641 cur.updateFlags(Update::FitCursor);
644 case LFUN_INSET_BEGIN:
645 case LFUN_INSET_BEGIN_SELECT:
646 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
647 if (cur.depth() == 1 || cur.pos() > 0)
648 needsUpdate |= cursorTop(cur);
651 cur.updateFlags(Update::FitCursor);
655 case LFUN_INSET_END_SELECT:
656 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
657 if (cur.depth() == 1 || cur.pos() < cur.lastpos())
658 needsUpdate |= cursorBottom(cur);
661 cur.updateFlags(Update::FitCursor);
664 case LFUN_CHAR_FORWARD:
665 case LFUN_CHAR_FORWARD_SELECT:
666 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
667 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
668 needsUpdate |= cursorForward(cur);
670 if (!needsUpdate && oldTopSlice == cur.top()
671 && cur.boundary() == oldBoundary) {
673 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
675 // we will probably be moving out the inset, so we should execute
676 // the depm-mechanism, but only when the cursor has a place to
677 // go outside this inset, i.e. in a slice above.
678 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
679 && cur.pit() == cur.lastpit()) {
680 // The cursor hasn't changed yet. To give the
681 // DEPM the possibility of doing something we must
682 // provide it with two different cursors.
684 dummy.pos() = dummy.pit() = 0;
685 cur.bv().checkDepm(dummy, cur);
690 case LFUN_CHAR_BACKWARD:
691 case LFUN_CHAR_BACKWARD_SELECT:
692 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
693 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
694 needsUpdate |= cursorBackward(cur);
696 if (!needsUpdate && oldTopSlice == cur.top()
697 && cur.boundary() == oldBoundary) {
699 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
701 // we will probably be moving out the inset, so we should execute
702 // the depm-mechanism, but only when the cursor has a place to
703 // go outside this inset, i.e. in a slice above.
704 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
705 // The cursor hasn't changed yet. To give the
706 // DEPM the possibility of doing something we must
707 // provide it with two different cursors.
709 dummy.pos() = cur.lastpos();
710 dummy.pit() = cur.lastpit();
711 cur.bv().checkDepm(dummy, cur);
717 case LFUN_CHAR_LEFT_SELECT:
718 if (lyxrc.visual_cursor) {
719 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
720 needsUpdate |= cursorVisLeft(cur);
721 if (!needsUpdate && oldTopSlice == cur.top()
722 && cur.boundary() == oldBoundary) {
724 cmd = FuncRequest(LFUN_FINISHED_LEFT);
727 if (reverseDirectionNeeded(cur)) {
728 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
729 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
731 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
732 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
739 case LFUN_CHAR_RIGHT:
740 case LFUN_CHAR_RIGHT_SELECT:
741 if (lyxrc.visual_cursor) {
742 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
743 needsUpdate |= cursorVisRight(cur);
744 if (!needsUpdate && oldTopSlice == cur.top()
745 && cur.boundary() == oldBoundary) {
747 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
750 if (reverseDirectionNeeded(cur)) {
751 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
752 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
754 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
755 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
764 case LFUN_DOWN_SELECT:
767 // stop/start the selection
768 bool select = cmd.action == LFUN_DOWN_SELECT ||
769 cmd.action == LFUN_UP_SELECT;
771 // move cursor up/down
772 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
773 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
775 if (!atFirstOrLastRow) {
776 needsUpdate |= cur.selHandle(select);
777 cur.selHandle(select);
778 cur.upDownInText(up, needsUpdate);
779 needsUpdate |= cur.beforeDispatchCursor().inMathed();
781 // if the cursor cannot be moved up or down do not remove
782 // the selection right now, but wait for the next dispatch.
784 needsUpdate |= cur.selHandle(select);
785 cur.upDownInText(up, needsUpdate);
792 case LFUN_PARAGRAPH_UP:
793 case LFUN_PARAGRAPH_UP_SELECT:
794 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
795 needsUpdate |= cursorUpParagraph(cur);
798 case LFUN_PARAGRAPH_DOWN:
799 case LFUN_PARAGRAPH_DOWN_SELECT:
800 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
801 needsUpdate |= cursorDownParagraph(cur);
804 case LFUN_LINE_BEGIN:
805 case LFUN_LINE_BEGIN_SELECT:
806 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
807 needsUpdate |= tm->cursorHome(cur);
811 case LFUN_LINE_END_SELECT:
812 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
813 needsUpdate |= tm->cursorEnd(cur);
816 case LFUN_SECTION_SELECT: {
817 Buffer const & buf = *cur.buffer();
818 pit_type const pit = cur.pit();
819 ParagraphList & pars = buf.text().paragraphs();
820 ParagraphList::iterator bgn = pars.begin();
821 // The first paragraph of the area to be selected:
822 ParagraphList::iterator start = boost::next(bgn, pit);
823 // The final paragraph of area to be selected:
824 ParagraphList::iterator finish = start;
825 ParagraphList::iterator end = pars.end();
827 int const thistoclevel = start->layout().toclevel;
828 if (thistoclevel == Layout::NOT_IN_TOC)
832 Cursor const old_cur = cur;
833 needsUpdate |= cur.selHandle(true);
835 // Move out (down) from this section header
839 // Seek the one (on same level) below
840 for (; finish != end; ++finish, ++cur.pit()) {
841 int const toclevel = finish->layout().toclevel;
842 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
845 cur.pos() = cur.lastpos();
847 needsUpdate |= cur != old_cur;
851 case LFUN_WORD_RIGHT:
852 case LFUN_WORD_RIGHT_SELECT:
853 if (lyxrc.visual_cursor) {
854 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
855 needsUpdate |= cursorVisRightOneWord(cur);
856 if (!needsUpdate && oldTopSlice == cur.top()
857 && cur.boundary() == oldBoundary) {
859 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
862 if (reverseDirectionNeeded(cur)) {
863 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
864 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
866 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
867 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
874 case LFUN_WORD_FORWARD:
875 case LFUN_WORD_FORWARD_SELECT:
876 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
877 needsUpdate |= cursorForwardOneWord(cur);
881 case LFUN_WORD_LEFT_SELECT:
882 if (lyxrc.visual_cursor) {
883 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
884 needsUpdate |= cursorVisLeftOneWord(cur);
885 if (!needsUpdate && oldTopSlice == cur.top()
886 && cur.boundary() == oldBoundary) {
888 cmd = FuncRequest(LFUN_FINISHED_LEFT);
891 if (reverseDirectionNeeded(cur)) {
892 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
893 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
895 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
896 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
903 case LFUN_WORD_BACKWARD:
904 case LFUN_WORD_BACKWARD_SELECT:
905 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
906 needsUpdate |= cursorBackwardOneWord(cur);
909 case LFUN_WORD_SELECT: {
910 selectWord(cur, WHOLE_WORD);
911 finishChange(cur, true);
915 case LFUN_NEWLINE_INSERT: {
916 InsetNewlineParams inp;
917 docstring arg = cmd.argument();
918 // this avoids a double undo
919 // FIXME: should not be needed, ideally
920 if (!cur.selection())
922 cap::replaceSelection(cur);
923 if (arg == "linebreak")
924 inp.kind = InsetNewlineParams::LINEBREAK;
926 inp.kind = InsetNewlineParams::NEWLINE;
927 cur.insert(new InsetNewline(inp));
929 moveCursor(cur, false);
933 case LFUN_TAB_INSERT: {
934 bool const multi_par_selection = cur.selection() &&
935 cur.selBegin().pit() != cur.selEnd().pit();
936 if (multi_par_selection) {
937 // If there is a multi-paragraph selection, a tab is inserted
938 // at the beginning of each paragraph.
939 cur.recordUndoSelection();
940 pit_type const pit_end = cur.selEnd().pit();
941 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
942 pars_[pit].insertChar(0, '\t',
943 bv->buffer().params().trackChanges);
944 // Update the selection pos to make sure the selection does not
945 // change as the inserted tab will increase the logical pos.
946 if (cur.anchor_.pit() == pit)
947 cur.anchor_.forwardPos();
948 if (cur.pit() == pit)
953 // Maybe we shouldn't allow tabs within a line, because they
954 // are not (yet) aligned as one might do expect.
955 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
961 case LFUN_TAB_DELETE: {
962 bool const tc = bv->buffer().params().trackChanges;
963 if (cur.selection()) {
964 // If there is a selection, a tab (if present) is removed from
965 // the beginning of each paragraph.
966 cur.recordUndoSelection();
967 pit_type const pit_end = cur.selEnd().pit();
968 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
969 Paragraph & par = paragraphs()[pit];
970 if (par.getChar(0) == '\t') {
971 if (cur.pit() == pit)
973 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
974 cur.anchor_.backwardPos();
976 par.eraseChar(0, tc);
978 // If no tab was present, try to remove up to four spaces.
979 for (int n_spaces = 0;
980 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
981 if (cur.pit() == pit)
983 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
984 cur.anchor_.backwardPos();
986 par.eraseChar(0, tc);
991 // If there is no selection, try to remove a tab or some spaces
992 // before the position of the cursor.
993 Paragraph & par = paragraphs()[cur.pit()];
994 pos_type const pos = cur.pos();
999 char_type const c = par.getChar(pos - 1);
1003 par.eraseChar(cur.pos(), tc);
1005 for (int n_spaces = 0;
1007 && par.getChar(cur.pos() - 1) == ' '
1011 par.eraseChar(cur.pos(), tc);
1018 case LFUN_CHAR_DELETE_FORWARD:
1019 if (!cur.selection()) {
1020 if (cur.pos() == cur.paragraph().size())
1021 // Par boundary, force full-screen update
1022 singleParUpdate = false;
1023 needsUpdate |= erase(cur);
1025 // It is possible to make it a lot faster still
1026 // just comment out the line below...
1028 cutSelection(cur, true, false);
1029 singleParUpdate = false;
1031 moveCursor(cur, false);
1034 case LFUN_CHAR_DELETE_BACKWARD:
1035 if (!cur.selection()) {
1036 if (bv->getIntl().getTransManager().backspace()) {
1037 // Par boundary, full-screen update
1039 singleParUpdate = false;
1040 needsUpdate |= backspace(cur);
1042 // It is possible to make it a lot faster still
1043 // just comment out the line below...
1046 cutSelection(cur, true, false);
1047 singleParUpdate = false;
1051 case LFUN_BREAK_PARAGRAPH:
1052 cap::replaceSelection(cur);
1053 breakParagraph(cur, cmd.argument() == "inverse");
1057 case LFUN_INSET_INSERT: {
1060 // We have to avoid triggering InstantPreview loading
1061 // before inserting into the document. See bug #5626.
1062 bool loaded = bv->buffer().isFullyLoaded();
1063 bv->buffer().setFullyLoaded(false);
1064 Inset * inset = createInset(bv->buffer(), cmd);
1065 bv->buffer().setFullyLoaded(loaded);
1068 // FIXME (Abdel 01/02/2006):
1069 // What follows would be a partial fix for bug 2154:
1070 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
1071 // This automatically put the label inset _after_ a
1072 // numbered section. It should be possible to extend the mechanism
1073 // to any kind of LateX environement.
1074 // The correct way to fix that bug would be at LateX generation.
1075 // I'll let the code here for reference as it could be used for some
1076 // other feature like "automatic labelling".
1078 Paragraph & par = pars_[cur.pit()];
1079 if (inset->lyxCode() == LABEL_CODE
1080 && par.layout().labeltype == LABEL_COUNTER) {
1081 // Go to the end of the paragraph
1082 // Warning: Because of Change-Tracking, the last
1083 // position is 'size()' and not 'size()-1':
1084 cur.pos() = par.size();
1085 // Insert a new paragraph
1086 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1090 if (cur.selection())
1091 cutSelection(cur, true, false);
1095 // trigger InstantPreview now
1096 if (inset->lyxCode() == EXTERNAL_CODE) {
1097 InsetExternal & ins =
1098 static_cast<InsetExternal &>(*inset);
1099 ins.updatePreview();
1106 case LFUN_INSET_DISSOLVE: {
1107 // first, try if there's an inset at cursor
1108 // FIXME: this first part should be moved to
1109 // a LFUN_NEXT_INSET_DISSOLVE, or be called via
1110 // some generic "next-inset inset-dissolve"
1111 Inset * inset = cur.nextInset();
1112 if (inset && inset->isActive()) {
1113 Cursor tmpcur = cur;
1114 tmpcur.pushBackward(*inset);
1115 inset->dispatch(tmpcur, cmd);
1116 if (tmpcur.result().dispatched()) {
1121 // if it did not work, try the underlying inset
1122 if (dissolveInset(cur)) {
1126 // if it did not work, do nothing.
1130 case LFUN_SET_GRAPHICS_GROUP: {
1131 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1137 string id = to_utf8(cmd.argument());
1138 string grp = graphics::getGroupParams(bv->buffer(), id);
1139 InsetGraphicsParams tmp, inspar = ins->getParams();
1142 inspar.groupId = to_utf8(cmd.argument());
1144 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1145 tmp.filename = inspar.filename;
1149 ins->setParams(inspar);
1152 case LFUN_SPACE_INSERT:
1153 if (cur.paragraph().layout().free_spacing)
1154 insertChar(cur, ' ');
1156 doInsertInset(cur, this, cmd, false, false);
1159 moveCursor(cur, false);
1162 case LFUN_SPECIALCHAR_INSERT: {
1163 string const name = to_utf8(cmd.argument());
1164 if (name == "hyphenation")
1165 specialChar(cur, InsetSpecialChar::HYPHENATION);
1166 else if (name == "ligature-break")
1167 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1168 else if (name == "slash")
1169 specialChar(cur, InsetSpecialChar::SLASH);
1170 else if (name == "nobreakdash")
1171 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1172 else if (name == "dots")
1173 specialChar(cur, InsetSpecialChar::LDOTS);
1174 else if (name == "end-of-sentence")
1175 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1176 else if (name == "menu-separator")
1177 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1178 else if (name.empty())
1179 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1181 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1185 case LFUN_WORD_UPCASE:
1186 changeCase(cur, text_uppercase);
1189 case LFUN_WORD_LOWCASE:
1190 changeCase(cur, text_lowercase);
1193 case LFUN_WORD_CAPITALIZE:
1194 changeCase(cur, text_capitalization);
1197 case LFUN_CHARS_TRANSPOSE:
1198 charsTranspose(cur);
1202 cur.message(_("Paste"));
1203 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1204 cap::replaceSelection(cur);
1206 // without argument?
1207 string const arg = to_utf8(cmd.argument());
1209 if (theClipboard().isInternal())
1210 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1211 else if (theClipboard().hasGraphicsContents()
1212 && !theClipboard().hasTextContents())
1213 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1215 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1216 } else if (isStrUnsignedInt(arg)) {
1217 // we have a numerical argument
1218 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1219 convert<unsigned int>(arg));
1221 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1223 type = Clipboard::PdfGraphicsType;
1224 else if (arg == "png")
1225 type = Clipboard::PngGraphicsType;
1226 else if (arg == "jpeg")
1227 type = Clipboard::JpegGraphicsType;
1228 else if (arg == "linkback")
1229 type = Clipboard::LinkBackGraphicsType;
1230 else if (arg == "emf")
1231 type = Clipboard::EmfGraphicsType;
1232 else if (arg == "wmf")
1233 type = Clipboard::WmfGraphicsType;
1236 LASSERT(false, /**/);
1238 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1241 bv->buffer().errors("Paste");
1242 cur.clearSelection(); // bug 393
1248 cutSelection(cur, true, true);
1249 cur.message(_("Cut"));
1254 cur.message(_("Copy"));
1257 case LFUN_SERVER_GET_XY:
1258 cur.message(from_utf8(
1259 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1260 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1263 case LFUN_SERVER_SET_XY: {
1266 istringstream is(to_utf8(cmd.argument()));
1269 lyxerr << "SETXY: Could not parse coordinates in '"
1270 << to_utf8(cmd.argument()) << endl;
1272 tm->setCursorFromCoordinates(cur, x, y);
1276 case LFUN_SERVER_GET_LAYOUT:
1277 cur.message(cur.paragraph().layout().name());
1281 docstring layout = cmd.argument();
1282 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1284 Paragraph const & para = cur.paragraph();
1285 docstring const old_layout = para.layout().name();
1286 DocumentClass const & tclass = bv->buffer().params().documentClass();
1289 layout = tclass.defaultLayoutName();
1291 if (owner_->forcePlainLayout())
1292 // in this case only the empty layout is allowed
1293 layout = tclass.plainLayoutName();
1294 else if (para.usePlainLayout()) {
1295 // in this case, default layout maps to empty layout
1296 if (layout == tclass.defaultLayoutName())
1297 layout = tclass.plainLayoutName();
1299 // otherwise, the empty layout maps to the default
1300 if (layout == tclass.plainLayoutName())
1301 layout = tclass.defaultLayoutName();
1304 bool hasLayout = tclass.hasLayout(layout);
1306 // If the entry is obsolete, use the new one instead.
1308 docstring const & obs = tclass[layout].obsoleted_by();
1314 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1315 from_utf8(N_(" not known")));
1319 bool change_layout = (old_layout != layout);
1321 if (!change_layout && cur.selection() &&
1322 cur.selBegin().pit() != cur.selEnd().pit())
1324 pit_type spit = cur.selBegin().pit();
1325 pit_type epit = cur.selEnd().pit() + 1;
1326 while (spit != epit) {
1327 if (pars_[spit].layout().name() != old_layout) {
1328 change_layout = true;
1336 setLayout(cur, layout);
1341 case LFUN_CLIPBOARD_PASTE:
1342 cur.clearSelection();
1343 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1344 cmd.argument() == "paragraph");
1345 bv->buffer().errors("Paste");
1348 case LFUN_PRIMARY_SELECTION_PASTE:
1349 pasteString(cur, theSelection().get(),
1350 cmd.argument() == "paragraph");
1353 case LFUN_SELECTION_PASTE:
1354 // Copy the selection buffer to the clipboard stack,
1355 // because we want it to appear in the "Edit->Paste
1357 cap::copySelectionToStack();
1358 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1359 bv->buffer().errors("Paste");
1362 case LFUN_UNICODE_INSERT: {
1363 if (cmd.argument().empty())
1365 docstring hexstring = cmd.argument();
1366 if (isHex(hexstring)) {
1367 char_type c = hexToInt(hexstring);
1368 if (c >= 32 && c < 0x10ffff) {
1369 lyxerr << "Inserting c: " << c << endl;
1370 docstring s = docstring(1, c);
1371 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1377 case LFUN_QUOTE_INSERT: {
1378 Paragraph & par = cur.paragraph();
1379 pos_type pos = cur.pos();
1380 BufferParams const & bufparams = bv->buffer().params();
1381 Layout const & style = par.layout();
1382 InsetLayout const & ilayout = cur.inset().getLayout();
1383 if (!style.pass_thru && !ilayout.isPassThru()
1384 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1385 // this avoids a double undo
1386 // FIXME: should not be needed, ideally
1387 if (!cur.selection())
1389 cap::replaceSelection(cur);
1394 else if (cur.prevInset() && cur.prevInset()->isSpace())
1397 c = par.getChar(pos - 1);
1398 string arg = to_utf8(cmd.argument());
1399 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1400 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1404 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1408 case LFUN_DATE_INSERT: {
1409 string const format = cmd.argument().empty()
1410 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1411 string const time = formatted_time(current_time(), format);
1412 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1416 case LFUN_MOUSE_TRIPLE:
1417 if (cmd.button() == mouse_button::button1) {
1418 tm->cursorHome(cur);
1426 case LFUN_MOUSE_DOUBLE:
1427 if (cmd.button() == mouse_button::button1) {
1428 selectWord(cur, WHOLE_WORD_STRICT);
1433 // Single-click on work area
1434 case LFUN_MOUSE_PRESS:
1435 // We are not marking a selection with the keyboard in any case.
1436 cur.bv().cursor().setMark(false);
1437 switch (cmd.button()) {
1438 case mouse_button::button1:
1440 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1441 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1444 case mouse_button::button2:
1445 // Middle mouse pasting.
1446 bv->mouseSetCursor(cur);
1448 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1449 "selection-paste ; primary-selection-paste paragraph"));
1453 case mouse_button::button3: {
1454 Cursor const & bvcur = cur.bv().cursor();
1455 // Don't do anything if we right-click a
1456 // selection, a context menu will popup.
1457 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1458 && cur < bvcur.selectionEnd()) {
1462 if (!bv->mouseSetCursor(cur, false))
1463 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1469 } // switch (cmd.button())
1472 case LFUN_MOUSE_MOTION: {
1473 // Mouse motion with right or middle mouse do nothing for now.
1474 if (cmd.button() != mouse_button::button1) {
1478 // ignore motions deeper nested than the real anchor
1479 Cursor & bvcur = cur.bv().cursor();
1480 if (!bvcur.anchor_.hasPart(cur)) {
1484 CursorSlice old = bvcur.top();
1486 int const wh = bv->workHeight();
1487 int const y = max(0, min(wh - 1, cmd.y));
1489 tm->setCursorFromCoordinates(cur, cmd.x, y);
1490 cur.setTargetX(cmd.x);
1492 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1494 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1495 // This is to allow jumping over large insets
1496 if (cur.top() == old) {
1498 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1500 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1502 // We continue with our existing selection or start a new one, so don't
1503 // reset the anchor.
1504 bvcur.setCursor(cur);
1505 bvcur.setSelection(true);
1506 if (cur.top() == old) {
1507 // We didn't move one iota, so no need to update the screen.
1508 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1515 case LFUN_MOUSE_RELEASE:
1516 switch (cmd.button()) {
1517 case mouse_button::button1:
1518 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1519 // If there is a new selection, update persistent selection;
1520 // otherwise, single click does not clear persistent selection
1522 if (cur.selection()) {
1523 // Finish selection. If double click,
1524 // cur is moved to the end of word by
1525 // selectWord but bvcur is current
1527 cur.bv().cursor().setSelection();
1528 // We might have removed an empty but drawn selection
1529 // (probably a margin)
1530 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1533 // FIXME: We could try to handle drag and drop of selection here.
1536 case mouse_button::button2:
1537 // Middle mouse pasting is handled at mouse press time,
1538 // see LFUN_MOUSE_PRESS.
1542 case mouse_button::button3:
1543 // Cursor was set at LFUN_MOUSE_PRESS time.
1544 // FIXME: If there is a selection we could try to handle a special
1545 // drag & drop context menu.
1549 case mouse_button::none:
1550 case mouse_button::button4:
1551 case mouse_button::button5:
1553 } // switch (cmd.button())
1557 case LFUN_SELF_INSERT: {
1558 if (cmd.argument().empty())
1561 // Automatically delete the currently selected
1562 // text and replace it with what is being
1563 // typed in now. Depends on lyxrc settings
1564 // "auto_region_delete", which defaults to
1567 if (lyxrc.auto_region_delete && cur.selection())
1568 cutSelection(cur, false, false);
1570 cur.clearSelection();
1572 docstring::const_iterator cit = cmd.argument().begin();
1573 docstring::const_iterator const end = cmd.argument().end();
1574 for (; cit != end; ++cit)
1575 bv->translateAndInsert(*cit, this, cur);
1578 moveCursor(cur, false);
1579 bv->bookmarkEditPosition();
1583 case LFUN_HYPERLINK_INSERT: {
1584 InsetCommandParams p(HYPERLINK_CODE);
1586 if (cur.selection()) {
1587 content = cur.selectionAsString(false);
1588 cutSelection(cur, true, false);
1590 p["target"] = (cmd.argument().empty()) ?
1591 content : cmd.argument();
1592 string const data = InsetCommand::params2string("href", p);
1593 if (p["target"].empty()) {
1594 bv->showDialog("href", data);
1596 FuncRequest fr(LFUN_INSET_INSERT, data);
1602 case LFUN_LABEL_INSERT: {
1603 InsetCommandParams p(LABEL_CODE);
1604 // Try to generate a valid label
1605 p["name"] = (cmd.argument().empty()) ?
1606 cur.getPossibleLabel() :
1608 string const data = InsetCommand::params2string("label", p);
1610 if (cmd.argument().empty()) {
1611 bv->showDialog("label", data);
1613 FuncRequest fr(LFUN_INSET_INSERT, data);
1619 case LFUN_INFO_INSERT: {
1621 if (cmd.argument().empty() && cur.selection()) {
1622 // if command argument is empty use current selection as parameter.
1623 docstring ds = cur.selectionAsString(false);
1624 cutSelection(cur, true, false);
1625 FuncRequest cmd0(cmd, ds);
1626 inset = createInset(cur.bv().buffer(), cmd0);
1628 inset = createInset(cur.bv().buffer(), cmd);
1633 insertInset(cur, inset);
1637 case LFUN_CAPTION_INSERT:
1638 case LFUN_FOOTNOTE_INSERT:
1639 case LFUN_NOTE_INSERT:
1640 case LFUN_FLEX_INSERT:
1641 case LFUN_BOX_INSERT:
1642 case LFUN_BRANCH_INSERT:
1643 case LFUN_PHANTOM_INSERT:
1644 case LFUN_ERT_INSERT:
1645 case LFUN_LISTING_INSERT:
1646 case LFUN_MARGINALNOTE_INSERT:
1647 case LFUN_OPTIONAL_INSERT:
1648 case LFUN_INDEX_INSERT:
1649 // Open the inset, and move the current selection
1651 doInsertInset(cur, this, cmd, true, true);
1653 // Some insets are numbered, others are shown in the outline pane so
1654 // let's update the labels and the toc backend.
1655 bv->buffer().updateLabels();
1658 case LFUN_TABULAR_INSERT:
1659 // if there were no arguments, just open the dialog
1660 if (doInsertInset(cur, this, cmd, false, true))
1663 bv->showDialog("tabularcreate");
1667 case LFUN_FLOAT_INSERT:
1668 case LFUN_FLOAT_WIDE_INSERT:
1669 case LFUN_WRAP_INSERT: {
1670 // will some text be moved into the inset?
1671 bool content = cur.selection();
1673 doInsertInset(cur, this, cmd, true, true);
1676 // If some text is moved into the inset, doInsertInset
1677 // puts the cursor outside the inset. To insert the
1678 // caption we put it back into the inset.
1682 ParagraphList & pars = cur.text()->paragraphs();
1684 DocumentClass const & tclass = bv->buffer().params().documentClass();
1686 // add a separate paragraph for the caption inset
1687 pars.push_back(Paragraph());
1688 pars.back().setInsetOwner(&pars[0].inInset());
1689 pars.back().setPlainOrDefaultLayout(tclass);
1690 int cap_pit = pars.size() - 1;
1692 // if an empty inset was created, we create an additional empty
1693 // paragraph at the bottom so that the user can choose where to put
1694 // the graphics (or table).
1696 pars.push_back(Paragraph());
1697 pars.back().setInsetOwner(&pars[0].inInset());
1698 pars.back().setPlainOrDefaultLayout(tclass);
1701 // reposition the cursor to the caption
1702 cur.pit() = cap_pit;
1704 // FIXME: This Text/Cursor dispatch handling is a mess!
1705 // We cannot use Cursor::dispatch here it needs access to up to
1707 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1708 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1709 bv->buffer().updateLabels();
1710 cur.updateFlags(Update::Force);
1711 // FIXME: When leaving the Float (or Wrap) inset we should
1712 // delete any empty paragraph left above or below the
1717 case LFUN_NOMENCL_INSERT: {
1718 InsetCommandParams p(NOMENCL_CODE);
1719 if (cmd.argument().empty())
1720 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1722 p["symbol"] = cmd.argument();
1723 string const data = InsetCommand::params2string("nomenclature", p);
1724 bv->showDialog("nomenclature", data);
1728 case LFUN_INDEX_PRINT: {
1729 InsetCommandParams p(INDEX_PRINT_CODE);
1730 if (cmd.argument().empty())
1731 p["type"] = from_ascii("idx");
1733 p["type"] = cmd.argument();
1734 string const data = InsetCommand::params2string("index_print", p);
1735 FuncRequest fr(LFUN_INSET_INSERT, data);
1740 case LFUN_NOMENCL_PRINT:
1741 case LFUN_TOC_INSERT:
1742 case LFUN_LINE_INSERT:
1743 case LFUN_NEWPAGE_INSERT:
1745 doInsertInset(cur, this, cmd, false, false);
1749 case LFUN_DEPTH_DECREMENT:
1750 changeDepth(cur, DEC_DEPTH);
1753 case LFUN_DEPTH_INCREMENT:
1754 changeDepth(cur, INC_DEPTH);
1757 case LFUN_MATH_DISPLAY:
1758 mathDispatch(cur, cmd, true);
1761 case LFUN_REGEXP_MODE:
1762 regexpDispatch(cur, cmd);
1765 case LFUN_MATH_MODE:
1766 if (cmd.argument() == "on")
1767 // don't pass "on" as argument
1768 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1770 mathDispatch(cur, cmd, false);
1773 case LFUN_MATH_MACRO:
1774 if (cmd.argument().empty())
1775 cur.errorMessage(from_utf8(N_("Missing argument")));
1777 string s = to_utf8(cmd.argument());
1778 string const s1 = token(s, ' ', 1);
1779 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1780 string const s2 = token(s, ' ', 2);
1781 MacroType type = MacroTypeNewcommand;
1783 type = MacroTypeDef;
1784 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1785 inset->setBuffer(bv->buffer());
1786 insertInset(cur, inset);
1788 // enter macro inset and select the name
1790 cur.top().pos() = cur.top().lastpos();
1792 cur.setSelection(true);
1793 cur.top().pos() = 0;
1797 // passthrough hat and underscore outside mathed:
1798 case LFUN_MATH_SUBSCRIPT:
1799 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1801 case LFUN_MATH_SUPERSCRIPT:
1802 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1805 case LFUN_MATH_INSERT:
1806 case LFUN_MATH_AMS_MATRIX:
1807 case LFUN_MATH_MATRIX:
1808 case LFUN_MATH_DELIM:
1809 case LFUN_MATH_BIGDELIM: {
1811 cap::replaceSelection(cur);
1812 cur.insert(new InsetMathHull(hullSimple));
1813 checkAndActivateInset(cur, true);
1814 LASSERT(cur.inMathed(), /**/);
1819 case LFUN_FONT_EMPH: {
1820 Font font(ignore_font, ignore_language);
1821 font.fontInfo().setEmph(FONT_TOGGLE);
1822 toggleAndShow(cur, this, font);
1826 case LFUN_FONT_ITAL: {
1827 Font font(ignore_font, ignore_language);
1828 font.fontInfo().setShape(ITALIC_SHAPE);
1829 toggleAndShow(cur, this, font);
1833 case LFUN_FONT_BOLD:
1834 case LFUN_FONT_BOLDSYMBOL: {
1835 Font font(ignore_font, ignore_language);
1836 font.fontInfo().setSeries(BOLD_SERIES);
1837 toggleAndShow(cur, this, font);
1841 case LFUN_FONT_NOUN: {
1842 Font font(ignore_font, ignore_language);
1843 font.fontInfo().setNoun(FONT_TOGGLE);
1844 toggleAndShow(cur, this, font);
1848 case LFUN_FONT_TYPEWRITER: {
1849 Font font(ignore_font, ignore_language);
1850 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1851 toggleAndShow(cur, this, font);
1855 case LFUN_FONT_SANS: {
1856 Font font(ignore_font, ignore_language);
1857 font.fontInfo().setFamily(SANS_FAMILY);
1858 toggleAndShow(cur, this, font);
1862 case LFUN_FONT_ROMAN: {
1863 Font font(ignore_font, ignore_language);
1864 font.fontInfo().setFamily(ROMAN_FAMILY);
1865 toggleAndShow(cur, this, font);
1869 case LFUN_FONT_DEFAULT: {
1870 Font font(inherit_font, ignore_language);
1871 toggleAndShow(cur, this, font);
1875 case LFUN_FONT_STRIKEOUT: {
1876 Font font(ignore_font, ignore_language);
1877 font.fontInfo().setStrikeout(FONT_TOGGLE);
1878 toggleAndShow(cur, this, font);
1882 case LFUN_FONT_UULINE: {
1883 Font font(ignore_font, ignore_language);
1884 font.fontInfo().setUuline(FONT_TOGGLE);
1885 toggleAndShow(cur, this, font);
1889 case LFUN_FONT_UWAVE: {
1890 Font font(ignore_font, ignore_language);
1891 font.fontInfo().setUwave(FONT_TOGGLE);
1892 toggleAndShow(cur, this, font);
1896 case LFUN_FONT_UNDERLINE: {
1897 Font font(ignore_font, ignore_language);
1898 font.fontInfo().setUnderbar(FONT_TOGGLE);
1899 toggleAndShow(cur, this, font);
1903 case LFUN_FONT_SIZE: {
1904 Font font(ignore_font, ignore_language);
1905 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1906 toggleAndShow(cur, this, font);
1910 case LFUN_LANGUAGE: {
1911 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1914 Font font(ignore_font, lang);
1915 toggleAndShow(cur, this, font);
1919 case LFUN_TEXTSTYLE_APPLY:
1920 toggleAndShow(cur, this, freefont, toggleall);
1921 cur.message(_("Character set"));
1924 // Set the freefont using the contents of \param data dispatched from
1925 // the frontends and apply it at the current cursor location.
1926 case LFUN_TEXTSTYLE_UPDATE: {
1929 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1932 toggleAndShow(cur, this, freefont, toggleall);
1933 cur.message(_("Character set"));
1935 lyxerr << "Argument not ok";
1940 case LFUN_FINISHED_LEFT:
1941 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1942 // We're leaving an inset, going left. If the inset is LTR, we're
1943 // leaving from the front, so we should not move (remain at --- but
1944 // not in --- the inset). If the inset is RTL, move left, without
1945 // entering the inset itself; i.e., move to after the inset.
1946 if (cur.paragraph().getFontSettings(
1947 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1948 cursorVisLeft(cur, true);
1951 case LFUN_FINISHED_RIGHT:
1952 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1953 // We're leaving an inset, going right. If the inset is RTL, we're
1954 // leaving from the front, so we should not move (remain at --- but
1955 // not in --- the inset). If the inset is LTR, move right, without
1956 // entering the inset itself; i.e., move to after the inset.
1957 if (!cur.paragraph().getFontSettings(
1958 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1959 cursorVisRight(cur, true);
1962 case LFUN_FINISHED_BACKWARD:
1963 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1966 case LFUN_FINISHED_FORWARD:
1967 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1969 cur.setCurrentFont();
1972 case LFUN_LAYOUT_PARAGRAPH: {
1974 params2string(cur.paragraph(), data);
1975 data = "show\n" + data;
1976 bv->showDialog("paragraph", data);
1980 case LFUN_PARAGRAPH_UPDATE: {
1982 params2string(cur.paragraph(), data);
1984 // Will the paragraph accept changes from the dialog?
1986 cur.inset().allowParagraphCustomization(cur.idx());
1988 data = "update " + convert<string>(accept) + '\n' + data;
1989 bv->updateDialog("paragraph", data);
1993 case LFUN_ACCENT_UMLAUT:
1994 case LFUN_ACCENT_CIRCUMFLEX:
1995 case LFUN_ACCENT_GRAVE:
1996 case LFUN_ACCENT_ACUTE:
1997 case LFUN_ACCENT_TILDE:
1998 case LFUN_ACCENT_CEDILLA:
1999 case LFUN_ACCENT_MACRON:
2000 case LFUN_ACCENT_DOT:
2001 case LFUN_ACCENT_UNDERDOT:
2002 case LFUN_ACCENT_UNDERBAR:
2003 case LFUN_ACCENT_CARON:
2004 case LFUN_ACCENT_BREVE:
2005 case LFUN_ACCENT_TIE:
2006 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2007 case LFUN_ACCENT_CIRCLE:
2008 case LFUN_ACCENT_OGONEK:
2009 theLyXFunc().handleKeyFunc(cmd.action);
2010 if (!cmd.argument().empty())
2011 // FIXME: Are all these characters encoded in one byte in utf8?
2012 bv->translateAndInsert(cmd.argument()[0], this, cur);
2015 case LFUN_FLOAT_LIST_INSERT: {
2016 DocumentClass const & tclass = bv->buffer().params().documentClass();
2017 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2019 if (cur.selection())
2020 cutSelection(cur, true, false);
2021 breakParagraph(cur);
2023 if (cur.lastpos() != 0) {
2024 cursorBackward(cur);
2025 breakParagraph(cur);
2028 docstring const laystr = cur.inset().usePlainLayout() ?
2029 tclass.plainLayoutName() :
2030 tclass.defaultLayoutName();
2031 setLayout(cur, laystr);
2032 ParagraphParameters p;
2033 // FIXME If this call were replaced with one to clearParagraphParams(),
2034 // then we could get rid of this method altogether.
2035 setParagraphs(cur, p);
2036 // FIXME This should be simplified when InsetFloatList takes a
2037 // Buffer in its constructor.
2038 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
2039 ifl->setBuffer(bv->buffer());
2040 insertInset(cur, ifl);
2043 lyxerr << "Non-existent float type: "
2044 << to_utf8(cmd.argument()) << endl;
2049 case LFUN_CHANGE_ACCEPT: {
2050 acceptOrRejectChanges(cur, ACCEPT);
2054 case LFUN_CHANGE_REJECT: {
2055 acceptOrRejectChanges(cur, REJECT);
2059 case LFUN_THESAURUS_ENTRY: {
2060 docstring arg = cmd.argument();
2062 arg = cur.selectionAsString(false);
2064 if (arg.size() > 100 || arg.empty()) {
2065 // Get word or selection
2066 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2067 arg = cur.selectionAsString(false);
2068 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2071 bv->showDialog("thesaurus", to_utf8(arg));
2075 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2076 // Given data, an encoding of the ParagraphParameters
2077 // generated in the Paragraph dialog, this function sets
2078 // the current paragraph, or currently selected paragraphs,
2080 // NOTE: This function overrides all existing settings.
2081 setParagraphs(cur, cmd.argument());
2082 cur.message(_("Paragraph layout set"));
2086 case LFUN_PARAGRAPH_PARAMS: {
2087 // Given data, an encoding of the ParagraphParameters as we'd
2088 // find them in a LyX file, this function modifies the current paragraph,
2089 // or currently selected paragraphs.
2090 // NOTE: This function only modifies, and does not override, existing
2092 setParagraphs(cur, cmd.argument(), true);
2093 cur.message(_("Paragraph layout set"));
2098 if (cur.selection()) {
2099 cur.setSelection(false);
2102 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2103 // correct, but I'm not 100% sure -- dov, 071019
2104 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2108 case LFUN_OUTLINE_UP:
2109 outline(OutlineUp, cur);
2110 setCursor(cur, cur.pit(), 0);
2111 cur.buffer()->updateLabels();
2115 case LFUN_OUTLINE_DOWN:
2116 outline(OutlineDown, cur);
2117 setCursor(cur, cur.pit(), 0);
2118 cur.buffer()->updateLabels();
2122 case LFUN_OUTLINE_IN:
2123 outline(OutlineIn, cur);
2124 cur.buffer()->updateLabels();
2128 case LFUN_OUTLINE_OUT:
2129 outline(OutlineOut, cur);
2130 cur.buffer()->updateLabels();
2134 case LFUN_OUTLINE_DRAGMOVE: {
2135 int const move_id = convert<int>(cmd.getArg(0));
2136 int const move_to_id = convert<int>(cmd.getArg(1));
2137 dragMove(cur, move_id, move_to_id);
2138 setCursor(cur, cur.pit(), 0);
2139 cur.buffer()->updateLabels();
2145 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2150 if (lyxrc.spellcheck_continuously && cur.inTexted())
2151 // Take this opportunity to spellcheck current word.
2152 cur.paragraph().isMisspelled(cur.pos());
2154 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2156 // FIXME: The cursor flag is reset two lines below
2157 // so we need to check here if some of the LFUN did touch that.
2158 // for now only Text::erase() and Text::backspace() do that.
2159 // The plan is to verify all the LFUNs and then to remove this
2160 // singleParUpdate boolean altogether.
2161 if (cur.result().update() & Update::Force) {
2162 singleParUpdate = false;
2166 // FIXME: the following code should go in favor of fine grained
2167 // update flag treatment.
2168 if (singleParUpdate) {
2169 // Inserting characters does not change par height in general. So, try
2170 // to update _only_ this paragraph. BufferView will detect if a full
2171 // metrics update is needed anyway.
2172 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2177 && &oldTopSlice.inset() == &cur.inset()
2178 && oldTopSlice.idx() == cur.idx()
2179 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2180 && !cur.selection())
2181 // FIXME: it would be better if we could just do this
2183 //if (cur.result().update() != Update::FitCursor)
2186 // But some LFUNs do not set Update::FitCursor when needed, so we
2187 // do it for all. This is not very harmfull as FitCursor will provoke
2188 // a full redraw only if needed but still, a proper review of all LFUN
2189 // should be done and this needsUpdate boolean can then be removed.
2190 cur.updateFlags(Update::FitCursor);
2192 cur.updateFlags(Update::Force | Update::FitCursor);
2196 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2197 FuncStatus & flag) const
2199 LASSERT(cur.text() == this, /**/);
2201 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2203 InsetCode code = NO_CODE;
2205 switch (cmd.action) {
2207 case LFUN_DEPTH_DECREMENT:
2208 enable = changeDepthAllowed(cur, DEC_DEPTH);
2211 case LFUN_DEPTH_INCREMENT:
2212 enable = changeDepthAllowed(cur, INC_DEPTH);
2216 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2219 case LFUN_DIALOG_SHOW_NEW_INSET:
2220 if (cmd.argument() == "bibitem")
2221 code = BIBITEM_CODE;
2222 else if (cmd.argument() == "bibtex") {
2224 // not allowed in description items
2225 enable = !inDescriptionItem(cur);
2227 else if (cmd.argument() == "box")
2229 else if (cmd.argument() == "branch")
2231 else if (cmd.argument() == "citation")
2233 else if (cmd.argument() == "ert")
2235 else if (cmd.argument() == "external")
2236 code = EXTERNAL_CODE;
2237 else if (cmd.argument() == "float")
2239 else if (cmd.argument() == "graphics")
2240 code = GRAPHICS_CODE;
2241 else if (cmd.argument() == "href")
2242 code = HYPERLINK_CODE;
2243 else if (cmd.argument() == "include")
2244 code = INCLUDE_CODE;
2245 else if (cmd.argument() == "index")
2247 else if (cmd.argument() == "index_print")
2248 code = INDEX_PRINT_CODE;
2249 else if (cmd.argument() == "nomenclature")
2250 code = NOMENCL_CODE;
2251 else if (cmd.argument() == "nomencl_print")
2252 code = NOMENCL_PRINT_CODE;
2253 else if (cmd.argument() == "label")
2255 else if (cmd.argument() == "note")
2257 else if (cmd.argument() == "phantom")
2258 code = PHANTOM_CODE;
2259 else if (cmd.argument() == "ref")
2261 else if (cmd.argument() == "space")
2263 else if (cmd.argument() == "toc")
2265 else if (cmd.argument() == "vspace")
2267 else if (cmd.argument() == "wrap")
2269 else if (cmd.argument() == "listings")
2270 code = LISTINGS_CODE;
2273 case LFUN_ERT_INSERT:
2276 case LFUN_LISTING_INSERT:
2277 code = LISTINGS_CODE;
2278 // not allowed in description items
2279 enable = !inDescriptionItem(cur);
2281 case LFUN_FOOTNOTE_INSERT:
2284 case LFUN_TABULAR_INSERT:
2285 code = TABULAR_CODE;
2287 case LFUN_MARGINALNOTE_INSERT:
2290 case LFUN_FLOAT_INSERT:
2291 case LFUN_FLOAT_WIDE_INSERT:
2292 // FIXME: If there is a selection, we should check whether there
2293 // are floats in the selection, but this has performance issues, see
2294 // LFUN_CHANGE_ACCEPT/REJECT.
2296 if (inDescriptionItem(cur))
2297 // not allowed in description items
2300 InsetCode const inset_code = cur.inset().lyxCode();
2302 // algorithm floats cannot be put in another float
2303 if (to_utf8(cmd.argument()) == "algorithm") {
2304 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2308 // for figures and tables: only allow in another
2309 // float or wrap if it is of the same type and
2310 // not a subfloat already
2311 if(cur.inset().lyxCode() == code) {
2312 InsetFloat const & ins =
2313 static_cast<InsetFloat const &>(cur.inset());
2314 enable = ins.params().type == to_utf8(cmd.argument())
2315 && !ins.params().subfloat;
2316 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2317 InsetWrap const & ins =
2318 static_cast<InsetWrap const &>(cur.inset());
2319 enable = ins.params().type == to_utf8(cmd.argument());
2323 case LFUN_WRAP_INSERT:
2325 // not allowed in description items
2326 enable = !inDescriptionItem(cur);
2328 case LFUN_FLOAT_LIST_INSERT:
2329 code = FLOAT_LIST_CODE;
2330 // not allowed in description items
2331 enable = !inDescriptionItem(cur);
2333 case LFUN_CAPTION_INSERT:
2334 code = CAPTION_CODE;
2335 // not allowed in description items
2336 enable = !inDescriptionItem(cur);
2338 case LFUN_NOTE_INSERT:
2340 // in commands (sections etc.) and description items,
2341 // only Notes are allowed
2342 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2343 (!cur.paragraph().layout().isCommand()
2344 && !inDescriptionItem(cur)));
2346 case LFUN_FLEX_INSERT: {
2348 string s = cmd.getArg(0);
2350 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2351 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2352 il.lyxtype() != InsetLayout::CUSTOM &&
2353 il.lyxtype() != InsetLayout::ELEMENT &&
2354 il.lyxtype ()!= InsetLayout::STANDARD)
2358 case LFUN_BOX_INSERT:
2361 case LFUN_BRANCH_INSERT:
2363 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2364 && cur.buffer()->params().branchlist().empty())
2367 case LFUN_PHANTOM_INSERT:
2368 code = PHANTOM_CODE;
2370 case LFUN_LABEL_INSERT:
2373 case LFUN_LINE_INSERT:
2376 case LFUN_INFO_INSERT:
2379 case LFUN_OPTIONAL_INSERT:
2381 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2382 < cur.paragraph().layout().optionalargs;
2384 case LFUN_INDEX_INSERT:
2387 case LFUN_INDEX_PRINT:
2388 code = INDEX_PRINT_CODE;
2389 // not allowed in description items
2390 enable = !inDescriptionItem(cur);
2392 case LFUN_NOMENCL_INSERT:
2393 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2397 code = NOMENCL_CODE;
2399 case LFUN_NOMENCL_PRINT:
2400 code = NOMENCL_PRINT_CODE;
2401 // not allowed in description items
2402 enable = !inDescriptionItem(cur);
2404 case LFUN_TOC_INSERT:
2406 // not allowed in description items
2407 enable = !inDescriptionItem(cur);
2409 case LFUN_HYPERLINK_INSERT:
2410 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2414 code = HYPERLINK_CODE;
2416 case LFUN_QUOTE_INSERT:
2417 // always allow this, since we will inset a raw quote
2418 // if an inset is not allowed.
2420 case LFUN_SPECIALCHAR_INSERT:
2421 code = SPECIALCHAR_CODE;
2423 case LFUN_SPACE_INSERT:
2424 // slight hack: we know this is allowed in math mode
2429 case LFUN_MATH_INSERT:
2430 case LFUN_MATH_AMS_MATRIX:
2431 case LFUN_MATH_MATRIX:
2432 case LFUN_MATH_DELIM:
2433 case LFUN_MATH_BIGDELIM:
2434 case LFUN_MATH_DISPLAY:
2435 case LFUN_MATH_MODE:
2436 case LFUN_MATH_MACRO:
2437 case LFUN_MATH_SUBSCRIPT:
2438 case LFUN_MATH_SUPERSCRIPT:
2439 code = MATH_HULL_CODE;
2442 case LFUN_INSET_MODIFY:
2443 // We need to disable this, because we may get called for a
2445 // InsetTabular::getStatus() -> InsetText::getStatus()
2446 // and we don't handle LFUN_INSET_MODIFY.
2450 case LFUN_FONT_EMPH:
2451 flag.setOnOff(fontinfo.emph() == FONT_ON);
2454 case LFUN_FONT_ITAL:
2455 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2458 case LFUN_FONT_NOUN:
2459 flag.setOnOff(fontinfo.noun() == FONT_ON);
2462 case LFUN_FONT_BOLD:
2463 case LFUN_FONT_BOLDSYMBOL:
2464 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2467 case LFUN_FONT_SANS:
2468 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2471 case LFUN_FONT_ROMAN:
2472 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2475 case LFUN_FONT_TYPEWRITER:
2476 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2481 enable = cur.selection();
2485 if (cmd.argument().empty()) {
2486 if (theClipboard().isInternal())
2487 enable = cap::numberOfSelections() > 0;
2489 enable = !theClipboard().empty();
2493 // we have an argument
2494 string const arg = to_utf8(cmd.argument());
2495 if (isStrUnsignedInt(arg)) {
2496 // it's a number and therefore means the internal stack
2497 unsigned int n = convert<unsigned int>(arg);
2498 enable = cap::numberOfSelections() > n;
2502 // explicit graphics type?
2503 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2504 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2505 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2506 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2507 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2508 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2509 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2510 enable = theClipboard().hasGraphicsContents(type);
2519 case LFUN_CLIPBOARD_PASTE:
2520 enable = !theClipboard().empty();
2523 case LFUN_PRIMARY_SELECTION_PASTE:
2524 enable = cur.selection() || !theSelection().empty();
2527 case LFUN_SELECTION_PASTE:
2528 enable = cap::selection();
2531 case LFUN_PARAGRAPH_MOVE_UP:
2532 enable = cur.pit() > 0 && !cur.selection();
2535 case LFUN_PARAGRAPH_MOVE_DOWN:
2536 enable = cur.pit() < cur.lastpit() && !cur.selection();
2539 case LFUN_INSET_DISSOLVE:
2540 if (!cmd.argument().empty()) {
2541 InsetLayout const & il = cur.inset().getLayout();
2542 InsetLayout::InsetLyXType const type =
2543 translateLyXType(to_utf8(cmd.argument()));
2544 enable = cur.inset().lyxCode() == FLEX_CODE
2545 && il.lyxtype() == type;
2547 enable = ((!isMainText()
2548 && cur.inset().nargs() == 1)
2550 && cur.nextInset()->nargs() == 1));
2554 case LFUN_CHANGE_ACCEPT:
2555 case LFUN_CHANGE_REJECT:
2556 // In principle, these LFUNs should only be enabled if there
2557 // is a change at the current position/in the current selection.
2558 // However, without proper optimizations, this will inevitably
2559 // result in unacceptable performance - just imagine a user who
2560 // wants to select the complete content of a long document.
2561 if (!cur.selection())
2562 enable = cur.paragraph().isChanged(cur.pos());
2564 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2569 case LFUN_OUTLINE_UP:
2570 case LFUN_OUTLINE_DOWN:
2571 case LFUN_OUTLINE_IN:
2572 case LFUN_OUTLINE_OUT:
2573 case LFUN_OUTLINE_DRAGMOVE:
2574 // FIXME: LyX is not ready for outlining within inset.
2575 enable = isMainText()
2576 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2579 case LFUN_NEWLINE_INSERT:
2580 // LaTeX restrictions (labels or empty par)
2581 enable = (cur.pos() > cur.paragraph().beginOfBody());
2584 case LFUN_TAB_INSERT:
2585 case LFUN_TAB_DELETE:
2586 enable = cur.inset().getLayout().isPassThru();
2589 case LFUN_SET_GRAPHICS_GROUP: {
2590 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2594 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2598 case LFUN_NEWPAGE_INSERT:
2599 // not allowed in description items
2600 code = NEWPAGE_CODE;
2601 enable = !inDescriptionItem(cur);
2604 case LFUN_DATE_INSERT: {
2605 string const format = cmd.argument().empty()
2606 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2607 enable = support::os::is_valid_strftime(format);
2612 enable = !cur.inset().getLayout().isPassThru();
2614 case LFUN_BREAK_PARAGRAPH:
2615 enable = cur.inset().getLayout().isMultiPar();
2617 case LFUN_WORD_DELETE_FORWARD:
2618 case LFUN_WORD_DELETE_BACKWARD:
2619 case LFUN_LINE_DELETE:
2620 case LFUN_WORD_FORWARD:
2621 case LFUN_WORD_BACKWARD:
2622 case LFUN_WORD_RIGHT:
2623 case LFUN_WORD_LEFT:
2624 case LFUN_CHAR_FORWARD:
2625 case LFUN_CHAR_FORWARD_SELECT:
2626 case LFUN_CHAR_BACKWARD:
2627 case LFUN_CHAR_BACKWARD_SELECT:
2628 case LFUN_CHAR_LEFT:
2629 case LFUN_CHAR_LEFT_SELECT:
2630 case LFUN_CHAR_RIGHT:
2631 case LFUN_CHAR_RIGHT_SELECT:
2633 case LFUN_UP_SELECT:
2635 case LFUN_DOWN_SELECT:
2636 case LFUN_PARAGRAPH_UP_SELECT:
2637 case LFUN_PARAGRAPH_DOWN_SELECT:
2638 case LFUN_LINE_BEGIN_SELECT:
2639 case LFUN_LINE_END_SELECT:
2640 case LFUN_WORD_FORWARD_SELECT:
2641 case LFUN_WORD_BACKWARD_SELECT:
2642 case LFUN_WORD_RIGHT_SELECT:
2643 case LFUN_WORD_LEFT_SELECT:
2644 case LFUN_WORD_SELECT:
2645 case LFUN_SECTION_SELECT:
2646 case LFUN_PARAGRAPH_UP:
2647 case LFUN_PARAGRAPH_DOWN:
2648 case LFUN_LINE_BEGIN:
2650 case LFUN_CHAR_DELETE_FORWARD:
2651 case LFUN_CHAR_DELETE_BACKWARD:
2652 case LFUN_INSET_INSERT:
2653 case LFUN_WORD_UPCASE:
2654 case LFUN_WORD_LOWCASE:
2655 case LFUN_WORD_CAPITALIZE:
2656 case LFUN_CHARS_TRANSPOSE:
2657 case LFUN_SERVER_GET_XY:
2658 case LFUN_SERVER_SET_XY:
2659 case LFUN_SERVER_GET_LAYOUT:
2661 case LFUN_SELF_INSERT:
2662 case LFUN_FONT_DEFAULT:
2663 case LFUN_FONT_UNDERLINE:
2664 case LFUN_FONT_STRIKEOUT:
2665 case LFUN_FONT_UULINE:
2666 case LFUN_FONT_UWAVE:
2667 case LFUN_FONT_SIZE:
2668 case LFUN_TEXTSTYLE_APPLY:
2669 case LFUN_TEXTSTYLE_UPDATE:
2670 case LFUN_LAYOUT_PARAGRAPH:
2671 case LFUN_PARAGRAPH_UPDATE:
2672 case LFUN_ACCENT_UMLAUT:
2673 case LFUN_ACCENT_CIRCUMFLEX:
2674 case LFUN_ACCENT_GRAVE:
2675 case LFUN_ACCENT_ACUTE:
2676 case LFUN_ACCENT_TILDE:
2677 case LFUN_ACCENT_CEDILLA:
2678 case LFUN_ACCENT_MACRON:
2679 case LFUN_ACCENT_DOT:
2680 case LFUN_ACCENT_UNDERDOT:
2681 case LFUN_ACCENT_UNDERBAR:
2682 case LFUN_ACCENT_CARON:
2683 case LFUN_ACCENT_BREVE:
2684 case LFUN_ACCENT_TIE:
2685 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2686 case LFUN_ACCENT_CIRCLE:
2687 case LFUN_ACCENT_OGONEK:
2688 case LFUN_THESAURUS_ENTRY:
2689 case LFUN_PARAGRAPH_PARAMS_APPLY:
2690 case LFUN_PARAGRAPH_PARAMS:
2692 case LFUN_BUFFER_BEGIN:
2693 case LFUN_BUFFER_END:
2694 case LFUN_BUFFER_BEGIN_SELECT:
2695 case LFUN_BUFFER_END_SELECT:
2696 case LFUN_INSET_BEGIN:
2697 case LFUN_INSET_END:
2698 case LFUN_INSET_BEGIN_SELECT:
2699 case LFUN_INSET_END_SELECT:
2700 case LFUN_UNICODE_INSERT:
2701 // these are handled in our dispatch()
2711 || !cur.inset().insetAllowed(code)
2712 || cur.paragraph().layout().pass_thru))
2715 switch (cmd.action) {
2716 case LFUN_ACCENT_ACUTE:
2717 case LFUN_ACCENT_BREVE:
2718 case LFUN_ACCENT_CARON:
2719 case LFUN_ACCENT_CEDILLA:
2720 case LFUN_ACCENT_CIRCLE:
2721 case LFUN_ACCENT_CIRCUMFLEX:
2722 case LFUN_ACCENT_DOT:
2723 case LFUN_ACCENT_GRAVE:
2724 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2725 case LFUN_ACCENT_MACRON:
2726 case LFUN_ACCENT_OGONEK:
2727 case LFUN_ACCENT_TIE:
2728 case LFUN_ACCENT_TILDE:
2729 case LFUN_ACCENT_UMLAUT:
2730 case LFUN_ACCENT_UNDERBAR:
2731 case LFUN_ACCENT_UNDERDOT:
2733 case LFUN_DEPTH_DECREMENT:
2734 case LFUN_DEPTH_INCREMENT:
2735 case LFUN_FILE_INSERT:
2736 case LFUN_FONT_BOLD:
2737 case LFUN_FONT_BOLDSYMBOL:
2738 case LFUN_FONT_TYPEWRITER:
2739 case LFUN_FONT_DEFAULT:
2740 case LFUN_FONT_EMPH:
2741 case LFUN_FONT_NOUN:
2742 case LFUN_FONT_ROMAN:
2743 case LFUN_FONT_SANS:
2744 case LFUN_FONT_FRAK:
2745 case LFUN_FONT_ITAL:
2746 case LFUN_FONT_SIZE:
2747 case LFUN_FONT_STATE:
2748 case LFUN_FONT_UNDERLINE:
2749 case LFUN_FONT_STRIKEOUT:
2750 case LFUN_FONT_UULINE:
2751 case LFUN_FONT_UWAVE:
2752 case LFUN_LABEL_GOTO:
2753 case LFUN_LAYOUT_TABULAR:
2754 case LFUN_MENU_OPEN:
2756 case LFUN_NOTE_NEXT:
2757 case LFUN_REFERENCE_NEXT:
2758 case LFUN_SERVER_GOTO_FILE_ROW:
2759 case LFUN_SERVER_NOTIFY:
2760 case LFUN_SERVER_SET_XY:
2761 case LFUN_TEXTSTYLE_APPLY:
2762 case LFUN_TEXTSTYLE_UPDATE:
2763 if (cur.inset().getLayout().isPassThru())
2770 flag.setEnabled(enable);
2775 void Text::pasteString(Cursor & cur, docstring const & clip,
2778 cur.clearSelection();
2779 if (!clip.empty()) {
2782 insertStringAsParagraphs(cur, clip, cur.current_font);
2784 insertStringAsLines(cur, clip, cur.current_font);
2789 // FIXME: an item inset would make things much easier.
2790 bool Text::inDescriptionItem(Cursor & cur) const
2792 Paragraph & par = cur.paragraph();
2793 pos_type const pos = cur.pos();
2794 pos_type const body_pos = par.beginOfBody();
2796 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2797 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2798 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2801 return (pos < body_pos
2803 && (pos == 0 || par.getChar(pos - 1) != ' ')));