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 LATTEST(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 LBUFERR(this == cur.text());
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 if (arg == "linebreak")
944 inp.kind = InsetNewlineParams::LINEBREAK;
946 inp.kind = InsetNewlineParams::NEWLINE;
947 cap::replaceSelection(cur);
949 cur.insert(new InsetNewline(inp));
951 moveCursor(cur, false);
955 case LFUN_TAB_INSERT: {
956 bool const multi_par_selection = cur.selection() &&
957 cur.selBegin().pit() != cur.selEnd().pit();
958 if (multi_par_selection) {
959 // If there is a multi-paragraph selection, a tab is inserted
960 // at the beginning of each paragraph.
961 cur.recordUndoSelection();
962 pit_type const pit_end = cur.selEnd().pit();
963 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
964 pars_[pit].insertChar(0, '\t',
965 bv->buffer().params().trackChanges);
966 // Update the selection pos to make sure the selection does not
967 // change as the inserted tab will increase the logical pos.
968 if (cur.realAnchor().pit() == pit)
969 cur.realAnchor().forwardPos();
970 if (cur.pit() == pit)
975 // Maybe we shouldn't allow tabs within a line, because they
976 // are not (yet) aligned as one might do expect.
977 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
983 case LFUN_TAB_DELETE: {
984 bool const tc = bv->buffer().params().trackChanges;
985 if (cur.selection()) {
986 // If there is a selection, a tab (if present) is removed from
987 // the beginning of each paragraph.
988 cur.recordUndoSelection();
989 pit_type const pit_end = cur.selEnd().pit();
990 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
991 Paragraph & par = paragraphs()[pit];
994 char_type const c = par.getChar(0);
995 if (c == '\t' || c == ' ') {
996 // remove either 1 tab or 4 spaces.
997 int const n = (c == ' ' ? 4 : 1);
998 for (int i = 0; i < n
999 && !par.empty() && par.getChar(0) == c; ++i) {
1000 if (cur.pit() == pit)
1002 if (cur.realAnchor().pit() == pit
1003 && cur.realAnchor().pos() > 0 )
1004 cur.realAnchor().backwardPos();
1005 par.eraseChar(0, tc);
1011 // If there is no selection, try to remove a tab or some spaces
1012 // before the position of the cursor.
1013 Paragraph & par = paragraphs()[cur.pit()];
1014 pos_type const pos = cur.pos();
1019 char_type const c = par.getChar(pos - 1);
1023 par.eraseChar(cur.pos(), tc);
1025 for (int n_spaces = 0;
1027 && par.getChar(cur.pos() - 1) == ' '
1031 par.eraseChar(cur.pos(), tc);
1038 case LFUN_CHAR_DELETE_FORWARD:
1039 if (!cur.selection()) {
1040 if (cur.pos() == cur.paragraph().size())
1041 // Par boundary, force full-screen update
1042 singleParUpdate = false;
1043 needsUpdate |= erase(cur);
1045 // It is possible to make it a lot faster still
1046 // just comment out the line below...
1048 cutSelection(cur, true, false);
1049 singleParUpdate = false;
1051 moveCursor(cur, false);
1054 case LFUN_CHAR_DELETE_BACKWARD:
1055 if (!cur.selection()) {
1056 if (bv->getIntl().getTransManager().backspace()) {
1057 // Par boundary, full-screen update
1059 singleParUpdate = false;
1060 needsUpdate |= backspace(cur);
1062 // It is possible to make it a lot faster still
1063 // just comment out the line below...
1066 cutSelection(cur, true, false);
1067 singleParUpdate = false;
1071 case LFUN_PARAGRAPH_BREAK:
1072 cap::replaceSelection(cur);
1073 breakParagraph(cur, cmd.argument() == "inverse");
1077 case LFUN_INSET_INSERT: {
1080 // We have to avoid triggering InstantPreview loading
1081 // before inserting into the document. See bug #5626.
1082 bool loaded = bv->buffer().isFullyLoaded();
1083 bv->buffer().setFullyLoaded(false);
1084 Inset * inset = createInset(&bv->buffer(), cmd);
1085 bv->buffer().setFullyLoaded(loaded);
1088 // FIXME (Abdel 01/02/2006):
1089 // What follows would be a partial fix for bug 2154:
1090 // http://www.lyx.org/trac/ticket/2154
1091 // This automatically put the label inset _after_ a
1092 // numbered section. It should be possible to extend the mechanism
1093 // to any kind of LateX environement.
1094 // The correct way to fix that bug would be at LateX generation.
1095 // I'll let the code here for reference as it could be used for some
1096 // other feature like "automatic labelling".
1098 Paragraph & par = pars_[cur.pit()];
1099 if (inset->lyxCode() == LABEL_CODE
1100 && !par.layout().counter.empty() {
1101 // Go to the end of the paragraph
1102 // Warning: Because of Change-Tracking, the last
1103 // position is 'size()' and not 'size()-1':
1104 cur.pos() = par.size();
1105 // Insert a new paragraph
1106 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1110 if (cur.selection())
1111 cutSelection(cur, true, false);
1113 if (inset->editable() && inset->asInsetText())
1114 inset->edit(cur, true);
1118 // trigger InstantPreview now
1119 if (inset->lyxCode() == EXTERNAL_CODE) {
1120 InsetExternal & ins =
1121 static_cast<InsetExternal &>(*inset);
1122 ins.updatePreview();
1129 case LFUN_INSET_DISSOLVE: {
1130 if (dissolveInset(cur)) {
1132 cur.forceBufferUpdate();
1137 case LFUN_SET_GRAPHICS_GROUP: {
1138 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1144 string id = to_utf8(cmd.argument());
1145 string grp = graphics::getGroupParams(bv->buffer(), id);
1146 InsetGraphicsParams tmp, inspar = ins->getParams();
1149 inspar.groupId = to_utf8(cmd.argument());
1151 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1152 tmp.filename = inspar.filename;
1156 ins->setParams(inspar);
1159 case LFUN_SPACE_INSERT:
1160 if (cur.paragraph().layout().free_spacing)
1161 insertChar(cur, ' ');
1163 doInsertInset(cur, this, cmd, false, false);
1166 moveCursor(cur, false);
1169 case LFUN_SPECIALCHAR_INSERT: {
1170 string const name = to_utf8(cmd.argument());
1171 if (name == "hyphenation")
1172 specialChar(cur, InsetSpecialChar::HYPHENATION);
1173 else if (name == "ligature-break")
1174 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1175 else if (name == "slash")
1176 specialChar(cur, InsetSpecialChar::SLASH);
1177 else if (name == "nobreakdash")
1178 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1179 else if (name == "dots")
1180 specialChar(cur, InsetSpecialChar::LDOTS);
1181 else if (name == "end-of-sentence")
1182 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1183 else if (name == "menu-separator")
1184 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1185 else if (name.empty())
1186 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1188 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1192 case LFUN_IPAMACRO_INSERT: {
1193 string const arg = cmd.getArg(0);
1194 if (arg == "deco") {
1195 // Open the inset, and move the current selection
1197 doInsertInset(cur, this, cmd, true, true);
1199 // Some insets are numbered, others are shown in the outline pane so
1200 // let's update the labels and the toc backend.
1201 cur.forceBufferUpdate();
1204 if (arg == "tone-falling")
1205 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1206 else if (arg == "tone-rising")
1207 ipaChar(cur, InsetIPAChar::TONE_RISING);
1208 else if (arg == "tone-high-rising")
1209 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1210 else if (arg == "tone-low-rising")
1211 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1212 else if (arg == "tone-high-rising-falling")
1213 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1214 else if (arg.empty())
1215 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1217 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1221 case LFUN_WORD_UPCASE:
1222 changeCase(cur, text_uppercase);
1225 case LFUN_WORD_LOWCASE:
1226 changeCase(cur, text_lowercase);
1229 case LFUN_WORD_CAPITALIZE:
1230 changeCase(cur, text_capitalization);
1233 case LFUN_CHARS_TRANSPOSE:
1234 charsTranspose(cur);
1238 cur.message(_("Paste"));
1239 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1240 cap::replaceSelection(cur);
1242 // without argument?
1243 string const arg = to_utf8(cmd.argument());
1245 if (theClipboard().isInternal())
1246 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1247 else if (theClipboard().hasGraphicsContents()
1248 && !theClipboard().hasTextContents())
1249 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1251 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true);
1252 } else if (isStrUnsignedInt(arg)) {
1253 // we have a numerical argument
1254 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1255 convert<unsigned int>(arg));
1256 } else if (arg == "html" || arg == "latex") {
1257 Clipboard::TextType type = (arg == "html") ?
1258 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1259 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1261 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1263 type = Clipboard::PdfGraphicsType;
1264 else if (arg == "png")
1265 type = Clipboard::PngGraphicsType;
1266 else if (arg == "jpeg")
1267 type = Clipboard::JpegGraphicsType;
1268 else if (arg == "linkback")
1269 type = Clipboard::LinkBackGraphicsType;
1270 else if (arg == "emf")
1271 type = Clipboard::EmfGraphicsType;
1272 else if (arg == "wmf")
1273 type = Clipboard::WmfGraphicsType;
1275 // We used to assert, but couldn't the argument come from, say, the
1276 // minibuffer and just be mistyped?
1277 LYXERR0("Unrecognized graphics type: " << arg);
1279 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1282 bv->buffer().errors("Paste");
1283 cur.clearSelection(); // bug 393
1289 cutSelection(cur, true, true);
1290 cur.message(_("Cut"));
1295 cur.message(_("Copy"));
1298 case LFUN_SERVER_GET_XY:
1299 cur.message(from_utf8(
1300 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1301 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1304 case LFUN_SERVER_SET_XY: {
1307 istringstream is(to_utf8(cmd.argument()));
1310 lyxerr << "SETXY: Could not parse coordinates in '"
1311 << to_utf8(cmd.argument()) << endl;
1313 tm->setCursorFromCoordinates(cur, x, y);
1317 case LFUN_SERVER_GET_LAYOUT:
1318 cur.message(cur.paragraph().layout().name());
1322 docstring layout = cmd.argument();
1323 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1325 Paragraph const & para = cur.paragraph();
1326 docstring const old_layout = para.layout().name();
1327 DocumentClass const & tclass = bv->buffer().params().documentClass();
1330 layout = tclass.defaultLayoutName();
1332 if (owner_->forcePlainLayout())
1333 // in this case only the empty layout is allowed
1334 layout = tclass.plainLayoutName();
1335 else if (para.usePlainLayout()) {
1336 // in this case, default layout maps to empty layout
1337 if (layout == tclass.defaultLayoutName())
1338 layout = tclass.plainLayoutName();
1340 // otherwise, the empty layout maps to the default
1341 if (layout == tclass.plainLayoutName())
1342 layout = tclass.defaultLayoutName();
1345 bool hasLayout = tclass.hasLayout(layout);
1347 // If the entry is obsolete, use the new one instead.
1349 docstring const & obs = tclass[layout].obsoleted_by();
1355 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1356 from_utf8(N_(" not known")));
1360 bool change_layout = (old_layout != layout);
1362 if (!change_layout && cur.selection() &&
1363 cur.selBegin().pit() != cur.selEnd().pit())
1365 pit_type spit = cur.selBegin().pit();
1366 pit_type epit = cur.selEnd().pit() + 1;
1367 while (spit != epit) {
1368 if (pars_[spit].layout().name() != old_layout) {
1369 change_layout = true;
1377 setLayout(cur, layout);
1379 Layout::LaTeXArgMap args = tclass[layout].args();
1380 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1381 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1382 for (; lait != laend; ++lait) {
1383 Layout::latexarg arg = (*lait).second;
1384 if (arg.autoinsert) {
1385 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1393 case LFUN_ENVIRONMENT_SPLIT: {
1394 Paragraph const & para = cur.paragraph();
1395 docstring const layout = para.layout().name();
1397 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1398 bool const morecont = cur.lastpos() > cur.pos();
1399 lyx::dispatch(FuncRequest(LFUN_LAYOUT, "Separator"));
1400 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1402 lyx::dispatch(FuncRequest(LFUN_DOWN));
1403 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1408 case LFUN_CLIPBOARD_PASTE:
1409 cap::replaceSelection(cur);
1410 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1411 cmd.argument() == "paragraph");
1412 bv->buffer().errors("Paste");
1415 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1416 cap::replaceSelection(cur);
1417 pasteSimpleText(cur, cmd.argument() == "paragraph");
1420 case LFUN_PRIMARY_SELECTION_PASTE:
1421 cap::replaceSelection(cur);
1422 pasteString(cur, theSelection().get(),
1423 cmd.argument() == "paragraph");
1426 case LFUN_SELECTION_PASTE:
1427 // Copy the selection buffer to the clipboard stack,
1428 // because we want it to appear in the "Edit->Paste
1430 cap::replaceSelection(cur);
1431 cap::copySelectionToStack();
1432 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1433 bv->buffer().errors("Paste");
1436 case LFUN_UNICODE_INSERT: {
1437 if (cmd.argument().empty())
1439 docstring hexstring = cmd.argument();
1440 if (isHex(hexstring)) {
1441 char_type c = hexToInt(hexstring);
1442 if (c >= 32 && c < 0x10ffff) {
1443 lyxerr << "Inserting c: " << c << endl;
1444 docstring s = docstring(1, c);
1445 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1451 case LFUN_QUOTE_INSERT: {
1452 cap::replaceSelection(cur);
1455 Paragraph const & par = cur.paragraph();
1456 pos_type pos = cur.pos();
1457 // Ignore deleted text before cursor
1458 while (pos > 0 && par.isDeleted(pos - 1))
1461 BufferParams const & bufparams = bv->buffer().params();
1463 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1464 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1466 if (allow_inset_quote) {
1468 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1469 c = par.getChar(pos - 1);
1470 string const arg = to_utf8(cmd.argument());
1471 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1472 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1473 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1476 // The cursor might have been invalidated by the replaceSelection.
1477 cur.buffer()->changed(true);
1478 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1483 case LFUN_DATE_INSERT: {
1484 string const format = cmd.argument().empty()
1485 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1486 string const time = formatted_time(current_time(), format);
1487 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1491 case LFUN_MOUSE_TRIPLE:
1492 if (cmd.button() == mouse_button::button1) {
1493 tm->cursorHome(cur);
1501 case LFUN_MOUSE_DOUBLE:
1502 if (cmd.button() == mouse_button::button1) {
1503 selectWord(cur, WHOLE_WORD_STRICT);
1508 // Single-click on work area
1509 case LFUN_MOUSE_PRESS: {
1510 // We are not marking a selection with the keyboard in any case.
1511 Cursor & bvcur = cur.bv().cursor();
1512 bvcur.setMark(false);
1513 switch (cmd.button()) {
1514 case mouse_button::button1:
1516 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1517 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1518 if (bvcur.wordSelection())
1519 selectWord(bvcur, WHOLE_WORD);
1522 case mouse_button::button2:
1523 // Middle mouse pasting.
1524 bv->mouseSetCursor(cur);
1526 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1527 "selection-paste ; primary-selection-paste paragraph"));
1528 cur.noScreenUpdate();
1531 case mouse_button::button3: {
1532 // Don't do anything if we right-click a
1533 // selection, a context menu will popup.
1534 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1535 && cur < bvcur.selectionEnd()) {
1536 cur.noScreenUpdate();
1539 if (!bv->mouseSetCursor(cur, false))
1540 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1546 } // switch (cmd.button())
1549 case LFUN_MOUSE_MOTION: {
1550 // Mouse motion with right or middle mouse do nothing for now.
1551 if (cmd.button() != mouse_button::button1) {
1552 cur.noScreenUpdate();
1555 // ignore motions deeper nested than the real anchor
1556 Cursor & bvcur = cur.bv().cursor();
1557 if (!bvcur.realAnchor().hasPart(cur)) {
1561 CursorSlice old = bvcur.top();
1563 int const wh = bv->workHeight();
1564 int const y = max(0, min(wh - 1, cmd.y()));
1566 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1567 cur.setTargetX(cmd.x());
1569 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1570 else if (cmd.y() < 0)
1571 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1572 // This is to allow jumping over large insets
1573 if (cur.top() == old) {
1575 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1576 else if (cmd.y() < 0)
1577 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1579 // We continue with our existing selection or start a new one, so don't
1580 // reset the anchor.
1581 bvcur.setCursor(cur);
1582 bvcur.setSelection(true);
1583 if (cur.top() == old) {
1584 // We didn't move one iota, so no need to update the screen.
1585 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1586 //cur.noScreenUpdate();
1592 case LFUN_MOUSE_RELEASE:
1593 switch (cmd.button()) {
1594 case mouse_button::button1:
1595 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1596 // If there is a new selection, update persistent selection;
1597 // otherwise, single click does not clear persistent selection
1599 if (cur.selection()) {
1600 // Finish selection. If double click,
1601 // cur is moved to the end of word by
1602 // selectWord but bvcur is current
1604 cur.bv().cursor().setSelection();
1605 // We might have removed an empty but drawn selection
1606 // (probably a margin)
1607 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1609 cur.noScreenUpdate();
1610 // FIXME: We could try to handle drag and drop of selection here.
1613 case mouse_button::button2:
1614 // Middle mouse pasting is handled at mouse press time,
1615 // see LFUN_MOUSE_PRESS.
1616 cur.noScreenUpdate();
1619 case mouse_button::button3:
1620 // Cursor was set at LFUN_MOUSE_PRESS time.
1621 // FIXME: If there is a selection we could try to handle a special
1622 // drag & drop context menu.
1623 cur.noScreenUpdate();
1626 case mouse_button::none:
1627 case mouse_button::button4:
1628 case mouse_button::button5:
1630 } // switch (cmd.button())
1634 case LFUN_SELF_INSERT: {
1635 if (cmd.argument().empty())
1638 // Automatically delete the currently selected
1639 // text and replace it with what is being
1640 // typed in now. Depends on lyxrc settings
1641 // "auto_region_delete", which defaults to
1644 if (lyxrc.auto_region_delete && cur.selection())
1645 cutSelection(cur, false, false);
1647 cur.clearSelection();
1649 docstring::const_iterator cit = cmd.argument().begin();
1650 docstring::const_iterator const end = cmd.argument().end();
1651 for (; cit != end; ++cit)
1652 bv->translateAndInsert(*cit, this, cur);
1655 moveCursor(cur, false);
1656 cur.markNewWordPosition();
1657 bv->bookmarkEditPosition();
1661 case LFUN_HYPERLINK_INSERT: {
1662 InsetCommandParams p(HYPERLINK_CODE);
1664 if (cur.selection()) {
1665 content = cur.selectionAsString(false);
1666 cutSelection(cur, true, false);
1668 p["target"] = (cmd.argument().empty()) ?
1669 content : cmd.argument();
1670 string const data = InsetCommand::params2string(p);
1671 if (p["target"].empty()) {
1672 bv->showDialog("href", data);
1674 FuncRequest fr(LFUN_INSET_INSERT, data);
1680 case LFUN_LABEL_INSERT: {
1681 InsetCommandParams p(LABEL_CODE);
1682 // Try to generate a valid label
1683 p["name"] = (cmd.argument().empty()) ?
1684 cur.getPossibleLabel() :
1686 string const data = InsetCommand::params2string(p);
1688 if (cmd.argument().empty()) {
1689 bv->showDialog("label", data);
1691 FuncRequest fr(LFUN_INSET_INSERT, data);
1697 case LFUN_INFO_INSERT: {
1699 if (cmd.argument().empty() && cur.selection()) {
1700 // if command argument is empty use current selection as parameter.
1701 docstring ds = cur.selectionAsString(false);
1702 cutSelection(cur, true, false);
1703 FuncRequest cmd0(cmd, ds);
1704 inset = createInset(cur.buffer(), cmd0);
1706 inset = createInset(cur.buffer(), cmd);
1711 insertInset(cur, inset);
1715 case LFUN_CAPTION_INSERT:
1716 case LFUN_FOOTNOTE_INSERT:
1717 case LFUN_NOTE_INSERT:
1718 case LFUN_BOX_INSERT:
1719 case LFUN_BRANCH_INSERT:
1720 case LFUN_PHANTOM_INSERT:
1721 case LFUN_ERT_INSERT:
1722 case LFUN_LISTING_INSERT:
1723 case LFUN_MARGINALNOTE_INSERT:
1724 case LFUN_ARGUMENT_INSERT:
1725 case LFUN_INDEX_INSERT:
1726 case LFUN_PREVIEW_INSERT:
1727 case LFUN_SCRIPT_INSERT:
1728 case LFUN_IPA_INSERT:
1729 // Open the inset, and move the current selection
1731 doInsertInset(cur, this, cmd, true, true);
1733 // Some insets are numbered, others are shown in the outline pane so
1734 // let's update the labels and the toc backend.
1735 cur.forceBufferUpdate();
1738 case LFUN_FLEX_INSERT: {
1739 // Open the inset, and move the current selection
1741 bool const sel = cur.selection();
1742 doInsertInset(cur, this, cmd, true, true);
1743 // Insert auto-insert arguments
1744 bool autoargs = false;
1745 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1746 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1747 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1748 for (; lait != laend; ++lait) {
1749 Layout::latexarg arg = (*lait).second;
1750 if (arg.autoinsert) {
1751 // The cursor might have been invalidated by the replaceSelection.
1752 cur.buffer()->changed(true);
1753 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1760 cur.leaveInset(cur.inset());
1763 // Some insets are numbered, others are shown in the outline pane so
1764 // let's update the labels and the toc backend.
1765 cur.forceBufferUpdate();
1769 case LFUN_TABULAR_INSERT:
1770 // if there were no arguments, just open the dialog
1771 if (doInsertInset(cur, this, cmd, false, true))
1774 bv->showDialog("tabularcreate");
1778 case LFUN_FLOAT_INSERT:
1779 case LFUN_FLOAT_WIDE_INSERT:
1780 case LFUN_WRAP_INSERT: {
1781 // will some content be moved into the inset?
1782 bool const content = cur.selection();
1783 // does the content consist of multiple paragraphs?
1784 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1786 doInsertInset(cur, this, cmd, true, true);
1789 // If some single-par content is moved into the inset,
1790 // doInsertInset puts the cursor outside the inset.
1791 // To insert the caption we put it back into the inset.
1792 // FIXME cleanup doInsertInset to avoid such dances!
1793 if (content && singlepar)
1796 ParagraphList & pars = cur.text()->paragraphs();
1798 DocumentClass const & tclass = bv->buffer().params().documentClass();
1800 // add a separate paragraph for the caption inset
1801 pars.push_back(Paragraph());
1802 pars.back().setInsetOwner(&cur.text()->inset());
1803 pars.back().setPlainOrDefaultLayout(tclass);
1804 int cap_pit = pars.size() - 1;
1806 // if an empty inset was created, we create an additional empty
1807 // paragraph at the bottom so that the user can choose where to put
1808 // the graphics (or table).
1810 pars.push_back(Paragraph());
1811 pars.back().setInsetOwner(&cur.text()->inset());
1812 pars.back().setPlainOrDefaultLayout(tclass);
1815 // reposition the cursor to the caption
1816 cur.pit() = cap_pit;
1818 // FIXME: This Text/Cursor dispatch handling is a mess!
1819 // We cannot use Cursor::dispatch here it needs access to up to
1821 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1822 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1823 cur.forceBufferUpdate();
1824 cur.screenUpdateFlags(Update::Force);
1825 // FIXME: When leaving the Float (or Wrap) inset we should
1826 // delete any empty paragraph left above or below the
1831 case LFUN_NOMENCL_INSERT: {
1832 InsetCommandParams p(NOMENCL_CODE);
1833 if (cmd.argument().empty())
1834 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1836 p["symbol"] = cmd.argument();
1837 string const data = InsetCommand::params2string(p);
1838 bv->showDialog("nomenclature", data);
1842 case LFUN_INDEX_PRINT: {
1843 InsetCommandParams p(INDEX_PRINT_CODE);
1844 if (cmd.argument().empty())
1845 p["type"] = from_ascii("idx");
1847 p["type"] = cmd.argument();
1848 string const data = InsetCommand::params2string(p);
1849 FuncRequest fr(LFUN_INSET_INSERT, data);
1854 case LFUN_NOMENCL_PRINT:
1855 case LFUN_NEWPAGE_INSERT:
1857 doInsertInset(cur, this, cmd, false, false);
1861 case LFUN_DEPTH_DECREMENT:
1862 changeDepth(cur, DEC_DEPTH);
1865 case LFUN_DEPTH_INCREMENT:
1866 changeDepth(cur, INC_DEPTH);
1869 case LFUN_MATH_DISPLAY:
1870 mathDispatch(cur, cmd, true);
1873 case LFUN_REGEXP_MODE:
1874 regexpDispatch(cur, cmd);
1877 case LFUN_MATH_MODE:
1878 if (cmd.argument() == "on")
1879 // don't pass "on" as argument
1880 // (it would appear literally in the first cell)
1881 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1883 mathDispatch(cur, cmd, false);
1886 case LFUN_MATH_MACRO:
1887 if (cmd.argument().empty())
1888 cur.errorMessage(from_utf8(N_("Missing argument")));
1891 string s = to_utf8(cmd.argument());
1892 string const s1 = token(s, ' ', 1);
1893 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1894 string const s2 = token(s, ' ', 2);
1895 MacroType type = MacroTypeNewcommand;
1897 type = MacroTypeDef;
1898 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1899 from_utf8(token(s, ' ', 0)), nargs, false, type);
1900 inset->setBuffer(bv->buffer());
1901 insertInset(cur, inset);
1903 // enter macro inset and select the name
1905 cur.top().pos() = cur.top().lastpos();
1907 cur.setSelection(true);
1908 cur.top().pos() = 0;
1912 // passthrough hat and underscore outside mathed:
1913 case LFUN_MATH_SUBSCRIPT:
1914 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1916 case LFUN_MATH_SUPERSCRIPT:
1917 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1920 case LFUN_MATH_INSERT:
1921 case LFUN_MATH_AMS_MATRIX:
1922 case LFUN_MATH_MATRIX:
1923 case LFUN_MATH_DELIM:
1924 case LFUN_MATH_BIGDELIM: {
1926 cap::replaceSelection(cur);
1927 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1928 checkAndActivateInset(cur, true);
1929 LASSERT(cur.inMathed(), break);
1934 case LFUN_FONT_EMPH: {
1935 Font font(ignore_font, ignore_language);
1936 font.fontInfo().setEmph(FONT_TOGGLE);
1937 toggleAndShow(cur, this, font);
1941 case LFUN_FONT_ITAL: {
1942 Font font(ignore_font, ignore_language);
1943 font.fontInfo().setShape(ITALIC_SHAPE);
1944 toggleAndShow(cur, this, font);
1948 case LFUN_FONT_BOLD:
1949 case LFUN_FONT_BOLDSYMBOL: {
1950 Font font(ignore_font, ignore_language);
1951 font.fontInfo().setSeries(BOLD_SERIES);
1952 toggleAndShow(cur, this, font);
1956 case LFUN_FONT_NOUN: {
1957 Font font(ignore_font, ignore_language);
1958 font.fontInfo().setNoun(FONT_TOGGLE);
1959 toggleAndShow(cur, this, font);
1963 case LFUN_FONT_TYPEWRITER: {
1964 Font font(ignore_font, ignore_language);
1965 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1966 toggleAndShow(cur, this, font);
1970 case LFUN_FONT_SANS: {
1971 Font font(ignore_font, ignore_language);
1972 font.fontInfo().setFamily(SANS_FAMILY);
1973 toggleAndShow(cur, this, font);
1977 case LFUN_FONT_ROMAN: {
1978 Font font(ignore_font, ignore_language);
1979 font.fontInfo().setFamily(ROMAN_FAMILY);
1980 toggleAndShow(cur, this, font);
1984 case LFUN_FONT_DEFAULT: {
1985 Font font(inherit_font, ignore_language);
1986 toggleAndShow(cur, this, font);
1990 case LFUN_FONT_STRIKEOUT: {
1991 Font font(ignore_font, ignore_language);
1992 font.fontInfo().setStrikeout(FONT_TOGGLE);
1993 toggleAndShow(cur, this, font);
1997 case LFUN_FONT_UULINE: {
1998 Font font(ignore_font, ignore_language);
1999 font.fontInfo().setUuline(FONT_TOGGLE);
2000 toggleAndShow(cur, this, font);
2004 case LFUN_FONT_UWAVE: {
2005 Font font(ignore_font, ignore_language);
2006 font.fontInfo().setUwave(FONT_TOGGLE);
2007 toggleAndShow(cur, this, font);
2011 case LFUN_FONT_UNDERLINE: {
2012 Font font(ignore_font, ignore_language);
2013 font.fontInfo().setUnderbar(FONT_TOGGLE);
2014 toggleAndShow(cur, this, font);
2018 case LFUN_FONT_SIZE: {
2019 Font font(ignore_font, ignore_language);
2020 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2021 toggleAndShow(cur, this, font);
2025 case LFUN_LANGUAGE: {
2026 string const lang_arg = cmd.getArg(0);
2027 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2028 Language const * lang =
2029 reset ? reset_language
2030 : languages.getLanguage(lang_arg);
2031 // we allow reset_language, which is 0, but only if it
2032 // was requested via empty or "reset" arg.
2033 if (!lang && !reset)
2035 bool const toggle = (cmd.getArg(1) != "set");
2036 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2037 Font font(ignore_font, lang);
2038 toggleAndShow(cur, this, font, toggle);
2042 case LFUN_TEXTSTYLE_APPLY:
2043 toggleAndShow(cur, this, freefont, toggleall);
2044 cur.message(_("Character set"));
2047 // Set the freefont using the contents of \param data dispatched from
2048 // the frontends and apply it at the current cursor location.
2049 case LFUN_TEXTSTYLE_UPDATE: {
2052 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2055 toggleAndShow(cur, this, freefont, toggleall);
2056 cur.message(_("Character set"));
2058 lyxerr << "Argument not ok";
2063 case LFUN_FINISHED_LEFT:
2064 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2065 // We're leaving an inset, going left. If the inset is LTR, we're
2066 // leaving from the front, so we should not move (remain at --- but
2067 // not in --- the inset). If the inset is RTL, move left, without
2068 // entering the inset itself; i.e., move to after the inset.
2069 if (cur.paragraph().getFontSettings(
2070 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2071 cursorVisLeft(cur, true);
2074 case LFUN_FINISHED_RIGHT:
2075 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2076 // We're leaving an inset, going right. If the inset is RTL, we're
2077 // leaving from the front, so we should not move (remain at --- but
2078 // not in --- the inset). If the inset is LTR, move right, without
2079 // entering the inset itself; i.e., move to after the inset.
2080 if (!cur.paragraph().getFontSettings(
2081 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2082 cursorVisRight(cur, true);
2085 case LFUN_FINISHED_BACKWARD:
2086 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2089 case LFUN_FINISHED_FORWARD:
2090 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2092 cur.setCurrentFont();
2095 case LFUN_LAYOUT_PARAGRAPH: {
2097 params2string(cur.paragraph(), data);
2098 data = "show\n" + data;
2099 bv->showDialog("paragraph", data);
2103 case LFUN_PARAGRAPH_UPDATE: {
2105 params2string(cur.paragraph(), data);
2107 // Will the paragraph accept changes from the dialog?
2109 cur.inset().allowParagraphCustomization(cur.idx());
2111 data = "update " + convert<string>(accept) + '\n' + data;
2112 bv->updateDialog("paragraph", data);
2116 case LFUN_ACCENT_UMLAUT:
2117 case LFUN_ACCENT_CIRCUMFLEX:
2118 case LFUN_ACCENT_GRAVE:
2119 case LFUN_ACCENT_ACUTE:
2120 case LFUN_ACCENT_TILDE:
2121 case LFUN_ACCENT_CEDILLA:
2122 case LFUN_ACCENT_MACRON:
2123 case LFUN_ACCENT_DOT:
2124 case LFUN_ACCENT_UNDERDOT:
2125 case LFUN_ACCENT_UNDERBAR:
2126 case LFUN_ACCENT_CARON:
2127 case LFUN_ACCENT_BREVE:
2128 case LFUN_ACCENT_TIE:
2129 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2130 case LFUN_ACCENT_CIRCLE:
2131 case LFUN_ACCENT_OGONEK:
2132 theApp()->handleKeyFunc(cmd.action());
2133 if (!cmd.argument().empty())
2134 // FIXME: Are all these characters encoded in one byte in utf8?
2135 bv->translateAndInsert(cmd.argument()[0], this, cur);
2136 cur.screenUpdateFlags(Update::FitCursor);
2139 case LFUN_FLOAT_LIST_INSERT: {
2140 DocumentClass const & tclass = bv->buffer().params().documentClass();
2141 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2143 if (cur.selection())
2144 cutSelection(cur, true, false);
2145 breakParagraph(cur);
2147 if (cur.lastpos() != 0) {
2148 cursorBackward(cur);
2149 breakParagraph(cur);
2152 docstring const laystr = cur.inset().usePlainLayout() ?
2153 tclass.plainLayoutName() :
2154 tclass.defaultLayoutName();
2155 setLayout(cur, laystr);
2156 ParagraphParameters p;
2157 // FIXME If this call were replaced with one to clearParagraphParams(),
2158 // then we could get rid of this method altogether.
2159 setParagraphs(cur, p);
2160 // FIXME This should be simplified when InsetFloatList takes a
2161 // Buffer in its constructor.
2162 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2163 ifl->setBuffer(bv->buffer());
2164 insertInset(cur, ifl);
2167 lyxerr << "Non-existent float type: "
2168 << to_utf8(cmd.argument()) << endl;
2173 case LFUN_CHANGE_ACCEPT: {
2174 acceptOrRejectChanges(cur, ACCEPT);
2178 case LFUN_CHANGE_REJECT: {
2179 acceptOrRejectChanges(cur, REJECT);
2183 case LFUN_THESAURUS_ENTRY: {
2184 docstring arg = cmd.argument();
2186 arg = cur.selectionAsString(false);
2188 if (arg.size() > 100 || arg.empty()) {
2189 // Get word or selection
2190 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2191 arg = cur.selectionAsString(false);
2192 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2195 bv->showDialog("thesaurus", to_utf8(arg));
2199 case LFUN_SPELLING_ADD: {
2200 docstring word = from_utf8(cmd.getArg(0));
2203 word = cur.selectionAsString(false);
2205 if (word.size() > 100 || word.empty()) {
2206 // Get word or selection
2207 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2208 word = cur.selectionAsString(false);
2210 lang = const_cast<Language *>(cur.getFont().language());
2212 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2213 WordLangTuple wl(word, lang);
2214 theSpellChecker()->insert(wl);
2218 case LFUN_SPELLING_IGNORE: {
2219 docstring word = from_utf8(cmd.getArg(0));
2222 word = cur.selectionAsString(false);
2224 if (word.size() > 100 || word.empty()) {
2225 // Get word or selection
2226 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2227 word = cur.selectionAsString(false);
2229 lang = const_cast<Language *>(cur.getFont().language());
2231 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2232 WordLangTuple wl(word, lang);
2233 theSpellChecker()->accept(wl);
2237 case LFUN_SPELLING_REMOVE: {
2238 docstring word = from_utf8(cmd.getArg(0));
2241 word = cur.selectionAsString(false);
2243 if (word.size() > 100 || word.empty()) {
2244 // Get word or selection
2245 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2246 word = cur.selectionAsString(false);
2248 lang = const_cast<Language *>(cur.getFont().language());
2250 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2251 WordLangTuple wl(word, lang);
2252 theSpellChecker()->remove(wl);
2256 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2257 // Given data, an encoding of the ParagraphParameters
2258 // generated in the Paragraph dialog, this function sets
2259 // the current paragraph, or currently selected paragraphs,
2261 // NOTE: This function overrides all existing settings.
2262 setParagraphs(cur, cmd.argument());
2263 cur.message(_("Paragraph layout set"));
2267 case LFUN_PARAGRAPH_PARAMS: {
2268 // Given data, an encoding of the ParagraphParameters as we'd
2269 // find them in a LyX file, this function modifies the current paragraph,
2270 // or currently selected paragraphs.
2271 // NOTE: This function only modifies, and does not override, existing
2273 setParagraphs(cur, cmd.argument(), true);
2274 cur.message(_("Paragraph layout set"));
2279 if (cur.selection()) {
2280 cur.setSelection(false);
2283 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2284 // correct, but I'm not 100% sure -- dov, 071019
2285 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2289 case LFUN_OUTLINE_UP:
2290 outline(OutlineUp, cur);
2291 setCursor(cur, cur.pit(), 0);
2292 cur.forceBufferUpdate();
2296 case LFUN_OUTLINE_DOWN:
2297 outline(OutlineDown, cur);
2298 setCursor(cur, cur.pit(), 0);
2299 cur.forceBufferUpdate();
2303 case LFUN_OUTLINE_IN:
2304 outline(OutlineIn, cur);
2305 cur.forceBufferUpdate();
2309 case LFUN_OUTLINE_OUT:
2310 outline(OutlineOut, cur);
2311 cur.forceBufferUpdate();
2316 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2321 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2323 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2324 // Check for misspelled text
2325 // The redraw is useful because of the painting of
2326 // misspelled markers depends on the cursor position.
2327 // Trigger a redraw for cursor moves inside misspelled text.
2328 if (!cur.inTexted()) {
2329 // move from regular text to math
2330 needsUpdate = last_misspelled;
2331 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2332 // move inside regular text
2333 needsUpdate = last_misspelled
2334 || cur.paragraph().isMisspelled(cur.pos(), true);
2338 // FIXME: The cursor flag is reset two lines below
2339 // so we need to check here if some of the LFUN did touch that.
2340 // for now only Text::erase() and Text::backspace() do that.
2341 // The plan is to verify all the LFUNs and then to remove this
2342 // singleParUpdate boolean altogether.
2343 if (cur.result().screenUpdate() & Update::Force) {
2344 singleParUpdate = false;
2348 // FIXME: the following code should go in favor of fine grained
2349 // update flag treatment.
2350 if (singleParUpdate) {
2351 // Inserting characters does not change par height in general. So, try
2352 // to update _only_ this paragraph. BufferView will detect if a full
2353 // metrics update is needed anyway.
2354 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2358 && &oldTopSlice.inset() == &cur.inset()
2359 && oldTopSlice.idx() == cur.idx()
2360 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2361 && !cur.selection())
2362 // FIXME: it would be better if we could just do this
2364 //if (cur.result().update() != Update::FitCursor)
2365 // cur.noScreenUpdate();
2367 // But some LFUNs do not set Update::FitCursor when needed, so we
2368 // do it for all. This is not very harmfull as FitCursor will provoke
2369 // a full redraw only if needed but still, a proper review of all LFUN
2370 // should be done and this needsUpdate boolean can then be removed.
2371 cur.screenUpdateFlags(Update::FitCursor);
2373 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2377 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2378 FuncStatus & flag) const
2380 LBUFERR(this == cur.text());
2382 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2384 bool allow_in_passthru = false;
2385 InsetCode code = NO_CODE;
2387 switch (cmd.action()) {
2389 case LFUN_DEPTH_DECREMENT:
2390 enable = changeDepthAllowed(cur, DEC_DEPTH);
2393 case LFUN_DEPTH_INCREMENT:
2394 enable = changeDepthAllowed(cur, INC_DEPTH);
2398 // FIXME We really should not allow this to be put, e.g.,
2399 // in a footnote, or in ERT. But it would make sense in a
2400 // branch, so I'm not sure what to do.
2401 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2404 case LFUN_DIALOG_SHOW_NEW_INSET:
2405 if (cmd.argument() == "bibitem")
2406 code = BIBITEM_CODE;
2407 else if (cmd.argument() == "bibtex") {
2409 // not allowed in description items
2410 enable = !inDescriptionItem(cur);
2412 else if (cmd.argument() == "box")
2414 else if (cmd.argument() == "branch")
2416 else if (cmd.argument() == "citation")
2418 else if (cmd.argument() == "ert")
2420 else if (cmd.argument() == "external")
2421 code = EXTERNAL_CODE;
2422 else if (cmd.argument() == "float")
2424 else if (cmd.argument() == "graphics")
2425 code = GRAPHICS_CODE;
2426 else if (cmd.argument() == "href")
2427 code = HYPERLINK_CODE;
2428 else if (cmd.argument() == "include")
2429 code = INCLUDE_CODE;
2430 else if (cmd.argument() == "index")
2432 else if (cmd.argument() == "index_print")
2433 code = INDEX_PRINT_CODE;
2434 else if (cmd.argument() == "nomenclature")
2435 code = NOMENCL_CODE;
2436 else if (cmd.argument() == "nomencl_print")
2437 code = NOMENCL_PRINT_CODE;
2438 else if (cmd.argument() == "label")
2440 else if (cmd.argument() == "line")
2442 else if (cmd.argument() == "note")
2444 else if (cmd.argument() == "phantom")
2445 code = PHANTOM_CODE;
2446 else if (cmd.argument() == "ref")
2448 else if (cmd.argument() == "space")
2450 else if (cmd.argument() == "toc")
2452 else if (cmd.argument() == "vspace")
2454 else if (cmd.argument() == "wrap")
2456 else if (cmd.argument() == "listings")
2457 code = LISTINGS_CODE;
2460 case LFUN_ERT_INSERT:
2463 case LFUN_LISTING_INSERT:
2464 code = LISTINGS_CODE;
2465 // not allowed in description items
2466 enable = !inDescriptionItem(cur);
2468 case LFUN_FOOTNOTE_INSERT:
2471 case LFUN_TABULAR_INSERT:
2472 code = TABULAR_CODE;
2474 case LFUN_MARGINALNOTE_INSERT:
2477 case LFUN_FLOAT_INSERT:
2478 case LFUN_FLOAT_WIDE_INSERT:
2479 // FIXME: If there is a selection, we should check whether there
2480 // are floats in the selection, but this has performance issues, see
2481 // LFUN_CHANGE_ACCEPT/REJECT.
2483 if (inDescriptionItem(cur))
2484 // not allowed in description items
2487 InsetCode const inset_code = cur.inset().lyxCode();
2489 // algorithm floats cannot be put in another float
2490 if (to_utf8(cmd.argument()) == "algorithm") {
2491 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2495 // for figures and tables: only allow in another
2496 // float or wrap if it is of the same type and
2497 // not a subfloat already
2498 if(cur.inset().lyxCode() == code) {
2499 InsetFloat const & ins =
2500 static_cast<InsetFloat const &>(cur.inset());
2501 enable = ins.params().type == to_utf8(cmd.argument())
2502 && !ins.params().subfloat;
2503 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2504 InsetWrap const & ins =
2505 static_cast<InsetWrap const &>(cur.inset());
2506 enable = ins.params().type == to_utf8(cmd.argument());
2510 case LFUN_WRAP_INSERT:
2512 // not allowed in description items
2513 enable = !inDescriptionItem(cur);
2515 case LFUN_FLOAT_LIST_INSERT: {
2516 code = FLOAT_LIST_CODE;
2517 // not allowed in description items
2518 enable = !inDescriptionItem(cur);
2520 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2521 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2522 // make sure we know about such floats
2523 if (cit == floats.end() ||
2524 // and that we know how to generate a list of them
2525 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2526 flag.setUnknown(true);
2527 // probably not necessary, but...
2533 case LFUN_CAPTION_INSERT: {
2534 code = CAPTION_CODE;
2535 string arg = cmd.getArg(0);
2536 bool varia = arg != "LongTableNoNumber";
2537 if (cur.depth() > 0) {
2538 if (&cur[cur.depth() - 1].inset())
2539 varia = cur[cur.depth() - 1].inset().allowsCaptionVariation(arg);
2541 // not allowed in description items,
2542 // and in specific insets
2543 enable = !inDescriptionItem(cur)
2544 && (varia || arg.empty() || arg == "Standard");
2547 case LFUN_NOTE_INSERT:
2549 // in commands (sections etc.) and description items,
2550 // only Notes are allowed
2551 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2552 (!cur.paragraph().layout().isCommand()
2553 && !inDescriptionItem(cur)));
2555 case LFUN_FLEX_INSERT: {
2557 string s = cmd.getArg(0);
2559 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2560 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2561 il.lyxtype() != InsetLayout::CUSTOM &&
2562 il.lyxtype() != InsetLayout::ELEMENT &&
2563 il.lyxtype ()!= InsetLayout::STANDARD)
2567 case LFUN_BOX_INSERT:
2570 case LFUN_BRANCH_INSERT:
2572 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2573 && cur.buffer()->params().branchlist().empty())
2576 case LFUN_IPA_INSERT:
2579 case LFUN_PHANTOM_INSERT:
2580 code = PHANTOM_CODE;
2582 case LFUN_LABEL_INSERT:
2585 case LFUN_INFO_INSERT:
2588 case LFUN_ARGUMENT_INSERT: {
2590 allow_in_passthru = true;
2591 string const arg = cmd.getArg(0);
2596 Layout const & lay = cur.paragraph().layout();
2597 Layout::LaTeXArgMap args = lay.args();
2598 Layout::LaTeXArgMap::const_iterator const lait =
2600 if (lait != args.end()) {
2602 pit_type pit = cur.pit();
2603 pit_type lastpit = cur.pit();
2604 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2605 // In a sequence of "merged" environment layouts, we only allow
2606 // non-item arguments once.
2607 lastpit = cur.lastpit();
2608 // get the first paragraph in sequence with this layout
2609 depth_type const current_depth = cur.paragraph().params().depth();
2613 Paragraph cpar = pars_[pit - 1];
2614 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2620 for (; pit <= lastpit; ++pit) {
2621 if (pars_[pit].layout() != lay)
2623 InsetList::const_iterator it = pars_[pit].insetList().begin();
2624 InsetList::const_iterator end = pars_[pit].insetList().end();
2625 for (; it != end; ++it) {
2626 if (it->inset->lyxCode() == ARG_CODE) {
2627 InsetArgument const * ins =
2628 static_cast<InsetArgument const *>(it->inset);
2629 if (ins->name() == arg) {
2630 // we have this already
2641 case LFUN_INDEX_INSERT:
2644 case LFUN_INDEX_PRINT:
2645 code = INDEX_PRINT_CODE;
2646 // not allowed in description items
2647 enable = !inDescriptionItem(cur);
2649 case LFUN_NOMENCL_INSERT:
2650 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2654 code = NOMENCL_CODE;
2656 case LFUN_NOMENCL_PRINT:
2657 code = NOMENCL_PRINT_CODE;
2658 // not allowed in description items
2659 enable = !inDescriptionItem(cur);
2661 case LFUN_HYPERLINK_INSERT:
2662 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2666 code = HYPERLINK_CODE;
2668 case LFUN_IPAMACRO_INSERT: {
2669 string const arg = cmd.getArg(0);
2671 code = IPADECO_CODE;
2673 code = IPACHAR_CODE;
2676 case LFUN_QUOTE_INSERT:
2677 // always allow this, since we will inset a raw quote
2678 // if an inset is not allowed.
2680 case LFUN_SPECIALCHAR_INSERT:
2681 code = SPECIALCHAR_CODE;
2683 case LFUN_SPACE_INSERT:
2684 // slight hack: we know this is allowed in math mode
2688 case LFUN_PREVIEW_INSERT:
2689 code = PREVIEW_CODE;
2691 case LFUN_SCRIPT_INSERT:
2695 case LFUN_MATH_INSERT:
2696 case LFUN_MATH_AMS_MATRIX:
2697 case LFUN_MATH_MATRIX:
2698 case LFUN_MATH_DELIM:
2699 case LFUN_MATH_BIGDELIM:
2700 case LFUN_MATH_DISPLAY:
2701 case LFUN_MATH_MODE:
2702 case LFUN_MATH_MACRO:
2703 case LFUN_MATH_SUBSCRIPT:
2704 case LFUN_MATH_SUPERSCRIPT:
2705 code = MATH_HULL_CODE;
2708 case LFUN_REGEXP_MODE:
2709 code = MATH_HULL_CODE;
2710 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2713 case LFUN_INSET_MODIFY:
2714 // We need to disable this, because we may get called for a
2716 // InsetTabular::getStatus() -> InsetText::getStatus()
2717 // and we don't handle LFUN_INSET_MODIFY.
2721 case LFUN_FONT_EMPH:
2722 flag.setOnOff(fontinfo.emph() == FONT_ON);
2723 enable = !cur.paragraph().isPassThru();
2726 case LFUN_FONT_ITAL:
2727 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2728 enable = !cur.paragraph().isPassThru();
2731 case LFUN_FONT_NOUN:
2732 flag.setOnOff(fontinfo.noun() == FONT_ON);
2733 enable = !cur.paragraph().isPassThru();
2736 case LFUN_FONT_BOLD:
2737 case LFUN_FONT_BOLDSYMBOL:
2738 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2739 enable = !cur.paragraph().isPassThru();
2742 case LFUN_FONT_SANS:
2743 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2744 enable = !cur.paragraph().isPassThru();
2747 case LFUN_FONT_ROMAN:
2748 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2749 enable = !cur.paragraph().isPassThru();
2752 case LFUN_FONT_TYPEWRITER:
2753 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2754 enable = !cur.paragraph().isPassThru();
2759 enable = cur.selection();
2763 if (cmd.argument().empty()) {
2764 if (theClipboard().isInternal())
2765 enable = cap::numberOfSelections() > 0;
2767 enable = !theClipboard().empty();
2771 // we have an argument
2772 string const arg = to_utf8(cmd.argument());
2773 if (isStrUnsignedInt(arg)) {
2774 // it's a number and therefore means the internal stack
2775 unsigned int n = convert<unsigned int>(arg);
2776 enable = cap::numberOfSelections() > n;
2780 // explicit text type?
2781 if (arg == "html") {
2782 // Do not enable for PlainTextType, since some tidying in the
2783 // frontend is needed for HTML, which is too unsafe for plain text.
2784 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
2786 } else if (arg == "latex") {
2787 // LaTeX is usually not available on the clipboard with
2788 // the correct MIME type, but in plain text.
2789 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
2790 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
2794 // explicit graphics type?
2795 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2796 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2797 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2798 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2799 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2800 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2801 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2802 enable = theClipboard().hasGraphicsContents(type);
2811 case LFUN_CLIPBOARD_PASTE:
2812 case LFUN_CLIPBOARD_PASTE_SIMPLE:
2813 enable = !theClipboard().empty();
2816 case LFUN_PRIMARY_SELECTION_PASTE:
2817 enable = cur.selection() || !theSelection().empty();
2820 case LFUN_SELECTION_PASTE:
2821 enable = cap::selection();
2824 case LFUN_PARAGRAPH_MOVE_UP:
2825 enable = cur.pit() > 0 && !cur.selection();
2828 case LFUN_PARAGRAPH_MOVE_DOWN:
2829 enable = cur.pit() < cur.lastpit() && !cur.selection();
2832 case LFUN_CHANGE_ACCEPT:
2833 case LFUN_CHANGE_REJECT:
2834 // In principle, these LFUNs should only be enabled if there
2835 // is a change at the current position/in the current selection.
2836 // However, without proper optimizations, this will inevitably
2837 // result in unacceptable performance - just imagine a user who
2838 // wants to select the complete content of a long document.
2839 if (!cur.selection())
2840 enable = cur.paragraph().isChanged(cur.pos());
2842 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2847 case LFUN_OUTLINE_UP:
2848 case LFUN_OUTLINE_DOWN:
2849 case LFUN_OUTLINE_IN:
2850 case LFUN_OUTLINE_OUT:
2851 // FIXME: LyX is not ready for outlining within inset.
2852 enable = isMainText()
2853 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
2856 case LFUN_NEWLINE_INSERT:
2857 // LaTeX restrictions (labels or empty par)
2858 enable = (cur.pos() > cur.paragraph().beginOfBody());
2861 case LFUN_TAB_INSERT:
2862 case LFUN_TAB_DELETE:
2863 enable = cur.paragraph().isPassThru();
2866 case LFUN_SET_GRAPHICS_GROUP: {
2867 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2871 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2875 case LFUN_NEWPAGE_INSERT:
2876 // not allowed in description items
2877 code = NEWPAGE_CODE;
2878 enable = !inDescriptionItem(cur);
2881 case LFUN_DATE_INSERT: {
2882 string const format = cmd.argument().empty()
2883 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2884 enable = support::os::is_valid_strftime(format);
2889 enable = !cur.paragraph().isPassThru();
2890 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
2893 case LFUN_PARAGRAPH_BREAK:
2894 enable = cur.inset().getLayout().isMultiPar();
2897 case LFUN_SPELLING_ADD:
2898 case LFUN_SPELLING_IGNORE:
2899 case LFUN_SPELLING_REMOVE:
2900 enable = theSpellChecker();
2904 enable = !cur.inset().forcePlainLayout();
2907 case LFUN_ENVIRONMENT_SPLIT: {
2908 if (cur.paragraph().layout().isEnvironment()
2909 && cur.buffer()->params().documentClass().hasLayout(from_ascii("Separator"))) {
2917 case LFUN_LAYOUT_PARAGRAPH:
2918 case LFUN_PARAGRAPH_PARAMS:
2919 case LFUN_PARAGRAPH_PARAMS_APPLY:
2920 case LFUN_PARAGRAPH_UPDATE:
2921 enable = cur.inset().allowParagraphCustomization();
2924 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2925 case LFUN_ACCENT_ACUTE:
2926 case LFUN_ACCENT_BREVE:
2927 case LFUN_ACCENT_CARON:
2928 case LFUN_ACCENT_CEDILLA:
2929 case LFUN_ACCENT_CIRCLE:
2930 case LFUN_ACCENT_CIRCUMFLEX:
2931 case LFUN_ACCENT_DOT:
2932 case LFUN_ACCENT_GRAVE:
2933 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2934 case LFUN_ACCENT_MACRON:
2935 case LFUN_ACCENT_OGONEK:
2936 case LFUN_ACCENT_TIE:
2937 case LFUN_ACCENT_TILDE:
2938 case LFUN_ACCENT_UMLAUT:
2939 case LFUN_ACCENT_UNDERBAR:
2940 case LFUN_ACCENT_UNDERDOT:
2941 case LFUN_FONT_DEFAULT:
2942 case LFUN_FONT_FRAK:
2943 case LFUN_FONT_SIZE:
2944 case LFUN_FONT_STATE:
2945 case LFUN_FONT_UNDERLINE:
2946 case LFUN_FONT_STRIKEOUT:
2947 case LFUN_FONT_UULINE:
2948 case LFUN_FONT_UWAVE:
2949 case LFUN_TEXTSTYLE_APPLY:
2950 case LFUN_TEXTSTYLE_UPDATE:
2951 enable = !cur.paragraph().isPassThru();
2954 case LFUN_WORD_DELETE_FORWARD:
2955 case LFUN_WORD_DELETE_BACKWARD:
2956 case LFUN_LINE_DELETE:
2957 case LFUN_WORD_FORWARD:
2958 case LFUN_WORD_BACKWARD:
2959 case LFUN_WORD_RIGHT:
2960 case LFUN_WORD_LEFT:
2961 case LFUN_CHAR_FORWARD:
2962 case LFUN_CHAR_FORWARD_SELECT:
2963 case LFUN_CHAR_BACKWARD:
2964 case LFUN_CHAR_BACKWARD_SELECT:
2965 case LFUN_CHAR_LEFT:
2966 case LFUN_CHAR_LEFT_SELECT:
2967 case LFUN_CHAR_RIGHT:
2968 case LFUN_CHAR_RIGHT_SELECT:
2970 case LFUN_UP_SELECT:
2972 case LFUN_DOWN_SELECT:
2973 case LFUN_PARAGRAPH_UP_SELECT:
2974 case LFUN_PARAGRAPH_DOWN_SELECT:
2975 case LFUN_LINE_BEGIN_SELECT:
2976 case LFUN_LINE_END_SELECT:
2977 case LFUN_WORD_FORWARD_SELECT:
2978 case LFUN_WORD_BACKWARD_SELECT:
2979 case LFUN_WORD_RIGHT_SELECT:
2980 case LFUN_WORD_LEFT_SELECT:
2981 case LFUN_WORD_SELECT:
2982 case LFUN_SECTION_SELECT:
2983 case LFUN_BUFFER_BEGIN:
2984 case LFUN_BUFFER_END:
2985 case LFUN_BUFFER_BEGIN_SELECT:
2986 case LFUN_BUFFER_END_SELECT:
2987 case LFUN_INSET_BEGIN:
2988 case LFUN_INSET_END:
2989 case LFUN_INSET_BEGIN_SELECT:
2990 case LFUN_INSET_END_SELECT:
2991 case LFUN_INSET_SELECT_ALL:
2992 case LFUN_PARAGRAPH_UP:
2993 case LFUN_PARAGRAPH_DOWN:
2994 case LFUN_LINE_BEGIN:
2996 case LFUN_CHAR_DELETE_FORWARD:
2997 case LFUN_CHAR_DELETE_BACKWARD:
2998 case LFUN_WORD_UPCASE:
2999 case LFUN_WORD_LOWCASE:
3000 case LFUN_WORD_CAPITALIZE:
3001 case LFUN_CHARS_TRANSPOSE:
3002 case LFUN_SERVER_GET_XY:
3003 case LFUN_SERVER_SET_XY:
3004 case LFUN_SERVER_GET_LAYOUT:
3005 case LFUN_SELF_INSERT:
3006 case LFUN_UNICODE_INSERT:
3007 case LFUN_THESAURUS_ENTRY:
3009 // these are handled in our dispatch()
3013 case LFUN_INSET_INSERT: {
3014 string const type = cmd.getArg(0);
3015 if (type == "toc") {
3017 // not allowed in description items
3018 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3019 enable = !inDescriptionItem(cur);
3032 || !cur.inset().insetAllowed(code)
3033 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3036 flag.setEnabled(enable);
3041 void Text::pasteString(Cursor & cur, docstring const & clip,
3044 if (!clip.empty()) {
3047 insertStringAsParagraphs(cur, clip, cur.current_font);
3049 insertStringAsLines(cur, clip, cur.current_font);
3054 // FIXME: an item inset would make things much easier.
3055 bool Text::inDescriptionItem(Cursor & cur) const
3057 Paragraph & par = cur.paragraph();
3058 pos_type const pos = cur.pos();
3059 pos_type const body_pos = par.beginOfBody();
3061 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3062 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3063 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3066 return (pos < body_pos
3068 && (pos == 0 || par.getChar(pos - 1) != ' ')));