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.
20 #include "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "SpellChecker.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
47 #include "Thesaurus.h"
48 #include "WordLangTuple.h"
50 #include "frontends/alert.h"
51 #include "frontends/Application.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetArgument.h"
56 #include "insets/InsetCollapsible.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetFloat.h"
60 #include "insets/InsetFloatList.h"
61 #include "insets/InsetGraphics.h"
62 #include "insets/InsetGraphicsParams.h"
63 #include "insets/InsetInfo.h"
64 #include "insets/InsetIPAMacro.h"
65 #include "insets/InsetNewline.h"
66 #include "insets/InsetQuotes.h"
67 #include "insets/InsetSpecialChar.h"
68 #include "insets/InsetText.h"
69 #include "insets/InsetWrap.h"
71 #include "support/convert.h"
72 #include "support/debug.h"
73 #include "support/gettext.h"
74 #include "support/lassert.h"
75 #include "support/lstrings.h"
76 #include "support/lyxalgo.h"
77 #include "support/lyxtime.h"
78 #include "support/os.h"
79 #include "support/regex.h"
81 #include "mathed/InsetMathHull.h"
82 #include "mathed/InsetMathMacroTemplate.h"
88 using namespace lyx::support;
92 using cap::copySelection;
93 using cap::copySelectionToTemp;
94 using cap::cutSelection;
95 using cap::cutSelectionToTemp;
96 using cap::pasteFromStack;
97 using cap::pasteFromTemp;
98 using cap::pasteClipboardText;
99 using cap::pasteClipboardGraphics;
100 using cap::replaceSelection;
101 using cap::grabAndEraseSelection;
102 using cap::selClearOrDel;
103 using cap::pasteSimpleText;
104 using frontend::Clipboard;
107 static Font freefont(ignore_font, ignore_language);
108 static bool toggleall = false;
110 static void toggleAndShow(Cursor & cur, Text * text,
111 Font const & font, bool togall = true)
113 text->toggleFree(cur, font, togall);
115 if (font.language() != ignore_language ||
116 font.fontInfo().number() != FONT_IGNORE) {
117 TextMetrics const & tm = cur.bv().textMetrics(text);
118 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
119 cur.real_current_font))
120 text->setCursor(cur, cur.pit(), cur.pos(),
121 false, !cur.boundary());
126 static void moveCursor(Cursor & cur, bool selecting)
128 if (selecting || cur.mark())
133 static void finishChange(Cursor & cur, bool selecting)
136 moveCursor(cur, selecting);
140 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
143 docstring sel = cur.selectionAsString(false);
145 // It may happen that sel is empty but there is a selection
146 replaceSelection(cur);
148 // Is this a valid formula?
152 #ifdef ENABLE_ASSERTIONS
153 const int old_pos = cur.pos();
155 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
156 #ifdef ENABLE_ASSERTIONS
157 LATTEST(old_pos == cur.pos());
159 cur.nextInset()->edit(cur, true);
160 if (cmd.action() != LFUN_MATH_MODE)
161 // LFUN_MATH_MODE has a different meaning in math mode
164 InsetMathHull * formula = new InsetMathHull(cur.buffer());
165 string const selstr = to_utf8(sel);
166 istringstream is(selstr);
169 if (!formula->readQuiet(lex)) {
170 // No valid formula, let's try with delims
171 is.str("$" + selstr + "$");
173 if (!formula->readQuiet(lex)) {
174 // Still not valid, leave it as is
182 cur.nextInset()->edit(cur, true);
183 LASSERT(cur.inMathed(), return);
187 cur.pos() = cur.lastpos();
188 if (cmd.action() != LFUN_MATH_MODE)
189 // LFUN_MATH_MODE has a different meaning in math mode
191 cur.clearSelection();
192 cur.pos() = cur.lastpos();
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 (InsetCollapsible * ci = inset->asInsetCollapsible())
251 ci->setButtonLabel();
254 if (cmd.action() == LFUN_ARGUMENT_INSERT) {
255 bool cotextinsert = false;
256 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
257 Layout const & lay = cur.paragraph().layout();
258 Layout::LaTeXArgMap args = lay.args();
259 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
260 if (lait != args.end())
261 cotextinsert = (*lait).second.insertcotext;
263 InsetLayout const & il = cur.inset().getLayout();
265 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
266 if (ilait != args.end())
267 cotextinsert = (*ilait).second.insertcotext;
269 // The argument requests to insert a copy of the co-text to the inset
272 // If we have a selection within a paragraph, use this
273 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
274 ds = cur.selectionAsString(false);
275 // else use the whole paragraph
277 ds = cur.paragraph().asString();
278 text->insertInset(cur, inset);
280 inset->edit(cur, true);
281 // Now put co-text into inset
282 Font const f(inherit_font, cur.current_font.language());
284 cur.text()->insertStringAsLines(cur, ds, f);
285 cur.leaveInset(*inset);
292 if (cur.selection()) {
293 if (cmd.action() == LFUN_INDEX_INSERT)
294 copySelectionToTemp(cur);
296 cutSelectionToTemp(cur, pastesel);
297 cur.clearSelection();
299 } else if (cmd.action() == LFUN_INDEX_INSERT) {
300 gotsel = text->selectWordWhenUnderCursor(cur, WHOLE_WORD);
301 copySelectionToTemp(cur);
302 cur.clearSelection();
304 text->insertInset(cur, inset);
307 inset->edit(cur, true);
309 if (!gotsel || !pastesel)
312 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
313 cur.buffer()->errors("Paste");
314 cur.clearSelection(); // bug 393
316 InsetText * inset_text = inset->asInsetText();
318 inset_text->fixParagraphsFont();
319 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
320 // reset first par to default
321 cur.text()->paragraphs().begin()
322 ->setPlainOrDefaultLayout(bparams.documentClass());
325 // Merge multiple paragraphs -- hack
326 while (cur.lastpit() > 0)
327 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
328 if (cmd.action() == LFUN_FLEX_INSERT)
331 cur.leaveInset(*inset);
332 if (cmd.action() == LFUN_PREVIEW_INSERT
333 || cmd.action() == LFUN_IPA_INSERT)
335 notifyCursorLeavesOrEnters(old, cur);
338 cur.leaveInset(*inset);
339 // reset surrounding par to default
340 DocumentClass const & dc = bparams.documentClass();
341 docstring const layoutname = inset->usePlainLayout()
342 ? dc.plainLayoutName()
343 : dc.defaultLayoutName();
344 text->setLayout(cur, layoutname);
350 /// the type of outline operation
352 OutlineUp, // Move this header with text down
353 OutlineDown, // Move this header with text up
354 OutlineIn, // Make this header deeper
355 OutlineOut // Make this header shallower
359 static void insertSeparator(Cursor & cur, depth_type const depth)
361 Buffer & buf = *cur.buffer();
362 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
363 DocumentClass const & tc = buf.params().documentClass();
364 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
365 + from_ascii("\" ignoreautonests")));
366 // FIXME: Bibitem mess!
367 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
368 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
369 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
370 while (cur.paragraph().params().depth() > depth)
371 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
375 static void outline(OutlineOp mode, Cursor & cur, Text * text)
377 Buffer & buf = *cur.buffer();
378 pit_type & pit = cur.pit();
379 ParagraphList & pars = buf.text().paragraphs();
380 ParagraphList::iterator const bgn = pars.begin();
381 // The first paragraph of the area to be copied:
382 ParagraphList::iterator start = lyx::next(bgn, pit);
383 // The final paragraph of area to be copied:
384 ParagraphList::iterator finish = start;
385 ParagraphList::iterator const end = pars.end();
386 depth_type const current_depth = cur.paragraph().params().depth();
388 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
391 // Move out (down) from this section header
395 // Seek the one (on same level) below
396 for (; finish != end; ++finish) {
397 toclevel = buf.text().getTocLevel(distance(bgn, finish));
398 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
404 if (start == pars.begin())
407 ParagraphList::iterator dest = start;
408 // Move out (up) from this header
411 // Search previous same-level header above
414 toclevel = buf.text().getTocLevel(distance(bgn, dest));
416 && (toclevel == Layout::NOT_IN_TOC
417 || toclevel > thistoclevel));
418 // Not found; do nothing
419 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
421 pit_type newpit = distance(bgn, dest);
422 pit_type const len = distance(start, finish);
423 pit_type const deletepit = pit + len;
424 buf.undo().recordUndo(cur, newpit, deletepit - 1);
425 // If we move an environment upwards, make sure it is
426 // separated from its new neighbour below:
427 // If an environment of the same layout follows, and the moved
428 // paragraph sequence does not end with a separator, insert one.
429 ParagraphList::iterator lastmoved = finish;
431 if (start->layout().isEnvironment()
432 && dest->layout() == start->layout()
433 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
434 cur.pit() = distance(bgn, lastmoved);
435 cur.pos() = cur.lastpos();
436 insertSeparator(cur, current_depth);
439 // Likewise, if we moved an environment upwards, make sure it
440 // is separated from its new neighbour above.
441 // The paragraph before the target of movement
443 ParagraphList::iterator before = dest;
445 // Get the parent paragraph (outer in nested context)
446 pit_type const parent =
447 before->params().depth() > current_depth
448 ? text->depthHook(distance(bgn, before), current_depth)
449 : distance(bgn, before);
450 // If a environment with same layout preceeds the moved one in the new
451 // position, and there is no separator yet, insert one.
452 if (start->layout().isEnvironment()
453 && pars[parent].layout() == start->layout()
454 && !before->isEnvSeparator(before->beginOfBody())) {
455 cur.pit() = distance(bgn, before);
456 cur.pos() = cur.lastpos();
457 insertSeparator(cur, current_depth);
461 newpit = distance(bgn, dest);
462 pars.splice(dest, start, finish);
470 // Go one down from *this* header:
471 ParagraphList::iterator dest = next(finish, 1);
472 // Go further down to find header to insert in front of:
473 for (; dest != end; ++dest) {
474 toclevel = buf.text().getTocLevel(distance(bgn, dest));
475 if (toclevel != Layout::NOT_IN_TOC
476 && toclevel <= thistoclevel)
479 // One such was found, so go on...
480 // If we move an environment downwards, make sure it is
481 // separated from its new neighbour above.
482 pit_type newpit = distance(bgn, dest);
483 buf.undo().recordUndo(cur, pit, newpit - 1);
484 // The paragraph before the target of movement
485 ParagraphList::iterator before = dest;
487 // Get the parent paragraph (outer in nested context)
488 pit_type const parent =
489 before->params().depth() > current_depth
490 ? text->depthHook(distance(bgn, before), current_depth)
491 : distance(bgn, before);
492 // If a environment with same layout preceeds the moved one in the new
493 // position, and there is no separator yet, insert one.
494 if (start->layout().isEnvironment()
495 && pars[parent].layout() == start->layout()
496 && !before->isEnvSeparator(before->beginOfBody())) {
497 cur.pit() = distance(bgn, before);
498 cur.pos() = cur.lastpos();
499 insertSeparator(cur, current_depth);
502 // Likewise, make sure moved environments are separated
503 // from their new neighbour below:
504 // If an environment of the same layout follows, and the moved
505 // paragraph sequence does not end with a separator, insert one.
506 ParagraphList::iterator lastmoved = finish;
509 && start->layout().isEnvironment()
510 && dest->layout() == start->layout()
511 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
512 cur.pit() = distance(bgn, lastmoved);
513 cur.pos() = cur.lastpos();
514 insertSeparator(cur, current_depth);
517 newpit = distance(bgn, dest);
518 pit_type const len = distance(start, finish);
519 pars.splice(dest, start, finish);
520 cur.pit() = newpit - len;
525 pit_type const len = distance(start, finish);
526 buf.undo().recordUndo(cur, pit, pit + len - 1);
527 for (; start != finish; ++start) {
528 toclevel = buf.text().getTocLevel(distance(bgn, start));
529 if (toclevel == Layout::NOT_IN_TOC)
532 DocumentClass const & tc = buf.params().documentClass();
533 DocumentClass::const_iterator lit = tc.begin();
534 DocumentClass::const_iterator len = tc.end();
535 int const newtoclevel =
536 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
537 LabelType const oldlabeltype = start->layout().labeltype;
539 for (; lit != len; ++lit) {
540 if (lit->toclevel == newtoclevel &&
541 lit->labeltype == oldlabeltype) {
542 start->setLayout(*lit);
553 void Text::number(Cursor & cur)
555 FontInfo font = ignore_font;
556 font.setNumber(FONT_TOGGLE);
557 toggleAndShow(cur, this, Font(font, ignore_language));
561 bool Text::isRTL(Paragraph const & par) const
563 Buffer const & buffer = owner_->buffer();
564 return par.isRTL(buffer.params());
570 Language const * getLanguage(Cursor const & cur, string const & lang)
572 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
576 docstring resolveLayout(docstring layout, DocIterator const & dit)
578 Paragraph const & par = dit.paragraph();
579 docstring const old_layout = par.layout().name();
580 DocumentClass const & tclass = dit.buffer()->params().documentClass();
583 layout = tclass.defaultLayoutName();
585 if (dit.inset().forcePlainLayout(dit.idx()))
586 // in this case only the empty layout is allowed
587 layout = tclass.plainLayoutName();
588 else if (par.usePlainLayout()) {
589 // in this case, default layout maps to empty layout
590 if (layout == tclass.defaultLayoutName())
591 layout = tclass.plainLayoutName();
593 // otherwise, the empty layout maps to the default
594 if (layout == tclass.plainLayoutName())
595 layout = tclass.defaultLayoutName();
598 // If the entry is obsolete, use the new one instead.
599 if (tclass.hasLayout(layout)) {
600 docstring const & obs = tclass[layout].obsoleted_by();
604 if (!tclass.hasLayout(layout))
610 bool isAlreadyLayout(docstring const & layout, CursorData const & cur)
612 ParagraphList const & pars = cur.text()->paragraphs();
614 pit_type pit = cur.selBegin().pit();
615 pit_type const epit = cur.selEnd().pit() + 1;
616 for ( ; pit != epit; ++pit)
617 if (pars[pit].layout().name() != layout)
627 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
629 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
631 // Dispatch if the cursor is inside the text. It is not the
632 // case for context menus (bug 5797).
633 if (cur.text() != this) {
638 BufferView * bv = &cur.bv();
639 TextMetrics * tm = &bv->textMetrics(this);
640 if (!tm->contains(cur.pit())) {
641 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
642 tm = &bv->textMetrics(this);
645 // FIXME: We use the update flag to indicates wether a singlePar or a
646 // full screen update is needed. We reset it here but shall we restore it
648 cur.noScreenUpdate();
650 LBUFERR(this == cur.text());
652 // NOTE: This should NOT be a reference. See commit 94a5481a.
653 CursorSlice const oldTopSlice = cur.top();
654 bool const oldBoundary = cur.boundary();
655 bool const oldSelection = cur.selection();
656 // Signals that, even if needsUpdate == false, an update of the
657 // cursor paragraph is required
658 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
659 LyXAction::SingleParUpdate);
660 // Signals that a full-screen update is required
661 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
662 LyXAction::NoUpdate) || singleParUpdate);
663 bool const last_misspelled = lyxrc.spellcheck_continuously
664 && cur.paragraph().isMisspelled(cur.pos(), true);
666 FuncCode const act = cmd.action();
669 case LFUN_PARAGRAPH_MOVE_DOWN: {
670 pit_type const pit = cur.pit();
671 cur.recordUndo(pit, pit + 1);
673 pars_.swap(pit, pit + 1);
675 cur.forceBufferUpdate();
680 case LFUN_PARAGRAPH_MOVE_UP: {
681 pit_type const pit = cur.pit();
682 cur.recordUndo(pit - 1, pit);
684 pars_.swap(pit, pit - 1);
687 cur.forceBufferUpdate();
691 case LFUN_APPENDIX: {
692 Paragraph & par = cur.paragraph();
693 bool start = !par.params().startOfAppendix();
695 // FIXME: The code below only makes sense at top level.
696 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
697 // ensure that we have only one start_of_appendix in this document
698 // FIXME: this don't work for multipart document!
699 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
700 if (pars_[tmp].params().startOfAppendix()) {
701 cur.recordUndo(tmp, tmp);
702 pars_[tmp].params().startOfAppendix(false);
708 par.params().startOfAppendix(start);
710 // we can set the refreshing parameters now
711 cur.forceBufferUpdate();
715 case LFUN_WORD_DELETE_FORWARD:
717 cutSelection(cur, false);
719 deleteWordForward(cur, cmd.getArg(0) == "force");
720 finishChange(cur, false);
723 case LFUN_WORD_DELETE_BACKWARD:
725 cutSelection(cur, false);
727 deleteWordBackward(cur, cmd.getArg(0) == "force");
728 finishChange(cur, false);
731 case LFUN_LINE_DELETE_FORWARD:
733 cutSelection(cur, false);
735 tm->deleteLineForward(cur);
736 finishChange(cur, false);
739 case LFUN_BUFFER_BEGIN:
740 case LFUN_BUFFER_BEGIN_SELECT:
741 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
742 if (cur.depth() == 1)
743 needsUpdate |= cursorTop(cur);
746 cur.screenUpdateFlags(Update::FitCursor);
749 case LFUN_BUFFER_END:
750 case LFUN_BUFFER_END_SELECT:
751 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
752 if (cur.depth() == 1)
753 needsUpdate |= cursorBottom(cur);
756 cur.screenUpdateFlags(Update::FitCursor);
759 case LFUN_INSET_BEGIN:
760 case LFUN_INSET_BEGIN_SELECT:
761 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
762 if (cur.depth() == 1 || !cur.top().at_begin())
763 needsUpdate |= cursorTop(cur);
766 cur.screenUpdateFlags(Update::FitCursor);
770 case LFUN_INSET_END_SELECT:
771 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
772 if (cur.depth() == 1 || !cur.top().at_end())
773 needsUpdate |= cursorBottom(cur);
776 cur.screenUpdateFlags(Update::FitCursor);
779 case LFUN_CHAR_FORWARD:
780 case LFUN_CHAR_FORWARD_SELECT: {
781 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
782 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
783 bool const cur_moved = cursorForward(cur);
784 needsUpdate |= cur_moved;
786 if (!cur_moved && oldTopSlice == cur.top()
787 && cur.boundary() == oldBoundary) {
789 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
791 // we will probably be moving out the inset, so we should execute
792 // the depm-mechanism, but only when the cursor has a place to
793 // go outside this inset, i.e. in a slice above.
794 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
795 && cur.pit() == cur.lastpit()) {
796 // The cursor hasn't changed yet. To give the
797 // DEPM the possibility of doing something we must
798 // provide it with two different cursors.
800 dummy.pos() = dummy.pit() = 0;
801 if (cur.bv().checkDepm(dummy, cur))
802 cur.forceBufferUpdate();
808 case LFUN_CHAR_BACKWARD:
809 case LFUN_CHAR_BACKWARD_SELECT: {
810 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
811 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
812 bool const cur_moved = cursorBackward(cur);
813 needsUpdate |= cur_moved;
815 if (!cur_moved && oldTopSlice == cur.top()
816 && cur.boundary() == oldBoundary) {
818 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
820 // we will probably be moving out the inset, so we should execute
821 // the depm-mechanism, but only when the cursor has a place to
822 // go outside this inset, i.e. in a slice above.
823 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
824 // The cursor hasn't changed yet. To give the
825 // DEPM the possibility of doing something we must
826 // provide it with two different cursors.
828 dummy.pos() = cur.lastpos();
829 dummy.pit() = cur.lastpit();
830 if (cur.bv().checkDepm(dummy, cur))
831 cur.forceBufferUpdate();
838 case LFUN_CHAR_LEFT_SELECT:
839 if (lyxrc.visual_cursor) {
840 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
841 bool const cur_moved = cursorVisLeft(cur);
842 needsUpdate |= cur_moved;
843 if (!cur_moved && oldTopSlice == cur.top()
844 && cur.boundary() == oldBoundary) {
846 cmd = FuncRequest(LFUN_FINISHED_LEFT);
849 if (cur.reverseDirectionNeeded()) {
850 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
851 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
853 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
854 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
861 case LFUN_CHAR_RIGHT:
862 case LFUN_CHAR_RIGHT_SELECT:
863 if (lyxrc.visual_cursor) {
864 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
865 bool const cur_moved = cursorVisRight(cur);
866 needsUpdate |= cur_moved;
867 if (!cur_moved && oldTopSlice == cur.top()
868 && cur.boundary() == oldBoundary) {
870 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
873 if (cur.reverseDirectionNeeded()) {
874 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
875 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
877 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
878 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
887 case LFUN_DOWN_SELECT:
890 // stop/start the selection
891 bool select = cmd.action() == LFUN_DOWN_SELECT ||
892 cmd.action() == LFUN_UP_SELECT;
894 // move cursor up/down
895 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
896 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
898 if (!atFirstOrLastRow) {
899 needsUpdate |= cur.selHandle(select);
900 cur.upDownInText(up, needsUpdate);
901 needsUpdate |= cur.beforeDispatchCursor().inMathed();
903 pos_type newpos = up ? 0 : cur.lastpos();
904 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
905 needsUpdate |= cur.selHandle(select);
906 // we do not reset the targetx of the cursor
908 needsUpdate |= bv->checkDepm(cur, bv->cursor());
909 cur.updateTextTargetOffset();
911 cur.forceBufferUpdate();
915 // if the cursor cannot be moved up or down do not remove
916 // the selection right now, but wait for the next dispatch.
918 needsUpdate |= cur.selHandle(select);
919 cur.upDownInText(up, needsUpdate);
926 case LFUN_PARAGRAPH_UP:
927 case LFUN_PARAGRAPH_UP_SELECT:
928 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
929 needsUpdate |= cursorUpParagraph(cur);
932 case LFUN_PARAGRAPH_DOWN:
933 case LFUN_PARAGRAPH_DOWN_SELECT:
934 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
935 needsUpdate |= cursorDownParagraph(cur);
938 case LFUN_LINE_BEGIN:
939 case LFUN_LINE_BEGIN_SELECT:
940 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
941 needsUpdate |= tm->cursorHome(cur);
945 case LFUN_LINE_END_SELECT:
946 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
947 needsUpdate |= tm->cursorEnd(cur);
950 case LFUN_SECTION_SELECT: {
951 Buffer const & buf = *cur.buffer();
952 pit_type const pit = cur.pit();
953 ParagraphList & pars = buf.text().paragraphs();
954 ParagraphList::iterator bgn = pars.begin();
955 // The first paragraph of the area to be selected:
956 ParagraphList::iterator start = lyx::next(bgn, pit);
957 // The final paragraph of area to be selected:
958 ParagraphList::iterator finish = start;
959 ParagraphList::iterator end = pars.end();
961 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
962 if (thistoclevel == Layout::NOT_IN_TOC)
966 Cursor const old_cur = cur;
967 needsUpdate |= cur.selHandle(true);
969 // Move out (down) from this section header
973 // Seek the one (on same level) below
974 for (; finish != end; ++finish, ++cur.pit()) {
975 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
976 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
979 cur.pos() = cur.lastpos();
981 cur.setCurrentFont();
983 needsUpdate |= cur != old_cur;
987 case LFUN_WORD_RIGHT:
988 case LFUN_WORD_RIGHT_SELECT:
989 if (lyxrc.visual_cursor) {
990 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
991 bool const cur_moved = cursorVisRightOneWord(cur);
992 needsUpdate |= cur_moved;
993 if (!cur_moved && oldTopSlice == cur.top()
994 && cur.boundary() == oldBoundary) {
996 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
999 if (cur.reverseDirectionNeeded()) {
1000 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1001 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1003 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1004 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1011 case LFUN_WORD_FORWARD:
1012 case LFUN_WORD_FORWARD_SELECT: {
1013 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
1014 bool const cur_moved = cursorForwardOneWord(cur);
1015 needsUpdate |= cur_moved;
1017 if (!cur_moved && oldTopSlice == cur.top()
1018 && cur.boundary() == oldBoundary) {
1020 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1022 // we will probably be moving out the inset, so we should execute
1023 // the depm-mechanism, but only when the cursor has a place to
1024 // go outside this inset, i.e. in a slice above.
1025 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
1026 && cur.pit() == cur.lastpit()) {
1027 // The cursor hasn't changed yet. To give the
1028 // DEPM the possibility of doing something we must
1029 // provide it with two different cursors.
1031 dummy.pos() = dummy.pit() = 0;
1032 if (cur.bv().checkDepm(dummy, cur))
1033 cur.forceBufferUpdate();
1039 case LFUN_WORD_LEFT:
1040 case LFUN_WORD_LEFT_SELECT:
1041 if (lyxrc.visual_cursor) {
1042 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
1043 bool const cur_moved = cursorVisLeftOneWord(cur);
1044 needsUpdate |= cur_moved;
1045 if (!cur_moved && oldTopSlice == cur.top()
1046 && cur.boundary() == oldBoundary) {
1048 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1051 if (cur.reverseDirectionNeeded()) {
1052 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1053 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1055 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1056 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1063 case LFUN_WORD_BACKWARD:
1064 case LFUN_WORD_BACKWARD_SELECT: {
1065 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1066 bool const cur_moved = cursorBackwardOneWord(cur);
1067 needsUpdate |= cur_moved;
1069 if (!cur_moved && oldTopSlice == cur.top()
1070 && cur.boundary() == oldBoundary) {
1072 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1074 // we will probably be moving out the inset, so we should execute
1075 // the depm-mechanism, but only when the cursor has a place to
1076 // go outside this inset, i.e. in a slice above.
1077 if (cur.depth() > 1 && cur.pos() == 0
1078 && cur.pit() == 0) {
1079 // The cursor hasn't changed yet. To give the
1080 // DEPM the possibility of doing something we must
1081 // provide it with two different cursors.
1083 dummy.pos() = cur.lastpos();
1084 dummy.pit() = cur.lastpit();
1085 if (cur.bv().checkDepm(dummy, cur))
1086 cur.forceBufferUpdate();
1092 case LFUN_WORD_SELECT: {
1093 selectWord(cur, WHOLE_WORD);
1094 finishChange(cur, true);
1098 case LFUN_NEWLINE_INSERT: {
1099 InsetNewlineParams inp;
1100 docstring arg = cmd.argument();
1101 if (arg == "linebreak")
1102 inp.kind = InsetNewlineParams::LINEBREAK;
1104 inp.kind = InsetNewlineParams::NEWLINE;
1105 cap::replaceSelection(cur);
1107 cur.insert(new InsetNewline(inp));
1109 moveCursor(cur, false);
1113 case LFUN_TAB_INSERT: {
1114 bool const multi_par_selection = cur.selection() &&
1115 cur.selBegin().pit() != cur.selEnd().pit();
1116 if (multi_par_selection) {
1117 // If there is a multi-paragraph selection, a tab is inserted
1118 // at the beginning of each paragraph.
1119 cur.recordUndoSelection();
1120 pit_type const pit_end = cur.selEnd().pit();
1121 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1122 pars_[pit].insertChar(0, '\t',
1123 bv->buffer().params().track_changes);
1124 // Update the selection pos to make sure the selection does not
1125 // change as the inserted tab will increase the logical pos.
1126 if (cur.realAnchor().pit() == pit)
1127 cur.realAnchor().forwardPos();
1128 if (cur.pit() == pit)
1133 // Maybe we shouldn't allow tabs within a line, because they
1134 // are not (yet) aligned as one might do expect.
1135 FuncRequest ncmd(LFUN_SELF_INSERT, from_ascii("\t"));
1136 dispatch(cur, ncmd);
1141 case LFUN_TAB_DELETE: {
1142 bool const tc = bv->buffer().params().track_changes;
1143 if (cur.selection()) {
1144 // If there is a selection, a tab (if present) is removed from
1145 // the beginning of each paragraph.
1146 cur.recordUndoSelection();
1147 pit_type const pit_end = cur.selEnd().pit();
1148 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1149 Paragraph & par = paragraphs()[pit];
1152 char_type const c = par.getChar(0);
1153 if (c == '\t' || c == ' ') {
1154 // remove either 1 tab or 4 spaces.
1155 int const n = (c == ' ' ? 4 : 1);
1156 for (int i = 0; i < n
1157 && !par.empty() && par.getChar(0) == c; ++i) {
1158 if (cur.pit() == pit)
1160 if (cur.realAnchor().pit() == pit
1161 && cur.realAnchor().pos() > 0 )
1162 cur.realAnchor().backwardPos();
1163 par.eraseChar(0, tc);
1169 // If there is no selection, try to remove a tab or some spaces
1170 // before the position of the cursor.
1171 Paragraph & par = paragraphs()[cur.pit()];
1172 pos_type const pos = cur.pos();
1177 char_type const c = par.getChar(pos - 1);
1181 par.eraseChar(cur.pos(), tc);
1183 for (int n_spaces = 0;
1185 && par.getChar(cur.pos() - 1) == ' '
1189 par.eraseChar(cur.pos(), tc);
1196 case LFUN_CHAR_DELETE_FORWARD:
1197 if (!cur.selection()) {
1198 if (cur.pos() == cur.paragraph().size())
1199 // Par boundary, force full-screen update
1200 singleParUpdate = false;
1201 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1203 cur.selection(true);
1208 needsUpdate |= erase(cur);
1211 cutSelection(cur, false);
1212 singleParUpdate = false;
1214 moveCursor(cur, false);
1217 case LFUN_CHAR_DELETE_BACKWARD:
1218 if (!cur.selection()) {
1219 if (bv->getIntl().getTransManager().backspace()) {
1220 bool par_boundary = cur.pos() == 0;
1221 bool first_par = cur.pit() == 0;
1222 // Par boundary, full-screen update
1224 singleParUpdate = false;
1225 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1227 cur.selection(true);
1232 needsUpdate |= backspace(cur);
1234 if (par_boundary && !first_par && cur.pos() > 0
1235 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1236 needsUpdate |= backspace(cur);
1241 cutSelection(cur, false);
1242 singleParUpdate = false;
1246 case LFUN_PARAGRAPH_BREAK: {
1247 cap::replaceSelection(cur);
1248 pit_type pit = cur.pit();
1249 Paragraph const & par = pars_[pit];
1250 bool lastpar = (pit == pit_type(pars_.size() - 1));
1251 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1252 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1253 if (prev < pit && cur.pos() == par.beginOfBody()
1254 && !par.size() && !par.isEnvSeparator(cur.pos())
1255 && !par.layout().isCommand()
1256 && pars_[prev].layout() != par.layout()
1257 && pars_[prev].layout().isEnvironment()
1258 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1259 if (par.layout().isEnvironment()
1260 && pars_[prev].getDepth() == par.getDepth()) {
1261 docstring const layout = par.layout().name();
1262 DocumentClass const & tc = bv->buffer().params().documentClass();
1263 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1264 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1265 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1266 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1268 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1269 breakParagraph(cur);
1271 Font const f(inherit_font, cur.current_font.language());
1272 pars_[cur.pit() - 1].resetFonts(f);
1274 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1276 breakParagraph(cur, cmd.getArg(0) == "inverse");
1279 // If we have a list and autoinsert item insets,
1281 Layout::LaTeXArgMap args = par.layout().args();
1282 for (auto const & thearg : args) {
1283 Layout::latexarg arg = thearg.second;
1284 if (arg.autoinsert && prefixIs(thearg.first, "item:")) {
1285 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, thearg.first);
1286 lyx::dispatch(cmd2);
1292 case LFUN_INSET_INSERT: {
1295 // We have to avoid triggering InstantPreview loading
1296 // before inserting into the document. See bug #5626.
1297 bool loaded = bv->buffer().isFullyLoaded();
1298 bv->buffer().setFullyLoaded(false);
1299 Inset * inset = createInset(&bv->buffer(), cmd);
1300 bv->buffer().setFullyLoaded(loaded);
1303 // FIXME (Abdel 01/02/2006):
1304 // What follows would be a partial fix for bug 2154:
1305 // http://www.lyx.org/trac/ticket/2154
1306 // This automatically put the label inset _after_ a
1307 // numbered section. It should be possible to extend the mechanism
1308 // to any kind of LateX environement.
1309 // The correct way to fix that bug would be at LateX generation.
1310 // I'll let the code here for reference as it could be used for some
1311 // other feature like "automatic labelling".
1313 Paragraph & par = pars_[cur.pit()];
1314 if (inset->lyxCode() == LABEL_CODE
1315 && !par.layout().counter.empty()) {
1316 // Go to the end of the paragraph
1317 // Warning: Because of Change-Tracking, the last
1318 // position is 'size()' and not 'size()-1':
1319 cur.pos() = par.size();
1320 // Insert a new paragraph
1321 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1325 if (cur.selection())
1326 cutSelection(cur, false);
1328 cur.forceBufferUpdate();
1329 if (inset->editable() && inset->asInsetText())
1330 inset->edit(cur, true);
1334 // trigger InstantPreview now
1335 if (inset->lyxCode() == EXTERNAL_CODE) {
1336 InsetExternal & ins =
1337 static_cast<InsetExternal &>(*inset);
1338 ins.updatePreview();
1345 case LFUN_INSET_DISSOLVE: {
1346 if (dissolveInset(cur)) {
1348 cur.forceBufferUpdate();
1353 case LFUN_GRAPHICS_SET_GROUP: {
1354 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1360 string id = to_utf8(cmd.argument());
1361 string grp = graphics::getGroupParams(bv->buffer(), id);
1362 InsetGraphicsParams tmp, inspar = ins->getParams();
1365 inspar.groupId = to_utf8(cmd.argument());
1367 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1368 tmp.filename = inspar.filename;
1372 ins->setParams(inspar);
1376 case LFUN_SPACE_INSERT:
1377 if (cur.paragraph().layout().free_spacing)
1378 insertChar(cur, ' ');
1380 doInsertInset(cur, this, cmd, false, false);
1383 moveCursor(cur, false);
1386 case LFUN_SPECIALCHAR_INSERT: {
1387 string const name = to_utf8(cmd.argument());
1388 if (name == "hyphenation")
1389 specialChar(cur, InsetSpecialChar::HYPHENATION);
1390 else if (name == "allowbreak")
1391 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1392 else if (name == "ligature-break")
1393 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1394 else if (name == "slash")
1395 specialChar(cur, InsetSpecialChar::SLASH);
1396 else if (name == "nobreakdash")
1397 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1398 else if (name == "dots")
1399 specialChar(cur, InsetSpecialChar::LDOTS);
1400 else if (name == "end-of-sentence")
1401 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1402 else if (name == "menu-separator")
1403 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1404 else if (name == "lyx")
1405 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1406 else if (name == "tex")
1407 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1408 else if (name == "latex")
1409 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1410 else if (name == "latex2e")
1411 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1412 else if (name.empty())
1413 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1415 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1419 case LFUN_IPAMACRO_INSERT: {
1420 string const arg = cmd.getArg(0);
1421 if (arg == "deco") {
1422 // Open the inset, and move the current selection
1424 doInsertInset(cur, this, cmd, true, true);
1426 // Some insets are numbered, others are shown in the outline pane so
1427 // let's update the labels and the toc backend.
1428 cur.forceBufferUpdate();
1431 if (arg == "tone-falling")
1432 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1433 else if (arg == "tone-rising")
1434 ipaChar(cur, InsetIPAChar::TONE_RISING);
1435 else if (arg == "tone-high-rising")
1436 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1437 else if (arg == "tone-low-rising")
1438 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1439 else if (arg == "tone-high-rising-falling")
1440 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1441 else if (arg.empty())
1442 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1444 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1448 case LFUN_WORD_UPCASE:
1449 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1452 case LFUN_WORD_LOWCASE:
1453 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1456 case LFUN_WORD_CAPITALIZE:
1457 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1460 case LFUN_CHARS_TRANSPOSE:
1461 charsTranspose(cur);
1465 cur.message(_("Paste"));
1466 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1467 cap::replaceSelection(cur);
1469 // without argument?
1470 string const arg = to_utf8(cmd.argument());
1472 bool tryGraphics = true;
1473 if (theClipboard().isInternal())
1474 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1475 else if (theClipboard().hasTextContents()) {
1476 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1477 true, Clipboard::AnyTextType))
1478 tryGraphics = false;
1480 if (tryGraphics && theClipboard().hasGraphicsContents())
1481 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1482 } else if (isStrUnsignedInt(arg)) {
1483 // we have a numerical argument
1484 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1485 convert<unsigned int>(arg));
1486 } else if (arg == "html" || arg == "latex") {
1487 Clipboard::TextType type = (arg == "html") ?
1488 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1489 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1491 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1493 type = Clipboard::PdfGraphicsType;
1494 else if (arg == "png")
1495 type = Clipboard::PngGraphicsType;
1496 else if (arg == "jpeg")
1497 type = Clipboard::JpegGraphicsType;
1498 else if (arg == "linkback")
1499 type = Clipboard::LinkBackGraphicsType;
1500 else if (arg == "emf")
1501 type = Clipboard::EmfGraphicsType;
1502 else if (arg == "wmf")
1503 type = Clipboard::WmfGraphicsType;
1505 // we also check in getStatus()
1506 LYXERR0("Unrecognized graphics type: " << arg);
1508 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1511 bv->buffer().errors("Paste");
1512 cur.clearSelection(); // bug 393
1518 cutSelection(cur, true);
1519 cur.message(_("Cut"));
1524 cur.message(_("Copy"));
1527 case LFUN_SERVER_GET_XY:
1528 cur.message(from_utf8(
1529 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1530 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1533 case LFUN_SERVER_SET_XY: {
1536 istringstream is(to_utf8(cmd.argument()));
1539 lyxerr << "SETXY: Could not parse coordinates in '"
1540 << to_utf8(cmd.argument()) << endl;
1542 tm->setCursorFromCoordinates(cur, x, y);
1546 case LFUN_SERVER_GET_LAYOUT:
1547 cur.message(cur.paragraph().layout().name());
1551 case LFUN_LAYOUT_TOGGLE: {
1552 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1553 docstring req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1554 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(req_layout));
1556 docstring layout = resolveLayout(req_layout, cur);
1557 if (layout.empty()) {
1558 cur.errorMessage(from_utf8(N_("Layout ")) + req_layout +
1559 from_utf8(N_(" not known")));
1563 docstring const old_layout = cur.paragraph().layout().name();
1564 bool change_layout = !isAlreadyLayout(layout, cur);
1566 if (cmd.action() == LFUN_LAYOUT_TOGGLE && !change_layout) {
1567 change_layout = true;
1568 layout = resolveLayout(docstring(), cur);
1571 if (change_layout) {
1572 setLayout(cur, layout);
1573 if (cur.pit() > 0 && !ignoreautonests) {
1574 set<docstring> const & autonests =
1575 pars_[cur.pit() - 1].layout().autonests();
1576 set<docstring> const & autonested =
1577 pars_[cur.pit()].layout().isAutonestedBy();
1578 if (autonests.find(layout) != autonests.end()
1579 || autonested.find(old_layout) != autonested.end())
1580 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1584 DocumentClass const & tclass = bv->buffer().params().documentClass();
1585 for (auto const & la_pair : tclass[layout].args()) {
1586 Layout::latexarg const & arg = la_pair.second;
1587 if (arg.autoinsert) {
1588 FuncRequest const cmd2(LFUN_ARGUMENT_INSERT, la_pair.first);
1589 lyx::dispatch(cmd2);
1596 case LFUN_ENVIRONMENT_SPLIT: {
1597 bool const outer = cmd.argument() == "outer";
1598 bool const previous = cmd.argument() == "previous";
1599 bool const before = cmd.argument() == "before";
1600 bool const normal = cmd.argument().empty();
1601 Paragraph const & para = cur.paragraph();
1603 if (para.layout().isEnvironment())
1604 layout = para.layout().name();
1605 depth_type split_depth = cur.paragraph().params().depth();
1606 depth_type nextpar_depth = 0;
1607 if (outer || previous) {
1608 // check if we have an environment in our scope
1609 pit_type pit = cur.pit();
1610 Paragraph cpar = pars_[pit];
1616 if (layout.empty() && previous
1617 && cpar.layout().isEnvironment()
1618 && cpar.params().depth() <= split_depth)
1619 layout = cpar.layout().name();
1620 if (cpar.params().depth() < split_depth
1621 && cpar.layout().isEnvironment()) {
1623 layout = cpar.layout().name();
1624 split_depth = cpar.params().depth();
1626 if (cpar.params().depth() == 0)
1630 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1631 // save nesting of following paragraph
1632 Paragraph cpar = pars_[cur.pit() + 1];
1633 nextpar_depth = cpar.params().depth();
1636 cur.top().setPitPos(cur.pit(), 0);
1637 if (before || cur.pos() > 0)
1638 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1639 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1640 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1642 while (cur.paragraph().params().depth() > split_depth)
1643 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1645 DocumentClass const & tc = bv->buffer().params().documentClass();
1646 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1647 + from_ascii("\" ignoreautonests")));
1648 // FIXME: Bibitem mess!
1649 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
1650 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
1651 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1654 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1655 while (cur.paragraph().params().depth() < split_depth)
1656 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1659 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1660 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1661 if ((outer || normal) && nextpar_depth > 0) {
1662 // restore nesting of following paragraph
1663 DocIterator scur = cur;
1664 depth_type const max_depth = cur.paragraph().params().depth() + 1;
1666 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth))
1667 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1668 cur.setCursor(scur);
1674 case LFUN_CLIPBOARD_PASTE:
1675 cap::replaceSelection(cur);
1676 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1677 cmd.argument() == "paragraph");
1678 bv->buffer().errors("Paste");
1681 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1682 cap::replaceSelection(cur);
1683 pasteSimpleText(cur, cmd.argument() == "paragraph");
1686 case LFUN_PRIMARY_SELECTION_PASTE:
1687 cap::replaceSelection(cur);
1688 pasteString(cur, theSelection().get(),
1689 cmd.argument() == "paragraph");
1692 case LFUN_SELECTION_PASTE:
1693 // Copy the selection buffer to the clipboard stack,
1694 // because we want it to appear in the "Edit->Paste
1696 cap::replaceSelection(cur);
1697 cap::copySelectionToStack();
1698 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1699 bv->buffer().errors("Paste");
1702 case LFUN_QUOTE_INSERT: {
1703 cap::replaceSelection(cur);
1706 Paragraph const & par = cur.paragraph();
1707 pos_type pos = cur.pos();
1708 // Ignore deleted text before cursor
1709 while (pos > 0 && par.isDeleted(pos - 1))
1712 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1714 // Guess quote side.
1715 // A space triggers an opening quote. This is passed if the preceding
1716 // char/inset is a space or at paragraph start.
1718 if (pos > 0 && !par.isSpace(pos - 1)) {
1719 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1720 // If an opening double quotation mark precedes, and this
1721 // is a single quote, make it opening as well
1723 static_cast<InsetQuotes &>(*cur.prevInset());
1724 string const type = ins.getType();
1725 if (!suffixIs(type, "ld") || !inner)
1726 c = par.getChar(pos - 1);
1728 else if (!cur.prevInset()
1729 || (cur.prevInset() && cur.prevInset()->isChar()))
1730 // If a char precedes, pass that and let InsetQuote decide
1731 c = par.getChar(pos - 1);
1734 if (par.getInset(pos - 1)
1735 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1736 // skip "invisible" insets
1740 c = par.getChar(pos - 1);
1745 InsetQuotesParams::QuoteLevel const quote_level = inner
1746 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1747 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1748 cur.buffer()->updateBuffer();
1753 case LFUN_MOUSE_TRIPLE:
1754 if (cmd.button() == mouse_button::button1) {
1755 tm->cursorHome(cur);
1763 case LFUN_MOUSE_DOUBLE:
1764 if (cmd.button() == mouse_button::button1) {
1765 selectWord(cur, WHOLE_WORD);
1770 // Single-click on work area
1771 case LFUN_MOUSE_PRESS: {
1772 // We are not marking a selection with the keyboard in any case.
1773 Cursor & bvcur = cur.bv().cursor();
1774 bvcur.setMark(false);
1775 switch (cmd.button()) {
1776 case mouse_button::button1:
1777 if (!bvcur.selection())
1779 bvcur.resetAnchor();
1780 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1781 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1782 if (bvcur.wordSelection())
1783 selectWord(bvcur, WHOLE_WORD);
1786 case mouse_button::button2:
1787 if (lyxrc.mouse_middlebutton_paste) {
1788 // Middle mouse pasting.
1789 bv->mouseSetCursor(cur);
1791 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1792 "selection-paste ; primary-selection-paste paragraph"));
1794 cur.noScreenUpdate();
1797 case mouse_button::button3: {
1798 // Don't do anything if we right-click a
1799 // selection, a context menu will popup.
1800 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1801 && cur < bvcur.selectionEnd()) {
1802 cur.noScreenUpdate();
1805 if (!bv->mouseSetCursor(cur, false))
1806 cur.screenUpdateFlags(Update::FitCursor);
1812 } // switch (cmd.button())
1815 case LFUN_MOUSE_MOTION: {
1816 // Mouse motion with right or middle mouse do nothing for now.
1817 if (cmd.button() != mouse_button::button1) {
1818 cur.noScreenUpdate();
1821 // ignore motions deeper nested than the real anchor
1822 Cursor & bvcur = cur.bv().cursor();
1823 if (!bvcur.realAnchor().hasPart(cur)) {
1827 CursorSlice old = bvcur.top();
1829 int const wh = bv->workHeight();
1830 int const y = max(0, min(wh - 1, cmd.y()));
1832 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1833 cur.setTargetX(cmd.x());
1834 // Don't allow selecting a separator inset
1835 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1838 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1839 else if (cmd.y() < 0)
1840 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1841 // This is to allow jumping over large insets
1842 if (cur.top() == old) {
1844 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1845 else if (cmd.y() < 0)
1846 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1848 // We continue with our existing selection or start a new one, so don't
1849 // reset the anchor.
1850 bvcur.setCursor(cur);
1851 bvcur.selection(true);
1852 bvcur.setCurrentFont();
1853 if (cur.top() == old) {
1854 // We didn't move one iota, so no need to update the screen.
1855 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1856 //cur.noScreenUpdate();
1862 case LFUN_MOUSE_RELEASE:
1863 switch (cmd.button()) {
1864 case mouse_button::button1:
1865 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1866 // If there is a new selection, update persistent selection;
1867 // otherwise, single click does not clear persistent selection
1869 if (cur.selection()) {
1870 // Finish selection. If double click,
1871 // cur is moved to the end of word by
1872 // selectWord but bvcur is current
1874 cur.bv().cursor().setSelection();
1875 // We might have removed an empty but drawn selection
1876 // (probably a margin)
1877 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1879 cur.noScreenUpdate();
1880 // FIXME: We could try to handle drag and drop of selection here.
1883 case mouse_button::button2:
1884 // Middle mouse pasting is handled at mouse press time,
1885 // see LFUN_MOUSE_PRESS.
1886 cur.noScreenUpdate();
1889 case mouse_button::button3:
1890 // Cursor was set at LFUN_MOUSE_PRESS time.
1891 // FIXME: If there is a selection we could try to handle a special
1892 // drag & drop context menu.
1893 cur.noScreenUpdate();
1896 case mouse_button::none:
1897 case mouse_button::button4:
1898 case mouse_button::button5:
1900 } // switch (cmd.button())
1904 case LFUN_SELF_INSERT: {
1905 if (cmd.argument().empty())
1908 // Automatically delete the currently selected
1909 // text and replace it with what is being
1910 // typed in now. Depends on lyxrc settings
1911 // "auto_region_delete", which defaults to
1914 if (lyxrc.auto_region_delete && cur.selection())
1915 cutSelection(cur, false);
1916 cur.clearSelection();
1918 for (char_type c : cmd.argument())
1919 bv->translateAndInsert(c, this, cur);
1922 moveCursor(cur, false);
1923 cur.markNewWordPosition();
1924 bv->bookmarkEditPosition();
1928 case LFUN_HREF_INSERT: {
1929 docstring content = cmd.argument();
1930 if (content.empty() && cur.selection())
1931 content = cur.selectionAsString(false);
1933 InsetCommandParams p(HYPERLINK_CODE);
1934 if (!content.empty()){
1935 // if it looks like a link, we'll put it as target,
1936 // otherwise as name (bug #8792).
1939 // regex_match(to_utf8(content), matches, link_re)
1940 // because smatch stores pointers to the substrings rather
1941 // than making copies of them. And those pointers become
1942 // invalid after regex_match returns, since it is then
1943 // being given a temporary object. (Thanks to Georg for
1944 // figuring that out.)
1945 regex const link_re("^([a-z]+):.*");
1947 string const c = to_utf8(lowercase(content));
1949 if (c.substr(0,7) == "mailto:") {
1950 p["target"] = content;
1951 p["type"] = from_ascii("mailto:");
1952 } else if (regex_match(c, matches, link_re)) {
1953 p["target"] = content;
1954 string protocol = matches.str(1);
1955 if (protocol == "file")
1956 p["type"] = from_ascii("file:");
1958 p["name"] = content;
1960 string const data = InsetCommand::params2string(p);
1962 // we need to have a target. if we already have one, then
1963 // that gets used at the default for the name, too, which
1964 // is probably what is wanted.
1965 if (p["target"].empty()) {
1966 bv->showDialog("href", data);
1968 FuncRequest fr(LFUN_INSET_INSERT, data);
1974 case LFUN_LABEL_INSERT: {
1975 InsetCommandParams p(LABEL_CODE);
1976 // Try to generate a valid label
1977 p["name"] = (cmd.argument().empty()) ?
1978 cur.getPossibleLabel() :
1980 string const data = InsetCommand::params2string(p);
1982 if (cmd.argument().empty()) {
1983 bv->showDialog("label", data);
1985 FuncRequest fr(LFUN_INSET_INSERT, data);
1991 case LFUN_INFO_INSERT: {
1992 if (cmd.argument().empty()) {
1993 bv->showDialog("info", cur.current_font.language()->lang());
1996 inset = createInset(cur.buffer(), cmd);
2000 insertInset(cur, inset);
2001 cur.forceBufferUpdate();
2006 case LFUN_CAPTION_INSERT:
2007 case LFUN_FOOTNOTE_INSERT:
2008 case LFUN_NOTE_INSERT:
2009 case LFUN_BOX_INSERT:
2010 case LFUN_BRANCH_INSERT:
2011 case LFUN_PHANTOM_INSERT:
2012 case LFUN_ERT_INSERT:
2013 case LFUN_LISTING_INSERT:
2014 case LFUN_MARGINALNOTE_INSERT:
2015 case LFUN_ARGUMENT_INSERT:
2016 case LFUN_INDEX_INSERT:
2017 case LFUN_PREVIEW_INSERT:
2018 case LFUN_SCRIPT_INSERT:
2019 case LFUN_IPA_INSERT:
2020 // Open the inset, and move the current selection
2022 doInsertInset(cur, this, cmd, true, true);
2024 // Some insets are numbered, others are shown in the outline pane so
2025 // let's update the labels and the toc backend.
2026 cur.forceBufferUpdate();
2029 case LFUN_FLEX_INSERT: {
2030 // Open the inset, and move the current selection
2032 bool const sel = cur.selection();
2033 doInsertInset(cur, this, cmd, true, true);
2034 // Insert auto-insert arguments
2035 bool autoargs = false;
2036 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
2037 Layout::LaTeXArgMap::const_iterator lait = args.begin();
2038 Layout::LaTeXArgMap::const_iterator const laend = args.end();
2039 for (; lait != laend; ++lait) {
2040 Layout::latexarg arg = (*lait).second;
2041 if (arg.autoinsert) {
2042 // The cursor might have been invalidated by the replaceSelection.
2043 cur.buffer()->changed(true);
2044 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first);
2045 lyx::dispatch(cmd2);
2051 cur.leaveInset(cur.inset());
2054 // Some insets are numbered, others are shown in the outline pane so
2055 // let's update the labels and the toc backend.
2056 cur.forceBufferUpdate();
2060 case LFUN_TABULAR_INSERT:
2061 // if there were no arguments, just open the dialog
2062 if (doInsertInset(cur, this, cmd, false, true))
2065 bv->showDialog("tabularcreate");
2069 case LFUN_FLOAT_INSERT:
2070 case LFUN_FLOAT_WIDE_INSERT:
2071 case LFUN_WRAP_INSERT: {
2072 // will some content be moved into the inset?
2073 bool const content = cur.selection();
2074 // does the content consist of multiple paragraphs?
2075 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2077 doInsertInset(cur, this, cmd, true, true);
2080 // If some single-par content is moved into the inset,
2081 // doInsertInset puts the cursor outside the inset.
2082 // To insert the caption we put it back into the inset.
2083 // FIXME cleanup doInsertInset to avoid such dances!
2084 if (content && singlepar)
2087 ParagraphList & pars = cur.text()->paragraphs();
2089 DocumentClass const & tclass = bv->buffer().params().documentClass();
2091 // add a separate paragraph for the caption inset
2092 pars.push_back(Paragraph());
2093 pars.back().setInsetOwner(&cur.text()->inset());
2094 pars.back().setPlainOrDefaultLayout(tclass);
2095 int cap_pit = pars.size() - 1;
2097 // if an empty inset was created, we create an additional empty
2098 // paragraph at the bottom so that the user can choose where to put
2099 // the graphics (or table).
2101 pars.push_back(Paragraph());
2102 pars.back().setInsetOwner(&cur.text()->inset());
2103 pars.back().setPlainOrDefaultLayout(tclass);
2106 // reposition the cursor to the caption
2107 cur.pit() = cap_pit;
2109 // FIXME: This Text/Cursor dispatch handling is a mess!
2110 // We cannot use Cursor::dispatch here it needs access to up to
2112 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2113 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2114 cur.forceBufferUpdate();
2115 cur.screenUpdateFlags(Update::Force);
2116 // FIXME: When leaving the Float (or Wrap) inset we should
2117 // delete any empty paragraph left above or below the
2122 case LFUN_NOMENCL_INSERT: {
2123 InsetCommandParams p(NOMENCL_CODE);
2124 if (cmd.argument().empty()) {
2126 bv->cursor().innerText()->getStringForDialog(bv->cursor());
2127 cur.clearSelection();
2129 p["symbol"] = cmd.argument();
2130 string const data = InsetCommand::params2string(p);
2131 bv->showDialog("nomenclature", data);
2135 case LFUN_INDEX_PRINT: {
2136 InsetCommandParams p(INDEX_PRINT_CODE);
2137 if (cmd.argument().empty())
2138 p["type"] = from_ascii("idx");
2140 p["type"] = cmd.argument();
2141 string const data = InsetCommand::params2string(p);
2142 FuncRequest fr(LFUN_INSET_INSERT, data);
2147 case LFUN_NOMENCL_PRINT:
2148 case LFUN_NEWPAGE_INSERT:
2150 doInsertInset(cur, this, cmd, false, false);
2154 case LFUN_SEPARATOR_INSERT: {
2155 doInsertInset(cur, this, cmd, false, false);
2157 // remove a following space
2158 Paragraph & par = cur.paragraph();
2159 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2160 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2164 case LFUN_DEPTH_DECREMENT:
2165 changeDepth(cur, DEC_DEPTH);
2168 case LFUN_DEPTH_INCREMENT:
2169 changeDepth(cur, INC_DEPTH);
2172 case LFUN_REGEXP_MODE:
2173 regexpDispatch(cur, cmd);
2176 case LFUN_MATH_MODE: {
2177 if (cmd.argument() == "on" || cmd.argument() == "") {
2178 // don't pass "on" as argument
2179 // (it would appear literally in the first cell)
2180 docstring sel = cur.selectionAsString(false);
2181 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2182 // create a macro template if we see "\\newcommand" somewhere, and
2183 // an ordinary formula otherwise
2185 && (sel.find(from_ascii("\\newcommand")) != string::npos
2186 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2187 || sel.find(from_ascii("\\def")) != string::npos)
2188 && macro->fromString(sel)) {
2190 replaceSelection(cur);
2193 // no meaningful macro template was found
2195 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2198 // The argument is meaningful
2199 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2200 // has a different meaning in math mode
2201 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2205 case LFUN_MATH_MACRO:
2206 if (cmd.argument().empty())
2207 cur.errorMessage(from_utf8(N_("Missing argument")));
2210 string s = to_utf8(cmd.argument());
2211 string const s1 = token(s, ' ', 1);
2212 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2213 string const s2 = token(s, ' ', 2);
2214 MacroType type = MacroTypeNewcommand;
2216 type = MacroTypeDef;
2217 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2218 from_utf8(token(s, ' ', 0)), nargs, false, type);
2219 inset->setBuffer(bv->buffer());
2220 insertInset(cur, inset);
2222 // enter macro inset and select the name
2224 cur.top().pos() = cur.top().lastpos();
2226 cur.selection(true);
2227 cur.top().pos() = 0;
2231 case LFUN_MATH_DISPLAY:
2232 case LFUN_MATH_SUBSCRIPT:
2233 case LFUN_MATH_SUPERSCRIPT:
2234 case LFUN_MATH_INSERT:
2235 case LFUN_MATH_AMS_MATRIX:
2236 case LFUN_MATH_MATRIX:
2237 case LFUN_MATH_DELIM:
2238 case LFUN_MATH_BIGDELIM:
2239 mathDispatch(cur, cmd);
2242 case LFUN_FONT_EMPH: {
2243 Font font(ignore_font, ignore_language);
2244 font.fontInfo().setEmph(FONT_TOGGLE);
2245 toggleAndShow(cur, this, font);
2249 case LFUN_FONT_ITAL: {
2250 Font font(ignore_font, ignore_language);
2251 font.fontInfo().setShape(ITALIC_SHAPE);
2252 toggleAndShow(cur, this, font);
2256 case LFUN_FONT_BOLD:
2257 case LFUN_FONT_BOLDSYMBOL: {
2258 Font font(ignore_font, ignore_language);
2259 font.fontInfo().setSeries(BOLD_SERIES);
2260 toggleAndShow(cur, this, font);
2264 case LFUN_FONT_NOUN: {
2265 Font font(ignore_font, ignore_language);
2266 font.fontInfo().setNoun(FONT_TOGGLE);
2267 toggleAndShow(cur, this, font);
2271 case LFUN_FONT_TYPEWRITER: {
2272 Font font(ignore_font, ignore_language);
2273 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2274 toggleAndShow(cur, this, font);
2278 case LFUN_FONT_SANS: {
2279 Font font(ignore_font, ignore_language);
2280 font.fontInfo().setFamily(SANS_FAMILY);
2281 toggleAndShow(cur, this, font);
2285 case LFUN_FONT_ROMAN: {
2286 Font font(ignore_font, ignore_language);
2287 font.fontInfo().setFamily(ROMAN_FAMILY);
2288 toggleAndShow(cur, this, font);
2292 case LFUN_FONT_DEFAULT: {
2293 Font font(inherit_font, ignore_language);
2294 toggleAndShow(cur, this, font);
2298 case LFUN_FONT_STRIKEOUT: {
2299 Font font(ignore_font, ignore_language);
2300 font.fontInfo().setStrikeout(FONT_TOGGLE);
2301 toggleAndShow(cur, this, font);
2305 case LFUN_FONT_CROSSOUT: {
2306 Font font(ignore_font, ignore_language);
2307 font.fontInfo().setXout(FONT_TOGGLE);
2308 toggleAndShow(cur, this, font);
2312 case LFUN_FONT_UNDERUNDERLINE: {
2313 Font font(ignore_font, ignore_language);
2314 font.fontInfo().setUuline(FONT_TOGGLE);
2315 toggleAndShow(cur, this, font);
2319 case LFUN_FONT_UNDERWAVE: {
2320 Font font(ignore_font, ignore_language);
2321 font.fontInfo().setUwave(FONT_TOGGLE);
2322 toggleAndShow(cur, this, font);
2326 case LFUN_FONT_UNDERLINE: {
2327 Font font(ignore_font, ignore_language);
2328 font.fontInfo().setUnderbar(FONT_TOGGLE);
2329 toggleAndShow(cur, this, font);
2333 case LFUN_FONT_SIZE: {
2334 Font font(ignore_font, ignore_language);
2335 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2336 toggleAndShow(cur, this, font);
2340 case LFUN_LANGUAGE: {
2341 string const lang_arg = cmd.getArg(0);
2342 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2343 Language const * lang =
2344 reset ? reset_language
2345 : languages.getLanguage(lang_arg);
2346 // we allow reset_language, which is 0, but only if it
2347 // was requested via empty or "reset" arg.
2348 if (!lang && !reset)
2350 bool const toggle = (cmd.getArg(1) != "set");
2351 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2352 Font font(ignore_font, lang);
2353 toggleAndShow(cur, this, font, toggle);
2354 // We need a buffer update if we change the language
2356 if (cur.insetInSelection(INFO_CODE))
2357 cur.forceBufferUpdate();
2361 case LFUN_TEXTSTYLE_APPLY:
2362 toggleAndShow(cur, this, freefont, toggleall);
2363 cur.message(_("Character set"));
2366 // Set the freefont using the contents of \param data dispatched from
2367 // the frontends and apply it at the current cursor location.
2368 case LFUN_TEXTSTYLE_UPDATE: {
2371 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2374 toggleAndShow(cur, this, freefont, toggleall);
2375 // We need a buffer update if we change the language
2377 if (cur.insetInSelection(INFO_CODE))
2378 cur.forceBufferUpdate();
2379 cur.message(_("Character set"));
2381 lyxerr << "Argument not ok";
2386 case LFUN_FINISHED_LEFT:
2387 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2388 // We're leaving an inset, going left. If the inset is LTR, we're
2389 // leaving from the front, so we should not move (remain at --- but
2390 // not in --- the inset). If the inset is RTL, move left, without
2391 // entering the inset itself; i.e., move to after the inset.
2392 if (cur.paragraph().getFontSettings(
2393 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2394 cursorVisLeft(cur, true);
2397 case LFUN_FINISHED_RIGHT:
2398 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2399 // We're leaving an inset, going right. If the inset is RTL, we're
2400 // leaving from the front, so we should not move (remain at --- but
2401 // not in --- the inset). If the inset is LTR, move right, without
2402 // entering the inset itself; i.e., move to after the inset.
2403 if (!cur.paragraph().getFontSettings(
2404 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2405 cursorVisRight(cur, true);
2408 case LFUN_FINISHED_BACKWARD:
2409 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2410 cur.setCurrentFont();
2413 case LFUN_FINISHED_FORWARD:
2414 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2416 cur.setCurrentFont();
2419 case LFUN_LAYOUT_PARAGRAPH: {
2421 params2string(cur.paragraph(), data);
2422 data = "show\n" + data;
2423 bv->showDialog("paragraph", data);
2427 case LFUN_PARAGRAPH_UPDATE: {
2429 params2string(cur.paragraph(), data);
2431 // Will the paragraph accept changes from the dialog?
2433 cur.inset().allowParagraphCustomization(cur.idx());
2435 data = "update " + convert<string>(accept) + '\n' + data;
2436 bv->updateDialog("paragraph", data);
2440 case LFUN_ACCENT_UMLAUT:
2441 case LFUN_ACCENT_CIRCUMFLEX:
2442 case LFUN_ACCENT_GRAVE:
2443 case LFUN_ACCENT_ACUTE:
2444 case LFUN_ACCENT_TILDE:
2445 case LFUN_ACCENT_PERISPOMENI:
2446 case LFUN_ACCENT_CEDILLA:
2447 case LFUN_ACCENT_MACRON:
2448 case LFUN_ACCENT_DOT:
2449 case LFUN_ACCENT_UNDERDOT:
2450 case LFUN_ACCENT_UNDERBAR:
2451 case LFUN_ACCENT_CARON:
2452 case LFUN_ACCENT_BREVE:
2453 case LFUN_ACCENT_TIE:
2454 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2455 case LFUN_ACCENT_CIRCLE:
2456 case LFUN_ACCENT_OGONEK:
2457 theApp()->handleKeyFunc(cmd.action());
2458 if (!cmd.argument().empty())
2459 // FIXME: Are all these characters encoded in one byte in utf8?
2460 bv->translateAndInsert(cmd.argument()[0], this, cur);
2461 cur.screenUpdateFlags(Update::FitCursor);
2464 case LFUN_FLOAT_LIST_INSERT: {
2465 DocumentClass const & tclass = bv->buffer().params().documentClass();
2466 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2468 if (cur.selection())
2469 cutSelection(cur, false);
2470 breakParagraph(cur);
2472 if (cur.lastpos() != 0) {
2473 cursorBackward(cur);
2474 breakParagraph(cur);
2477 docstring const laystr = cur.inset().usePlainLayout() ?
2478 tclass.plainLayoutName() :
2479 tclass.defaultLayoutName();
2480 setLayout(cur, laystr);
2481 ParagraphParameters p;
2482 // FIXME If this call were replaced with one to clearParagraphParams(),
2483 // then we could get rid of this method altogether.
2484 setParagraphs(cur, p);
2485 // FIXME This should be simplified when InsetFloatList takes a
2486 // Buffer in its constructor.
2487 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2488 ifl->setBuffer(bv->buffer());
2489 insertInset(cur, ifl);
2492 lyxerr << "Non-existent float type: "
2493 << to_utf8(cmd.argument()) << endl;
2498 case LFUN_CHANGE_ACCEPT: {
2499 acceptOrRejectChanges(cur, ACCEPT);
2503 case LFUN_CHANGE_REJECT: {
2504 acceptOrRejectChanges(cur, REJECT);
2508 case LFUN_THESAURUS_ENTRY: {
2509 Language const * language = cur.getFont().language();
2510 docstring arg = cmd.argument();
2512 arg = cur.selectionAsString(false);
2514 if (arg.size() > 100 || arg.empty()) {
2515 // Get word or selection
2516 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2517 arg = cur.selectionAsString(false);
2518 arg += " lang=" + from_ascii(language->lang());
2521 string lang = cmd.getArg(1);
2522 // This duplicates the code in GuiThesaurus::initialiseParams
2523 if (prefixIs(lang, "lang=")) {
2524 language = languages.getLanguage(lang.substr(5));
2526 language = cur.getFont().language();
2529 string lang = language->code();
2530 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2531 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2532 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2533 _("The path to the thesaurus directory has not been specified.\n"
2534 "The thesaurus is not functional.\n"
2535 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2538 bv->showDialog("thesaurus", to_utf8(arg));
2542 case LFUN_SPELLING_ADD: {
2543 Language const * language = getLanguage(cur, cmd.getArg(1));
2544 docstring word = from_utf8(cmd.getArg(0));
2546 word = cur.selectionAsString(false);
2548 if (word.size() > 100 || word.empty()) {
2549 // Get word or selection
2550 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2551 word = cur.selectionAsString(false);
2554 WordLangTuple wl(word, language);
2555 theSpellChecker()->insert(wl);
2559 case LFUN_SPELLING_IGNORE: {
2560 Language const * language = getLanguage(cur, cmd.getArg(1));
2561 docstring word = from_utf8(cmd.getArg(0));
2563 word = cur.selectionAsString(false);
2565 if (word.size() > 100 || word.empty()) {
2566 // Get word or selection
2567 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2568 word = cur.selectionAsString(false);
2571 WordLangTuple wl(word, language);
2572 theSpellChecker()->accept(wl);
2576 case LFUN_SPELLING_REMOVE: {
2577 Language const * language = getLanguage(cur, cmd.getArg(1));
2578 docstring word = from_utf8(cmd.getArg(0));
2580 word = cur.selectionAsString(false);
2582 if (word.size() > 100 || word.empty()) {
2583 // Get word or selection
2584 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2585 word = cur.selectionAsString(false);
2588 WordLangTuple wl(word, language);
2589 theSpellChecker()->remove(wl);
2593 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2594 // Given data, an encoding of the ParagraphParameters
2595 // generated in the Paragraph dialog, this function sets
2596 // the current paragraph, or currently selected paragraphs,
2598 // NOTE: This function overrides all existing settings.
2599 setParagraphs(cur, cmd.argument());
2600 cur.message(_("Paragraph layout set"));
2604 case LFUN_PARAGRAPH_PARAMS: {
2605 // Given data, an encoding of the ParagraphParameters as we'd
2606 // find them in a LyX file, this function modifies the current paragraph,
2607 // or currently selected paragraphs.
2608 // NOTE: This function only modifies, and does not override, existing
2610 setParagraphs(cur, cmd.argument(), true);
2611 cur.message(_("Paragraph layout set"));
2616 if (cur.selection()) {
2617 cur.selection(false);
2620 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2621 // correct, but I'm not 100% sure -- dov, 071019
2622 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2626 case LFUN_OUTLINE_UP:
2627 outline(OutlineUp, cur, this);
2628 setCursor(cur, cur.pit(), 0);
2629 cur.forceBufferUpdate();
2633 case LFUN_OUTLINE_DOWN:
2634 outline(OutlineDown, cur, this);
2635 setCursor(cur, cur.pit(), 0);
2636 cur.forceBufferUpdate();
2640 case LFUN_OUTLINE_IN:
2641 outline(OutlineIn, cur, this);
2642 cur.forceBufferUpdate();
2646 case LFUN_OUTLINE_OUT:
2647 outline(OutlineOut, cur, this);
2648 cur.forceBufferUpdate();
2652 case LFUN_SERVER_GET_STATISTICS:
2654 DocIterator from, to;
2655 if (cur.selection()) {
2656 from = cur.selectionBegin();
2657 to = cur.selectionEnd();
2659 from = doc_iterator_begin(cur.buffer());
2660 to = doc_iterator_end(cur.buffer());
2663 cur.buffer()->updateStatistics(from, to);
2664 string const arg0 = cmd.getArg(0);
2665 if (arg0 == "words") {
2666 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2667 } else if (arg0 == "chars") {
2668 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2669 } else if (arg0 == "chars-space") {
2670 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2672 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2673 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2674 + convert<docstring>(cur.buffer()->charCount(true)));
2680 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2685 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2687 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2688 // Check for misspelled text
2689 // The redraw is useful because of the painting of
2690 // misspelled markers depends on the cursor position.
2691 // Trigger a redraw for cursor moves inside misspelled text.
2692 if (!cur.inTexted()) {
2693 // move from regular text to math
2694 needsUpdate = last_misspelled;
2695 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2696 // move inside regular text
2697 needsUpdate = last_misspelled
2698 || cur.paragraph().isMisspelled(cur.pos(), true);
2702 // FIXME: The cursor flag is reset two lines below
2703 // so we need to check here if some of the LFUN did touch that.
2704 // for now only Text::erase() and Text::backspace() do that.
2705 // The plan is to verify all the LFUNs and then to remove this
2706 // singleParUpdate boolean altogether.
2707 if (cur.result().screenUpdate() & Update::Force) {
2708 singleParUpdate = false;
2712 // FIXME: the following code should go in favor of fine grained
2713 // update flag treatment.
2714 if (singleParUpdate) {
2715 // Inserting characters does not change par height in general. So, try
2716 // to update _only_ this paragraph. BufferView will detect if a full
2717 // metrics update is needed anyway.
2718 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2722 && &oldTopSlice.inset() == &cur.inset()
2723 && oldTopSlice.idx() == cur.idx()
2724 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2725 && !cur.selection())
2726 // FIXME: it would be better if we could just do this
2728 //if (cur.result().update() != Update::FitCursor)
2729 // cur.noScreenUpdate();
2731 // But some LFUNs do not set Update::FitCursor when needed, so we
2732 // do it for all. This is not very harmfull as FitCursor will provoke
2733 // a full redraw only if needed but still, a proper review of all LFUN
2734 // should be done and this needsUpdate boolean can then be removed.
2735 cur.screenUpdateFlags(Update::FitCursor);
2737 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2741 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2742 FuncStatus & flag) const
2744 LBUFERR(this == cur.text());
2746 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2748 bool allow_in_passthru = false;
2749 InsetCode code = NO_CODE;
2751 switch (cmd.action()) {
2753 case LFUN_DEPTH_DECREMENT:
2754 enable = changeDepthAllowed(cur, DEC_DEPTH);
2757 case LFUN_DEPTH_INCREMENT:
2758 enable = changeDepthAllowed(cur, INC_DEPTH);
2762 // FIXME We really should not allow this to be put, e.g.,
2763 // in a footnote, or in ERT. But it would make sense in a
2764 // branch, so I'm not sure what to do.
2765 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2768 case LFUN_DIALOG_SHOW_NEW_INSET:
2769 if (cmd.argument() == "bibitem")
2770 code = BIBITEM_CODE;
2771 else if (cmd.argument() == "bibtex") {
2773 // not allowed in description items
2774 enable = !inDescriptionItem(cur);
2776 else if (cmd.argument() == "box")
2778 else if (cmd.argument() == "branch")
2780 else if (cmd.argument() == "citation")
2782 else if (cmd.argument() == "ert")
2784 else if (cmd.argument() == "external")
2785 code = EXTERNAL_CODE;
2786 else if (cmd.argument() == "float")
2788 else if (cmd.argument() == "graphics")
2789 code = GRAPHICS_CODE;
2790 else if (cmd.argument() == "href")
2791 code = HYPERLINK_CODE;
2792 else if (cmd.argument() == "include")
2793 code = INCLUDE_CODE;
2794 else if (cmd.argument() == "index")
2796 else if (cmd.argument() == "index_print")
2797 code = INDEX_PRINT_CODE;
2798 else if (cmd.argument() == "listings")
2799 code = LISTINGS_CODE;
2800 else if (cmd.argument() == "mathspace")
2801 code = MATH_HULL_CODE;
2802 else if (cmd.argument() == "nomenclature")
2803 code = NOMENCL_CODE;
2804 else if (cmd.argument() == "nomencl_print")
2805 code = NOMENCL_PRINT_CODE;
2806 else if (cmd.argument() == "label")
2808 else if (cmd.argument() == "line")
2810 else if (cmd.argument() == "note")
2812 else if (cmd.argument() == "phantom")
2813 code = PHANTOM_CODE;
2814 else if (cmd.argument() == "ref")
2816 else if (cmd.argument() == "space")
2818 else if (cmd.argument() == "toc")
2820 else if (cmd.argument() == "vspace")
2822 else if (cmd.argument() == "wrap")
2826 case LFUN_ERT_INSERT:
2829 case LFUN_LISTING_INSERT:
2830 code = LISTINGS_CODE;
2831 // not allowed in description items
2832 enable = !inDescriptionItem(cur);
2834 case LFUN_FOOTNOTE_INSERT:
2837 case LFUN_TABULAR_INSERT:
2838 code = TABULAR_CODE;
2840 case LFUN_MARGINALNOTE_INSERT:
2843 case LFUN_FLOAT_INSERT:
2844 case LFUN_FLOAT_WIDE_INSERT:
2845 // FIXME: If there is a selection, we should check whether there
2846 // are floats in the selection, but this has performance issues, see
2847 // LFUN_CHANGE_ACCEPT/REJECT.
2849 if (inDescriptionItem(cur))
2850 // not allowed in description items
2853 InsetCode const inset_code = cur.inset().lyxCode();
2855 // algorithm floats cannot be put in another float
2856 if (to_utf8(cmd.argument()) == "algorithm") {
2857 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2861 // for figures and tables: only allow in another
2862 // float or wrap if it is of the same type and
2863 // not a subfloat already
2864 if(cur.inset().lyxCode() == code) {
2865 InsetFloat const & ins =
2866 static_cast<InsetFloat const &>(cur.inset());
2867 enable = ins.params().type == to_utf8(cmd.argument())
2868 && !ins.params().subfloat;
2869 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2870 InsetWrap const & ins =
2871 static_cast<InsetWrap const &>(cur.inset());
2872 enable = ins.params().type == to_utf8(cmd.argument());
2876 case LFUN_WRAP_INSERT:
2878 // not allowed in description items
2879 enable = !inDescriptionItem(cur);
2881 case LFUN_FLOAT_LIST_INSERT: {
2882 code = FLOAT_LIST_CODE;
2883 // not allowed in description items
2884 enable = !inDescriptionItem(cur);
2886 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2887 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2888 // make sure we know about such floats
2889 if (cit == floats.end() ||
2890 // and that we know how to generate a list of them
2891 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2892 flag.setUnknown(true);
2893 // probably not necessary, but...
2899 case LFUN_CAPTION_INSERT: {
2900 code = CAPTION_CODE;
2901 string arg = cmd.getArg(0);
2902 bool varia = arg != "Unnumbered"
2903 && cur.inset().allowsCaptionVariation(arg);
2904 // not allowed in description items,
2905 // and in specific insets
2906 enable = !inDescriptionItem(cur)
2907 && (varia || arg.empty() || arg == "Standard");
2910 case LFUN_NOTE_INSERT:
2913 case LFUN_FLEX_INSERT: {
2915 string s = cmd.getArg(0);
2917 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2918 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2919 il.lyxtype() != InsetLayout::CUSTOM &&
2920 il.lyxtype() != InsetLayout::ELEMENT &&
2921 il.lyxtype ()!= InsetLayout::STANDARD)
2925 case LFUN_BOX_INSERT:
2928 case LFUN_BRANCH_INSERT:
2930 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2931 && cur.buffer()->params().branchlist().empty())
2934 case LFUN_IPA_INSERT:
2937 case LFUN_PHANTOM_INSERT:
2938 code = PHANTOM_CODE;
2940 case LFUN_LABEL_INSERT:
2943 case LFUN_INFO_INSERT:
2945 enable = infoparams.validateArgument(cur.buffer(), cmd.argument(), true);
2947 case LFUN_ARGUMENT_INSERT: {
2949 allow_in_passthru = true;
2950 string const arg = cmd.getArg(0);
2955 Layout const & lay = cur.paragraph().layout();
2956 Layout::LaTeXArgMap args = lay.args();
2957 Layout::LaTeXArgMap::const_iterator const lait =
2959 if (lait != args.end()) {
2961 pit_type pit = cur.pit();
2962 pit_type lastpit = cur.pit();
2963 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2964 // In a sequence of "merged" environment layouts, we only allow
2965 // non-item arguments once.
2966 lastpit = cur.lastpit();
2967 // get the first paragraph in sequence with this layout
2968 depth_type const current_depth = cur.paragraph().params().depth();
2972 Paragraph cpar = pars_[pit - 1];
2973 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2979 for (; pit <= lastpit; ++pit) {
2980 if (pars_[pit].layout() != lay)
2982 for (auto const & table : pars_[pit].insetList())
2983 if (InsetArgument const * ins = table.inset->asInsetArgument())
2984 if (ins->name() == arg) {
2985 // we have this already
2994 case LFUN_INDEX_INSERT:
2997 case LFUN_INDEX_PRINT:
2998 code = INDEX_PRINT_CODE;
2999 // not allowed in description items
3000 enable = !inDescriptionItem(cur);
3002 case LFUN_NOMENCL_INSERT:
3003 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3007 code = NOMENCL_CODE;
3009 case LFUN_NOMENCL_PRINT:
3010 code = NOMENCL_PRINT_CODE;
3011 // not allowed in description items
3012 enable = !inDescriptionItem(cur);
3014 case LFUN_HREF_INSERT:
3015 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3019 code = HYPERLINK_CODE;
3021 case LFUN_IPAMACRO_INSERT: {
3022 string const arg = cmd.getArg(0);
3024 code = IPADECO_CODE;
3026 code = IPACHAR_CODE;
3029 case LFUN_QUOTE_INSERT:
3030 // always allow this, since we will inset a raw quote
3031 // if an inset is not allowed.
3032 allow_in_passthru = true;
3034 case LFUN_SPECIALCHAR_INSERT:
3035 code = SPECIALCHAR_CODE;
3037 case LFUN_SPACE_INSERT:
3038 // slight hack: we know this is allowed in math mode
3042 case LFUN_PREVIEW_INSERT:
3043 code = PREVIEW_CODE;
3045 case LFUN_SCRIPT_INSERT:
3049 case LFUN_MATH_INSERT:
3050 case LFUN_MATH_AMS_MATRIX:
3051 case LFUN_MATH_MATRIX:
3052 case LFUN_MATH_DELIM:
3053 case LFUN_MATH_BIGDELIM:
3054 case LFUN_MATH_DISPLAY:
3055 case LFUN_MATH_MODE:
3056 case LFUN_MATH_MACRO:
3057 case LFUN_MATH_SUBSCRIPT:
3058 case LFUN_MATH_SUPERSCRIPT:
3059 code = MATH_HULL_CODE;
3062 case LFUN_REGEXP_MODE:
3063 code = MATH_HULL_CODE;
3064 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3067 case LFUN_INSET_MODIFY:
3068 // We need to disable this, because we may get called for a
3070 // InsetTabular::getStatus() -> InsetText::getStatus()
3071 // and we don't handle LFUN_INSET_MODIFY.
3075 case LFUN_FONT_EMPH:
3076 flag.setOnOff(fontinfo.emph() == FONT_ON);
3077 enable = !cur.paragraph().isPassThru();
3080 case LFUN_FONT_ITAL:
3081 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3082 enable = !cur.paragraph().isPassThru();
3085 case LFUN_FONT_NOUN:
3086 flag.setOnOff(fontinfo.noun() == FONT_ON);
3087 enable = !cur.paragraph().isPassThru();
3090 case LFUN_FONT_BOLD:
3091 case LFUN_FONT_BOLDSYMBOL:
3092 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3093 enable = !cur.paragraph().isPassThru();
3096 case LFUN_FONT_SANS:
3097 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3098 enable = !cur.paragraph().isPassThru();
3101 case LFUN_FONT_ROMAN:
3102 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3103 enable = !cur.paragraph().isPassThru();
3106 case LFUN_FONT_TYPEWRITER:
3107 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3108 enable = !cur.paragraph().isPassThru();
3113 enable = cur.selection();
3117 if (cmd.argument().empty()) {
3118 if (theClipboard().isInternal())
3119 enable = cap::numberOfSelections() > 0;
3121 enable = !theClipboard().empty();
3125 // we have an argument
3126 string const arg = to_utf8(cmd.argument());
3127 if (isStrUnsignedInt(arg)) {
3128 // it's a number and therefore means the internal stack
3129 unsigned int n = convert<unsigned int>(arg);
3130 enable = cap::numberOfSelections() > n;
3134 // explicit text type?
3135 if (arg == "html") {
3136 // Do not enable for PlainTextType, since some tidying in the
3137 // frontend is needed for HTML, which is too unsafe for plain text.
3138 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3140 } else if (arg == "latex") {
3141 // LaTeX is usually not available on the clipboard with
3142 // the correct MIME type, but in plain text.
3143 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3144 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3148 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3150 type = Clipboard::PdfGraphicsType;
3151 else if (arg == "png")
3152 type = Clipboard::PngGraphicsType;
3153 else if (arg == "jpeg")
3154 type = Clipboard::JpegGraphicsType;
3155 else if (arg == "linkback")
3156 type = Clipboard::LinkBackGraphicsType;
3157 else if (arg == "emf")
3158 type = Clipboard::EmfGraphicsType;
3159 else if (arg == "wmf")
3160 type = Clipboard::WmfGraphicsType;
3163 LYXERR0("Unrecognized graphics type: " << arg);
3164 // we don't want to assert if the user just mistyped the LFUN
3165 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3169 enable = theClipboard().hasGraphicsContents(type);
3173 case LFUN_CLIPBOARD_PASTE:
3174 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3175 enable = !theClipboard().empty();
3178 case LFUN_PRIMARY_SELECTION_PASTE:
3179 enable = cur.selection() || !theSelection().empty();
3182 case LFUN_SELECTION_PASTE:
3183 enable = cap::selection();
3186 case LFUN_PARAGRAPH_MOVE_UP:
3187 enable = cur.pit() > 0 && !cur.selection();
3190 case LFUN_PARAGRAPH_MOVE_DOWN:
3191 enable = cur.pit() < cur.lastpit() && !cur.selection();
3194 case LFUN_CHANGE_ACCEPT:
3195 case LFUN_CHANGE_REJECT:
3196 if (!cur.selection())
3197 enable = cur.paragraph().isChanged(cur.pos());
3199 // will enable if there is a change in the selection
3202 // cheap improvement for efficiency: using cached
3203 // buffer variable, if there is no change in the
3204 // document, no need to check further.
3205 if (!cur.buffer()->areChangesPresent())
3208 for (DocIterator it = cur.selectionBegin(); ; it.forwardPar()) {
3209 pos_type const beg = it.pos();
3211 bool const in_last_par = (it.pit() == cur.selectionEnd().pit());
3213 end = cur.selectionEnd().pos();
3216 if (beg != end && it.paragraph().isChanged(beg, end)) {
3226 case LFUN_OUTLINE_UP:
3227 case LFUN_OUTLINE_DOWN:
3228 case LFUN_OUTLINE_IN:
3229 case LFUN_OUTLINE_OUT:
3230 // FIXME: LyX is not ready for outlining within inset.
3231 enable = isMainText()
3232 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3235 case LFUN_NEWLINE_INSERT:
3236 // LaTeX restrictions (labels or empty par)
3237 enable = !cur.paragraph().isPassThru()
3238 && cur.pos() > cur.paragraph().beginOfBody();
3241 case LFUN_SEPARATOR_INSERT:
3242 // Always enabled for now
3246 case LFUN_TAB_INSERT:
3247 case LFUN_TAB_DELETE:
3248 enable = cur.paragraph().isPassThru();
3251 case LFUN_GRAPHICS_SET_GROUP: {
3252 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3256 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3260 case LFUN_NEWPAGE_INSERT:
3261 // not allowed in description items
3262 code = NEWPAGE_CODE;
3263 enable = !inDescriptionItem(cur);
3267 enable = !cur.paragraph().isPassThru();
3268 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3271 case LFUN_PARAGRAPH_BREAK:
3272 enable = inset().allowMultiPar();
3275 case LFUN_SPELLING_ADD:
3276 case LFUN_SPELLING_IGNORE:
3277 case LFUN_SPELLING_REMOVE:
3278 enable = theSpellChecker() != NULL;
3279 if (enable && !cmd.getArg(1).empty()) {
3280 // validate explicitly given language
3281 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3282 enable &= lang != NULL;
3287 case LFUN_LAYOUT_TOGGLE: {
3288 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3289 docstring const req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3290 docstring const layout = resolveLayout(req_layout, cur);
3292 enable = !owner_->forcePlainLayout() && !layout.empty();
3293 flag.setOnOff(isAlreadyLayout(layout, cur));
3297 case LFUN_ENVIRONMENT_SPLIT: {
3298 if (cmd.argument() == "outer") {
3299 // check if we have an environment in our nesting hierarchy
3301 depth_type const current_depth = cur.paragraph().params().depth();
3302 pit_type pit = cur.pit();
3303 Paragraph cpar = pars_[pit];
3305 if (pit == 0 || cpar.params().depth() == 0)
3309 if (cpar.params().depth() < current_depth)
3310 res = cpar.layout().isEnvironment();
3315 else if (cmd.argument() == "previous") {
3316 // look if we have an environment in the previous par
3317 pit_type pit = cur.pit();
3318 Paragraph cpar = pars_[pit];
3322 enable = cpar.layout().isEnvironment();
3328 else if (cur.paragraph().layout().isEnvironment()) {
3329 enable = cmd.argument() == "before"
3330 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3337 case LFUN_LAYOUT_PARAGRAPH:
3338 case LFUN_PARAGRAPH_PARAMS:
3339 case LFUN_PARAGRAPH_PARAMS_APPLY:
3340 case LFUN_PARAGRAPH_UPDATE:
3341 enable = owner_->allowParagraphCustomization();
3344 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3345 // Because they insert COMBINING DIACRITICAL Unicode characters,
3346 // that cannot be handled by LaTeX but must be converted according
3347 // to the definition in lib/unicodesymbols?
3348 case LFUN_ACCENT_ACUTE:
3349 case LFUN_ACCENT_BREVE:
3350 case LFUN_ACCENT_CARON:
3351 case LFUN_ACCENT_CEDILLA:
3352 case LFUN_ACCENT_CIRCLE:
3353 case LFUN_ACCENT_CIRCUMFLEX:
3354 case LFUN_ACCENT_DOT:
3355 case LFUN_ACCENT_GRAVE:
3356 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3357 case LFUN_ACCENT_MACRON:
3358 case LFUN_ACCENT_OGONEK:
3359 case LFUN_ACCENT_TIE:
3360 case LFUN_ACCENT_TILDE:
3361 case LFUN_ACCENT_PERISPOMENI:
3362 case LFUN_ACCENT_UMLAUT:
3363 case LFUN_ACCENT_UNDERBAR:
3364 case LFUN_ACCENT_UNDERDOT:
3365 case LFUN_FONT_DEFAULT:
3366 case LFUN_FONT_FRAK:
3367 case LFUN_FONT_SIZE:
3368 case LFUN_FONT_STATE:
3369 case LFUN_FONT_UNDERLINE:
3370 case LFUN_FONT_STRIKEOUT:
3371 case LFUN_FONT_CROSSOUT:
3372 case LFUN_FONT_UNDERUNDERLINE:
3373 case LFUN_FONT_UNDERWAVE:
3374 case LFUN_TEXTSTYLE_APPLY:
3375 case LFUN_TEXTSTYLE_UPDATE:
3376 enable = !cur.paragraph().isPassThru();
3379 case LFUN_WORD_DELETE_FORWARD:
3380 case LFUN_WORD_DELETE_BACKWARD:
3381 case LFUN_LINE_DELETE_FORWARD:
3382 case LFUN_WORD_FORWARD:
3383 case LFUN_WORD_BACKWARD:
3384 case LFUN_WORD_RIGHT:
3385 case LFUN_WORD_LEFT:
3386 case LFUN_CHAR_FORWARD:
3387 case LFUN_CHAR_FORWARD_SELECT:
3388 case LFUN_CHAR_BACKWARD:
3389 case LFUN_CHAR_BACKWARD_SELECT:
3390 case LFUN_CHAR_LEFT:
3391 case LFUN_CHAR_LEFT_SELECT:
3392 case LFUN_CHAR_RIGHT:
3393 case LFUN_CHAR_RIGHT_SELECT:
3395 case LFUN_UP_SELECT:
3397 case LFUN_DOWN_SELECT:
3398 case LFUN_PARAGRAPH_UP_SELECT:
3399 case LFUN_PARAGRAPH_DOWN_SELECT:
3400 case LFUN_LINE_BEGIN_SELECT:
3401 case LFUN_LINE_END_SELECT:
3402 case LFUN_WORD_FORWARD_SELECT:
3403 case LFUN_WORD_BACKWARD_SELECT:
3404 case LFUN_WORD_RIGHT_SELECT:
3405 case LFUN_WORD_LEFT_SELECT:
3406 case LFUN_WORD_SELECT:
3407 case LFUN_SECTION_SELECT:
3408 case LFUN_BUFFER_BEGIN:
3409 case LFUN_BUFFER_END:
3410 case LFUN_BUFFER_BEGIN_SELECT:
3411 case LFUN_BUFFER_END_SELECT:
3412 case LFUN_INSET_BEGIN:
3413 case LFUN_INSET_END:
3414 case LFUN_INSET_BEGIN_SELECT:
3415 case LFUN_INSET_END_SELECT:
3416 case LFUN_PARAGRAPH_UP:
3417 case LFUN_PARAGRAPH_DOWN:
3418 case LFUN_LINE_BEGIN:
3420 case LFUN_CHAR_DELETE_FORWARD:
3421 case LFUN_CHAR_DELETE_BACKWARD:
3422 case LFUN_WORD_UPCASE:
3423 case LFUN_WORD_LOWCASE:
3424 case LFUN_WORD_CAPITALIZE:
3425 case LFUN_CHARS_TRANSPOSE:
3426 case LFUN_SERVER_GET_XY:
3427 case LFUN_SERVER_SET_XY:
3428 case LFUN_SERVER_GET_LAYOUT:
3429 case LFUN_SELF_INSERT:
3430 case LFUN_UNICODE_INSERT:
3431 case LFUN_THESAURUS_ENTRY:
3433 case LFUN_SERVER_GET_STATISTICS:
3434 // these are handled in our dispatch()
3438 case LFUN_INSET_INSERT: {
3439 string const type = cmd.getArg(0);
3440 if (type == "toc") {
3442 // not allowed in description items
3443 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3444 enable = !inDescriptionItem(cur);
3457 || !cur.inset().insetAllowed(code)
3458 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3461 flag.setEnabled(enable);
3466 void Text::pasteString(Cursor & cur, docstring const & clip,
3469 if (!clip.empty()) {
3472 insertStringAsParagraphs(cur, clip, cur.current_font);
3474 insertStringAsLines(cur, clip, cur.current_font);
3479 // FIXME: an item inset would make things much easier.
3480 bool Text::inDescriptionItem(Cursor & cur) const
3482 Paragraph & par = cur.paragraph();
3483 pos_type const pos = cur.pos();
3484 pos_type const body_pos = par.beginOfBody();
3486 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3487 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3488 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3491 return (pos < body_pos
3493 && (pos == 0 || par.getChar(pos - 1) != ' ')));