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);
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(Buffer const & buffer, Paragraph const & par) const
512 return par.isRTL(buffer.params());
516 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
518 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
520 // Dispatch if the cursor is inside the text. It is not the
521 // case for context menus (bug 5797).
522 if (cur.text() != this) {
527 BufferView * bv = &cur.bv();
528 TextMetrics * tm = &bv->textMetrics(this);
529 if (!tm->contains(cur.pit())) {
530 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
531 tm = &bv->textMetrics(this);
534 // FIXME: We use the update flag to indicates wether a singlePar or a
535 // full screen update is needed. We reset it here but shall we restore it
539 LASSERT(cur.text() == this, /**/);
540 CursorSlice oldTopSlice = cur.top();
541 bool oldBoundary = cur.boundary();
542 bool sel = cur.selection();
543 // Signals that, even if needsUpdate == false, an update of the
544 // cursor paragraph is required
545 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
546 LyXAction::SingleParUpdate);
547 // Signals that a full-screen update is required
548 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
549 LyXAction::NoUpdate) || singleParUpdate);
551 switch (cmd.action) {
553 case LFUN_PARAGRAPH_MOVE_DOWN: {
554 pit_type const pit = cur.pit();
555 recUndo(cur, pit, pit + 1);
557 swap(pars_[pit], pars_[pit + 1]);
558 cur.buffer()->updateLabels();
564 case LFUN_PARAGRAPH_MOVE_UP: {
565 pit_type const pit = cur.pit();
566 recUndo(cur, pit - 1, pit);
568 swap(pars_[pit], pars_[pit - 1]);
569 cur.buffer()->updateLabels();
575 case LFUN_APPENDIX: {
576 Paragraph & par = cur.paragraph();
577 bool start = !par.params().startOfAppendix();
579 // FIXME: The code below only makes sense at top level.
580 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
581 // ensure that we have only one start_of_appendix in this document
582 // FIXME: this don't work for multipart document!
583 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
584 if (pars_[tmp].params().startOfAppendix()) {
586 pars_[tmp].params().startOfAppendix(false);
592 par.params().startOfAppendix(start);
594 // we can set the refreshing parameters now
595 cur.buffer()->updateLabels();
599 case LFUN_WORD_DELETE_FORWARD:
601 cutSelection(cur, true, false);
603 deleteWordForward(cur);
604 finishChange(cur, false);
607 case LFUN_WORD_DELETE_BACKWARD:
609 cutSelection(cur, true, false);
611 deleteWordBackward(cur);
612 finishChange(cur, false);
615 case LFUN_LINE_DELETE:
617 cutSelection(cur, true, false);
619 tm->deleteLineForward(cur);
620 finishChange(cur, false);
623 case LFUN_BUFFER_BEGIN:
624 case LFUN_BUFFER_BEGIN_SELECT:
625 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
626 if (cur.depth() == 1)
627 needsUpdate |= cursorTop(cur);
630 cur.updateFlags(Update::FitCursor);
633 case LFUN_BUFFER_END:
634 case LFUN_BUFFER_END_SELECT:
635 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
636 if (cur.depth() == 1)
637 needsUpdate |= cursorBottom(cur);
640 cur.updateFlags(Update::FitCursor);
643 case LFUN_INSET_BEGIN:
644 case LFUN_INSET_BEGIN_SELECT:
645 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
646 if (cur.depth() == 1 || cur.pos() > 0)
647 needsUpdate |= cursorTop(cur);
650 cur.updateFlags(Update::FitCursor);
654 case LFUN_INSET_END_SELECT:
655 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
656 if (cur.depth() == 1 || cur.pos() < cur.lastpos())
657 needsUpdate |= cursorBottom(cur);
660 cur.updateFlags(Update::FitCursor);
663 case LFUN_CHAR_FORWARD:
664 case LFUN_CHAR_FORWARD_SELECT:
665 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
666 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
667 needsUpdate |= cursorForward(cur);
669 if (!needsUpdate && oldTopSlice == cur.top()
670 && cur.boundary() == oldBoundary) {
672 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
674 // we will probably be moving out the inset, so we should execute
675 // the depm-mechanism, but only when the cursor has a place to
676 // go outside this inset, i.e. in a slice above.
677 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
678 && cur.pit() == cur.lastpit()) {
679 // The cursor hasn't changed yet. To give the
680 // DEPM the possibility of doing something we must
681 // provide it with two different cursors.
683 dummy.pos() = dummy.pit() = 0;
684 cur.bv().checkDepm(dummy, cur);
689 case LFUN_CHAR_BACKWARD:
690 case LFUN_CHAR_BACKWARD_SELECT:
691 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
692 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
693 needsUpdate |= cursorBackward(cur);
695 if (!needsUpdate && oldTopSlice == cur.top()
696 && cur.boundary() == oldBoundary) {
698 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
700 // we will probably be moving out the inset, so we should execute
701 // the depm-mechanism, but only when the cursor has a place to
702 // go outside this inset, i.e. in a slice above.
703 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
704 // The cursor hasn't changed yet. To give the
705 // DEPM the possibility of doing something we must
706 // provide it with two different cursors.
708 dummy.pos() = cur.lastpos();
709 dummy.pit() = cur.lastpit();
710 cur.bv().checkDepm(dummy, cur);
716 case LFUN_CHAR_LEFT_SELECT:
717 if (lyxrc.visual_cursor) {
718 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
719 needsUpdate |= cursorVisLeft(cur);
720 if (!needsUpdate && oldTopSlice == cur.top()
721 && cur.boundary() == oldBoundary) {
723 cmd = FuncRequest(LFUN_FINISHED_LEFT);
726 if (reverseDirectionNeeded(cur)) {
727 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
728 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
730 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
731 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
738 case LFUN_CHAR_RIGHT:
739 case LFUN_CHAR_RIGHT_SELECT:
740 if (lyxrc.visual_cursor) {
741 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
742 needsUpdate |= cursorVisRight(cur);
743 if (!needsUpdate && oldTopSlice == cur.top()
744 && cur.boundary() == oldBoundary) {
746 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
749 if (reverseDirectionNeeded(cur)) {
750 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
751 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
753 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
754 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
763 case LFUN_DOWN_SELECT:
766 // stop/start the selection
767 bool select = cmd.action == LFUN_DOWN_SELECT ||
768 cmd.action == LFUN_UP_SELECT;
770 // move cursor up/down
771 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
772 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
774 if (!atFirstOrLastRow) {
775 needsUpdate |= cur.selHandle(select);
776 cur.selHandle(select);
777 cur.upDownInText(up, needsUpdate);
778 needsUpdate |= cur.beforeDispatchCursor().inMathed();
780 // if the cursor cannot be moved up or down do not remove
781 // the selection right now, but wait for the next dispatch.
783 needsUpdate |= cur.selHandle(select);
784 cur.upDownInText(up, needsUpdate);
791 case LFUN_PARAGRAPH_UP:
792 case LFUN_PARAGRAPH_UP_SELECT:
793 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
794 needsUpdate |= cursorUpParagraph(cur);
797 case LFUN_PARAGRAPH_DOWN:
798 case LFUN_PARAGRAPH_DOWN_SELECT:
799 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
800 needsUpdate |= cursorDownParagraph(cur);
803 case LFUN_LINE_BEGIN:
804 case LFUN_LINE_BEGIN_SELECT:
805 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
806 needsUpdate |= tm->cursorHome(cur);
810 case LFUN_LINE_END_SELECT:
811 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
812 needsUpdate |= tm->cursorEnd(cur);
815 case LFUN_SECTION_SELECT: {
816 Buffer const & buf = *cur.buffer();
817 pit_type const pit = cur.pit();
818 ParagraphList & pars = buf.text().paragraphs();
819 ParagraphList::iterator bgn = pars.begin();
820 // The first paragraph of the area to be selected:
821 ParagraphList::iterator start = boost::next(bgn, pit);
822 // The final paragraph of area to be selected:
823 ParagraphList::iterator finish = start;
824 ParagraphList::iterator end = pars.end();
826 int const thistoclevel = start->layout().toclevel;
827 if (thistoclevel == Layout::NOT_IN_TOC)
831 Cursor const old_cur = cur;
832 needsUpdate |= cur.selHandle(true);
834 // Move out (down) from this section header
838 // Seek the one (on same level) below
839 for (; finish != end; ++finish, ++cur.pit()) {
840 int const toclevel = finish->layout().toclevel;
841 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
844 cur.pos() = cur.lastpos();
846 needsUpdate |= cur != old_cur;
850 case LFUN_WORD_RIGHT:
851 case LFUN_WORD_RIGHT_SELECT:
852 if (lyxrc.visual_cursor) {
853 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
854 needsUpdate |= cursorVisRightOneWord(cur);
855 if (!needsUpdate && oldTopSlice == cur.top()
856 && cur.boundary() == oldBoundary) {
858 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
861 if (reverseDirectionNeeded(cur)) {
862 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
863 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
865 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
866 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
873 case LFUN_WORD_FORWARD:
874 case LFUN_WORD_FORWARD_SELECT:
875 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
876 needsUpdate |= cursorForwardOneWord(cur);
880 case LFUN_WORD_LEFT_SELECT:
881 if (lyxrc.visual_cursor) {
882 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
883 needsUpdate |= cursorVisLeftOneWord(cur);
884 if (!needsUpdate && oldTopSlice == cur.top()
885 && cur.boundary() == oldBoundary) {
887 cmd = FuncRequest(LFUN_FINISHED_LEFT);
890 if (reverseDirectionNeeded(cur)) {
891 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
892 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
894 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
895 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
902 case LFUN_WORD_BACKWARD:
903 case LFUN_WORD_BACKWARD_SELECT:
904 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
905 needsUpdate |= cursorBackwardOneWord(cur);
908 case LFUN_WORD_SELECT: {
909 selectWord(cur, WHOLE_WORD);
910 finishChange(cur, true);
914 case LFUN_NEWLINE_INSERT: {
915 InsetNewlineParams inp;
916 docstring arg = cmd.argument();
917 // this avoids a double undo
918 // FIXME: should not be needed, ideally
919 if (!cur.selection())
921 cap::replaceSelection(cur);
922 if (arg == "linebreak")
923 inp.kind = InsetNewlineParams::LINEBREAK;
925 inp.kind = InsetNewlineParams::NEWLINE;
926 cur.insert(new InsetNewline(inp));
928 moveCursor(cur, false);
932 case LFUN_TAB_INSERT: {
933 bool const multi_par_selection = cur.selection() &&
934 cur.selBegin().pit() != cur.selEnd().pit();
935 if (multi_par_selection) {
936 // If there is a multi-paragraph selection, a tab is inserted
937 // at the beginning of each paragraph.
938 cur.recordUndoSelection();
939 pit_type const pit_end = cur.selEnd().pit();
940 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
941 pars_[pit].insertChar(0, '\t',
942 bv->buffer().params().trackChanges);
943 // Update the selection pos to make sure the selection does not
944 // change as the inserted tab will increase the logical pos.
945 if (cur.anchor_.pit() == pit)
946 cur.anchor_.forwardPos();
947 if (cur.pit() == pit)
952 // Maybe we shouldn't allow tabs within a line, because they
953 // are not (yet) aligned as one might do expect.
954 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
960 case LFUN_TAB_DELETE: {
961 bool const tc = bv->buffer().params().trackChanges;
962 if (cur.selection()) {
963 // If there is a selection, a tab (if present) is removed from
964 // the beginning of each paragraph.
965 cur.recordUndoSelection();
966 pit_type const pit_end = cur.selEnd().pit();
967 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
968 Paragraph & par = paragraphs()[pit];
969 if (par.getChar(0) == '\t') {
970 if (cur.pit() == pit)
972 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
973 cur.anchor_.backwardPos();
975 par.eraseChar(0, tc);
977 // If no tab was present, try to remove up to four spaces.
978 for (int n_spaces = 0;
979 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
980 if (cur.pit() == pit)
982 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
983 cur.anchor_.backwardPos();
985 par.eraseChar(0, tc);
990 // If there is no selection, try to remove a tab or some spaces
991 // before the position of the cursor.
992 Paragraph & par = paragraphs()[cur.pit()];
993 pos_type const pos = cur.pos();
998 char_type const c = par.getChar(pos - 1);
1002 par.eraseChar(cur.pos(), tc);
1004 for (int n_spaces = 0;
1006 && par.getChar(cur.pos() - 1) == ' '
1010 par.eraseChar(cur.pos(), tc);
1017 case LFUN_CHAR_DELETE_FORWARD:
1018 if (!cur.selection()) {
1019 if (cur.pos() == cur.paragraph().size())
1020 // Par boundary, force full-screen update
1021 singleParUpdate = false;
1022 needsUpdate |= erase(cur);
1024 // It is possible to make it a lot faster still
1025 // just comment out the line below...
1027 cutSelection(cur, true, false);
1028 singleParUpdate = false;
1030 moveCursor(cur, false);
1033 case LFUN_CHAR_DELETE_BACKWARD:
1034 if (!cur.selection()) {
1035 if (bv->getIntl().getTransManager().backspace()) {
1036 // Par boundary, full-screen update
1038 singleParUpdate = false;
1039 needsUpdate |= backspace(cur);
1041 // It is possible to make it a lot faster still
1042 // just comment out the line below...
1045 cutSelection(cur, true, false);
1046 singleParUpdate = false;
1050 case LFUN_BREAK_PARAGRAPH:
1051 cap::replaceSelection(cur);
1052 breakParagraph(cur, cmd.argument() == "inverse");
1056 case LFUN_INSET_INSERT: {
1059 // We have to avoid triggering InstantPreview loading
1060 // before inserting into the document. See bug #5626.
1061 bool loaded = bv->buffer().isFullyLoaded();
1062 bv->buffer().setFullyLoaded(false);
1063 Inset * inset = createInset(bv->buffer(), cmd);
1064 bv->buffer().setFullyLoaded(loaded);
1067 // FIXME (Abdel 01/02/2006):
1068 // What follows would be a partial fix for bug 2154:
1069 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
1070 // This automatically put the label inset _after_ a
1071 // numbered section. It should be possible to extend the mechanism
1072 // to any kind of LateX environement.
1073 // The correct way to fix that bug would be at LateX generation.
1074 // I'll let the code here for reference as it could be used for some
1075 // other feature like "automatic labelling".
1077 Paragraph & par = pars_[cur.pit()];
1078 if (inset->lyxCode() == LABEL_CODE
1079 && par.layout().labeltype == LABEL_COUNTER) {
1080 // Go to the end of the paragraph
1081 // Warning: Because of Change-Tracking, the last
1082 // position is 'size()' and not 'size()-1':
1083 cur.pos() = par.size();
1084 // Insert a new paragraph
1085 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1089 if (cur.selection())
1090 cutSelection(cur, true, false);
1094 // trigger InstantPreview now
1095 if (inset->lyxCode() == EXTERNAL_CODE) {
1096 InsetExternal & ins =
1097 static_cast<InsetExternal &>(*inset);
1098 ins.updatePreview();
1105 case LFUN_INSET_DISSOLVE: {
1106 // first, try if there's an inset at cursor
1107 // FIXME: this first part should be moved to
1108 // a LFUN_NEXT_INSET_DISSOLVE, or be called via
1109 // some generic "next-inset inset-dissolve"
1110 Inset * inset = cur.nextInset();
1111 if (inset && inset->isActive()) {
1112 Cursor tmpcur = cur;
1113 tmpcur.pushBackward(*inset);
1114 inset->dispatch(tmpcur, cmd);
1115 if (tmpcur.result().dispatched()) {
1120 // if it did not work, try the underlying inset
1121 if (dissolveInset(cur)) {
1125 // if it did not work, do nothing.
1129 case LFUN_SET_GRAPHICS_GROUP: {
1130 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1136 string id = to_utf8(cmd.argument());
1137 string grp = graphics::getGroupParams(bv->buffer(), id);
1138 InsetGraphicsParams tmp, inspar = ins->getParams();
1141 inspar.groupId = to_utf8(cmd.argument());
1143 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1144 tmp.filename = inspar.filename;
1148 ins->setParams(inspar);
1151 case LFUN_SPACE_INSERT:
1152 if (cur.paragraph().layout().free_spacing)
1153 insertChar(cur, ' ');
1155 doInsertInset(cur, this, cmd, false, false);
1158 moveCursor(cur, false);
1161 case LFUN_SPECIALCHAR_INSERT: {
1162 string const name = to_utf8(cmd.argument());
1163 if (name == "hyphenation")
1164 specialChar(cur, InsetSpecialChar::HYPHENATION);
1165 else if (name == "ligature-break")
1166 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1167 else if (name == "slash")
1168 specialChar(cur, InsetSpecialChar::SLASH);
1169 else if (name == "nobreakdash")
1170 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1171 else if (name == "dots")
1172 specialChar(cur, InsetSpecialChar::LDOTS);
1173 else if (name == "end-of-sentence")
1174 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1175 else if (name == "menu-separator")
1176 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1177 else if (name.empty())
1178 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1180 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1184 case LFUN_WORD_UPCASE:
1185 changeCase(cur, text_uppercase);
1188 case LFUN_WORD_LOWCASE:
1189 changeCase(cur, text_lowercase);
1192 case LFUN_WORD_CAPITALIZE:
1193 changeCase(cur, text_capitalization);
1196 case LFUN_CHARS_TRANSPOSE:
1197 charsTranspose(cur);
1201 cur.message(_("Paste"));
1202 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1203 cap::replaceSelection(cur);
1205 // without argument?
1206 string const arg = to_utf8(cmd.argument());
1208 if (theClipboard().isInternal())
1209 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1210 else if (theClipboard().hasGraphicsContents()
1211 && !theClipboard().hasTextContents())
1212 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1214 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1215 } else if (isStrUnsignedInt(arg)) {
1216 // we have a numerical argument
1217 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1218 convert<unsigned int>(arg));
1220 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1222 type = Clipboard::PdfGraphicsType;
1223 else if (arg == "png")
1224 type = Clipboard::PngGraphicsType;
1225 else if (arg == "jpeg")
1226 type = Clipboard::JpegGraphicsType;
1227 else if (arg == "linkback")
1228 type = Clipboard::LinkBackGraphicsType;
1229 else if (arg == "emf")
1230 type = Clipboard::EmfGraphicsType;
1231 else if (arg == "wmf")
1232 type = Clipboard::WmfGraphicsType;
1235 LASSERT(false, /**/);
1237 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1240 bv->buffer().errors("Paste");
1241 cur.clearSelection(); // bug 393
1247 cutSelection(cur, true, true);
1248 cur.message(_("Cut"));
1253 cur.message(_("Copy"));
1256 case LFUN_SERVER_GET_XY:
1257 cur.message(from_utf8(
1258 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1259 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1262 case LFUN_SERVER_SET_XY: {
1265 istringstream is(to_utf8(cmd.argument()));
1268 lyxerr << "SETXY: Could not parse coordinates in '"
1269 << to_utf8(cmd.argument()) << endl;
1271 tm->setCursorFromCoordinates(cur, x, y);
1275 case LFUN_SERVER_GET_LAYOUT:
1276 cur.message(cur.paragraph().layout().name());
1280 docstring layout = cmd.argument();
1281 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1283 Paragraph const & para = cur.paragraph();
1284 docstring const old_layout = para.layout().name();
1285 DocumentClass const & tclass = bv->buffer().params().documentClass();
1288 layout = tclass.defaultLayoutName();
1290 if (para.forcePlainLayout())
1291 // in this case only the empty layout is allowed
1292 layout = tclass.plainLayoutName();
1293 else if (para.usePlainLayout()) {
1294 // in this case, default layout maps to empty layout
1295 if (layout == tclass.defaultLayoutName())
1296 layout = tclass.plainLayoutName();
1298 // otherwise, the empty layout maps to the default
1299 if (layout == tclass.plainLayoutName())
1300 layout = tclass.defaultLayoutName();
1303 bool hasLayout = tclass.hasLayout(layout);
1305 // If the entry is obsolete, use the new one instead.
1307 docstring const & obs = tclass[layout].obsoleted_by();
1313 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1314 from_utf8(N_(" not known")));
1318 bool change_layout = (old_layout != layout);
1320 if (!change_layout && cur.selection() &&
1321 cur.selBegin().pit() != cur.selEnd().pit())
1323 pit_type spit = cur.selBegin().pit();
1324 pit_type epit = cur.selEnd().pit() + 1;
1325 while (spit != epit) {
1326 if (pars_[spit].layout().name() != old_layout) {
1327 change_layout = true;
1335 setLayout(cur, layout);
1340 case LFUN_CLIPBOARD_PASTE:
1341 cur.clearSelection();
1342 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1343 cmd.argument() == "paragraph");
1344 bv->buffer().errors("Paste");
1347 case LFUN_PRIMARY_SELECTION_PASTE:
1348 pasteString(cur, theSelection().get(),
1349 cmd.argument() == "paragraph");
1352 case LFUN_SELECTION_PASTE:
1353 // Copy the selection buffer to the clipboard stack,
1354 // because we want it to appear in the "Edit->Paste
1356 cap::copySelectionToStack();
1357 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1358 bv->buffer().errors("Paste");
1361 case LFUN_UNICODE_INSERT: {
1362 if (cmd.argument().empty())
1364 docstring hexstring = cmd.argument();
1365 if (isHex(hexstring)) {
1366 char_type c = hexToInt(hexstring);
1367 if (c >= 32 && c < 0x10ffff) {
1368 lyxerr << "Inserting c: " << c << endl;
1369 docstring s = docstring(1, c);
1370 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1376 case LFUN_QUOTE_INSERT: {
1377 Paragraph & par = cur.paragraph();
1378 pos_type pos = cur.pos();
1379 BufferParams const & bufparams = bv->buffer().params();
1380 Layout const & style = par.layout();
1381 InsetLayout const & ilayout = cur.inset().getLayout();
1382 if (!style.pass_thru && !ilayout.isPassThru()
1383 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1384 // this avoids a double undo
1385 // FIXME: should not be needed, ideally
1386 if (!cur.selection())
1388 cap::replaceSelection(cur);
1393 else if (cur.prevInset() && cur.prevInset()->isSpace())
1396 c = par.getChar(pos - 1);
1397 string arg = to_utf8(cmd.argument());
1398 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1399 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1403 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1407 case LFUN_DATE_INSERT: {
1408 string const format = cmd.argument().empty()
1409 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1410 string const time = formatted_time(current_time(), format);
1411 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1415 case LFUN_MOUSE_TRIPLE:
1416 if (cmd.button() == mouse_button::button1) {
1417 tm->cursorHome(cur);
1425 case LFUN_MOUSE_DOUBLE:
1426 if (cmd.button() == mouse_button::button1) {
1427 selectWord(cur, WHOLE_WORD_STRICT);
1432 // Single-click on work area
1433 case LFUN_MOUSE_PRESS:
1434 // We are not marking a selection with the keyboard in any case.
1435 cur.bv().cursor().setMark(false);
1436 switch (cmd.button()) {
1437 case mouse_button::button1:
1439 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1440 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1443 case mouse_button::button2:
1444 // Middle mouse pasting.
1445 bv->mouseSetCursor(cur);
1447 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1448 "selection-paste ; primary-selection-paste paragraph"));
1452 case mouse_button::button3: {
1453 Cursor const & bvcur = cur.bv().cursor();
1454 // Don't do anything if we right-click a
1455 // selection, a context menu will popup.
1456 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1457 && cur < bvcur.selectionEnd()) {
1461 if (!bv->mouseSetCursor(cur, false))
1462 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1468 } // switch (cmd.button())
1471 case LFUN_MOUSE_MOTION: {
1472 // Mouse motion with right or middle mouse do nothing for now.
1473 if (cmd.button() != mouse_button::button1) {
1477 // ignore motions deeper nested than the real anchor
1478 Cursor & bvcur = cur.bv().cursor();
1479 if (!bvcur.anchor_.hasPart(cur)) {
1483 CursorSlice old = bvcur.top();
1485 int const wh = bv->workHeight();
1486 int const y = max(0, min(wh - 1, cmd.y));
1488 tm->setCursorFromCoordinates(cur, cmd.x, y);
1489 cur.setTargetX(cmd.x);
1491 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1493 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1494 // This is to allow jumping over large insets
1495 if (cur.top() == old) {
1497 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1499 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1501 // We continue with our existing selection or start a new one, so don't
1502 // reset the anchor.
1503 bvcur.setCursor(cur);
1504 bvcur.setSelection(true);
1505 if (cur.top() == old) {
1506 // We didn't move one iota, so no need to update the screen.
1507 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1514 case LFUN_MOUSE_RELEASE:
1515 switch (cmd.button()) {
1516 case mouse_button::button1:
1517 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1518 // If there is a new selection, update persistent selection;
1519 // otherwise, single click does not clear persistent selection
1521 if (cur.selection()) {
1522 // Finish selection. If double click,
1523 // cur is moved to the end of word by
1524 // selectWord but bvcur is current
1526 cur.bv().cursor().setSelection();
1527 // We might have removed an empty but drawn selection
1528 // (probably a margin)
1529 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1532 // FIXME: We could try to handle drag and drop of selection here.
1535 case mouse_button::button2:
1536 // Middle mouse pasting is handled at mouse press time,
1537 // see LFUN_MOUSE_PRESS.
1541 case mouse_button::button3:
1542 // Cursor was set at LFUN_MOUSE_PRESS time.
1543 // FIXME: If there is a selection we could try to handle a special
1544 // drag & drop context menu.
1548 case mouse_button::none:
1549 case mouse_button::button4:
1550 case mouse_button::button5:
1552 } // switch (cmd.button())
1556 case LFUN_SELF_INSERT: {
1557 if (cmd.argument().empty())
1560 // Automatically delete the currently selected
1561 // text and replace it with what is being
1562 // typed in now. Depends on lyxrc settings
1563 // "auto_region_delete", which defaults to
1566 if (lyxrc.auto_region_delete && cur.selection())
1567 cutSelection(cur, false, false);
1569 cur.clearSelection();
1571 docstring::const_iterator cit = cmd.argument().begin();
1572 docstring::const_iterator const end = cmd.argument().end();
1573 for (; cit != end; ++cit)
1574 bv->translateAndInsert(*cit, this, cur);
1577 moveCursor(cur, false);
1578 bv->bookmarkEditPosition();
1582 case LFUN_HYPERLINK_INSERT: {
1583 InsetCommandParams p(HYPERLINK_CODE);
1585 if (cur.selection()) {
1586 content = cur.selectionAsString(false);
1587 cutSelection(cur, true, false);
1589 p["target"] = (cmd.argument().empty()) ?
1590 content : cmd.argument();
1591 string const data = InsetCommand::params2string("href", p);
1592 if (p["target"].empty()) {
1593 bv->showDialog("href", data);
1595 FuncRequest fr(LFUN_INSET_INSERT, data);
1601 case LFUN_LABEL_INSERT: {
1602 InsetCommandParams p(LABEL_CODE);
1603 // Try to generate a valid label
1604 p["name"] = (cmd.argument().empty()) ?
1605 cur.getPossibleLabel() :
1607 string const data = InsetCommand::params2string("label", p);
1609 if (cmd.argument().empty()) {
1610 bv->showDialog("label", data);
1612 FuncRequest fr(LFUN_INSET_INSERT, data);
1618 case LFUN_INFO_INSERT: {
1620 if (cmd.argument().empty() && cur.selection()) {
1621 // if command argument is empty use current selection as parameter.
1622 docstring ds = cur.selectionAsString(false);
1623 cutSelection(cur, true, false);
1624 FuncRequest cmd0(cmd, ds);
1625 inset = createInset(cur.bv().buffer(), cmd0);
1627 inset = createInset(cur.bv().buffer(), cmd);
1632 insertInset(cur, inset);
1636 case LFUN_CAPTION_INSERT:
1637 case LFUN_FOOTNOTE_INSERT:
1638 case LFUN_NOTE_INSERT:
1639 case LFUN_FLEX_INSERT:
1640 case LFUN_BOX_INSERT:
1641 case LFUN_BRANCH_INSERT:
1642 case LFUN_PHANTOM_INSERT:
1643 case LFUN_ERT_INSERT:
1644 case LFUN_LISTING_INSERT:
1645 case LFUN_MARGINALNOTE_INSERT:
1646 case LFUN_OPTIONAL_INSERT:
1647 case LFUN_INDEX_INSERT:
1648 // Open the inset, and move the current selection
1650 doInsertInset(cur, this, cmd, true, true);
1652 // Some insets are numbered, others are shown in the outline pane so
1653 // let's update the labels and the toc backend.
1654 bv->buffer().updateLabels();
1657 case LFUN_TABULAR_INSERT:
1658 // if there were no arguments, just open the dialog
1659 if (doInsertInset(cur, this, cmd, false, true))
1662 bv->showDialog("tabularcreate");
1666 case LFUN_FLOAT_INSERT:
1667 case LFUN_FLOAT_WIDE_INSERT:
1668 case LFUN_WRAP_INSERT: {
1669 // will some text be moved into the inset?
1670 bool content = cur.selection();
1672 doInsertInset(cur, this, cmd, true, true);
1675 // If some text is moved into the inset, doInsertInset
1676 // puts the cursor outside the inset. To insert the
1677 // caption we put it back into the inset.
1681 ParagraphList & pars = cur.text()->paragraphs();
1683 DocumentClass const & tclass = bv->buffer().params().documentClass();
1685 // add a separate paragraph for the caption inset
1686 pars.push_back(Paragraph());
1687 pars.back().setInsetOwner(&pars[0].inInset());
1688 pars.back().setPlainOrDefaultLayout(tclass);
1689 int cap_pit = pars.size() - 1;
1691 // if an empty inset was created, we create an additional empty
1692 // paragraph at the bottom so that the user can choose where to put
1693 // the graphics (or table).
1695 pars.push_back(Paragraph());
1696 pars.back().setInsetOwner(&pars[0].inInset());
1697 pars.back().setPlainOrDefaultLayout(tclass);
1700 // reposition the cursor to the caption
1701 cur.pit() = cap_pit;
1703 // FIXME: This Text/Cursor dispatch handling is a mess!
1704 // We cannot use Cursor::dispatch here it needs access to up to
1706 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1707 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1708 bv->buffer().updateLabels();
1709 cur.updateFlags(Update::Force);
1710 // FIXME: When leaving the Float (or Wrap) inset we should
1711 // delete any empty paragraph left above or below the
1716 case LFUN_NOMENCL_INSERT: {
1717 InsetCommandParams p(NOMENCL_CODE);
1718 if (cmd.argument().empty())
1719 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1721 p["symbol"] = cmd.argument();
1722 string const data = InsetCommand::params2string("nomenclature", p);
1723 bv->showDialog("nomenclature", data);
1727 case LFUN_INDEX_PRINT: {
1728 InsetCommandParams p(INDEX_PRINT_CODE);
1729 if (cmd.argument().empty())
1730 p["type"] = from_ascii("idx");
1732 p["type"] = cmd.argument();
1733 string const data = InsetCommand::params2string("index_print", p);
1734 FuncRequest fr(LFUN_INSET_INSERT, data);
1739 case LFUN_NOMENCL_PRINT:
1740 case LFUN_TOC_INSERT:
1741 case LFUN_LINE_INSERT:
1742 case LFUN_NEWPAGE_INSERT:
1744 doInsertInset(cur, this, cmd, false, false);
1748 case LFUN_DEPTH_DECREMENT:
1749 changeDepth(cur, DEC_DEPTH);
1752 case LFUN_DEPTH_INCREMENT:
1753 changeDepth(cur, INC_DEPTH);
1756 case LFUN_MATH_DISPLAY:
1757 mathDispatch(cur, cmd, true);
1760 case LFUN_REGEXP_MODE:
1761 regexpDispatch(cur, cmd);
1764 case LFUN_MATH_MODE:
1765 if (cmd.argument() == "on")
1766 // don't pass "on" as argument
1767 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1769 mathDispatch(cur, cmd, false);
1772 case LFUN_MATH_MACRO:
1773 if (cmd.argument().empty())
1774 cur.errorMessage(from_utf8(N_("Missing argument")));
1776 string s = to_utf8(cmd.argument());
1777 string const s1 = token(s, ' ', 1);
1778 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1779 string const s2 = token(s, ' ', 2);
1780 MacroType type = MacroTypeNewcommand;
1782 type = MacroTypeDef;
1783 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1784 inset->setBuffer(bv->buffer());
1785 insertInset(cur, inset);
1787 // enter macro inset and select the name
1789 cur.top().pos() = cur.top().lastpos();
1791 cur.setSelection(true);
1792 cur.top().pos() = 0;
1796 // passthrough hat and underscore outside mathed:
1797 case LFUN_MATH_SUBSCRIPT:
1798 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1800 case LFUN_MATH_SUPERSCRIPT:
1801 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1804 case LFUN_MATH_INSERT:
1805 case LFUN_MATH_AMS_MATRIX:
1806 case LFUN_MATH_MATRIX:
1807 case LFUN_MATH_DELIM:
1808 case LFUN_MATH_BIGDELIM: {
1810 cap::replaceSelection(cur);
1811 cur.insert(new InsetMathHull(hullSimple));
1812 checkAndActivateInset(cur, true);
1813 LASSERT(cur.inMathed(), /**/);
1818 case LFUN_FONT_EMPH: {
1819 Font font(ignore_font, ignore_language);
1820 font.fontInfo().setEmph(FONT_TOGGLE);
1821 toggleAndShow(cur, this, font);
1825 case LFUN_FONT_ITAL: {
1826 Font font(ignore_font, ignore_language);
1827 font.fontInfo().setShape(ITALIC_SHAPE);
1828 toggleAndShow(cur, this, font);
1832 case LFUN_FONT_BOLD:
1833 case LFUN_FONT_BOLDSYMBOL: {
1834 Font font(ignore_font, ignore_language);
1835 font.fontInfo().setSeries(BOLD_SERIES);
1836 toggleAndShow(cur, this, font);
1840 case LFUN_FONT_NOUN: {
1841 Font font(ignore_font, ignore_language);
1842 font.fontInfo().setNoun(FONT_TOGGLE);
1843 toggleAndShow(cur, this, font);
1847 case LFUN_FONT_TYPEWRITER: {
1848 Font font(ignore_font, ignore_language);
1849 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1850 toggleAndShow(cur, this, font);
1854 case LFUN_FONT_SANS: {
1855 Font font(ignore_font, ignore_language);
1856 font.fontInfo().setFamily(SANS_FAMILY);
1857 toggleAndShow(cur, this, font);
1861 case LFUN_FONT_ROMAN: {
1862 Font font(ignore_font, ignore_language);
1863 font.fontInfo().setFamily(ROMAN_FAMILY);
1864 toggleAndShow(cur, this, font);
1868 case LFUN_FONT_DEFAULT: {
1869 Font font(inherit_font, ignore_language);
1870 toggleAndShow(cur, this, font);
1874 case LFUN_FONT_STRIKEOUT: {
1875 Font font(ignore_font, ignore_language);
1876 font.fontInfo().setStrikeout(FONT_TOGGLE);
1877 toggleAndShow(cur, this, font);
1881 case LFUN_FONT_UULINE: {
1882 Font font(ignore_font, ignore_language);
1883 font.fontInfo().setUuline(FONT_TOGGLE);
1884 toggleAndShow(cur, this, font);
1888 case LFUN_FONT_UWAVE: {
1889 Font font(ignore_font, ignore_language);
1890 font.fontInfo().setUwave(FONT_TOGGLE);
1891 toggleAndShow(cur, this, font);
1895 case LFUN_FONT_UNDERLINE: {
1896 Font font(ignore_font, ignore_language);
1897 font.fontInfo().setUnderbar(FONT_TOGGLE);
1898 toggleAndShow(cur, this, font);
1902 case LFUN_FONT_SIZE: {
1903 Font font(ignore_font, ignore_language);
1904 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1905 toggleAndShow(cur, this, font);
1909 case LFUN_LANGUAGE: {
1910 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1913 Font font(ignore_font, lang);
1914 toggleAndShow(cur, this, font);
1918 case LFUN_TEXTSTYLE_APPLY:
1919 toggleAndShow(cur, this, freefont, toggleall);
1920 cur.message(_("Character set"));
1923 // Set the freefont using the contents of \param data dispatched from
1924 // the frontends and apply it at the current cursor location.
1925 case LFUN_TEXTSTYLE_UPDATE: {
1928 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1931 toggleAndShow(cur, this, freefont, toggleall);
1932 cur.message(_("Character set"));
1934 lyxerr << "Argument not ok";
1939 case LFUN_FINISHED_LEFT:
1940 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1941 // We're leaving an inset, going left. If the inset is LTR, we're
1942 // leaving from the front, so we should not move (remain at --- but
1943 // not in --- the inset). If the inset is RTL, move left, without
1944 // entering the inset itself; i.e., move to after the inset.
1945 if (cur.paragraph().getFontSettings(
1946 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1947 cursorVisLeft(cur, true);
1950 case LFUN_FINISHED_RIGHT:
1951 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1952 // We're leaving an inset, going right. If the inset is RTL, we're
1953 // leaving from the front, so we should not move (remain at --- but
1954 // not in --- the inset). If the inset is LTR, move right, without
1955 // entering the inset itself; i.e., move to after the inset.
1956 if (!cur.paragraph().getFontSettings(
1957 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1958 cursorVisRight(cur, true);
1961 case LFUN_FINISHED_BACKWARD:
1962 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1965 case LFUN_FINISHED_FORWARD:
1966 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1968 cur.setCurrentFont();
1971 case LFUN_LAYOUT_PARAGRAPH: {
1973 params2string(cur.paragraph(), data);
1974 data = "show\n" + data;
1975 bv->showDialog("paragraph", data);
1979 case LFUN_PARAGRAPH_UPDATE: {
1981 params2string(cur.paragraph(), data);
1983 // Will the paragraph accept changes from the dialog?
1985 cur.inset().allowParagraphCustomization(cur.idx());
1987 data = "update " + convert<string>(accept) + '\n' + data;
1988 bv->updateDialog("paragraph", data);
1992 case LFUN_ACCENT_UMLAUT:
1993 case LFUN_ACCENT_CIRCUMFLEX:
1994 case LFUN_ACCENT_GRAVE:
1995 case LFUN_ACCENT_ACUTE:
1996 case LFUN_ACCENT_TILDE:
1997 case LFUN_ACCENT_CEDILLA:
1998 case LFUN_ACCENT_MACRON:
1999 case LFUN_ACCENT_DOT:
2000 case LFUN_ACCENT_UNDERDOT:
2001 case LFUN_ACCENT_UNDERBAR:
2002 case LFUN_ACCENT_CARON:
2003 case LFUN_ACCENT_BREVE:
2004 case LFUN_ACCENT_TIE:
2005 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2006 case LFUN_ACCENT_CIRCLE:
2007 case LFUN_ACCENT_OGONEK:
2008 theLyXFunc().handleKeyFunc(cmd.action);
2009 if (!cmd.argument().empty())
2010 // FIXME: Are all these characters encoded in one byte in utf8?
2011 bv->translateAndInsert(cmd.argument()[0], this, cur);
2014 case LFUN_FLOAT_LIST_INSERT: {
2015 DocumentClass const & tclass = bv->buffer().params().documentClass();
2016 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2018 if (cur.selection())
2019 cutSelection(cur, true, false);
2020 breakParagraph(cur);
2022 if (cur.lastpos() != 0) {
2023 cursorBackward(cur);
2024 breakParagraph(cur);
2027 docstring const laystr = cur.inset().usePlainLayout() ?
2028 tclass.plainLayoutName() :
2029 tclass.defaultLayoutName();
2030 setLayout(cur, laystr);
2031 ParagraphParameters p;
2032 // FIXME If this call were replaced with one to clearParagraphParams(),
2033 // then we could get rid of this method altogether.
2034 setParagraphs(cur, p);
2035 // FIXME This should be simplified when InsetFloatList takes a
2036 // Buffer in its constructor.
2037 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
2038 ifl->setBuffer(bv->buffer());
2039 insertInset(cur, ifl);
2042 lyxerr << "Non-existent float type: "
2043 << to_utf8(cmd.argument()) << endl;
2048 case LFUN_CHANGE_ACCEPT: {
2049 acceptOrRejectChanges(cur, ACCEPT);
2053 case LFUN_CHANGE_REJECT: {
2054 acceptOrRejectChanges(cur, REJECT);
2058 case LFUN_THESAURUS_ENTRY: {
2059 docstring arg = cmd.argument();
2061 arg = cur.selectionAsString(false);
2063 if (arg.size() > 100 || arg.empty()) {
2064 // Get word or selection
2065 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2066 arg = cur.selectionAsString(false);
2067 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2070 bv->showDialog("thesaurus", to_utf8(arg));
2074 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2075 // Given data, an encoding of the ParagraphParameters
2076 // generated in the Paragraph dialog, this function sets
2077 // the current paragraph, or currently selected paragraphs,
2079 // NOTE: This function overrides all existing settings.
2080 setParagraphs(cur, cmd.argument());
2081 cur.message(_("Paragraph layout set"));
2085 case LFUN_PARAGRAPH_PARAMS: {
2086 // Given data, an encoding of the ParagraphParameters as we'd
2087 // find them in a LyX file, this function modifies the current paragraph,
2088 // or currently selected paragraphs.
2089 // NOTE: This function only modifies, and does not override, existing
2091 setParagraphs(cur, cmd.argument(), true);
2092 cur.message(_("Paragraph layout set"));
2097 if (cur.selection()) {
2098 cur.setSelection(false);
2101 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2102 // correct, but I'm not 100% sure -- dov, 071019
2103 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2107 case LFUN_OUTLINE_UP:
2108 outline(OutlineUp, cur);
2109 setCursor(cur, cur.pit(), 0);
2110 cur.buffer()->updateLabels();
2114 case LFUN_OUTLINE_DOWN:
2115 outline(OutlineDown, cur);
2116 setCursor(cur, cur.pit(), 0);
2117 cur.buffer()->updateLabels();
2121 case LFUN_OUTLINE_IN:
2122 outline(OutlineIn, cur);
2123 cur.buffer()->updateLabels();
2127 case LFUN_OUTLINE_OUT:
2128 outline(OutlineOut, cur);
2129 cur.buffer()->updateLabels();
2133 case LFUN_OUTLINE_DRAGMOVE: {
2134 int const move_id = convert<int>(cmd.getArg(0));
2135 int const move_to_id = convert<int>(cmd.getArg(1));
2136 dragMove(cur, move_id, move_to_id);
2137 setCursor(cur, cur.pit(), 0);
2138 cur.buffer()->updateLabels();
2144 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2149 if (lyxrc.spellcheck_continuously && cur.inTexted())
2150 // Take this opportunity to spellcheck current word.
2151 cur.paragraph().isMisspelled(cur.pos());
2153 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2155 // FIXME: The cursor flag is reset two lines below
2156 // so we need to check here if some of the LFUN did touch that.
2157 // for now only Text::erase() and Text::backspace() do that.
2158 // The plan is to verify all the LFUNs and then to remove this
2159 // singleParUpdate boolean altogether.
2160 if (cur.result().update() & Update::Force) {
2161 singleParUpdate = false;
2165 // FIXME: the following code should go in favor of fine grained
2166 // update flag treatment.
2167 if (singleParUpdate) {
2168 // Inserting characters does not change par height in general. So, try
2169 // to update _only_ this paragraph. BufferView will detect if a full
2170 // metrics update is needed anyway.
2171 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2176 && &oldTopSlice.inset() == &cur.inset()
2177 && oldTopSlice.idx() == cur.idx()
2178 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2179 && !cur.selection())
2180 // FIXME: it would be better if we could just do this
2182 //if (cur.result().update() != Update::FitCursor)
2185 // But some LFUNs do not set Update::FitCursor when needed, so we
2186 // do it for all. This is not very harmfull as FitCursor will provoke
2187 // a full redraw only if needed but still, a proper review of all LFUN
2188 // should be done and this needsUpdate boolean can then be removed.
2189 cur.updateFlags(Update::FitCursor);
2191 cur.updateFlags(Update::Force | Update::FitCursor);
2195 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2196 FuncStatus & flag) const
2198 LASSERT(cur.text() == this, /**/);
2200 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2202 InsetCode code = NO_CODE;
2204 switch (cmd.action) {
2206 case LFUN_DEPTH_DECREMENT:
2207 enable = changeDepthAllowed(cur, DEC_DEPTH);
2210 case LFUN_DEPTH_INCREMENT:
2211 enable = changeDepthAllowed(cur, INC_DEPTH);
2215 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2218 case LFUN_DIALOG_SHOW_NEW_INSET:
2219 if (cmd.argument() == "bibitem")
2220 code = BIBITEM_CODE;
2221 else if (cmd.argument() == "bibtex") {
2223 // not allowed in description items
2224 enable = !inDescriptionItem(cur);
2226 else if (cmd.argument() == "box")
2228 else if (cmd.argument() == "branch")
2230 else if (cmd.argument() == "citation")
2232 else if (cmd.argument() == "ert")
2234 else if (cmd.argument() == "external")
2235 code = EXTERNAL_CODE;
2236 else if (cmd.argument() == "float")
2238 else if (cmd.argument() == "graphics")
2239 code = GRAPHICS_CODE;
2240 else if (cmd.argument() == "href")
2241 code = HYPERLINK_CODE;
2242 else if (cmd.argument() == "include")
2243 code = INCLUDE_CODE;
2244 else if (cmd.argument() == "index")
2246 else if (cmd.argument() == "index_print")
2247 code = INDEX_PRINT_CODE;
2248 else if (cmd.argument() == "nomenclature")
2249 code = NOMENCL_CODE;
2250 else if (cmd.argument() == "nomencl_print")
2251 code = NOMENCL_PRINT_CODE;
2252 else if (cmd.argument() == "label")
2254 else if (cmd.argument() == "note")
2256 else if (cmd.argument() == "phantom")
2257 code = PHANTOM_CODE;
2258 else if (cmd.argument() == "ref")
2260 else if (cmd.argument() == "space")
2262 else if (cmd.argument() == "toc")
2264 else if (cmd.argument() == "vspace")
2266 else if (cmd.argument() == "wrap")
2268 else if (cmd.argument() == "listings")
2269 code = LISTINGS_CODE;
2272 case LFUN_ERT_INSERT:
2275 case LFUN_LISTING_INSERT:
2276 code = LISTINGS_CODE;
2277 // not allowed in description items
2278 enable = !inDescriptionItem(cur);
2280 case LFUN_FOOTNOTE_INSERT:
2283 case LFUN_TABULAR_INSERT:
2284 code = TABULAR_CODE;
2286 case LFUN_MARGINALNOTE_INSERT:
2289 case LFUN_FLOAT_INSERT:
2290 case LFUN_FLOAT_WIDE_INSERT:
2291 // FIXME: If there is a selection, we should check whether there
2292 // are floats in the selection, but this has performance issues, see
2293 // LFUN_CHANGE_ACCEPT/REJECT.
2295 if (inDescriptionItem(cur))
2296 // not allowed in description items
2299 InsetCode const inset_code = cur.inset().lyxCode();
2301 // algorithm floats cannot be put in another float
2302 if (to_utf8(cmd.argument()) == "algorithm") {
2303 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2307 // for figures and tables: only allow in another
2308 // float or wrap if it is of the same type and
2309 // not a subfloat already
2310 if(cur.inset().lyxCode() == code) {
2311 InsetFloat const & ins =
2312 static_cast<InsetFloat const &>(cur.inset());
2313 enable = ins.params().type == to_utf8(cmd.argument())
2314 && !ins.params().subfloat;
2315 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2316 InsetWrap const & ins =
2317 static_cast<InsetWrap const &>(cur.inset());
2318 enable = ins.params().type == to_utf8(cmd.argument());
2322 case LFUN_WRAP_INSERT:
2324 // not allowed in description items
2325 enable = !inDescriptionItem(cur);
2327 case LFUN_FLOAT_LIST_INSERT:
2328 code = FLOAT_LIST_CODE;
2329 // not allowed in description items
2330 enable = !inDescriptionItem(cur);
2332 case LFUN_CAPTION_INSERT:
2333 code = CAPTION_CODE;
2334 // not allowed in description items
2335 enable = !inDescriptionItem(cur);
2337 case LFUN_NOTE_INSERT:
2339 // in commands (sections etc.) and description items,
2340 // only Notes are allowed
2341 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2342 (!cur.paragraph().layout().isCommand()
2343 && !inDescriptionItem(cur)));
2345 case LFUN_FLEX_INSERT: {
2347 string s = cmd.getArg(0);
2349 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2350 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2351 il.lyxtype() != InsetLayout::CUSTOM &&
2352 il.lyxtype() != InsetLayout::ELEMENT &&
2353 il.lyxtype ()!= InsetLayout::STANDARD)
2357 case LFUN_BOX_INSERT:
2360 case LFUN_BRANCH_INSERT:
2362 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2363 && cur.buffer()->params().branchlist().empty())
2366 case LFUN_PHANTOM_INSERT:
2367 code = PHANTOM_CODE;
2369 case LFUN_LABEL_INSERT:
2372 case LFUN_LINE_INSERT:
2375 case LFUN_INFO_INSERT:
2378 case LFUN_OPTIONAL_INSERT:
2380 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2381 < cur.paragraph().layout().optionalargs;
2383 case LFUN_INDEX_INSERT:
2386 case LFUN_INDEX_PRINT:
2387 code = INDEX_PRINT_CODE;
2388 // not allowed in description items
2389 enable = !inDescriptionItem(cur);
2391 case LFUN_NOMENCL_INSERT:
2392 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2396 code = NOMENCL_CODE;
2398 case LFUN_NOMENCL_PRINT:
2399 code = NOMENCL_PRINT_CODE;
2400 // not allowed in description items
2401 enable = !inDescriptionItem(cur);
2403 case LFUN_TOC_INSERT:
2405 // not allowed in description items
2406 enable = !inDescriptionItem(cur);
2408 case LFUN_HYPERLINK_INSERT:
2409 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2413 code = HYPERLINK_CODE;
2415 case LFUN_QUOTE_INSERT:
2416 // always allow this, since we will inset a raw quote
2417 // if an inset is not allowed.
2419 case LFUN_SPECIALCHAR_INSERT:
2420 code = SPECIALCHAR_CODE;
2422 case LFUN_SPACE_INSERT:
2423 // slight hack: we know this is allowed in math mode
2428 case LFUN_MATH_INSERT:
2429 case LFUN_MATH_AMS_MATRIX:
2430 case LFUN_MATH_MATRIX:
2431 case LFUN_MATH_DELIM:
2432 case LFUN_MATH_BIGDELIM:
2433 case LFUN_MATH_DISPLAY:
2434 case LFUN_MATH_MODE:
2435 case LFUN_MATH_MACRO:
2436 case LFUN_MATH_SUBSCRIPT:
2437 case LFUN_MATH_SUPERSCRIPT:
2438 code = MATH_HULL_CODE;
2441 case LFUN_INSET_MODIFY:
2442 // We need to disable this, because we may get called for a
2444 // InsetTabular::getStatus() -> InsetText::getStatus()
2445 // and we don't handle LFUN_INSET_MODIFY.
2449 case LFUN_FONT_EMPH:
2450 flag.setOnOff(fontinfo.emph() == FONT_ON);
2453 case LFUN_FONT_ITAL:
2454 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2457 case LFUN_FONT_NOUN:
2458 flag.setOnOff(fontinfo.noun() == FONT_ON);
2461 case LFUN_FONT_BOLD:
2462 case LFUN_FONT_BOLDSYMBOL:
2463 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2466 case LFUN_FONT_SANS:
2467 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2470 case LFUN_FONT_ROMAN:
2471 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2474 case LFUN_FONT_TYPEWRITER:
2475 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2480 enable = cur.selection();
2484 if (cmd.argument().empty()) {
2485 if (theClipboard().isInternal())
2486 enable = cap::numberOfSelections() > 0;
2488 enable = !theClipboard().empty();
2492 // we have an argument
2493 string const arg = to_utf8(cmd.argument());
2494 if (isStrUnsignedInt(arg)) {
2495 // it's a number and therefore means the internal stack
2496 unsigned int n = convert<unsigned int>(arg);
2497 enable = cap::numberOfSelections() > n;
2501 // explicit graphics type?
2502 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2503 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2504 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2505 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2506 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2507 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2508 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2509 enable = theClipboard().hasGraphicsContents(type);
2518 case LFUN_CLIPBOARD_PASTE:
2519 enable = !theClipboard().empty();
2522 case LFUN_PRIMARY_SELECTION_PASTE:
2523 enable = cur.selection() || !theSelection().empty();
2526 case LFUN_SELECTION_PASTE:
2527 enable = cap::selection();
2530 case LFUN_PARAGRAPH_MOVE_UP:
2531 enable = cur.pit() > 0 && !cur.selection();
2534 case LFUN_PARAGRAPH_MOVE_DOWN:
2535 enable = cur.pit() < cur.lastpit() && !cur.selection();
2538 case LFUN_INSET_DISSOLVE:
2539 if (!cmd.argument().empty()) {
2540 InsetLayout const & il = cur.inset().getLayout();
2541 InsetLayout::InsetLyXType const type =
2542 translateLyXType(to_utf8(cmd.argument()));
2543 enable = cur.inset().lyxCode() == FLEX_CODE
2544 && il.lyxtype() == type;
2546 enable = ((!isMainText(cur.bv().buffer())
2547 && cur.inset().nargs() == 1)
2549 && cur.nextInset()->nargs() == 1));
2553 case LFUN_CHANGE_ACCEPT:
2554 case LFUN_CHANGE_REJECT:
2555 // In principle, these LFUNs should only be enabled if there
2556 // is a change at the current position/in the current selection.
2557 // However, without proper optimizations, this will inevitably
2558 // result in unacceptable performance - just imagine a user who
2559 // wants to select the complete content of a long document.
2560 if (!cur.selection()) {
2561 Change const & change = cur.paragraph().lookupChange(cur.pos());
2562 enable = change.changed();
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(cur.bv().buffer())
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);
2784 insertStringAsLines(cur, clip);
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) != ' ')));