3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
35 #include "InsetList.h"
39 #include "LyXAction.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "SpellChecker.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
49 #include "WordLangTuple.h"
51 #include "frontends/Application.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetCollapsable.h"
56 #include "insets/InsetCommand.h"
57 #include "insets/InsetExternal.h"
58 #include "insets/InsetFloat.h"
59 #include "insets/InsetFloatList.h"
60 #include "insets/InsetGraphics.h"
61 #include "insets/InsetGraphicsParams.h"
62 #include "insets/InsetNewline.h"
63 #include "insets/InsetQuotes.h"
64 #include "insets/InsetSpecialChar.h"
65 #include "insets/InsetText.h"
66 #include "insets/InsetWrap.h"
68 #include "support/convert.h"
69 #include "support/debug.h"
70 #include "support/gettext.h"
71 #include "support/lstrings.h"
72 #include "support/lyxtime.h"
73 #include "support/os.h"
75 #include "mathed/InsetMathHull.h"
76 #include "mathed/MathMacroTemplate.h"
78 #include <boost/next_prior.hpp>
84 using namespace lyx::support;
88 using cap::copySelection;
89 using cap::cutSelection;
90 using cap::pasteFromStack;
91 using cap::pasteClipboardText;
92 using cap::pasteClipboardGraphics;
93 using cap::replaceSelection;
94 using cap::grabAndEraseSelection;
95 using cap::selClearOrDel;
98 static Font freefont(ignore_font, ignore_language);
99 static bool toggleall = false;
101 static void toggleAndShow(Cursor & cur, Text * text,
102 Font const & font, bool toggleall = true)
104 text->toggleFree(cur, font, toggleall);
106 if (font.language() != ignore_language ||
107 font.fontInfo().number() != FONT_IGNORE) {
108 TextMetrics const & tm = cur.bv().textMetrics(text);
109 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
110 cur.real_current_font))
111 text->setCursor(cur, cur.pit(), cur.pos(),
112 false, !cur.boundary());
117 static void moveCursor(Cursor & cur, bool selecting)
119 if (selecting || cur.mark())
124 static void finishChange(Cursor & cur, bool selecting)
127 moveCursor(cur, selecting);
131 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
134 docstring sel = cur.selectionAsString(false);
136 // It may happen that sel is empty but there is a selection
137 replaceSelection(cur);
139 // Is this a valid formula?
143 #ifdef ENABLE_ASSERTIONS
144 const int old_pos = cur.pos();
146 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
147 #ifdef ENABLE_ASSERTIONS
148 LASSERT(old_pos == cur.pos(), /**/);
150 cur.nextInset()->edit(cur, true);
151 // don't do that also for LFUN_MATH_MODE
152 // unless you want end up with always changing
153 // to mathrm when opening an inlined inset --
154 // I really hate "LyXfunc overloading"...
156 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
157 // Avoid an unnecessary undo step if cmd.argument
159 if (!cmd.argument().empty())
160 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
163 // create a macro if we see "\\newcommand"
164 // somewhere, and an ordinary formula
166 if (sel.find(from_ascii("\\newcommand")) == string::npos
167 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
168 && sel.find(from_ascii("\\def")) == string::npos)
170 InsetMathHull * formula = new InsetMathHull(cur.buffer());
171 string const selstr = to_utf8(sel);
172 istringstream is(selstr);
175 if (!formula->readQuiet(lex)) {
176 // No valid formula, let's try with delims
177 is.str("$" + selstr + "$");
179 if (!formula->readQuiet(lex)) {
180 // Still not valid, leave it as is
189 cur.insert(new MathMacroTemplate(cur.buffer(), sel));
193 cur.message(from_utf8(N_("Math editor mode")));
195 cur.message(from_utf8(N_("No valid math formula")));
199 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
201 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
202 if (cur.inRegexped()) {
203 cur.message(_("Already in regular expression mode"));
207 docstring sel = cur.selectionAsString(false);
209 // It may happen that sel is empty but there is a selection
210 replaceSelection(cur);
212 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
213 cur.nextInset()->edit(cur, true);
216 cur.message(_("Regexp editor mode"));
220 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
223 cap::replaceSelection(cur);
224 cur.insert(new InsetSpecialChar(kind));
229 static bool doInsertInset(Cursor & cur, Text * text,
230 FuncRequest const & cmd, bool edit, bool pastesel)
232 Buffer & buffer = cur.bv().buffer();
233 BufferParams const & bparams = buffer.params();
234 Inset * inset = createInset(&buffer, cmd);
238 if (InsetCollapsable * ci = inset->asInsetCollapsable())
239 ci->setButtonLabel();
242 if (cmd.action() == LFUN_INDEX_INSERT) {
243 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
244 text->insertInset(cur, inset);
246 inset->edit(cur, true);
247 // Now put this into inset
248 cur.text()->insertStringAsLines(cur, ds, cur.current_font);
249 cur.leaveInset(*inset);
254 if (cur.selection()) {
255 cutSelection(cur, false, pastesel);
256 cur.clearSelection();
259 text->insertInset(cur, inset);
262 inset->edit(cur, true);
264 if (!gotsel || !pastesel)
267 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
268 cur.buffer()->errors("Paste");
269 cur.clearSelection(); // bug 393
271 InsetText * inset_text = inset->asInsetText();
273 inset_text->fixParagraphsFont();
274 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
275 // reset first par to default
276 cur.text()->paragraphs().begin()
277 ->setPlainOrDefaultLayout(bparams.documentClass());
280 // Merge multiple paragraphs -- hack
281 while (cur.lastpit() > 0)
282 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
283 cur.leaveInset(*inset);
286 cur.leaveInset(*inset);
287 // reset surrounding par to default
288 DocumentClass const & dc = bparams.documentClass();
289 docstring const layoutname = inset->usePlainLayout()
290 ? dc.plainLayoutName()
291 : dc.defaultLayoutName();
292 text->setLayout(cur, layoutname);
298 string const freefont2string()
300 return freefont.toString(toggleall);
304 /// the type of outline operation
306 OutlineUp, // Move this header with text down
307 OutlineDown, // Move this header with text up
308 OutlineIn, // Make this header deeper
309 OutlineOut // Make this header shallower
313 static void outline(OutlineOp mode, Cursor & cur)
315 Buffer & buf = *cur.buffer();
316 pit_type & pit = cur.pit();
317 ParagraphList & pars = buf.text().paragraphs();
318 ParagraphList::iterator const bgn = pars.begin();
319 // The first paragraph of the area to be copied:
320 ParagraphList::iterator start = boost::next(bgn, pit);
321 // The final paragraph of area to be copied:
322 ParagraphList::iterator finish = start;
323 ParagraphList::iterator const end = pars.end();
325 DocumentClass const & tc = buf.params().documentClass();
327 int const thistoclevel = start->layout().toclevel;
330 // Move out (down) from this section header
334 // Seek the one (on same level) below
335 for (; finish != end; ++finish) {
336 toclevel = finish->layout().toclevel;
337 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
343 if (start == pars.begin())
346 ParagraphList::iterator dest = start;
347 // Move out (up) from this header
350 // Search previous same-level header above
353 toclevel = dest->layout().toclevel;
355 && (toclevel == Layout::NOT_IN_TOC
356 || toclevel > thistoclevel));
357 // Not found; do nothing
358 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
360 pit_type const newpit = distance(bgn, dest);
361 pit_type const len = distance(start, finish);
362 pit_type const deletepit = pit + len;
363 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
364 pars.splice(dest, start, finish);
372 // Go one down from *this* header:
373 ParagraphList::iterator dest = boost::next(finish, 1);
374 // Go further down to find header to insert in front of:
375 for (; dest != end; ++dest) {
376 toclevel = dest->layout().toclevel;
377 if (toclevel != Layout::NOT_IN_TOC
378 && toclevel <= thistoclevel)
381 // One such was found:
382 pit_type newpit = distance(bgn, dest);
383 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
384 pit_type const len = distance(start, finish);
385 pars.splice(dest, start, finish);
386 cur.pit() = newpit - len;
390 pit_type const len = distance(start, finish);
391 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
392 for (; start != finish; ++start) {
393 toclevel = start->layout().toclevel;
394 if (toclevel == Layout::NOT_IN_TOC)
396 DocumentClass::const_iterator lit = tc.begin();
397 DocumentClass::const_iterator len = tc.end();
398 for (; lit != len; ++lit) {
399 if (lit->toclevel == toclevel + 1 &&
400 start->layout().labeltype == lit->labeltype) {
401 start->setLayout(*lit);
409 pit_type const len = distance(start, finish);
410 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
411 for (; start != finish; ++start) {
412 toclevel = start->layout().toclevel;
413 if (toclevel == Layout::NOT_IN_TOC)
415 DocumentClass::const_iterator lit = tc.begin();
416 DocumentClass::const_iterator len = tc.end();
417 for (; lit != len; ++lit) {
418 if (lit->toclevel == toclevel - 1 &&
419 start->layout().labeltype == lit->labeltype) {
420 start->setLayout(*lit);
431 void Text::number(Cursor & cur)
433 FontInfo font = ignore_font;
434 font.setNumber(FONT_TOGGLE);
435 toggleAndShow(cur, this, Font(font, ignore_language));
439 bool Text::isRTL(Paragraph const & par) const
441 Buffer const & buffer = owner_->buffer();
442 return par.isRTL(buffer.params());
446 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
448 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
450 // Dispatch if the cursor is inside the text. It is not the
451 // case for context menus (bug 5797).
452 if (cur.text() != this) {
457 BufferView * bv = &cur.bv();
458 TextMetrics * tm = &bv->textMetrics(this);
459 if (!tm->contains(cur.pit())) {
460 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
461 tm = &bv->textMetrics(this);
464 // FIXME: We use the update flag to indicates wether a singlePar or a
465 // full screen update is needed. We reset it here but shall we restore it
467 cur.noScreenUpdate();
469 LASSERT(cur.text() == this, /**/);
470 CursorSlice oldTopSlice = cur.top();
471 bool oldBoundary = cur.boundary();
472 bool sel = cur.selection();
473 // Signals that, even if needsUpdate == false, an update of the
474 // cursor paragraph is required
475 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
476 LyXAction::SingleParUpdate);
477 // Signals that a full-screen update is required
478 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
479 LyXAction::NoUpdate) || singleParUpdate);
480 int const last_pid = cur.paragraph().id();
481 pos_type const last_pos = cur.pos();
482 bool const last_misspelled = lyxrc.spellcheck_continuously && cur.paragraph().isMisspelled(cur.pos());
484 FuncCode const act = cmd.action();
487 case LFUN_PARAGRAPH_MOVE_DOWN: {
488 pit_type const pit = cur.pit();
489 recUndo(cur, pit, pit + 1);
491 pars_.swap(pit, pit + 1);
493 cur.forceBufferUpdate();
498 case LFUN_PARAGRAPH_MOVE_UP: {
499 pit_type const pit = cur.pit();
500 recUndo(cur, pit - 1, pit);
502 pars_.swap(pit, pit - 1);
505 cur.forceBufferUpdate();
509 case LFUN_APPENDIX: {
510 Paragraph & par = cur.paragraph();
511 bool start = !par.params().startOfAppendix();
513 // FIXME: The code below only makes sense at top level.
514 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
515 // ensure that we have only one start_of_appendix in this document
516 // FIXME: this don't work for multipart document!
517 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
518 if (pars_[tmp].params().startOfAppendix()) {
520 pars_[tmp].params().startOfAppendix(false);
526 par.params().startOfAppendix(start);
528 // we can set the refreshing parameters now
529 cur.forceBufferUpdate();
533 case LFUN_WORD_DELETE_FORWARD:
535 cutSelection(cur, true, false);
537 deleteWordForward(cur);
538 finishChange(cur, false);
541 case LFUN_WORD_DELETE_BACKWARD:
543 cutSelection(cur, true, false);
545 deleteWordBackward(cur);
546 finishChange(cur, false);
549 case LFUN_LINE_DELETE:
551 cutSelection(cur, true, false);
553 tm->deleteLineForward(cur);
554 finishChange(cur, false);
557 case LFUN_BUFFER_BEGIN:
558 case LFUN_BUFFER_BEGIN_SELECT:
559 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
560 if (cur.depth() == 1)
561 needsUpdate |= cursorTop(cur);
564 cur.screenUpdateFlags(Update::FitCursor);
567 case LFUN_BUFFER_END:
568 case LFUN_BUFFER_END_SELECT:
569 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
570 if (cur.depth() == 1)
571 needsUpdate |= cursorBottom(cur);
574 cur.screenUpdateFlags(Update::FitCursor);
577 case LFUN_INSET_BEGIN:
578 case LFUN_INSET_BEGIN_SELECT:
579 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
580 if (cur.depth() == 1 || !cur.top().at_begin())
581 needsUpdate |= cursorTop(cur);
584 cur.screenUpdateFlags(Update::FitCursor);
588 case LFUN_INSET_END_SELECT:
589 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
590 if (cur.depth() == 1 || !cur.top().at_end())
591 needsUpdate |= cursorBottom(cur);
594 cur.screenUpdateFlags(Update::FitCursor);
597 case LFUN_INSET_SELECT_ALL:
598 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
599 || !cur.selEnd().at_end()) {
600 needsUpdate |= cur.selHandle(false);
601 needsUpdate |= cursorTop(cur);
602 needsUpdate |= cur.selHandle(true);
603 needsUpdate |= cursorBottom(cur);
606 cur.screenUpdateFlags(Update::FitCursor);
609 case LFUN_CHAR_FORWARD:
610 case LFUN_CHAR_FORWARD_SELECT:
611 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
612 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
613 needsUpdate |= cursorForward(cur);
615 if (!needsUpdate && oldTopSlice == cur.top()
616 && cur.boundary() == oldBoundary) {
618 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
620 // we will probably be moving out the inset, so we should execute
621 // the depm-mechanism, but only when the cursor has a place to
622 // go outside this inset, i.e. in a slice above.
623 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
624 && cur.pit() == cur.lastpit()) {
625 // The cursor hasn't changed yet. To give the
626 // DEPM the possibility of doing something we must
627 // provide it with two different cursors.
629 dummy.pos() = dummy.pit() = 0;
630 if (cur.bv().checkDepm(dummy, cur))
631 cur.forceBufferUpdate();
636 case LFUN_CHAR_BACKWARD:
637 case LFUN_CHAR_BACKWARD_SELECT:
638 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
639 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
640 needsUpdate |= cursorBackward(cur);
642 if (!needsUpdate && oldTopSlice == cur.top()
643 && cur.boundary() == oldBoundary) {
645 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
647 // we will probably be moving out the inset, so we should execute
648 // the depm-mechanism, but only when the cursor has a place to
649 // go outside this inset, i.e. in a slice above.
650 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
651 // The cursor hasn't changed yet. To give the
652 // DEPM the possibility of doing something we must
653 // provide it with two different cursors.
655 dummy.pos() = cur.lastpos();
656 dummy.pit() = cur.lastpit();
657 if (cur.bv().checkDepm(dummy, cur))
658 cur.forceBufferUpdate();
664 case LFUN_CHAR_LEFT_SELECT:
665 if (lyxrc.visual_cursor) {
666 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
667 needsUpdate |= cursorVisLeft(cur);
668 if (!needsUpdate && oldTopSlice == cur.top()
669 && cur.boundary() == oldBoundary) {
671 cmd = FuncRequest(LFUN_FINISHED_LEFT);
674 if (reverseDirectionNeeded(cur)) {
675 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
676 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
678 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
679 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
686 case LFUN_CHAR_RIGHT:
687 case LFUN_CHAR_RIGHT_SELECT:
688 if (lyxrc.visual_cursor) {
689 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
690 needsUpdate |= cursorVisRight(cur);
691 if (!needsUpdate && oldTopSlice == cur.top()
692 && cur.boundary() == oldBoundary) {
694 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
697 if (reverseDirectionNeeded(cur)) {
698 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
699 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
701 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
702 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
711 case LFUN_DOWN_SELECT:
714 // stop/start the selection
715 bool select = cmd.action() == LFUN_DOWN_SELECT ||
716 cmd.action() == LFUN_UP_SELECT;
718 // move cursor up/down
719 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
720 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
722 if (!atFirstOrLastRow) {
723 needsUpdate |= cur.selHandle(select);
724 cur.selHandle(select);
725 cur.upDownInText(up, needsUpdate);
726 needsUpdate |= cur.beforeDispatchCursor().inMathed();
728 // if the cursor cannot be moved up or down do not remove
729 // the selection right now, but wait for the next dispatch.
731 needsUpdate |= cur.selHandle(select);
732 cur.upDownInText(up, needsUpdate);
739 case LFUN_PARAGRAPH_UP:
740 case LFUN_PARAGRAPH_UP_SELECT:
741 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
742 needsUpdate |= cursorUpParagraph(cur);
745 case LFUN_PARAGRAPH_DOWN:
746 case LFUN_PARAGRAPH_DOWN_SELECT:
747 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
748 needsUpdate |= cursorDownParagraph(cur);
751 case LFUN_LINE_BEGIN:
752 case LFUN_LINE_BEGIN_SELECT:
753 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
754 needsUpdate |= tm->cursorHome(cur);
758 case LFUN_LINE_END_SELECT:
759 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
760 needsUpdate |= tm->cursorEnd(cur);
763 case LFUN_SECTION_SELECT: {
764 Buffer const & buf = *cur.buffer();
765 pit_type const pit = cur.pit();
766 ParagraphList & pars = buf.text().paragraphs();
767 ParagraphList::iterator bgn = pars.begin();
768 // The first paragraph of the area to be selected:
769 ParagraphList::iterator start = boost::next(bgn, pit);
770 // The final paragraph of area to be selected:
771 ParagraphList::iterator finish = start;
772 ParagraphList::iterator end = pars.end();
774 int const thistoclevel = start->layout().toclevel;
775 if (thistoclevel == Layout::NOT_IN_TOC)
779 Cursor const old_cur = cur;
780 needsUpdate |= cur.selHandle(true);
782 // Move out (down) from this section header
786 // Seek the one (on same level) below
787 for (; finish != end; ++finish, ++cur.pit()) {
788 int const toclevel = finish->layout().toclevel;
789 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
792 cur.pos() = cur.lastpos();
794 needsUpdate |= cur != old_cur;
798 case LFUN_WORD_RIGHT:
799 case LFUN_WORD_RIGHT_SELECT:
800 if (lyxrc.visual_cursor) {
801 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
802 needsUpdate |= cursorVisRightOneWord(cur);
803 if (!needsUpdate && oldTopSlice == cur.top()
804 && cur.boundary() == oldBoundary) {
806 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
809 if (reverseDirectionNeeded(cur)) {
810 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
811 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
813 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
814 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
821 case LFUN_WORD_FORWARD:
822 case LFUN_WORD_FORWARD_SELECT:
823 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
824 needsUpdate |= cursorForwardOneWord(cur);
826 if (!needsUpdate && oldTopSlice == cur.top()
827 && cur.boundary() == oldBoundary) {
829 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
831 // we will probably be moving out the inset, so we should execute
832 // the depm-mechanism, but only when the cursor has a place to
833 // go outside this inset, i.e. in a slice above.
834 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
835 && cur.pit() == cur.lastpit()) {
836 // The cursor hasn't changed yet. To give the
837 // DEPM the possibility of doing something we must
838 // provide it with two different cursors.
840 dummy.pos() = dummy.pit() = 0;
841 if (cur.bv().checkDepm(dummy, cur))
842 cur.forceBufferUpdate();;
848 case LFUN_WORD_LEFT_SELECT:
849 if (lyxrc.visual_cursor) {
850 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
851 needsUpdate |= cursorVisLeftOneWord(cur);
852 if (!needsUpdate && oldTopSlice == cur.top()
853 && cur.boundary() == oldBoundary) {
855 cmd = FuncRequest(LFUN_FINISHED_LEFT);
858 if (reverseDirectionNeeded(cur)) {
859 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
860 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
862 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
863 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
870 case LFUN_WORD_BACKWARD:
871 case LFUN_WORD_BACKWARD_SELECT:
872 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
873 needsUpdate |= cursorBackwardOneWord(cur);
875 if (!needsUpdate && oldTopSlice == cur.top()
876 && cur.boundary() == oldBoundary) {
878 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
880 // we will probably be moving out the inset, so we should execute
881 // the depm-mechanism, but only when the cursor has a place to
882 // go outside this inset, i.e. in a slice above.
883 if (cur.depth() > 1 && cur.pos() == 0
885 // The cursor hasn't changed yet. To give the
886 // DEPM the possibility of doing something we must
887 // provide it with two different cursors.
889 dummy.pos() = cur.lastpos();
890 dummy.pit() = cur.lastpit();
891 if (cur.bv().checkDepm(dummy, cur))
892 cur.forceBufferUpdate();
897 case LFUN_WORD_SELECT: {
898 selectWord(cur, WHOLE_WORD);
899 finishChange(cur, true);
903 case LFUN_NEWLINE_INSERT: {
904 InsetNewlineParams inp;
905 docstring arg = cmd.argument();
906 // this avoids a double undo
907 // FIXME: should not be needed, ideally
908 if (!cur.selection())
910 cap::replaceSelection(cur);
911 if (arg == "linebreak")
912 inp.kind = InsetNewlineParams::LINEBREAK;
914 inp.kind = InsetNewlineParams::NEWLINE;
915 cur.insert(new InsetNewline(inp));
917 moveCursor(cur, false);
921 case LFUN_TAB_INSERT: {
922 bool const multi_par_selection = cur.selection() &&
923 cur.selBegin().pit() != cur.selEnd().pit();
924 if (multi_par_selection) {
925 // If there is a multi-paragraph selection, a tab is inserted
926 // at the beginning of each paragraph.
927 cur.recordUndoSelection();
928 pit_type const pit_end = cur.selEnd().pit();
929 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
930 pars_[pit].insertChar(0, '\t',
931 bv->buffer().params().trackChanges);
932 // Update the selection pos to make sure the selection does not
933 // change as the inserted tab will increase the logical pos.
934 if (cur.realAnchor().pit() == pit)
935 cur.realAnchor().forwardPos();
936 if (cur.pit() == pit)
941 // Maybe we shouldn't allow tabs within a line, because they
942 // are not (yet) aligned as one might do expect.
943 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
949 case LFUN_TAB_DELETE: {
950 bool const tc = bv->buffer().params().trackChanges;
951 if (cur.selection()) {
952 // If there is a selection, a tab (if present) is removed from
953 // the beginning of each paragraph.
954 cur.recordUndoSelection();
955 pit_type const pit_end = cur.selEnd().pit();
956 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
957 Paragraph & par = paragraphs()[pit];
960 char_type const c = par.getChar(0);
961 if (c == '\t' || c == ' ') {
962 // remove either 1 tab or 4 spaces.
963 int const n = (c == ' ' ? 4 : 1);
964 for (int i = 0; i < n
965 && !par.empty() && par.getChar(0) == c; ++i) {
966 if (cur.pit() == pit)
968 if (cur.realAnchor().pit() == pit
969 && cur.realAnchor().pos() > 0 )
970 cur.realAnchor().backwardPos();
971 par.eraseChar(0, tc);
977 // If there is no selection, try to remove a tab or some spaces
978 // before the position of the cursor.
979 Paragraph & par = paragraphs()[cur.pit()];
980 pos_type const pos = cur.pos();
985 char_type const c = par.getChar(pos - 1);
989 par.eraseChar(cur.pos(), tc);
991 for (int n_spaces = 0;
993 && par.getChar(cur.pos() - 1) == ' '
997 par.eraseChar(cur.pos(), tc);
1004 case LFUN_CHAR_DELETE_FORWARD:
1005 if (!cur.selection()) {
1006 if (cur.pos() == cur.paragraph().size())
1007 // Par boundary, force full-screen update
1008 singleParUpdate = false;
1009 needsUpdate |= erase(cur);
1011 // It is possible to make it a lot faster still
1012 // just comment out the line below...
1014 cutSelection(cur, true, false);
1015 singleParUpdate = false;
1017 moveCursor(cur, false);
1020 case LFUN_CHAR_DELETE_BACKWARD:
1021 if (!cur.selection()) {
1022 if (bv->getIntl().getTransManager().backspace()) {
1023 // Par boundary, full-screen update
1025 singleParUpdate = false;
1026 needsUpdate |= backspace(cur);
1028 // It is possible to make it a lot faster still
1029 // just comment out the line below...
1032 cutSelection(cur, true, false);
1033 singleParUpdate = false;
1037 case LFUN_BREAK_PARAGRAPH:
1038 cap::replaceSelection(cur);
1039 breakParagraph(cur, cmd.argument() == "inverse");
1043 case LFUN_INSET_INSERT: {
1046 // We have to avoid triggering InstantPreview loading
1047 // before inserting into the document. See bug #5626.
1048 bool loaded = bv->buffer().isFullyLoaded();
1049 bv->buffer().setFullyLoaded(false);
1050 Inset * inset = createInset(&bv->buffer(), cmd);
1051 bv->buffer().setFullyLoaded(loaded);
1054 // FIXME (Abdel 01/02/2006):
1055 // What follows would be a partial fix for bug 2154:
1056 // http://www.lyx.org/trac/ticket/2154
1057 // This automatically put the label inset _after_ a
1058 // numbered section. It should be possible to extend the mechanism
1059 // to any kind of LateX environement.
1060 // The correct way to fix that bug would be at LateX generation.
1061 // I'll let the code here for reference as it could be used for some
1062 // other feature like "automatic labelling".
1064 Paragraph & par = pars_[cur.pit()];
1065 if (inset->lyxCode() == LABEL_CODE
1066 && par.layout().labeltype == LABEL_COUNTER) {
1067 // Go to the end of the paragraph
1068 // Warning: Because of Change-Tracking, the last
1069 // position is 'size()' and not 'size()-1':
1070 cur.pos() = par.size();
1071 // Insert a new paragraph
1072 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1076 if (cur.selection())
1077 cutSelection(cur, true, false);
1079 if (inset->editable() && inset->asInsetText())
1080 inset->edit(cur, true);
1084 // trigger InstantPreview now
1085 if (inset->lyxCode() == EXTERNAL_CODE) {
1086 InsetExternal & ins =
1087 static_cast<InsetExternal &>(*inset);
1088 ins.updatePreview();
1095 case LFUN_INSET_DISSOLVE: {
1096 if (dissolveInset(cur)) {
1098 cur.forceBufferUpdate();
1103 case LFUN_SET_GRAPHICS_GROUP: {
1104 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1110 string id = to_utf8(cmd.argument());
1111 string grp = graphics::getGroupParams(bv->buffer(), id);
1112 InsetGraphicsParams tmp, inspar = ins->getParams();
1115 inspar.groupId = to_utf8(cmd.argument());
1117 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1118 tmp.filename = inspar.filename;
1122 ins->setParams(inspar);
1125 case LFUN_SPACE_INSERT:
1126 if (cur.paragraph().layout().free_spacing)
1127 insertChar(cur, ' ');
1129 doInsertInset(cur, this, cmd, false, false);
1132 moveCursor(cur, false);
1135 case LFUN_SPECIALCHAR_INSERT: {
1136 string const name = to_utf8(cmd.argument());
1137 if (name == "hyphenation")
1138 specialChar(cur, InsetSpecialChar::HYPHENATION);
1139 else if (name == "ligature-break")
1140 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1141 else if (name == "slash")
1142 specialChar(cur, InsetSpecialChar::SLASH);
1143 else if (name == "nobreakdash")
1144 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1145 else if (name == "dots")
1146 specialChar(cur, InsetSpecialChar::LDOTS);
1147 else if (name == "end-of-sentence")
1148 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1149 else if (name == "menu-separator")
1150 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1151 else if (name.empty())
1152 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1154 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1158 case LFUN_WORD_UPCASE:
1159 changeCase(cur, text_uppercase);
1162 case LFUN_WORD_LOWCASE:
1163 changeCase(cur, text_lowercase);
1166 case LFUN_WORD_CAPITALIZE:
1167 changeCase(cur, text_capitalization);
1170 case LFUN_CHARS_TRANSPOSE:
1171 charsTranspose(cur);
1175 cur.message(_("Paste"));
1176 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1177 cap::replaceSelection(cur);
1179 // without argument?
1180 string const arg = to_utf8(cmd.argument());
1182 if (theClipboard().isInternal())
1183 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1184 else if (theClipboard().hasGraphicsContents()
1185 && !theClipboard().hasTextContents())
1186 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1188 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1189 } else if (isStrUnsignedInt(arg)) {
1190 // we have a numerical argument
1191 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1192 convert<unsigned int>(arg));
1194 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1196 type = Clipboard::PdfGraphicsType;
1197 else if (arg == "png")
1198 type = Clipboard::PngGraphicsType;
1199 else if (arg == "jpeg")
1200 type = Clipboard::JpegGraphicsType;
1201 else if (arg == "linkback")
1202 type = Clipboard::LinkBackGraphicsType;
1203 else if (arg == "emf")
1204 type = Clipboard::EmfGraphicsType;
1205 else if (arg == "wmf")
1206 type = Clipboard::WmfGraphicsType;
1209 LASSERT(false, /**/);
1211 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1214 bv->buffer().errors("Paste");
1215 cur.clearSelection(); // bug 393
1221 cutSelection(cur, true, true);
1222 cur.message(_("Cut"));
1227 cur.message(_("Copy"));
1230 case LFUN_SERVER_GET_XY:
1231 cur.message(from_utf8(
1232 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1233 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1236 case LFUN_SERVER_SET_XY: {
1239 istringstream is(to_utf8(cmd.argument()));
1242 lyxerr << "SETXY: Could not parse coordinates in '"
1243 << to_utf8(cmd.argument()) << endl;
1245 tm->setCursorFromCoordinates(cur, x, y);
1249 case LFUN_SERVER_GET_LAYOUT:
1250 cur.message(cur.paragraph().layout().name());
1254 docstring layout = cmd.argument();
1255 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1257 Paragraph const & para = cur.paragraph();
1258 docstring const old_layout = para.layout().name();
1259 DocumentClass const & tclass = bv->buffer().params().documentClass();
1262 layout = tclass.defaultLayoutName();
1264 if (owner_->forcePlainLayout())
1265 // in this case only the empty layout is allowed
1266 layout = tclass.plainLayoutName();
1267 else if (para.usePlainLayout()) {
1268 // in this case, default layout maps to empty layout
1269 if (layout == tclass.defaultLayoutName())
1270 layout = tclass.plainLayoutName();
1272 // otherwise, the empty layout maps to the default
1273 if (layout == tclass.plainLayoutName())
1274 layout = tclass.defaultLayoutName();
1277 bool hasLayout = tclass.hasLayout(layout);
1279 // If the entry is obsolete, use the new one instead.
1281 docstring const & obs = tclass[layout].obsoleted_by();
1287 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1288 from_utf8(N_(" not known")));
1292 bool change_layout = (old_layout != layout);
1294 if (!change_layout && cur.selection() &&
1295 cur.selBegin().pit() != cur.selEnd().pit())
1297 pit_type spit = cur.selBegin().pit();
1298 pit_type epit = cur.selEnd().pit() + 1;
1299 while (spit != epit) {
1300 if (pars_[spit].layout().name() != old_layout) {
1301 change_layout = true;
1309 setLayout(cur, layout);
1314 case LFUN_CLIPBOARD_PASTE:
1315 cur.clearSelection();
1316 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1317 cmd.argument() == "paragraph");
1318 bv->buffer().errors("Paste");
1321 case LFUN_PRIMARY_SELECTION_PASTE:
1322 pasteString(cur, theSelection().get(),
1323 cmd.argument() == "paragraph");
1326 case LFUN_SELECTION_PASTE:
1327 // Copy the selection buffer to the clipboard stack,
1328 // because we want it to appear in the "Edit->Paste
1330 cap::copySelectionToStack();
1331 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1332 bv->buffer().errors("Paste");
1335 case LFUN_UNICODE_INSERT: {
1336 if (cmd.argument().empty())
1338 docstring hexstring = cmd.argument();
1339 if (isHex(hexstring)) {
1340 char_type c = hexToInt(hexstring);
1341 if (c >= 32 && c < 0x10ffff) {
1342 lyxerr << "Inserting c: " << c << endl;
1343 docstring s = docstring(1, c);
1344 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1350 case LFUN_QUOTE_INSERT: {
1351 Paragraph & par = cur.paragraph();
1352 pos_type pos = cur.pos();
1353 BufferParams const & bufparams = bv->buffer().params();
1354 Layout const & style = par.layout();
1355 InsetLayout const & ilayout = cur.inset().getLayout();
1356 if (!style.pass_thru && !ilayout.isPassThru()
1357 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1358 // this avoids a double undo
1359 // FIXME: should not be needed, ideally
1360 if (!cur.selection())
1362 cap::replaceSelection(cur);
1367 else if (cur.prevInset() && cur.prevInset()->isSpace())
1370 c = par.getChar(pos - 1);
1371 string arg = to_utf8(cmd.argument());
1372 cur.insert(new InsetQuotes(cur.buffer(), c, (arg == "single")
1373 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1377 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1381 case LFUN_DATE_INSERT: {
1382 string const format = cmd.argument().empty()
1383 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1384 string const time = formatted_time(current_time(), format);
1385 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1389 case LFUN_MOUSE_TRIPLE:
1390 if (cmd.button() == mouse_button::button1) {
1391 tm->cursorHome(cur);
1399 case LFUN_MOUSE_DOUBLE:
1400 if (cmd.button() == mouse_button::button1) {
1401 selectWord(cur, WHOLE_WORD_STRICT);
1406 // Single-click on work area
1407 case LFUN_MOUSE_PRESS: {
1408 // We are not marking a selection with the keyboard in any case.
1409 Cursor & bvcur = cur.bv().cursor();
1410 bvcur.setMark(false);
1411 switch (cmd.button()) {
1412 case mouse_button::button1:
1414 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1415 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1416 if (bvcur.wordSelection())
1417 selectWord(bvcur, WHOLE_WORD);
1420 case mouse_button::button2:
1421 // Middle mouse pasting.
1422 bv->mouseSetCursor(cur);
1424 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1425 "selection-paste ; primary-selection-paste paragraph"));
1426 cur.noScreenUpdate();
1429 case mouse_button::button3: {
1430 // Don't do anything if we right-click a
1431 // selection, a context menu will popup.
1432 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1433 && cur < bvcur.selectionEnd()) {
1434 cur.noScreenUpdate();
1437 if (!bv->mouseSetCursor(cur, false))
1438 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1444 } // switch (cmd.button())
1447 case LFUN_MOUSE_MOTION: {
1448 // Mouse motion with right or middle mouse do nothing for now.
1449 if (cmd.button() != mouse_button::button1) {
1450 cur.noScreenUpdate();
1453 // ignore motions deeper nested than the real anchor
1454 Cursor & bvcur = cur.bv().cursor();
1455 if (!bvcur.realAnchor().hasPart(cur)) {
1459 CursorSlice old = bvcur.top();
1461 int const wh = bv->workHeight();
1462 int const y = max(0, min(wh - 1, cmd.y()));
1464 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1465 cur.setTargetX(cmd.x());
1467 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1468 else if (cmd.y() < 0)
1469 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1470 // This is to allow jumping over large insets
1471 if (cur.top() == old) {
1473 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1474 else if (cmd.y() < 0)
1475 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1477 // We continue with our existing selection or start a new one, so don't
1478 // reset the anchor.
1479 bvcur.setCursor(cur);
1480 bvcur.setSelection(true);
1481 if (cur.top() == old) {
1482 // We didn't move one iota, so no need to update the screen.
1483 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1484 //cur.noScreenUpdate();
1490 case LFUN_MOUSE_RELEASE:
1491 switch (cmd.button()) {
1492 case mouse_button::button1:
1493 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1494 // If there is a new selection, update persistent selection;
1495 // otherwise, single click does not clear persistent selection
1497 if (cur.selection()) {
1498 // Finish selection. If double click,
1499 // cur is moved to the end of word by
1500 // selectWord but bvcur is current
1502 cur.bv().cursor().setSelection();
1503 // We might have removed an empty but drawn selection
1504 // (probably a margin)
1505 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1507 cur.noScreenUpdate();
1508 // FIXME: We could try to handle drag and drop of selection here.
1511 case mouse_button::button2:
1512 // Middle mouse pasting is handled at mouse press time,
1513 // see LFUN_MOUSE_PRESS.
1514 cur.noScreenUpdate();
1517 case mouse_button::button3:
1518 // Cursor was set at LFUN_MOUSE_PRESS time.
1519 // FIXME: If there is a selection we could try to handle a special
1520 // drag & drop context menu.
1521 cur.noScreenUpdate();
1524 case mouse_button::none:
1525 case mouse_button::button4:
1526 case mouse_button::button5:
1528 } // switch (cmd.button())
1532 case LFUN_SELF_INSERT: {
1533 if (cmd.argument().empty())
1536 // Automatically delete the currently selected
1537 // text and replace it with what is being
1538 // typed in now. Depends on lyxrc settings
1539 // "auto_region_delete", which defaults to
1542 if (lyxrc.auto_region_delete && cur.selection())
1543 cutSelection(cur, false, false);
1545 cur.clearSelection();
1547 docstring::const_iterator cit = cmd.argument().begin();
1548 docstring::const_iterator const end = cmd.argument().end();
1549 for (; cit != end; ++cit)
1550 bv->translateAndInsert(*cit, this, cur);
1553 moveCursor(cur, false);
1554 bv->bookmarkEditPosition();
1558 case LFUN_HYPERLINK_INSERT: {
1559 InsetCommandParams p(HYPERLINK_CODE);
1561 if (cur.selection()) {
1562 content = cur.selectionAsString(false);
1563 cutSelection(cur, true, false);
1565 p["target"] = (cmd.argument().empty()) ?
1566 content : cmd.argument();
1567 string const data = InsetCommand::params2string(p);
1568 if (p["target"].empty()) {
1569 bv->showDialog("href", data);
1571 FuncRequest fr(LFUN_INSET_INSERT, data);
1577 case LFUN_LABEL_INSERT: {
1578 InsetCommandParams p(LABEL_CODE);
1579 // Try to generate a valid label
1580 p["name"] = (cmd.argument().empty()) ?
1581 cur.getPossibleLabel() :
1583 string const data = InsetCommand::params2string(p);
1585 if (cmd.argument().empty()) {
1586 bv->showDialog("label", data);
1588 FuncRequest fr(LFUN_INSET_INSERT, data);
1594 case LFUN_INFO_INSERT: {
1596 if (cmd.argument().empty() && cur.selection()) {
1597 // if command argument is empty use current selection as parameter.
1598 docstring ds = cur.selectionAsString(false);
1599 cutSelection(cur, true, false);
1600 FuncRequest cmd0(cmd, ds);
1601 inset = createInset(cur.buffer(), cmd0);
1603 inset = createInset(cur.buffer(), cmd);
1608 insertInset(cur, inset);
1612 case LFUN_CAPTION_INSERT:
1613 case LFUN_FOOTNOTE_INSERT:
1614 case LFUN_NOTE_INSERT:
1615 case LFUN_FLEX_INSERT:
1616 case LFUN_BOX_INSERT:
1617 case LFUN_BRANCH_INSERT:
1618 case LFUN_PHANTOM_INSERT:
1619 case LFUN_ERT_INSERT:
1620 case LFUN_LISTING_INSERT:
1621 case LFUN_MARGINALNOTE_INSERT:
1622 case LFUN_ARGUMENT_INSERT:
1623 case LFUN_INDEX_INSERT:
1624 case LFUN_PREVIEW_INSERT:
1625 case LFUN_SCRIPT_INSERT:
1626 // Open the inset, and move the current selection
1628 doInsertInset(cur, this, cmd, true, true);
1630 // Some insets are numbered, others are shown in the outline pane so
1631 // let's update the labels and the toc backend.
1632 cur.forceBufferUpdate();
1635 case LFUN_TABULAR_INSERT:
1636 // if there were no arguments, just open the dialog
1637 if (doInsertInset(cur, this, cmd, false, true))
1640 bv->showDialog("tabularcreate");
1644 case LFUN_FLOAT_INSERT:
1645 case LFUN_FLOAT_WIDE_INSERT:
1646 case LFUN_WRAP_INSERT: {
1647 // will some text be moved into the inset?
1648 bool content = cur.selection();
1650 doInsertInset(cur, this, cmd, true, true);
1653 // If some text is moved into the inset, doInsertInset
1654 // puts the cursor outside the inset. To insert the
1655 // caption we put it back into the inset.
1659 ParagraphList & pars = cur.text()->paragraphs();
1661 DocumentClass const & tclass = bv->buffer().params().documentClass();
1663 // add a separate paragraph for the caption inset
1664 pars.push_back(Paragraph());
1665 pars.back().setInsetOwner(&cur.text()->inset());
1666 pars.back().setPlainOrDefaultLayout(tclass);
1667 int cap_pit = pars.size() - 1;
1669 // if an empty inset was created, we create an additional empty
1670 // paragraph at the bottom so that the user can choose where to put
1671 // the graphics (or table).
1673 pars.push_back(Paragraph());
1674 pars.back().setInsetOwner(&cur.text()->inset());
1675 pars.back().setPlainOrDefaultLayout(tclass);
1678 // reposition the cursor to the caption
1679 cur.pit() = cap_pit;
1681 // FIXME: This Text/Cursor dispatch handling is a mess!
1682 // We cannot use Cursor::dispatch here it needs access to up to
1684 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1685 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1686 cur.forceBufferUpdate();
1687 cur.screenUpdateFlags(Update::Force);
1688 // FIXME: When leaving the Float (or Wrap) inset we should
1689 // delete any empty paragraph left above or below the
1694 case LFUN_NOMENCL_INSERT: {
1695 InsetCommandParams p(NOMENCL_CODE);
1696 if (cmd.argument().empty())
1697 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1699 p["symbol"] = cmd.argument();
1700 string const data = InsetCommand::params2string(p);
1701 bv->showDialog("nomenclature", data);
1705 case LFUN_INDEX_PRINT: {
1706 InsetCommandParams p(INDEX_PRINT_CODE);
1707 if (cmd.argument().empty())
1708 p["type"] = from_ascii("idx");
1710 p["type"] = cmd.argument();
1711 string const data = InsetCommand::params2string(p);
1712 FuncRequest fr(LFUN_INSET_INSERT, data);
1717 case LFUN_NOMENCL_PRINT:
1718 case LFUN_NEWPAGE_INSERT:
1720 doInsertInset(cur, this, cmd, false, false);
1724 case LFUN_DEPTH_DECREMENT:
1725 changeDepth(cur, DEC_DEPTH);
1728 case LFUN_DEPTH_INCREMENT:
1729 changeDepth(cur, INC_DEPTH);
1732 case LFUN_MATH_DISPLAY:
1733 mathDispatch(cur, cmd, true);
1736 case LFUN_REGEXP_MODE:
1737 regexpDispatch(cur, cmd);
1740 case LFUN_MATH_MODE:
1741 if (cmd.argument() == "on")
1742 // don't pass "on" as argument
1743 // (it would appear literally in the first cell)
1744 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1746 mathDispatch(cur, cmd, false);
1749 case LFUN_MATH_MACRO:
1750 if (cmd.argument().empty())
1751 cur.errorMessage(from_utf8(N_("Missing argument")));
1753 string s = to_utf8(cmd.argument());
1754 string const s1 = token(s, ' ', 1);
1755 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1756 string const s2 = token(s, ' ', 2);
1757 MacroType type = MacroTypeNewcommand;
1759 type = MacroTypeDef;
1760 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1761 from_utf8(token(s, ' ', 0)), nargs, false, type);
1762 inset->setBuffer(bv->buffer());
1763 insertInset(cur, inset);
1765 // enter macro inset and select the name
1767 cur.top().pos() = cur.top().lastpos();
1769 cur.setSelection(true);
1770 cur.top().pos() = 0;
1774 // passthrough hat and underscore outside mathed:
1775 case LFUN_MATH_SUBSCRIPT:
1776 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1778 case LFUN_MATH_SUPERSCRIPT:
1779 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1782 case LFUN_MATH_INSERT:
1783 case LFUN_MATH_AMS_MATRIX:
1784 case LFUN_MATH_MATRIX:
1785 case LFUN_MATH_DELIM:
1786 case LFUN_MATH_BIGDELIM: {
1788 cap::replaceSelection(cur);
1789 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1790 checkAndActivateInset(cur, true);
1791 LASSERT(cur.inMathed(), /**/);
1796 case LFUN_FONT_EMPH: {
1797 Font font(ignore_font, ignore_language);
1798 font.fontInfo().setEmph(FONT_TOGGLE);
1799 toggleAndShow(cur, this, font);
1803 case LFUN_FONT_ITAL: {
1804 Font font(ignore_font, ignore_language);
1805 font.fontInfo().setShape(ITALIC_SHAPE);
1806 toggleAndShow(cur, this, font);
1810 case LFUN_FONT_BOLD:
1811 case LFUN_FONT_BOLDSYMBOL: {
1812 Font font(ignore_font, ignore_language);
1813 font.fontInfo().setSeries(BOLD_SERIES);
1814 toggleAndShow(cur, this, font);
1818 case LFUN_FONT_NOUN: {
1819 Font font(ignore_font, ignore_language);
1820 font.fontInfo().setNoun(FONT_TOGGLE);
1821 toggleAndShow(cur, this, font);
1825 case LFUN_FONT_TYPEWRITER: {
1826 Font font(ignore_font, ignore_language);
1827 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1828 toggleAndShow(cur, this, font);
1832 case LFUN_FONT_SANS: {
1833 Font font(ignore_font, ignore_language);
1834 font.fontInfo().setFamily(SANS_FAMILY);
1835 toggleAndShow(cur, this, font);
1839 case LFUN_FONT_ROMAN: {
1840 Font font(ignore_font, ignore_language);
1841 font.fontInfo().setFamily(ROMAN_FAMILY);
1842 toggleAndShow(cur, this, font);
1846 case LFUN_FONT_DEFAULT: {
1847 Font font(inherit_font, ignore_language);
1848 toggleAndShow(cur, this, font);
1852 case LFUN_FONT_STRIKEOUT: {
1853 Font font(ignore_font, ignore_language);
1854 font.fontInfo().setStrikeout(FONT_TOGGLE);
1855 toggleAndShow(cur, this, font);
1859 case LFUN_FONT_UULINE: {
1860 Font font(ignore_font, ignore_language);
1861 font.fontInfo().setUuline(FONT_TOGGLE);
1862 toggleAndShow(cur, this, font);
1866 case LFUN_FONT_UWAVE: {
1867 Font font(ignore_font, ignore_language);
1868 font.fontInfo().setUwave(FONT_TOGGLE);
1869 toggleAndShow(cur, this, font);
1873 case LFUN_FONT_UNDERLINE: {
1874 Font font(ignore_font, ignore_language);
1875 font.fontInfo().setUnderbar(FONT_TOGGLE);
1876 toggleAndShow(cur, this, font);
1880 case LFUN_FONT_SIZE: {
1881 Font font(ignore_font, ignore_language);
1882 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1883 toggleAndShow(cur, this, font);
1887 case LFUN_LANGUAGE: {
1888 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1891 Font font(ignore_font, lang);
1892 toggleAndShow(cur, this, font);
1896 case LFUN_TEXTSTYLE_APPLY:
1897 toggleAndShow(cur, this, freefont, toggleall);
1898 cur.message(_("Character set"));
1901 // Set the freefont using the contents of \param data dispatched from
1902 // the frontends and apply it at the current cursor location.
1903 case LFUN_TEXTSTYLE_UPDATE: {
1906 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1909 toggleAndShow(cur, this, freefont, toggleall);
1910 cur.message(_("Character set"));
1912 lyxerr << "Argument not ok";
1917 case LFUN_FINISHED_LEFT:
1918 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1919 // We're leaving an inset, going left. If the inset is LTR, we're
1920 // leaving from the front, so we should not move (remain at --- but
1921 // not in --- the inset). If the inset is RTL, move left, without
1922 // entering the inset itself; i.e., move to after the inset.
1923 if (cur.paragraph().getFontSettings(
1924 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1925 cursorVisLeft(cur, true);
1928 case LFUN_FINISHED_RIGHT:
1929 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1930 // We're leaving an inset, going right. If the inset is RTL, we're
1931 // leaving from the front, so we should not move (remain at --- but
1932 // not in --- the inset). If the inset is LTR, move right, without
1933 // entering the inset itself; i.e., move to after the inset.
1934 if (!cur.paragraph().getFontSettings(
1935 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1936 cursorVisRight(cur, true);
1939 case LFUN_FINISHED_BACKWARD:
1940 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1943 case LFUN_FINISHED_FORWARD:
1944 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1946 cur.setCurrentFont();
1949 case LFUN_LAYOUT_PARAGRAPH: {
1951 params2string(cur.paragraph(), data);
1952 data = "show\n" + data;
1953 bv->showDialog("paragraph", data);
1957 case LFUN_PARAGRAPH_UPDATE: {
1959 params2string(cur.paragraph(), data);
1961 // Will the paragraph accept changes from the dialog?
1963 cur.inset().allowParagraphCustomization(cur.idx());
1965 data = "update " + convert<string>(accept) + '\n' + data;
1966 bv->updateDialog("paragraph", data);
1970 case LFUN_ACCENT_UMLAUT:
1971 case LFUN_ACCENT_CIRCUMFLEX:
1972 case LFUN_ACCENT_GRAVE:
1973 case LFUN_ACCENT_ACUTE:
1974 case LFUN_ACCENT_TILDE:
1975 case LFUN_ACCENT_CEDILLA:
1976 case LFUN_ACCENT_MACRON:
1977 case LFUN_ACCENT_DOT:
1978 case LFUN_ACCENT_UNDERDOT:
1979 case LFUN_ACCENT_UNDERBAR:
1980 case LFUN_ACCENT_CARON:
1981 case LFUN_ACCENT_BREVE:
1982 case LFUN_ACCENT_TIE:
1983 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1984 case LFUN_ACCENT_CIRCLE:
1985 case LFUN_ACCENT_OGONEK:
1986 theApp()->handleKeyFunc(cmd.action());
1987 if (!cmd.argument().empty())
1988 // FIXME: Are all these characters encoded in one byte in utf8?
1989 bv->translateAndInsert(cmd.argument()[0], this, cur);
1990 cur.screenUpdateFlags(Update::FitCursor);
1993 case LFUN_FLOAT_LIST_INSERT: {
1994 DocumentClass const & tclass = bv->buffer().params().documentClass();
1995 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1997 if (cur.selection())
1998 cutSelection(cur, true, false);
1999 breakParagraph(cur);
2001 if (cur.lastpos() != 0) {
2002 cursorBackward(cur);
2003 breakParagraph(cur);
2006 docstring const laystr = cur.inset().usePlainLayout() ?
2007 tclass.plainLayoutName() :
2008 tclass.defaultLayoutName();
2009 setLayout(cur, laystr);
2010 ParagraphParameters p;
2011 // FIXME If this call were replaced with one to clearParagraphParams(),
2012 // then we could get rid of this method altogether.
2013 setParagraphs(cur, p);
2014 // FIXME This should be simplified when InsetFloatList takes a
2015 // Buffer in its constructor.
2016 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2017 ifl->setBuffer(bv->buffer());
2018 insertInset(cur, ifl);
2021 lyxerr << "Non-existent float type: "
2022 << to_utf8(cmd.argument()) << endl;
2027 case LFUN_CHANGE_ACCEPT: {
2028 acceptOrRejectChanges(cur, ACCEPT);
2032 case LFUN_CHANGE_REJECT: {
2033 acceptOrRejectChanges(cur, REJECT);
2037 case LFUN_THESAURUS_ENTRY: {
2038 docstring arg = cmd.argument();
2040 arg = cur.selectionAsString(false);
2042 if (arg.size() > 100 || arg.empty()) {
2043 // Get word or selection
2044 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2045 arg = cur.selectionAsString(false);
2046 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2049 bv->showDialog("thesaurus", to_utf8(arg));
2053 case LFUN_SPELLING_ADD: {
2054 docstring word = from_utf8(cmd.getArg(0));
2057 word = cur.selectionAsString(false);
2059 if (word.size() > 100 || word.empty()) {
2060 // Get word or selection
2061 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2062 word = cur.selectionAsString(false);
2064 lang = const_cast<Language *>(cur.getFont().language());
2066 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2067 WordLangTuple wl(word, lang);
2068 theSpellChecker()->insert(wl);
2072 case LFUN_SPELLING_IGNORE: {
2073 docstring word = from_utf8(cmd.getArg(0));
2076 word = cur.selectionAsString(false);
2078 if (word.size() > 100 || word.empty()) {
2079 // Get word or selection
2080 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2081 word = cur.selectionAsString(false);
2083 lang = const_cast<Language *>(cur.getFont().language());
2085 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2086 WordLangTuple wl(word, lang);
2087 theSpellChecker()->accept(wl);
2091 case LFUN_SPELLING_REMOVE: {
2092 docstring word = from_utf8(cmd.getArg(0));
2095 word = cur.selectionAsString(false);
2097 if (word.size() > 100 || word.empty()) {
2098 // Get word or selection
2099 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2100 word = cur.selectionAsString(false);
2102 lang = const_cast<Language *>(cur.getFont().language());
2104 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2105 WordLangTuple wl(word, lang);
2106 theSpellChecker()->remove(wl);
2110 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2111 // Given data, an encoding of the ParagraphParameters
2112 // generated in the Paragraph dialog, this function sets
2113 // the current paragraph, or currently selected paragraphs,
2115 // NOTE: This function overrides all existing settings.
2116 setParagraphs(cur, cmd.argument());
2117 cur.message(_("Paragraph layout set"));
2121 case LFUN_PARAGRAPH_PARAMS: {
2122 // Given data, an encoding of the ParagraphParameters as we'd
2123 // find them in a LyX file, this function modifies the current paragraph,
2124 // or currently selected paragraphs.
2125 // NOTE: This function only modifies, and does not override, existing
2127 setParagraphs(cur, cmd.argument(), true);
2128 cur.message(_("Paragraph layout set"));
2133 if (cur.selection()) {
2134 cur.setSelection(false);
2137 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2138 // correct, but I'm not 100% sure -- dov, 071019
2139 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2143 case LFUN_OUTLINE_UP:
2144 outline(OutlineUp, cur);
2145 setCursor(cur, cur.pit(), 0);
2146 cur.forceBufferUpdate();
2150 case LFUN_OUTLINE_DOWN:
2151 outline(OutlineDown, cur);
2152 setCursor(cur, cur.pit(), 0);
2153 cur.forceBufferUpdate();
2157 case LFUN_OUTLINE_IN:
2158 outline(OutlineIn, cur);
2159 cur.forceBufferUpdate();
2163 case LFUN_OUTLINE_OUT:
2164 outline(OutlineOut, cur);
2165 cur.forceBufferUpdate();
2170 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2175 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2177 if (lyxrc.spellcheck_continuously && !needsUpdate && cur.inTexted()) {
2178 // Check for misspelled text
2179 // The redraw is useful because of the painting of
2180 // misspelled markers depends on the cursor position.
2181 // Trigger a redraw for cursor moves inside misspelled text.
2182 if (cur.paragraph().id() != last_pid || cur.pos() != last_pos) {
2183 needsUpdate |= last_misspelled || cur.paragraph().isMisspelled(cur.pos());
2187 // FIXME: The cursor flag is reset two lines below
2188 // so we need to check here if some of the LFUN did touch that.
2189 // for now only Text::erase() and Text::backspace() do that.
2190 // The plan is to verify all the LFUNs and then to remove this
2191 // singleParUpdate boolean altogether.
2192 if (cur.result().screenUpdate() & Update::Force) {
2193 singleParUpdate = false;
2197 // FIXME: the following code should go in favor of fine grained
2198 // update flag treatment.
2199 if (singleParUpdate) {
2200 // Inserting characters does not change par height in general. So, try
2201 // to update _only_ this paragraph. BufferView will detect if a full
2202 // metrics update is needed anyway.
2203 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2208 && &oldTopSlice.inset() == &cur.inset()
2209 && oldTopSlice.idx() == cur.idx()
2210 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2211 && !cur.selection())
2212 // FIXME: it would be better if we could just do this
2214 //if (cur.result().update() != Update::FitCursor)
2215 // cur.noScreenUpdate();
2217 // But some LFUNs do not set Update::FitCursor when needed, so we
2218 // do it for all. This is not very harmfull as FitCursor will provoke
2219 // a full redraw only if needed but still, a proper review of all LFUN
2220 // should be done and this needsUpdate boolean can then be removed.
2221 cur.screenUpdateFlags(Update::FitCursor);
2223 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2227 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2228 FuncStatus & flag) const
2230 LASSERT(cur.text() == this, /**/);
2232 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2234 InsetCode code = NO_CODE;
2236 switch (cmd.action()) {
2238 case LFUN_DEPTH_DECREMENT:
2239 enable = changeDepthAllowed(cur, DEC_DEPTH);
2242 case LFUN_DEPTH_INCREMENT:
2243 enable = changeDepthAllowed(cur, INC_DEPTH);
2247 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2250 case LFUN_DIALOG_SHOW_NEW_INSET:
2251 if (cmd.argument() == "bibitem")
2252 code = BIBITEM_CODE;
2253 else if (cmd.argument() == "bibtex") {
2255 // not allowed in description items
2256 enable = !inDescriptionItem(cur);
2258 else if (cmd.argument() == "box")
2260 else if (cmd.argument() == "branch")
2262 else if (cmd.argument() == "citation")
2264 else if (cmd.argument() == "ert")
2266 else if (cmd.argument() == "external")
2267 code = EXTERNAL_CODE;
2268 else if (cmd.argument() == "float")
2270 else if (cmd.argument() == "graphics")
2271 code = GRAPHICS_CODE;
2272 else if (cmd.argument() == "href")
2273 code = HYPERLINK_CODE;
2274 else if (cmd.argument() == "include")
2275 code = INCLUDE_CODE;
2276 else if (cmd.argument() == "index")
2278 else if (cmd.argument() == "index_print")
2279 code = INDEX_PRINT_CODE;
2280 else if (cmd.argument() == "nomenclature")
2281 code = NOMENCL_CODE;
2282 else if (cmd.argument() == "nomencl_print")
2283 code = NOMENCL_PRINT_CODE;
2284 else if (cmd.argument() == "label")
2286 else if (cmd.argument() == "line")
2288 else if (cmd.argument() == "note")
2290 else if (cmd.argument() == "phantom")
2291 code = PHANTOM_CODE;
2292 else if (cmd.argument() == "ref")
2294 else if (cmd.argument() == "space")
2296 else if (cmd.argument() == "toc")
2298 else if (cmd.argument() == "vspace")
2300 else if (cmd.argument() == "wrap")
2302 else if (cmd.argument() == "listings")
2303 code = LISTINGS_CODE;
2306 case LFUN_ERT_INSERT:
2309 case LFUN_LISTING_INSERT:
2310 code = LISTINGS_CODE;
2311 // not allowed in description items
2312 enable = !inDescriptionItem(cur);
2314 case LFUN_FOOTNOTE_INSERT:
2317 case LFUN_TABULAR_INSERT:
2318 code = TABULAR_CODE;
2320 case LFUN_MARGINALNOTE_INSERT:
2323 case LFUN_FLOAT_INSERT:
2324 case LFUN_FLOAT_WIDE_INSERT:
2325 // FIXME: If there is a selection, we should check whether there
2326 // are floats in the selection, but this has performance issues, see
2327 // LFUN_CHANGE_ACCEPT/REJECT.
2329 if (inDescriptionItem(cur))
2330 // not allowed in description items
2333 InsetCode const inset_code = cur.inset().lyxCode();
2335 // algorithm floats cannot be put in another float
2336 if (to_utf8(cmd.argument()) == "algorithm") {
2337 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2341 // for figures and tables: only allow in another
2342 // float or wrap if it is of the same type and
2343 // not a subfloat already
2344 if(cur.inset().lyxCode() == code) {
2345 InsetFloat const & ins =
2346 static_cast<InsetFloat const &>(cur.inset());
2347 enable = ins.params().type == to_utf8(cmd.argument())
2348 && !ins.params().subfloat;
2349 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2350 InsetWrap const & ins =
2351 static_cast<InsetWrap const &>(cur.inset());
2352 enable = ins.params().type == to_utf8(cmd.argument());
2356 case LFUN_WRAP_INSERT:
2358 // not allowed in description items
2359 enable = !inDescriptionItem(cur);
2361 case LFUN_FLOAT_LIST_INSERT: {
2362 code = FLOAT_LIST_CODE;
2363 // not allowed in description items
2364 enable = !inDescriptionItem(cur);
2366 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2367 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2368 // make sure we know about such floats
2369 if (cit == floats.end() ||
2370 // and that we know how to generate a list of them
2371 (!cit->second.needsFloatPkg() && cit->second.listCommand().empty())) {
2372 flag.setUnknown(true);
2373 // probably not necessary, but...
2379 case LFUN_CAPTION_INSERT:
2380 code = CAPTION_CODE;
2381 // not allowed in description items
2382 enable = !inDescriptionItem(cur);
2384 case LFUN_NOTE_INSERT:
2386 // in commands (sections etc.) and description items,
2387 // only Notes are allowed
2388 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2389 (!cur.paragraph().layout().isCommand()
2390 && !inDescriptionItem(cur)));
2392 case LFUN_FLEX_INSERT: {
2394 string s = cmd.getArg(0);
2396 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2397 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2398 il.lyxtype() != InsetLayout::CUSTOM &&
2399 il.lyxtype() != InsetLayout::ELEMENT &&
2400 il.lyxtype ()!= InsetLayout::STANDARD)
2404 case LFUN_BOX_INSERT:
2407 case LFUN_BRANCH_INSERT:
2409 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2410 && cur.buffer()->params().branchlist().empty())
2413 case LFUN_PHANTOM_INSERT:
2414 code = PHANTOM_CODE;
2416 case LFUN_LABEL_INSERT:
2419 case LFUN_INFO_INSERT:
2422 case LFUN_ARGUMENT_INSERT: {
2424 Layout const & lay = cur.paragraph().layout();
2425 int const numargs = lay.reqargs + lay.optargs;
2426 enable = cur.paragraph().insetList().count(ARG_CODE) < numargs;
2429 case LFUN_INDEX_INSERT:
2432 case LFUN_INDEX_PRINT:
2433 code = INDEX_PRINT_CODE;
2434 // not allowed in description items
2435 enable = !inDescriptionItem(cur);
2437 case LFUN_NOMENCL_INSERT:
2438 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2442 code = NOMENCL_CODE;
2444 case LFUN_NOMENCL_PRINT:
2445 code = NOMENCL_PRINT_CODE;
2446 // not allowed in description items
2447 enable = !inDescriptionItem(cur);
2449 case LFUN_HYPERLINK_INSERT:
2450 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2454 code = HYPERLINK_CODE;
2456 case LFUN_QUOTE_INSERT:
2457 // always allow this, since we will inset a raw quote
2458 // if an inset is not allowed.
2460 case LFUN_SPECIALCHAR_INSERT:
2461 code = SPECIALCHAR_CODE;
2463 case LFUN_SPACE_INSERT:
2464 // slight hack: we know this is allowed in math mode
2468 case LFUN_PREVIEW_INSERT:
2469 code = PREVIEW_CODE;
2471 case LFUN_SCRIPT_INSERT:
2475 case LFUN_MATH_INSERT:
2476 case LFUN_MATH_AMS_MATRIX:
2477 case LFUN_MATH_MATRIX:
2478 case LFUN_MATH_DELIM:
2479 case LFUN_MATH_BIGDELIM:
2480 case LFUN_MATH_DISPLAY:
2481 case LFUN_MATH_MODE:
2482 case LFUN_MATH_MACRO:
2483 case LFUN_MATH_SUBSCRIPT:
2484 case LFUN_MATH_SUPERSCRIPT:
2485 code = MATH_HULL_CODE;
2488 case LFUN_REGEXP_MODE:
2489 code = MATH_HULL_CODE;
2490 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2493 case LFUN_INSET_MODIFY:
2494 // We need to disable this, because we may get called for a
2496 // InsetTabular::getStatus() -> InsetText::getStatus()
2497 // and we don't handle LFUN_INSET_MODIFY.
2501 case LFUN_FONT_EMPH:
2502 flag.setOnOff(fontinfo.emph() == FONT_ON);
2503 enable = !cur.inset().getLayout().isPassThru();
2506 case LFUN_FONT_ITAL:
2507 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2508 enable = !cur.inset().getLayout().isPassThru();
2511 case LFUN_FONT_NOUN:
2512 flag.setOnOff(fontinfo.noun() == FONT_ON);
2513 enable = !cur.inset().getLayout().isPassThru();
2516 case LFUN_FONT_BOLD:
2517 case LFUN_FONT_BOLDSYMBOL:
2518 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2519 enable = !cur.inset().getLayout().isPassThru();
2522 case LFUN_FONT_SANS:
2523 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2524 enable = !cur.inset().getLayout().isPassThru();
2527 case LFUN_FONT_ROMAN:
2528 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2529 enable = !cur.inset().getLayout().isPassThru();
2532 case LFUN_FONT_TYPEWRITER:
2533 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2534 enable = !cur.inset().getLayout().isPassThru();
2539 enable = cur.selection();
2543 if (cmd.argument().empty()) {
2544 if (theClipboard().isInternal())
2545 enable = cap::numberOfSelections() > 0;
2547 enable = !theClipboard().empty();
2551 // we have an argument
2552 string const arg = to_utf8(cmd.argument());
2553 if (isStrUnsignedInt(arg)) {
2554 // it's a number and therefore means the internal stack
2555 unsigned int n = convert<unsigned int>(arg);
2556 enable = cap::numberOfSelections() > n;
2560 // explicit graphics type?
2561 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2562 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2563 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2564 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2565 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2566 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2567 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2568 enable = theClipboard().hasGraphicsContents(type);
2577 case LFUN_CLIPBOARD_PASTE:
2578 enable = !theClipboard().empty();
2581 case LFUN_PRIMARY_SELECTION_PASTE:
2582 enable = cur.selection() || !theSelection().empty();
2585 case LFUN_SELECTION_PASTE:
2586 enable = cap::selection();
2589 case LFUN_PARAGRAPH_MOVE_UP:
2590 enable = cur.pit() > 0 && !cur.selection();
2593 case LFUN_PARAGRAPH_MOVE_DOWN:
2594 enable = cur.pit() < cur.lastpit() && !cur.selection();
2597 case LFUN_CHANGE_ACCEPT:
2598 case LFUN_CHANGE_REJECT:
2599 // In principle, these LFUNs should only be enabled if there
2600 // is a change at the current position/in the current selection.
2601 // However, without proper optimizations, this will inevitably
2602 // result in unacceptable performance - just imagine a user who
2603 // wants to select the complete content of a long document.
2604 if (!cur.selection())
2605 enable = cur.paragraph().isChanged(cur.pos());
2607 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2612 case LFUN_OUTLINE_UP:
2613 case LFUN_OUTLINE_DOWN:
2614 case LFUN_OUTLINE_IN:
2615 case LFUN_OUTLINE_OUT:
2616 // FIXME: LyX is not ready for outlining within inset.
2617 enable = isMainText()
2618 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2621 case LFUN_NEWLINE_INSERT:
2622 // LaTeX restrictions (labels or empty par)
2623 enable = (cur.pos() > cur.paragraph().beginOfBody());
2626 case LFUN_TAB_INSERT:
2627 case LFUN_TAB_DELETE:
2628 enable = cur.inset().getLayout().isPassThru();
2631 case LFUN_SET_GRAPHICS_GROUP: {
2632 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2636 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2640 case LFUN_NEWPAGE_INSERT:
2641 // not allowed in description items
2642 code = NEWPAGE_CODE;
2643 enable = !inDescriptionItem(cur);
2646 case LFUN_DATE_INSERT: {
2647 string const format = cmd.argument().empty()
2648 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2649 enable = support::os::is_valid_strftime(format);
2654 enable = !cur.inset().getLayout().isPassThru();
2655 flag.setOnOff(to_utf8(cmd.argument()) == cur.real_current_font.language()->lang());
2658 case LFUN_BREAK_PARAGRAPH:
2659 enable = cur.inset().getLayout().isMultiPar();
2662 case LFUN_SPELLING_ADD:
2663 case LFUN_SPELLING_IGNORE:
2664 case LFUN_SPELLING_REMOVE:
2665 enable = theSpellChecker();
2669 enable = !cur.inset().forcePlainLayout();
2672 case LFUN_LAYOUT_PARAGRAPH:
2673 case LFUN_PARAGRAPH_PARAMS:
2674 case LFUN_PARAGRAPH_PARAMS_APPLY:
2675 case LFUN_PARAGRAPH_UPDATE:
2676 enable = cur.inset().allowParagraphCustomization();
2679 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2680 case LFUN_ACCENT_ACUTE:
2681 case LFUN_ACCENT_BREVE:
2682 case LFUN_ACCENT_CARON:
2683 case LFUN_ACCENT_CEDILLA:
2684 case LFUN_ACCENT_CIRCLE:
2685 case LFUN_ACCENT_CIRCUMFLEX:
2686 case LFUN_ACCENT_DOT:
2687 case LFUN_ACCENT_GRAVE:
2688 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2689 case LFUN_ACCENT_MACRON:
2690 case LFUN_ACCENT_OGONEK:
2691 case LFUN_ACCENT_TIE:
2692 case LFUN_ACCENT_TILDE:
2693 case LFUN_ACCENT_UMLAUT:
2694 case LFUN_ACCENT_UNDERBAR:
2695 case LFUN_ACCENT_UNDERDOT:
2696 case LFUN_FONT_DEFAULT:
2697 case LFUN_FONT_FRAK:
2698 case LFUN_FONT_SIZE:
2699 case LFUN_FONT_STATE:
2700 case LFUN_FONT_UNDERLINE:
2701 case LFUN_FONT_STRIKEOUT:
2702 case LFUN_FONT_UULINE:
2703 case LFUN_FONT_UWAVE:
2704 case LFUN_TEXTSTYLE_APPLY:
2705 case LFUN_TEXTSTYLE_UPDATE:
2706 enable = !cur.inset().getLayout().isPassThru();
2709 case LFUN_WORD_DELETE_FORWARD:
2710 case LFUN_WORD_DELETE_BACKWARD:
2711 case LFUN_LINE_DELETE:
2712 case LFUN_WORD_FORWARD:
2713 case LFUN_WORD_BACKWARD:
2714 case LFUN_WORD_RIGHT:
2715 case LFUN_WORD_LEFT:
2716 case LFUN_CHAR_FORWARD:
2717 case LFUN_CHAR_FORWARD_SELECT:
2718 case LFUN_CHAR_BACKWARD:
2719 case LFUN_CHAR_BACKWARD_SELECT:
2720 case LFUN_CHAR_LEFT:
2721 case LFUN_CHAR_LEFT_SELECT:
2722 case LFUN_CHAR_RIGHT:
2723 case LFUN_CHAR_RIGHT_SELECT:
2725 case LFUN_UP_SELECT:
2727 case LFUN_DOWN_SELECT:
2728 case LFUN_PARAGRAPH_UP_SELECT:
2729 case LFUN_PARAGRAPH_DOWN_SELECT:
2730 case LFUN_LINE_BEGIN_SELECT:
2731 case LFUN_LINE_END_SELECT:
2732 case LFUN_WORD_FORWARD_SELECT:
2733 case LFUN_WORD_BACKWARD_SELECT:
2734 case LFUN_WORD_RIGHT_SELECT:
2735 case LFUN_WORD_LEFT_SELECT:
2736 case LFUN_WORD_SELECT:
2737 case LFUN_SECTION_SELECT:
2738 case LFUN_BUFFER_BEGIN:
2739 case LFUN_BUFFER_END:
2740 case LFUN_BUFFER_BEGIN_SELECT:
2741 case LFUN_BUFFER_END_SELECT:
2742 case LFUN_INSET_BEGIN:
2743 case LFUN_INSET_END:
2744 case LFUN_INSET_BEGIN_SELECT:
2745 case LFUN_INSET_END_SELECT:
2746 case LFUN_INSET_SELECT_ALL:
2747 case LFUN_PARAGRAPH_UP:
2748 case LFUN_PARAGRAPH_DOWN:
2749 case LFUN_LINE_BEGIN:
2751 case LFUN_CHAR_DELETE_FORWARD:
2752 case LFUN_CHAR_DELETE_BACKWARD:
2753 case LFUN_WORD_UPCASE:
2754 case LFUN_WORD_LOWCASE:
2755 case LFUN_WORD_CAPITALIZE:
2756 case LFUN_CHARS_TRANSPOSE:
2757 case LFUN_SERVER_GET_XY:
2758 case LFUN_SERVER_SET_XY:
2759 case LFUN_SERVER_GET_LAYOUT:
2760 case LFUN_SELF_INSERT:
2761 case LFUN_UNICODE_INSERT:
2762 case LFUN_THESAURUS_ENTRY:
2764 // these are handled in our dispatch()
2768 case LFUN_INSET_INSERT: {
2769 string const type = cmd.getArg(0);
2770 if (type == "toc") {
2772 // not allowed in description items
2773 //FIXME: couldn't this be merged in Inset::insetAllowed()?
2774 enable = !inDescriptionItem(cur);
2787 || !cur.inset().insetAllowed(code)
2788 || cur.paragraph().layout().pass_thru))
2791 flag.setEnabled(enable);
2796 void Text::pasteString(Cursor & cur, docstring const & clip,
2799 cur.clearSelection();
2800 if (!clip.empty()) {
2803 insertStringAsParagraphs(cur, clip, cur.current_font);
2805 insertStringAsLines(cur, clip, cur.current_font);
2810 // FIXME: an item inset would make things much easier.
2811 bool Text::inDescriptionItem(Cursor & cur) const
2813 Paragraph & par = cur.paragraph();
2814 pos_type const pos = cur.pos();
2815 pos_type const body_pos = par.beginOfBody();
2817 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2818 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2819 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2822 return (pos < body_pos
2824 && (pos == 0 || par.getChar(pos - 1) != ' ')));