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 * insetText = dynamic_cast<InsetText *>(inset);
273 insetText->fixParagraphsFont();
274 if (!insetText->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);
481 FuncCode const act = cmd.action();
484 case LFUN_PARAGRAPH_MOVE_DOWN: {
485 pit_type const pit = cur.pit();
486 recUndo(cur, pit, pit + 1);
488 pars_.swap(pit, pit + 1);
490 cur.forceBufferUpdate();
495 case LFUN_PARAGRAPH_MOVE_UP: {
496 pit_type const pit = cur.pit();
497 recUndo(cur, pit - 1, pit);
499 pars_.swap(pit, pit - 1);
502 cur.forceBufferUpdate();
506 case LFUN_APPENDIX: {
507 Paragraph & par = cur.paragraph();
508 bool start = !par.params().startOfAppendix();
510 // FIXME: The code below only makes sense at top level.
511 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
512 // ensure that we have only one start_of_appendix in this document
513 // FIXME: this don't work for multipart document!
514 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
515 if (pars_[tmp].params().startOfAppendix()) {
517 pars_[tmp].params().startOfAppendix(false);
523 par.params().startOfAppendix(start);
525 // we can set the refreshing parameters now
526 cur.forceBufferUpdate();
530 case LFUN_WORD_DELETE_FORWARD:
532 cutSelection(cur, true, false);
534 deleteWordForward(cur);
535 finishChange(cur, false);
538 case LFUN_WORD_DELETE_BACKWARD:
540 cutSelection(cur, true, false);
542 deleteWordBackward(cur);
543 finishChange(cur, false);
546 case LFUN_LINE_DELETE:
548 cutSelection(cur, true, false);
550 tm->deleteLineForward(cur);
551 finishChange(cur, false);
554 case LFUN_BUFFER_BEGIN:
555 case LFUN_BUFFER_BEGIN_SELECT:
556 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
557 if (cur.depth() == 1)
558 needsUpdate |= cursorTop(cur);
561 cur.screenUpdateFlags(Update::FitCursor);
564 case LFUN_BUFFER_END:
565 case LFUN_BUFFER_END_SELECT:
566 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
567 if (cur.depth() == 1)
568 needsUpdate |= cursorBottom(cur);
571 cur.screenUpdateFlags(Update::FitCursor);
574 case LFUN_INSET_BEGIN:
575 case LFUN_INSET_BEGIN_SELECT:
576 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
577 if (cur.depth() == 1 || !cur.top().at_begin())
578 needsUpdate |= cursorTop(cur);
581 cur.screenUpdateFlags(Update::FitCursor);
585 case LFUN_INSET_END_SELECT:
586 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
587 if (cur.depth() == 1 || !cur.top().at_end())
588 needsUpdate |= cursorBottom(cur);
591 cur.screenUpdateFlags(Update::FitCursor);
594 case LFUN_INSET_SELECT_ALL:
595 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
596 || !cur.selEnd().at_end()) {
597 needsUpdate |= cur.selHandle(false);
598 needsUpdate |= cursorTop(cur);
599 needsUpdate |= cur.selHandle(true);
600 needsUpdate |= cursorBottom(cur);
603 cur.screenUpdateFlags(Update::FitCursor);
606 case LFUN_CHAR_FORWARD:
607 case LFUN_CHAR_FORWARD_SELECT:
608 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
609 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
610 needsUpdate |= cursorForward(cur);
612 if (!needsUpdate && oldTopSlice == cur.top()
613 && cur.boundary() == oldBoundary) {
615 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
617 // we will probably be moving out the inset, so we should execute
618 // the depm-mechanism, but only when the cursor has a place to
619 // go outside this inset, i.e. in a slice above.
620 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
621 && cur.pit() == cur.lastpit()) {
622 // The cursor hasn't changed yet. To give the
623 // DEPM the possibility of doing something we must
624 // provide it with two different cursors.
626 dummy.pos() = dummy.pit() = 0;
627 if (cur.bv().checkDepm(dummy, cur))
628 cur.forceBufferUpdate();;
633 case LFUN_CHAR_BACKWARD:
634 case LFUN_CHAR_BACKWARD_SELECT:
635 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
636 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
637 needsUpdate |= cursorBackward(cur);
639 if (!needsUpdate && oldTopSlice == cur.top()
640 && cur.boundary() == oldBoundary) {
642 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
644 // we will probably be moving out the inset, so we should execute
645 // the depm-mechanism, but only when the cursor has a place to
646 // go outside this inset, i.e. in a slice above.
647 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
648 // The cursor hasn't changed yet. To give the
649 // DEPM the possibility of doing something we must
650 // provide it with two different cursors.
652 dummy.pos() = cur.lastpos();
653 dummy.pit() = cur.lastpit();
654 if (cur.bv().checkDepm(dummy, cur))
655 cur.forceBufferUpdate();
661 case LFUN_CHAR_LEFT_SELECT:
662 if (lyxrc.visual_cursor) {
663 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
664 needsUpdate |= cursorVisLeft(cur);
665 if (!needsUpdate && oldTopSlice == cur.top()
666 && cur.boundary() == oldBoundary) {
668 cmd = FuncRequest(LFUN_FINISHED_LEFT);
671 if (reverseDirectionNeeded(cur)) {
672 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
673 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
675 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
676 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
683 case LFUN_CHAR_RIGHT:
684 case LFUN_CHAR_RIGHT_SELECT:
685 if (lyxrc.visual_cursor) {
686 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
687 needsUpdate |= cursorVisRight(cur);
688 if (!needsUpdate && oldTopSlice == cur.top()
689 && cur.boundary() == oldBoundary) {
691 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
694 if (reverseDirectionNeeded(cur)) {
695 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
696 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
698 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
699 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
708 case LFUN_DOWN_SELECT:
711 // stop/start the selection
712 bool select = cmd.action() == LFUN_DOWN_SELECT ||
713 cmd.action() == LFUN_UP_SELECT;
715 // move cursor up/down
716 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
717 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
719 if (!atFirstOrLastRow) {
720 needsUpdate |= cur.selHandle(select);
721 cur.selHandle(select);
722 cur.upDownInText(up, needsUpdate);
723 needsUpdate |= cur.beforeDispatchCursor().inMathed();
725 // if the cursor cannot be moved up or down do not remove
726 // the selection right now, but wait for the next dispatch.
728 needsUpdate |= cur.selHandle(select);
729 cur.upDownInText(up, needsUpdate);
736 case LFUN_PARAGRAPH_UP:
737 case LFUN_PARAGRAPH_UP_SELECT:
738 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
739 needsUpdate |= cursorUpParagraph(cur);
742 case LFUN_PARAGRAPH_DOWN:
743 case LFUN_PARAGRAPH_DOWN_SELECT:
744 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
745 needsUpdate |= cursorDownParagraph(cur);
748 case LFUN_LINE_BEGIN:
749 case LFUN_LINE_BEGIN_SELECT:
750 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
751 needsUpdate |= tm->cursorHome(cur);
755 case LFUN_LINE_END_SELECT:
756 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
757 needsUpdate |= tm->cursorEnd(cur);
760 case LFUN_SECTION_SELECT: {
761 Buffer const & buf = *cur.buffer();
762 pit_type const pit = cur.pit();
763 ParagraphList & pars = buf.text().paragraphs();
764 ParagraphList::iterator bgn = pars.begin();
765 // The first paragraph of the area to be selected:
766 ParagraphList::iterator start = boost::next(bgn, pit);
767 // The final paragraph of area to be selected:
768 ParagraphList::iterator finish = start;
769 ParagraphList::iterator end = pars.end();
771 int const thistoclevel = start->layout().toclevel;
772 if (thistoclevel == Layout::NOT_IN_TOC)
776 Cursor const old_cur = cur;
777 needsUpdate |= cur.selHandle(true);
779 // Move out (down) from this section header
783 // Seek the one (on same level) below
784 for (; finish != end; ++finish, ++cur.pit()) {
785 int const toclevel = finish->layout().toclevel;
786 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
789 cur.pos() = cur.lastpos();
791 needsUpdate |= cur != old_cur;
795 case LFUN_WORD_RIGHT:
796 case LFUN_WORD_RIGHT_SELECT:
797 if (lyxrc.visual_cursor) {
798 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
799 needsUpdate |= cursorVisRightOneWord(cur);
800 if (!needsUpdate && oldTopSlice == cur.top()
801 && cur.boundary() == oldBoundary) {
803 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
806 if (reverseDirectionNeeded(cur)) {
807 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
808 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
810 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
811 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
818 case LFUN_WORD_FORWARD:
819 case LFUN_WORD_FORWARD_SELECT:
820 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
821 needsUpdate |= cursorForwardOneWord(cur);
825 case LFUN_WORD_LEFT_SELECT:
826 if (lyxrc.visual_cursor) {
827 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
828 needsUpdate |= cursorVisLeftOneWord(cur);
829 if (!needsUpdate && oldTopSlice == cur.top()
830 && cur.boundary() == oldBoundary) {
832 cmd = FuncRequest(LFUN_FINISHED_LEFT);
835 if (reverseDirectionNeeded(cur)) {
836 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
837 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
839 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
840 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
847 case LFUN_WORD_BACKWARD:
848 case LFUN_WORD_BACKWARD_SELECT:
849 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
850 needsUpdate |= cursorBackwardOneWord(cur);
853 case LFUN_WORD_SELECT: {
854 selectWord(cur, WHOLE_WORD);
855 finishChange(cur, true);
859 case LFUN_NEWLINE_INSERT: {
860 InsetNewlineParams inp;
861 docstring arg = cmd.argument();
862 // this avoids a double undo
863 // FIXME: should not be needed, ideally
864 if (!cur.selection())
866 cap::replaceSelection(cur);
867 if (arg == "linebreak")
868 inp.kind = InsetNewlineParams::LINEBREAK;
870 inp.kind = InsetNewlineParams::NEWLINE;
871 cur.insert(new InsetNewline(inp));
873 moveCursor(cur, false);
877 case LFUN_TAB_INSERT: {
878 bool const multi_par_selection = cur.selection() &&
879 cur.selBegin().pit() != cur.selEnd().pit();
880 if (multi_par_selection) {
881 // If there is a multi-paragraph selection, a tab is inserted
882 // at the beginning of each paragraph.
883 cur.recordUndoSelection();
884 pit_type const pit_end = cur.selEnd().pit();
885 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
886 pars_[pit].insertChar(0, '\t',
887 bv->buffer().params().trackChanges);
888 // Update the selection pos to make sure the selection does not
889 // change as the inserted tab will increase the logical pos.
890 if (cur.realAnchor().pit() == pit)
891 cur.realAnchor().forwardPos();
892 if (cur.pit() == pit)
897 // Maybe we shouldn't allow tabs within a line, because they
898 // are not (yet) aligned as one might do expect.
899 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
905 case LFUN_TAB_DELETE: {
906 bool const tc = bv->buffer().params().trackChanges;
907 if (cur.selection()) {
908 // If there is a selection, a tab (if present) is removed from
909 // the beginning of each paragraph.
910 cur.recordUndoSelection();
911 pit_type const pit_end = cur.selEnd().pit();
912 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
913 Paragraph & par = paragraphs()[pit];
914 if (par.getChar(0) == '\t') {
915 if (cur.pit() == pit)
917 if (cur.realAnchor().pit() == pit && cur.realAnchor().pos() > 0 )
918 cur.realAnchor().backwardPos();
920 par.eraseChar(0, tc);
922 // If no tab was present, try to remove up to four spaces.
923 for (int n_spaces = 0;
924 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
925 if (cur.pit() == pit)
927 if (cur.realAnchor().pit() == pit && cur.realAnchor().pos() > 0 )
928 cur.realAnchor().backwardPos();
930 par.eraseChar(0, tc);
935 // If there is no selection, try to remove a tab or some spaces
936 // before the position of the cursor.
937 Paragraph & par = paragraphs()[cur.pit()];
938 pos_type const pos = cur.pos();
943 char_type const c = par.getChar(pos - 1);
947 par.eraseChar(cur.pos(), tc);
949 for (int n_spaces = 0;
951 && par.getChar(cur.pos() - 1) == ' '
955 par.eraseChar(cur.pos(), tc);
962 case LFUN_CHAR_DELETE_FORWARD:
963 if (!cur.selection()) {
964 if (cur.pos() == cur.paragraph().size())
965 // Par boundary, force full-screen update
966 singleParUpdate = false;
967 needsUpdate |= erase(cur);
969 // It is possible to make it a lot faster still
970 // just comment out the line below...
972 cutSelection(cur, true, false);
973 singleParUpdate = false;
975 moveCursor(cur, false);
976 cur.forceBufferUpdate();
979 case LFUN_CHAR_DELETE_BACKWARD:
980 if (!cur.selection()) {
981 if (bv->getIntl().getTransManager().backspace()) {
982 // Par boundary, full-screen update
984 singleParUpdate = false;
985 needsUpdate |= backspace(cur);
987 // It is possible to make it a lot faster still
988 // just comment out the line below...
991 cutSelection(cur, true, false);
992 singleParUpdate = false;
994 cur.forceBufferUpdate();
997 case LFUN_BREAK_PARAGRAPH:
998 cap::replaceSelection(cur);
999 breakParagraph(cur, cmd.argument() == "inverse");
1003 case LFUN_INSET_INSERT: {
1006 // We have to avoid triggering InstantPreview loading
1007 // before inserting into the document. See bug #5626.
1008 bool loaded = bv->buffer().isFullyLoaded();
1009 bv->buffer().setFullyLoaded(false);
1010 Inset * inset = createInset(&bv->buffer(), cmd);
1011 bv->buffer().setFullyLoaded(loaded);
1014 // FIXME (Abdel 01/02/2006):
1015 // What follows would be a partial fix for bug 2154:
1016 // http://www.lyx.org/trac/ticket/2154
1017 // This automatically put the label inset _after_ a
1018 // numbered section. It should be possible to extend the mechanism
1019 // to any kind of LateX environement.
1020 // The correct way to fix that bug would be at LateX generation.
1021 // I'll let the code here for reference as it could be used for some
1022 // other feature like "automatic labelling".
1024 Paragraph & par = pars_[cur.pit()];
1025 if (inset->lyxCode() == LABEL_CODE
1026 && par.layout().labeltype == LABEL_COUNTER) {
1027 // Go to the end of the paragraph
1028 // Warning: Because of Change-Tracking, the last
1029 // position is 'size()' and not 'size()-1':
1030 cur.pos() = par.size();
1031 // Insert a new paragraph
1032 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1036 if (cur.selection())
1037 cutSelection(cur, true, false);
1041 // trigger InstantPreview now
1042 if (inset->lyxCode() == EXTERNAL_CODE) {
1043 InsetExternal & ins =
1044 static_cast<InsetExternal &>(*inset);
1045 ins.updatePreview();
1052 case LFUN_INSET_DISSOLVE: {
1053 if (dissolveInset(cur)) {
1055 cur.forceBufferUpdate();
1060 case LFUN_SET_GRAPHICS_GROUP: {
1061 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1067 string id = to_utf8(cmd.argument());
1068 string grp = graphics::getGroupParams(bv->buffer(), id);
1069 InsetGraphicsParams tmp, inspar = ins->getParams();
1072 inspar.groupId = to_utf8(cmd.argument());
1074 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1075 tmp.filename = inspar.filename;
1079 ins->setParams(inspar);
1082 case LFUN_SPACE_INSERT:
1083 if (cur.paragraph().layout().free_spacing)
1084 insertChar(cur, ' ');
1086 doInsertInset(cur, this, cmd, false, false);
1089 moveCursor(cur, false);
1092 case LFUN_SPECIALCHAR_INSERT: {
1093 string const name = to_utf8(cmd.argument());
1094 if (name == "hyphenation")
1095 specialChar(cur, InsetSpecialChar::HYPHENATION);
1096 else if (name == "ligature-break")
1097 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1098 else if (name == "slash")
1099 specialChar(cur, InsetSpecialChar::SLASH);
1100 else if (name == "nobreakdash")
1101 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1102 else if (name == "dots")
1103 specialChar(cur, InsetSpecialChar::LDOTS);
1104 else if (name == "end-of-sentence")
1105 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1106 else if (name == "menu-separator")
1107 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1108 else if (name.empty())
1109 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1111 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1115 case LFUN_WORD_UPCASE:
1116 changeCase(cur, text_uppercase);
1119 case LFUN_WORD_LOWCASE:
1120 changeCase(cur, text_lowercase);
1123 case LFUN_WORD_CAPITALIZE:
1124 changeCase(cur, text_capitalization);
1127 case LFUN_CHARS_TRANSPOSE:
1128 charsTranspose(cur);
1132 cur.message(_("Paste"));
1133 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1134 cap::replaceSelection(cur);
1136 // without argument?
1137 string const arg = to_utf8(cmd.argument());
1139 if (theClipboard().isInternal())
1140 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1141 else if (theClipboard().hasGraphicsContents()
1142 && !theClipboard().hasTextContents())
1143 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1145 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1146 } else if (isStrUnsignedInt(arg)) {
1147 // we have a numerical argument
1148 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1149 convert<unsigned int>(arg));
1151 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1153 type = Clipboard::PdfGraphicsType;
1154 else if (arg == "png")
1155 type = Clipboard::PngGraphicsType;
1156 else if (arg == "jpeg")
1157 type = Clipboard::JpegGraphicsType;
1158 else if (arg == "linkback")
1159 type = Clipboard::LinkBackGraphicsType;
1160 else if (arg == "emf")
1161 type = Clipboard::EmfGraphicsType;
1162 else if (arg == "wmf")
1163 type = Clipboard::WmfGraphicsType;
1166 LASSERT(false, /**/);
1168 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1171 bv->buffer().errors("Paste");
1172 cur.clearSelection(); // bug 393
1178 cutSelection(cur, true, true);
1179 cur.message(_("Cut"));
1184 cur.message(_("Copy"));
1187 case LFUN_SERVER_GET_XY:
1188 cur.message(from_utf8(
1189 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1190 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1193 case LFUN_SERVER_SET_XY: {
1196 istringstream is(to_utf8(cmd.argument()));
1199 lyxerr << "SETXY: Could not parse coordinates in '"
1200 << to_utf8(cmd.argument()) << endl;
1202 tm->setCursorFromCoordinates(cur, x, y);
1206 case LFUN_SERVER_GET_LAYOUT:
1207 cur.message(cur.paragraph().layout().name());
1211 docstring layout = cmd.argument();
1212 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1214 Paragraph const & para = cur.paragraph();
1215 docstring const old_layout = para.layout().name();
1216 DocumentClass const & tclass = bv->buffer().params().documentClass();
1219 layout = tclass.defaultLayoutName();
1221 if (owner_->forcePlainLayout())
1222 // in this case only the empty layout is allowed
1223 layout = tclass.plainLayoutName();
1224 else if (para.usePlainLayout()) {
1225 // in this case, default layout maps to empty layout
1226 if (layout == tclass.defaultLayoutName())
1227 layout = tclass.plainLayoutName();
1229 // otherwise, the empty layout maps to the default
1230 if (layout == tclass.plainLayoutName())
1231 layout = tclass.defaultLayoutName();
1234 bool hasLayout = tclass.hasLayout(layout);
1236 // If the entry is obsolete, use the new one instead.
1238 docstring const & obs = tclass[layout].obsoleted_by();
1244 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1245 from_utf8(N_(" not known")));
1249 bool change_layout = (old_layout != layout);
1251 if (!change_layout && cur.selection() &&
1252 cur.selBegin().pit() != cur.selEnd().pit())
1254 pit_type spit = cur.selBegin().pit();
1255 pit_type epit = cur.selEnd().pit() + 1;
1256 while (spit != epit) {
1257 if (pars_[spit].layout().name() != old_layout) {
1258 change_layout = true;
1266 setLayout(cur, layout);
1271 case LFUN_CLIPBOARD_PASTE:
1272 cur.clearSelection();
1273 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1274 cmd.argument() == "paragraph");
1275 bv->buffer().errors("Paste");
1278 case LFUN_PRIMARY_SELECTION_PASTE:
1279 pasteString(cur, theSelection().get(),
1280 cmd.argument() == "paragraph");
1283 case LFUN_SELECTION_PASTE:
1284 // Copy the selection buffer to the clipboard stack,
1285 // because we want it to appear in the "Edit->Paste
1287 cap::copySelectionToStack();
1288 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1289 bv->buffer().errors("Paste");
1292 case LFUN_UNICODE_INSERT: {
1293 if (cmd.argument().empty())
1295 docstring hexstring = cmd.argument();
1296 if (isHex(hexstring)) {
1297 char_type c = hexToInt(hexstring);
1298 if (c >= 32 && c < 0x10ffff) {
1299 lyxerr << "Inserting c: " << c << endl;
1300 docstring s = docstring(1, c);
1301 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1307 case LFUN_QUOTE_INSERT: {
1308 Paragraph & par = cur.paragraph();
1309 pos_type pos = cur.pos();
1310 BufferParams const & bufparams = bv->buffer().params();
1311 Layout const & style = par.layout();
1312 InsetLayout const & ilayout = cur.inset().getLayout();
1313 if (!style.pass_thru && !ilayout.isPassThru()
1314 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1315 // this avoids a double undo
1316 // FIXME: should not be needed, ideally
1317 if (!cur.selection())
1319 cap::replaceSelection(cur);
1324 else if (cur.prevInset() && cur.prevInset()->isSpace())
1327 c = par.getChar(pos - 1);
1328 string arg = to_utf8(cmd.argument());
1329 cur.insert(new InsetQuotes(cur.buffer(), c, (arg == "single")
1330 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1334 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1338 case LFUN_DATE_INSERT: {
1339 string const format = cmd.argument().empty()
1340 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1341 string const time = formatted_time(current_time(), format);
1342 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1346 case LFUN_MOUSE_TRIPLE:
1347 if (cmd.button() == mouse_button::button1) {
1348 tm->cursorHome(cur);
1356 case LFUN_MOUSE_DOUBLE:
1357 if (cmd.button() == mouse_button::button1) {
1358 selectWord(cur, WHOLE_WORD_STRICT);
1363 // Single-click on work area
1364 case LFUN_MOUSE_PRESS: {
1365 // We are not marking a selection with the keyboard in any case.
1366 Cursor & bvcur = cur.bv().cursor();
1367 bvcur.setMark(false);
1368 switch (cmd.button()) {
1369 case mouse_button::button1:
1371 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1372 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1373 if (bvcur.wordSelection())
1374 selectWord(bvcur, WHOLE_WORD);
1377 case mouse_button::button2:
1378 // Middle mouse pasting.
1379 bv->mouseSetCursor(cur);
1381 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1382 "selection-paste ; primary-selection-paste paragraph"));
1383 cur.noScreenUpdate();
1386 case mouse_button::button3: {
1387 // Don't do anything if we right-click a
1388 // selection, a context menu will popup.
1389 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1390 && cur < bvcur.selectionEnd()) {
1391 cur.noScreenUpdate();
1394 if (!bv->mouseSetCursor(cur, false))
1395 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1401 } // switch (cmd.button())
1404 case LFUN_MOUSE_MOTION: {
1405 // Mouse motion with right or middle mouse do nothing for now.
1406 if (cmd.button() != mouse_button::button1) {
1407 cur.noScreenUpdate();
1410 // ignore motions deeper nested than the real anchor
1411 Cursor & bvcur = cur.bv().cursor();
1412 if (!bvcur.realAnchor().hasPart(cur)) {
1416 CursorSlice old = bvcur.top();
1418 int const wh = bv->workHeight();
1419 int const y = max(0, min(wh - 1, cmd.y()));
1421 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1422 cur.setTargetX(cmd.x());
1424 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1425 else if (cmd.y() < 0)
1426 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1427 // This is to allow jumping over large insets
1428 if (cur.top() == old) {
1430 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1431 else if (cmd.y() < 0)
1432 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1434 // We continue with our existing selection or start a new one, so don't
1435 // reset the anchor.
1436 bvcur.setCursor(cur);
1437 bvcur.setSelection(true);
1438 if (cur.top() == old) {
1439 // We didn't move one iota, so no need to update the screen.
1440 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1441 //cur.noScreenUpdate();
1447 case LFUN_MOUSE_RELEASE:
1448 switch (cmd.button()) {
1449 case mouse_button::button1:
1450 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1451 // If there is a new selection, update persistent selection;
1452 // otherwise, single click does not clear persistent selection
1454 if (cur.selection()) {
1455 // Finish selection. If double click,
1456 // cur is moved to the end of word by
1457 // selectWord but bvcur is current
1459 cur.bv().cursor().setSelection();
1460 // We might have removed an empty but drawn selection
1461 // (probably a margin)
1462 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1464 cur.noScreenUpdate();
1465 // FIXME: We could try to handle drag and drop of selection here.
1468 case mouse_button::button2:
1469 // Middle mouse pasting is handled at mouse press time,
1470 // see LFUN_MOUSE_PRESS.
1471 cur.noScreenUpdate();
1474 case mouse_button::button3:
1475 // Cursor was set at LFUN_MOUSE_PRESS time.
1476 // FIXME: If there is a selection we could try to handle a special
1477 // drag & drop context menu.
1478 cur.noScreenUpdate();
1481 case mouse_button::none:
1482 case mouse_button::button4:
1483 case mouse_button::button5:
1485 } // switch (cmd.button())
1489 case LFUN_SELF_INSERT: {
1490 if (cmd.argument().empty())
1493 // Automatically delete the currently selected
1494 // text and replace it with what is being
1495 // typed in now. Depends on lyxrc settings
1496 // "auto_region_delete", which defaults to
1499 if (lyxrc.auto_region_delete && cur.selection())
1500 cutSelection(cur, false, false);
1502 cur.clearSelection();
1504 docstring::const_iterator cit = cmd.argument().begin();
1505 docstring::const_iterator const end = cmd.argument().end();
1506 for (; cit != end; ++cit)
1507 bv->translateAndInsert(*cit, this, cur);
1510 moveCursor(cur, false);
1511 bv->bookmarkEditPosition();
1515 case LFUN_HYPERLINK_INSERT: {
1516 InsetCommandParams p(HYPERLINK_CODE);
1518 if (cur.selection()) {
1519 content = cur.selectionAsString(false);
1520 cutSelection(cur, true, false);
1522 p["target"] = (cmd.argument().empty()) ?
1523 content : cmd.argument();
1524 string const data = InsetCommand::params2string("href", p);
1525 if (p["target"].empty()) {
1526 bv->showDialog("href", data);
1528 FuncRequest fr(LFUN_INSET_INSERT, data);
1534 case LFUN_LABEL_INSERT: {
1535 InsetCommandParams p(LABEL_CODE);
1536 // Try to generate a valid label
1537 p["name"] = (cmd.argument().empty()) ?
1538 cur.getPossibleLabel() :
1540 string const data = InsetCommand::params2string("label", p);
1542 if (cmd.argument().empty()) {
1543 bv->showDialog("label", data);
1545 FuncRequest fr(LFUN_INSET_INSERT, data);
1551 case LFUN_INFO_INSERT: {
1553 if (cmd.argument().empty() && cur.selection()) {
1554 // if command argument is empty use current selection as parameter.
1555 docstring ds = cur.selectionAsString(false);
1556 cutSelection(cur, true, false);
1557 FuncRequest cmd0(cmd, ds);
1558 inset = createInset(cur.buffer(), cmd0);
1560 inset = createInset(cur.buffer(), cmd);
1565 insertInset(cur, inset);
1569 case LFUN_CAPTION_INSERT:
1570 case LFUN_FOOTNOTE_INSERT:
1571 case LFUN_NOTE_INSERT:
1572 case LFUN_FLEX_INSERT:
1573 case LFUN_BOX_INSERT:
1574 case LFUN_BRANCH_INSERT:
1575 case LFUN_PHANTOM_INSERT:
1576 case LFUN_ERT_INSERT:
1577 case LFUN_LISTING_INSERT:
1578 case LFUN_MARGINALNOTE_INSERT:
1579 case LFUN_ARGUMENT_INSERT:
1580 case LFUN_INDEX_INSERT:
1581 case LFUN_PREVIEW_INSERT:
1582 // Open the inset, and move the current selection
1584 doInsertInset(cur, this, cmd, true, true);
1586 // Some insets are numbered, others are shown in the outline pane so
1587 // let's update the labels and the toc backend.
1588 cur.forceBufferUpdate();
1591 case LFUN_TABULAR_INSERT:
1592 // if there were no arguments, just open the dialog
1593 if (doInsertInset(cur, this, cmd, false, true))
1596 bv->showDialog("tabularcreate");
1600 case LFUN_FLOAT_INSERT:
1601 case LFUN_FLOAT_WIDE_INSERT:
1602 case LFUN_WRAP_INSERT: {
1603 // will some text be moved into the inset?
1604 bool content = cur.selection();
1606 doInsertInset(cur, this, cmd, true, true);
1609 // If some text is moved into the inset, doInsertInset
1610 // puts the cursor outside the inset. To insert the
1611 // caption we put it back into the inset.
1615 ParagraphList & pars = cur.text()->paragraphs();
1617 DocumentClass const & tclass = bv->buffer().params().documentClass();
1619 // add a separate paragraph for the caption inset
1620 pars.push_back(Paragraph());
1621 pars.back().setInsetOwner(&cur.text()->inset());
1622 pars.back().setPlainOrDefaultLayout(tclass);
1623 int cap_pit = pars.size() - 1;
1625 // if an empty inset was created, we create an additional empty
1626 // paragraph at the bottom so that the user can choose where to put
1627 // the graphics (or table).
1629 pars.push_back(Paragraph());
1630 pars.back().setInsetOwner(&cur.text()->inset());
1631 pars.back().setPlainOrDefaultLayout(tclass);
1634 // reposition the cursor to the caption
1635 cur.pit() = cap_pit;
1637 // FIXME: This Text/Cursor dispatch handling is a mess!
1638 // We cannot use Cursor::dispatch here it needs access to up to
1640 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1641 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1642 cur.forceBufferUpdate();
1643 cur.screenUpdateFlags(Update::Force);
1644 // FIXME: When leaving the Float (or Wrap) inset we should
1645 // delete any empty paragraph left above or below the
1650 case LFUN_NOMENCL_INSERT: {
1651 InsetCommandParams p(NOMENCL_CODE);
1652 if (cmd.argument().empty())
1653 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1655 p["symbol"] = cmd.argument();
1656 string const data = InsetCommand::params2string("nomenclature", p);
1657 bv->showDialog("nomenclature", data);
1661 case LFUN_INDEX_PRINT: {
1662 InsetCommandParams p(INDEX_PRINT_CODE);
1663 if (cmd.argument().empty())
1664 p["type"] = from_ascii("idx");
1666 p["type"] = cmd.argument();
1667 string const data = InsetCommand::params2string("index_print", p);
1668 FuncRequest fr(LFUN_INSET_INSERT, data);
1673 case LFUN_NOMENCL_PRINT:
1674 case LFUN_NEWPAGE_INSERT:
1676 doInsertInset(cur, this, cmd, false, false);
1680 case LFUN_DEPTH_DECREMENT:
1681 changeDepth(cur, DEC_DEPTH);
1684 case LFUN_DEPTH_INCREMENT:
1685 changeDepth(cur, INC_DEPTH);
1688 case LFUN_MATH_DISPLAY:
1689 mathDispatch(cur, cmd, true);
1692 case LFUN_REGEXP_MODE:
1693 regexpDispatch(cur, cmd);
1696 case LFUN_MATH_MODE:
1697 if (cmd.argument() == "on")
1698 // don't pass "on" as argument
1699 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1701 mathDispatch(cur, cmd, false);
1704 case LFUN_MATH_MACRO:
1705 if (cmd.argument().empty())
1706 cur.errorMessage(from_utf8(N_("Missing argument")));
1708 string s = to_utf8(cmd.argument());
1709 string const s1 = token(s, ' ', 1);
1710 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1711 string const s2 = token(s, ' ', 2);
1712 MacroType type = MacroTypeNewcommand;
1714 type = MacroTypeDef;
1715 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1716 from_utf8(token(s, ' ', 0)), nargs, false, type);
1717 inset->setBuffer(bv->buffer());
1718 insertInset(cur, inset);
1720 // enter macro inset and select the name
1722 cur.top().pos() = cur.top().lastpos();
1724 cur.setSelection(true);
1725 cur.top().pos() = 0;
1729 // passthrough hat and underscore outside mathed:
1730 case LFUN_MATH_SUBSCRIPT:
1731 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1733 case LFUN_MATH_SUPERSCRIPT:
1734 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1737 case LFUN_MATH_INSERT:
1738 case LFUN_MATH_AMS_MATRIX:
1739 case LFUN_MATH_MATRIX:
1740 case LFUN_MATH_DELIM:
1741 case LFUN_MATH_BIGDELIM: {
1743 cap::replaceSelection(cur);
1744 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1745 checkAndActivateInset(cur, true);
1746 LASSERT(cur.inMathed(), /**/);
1751 case LFUN_FONT_EMPH: {
1752 Font font(ignore_font, ignore_language);
1753 font.fontInfo().setEmph(FONT_TOGGLE);
1754 toggleAndShow(cur, this, font);
1758 case LFUN_FONT_ITAL: {
1759 Font font(ignore_font, ignore_language);
1760 font.fontInfo().setShape(ITALIC_SHAPE);
1761 toggleAndShow(cur, this, font);
1765 case LFUN_FONT_BOLD:
1766 case LFUN_FONT_BOLDSYMBOL: {
1767 Font font(ignore_font, ignore_language);
1768 font.fontInfo().setSeries(BOLD_SERIES);
1769 toggleAndShow(cur, this, font);
1773 case LFUN_FONT_NOUN: {
1774 Font font(ignore_font, ignore_language);
1775 font.fontInfo().setNoun(FONT_TOGGLE);
1776 toggleAndShow(cur, this, font);
1780 case LFUN_FONT_TYPEWRITER: {
1781 Font font(ignore_font, ignore_language);
1782 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1783 toggleAndShow(cur, this, font);
1787 case LFUN_FONT_SANS: {
1788 Font font(ignore_font, ignore_language);
1789 font.fontInfo().setFamily(SANS_FAMILY);
1790 toggleAndShow(cur, this, font);
1794 case LFUN_FONT_ROMAN: {
1795 Font font(ignore_font, ignore_language);
1796 font.fontInfo().setFamily(ROMAN_FAMILY);
1797 toggleAndShow(cur, this, font);
1801 case LFUN_FONT_DEFAULT: {
1802 Font font(inherit_font, ignore_language);
1803 toggleAndShow(cur, this, font);
1807 case LFUN_FONT_STRIKEOUT: {
1808 Font font(ignore_font, ignore_language);
1809 font.fontInfo().setStrikeout(FONT_TOGGLE);
1810 toggleAndShow(cur, this, font);
1814 case LFUN_FONT_UULINE: {
1815 Font font(ignore_font, ignore_language);
1816 font.fontInfo().setUuline(FONT_TOGGLE);
1817 toggleAndShow(cur, this, font);
1821 case LFUN_FONT_UWAVE: {
1822 Font font(ignore_font, ignore_language);
1823 font.fontInfo().setUwave(FONT_TOGGLE);
1824 toggleAndShow(cur, this, font);
1828 case LFUN_FONT_UNDERLINE: {
1829 Font font(ignore_font, ignore_language);
1830 font.fontInfo().setUnderbar(FONT_TOGGLE);
1831 toggleAndShow(cur, this, font);
1835 case LFUN_FONT_SIZE: {
1836 Font font(ignore_font, ignore_language);
1837 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1838 toggleAndShow(cur, this, font);
1842 case LFUN_LANGUAGE: {
1843 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1846 Font font(ignore_font, lang);
1847 toggleAndShow(cur, this, font);
1851 case LFUN_TEXTSTYLE_APPLY:
1852 toggleAndShow(cur, this, freefont, toggleall);
1853 cur.message(_("Character set"));
1856 // Set the freefont using the contents of \param data dispatched from
1857 // the frontends and apply it at the current cursor location.
1858 case LFUN_TEXTSTYLE_UPDATE: {
1861 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1864 toggleAndShow(cur, this, freefont, toggleall);
1865 cur.message(_("Character set"));
1867 lyxerr << "Argument not ok";
1872 case LFUN_FINISHED_LEFT:
1873 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1874 // We're leaving an inset, going left. If the inset is LTR, we're
1875 // leaving from the front, so we should not move (remain at --- but
1876 // not in --- the inset). If the inset is RTL, move left, without
1877 // entering the inset itself; i.e., move to after the inset.
1878 if (cur.paragraph().getFontSettings(
1879 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1880 cursorVisLeft(cur, true);
1883 case LFUN_FINISHED_RIGHT:
1884 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1885 // We're leaving an inset, going right. If the inset is RTL, we're
1886 // leaving from the front, so we should not move (remain at --- but
1887 // not in --- the inset). If the inset is LTR, move right, without
1888 // entering the inset itself; i.e., move to after the inset.
1889 if (!cur.paragraph().getFontSettings(
1890 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1891 cursorVisRight(cur, true);
1894 case LFUN_FINISHED_BACKWARD:
1895 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1898 case LFUN_FINISHED_FORWARD:
1899 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1901 cur.setCurrentFont();
1904 case LFUN_LAYOUT_PARAGRAPH: {
1906 params2string(cur.paragraph(), data);
1907 data = "show\n" + data;
1908 bv->showDialog("paragraph", data);
1912 case LFUN_PARAGRAPH_UPDATE: {
1914 params2string(cur.paragraph(), data);
1916 // Will the paragraph accept changes from the dialog?
1918 cur.inset().allowParagraphCustomization(cur.idx());
1920 data = "update " + convert<string>(accept) + '\n' + data;
1921 bv->updateDialog("paragraph", data);
1925 case LFUN_ACCENT_UMLAUT:
1926 case LFUN_ACCENT_CIRCUMFLEX:
1927 case LFUN_ACCENT_GRAVE:
1928 case LFUN_ACCENT_ACUTE:
1929 case LFUN_ACCENT_TILDE:
1930 case LFUN_ACCENT_CEDILLA:
1931 case LFUN_ACCENT_MACRON:
1932 case LFUN_ACCENT_DOT:
1933 case LFUN_ACCENT_UNDERDOT:
1934 case LFUN_ACCENT_UNDERBAR:
1935 case LFUN_ACCENT_CARON:
1936 case LFUN_ACCENT_BREVE:
1937 case LFUN_ACCENT_TIE:
1938 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1939 case LFUN_ACCENT_CIRCLE:
1940 case LFUN_ACCENT_OGONEK:
1941 theApp()->handleKeyFunc(cmd.action());
1942 if (!cmd.argument().empty())
1943 // FIXME: Are all these characters encoded in one byte in utf8?
1944 bv->translateAndInsert(cmd.argument()[0], this, cur);
1947 case LFUN_FLOAT_LIST_INSERT: {
1948 DocumentClass const & tclass = bv->buffer().params().documentClass();
1949 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1951 if (cur.selection())
1952 cutSelection(cur, true, false);
1953 breakParagraph(cur);
1955 if (cur.lastpos() != 0) {
1956 cursorBackward(cur);
1957 breakParagraph(cur);
1960 docstring const laystr = cur.inset().usePlainLayout() ?
1961 tclass.plainLayoutName() :
1962 tclass.defaultLayoutName();
1963 setLayout(cur, laystr);
1964 ParagraphParameters p;
1965 // FIXME If this call were replaced with one to clearParagraphParams(),
1966 // then we could get rid of this method altogether.
1967 setParagraphs(cur, p);
1968 // FIXME This should be simplified when InsetFloatList takes a
1969 // Buffer in its constructor.
1970 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
1971 ifl->setBuffer(bv->buffer());
1972 insertInset(cur, ifl);
1975 lyxerr << "Non-existent float type: "
1976 << to_utf8(cmd.argument()) << endl;
1981 case LFUN_CHANGE_ACCEPT: {
1982 acceptOrRejectChanges(cur, ACCEPT);
1986 case LFUN_CHANGE_REJECT: {
1987 acceptOrRejectChanges(cur, REJECT);
1991 case LFUN_THESAURUS_ENTRY: {
1992 docstring arg = cmd.argument();
1994 arg = cur.selectionAsString(false);
1996 if (arg.size() > 100 || arg.empty()) {
1997 // Get word or selection
1998 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1999 arg = cur.selectionAsString(false);
2000 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2003 bv->showDialog("thesaurus", to_utf8(arg));
2007 case LFUN_SPELLING_ADD: {
2008 docstring word = from_utf8(cmd.getArg(0));
2011 word = cur.selectionAsString(false);
2013 if (word.size() > 100 || word.empty()) {
2014 // Get word or selection
2015 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2016 word = cur.selectionAsString(false);
2018 lang = const_cast<Language *>(cur.getFont().language());
2020 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2021 WordLangTuple wl(word, lang);
2022 theSpellChecker()->insert(wl);
2026 case LFUN_SPELLING_IGNORE: {
2027 docstring word = from_utf8(cmd.getArg(0));
2030 word = cur.selectionAsString(false);
2032 if (word.size() > 100 || word.empty()) {
2033 // Get word or selection
2034 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2035 word = cur.selectionAsString(false);
2037 lang = const_cast<Language *>(cur.getFont().language());
2039 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2040 WordLangTuple wl(word, lang);
2041 theSpellChecker()->accept(wl);
2045 case LFUN_SPELLING_REMOVE: {
2046 docstring word = from_utf8(cmd.getArg(0));
2049 word = cur.selectionAsString(false);
2051 if (word.size() > 100 || word.empty()) {
2052 // Get word or selection
2053 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2054 word = cur.selectionAsString(false);
2056 lang = const_cast<Language *>(cur.getFont().language());
2058 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2059 WordLangTuple wl(word, lang);
2060 theSpellChecker()->remove(wl);
2064 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2065 // Given data, an encoding of the ParagraphParameters
2066 // generated in the Paragraph dialog, this function sets
2067 // the current paragraph, or currently selected paragraphs,
2069 // NOTE: This function overrides all existing settings.
2070 setParagraphs(cur, cmd.argument());
2071 cur.message(_("Paragraph layout set"));
2075 case LFUN_PARAGRAPH_PARAMS: {
2076 // Given data, an encoding of the ParagraphParameters as we'd
2077 // find them in a LyX file, this function modifies the current paragraph,
2078 // or currently selected paragraphs.
2079 // NOTE: This function only modifies, and does not override, existing
2081 setParagraphs(cur, cmd.argument(), true);
2082 cur.message(_("Paragraph layout set"));
2087 if (cur.selection()) {
2088 cur.setSelection(false);
2091 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2092 // correct, but I'm not 100% sure -- dov, 071019
2093 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2097 case LFUN_OUTLINE_UP:
2098 outline(OutlineUp, cur);
2099 setCursor(cur, cur.pit(), 0);
2100 cur.forceBufferUpdate();
2104 case LFUN_OUTLINE_DOWN:
2105 outline(OutlineDown, cur);
2106 setCursor(cur, cur.pit(), 0);
2107 cur.forceBufferUpdate();
2111 case LFUN_OUTLINE_IN:
2112 outline(OutlineIn, cur);
2113 cur.forceBufferUpdate();
2117 case LFUN_OUTLINE_OUT:
2118 outline(OutlineOut, cur);
2119 cur.forceBufferUpdate();
2124 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2129 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2131 // FIXME: The cursor flag is reset two lines below
2132 // so we need to check here if some of the LFUN did touch that.
2133 // for now only Text::erase() and Text::backspace() do that.
2134 // The plan is to verify all the LFUNs and then to remove this
2135 // singleParUpdate boolean altogether.
2136 if (cur.result().update() & Update::Force) {
2137 singleParUpdate = false;
2141 // FIXME: the following code should go in favor of fine grained
2142 // update flag treatment.
2143 if (singleParUpdate) {
2144 // Inserting characters does not change par height in general. So, try
2145 // to update _only_ this paragraph. BufferView will detect if a full
2146 // metrics update is needed anyway.
2147 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2152 && &oldTopSlice.inset() == &cur.inset()
2153 && oldTopSlice.idx() == cur.idx()
2154 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2155 && !cur.selection())
2156 // FIXME: it would be better if we could just do this
2158 //if (cur.result().update() != Update::FitCursor)
2159 // cur.noScreenUpdate();
2161 // But some LFUNs do not set Update::FitCursor when needed, so we
2162 // do it for all. This is not very harmfull as FitCursor will provoke
2163 // a full redraw only if needed but still, a proper review of all LFUN
2164 // should be done and this needsUpdate boolean can then be removed.
2165 cur.screenUpdateFlags(Update::FitCursor);
2167 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2171 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2172 FuncStatus & flag) const
2174 LASSERT(cur.text() == this, /**/);
2176 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2178 InsetCode code = NO_CODE;
2180 switch (cmd.action()) {
2182 case LFUN_DEPTH_DECREMENT:
2183 enable = changeDepthAllowed(cur, DEC_DEPTH);
2186 case LFUN_DEPTH_INCREMENT:
2187 enable = changeDepthAllowed(cur, INC_DEPTH);
2191 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2194 case LFUN_DIALOG_SHOW_NEW_INSET:
2195 if (cmd.argument() == "bibitem")
2196 code = BIBITEM_CODE;
2197 else if (cmd.argument() == "bibtex") {
2199 // not allowed in description items
2200 enable = !inDescriptionItem(cur);
2202 else if (cmd.argument() == "box")
2204 else if (cmd.argument() == "branch")
2206 else if (cmd.argument() == "citation")
2208 else if (cmd.argument() == "ert")
2210 else if (cmd.argument() == "external")
2211 code = EXTERNAL_CODE;
2212 else if (cmd.argument() == "float")
2214 else if (cmd.argument() == "graphics")
2215 code = GRAPHICS_CODE;
2216 else if (cmd.argument() == "href")
2217 code = HYPERLINK_CODE;
2218 else if (cmd.argument() == "include")
2219 code = INCLUDE_CODE;
2220 else if (cmd.argument() == "index")
2222 else if (cmd.argument() == "index_print")
2223 code = INDEX_PRINT_CODE;
2224 else if (cmd.argument() == "nomenclature")
2225 code = NOMENCL_CODE;
2226 else if (cmd.argument() == "nomencl_print")
2227 code = NOMENCL_PRINT_CODE;
2228 else if (cmd.argument() == "label")
2230 else if (cmd.argument() == "line")
2232 else if (cmd.argument() == "note")
2234 else if (cmd.argument() == "phantom")
2235 code = PHANTOM_CODE;
2236 else if (cmd.argument() == "ref")
2238 else if (cmd.argument() == "space")
2240 else if (cmd.argument() == "toc")
2242 else if (cmd.argument() == "vspace")
2244 else if (cmd.argument() == "wrap")
2246 else if (cmd.argument() == "listings")
2247 code = LISTINGS_CODE;
2250 case LFUN_ERT_INSERT:
2253 case LFUN_LISTING_INSERT:
2254 code = LISTINGS_CODE;
2255 // not allowed in description items
2256 enable = !inDescriptionItem(cur);
2258 case LFUN_FOOTNOTE_INSERT:
2261 case LFUN_TABULAR_INSERT:
2262 code = TABULAR_CODE;
2264 case LFUN_MARGINALNOTE_INSERT:
2267 case LFUN_FLOAT_INSERT:
2268 case LFUN_FLOAT_WIDE_INSERT:
2269 // FIXME: If there is a selection, we should check whether there
2270 // are floats in the selection, but this has performance issues, see
2271 // LFUN_CHANGE_ACCEPT/REJECT.
2273 if (inDescriptionItem(cur))
2274 // not allowed in description items
2277 InsetCode const inset_code = cur.inset().lyxCode();
2279 // algorithm floats cannot be put in another float
2280 if (to_utf8(cmd.argument()) == "algorithm") {
2281 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2285 // for figures and tables: only allow in another
2286 // float or wrap if it is of the same type and
2287 // not a subfloat already
2288 if(cur.inset().lyxCode() == code) {
2289 InsetFloat const & ins =
2290 static_cast<InsetFloat const &>(cur.inset());
2291 enable = ins.params().type == to_utf8(cmd.argument())
2292 && !ins.params().subfloat;
2293 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2294 InsetWrap const & ins =
2295 static_cast<InsetWrap const &>(cur.inset());
2296 enable = ins.params().type == to_utf8(cmd.argument());
2300 case LFUN_WRAP_INSERT:
2302 // not allowed in description items
2303 enable = !inDescriptionItem(cur);
2305 case LFUN_FLOAT_LIST_INSERT: {
2306 code = FLOAT_LIST_CODE;
2307 // not allowed in description items
2308 enable = !inDescriptionItem(cur);
2310 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2311 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2312 // make sure we know about such floats
2313 if (cit == floats.end() ||
2314 // and that we know how to generate a list of them
2315 (!cit->second.needsFloatPkg() && cit->second.listCommand().empty())) {
2316 flag.setUnknown(true);
2317 // probably not necessary, but...
2323 case LFUN_CAPTION_INSERT:
2324 code = CAPTION_CODE;
2325 // not allowed in description items
2326 enable = !inDescriptionItem(cur);
2328 case LFUN_NOTE_INSERT:
2330 // in commands (sections etc.) and description items,
2331 // only Notes are allowed
2332 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2333 (!cur.paragraph().layout().isCommand()
2334 && !inDescriptionItem(cur)));
2336 case LFUN_FLEX_INSERT: {
2338 string s = cmd.getArg(0);
2340 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2341 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2342 il.lyxtype() != InsetLayout::CUSTOM &&
2343 il.lyxtype() != InsetLayout::ELEMENT &&
2344 il.lyxtype ()!= InsetLayout::STANDARD)
2348 case LFUN_BOX_INSERT:
2351 case LFUN_BRANCH_INSERT:
2353 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2354 && cur.buffer()->params().branchlist().empty())
2357 case LFUN_PHANTOM_INSERT:
2358 code = PHANTOM_CODE;
2360 case LFUN_LABEL_INSERT:
2363 case LFUN_INFO_INSERT:
2366 case LFUN_ARGUMENT_INSERT: {
2368 Layout const & lay = cur.paragraph().layout();
2369 int const numargs = lay.reqargs + lay.optargs;
2370 enable = cur.paragraph().insetList().count(ARG_CODE) < numargs;
2373 case LFUN_INDEX_INSERT:
2376 case LFUN_INDEX_PRINT:
2377 code = INDEX_PRINT_CODE;
2378 // not allowed in description items
2379 enable = !inDescriptionItem(cur);
2381 case LFUN_NOMENCL_INSERT:
2382 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2386 code = NOMENCL_CODE;
2388 case LFUN_NOMENCL_PRINT:
2389 code = NOMENCL_PRINT_CODE;
2390 // not allowed in description items
2391 enable = !inDescriptionItem(cur);
2393 case LFUN_HYPERLINK_INSERT:
2394 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2398 code = HYPERLINK_CODE;
2400 case LFUN_QUOTE_INSERT:
2401 // always allow this, since we will inset a raw quote
2402 // if an inset is not allowed.
2404 case LFUN_SPECIALCHAR_INSERT:
2405 code = SPECIALCHAR_CODE;
2407 case LFUN_SPACE_INSERT:
2408 // slight hack: we know this is allowed in math mode
2412 case LFUN_PREVIEW_INSERT:
2413 code = PREVIEW_CODE;
2416 case LFUN_MATH_INSERT:
2417 case LFUN_MATH_AMS_MATRIX:
2418 case LFUN_MATH_MATRIX:
2419 case LFUN_MATH_DELIM:
2420 case LFUN_MATH_BIGDELIM:
2421 case LFUN_MATH_DISPLAY:
2422 case LFUN_MATH_MODE:
2423 case LFUN_MATH_MACRO:
2424 case LFUN_MATH_SUBSCRIPT:
2425 case LFUN_MATH_SUPERSCRIPT:
2426 code = MATH_HULL_CODE;
2429 case LFUN_REGEXP_MODE:
2430 code = MATH_HULL_CODE;
2431 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2434 case LFUN_INSET_MODIFY:
2435 // We need to disable this, because we may get called for a
2437 // InsetTabular::getStatus() -> InsetText::getStatus()
2438 // and we don't handle LFUN_INSET_MODIFY.
2442 case LFUN_FONT_EMPH:
2443 flag.setOnOff(fontinfo.emph() == FONT_ON);
2444 enable = !cur.inset().getLayout().isPassThru();
2447 case LFUN_FONT_ITAL:
2448 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2449 enable = !cur.inset().getLayout().isPassThru();
2452 case LFUN_FONT_NOUN:
2453 flag.setOnOff(fontinfo.noun() == FONT_ON);
2454 enable = !cur.inset().getLayout().isPassThru();
2457 case LFUN_FONT_BOLD:
2458 case LFUN_FONT_BOLDSYMBOL:
2459 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2460 enable = !cur.inset().getLayout().isPassThru();
2463 case LFUN_FONT_SANS:
2464 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2465 enable = !cur.inset().getLayout().isPassThru();
2468 case LFUN_FONT_ROMAN:
2469 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2470 enable = !cur.inset().getLayout().isPassThru();
2473 case LFUN_FONT_TYPEWRITER:
2474 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2475 enable = !cur.inset().getLayout().isPassThru();
2480 enable = cur.selection();
2484 if (cmd.argument().empty()) {
2485 if (theClipboard().isInternal())
2486 enable = cap::numberOfSelections() > 0;
2488 enable = !theClipboard().empty();
2492 // we have an argument
2493 string const arg = to_utf8(cmd.argument());
2494 if (isStrUnsignedInt(arg)) {
2495 // it's a number and therefore means the internal stack
2496 unsigned int n = convert<unsigned int>(arg);
2497 enable = cap::numberOfSelections() > n;
2501 // explicit graphics type?
2502 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2503 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2504 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2505 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2506 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2507 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2508 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2509 enable = theClipboard().hasGraphicsContents(type);
2518 case LFUN_CLIPBOARD_PASTE:
2519 enable = !theClipboard().empty();
2522 case LFUN_PRIMARY_SELECTION_PASTE:
2523 enable = cur.selection() || !theSelection().empty();
2526 case LFUN_SELECTION_PASTE:
2527 enable = cap::selection();
2530 case LFUN_PARAGRAPH_MOVE_UP:
2531 enable = cur.pit() > 0 && !cur.selection();
2534 case LFUN_PARAGRAPH_MOVE_DOWN:
2535 enable = cur.pit() < cur.lastpit() && !cur.selection();
2538 case LFUN_CHANGE_ACCEPT:
2539 case LFUN_CHANGE_REJECT:
2540 // In principle, these LFUNs should only be enabled if there
2541 // is a change at the current position/in the current selection.
2542 // However, without proper optimizations, this will inevitably
2543 // result in unacceptable performance - just imagine a user who
2544 // wants to select the complete content of a long document.
2545 if (!cur.selection())
2546 enable = cur.paragraph().isChanged(cur.pos());
2548 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2553 case LFUN_OUTLINE_UP:
2554 case LFUN_OUTLINE_DOWN:
2555 case LFUN_OUTLINE_IN:
2556 case LFUN_OUTLINE_OUT:
2557 // FIXME: LyX is not ready for outlining within inset.
2558 enable = isMainText()
2559 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2562 case LFUN_NEWLINE_INSERT:
2563 // LaTeX restrictions (labels or empty par)
2564 enable = (cur.pos() > cur.paragraph().beginOfBody());
2567 case LFUN_TAB_INSERT:
2568 case LFUN_TAB_DELETE:
2569 enable = cur.inset().getLayout().isPassThru();
2572 case LFUN_SET_GRAPHICS_GROUP: {
2573 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2577 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2581 case LFUN_NEWPAGE_INSERT:
2582 // not allowed in description items
2583 code = NEWPAGE_CODE;
2584 enable = !inDescriptionItem(cur);
2587 case LFUN_DATE_INSERT: {
2588 string const format = cmd.argument().empty()
2589 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2590 enable = support::os::is_valid_strftime(format);
2595 enable = !cur.inset().getLayout().isPassThru();
2596 flag.setOnOff(to_utf8(cmd.argument()) == cur.real_current_font.language()->lang());
2599 case LFUN_BREAK_PARAGRAPH:
2600 enable = cur.inset().getLayout().isMultiPar();
2603 case LFUN_SPELLING_ADD:
2604 case LFUN_SPELLING_IGNORE:
2605 case LFUN_SPELLING_REMOVE:
2606 enable = theSpellChecker();
2610 enable = !cur.inset().forcePlainLayout();
2613 case LFUN_LAYOUT_PARAGRAPH:
2614 case LFUN_PARAGRAPH_PARAMS:
2615 case LFUN_PARAGRAPH_PARAMS_APPLY:
2616 case LFUN_PARAGRAPH_UPDATE:
2617 enable = cur.inset().allowParagraphCustomization();
2620 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2621 case LFUN_ACCENT_ACUTE:
2622 case LFUN_ACCENT_BREVE:
2623 case LFUN_ACCENT_CARON:
2624 case LFUN_ACCENT_CEDILLA:
2625 case LFUN_ACCENT_CIRCLE:
2626 case LFUN_ACCENT_CIRCUMFLEX:
2627 case LFUN_ACCENT_DOT:
2628 case LFUN_ACCENT_GRAVE:
2629 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2630 case LFUN_ACCENT_MACRON:
2631 case LFUN_ACCENT_OGONEK:
2632 case LFUN_ACCENT_TIE:
2633 case LFUN_ACCENT_TILDE:
2634 case LFUN_ACCENT_UMLAUT:
2635 case LFUN_ACCENT_UNDERBAR:
2636 case LFUN_ACCENT_UNDERDOT:
2637 case LFUN_FONT_DEFAULT:
2638 case LFUN_FONT_FRAK:
2639 case LFUN_FONT_SIZE:
2640 case LFUN_FONT_STATE:
2641 case LFUN_FONT_UNDERLINE:
2642 case LFUN_FONT_STRIKEOUT:
2643 case LFUN_FONT_UULINE:
2644 case LFUN_FONT_UWAVE:
2645 case LFUN_TEXTSTYLE_APPLY:
2646 case LFUN_TEXTSTYLE_UPDATE:
2647 enable = !cur.inset().getLayout().isPassThru();
2650 case LFUN_WORD_DELETE_FORWARD:
2651 case LFUN_WORD_DELETE_BACKWARD:
2652 case LFUN_LINE_DELETE:
2653 case LFUN_WORD_FORWARD:
2654 case LFUN_WORD_BACKWARD:
2655 case LFUN_WORD_RIGHT:
2656 case LFUN_WORD_LEFT:
2657 case LFUN_CHAR_FORWARD:
2658 case LFUN_CHAR_FORWARD_SELECT:
2659 case LFUN_CHAR_BACKWARD:
2660 case LFUN_CHAR_BACKWARD_SELECT:
2661 case LFUN_CHAR_LEFT:
2662 case LFUN_CHAR_LEFT_SELECT:
2663 case LFUN_CHAR_RIGHT:
2664 case LFUN_CHAR_RIGHT_SELECT:
2666 case LFUN_UP_SELECT:
2668 case LFUN_DOWN_SELECT:
2669 case LFUN_PARAGRAPH_UP_SELECT:
2670 case LFUN_PARAGRAPH_DOWN_SELECT:
2671 case LFUN_LINE_BEGIN_SELECT:
2672 case LFUN_LINE_END_SELECT:
2673 case LFUN_WORD_FORWARD_SELECT:
2674 case LFUN_WORD_BACKWARD_SELECT:
2675 case LFUN_WORD_RIGHT_SELECT:
2676 case LFUN_WORD_LEFT_SELECT:
2677 case LFUN_WORD_SELECT:
2678 case LFUN_SECTION_SELECT:
2679 case LFUN_BUFFER_BEGIN:
2680 case LFUN_BUFFER_END:
2681 case LFUN_BUFFER_BEGIN_SELECT:
2682 case LFUN_BUFFER_END_SELECT:
2683 case LFUN_INSET_BEGIN:
2684 case LFUN_INSET_END:
2685 case LFUN_INSET_BEGIN_SELECT:
2686 case LFUN_INSET_END_SELECT:
2687 case LFUN_INSET_SELECT_ALL:
2688 case LFUN_PARAGRAPH_UP:
2689 case LFUN_PARAGRAPH_DOWN:
2690 case LFUN_LINE_BEGIN:
2692 case LFUN_CHAR_DELETE_FORWARD:
2693 case LFUN_CHAR_DELETE_BACKWARD:
2694 case LFUN_WORD_UPCASE:
2695 case LFUN_WORD_LOWCASE:
2696 case LFUN_WORD_CAPITALIZE:
2697 case LFUN_CHARS_TRANSPOSE:
2698 case LFUN_SERVER_GET_XY:
2699 case LFUN_SERVER_SET_XY:
2700 case LFUN_SERVER_GET_LAYOUT:
2701 case LFUN_SELF_INSERT:
2702 case LFUN_UNICODE_INSERT:
2703 case LFUN_THESAURUS_ENTRY:
2705 // these are handled in our dispatch()
2709 case LFUN_INSET_INSERT: {
2710 string const type = cmd.getArg(0);
2711 if (type == "toc") {
2713 // not allowed in description items
2714 //FIXME: couldn't this be merged in Inset::insetAllowed()?
2715 enable = !inDescriptionItem(cur);
2728 || !cur.inset().insetAllowed(code)
2729 || cur.paragraph().layout().pass_thru))
2732 flag.setEnabled(enable);
2737 void Text::pasteString(Cursor & cur, docstring const & clip,
2740 cur.clearSelection();
2741 if (!clip.empty()) {
2744 insertStringAsParagraphs(cur, clip, cur.current_font);
2746 insertStringAsLines(cur, clip, cur.current_font);
2751 // FIXME: an item inset would make things much easier.
2752 bool Text::inDescriptionItem(Cursor & cur) const
2754 Paragraph & par = cur.paragraph();
2755 pos_type const pos = cur.pos();
2756 pos_type const body_pos = par.beginOfBody();
2758 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2759 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2760 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2763 return (pos < body_pos
2765 && (pos == 0 || par.getChar(pos - 1) != ' ')));