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);
298 if (cmd.action() == LFUN_FLEX_INSERT)
301 cur.leaveInset(*inset);
302 if (cmd.action() == LFUN_PREVIEW_INSERT
303 || cmd.action() == LFUN_IPA_INSERT)
305 notifyCursorLeavesOrEnters(old, cur);
308 cur.leaveInset(*inset);
309 // reset surrounding par to default
310 DocumentClass const & dc = bparams.documentClass();
311 docstring const layoutname = inset->usePlainLayout()
312 ? dc.plainLayoutName()
313 : dc.defaultLayoutName();
314 text->setLayout(cur, layoutname);
320 string const freefont2string()
322 return freefont.toString(toggleall);
326 /// the type of outline operation
328 OutlineUp, // Move this header with text down
329 OutlineDown, // Move this header with text up
330 OutlineIn, // Make this header deeper
331 OutlineOut // Make this header shallower
335 static void outline(OutlineOp mode, Cursor & cur)
337 Buffer & buf = *cur.buffer();
338 pit_type & pit = cur.pit();
339 ParagraphList & pars = buf.text().paragraphs();
340 ParagraphList::iterator const bgn = pars.begin();
341 // The first paragraph of the area to be copied:
342 ParagraphList::iterator start = boost::next(bgn, pit);
343 // The final paragraph of area to be copied:
344 ParagraphList::iterator finish = start;
345 ParagraphList::iterator const end = pars.end();
347 DocumentClass const & tc = buf.params().documentClass();
349 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
352 // Move out (down) from this section header
356 // Seek the one (on same level) below
357 for (; finish != end; ++finish) {
358 toclevel = buf.text().getTocLevel(distance(bgn, finish));
359 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
365 if (start == pars.begin())
368 ParagraphList::iterator dest = start;
369 // Move out (up) from this header
372 // Search previous same-level header above
375 toclevel = buf.text().getTocLevel(distance(bgn, dest));
377 && (toclevel == Layout::NOT_IN_TOC
378 || toclevel > thistoclevel));
379 // Not found; do nothing
380 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
382 pit_type const newpit = distance(bgn, dest);
383 pit_type const len = distance(start, finish);
384 pit_type const deletepit = pit + len;
385 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
386 pars.splice(dest, start, finish);
394 // Go one down from *this* header:
395 ParagraphList::iterator dest = boost::next(finish, 1);
396 // Go further down to find header to insert in front of:
397 for (; dest != end; ++dest) {
398 toclevel = buf.text().getTocLevel(distance(bgn, dest));
399 if (toclevel != Layout::NOT_IN_TOC
400 && toclevel <= thistoclevel)
403 // One such was found:
404 pit_type newpit = distance(bgn, dest);
405 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
406 pit_type const len = distance(start, finish);
407 pars.splice(dest, start, finish);
408 cur.pit() = newpit - len;
412 pit_type const len = distance(start, finish);
413 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
414 for (; start != finish; ++start) {
415 toclevel = buf.text().getTocLevel(distance(bgn, start));
416 if (toclevel == Layout::NOT_IN_TOC)
418 DocumentClass::const_iterator lit = tc.begin();
419 DocumentClass::const_iterator len = tc.end();
420 for (; lit != len; ++lit) {
421 if (lit->toclevel == toclevel + 1 &&
422 start->layout().labeltype == lit->labeltype) {
423 start->setLayout(*lit);
431 pit_type const len = distance(start, finish);
432 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
433 for (; start != finish; ++start) {
434 toclevel = buf.text().getTocLevel(distance(bgn, start));
435 if (toclevel == Layout::NOT_IN_TOC)
437 DocumentClass::const_iterator lit = tc.begin();
438 DocumentClass::const_iterator len = tc.end();
439 for (; lit != len; ++lit) {
440 if (lit->toclevel == toclevel - 1 &&
441 start->layout().labeltype == lit->labeltype) {
442 start->setLayout(*lit);
453 void Text::number(Cursor & cur)
455 FontInfo font = ignore_font;
456 font.setNumber(FONT_TOGGLE);
457 toggleAndShow(cur, this, Font(font, ignore_language));
461 bool Text::isRTL(Paragraph const & par) const
463 Buffer const & buffer = owner_->buffer();
464 return par.isRTL(buffer.params());
468 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
470 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
472 // Dispatch if the cursor is inside the text. It is not the
473 // case for context menus (bug 5797).
474 if (cur.text() != this) {
479 BufferView * bv = &cur.bv();
480 TextMetrics * tm = &bv->textMetrics(this);
481 if (!tm->contains(cur.pit())) {
482 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
483 tm = &bv->textMetrics(this);
486 // FIXME: We use the update flag to indicates wether a singlePar or a
487 // full screen update is needed. We reset it here but shall we restore it
489 cur.noScreenUpdate();
491 LASSERT(cur.text() == this, /**/);
492 CursorSlice const oldTopSlice = cur.top();
493 bool const oldBoundary = cur.boundary();
494 bool const oldSelection = cur.selection();
495 // Signals that, even if needsUpdate == false, an update of the
496 // cursor paragraph is required
497 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
498 LyXAction::SingleParUpdate);
499 // Signals that a full-screen update is required
500 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
501 LyXAction::NoUpdate) || singleParUpdate);
502 bool const last_misspelled = lyxrc.spellcheck_continuously
503 && cur.paragraph().isMisspelled(cur.pos(), true);
505 FuncCode const act = cmd.action();
508 case LFUN_PARAGRAPH_MOVE_DOWN: {
509 pit_type const pit = cur.pit();
510 recUndo(cur, pit, pit + 1);
512 pars_.swap(pit, pit + 1);
514 cur.forceBufferUpdate();
519 case LFUN_PARAGRAPH_MOVE_UP: {
520 pit_type const pit = cur.pit();
521 recUndo(cur, pit - 1, pit);
523 pars_.swap(pit, pit - 1);
526 cur.forceBufferUpdate();
530 case LFUN_APPENDIX: {
531 Paragraph & par = cur.paragraph();
532 bool start = !par.params().startOfAppendix();
534 // FIXME: The code below only makes sense at top level.
535 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
536 // ensure that we have only one start_of_appendix in this document
537 // FIXME: this don't work for multipart document!
538 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
539 if (pars_[tmp].params().startOfAppendix()) {
541 pars_[tmp].params().startOfAppendix(false);
547 par.params().startOfAppendix(start);
549 // we can set the refreshing parameters now
550 cur.forceBufferUpdate();
554 case LFUN_WORD_DELETE_FORWARD:
556 cutSelection(cur, true, false);
558 deleteWordForward(cur);
559 finishChange(cur, false);
562 case LFUN_WORD_DELETE_BACKWARD:
564 cutSelection(cur, true, false);
566 deleteWordBackward(cur);
567 finishChange(cur, false);
570 case LFUN_LINE_DELETE:
572 cutSelection(cur, true, false);
574 tm->deleteLineForward(cur);
575 finishChange(cur, false);
578 case LFUN_BUFFER_BEGIN:
579 case LFUN_BUFFER_BEGIN_SELECT:
580 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
581 if (cur.depth() == 1)
582 needsUpdate |= cursorTop(cur);
585 cur.screenUpdateFlags(Update::FitCursor);
588 case LFUN_BUFFER_END:
589 case LFUN_BUFFER_END_SELECT:
590 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
591 if (cur.depth() == 1)
592 needsUpdate |= cursorBottom(cur);
595 cur.screenUpdateFlags(Update::FitCursor);
598 case LFUN_INSET_BEGIN:
599 case LFUN_INSET_BEGIN_SELECT:
600 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
601 if (cur.depth() == 1 || !cur.top().at_begin())
602 needsUpdate |= cursorTop(cur);
605 cur.screenUpdateFlags(Update::FitCursor);
609 case LFUN_INSET_END_SELECT:
610 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
611 if (cur.depth() == 1 || !cur.top().at_end())
612 needsUpdate |= cursorBottom(cur);
615 cur.screenUpdateFlags(Update::FitCursor);
618 case LFUN_INSET_SELECT_ALL:
619 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
620 || !cur.selEnd().at_end()) {
621 needsUpdate |= cur.selHandle(false);
622 needsUpdate |= cursorTop(cur);
623 needsUpdate |= cur.selHandle(true);
624 needsUpdate |= cursorBottom(cur);
627 cur.screenUpdateFlags(Update::FitCursor);
630 case LFUN_CHAR_FORWARD:
631 case LFUN_CHAR_FORWARD_SELECT:
632 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
633 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
634 needsUpdate |= cursorForward(cur);
636 if (!needsUpdate && oldTopSlice == cur.top()
637 && cur.boundary() == oldBoundary) {
639 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
641 // we will probably be moving out the inset, so we should execute
642 // the depm-mechanism, but only when the cursor has a place to
643 // go outside this inset, i.e. in a slice above.
644 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
645 && cur.pit() == cur.lastpit()) {
646 // The cursor hasn't changed yet. To give the
647 // DEPM the possibility of doing something we must
648 // provide it with two different cursors.
650 dummy.pos() = dummy.pit() = 0;
651 if (cur.bv().checkDepm(dummy, cur)) {
652 cur.forceBufferUpdate();
653 // DEPM may have requested a screen update
654 cur.screenUpdateFlags(
655 cur.screenUpdate() | dummy.screenUpdate());
661 case LFUN_CHAR_BACKWARD:
662 case LFUN_CHAR_BACKWARD_SELECT:
663 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
664 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
665 needsUpdate |= cursorBackward(cur);
667 if (!needsUpdate && oldTopSlice == cur.top()
668 && cur.boundary() == oldBoundary) {
670 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
672 // we will probably be moving out the inset, so we should execute
673 // the depm-mechanism, but only when the cursor has a place to
674 // go outside this inset, i.e. in a slice above.
675 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
676 // The cursor hasn't changed yet. To give the
677 // DEPM the possibility of doing something we must
678 // provide it with two different cursors.
680 dummy.pos() = cur.lastpos();
681 dummy.pit() = cur.lastpit();
682 if (cur.bv().checkDepm(dummy, cur)) {
683 cur.forceBufferUpdate();
684 // DEPM may have requested a screen update
685 cur.screenUpdateFlags(
686 cur.screenUpdate() | dummy.screenUpdate());
693 case LFUN_CHAR_LEFT_SELECT:
694 if (lyxrc.visual_cursor) {
695 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
696 needsUpdate |= cursorVisLeft(cur);
697 if (!needsUpdate && oldTopSlice == cur.top()
698 && cur.boundary() == oldBoundary) {
700 cmd = FuncRequest(LFUN_FINISHED_LEFT);
703 if (reverseDirectionNeeded(cur)) {
704 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
705 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
707 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
708 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
715 case LFUN_CHAR_RIGHT:
716 case LFUN_CHAR_RIGHT_SELECT:
717 if (lyxrc.visual_cursor) {
718 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
719 needsUpdate |= cursorVisRight(cur);
720 if (!needsUpdate && oldTopSlice == cur.top()
721 && cur.boundary() == oldBoundary) {
723 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
726 if (reverseDirectionNeeded(cur)) {
727 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
728 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
730 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
731 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
740 case LFUN_DOWN_SELECT:
743 // stop/start the selection
744 bool select = cmd.action() == LFUN_DOWN_SELECT ||
745 cmd.action() == LFUN_UP_SELECT;
747 // move cursor up/down
748 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
749 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
751 if (!atFirstOrLastRow) {
752 needsUpdate |= cur.selHandle(select);
753 cur.selHandle(select);
754 cur.upDownInText(up, needsUpdate);
755 needsUpdate |= cur.beforeDispatchCursor().inMathed();
757 // if the cursor cannot be moved up or down do not remove
758 // the selection right now, but wait for the next dispatch.
760 needsUpdate |= cur.selHandle(select);
761 cur.upDownInText(up, needsUpdate);
768 case LFUN_PARAGRAPH_UP:
769 case LFUN_PARAGRAPH_UP_SELECT:
770 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
771 needsUpdate |= cursorUpParagraph(cur);
774 case LFUN_PARAGRAPH_DOWN:
775 case LFUN_PARAGRAPH_DOWN_SELECT:
776 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
777 needsUpdate |= cursorDownParagraph(cur);
780 case LFUN_LINE_BEGIN:
781 case LFUN_LINE_BEGIN_SELECT:
782 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
783 needsUpdate |= tm->cursorHome(cur);
787 case LFUN_LINE_END_SELECT:
788 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
789 needsUpdate |= tm->cursorEnd(cur);
792 case LFUN_SECTION_SELECT: {
793 Buffer const & buf = *cur.buffer();
794 pit_type const pit = cur.pit();
795 ParagraphList & pars = buf.text().paragraphs();
796 ParagraphList::iterator bgn = pars.begin();
797 // The first paragraph of the area to be selected:
798 ParagraphList::iterator start = boost::next(bgn, pit);
799 // The final paragraph of area to be selected:
800 ParagraphList::iterator finish = start;
801 ParagraphList::iterator end = pars.end();
803 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
804 if (thistoclevel == Layout::NOT_IN_TOC)
808 Cursor const old_cur = cur;
809 needsUpdate |= cur.selHandle(true);
811 // Move out (down) from this section header
815 // Seek the one (on same level) below
816 for (; finish != end; ++finish, ++cur.pit()) {
817 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
818 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
821 cur.pos() = cur.lastpos();
823 needsUpdate |= cur != old_cur;
827 case LFUN_WORD_RIGHT:
828 case LFUN_WORD_RIGHT_SELECT:
829 if (lyxrc.visual_cursor) {
830 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
831 needsUpdate |= cursorVisRightOneWord(cur);
832 if (!needsUpdate && oldTopSlice == cur.top()
833 && cur.boundary() == oldBoundary) {
835 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
838 if (reverseDirectionNeeded(cur)) {
839 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
840 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
842 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
843 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
850 case LFUN_WORD_FORWARD:
851 case LFUN_WORD_FORWARD_SELECT:
852 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
853 needsUpdate |= cursorForwardOneWord(cur);
855 if (!needsUpdate && oldTopSlice == cur.top()
856 && cur.boundary() == oldBoundary) {
858 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
860 // we will probably be moving out the inset, so we should execute
861 // the depm-mechanism, but only when the cursor has a place to
862 // go outside this inset, i.e. in a slice above.
863 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
864 && cur.pit() == cur.lastpit()) {
865 // The cursor hasn't changed yet. To give the
866 // DEPM the possibility of doing something we must
867 // provide it with two different cursors.
869 dummy.pos() = dummy.pit() = 0;
870 if (cur.bv().checkDepm(dummy, cur)) {
871 cur.forceBufferUpdate();
872 // DEPM may have requested a screen update
873 cur.screenUpdateFlags(
874 cur.screenUpdate() | dummy.screenUpdate());
881 case LFUN_WORD_LEFT_SELECT:
882 if (lyxrc.visual_cursor) {
883 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
884 needsUpdate |= cursorVisLeftOneWord(cur);
885 if (!needsUpdate && oldTopSlice == cur.top()
886 && cur.boundary() == oldBoundary) {
888 cmd = FuncRequest(LFUN_FINISHED_LEFT);
891 if (reverseDirectionNeeded(cur)) {
892 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
893 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
895 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
896 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
903 case LFUN_WORD_BACKWARD:
904 case LFUN_WORD_BACKWARD_SELECT:
905 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
906 needsUpdate |= cursorBackwardOneWord(cur);
908 if (!needsUpdate && oldTopSlice == cur.top()
909 && cur.boundary() == oldBoundary) {
911 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
913 // we will probably be moving out the inset, so we should execute
914 // the depm-mechanism, but only when the cursor has a place to
915 // go outside this inset, i.e. in a slice above.
916 if (cur.depth() > 1 && cur.pos() == 0
918 // The cursor hasn't changed yet. To give the
919 // DEPM the possibility of doing something we must
920 // provide it with two different cursors.
922 dummy.pos() = cur.lastpos();
923 dummy.pit() = cur.lastpit();
924 if (cur.bv().checkDepm(dummy, cur)) {
925 cur.forceBufferUpdate();
926 // DEPM may have requested a screen update
927 cur.screenUpdateFlags(
928 cur.screenUpdate() | dummy.screenUpdate());
934 case LFUN_WORD_SELECT: {
935 selectWord(cur, WHOLE_WORD);
936 finishChange(cur, true);
940 case LFUN_NEWLINE_INSERT: {
941 InsetNewlineParams inp;
942 docstring arg = cmd.argument();
943 // this avoids a double undo
944 // FIXME: should not be needed, ideally
945 if (!cur.selection())
947 cap::replaceSelection(cur);
948 if (arg == "linebreak")
949 inp.kind = InsetNewlineParams::LINEBREAK;
951 inp.kind = InsetNewlineParams::NEWLINE;
952 cur.insert(new InsetNewline(inp));
954 moveCursor(cur, false);
958 case LFUN_TAB_INSERT: {
959 bool const multi_par_selection = cur.selection() &&
960 cur.selBegin().pit() != cur.selEnd().pit();
961 if (multi_par_selection) {
962 // If there is a multi-paragraph selection, a tab is inserted
963 // at the beginning of each paragraph.
964 cur.recordUndoSelection();
965 pit_type const pit_end = cur.selEnd().pit();
966 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
967 pars_[pit].insertChar(0, '\t',
968 bv->buffer().params().trackChanges);
969 // Update the selection pos to make sure the selection does not
970 // change as the inserted tab will increase the logical pos.
971 if (cur.realAnchor().pit() == pit)
972 cur.realAnchor().forwardPos();
973 if (cur.pit() == pit)
978 // Maybe we shouldn't allow tabs within a line, because they
979 // are not (yet) aligned as one might do expect.
980 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
986 case LFUN_TAB_DELETE: {
987 bool const tc = bv->buffer().params().trackChanges;
988 if (cur.selection()) {
989 // If there is a selection, a tab (if present) is removed from
990 // the beginning of each paragraph.
991 cur.recordUndoSelection();
992 pit_type const pit_end = cur.selEnd().pit();
993 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
994 Paragraph & par = paragraphs()[pit];
997 char_type const c = par.getChar(0);
998 if (c == '\t' || c == ' ') {
999 // remove either 1 tab or 4 spaces.
1000 int const n = (c == ' ' ? 4 : 1);
1001 for (int i = 0; i < n
1002 && !par.empty() && par.getChar(0) == c; ++i) {
1003 if (cur.pit() == pit)
1005 if (cur.realAnchor().pit() == pit
1006 && cur.realAnchor().pos() > 0 )
1007 cur.realAnchor().backwardPos();
1008 par.eraseChar(0, tc);
1014 // If there is no selection, try to remove a tab or some spaces
1015 // before the position of the cursor.
1016 Paragraph & par = paragraphs()[cur.pit()];
1017 pos_type const pos = cur.pos();
1022 char_type const c = par.getChar(pos - 1);
1026 par.eraseChar(cur.pos(), tc);
1028 for (int n_spaces = 0;
1030 && par.getChar(cur.pos() - 1) == ' '
1034 par.eraseChar(cur.pos(), tc);
1041 case LFUN_CHAR_DELETE_FORWARD:
1042 if (!cur.selection()) {
1043 if (cur.pos() == cur.paragraph().size())
1044 // Par boundary, force full-screen update
1045 singleParUpdate = false;
1046 needsUpdate |= erase(cur);
1048 // It is possible to make it a lot faster still
1049 // just comment out the line below...
1051 cutSelection(cur, true, false);
1052 singleParUpdate = false;
1054 moveCursor(cur, false);
1057 case LFUN_CHAR_DELETE_BACKWARD:
1058 if (!cur.selection()) {
1059 if (bv->getIntl().getTransManager().backspace()) {
1060 // Par boundary, full-screen update
1062 singleParUpdate = false;
1063 needsUpdate |= backspace(cur);
1065 // It is possible to make it a lot faster still
1066 // just comment out the line below...
1069 cutSelection(cur, true, false);
1070 singleParUpdate = false;
1074 case LFUN_PARAGRAPH_BREAK:
1075 cap::replaceSelection(cur);
1076 breakParagraph(cur, cmd.argument() == "inverse");
1080 case LFUN_INSET_INSERT: {
1083 // We have to avoid triggering InstantPreview loading
1084 // before inserting into the document. See bug #5626.
1085 bool loaded = bv->buffer().isFullyLoaded();
1086 bv->buffer().setFullyLoaded(false);
1087 Inset * inset = createInset(&bv->buffer(), cmd);
1088 bv->buffer().setFullyLoaded(loaded);
1091 // FIXME (Abdel 01/02/2006):
1092 // What follows would be a partial fix for bug 2154:
1093 // http://www.lyx.org/trac/ticket/2154
1094 // This automatically put the label inset _after_ a
1095 // numbered section. It should be possible to extend the mechanism
1096 // to any kind of LateX environement.
1097 // The correct way to fix that bug would be at LateX generation.
1098 // I'll let the code here for reference as it could be used for some
1099 // other feature like "automatic labelling".
1101 Paragraph & par = pars_[cur.pit()];
1102 if (inset->lyxCode() == LABEL_CODE
1103 && par.layout().labeltype == LABEL_COUNTER) {
1104 // Go to the end of the paragraph
1105 // Warning: Because of Change-Tracking, the last
1106 // position is 'size()' and not 'size()-1':
1107 cur.pos() = par.size();
1108 // Insert a new paragraph
1109 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1113 if (cur.selection())
1114 cutSelection(cur, true, false);
1116 if (inset->editable() && inset->asInsetText())
1117 inset->edit(cur, true);
1121 // trigger InstantPreview now
1122 if (inset->lyxCode() == EXTERNAL_CODE) {
1123 InsetExternal & ins =
1124 static_cast<InsetExternal &>(*inset);
1125 ins.updatePreview();
1132 case LFUN_INSET_DISSOLVE: {
1133 if (dissolveInset(cur)) {
1135 cur.forceBufferUpdate();
1140 case LFUN_SET_GRAPHICS_GROUP: {
1141 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1147 string id = to_utf8(cmd.argument());
1148 string grp = graphics::getGroupParams(bv->buffer(), id);
1149 InsetGraphicsParams tmp, inspar = ins->getParams();
1152 inspar.groupId = to_utf8(cmd.argument());
1154 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1155 tmp.filename = inspar.filename;
1159 ins->setParams(inspar);
1162 case LFUN_SPACE_INSERT:
1163 if (cur.paragraph().layout().free_spacing)
1164 insertChar(cur, ' ');
1166 doInsertInset(cur, this, cmd, false, false);
1169 moveCursor(cur, false);
1172 case LFUN_SPECIALCHAR_INSERT: {
1173 string const name = to_utf8(cmd.argument());
1174 if (name == "hyphenation")
1175 specialChar(cur, InsetSpecialChar::HYPHENATION);
1176 else if (name == "ligature-break")
1177 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1178 else if (name == "slash")
1179 specialChar(cur, InsetSpecialChar::SLASH);
1180 else if (name == "nobreakdash")
1181 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1182 else if (name == "dots")
1183 specialChar(cur, InsetSpecialChar::LDOTS);
1184 else if (name == "end-of-sentence")
1185 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1186 else if (name == "menu-separator")
1187 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1188 else if (name.empty())
1189 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1191 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1195 case LFUN_IPAMACRO_INSERT: {
1196 string const arg = cmd.getArg(0);
1197 if (arg == "deco") {
1198 // Open the inset, and move the current selection
1200 doInsertInset(cur, this, cmd, true, true);
1202 // Some insets are numbered, others are shown in the outline pane so
1203 // let's update the labels and the toc backend.
1204 cur.forceBufferUpdate();
1207 if (arg == "tone-falling")
1208 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1209 else if (arg == "tone-rising")
1210 ipaChar(cur, InsetIPAChar::TONE_RISING);
1211 else if (arg == "tone-high-rising")
1212 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1213 else if (arg == "tone-low-rising")
1214 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1215 else if (arg == "tone-high-rising-falling")
1216 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1217 else if (arg.empty())
1218 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1220 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1224 case LFUN_WORD_UPCASE:
1225 changeCase(cur, text_uppercase);
1228 case LFUN_WORD_LOWCASE:
1229 changeCase(cur, text_lowercase);
1232 case LFUN_WORD_CAPITALIZE:
1233 changeCase(cur, text_capitalization);
1236 case LFUN_CHARS_TRANSPOSE:
1237 charsTranspose(cur);
1241 cur.message(_("Paste"));
1242 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1243 cap::replaceSelection(cur);
1245 // without argument?
1246 string const arg = to_utf8(cmd.argument());
1248 if (theClipboard().isInternal())
1249 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1250 else if (theClipboard().hasGraphicsContents()
1251 && !theClipboard().hasTextContents())
1252 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1254 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1255 } else if (isStrUnsignedInt(arg)) {
1256 // we have a numerical argument
1257 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1258 convert<unsigned int>(arg));
1260 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1262 type = Clipboard::PdfGraphicsType;
1263 else if (arg == "png")
1264 type = Clipboard::PngGraphicsType;
1265 else if (arg == "jpeg")
1266 type = Clipboard::JpegGraphicsType;
1267 else if (arg == "linkback")
1268 type = Clipboard::LinkBackGraphicsType;
1269 else if (arg == "emf")
1270 type = Clipboard::EmfGraphicsType;
1271 else if (arg == "wmf")
1272 type = Clipboard::WmfGraphicsType;
1275 LASSERT(false, /**/);
1277 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1280 bv->buffer().errors("Paste");
1281 cur.clearSelection(); // bug 393
1287 cutSelection(cur, true, true);
1288 cur.message(_("Cut"));
1293 cur.message(_("Copy"));
1296 case LFUN_SERVER_GET_XY:
1297 cur.message(from_utf8(
1298 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1299 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1302 case LFUN_SERVER_SET_XY: {
1305 istringstream is(to_utf8(cmd.argument()));
1308 lyxerr << "SETXY: Could not parse coordinates in '"
1309 << to_utf8(cmd.argument()) << endl;
1311 tm->setCursorFromCoordinates(cur, x, y);
1315 case LFUN_SERVER_GET_LAYOUT:
1316 cur.message(cur.paragraph().layout().name());
1320 docstring layout = cmd.argument();
1321 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1323 Paragraph const & para = cur.paragraph();
1324 docstring const old_layout = para.layout().name();
1325 DocumentClass const & tclass = bv->buffer().params().documentClass();
1328 layout = tclass.defaultLayoutName();
1330 if (owner_->forcePlainLayout())
1331 // in this case only the empty layout is allowed
1332 layout = tclass.plainLayoutName();
1333 else if (para.usePlainLayout()) {
1334 // in this case, default layout maps to empty layout
1335 if (layout == tclass.defaultLayoutName())
1336 layout = tclass.plainLayoutName();
1338 // otherwise, the empty layout maps to the default
1339 if (layout == tclass.plainLayoutName())
1340 layout = tclass.defaultLayoutName();
1343 bool hasLayout = tclass.hasLayout(layout);
1345 // If the entry is obsolete, use the new one instead.
1347 docstring const & obs = tclass[layout].obsoleted_by();
1353 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1354 from_utf8(N_(" not known")));
1358 bool change_layout = (old_layout != layout);
1360 if (!change_layout && cur.selection() &&
1361 cur.selBegin().pit() != cur.selEnd().pit())
1363 pit_type spit = cur.selBegin().pit();
1364 pit_type epit = cur.selEnd().pit() + 1;
1365 while (spit != epit) {
1366 if (pars_[spit].layout().name() != old_layout) {
1367 change_layout = true;
1375 setLayout(cur, layout);
1377 Layout::LaTeXArgMap args = tclass[layout].latexargs();
1378 Layout::LaTeXArgMap itemargs = tclass[layout].itemargs();
1379 if (!itemargs.empty())
1380 args.insert(itemargs.begin(), itemargs.end());
1381 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1382 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1383 for (; lait != laend; ++lait) {
1384 Layout::latexarg arg = (*lait).second;
1385 if (arg.autoinsert) {
1386 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1394 case LFUN_CLIPBOARD_PASTE:
1395 cap::replaceSelection(cur);
1396 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1397 cmd.argument() == "paragraph");
1398 bv->buffer().errors("Paste");
1401 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1402 cap::replaceSelection(cur);
1403 pasteSimpleText(cur, cmd.argument() == "paragraph");
1406 case LFUN_PRIMARY_SELECTION_PASTE:
1407 cap::replaceSelection(cur);
1408 pasteString(cur, theSelection().get(),
1409 cmd.argument() == "paragraph");
1412 case LFUN_SELECTION_PASTE:
1413 // Copy the selection buffer to the clipboard stack,
1414 // because we want it to appear in the "Edit->Paste
1416 cap::replaceSelection(cur);
1417 cap::copySelectionToStack();
1418 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1419 bv->buffer().errors("Paste");
1422 case LFUN_UNICODE_INSERT: {
1423 if (cmd.argument().empty())
1425 docstring hexstring = cmd.argument();
1426 if (isHex(hexstring)) {
1427 char_type c = hexToInt(hexstring);
1428 if (c >= 32 && c < 0x10ffff) {
1429 lyxerr << "Inserting c: " << c << endl;
1430 docstring s = docstring(1, c);
1431 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1437 case LFUN_QUOTE_INSERT: {
1438 // this avoids a double undo
1439 // FIXME: should not be needed, ideally
1440 if (!cur.selection())
1442 cap::replaceSelection(cur);
1444 Paragraph const & par = cur.paragraph();
1445 pos_type pos = cur.pos();
1447 BufferParams const & bufparams = bv->buffer().params();
1449 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1450 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1452 if (allow_inset_quote) {
1454 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1455 c = par.getChar(pos - 1);
1456 string const arg = to_utf8(cmd.argument());
1457 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1458 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1459 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1462 // The cursor might have been invalidated by the replaceSelection.
1463 cur.buffer()->changed(true);
1464 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1469 case LFUN_DATE_INSERT: {
1470 string const format = cmd.argument().empty()
1471 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1472 string const time = formatted_time(current_time(), format);
1473 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1477 case LFUN_MOUSE_TRIPLE:
1478 if (cmd.button() == mouse_button::button1) {
1479 tm->cursorHome(cur);
1487 case LFUN_MOUSE_DOUBLE:
1488 if (cmd.button() == mouse_button::button1) {
1489 selectWord(cur, WHOLE_WORD_STRICT);
1494 // Single-click on work area
1495 case LFUN_MOUSE_PRESS: {
1496 // We are not marking a selection with the keyboard in any case.
1497 Cursor & bvcur = cur.bv().cursor();
1498 bvcur.setMark(false);
1499 switch (cmd.button()) {
1500 case mouse_button::button1:
1502 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1503 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1504 if (bvcur.wordSelection())
1505 selectWord(bvcur, WHOLE_WORD);
1508 case mouse_button::button2:
1509 // Middle mouse pasting.
1510 bv->mouseSetCursor(cur);
1512 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1513 "selection-paste ; primary-selection-paste paragraph"));
1514 cur.noScreenUpdate();
1517 case mouse_button::button3: {
1518 // Don't do anything if we right-click a
1519 // selection, a context menu will popup.
1520 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1521 && cur < bvcur.selectionEnd()) {
1522 cur.noScreenUpdate();
1525 if (!bv->mouseSetCursor(cur, false))
1526 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1532 } // switch (cmd.button())
1535 case LFUN_MOUSE_MOTION: {
1536 // Mouse motion with right or middle mouse do nothing for now.
1537 if (cmd.button() != mouse_button::button1) {
1538 cur.noScreenUpdate();
1541 // ignore motions deeper nested than the real anchor
1542 Cursor & bvcur = cur.bv().cursor();
1543 if (!bvcur.realAnchor().hasPart(cur)) {
1547 CursorSlice old = bvcur.top();
1549 int const wh = bv->workHeight();
1550 int const y = max(0, min(wh - 1, cmd.y()));
1552 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1553 cur.setTargetX(cmd.x());
1555 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1556 else if (cmd.y() < 0)
1557 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1558 // This is to allow jumping over large insets
1559 if (cur.top() == old) {
1561 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1562 else if (cmd.y() < 0)
1563 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1565 // We continue with our existing selection or start a new one, so don't
1566 // reset the anchor.
1567 bvcur.setCursor(cur);
1568 bvcur.setSelection(true);
1569 if (cur.top() == old) {
1570 // We didn't move one iota, so no need to update the screen.
1571 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1572 //cur.noScreenUpdate();
1578 case LFUN_MOUSE_RELEASE:
1579 switch (cmd.button()) {
1580 case mouse_button::button1:
1581 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1582 // If there is a new selection, update persistent selection;
1583 // otherwise, single click does not clear persistent selection
1585 if (cur.selection()) {
1586 // Finish selection. If double click,
1587 // cur is moved to the end of word by
1588 // selectWord but bvcur is current
1590 cur.bv().cursor().setSelection();
1591 // We might have removed an empty but drawn selection
1592 // (probably a margin)
1593 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1595 cur.noScreenUpdate();
1596 // FIXME: We could try to handle drag and drop of selection here.
1599 case mouse_button::button2:
1600 // Middle mouse pasting is handled at mouse press time,
1601 // see LFUN_MOUSE_PRESS.
1602 cur.noScreenUpdate();
1605 case mouse_button::button3:
1606 // Cursor was set at LFUN_MOUSE_PRESS time.
1607 // FIXME: If there is a selection we could try to handle a special
1608 // drag & drop context menu.
1609 cur.noScreenUpdate();
1612 case mouse_button::none:
1613 case mouse_button::button4:
1614 case mouse_button::button5:
1616 } // switch (cmd.button())
1620 case LFUN_SELF_INSERT: {
1621 if (cmd.argument().empty())
1624 // Automatically delete the currently selected
1625 // text and replace it with what is being
1626 // typed in now. Depends on lyxrc settings
1627 // "auto_region_delete", which defaults to
1630 if (lyxrc.auto_region_delete && cur.selection())
1631 cutSelection(cur, false, false);
1633 cur.clearSelection();
1635 docstring::const_iterator cit = cmd.argument().begin();
1636 docstring::const_iterator const end = cmd.argument().end();
1637 for (; cit != end; ++cit)
1638 bv->translateAndInsert(*cit, this, cur);
1641 moveCursor(cur, false);
1642 cur.markNewWordPosition();
1643 bv->bookmarkEditPosition();
1647 case LFUN_HYPERLINK_INSERT: {
1648 InsetCommandParams p(HYPERLINK_CODE);
1650 if (cur.selection()) {
1651 content = cur.selectionAsString(false);
1652 cutSelection(cur, true, false);
1654 p["target"] = (cmd.argument().empty()) ?
1655 content : cmd.argument();
1656 string const data = InsetCommand::params2string(p);
1657 if (p["target"].empty()) {
1658 bv->showDialog("href", data);
1660 FuncRequest fr(LFUN_INSET_INSERT, data);
1666 case LFUN_LABEL_INSERT: {
1667 InsetCommandParams p(LABEL_CODE);
1668 // Try to generate a valid label
1669 p["name"] = (cmd.argument().empty()) ?
1670 cur.getPossibleLabel() :
1672 string const data = InsetCommand::params2string(p);
1674 if (cmd.argument().empty()) {
1675 bv->showDialog("label", data);
1677 FuncRequest fr(LFUN_INSET_INSERT, data);
1683 case LFUN_INFO_INSERT: {
1685 if (cmd.argument().empty() && cur.selection()) {
1686 // if command argument is empty use current selection as parameter.
1687 docstring ds = cur.selectionAsString(false);
1688 cutSelection(cur, true, false);
1689 FuncRequest cmd0(cmd, ds);
1690 inset = createInset(cur.buffer(), cmd0);
1692 inset = createInset(cur.buffer(), cmd);
1697 insertInset(cur, inset);
1701 case LFUN_CAPTION_INSERT:
1702 case LFUN_FOOTNOTE_INSERT:
1703 case LFUN_NOTE_INSERT:
1704 case LFUN_BOX_INSERT:
1705 case LFUN_BRANCH_INSERT:
1706 case LFUN_PHANTOM_INSERT:
1707 case LFUN_ERT_INSERT:
1708 case LFUN_LISTING_INSERT:
1709 case LFUN_MARGINALNOTE_INSERT:
1710 case LFUN_ARGUMENT_INSERT:
1711 case LFUN_INDEX_INSERT:
1712 case LFUN_PREVIEW_INSERT:
1713 case LFUN_SCRIPT_INSERT:
1714 case LFUN_IPA_INSERT:
1715 // Open the inset, and move the current selection
1717 doInsertInset(cur, this, cmd, true, true);
1719 // Some insets are numbered, others are shown in the outline pane so
1720 // let's update the labels and the toc backend.
1721 cur.forceBufferUpdate();
1724 case LFUN_FLEX_INSERT: {
1725 // Open the inset, and move the current selection
1727 bool const sel = cur.selection();
1728 doInsertInset(cur, this, cmd, true, true);
1729 // Insert auto-insert arguments
1730 bool autoargs = false;
1731 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1732 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1733 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1734 for (; lait != laend; ++lait) {
1735 Layout::latexarg arg = (*lait).second;
1736 if (arg.autoinsert) {
1737 // The cursor might have been invalidated by the replaceSelection.
1738 cur.buffer()->changed(true);
1739 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1746 cur.leaveInset(cur.inset());
1749 // Some insets are numbered, others are shown in the outline pane so
1750 // let's update the labels and the toc backend.
1751 cur.forceBufferUpdate();
1755 case LFUN_TABULAR_INSERT:
1756 // if there were no arguments, just open the dialog
1757 if (doInsertInset(cur, this, cmd, false, true))
1760 bv->showDialog("tabularcreate");
1764 case LFUN_FLOAT_INSERT:
1765 case LFUN_FLOAT_WIDE_INSERT:
1766 case LFUN_WRAP_INSERT: {
1767 // will some content be moved into the inset?
1768 bool const content = cur.selection();
1769 // does the content consist of multiple paragraphs?
1770 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1772 doInsertInset(cur, this, cmd, true, true);
1775 // If some single-par content is moved into the inset,
1776 // doInsertInset puts the cursor outside the inset.
1777 // To insert the caption we put it back into the inset.
1778 // FIXME cleanup doInsertInset to avoid such dances!
1779 if (content && singlepar)
1782 ParagraphList & pars = cur.text()->paragraphs();
1784 DocumentClass const & tclass = bv->buffer().params().documentClass();
1786 // add a separate paragraph for the caption inset
1787 pars.push_back(Paragraph());
1788 pars.back().setInsetOwner(&cur.text()->inset());
1789 pars.back().setPlainOrDefaultLayout(tclass);
1790 int cap_pit = pars.size() - 1;
1792 // if an empty inset was created, we create an additional empty
1793 // paragraph at the bottom so that the user can choose where to put
1794 // the graphics (or table).
1796 pars.push_back(Paragraph());
1797 pars.back().setInsetOwner(&cur.text()->inset());
1798 pars.back().setPlainOrDefaultLayout(tclass);
1801 // reposition the cursor to the caption
1802 cur.pit() = cap_pit;
1804 // FIXME: This Text/Cursor dispatch handling is a mess!
1805 // We cannot use Cursor::dispatch here it needs access to up to
1807 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1808 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1809 cur.forceBufferUpdate();
1810 cur.screenUpdateFlags(Update::Force);
1811 // FIXME: When leaving the Float (or Wrap) inset we should
1812 // delete any empty paragraph left above or below the
1817 case LFUN_NOMENCL_INSERT: {
1818 InsetCommandParams p(NOMENCL_CODE);
1819 if (cmd.argument().empty())
1820 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1822 p["symbol"] = cmd.argument();
1823 string const data = InsetCommand::params2string(p);
1824 bv->showDialog("nomenclature", data);
1828 case LFUN_INDEX_PRINT: {
1829 InsetCommandParams p(INDEX_PRINT_CODE);
1830 if (cmd.argument().empty())
1831 p["type"] = from_ascii("idx");
1833 p["type"] = cmd.argument();
1834 string const data = InsetCommand::params2string(p);
1835 FuncRequest fr(LFUN_INSET_INSERT, data);
1840 case LFUN_NOMENCL_PRINT:
1841 case LFUN_NEWPAGE_INSERT:
1843 doInsertInset(cur, this, cmd, false, false);
1847 case LFUN_DEPTH_DECREMENT:
1848 changeDepth(cur, DEC_DEPTH);
1851 case LFUN_DEPTH_INCREMENT:
1852 changeDepth(cur, INC_DEPTH);
1855 case LFUN_MATH_DISPLAY:
1856 mathDispatch(cur, cmd, true);
1859 case LFUN_REGEXP_MODE:
1860 regexpDispatch(cur, cmd);
1863 case LFUN_MATH_MODE:
1864 if (cmd.argument() == "on")
1865 // don't pass "on" as argument
1866 // (it would appear literally in the first cell)
1867 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1869 mathDispatch(cur, cmd, false);
1872 case LFUN_MATH_MACRO:
1873 if (cmd.argument().empty())
1874 cur.errorMessage(from_utf8(N_("Missing argument")));
1877 string s = to_utf8(cmd.argument());
1878 string const s1 = token(s, ' ', 1);
1879 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1880 string const s2 = token(s, ' ', 2);
1881 MacroType type = MacroTypeNewcommand;
1883 type = MacroTypeDef;
1884 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1885 from_utf8(token(s, ' ', 0)), nargs, false, type);
1886 inset->setBuffer(bv->buffer());
1887 insertInset(cur, inset);
1889 // enter macro inset and select the name
1891 cur.top().pos() = cur.top().lastpos();
1893 cur.setSelection(true);
1894 cur.top().pos() = 0;
1898 // passthrough hat and underscore outside mathed:
1899 case LFUN_MATH_SUBSCRIPT:
1900 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1902 case LFUN_MATH_SUPERSCRIPT:
1903 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1906 case LFUN_MATH_INSERT:
1907 case LFUN_MATH_AMS_MATRIX:
1908 case LFUN_MATH_MATRIX:
1909 case LFUN_MATH_DELIM:
1910 case LFUN_MATH_BIGDELIM: {
1912 cap::replaceSelection(cur);
1913 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1914 checkAndActivateInset(cur, true);
1915 LASSERT(cur.inMathed(), /**/);
1920 case LFUN_FONT_EMPH: {
1921 Font font(ignore_font, ignore_language);
1922 font.fontInfo().setEmph(FONT_TOGGLE);
1923 toggleAndShow(cur, this, font);
1927 case LFUN_FONT_ITAL: {
1928 Font font(ignore_font, ignore_language);
1929 font.fontInfo().setShape(ITALIC_SHAPE);
1930 toggleAndShow(cur, this, font);
1934 case LFUN_FONT_BOLD:
1935 case LFUN_FONT_BOLDSYMBOL: {
1936 Font font(ignore_font, ignore_language);
1937 font.fontInfo().setSeries(BOLD_SERIES);
1938 toggleAndShow(cur, this, font);
1942 case LFUN_FONT_NOUN: {
1943 Font font(ignore_font, ignore_language);
1944 font.fontInfo().setNoun(FONT_TOGGLE);
1945 toggleAndShow(cur, this, font);
1949 case LFUN_FONT_TYPEWRITER: {
1950 Font font(ignore_font, ignore_language);
1951 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1952 toggleAndShow(cur, this, font);
1956 case LFUN_FONT_SANS: {
1957 Font font(ignore_font, ignore_language);
1958 font.fontInfo().setFamily(SANS_FAMILY);
1959 toggleAndShow(cur, this, font);
1963 case LFUN_FONT_ROMAN: {
1964 Font font(ignore_font, ignore_language);
1965 font.fontInfo().setFamily(ROMAN_FAMILY);
1966 toggleAndShow(cur, this, font);
1970 case LFUN_FONT_DEFAULT: {
1971 Font font(inherit_font, ignore_language);
1972 toggleAndShow(cur, this, font);
1976 case LFUN_FONT_STRIKEOUT: {
1977 Font font(ignore_font, ignore_language);
1978 font.fontInfo().setStrikeout(FONT_TOGGLE);
1979 toggleAndShow(cur, this, font);
1983 case LFUN_FONT_UULINE: {
1984 Font font(ignore_font, ignore_language);
1985 font.fontInfo().setUuline(FONT_TOGGLE);
1986 toggleAndShow(cur, this, font);
1990 case LFUN_FONT_UWAVE: {
1991 Font font(ignore_font, ignore_language);
1992 font.fontInfo().setUwave(FONT_TOGGLE);
1993 toggleAndShow(cur, this, font);
1997 case LFUN_FONT_UNDERLINE: {
1998 Font font(ignore_font, ignore_language);
1999 font.fontInfo().setUnderbar(FONT_TOGGLE);
2000 toggleAndShow(cur, this, font);
2004 case LFUN_FONT_SIZE: {
2005 Font font(ignore_font, ignore_language);
2006 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2007 toggleAndShow(cur, this, font);
2011 case LFUN_LANGUAGE: {
2012 string const lang_arg = cmd.getArg(0);
2013 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2014 Language const * lang =
2015 reset ? reset_language
2016 : languages.getLanguage(lang_arg);
2017 // we allow reset_language, which is 0, but only if it
2018 // was requested via empty or "reset" arg.
2019 if (!lang && !reset)
2021 bool const toggle = (cmd.getArg(1) != "set");
2022 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2023 Font font(ignore_font, lang);
2024 toggleAndShow(cur, this, font, toggle);
2028 case LFUN_TEXTSTYLE_APPLY:
2029 toggleAndShow(cur, this, freefont, toggleall);
2030 cur.message(_("Character set"));
2033 // Set the freefont using the contents of \param data dispatched from
2034 // the frontends and apply it at the current cursor location.
2035 case LFUN_TEXTSTYLE_UPDATE: {
2038 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2041 toggleAndShow(cur, this, freefont, toggleall);
2042 cur.message(_("Character set"));
2044 lyxerr << "Argument not ok";
2049 case LFUN_FINISHED_LEFT:
2050 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2051 // We're leaving an inset, going left. If the inset is LTR, we're
2052 // leaving from the front, so we should not move (remain at --- but
2053 // not in --- the inset). If the inset is RTL, move left, without
2054 // entering the inset itself; i.e., move to after the inset.
2055 if (cur.paragraph().getFontSettings(
2056 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2057 cursorVisLeft(cur, true);
2060 case LFUN_FINISHED_RIGHT:
2061 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2062 // We're leaving an inset, going right. If the inset is RTL, we're
2063 // leaving from the front, so we should not move (remain at --- but
2064 // not in --- the inset). If the inset is LTR, move right, without
2065 // entering the inset itself; i.e., move to after the inset.
2066 if (!cur.paragraph().getFontSettings(
2067 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2068 cursorVisRight(cur, true);
2071 case LFUN_FINISHED_BACKWARD:
2072 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2075 case LFUN_FINISHED_FORWARD:
2076 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2078 cur.setCurrentFont();
2081 case LFUN_LAYOUT_PARAGRAPH: {
2083 params2string(cur.paragraph(), data);
2084 data = "show\n" + data;
2085 bv->showDialog("paragraph", data);
2089 case LFUN_PARAGRAPH_UPDATE: {
2091 params2string(cur.paragraph(), data);
2093 // Will the paragraph accept changes from the dialog?
2095 cur.inset().allowParagraphCustomization(cur.idx());
2097 data = "update " + convert<string>(accept) + '\n' + data;
2098 bv->updateDialog("paragraph", data);
2102 case LFUN_ACCENT_UMLAUT:
2103 case LFUN_ACCENT_CIRCUMFLEX:
2104 case LFUN_ACCENT_GRAVE:
2105 case LFUN_ACCENT_ACUTE:
2106 case LFUN_ACCENT_TILDE:
2107 case LFUN_ACCENT_CEDILLA:
2108 case LFUN_ACCENT_MACRON:
2109 case LFUN_ACCENT_DOT:
2110 case LFUN_ACCENT_UNDERDOT:
2111 case LFUN_ACCENT_UNDERBAR:
2112 case LFUN_ACCENT_CARON:
2113 case LFUN_ACCENT_BREVE:
2114 case LFUN_ACCENT_TIE:
2115 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2116 case LFUN_ACCENT_CIRCLE:
2117 case LFUN_ACCENT_OGONEK:
2118 theApp()->handleKeyFunc(cmd.action());
2119 if (!cmd.argument().empty())
2120 // FIXME: Are all these characters encoded in one byte in utf8?
2121 bv->translateAndInsert(cmd.argument()[0], this, cur);
2122 cur.screenUpdateFlags(Update::FitCursor);
2125 case LFUN_FLOAT_LIST_INSERT: {
2126 DocumentClass const & tclass = bv->buffer().params().documentClass();
2127 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2129 if (cur.selection())
2130 cutSelection(cur, true, false);
2131 breakParagraph(cur);
2133 if (cur.lastpos() != 0) {
2134 cursorBackward(cur);
2135 breakParagraph(cur);
2138 docstring const laystr = cur.inset().usePlainLayout() ?
2139 tclass.plainLayoutName() :
2140 tclass.defaultLayoutName();
2141 setLayout(cur, laystr);
2142 ParagraphParameters p;
2143 // FIXME If this call were replaced with one to clearParagraphParams(),
2144 // then we could get rid of this method altogether.
2145 setParagraphs(cur, p);
2146 // FIXME This should be simplified when InsetFloatList takes a
2147 // Buffer in its constructor.
2148 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2149 ifl->setBuffer(bv->buffer());
2150 insertInset(cur, ifl);
2153 lyxerr << "Non-existent float type: "
2154 << to_utf8(cmd.argument()) << endl;
2159 case LFUN_CHANGE_ACCEPT: {
2160 acceptOrRejectChanges(cur, ACCEPT);
2164 case LFUN_CHANGE_REJECT: {
2165 acceptOrRejectChanges(cur, REJECT);
2169 case LFUN_THESAURUS_ENTRY: {
2170 docstring arg = cmd.argument();
2172 arg = cur.selectionAsString(false);
2174 if (arg.size() > 100 || arg.empty()) {
2175 // Get word or selection
2176 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2177 arg = cur.selectionAsString(false);
2178 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2181 bv->showDialog("thesaurus", to_utf8(arg));
2185 case LFUN_SPELLING_ADD: {
2186 docstring word = from_utf8(cmd.getArg(0));
2189 word = cur.selectionAsString(false);
2191 if (word.size() > 100 || word.empty()) {
2192 // Get word or selection
2193 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2194 word = cur.selectionAsString(false);
2196 lang = const_cast<Language *>(cur.getFont().language());
2198 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2199 WordLangTuple wl(word, lang);
2200 theSpellChecker()->insert(wl);
2204 case LFUN_SPELLING_IGNORE: {
2205 docstring word = from_utf8(cmd.getArg(0));
2208 word = cur.selectionAsString(false);
2210 if (word.size() > 100 || word.empty()) {
2211 // Get word or selection
2212 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2213 word = cur.selectionAsString(false);
2215 lang = const_cast<Language *>(cur.getFont().language());
2217 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2218 WordLangTuple wl(word, lang);
2219 theSpellChecker()->accept(wl);
2223 case LFUN_SPELLING_REMOVE: {
2224 docstring word = from_utf8(cmd.getArg(0));
2227 word = cur.selectionAsString(false);
2229 if (word.size() > 100 || word.empty()) {
2230 // Get word or selection
2231 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2232 word = cur.selectionAsString(false);
2234 lang = const_cast<Language *>(cur.getFont().language());
2236 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2237 WordLangTuple wl(word, lang);
2238 theSpellChecker()->remove(wl);
2242 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2243 // Given data, an encoding of the ParagraphParameters
2244 // generated in the Paragraph dialog, this function sets
2245 // the current paragraph, or currently selected paragraphs,
2247 // NOTE: This function overrides all existing settings.
2248 setParagraphs(cur, cmd.argument());
2249 cur.message(_("Paragraph layout set"));
2253 case LFUN_PARAGRAPH_PARAMS: {
2254 // Given data, an encoding of the ParagraphParameters as we'd
2255 // find them in a LyX file, this function modifies the current paragraph,
2256 // or currently selected paragraphs.
2257 // NOTE: This function only modifies, and does not override, existing
2259 setParagraphs(cur, cmd.argument(), true);
2260 cur.message(_("Paragraph layout set"));
2265 if (cur.selection()) {
2266 cur.setSelection(false);
2269 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2270 // correct, but I'm not 100% sure -- dov, 071019
2271 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2275 case LFUN_OUTLINE_UP:
2276 outline(OutlineUp, cur);
2277 setCursor(cur, cur.pit(), 0);
2278 cur.forceBufferUpdate();
2282 case LFUN_OUTLINE_DOWN:
2283 outline(OutlineDown, cur);
2284 setCursor(cur, cur.pit(), 0);
2285 cur.forceBufferUpdate();
2289 case LFUN_OUTLINE_IN:
2290 outline(OutlineIn, cur);
2291 cur.forceBufferUpdate();
2295 case LFUN_OUTLINE_OUT:
2296 outline(OutlineOut, cur);
2297 cur.forceBufferUpdate();
2302 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2307 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2309 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2310 // Check for misspelled text
2311 // The redraw is useful because of the painting of
2312 // misspelled markers depends on the cursor position.
2313 // Trigger a redraw for cursor moves inside misspelled text.
2314 if (!cur.inTexted()) {
2315 // move from regular text to math
2316 needsUpdate = last_misspelled;
2317 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2318 // move inside regular text
2319 needsUpdate = last_misspelled
2320 || cur.paragraph().isMisspelled(cur.pos(), true);
2324 // FIXME: The cursor flag is reset two lines below
2325 // so we need to check here if some of the LFUN did touch that.
2326 // for now only Text::erase() and Text::backspace() do that.
2327 // The plan is to verify all the LFUNs and then to remove this
2328 // singleParUpdate boolean altogether.
2329 if (cur.result().screenUpdate() & Update::Force) {
2330 singleParUpdate = false;
2334 // FIXME: the following code should go in favor of fine grained
2335 // update flag treatment.
2336 if (singleParUpdate) {
2337 // Inserting characters does not change par height in general. So, try
2338 // to update _only_ this paragraph. BufferView will detect if a full
2339 // metrics update is needed anyway.
2340 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2344 && &oldTopSlice.inset() == &cur.inset()
2345 && oldTopSlice.idx() == cur.idx()
2346 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2347 && !cur.selection())
2348 // FIXME: it would be better if we could just do this
2350 //if (cur.result().update() != Update::FitCursor)
2351 // cur.noScreenUpdate();
2353 // But some LFUNs do not set Update::FitCursor when needed, so we
2354 // do it for all. This is not very harmfull as FitCursor will provoke
2355 // a full redraw only if needed but still, a proper review of all LFUN
2356 // should be done and this needsUpdate boolean can then be removed.
2357 cur.screenUpdateFlags(Update::FitCursor);
2359 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2363 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2364 FuncStatus & flag) const
2366 LASSERT(cur.text() == this, /**/);
2368 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2370 bool allow_in_passthru = false;
2371 InsetCode code = NO_CODE;
2373 switch (cmd.action()) {
2375 case LFUN_DEPTH_DECREMENT:
2376 enable = changeDepthAllowed(cur, DEC_DEPTH);
2379 case LFUN_DEPTH_INCREMENT:
2380 enable = changeDepthAllowed(cur, INC_DEPTH);
2384 // FIXME We really should not allow this to be put, e.g.,
2385 // in a footnote, or in ERT. But it would make sense in a
2386 // branch, so I'm not sure what to do.
2387 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2390 case LFUN_DIALOG_SHOW_NEW_INSET:
2391 if (cmd.argument() == "bibitem")
2392 code = BIBITEM_CODE;
2393 else if (cmd.argument() == "bibtex") {
2395 // not allowed in description items
2396 enable = !inDescriptionItem(cur);
2398 else if (cmd.argument() == "box")
2400 else if (cmd.argument() == "branch")
2402 else if (cmd.argument() == "citation")
2404 else if (cmd.argument() == "ert")
2406 else if (cmd.argument() == "external")
2407 code = EXTERNAL_CODE;
2408 else if (cmd.argument() == "float")
2410 else if (cmd.argument() == "graphics")
2411 code = GRAPHICS_CODE;
2412 else if (cmd.argument() == "href")
2413 code = HYPERLINK_CODE;
2414 else if (cmd.argument() == "include")
2415 code = INCLUDE_CODE;
2416 else if (cmd.argument() == "index")
2418 else if (cmd.argument() == "index_print")
2419 code = INDEX_PRINT_CODE;
2420 else if (cmd.argument() == "nomenclature")
2421 code = NOMENCL_CODE;
2422 else if (cmd.argument() == "nomencl_print")
2423 code = NOMENCL_PRINT_CODE;
2424 else if (cmd.argument() == "label")
2426 else if (cmd.argument() == "line")
2428 else if (cmd.argument() == "note")
2430 else if (cmd.argument() == "phantom")
2431 code = PHANTOM_CODE;
2432 else if (cmd.argument() == "ref")
2434 else if (cmd.argument() == "space")
2436 else if (cmd.argument() == "toc")
2438 else if (cmd.argument() == "vspace")
2440 else if (cmd.argument() == "wrap")
2442 else if (cmd.argument() == "listings")
2443 code = LISTINGS_CODE;
2446 case LFUN_ERT_INSERT:
2449 case LFUN_LISTING_INSERT:
2450 code = LISTINGS_CODE;
2451 // not allowed in description items
2452 enable = !inDescriptionItem(cur);
2454 case LFUN_FOOTNOTE_INSERT:
2457 case LFUN_TABULAR_INSERT:
2458 code = TABULAR_CODE;
2460 case LFUN_MARGINALNOTE_INSERT:
2463 case LFUN_FLOAT_INSERT:
2464 case LFUN_FLOAT_WIDE_INSERT:
2465 // FIXME: If there is a selection, we should check whether there
2466 // are floats in the selection, but this has performance issues, see
2467 // LFUN_CHANGE_ACCEPT/REJECT.
2469 if (inDescriptionItem(cur))
2470 // not allowed in description items
2473 InsetCode const inset_code = cur.inset().lyxCode();
2475 // algorithm floats cannot be put in another float
2476 if (to_utf8(cmd.argument()) == "algorithm") {
2477 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2481 // for figures and tables: only allow in another
2482 // float or wrap if it is of the same type and
2483 // not a subfloat already
2484 if(cur.inset().lyxCode() == code) {
2485 InsetFloat const & ins =
2486 static_cast<InsetFloat const &>(cur.inset());
2487 enable = ins.params().type == to_utf8(cmd.argument())
2488 && !ins.params().subfloat;
2489 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2490 InsetWrap const & ins =
2491 static_cast<InsetWrap const &>(cur.inset());
2492 enable = ins.params().type == to_utf8(cmd.argument());
2496 case LFUN_WRAP_INSERT:
2498 // not allowed in description items
2499 enable = !inDescriptionItem(cur);
2501 case LFUN_FLOAT_LIST_INSERT: {
2502 code = FLOAT_LIST_CODE;
2503 // not allowed in description items
2504 enable = !inDescriptionItem(cur);
2506 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2507 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2508 // make sure we know about such floats
2509 if (cit == floats.end() ||
2510 // and that we know how to generate a list of them
2511 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2512 flag.setUnknown(true);
2513 // probably not necessary, but...
2519 case LFUN_CAPTION_INSERT:
2520 code = CAPTION_CODE;
2521 // not allowed in description items
2522 enable = !inDescriptionItem(cur);
2524 case LFUN_NOTE_INSERT:
2526 // in commands (sections etc.) and description items,
2527 // only Notes are allowed
2528 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2529 (!cur.paragraph().layout().isCommand()
2530 && !inDescriptionItem(cur)));
2532 case LFUN_FLEX_INSERT: {
2534 string s = cmd.getArg(0);
2536 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2537 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2538 il.lyxtype() != InsetLayout::CUSTOM &&
2539 il.lyxtype() != InsetLayout::ELEMENT &&
2540 il.lyxtype ()!= InsetLayout::STANDARD)
2544 case LFUN_BOX_INSERT:
2547 case LFUN_BRANCH_INSERT:
2549 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2550 && cur.buffer()->params().branchlist().empty())
2553 case LFUN_IPA_INSERT:
2556 case LFUN_PHANTOM_INSERT:
2557 code = PHANTOM_CODE;
2559 case LFUN_LABEL_INSERT:
2562 case LFUN_INFO_INSERT:
2565 case LFUN_ARGUMENT_INSERT: {
2567 allow_in_passthru = true;
2568 string const arg = cmd.getArg(0);
2573 Layout const & lay = cur.paragraph().layout();
2574 Layout::LaTeXArgMap args = lay.latexargs();
2575 Layout::LaTeXArgMap itemargs = lay.itemargs();
2576 if (!itemargs.empty())
2577 args.insert(itemargs.begin(), itemargs.end());
2578 Layout::LaTeXArgMap::const_iterator const lait =
2580 if (lait != args.end()) {
2582 pit_type pit = cur.pit();
2583 pit_type lastpit = cur.pit();
2584 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2585 // In a sequence of "merged" environment layouts, we only allow
2586 // non-item arguments once.
2587 lastpit = cur.lastpit();
2588 // get the first paragraph in sequence with this layout
2589 depth_type const current_depth = cur.paragraph().params().depth();
2593 Paragraph cpar = pars_[pit - 1];
2594 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2600 for (; pit <= lastpit; ++pit) {
2601 if (pars_[pit].layout() != lay)
2603 InsetList::const_iterator it = pars_[pit].insetList().begin();
2604 InsetList::const_iterator end = pars_[pit].insetList().end();
2605 for (; it != end; ++it) {
2606 if (it->inset->lyxCode() == ARG_CODE) {
2607 InsetArgument const * ins =
2608 static_cast<InsetArgument const *>(it->inset);
2609 if (ins->name() == arg) {
2610 // we have this already
2621 case LFUN_INDEX_INSERT:
2624 case LFUN_INDEX_PRINT:
2625 code = INDEX_PRINT_CODE;
2626 // not allowed in description items
2627 enable = !inDescriptionItem(cur);
2629 case LFUN_NOMENCL_INSERT:
2630 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2634 code = NOMENCL_CODE;
2636 case LFUN_NOMENCL_PRINT:
2637 code = NOMENCL_PRINT_CODE;
2638 // not allowed in description items
2639 enable = !inDescriptionItem(cur);
2641 case LFUN_HYPERLINK_INSERT:
2642 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2646 code = HYPERLINK_CODE;
2648 case LFUN_IPAMACRO_INSERT: {
2649 string const arg = cmd.getArg(0);
2651 code = IPADECO_CODE;
2653 code = IPACHAR_CODE;
2656 case LFUN_QUOTE_INSERT:
2657 // always allow this, since we will inset a raw quote
2658 // if an inset is not allowed.
2660 case LFUN_SPECIALCHAR_INSERT:
2661 code = SPECIALCHAR_CODE;
2663 case LFUN_SPACE_INSERT:
2664 // slight hack: we know this is allowed in math mode
2668 case LFUN_PREVIEW_INSERT:
2669 code = PREVIEW_CODE;
2671 case LFUN_SCRIPT_INSERT:
2675 case LFUN_MATH_INSERT:
2676 case LFUN_MATH_AMS_MATRIX:
2677 case LFUN_MATH_MATRIX:
2678 case LFUN_MATH_DELIM:
2679 case LFUN_MATH_BIGDELIM:
2680 case LFUN_MATH_DISPLAY:
2681 case LFUN_MATH_MODE:
2682 case LFUN_MATH_MACRO:
2683 case LFUN_MATH_SUBSCRIPT:
2684 case LFUN_MATH_SUPERSCRIPT:
2685 code = MATH_HULL_CODE;
2688 case LFUN_REGEXP_MODE:
2689 code = MATH_HULL_CODE;
2690 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2693 case LFUN_INSET_MODIFY:
2694 // We need to disable this, because we may get called for a
2696 // InsetTabular::getStatus() -> InsetText::getStatus()
2697 // and we don't handle LFUN_INSET_MODIFY.
2701 case LFUN_FONT_EMPH:
2702 flag.setOnOff(fontinfo.emph() == FONT_ON);
2703 enable = !cur.paragraph().isPassThru();
2706 case LFUN_FONT_ITAL:
2707 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2708 enable = !cur.paragraph().isPassThru();
2711 case LFUN_FONT_NOUN:
2712 flag.setOnOff(fontinfo.noun() == FONT_ON);
2713 enable = !cur.paragraph().isPassThru();
2716 case LFUN_FONT_BOLD:
2717 case LFUN_FONT_BOLDSYMBOL:
2718 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2719 enable = !cur.paragraph().isPassThru();
2722 case LFUN_FONT_SANS:
2723 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2724 enable = !cur.paragraph().isPassThru();
2727 case LFUN_FONT_ROMAN:
2728 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2729 enable = !cur.paragraph().isPassThru();
2732 case LFUN_FONT_TYPEWRITER:
2733 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2734 enable = !cur.paragraph().isPassThru();
2739 enable = cur.selection();
2743 if (cmd.argument().empty()) {
2744 if (theClipboard().isInternal())
2745 enable = cap::numberOfSelections() > 0;
2747 enable = !theClipboard().empty();
2751 // we have an argument
2752 string const arg = to_utf8(cmd.argument());
2753 if (isStrUnsignedInt(arg)) {
2754 // it's a number and therefore means the internal stack
2755 unsigned int n = convert<unsigned int>(arg);
2756 enable = cap::numberOfSelections() > n;
2760 // explicit graphics type?
2761 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2762 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2763 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2764 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2765 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2766 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2767 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2768 enable = theClipboard().hasGraphicsContents(type);
2777 case LFUN_CLIPBOARD_PASTE:
2778 case LFUN_CLIPBOARD_PASTE_SIMPLE:
2779 enable = !theClipboard().empty();
2782 case LFUN_PRIMARY_SELECTION_PASTE:
2783 enable = cur.selection() || !theSelection().empty();
2786 case LFUN_SELECTION_PASTE:
2787 enable = cap::selection();
2790 case LFUN_PARAGRAPH_MOVE_UP:
2791 enable = cur.pit() > 0 && !cur.selection();
2794 case LFUN_PARAGRAPH_MOVE_DOWN:
2795 enable = cur.pit() < cur.lastpit() && !cur.selection();
2798 case LFUN_CHANGE_ACCEPT:
2799 case LFUN_CHANGE_REJECT:
2800 // In principle, these LFUNs should only be enabled if there
2801 // is a change at the current position/in the current selection.
2802 // However, without proper optimizations, this will inevitably
2803 // result in unacceptable performance - just imagine a user who
2804 // wants to select the complete content of a long document.
2805 if (!cur.selection())
2806 enable = cur.paragraph().isChanged(cur.pos());
2808 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2813 case LFUN_OUTLINE_UP:
2814 case LFUN_OUTLINE_DOWN:
2815 case LFUN_OUTLINE_IN:
2816 case LFUN_OUTLINE_OUT:
2817 // FIXME: LyX is not ready for outlining within inset.
2818 enable = isMainText()
2819 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
2822 case LFUN_NEWLINE_INSERT:
2823 // LaTeX restrictions (labels or empty par)
2824 enable = (cur.pos() > cur.paragraph().beginOfBody());
2827 case LFUN_TAB_INSERT:
2828 case LFUN_TAB_DELETE:
2829 enable = cur.paragraph().isPassThru();
2832 case LFUN_SET_GRAPHICS_GROUP: {
2833 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2837 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2841 case LFUN_NEWPAGE_INSERT:
2842 // not allowed in description items
2843 code = NEWPAGE_CODE;
2844 enable = !inDescriptionItem(cur);
2847 case LFUN_DATE_INSERT: {
2848 string const format = cmd.argument().empty()
2849 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2850 enable = support::os::is_valid_strftime(format);
2855 enable = !cur.paragraph().isPassThru();
2856 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
2859 case LFUN_PARAGRAPH_BREAK:
2860 enable = cur.inset().getLayout().isMultiPar();
2863 case LFUN_SPELLING_ADD:
2864 case LFUN_SPELLING_IGNORE:
2865 case LFUN_SPELLING_REMOVE:
2866 enable = theSpellChecker();
2870 enable = !cur.inset().forcePlainLayout();
2873 case LFUN_LAYOUT_PARAGRAPH:
2874 case LFUN_PARAGRAPH_PARAMS:
2875 case LFUN_PARAGRAPH_PARAMS_APPLY:
2876 case LFUN_PARAGRAPH_UPDATE:
2877 enable = cur.inset().allowParagraphCustomization();
2880 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2881 case LFUN_ACCENT_ACUTE:
2882 case LFUN_ACCENT_BREVE:
2883 case LFUN_ACCENT_CARON:
2884 case LFUN_ACCENT_CEDILLA:
2885 case LFUN_ACCENT_CIRCLE:
2886 case LFUN_ACCENT_CIRCUMFLEX:
2887 case LFUN_ACCENT_DOT:
2888 case LFUN_ACCENT_GRAVE:
2889 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2890 case LFUN_ACCENT_MACRON:
2891 case LFUN_ACCENT_OGONEK:
2892 case LFUN_ACCENT_TIE:
2893 case LFUN_ACCENT_TILDE:
2894 case LFUN_ACCENT_UMLAUT:
2895 case LFUN_ACCENT_UNDERBAR:
2896 case LFUN_ACCENT_UNDERDOT:
2897 case LFUN_FONT_DEFAULT:
2898 case LFUN_FONT_FRAK:
2899 case LFUN_FONT_SIZE:
2900 case LFUN_FONT_STATE:
2901 case LFUN_FONT_UNDERLINE:
2902 case LFUN_FONT_STRIKEOUT:
2903 case LFUN_FONT_UULINE:
2904 case LFUN_FONT_UWAVE:
2905 case LFUN_TEXTSTYLE_APPLY:
2906 case LFUN_TEXTSTYLE_UPDATE:
2907 enable = !cur.paragraph().isPassThru();
2910 case LFUN_WORD_DELETE_FORWARD:
2911 case LFUN_WORD_DELETE_BACKWARD:
2912 case LFUN_LINE_DELETE:
2913 case LFUN_WORD_FORWARD:
2914 case LFUN_WORD_BACKWARD:
2915 case LFUN_WORD_RIGHT:
2916 case LFUN_WORD_LEFT:
2917 case LFUN_CHAR_FORWARD:
2918 case LFUN_CHAR_FORWARD_SELECT:
2919 case LFUN_CHAR_BACKWARD:
2920 case LFUN_CHAR_BACKWARD_SELECT:
2921 case LFUN_CHAR_LEFT:
2922 case LFUN_CHAR_LEFT_SELECT:
2923 case LFUN_CHAR_RIGHT:
2924 case LFUN_CHAR_RIGHT_SELECT:
2926 case LFUN_UP_SELECT:
2928 case LFUN_DOWN_SELECT:
2929 case LFUN_PARAGRAPH_UP_SELECT:
2930 case LFUN_PARAGRAPH_DOWN_SELECT:
2931 case LFUN_LINE_BEGIN_SELECT:
2932 case LFUN_LINE_END_SELECT:
2933 case LFUN_WORD_FORWARD_SELECT:
2934 case LFUN_WORD_BACKWARD_SELECT:
2935 case LFUN_WORD_RIGHT_SELECT:
2936 case LFUN_WORD_LEFT_SELECT:
2937 case LFUN_WORD_SELECT:
2938 case LFUN_SECTION_SELECT:
2939 case LFUN_BUFFER_BEGIN:
2940 case LFUN_BUFFER_END:
2941 case LFUN_BUFFER_BEGIN_SELECT:
2942 case LFUN_BUFFER_END_SELECT:
2943 case LFUN_INSET_BEGIN:
2944 case LFUN_INSET_END:
2945 case LFUN_INSET_BEGIN_SELECT:
2946 case LFUN_INSET_END_SELECT:
2947 case LFUN_INSET_SELECT_ALL:
2948 case LFUN_PARAGRAPH_UP:
2949 case LFUN_PARAGRAPH_DOWN:
2950 case LFUN_LINE_BEGIN:
2952 case LFUN_CHAR_DELETE_FORWARD:
2953 case LFUN_CHAR_DELETE_BACKWARD:
2954 case LFUN_WORD_UPCASE:
2955 case LFUN_WORD_LOWCASE:
2956 case LFUN_WORD_CAPITALIZE:
2957 case LFUN_CHARS_TRANSPOSE:
2958 case LFUN_SERVER_GET_XY:
2959 case LFUN_SERVER_SET_XY:
2960 case LFUN_SERVER_GET_LAYOUT:
2961 case LFUN_SELF_INSERT:
2962 case LFUN_UNICODE_INSERT:
2963 case LFUN_THESAURUS_ENTRY:
2965 // these are handled in our dispatch()
2969 case LFUN_INSET_INSERT: {
2970 string const type = cmd.getArg(0);
2971 if (type == "toc") {
2973 // not allowed in description items
2974 //FIXME: couldn't this be merged in Inset::insetAllowed()?
2975 enable = !inDescriptionItem(cur);
2988 || !cur.inset().insetAllowed(code)
2989 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
2992 flag.setEnabled(enable);
2997 void Text::pasteString(Cursor & cur, docstring const & clip,
3000 if (!clip.empty()) {
3003 insertStringAsParagraphs(cur, clip, cur.current_font);
3005 insertStringAsLines(cur, clip, cur.current_font);
3010 // FIXME: an item inset would make things much easier.
3011 bool Text::inDescriptionItem(Cursor & cur) const
3013 Paragraph & par = cur.paragraph();
3014 pos_type const pos = cur.pos();
3015 pos_type const body_pos = par.beginOfBody();
3017 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3018 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3019 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3022 return (pos < body_pos
3024 && (pos == 0 || par.getChar(pos - 1) != ' ')));