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/InsetFloatList.h"
60 #include "insets/InsetGraphics.h"
61 #include "insets/InsetGraphicsParams.h"
62 #include "insets/InsetNewline.h"
63 #include "insets/InsetQuotes.h"
64 #include "insets/InsetSpecialChar.h"
65 #include "insets/InsetText.h"
67 #include "support/convert.h"
68 #include "support/debug.h"
69 #include "support/gettext.h"
70 #include "support/lstrings.h"
71 #include "support/lyxtime.h"
72 #include "support/os.h"
74 #include "mathed/InsetMathHull.h"
75 #include "mathed/MathMacroTemplate.h"
77 #include <boost/next_prior.hpp>
83 using namespace lyx::support;
87 using cap::copySelection;
88 using cap::cutSelection;
89 using cap::pasteFromStack;
90 using cap::pasteClipboardText;
91 using cap::pasteClipboardGraphics;
92 using cap::replaceSelection;
93 using cap::grabAndEraseSelection;
94 using cap::selClearOrDel;
97 static Font freefont(ignore_font, ignore_language);
98 static bool toggleall = false;
100 static void toggleAndShow(Cursor & cur, Text * text,
101 Font const & font, bool toggleall = true)
103 text->toggleFree(cur, font, toggleall);
105 if (font.language() != ignore_language ||
106 font.fontInfo().number() != FONT_IGNORE) {
107 TextMetrics const & tm = cur.bv().textMetrics(text);
108 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
109 cur.real_current_font))
110 text->setCursor(cur, cur.pit(), cur.pos(),
111 false, !cur.boundary());
116 static void moveCursor(Cursor & cur, bool selecting)
118 if (selecting || cur.mark())
123 static void finishChange(Cursor & cur, bool selecting)
126 moveCursor(cur, selecting);
130 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
133 docstring sel = cur.selectionAsString(false);
135 // It may happen that sel is empty but there is a selection
136 replaceSelection(cur);
138 // Is this a valid formula?
142 #ifdef ENABLE_ASSERTIONS
143 const int old_pos = cur.pos();
145 cur.insert(new InsetMathHull(hullSimple));
146 #ifdef ENABLE_ASSERTIONS
147 LASSERT(old_pos == cur.pos(), /**/);
149 cur.nextInset()->edit(cur, true);
150 // don't do that also for LFUN_MATH_MODE
151 // unless you want end up with always changing
152 // to mathrm when opening an inlined inset --
153 // I really hate "LyXfunc overloading"...
155 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
156 // Avoid an unnecessary undo step if cmd.argument
158 if (!cmd.argument().empty())
159 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
162 // create a macro if we see "\\newcommand"
163 // somewhere, and an ordinary formula
165 if (sel.find(from_ascii("\\newcommand")) == string::npos
166 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
167 && sel.find(from_ascii("\\def")) == string::npos)
169 InsetMathHull * formula = new InsetMathHull;
170 string const selstr = to_utf8(sel);
171 istringstream is(selstr);
174 if (!formula->readQuiet(lex)) {
175 // No valid formula, let's try with delims
176 is.str("$" + selstr + "$");
178 if (!formula->readQuiet(lex)) {
179 // Still not valid, leave it as is
188 cur.insert(new MathMacroTemplate(sel));
192 cur.message(from_utf8(N_("Math editor mode")));
194 cur.message(from_utf8(N_("No valid math formula")));
198 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
200 BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
201 if (cur.inRegexped()) {
202 cur.message(_("Already in regexp mode"));
206 docstring const save_selection = grabAndEraseSelection(cur);
208 // replaceSelection(cur);
210 cur.insert(new InsetMathHull(hullRegexp));
211 cur.nextInset()->edit(cur, true);
212 cur.niceInsert(save_selection);
214 cur.message(_("Regexp editor mode"));
218 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
221 cap::replaceSelection(cur);
222 cur.insert(new InsetSpecialChar(kind));
227 static bool doInsertInset(Cursor & cur, Text * text,
228 FuncRequest const & cmd, bool edit, bool pastesel)
230 Buffer & buffer = cur.bv().buffer();
231 BufferParams const & bparams = buffer.params();
232 Inset * inset = createInset(buffer, cmd);
236 if (InsetCollapsable * ci = inset->asInsetCollapsable())
237 ci->setLayout(bparams);
240 if (cmd.action == LFUN_INDEX_INSERT) {
241 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
242 text->insertInset(cur, inset);
244 inset->edit(cur, true);
245 // Now put this into inset
246 static_cast<InsetCollapsable *>(inset)->
247 text().insertStringAsParagraphs(cur, ds);
252 if (cur.selection()) {
253 cutSelection(cur, false, pastesel);
254 cur.clearSelection();
257 text->insertInset(cur, inset);
260 inset->edit(cur, true);
262 if (!gotsel || !pastesel)
265 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
266 cur.buffer()->errors("Paste");
267 cur.clearSelection(); // bug 393
269 InsetText * insetText = dynamic_cast<InsetText *>(inset);
270 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
271 // reset first par to default
272 cur.text()->paragraphs().begin()
273 ->setPlainOrDefaultLayout(bparams.documentClass());
276 // Merge multiple paragraphs -- hack
277 while (cur.lastpit() > 0)
278 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
279 cur.leaveInset(*inset);
281 cur.leaveInset(*inset);
282 // reset surrounding par to default
283 DocumentClass const & dc = bparams.documentClass();
284 docstring const layoutname = inset->usePlainLayout()
285 ? dc.plainLayoutName()
286 : dc.defaultLayoutName();
287 text->setLayout(cur, layoutname);
294 string const freefont2string()
296 return freefont.toString(toggleall);
300 static void dragMove(Cursor & cur, int moveid, int movetoid)
302 // Create pointers to buffers
303 Buffer & buf_move = *cur.buffer();
304 DocIterator dit_move = buf_move.getParFromID(moveid);
305 DocIterator dit_dest = buf_move.getParFromID(movetoid);
307 pit_type & pit_move = dit_move.pit();
308 pit_type & pit_dest = dit_dest.pit();
309 ParagraphList & pars = dit_move.text()->paragraphs();
311 // Create references to the paragraphs to be moved
312 ParagraphList::iterator const bgn = pars.begin();
313 ParagraphList::iterator dest_start = boost::next(bgn, pit_dest);
315 // The first paragraph of the area to be copied:
316 ParagraphList::iterator start = boost::next(bgn, pit_move);
317 // The final paragraph of area to be copied:
318 ParagraphList::iterator finish = start;
319 ParagraphList::iterator const end = pars.end();
321 // Move out (down) from this section header
325 // Seek the one (on same level) below
326 int const thistoclevel = start->layout().toclevel;
327 for (; finish != end; ++finish) {
328 int const toclevel = finish->layout().toclevel;
329 if (toclevel != Layout::NOT_IN_TOC
330 && toclevel <= thistoclevel)
334 if (start == pars.begin() || start == dest_start)
338 pars.insert(dest_start, start, finish);
339 pars.erase(start, finish);
341 // FIXME: This only really needs doing for the newly
342 // introduced paragraphs. Something like:
343 // pit_type const numpars = distance(start, finish);
344 // start = boost::next(bgn, pit);
345 // finish = boost::next(start, numpars);
346 // for (; start != finish; ++start)
347 // start->setBuffer(buf);
348 // But while this seems to work, it is kind of fragile.
349 buf_move.inset().setBuffer(buf_move);
353 /// the type of outline operation
355 OutlineUp, // Move this header with text down
356 OutlineDown, // Move this header with text up
357 OutlineIn, // Make this header deeper
358 OutlineOut // Make this header shallower
362 static void outline(OutlineOp mode, Cursor & cur)
364 Buffer & buf = *cur.buffer();
365 pit_type & pit = cur.pit();
366 ParagraphList & pars = buf.text().paragraphs();
367 ParagraphList::iterator const bgn = pars.begin();
368 // The first paragraph of the area to be copied:
369 ParagraphList::iterator start = boost::next(bgn, pit);
370 // The final paragraph of area to be copied:
371 ParagraphList::iterator finish = start;
372 ParagraphList::iterator const end = pars.end();
374 DocumentClass const & tc = buf.params().documentClass();
376 int const thistoclevel = start->layout().toclevel;
379 // Move out (down) from this section header
383 // Seek the one (on same level) below
384 for (; finish != end; ++finish) {
385 toclevel = finish->layout().toclevel;
386 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
390 // Do we need to set insets' buffer_ members, because we copied
391 // some stuff? We'll assume we do and reset it otherwise.
392 bool set_buffers = true;
396 if (start == pars.begin())
399 ParagraphList::iterator dest = start;
400 // Move out (up) from this header
403 // Search previous same-level header above
406 toclevel = dest->layout().toclevel;
408 && (toclevel == Layout::NOT_IN_TOC
409 || toclevel > thistoclevel));
410 // Not found; do nothing
411 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
413 pit_type const newpit = distance(bgn, dest);
414 pit_type const len = distance(start, finish);
415 pit_type const deletepit = pit + len;
416 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
417 pars.insert(dest, start, finish);
418 start = boost::next(pars.begin(), deletepit);
420 pars.erase(start, finish);
427 // Go one down from *this* header:
428 ParagraphList::iterator dest = boost::next(finish, 1);
429 // Go further down to find header to insert in front of:
430 for (; dest != end; ++dest) {
431 toclevel = dest->layout().toclevel;
432 if (toclevel != Layout::NOT_IN_TOC
433 && toclevel <= thistoclevel)
436 // One such was found:
437 pit_type newpit = distance(bgn, dest);
438 pit_type const len = distance(start, finish);
439 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
440 pars.insert(dest, start, finish);
441 start = boost::next(bgn, pit);
443 pars.erase(start, finish);
447 pit_type const len = distance(start, finish);
448 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
449 for (; start != finish; ++start) {
450 toclevel = start->layout().toclevel;
451 if (toclevel == Layout::NOT_IN_TOC)
453 DocumentClass::const_iterator lit = tc.begin();
454 DocumentClass::const_iterator len = tc.end();
455 for (; lit != len; ++lit) {
456 if (lit->toclevel == toclevel + 1 &&
457 start->layout().labeltype == lit->labeltype) {
458 start->setLayout(*lit);
467 pit_type const len = distance(start, finish);
468 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
469 for (; start != finish; ++start) {
470 toclevel = start->layout().toclevel;
471 if (toclevel == Layout::NOT_IN_TOC)
473 DocumentClass::const_iterator lit = tc.begin();
474 DocumentClass::const_iterator len = tc.end();
475 for (; lit != len; ++lit) {
476 if (lit->toclevel == toclevel - 1 &&
477 start->layout().labeltype == lit->labeltype) {
478 start->setLayout(*lit);
488 // FIXME This only really needs doing for the newly introduced
489 // paragraphs. Something like:
490 // pit_type const numpars = distance(start, finish);
491 // start = boost::next(bgn, pit);
492 // finish = boost::next(start, numpars);
493 // for (; start != finish; ++start)
494 // start->setBuffer(buf);
495 // But while this seems to work, it is kind of fragile.
496 buf.inset().setBuffer(buf);
500 void Text::number(Cursor & cur)
502 FontInfo font = ignore_font;
503 font.setNumber(FONT_TOGGLE);
504 toggleAndShow(cur, this, Font(font, ignore_language));
508 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
510 return par.isRTL(buffer.params());
514 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
516 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
518 BufferView * bv = &cur.bv();
519 TextMetrics * tm = &bv->textMetrics(this);
520 if (!tm->contains(cur.pit())) {
521 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
522 tm = &bv->textMetrics(this);
525 // FIXME: We use the update flag to indicates wether a singlePar or a
526 // full screen update is needed. We reset it here but shall we restore it
530 LASSERT(cur.text() == this, /**/);
531 CursorSlice oldTopSlice = cur.top();
532 bool oldBoundary = cur.boundary();
533 bool sel = cur.selection();
534 // Signals that, even if needsUpdate == false, an update of the
535 // cursor paragraph is required
536 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
537 LyXAction::SingleParUpdate);
538 // Signals that a full-screen update is required
539 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
540 LyXAction::NoUpdate) || singleParUpdate);
542 switch (cmd.action) {
544 case LFUN_PARAGRAPH_MOVE_DOWN: {
545 pit_type const pit = cur.pit();
546 recUndo(cur, pit, pit + 1);
548 swap(pars_[pit], pars_[pit + 1]);
549 cur.buffer()->updateLabels();
555 case LFUN_PARAGRAPH_MOVE_UP: {
556 pit_type const pit = cur.pit();
557 recUndo(cur, pit - 1, pit);
559 swap(pars_[pit], pars_[pit - 1]);
560 cur.buffer()->updateLabels();
566 case LFUN_APPENDIX: {
567 Paragraph & par = cur.paragraph();
568 bool start = !par.params().startOfAppendix();
570 // FIXME: The code below only makes sense at top level.
571 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
572 // ensure that we have only one start_of_appendix in this document
573 // FIXME: this don't work for multipart document!
574 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
575 if (pars_[tmp].params().startOfAppendix()) {
577 pars_[tmp].params().startOfAppendix(false);
583 par.params().startOfAppendix(start);
585 // we can set the refreshing parameters now
586 cur.buffer()->updateLabels();
590 case LFUN_WORD_DELETE_FORWARD:
592 cutSelection(cur, true, false);
594 deleteWordForward(cur);
595 finishChange(cur, false);
598 case LFUN_WORD_DELETE_BACKWARD:
600 cutSelection(cur, true, false);
602 deleteWordBackward(cur);
603 finishChange(cur, false);
606 case LFUN_LINE_DELETE:
608 cutSelection(cur, true, false);
610 tm->deleteLineForward(cur);
611 finishChange(cur, false);
614 case LFUN_BUFFER_BEGIN:
615 case LFUN_BUFFER_BEGIN_SELECT:
616 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
617 if (cur.depth() == 1)
618 needsUpdate |= cursorTop(cur);
621 cur.updateFlags(Update::FitCursor);
624 case LFUN_BUFFER_END:
625 case LFUN_BUFFER_END_SELECT:
626 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
627 if (cur.depth() == 1)
628 needsUpdate |= cursorBottom(cur);
631 cur.updateFlags(Update::FitCursor);
634 case LFUN_INSET_BEGIN:
635 case LFUN_INSET_BEGIN_SELECT:
636 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
637 if (cur.depth() == 1 || cur.pos() > 0)
638 needsUpdate |= cursorTop(cur);
641 cur.updateFlags(Update::FitCursor);
645 case LFUN_INSET_END_SELECT:
646 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
647 if (cur.depth() == 1 || cur.pos() < cur.lastpos())
648 needsUpdate |= cursorBottom(cur);
651 cur.updateFlags(Update::FitCursor);
654 case LFUN_CHAR_FORWARD:
655 case LFUN_CHAR_FORWARD_SELECT:
656 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
657 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
658 needsUpdate |= cursorForward(cur);
660 if (!needsUpdate && oldTopSlice == cur.top()
661 && cur.boundary() == oldBoundary) {
663 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
665 // we will probably be moving out the inset, so we should execute
666 // the depm-mechanism, but only when the cursor has a place to
667 // go outside this inset, i.e. in a slice above.
668 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
669 && cur.pit() == cur.lastpit()) {
670 // The cursor hasn't changed yet. To give the
671 // DEPM the possibility of doing something we must
672 // provide it with two different cursors.
674 dummy.pos() = dummy.pit() = 0;
675 cur.bv().checkDepm(dummy, cur);
680 case LFUN_CHAR_BACKWARD:
681 case LFUN_CHAR_BACKWARD_SELECT:
682 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
683 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
684 needsUpdate |= cursorBackward(cur);
686 if (!needsUpdate && oldTopSlice == cur.top()
687 && cur.boundary() == oldBoundary) {
689 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
691 // we will probably be moving out the inset, so we should execute
692 // the depm-mechanism, but only when the cursor has a place to
693 // go outside this inset, i.e. in a slice above.
694 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
695 // The cursor hasn't changed yet. To give the
696 // DEPM the possibility of doing something we must
697 // provide it with two different cursors.
699 dummy.pos() = cur.lastpos();
700 dummy.pit() = cur.lastpit();
701 cur.bv().checkDepm(dummy, cur);
707 case LFUN_CHAR_LEFT_SELECT:
708 if (lyxrc.visual_cursor) {
709 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
710 needsUpdate |= cursorVisLeft(cur);
711 if (!needsUpdate && oldTopSlice == cur.top()
712 && cur.boundary() == oldBoundary) {
714 cmd = FuncRequest(LFUN_FINISHED_LEFT);
717 if (reverseDirectionNeeded(cur)) {
718 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
719 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
721 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
722 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
729 case LFUN_CHAR_RIGHT:
730 case LFUN_CHAR_RIGHT_SELECT:
731 if (lyxrc.visual_cursor) {
732 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
733 needsUpdate |= cursorVisRight(cur);
734 if (!needsUpdate && oldTopSlice == cur.top()
735 && cur.boundary() == oldBoundary) {
737 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
740 if (reverseDirectionNeeded(cur)) {
741 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
742 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
744 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
745 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
754 case LFUN_DOWN_SELECT:
757 // stop/start the selection
758 bool select = cmd.action == LFUN_DOWN_SELECT ||
759 cmd.action == LFUN_UP_SELECT;
761 // move cursor up/down
762 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
763 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
765 if (!atFirstOrLastRow) {
766 needsUpdate |= cur.selHandle(select);
767 cur.selHandle(select);
768 cur.upDownInText(up, needsUpdate);
769 needsUpdate |= cur.beforeDispatchCursor().inMathed();
771 // if the cursor cannot be moved up or down do not remove
772 // the selection right now, but wait for the next dispatch.
774 needsUpdate |= cur.selHandle(select);
775 cur.upDownInText(up, needsUpdate);
782 case LFUN_PARAGRAPH_UP:
783 case LFUN_PARAGRAPH_UP_SELECT:
784 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
785 needsUpdate |= cursorUpParagraph(cur);
788 case LFUN_PARAGRAPH_DOWN:
789 case LFUN_PARAGRAPH_DOWN_SELECT:
790 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
791 needsUpdate |= cursorDownParagraph(cur);
794 case LFUN_LINE_BEGIN:
795 case LFUN_LINE_BEGIN_SELECT:
796 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
797 needsUpdate |= tm->cursorHome(cur);
801 case LFUN_LINE_END_SELECT:
802 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
803 needsUpdate |= tm->cursorEnd(cur);
806 case LFUN_SECTION_SELECT: {
807 Buffer const & buf = *cur.buffer();
808 pit_type const pit = cur.pit();
809 ParagraphList & pars = buf.text().paragraphs();
810 ParagraphList::iterator bgn = pars.begin();
811 // The first paragraph of the area to be selected:
812 ParagraphList::iterator start = boost::next(bgn, pit);
813 // The final paragraph of area to be selected:
814 ParagraphList::iterator finish = start;
815 ParagraphList::iterator end = pars.end();
817 int const thistoclevel = start->layout().toclevel;
818 if (thistoclevel == Layout::NOT_IN_TOC)
822 Cursor const old_cur = cur;
823 needsUpdate |= cur.selHandle(true);
825 // Move out (down) from this section header
829 // Seek the one (on same level) below
830 for (; finish != end; ++finish, ++cur.pit()) {
831 int const toclevel = finish->layout().toclevel;
832 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
835 cur.pos() = cur.lastpos();
837 needsUpdate |= cur != old_cur;
841 case LFUN_WORD_RIGHT:
842 case LFUN_WORD_RIGHT_SELECT:
843 if (lyxrc.visual_cursor) {
844 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
845 needsUpdate |= cursorVisRightOneWord(cur);
846 if (!needsUpdate && oldTopSlice == cur.top()
847 && cur.boundary() == oldBoundary) {
849 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
852 if (reverseDirectionNeeded(cur)) {
853 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
854 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
856 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
857 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
864 case LFUN_WORD_FORWARD:
865 case LFUN_WORD_FORWARD_SELECT:
866 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
867 needsUpdate |= cursorForwardOneWord(cur);
871 case LFUN_WORD_LEFT_SELECT:
872 if (lyxrc.visual_cursor) {
873 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
874 needsUpdate |= cursorVisLeftOneWord(cur);
875 if (!needsUpdate && oldTopSlice == cur.top()
876 && cur.boundary() == oldBoundary) {
878 cmd = FuncRequest(LFUN_FINISHED_LEFT);
881 if (reverseDirectionNeeded(cur)) {
882 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
883 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
885 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
886 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
893 case LFUN_WORD_BACKWARD:
894 case LFUN_WORD_BACKWARD_SELECT:
895 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
896 needsUpdate |= cursorBackwardOneWord(cur);
899 case LFUN_WORD_SELECT: {
900 selectWord(cur, WHOLE_WORD);
901 finishChange(cur, true);
905 case LFUN_NEWLINE_INSERT: {
906 InsetNewlineParams inp;
907 docstring arg = cmd.argument();
908 // this avoids a double undo
909 // FIXME: should not be needed, ideally
910 if (!cur.selection())
912 cap::replaceSelection(cur);
913 if (arg == "linebreak")
914 inp.kind = InsetNewlineParams::LINEBREAK;
916 inp.kind = InsetNewlineParams::NEWLINE;
917 cur.insert(new InsetNewline(inp));
919 moveCursor(cur, false);
923 case LFUN_CHAR_DELETE_FORWARD:
924 if (!cur.selection()) {
925 if (cur.pos() == cur.paragraph().size())
926 // Par boundary, force full-screen update
927 singleParUpdate = false;
928 needsUpdate |= erase(cur);
930 // It is possible to make it a lot faster still
931 // just comment out the line below...
933 cutSelection(cur, true, false);
934 singleParUpdate = false;
936 moveCursor(cur, false);
939 case LFUN_CHAR_DELETE_BACKWARD:
940 if (!cur.selection()) {
941 if (bv->getIntl().getTransManager().backspace()) {
942 // Par boundary, full-screen update
944 singleParUpdate = false;
945 needsUpdate |= backspace(cur);
947 // It is possible to make it a lot faster still
948 // just comment out the line below...
951 cutSelection(cur, true, false);
952 singleParUpdate = false;
956 case LFUN_BREAK_PARAGRAPH:
957 cap::replaceSelection(cur);
958 breakParagraph(cur, cmd.argument() == "inverse");
963 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
964 // as its duties can be performed there. Should it be removed??
965 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
966 case LFUN_PARAGRAPH_SPACING: {
967 Paragraph & par = cur.paragraph();
968 Spacing::Space cur_spacing = par.params().spacing().getSpace();
969 string cur_value = "1.0";
970 if (cur_spacing == Spacing::Other)
971 cur_value = par.params().spacing().getValueAsString();
973 istringstream is(to_utf8(cmd.argument()));
976 Spacing::Space new_spacing = cur_spacing;
977 string new_value = cur_value;
979 lyxerr << "Missing argument to `paragraph-spacing'"
981 } else if (tmp == "single") {
982 new_spacing = Spacing::Single;
983 } else if (tmp == "onehalf") {
984 new_spacing = Spacing::Onehalf;
985 } else if (tmp == "double") {
986 new_spacing = Spacing::Double;
987 } else if (tmp == "other") {
988 new_spacing = Spacing::Other;
989 string tmpval = "0.0";
991 lyxerr << "new_value = " << tmpval << endl;
994 } else if (tmp == "default") {
995 new_spacing = Spacing::Default;
997 lyxerr << to_utf8(_("Unknown spacing argument: "))
998 << to_utf8(cmd.argument()) << endl;
1000 if (cur_spacing != new_spacing || cur_value != new_value)
1001 par.params().spacing(Spacing(new_spacing, new_value));
1005 case LFUN_INSET_INSERT: {
1008 // We have to avoid triggering InstantPreview loading
1009 // before inserting into the document. See bug #5626.
1010 bool loaded = bv->buffer().isFullyLoaded();
1011 bv->buffer().setFullyLoaded(false);
1012 Inset * inset = createInset(bv->buffer(), cmd);
1013 bv->buffer().setFullyLoaded(loaded);
1016 // FIXME (Abdel 01/02/2006):
1017 // What follows would be a partial fix for bug 2154:
1018 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
1019 // This automatically put the label inset _after_ a
1020 // numbered section. It should be possible to extend the mechanism
1021 // to any kind of LateX environement.
1022 // The correct way to fix that bug would be at LateX generation.
1023 // I'll let the code here for reference as it could be used for some
1024 // other feature like "automatic labelling".
1026 Paragraph & par = pars_[cur.pit()];
1027 if (inset->lyxCode() == LABEL_CODE
1028 && par.layout().labeltype == LABEL_COUNTER) {
1029 // Go to the end of the paragraph
1030 // Warning: Because of Change-Tracking, the last
1031 // position is 'size()' and not 'size()-1':
1032 cur.pos() = par.size();
1033 // Insert a new paragraph
1034 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1038 if (cur.selection())
1039 cutSelection(cur, true, false);
1043 // trigger InstantPreview now
1044 if (inset->lyxCode() == EXTERNAL_CODE) {
1045 InsetExternal & ins =
1046 static_cast<InsetExternal &>(*inset);
1047 ins.updatePreview();
1054 case LFUN_INSET_DISSOLVE: {
1055 // first, try if there's an inset at cursor
1056 // FIXME: this first part should be moved to
1057 // a LFUN_NEXT_INSET_DISSOLVE, or be called via
1058 // some generic "next-inset inset-dissolve"
1059 Inset * inset = cur.nextInset();
1060 if (inset && inset->isActive()) {
1061 Cursor tmpcur = cur;
1062 tmpcur.pushBackward(*inset);
1063 inset->dispatch(tmpcur, cmd);
1064 if (tmpcur.result().dispatched()) {
1069 // if it did not work, try the underlying inset
1070 if (dissolveInset(cur)) {
1074 // if it did not work, do nothing.
1078 case LFUN_SET_GRAPHICS_GROUP: {
1079 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1085 string id = to_utf8(cmd.argument());
1086 string grp = graphics::getGroupParams(bv->buffer(), id);
1087 InsetGraphicsParams tmp, inspar = ins->getParams();
1090 inspar.groupId = to_utf8(cmd.argument());
1092 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1093 tmp.filename = inspar.filename;
1097 ins->setParams(inspar);
1100 case LFUN_SPACE_INSERT:
1101 if (cur.paragraph().layout().free_spacing)
1102 insertChar(cur, ' ');
1104 doInsertInset(cur, this, cmd, false, false);
1107 moveCursor(cur, false);
1110 case LFUN_SPECIALCHAR_INSERT: {
1111 string const name = to_utf8(cmd.argument());
1112 if (name == "hyphenation")
1113 specialChar(cur, InsetSpecialChar::HYPHENATION);
1114 else if (name == "ligature-break")
1115 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1116 else if (name == "slash")
1117 specialChar(cur, InsetSpecialChar::SLASH);
1118 else if (name == "nobreakdash")
1119 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1120 else if (name == "dots")
1121 specialChar(cur, InsetSpecialChar::LDOTS);
1122 else if (name == "end-of-sentence")
1123 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1124 else if (name == "menu-separator")
1125 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1126 else if (name.empty())
1127 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1129 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1133 case LFUN_WORD_UPCASE:
1134 changeCase(cur, text_uppercase);
1137 case LFUN_WORD_LOWCASE:
1138 changeCase(cur, text_lowercase);
1141 case LFUN_WORD_CAPITALIZE:
1142 changeCase(cur, text_capitalization);
1145 case LFUN_CHARS_TRANSPOSE:
1146 charsTranspose(cur);
1150 cur.message(_("Paste"));
1151 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1152 cap::replaceSelection(cur);
1154 // without argument?
1155 string const arg = to_utf8(cmd.argument());
1157 if (theClipboard().isInternal())
1158 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1159 else if (theClipboard().hasGraphicsContents()
1160 && !theClipboard().hasTextContents())
1161 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1163 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1164 } else if (isStrUnsignedInt(arg)) {
1165 // we have a numerical argument
1166 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1167 convert<unsigned int>(arg));
1169 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1171 type = Clipboard::PdfGraphicsType;
1172 else if (arg == "png")
1173 type = Clipboard::PngGraphicsType;
1174 else if (arg == "jpeg")
1175 type = Clipboard::JpegGraphicsType;
1176 else if (arg == "linkback")
1177 type = Clipboard::LinkBackGraphicsType;
1178 else if (arg == "emf")
1179 type = Clipboard::EmfGraphicsType;
1180 else if (arg == "wmf")
1181 type = Clipboard::WmfGraphicsType;
1184 LASSERT(false, /**/);
1186 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1189 bv->buffer().errors("Paste");
1190 cur.clearSelection(); // bug 393
1196 cutSelection(cur, true, true);
1197 cur.message(_("Cut"));
1202 cur.message(_("Copy"));
1205 case LFUN_SERVER_GET_XY:
1206 cur.message(from_utf8(
1207 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1208 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1211 case LFUN_SERVER_SET_XY: {
1214 istringstream is(to_utf8(cmd.argument()));
1217 lyxerr << "SETXY: Could not parse coordinates in '"
1218 << to_utf8(cmd.argument()) << endl;
1220 tm->setCursorFromCoordinates(cur, x, y);
1224 case LFUN_SERVER_GET_LAYOUT:
1225 cur.message(cur.paragraph().layout().name());
1229 docstring layout = cmd.argument();
1230 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1232 Paragraph const & para = cur.paragraph();
1233 docstring const old_layout = para.layout().name();
1234 DocumentClass const & tclass = bv->buffer().params().documentClass();
1237 layout = tclass.defaultLayoutName();
1239 if (para.forcePlainLayout())
1240 // in this case only the empty layout is allowed
1241 layout = tclass.plainLayoutName();
1242 else if (para.usePlainLayout()) {
1243 // in this case, default layout maps to empty layout
1244 if (layout == tclass.defaultLayoutName())
1245 layout = tclass.plainLayoutName();
1247 // otherwise, the empty layout maps to the default
1248 if (layout == tclass.plainLayoutName())
1249 layout = tclass.defaultLayoutName();
1252 bool hasLayout = tclass.hasLayout(layout);
1254 // If the entry is obsolete, use the new one instead.
1256 docstring const & obs = tclass[layout].obsoleted_by();
1262 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1263 from_utf8(N_(" not known")));
1267 bool change_layout = (old_layout != layout);
1269 if (!change_layout && cur.selection() &&
1270 cur.selBegin().pit() != cur.selEnd().pit())
1272 pit_type spit = cur.selBegin().pit();
1273 pit_type epit = cur.selEnd().pit() + 1;
1274 while (spit != epit) {
1275 if (pars_[spit].layout().name() != old_layout) {
1276 change_layout = true;
1284 setLayout(cur, layout);
1289 case LFUN_CLIPBOARD_PASTE:
1290 cur.clearSelection();
1291 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1292 cmd.argument() == "paragraph");
1293 bv->buffer().errors("Paste");
1296 case LFUN_PRIMARY_SELECTION_PASTE:
1297 pasteString(cur, theSelection().get(),
1298 cmd.argument() == "paragraph");
1301 case LFUN_SELECTION_PASTE:
1302 // Copy the selection buffer to the clipboard stack,
1303 // because we want it to appear in the "Edit->Paste
1305 cap::copySelectionToStack();
1306 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1307 bv->buffer().errors("Paste");
1310 case LFUN_UNICODE_INSERT: {
1311 if (cmd.argument().empty())
1313 docstring hexstring = cmd.argument();
1314 if (isHex(hexstring)) {
1315 char_type c = hexToInt(hexstring);
1316 if (c >= 32 && c < 0x10ffff) {
1317 lyxerr << "Inserting c: " << c << endl;
1318 docstring s = docstring(1, c);
1319 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1325 case LFUN_QUOTE_INSERT: {
1326 Paragraph & par = cur.paragraph();
1327 pos_type pos = cur.pos();
1328 BufferParams const & bufparams = bv->buffer().params();
1329 Layout const & style = par.layout();
1330 InsetLayout const & ilayout = cur.inset().getLayout(bufparams);
1331 if (!style.pass_thru && !ilayout.isPassThru()
1332 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1333 // this avoids a double undo
1334 // FIXME: should not be needed, ideally
1335 if (!cur.selection())
1337 cap::replaceSelection(cur);
1342 else if (cur.prevInset() && cur.prevInset()->isSpace())
1345 c = par.getChar(pos - 1);
1346 string arg = to_utf8(cmd.argument());
1347 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1348 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1352 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1356 case LFUN_DATE_INSERT: {
1357 string const format = cmd.argument().empty()
1358 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1359 string const time = formatted_time(current_time(), format);
1360 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1364 case LFUN_MOUSE_TRIPLE:
1365 if (cmd.button() == mouse_button::button1) {
1366 tm->cursorHome(cur);
1374 case LFUN_MOUSE_DOUBLE:
1375 if (cmd.button() == mouse_button::button1) {
1376 selectWord(cur, WHOLE_WORD_STRICT);
1381 // Single-click on work area
1382 case LFUN_MOUSE_PRESS:
1383 // We are not marking a selection with the keyboard in any case.
1384 cur.bv().cursor().setMark(false);
1385 switch (cmd.button()) {
1386 case mouse_button::button1:
1388 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1389 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1392 case mouse_button::button2:
1393 // Middle mouse pasting.
1394 bv->mouseSetCursor(cur);
1396 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1397 "selection-paste ; primary-selection-paste paragraph"));
1401 case mouse_button::button3: {
1402 Cursor const & bvcur = cur.bv().cursor();
1403 // Don't do anything if we right-click a
1404 // selection, a context menu will popup.
1405 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1406 && cur < bvcur.selectionEnd()) {
1410 if (!bv->mouseSetCursor(cur, false))
1411 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1417 } // switch (cmd.button())
1420 case LFUN_MOUSE_MOTION: {
1421 // Mouse motion with right or middle mouse do nothing for now.
1422 if (cmd.button() != mouse_button::button1) {
1426 // ignore motions deeper nested than the real anchor
1427 Cursor & bvcur = cur.bv().cursor();
1428 if (!bvcur.anchor_.hasPart(cur)) {
1432 CursorSlice old = bvcur.top();
1434 int const wh = bv->workHeight();
1435 int const y = max(0, min(wh - 1, cmd.y));
1437 tm->setCursorFromCoordinates(cur, cmd.x, y);
1438 cur.setTargetX(cmd.x);
1440 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1442 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1443 // This is to allow jumping over large insets
1444 if (cur.top() == old) {
1446 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1448 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1450 // We continue with our existing selection or start a new one, so don't
1451 // reset the anchor.
1452 bvcur.setCursor(cur);
1453 bvcur.setSelection(true);
1454 if (cur.top() == old) {
1455 // We didn't move one iota, so no need to update the screen.
1456 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1463 case LFUN_MOUSE_RELEASE:
1464 switch (cmd.button()) {
1465 case mouse_button::button1:
1466 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1467 // If there is a new selection, update persistent selection;
1468 // otherwise, single click does not clear persistent selection
1470 if (cur.selection()) {
1471 // Finish selection. If double click,
1472 // cur is moved to the end of word by
1473 // selectWord but bvcur is current
1475 cur.bv().cursor().setSelection();
1476 // We might have removed an empty but drawn selection
1477 // (probably a margin)
1478 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1481 // FIXME: We could try to handle drag and drop of selection here.
1484 case mouse_button::button2:
1485 // Middle mouse pasting is handled at mouse press time,
1486 // see LFUN_MOUSE_PRESS.
1490 case mouse_button::button3:
1491 // Cursor was set at LFUN_MOUSE_PRESS time.
1492 // FIXME: If there is a selection we could try to handle a special
1493 // drag & drop context menu.
1497 case mouse_button::none:
1498 case mouse_button::button4:
1499 case mouse_button::button5:
1501 } // switch (cmd.button())
1505 case LFUN_SELF_INSERT: {
1506 if (cmd.argument().empty())
1509 // Automatically delete the currently selected
1510 // text and replace it with what is being
1511 // typed in now. Depends on lyxrc settings
1512 // "auto_region_delete", which defaults to
1515 if (lyxrc.auto_region_delete && cur.selection())
1516 cutSelection(cur, false, false);
1518 cur.clearSelection();
1520 docstring::const_iterator cit = cmd.argument().begin();
1521 docstring::const_iterator const end = cmd.argument().end();
1522 for (; cit != end; ++cit)
1523 bv->translateAndInsert(*cit, this, cur);
1526 moveCursor(cur, false);
1527 bv->bookmarkEditPosition();
1531 case LFUN_HYPERLINK_INSERT: {
1532 InsetCommandParams p(HYPERLINK_CODE);
1534 if (cur.selection()) {
1535 content = cur.selectionAsString(false);
1536 cutSelection(cur, true, false);
1538 p["target"] = (cmd.argument().empty()) ?
1539 content : cmd.argument();
1540 string const data = InsetCommand::params2string("href", p);
1541 if (p["target"].empty()) {
1542 bv->showDialog("href", data);
1544 FuncRequest fr(LFUN_INSET_INSERT, data);
1550 case LFUN_LABEL_INSERT: {
1551 InsetCommandParams p(LABEL_CODE);
1552 // Try to generate a valid label
1553 p["name"] = (cmd.argument().empty()) ?
1554 cur.getPossibleLabel() :
1556 string const data = InsetCommand::params2string("label", p);
1558 if (cmd.argument().empty()) {
1559 bv->showDialog("label", data);
1561 FuncRequest fr(LFUN_INSET_INSERT, data);
1567 case LFUN_INFO_INSERT: {
1569 if (cmd.argument().empty() && cur.selection()) {
1570 // if command argument is empty use current selection as parameter.
1571 docstring ds = cur.selectionAsString(false);
1572 cutSelection(cur, true, false);
1573 FuncRequest cmd0(cmd, ds);
1574 inset = createInset(cur.bv().buffer(), cmd0);
1576 inset = createInset(cur.bv().buffer(), cmd);
1581 insertInset(cur, inset);
1585 case LFUN_CAPTION_INSERT:
1586 case LFUN_FOOTNOTE_INSERT:
1587 case LFUN_NOTE_INSERT:
1588 case LFUN_FLEX_INSERT:
1589 case LFUN_BOX_INSERT:
1590 case LFUN_BRANCH_INSERT:
1591 case LFUN_PHANTOM_INSERT:
1592 case LFUN_ERT_INSERT:
1593 case LFUN_LISTING_INSERT:
1594 case LFUN_MARGINALNOTE_INSERT:
1595 case LFUN_OPTIONAL_INSERT:
1596 case LFUN_INDEX_INSERT:
1597 // Open the inset, and move the current selection
1599 doInsertInset(cur, this, cmd, true, true);
1601 // Some insets are numbered, others are shown in the outline pane so
1602 // let's update the labels and the toc backend.
1603 bv->buffer().updateLabels();
1606 case LFUN_TABULAR_INSERT:
1607 // if there were no arguments, just open the dialog
1608 if (doInsertInset(cur, this, cmd, false, true))
1611 bv->showDialog("tabularcreate");
1615 case LFUN_FLOAT_INSERT:
1616 case LFUN_FLOAT_WIDE_INSERT:
1617 case LFUN_WRAP_INSERT: {
1618 // will some text be moved into the inset?
1619 bool content = cur.selection();
1621 doInsertInset(cur, this, cmd, true, true);
1624 // If some text is moved into the inset, doInsertInset
1625 // puts the cursor outside the inset. To insert the
1626 // caption we put it back into the inset.
1630 ParagraphList & pars = cur.text()->paragraphs();
1632 DocumentClass const & tclass = bv->buffer().params().documentClass();
1634 // add a separate paragraph for the caption inset
1635 pars.push_back(Paragraph());
1636 pars.back().setInsetOwner(&pars[0].inInset());
1637 pars.back().setPlainOrDefaultLayout(tclass);
1638 int cap_pit = pars.size() - 1;
1640 // if an empty inset was created, we create an additional empty
1641 // paragraph at the bottom so that the user can choose where to put
1642 // the graphics (or table).
1644 pars.push_back(Paragraph());
1645 pars.back().setInsetOwner(&pars[0].inInset());
1646 pars.back().setPlainOrDefaultLayout(tclass);
1649 // reposition the cursor to the caption
1650 cur.pit() = cap_pit;
1652 // FIXME: This Text/Cursor dispatch handling is a mess!
1653 // We cannot use Cursor::dispatch here it needs access to up to
1655 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1656 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1657 bv->buffer().updateLabels();
1658 cur.updateFlags(Update::Force);
1659 // FIXME: When leaving the Float (or Wrap) inset we should
1660 // delete any empty paragraph left above or below the
1665 case LFUN_NOMENCL_INSERT: {
1666 InsetCommandParams p(NOMENCL_CODE);
1667 if (cmd.argument().empty())
1668 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1670 p["symbol"] = cmd.argument();
1671 string const data = InsetCommand::params2string("nomenclature", p);
1672 bv->showDialog("nomenclature", data);
1676 case LFUN_INDEX_PRINT: {
1677 InsetCommandParams p(INDEX_PRINT_CODE);
1678 if (cmd.argument().empty())
1679 p["type"] = from_ascii("idx");
1681 p["type"] = cmd.argument();
1682 string const data = InsetCommand::params2string("index_print", p);
1683 FuncRequest fr(LFUN_INSET_INSERT, data);
1688 case LFUN_NOMENCL_PRINT:
1689 case LFUN_TOC_INSERT:
1690 case LFUN_LINE_INSERT:
1691 case LFUN_NEWPAGE_INSERT:
1693 doInsertInset(cur, this, cmd, false, false);
1697 case LFUN_DEPTH_DECREMENT:
1698 changeDepth(cur, DEC_DEPTH);
1701 case LFUN_DEPTH_INCREMENT:
1702 changeDepth(cur, INC_DEPTH);
1705 case LFUN_MATH_DISPLAY:
1706 mathDispatch(cur, cmd, true);
1709 case LFUN_REGEXP_MODE:
1710 regexpDispatch(cur, cmd);
1713 case LFUN_MATH_MODE:
1714 if (cmd.argument() == "on")
1715 // don't pass "on" as argument
1716 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1718 mathDispatch(cur, cmd, false);
1721 case LFUN_MATH_MACRO:
1722 if (cmd.argument().empty())
1723 cur.errorMessage(from_utf8(N_("Missing argument")));
1725 string s = to_utf8(cmd.argument());
1726 string const s1 = token(s, ' ', 1);
1727 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1728 string const s2 = token(s, ' ', 2);
1729 MacroType type = MacroTypeNewcommand;
1731 type = MacroTypeDef;
1732 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1733 inset->setBuffer(bv->buffer());
1734 insertInset(cur, inset);
1736 // enter macro inset and select the name
1738 cur.top().pos() = cur.top().lastpos();
1740 cur.setSelection(true);
1741 cur.top().pos() = 0;
1745 // passthrough hat and underscore outside mathed:
1746 case LFUN_MATH_SUBSCRIPT:
1747 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1749 case LFUN_MATH_SUPERSCRIPT:
1750 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1753 case LFUN_MATH_INSERT:
1754 case LFUN_MATH_AMS_MATRIX:
1755 case LFUN_MATH_MATRIX:
1756 case LFUN_MATH_DELIM:
1757 case LFUN_MATH_BIGDELIM: {
1759 cap::replaceSelection(cur);
1760 cur.insert(new InsetMathHull(hullSimple));
1761 checkAndActivateInset(cur, true);
1762 LASSERT(cur.inMathed(), /**/);
1767 case LFUN_FONT_EMPH: {
1768 Font font(ignore_font, ignore_language);
1769 font.fontInfo().setEmph(FONT_TOGGLE);
1770 toggleAndShow(cur, this, font);
1774 case LFUN_FONT_ITAL: {
1775 Font font(ignore_font, ignore_language);
1776 font.fontInfo().setShape(ITALIC_SHAPE);
1777 toggleAndShow(cur, this, font);
1781 case LFUN_FONT_BOLD:
1782 case LFUN_FONT_BOLDSYMBOL: {
1783 Font font(ignore_font, ignore_language);
1784 font.fontInfo().setSeries(BOLD_SERIES);
1785 toggleAndShow(cur, this, font);
1789 case LFUN_FONT_NOUN: {
1790 Font font(ignore_font, ignore_language);
1791 font.fontInfo().setNoun(FONT_TOGGLE);
1792 toggleAndShow(cur, this, font);
1796 case LFUN_FONT_TYPEWRITER: {
1797 Font font(ignore_font, ignore_language);
1798 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1799 toggleAndShow(cur, this, font);
1803 case LFUN_FONT_SANS: {
1804 Font font(ignore_font, ignore_language);
1805 font.fontInfo().setFamily(SANS_FAMILY);
1806 toggleAndShow(cur, this, font);
1810 case LFUN_FONT_ROMAN: {
1811 Font font(ignore_font, ignore_language);
1812 font.fontInfo().setFamily(ROMAN_FAMILY);
1813 toggleAndShow(cur, this, font);
1817 case LFUN_FONT_DEFAULT: {
1818 Font font(inherit_font, ignore_language);
1819 toggleAndShow(cur, this, font);
1823 case LFUN_FONT_STRIKEOUT: {
1824 Font font(ignore_font, ignore_language);
1825 font.fontInfo().setStrikeout(FONT_TOGGLE);
1826 toggleAndShow(cur, this, font);
1830 case LFUN_FONT_UULINE: {
1831 Font font(ignore_font, ignore_language);
1832 font.fontInfo().setUuline(FONT_TOGGLE);
1833 toggleAndShow(cur, this, font);
1837 case LFUN_FONT_UWAVE: {
1838 Font font(ignore_font, ignore_language);
1839 font.fontInfo().setUwave(FONT_TOGGLE);
1840 toggleAndShow(cur, this, font);
1844 case LFUN_FONT_UNDERLINE: {
1845 Font font(ignore_font, ignore_language);
1846 font.fontInfo().setUnderbar(FONT_TOGGLE);
1847 toggleAndShow(cur, this, font);
1851 case LFUN_FONT_SIZE: {
1852 Font font(ignore_font, ignore_language);
1853 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1854 toggleAndShow(cur, this, font);
1858 case LFUN_LANGUAGE: {
1859 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1862 Font font(ignore_font, lang);
1863 toggleAndShow(cur, this, font);
1867 case LFUN_TEXTSTYLE_APPLY:
1868 toggleAndShow(cur, this, freefont, toggleall);
1869 cur.message(_("Character set"));
1872 // Set the freefont using the contents of \param data dispatched from
1873 // the frontends and apply it at the current cursor location.
1874 case LFUN_TEXTSTYLE_UPDATE: {
1877 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1880 toggleAndShow(cur, this, freefont, toggleall);
1881 cur.message(_("Character set"));
1883 lyxerr << "Argument not ok";
1888 case LFUN_FINISHED_LEFT:
1889 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1890 // We're leaving an inset, going left. If the inset is LTR, we're
1891 // leaving from the front, so we should not move (remain at --- but
1892 // not in --- the inset). If the inset is RTL, move left, without
1893 // entering the inset itself; i.e., move to after the inset.
1894 if (cur.paragraph().getFontSettings(
1895 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1896 cursorVisLeft(cur, true);
1899 case LFUN_FINISHED_RIGHT:
1900 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1901 // We're leaving an inset, going right. If the inset is RTL, we're
1902 // leaving from the front, so we should not move (remain at --- but
1903 // not in --- the inset). If the inset is LTR, move right, without
1904 // entering the inset itself; i.e., move to after the inset.
1905 if (!cur.paragraph().getFontSettings(
1906 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1907 cursorVisRight(cur, true);
1910 case LFUN_FINISHED_BACKWARD:
1911 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1914 case LFUN_FINISHED_FORWARD:
1915 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1917 cur.setCurrentFont();
1920 case LFUN_LAYOUT_PARAGRAPH: {
1922 params2string(cur.paragraph(), data);
1923 data = "show\n" + data;
1924 bv->showDialog("paragraph", data);
1928 case LFUN_PARAGRAPH_UPDATE: {
1930 params2string(cur.paragraph(), data);
1932 // Will the paragraph accept changes from the dialog?
1934 cur.inset().allowParagraphCustomization(cur.idx());
1936 data = "update " + convert<string>(accept) + '\n' + data;
1937 bv->updateDialog("paragraph", data);
1941 case LFUN_ACCENT_UMLAUT:
1942 case LFUN_ACCENT_CIRCUMFLEX:
1943 case LFUN_ACCENT_GRAVE:
1944 case LFUN_ACCENT_ACUTE:
1945 case LFUN_ACCENT_TILDE:
1946 case LFUN_ACCENT_CEDILLA:
1947 case LFUN_ACCENT_MACRON:
1948 case LFUN_ACCENT_DOT:
1949 case LFUN_ACCENT_UNDERDOT:
1950 case LFUN_ACCENT_UNDERBAR:
1951 case LFUN_ACCENT_CARON:
1952 case LFUN_ACCENT_BREVE:
1953 case LFUN_ACCENT_TIE:
1954 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1955 case LFUN_ACCENT_CIRCLE:
1956 case LFUN_ACCENT_OGONEK:
1957 theLyXFunc().handleKeyFunc(cmd.action);
1958 if (!cmd.argument().empty())
1959 // FIXME: Are all these characters encoded in one byte in utf8?
1960 bv->translateAndInsert(cmd.argument()[0], this, cur);
1963 case LFUN_FLOAT_LIST_INSERT: {
1964 DocumentClass const & tclass = bv->buffer().params().documentClass();
1965 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1967 if (cur.selection())
1968 cutSelection(cur, true, false);
1969 breakParagraph(cur);
1971 if (cur.lastpos() != 0) {
1972 cursorBackward(cur);
1973 breakParagraph(cur);
1976 docstring const laystr = cur.inset().usePlainLayout() ?
1977 tclass.plainLayoutName() :
1978 tclass.defaultLayoutName();
1979 setLayout(cur, laystr);
1980 ParagraphParameters p;
1981 // FIXME If this call were replaced with one to clearParagraphParams(),
1982 // then we could get rid of this method altogether.
1983 setParagraphs(cur, p);
1984 // FIXME This should be simplified when InsetFloatList takes a
1985 // Buffer in its constructor.
1986 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1987 ifl->setBuffer(bv->buffer());
1988 insertInset(cur, ifl);
1991 lyxerr << "Non-existent float type: "
1992 << to_utf8(cmd.argument()) << endl;
1997 case LFUN_CHANGE_ACCEPT: {
1998 acceptOrRejectChanges(cur, ACCEPT);
2002 case LFUN_CHANGE_REJECT: {
2003 acceptOrRejectChanges(cur, REJECT);
2007 case LFUN_THESAURUS_ENTRY: {
2008 docstring arg = cmd.argument();
2010 arg = cur.selectionAsString(false);
2012 if (arg.size() > 100 || arg.empty()) {
2013 // Get word or selection
2014 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2015 arg = cur.selectionAsString(false);
2016 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2019 bv->showDialog("thesaurus", to_utf8(arg));
2023 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2024 // Given data, an encoding of the ParagraphParameters
2025 // generated in the Paragraph dialog, this function sets
2026 // the current paragraph, or currently selected paragraphs,
2028 // NOTE: This function overrides all existing settings.
2029 setParagraphs(cur, cmd.argument());
2030 cur.message(_("Paragraph layout set"));
2034 case LFUN_PARAGRAPH_PARAMS: {
2035 // Given data, an encoding of the ParagraphParameters as we'd
2036 // find them in a LyX file, this function modifies the current paragraph,
2037 // or currently selected paragraphs.
2038 // NOTE: This function only modifies, and does not override, existing
2040 setParagraphs(cur, cmd.argument(), true);
2041 cur.message(_("Paragraph layout set"));
2046 if (cur.selection()) {
2047 cur.setSelection(false);
2050 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2051 // correct, but I'm not 100% sure -- dov, 071019
2052 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2056 case LFUN_OUTLINE_UP:
2057 outline(OutlineUp, cur);
2058 setCursor(cur, cur.pit(), 0);
2059 cur.buffer()->updateLabels();
2063 case LFUN_OUTLINE_DOWN:
2064 outline(OutlineDown, cur);
2065 setCursor(cur, cur.pit(), 0);
2066 cur.buffer()->updateLabels();
2070 case LFUN_OUTLINE_IN:
2071 outline(OutlineIn, cur);
2072 cur.buffer()->updateLabels();
2076 case LFUN_OUTLINE_OUT:
2077 outline(OutlineOut, cur);
2078 cur.buffer()->updateLabels();
2082 case LFUN_OUTLINE_DRAGMOVE: {
2083 int const move_id = convert<int>(cmd.getArg(0));
2084 int const move_to_id = convert<int>(cmd.getArg(1));
2085 dragMove(cur, move_id, move_to_id);
2086 setCursor(cur, cur.pit(), 0);
2087 cur.buffer()->updateLabels();
2093 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2098 if (lyxrc.spellcheck_continuously && cur.inTexted())
2099 // Take this opportunity to spellcheck current word.
2100 cur.paragraph().isMisspelled(cur.pos());
2102 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2104 // FIXME: The cursor flag is reset two lines below
2105 // so we need to check here if some of the LFUN did touch that.
2106 // for now only Text::erase() and Text::backspace() do that.
2107 // The plan is to verify all the LFUNs and then to remove this
2108 // singleParUpdate boolean altogether.
2109 if (cur.result().update() & Update::Force) {
2110 singleParUpdate = false;
2114 // FIXME: the following code should go in favor of fine grained
2115 // update flag treatment.
2116 if (singleParUpdate) {
2117 // Inserting characters does not change par height in general. So, try
2118 // to update _only_ this paragraph. BufferView will detect if a full
2119 // metrics update is needed anyway.
2120 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2125 && &oldTopSlice.inset() == &cur.inset()
2126 && oldTopSlice.idx() == cur.idx()
2127 && !sel // sel is a backup of cur.selection() at the biginning of the function.
2128 && !cur.selection())
2129 // FIXME: it would be better if we could just do this
2131 //if (cur.result().update() != Update::FitCursor)
2134 // But some LFUNs do not set Update::FitCursor when needed, so we
2135 // do it for all. This is not very harmfull as FitCursor will provoke
2136 // a full redraw only if needed but still, a proper review of all LFUN
2137 // should be done and this needsUpdate boolean can then be removed.
2138 cur.updateFlags(Update::FitCursor);
2140 cur.updateFlags(Update::Force | Update::FitCursor);
2144 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2145 FuncStatus & flag) const
2147 LASSERT(cur.text() == this, /**/);
2149 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2151 InsetCode code = NO_CODE;
2153 switch (cmd.action) {
2155 case LFUN_DEPTH_DECREMENT:
2156 enable = changeDepthAllowed(cur, DEC_DEPTH);
2159 case LFUN_DEPTH_INCREMENT:
2160 enable = changeDepthAllowed(cur, INC_DEPTH);
2164 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2167 case LFUN_DIALOG_SHOW_NEW_INSET:
2168 if (cmd.argument() == "bibitem")
2169 code = BIBITEM_CODE;
2170 else if (cmd.argument() == "bibtex") {
2172 // not allowed in description items
2173 enable = !inDescriptionItem(cur);
2175 else if (cmd.argument() == "box")
2177 else if (cmd.argument() == "branch")
2179 else if (cmd.argument() == "citation")
2181 else if (cmd.argument() == "ert")
2183 else if (cmd.argument() == "external")
2184 code = EXTERNAL_CODE;
2185 else if (cmd.argument() == "float")
2187 else if (cmd.argument() == "graphics")
2188 code = GRAPHICS_CODE;
2189 else if (cmd.argument() == "href")
2190 code = HYPERLINK_CODE;
2191 else if (cmd.argument() == "include")
2192 code = INCLUDE_CODE;
2193 else if (cmd.argument() == "index")
2195 else if (cmd.argument() == "index_print")
2196 code = INDEX_PRINT_CODE;
2197 else if (cmd.argument() == "nomenclature")
2198 code = NOMENCL_CODE;
2199 else if (cmd.argument() == "nomencl_print")
2200 code = NOMENCL_PRINT_CODE;
2201 else if (cmd.argument() == "label")
2203 else if (cmd.argument() == "note")
2205 else if (cmd.argument() == "phantom")
2206 code = PHANTOM_CODE;
2207 else if (cmd.argument() == "ref")
2209 else if (cmd.argument() == "space")
2211 else if (cmd.argument() == "toc")
2213 else if (cmd.argument() == "vspace")
2215 else if (cmd.argument() == "wrap")
2217 else if (cmd.argument() == "listings")
2218 code = LISTINGS_CODE;
2221 case LFUN_ERT_INSERT:
2224 case LFUN_LISTING_INSERT:
2225 code = LISTINGS_CODE;
2226 // not allowed in description items
2227 enable = !inDescriptionItem(cur);
2229 case LFUN_FOOTNOTE_INSERT:
2232 case LFUN_TABULAR_INSERT:
2233 code = TABULAR_CODE;
2235 case LFUN_MARGINALNOTE_INSERT:
2238 case LFUN_FLOAT_INSERT:
2239 case LFUN_FLOAT_WIDE_INSERT:
2241 // not allowed in description items
2242 enable = !inDescriptionItem(cur);
2244 case LFUN_WRAP_INSERT:
2246 // not allowed in description items
2247 enable = !inDescriptionItem(cur);
2249 case LFUN_FLOAT_LIST_INSERT:
2250 code = FLOAT_LIST_CODE;
2251 // not allowed in description items
2252 enable = !inDescriptionItem(cur);
2254 case LFUN_CAPTION_INSERT:
2255 code = CAPTION_CODE;
2256 // not allowed in description items
2257 enable = !inDescriptionItem(cur);
2259 case LFUN_NOTE_INSERT:
2261 // in commands (sections etc.) and description items,
2262 // only Notes are allowed
2263 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2264 (!cur.paragraph().layout().isCommand()
2265 && !inDescriptionItem(cur)));
2267 case LFUN_FLEX_INSERT: {
2269 string s = cmd.getArg(0);
2271 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2272 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2273 il.lyxtype() != InsetLayout::CUSTOM &&
2274 il.lyxtype() != InsetLayout::ELEMENT &&
2275 il.lyxtype ()!= InsetLayout::STANDARD)
2279 case LFUN_BOX_INSERT:
2282 case LFUN_BRANCH_INSERT:
2284 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2285 && cur.buffer()->params().branchlist().empty())
2288 case LFUN_PHANTOM_INSERT:
2289 code = PHANTOM_CODE;
2291 case LFUN_LABEL_INSERT:
2294 case LFUN_INFO_INSERT:
2297 case LFUN_OPTIONAL_INSERT:
2299 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2300 < cur.paragraph().layout().optionalargs;
2302 case LFUN_INDEX_INSERT:
2305 case LFUN_INDEX_PRINT:
2306 code = INDEX_PRINT_CODE;
2307 // not allowed in description items
2308 enable = !inDescriptionItem(cur);
2310 case LFUN_NOMENCL_INSERT:
2311 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2315 code = NOMENCL_CODE;
2317 case LFUN_NOMENCL_PRINT:
2318 code = NOMENCL_PRINT_CODE;
2319 // not allowed in description items
2320 enable = !inDescriptionItem(cur);
2322 case LFUN_TOC_INSERT:
2324 // not allowed in description items
2325 enable = !inDescriptionItem(cur);
2327 case LFUN_HYPERLINK_INSERT:
2328 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2332 code = HYPERLINK_CODE;
2334 case LFUN_QUOTE_INSERT:
2335 // always allow this, since we will inset a raw quote
2336 // if an inset is not allowed.
2338 case LFUN_SPECIALCHAR_INSERT:
2339 code = SPECIALCHAR_CODE;
2341 case LFUN_SPACE_INSERT:
2342 // slight hack: we know this is allowed in math mode
2347 case LFUN_INSET_MODIFY:
2348 // We need to disable this, because we may get called for a
2350 // InsetTabular::getStatus() -> InsetText::getStatus()
2351 // and we don't handle LFUN_INSET_MODIFY.
2355 case LFUN_FONT_EMPH:
2356 flag.setOnOff(fontinfo.emph() == FONT_ON);
2359 case LFUN_FONT_ITAL:
2360 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2363 case LFUN_FONT_NOUN:
2364 flag.setOnOff(fontinfo.noun() == FONT_ON);
2367 case LFUN_FONT_BOLD:
2368 case LFUN_FONT_BOLDSYMBOL:
2369 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2372 case LFUN_FONT_SANS:
2373 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2376 case LFUN_FONT_ROMAN:
2377 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2380 case LFUN_FONT_TYPEWRITER:
2381 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2386 enable = cur.selection();
2390 if (cmd.argument().empty()) {
2391 if (theClipboard().isInternal())
2392 enable = cap::numberOfSelections() > 0;
2394 enable = !theClipboard().empty();
2398 // we have an argument
2399 string const arg = to_utf8(cmd.argument());
2400 if (isStrUnsignedInt(arg)) {
2401 // it's a number and therefore means the internal stack
2402 unsigned int n = convert<unsigned int>(arg);
2403 enable = cap::numberOfSelections() > n;
2407 // explicit graphics type?
2408 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2409 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2410 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2411 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2412 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2413 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2414 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2415 enable = theClipboard().hasGraphicsContents(type);
2424 case LFUN_CLIPBOARD_PASTE:
2425 enable = !theClipboard().empty();
2428 case LFUN_PRIMARY_SELECTION_PASTE:
2429 enable = cur.selection() || !theSelection().empty();
2432 case LFUN_SELECTION_PASTE:
2433 enable = cap::selection();
2436 case LFUN_PARAGRAPH_MOVE_UP:
2437 enable = cur.pit() > 0 && !cur.selection();
2440 case LFUN_PARAGRAPH_MOVE_DOWN:
2441 enable = cur.pit() < cur.lastpit() && !cur.selection();
2444 case LFUN_INSET_DISSOLVE:
2445 if (!cmd.argument().empty()) {
2446 InsetLayout const & il = cur.inset().getLayout(cur.buffer()->params());
2447 InsetLayout::InsetLyXType const type =
2448 translateLyXType(to_utf8(cmd.argument()));
2449 enable = cur.inset().lyxCode() == FLEX_CODE
2450 && il.lyxtype() == type;
2452 enable = ((!isMainText(cur.bv().buffer())
2453 && cur.inset().nargs() == 1)
2455 && cur.nextInset()->nargs() == 1));
2459 case LFUN_CHANGE_ACCEPT:
2460 case LFUN_CHANGE_REJECT:
2461 // In principle, these LFUNs should only be enabled if there
2462 // is a change at the current position/in the current selection.
2463 // However, without proper optimizations, this will inevitably
2464 // result in unacceptable performance - just imagine a user who
2465 // wants to select the complete content of a long document.
2466 if (!cur.selection()) {
2467 Change const & change = cur.paragraph().lookupChange(cur.pos());
2468 enable = change.changed();
2470 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2475 case LFUN_OUTLINE_UP:
2476 case LFUN_OUTLINE_DOWN:
2477 case LFUN_OUTLINE_IN:
2478 case LFUN_OUTLINE_OUT:
2479 case LFUN_OUTLINE_DRAGMOVE:
2480 // FIXME: LyX is not ready for outlining within inset.
2481 enable = isMainText(cur.bv().buffer())
2482 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2485 case LFUN_NEWLINE_INSERT:
2486 // LaTeX restrictions (labels or empty par)
2487 enable = (cur.pos() > cur.paragraph().beginOfBody());
2490 case LFUN_SET_GRAPHICS_GROUP: {
2491 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2495 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2499 case LFUN_NEWPAGE_INSERT:
2500 // not allowed in description items
2501 enable = !inDescriptionItem(cur);
2504 case LFUN_MATH_INSERT:
2505 case LFUN_MATH_AMS_MATRIX:
2506 case LFUN_MATH_MATRIX:
2507 case LFUN_MATH_DELIM:
2508 case LFUN_MATH_BIGDELIM:
2509 // not allowed in ERT, for example.
2510 enable = cur.inset().insetAllowed(MATH_CODE);
2513 case LFUN_DATE_INSERT: {
2514 string const format = cmd.argument().empty()
2515 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2516 enable = support::os::is_valid_strftime(format);
2520 case LFUN_WORD_DELETE_FORWARD:
2521 case LFUN_WORD_DELETE_BACKWARD:
2522 case LFUN_LINE_DELETE:
2523 case LFUN_WORD_FORWARD:
2524 case LFUN_WORD_BACKWARD:
2525 case LFUN_WORD_RIGHT:
2526 case LFUN_WORD_LEFT:
2527 case LFUN_CHAR_FORWARD:
2528 case LFUN_CHAR_FORWARD_SELECT:
2529 case LFUN_CHAR_BACKWARD:
2530 case LFUN_CHAR_BACKWARD_SELECT:
2531 case LFUN_CHAR_LEFT:
2532 case LFUN_CHAR_LEFT_SELECT:
2533 case LFUN_CHAR_RIGHT:
2534 case LFUN_CHAR_RIGHT_SELECT:
2536 case LFUN_UP_SELECT:
2538 case LFUN_DOWN_SELECT:
2539 case LFUN_PARAGRAPH_UP_SELECT:
2540 case LFUN_PARAGRAPH_DOWN_SELECT:
2541 case LFUN_LINE_BEGIN_SELECT:
2542 case LFUN_LINE_END_SELECT:
2543 case LFUN_WORD_FORWARD_SELECT:
2544 case LFUN_WORD_BACKWARD_SELECT:
2545 case LFUN_WORD_RIGHT_SELECT:
2546 case LFUN_WORD_LEFT_SELECT:
2547 case LFUN_WORD_SELECT:
2548 case LFUN_SECTION_SELECT:
2549 case LFUN_PARAGRAPH_UP:
2550 case LFUN_PARAGRAPH_DOWN:
2551 case LFUN_LINE_BEGIN:
2553 case LFUN_CHAR_DELETE_FORWARD:
2554 case LFUN_CHAR_DELETE_BACKWARD:
2555 case LFUN_BREAK_PARAGRAPH:
2556 case LFUN_PARAGRAPH_SPACING:
2557 case LFUN_INSET_INSERT:
2558 case LFUN_WORD_UPCASE:
2559 case LFUN_WORD_LOWCASE:
2560 case LFUN_WORD_CAPITALIZE:
2561 case LFUN_CHARS_TRANSPOSE:
2562 case LFUN_SERVER_GET_XY:
2563 case LFUN_SERVER_SET_XY:
2564 case LFUN_SERVER_GET_LAYOUT:
2566 case LFUN_SELF_INSERT:
2567 case LFUN_LINE_INSERT:
2568 case LFUN_MATH_DISPLAY:
2569 case LFUN_MATH_MODE:
2570 case LFUN_MATH_MACRO:
2571 case LFUN_MATH_SUBSCRIPT:
2572 case LFUN_MATH_SUPERSCRIPT:
2573 case LFUN_FONT_DEFAULT:
2574 case LFUN_FONT_UNDERLINE:
2575 case LFUN_FONT_STRIKEOUT:
2576 case LFUN_FONT_UULINE:
2577 case LFUN_FONT_UWAVE:
2578 case LFUN_FONT_SIZE:
2580 case LFUN_TEXTSTYLE_APPLY:
2581 case LFUN_TEXTSTYLE_UPDATE:
2582 case LFUN_LAYOUT_PARAGRAPH:
2583 case LFUN_PARAGRAPH_UPDATE:
2584 case LFUN_ACCENT_UMLAUT:
2585 case LFUN_ACCENT_CIRCUMFLEX:
2586 case LFUN_ACCENT_GRAVE:
2587 case LFUN_ACCENT_ACUTE:
2588 case LFUN_ACCENT_TILDE:
2589 case LFUN_ACCENT_CEDILLA:
2590 case LFUN_ACCENT_MACRON:
2591 case LFUN_ACCENT_DOT:
2592 case LFUN_ACCENT_UNDERDOT:
2593 case LFUN_ACCENT_UNDERBAR:
2594 case LFUN_ACCENT_CARON:
2595 case LFUN_ACCENT_BREVE:
2596 case LFUN_ACCENT_TIE:
2597 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2598 case LFUN_ACCENT_CIRCLE:
2599 case LFUN_ACCENT_OGONEK:
2600 case LFUN_THESAURUS_ENTRY:
2601 case LFUN_PARAGRAPH_PARAMS_APPLY:
2602 case LFUN_PARAGRAPH_PARAMS:
2604 case LFUN_BUFFER_BEGIN:
2605 case LFUN_BUFFER_END:
2606 case LFUN_BUFFER_BEGIN_SELECT:
2607 case LFUN_BUFFER_END_SELECT:
2608 case LFUN_INSET_BEGIN:
2609 case LFUN_INSET_END:
2610 case LFUN_INSET_BEGIN_SELECT:
2611 case LFUN_INSET_END_SELECT:
2612 case LFUN_UNICODE_INSERT:
2613 // these are handled in our dispatch()
2622 && (cur.empty() || !cur.inset().insetAllowed(code)))
2625 flag.setEnabled(enable);
2630 void Text::pasteString(Cursor & cur, docstring const & clip,
2633 cur.clearSelection();
2634 if (!clip.empty()) {
2637 insertStringAsParagraphs(cur, clip);
2639 insertStringAsLines(cur, clip);
2644 // FIXME: an item inset would make things much easier.
2645 bool Text::inDescriptionItem(Cursor & cur) const
2647 Paragraph & par = cur.paragraph();
2648 pos_type const pos = cur.pos();
2649 pos_type const body_pos = par.beginOfBody();
2651 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2652 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2653 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2656 return (pos < body_pos
2658 && (pos == 0 || par.getChar(pos - 1) != ' ')));