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();
657 case LFUN_CHAR_BACKWARD:
658 case LFUN_CHAR_BACKWARD_SELECT:
659 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
660 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
661 needsUpdate |= cursorBackward(cur);
663 if (!needsUpdate && oldTopSlice == cur.top()
664 && cur.boundary() == oldBoundary) {
666 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
668 // we will probably be moving out the inset, so we should execute
669 // the depm-mechanism, but only when the cursor has a place to
670 // go outside this inset, i.e. in a slice above.
671 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
672 // The cursor hasn't changed yet. To give the
673 // DEPM the possibility of doing something we must
674 // provide it with two different cursors.
676 dummy.pos() = cur.lastpos();
677 dummy.pit() = cur.lastpit();
678 if (cur.bv().checkDepm(dummy, cur))
679 cur.forceBufferUpdate();
685 case LFUN_CHAR_LEFT_SELECT:
686 if (lyxrc.visual_cursor) {
687 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
688 needsUpdate |= cursorVisLeft(cur);
689 if (!needsUpdate && oldTopSlice == cur.top()
690 && cur.boundary() == oldBoundary) {
692 cmd = FuncRequest(LFUN_FINISHED_LEFT);
695 if (reverseDirectionNeeded(cur)) {
696 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
697 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
699 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
700 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
707 case LFUN_CHAR_RIGHT:
708 case LFUN_CHAR_RIGHT_SELECT:
709 if (lyxrc.visual_cursor) {
710 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
711 needsUpdate |= cursorVisRight(cur);
712 if (!needsUpdate && oldTopSlice == cur.top()
713 && cur.boundary() == oldBoundary) {
715 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
718 if (reverseDirectionNeeded(cur)) {
719 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
720 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
722 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
723 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
732 case LFUN_DOWN_SELECT:
735 // stop/start the selection
736 bool select = cmd.action() == LFUN_DOWN_SELECT ||
737 cmd.action() == LFUN_UP_SELECT;
739 // move cursor up/down
740 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
741 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
743 if (!atFirstOrLastRow) {
744 needsUpdate |= cur.selHandle(select);
745 cur.selHandle(select);
746 cur.upDownInText(up, needsUpdate);
747 needsUpdate |= cur.beforeDispatchCursor().inMathed();
749 // if the cursor cannot be moved up or down do not remove
750 // the selection right now, but wait for the next dispatch.
752 needsUpdate |= cur.selHandle(select);
753 cur.upDownInText(up, needsUpdate);
760 case LFUN_PARAGRAPH_UP:
761 case LFUN_PARAGRAPH_UP_SELECT:
762 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
763 needsUpdate |= cursorUpParagraph(cur);
766 case LFUN_PARAGRAPH_DOWN:
767 case LFUN_PARAGRAPH_DOWN_SELECT:
768 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
769 needsUpdate |= cursorDownParagraph(cur);
772 case LFUN_LINE_BEGIN:
773 case LFUN_LINE_BEGIN_SELECT:
774 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
775 needsUpdate |= tm->cursorHome(cur);
779 case LFUN_LINE_END_SELECT:
780 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
781 needsUpdate |= tm->cursorEnd(cur);
784 case LFUN_SECTION_SELECT: {
785 Buffer const & buf = *cur.buffer();
786 pit_type const pit = cur.pit();
787 ParagraphList & pars = buf.text().paragraphs();
788 ParagraphList::iterator bgn = pars.begin();
789 // The first paragraph of the area to be selected:
790 ParagraphList::iterator start = boost::next(bgn, pit);
791 // The final paragraph of area to be selected:
792 ParagraphList::iterator finish = start;
793 ParagraphList::iterator end = pars.end();
795 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
796 if (thistoclevel == Layout::NOT_IN_TOC)
800 Cursor const old_cur = cur;
801 needsUpdate |= cur.selHandle(true);
803 // Move out (down) from this section header
807 // Seek the one (on same level) below
808 for (; finish != end; ++finish, ++cur.pit()) {
809 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
810 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
813 cur.pos() = cur.lastpos();
815 needsUpdate |= cur != old_cur;
819 case LFUN_WORD_RIGHT:
820 case LFUN_WORD_RIGHT_SELECT:
821 if (lyxrc.visual_cursor) {
822 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
823 needsUpdate |= cursorVisRightOneWord(cur);
824 if (!needsUpdate && oldTopSlice == cur.top()
825 && cur.boundary() == oldBoundary) {
827 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
830 if (reverseDirectionNeeded(cur)) {
831 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
832 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
834 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
835 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
842 case LFUN_WORD_FORWARD:
843 case LFUN_WORD_FORWARD_SELECT:
844 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
845 needsUpdate |= cursorForwardOneWord(cur);
847 if (!needsUpdate && oldTopSlice == cur.top()
848 && cur.boundary() == oldBoundary) {
850 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
852 // we will probably be moving out the inset, so we should execute
853 // the depm-mechanism, but only when the cursor has a place to
854 // go outside this inset, i.e. in a slice above.
855 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
856 && cur.pit() == cur.lastpit()) {
857 // The cursor hasn't changed yet. To give the
858 // DEPM the possibility of doing something we must
859 // provide it with two different cursors.
861 dummy.pos() = dummy.pit() = 0;
862 if (cur.bv().checkDepm(dummy, cur))
863 cur.forceBufferUpdate();
869 case LFUN_WORD_LEFT_SELECT:
870 if (lyxrc.visual_cursor) {
871 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
872 needsUpdate |= cursorVisLeftOneWord(cur);
873 if (!needsUpdate && oldTopSlice == cur.top()
874 && cur.boundary() == oldBoundary) {
876 cmd = FuncRequest(LFUN_FINISHED_LEFT);
879 if (reverseDirectionNeeded(cur)) {
880 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
881 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
883 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
884 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
891 case LFUN_WORD_BACKWARD:
892 case LFUN_WORD_BACKWARD_SELECT:
893 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
894 needsUpdate |= cursorBackwardOneWord(cur);
896 if (!needsUpdate && oldTopSlice == cur.top()
897 && cur.boundary() == oldBoundary) {
899 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
901 // we will probably be moving out the inset, so we should execute
902 // the depm-mechanism, but only when the cursor has a place to
903 // go outside this inset, i.e. in a slice above.
904 if (cur.depth() > 1 && cur.pos() == 0
906 // The cursor hasn't changed yet. To give the
907 // DEPM the possibility of doing something we must
908 // provide it with two different cursors.
910 dummy.pos() = cur.lastpos();
911 dummy.pit() = cur.lastpit();
912 if (cur.bv().checkDepm(dummy, cur))
913 cur.forceBufferUpdate();
918 case LFUN_WORD_SELECT: {
919 selectWord(cur, WHOLE_WORD);
920 finishChange(cur, true);
924 case LFUN_NEWLINE_INSERT: {
925 InsetNewlineParams inp;
926 docstring arg = cmd.argument();
927 if (arg == "linebreak")
928 inp.kind = InsetNewlineParams::LINEBREAK;
930 inp.kind = InsetNewlineParams::NEWLINE;
931 cap::replaceSelection(cur);
933 cur.insert(new InsetNewline(inp));
935 moveCursor(cur, false);
939 case LFUN_TAB_INSERT: {
940 bool const multi_par_selection = cur.selection() &&
941 cur.selBegin().pit() != cur.selEnd().pit();
942 if (multi_par_selection) {
943 // If there is a multi-paragraph selection, a tab is inserted
944 // at the beginning of each paragraph.
945 cur.recordUndoSelection();
946 pit_type const pit_end = cur.selEnd().pit();
947 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
948 pars_[pit].insertChar(0, '\t',
949 bv->buffer().params().trackChanges);
950 // Update the selection pos to make sure the selection does not
951 // change as the inserted tab will increase the logical pos.
952 if (cur.realAnchor().pit() == pit)
953 cur.realAnchor().forwardPos();
954 if (cur.pit() == pit)
959 // Maybe we shouldn't allow tabs within a line, because they
960 // are not (yet) aligned as one might do expect.
961 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
967 case LFUN_TAB_DELETE: {
968 bool const tc = bv->buffer().params().trackChanges;
969 if (cur.selection()) {
970 // If there is a selection, a tab (if present) is removed from
971 // the beginning of each paragraph.
972 cur.recordUndoSelection();
973 pit_type const pit_end = cur.selEnd().pit();
974 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
975 Paragraph & par = paragraphs()[pit];
978 char_type const c = par.getChar(0);
979 if (c == '\t' || c == ' ') {
980 // remove either 1 tab or 4 spaces.
981 int const n = (c == ' ' ? 4 : 1);
982 for (int i = 0; i < n
983 && !par.empty() && par.getChar(0) == c; ++i) {
984 if (cur.pit() == pit)
986 if (cur.realAnchor().pit() == pit
987 && cur.realAnchor().pos() > 0 )
988 cur.realAnchor().backwardPos();
989 par.eraseChar(0, tc);
995 // If there is no selection, try to remove a tab or some spaces
996 // before the position of the cursor.
997 Paragraph & par = paragraphs()[cur.pit()];
998 pos_type const pos = cur.pos();
1003 char_type const c = par.getChar(pos - 1);
1007 par.eraseChar(cur.pos(), tc);
1009 for (int n_spaces = 0;
1011 && par.getChar(cur.pos() - 1) == ' '
1015 par.eraseChar(cur.pos(), tc);
1022 case LFUN_CHAR_DELETE_FORWARD:
1023 if (!cur.selection()) {
1024 if (cur.pos() == cur.paragraph().size())
1025 // Par boundary, force full-screen update
1026 singleParUpdate = false;
1027 needsUpdate |= erase(cur);
1029 // It is possible to make it a lot faster still
1030 // just comment out the line below...
1032 cutSelection(cur, true, false);
1033 singleParUpdate = false;
1035 moveCursor(cur, false);
1038 case LFUN_CHAR_DELETE_BACKWARD:
1039 if (!cur.selection()) {
1040 if (bv->getIntl().getTransManager().backspace()) {
1041 // Par boundary, full-screen update
1043 singleParUpdate = false;
1044 needsUpdate |= backspace(cur);
1046 // It is possible to make it a lot faster still
1047 // just comment out the line below...
1050 cutSelection(cur, true, false);
1051 singleParUpdate = false;
1055 case LFUN_PARAGRAPH_BREAK:
1056 cap::replaceSelection(cur);
1057 breakParagraph(cur, cmd.argument() == "inverse");
1061 case LFUN_INSET_INSERT: {
1064 // We have to avoid triggering InstantPreview loading
1065 // before inserting into the document. See bug #5626.
1066 bool loaded = bv->buffer().isFullyLoaded();
1067 bv->buffer().setFullyLoaded(false);
1068 Inset * inset = createInset(&bv->buffer(), cmd);
1069 bv->buffer().setFullyLoaded(loaded);
1072 // FIXME (Abdel 01/02/2006):
1073 // What follows would be a partial fix for bug 2154:
1074 // http://www.lyx.org/trac/ticket/2154
1075 // This automatically put the label inset _after_ a
1076 // numbered section. It should be possible to extend the mechanism
1077 // to any kind of LateX environement.
1078 // The correct way to fix that bug would be at LateX generation.
1079 // I'll let the code here for reference as it could be used for some
1080 // other feature like "automatic labelling".
1082 Paragraph & par = pars_[cur.pit()];
1083 if (inset->lyxCode() == LABEL_CODE
1084 && !par.layout().counter.empty() {
1085 // Go to the end of the paragraph
1086 // Warning: Because of Change-Tracking, the last
1087 // position is 'size()' and not 'size()-1':
1088 cur.pos() = par.size();
1089 // Insert a new paragraph
1090 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1094 if (cur.selection())
1095 cutSelection(cur, true, false);
1097 if (inset->editable() && inset->asInsetText())
1098 inset->edit(cur, true);
1102 // trigger InstantPreview now
1103 if (inset->lyxCode() == EXTERNAL_CODE) {
1104 InsetExternal & ins =
1105 static_cast<InsetExternal &>(*inset);
1106 ins.updatePreview();
1113 case LFUN_INSET_DISSOLVE: {
1114 if (dissolveInset(cur)) {
1116 cur.forceBufferUpdate();
1121 case LFUN_SET_GRAPHICS_GROUP: {
1122 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1128 string id = to_utf8(cmd.argument());
1129 string grp = graphics::getGroupParams(bv->buffer(), id);
1130 InsetGraphicsParams tmp, inspar = ins->getParams();
1133 inspar.groupId = to_utf8(cmd.argument());
1135 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1136 tmp.filename = inspar.filename;
1140 ins->setParams(inspar);
1143 case LFUN_SPACE_INSERT:
1144 if (cur.paragraph().layout().free_spacing)
1145 insertChar(cur, ' ');
1147 doInsertInset(cur, this, cmd, false, false);
1150 moveCursor(cur, false);
1153 case LFUN_SPECIALCHAR_INSERT: {
1154 string const name = to_utf8(cmd.argument());
1155 if (name == "hyphenation")
1156 specialChar(cur, InsetSpecialChar::HYPHENATION);
1157 else if (name == "ligature-break")
1158 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1159 else if (name == "slash")
1160 specialChar(cur, InsetSpecialChar::SLASH);
1161 else if (name == "nobreakdash")
1162 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1163 else if (name == "dots")
1164 specialChar(cur, InsetSpecialChar::LDOTS);
1165 else if (name == "end-of-sentence")
1166 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1167 else if (name == "menu-separator")
1168 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1169 else if (name.empty())
1170 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1172 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1176 case LFUN_IPAMACRO_INSERT: {
1177 string const arg = cmd.getArg(0);
1178 if (arg == "deco") {
1179 // Open the inset, and move the current selection
1181 doInsertInset(cur, this, cmd, true, true);
1183 // Some insets are numbered, others are shown in the outline pane so
1184 // let's update the labels and the toc backend.
1185 cur.forceBufferUpdate();
1188 if (arg == "tone-falling")
1189 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1190 else if (arg == "tone-rising")
1191 ipaChar(cur, InsetIPAChar::TONE_RISING);
1192 else if (arg == "tone-high-rising")
1193 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1194 else if (arg == "tone-low-rising")
1195 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1196 else if (arg == "tone-high-rising-falling")
1197 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1198 else if (arg.empty())
1199 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1201 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1205 case LFUN_WORD_UPCASE:
1206 changeCase(cur, text_uppercase);
1209 case LFUN_WORD_LOWCASE:
1210 changeCase(cur, text_lowercase);
1213 case LFUN_WORD_CAPITALIZE:
1214 changeCase(cur, text_capitalization);
1217 case LFUN_CHARS_TRANSPOSE:
1218 charsTranspose(cur);
1222 cur.message(_("Paste"));
1223 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1224 cap::replaceSelection(cur);
1226 // without argument?
1227 string const arg = to_utf8(cmd.argument());
1229 bool tryGraphics = true;
1230 if (theClipboard().isInternal())
1231 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1232 else if (theClipboard().hasTextContents()) {
1233 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1234 true, Clipboard::AnyTextType))
1235 tryGraphics = false;
1237 if (tryGraphics && theClipboard().hasGraphicsContents())
1238 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1239 } else if (isStrUnsignedInt(arg)) {
1240 // we have a numerical argument
1241 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1242 convert<unsigned int>(arg));
1243 } else if (arg == "html" || arg == "latex") {
1244 Clipboard::TextType type = (arg == "html") ?
1245 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1246 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1248 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1250 type = Clipboard::PdfGraphicsType;
1251 else if (arg == "png")
1252 type = Clipboard::PngGraphicsType;
1253 else if (arg == "jpeg")
1254 type = Clipboard::JpegGraphicsType;
1255 else if (arg == "linkback")
1256 type = Clipboard::LinkBackGraphicsType;
1257 else if (arg == "emf")
1258 type = Clipboard::EmfGraphicsType;
1259 else if (arg == "wmf")
1260 type = Clipboard::WmfGraphicsType;
1262 // We used to assert, but couldn't the argument come from, say, the
1263 // minibuffer and just be mistyped?
1264 LYXERR0("Unrecognized graphics type: " << arg);
1266 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1269 bv->buffer().errors("Paste");
1270 cur.clearSelection(); // bug 393
1276 cutSelection(cur, true, true);
1277 cur.message(_("Cut"));
1282 cur.message(_("Copy"));
1285 case LFUN_SERVER_GET_XY:
1286 cur.message(from_utf8(
1287 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1288 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1291 case LFUN_SERVER_SET_XY: {
1294 istringstream is(to_utf8(cmd.argument()));
1297 lyxerr << "SETXY: Could not parse coordinates in '"
1298 << to_utf8(cmd.argument()) << endl;
1300 tm->setCursorFromCoordinates(cur, x, y);
1304 case LFUN_SERVER_GET_LAYOUT:
1305 cur.message(cur.paragraph().layout().name());
1309 docstring layout = cmd.argument();
1310 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1312 Paragraph const & para = cur.paragraph();
1313 docstring const old_layout = para.layout().name();
1314 DocumentClass const & tclass = bv->buffer().params().documentClass();
1317 layout = tclass.defaultLayoutName();
1319 if (owner_->forcePlainLayout())
1320 // in this case only the empty layout is allowed
1321 layout = tclass.plainLayoutName();
1322 else if (para.usePlainLayout()) {
1323 // in this case, default layout maps to empty layout
1324 if (layout == tclass.defaultLayoutName())
1325 layout = tclass.plainLayoutName();
1327 // otherwise, the empty layout maps to the default
1328 if (layout == tclass.plainLayoutName())
1329 layout = tclass.defaultLayoutName();
1332 bool hasLayout = tclass.hasLayout(layout);
1334 // If the entry is obsolete, use the new one instead.
1336 docstring const & obs = tclass[layout].obsoleted_by();
1342 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1343 from_utf8(N_(" not known")));
1347 bool change_layout = (old_layout != layout);
1349 if (!change_layout && cur.selection() &&
1350 cur.selBegin().pit() != cur.selEnd().pit())
1352 pit_type spit = cur.selBegin().pit();
1353 pit_type epit = cur.selEnd().pit() + 1;
1354 while (spit != epit) {
1355 if (pars_[spit].layout().name() != old_layout) {
1356 change_layout = true;
1364 setLayout(cur, layout);
1366 Layout::LaTeXArgMap args = tclass[layout].args();
1367 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1368 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1369 for (; lait != laend; ++lait) {
1370 Layout::latexarg arg = (*lait).second;
1371 if (arg.autoinsert) {
1372 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1380 case LFUN_ENVIRONMENT_SPLIT: {
1381 Paragraph const & para = cur.paragraph();
1382 docstring const layout = para.layout().name();
1384 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1385 bool const morecont = cur.lastpos() > cur.pos();
1386 lyx::dispatch(FuncRequest(LFUN_LAYOUT, "Separator"));
1387 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1389 lyx::dispatch(FuncRequest(LFUN_DOWN));
1390 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1395 case LFUN_CLIPBOARD_PASTE:
1396 cap::replaceSelection(cur);
1397 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1398 cmd.argument() == "paragraph");
1399 bv->buffer().errors("Paste");
1402 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1403 cap::replaceSelection(cur);
1404 pasteSimpleText(cur, cmd.argument() == "paragraph");
1407 case LFUN_PRIMARY_SELECTION_PASTE:
1408 cap::replaceSelection(cur);
1409 pasteString(cur, theSelection().get(),
1410 cmd.argument() == "paragraph");
1413 case LFUN_SELECTION_PASTE:
1414 // Copy the selection buffer to the clipboard stack,
1415 // because we want it to appear in the "Edit->Paste
1417 cap::replaceSelection(cur);
1418 cap::copySelectionToStack();
1419 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1420 bv->buffer().errors("Paste");
1423 case LFUN_UNICODE_INSERT: {
1424 if (cmd.argument().empty())
1426 docstring hexstring = cmd.argument();
1427 if (isHex(hexstring)) {
1428 char_type c = hexToInt(hexstring);
1429 if (c >= 32 && c < 0x10ffff) {
1430 lyxerr << "Inserting c: " << c << endl;
1431 docstring s = docstring(1, c);
1432 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1438 case LFUN_QUOTE_INSERT: {
1439 cap::replaceSelection(cur);
1442 Paragraph const & par = cur.paragraph();
1443 pos_type pos = cur.pos();
1444 // Ignore deleted text before cursor
1445 while (pos > 0 && par.isDeleted(pos - 1))
1448 BufferParams const & bufparams = bv->buffer().params();
1450 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1451 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1453 if (allow_inset_quote) {
1455 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1456 c = par.getChar(pos - 1);
1457 string const arg = to_utf8(cmd.argument());
1458 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1459 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1460 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1463 // The cursor might have been invalidated by the replaceSelection.
1464 cur.buffer()->changed(true);
1465 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1470 case LFUN_DATE_INSERT: {
1471 string const format = cmd.argument().empty()
1472 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1473 string const time = formatted_time(current_time(), format);
1474 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1478 case LFUN_MOUSE_TRIPLE:
1479 if (cmd.button() == mouse_button::button1) {
1480 tm->cursorHome(cur);
1488 case LFUN_MOUSE_DOUBLE:
1489 if (cmd.button() == mouse_button::button1) {
1490 selectWord(cur, WHOLE_WORD_STRICT);
1495 // Single-click on work area
1496 case LFUN_MOUSE_PRESS: {
1497 // We are not marking a selection with the keyboard in any case.
1498 Cursor & bvcur = cur.bv().cursor();
1499 bvcur.setMark(false);
1500 switch (cmd.button()) {
1501 case mouse_button::button1:
1503 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1504 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1505 if (bvcur.wordSelection())
1506 selectWord(bvcur, WHOLE_WORD);
1509 case mouse_button::button2:
1510 // Middle mouse pasting.
1511 bv->mouseSetCursor(cur);
1513 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1514 "selection-paste ; primary-selection-paste paragraph"));
1515 cur.noScreenUpdate();
1518 case mouse_button::button3: {
1519 // Don't do anything if we right-click a
1520 // selection, a context menu will popup.
1521 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1522 && cur < bvcur.selectionEnd()) {
1523 cur.noScreenUpdate();
1526 if (!bv->mouseSetCursor(cur, false))
1527 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1533 } // switch (cmd.button())
1536 case LFUN_MOUSE_MOTION: {
1537 // Mouse motion with right or middle mouse do nothing for now.
1538 if (cmd.button() != mouse_button::button1) {
1539 cur.noScreenUpdate();
1542 // ignore motions deeper nested than the real anchor
1543 Cursor & bvcur = cur.bv().cursor();
1544 if (!bvcur.realAnchor().hasPart(cur)) {
1548 CursorSlice old = bvcur.top();
1550 int const wh = bv->workHeight();
1551 int const y = max(0, min(wh - 1, cmd.y()));
1553 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1554 cur.setTargetX(cmd.x());
1556 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1557 else if (cmd.y() < 0)
1558 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1559 // This is to allow jumping over large insets
1560 if (cur.top() == old) {
1562 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1563 else if (cmd.y() < 0)
1564 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1566 // We continue with our existing selection or start a new one, so don't
1567 // reset the anchor.
1568 bvcur.setCursor(cur);
1569 bvcur.setSelection(true);
1570 if (cur.top() == old) {
1571 // We didn't move one iota, so no need to update the screen.
1572 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1573 //cur.noScreenUpdate();
1579 case LFUN_MOUSE_RELEASE:
1580 switch (cmd.button()) {
1581 case mouse_button::button1:
1582 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1583 // If there is a new selection, update persistent selection;
1584 // otherwise, single click does not clear persistent selection
1586 if (cur.selection()) {
1587 // Finish selection. If double click,
1588 // cur is moved to the end of word by
1589 // selectWord but bvcur is current
1591 cur.bv().cursor().setSelection();
1592 // We might have removed an empty but drawn selection
1593 // (probably a margin)
1594 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1596 cur.noScreenUpdate();
1597 // FIXME: We could try to handle drag and drop of selection here.
1600 case mouse_button::button2:
1601 // Middle mouse pasting is handled at mouse press time,
1602 // see LFUN_MOUSE_PRESS.
1603 cur.noScreenUpdate();
1606 case mouse_button::button3:
1607 // Cursor was set at LFUN_MOUSE_PRESS time.
1608 // FIXME: If there is a selection we could try to handle a special
1609 // drag & drop context menu.
1610 cur.noScreenUpdate();
1613 case mouse_button::none:
1614 case mouse_button::button4:
1615 case mouse_button::button5:
1617 } // switch (cmd.button())
1621 case LFUN_SELF_INSERT: {
1622 if (cmd.argument().empty())
1625 // Automatically delete the currently selected
1626 // text and replace it with what is being
1627 // typed in now. Depends on lyxrc settings
1628 // "auto_region_delete", which defaults to
1631 if (lyxrc.auto_region_delete && cur.selection())
1632 cutSelection(cur, false, false);
1634 cur.clearSelection();
1636 docstring::const_iterator cit = cmd.argument().begin();
1637 docstring::const_iterator const end = cmd.argument().end();
1638 for (; cit != end; ++cit)
1639 bv->translateAndInsert(*cit, this, cur);
1642 moveCursor(cur, false);
1643 cur.markNewWordPosition();
1644 bv->bookmarkEditPosition();
1648 case LFUN_HYPERLINK_INSERT: {
1649 InsetCommandParams p(HYPERLINK_CODE);
1651 if (cur.selection()) {
1652 content = cur.selectionAsString(false);
1653 cutSelection(cur, true, false);
1655 p["target"] = (cmd.argument().empty()) ?
1656 content : cmd.argument();
1657 string const data = InsetCommand::params2string(p);
1658 if (p["target"].empty()) {
1659 bv->showDialog("href", data);
1661 FuncRequest fr(LFUN_INSET_INSERT, data);
1667 case LFUN_LABEL_INSERT: {
1668 InsetCommandParams p(LABEL_CODE);
1669 // Try to generate a valid label
1670 p["name"] = (cmd.argument().empty()) ?
1671 cur.getPossibleLabel() :
1673 string const data = InsetCommand::params2string(p);
1675 if (cmd.argument().empty()) {
1676 bv->showDialog("label", data);
1678 FuncRequest fr(LFUN_INSET_INSERT, data);
1684 case LFUN_INFO_INSERT: {
1686 if (cmd.argument().empty() && cur.selection()) {
1687 // if command argument is empty use current selection as parameter.
1688 docstring ds = cur.selectionAsString(false);
1689 cutSelection(cur, true, false);
1690 FuncRequest cmd0(cmd, ds);
1691 inset = createInset(cur.buffer(), cmd0);
1693 inset = createInset(cur.buffer(), cmd);
1698 insertInset(cur, inset);
1702 case LFUN_CAPTION_INSERT:
1703 case LFUN_FOOTNOTE_INSERT:
1704 case LFUN_NOTE_INSERT:
1705 case LFUN_BOX_INSERT:
1706 case LFUN_BRANCH_INSERT:
1707 case LFUN_PHANTOM_INSERT:
1708 case LFUN_ERT_INSERT:
1709 case LFUN_LISTING_INSERT:
1710 case LFUN_MARGINALNOTE_INSERT:
1711 case LFUN_ARGUMENT_INSERT:
1712 case LFUN_INDEX_INSERT:
1713 case LFUN_PREVIEW_INSERT:
1714 case LFUN_SCRIPT_INSERT:
1715 case LFUN_IPA_INSERT:
1716 // Open the inset, and move the current selection
1718 doInsertInset(cur, this, cmd, true, true);
1720 // Some insets are numbered, others are shown in the outline pane so
1721 // let's update the labels and the toc backend.
1722 cur.forceBufferUpdate();
1725 case LFUN_FLEX_INSERT: {
1726 // Open the inset, and move the current selection
1728 bool const sel = cur.selection();
1729 doInsertInset(cur, this, cmd, true, true);
1730 // Insert auto-insert arguments
1731 bool autoargs = false;
1732 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1733 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1734 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1735 for (; lait != laend; ++lait) {
1736 Layout::latexarg arg = (*lait).second;
1737 if (arg.autoinsert) {
1738 // The cursor might have been invalidated by the replaceSelection.
1739 cur.buffer()->changed(true);
1740 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1747 cur.leaveInset(cur.inset());
1750 // Some insets are numbered, others are shown in the outline pane so
1751 // let's update the labels and the toc backend.
1752 cur.forceBufferUpdate();
1756 case LFUN_TABULAR_INSERT:
1757 // if there were no arguments, just open the dialog
1758 if (doInsertInset(cur, this, cmd, false, true))
1761 bv->showDialog("tabularcreate");
1765 case LFUN_FLOAT_INSERT:
1766 case LFUN_FLOAT_WIDE_INSERT:
1767 case LFUN_WRAP_INSERT: {
1768 // will some content be moved into the inset?
1769 bool const content = cur.selection();
1770 // does the content consist of multiple paragraphs?
1771 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1773 doInsertInset(cur, this, cmd, true, true);
1776 // If some single-par content is moved into the inset,
1777 // doInsertInset puts the cursor outside the inset.
1778 // To insert the caption we put it back into the inset.
1779 // FIXME cleanup doInsertInset to avoid such dances!
1780 if (content && singlepar)
1783 ParagraphList & pars = cur.text()->paragraphs();
1785 DocumentClass const & tclass = bv->buffer().params().documentClass();
1787 // add a separate paragraph for the caption inset
1788 pars.push_back(Paragraph());
1789 pars.back().setInsetOwner(&cur.text()->inset());
1790 pars.back().setPlainOrDefaultLayout(tclass);
1791 int cap_pit = pars.size() - 1;
1793 // if an empty inset was created, we create an additional empty
1794 // paragraph at the bottom so that the user can choose where to put
1795 // the graphics (or table).
1797 pars.push_back(Paragraph());
1798 pars.back().setInsetOwner(&cur.text()->inset());
1799 pars.back().setPlainOrDefaultLayout(tclass);
1802 // reposition the cursor to the caption
1803 cur.pit() = cap_pit;
1805 // FIXME: This Text/Cursor dispatch handling is a mess!
1806 // We cannot use Cursor::dispatch here it needs access to up to
1808 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1809 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1810 cur.forceBufferUpdate();
1811 cur.screenUpdateFlags(Update::Force);
1812 // FIXME: When leaving the Float (or Wrap) inset we should
1813 // delete any empty paragraph left above or below the
1818 case LFUN_NOMENCL_INSERT: {
1819 InsetCommandParams p(NOMENCL_CODE);
1820 if (cmd.argument().empty())
1821 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1823 p["symbol"] = cmd.argument();
1824 string const data = InsetCommand::params2string(p);
1825 bv->showDialog("nomenclature", data);
1829 case LFUN_INDEX_PRINT: {
1830 InsetCommandParams p(INDEX_PRINT_CODE);
1831 if (cmd.argument().empty())
1832 p["type"] = from_ascii("idx");
1834 p["type"] = cmd.argument();
1835 string const data = InsetCommand::params2string(p);
1836 FuncRequest fr(LFUN_INSET_INSERT, data);
1841 case LFUN_NOMENCL_PRINT:
1842 case LFUN_NEWPAGE_INSERT:
1844 doInsertInset(cur, this, cmd, false, false);
1848 case LFUN_DEPTH_DECREMENT:
1849 changeDepth(cur, DEC_DEPTH);
1852 case LFUN_DEPTH_INCREMENT:
1853 changeDepth(cur, INC_DEPTH);
1856 case LFUN_MATH_DISPLAY:
1857 mathDispatch(cur, cmd, true);
1860 case LFUN_REGEXP_MODE:
1861 regexpDispatch(cur, cmd);
1864 case LFUN_MATH_MODE:
1865 if (cmd.argument() == "on")
1866 // don't pass "on" as argument
1867 // (it would appear literally in the first cell)
1868 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1870 mathDispatch(cur, cmd, false);
1873 case LFUN_MATH_MACRO:
1874 if (cmd.argument().empty())
1875 cur.errorMessage(from_utf8(N_("Missing argument")));
1878 string s = to_utf8(cmd.argument());
1879 string const s1 = token(s, ' ', 1);
1880 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1881 string const s2 = token(s, ' ', 2);
1882 MacroType type = MacroTypeNewcommand;
1884 type = MacroTypeDef;
1885 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1886 from_utf8(token(s, ' ', 0)), nargs, false, type);
1887 inset->setBuffer(bv->buffer());
1888 insertInset(cur, inset);
1890 // enter macro inset and select the name
1892 cur.top().pos() = cur.top().lastpos();
1894 cur.setSelection(true);
1895 cur.top().pos() = 0;
1899 // passthrough hat and underscore outside mathed:
1900 case LFUN_MATH_SUBSCRIPT:
1901 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1903 case LFUN_MATH_SUPERSCRIPT:
1904 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1907 case LFUN_MATH_INSERT:
1908 case LFUN_MATH_AMS_MATRIX:
1909 case LFUN_MATH_MATRIX:
1910 case LFUN_MATH_DELIM:
1911 case LFUN_MATH_BIGDELIM: {
1913 cap::replaceSelection(cur);
1914 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1915 checkAndActivateInset(cur, true);
1916 LASSERT(cur.inMathed(), break);
1921 case LFUN_FONT_EMPH: {
1922 Font font(ignore_font, ignore_language);
1923 font.fontInfo().setEmph(FONT_TOGGLE);
1924 toggleAndShow(cur, this, font);
1928 case LFUN_FONT_ITAL: {
1929 Font font(ignore_font, ignore_language);
1930 font.fontInfo().setShape(ITALIC_SHAPE);
1931 toggleAndShow(cur, this, font);
1935 case LFUN_FONT_BOLD:
1936 case LFUN_FONT_BOLDSYMBOL: {
1937 Font font(ignore_font, ignore_language);
1938 font.fontInfo().setSeries(BOLD_SERIES);
1939 toggleAndShow(cur, this, font);
1943 case LFUN_FONT_NOUN: {
1944 Font font(ignore_font, ignore_language);
1945 font.fontInfo().setNoun(FONT_TOGGLE);
1946 toggleAndShow(cur, this, font);
1950 case LFUN_FONT_TYPEWRITER: {
1951 Font font(ignore_font, ignore_language);
1952 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1953 toggleAndShow(cur, this, font);
1957 case LFUN_FONT_SANS: {
1958 Font font(ignore_font, ignore_language);
1959 font.fontInfo().setFamily(SANS_FAMILY);
1960 toggleAndShow(cur, this, font);
1964 case LFUN_FONT_ROMAN: {
1965 Font font(ignore_font, ignore_language);
1966 font.fontInfo().setFamily(ROMAN_FAMILY);
1967 toggleAndShow(cur, this, font);
1971 case LFUN_FONT_DEFAULT: {
1972 Font font(inherit_font, ignore_language);
1973 toggleAndShow(cur, this, font);
1977 case LFUN_FONT_STRIKEOUT: {
1978 Font font(ignore_font, ignore_language);
1979 font.fontInfo().setStrikeout(FONT_TOGGLE);
1980 toggleAndShow(cur, this, font);
1984 case LFUN_FONT_UULINE: {
1985 Font font(ignore_font, ignore_language);
1986 font.fontInfo().setUuline(FONT_TOGGLE);
1987 toggleAndShow(cur, this, font);
1991 case LFUN_FONT_UWAVE: {
1992 Font font(ignore_font, ignore_language);
1993 font.fontInfo().setUwave(FONT_TOGGLE);
1994 toggleAndShow(cur, this, font);
1998 case LFUN_FONT_UNDERLINE: {
1999 Font font(ignore_font, ignore_language);
2000 font.fontInfo().setUnderbar(FONT_TOGGLE);
2001 toggleAndShow(cur, this, font);
2005 case LFUN_FONT_SIZE: {
2006 Font font(ignore_font, ignore_language);
2007 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2008 toggleAndShow(cur, this, font);
2012 case LFUN_LANGUAGE: {
2013 string const lang_arg = cmd.getArg(0);
2014 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2015 Language const * lang =
2016 reset ? reset_language
2017 : languages.getLanguage(lang_arg);
2018 // we allow reset_language, which is 0, but only if it
2019 // was requested via empty or "reset" arg.
2020 if (!lang && !reset)
2022 bool const toggle = (cmd.getArg(1) != "set");
2023 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2024 Font font(ignore_font, lang);
2025 toggleAndShow(cur, this, font, toggle);
2029 case LFUN_TEXTSTYLE_APPLY:
2030 toggleAndShow(cur, this, freefont, toggleall);
2031 cur.message(_("Character set"));
2034 // Set the freefont using the contents of \param data dispatched from
2035 // the frontends and apply it at the current cursor location.
2036 case LFUN_TEXTSTYLE_UPDATE: {
2039 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2042 toggleAndShow(cur, this, freefont, toggleall);
2043 cur.message(_("Character set"));
2045 lyxerr << "Argument not ok";
2050 case LFUN_FINISHED_LEFT:
2051 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2052 // We're leaving an inset, going left. If the inset is LTR, we're
2053 // leaving from the front, so we should not move (remain at --- but
2054 // not in --- the inset). If the inset is RTL, move left, without
2055 // entering the inset itself; i.e., move to after the inset.
2056 if (cur.paragraph().getFontSettings(
2057 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2058 cursorVisLeft(cur, true);
2061 case LFUN_FINISHED_RIGHT:
2062 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2063 // We're leaving an inset, going right. If the inset is RTL, we're
2064 // leaving from the front, so we should not move (remain at --- but
2065 // not in --- the inset). If the inset is LTR, move right, without
2066 // entering the inset itself; i.e., move to after the inset.
2067 if (!cur.paragraph().getFontSettings(
2068 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2069 cursorVisRight(cur, true);
2072 case LFUN_FINISHED_BACKWARD:
2073 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2076 case LFUN_FINISHED_FORWARD:
2077 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2079 cur.setCurrentFont();
2082 case LFUN_LAYOUT_PARAGRAPH: {
2084 params2string(cur.paragraph(), data);
2085 data = "show\n" + data;
2086 bv->showDialog("paragraph", data);
2090 case LFUN_PARAGRAPH_UPDATE: {
2092 params2string(cur.paragraph(), data);
2094 // Will the paragraph accept changes from the dialog?
2096 cur.inset().allowParagraphCustomization(cur.idx());
2098 data = "update " + convert<string>(accept) + '\n' + data;
2099 bv->updateDialog("paragraph", data);
2103 case LFUN_ACCENT_UMLAUT:
2104 case LFUN_ACCENT_CIRCUMFLEX:
2105 case LFUN_ACCENT_GRAVE:
2106 case LFUN_ACCENT_ACUTE:
2107 case LFUN_ACCENT_TILDE:
2108 case LFUN_ACCENT_CEDILLA:
2109 case LFUN_ACCENT_MACRON:
2110 case LFUN_ACCENT_DOT:
2111 case LFUN_ACCENT_UNDERDOT:
2112 case LFUN_ACCENT_UNDERBAR:
2113 case LFUN_ACCENT_CARON:
2114 case LFUN_ACCENT_BREVE:
2115 case LFUN_ACCENT_TIE:
2116 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2117 case LFUN_ACCENT_CIRCLE:
2118 case LFUN_ACCENT_OGONEK:
2119 theApp()->handleKeyFunc(cmd.action());
2120 if (!cmd.argument().empty())
2121 // FIXME: Are all these characters encoded in one byte in utf8?
2122 bv->translateAndInsert(cmd.argument()[0], this, cur);
2123 cur.screenUpdateFlags(Update::FitCursor);
2126 case LFUN_FLOAT_LIST_INSERT: {
2127 DocumentClass const & tclass = bv->buffer().params().documentClass();
2128 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2130 if (cur.selection())
2131 cutSelection(cur, true, false);
2132 breakParagraph(cur);
2134 if (cur.lastpos() != 0) {
2135 cursorBackward(cur);
2136 breakParagraph(cur);
2139 docstring const laystr = cur.inset().usePlainLayout() ?
2140 tclass.plainLayoutName() :
2141 tclass.defaultLayoutName();
2142 setLayout(cur, laystr);
2143 ParagraphParameters p;
2144 // FIXME If this call were replaced with one to clearParagraphParams(),
2145 // then we could get rid of this method altogether.
2146 setParagraphs(cur, p);
2147 // FIXME This should be simplified when InsetFloatList takes a
2148 // Buffer in its constructor.
2149 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2150 ifl->setBuffer(bv->buffer());
2151 insertInset(cur, ifl);
2154 lyxerr << "Non-existent float type: "
2155 << to_utf8(cmd.argument()) << endl;
2160 case LFUN_CHANGE_ACCEPT: {
2161 acceptOrRejectChanges(cur, ACCEPT);
2165 case LFUN_CHANGE_REJECT: {
2166 acceptOrRejectChanges(cur, REJECT);
2170 case LFUN_THESAURUS_ENTRY: {
2171 docstring arg = cmd.argument();
2173 arg = cur.selectionAsString(false);
2175 if (arg.size() > 100 || arg.empty()) {
2176 // Get word or selection
2177 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2178 arg = cur.selectionAsString(false);
2179 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2182 bv->showDialog("thesaurus", to_utf8(arg));
2186 case LFUN_SPELLING_ADD: {
2187 docstring word = from_utf8(cmd.getArg(0));
2190 word = cur.selectionAsString(false);
2192 if (word.size() > 100 || word.empty()) {
2193 // Get word or selection
2194 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2195 word = cur.selectionAsString(false);
2197 lang = const_cast<Language *>(cur.getFont().language());
2199 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2200 WordLangTuple wl(word, lang);
2201 theSpellChecker()->insert(wl);
2205 case LFUN_SPELLING_IGNORE: {
2206 docstring word = from_utf8(cmd.getArg(0));
2209 word = cur.selectionAsString(false);
2211 if (word.size() > 100 || word.empty()) {
2212 // Get word or selection
2213 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2214 word = cur.selectionAsString(false);
2216 lang = const_cast<Language *>(cur.getFont().language());
2218 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2219 WordLangTuple wl(word, lang);
2220 theSpellChecker()->accept(wl);
2224 case LFUN_SPELLING_REMOVE: {
2225 docstring word = from_utf8(cmd.getArg(0));
2228 word = cur.selectionAsString(false);
2230 if (word.size() > 100 || word.empty()) {
2231 // Get word or selection
2232 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2233 word = cur.selectionAsString(false);
2235 lang = const_cast<Language *>(cur.getFont().language());
2237 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2238 WordLangTuple wl(word, lang);
2239 theSpellChecker()->remove(wl);
2243 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2244 // Given data, an encoding of the ParagraphParameters
2245 // generated in the Paragraph dialog, this function sets
2246 // the current paragraph, or currently selected paragraphs,
2248 // NOTE: This function overrides all existing settings.
2249 setParagraphs(cur, cmd.argument());
2250 cur.message(_("Paragraph layout set"));
2254 case LFUN_PARAGRAPH_PARAMS: {
2255 // Given data, an encoding of the ParagraphParameters as we'd
2256 // find them in a LyX file, this function modifies the current paragraph,
2257 // or currently selected paragraphs.
2258 // NOTE: This function only modifies, and does not override, existing
2260 setParagraphs(cur, cmd.argument(), true);
2261 cur.message(_("Paragraph layout set"));
2266 if (cur.selection()) {
2267 cur.setSelection(false);
2270 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2271 // correct, but I'm not 100% sure -- dov, 071019
2272 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2276 case LFUN_OUTLINE_UP:
2277 outline(OutlineUp, cur);
2278 setCursor(cur, cur.pit(), 0);
2279 cur.forceBufferUpdate();
2283 case LFUN_OUTLINE_DOWN:
2284 outline(OutlineDown, cur);
2285 setCursor(cur, cur.pit(), 0);
2286 cur.forceBufferUpdate();
2290 case LFUN_OUTLINE_IN:
2291 outline(OutlineIn, cur);
2292 cur.forceBufferUpdate();
2296 case LFUN_OUTLINE_OUT:
2297 outline(OutlineOut, cur);
2298 cur.forceBufferUpdate();
2303 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2308 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2310 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2311 // Check for misspelled text
2312 // The redraw is useful because of the painting of
2313 // misspelled markers depends on the cursor position.
2314 // Trigger a redraw for cursor moves inside misspelled text.
2315 if (!cur.inTexted()) {
2316 // move from regular text to math
2317 needsUpdate = last_misspelled;
2318 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2319 // move inside regular text
2320 needsUpdate = last_misspelled
2321 || cur.paragraph().isMisspelled(cur.pos(), true);
2325 // FIXME: The cursor flag is reset two lines below
2326 // so we need to check here if some of the LFUN did touch that.
2327 // for now only Text::erase() and Text::backspace() do that.
2328 // The plan is to verify all the LFUNs and then to remove this
2329 // singleParUpdate boolean altogether.
2330 if (cur.result().screenUpdate() & Update::Force) {
2331 singleParUpdate = false;
2335 // FIXME: the following code should go in favor of fine grained
2336 // update flag treatment.
2337 if (singleParUpdate) {
2338 // Inserting characters does not change par height in general. So, try
2339 // to update _only_ this paragraph. BufferView will detect if a full
2340 // metrics update is needed anyway.
2341 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2345 && &oldTopSlice.inset() == &cur.inset()
2346 && oldTopSlice.idx() == cur.idx()
2347 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2348 && !cur.selection())
2349 // FIXME: it would be better if we could just do this
2351 //if (cur.result().update() != Update::FitCursor)
2352 // cur.noScreenUpdate();
2354 // But some LFUNs do not set Update::FitCursor when needed, so we
2355 // do it for all. This is not very harmfull as FitCursor will provoke
2356 // a full redraw only if needed but still, a proper review of all LFUN
2357 // should be done and this needsUpdate boolean can then be removed.
2358 cur.screenUpdateFlags(Update::FitCursor);
2360 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2364 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2365 FuncStatus & flag) const
2367 LBUFERR(this == cur.text());
2369 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2371 bool allow_in_passthru = false;
2372 InsetCode code = NO_CODE;
2374 switch (cmd.action()) {
2376 case LFUN_DEPTH_DECREMENT:
2377 enable = changeDepthAllowed(cur, DEC_DEPTH);
2380 case LFUN_DEPTH_INCREMENT:
2381 enable = changeDepthAllowed(cur, INC_DEPTH);
2385 // FIXME We really should not allow this to be put, e.g.,
2386 // in a footnote, or in ERT. But it would make sense in a
2387 // branch, so I'm not sure what to do.
2388 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2391 case LFUN_DIALOG_SHOW_NEW_INSET:
2392 if (cmd.argument() == "bibitem")
2393 code = BIBITEM_CODE;
2394 else if (cmd.argument() == "bibtex") {
2396 // not allowed in description items
2397 enable = !inDescriptionItem(cur);
2399 else if (cmd.argument() == "box")
2401 else if (cmd.argument() == "branch")
2403 else if (cmd.argument() == "citation")
2405 else if (cmd.argument() == "ert")
2407 else if (cmd.argument() == "external")
2408 code = EXTERNAL_CODE;
2409 else if (cmd.argument() == "float")
2411 else if (cmd.argument() == "graphics")
2412 code = GRAPHICS_CODE;
2413 else if (cmd.argument() == "href")
2414 code = HYPERLINK_CODE;
2415 else if (cmd.argument() == "include")
2416 code = INCLUDE_CODE;
2417 else if (cmd.argument() == "index")
2419 else if (cmd.argument() == "index_print")
2420 code = INDEX_PRINT_CODE;
2421 else if (cmd.argument() == "nomenclature")
2422 code = NOMENCL_CODE;
2423 else if (cmd.argument() == "nomencl_print")
2424 code = NOMENCL_PRINT_CODE;
2425 else if (cmd.argument() == "label")
2427 else if (cmd.argument() == "line")
2429 else if (cmd.argument() == "note")
2431 else if (cmd.argument() == "phantom")
2432 code = PHANTOM_CODE;
2433 else if (cmd.argument() == "ref")
2435 else if (cmd.argument() == "space")
2437 else if (cmd.argument() == "toc")
2439 else if (cmd.argument() == "vspace")
2441 else if (cmd.argument() == "wrap")
2443 else if (cmd.argument() == "listings")
2444 code = LISTINGS_CODE;
2447 case LFUN_ERT_INSERT:
2450 case LFUN_LISTING_INSERT:
2451 code = LISTINGS_CODE;
2452 // not allowed in description items
2453 enable = !inDescriptionItem(cur);
2455 case LFUN_FOOTNOTE_INSERT:
2458 case LFUN_TABULAR_INSERT:
2459 code = TABULAR_CODE;
2461 case LFUN_MARGINALNOTE_INSERT:
2464 case LFUN_FLOAT_INSERT:
2465 case LFUN_FLOAT_WIDE_INSERT:
2466 // FIXME: If there is a selection, we should check whether there
2467 // are floats in the selection, but this has performance issues, see
2468 // LFUN_CHANGE_ACCEPT/REJECT.
2470 if (inDescriptionItem(cur))
2471 // not allowed in description items
2474 InsetCode const inset_code = cur.inset().lyxCode();
2476 // algorithm floats cannot be put in another float
2477 if (to_utf8(cmd.argument()) == "algorithm") {
2478 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2482 // for figures and tables: only allow in another
2483 // float or wrap if it is of the same type and
2484 // not a subfloat already
2485 if(cur.inset().lyxCode() == code) {
2486 InsetFloat const & ins =
2487 static_cast<InsetFloat const &>(cur.inset());
2488 enable = ins.params().type == to_utf8(cmd.argument())
2489 && !ins.params().subfloat;
2490 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2491 InsetWrap const & ins =
2492 static_cast<InsetWrap const &>(cur.inset());
2493 enable = ins.params().type == to_utf8(cmd.argument());
2497 case LFUN_WRAP_INSERT:
2499 // not allowed in description items
2500 enable = !inDescriptionItem(cur);
2502 case LFUN_FLOAT_LIST_INSERT: {
2503 code = FLOAT_LIST_CODE;
2504 // not allowed in description items
2505 enable = !inDescriptionItem(cur);
2507 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2508 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2509 // make sure we know about such floats
2510 if (cit == floats.end() ||
2511 // and that we know how to generate a list of them
2512 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2513 flag.setUnknown(true);
2514 // probably not necessary, but...
2520 case LFUN_CAPTION_INSERT: {
2521 code = CAPTION_CODE;
2522 string arg = cmd.getArg(0);
2523 bool varia = arg != "LongTableNoNumber";
2524 if (cur.depth() > 0) {
2525 if (&cur[cur.depth() - 1].inset())
2526 varia = cur[cur.depth() - 1].inset().allowsCaptionVariation(arg);
2528 // not allowed in description items,
2529 // and in specific insets
2530 enable = !inDescriptionItem(cur)
2531 && (varia || arg.empty() || arg == "Standard");
2534 case LFUN_NOTE_INSERT:
2536 // in commands (sections etc.) and description items,
2537 // only Notes are allowed
2538 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2539 (!cur.paragraph().layout().isCommand()
2540 && !inDescriptionItem(cur)));
2542 case LFUN_FLEX_INSERT: {
2544 string s = cmd.getArg(0);
2546 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2547 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2548 il.lyxtype() != InsetLayout::CUSTOM &&
2549 il.lyxtype() != InsetLayout::ELEMENT &&
2550 il.lyxtype ()!= InsetLayout::STANDARD)
2554 case LFUN_BOX_INSERT:
2557 case LFUN_BRANCH_INSERT:
2559 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2560 && cur.buffer()->params().branchlist().empty())
2563 case LFUN_IPA_INSERT:
2566 case LFUN_PHANTOM_INSERT:
2567 code = PHANTOM_CODE;
2569 case LFUN_LABEL_INSERT:
2572 case LFUN_INFO_INSERT:
2575 case LFUN_ARGUMENT_INSERT: {
2577 allow_in_passthru = true;
2578 string const arg = cmd.getArg(0);
2583 Layout const & lay = cur.paragraph().layout();
2584 Layout::LaTeXArgMap args = lay.args();
2585 Layout::LaTeXArgMap::const_iterator const lait =
2587 if (lait != args.end()) {
2589 pit_type pit = cur.pit();
2590 pit_type lastpit = cur.pit();
2591 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2592 // In a sequence of "merged" environment layouts, we only allow
2593 // non-item arguments once.
2594 lastpit = cur.lastpit();
2595 // get the first paragraph in sequence with this layout
2596 depth_type const current_depth = cur.paragraph().params().depth();
2600 Paragraph cpar = pars_[pit - 1];
2601 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2607 for (; pit <= lastpit; ++pit) {
2608 if (pars_[pit].layout() != lay)
2610 InsetList::const_iterator it = pars_[pit].insetList().begin();
2611 InsetList::const_iterator end = pars_[pit].insetList().end();
2612 for (; it != end; ++it) {
2613 if (it->inset->lyxCode() == ARG_CODE) {
2614 InsetArgument const * ins =
2615 static_cast<InsetArgument const *>(it->inset);
2616 if (ins->name() == arg) {
2617 // we have this already
2628 case LFUN_INDEX_INSERT:
2631 case LFUN_INDEX_PRINT:
2632 code = INDEX_PRINT_CODE;
2633 // not allowed in description items
2634 enable = !inDescriptionItem(cur);
2636 case LFUN_NOMENCL_INSERT:
2637 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2641 code = NOMENCL_CODE;
2643 case LFUN_NOMENCL_PRINT:
2644 code = NOMENCL_PRINT_CODE;
2645 // not allowed in description items
2646 enable = !inDescriptionItem(cur);
2648 case LFUN_HYPERLINK_INSERT:
2649 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2653 code = HYPERLINK_CODE;
2655 case LFUN_IPAMACRO_INSERT: {
2656 string const arg = cmd.getArg(0);
2658 code = IPADECO_CODE;
2660 code = IPACHAR_CODE;
2663 case LFUN_QUOTE_INSERT:
2664 // always allow this, since we will inset a raw quote
2665 // if an inset is not allowed.
2667 case LFUN_SPECIALCHAR_INSERT:
2668 code = SPECIALCHAR_CODE;
2670 case LFUN_SPACE_INSERT:
2671 // slight hack: we know this is allowed in math mode
2675 case LFUN_PREVIEW_INSERT:
2676 code = PREVIEW_CODE;
2678 case LFUN_SCRIPT_INSERT:
2682 case LFUN_MATH_INSERT:
2683 case LFUN_MATH_AMS_MATRIX:
2684 case LFUN_MATH_MATRIX:
2685 case LFUN_MATH_DELIM:
2686 case LFUN_MATH_BIGDELIM:
2687 case LFUN_MATH_DISPLAY:
2688 case LFUN_MATH_MODE:
2689 case LFUN_MATH_MACRO:
2690 case LFUN_MATH_SUBSCRIPT:
2691 case LFUN_MATH_SUPERSCRIPT:
2692 code = MATH_HULL_CODE;
2695 case LFUN_REGEXP_MODE:
2696 code = MATH_HULL_CODE;
2697 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2700 case LFUN_INSET_MODIFY:
2701 // We need to disable this, because we may get called for a
2703 // InsetTabular::getStatus() -> InsetText::getStatus()
2704 // and we don't handle LFUN_INSET_MODIFY.
2708 case LFUN_FONT_EMPH:
2709 flag.setOnOff(fontinfo.emph() == FONT_ON);
2710 enable = !cur.paragraph().isPassThru();
2713 case LFUN_FONT_ITAL:
2714 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2715 enable = !cur.paragraph().isPassThru();
2718 case LFUN_FONT_NOUN:
2719 flag.setOnOff(fontinfo.noun() == FONT_ON);
2720 enable = !cur.paragraph().isPassThru();
2723 case LFUN_FONT_BOLD:
2724 case LFUN_FONT_BOLDSYMBOL:
2725 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2726 enable = !cur.paragraph().isPassThru();
2729 case LFUN_FONT_SANS:
2730 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2731 enable = !cur.paragraph().isPassThru();
2734 case LFUN_FONT_ROMAN:
2735 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2736 enable = !cur.paragraph().isPassThru();
2739 case LFUN_FONT_TYPEWRITER:
2740 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2741 enable = !cur.paragraph().isPassThru();
2746 enable = cur.selection();
2750 if (cmd.argument().empty()) {
2751 if (theClipboard().isInternal())
2752 enable = cap::numberOfSelections() > 0;
2754 enable = !theClipboard().empty();
2758 // we have an argument
2759 string const arg = to_utf8(cmd.argument());
2760 if (isStrUnsignedInt(arg)) {
2761 // it's a number and therefore means the internal stack
2762 unsigned int n = convert<unsigned int>(arg);
2763 enable = cap::numberOfSelections() > n;
2767 // explicit text type?
2768 if (arg == "html") {
2769 // Do not enable for PlainTextType, since some tidying in the
2770 // frontend is needed for HTML, which is too unsafe for plain text.
2771 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
2773 } else if (arg == "latex") {
2774 // LaTeX is usually not available on the clipboard with
2775 // the correct MIME type, but in plain text.
2776 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
2777 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
2781 // explicit graphics type?
2782 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2783 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2784 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2785 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2786 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2787 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2788 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2789 enable = theClipboard().hasGraphicsContents(type);
2798 case LFUN_CLIPBOARD_PASTE:
2799 case LFUN_CLIPBOARD_PASTE_SIMPLE:
2800 enable = !theClipboard().empty();
2803 case LFUN_PRIMARY_SELECTION_PASTE:
2804 enable = cur.selection() || !theSelection().empty();
2807 case LFUN_SELECTION_PASTE:
2808 enable = cap::selection();
2811 case LFUN_PARAGRAPH_MOVE_UP:
2812 enable = cur.pit() > 0 && !cur.selection();
2815 case LFUN_PARAGRAPH_MOVE_DOWN:
2816 enable = cur.pit() < cur.lastpit() && !cur.selection();
2819 case LFUN_CHANGE_ACCEPT:
2820 case LFUN_CHANGE_REJECT:
2821 // In principle, these LFUNs should only be enabled if there
2822 // is a change at the current position/in the current selection.
2823 // However, without proper optimizations, this will inevitably
2824 // result in unacceptable performance - just imagine a user who
2825 // wants to select the complete content of a long document.
2826 if (!cur.selection())
2827 enable = cur.paragraph().isChanged(cur.pos());
2829 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2834 case LFUN_OUTLINE_UP:
2835 case LFUN_OUTLINE_DOWN:
2836 case LFUN_OUTLINE_IN:
2837 case LFUN_OUTLINE_OUT:
2838 // FIXME: LyX is not ready for outlining within inset.
2839 enable = isMainText()
2840 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
2843 case LFUN_NEWLINE_INSERT:
2844 // LaTeX restrictions (labels or empty par)
2845 enable = (cur.pos() > cur.paragraph().beginOfBody());
2848 case LFUN_TAB_INSERT:
2849 case LFUN_TAB_DELETE:
2850 enable = cur.paragraph().isPassThru();
2853 case LFUN_SET_GRAPHICS_GROUP: {
2854 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2858 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2862 case LFUN_NEWPAGE_INSERT:
2863 // not allowed in description items
2864 code = NEWPAGE_CODE;
2865 enable = !inDescriptionItem(cur);
2868 case LFUN_DATE_INSERT: {
2869 string const format = cmd.argument().empty()
2870 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2871 enable = support::os::is_valid_strftime(format);
2876 enable = !cur.paragraph().isPassThru();
2877 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
2880 case LFUN_PARAGRAPH_BREAK:
2881 enable = cur.inset().getLayout().isMultiPar();
2884 case LFUN_SPELLING_ADD:
2885 case LFUN_SPELLING_IGNORE:
2886 case LFUN_SPELLING_REMOVE:
2887 enable = theSpellChecker();
2891 enable = !cur.inset().forcePlainLayout();
2894 case LFUN_ENVIRONMENT_SPLIT: {
2895 if (cur.paragraph().layout().isEnvironment()
2896 && cur.buffer()->params().documentClass().hasLayout(from_ascii("Separator"))) {
2904 case LFUN_LAYOUT_PARAGRAPH:
2905 case LFUN_PARAGRAPH_PARAMS:
2906 case LFUN_PARAGRAPH_PARAMS_APPLY:
2907 case LFUN_PARAGRAPH_UPDATE:
2908 enable = cur.inset().allowParagraphCustomization();
2911 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
2912 case LFUN_ACCENT_ACUTE:
2913 case LFUN_ACCENT_BREVE:
2914 case LFUN_ACCENT_CARON:
2915 case LFUN_ACCENT_CEDILLA:
2916 case LFUN_ACCENT_CIRCLE:
2917 case LFUN_ACCENT_CIRCUMFLEX:
2918 case LFUN_ACCENT_DOT:
2919 case LFUN_ACCENT_GRAVE:
2920 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2921 case LFUN_ACCENT_MACRON:
2922 case LFUN_ACCENT_OGONEK:
2923 case LFUN_ACCENT_TIE:
2924 case LFUN_ACCENT_TILDE:
2925 case LFUN_ACCENT_UMLAUT:
2926 case LFUN_ACCENT_UNDERBAR:
2927 case LFUN_ACCENT_UNDERDOT:
2928 case LFUN_FONT_DEFAULT:
2929 case LFUN_FONT_FRAK:
2930 case LFUN_FONT_SIZE:
2931 case LFUN_FONT_STATE:
2932 case LFUN_FONT_UNDERLINE:
2933 case LFUN_FONT_STRIKEOUT:
2934 case LFUN_FONT_UULINE:
2935 case LFUN_FONT_UWAVE:
2936 case LFUN_TEXTSTYLE_APPLY:
2937 case LFUN_TEXTSTYLE_UPDATE:
2938 enable = !cur.paragraph().isPassThru();
2941 case LFUN_WORD_DELETE_FORWARD:
2942 case LFUN_WORD_DELETE_BACKWARD:
2943 case LFUN_LINE_DELETE:
2944 case LFUN_WORD_FORWARD:
2945 case LFUN_WORD_BACKWARD:
2946 case LFUN_WORD_RIGHT:
2947 case LFUN_WORD_LEFT:
2948 case LFUN_CHAR_FORWARD:
2949 case LFUN_CHAR_FORWARD_SELECT:
2950 case LFUN_CHAR_BACKWARD:
2951 case LFUN_CHAR_BACKWARD_SELECT:
2952 case LFUN_CHAR_LEFT:
2953 case LFUN_CHAR_LEFT_SELECT:
2954 case LFUN_CHAR_RIGHT:
2955 case LFUN_CHAR_RIGHT_SELECT:
2957 case LFUN_UP_SELECT:
2959 case LFUN_DOWN_SELECT:
2960 case LFUN_PARAGRAPH_UP_SELECT:
2961 case LFUN_PARAGRAPH_DOWN_SELECT:
2962 case LFUN_LINE_BEGIN_SELECT:
2963 case LFUN_LINE_END_SELECT:
2964 case LFUN_WORD_FORWARD_SELECT:
2965 case LFUN_WORD_BACKWARD_SELECT:
2966 case LFUN_WORD_RIGHT_SELECT:
2967 case LFUN_WORD_LEFT_SELECT:
2968 case LFUN_WORD_SELECT:
2969 case LFUN_SECTION_SELECT:
2970 case LFUN_BUFFER_BEGIN:
2971 case LFUN_BUFFER_END:
2972 case LFUN_BUFFER_BEGIN_SELECT:
2973 case LFUN_BUFFER_END_SELECT:
2974 case LFUN_INSET_BEGIN:
2975 case LFUN_INSET_END:
2976 case LFUN_INSET_BEGIN_SELECT:
2977 case LFUN_INSET_END_SELECT:
2978 case LFUN_INSET_SELECT_ALL:
2979 case LFUN_PARAGRAPH_UP:
2980 case LFUN_PARAGRAPH_DOWN:
2981 case LFUN_LINE_BEGIN:
2983 case LFUN_CHAR_DELETE_FORWARD:
2984 case LFUN_CHAR_DELETE_BACKWARD:
2985 case LFUN_WORD_UPCASE:
2986 case LFUN_WORD_LOWCASE:
2987 case LFUN_WORD_CAPITALIZE:
2988 case LFUN_CHARS_TRANSPOSE:
2989 case LFUN_SERVER_GET_XY:
2990 case LFUN_SERVER_SET_XY:
2991 case LFUN_SERVER_GET_LAYOUT:
2992 case LFUN_SELF_INSERT:
2993 case LFUN_UNICODE_INSERT:
2994 case LFUN_THESAURUS_ENTRY:
2996 // these are handled in our dispatch()
3000 case LFUN_INSET_INSERT: {
3001 string const type = cmd.getArg(0);
3002 if (type == "toc") {
3004 // not allowed in description items
3005 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3006 enable = !inDescriptionItem(cur);
3019 || !cur.inset().insetAllowed(code)
3020 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3023 flag.setEnabled(enable);
3028 void Text::pasteString(Cursor & cur, docstring const & clip,
3031 if (!clip.empty()) {
3034 insertStringAsParagraphs(cur, clip, cur.current_font);
3036 insertStringAsLines(cur, clip, cur.current_font);
3041 // FIXME: an item inset would make things much easier.
3042 bool Text::inDescriptionItem(Cursor & cur) const
3044 Paragraph & par = cur.paragraph();
3045 pos_type const pos = cur.pos();
3046 pos_type const body_pos = par.beginOfBody();
3048 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3049 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3050 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3053 return (pos < body_pos
3055 && (pos == 0 || par.getChar(pos - 1) != ' ')));