3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
35 #include "InsetList.h"
39 #include "LyXAction.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Selection.h"
53 #include "insets/InsetCollapsable.h"
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetExternal.h"
56 #include "insets/InsetFloat.h"
57 #include "insets/InsetFloatList.h"
58 #include "insets/InsetGraphics.h"
59 #include "insets/InsetGraphicsParams.h"
60 #include "insets/InsetNewline.h"
61 #include "insets/InsetQuotes.h"
62 #include "insets/InsetSpecialChar.h"
63 #include "insets/InsetText.h"
64 #include "insets/InsetWrap.h"
66 #include "support/convert.h"
67 #include "support/debug.h"
68 #include "support/gettext.h"
69 #include "support/lstrings.h"
70 #include "support/lyxtime.h"
71 #include "support/os.h"
73 #include "mathed/InsetMathHull.h"
74 #include "mathed/MathMacroTemplate.h"
76 #include <boost/next_prior.hpp>
82 using namespace lyx::support;
86 using cap::copySelection;
87 using cap::cutSelection;
88 using cap::pasteFromStack;
89 using cap::pasteClipboardText;
90 using cap::pasteClipboardGraphics;
91 using cap::replaceSelection;
92 using cap::grabAndEraseSelection;
93 using cap::selClearOrDel;
96 static Font freefont(ignore_font, ignore_language);
97 static bool toggleall = false;
99 static void toggleAndShow(Cursor & cur, Text * text,
100 Font const & font, bool toggleall = true)
102 text->toggleFree(cur, font, toggleall);
104 if (font.language() != ignore_language ||
105 font.fontInfo().number() != FONT_IGNORE) {
106 TextMetrics const & tm = cur.bv().textMetrics(text);
107 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
108 cur.real_current_font))
109 text->setCursor(cur, cur.pit(), cur.pos(),
110 false, !cur.boundary());
115 static void moveCursor(Cursor & cur, bool selecting)
117 if (selecting || cur.mark())
122 static void finishChange(Cursor & cur, bool selecting)
125 moveCursor(cur, selecting);
129 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
132 docstring sel = cur.selectionAsString(false);
134 // It may happen that sel is empty but there is a selection
135 replaceSelection(cur);
137 // Is this a valid formula?
141 #ifdef ENABLE_ASSERTIONS
142 const int old_pos = cur.pos();
144 cur.insert(new InsetMathHull(hullSimple));
145 #ifdef ENABLE_ASSERTIONS
146 LASSERT(old_pos == cur.pos(), /**/);
148 cur.nextInset()->edit(cur, true);
149 // don't do that also for LFUN_MATH_MODE
150 // unless you want end up with always changing
151 // to mathrm when opening an inlined inset --
152 // I really hate "LyXfunc overloading"...
154 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
155 // Avoid an unnecessary undo step if cmd.argument
157 if (!cmd.argument().empty())
158 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
161 // create a macro if we see "\\newcommand"
162 // somewhere, and an ordinary formula
164 if (sel.find(from_ascii("\\newcommand")) == string::npos
165 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
166 && sel.find(from_ascii("\\def")) == string::npos)
168 InsetMathHull * formula = new InsetMathHull;
169 string const selstr = to_utf8(sel);
170 istringstream is(selstr);
173 if (!formula->readQuiet(lex)) {
174 // No valid formula, let's try with delims
175 is.str("$" + selstr + "$");
177 if (!formula->readQuiet(lex)) {
178 // Still not valid, leave it as is
187 cur.insert(new MathMacroTemplate(sel));
191 cur.message(from_utf8(N_("Math editor mode")));
193 cur.message(from_utf8(N_("No valid math formula")));
197 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
199 BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
200 if (cur.inRegexped()) {
201 cur.message(_("Already in regexp mode"));
205 docstring sel = cur.selectionAsString(false);
207 // It may happen that sel is empty but there is a selection
208 replaceSelection(cur);
210 cur.insert(new InsetMathHull(hullRegexp));
211 cur.nextInset()->edit(cur, true);
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->setButtonLabel();
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 cur.text()->insertStringAsLines(cur, ds, cur.current_font);
247 cur.leaveInset(*inset);
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)
392 if (start == pars.begin())
395 ParagraphList::iterator dest = start;
396 // Move out (up) from this header
399 // Search previous same-level header above
402 toclevel = dest->layout().toclevel;
404 && (toclevel == Layout::NOT_IN_TOC
405 || toclevel > thistoclevel));
406 // Not found; do nothing
407 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
409 pit_type const newpit = distance(bgn, dest);
410 pit_type const len = distance(start, finish);
411 pit_type const deletepit = pit + len;
412 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
413 pars.splice(dest, start, finish);
421 // Go one down from *this* header:
422 ParagraphList::iterator dest = boost::next(finish, 1);
423 // Go further down to find header to insert in front of:
424 for (; dest != end; ++dest) {
425 toclevel = dest->layout().toclevel;
426 if (toclevel != Layout::NOT_IN_TOC
427 && toclevel <= thistoclevel)
430 // One such was found:
431 pit_type newpit = distance(bgn, dest);
432 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
433 pit_type const len = distance(start, finish);
434 pars.splice(dest, start, finish);
435 cur.pit() = newpit - len;
439 pit_type const len = distance(start, finish);
440 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
441 for (; start != finish; ++start) {
442 toclevel = start->layout().toclevel;
443 if (toclevel == Layout::NOT_IN_TOC)
445 DocumentClass::const_iterator lit = tc.begin();
446 DocumentClass::const_iterator len = tc.end();
447 for (; lit != len; ++lit) {
448 if (lit->toclevel == toclevel + 1 &&
449 start->layout().labeltype == lit->labeltype) {
450 start->setLayout(*lit);
458 pit_type const len = distance(start, finish);
459 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
460 for (; start != finish; ++start) {
461 toclevel = start->layout().toclevel;
462 if (toclevel == Layout::NOT_IN_TOC)
464 DocumentClass::const_iterator lit = tc.begin();
465 DocumentClass::const_iterator len = tc.end();
466 for (; lit != len; ++lit) {
467 if (lit->toclevel == toclevel - 1 &&
468 start->layout().labeltype == lit->labeltype) {
469 start->setLayout(*lit);
480 void Text::number(Cursor & cur)
482 FontInfo font = ignore_font;
483 font.setNumber(FONT_TOGGLE);
484 toggleAndShow(cur, this, Font(font, ignore_language));
488 bool Text::isRTL(Paragraph const & par) const
490 Buffer const & buffer = owner_->buffer();
491 return par.isRTL(buffer.params());
495 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
497 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
499 // Dispatch if the cursor is inside the text. It is not the
500 // case for context menus (bug 5797).
501 if (cur.text() != this) {
506 BufferView * bv = &cur.bv();
507 TextMetrics * tm = &bv->textMetrics(this);
508 if (!tm->contains(cur.pit())) {
509 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
510 tm = &bv->textMetrics(this);
513 // FIXME: We use the update flag to indicates wether a singlePar or a
514 // full screen update is needed. We reset it here but shall we restore it
518 LASSERT(cur.text() == this, /**/);
519 CursorSlice oldTopSlice = cur.top();
520 bool oldBoundary = cur.boundary();
521 bool sel = cur.selection();
522 // Signals that, even if needsUpdate == false, an update of the
523 // cursor paragraph is required
524 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
525 LyXAction::SingleParUpdate);
526 // Signals that a full-screen update is required
527 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
528 LyXAction::NoUpdate) || singleParUpdate);
530 switch (cmd.action) {
532 case LFUN_PARAGRAPH_MOVE_DOWN: {
533 pit_type const pit = cur.pit();
534 recUndo(cur, pit, pit + 1);
536 pars_.swap(pit, pit + 1);
537 cur.buffer()->updateLabels();
543 case LFUN_PARAGRAPH_MOVE_UP: {
544 pit_type const pit = cur.pit();
545 recUndo(cur, pit - 1, pit);
547 pars_.swap(pit, pit - 1);
548 cur.buffer()->updateLabels();
554 case LFUN_APPENDIX: {
555 Paragraph & par = cur.paragraph();
556 bool start = !par.params().startOfAppendix();
558 // FIXME: The code below only makes sense at top level.
559 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
560 // ensure that we have only one start_of_appendix in this document
561 // FIXME: this don't work for multipart document!
562 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
563 if (pars_[tmp].params().startOfAppendix()) {
565 pars_[tmp].params().startOfAppendix(false);
571 par.params().startOfAppendix(start);
573 // we can set the refreshing parameters now
574 cur.buffer()->updateLabels();
578 case LFUN_WORD_DELETE_FORWARD:
580 cutSelection(cur, true, false);
582 deleteWordForward(cur);
583 finishChange(cur, false);
586 case LFUN_WORD_DELETE_BACKWARD:
588 cutSelection(cur, true, false);
590 deleteWordBackward(cur);
591 finishChange(cur, false);
594 case LFUN_LINE_DELETE:
596 cutSelection(cur, true, false);
598 tm->deleteLineForward(cur);
599 finishChange(cur, false);
602 case LFUN_BUFFER_BEGIN:
603 case LFUN_BUFFER_BEGIN_SELECT:
604 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
605 if (cur.depth() == 1)
606 needsUpdate |= cursorTop(cur);
609 cur.updateFlags(Update::FitCursor);
612 case LFUN_BUFFER_END:
613 case LFUN_BUFFER_END_SELECT:
614 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
615 if (cur.depth() == 1)
616 needsUpdate |= cursorBottom(cur);
619 cur.updateFlags(Update::FitCursor);
622 case LFUN_INSET_BEGIN:
623 case LFUN_INSET_BEGIN_SELECT:
624 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
625 if (cur.depth() == 1 || !cur.top().at_begin())
626 needsUpdate |= cursorTop(cur);
629 cur.updateFlags(Update::FitCursor);
633 case LFUN_INSET_END_SELECT:
634 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
635 if (cur.depth() == 1 || !cur.top().at_end())
636 needsUpdate |= cursorBottom(cur);
639 cur.updateFlags(Update::FitCursor);
642 case LFUN_INSET_SELECT_ALL:
643 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
644 || !cur.selEnd().at_end()) {
645 needsUpdate |= cur.selHandle(false);
646 needsUpdate |= cursorTop(cur);
647 needsUpdate |= cur.selHandle(true);
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_TAB_INSERT: {
924 bool const multi_par_selection = cur.selection() &&
925 cur.selBegin().pit() != cur.selEnd().pit();
926 if (multi_par_selection) {
927 // If there is a multi-paragraph selection, a tab is inserted
928 // at the beginning of each paragraph.
929 cur.recordUndoSelection();
930 pit_type const pit_end = cur.selEnd().pit();
931 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
932 pars_[pit].insertChar(0, '\t',
933 bv->buffer().params().trackChanges);
934 // Update the selection pos to make sure the selection does not
935 // change as the inserted tab will increase the logical pos.
936 if (cur.anchor_.pit() == pit)
937 cur.anchor_.forwardPos();
938 if (cur.pit() == pit)
943 // Maybe we shouldn't allow tabs within a line, because they
944 // are not (yet) aligned as one might do expect.
945 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
951 case LFUN_TAB_DELETE: {
952 bool const tc = bv->buffer().params().trackChanges;
953 if (cur.selection()) {
954 // If there is a selection, a tab (if present) is removed from
955 // the beginning of each paragraph.
956 cur.recordUndoSelection();
957 pit_type const pit_end = cur.selEnd().pit();
958 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
959 Paragraph & par = paragraphs()[pit];
960 if (par.getChar(0) == '\t') {
961 if (cur.pit() == pit)
963 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
964 cur.anchor_.backwardPos();
966 par.eraseChar(0, tc);
968 // If no tab was present, try to remove up to four spaces.
969 for (int n_spaces = 0;
970 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
971 if (cur.pit() == pit)
973 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
974 cur.anchor_.backwardPos();
976 par.eraseChar(0, tc);
981 // If there is no selection, try to remove a tab or some spaces
982 // before the position of the cursor.
983 Paragraph & par = paragraphs()[cur.pit()];
984 pos_type const pos = cur.pos();
989 char_type const c = par.getChar(pos - 1);
993 par.eraseChar(cur.pos(), tc);
995 for (int n_spaces = 0;
997 && par.getChar(cur.pos() - 1) == ' '
1001 par.eraseChar(cur.pos(), tc);
1008 case LFUN_CHAR_DELETE_FORWARD:
1009 if (!cur.selection()) {
1010 if (cur.pos() == cur.paragraph().size())
1011 // Par boundary, force full-screen update
1012 singleParUpdate = false;
1013 needsUpdate |= erase(cur);
1015 // It is possible to make it a lot faster still
1016 // just comment out the line below...
1018 cutSelection(cur, true, false);
1019 singleParUpdate = false;
1021 moveCursor(cur, false);
1024 case LFUN_CHAR_DELETE_BACKWARD:
1025 if (!cur.selection()) {
1026 if (bv->getIntl().getTransManager().backspace()) {
1027 // Par boundary, full-screen update
1029 singleParUpdate = false;
1030 needsUpdate |= backspace(cur);
1032 // It is possible to make it a lot faster still
1033 // just comment out the line below...
1036 cutSelection(cur, true, false);
1037 singleParUpdate = false;
1041 case LFUN_BREAK_PARAGRAPH:
1042 cap::replaceSelection(cur);
1043 breakParagraph(cur, cmd.argument() == "inverse");
1047 case LFUN_INSET_INSERT: {
1050 // We have to avoid triggering InstantPreview loading
1051 // before inserting into the document. See bug #5626.
1052 bool loaded = bv->buffer().isFullyLoaded();
1053 bv->buffer().setFullyLoaded(false);
1054 Inset * inset = createInset(bv->buffer(), cmd);
1055 bv->buffer().setFullyLoaded(loaded);
1058 // FIXME (Abdel 01/02/2006):
1059 // What follows would be a partial fix for bug 2154:
1060 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
1061 // This automatically put the label inset _after_ a
1062 // numbered section. It should be possible to extend the mechanism
1063 // to any kind of LateX environement.
1064 // The correct way to fix that bug would be at LateX generation.
1065 // I'll let the code here for reference as it could be used for some
1066 // other feature like "automatic labelling".
1068 Paragraph & par = pars_[cur.pit()];
1069 if (inset->lyxCode() == LABEL_CODE
1070 && par.layout().labeltype == LABEL_COUNTER) {
1071 // Go to the end of the paragraph
1072 // Warning: Because of Change-Tracking, the last
1073 // position is 'size()' and not 'size()-1':
1074 cur.pos() = par.size();
1075 // Insert a new paragraph
1076 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1080 if (cur.selection())
1081 cutSelection(cur, true, false);
1085 // trigger InstantPreview now
1086 if (inset->lyxCode() == EXTERNAL_CODE) {
1087 InsetExternal & ins =
1088 static_cast<InsetExternal &>(*inset);
1089 ins.updatePreview();
1096 case LFUN_INSET_DISSOLVE: {
1097 // first, try if there's an inset at cursor
1098 // FIXME: this first part should be moved to
1099 // a LFUN_NEXT_INSET_DISSOLVE, or be called via
1100 // some generic "next-inset inset-dissolve"
1101 Inset * inset = cur.nextInset();
1102 if (inset && inset->isActive()) {
1103 Cursor tmpcur = cur;
1104 tmpcur.pushBackward(*inset);
1105 inset->dispatch(tmpcur, cmd);
1106 if (tmpcur.result().dispatched()) {
1111 // if it did not work, try the underlying inset
1112 if (dissolveInset(cur)) {
1116 // if it did not work, do nothing.
1120 case LFUN_SET_GRAPHICS_GROUP: {
1121 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1127 string id = to_utf8(cmd.argument());
1128 string grp = graphics::getGroupParams(bv->buffer(), id);
1129 InsetGraphicsParams tmp, inspar = ins->getParams();
1132 inspar.groupId = to_utf8(cmd.argument());
1134 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1135 tmp.filename = inspar.filename;
1139 ins->setParams(inspar);
1142 case LFUN_SPACE_INSERT:
1143 if (cur.paragraph().layout().free_spacing)
1144 insertChar(cur, ' ');
1146 doInsertInset(cur, this, cmd, false, false);
1149 moveCursor(cur, false);
1152 case LFUN_SPECIALCHAR_INSERT: {
1153 string const name = to_utf8(cmd.argument());
1154 if (name == "hyphenation")
1155 specialChar(cur, InsetSpecialChar::HYPHENATION);
1156 else if (name == "ligature-break")
1157 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1158 else if (name == "slash")
1159 specialChar(cur, InsetSpecialChar::SLASH);
1160 else if (name == "nobreakdash")
1161 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1162 else if (name == "dots")
1163 specialChar(cur, InsetSpecialChar::LDOTS);
1164 else if (name == "end-of-sentence")
1165 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1166 else if (name == "menu-separator")
1167 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1168 else if (name.empty())
1169 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1171 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1175 case LFUN_WORD_UPCASE:
1176 changeCase(cur, text_uppercase);
1179 case LFUN_WORD_LOWCASE:
1180 changeCase(cur, text_lowercase);
1183 case LFUN_WORD_CAPITALIZE:
1184 changeCase(cur, text_capitalization);
1187 case LFUN_CHARS_TRANSPOSE:
1188 charsTranspose(cur);
1192 cur.message(_("Paste"));
1193 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1194 cap::replaceSelection(cur);
1196 // without argument?
1197 string const arg = to_utf8(cmd.argument());
1199 if (theClipboard().isInternal())
1200 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1201 else if (theClipboard().hasGraphicsContents()
1202 && !theClipboard().hasTextContents())
1203 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1205 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1206 } else if (isStrUnsignedInt(arg)) {
1207 // we have a numerical argument
1208 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1209 convert<unsigned int>(arg));
1211 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1213 type = Clipboard::PdfGraphicsType;
1214 else if (arg == "png")
1215 type = Clipboard::PngGraphicsType;
1216 else if (arg == "jpeg")
1217 type = Clipboard::JpegGraphicsType;
1218 else if (arg == "linkback")
1219 type = Clipboard::LinkBackGraphicsType;
1220 else if (arg == "emf")
1221 type = Clipboard::EmfGraphicsType;
1222 else if (arg == "wmf")
1223 type = Clipboard::WmfGraphicsType;
1226 LASSERT(false, /**/);
1228 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1231 bv->buffer().errors("Paste");
1232 cur.clearSelection(); // bug 393
1238 cutSelection(cur, true, true);
1239 cur.message(_("Cut"));
1244 cur.message(_("Copy"));
1247 case LFUN_SERVER_GET_XY:
1248 cur.message(from_utf8(
1249 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1250 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1253 case LFUN_SERVER_SET_XY: {
1256 istringstream is(to_utf8(cmd.argument()));
1259 lyxerr << "SETXY: Could not parse coordinates in '"
1260 << to_utf8(cmd.argument()) << endl;
1262 tm->setCursorFromCoordinates(cur, x, y);
1266 case LFUN_SERVER_GET_LAYOUT:
1267 cur.message(cur.paragraph().layout().name());
1271 docstring layout = cmd.argument();
1272 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1274 Paragraph const & para = cur.paragraph();
1275 docstring const old_layout = para.layout().name();
1276 DocumentClass const & tclass = bv->buffer().params().documentClass();
1279 layout = tclass.defaultLayoutName();
1281 if (owner_->forcePlainLayout())
1282 // in this case only the empty layout is allowed
1283 layout = tclass.plainLayoutName();
1284 else if (para.usePlainLayout()) {
1285 // in this case, default layout maps to empty layout
1286 if (layout == tclass.defaultLayoutName())
1287 layout = tclass.plainLayoutName();
1289 // otherwise, the empty layout maps to the default
1290 if (layout == tclass.plainLayoutName())
1291 layout = tclass.defaultLayoutName();
1294 bool hasLayout = tclass.hasLayout(layout);
1296 // If the entry is obsolete, use the new one instead.
1298 docstring const & obs = tclass[layout].obsoleted_by();
1304 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1305 from_utf8(N_(" not known")));
1309 bool change_layout = (old_layout != layout);
1311 if (!change_layout && cur.selection() &&
1312 cur.selBegin().pit() != cur.selEnd().pit())
1314 pit_type spit = cur.selBegin().pit();
1315 pit_type epit = cur.selEnd().pit() + 1;
1316 while (spit != epit) {
1317 if (pars_[spit].layout().name() != old_layout) {
1318 change_layout = true;
1326 setLayout(cur, layout);
1331 case LFUN_CLIPBOARD_PASTE:
1332 cur.clearSelection();
1333 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1334 cmd.argument() == "paragraph");
1335 bv->buffer().errors("Paste");
1338 case LFUN_PRIMARY_SELECTION_PASTE:
1339 pasteString(cur, theSelection().get(),
1340 cmd.argument() == "paragraph");
1343 case LFUN_SELECTION_PASTE:
1344 // Copy the selection buffer to the clipboard stack,
1345 // because we want it to appear in the "Edit->Paste
1347 cap::copySelectionToStack();
1348 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1349 bv->buffer().errors("Paste");
1352 case LFUN_UNICODE_INSERT: {
1353 if (cmd.argument().empty())
1355 docstring hexstring = cmd.argument();
1356 if (isHex(hexstring)) {
1357 char_type c = hexToInt(hexstring);
1358 if (c >= 32 && c < 0x10ffff) {
1359 lyxerr << "Inserting c: " << c << endl;
1360 docstring s = docstring(1, c);
1361 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1367 case LFUN_QUOTE_INSERT: {
1368 Paragraph & par = cur.paragraph();
1369 pos_type pos = cur.pos();
1370 BufferParams const & bufparams = bv->buffer().params();
1371 Layout const & style = par.layout();
1372 InsetLayout const & ilayout = cur.inset().getLayout();
1373 if (!style.pass_thru && !ilayout.isPassThru()
1374 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1375 // this avoids a double undo
1376 // FIXME: should not be needed, ideally
1377 if (!cur.selection())
1379 cap::replaceSelection(cur);
1384 else if (cur.prevInset() && cur.prevInset()->isSpace())
1387 c = par.getChar(pos - 1);
1388 string arg = to_utf8(cmd.argument());
1389 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1390 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1394 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1398 case LFUN_DATE_INSERT: {
1399 string const format = cmd.argument().empty()
1400 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1401 string const time = formatted_time(current_time(), format);
1402 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1406 case LFUN_MOUSE_TRIPLE:
1407 if (cmd.button() == mouse_button::button1) {
1408 tm->cursorHome(cur);
1416 case LFUN_MOUSE_DOUBLE:
1417 if (cmd.button() == mouse_button::button1) {
1418 selectWord(cur, WHOLE_WORD_STRICT);
1423 // Single-click on work area
1424 case LFUN_MOUSE_PRESS:
1425 // We are not marking a selection with the keyboard in any case.
1426 cur.bv().cursor().setMark(false);
1427 switch (cmd.button()) {
1428 case mouse_button::button1:
1430 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1431 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1434 case mouse_button::button2:
1435 // Middle mouse pasting.
1436 bv->mouseSetCursor(cur);
1438 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1439 "selection-paste ; primary-selection-paste paragraph"));
1443 case mouse_button::button3: {
1444 Cursor const & bvcur = cur.bv().cursor();
1445 // Don't do anything if we right-click a
1446 // selection, a context menu will popup.
1447 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1448 && cur < bvcur.selectionEnd()) {
1452 if (!bv->mouseSetCursor(cur, false))
1453 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1459 } // switch (cmd.button())
1462 case LFUN_MOUSE_MOTION: {
1463 // Mouse motion with right or middle mouse do nothing for now.
1464 if (cmd.button() != mouse_button::button1) {
1468 // ignore motions deeper nested than the real anchor
1469 Cursor & bvcur = cur.bv().cursor();
1470 if (!bvcur.anchor_.hasPart(cur)) {
1474 CursorSlice old = bvcur.top();
1476 int const wh = bv->workHeight();
1477 int const y = max(0, min(wh - 1, cmd.y));
1479 tm->setCursorFromCoordinates(cur, cmd.x, y);
1480 cur.setTargetX(cmd.x);
1482 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1484 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1485 // This is to allow jumping over large insets
1486 if (cur.top() == old) {
1488 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1490 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1492 // We continue with our existing selection or start a new one, so don't
1493 // reset the anchor.
1494 bvcur.setCursor(cur);
1495 bvcur.setSelection(true);
1496 if (cur.top() == old) {
1497 // We didn't move one iota, so no need to update the screen.
1498 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1505 case LFUN_MOUSE_RELEASE:
1506 switch (cmd.button()) {
1507 case mouse_button::button1:
1508 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1509 // If there is a new selection, update persistent selection;
1510 // otherwise, single click does not clear persistent selection
1512 if (cur.selection()) {
1513 // Finish selection. If double click,
1514 // cur is moved to the end of word by
1515 // selectWord but bvcur is current
1517 cur.bv().cursor().setSelection();
1518 // We might have removed an empty but drawn selection
1519 // (probably a margin)
1520 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1523 // FIXME: We could try to handle drag and drop of selection here.
1526 case mouse_button::button2:
1527 // Middle mouse pasting is handled at mouse press time,
1528 // see LFUN_MOUSE_PRESS.
1532 case mouse_button::button3:
1533 // Cursor was set at LFUN_MOUSE_PRESS time.
1534 // FIXME: If there is a selection we could try to handle a special
1535 // drag & drop context menu.
1539 case mouse_button::none:
1540 case mouse_button::button4:
1541 case mouse_button::button5:
1543 } // switch (cmd.button())
1547 case LFUN_SELF_INSERT: {
1548 if (cmd.argument().empty())
1551 // Automatically delete the currently selected
1552 // text and replace it with what is being
1553 // typed in now. Depends on lyxrc settings
1554 // "auto_region_delete", which defaults to
1557 if (lyxrc.auto_region_delete && cur.selection())
1558 cutSelection(cur, false, false);
1560 cur.clearSelection();
1562 docstring::const_iterator cit = cmd.argument().begin();
1563 docstring::const_iterator const end = cmd.argument().end();
1564 for (; cit != end; ++cit)
1565 bv->translateAndInsert(*cit, this, cur);
1568 moveCursor(cur, false);
1569 bv->bookmarkEditPosition();
1573 case LFUN_HYPERLINK_INSERT: {
1574 InsetCommandParams p(HYPERLINK_CODE);
1576 if (cur.selection()) {
1577 content = cur.selectionAsString(false);
1578 cutSelection(cur, true, false);
1580 p["target"] = (cmd.argument().empty()) ?
1581 content : cmd.argument();
1582 string const data = InsetCommand::params2string("href", p);
1583 if (p["target"].empty()) {
1584 bv->showDialog("href", data);
1586 FuncRequest fr(LFUN_INSET_INSERT, data);
1592 case LFUN_LABEL_INSERT: {
1593 InsetCommandParams p(LABEL_CODE);
1594 // Try to generate a valid label
1595 p["name"] = (cmd.argument().empty()) ?
1596 cur.getPossibleLabel() :
1598 string const data = InsetCommand::params2string("label", p);
1600 if (cmd.argument().empty()) {
1601 bv->showDialog("label", data);
1603 FuncRequest fr(LFUN_INSET_INSERT, data);
1609 case LFUN_INFO_INSERT: {
1611 if (cmd.argument().empty() && cur.selection()) {
1612 // if command argument is empty use current selection as parameter.
1613 docstring ds = cur.selectionAsString(false);
1614 cutSelection(cur, true, false);
1615 FuncRequest cmd0(cmd, ds);
1616 inset = createInset(cur.bv().buffer(), cmd0);
1618 inset = createInset(cur.bv().buffer(), cmd);
1623 insertInset(cur, inset);
1627 case LFUN_CAPTION_INSERT:
1628 case LFUN_FOOTNOTE_INSERT:
1629 case LFUN_NOTE_INSERT:
1630 case LFUN_FLEX_INSERT:
1631 case LFUN_BOX_INSERT:
1632 case LFUN_BRANCH_INSERT:
1633 case LFUN_PHANTOM_INSERT:
1634 case LFUN_ERT_INSERT:
1635 case LFUN_LISTING_INSERT:
1636 case LFUN_MARGINALNOTE_INSERT:
1637 case LFUN_OPTIONAL_INSERT:
1638 case LFUN_INDEX_INSERT:
1639 // Open the inset, and move the current selection
1641 doInsertInset(cur, this, cmd, true, true);
1643 // Some insets are numbered, others are shown in the outline pane so
1644 // let's update the labels and the toc backend.
1645 bv->buffer().updateLabels();
1648 case LFUN_TABULAR_INSERT:
1649 // if there were no arguments, just open the dialog
1650 if (doInsertInset(cur, this, cmd, false, true))
1653 bv->showDialog("tabularcreate");
1657 case LFUN_FLOAT_INSERT:
1658 case LFUN_FLOAT_WIDE_INSERT:
1659 case LFUN_WRAP_INSERT: {
1660 // will some text be moved into the inset?
1661 bool content = cur.selection();
1663 doInsertInset(cur, this, cmd, true, true);
1666 // If some text is moved into the inset, doInsertInset
1667 // puts the cursor outside the inset. To insert the
1668 // caption we put it back into the inset.
1672 ParagraphList & pars = cur.text()->paragraphs();
1674 DocumentClass const & tclass = bv->buffer().params().documentClass();
1676 // add a separate paragraph for the caption inset
1677 pars.push_back(Paragraph());
1678 pars.back().setInsetOwner(&cur.text()->inset());
1679 pars.back().setPlainOrDefaultLayout(tclass);
1680 int cap_pit = pars.size() - 1;
1682 // if an empty inset was created, we create an additional empty
1683 // paragraph at the bottom so that the user can choose where to put
1684 // the graphics (or table).
1686 pars.push_back(Paragraph());
1687 pars.back().setInsetOwner(&cur.text()->inset());
1688 pars.back().setPlainOrDefaultLayout(tclass);
1691 // reposition the cursor to the caption
1692 cur.pit() = cap_pit;
1694 // FIXME: This Text/Cursor dispatch handling is a mess!
1695 // We cannot use Cursor::dispatch here it needs access to up to
1697 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1698 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1699 bv->buffer().updateLabels();
1700 cur.updateFlags(Update::Force);
1701 // FIXME: When leaving the Float (or Wrap) inset we should
1702 // delete any empty paragraph left above or below the
1707 case LFUN_NOMENCL_INSERT: {
1708 InsetCommandParams p(NOMENCL_CODE);
1709 if (cmd.argument().empty())
1710 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1712 p["symbol"] = cmd.argument();
1713 string const data = InsetCommand::params2string("nomenclature", p);
1714 bv->showDialog("nomenclature", data);
1718 case LFUN_INDEX_PRINT: {
1719 InsetCommandParams p(INDEX_PRINT_CODE);
1720 if (cmd.argument().empty())
1721 p["type"] = from_ascii("idx");
1723 p["type"] = cmd.argument();
1724 string const data = InsetCommand::params2string("index_print", p);
1725 FuncRequest fr(LFUN_INSET_INSERT, data);
1730 case LFUN_NOMENCL_PRINT:
1731 case LFUN_TOC_INSERT:
1732 case LFUN_LINE_INSERT:
1733 case LFUN_NEWPAGE_INSERT:
1735 doInsertInset(cur, this, cmd, false, false);
1739 case LFUN_DEPTH_DECREMENT:
1740 changeDepth(cur, DEC_DEPTH);
1743 case LFUN_DEPTH_INCREMENT:
1744 changeDepth(cur, INC_DEPTH);
1747 case LFUN_MATH_DISPLAY:
1748 mathDispatch(cur, cmd, true);
1751 case LFUN_REGEXP_MODE:
1752 regexpDispatch(cur, cmd);
1755 case LFUN_MATH_MODE:
1756 if (cmd.argument() == "on")
1757 // don't pass "on" as argument
1758 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1760 mathDispatch(cur, cmd, false);
1763 case LFUN_MATH_MACRO:
1764 if (cmd.argument().empty())
1765 cur.errorMessage(from_utf8(N_("Missing argument")));
1767 string s = to_utf8(cmd.argument());
1768 string const s1 = token(s, ' ', 1);
1769 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1770 string const s2 = token(s, ' ', 2);
1771 MacroType type = MacroTypeNewcommand;
1773 type = MacroTypeDef;
1774 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1775 inset->setBuffer(bv->buffer());
1776 insertInset(cur, inset);
1778 // enter macro inset and select the name
1780 cur.top().pos() = cur.top().lastpos();
1782 cur.setSelection(true);
1783 cur.top().pos() = 0;
1787 // passthrough hat and underscore outside mathed:
1788 case LFUN_MATH_SUBSCRIPT:
1789 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1791 case LFUN_MATH_SUPERSCRIPT:
1792 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1795 case LFUN_MATH_INSERT:
1796 case LFUN_MATH_AMS_MATRIX:
1797 case LFUN_MATH_MATRIX:
1798 case LFUN_MATH_DELIM:
1799 case LFUN_MATH_BIGDELIM: {
1801 cap::replaceSelection(cur);
1802 cur.insert(new InsetMathHull(hullSimple));
1803 checkAndActivateInset(cur, true);
1804 LASSERT(cur.inMathed(), /**/);
1809 case LFUN_FONT_EMPH: {
1810 Font font(ignore_font, ignore_language);
1811 font.fontInfo().setEmph(FONT_TOGGLE);
1812 toggleAndShow(cur, this, font);
1816 case LFUN_FONT_ITAL: {
1817 Font font(ignore_font, ignore_language);
1818 font.fontInfo().setShape(ITALIC_SHAPE);
1819 toggleAndShow(cur, this, font);
1823 case LFUN_FONT_BOLD:
1824 case LFUN_FONT_BOLDSYMBOL: {
1825 Font font(ignore_font, ignore_language);
1826 font.fontInfo().setSeries(BOLD_SERIES);
1827 toggleAndShow(cur, this, font);
1831 case LFUN_FONT_NOUN: {
1832 Font font(ignore_font, ignore_language);
1833 font.fontInfo().setNoun(FONT_TOGGLE);
1834 toggleAndShow(cur, this, font);
1838 case LFUN_FONT_TYPEWRITER: {
1839 Font font(ignore_font, ignore_language);
1840 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1841 toggleAndShow(cur, this, font);
1845 case LFUN_FONT_SANS: {
1846 Font font(ignore_font, ignore_language);
1847 font.fontInfo().setFamily(SANS_FAMILY);
1848 toggleAndShow(cur, this, font);
1852 case LFUN_FONT_ROMAN: {
1853 Font font(ignore_font, ignore_language);
1854 font.fontInfo().setFamily(ROMAN_FAMILY);
1855 toggleAndShow(cur, this, font);
1859 case LFUN_FONT_DEFAULT: {
1860 Font font(inherit_font, ignore_language);
1861 toggleAndShow(cur, this, font);
1865 case LFUN_FONT_STRIKEOUT: {
1866 Font font(ignore_font, ignore_language);
1867 font.fontInfo().setStrikeout(FONT_TOGGLE);
1868 toggleAndShow(cur, this, font);
1872 case LFUN_FONT_UULINE: {
1873 Font font(ignore_font, ignore_language);
1874 font.fontInfo().setUuline(FONT_TOGGLE);
1875 toggleAndShow(cur, this, font);
1879 case LFUN_FONT_UWAVE: {
1880 Font font(ignore_font, ignore_language);
1881 font.fontInfo().setUwave(FONT_TOGGLE);
1882 toggleAndShow(cur, this, font);
1886 case LFUN_FONT_UNDERLINE: {
1887 Font font(ignore_font, ignore_language);
1888 font.fontInfo().setUnderbar(FONT_TOGGLE);
1889 toggleAndShow(cur, this, font);
1893 case LFUN_FONT_SIZE: {
1894 Font font(ignore_font, ignore_language);
1895 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1896 toggleAndShow(cur, this, font);
1900 case LFUN_LANGUAGE: {
1901 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1904 Font font(ignore_font, lang);
1905 toggleAndShow(cur, this, font);
1909 case LFUN_TEXTSTYLE_APPLY:
1910 toggleAndShow(cur, this, freefont, toggleall);
1911 cur.message(_("Character set"));
1914 // Set the freefont using the contents of \param data dispatched from
1915 // the frontends and apply it at the current cursor location.
1916 case LFUN_TEXTSTYLE_UPDATE: {
1919 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1922 toggleAndShow(cur, this, freefont, toggleall);
1923 cur.message(_("Character set"));
1925 lyxerr << "Argument not ok";
1930 case LFUN_FINISHED_LEFT:
1931 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1932 // We're leaving an inset, going left. If the inset is LTR, we're
1933 // leaving from the front, so we should not move (remain at --- but
1934 // not in --- the inset). If the inset is RTL, move left, without
1935 // entering the inset itself; i.e., move to after the inset.
1936 if (cur.paragraph().getFontSettings(
1937 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1938 cursorVisLeft(cur, true);
1941 case LFUN_FINISHED_RIGHT:
1942 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1943 // We're leaving an inset, going right. If the inset is RTL, we're
1944 // leaving from the front, so we should not move (remain at --- but
1945 // not in --- the inset). If the inset is LTR, move right, without
1946 // entering the inset itself; i.e., move to after the inset.
1947 if (!cur.paragraph().getFontSettings(
1948 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1949 cursorVisRight(cur, true);
1952 case LFUN_FINISHED_BACKWARD:
1953 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1956 case LFUN_FINISHED_FORWARD:
1957 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1959 cur.setCurrentFont();
1962 case LFUN_LAYOUT_PARAGRAPH: {
1964 params2string(cur.paragraph(), data);
1965 data = "show\n" + data;
1966 bv->showDialog("paragraph", data);
1970 case LFUN_PARAGRAPH_UPDATE: {
1972 params2string(cur.paragraph(), data);
1974 // Will the paragraph accept changes from the dialog?
1976 cur.inset().allowParagraphCustomization(cur.idx());
1978 data = "update " + convert<string>(accept) + '\n' + data;
1979 bv->updateDialog("paragraph", data);
1983 case LFUN_ACCENT_UMLAUT:
1984 case LFUN_ACCENT_CIRCUMFLEX:
1985 case LFUN_ACCENT_GRAVE:
1986 case LFUN_ACCENT_ACUTE:
1987 case LFUN_ACCENT_TILDE:
1988 case LFUN_ACCENT_CEDILLA:
1989 case LFUN_ACCENT_MACRON:
1990 case LFUN_ACCENT_DOT:
1991 case LFUN_ACCENT_UNDERDOT:
1992 case LFUN_ACCENT_UNDERBAR:
1993 case LFUN_ACCENT_CARON:
1994 case LFUN_ACCENT_BREVE:
1995 case LFUN_ACCENT_TIE:
1996 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1997 case LFUN_ACCENT_CIRCLE:
1998 case LFUN_ACCENT_OGONEK:
1999 theLyXFunc().handleKeyFunc(cmd.action);
2000 if (!cmd.argument().empty())
2001 // FIXME: Are all these characters encoded in one byte in utf8?
2002 bv->translateAndInsert(cmd.argument()[0], this, cur);
2005 case LFUN_FLOAT_LIST_INSERT: {
2006 DocumentClass const & tclass = bv->buffer().params().documentClass();
2007 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2009 if (cur.selection())
2010 cutSelection(cur, true, false);
2011 breakParagraph(cur);
2013 if (cur.lastpos() != 0) {
2014 cursorBackward(cur);
2015 breakParagraph(cur);
2018 docstring const laystr = cur.inset().usePlainLayout() ?
2019 tclass.plainLayoutName() :
2020 tclass.defaultLayoutName();
2021 setLayout(cur, laystr);
2022 ParagraphParameters p;
2023 // FIXME If this call were replaced with one to clearParagraphParams(),
2024 // then we could get rid of this method altogether.
2025 setParagraphs(cur, p);
2026 // FIXME This should be simplified when InsetFloatList takes a
2027 // Buffer in its constructor.
2028 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
2029 ifl->setBuffer(bv->buffer());
2030 insertInset(cur, ifl);
2033 lyxerr << "Non-existent float type: "
2034 << to_utf8(cmd.argument()) << endl;
2039 case LFUN_CHANGE_ACCEPT: {
2040 acceptOrRejectChanges(cur, ACCEPT);
2044 case LFUN_CHANGE_REJECT: {
2045 acceptOrRejectChanges(cur, REJECT);
2049 case LFUN_THESAURUS_ENTRY: {
2050 docstring arg = cmd.argument();
2052 arg = cur.selectionAsString(false);
2054 if (arg.size() > 100 || arg.empty()) {
2055 // Get word or selection
2056 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2057 arg = cur.selectionAsString(false);
2058 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2061 bv->showDialog("thesaurus", to_utf8(arg));
2065 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2066 // Given data, an encoding of the ParagraphParameters
2067 // generated in the Paragraph dialog, this function sets
2068 // the current paragraph, or currently selected paragraphs,
2070 // NOTE: This function overrides all existing settings.
2071 setParagraphs(cur, cmd.argument());
2072 cur.message(_("Paragraph layout set"));
2076 case LFUN_PARAGRAPH_PARAMS: {
2077 // Given data, an encoding of the ParagraphParameters as we'd
2078 // find them in a LyX file, this function modifies the current paragraph,
2079 // or currently selected paragraphs.
2080 // NOTE: This function only modifies, and does not override, existing
2082 setParagraphs(cur, cmd.argument(), true);
2083 cur.message(_("Paragraph layout set"));
2088 if (cur.selection()) {
2089 cur.setSelection(false);
2092 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2093 // correct, but I'm not 100% sure -- dov, 071019
2094 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2098 case LFUN_OUTLINE_UP:
2099 outline(OutlineUp, cur);
2100 setCursor(cur, cur.pit(), 0);
2101 cur.buffer()->updateLabels();
2105 case LFUN_OUTLINE_DOWN:
2106 outline(OutlineDown, cur);
2107 setCursor(cur, cur.pit(), 0);
2108 cur.buffer()->updateLabels();
2112 case LFUN_OUTLINE_IN:
2113 outline(OutlineIn, cur);
2114 cur.buffer()->updateLabels();
2118 case LFUN_OUTLINE_OUT:
2119 outline(OutlineOut, cur);
2120 cur.buffer()->updateLabels();
2124 case LFUN_OUTLINE_DRAGMOVE: {
2125 int const move_id = convert<int>(cmd.getArg(0));
2126 int const move_to_id = convert<int>(cmd.getArg(1));
2127 dragMove(cur, move_id, move_to_id);
2128 setCursor(cur, cur.pit(), 0);
2129 cur.buffer()->updateLabels();
2135 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2140 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2142 // FIXME: The cursor flag is reset two lines below
2143 // so we need to check here if some of the LFUN did touch that.
2144 // for now only Text::erase() and Text::backspace() do that.
2145 // The plan is to verify all the LFUNs and then to remove this
2146 // singleParUpdate boolean altogether.
2147 if (cur.result().update() & Update::Force) {
2148 singleParUpdate = false;
2152 // FIXME: the following code should go in favor of fine grained
2153 // update flag treatment.
2154 if (singleParUpdate) {
2155 // Inserting characters does not change par height in general. So, try
2156 // to update _only_ this paragraph. BufferView will detect if a full
2157 // metrics update is needed anyway.
2158 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2163 && &oldTopSlice.inset() == &cur.inset()
2164 && oldTopSlice.idx() == cur.idx()
2165 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2166 && !cur.selection())
2167 // FIXME: it would be better if we could just do this
2169 //if (cur.result().update() != Update::FitCursor)
2172 // But some LFUNs do not set Update::FitCursor when needed, so we
2173 // do it for all. This is not very harmfull as FitCursor will provoke
2174 // a full redraw only if needed but still, a proper review of all LFUN
2175 // should be done and this needsUpdate boolean can then be removed.
2176 cur.updateFlags(Update::FitCursor);
2178 cur.updateFlags(Update::Force | Update::FitCursor);
2182 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2183 FuncStatus & flag) const
2185 LASSERT(cur.text() == this, /**/);
2187 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2189 InsetCode code = NO_CODE;
2191 switch (cmd.action) {
2193 case LFUN_DEPTH_DECREMENT:
2194 enable = changeDepthAllowed(cur, DEC_DEPTH);
2197 case LFUN_DEPTH_INCREMENT:
2198 enable = changeDepthAllowed(cur, INC_DEPTH);
2202 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2205 case LFUN_DIALOG_SHOW_NEW_INSET:
2206 if (cmd.argument() == "bibitem")
2207 code = BIBITEM_CODE;
2208 else if (cmd.argument() == "bibtex") {
2210 // not allowed in description items
2211 enable = !inDescriptionItem(cur);
2213 else if (cmd.argument() == "box")
2215 else if (cmd.argument() == "branch")
2217 else if (cmd.argument() == "citation")
2219 else if (cmd.argument() == "ert")
2221 else if (cmd.argument() == "external")
2222 code = EXTERNAL_CODE;
2223 else if (cmd.argument() == "float")
2225 else if (cmd.argument() == "graphics")
2226 code = GRAPHICS_CODE;
2227 else if (cmd.argument() == "href")
2228 code = HYPERLINK_CODE;
2229 else if (cmd.argument() == "include")
2230 code = INCLUDE_CODE;
2231 else if (cmd.argument() == "index")
2233 else if (cmd.argument() == "index_print")
2234 code = INDEX_PRINT_CODE;
2235 else if (cmd.argument() == "nomenclature")
2236 code = NOMENCL_CODE;
2237 else if (cmd.argument() == "nomencl_print")
2238 code = NOMENCL_PRINT_CODE;
2239 else if (cmd.argument() == "label")
2241 else if (cmd.argument() == "note")
2243 else if (cmd.argument() == "phantom")
2244 code = PHANTOM_CODE;
2245 else if (cmd.argument() == "ref")
2247 else if (cmd.argument() == "space")
2249 else if (cmd.argument() == "toc")
2251 else if (cmd.argument() == "vspace")
2253 else if (cmd.argument() == "wrap")
2255 else if (cmd.argument() == "listings")
2256 code = LISTINGS_CODE;
2259 case LFUN_ERT_INSERT:
2262 case LFUN_LISTING_INSERT:
2263 code = LISTINGS_CODE;
2264 // not allowed in description items
2265 enable = !inDescriptionItem(cur);
2267 case LFUN_FOOTNOTE_INSERT:
2270 case LFUN_TABULAR_INSERT:
2271 code = TABULAR_CODE;
2273 case LFUN_MARGINALNOTE_INSERT:
2276 case LFUN_FLOAT_INSERT:
2277 case LFUN_FLOAT_WIDE_INSERT:
2278 // FIXME: If there is a selection, we should check whether there
2279 // are floats in the selection, but this has performance issues, see
2280 // LFUN_CHANGE_ACCEPT/REJECT.
2282 if (inDescriptionItem(cur))
2283 // not allowed in description items
2286 InsetCode const inset_code = cur.inset().lyxCode();
2288 // algorithm floats cannot be put in another float
2289 if (to_utf8(cmd.argument()) == "algorithm") {
2290 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2294 // for figures and tables: only allow in another
2295 // float or wrap if it is of the same type and
2296 // not a subfloat already
2297 if(cur.inset().lyxCode() == code) {
2298 InsetFloat const & ins =
2299 static_cast<InsetFloat const &>(cur.inset());
2300 enable = ins.params().type == to_utf8(cmd.argument())
2301 && !ins.params().subfloat;
2302 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2303 InsetWrap const & ins =
2304 static_cast<InsetWrap const &>(cur.inset());
2305 enable = ins.params().type == to_utf8(cmd.argument());
2309 case LFUN_WRAP_INSERT:
2311 // not allowed in description items
2312 enable = !inDescriptionItem(cur);
2314 case LFUN_FLOAT_LIST_INSERT:
2315 code = FLOAT_LIST_CODE;
2316 // not allowed in description items
2317 enable = !inDescriptionItem(cur);
2319 case LFUN_CAPTION_INSERT:
2320 code = CAPTION_CODE;
2321 // not allowed in description items
2322 enable = !inDescriptionItem(cur);
2324 case LFUN_NOTE_INSERT:
2326 // in commands (sections etc.) and description items,
2327 // only Notes are allowed
2328 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2329 (!cur.paragraph().layout().isCommand()
2330 && !inDescriptionItem(cur)));
2332 case LFUN_FLEX_INSERT: {
2334 string s = cmd.getArg(0);
2336 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2337 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2338 il.lyxtype() != InsetLayout::CUSTOM &&
2339 il.lyxtype() != InsetLayout::ELEMENT &&
2340 il.lyxtype ()!= InsetLayout::STANDARD)
2344 case LFUN_BOX_INSERT:
2347 case LFUN_BRANCH_INSERT:
2349 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2350 && cur.buffer()->params().branchlist().empty())
2353 case LFUN_PHANTOM_INSERT:
2354 code = PHANTOM_CODE;
2356 case LFUN_LABEL_INSERT:
2359 case LFUN_LINE_INSERT:
2362 case LFUN_INFO_INSERT:
2365 case LFUN_OPTIONAL_INSERT:
2367 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2368 < cur.paragraph().layout().optionalargs;
2370 case LFUN_INDEX_INSERT:
2373 case LFUN_INDEX_PRINT:
2374 code = INDEX_PRINT_CODE;
2375 // not allowed in description items
2376 enable = !inDescriptionItem(cur);
2378 case LFUN_NOMENCL_INSERT:
2379 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2383 code = NOMENCL_CODE;
2385 case LFUN_NOMENCL_PRINT:
2386 code = NOMENCL_PRINT_CODE;
2387 // not allowed in description items
2388 enable = !inDescriptionItem(cur);
2390 case LFUN_TOC_INSERT:
2392 // not allowed in description items
2393 enable = !inDescriptionItem(cur);
2395 case LFUN_HYPERLINK_INSERT:
2396 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2400 code = HYPERLINK_CODE;
2402 case LFUN_QUOTE_INSERT:
2403 // always allow this, since we will inset a raw quote
2404 // if an inset is not allowed.
2406 case LFUN_SPECIALCHAR_INSERT:
2407 code = SPECIALCHAR_CODE;
2409 case LFUN_SPACE_INSERT:
2410 // slight hack: we know this is allowed in math mode
2415 case LFUN_MATH_INSERT:
2416 case LFUN_MATH_AMS_MATRIX:
2417 case LFUN_MATH_MATRIX:
2418 case LFUN_MATH_DELIM:
2419 case LFUN_MATH_BIGDELIM:
2420 case LFUN_MATH_DISPLAY:
2421 case LFUN_MATH_MODE:
2422 case LFUN_MATH_MACRO:
2423 case LFUN_MATH_SUBSCRIPT:
2424 case LFUN_MATH_SUPERSCRIPT:
2425 code = MATH_HULL_CODE;
2428 case LFUN_INSET_MODIFY:
2429 // We need to disable this, because we may get called for a
2431 // InsetTabular::getStatus() -> InsetText::getStatus()
2432 // and we don't handle LFUN_INSET_MODIFY.
2436 case LFUN_FONT_EMPH:
2437 flag.setOnOff(fontinfo.emph() == FONT_ON);
2440 case LFUN_FONT_ITAL:
2441 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2444 case LFUN_FONT_NOUN:
2445 flag.setOnOff(fontinfo.noun() == FONT_ON);
2448 case LFUN_FONT_BOLD:
2449 case LFUN_FONT_BOLDSYMBOL:
2450 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2453 case LFUN_FONT_SANS:
2454 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2457 case LFUN_FONT_ROMAN:
2458 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2461 case LFUN_FONT_TYPEWRITER:
2462 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2467 enable = cur.selection();
2471 if (cmd.argument().empty()) {
2472 if (theClipboard().isInternal())
2473 enable = cap::numberOfSelections() > 0;
2475 enable = !theClipboard().empty();
2479 // we have an argument
2480 string const arg = to_utf8(cmd.argument());
2481 if (isStrUnsignedInt(arg)) {
2482 // it's a number and therefore means the internal stack
2483 unsigned int n = convert<unsigned int>(arg);
2484 enable = cap::numberOfSelections() > n;
2488 // explicit graphics type?
2489 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2490 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2491 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2492 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2493 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2494 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2495 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2496 enable = theClipboard().hasGraphicsContents(type);
2505 case LFUN_CLIPBOARD_PASTE:
2506 enable = !theClipboard().empty();
2509 case LFUN_PRIMARY_SELECTION_PASTE:
2510 enable = cur.selection() || !theSelection().empty();
2513 case LFUN_SELECTION_PASTE:
2514 enable = cap::selection();
2517 case LFUN_PARAGRAPH_MOVE_UP:
2518 enable = cur.pit() > 0 && !cur.selection();
2521 case LFUN_PARAGRAPH_MOVE_DOWN:
2522 enable = cur.pit() < cur.lastpit() && !cur.selection();
2525 case LFUN_INSET_DISSOLVE:
2526 if (!cmd.argument().empty()) {
2527 InsetLayout const & il = cur.inset().getLayout();
2528 InsetLayout::InsetLyXType const type =
2529 translateLyXType(to_utf8(cmd.argument()));
2530 enable = cur.inset().lyxCode() == FLEX_CODE
2531 && il.lyxtype() == type;
2533 enable = ((!isMainText()
2534 && cur.inset().nargs() == 1)
2536 && cur.nextInset()->nargs() == 1));
2540 case LFUN_CHANGE_ACCEPT:
2541 case LFUN_CHANGE_REJECT:
2542 // In principle, these LFUNs should only be enabled if there
2543 // is a change at the current position/in the current selection.
2544 // However, without proper optimizations, this will inevitably
2545 // result in unacceptable performance - just imagine a user who
2546 // wants to select the complete content of a long document.
2547 if (!cur.selection())
2548 enable = cur.paragraph().isChanged(cur.pos());
2550 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2555 case LFUN_OUTLINE_UP:
2556 case LFUN_OUTLINE_DOWN:
2557 case LFUN_OUTLINE_IN:
2558 case LFUN_OUTLINE_OUT:
2559 case LFUN_OUTLINE_DRAGMOVE:
2560 // FIXME: LyX is not ready for outlining within inset.
2561 enable = isMainText()
2562 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2565 case LFUN_NEWLINE_INSERT:
2566 // LaTeX restrictions (labels or empty par)
2567 enable = (cur.pos() > cur.paragraph().beginOfBody());
2570 case LFUN_TAB_INSERT:
2571 case LFUN_TAB_DELETE:
2572 enable = cur.inset().getLayout().isPassThru();
2575 case LFUN_SET_GRAPHICS_GROUP: {
2576 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2580 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2584 case LFUN_NEWPAGE_INSERT:
2585 // not allowed in description items
2586 code = NEWPAGE_CODE;
2587 enable = !inDescriptionItem(cur);
2590 case LFUN_DATE_INSERT: {
2591 string const format = cmd.argument().empty()
2592 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2593 enable = support::os::is_valid_strftime(format);
2598 enable = !cur.inset().getLayout().isPassThru();
2600 case LFUN_BREAK_PARAGRAPH:
2601 enable = cur.inset().getLayout().isMultiPar();
2603 case LFUN_WORD_DELETE_FORWARD:
2604 case LFUN_WORD_DELETE_BACKWARD:
2605 case LFUN_LINE_DELETE:
2606 case LFUN_WORD_FORWARD:
2607 case LFUN_WORD_BACKWARD:
2608 case LFUN_WORD_RIGHT:
2609 case LFUN_WORD_LEFT:
2610 case LFUN_CHAR_FORWARD:
2611 case LFUN_CHAR_FORWARD_SELECT:
2612 case LFUN_CHAR_BACKWARD:
2613 case LFUN_CHAR_BACKWARD_SELECT:
2614 case LFUN_CHAR_LEFT:
2615 case LFUN_CHAR_LEFT_SELECT:
2616 case LFUN_CHAR_RIGHT:
2617 case LFUN_CHAR_RIGHT_SELECT:
2619 case LFUN_UP_SELECT:
2621 case LFUN_DOWN_SELECT:
2622 case LFUN_PARAGRAPH_UP_SELECT:
2623 case LFUN_PARAGRAPH_DOWN_SELECT:
2624 case LFUN_LINE_BEGIN_SELECT:
2625 case LFUN_LINE_END_SELECT:
2626 case LFUN_WORD_FORWARD_SELECT:
2627 case LFUN_WORD_BACKWARD_SELECT:
2628 case LFUN_WORD_RIGHT_SELECT:
2629 case LFUN_WORD_LEFT_SELECT:
2630 case LFUN_WORD_SELECT:
2631 case LFUN_SECTION_SELECT:
2632 case LFUN_PARAGRAPH_UP:
2633 case LFUN_PARAGRAPH_DOWN:
2634 case LFUN_LINE_BEGIN:
2636 case LFUN_CHAR_DELETE_FORWARD:
2637 case LFUN_CHAR_DELETE_BACKWARD:
2638 case LFUN_INSET_INSERT:
2639 case LFUN_WORD_UPCASE:
2640 case LFUN_WORD_LOWCASE:
2641 case LFUN_WORD_CAPITALIZE:
2642 case LFUN_CHARS_TRANSPOSE:
2643 case LFUN_SERVER_GET_XY:
2644 case LFUN_SERVER_SET_XY:
2645 case LFUN_SERVER_GET_LAYOUT:
2647 case LFUN_SELF_INSERT:
2648 case LFUN_FONT_DEFAULT:
2649 case LFUN_FONT_UNDERLINE:
2650 case LFUN_FONT_STRIKEOUT:
2651 case LFUN_FONT_UULINE:
2652 case LFUN_FONT_UWAVE:
2653 case LFUN_FONT_SIZE:
2654 case LFUN_TEXTSTYLE_APPLY:
2655 case LFUN_TEXTSTYLE_UPDATE:
2656 case LFUN_LAYOUT_PARAGRAPH:
2657 case LFUN_PARAGRAPH_UPDATE:
2658 case LFUN_ACCENT_UMLAUT:
2659 case LFUN_ACCENT_CIRCUMFLEX:
2660 case LFUN_ACCENT_GRAVE:
2661 case LFUN_ACCENT_ACUTE:
2662 case LFUN_ACCENT_TILDE:
2663 case LFUN_ACCENT_CEDILLA:
2664 case LFUN_ACCENT_MACRON:
2665 case LFUN_ACCENT_DOT:
2666 case LFUN_ACCENT_UNDERDOT:
2667 case LFUN_ACCENT_UNDERBAR:
2668 case LFUN_ACCENT_CARON:
2669 case LFUN_ACCENT_BREVE:
2670 case LFUN_ACCENT_TIE:
2671 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2672 case LFUN_ACCENT_CIRCLE:
2673 case LFUN_ACCENT_OGONEK:
2674 case LFUN_THESAURUS_ENTRY:
2675 case LFUN_PARAGRAPH_PARAMS_APPLY:
2676 case LFUN_PARAGRAPH_PARAMS:
2678 case LFUN_BUFFER_BEGIN:
2679 case LFUN_BUFFER_END:
2680 case LFUN_BUFFER_BEGIN_SELECT:
2681 case LFUN_BUFFER_END_SELECT:
2682 case LFUN_INSET_BEGIN:
2683 case LFUN_INSET_END:
2684 case LFUN_INSET_BEGIN_SELECT:
2685 case LFUN_INSET_END_SELECT:
2686 case LFUN_INSET_SELECT_ALL:
2687 case LFUN_UNICODE_INSERT:
2688 // these are handled in our dispatch()
2698 || !cur.inset().insetAllowed(code)
2699 || cur.paragraph().layout().pass_thru))
2702 switch (cmd.action) {
2703 case LFUN_ACCENT_ACUTE:
2704 case LFUN_ACCENT_BREVE:
2705 case LFUN_ACCENT_CARON:
2706 case LFUN_ACCENT_CEDILLA:
2707 case LFUN_ACCENT_CIRCLE:
2708 case LFUN_ACCENT_CIRCUMFLEX:
2709 case LFUN_ACCENT_DOT:
2710 case LFUN_ACCENT_GRAVE:
2711 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2712 case LFUN_ACCENT_MACRON:
2713 case LFUN_ACCENT_OGONEK:
2714 case LFUN_ACCENT_TIE:
2715 case LFUN_ACCENT_TILDE:
2716 case LFUN_ACCENT_UMLAUT:
2717 case LFUN_ACCENT_UNDERBAR:
2718 case LFUN_ACCENT_UNDERDOT:
2720 case LFUN_DEPTH_DECREMENT:
2721 case LFUN_DEPTH_INCREMENT:
2722 case LFUN_FILE_INSERT:
2723 case LFUN_FONT_BOLD:
2724 case LFUN_FONT_BOLDSYMBOL:
2725 case LFUN_FONT_TYPEWRITER:
2726 case LFUN_FONT_DEFAULT:
2727 case LFUN_FONT_EMPH:
2728 case LFUN_FONT_NOUN:
2729 case LFUN_FONT_ROMAN:
2730 case LFUN_FONT_SANS:
2731 case LFUN_FONT_FRAK:
2732 case LFUN_FONT_ITAL:
2733 case LFUN_FONT_SIZE:
2734 case LFUN_FONT_STATE:
2735 case LFUN_FONT_UNDERLINE:
2736 case LFUN_FONT_STRIKEOUT:
2737 case LFUN_FONT_UULINE:
2738 case LFUN_FONT_UWAVE:
2739 case LFUN_LABEL_GOTO:
2740 case LFUN_LAYOUT_TABULAR:
2742 case LFUN_NOTE_NEXT:
2743 case LFUN_REFERENCE_NEXT:
2744 case LFUN_SERVER_NOTIFY:
2745 case LFUN_SERVER_SET_XY:
2746 case LFUN_TEXTSTYLE_APPLY:
2747 case LFUN_TEXTSTYLE_UPDATE:
2748 if (cur.inset().getLayout().isPassThru())
2755 flag.setEnabled(enable);
2760 void Text::pasteString(Cursor & cur, docstring const & clip,
2763 cur.clearSelection();
2764 if (!clip.empty()) {
2767 insertStringAsParagraphs(cur, clip, cur.current_font);
2769 insertStringAsLines(cur, clip, cur.current_font);
2774 // FIXME: an item inset would make things much easier.
2775 bool Text::inDescriptionItem(Cursor & cur) const
2777 Paragraph & par = cur.paragraph();
2778 pos_type const pos = cur.pos();
2779 pos_type const body_pos = par.beginOfBody();
2781 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2782 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2783 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2786 return (pos < body_pos
2788 && (pos == 0 || par.getChar(pos - 1) != ' ')));