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"
48 #include "WordLangTuple.h"
50 #include "frontends/Application.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/Selection.h"
54 #include "insets/InsetArgument.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/InsetIPAMacro.h"
63 #include "insets/InsetNewline.h"
64 #include "insets/InsetQuotes.h"
65 #include "insets/InsetSpecialChar.h"
66 #include "insets/InsetText.h"
67 #include "insets/InsetWrap.h"
69 #include "support/convert.h"
70 #include "support/debug.h"
71 #include "support/gettext.h"
72 #include "support/lassert.h"
73 #include "support/lstrings.h"
74 #include "support/lyxtime.h"
75 #include "support/os.h"
77 #include "mathed/InsetMathHull.h"
78 #include "mathed/MathMacroTemplate.h"
80 #include <boost/next_prior.hpp>
86 using namespace lyx::support;
90 using cap::copySelection;
91 using cap::cutSelection;
92 using cap::pasteFromStack;
93 using cap::pasteClipboardText;
94 using cap::pasteClipboardGraphics;
95 using cap::replaceSelection;
96 using cap::grabAndEraseSelection;
97 using cap::selClearOrDel;
98 using cap::pasteSimpleText;
101 static Font freefont(ignore_font, ignore_language);
102 static bool toggleall = false;
104 static void toggleAndShow(Cursor & cur, Text * text,
105 Font const & font, bool toggleall = true)
107 text->toggleFree(cur, font, toggleall);
109 if (font.language() != ignore_language ||
110 font.fontInfo().number() != FONT_IGNORE) {
111 TextMetrics const & tm = cur.bv().textMetrics(text);
112 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
113 cur.real_current_font))
114 text->setCursor(cur, cur.pit(), cur.pos(),
115 false, !cur.boundary());
120 static void moveCursor(Cursor & cur, bool selecting)
122 if (selecting || cur.mark())
127 static void finishChange(Cursor & cur, bool selecting)
130 moveCursor(cur, selecting);
134 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
137 docstring sel = cur.selectionAsString(false);
139 // It may happen that sel is empty but there is a selection
140 replaceSelection(cur);
142 // Is this a valid formula?
146 #ifdef ENABLE_ASSERTIONS
147 const int old_pos = cur.pos();
149 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
150 #ifdef ENABLE_ASSERTIONS
151 LASSERT(old_pos == cur.pos(), /**/);
153 cur.nextInset()->edit(cur, true);
154 // don't do that also for LFUN_MATH_MODE
155 // unless you want end up with always changing
156 // to mathrm when opening an inlined inset --
157 // I really hate "LyXfunc overloading"...
159 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
160 // Avoid an unnecessary undo step if cmd.argument
162 if (!cmd.argument().empty())
163 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
166 // create a macro if we see "\\newcommand"
167 // somewhere, and an ordinary formula
169 if (sel.find(from_ascii("\\newcommand")) == string::npos
170 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
171 && sel.find(from_ascii("\\def")) == string::npos)
173 InsetMathHull * formula = new InsetMathHull(cur.buffer());
174 string const selstr = to_utf8(sel);
175 istringstream is(selstr);
178 if (!formula->readQuiet(lex)) {
179 // No valid formula, let's try with delims
180 is.str("$" + selstr + "$");
182 if (!formula->readQuiet(lex)) {
183 // Still not valid, leave it as is
192 cur.insert(new MathMacroTemplate(cur.buffer(), sel));
196 cur.message(from_utf8(N_("Math editor mode")));
198 cur.message(from_utf8(N_("No valid math formula")));
202 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
204 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
205 if (cur.inRegexped()) {
206 cur.message(_("Already in regular expression mode"));
210 docstring sel = cur.selectionAsString(false);
212 // It may happen that sel is empty but there is a selection
213 replaceSelection(cur);
215 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
216 cur.nextInset()->edit(cur, true);
219 cur.message(_("Regexp editor mode"));
223 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
226 cap::replaceSelection(cur);
227 cur.insert(new InsetSpecialChar(kind));
232 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
235 cap::replaceSelection(cur);
236 cur.insert(new InsetIPAChar(kind));
241 static bool doInsertInset(Cursor & cur, Text * text,
242 FuncRequest const & cmd, bool edit, bool pastesel)
244 Buffer & buffer = cur.bv().buffer();
245 BufferParams const & bparams = buffer.params();
246 Inset * inset = createInset(&buffer, cmd);
250 if (InsetCollapsable * ci = inset->asInsetCollapsable())
251 ci->setButtonLabel();
254 if (cmd.action() == LFUN_INDEX_INSERT) {
255 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
256 text->insertInset(cur, inset);
258 inset->edit(cur, true);
259 // Now put this into inset
260 Font const f(inherit_font, cur.current_font.language());
262 cur.text()->insertStringAsLines(cur, ds, f);
263 cur.leaveInset(*inset);
269 if (cur.selection()) {
270 cutSelection(cur, false, pastesel);
271 cur.clearSelection();
274 text->insertInset(cur, inset);
277 inset->edit(cur, true);
279 if (!gotsel || !pastesel)
282 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
283 cur.buffer()->errors("Paste");
284 cur.clearSelection(); // bug 393
286 InsetText * inset_text = inset->asInsetText();
288 inset_text->fixParagraphsFont();
289 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
290 // reset first par to default
291 cur.text()->paragraphs().begin()
292 ->setPlainOrDefaultLayout(bparams.documentClass());
295 // Merge multiple paragraphs -- hack
296 while (cur.lastpit() > 0)
297 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
299 cur.leaveInset(*inset);
300 if (cmd.action() == LFUN_PREVIEW_INSERT
301 || cmd.action() == LFUN_IPA_INSERT)
303 notifyCursorLeavesOrEnters(old, cur);
306 cur.leaveInset(*inset);
307 // reset surrounding par to default
308 DocumentClass const & dc = bparams.documentClass();
309 docstring const layoutname = inset->usePlainLayout()
310 ? dc.plainLayoutName()
311 : dc.defaultLayoutName();
312 text->setLayout(cur, layoutname);
318 string const freefont2string()
320 return freefont.toString(toggleall);
324 /// the type of outline operation
326 OutlineUp, // Move this header with text down
327 OutlineDown, // Move this header with text up
328 OutlineIn, // Make this header deeper
329 OutlineOut // Make this header shallower
333 static void outline(OutlineOp mode, Cursor & cur)
335 Buffer & buf = *cur.buffer();
336 pit_type & pit = cur.pit();
337 ParagraphList & pars = buf.text().paragraphs();
338 ParagraphList::iterator const bgn = pars.begin();
339 // The first paragraph of the area to be copied:
340 ParagraphList::iterator start = boost::next(bgn, pit);
341 // The final paragraph of area to be copied:
342 ParagraphList::iterator finish = start;
343 ParagraphList::iterator const end = pars.end();
345 DocumentClass const & tc = buf.params().documentClass();
347 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
350 // Move out (down) from this section header
354 // Seek the one (on same level) below
355 for (; finish != end; ++finish) {
356 toclevel = buf.text().getTocLevel(distance(bgn, finish));
357 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
363 if (start == pars.begin())
366 ParagraphList::iterator dest = start;
367 // Move out (up) from this header
370 // Search previous same-level header above
373 toclevel = buf.text().getTocLevel(distance(bgn, dest));
375 && (toclevel == Layout::NOT_IN_TOC
376 || toclevel > thistoclevel));
377 // Not found; do nothing
378 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
380 pit_type const newpit = distance(bgn, dest);
381 pit_type const len = distance(start, finish);
382 pit_type const deletepit = pit + len;
383 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
384 pars.splice(dest, start, finish);
392 // Go one down from *this* header:
393 ParagraphList::iterator dest = boost::next(finish, 1);
394 // Go further down to find header to insert in front of:
395 for (; dest != end; ++dest) {
396 toclevel = buf.text().getTocLevel(distance(bgn, dest));
397 if (toclevel != Layout::NOT_IN_TOC
398 && toclevel <= thistoclevel)
401 // One such was found:
402 pit_type newpit = distance(bgn, dest);
403 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
404 pit_type const len = distance(start, finish);
405 pars.splice(dest, start, finish);
406 cur.pit() = newpit - len;
410 pit_type const len = distance(start, finish);
411 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
412 for (; start != finish; ++start) {
413 toclevel = buf.text().getTocLevel(distance(bgn, start));
414 if (toclevel == Layout::NOT_IN_TOC)
416 DocumentClass::const_iterator lit = tc.begin();
417 DocumentClass::const_iterator len = tc.end();
418 for (; lit != len; ++lit) {
419 if (lit->toclevel == toclevel + 1 &&
420 start->layout().labeltype == lit->labeltype) {
421 start->setLayout(*lit);
429 pit_type const len = distance(start, finish);
430 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
431 for (; start != finish; ++start) {
432 toclevel = buf.text().getTocLevel(distance(bgn, start));
433 if (toclevel == Layout::NOT_IN_TOC)
435 DocumentClass::const_iterator lit = tc.begin();
436 DocumentClass::const_iterator len = tc.end();
437 for (; lit != len; ++lit) {
438 if (lit->toclevel == toclevel - 1 &&
439 start->layout().labeltype == lit->labeltype) {
440 start->setLayout(*lit);
451 void Text::number(Cursor & cur)
453 FontInfo font = ignore_font;
454 font.setNumber(FONT_TOGGLE);
455 toggleAndShow(cur, this, Font(font, ignore_language));
459 bool Text::isRTL(Paragraph const & par) const
461 Buffer const & buffer = owner_->buffer();
462 return par.isRTL(buffer.params());
466 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
468 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
470 // Dispatch if the cursor is inside the text. It is not the
471 // case for context menus (bug 5797).
472 if (cur.text() != this) {
477 BufferView * bv = &cur.bv();
478 TextMetrics * tm = &bv->textMetrics(this);
479 if (!tm->contains(cur.pit())) {
480 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
481 tm = &bv->textMetrics(this);
484 // FIXME: We use the update flag to indicates wether a singlePar or a
485 // full screen update is needed. We reset it here but shall we restore it
487 cur.noScreenUpdate();
489 LASSERT(cur.text() == this, /**/);
490 CursorSlice const oldTopSlice = cur.top();
491 bool const oldBoundary = cur.boundary();
492 bool const oldSelection = cur.selection();
493 // Signals that, even if needsUpdate == false, an update of the
494 // cursor paragraph is required
495 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
496 LyXAction::SingleParUpdate);
497 // Signals that a full-screen update is required
498 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
499 LyXAction::NoUpdate) || singleParUpdate);
500 bool const last_misspelled = lyxrc.spellcheck_continuously
501 && cur.paragraph().isMisspelled(cur.pos(), true);
503 FuncCode const act = cmd.action();
506 case LFUN_PARAGRAPH_MOVE_DOWN: {
507 pit_type const pit = cur.pit();
508 recUndo(cur, pit, pit + 1);
510 pars_.swap(pit, pit + 1);
512 cur.forceBufferUpdate();
517 case LFUN_PARAGRAPH_MOVE_UP: {
518 pit_type const pit = cur.pit();
519 recUndo(cur, pit - 1, pit);
521 pars_.swap(pit, pit - 1);
524 cur.forceBufferUpdate();
528 case LFUN_APPENDIX: {
529 Paragraph & par = cur.paragraph();
530 bool start = !par.params().startOfAppendix();
532 // FIXME: The code below only makes sense at top level.
533 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
534 // ensure that we have only one start_of_appendix in this document
535 // FIXME: this don't work for multipart document!
536 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
537 if (pars_[tmp].params().startOfAppendix()) {
539 pars_[tmp].params().startOfAppendix(false);
545 par.params().startOfAppendix(start);
547 // we can set the refreshing parameters now
548 cur.forceBufferUpdate();
552 case LFUN_WORD_DELETE_FORWARD:
554 cutSelection(cur, true, false);
556 deleteWordForward(cur);
557 finishChange(cur, false);
560 case LFUN_WORD_DELETE_BACKWARD:
562 cutSelection(cur, true, false);
564 deleteWordBackward(cur);
565 finishChange(cur, false);
568 case LFUN_LINE_DELETE:
570 cutSelection(cur, true, false);
572 tm->deleteLineForward(cur);
573 finishChange(cur, false);
576 case LFUN_BUFFER_BEGIN:
577 case LFUN_BUFFER_BEGIN_SELECT:
578 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
579 if (cur.depth() == 1)
580 needsUpdate |= cursorTop(cur);
583 cur.screenUpdateFlags(Update::FitCursor);
586 case LFUN_BUFFER_END:
587 case LFUN_BUFFER_END_SELECT:
588 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
589 if (cur.depth() == 1)
590 needsUpdate |= cursorBottom(cur);
593 cur.screenUpdateFlags(Update::FitCursor);
596 case LFUN_INSET_BEGIN:
597 case LFUN_INSET_BEGIN_SELECT:
598 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
599 if (cur.depth() == 1 || !cur.top().at_begin())
600 needsUpdate |= cursorTop(cur);
603 cur.screenUpdateFlags(Update::FitCursor);
607 case LFUN_INSET_END_SELECT:
608 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
609 if (cur.depth() == 1 || !cur.top().at_end())
610 needsUpdate |= cursorBottom(cur);
613 cur.screenUpdateFlags(Update::FitCursor);
616 case LFUN_INSET_SELECT_ALL:
617 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
618 || !cur.selEnd().at_end()) {
619 needsUpdate |= cur.selHandle(false);
620 needsUpdate |= cursorTop(cur);
621 needsUpdate |= cur.selHandle(true);
622 needsUpdate |= cursorBottom(cur);
625 cur.screenUpdateFlags(Update::FitCursor);
628 case LFUN_CHAR_FORWARD:
629 case LFUN_CHAR_FORWARD_SELECT:
630 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
631 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
632 needsUpdate |= cursorForward(cur);
634 if (!needsUpdate && oldTopSlice == cur.top()
635 && cur.boundary() == oldBoundary) {
637 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
639 // we will probably be moving out the inset, so we should execute
640 // the depm-mechanism, but only when the cursor has a place to
641 // go outside this inset, i.e. in a slice above.
642 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
643 && cur.pit() == cur.lastpit()) {
644 // The cursor hasn't changed yet. To give the
645 // DEPM the possibility of doing something we must
646 // provide it with two different cursors.
648 dummy.pos() = dummy.pit() = 0;
649 if (cur.bv().checkDepm(dummy, cur)) {
650 cur.forceBufferUpdate();
651 // DEPM may have requested a screen update
652 cur.screenUpdateFlags(
653 cur.screenUpdate() | dummy.screenUpdate());
659 case LFUN_CHAR_BACKWARD:
660 case LFUN_CHAR_BACKWARD_SELECT:
661 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
662 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
663 needsUpdate |= cursorBackward(cur);
665 if (!needsUpdate && oldTopSlice == cur.top()
666 && cur.boundary() == oldBoundary) {
668 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
670 // we will probably be moving out the inset, so we should execute
671 // the depm-mechanism, but only when the cursor has a place to
672 // go outside this inset, i.e. in a slice above.
673 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
674 // The cursor hasn't changed yet. To give the
675 // DEPM the possibility of doing something we must
676 // provide it with two different cursors.
678 dummy.pos() = cur.lastpos();
679 dummy.pit() = cur.lastpit();
680 if (cur.bv().checkDepm(dummy, cur)) {
681 cur.forceBufferUpdate();
682 // DEPM may have requested a screen update
683 cur.screenUpdateFlags(
684 cur.screenUpdate() | dummy.screenUpdate());
691 case LFUN_CHAR_LEFT_SELECT:
692 if (lyxrc.visual_cursor) {
693 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
694 needsUpdate |= cursorVisLeft(cur);
695 if (!needsUpdate && oldTopSlice == cur.top()
696 && cur.boundary() == oldBoundary) {
698 cmd = FuncRequest(LFUN_FINISHED_LEFT);
701 if (reverseDirectionNeeded(cur)) {
702 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
703 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
705 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
706 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
713 case LFUN_CHAR_RIGHT:
714 case LFUN_CHAR_RIGHT_SELECT:
715 if (lyxrc.visual_cursor) {
716 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
717 needsUpdate |= cursorVisRight(cur);
718 if (!needsUpdate && oldTopSlice == cur.top()
719 && cur.boundary() == oldBoundary) {
721 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
724 if (reverseDirectionNeeded(cur)) {
725 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
726 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
728 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
729 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
738 case LFUN_DOWN_SELECT:
741 // stop/start the selection
742 bool select = cmd.action() == LFUN_DOWN_SELECT ||
743 cmd.action() == LFUN_UP_SELECT;
745 // move cursor up/down
746 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
747 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
749 if (!atFirstOrLastRow) {
750 needsUpdate |= cur.selHandle(select);
751 cur.selHandle(select);
752 cur.upDownInText(up, needsUpdate);
753 needsUpdate |= cur.beforeDispatchCursor().inMathed();
755 // if the cursor cannot be moved up or down do not remove
756 // the selection right now, but wait for the next dispatch.
758 needsUpdate |= cur.selHandle(select);
759 cur.upDownInText(up, needsUpdate);
766 case LFUN_PARAGRAPH_UP:
767 case LFUN_PARAGRAPH_UP_SELECT:
768 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
769 needsUpdate |= cursorUpParagraph(cur);
772 case LFUN_PARAGRAPH_DOWN:
773 case LFUN_PARAGRAPH_DOWN_SELECT:
774 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
775 needsUpdate |= cursorDownParagraph(cur);
778 case LFUN_LINE_BEGIN:
779 case LFUN_LINE_BEGIN_SELECT:
780 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
781 needsUpdate |= tm->cursorHome(cur);
785 case LFUN_LINE_END_SELECT:
786 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
787 needsUpdate |= tm->cursorEnd(cur);
790 case LFUN_SECTION_SELECT: {
791 Buffer const & buf = *cur.buffer();
792 pit_type const pit = cur.pit();
793 ParagraphList & pars = buf.text().paragraphs();
794 ParagraphList::iterator bgn = pars.begin();
795 // The first paragraph of the area to be selected:
796 ParagraphList::iterator start = boost::next(bgn, pit);
797 // The final paragraph of area to be selected:
798 ParagraphList::iterator finish = start;
799 ParagraphList::iterator end = pars.end();
801 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
802 if (thistoclevel == Layout::NOT_IN_TOC)
806 Cursor const old_cur = cur;
807 needsUpdate |= cur.selHandle(true);
809 // Move out (down) from this section header
813 // Seek the one (on same level) below
814 for (; finish != end; ++finish, ++cur.pit()) {
815 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
816 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
819 cur.pos() = cur.lastpos();
821 needsUpdate |= cur != old_cur;
825 case LFUN_WORD_RIGHT:
826 case LFUN_WORD_RIGHT_SELECT:
827 if (lyxrc.visual_cursor) {
828 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
829 needsUpdate |= cursorVisRightOneWord(cur);
830 if (!needsUpdate && oldTopSlice == cur.top()
831 && cur.boundary() == oldBoundary) {
833 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
836 if (reverseDirectionNeeded(cur)) {
837 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
838 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
840 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
841 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
848 case LFUN_WORD_FORWARD:
849 case LFUN_WORD_FORWARD_SELECT:
850 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
851 needsUpdate |= cursorForwardOneWord(cur);
853 if (!needsUpdate && oldTopSlice == cur.top()
854 && cur.boundary() == oldBoundary) {
856 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
858 // we will probably be moving out the inset, so we should execute
859 // the depm-mechanism, but only when the cursor has a place to
860 // go outside this inset, i.e. in a slice above.
861 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
862 && cur.pit() == cur.lastpit()) {
863 // The cursor hasn't changed yet. To give the
864 // DEPM the possibility of doing something we must
865 // provide it with two different cursors.
867 dummy.pos() = dummy.pit() = 0;
868 if (cur.bv().checkDepm(dummy, cur)) {
869 cur.forceBufferUpdate();
870 // DEPM may have requested a screen update
871 cur.screenUpdateFlags(
872 cur.screenUpdate() | dummy.screenUpdate());
879 case LFUN_WORD_LEFT_SELECT:
880 if (lyxrc.visual_cursor) {
881 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
882 needsUpdate |= cursorVisLeftOneWord(cur);
883 if (!needsUpdate && oldTopSlice == cur.top()
884 && cur.boundary() == oldBoundary) {
886 cmd = FuncRequest(LFUN_FINISHED_LEFT);
889 if (reverseDirectionNeeded(cur)) {
890 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
891 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
893 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
894 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
901 case LFUN_WORD_BACKWARD:
902 case LFUN_WORD_BACKWARD_SELECT:
903 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
904 needsUpdate |= cursorBackwardOneWord(cur);
906 if (!needsUpdate && oldTopSlice == cur.top()
907 && cur.boundary() == oldBoundary) {
909 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
911 // we will probably be moving out the inset, so we should execute
912 // the depm-mechanism, but only when the cursor has a place to
913 // go outside this inset, i.e. in a slice above.
914 if (cur.depth() > 1 && cur.pos() == 0
916 // The cursor hasn't changed yet. To give the
917 // DEPM the possibility of doing something we must
918 // provide it with two different cursors.
920 dummy.pos() = cur.lastpos();
921 dummy.pit() = cur.lastpit();
922 if (cur.bv().checkDepm(dummy, cur)) {
923 cur.forceBufferUpdate();
924 // DEPM may have requested a screen update
925 cur.screenUpdateFlags(
926 cur.screenUpdate() | dummy.screenUpdate());
932 case LFUN_WORD_SELECT: {
933 selectWord(cur, WHOLE_WORD);
934 finishChange(cur, true);
938 case LFUN_NEWLINE_INSERT: {
939 InsetNewlineParams inp;
940 docstring arg = cmd.argument();
941 // this avoids a double undo
942 // FIXME: should not be needed, ideally
943 if (!cur.selection())
945 cap::replaceSelection(cur);
946 if (arg == "linebreak")
947 inp.kind = InsetNewlineParams::LINEBREAK;
949 inp.kind = InsetNewlineParams::NEWLINE;
950 cur.insert(new InsetNewline(inp));
952 moveCursor(cur, false);
956 case LFUN_TAB_INSERT: {
957 bool const multi_par_selection = cur.selection() &&
958 cur.selBegin().pit() != cur.selEnd().pit();
959 if (multi_par_selection) {
960 // If there is a multi-paragraph selection, a tab is inserted
961 // at the beginning of each paragraph.
962 cur.recordUndoSelection();
963 pit_type const pit_end = cur.selEnd().pit();
964 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
965 pars_[pit].insertChar(0, '\t',
966 bv->buffer().params().trackChanges);
967 // Update the selection pos to make sure the selection does not
968 // change as the inserted tab will increase the logical pos.
969 if (cur.realAnchor().pit() == pit)
970 cur.realAnchor().forwardPos();
971 if (cur.pit() == pit)
976 // Maybe we shouldn't allow tabs within a line, because they
977 // are not (yet) aligned as one might do expect.
978 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
984 case LFUN_TAB_DELETE: {
985 bool const tc = bv->buffer().params().trackChanges;
986 if (cur.selection()) {
987 // If there is a selection, a tab (if present) is removed from
988 // the beginning of each paragraph.
989 cur.recordUndoSelection();
990 pit_type const pit_end = cur.selEnd().pit();
991 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
992 Paragraph & par = paragraphs()[pit];
995 char_type const c = par.getChar(0);
996 if (c == '\t' || c == ' ') {
997 // remove either 1 tab or 4 spaces.
998 int const n = (c == ' ' ? 4 : 1);
999 for (int i = 0; i < n
1000 && !par.empty() && par.getChar(0) == c; ++i) {
1001 if (cur.pit() == pit)
1003 if (cur.realAnchor().pit() == pit
1004 && cur.realAnchor().pos() > 0 )
1005 cur.realAnchor().backwardPos();
1006 par.eraseChar(0, tc);
1012 // If there is no selection, try to remove a tab or some spaces
1013 // before the position of the cursor.
1014 Paragraph & par = paragraphs()[cur.pit()];
1015 pos_type const pos = cur.pos();
1020 char_type const c = par.getChar(pos - 1);
1024 par.eraseChar(cur.pos(), tc);
1026 for (int n_spaces = 0;
1028 && par.getChar(cur.pos() - 1) == ' '
1032 par.eraseChar(cur.pos(), tc);
1039 case LFUN_CHAR_DELETE_FORWARD:
1040 if (!cur.selection()) {
1041 if (cur.pos() == cur.paragraph().size())
1042 // Par boundary, force full-screen update
1043 singleParUpdate = false;
1044 needsUpdate |= erase(cur);
1046 // It is possible to make it a lot faster still
1047 // just comment out the line below...
1049 cutSelection(cur, true, false);
1050 singleParUpdate = false;
1052 moveCursor(cur, false);
1055 case LFUN_CHAR_DELETE_BACKWARD:
1056 if (!cur.selection()) {
1057 if (bv->getIntl().getTransManager().backspace()) {
1058 // Par boundary, full-screen update
1060 singleParUpdate = false;
1061 needsUpdate |= backspace(cur);
1063 // It is possible to make it a lot faster still
1064 // just comment out the line below...
1067 cutSelection(cur, true, false);
1068 singleParUpdate = false;
1072 case LFUN_PARAGRAPH_BREAK:
1073 cap::replaceSelection(cur);
1074 breakParagraph(cur, cmd.argument() == "inverse");
1078 case LFUN_INSET_INSERT: {
1081 // We have to avoid triggering InstantPreview loading
1082 // before inserting into the document. See bug #5626.
1083 bool loaded = bv->buffer().isFullyLoaded();
1084 bv->buffer().setFullyLoaded(false);
1085 Inset * inset = createInset(&bv->buffer(), cmd);
1086 bv->buffer().setFullyLoaded(loaded);
1089 // FIXME (Abdel 01/02/2006):
1090 // What follows would be a partial fix for bug 2154:
1091 // http://www.lyx.org/trac/ticket/2154
1092 // This automatically put the label inset _after_ a
1093 // numbered section. It should be possible to extend the mechanism
1094 // to any kind of LateX environement.
1095 // The correct way to fix that bug would be at LateX generation.
1096 // I'll let the code here for reference as it could be used for some
1097 // other feature like "automatic labelling".
1099 Paragraph & par = pars_[cur.pit()];
1100 if (inset->lyxCode() == LABEL_CODE
1101 && par.layout().labeltype == LABEL_COUNTER) {
1102 // Go to the end of the paragraph
1103 // Warning: Because of Change-Tracking, the last
1104 // position is 'size()' and not 'size()-1':
1105 cur.pos() = par.size();
1106 // Insert a new paragraph
1107 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1111 if (cur.selection())
1112 cutSelection(cur, true, false);
1114 if (inset->editable() && inset->asInsetText())
1115 inset->edit(cur, true);
1119 // trigger InstantPreview now
1120 if (inset->lyxCode() == EXTERNAL_CODE) {
1121 InsetExternal & ins =
1122 static_cast<InsetExternal &>(*inset);
1123 ins.updatePreview();
1130 case LFUN_INSET_DISSOLVE: {
1131 if (dissolveInset(cur)) {
1133 cur.forceBufferUpdate();
1138 case LFUN_SET_GRAPHICS_GROUP: {
1139 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1145 string id = to_utf8(cmd.argument());
1146 string grp = graphics::getGroupParams(bv->buffer(), id);
1147 InsetGraphicsParams tmp, inspar = ins->getParams();
1150 inspar.groupId = to_utf8(cmd.argument());
1152 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1153 tmp.filename = inspar.filename;
1157 ins->setParams(inspar);
1160 case LFUN_SPACE_INSERT:
1161 if (cur.paragraph().layout().free_spacing)
1162 insertChar(cur, ' ');
1164 doInsertInset(cur, this, cmd, false, false);
1167 moveCursor(cur, false);
1170 case LFUN_SPECIALCHAR_INSERT: {
1171 string const name = to_utf8(cmd.argument());
1172 if (name == "hyphenation")
1173 specialChar(cur, InsetSpecialChar::HYPHENATION);
1174 else if (name == "ligature-break")
1175 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1176 else if (name == "slash")
1177 specialChar(cur, InsetSpecialChar::SLASH);
1178 else if (name == "nobreakdash")
1179 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1180 else if (name == "dots")
1181 specialChar(cur, InsetSpecialChar::LDOTS);
1182 else if (name == "end-of-sentence")
1183 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1184 else if (name == "menu-separator")
1185 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1186 else if (name.empty())
1187 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1189 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1193 case LFUN_IPAMACRO_INSERT: {
1194 string const arg = cmd.getArg(0);
1195 if (arg == "deco") {
1196 // Open the inset, and move the current selection
1198 doInsertInset(cur, this, cmd, true, true);
1200 // Some insets are numbered, others are shown in the outline pane so
1201 // let's update the labels and the toc backend.
1202 cur.forceBufferUpdate();
1205 if (arg == "tone-falling")
1206 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1207 else if (arg == "tone-rising")
1208 ipaChar(cur, InsetIPAChar::TONE_RISING);
1209 else if (arg == "tone-high-rising")
1210 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1211 else if (arg == "tone-low-rising")
1212 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1213 else if (arg == "tone-high-rising-falling")
1214 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1215 else if (arg.empty())
1216 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1218 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1222 case LFUN_WORD_UPCASE:
1223 changeCase(cur, text_uppercase);
1226 case LFUN_WORD_LOWCASE:
1227 changeCase(cur, text_lowercase);
1230 case LFUN_WORD_CAPITALIZE:
1231 changeCase(cur, text_capitalization);
1234 case LFUN_CHARS_TRANSPOSE:
1235 charsTranspose(cur);
1239 cur.message(_("Paste"));
1240 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1241 cap::replaceSelection(cur);
1243 // without argument?
1244 string const arg = to_utf8(cmd.argument());
1246 if (theClipboard().isInternal())
1247 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1248 else if (theClipboard().hasGraphicsContents()
1249 && !theClipboard().hasTextContents())
1250 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1252 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1253 } else if (isStrUnsignedInt(arg)) {
1254 // we have a numerical argument
1255 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1256 convert<unsigned int>(arg));
1258 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1260 type = Clipboard::PdfGraphicsType;
1261 else if (arg == "png")
1262 type = Clipboard::PngGraphicsType;
1263 else if (arg == "jpeg")
1264 type = Clipboard::JpegGraphicsType;
1265 else if (arg == "linkback")
1266 type = Clipboard::LinkBackGraphicsType;
1267 else if (arg == "emf")
1268 type = Clipboard::EmfGraphicsType;
1269 else if (arg == "wmf")
1270 type = Clipboard::WmfGraphicsType;
1273 LASSERT(false, /**/);
1275 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1278 bv->buffer().errors("Paste");
1279 cur.clearSelection(); // bug 393
1285 cutSelection(cur, true, true);
1286 cur.message(_("Cut"));
1291 cur.message(_("Copy"));
1294 case LFUN_SERVER_GET_XY:
1295 cur.message(from_utf8(
1296 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1297 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1300 case LFUN_SERVER_SET_XY: {
1303 istringstream is(to_utf8(cmd.argument()));
1306 lyxerr << "SETXY: Could not parse coordinates in '"
1307 << to_utf8(cmd.argument()) << endl;
1309 tm->setCursorFromCoordinates(cur, x, y);
1313 case LFUN_SERVER_GET_LAYOUT:
1314 cur.message(cur.paragraph().layout().name());
1318 docstring layout = cmd.argument();
1319 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1321 Paragraph const & para = cur.paragraph();
1322 docstring const old_layout = para.layout().name();
1323 DocumentClass const & tclass = bv->buffer().params().documentClass();
1326 layout = tclass.defaultLayoutName();
1328 if (owner_->forcePlainLayout())
1329 // in this case only the empty layout is allowed
1330 layout = tclass.plainLayoutName();
1331 else if (para.usePlainLayout()) {
1332 // in this case, default layout maps to empty layout
1333 if (layout == tclass.defaultLayoutName())
1334 layout = tclass.plainLayoutName();
1336 // otherwise, the empty layout maps to the default
1337 if (layout == tclass.plainLayoutName())
1338 layout = tclass.defaultLayoutName();
1341 bool hasLayout = tclass.hasLayout(layout);
1343 // If the entry is obsolete, use the new one instead.
1345 docstring const & obs = tclass[layout].obsoleted_by();
1351 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1352 from_utf8(N_(" not known")));
1356 bool change_layout = (old_layout != layout);
1358 if (!change_layout && cur.selection() &&
1359 cur.selBegin().pit() != cur.selEnd().pit())
1361 pit_type spit = cur.selBegin().pit();
1362 pit_type epit = cur.selEnd().pit() + 1;
1363 while (spit != epit) {
1364 if (pars_[spit].layout().name() != old_layout) {
1365 change_layout = true;
1373 setLayout(cur, layout);
1378 case LFUN_CLIPBOARD_PASTE:
1379 cap::replaceSelection(cur);
1380 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1381 cmd.argument() == "paragraph");
1382 bv->buffer().errors("Paste");
1385 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1386 cap::replaceSelection(cur);
1387 pasteSimpleText(cur, cmd.argument() == "paragraph");
1390 case LFUN_PRIMARY_SELECTION_PASTE:
1391 cap::replaceSelection(cur);
1392 pasteString(cur, theSelection().get(),
1393 cmd.argument() == "paragraph");
1396 case LFUN_SELECTION_PASTE:
1397 // Copy the selection buffer to the clipboard stack,
1398 // because we want it to appear in the "Edit->Paste
1400 cap::replaceSelection(cur);
1401 cap::copySelectionToStack();
1402 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1403 bv->buffer().errors("Paste");
1406 case LFUN_UNICODE_INSERT: {
1407 if (cmd.argument().empty())
1409 docstring hexstring = cmd.argument();
1410 if (isHex(hexstring)) {
1411 char_type c = hexToInt(hexstring);
1412 if (c >= 32 && c < 0x10ffff) {
1413 lyxerr << "Inserting c: " << c << endl;
1414 docstring s = docstring(1, c);
1415 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1421 case LFUN_QUOTE_INSERT: {
1422 // this avoids a double undo
1423 // FIXME: should not be needed, ideally
1424 if (!cur.selection())
1426 cap::replaceSelection(cur);
1428 Paragraph const & par = cur.paragraph();
1429 pos_type pos = cur.pos();
1431 BufferParams const & bufparams = bv->buffer().params();
1433 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1434 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1436 if (allow_inset_quote) {
1438 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1439 c = par.getChar(pos - 1);
1440 string const arg = to_utf8(cmd.argument());
1441 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1442 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1443 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1446 // The cursor might have been invalidated by the replaceSelection.
1447 cur.buffer()->changed(true);
1448 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1453 case LFUN_DATE_INSERT: {
1454 string const format = cmd.argument().empty()
1455 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1456 string const time = formatted_time(current_time(), format);
1457 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1461 case LFUN_MOUSE_TRIPLE:
1462 if (cmd.button() == mouse_button::button1) {
1463 tm->cursorHome(cur);
1471 case LFUN_MOUSE_DOUBLE:
1472 if (cmd.button() == mouse_button::button1) {
1473 selectWord(cur, WHOLE_WORD_STRICT);
1478 // Single-click on work area
1479 case LFUN_MOUSE_PRESS: {
1480 // We are not marking a selection with the keyboard in any case.
1481 Cursor & bvcur = cur.bv().cursor();
1482 bvcur.setMark(false);
1483 switch (cmd.button()) {
1484 case mouse_button::button1:
1486 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1487 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1488 if (bvcur.wordSelection())
1489 selectWord(bvcur, WHOLE_WORD);
1492 case mouse_button::button2:
1493 // Middle mouse pasting.
1494 bv->mouseSetCursor(cur);
1496 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1497 "selection-paste ; primary-selection-paste paragraph"));
1498 cur.noScreenUpdate();
1501 case mouse_button::button3: {
1502 // Don't do anything if we right-click a
1503 // selection, a context menu will popup.
1504 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1505 && cur < bvcur.selectionEnd()) {
1506 cur.noScreenUpdate();
1509 if (!bv->mouseSetCursor(cur, false))
1510 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1516 } // switch (cmd.button())
1519 case LFUN_MOUSE_MOTION: {
1520 // Mouse motion with right or middle mouse do nothing for now.
1521 if (cmd.button() != mouse_button::button1) {
1522 cur.noScreenUpdate();
1525 // ignore motions deeper nested than the real anchor
1526 Cursor & bvcur = cur.bv().cursor();
1527 if (!bvcur.realAnchor().hasPart(cur)) {
1531 CursorSlice old = bvcur.top();
1533 int const wh = bv->workHeight();
1534 int const y = max(0, min(wh - 1, cmd.y()));
1536 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1537 cur.setTargetX(cmd.x());
1539 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1540 else if (cmd.y() < 0)
1541 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1542 // This is to allow jumping over large insets
1543 if (cur.top() == old) {
1545 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1546 else if (cmd.y() < 0)
1547 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1549 // We continue with our existing selection or start a new one, so don't
1550 // reset the anchor.
1551 bvcur.setCursor(cur);
1552 bvcur.setSelection(true);
1553 if (cur.top() == old) {
1554 // We didn't move one iota, so no need to update the screen.
1555 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1556 //cur.noScreenUpdate();
1562 case LFUN_MOUSE_RELEASE:
1563 switch (cmd.button()) {
1564 case mouse_button::button1:
1565 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1566 // If there is a new selection, update persistent selection;
1567 // otherwise, single click does not clear persistent selection
1569 if (cur.selection()) {
1570 // Finish selection. If double click,
1571 // cur is moved to the end of word by
1572 // selectWord but bvcur is current
1574 cur.bv().cursor().setSelection();
1575 // We might have removed an empty but drawn selection
1576 // (probably a margin)
1577 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1579 cur.noScreenUpdate();
1580 // FIXME: We could try to handle drag and drop of selection here.
1583 case mouse_button::button2:
1584 // Middle mouse pasting is handled at mouse press time,
1585 // see LFUN_MOUSE_PRESS.
1586 cur.noScreenUpdate();
1589 case mouse_button::button3:
1590 // Cursor was set at LFUN_MOUSE_PRESS time.
1591 // FIXME: If there is a selection we could try to handle a special
1592 // drag & drop context menu.
1593 cur.noScreenUpdate();
1596 case mouse_button::none:
1597 case mouse_button::button4:
1598 case mouse_button::button5:
1600 } // switch (cmd.button())
1604 case LFUN_SELF_INSERT: {
1605 if (cmd.argument().empty())
1608 // Automatically delete the currently selected
1609 // text and replace it with what is being
1610 // typed in now. Depends on lyxrc settings
1611 // "auto_region_delete", which defaults to
1614 if (lyxrc.auto_region_delete && cur.selection())
1615 cutSelection(cur, false, false);
1617 cur.clearSelection();
1619 docstring::const_iterator cit = cmd.argument().begin();
1620 docstring::const_iterator const end = cmd.argument().end();
1621 for (; cit != end; ++cit)
1622 bv->translateAndInsert(*cit, this, cur);
1625 moveCursor(cur, false);
1626 cur.markNewWordPosition();
1627 bv->bookmarkEditPosition();
1631 case LFUN_HYPERLINK_INSERT: {
1632 InsetCommandParams p(HYPERLINK_CODE);
1634 if (cur.selection()) {
1635 content = cur.selectionAsString(false);
1636 cutSelection(cur, true, false);
1638 p["target"] = (cmd.argument().empty()) ?
1639 content : cmd.argument();
1640 string const data = InsetCommand::params2string(p);
1641 if (p["target"].empty()) {
1642 bv->showDialog("href", data);
1644 FuncRequest fr(LFUN_INSET_INSERT, data);
1650 case LFUN_LABEL_INSERT: {
1651 InsetCommandParams p(LABEL_CODE);
1652 // Try to generate a valid label
1653 p["name"] = (cmd.argument().empty()) ?
1654 cur.getPossibleLabel() :
1656 string const data = InsetCommand::params2string(p);
1658 if (cmd.argument().empty()) {
1659 bv->showDialog("label", data);
1661 FuncRequest fr(LFUN_INSET_INSERT, data);
1667 case LFUN_INFO_INSERT: {
1669 if (cmd.argument().empty() && cur.selection()) {
1670 // if command argument is empty use current selection as parameter.
1671 docstring ds = cur.selectionAsString(false);
1672 cutSelection(cur, true, false);
1673 FuncRequest cmd0(cmd, ds);
1674 inset = createInset(cur.buffer(), cmd0);
1676 inset = createInset(cur.buffer(), cmd);
1681 insertInset(cur, inset);
1685 case LFUN_CAPTION_INSERT:
1686 case LFUN_FOOTNOTE_INSERT:
1687 case LFUN_NOTE_INSERT:
1688 case LFUN_FLEX_INSERT:
1689 case LFUN_BOX_INSERT:
1690 case LFUN_BRANCH_INSERT:
1691 case LFUN_PHANTOM_INSERT:
1692 case LFUN_ERT_INSERT:
1693 case LFUN_LISTING_INSERT:
1694 case LFUN_MARGINALNOTE_INSERT:
1695 case LFUN_ARGUMENT_INSERT:
1696 case LFUN_INDEX_INSERT:
1697 case LFUN_PREVIEW_INSERT:
1698 case LFUN_SCRIPT_INSERT:
1699 case LFUN_IPA_INSERT:
1700 // Open the inset, and move the current selection
1702 doInsertInset(cur, this, cmd, true, true);
1704 // Some insets are numbered, others are shown in the outline pane so
1705 // let's update the labels and the toc backend.
1706 cur.forceBufferUpdate();
1709 case LFUN_TABULAR_INSERT:
1710 // if there were no arguments, just open the dialog
1711 if (doInsertInset(cur, this, cmd, false, true))
1714 bv->showDialog("tabularcreate");
1718 case LFUN_FLOAT_INSERT:
1719 case LFUN_FLOAT_WIDE_INSERT:
1720 case LFUN_WRAP_INSERT: {
1721 // will some content be moved into the inset?
1722 bool const content = cur.selection();
1723 // does the content consist of multiple paragraphs?
1724 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1726 doInsertInset(cur, this, cmd, true, true);
1729 // If some single-par content is moved into the inset,
1730 // doInsertInset puts the cursor outside the inset.
1731 // To insert the caption we put it back into the inset.
1732 // FIXME cleanup doInsertInset to avoid such dances!
1733 if (content && singlepar)
1736 ParagraphList & pars = cur.text()->paragraphs();
1738 DocumentClass const & tclass = bv->buffer().params().documentClass();
1740 // add a separate paragraph for the caption inset
1741 pars.push_back(Paragraph());
1742 pars.back().setInsetOwner(&cur.text()->inset());
1743 pars.back().setPlainOrDefaultLayout(tclass);
1744 int cap_pit = pars.size() - 1;
1746 // if an empty inset was created, we create an additional empty
1747 // paragraph at the bottom so that the user can choose where to put
1748 // the graphics (or table).
1750 pars.push_back(Paragraph());
1751 pars.back().setInsetOwner(&cur.text()->inset());
1752 pars.back().setPlainOrDefaultLayout(tclass);
1755 // reposition the cursor to the caption
1756 cur.pit() = cap_pit;
1758 // FIXME: This Text/Cursor dispatch handling is a mess!
1759 // We cannot use Cursor::dispatch here it needs access to up to
1761 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1762 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1763 cur.forceBufferUpdate();
1764 cur.screenUpdateFlags(Update::Force);
1765 // FIXME: When leaving the Float (or Wrap) inset we should
1766 // delete any empty paragraph left above or below the
1771 case LFUN_NOMENCL_INSERT: {
1772 InsetCommandParams p(NOMENCL_CODE);
1773 if (cmd.argument().empty())
1774 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1776 p["symbol"] = cmd.argument();
1777 string const data = InsetCommand::params2string(p);
1778 bv->showDialog("nomenclature", data);
1782 case LFUN_INDEX_PRINT: {
1783 InsetCommandParams p(INDEX_PRINT_CODE);
1784 if (cmd.argument().empty())
1785 p["type"] = from_ascii("idx");
1787 p["type"] = cmd.argument();
1788 string const data = InsetCommand::params2string(p);
1789 FuncRequest fr(LFUN_INSET_INSERT, data);
1794 case LFUN_NOMENCL_PRINT:
1795 case LFUN_NEWPAGE_INSERT:
1797 doInsertInset(cur, this, cmd, false, false);
1801 case LFUN_DEPTH_DECREMENT:
1802 changeDepth(cur, DEC_DEPTH);
1805 case LFUN_DEPTH_INCREMENT:
1806 changeDepth(cur, INC_DEPTH);
1809 case LFUN_MATH_DISPLAY:
1810 mathDispatch(cur, cmd, true);
1813 case LFUN_REGEXP_MODE:
1814 regexpDispatch(cur, cmd);
1817 case LFUN_MATH_MODE:
1818 if (cmd.argument() == "on")
1819 // don't pass "on" as argument
1820 // (it would appear literally in the first cell)
1821 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1823 mathDispatch(cur, cmd, false);
1826 case LFUN_MATH_MACRO:
1827 if (cmd.argument().empty())
1828 cur.errorMessage(from_utf8(N_("Missing argument")));
1831 string s = to_utf8(cmd.argument());
1832 string const s1 = token(s, ' ', 1);
1833 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1834 string const s2 = token(s, ' ', 2);
1835 MacroType type = MacroTypeNewcommand;
1837 type = MacroTypeDef;
1838 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1839 from_utf8(token(s, ' ', 0)), nargs, false, type);
1840 inset->setBuffer(bv->buffer());
1841 insertInset(cur, inset);
1843 // enter macro inset and select the name
1845 cur.top().pos() = cur.top().lastpos();
1847 cur.setSelection(true);
1848 cur.top().pos() = 0;
1852 // passthrough hat and underscore outside mathed:
1853 case LFUN_MATH_SUBSCRIPT:
1854 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1856 case LFUN_MATH_SUPERSCRIPT:
1857 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1860 case LFUN_MATH_INSERT:
1861 case LFUN_MATH_AMS_MATRIX:
1862 case LFUN_MATH_MATRIX:
1863 case LFUN_MATH_DELIM:
1864 case LFUN_MATH_BIGDELIM: {
1866 cap::replaceSelection(cur);
1867 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1868 checkAndActivateInset(cur, true);
1869 LASSERT(cur.inMathed(), /**/);
1874 case LFUN_FONT_EMPH: {
1875 Font font(ignore_font, ignore_language);
1876 font.fontInfo().setEmph(FONT_TOGGLE);
1877 toggleAndShow(cur, this, font);
1881 case LFUN_FONT_ITAL: {
1882 Font font(ignore_font, ignore_language);
1883 font.fontInfo().setShape(ITALIC_SHAPE);
1884 toggleAndShow(cur, this, font);
1888 case LFUN_FONT_BOLD:
1889 case LFUN_FONT_BOLDSYMBOL: {
1890 Font font(ignore_font, ignore_language);
1891 font.fontInfo().setSeries(BOLD_SERIES);
1892 toggleAndShow(cur, this, font);
1896 case LFUN_FONT_NOUN: {
1897 Font font(ignore_font, ignore_language);
1898 font.fontInfo().setNoun(FONT_TOGGLE);
1899 toggleAndShow(cur, this, font);
1903 case LFUN_FONT_TYPEWRITER: {
1904 Font font(ignore_font, ignore_language);
1905 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1906 toggleAndShow(cur, this, font);
1910 case LFUN_FONT_SANS: {
1911 Font font(ignore_font, ignore_language);
1912 font.fontInfo().setFamily(SANS_FAMILY);
1913 toggleAndShow(cur, this, font);
1917 case LFUN_FONT_ROMAN: {
1918 Font font(ignore_font, ignore_language);
1919 font.fontInfo().setFamily(ROMAN_FAMILY);
1920 toggleAndShow(cur, this, font);
1924 case LFUN_FONT_DEFAULT: {
1925 Font font(inherit_font, ignore_language);
1926 toggleAndShow(cur, this, font);
1930 case LFUN_FONT_STRIKEOUT: {
1931 Font font(ignore_font, ignore_language);
1932 font.fontInfo().setStrikeout(FONT_TOGGLE);
1933 toggleAndShow(cur, this, font);
1937 case LFUN_FONT_UULINE: {
1938 Font font(ignore_font, ignore_language);
1939 font.fontInfo().setUuline(FONT_TOGGLE);
1940 toggleAndShow(cur, this, font);
1944 case LFUN_FONT_UWAVE: {
1945 Font font(ignore_font, ignore_language);
1946 font.fontInfo().setUwave(FONT_TOGGLE);
1947 toggleAndShow(cur, this, font);
1951 case LFUN_FONT_UNDERLINE: {
1952 Font font(ignore_font, ignore_language);
1953 font.fontInfo().setUnderbar(FONT_TOGGLE);
1954 toggleAndShow(cur, this, font);
1958 case LFUN_FONT_SIZE: {
1959 Font font(ignore_font, ignore_language);
1960 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1961 toggleAndShow(cur, this, font);
1965 case LFUN_LANGUAGE: {
1966 string const lang_arg = cmd.getArg(0);
1967 bool const reset = (lang_arg.empty() || lang_arg == "reset");
1968 Language const * lang =
1969 reset ? reset_language
1970 : languages.getLanguage(lang_arg);
1971 // we allow reset_language, which is 0, but only if it
1972 // was requested via empty or "reset" arg.
1973 if (!lang && !reset)
1975 bool const toggle = (cmd.getArg(1) != "set");
1976 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
1977 Font font(ignore_font, lang);
1978 toggleAndShow(cur, this, font, toggle);
1982 case LFUN_TEXTSTYLE_APPLY:
1983 toggleAndShow(cur, this, freefont, toggleall);
1984 cur.message(_("Character set"));
1987 // Set the freefont using the contents of \param data dispatched from
1988 // the frontends and apply it at the current cursor location.
1989 case LFUN_TEXTSTYLE_UPDATE: {
1992 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1995 toggleAndShow(cur, this, freefont, toggleall);
1996 cur.message(_("Character set"));
1998 lyxerr << "Argument not ok";
2003 case LFUN_FINISHED_LEFT:
2004 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2005 // We're leaving an inset, going left. If the inset is LTR, we're
2006 // leaving from the front, so we should not move (remain at --- but
2007 // not in --- the inset). If the inset is RTL, move left, without
2008 // entering the inset itself; i.e., move to after the inset.
2009 if (cur.paragraph().getFontSettings(
2010 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2011 cursorVisLeft(cur, true);
2014 case LFUN_FINISHED_RIGHT:
2015 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2016 // We're leaving an inset, going right. If the inset is RTL, we're
2017 // leaving from the front, so we should not move (remain at --- but
2018 // not in --- the inset). If the inset is LTR, move right, without
2019 // entering the inset itself; i.e., move to after the inset.
2020 if (!cur.paragraph().getFontSettings(
2021 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2022 cursorVisRight(cur, true);
2025 case LFUN_FINISHED_BACKWARD:
2026 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2029 case LFUN_FINISHED_FORWARD:
2030 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2032 cur.setCurrentFont();
2035 case LFUN_LAYOUT_PARAGRAPH: {
2037 params2string(cur.paragraph(), data);
2038 data = "show\n" + data;
2039 bv->showDialog("paragraph", data);
2043 case LFUN_PARAGRAPH_UPDATE: {
2045 params2string(cur.paragraph(), data);
2047 // Will the paragraph accept changes from the dialog?
2049 cur.inset().allowParagraphCustomization(cur.idx());
2051 data = "update " + convert<string>(accept) + '\n' + data;
2052 bv->updateDialog("paragraph", data);
2056 case LFUN_ACCENT_UMLAUT:
2057 case LFUN_ACCENT_CIRCUMFLEX:
2058 case LFUN_ACCENT_GRAVE:
2059 case LFUN_ACCENT_ACUTE:
2060 case LFUN_ACCENT_TILDE:
2061 case LFUN_ACCENT_CEDILLA:
2062 case LFUN_ACCENT_MACRON:
2063 case LFUN_ACCENT_DOT:
2064 case LFUN_ACCENT_UNDERDOT:
2065 case LFUN_ACCENT_UNDERBAR:
2066 case LFUN_ACCENT_CARON:
2067 case LFUN_ACCENT_BREVE:
2068 case LFUN_ACCENT_TIE:
2069 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2070 case LFUN_ACCENT_CIRCLE:
2071 case LFUN_ACCENT_OGONEK:
2072 theApp()->handleKeyFunc(cmd.action());
2073 if (!cmd.argument().empty())
2074 // FIXME: Are all these characters encoded in one byte in utf8?
2075 bv->translateAndInsert(cmd.argument()[0], this, cur);
2076 cur.screenUpdateFlags(Update::FitCursor);
2079 case LFUN_FLOAT_LIST_INSERT: {
2080 DocumentClass const & tclass = bv->buffer().params().documentClass();
2081 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2083 if (cur.selection())
2084 cutSelection(cur, true, false);
2085 breakParagraph(cur);
2087 if (cur.lastpos() != 0) {
2088 cursorBackward(cur);
2089 breakParagraph(cur);
2092 docstring const laystr = cur.inset().usePlainLayout() ?
2093 tclass.plainLayoutName() :
2094 tclass.defaultLayoutName();
2095 setLayout(cur, laystr);
2096 ParagraphParameters p;
2097 // FIXME If this call were replaced with one to clearParagraphParams(),
2098 // then we could get rid of this method altogether.
2099 setParagraphs(cur, p);
2100 // FIXME This should be simplified when InsetFloatList takes a
2101 // Buffer in its constructor.
2102 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2103 ifl->setBuffer(bv->buffer());
2104 insertInset(cur, ifl);
2107 lyxerr << "Non-existent float type: "
2108 << to_utf8(cmd.argument()) << endl;
2113 case LFUN_CHANGE_ACCEPT: {
2114 acceptOrRejectChanges(cur, ACCEPT);
2118 case LFUN_CHANGE_REJECT: {
2119 acceptOrRejectChanges(cur, REJECT);
2123 case LFUN_THESAURUS_ENTRY: {
2124 docstring arg = cmd.argument();
2126 arg = cur.selectionAsString(false);
2128 if (arg.size() > 100 || arg.empty()) {
2129 // Get word or selection
2130 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2131 arg = cur.selectionAsString(false);
2132 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2135 bv->showDialog("thesaurus", to_utf8(arg));
2139 case LFUN_SPELLING_ADD: {
2140 docstring word = from_utf8(cmd.getArg(0));
2143 word = cur.selectionAsString(false);
2145 if (word.size() > 100 || word.empty()) {
2146 // Get word or selection
2147 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2148 word = cur.selectionAsString(false);
2150 lang = const_cast<Language *>(cur.getFont().language());
2152 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2153 WordLangTuple wl(word, lang);
2154 theSpellChecker()->insert(wl);
2158 case LFUN_SPELLING_IGNORE: {
2159 docstring word = from_utf8(cmd.getArg(0));
2162 word = cur.selectionAsString(false);
2164 if (word.size() > 100 || word.empty()) {
2165 // Get word or selection
2166 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2167 word = cur.selectionAsString(false);
2169 lang = const_cast<Language *>(cur.getFont().language());
2171 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2172 WordLangTuple wl(word, lang);
2173 theSpellChecker()->accept(wl);
2177 case LFUN_SPELLING_REMOVE: {
2178 docstring word = from_utf8(cmd.getArg(0));
2181 word = cur.selectionAsString(false);
2183 if (word.size() > 100 || word.empty()) {
2184 // Get word or selection
2185 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2186 word = cur.selectionAsString(false);
2188 lang = const_cast<Language *>(cur.getFont().language());
2190 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2191 WordLangTuple wl(word, lang);
2192 theSpellChecker()->remove(wl);
2196 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2197 // Given data, an encoding of the ParagraphParameters
2198 // generated in the Paragraph dialog, this function sets
2199 // the current paragraph, or currently selected paragraphs,
2201 // NOTE: This function overrides all existing settings.
2202 setParagraphs(cur, cmd.argument());
2203 cur.message(_("Paragraph layout set"));
2207 case LFUN_PARAGRAPH_PARAMS: {
2208 // Given data, an encoding of the ParagraphParameters as we'd
2209 // find them in a LyX file, this function modifies the current paragraph,
2210 // or currently selected paragraphs.
2211 // NOTE: This function only modifies, and does not override, existing
2213 setParagraphs(cur, cmd.argument(), true);
2214 cur.message(_("Paragraph layout set"));
2219 if (cur.selection()) {
2220 cur.setSelection(false);
2223 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2224 // correct, but I'm not 100% sure -- dov, 071019
2225 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2229 case LFUN_OUTLINE_UP:
2230 outline(OutlineUp, cur);
2231 setCursor(cur, cur.pit(), 0);
2232 cur.forceBufferUpdate();
2236 case LFUN_OUTLINE_DOWN:
2237 outline(OutlineDown, cur);
2238 setCursor(cur, cur.pit(), 0);
2239 cur.forceBufferUpdate();
2243 case LFUN_OUTLINE_IN:
2244 outline(OutlineIn, cur);
2245 cur.forceBufferUpdate();
2249 case LFUN_OUTLINE_OUT:
2250 outline(OutlineOut, cur);
2251 cur.forceBufferUpdate();
2256 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2261 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2263 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2264 // Check for misspelled text
2265 // The redraw is useful because of the painting of
2266 // misspelled markers depends on the cursor position.
2267 // Trigger a redraw for cursor moves inside misspelled text.
2268 if (!cur.inTexted()) {
2269 // move from regular text to math
2270 needsUpdate = last_misspelled;
2271 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2272 // move inside regular text
2273 needsUpdate = last_misspelled
2274 || cur.paragraph().isMisspelled(cur.pos(), true);
2278 // FIXME: The cursor flag is reset two lines below
2279 // so we need to check here if some of the LFUN did touch that.
2280 // for now only Text::erase() and Text::backspace() do that.
2281 // The plan is to verify all the LFUNs and then to remove this
2282 // singleParUpdate boolean altogether.
2283 if (cur.result().screenUpdate() & Update::Force) {
2284 singleParUpdate = false;
2288 // FIXME: the following code should go in favor of fine grained
2289 // update flag treatment.
2290 if (singleParUpdate) {
2291 // Inserting characters does not change par height in general. So, try
2292 // to update _only_ this paragraph. BufferView will detect if a full
2293 // metrics update is needed anyway.
2294 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2298 && &oldTopSlice.inset() == &cur.inset()
2299 && oldTopSlice.idx() == cur.idx()
2300 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2301 && !cur.selection())
2302 // FIXME: it would be better if we could just do this
2304 //if (cur.result().update() != Update::FitCursor)
2305 // cur.noScreenUpdate();
2307 // But some LFUNs do not set Update::FitCursor when needed, so we
2308 // do it for all. This is not very harmfull as FitCursor will provoke
2309 // a full redraw only if needed but still, a proper review of all LFUN
2310 // should be done and this needsUpdate boolean can then be removed.
2311 cur.screenUpdateFlags(Update::FitCursor);
2313 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2317 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2318 FuncStatus & flag) const
2320 LASSERT(cur.text() == this, /**/);
2322 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2324 bool allow_in_passthru = false;
2325 InsetCode code = NO_CODE;
2327 switch (cmd.action()) {
2329 case LFUN_DEPTH_DECREMENT:
2330 enable = changeDepthAllowed(cur, DEC_DEPTH);
2333 case LFUN_DEPTH_INCREMENT:
2334 enable = changeDepthAllowed(cur, INC_DEPTH);
2338 // FIXME We really should not allow this to be put, e.g.,
2339 // in a footnote, or in ERT. But it would make sense in a
2340 // branch, so I'm not sure what to do.
2341 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2344 case LFUN_DIALOG_SHOW_NEW_INSET:
2345 if (cmd.argument() == "bibitem")
2346 code = BIBITEM_CODE;
2347 else if (cmd.argument() == "bibtex") {
2349 // not allowed in description items
2350 enable = !inDescriptionItem(cur);
2352 else if (cmd.argument() == "box")
2354 else if (cmd.argument() == "branch")
2356 else if (cmd.argument() == "citation")
2358 else if (cmd.argument() == "ert")
2360 else if (cmd.argument() == "external")
2361 code = EXTERNAL_CODE;
2362 else if (cmd.argument() == "float")
2364 else if (cmd.argument() == "graphics")
2365 code = GRAPHICS_CODE;
2366 else if (cmd.argument() == "href")
2367 code = HYPERLINK_CODE;
2368 else if (cmd.argument() == "include")
2369 code = INCLUDE_CODE;
2370 else if (cmd.argument() == "index")
2372 else if (cmd.argument() == "index_print")
2373 code = INDEX_PRINT_CODE;
2374 else if (cmd.argument() == "nomenclature")
2375 code = NOMENCL_CODE;
2376 else if (cmd.argument() == "nomencl_print")
2377 code = NOMENCL_PRINT_CODE;
2378 else if (cmd.argument() == "label")
2380 else if (cmd.argument() == "line")
2382 else if (cmd.argument() == "note")
2384 else if (cmd.argument() == "phantom")
2385 code = PHANTOM_CODE;
2386 else if (cmd.argument() == "ref")
2388 else if (cmd.argument() == "space")
2390 else if (cmd.argument() == "toc")
2392 else if (cmd.argument() == "vspace")
2394 else if (cmd.argument() == "wrap")
2396 else if (cmd.argument() == "listings")
2397 code = LISTINGS_CODE;
2400 case LFUN_ERT_INSERT:
2403 case LFUN_LISTING_INSERT:
2404 code = LISTINGS_CODE;
2405 // not allowed in description items
2406 enable = !inDescriptionItem(cur);
2408 case LFUN_FOOTNOTE_INSERT:
2411 case LFUN_TABULAR_INSERT:
2412 code = TABULAR_CODE;
2414 case LFUN_MARGINALNOTE_INSERT:
2417 case LFUN_FLOAT_INSERT:
2418 case LFUN_FLOAT_WIDE_INSERT:
2419 // FIXME: If there is a selection, we should check whether there
2420 // are floats in the selection, but this has performance issues, see
2421 // LFUN_CHANGE_ACCEPT/REJECT.
2423 if (inDescriptionItem(cur))
2424 // not allowed in description items
2427 InsetCode const inset_code = cur.inset().lyxCode();
2429 // algorithm floats cannot be put in another float
2430 if (to_utf8(cmd.argument()) == "algorithm") {
2431 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2435 // for figures and tables: only allow in another
2436 // float or wrap if it is of the same type and
2437 // not a subfloat already
2438 if(cur.inset().lyxCode() == code) {
2439 InsetFloat const & ins =
2440 static_cast<InsetFloat const &>(cur.inset());
2441 enable = ins.params().type == to_utf8(cmd.argument())
2442 && !ins.params().subfloat;
2443 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2444 InsetWrap const & ins =
2445 static_cast<InsetWrap const &>(cur.inset());
2446 enable = ins.params().type == to_utf8(cmd.argument());
2450 case LFUN_WRAP_INSERT:
2452 // not allowed in description items
2453 enable = !inDescriptionItem(cur);
2455 case LFUN_FLOAT_LIST_INSERT: {
2456 code = FLOAT_LIST_CODE;
2457 // not allowed in description items
2458 enable = !inDescriptionItem(cur);
2460 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2461 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2462 // make sure we know about such floats
2463 if (cit == floats.end() ||
2464 // and that we know how to generate a list of them
2465 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2466 flag.setUnknown(true);
2467 // probably not necessary, but...
2473 case LFUN_CAPTION_INSERT:
2474 code = CAPTION_CODE;
2475 // not allowed in description items
2476 enable = !inDescriptionItem(cur);
2478 case LFUN_NOTE_INSERT:
2480 // in commands (sections etc.) and description items,
2481 // only Notes are allowed
2482 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2483 (!cur.paragraph().layout().isCommand()
2484 && !inDescriptionItem(cur)));
2486 case LFUN_FLEX_INSERT: {
2488 string s = cmd.getArg(0);
2490 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2491 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2492 il.lyxtype() != InsetLayout::CUSTOM &&
2493 il.lyxtype() != InsetLayout::ELEMENT &&
2494 il.lyxtype ()!= InsetLayout::STANDARD)
2498 case LFUN_BOX_INSERT:
2501 case LFUN_BRANCH_INSERT:
2503 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2504 && cur.buffer()->params().branchlist().empty())
2507 case LFUN_IPA_INSERT:
2510 case LFUN_PHANTOM_INSERT:
2511 code = PHANTOM_CODE;
2513 case LFUN_LABEL_INSERT:
2516 case LFUN_INFO_INSERT:
2519 case LFUN_ARGUMENT_INSERT: {
2521 allow_in_passthru = true;
2522 string const arg = cmd.getArg(0);
2527 Layout const & lay = cur.paragraph().layout();
2528 Layout::LaTeXArgMap args = lay.latexargs();
2529 Layout::LaTeXArgMap itemargs = lay.itemargs();
2530 if (!itemargs.empty())
2531 args.insert(itemargs.begin(), itemargs.end());
2532 Layout::LaTeXArgMap::const_iterator const lait =
2534 if (lait != args.end()) {
2536 pit_type pit = cur.pit();
2537 pit_type lastpit = cur.pit();
2538 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2539 // In a sequence of "merged" environment layouts, we only allow
2540 // non-item arguments once.
2541 lastpit = cur.lastpit();
2542 // get the first paragraph in sequence with this layout
2543 depth_type const current_depth = cur.paragraph().params().depth();
2547 Paragraph cpar = pars_[pit - 1];
2548 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2554 for (; pit <= lastpit; ++pit) {
2555 if (pars_[pit].layout() != lay)
2557 InsetList::const_iterator it = pars_[pit].insetList().begin();
2558 InsetList::const_iterator end = pars_[pit].insetList().end();
2559 for (; it != end; ++it) {
2560 if (it->inset->lyxCode() == ARG_CODE) {
2561 InsetArgument const * ins =
2562 static_cast<InsetArgument const *>(it->inset);
2563 if (ins->name() == arg) {
2564 // we have this already
2575 case LFUN_INDEX_INSERT:
2578 case LFUN_INDEX_PRINT:
2579 code = INDEX_PRINT_CODE;
2580 // not allowed in description items
2581 enable = !inDescriptionItem(cur);
2583 case LFUN_NOMENCL_INSERT:
2584 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2588 code = NOMENCL_CODE;
2590 case LFUN_NOMENCL_PRINT:
2591 code = NOMENCL_PRINT_CODE;
2592 // not allowed in description items
2593 enable = !inDescriptionItem(cur);
2595 case LFUN_HYPERLINK_INSERT:
2596 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2600 code = HYPERLINK_CODE;
2602 case LFUN_IPAMACRO_INSERT: {
2603 string const arg = cmd.getArg(0);
2605 code = IPADECO_CODE;
2607 code = IPACHAR_CODE;
2610 case LFUN_QUOTE_INSERT:
2611 // always allow this, since we will inset a raw quote
2612 // if an inset is not allowed.
2614 case LFUN_SPECIALCHAR_INSERT:
2615 code = SPECIALCHAR_CODE;
2617 case LFUN_SPACE_INSERT:
2618 // slight hack: we know this is allowed in math mode
2622 case LFUN_PREVIEW_INSERT:
2623 code = PREVIEW_CODE;
2625 case LFUN_SCRIPT_INSERT:
2629 case LFUN_MATH_INSERT:
2630 case LFUN_MATH_AMS_MATRIX:
2631 case LFUN_MATH_MATRIX:
2632 case LFUN_MATH_DELIM:
2633 case LFUN_MATH_BIGDELIM:
2634 case LFUN_MATH_DISPLAY:
2635 case LFUN_MATH_MODE:
2636 case LFUN_MATH_MACRO:
2637 case LFUN_MATH_SUBSCRIPT:
2638 case LFUN_MATH_SUPERSCRIPT:
2639 code = MATH_HULL_CODE;
2642 case LFUN_REGEXP_MODE:
2643 code = MATH_HULL_CODE;
2644 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2647 case LFUN_INSET_MODIFY:
2648 // We need to disable this, because we may get called for a
2650 // InsetTabular::getStatus() -> InsetText::getStatus()
2651 // and we don't handle LFUN_INSET_MODIFY.
2655 case LFUN_FONT_EMPH:
2656 flag.setOnOff(fontinfo.emph() == FONT_ON);
2657 enable = !cur.paragraph().isPassThru();
2660 case LFUN_FONT_ITAL:
2661 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2662 enable = !cur.paragraph().isPassThru();
2665 case LFUN_FONT_NOUN:
2666 flag.setOnOff(fontinfo.noun() == FONT_ON);
2667 enable = !cur.paragraph().isPassThru();
2670 case LFUN_FONT_BOLD:
2671 case LFUN_FONT_BOLDSYMBOL:
2672 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2673 enable = !cur.paragraph().isPassThru();
2676 case LFUN_FONT_SANS:
2677 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2678 enable = !cur.paragraph().isPassThru();
2681 case LFUN_FONT_ROMAN:
2682 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2683 enable = !cur.paragraph().isPassThru();
2686 case LFUN_FONT_TYPEWRITER:
2687 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2688 enable = !cur.paragraph().isPassThru();
2693 enable = cur.selection();
2697 if (cmd.argument().empty()) {
2698 if (theClipboard().isInternal())
2699 enable = cap::numberOfSelections() > 0;
2701 enable = !theClipboard().empty();
2705 // we have an argument
2706 string const arg = to_utf8(cmd.argument());
2707 if (isStrUnsignedInt(arg)) {
2708 // it's a number and therefore means the internal stack
2709 unsigned int n = convert<unsigned int>(arg);
2710 enable = cap::numberOfSelections() > n;
2714 // explicit graphics type?
2715 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2716 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2717 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2718 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2719 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2720 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2721 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2722 enable = theClipboard().hasGraphicsContents(type);
2731 case LFUN_CLIPBOARD_PASTE:
2732 case LFUN_CLIPBOARD_PASTE_SIMPLE:
2733 enable = !theClipboard().empty();
2736 case LFUN_PRIMARY_SELECTION_PASTE:
2737 enable = cur.selection() || !theSelection().empty();
2740 case LFUN_SELECTION_PASTE:
2741 enable = cap::selection();
2744 case LFUN_PARAGRAPH_MOVE_UP:
2745 enable = cur.pit() > 0 && !cur.selection();
2748 case LFUN_PARAGRAPH_MOVE_DOWN:
2749 enable = cur.pit() < cur.lastpit() && !cur.selection();
2752 case LFUN_CHANGE_ACCEPT:
2753 case LFUN_CHANGE_REJECT:
2754 // In principle, these LFUNs should only be enabled if there
2755 // is a change at the current position/in the current selection.
2756 // However, without proper optimizations, this will inevitably
2757 // result in unacceptable performance - just imagine a user who
2758 // wants to select the complete content of a long document.
2759 if (!cur.selection())
2760 enable = cur.paragraph().isChanged(cur.pos());
2762 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2767 case LFUN_OUTLINE_UP:
2768 case LFUN_OUTLINE_DOWN:
2769 case LFUN_OUTLINE_IN:
2770 case LFUN_OUTLINE_OUT:
2771 // FIXME: LyX is not ready for outlining within inset.
2772 enable = isMainText()
2773 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
2776 case LFUN_NEWLINE_INSERT:
2777 // LaTeX restrictions (labels or empty par)
2778 enable = (cur.pos() > cur.paragraph().beginOfBody());
2781 case LFUN_TAB_INSERT:
2782 case LFUN_TAB_DELETE:
2783 enable = cur.paragraph().isPassThru();
2786 case LFUN_SET_GRAPHICS_GROUP: {
2787 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2791 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2795 case LFUN_NEWPAGE_INSERT:
2796 // not allowed in description items
2797 code = NEWPAGE_CODE;
2798 enable = !inDescriptionItem(cur);
2801 case LFUN_DATE_INSERT: {
2802 string const format = cmd.argument().empty()
2803 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2804 enable = support::os::is_valid_strftime(format);
2809 enable = !cur.paragraph().isPassThru();
2810 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
2813 case LFUN_PARAGRAPH_BREAK:
2814 enable = cur.inset().getLayout().isMultiPar();
2817 case LFUN_SPELLING_ADD:
2818 case LFUN_SPELLING_IGNORE:
2819 case LFUN_SPELLING_REMOVE:
2820 enable = theSpellChecker();
2824 enable = !cur.inset().forcePlainLayout();
2827 case LFUN_LAYOUT_PARAGRAPH:
2828 case LFUN_PARAGRAPH_PARAMS:
2829 case LFUN_PARAGRAPH_PARAMS_APPLY:
2830 case LFUN_PARAGRAPH_UPDATE:
2831 enable = cur.inset().allowParagraphCustomization();
2834 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2835 case LFUN_ACCENT_ACUTE:
2836 case LFUN_ACCENT_BREVE:
2837 case LFUN_ACCENT_CARON:
2838 case LFUN_ACCENT_CEDILLA:
2839 case LFUN_ACCENT_CIRCLE:
2840 case LFUN_ACCENT_CIRCUMFLEX:
2841 case LFUN_ACCENT_DOT:
2842 case LFUN_ACCENT_GRAVE:
2843 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2844 case LFUN_ACCENT_MACRON:
2845 case LFUN_ACCENT_OGONEK:
2846 case LFUN_ACCENT_TIE:
2847 case LFUN_ACCENT_TILDE:
2848 case LFUN_ACCENT_UMLAUT:
2849 case LFUN_ACCENT_UNDERBAR:
2850 case LFUN_ACCENT_UNDERDOT:
2851 case LFUN_FONT_DEFAULT:
2852 case LFUN_FONT_FRAK:
2853 case LFUN_FONT_SIZE:
2854 case LFUN_FONT_STATE:
2855 case LFUN_FONT_UNDERLINE:
2856 case LFUN_FONT_STRIKEOUT:
2857 case LFUN_FONT_UULINE:
2858 case LFUN_FONT_UWAVE:
2859 case LFUN_TEXTSTYLE_APPLY:
2860 case LFUN_TEXTSTYLE_UPDATE:
2861 enable = !cur.paragraph().isPassThru();
2864 case LFUN_WORD_DELETE_FORWARD:
2865 case LFUN_WORD_DELETE_BACKWARD:
2866 case LFUN_LINE_DELETE:
2867 case LFUN_WORD_FORWARD:
2868 case LFUN_WORD_BACKWARD:
2869 case LFUN_WORD_RIGHT:
2870 case LFUN_WORD_LEFT:
2871 case LFUN_CHAR_FORWARD:
2872 case LFUN_CHAR_FORWARD_SELECT:
2873 case LFUN_CHAR_BACKWARD:
2874 case LFUN_CHAR_BACKWARD_SELECT:
2875 case LFUN_CHAR_LEFT:
2876 case LFUN_CHAR_LEFT_SELECT:
2877 case LFUN_CHAR_RIGHT:
2878 case LFUN_CHAR_RIGHT_SELECT:
2880 case LFUN_UP_SELECT:
2882 case LFUN_DOWN_SELECT:
2883 case LFUN_PARAGRAPH_UP_SELECT:
2884 case LFUN_PARAGRAPH_DOWN_SELECT:
2885 case LFUN_LINE_BEGIN_SELECT:
2886 case LFUN_LINE_END_SELECT:
2887 case LFUN_WORD_FORWARD_SELECT:
2888 case LFUN_WORD_BACKWARD_SELECT:
2889 case LFUN_WORD_RIGHT_SELECT:
2890 case LFUN_WORD_LEFT_SELECT:
2891 case LFUN_WORD_SELECT:
2892 case LFUN_SECTION_SELECT:
2893 case LFUN_BUFFER_BEGIN:
2894 case LFUN_BUFFER_END:
2895 case LFUN_BUFFER_BEGIN_SELECT:
2896 case LFUN_BUFFER_END_SELECT:
2897 case LFUN_INSET_BEGIN:
2898 case LFUN_INSET_END:
2899 case LFUN_INSET_BEGIN_SELECT:
2900 case LFUN_INSET_END_SELECT:
2901 case LFUN_INSET_SELECT_ALL:
2902 case LFUN_PARAGRAPH_UP:
2903 case LFUN_PARAGRAPH_DOWN:
2904 case LFUN_LINE_BEGIN:
2906 case LFUN_CHAR_DELETE_FORWARD:
2907 case LFUN_CHAR_DELETE_BACKWARD:
2908 case LFUN_WORD_UPCASE:
2909 case LFUN_WORD_LOWCASE:
2910 case LFUN_WORD_CAPITALIZE:
2911 case LFUN_CHARS_TRANSPOSE:
2912 case LFUN_SERVER_GET_XY:
2913 case LFUN_SERVER_SET_XY:
2914 case LFUN_SERVER_GET_LAYOUT:
2915 case LFUN_SELF_INSERT:
2916 case LFUN_UNICODE_INSERT:
2917 case LFUN_THESAURUS_ENTRY:
2919 // these are handled in our dispatch()
2923 case LFUN_INSET_INSERT: {
2924 string const type = cmd.getArg(0);
2925 if (type == "toc") {
2927 // not allowed in description items
2928 //FIXME: couldn't this be merged in Inset::insetAllowed()?
2929 enable = !inDescriptionItem(cur);
2942 || !cur.inset().insetAllowed(code)
2943 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
2946 flag.setEnabled(enable);
2951 void Text::pasteString(Cursor & cur, docstring const & clip,
2954 if (!clip.empty()) {
2957 insertStringAsParagraphs(cur, clip, cur.current_font);
2959 insertStringAsLines(cur, clip, cur.current_font);
2964 // FIXME: an item inset would make things much easier.
2965 bool Text::inDescriptionItem(Cursor & cur) const
2967 Paragraph & par = cur.paragraph();
2968 pos_type const pos = cur.pos();
2969 pos_type const body_pos = par.beginOfBody();
2971 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2972 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2973 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2976 return (pos < body_pos
2978 && (pos == 0 || par.getChar(pos - 1) != ' ')));