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"
22 #include "BufferParams.h"
23 #include "BufferView.h"
25 #include "CutAndPaste.h"
26 #include "DispatchResult.h"
28 #include "FloatList.h"
29 #include "FuncStatus.h"
30 #include "FuncRequest.h"
31 #include "InsetList.h"
35 #include "LyXAction.h"
39 #include "Paragraph.h"
40 #include "ParagraphParameters.h"
41 #include "SpellChecker.h"
42 #include "TextClass.h"
43 #include "TextMetrics.h"
44 #include "Thesaurus.h"
45 #include "WordLangTuple.h"
47 #include "frontends/alert.h"
48 #include "frontends/Application.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Selection.h"
52 #include "insets/InsetArgument.h"
53 #include "insets/InsetCollapsible.h"
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetExternal.h"
56 #include "insets/InsetFloat.h"
57 #include "insets/InsetFloatList.h"
58 #include "insets/InsetGraphics.h"
59 #include "insets/InsetGraphicsParams.h"
60 #include "insets/InsetInfo.h"
61 #include "insets/InsetIPAMacro.h"
62 #include "insets/InsetNewline.h"
63 #include "insets/InsetQuotes.h"
64 #include "insets/InsetSpecialChar.h"
65 #include "insets/InsetText.h"
66 #include "insets/InsetWrap.h"
68 #include "support/convert.h"
69 #include "support/debug.h"
70 #include "support/filetools.h"
71 #include "support/gettext.h"
72 #include "support/lassert.h"
73 #include "support/limited_stack.h"
74 #include "support/lstrings.h"
76 #include "mathed/InsetMathHull.h"
77 #include "mathed/InsetMathMacroTemplate.h"
85 using namespace lyx::support;
89 using cap::copySelection;
90 using cap::copySelectionToTemp;
91 using cap::cutSelection;
92 using cap::cutSelectionToTemp;
93 using cap::pasteFromStack;
94 using cap::pasteFromTemp;
95 using cap::pasteClipboardText;
96 using cap::pasteClipboardGraphics;
97 using cap::replaceSelection;
98 using cap::grabAndEraseSelection;
99 using cap::selClearOrDel;
100 using cap::pasteSimpleText;
101 using frontend::Clipboard;
104 typedef limited_stack<pair<docstring, Font>> FontStack;
105 static FontStack freeFonts(15);
106 static bool toggleall = false;
108 static void toggleAndShow(Cursor & cur, Text * text,
109 Font const & font, bool togall = true)
111 text->toggleFree(cur, font, togall);
113 if (font.language() != ignore_language ||
114 font.fontInfo().number() != FONT_IGNORE) {
115 TextMetrics const & tm = cur.bv().textMetrics(text);
116 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
117 cur.real_current_font))
118 text->setCursor(cur, cur.pit(), cur.pos(),
119 false, !cur.boundary());
124 static void moveCursor(Cursor & cur, bool selecting)
126 if (selecting || cur.mark())
131 static void finishChange(Cursor & cur, bool selecting)
134 moveCursor(cur, selecting);
138 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
141 docstring sel = cur.selectionAsString(false);
143 // It may happen that sel is empty but there is a selection
144 replaceSelection(cur);
146 // Is this a valid formula?
150 #ifdef ENABLE_ASSERTIONS
151 const int old_pos = cur.pos();
153 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
154 #ifdef ENABLE_ASSERTIONS
155 LATTEST(old_pos == cur.pos());
157 cur.nextInset()->edit(cur, true);
158 if (cmd.action() != LFUN_MATH_MODE)
159 // LFUN_MATH_MODE has a different meaning in math mode
162 InsetMathHull * formula = new InsetMathHull(cur.buffer());
163 string const selstr = to_utf8(sel);
164 istringstream is(selstr);
167 if (!formula->readQuiet(lex)) {
168 // No valid formula, let's try with delims
169 is.str("$" + selstr + "$");
171 if (!formula->readQuiet(lex)) {
172 // Still not valid, leave it as is
180 cur.nextInset()->edit(cur, true);
181 LASSERT(cur.inMathed(), return);
185 cur.pos() = cur.lastpos();
186 if (cmd.action() != LFUN_MATH_MODE)
187 // LFUN_MATH_MODE has a different meaning in math mode
189 cur.clearSelection();
190 cur.pos() = cur.lastpos();
194 cur.message(from_utf8(N_("Math editor mode")));
196 cur.message(from_utf8(N_("No valid math formula")));
200 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
202 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
203 if (cur.inRegexped()) {
204 cur.message(_("Already in regular expression mode"));
208 docstring sel = cur.selectionAsString(false);
210 // It may happen that sel is empty but there is a selection
211 replaceSelection(cur);
213 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
214 cur.nextInset()->edit(cur, true);
217 cur.message(_("Regexp editor mode"));
221 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
224 cap::replaceSelection(cur);
225 cur.insert(new InsetSpecialChar(kind));
230 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
233 cap::replaceSelection(cur);
234 cur.insert(new InsetIPAChar(kind));
239 static bool doInsertInset(Cursor & cur, Text * text,
240 FuncRequest const & cmd, bool edit, bool pastesel)
242 Buffer & buffer = cur.bv().buffer();
243 BufferParams const & bparams = buffer.params();
244 Inset * inset = createInset(&buffer, cmd);
248 if (InsetCollapsible * ci = inset->asInsetCollapsible())
249 ci->setButtonLabel();
252 if (cmd.action() == LFUN_ARGUMENT_INSERT) {
253 bool cotextinsert = false;
254 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
255 Layout const & lay = cur.paragraph().layout();
256 Layout::LaTeXArgMap args = lay.args();
257 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
258 if (lait != args.end())
259 cotextinsert = (*lait).second.insertcotext;
261 InsetLayout const & il = cur.inset().getLayout();
263 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
264 if (ilait != args.end())
265 cotextinsert = (*ilait).second.insertcotext;
267 // The argument requests to insert a copy of the co-text to the inset
270 // If we have a selection within a paragraph, use this
271 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
272 ds = cur.selectionAsString(false);
273 // else use the whole paragraph
275 ds = cur.paragraph().asString();
276 text->insertInset(cur, inset);
278 inset->edit(cur, true);
279 // Now put co-text into inset
280 Font const f(inherit_font, cur.current_font.language());
282 cur.text()->insertStringAsLines(cur, ds, f);
283 cur.leaveInset(*inset);
290 bool move_layout = false;
291 if (cur.selection()) {
292 if (cmd.action() == LFUN_INDEX_INSERT)
293 copySelectionToTemp(cur);
295 cutSelectionToTemp(cur, pastesel);
296 /* Move layout information inside the inset if the whole
297 * paragraph and the inset allows setting layout
298 * FIXME: this does not work as expected when change tracking is on
299 * However, we do not really know what to do in this case.
301 if (cur.paragraph().empty() && !inset->forcePlainLayout()) {
302 cur.paragraph().setPlainOrDefaultLayout(bparams.documentClass());
306 cur.clearSelection();
308 } else if (cmd.action() == LFUN_INDEX_INSERT) {
309 gotsel = text->selectWordWhenUnderCursor(cur, WHOLE_WORD);
310 copySelectionToTemp(cur);
311 cur.clearSelection();
313 text->insertInset(cur, inset);
316 inset->edit(cur, true);
318 if (!gotsel || !pastesel)
321 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
322 cur.buffer()->errors("Paste");
323 cur.clearSelection(); // bug 393
325 InsetText * inset_text = inset->asInsetText();
327 inset_text->fixParagraphsFont();
330 /* If the containing paragraph has kept its layout, reset the
331 * layout of the first paragraph of the inset.
334 cur.paragraph().setPlainOrDefaultLayout(bparams.documentClass());
335 // FIXME: what does this do?
336 if (cmd.action() == LFUN_FLEX_INSERT)
339 cur.leaveInset(*inset);
340 if (cmd.action() == LFUN_PREVIEW_INSERT
341 || cmd.action() == LFUN_IPA_INSERT)
343 notifyCursorLeavesOrEnters(old, cur);
345 cur.leaveInset(*inset);
346 // reset surrounding par to default
347 DocumentClass const & dc = bparams.documentClass();
348 docstring const layoutname = inset->usePlainLayout()
349 ? dc.plainLayoutName()
350 : dc.defaultLayoutName();
351 text->setLayout(cur, layoutname);
357 /// the type of outline operation
359 OutlineUp, // Move this header with text down
360 OutlineDown, // Move this header with text up
361 OutlineIn, // Make this header deeper
362 OutlineOut // Make this header shallower
366 static void insertSeparator(Cursor const & cur, depth_type const depth)
368 Buffer & buf = *cur.buffer();
369 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
370 DocumentClass const & tc = buf.params().documentClass();
371 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
372 + from_ascii("\" ignoreautonests")));
373 // FIXME: Bibitem mess!
374 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
375 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
376 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
377 while (cur.paragraph().params().depth() > depth)
378 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
382 static void outline(OutlineOp mode, Cursor & cur, Text * text)
384 Buffer & buf = *cur.buffer();
385 pit_type & pit = cur.pit();
386 ParagraphList & pars = buf.text().paragraphs();
387 ParagraphList::iterator const bgn = pars.begin();
388 // The first paragraph of the area to be copied:
389 ParagraphList::iterator start = pars.iterator_at(pit);
390 // The final paragraph of area to be copied:
391 ParagraphList::iterator finish = start;
392 ParagraphList::iterator const end = pars.end();
393 depth_type const current_depth = cur.paragraph().params().depth();
395 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
398 // Move out (down) from this section header
402 // Seek the one (on same level) below
403 for (; finish != end; ++finish) {
404 toclevel = buf.text().getTocLevel(distance(bgn, finish));
405 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
411 if (start == pars.begin())
414 ParagraphList::iterator dest = start;
415 // Move out (up) from this header
418 // Search previous same-level header above
421 toclevel = buf.text().getTocLevel(distance(bgn, dest));
423 && (toclevel == Layout::NOT_IN_TOC
424 || toclevel > thistoclevel));
425 // Not found; do nothing
426 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
428 pit_type newpit = distance(bgn, dest);
429 pit_type const len = distance(start, finish);
430 pit_type const deletepit = pit + len;
431 buf.undo().recordUndo(cur, newpit, deletepit - 1);
432 // If we move an environment upwards, make sure it is
433 // separated from its new neighbour below:
434 // If an environment of the same layout follows, and the moved
435 // paragraph sequence does not end with a separator, insert one.
436 ParagraphList::iterator lastmoved = finish;
438 if (start->layout().isEnvironment()
439 && dest->layout() == start->layout()
440 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
441 cur.pit() = distance(bgn, lastmoved);
442 cur.pos() = cur.lastpos();
443 insertSeparator(cur, current_depth);
446 // Likewise, if we moved an environment upwards, make sure it
447 // is separated from its new neighbour above.
448 // The paragraph before the target of movement
450 ParagraphList::iterator before = dest;
452 // Get the parent paragraph (outer in nested context)
453 pit_type const parent =
454 before->params().depth() > current_depth
455 ? text->depthHook(distance(bgn, before), current_depth)
456 : distance(bgn, before);
457 // If a environment with same layout preceeds the moved one in the new
458 // position, and there is no separator yet, insert one.
459 if (start->layout().isEnvironment()
460 && pars[parent].layout() == start->layout()
461 && !before->isEnvSeparator(before->beginOfBody())) {
462 cur.pit() = distance(bgn, before);
463 cur.pos() = cur.lastpos();
464 insertSeparator(cur, current_depth);
468 newpit = distance(bgn, dest);
469 pars.splice(dest, start, finish);
477 // Go one down from *this* header:
478 ParagraphList::iterator dest = next(finish, 1);
479 // Go further down to find header to insert in front of:
480 for (; dest != end; ++dest) {
481 toclevel = buf.text().getTocLevel(distance(bgn, dest));
482 if (toclevel != Layout::NOT_IN_TOC
483 && toclevel <= thistoclevel)
486 // One such was found, so go on...
487 // If we move an environment downwards, make sure it is
488 // separated from its new neighbour above.
489 pit_type newpit = distance(bgn, dest);
490 buf.undo().recordUndo(cur, pit, newpit - 1);
491 // The paragraph before the target of movement
492 ParagraphList::iterator before = dest;
494 // Get the parent paragraph (outer in nested context)
495 pit_type const parent =
496 before->params().depth() > current_depth
497 ? text->depthHook(distance(bgn, before), current_depth)
498 : distance(bgn, before);
499 // If a environment with same layout preceeds the moved one in the new
500 // position, and there is no separator yet, insert one.
501 if (start->layout().isEnvironment()
502 && pars[parent].layout() == start->layout()
503 && !before->isEnvSeparator(before->beginOfBody())) {
504 cur.pit() = distance(bgn, before);
505 cur.pos() = cur.lastpos();
506 insertSeparator(cur, current_depth);
509 // Likewise, make sure moved environments are separated
510 // from their new neighbour below:
511 // If an environment of the same layout follows, and the moved
512 // paragraph sequence does not end with a separator, insert one.
513 ParagraphList::iterator lastmoved = finish;
516 && start->layout().isEnvironment()
517 && dest->layout() == start->layout()
518 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
519 cur.pit() = distance(bgn, lastmoved);
520 cur.pos() = cur.lastpos();
521 insertSeparator(cur, current_depth);
524 newpit = distance(bgn, dest);
525 pit_type const len = distance(start, finish);
526 pars.splice(dest, start, finish);
527 cur.pit() = newpit - len;
532 pit_type const len = distance(start, finish);
533 buf.undo().recordUndo(cur, pit, pit + len - 1);
534 for (; start != finish; ++start) {
535 toclevel = buf.text().getTocLevel(distance(bgn, start));
536 if (toclevel == Layout::NOT_IN_TOC)
539 DocumentClass const & tc = buf.params().documentClass();
540 int const newtoclevel =
541 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
542 LabelType const oldlabeltype = start->layout().labeltype;
544 for (auto const & lay : tc) {
545 if (lay.toclevel == newtoclevel &&
546 lay.labeltype == oldlabeltype) {
547 start->setLayout(lay);
558 void Text::number(Cursor & cur)
560 FontInfo font = ignore_font;
561 font.setNumber(FONT_TOGGLE);
562 toggleAndShow(cur, this, Font(font, ignore_language));
566 bool Text::isRTL(pit_type const pit) const
568 Buffer const & buffer = owner_->buffer();
569 return pars_[pit].isRTL(buffer.params());
575 Language const * getLanguage(Cursor const & cur, string const & lang)
577 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
581 docstring resolveLayout(docstring layout, DocIterator const & dit)
583 Paragraph const & par = dit.paragraph();
584 DocumentClass const & tclass = dit.buffer()->params().documentClass();
587 layout = tclass.defaultLayoutName();
589 if (dit.inset().forcePlainLayout(dit.idx()))
590 // in this case only the empty layout is allowed
591 layout = tclass.plainLayoutName();
592 else if (par.usePlainLayout()) {
593 // in this case, default layout maps to empty layout
594 if (layout == tclass.defaultLayoutName())
595 layout = tclass.plainLayoutName();
597 // otherwise, the empty layout maps to the default
598 if (layout == tclass.plainLayoutName())
599 layout = tclass.defaultLayoutName();
602 // If the entry is obsolete, use the new one instead.
603 if (tclass.hasLayout(layout)) {
604 docstring const & obs = tclass[layout].obsoleted_by();
608 if (!tclass.hasLayout(layout))
614 bool isAlreadyLayout(docstring const & layout, CursorData const & cur)
616 ParagraphList const & pars = cur.text()->paragraphs();
618 pit_type pit = cur.selBegin().pit();
619 pit_type const epit = cur.selEnd().pit() + 1;
620 for ( ; pit != epit; ++pit)
621 if (pars[pit].layout().name() != layout)
631 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
633 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
635 // Dispatch if the cursor is inside the text. It is not the
636 // case for context menus (bug 5797).
637 if (cur.text() != this) {
642 BufferView * bv = &cur.bv();
643 TextMetrics * tm = &bv->textMetrics(this);
644 if (!tm->contains(cur.pit())) {
645 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
646 tm = &bv->textMetrics(this);
649 // FIXME: We use the update flag to indicates wether a singlePar or a
650 // full screen update is needed. We reset it here but shall we restore it
652 cur.noScreenUpdate();
654 LBUFERR(this == cur.text());
656 // NOTE: This should NOT be a reference. See commit 94a5481a.
657 CursorSlice const oldTopSlice = cur.top();
658 bool const oldBoundary = cur.boundary();
659 bool const oldSelection = cur.selection();
660 // Signals that, even if needsUpdate == false, an update of the
661 // cursor paragraph is required
662 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
663 LyXAction::SingleParUpdate);
664 // Signals that a full-screen update is required
665 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
666 LyXAction::NoUpdate) || singleParUpdate);
667 bool const last_misspelled = lyxrc.spellcheck_continuously
668 && cur.paragraph().isMisspelled(cur.pos(), true);
670 FuncCode const act = cmd.action();
673 case LFUN_PARAGRAPH_MOVE_DOWN: {
674 pit_type const pit = cur.pit();
675 cur.recordUndo(pit, pit + 1);
676 pars_.swap(pit, pit + 1);
678 cur.forceBufferUpdate();
683 case LFUN_PARAGRAPH_MOVE_UP: {
684 pit_type const pit = cur.pit();
685 cur.recordUndo(pit - 1, pit);
687 pars_.swap(pit, pit - 1);
690 cur.forceBufferUpdate();
694 case LFUN_APPENDIX: {
695 Paragraph & par = cur.paragraph();
696 bool start = !par.params().startOfAppendix();
698 // FIXME: The code below only makes sense at top level.
699 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
700 // ensure that we have only one start_of_appendix in this document
701 // FIXME: this don't work for multipart document!
702 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
703 if (pars_[tmp].params().startOfAppendix()) {
704 cur.recordUndo(tmp, tmp);
705 pars_[tmp].params().startOfAppendix(false);
711 par.params().startOfAppendix(start);
713 // we can set the refreshing parameters now
714 cur.forceBufferUpdate();
718 case LFUN_WORD_DELETE_FORWARD:
720 cutSelection(cur, false);
722 deleteWordForward(cur, cmd.getArg(0) == "force");
723 finishChange(cur, false);
726 case LFUN_WORD_DELETE_BACKWARD:
728 cutSelection(cur, false);
730 deleteWordBackward(cur, cmd.getArg(0) == "force");
731 finishChange(cur, false);
734 case LFUN_LINE_DELETE_FORWARD:
736 cutSelection(cur, false);
738 tm->deleteLineForward(cur);
739 finishChange(cur, false);
742 case LFUN_BUFFER_BEGIN:
743 case LFUN_BUFFER_BEGIN_SELECT:
744 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
745 if (cur.depth() == 1)
746 needsUpdate |= cursorTop(cur);
749 cur.screenUpdateFlags(Update::FitCursor);
752 case LFUN_BUFFER_END:
753 case LFUN_BUFFER_END_SELECT:
754 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
755 if (cur.depth() == 1)
756 needsUpdate |= cursorBottom(cur);
759 cur.screenUpdateFlags(Update::FitCursor);
762 case LFUN_INSET_BEGIN:
763 case LFUN_INSET_BEGIN_SELECT:
764 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
765 if (cur.depth() == 1 || !cur.top().at_begin())
766 needsUpdate |= cursorTop(cur);
769 cur.screenUpdateFlags(Update::FitCursor);
773 case LFUN_INSET_END_SELECT:
774 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
775 if (cur.depth() == 1 || !cur.top().at_end())
776 needsUpdate |= cursorBottom(cur);
779 cur.screenUpdateFlags(Update::FitCursor);
782 case LFUN_CHAR_FORWARD:
783 case LFUN_CHAR_FORWARD_SELECT: {
784 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
785 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
786 bool const cur_moved = cursorForward(cur);
787 needsUpdate |= cur_moved;
789 if (!cur_moved && oldTopSlice == cur.top()
790 && cur.boundary() == oldBoundary) {
792 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
794 // we will probably be moving out the inset, so we should execute
795 // the depm-mechanism, but only when the cursor has a place to
796 // go outside this inset, i.e. in a slice above.
797 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
798 && cur.pit() == cur.lastpit()) {
799 // The cursor hasn't changed yet. To give the
800 // DEPM the possibility of doing something we must
801 // provide it with two different cursors.
803 dummy.pos() = dummy.pit() = 0;
804 if (cur.bv().checkDepm(dummy, cur))
805 cur.forceBufferUpdate();
811 case LFUN_CHAR_BACKWARD:
812 case LFUN_CHAR_BACKWARD_SELECT: {
813 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
814 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
815 bool const cur_moved = cursorBackward(cur);
816 needsUpdate |= cur_moved;
818 if (!cur_moved && oldTopSlice == cur.top()
819 && cur.boundary() == oldBoundary) {
821 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
823 // we will probably be moving out the inset, so we should execute
824 // the depm-mechanism, but only when the cursor has a place to
825 // go outside this inset, i.e. in a slice above.
826 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
827 // The cursor hasn't changed yet. To give the
828 // DEPM the possibility of doing something we must
829 // provide it with two different cursors.
831 dummy.pos() = cur.lastpos();
832 dummy.pit() = cur.lastpit();
833 if (cur.bv().checkDepm(dummy, cur))
834 cur.forceBufferUpdate();
841 case LFUN_CHAR_LEFT_SELECT:
842 if (lyxrc.visual_cursor) {
843 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
844 bool const cur_moved = cursorVisLeft(cur);
845 needsUpdate |= cur_moved;
846 if (!cur_moved && oldTopSlice == cur.top()
847 && cur.boundary() == oldBoundary) {
849 cmd = FuncRequest(LFUN_FINISHED_LEFT);
852 if (cur.reverseDirectionNeeded()) {
853 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
854 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
856 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
857 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
864 case LFUN_CHAR_RIGHT:
865 case LFUN_CHAR_RIGHT_SELECT:
866 if (lyxrc.visual_cursor) {
867 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
868 bool const cur_moved = cursorVisRight(cur);
869 needsUpdate |= cur_moved;
870 if (!cur_moved && oldTopSlice == cur.top()
871 && cur.boundary() == oldBoundary) {
873 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
876 if (cur.reverseDirectionNeeded()) {
877 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
878 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
880 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
881 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
890 case LFUN_DOWN_SELECT:
893 // stop/start the selection
894 bool select = cmd.action() == LFUN_DOWN_SELECT ||
895 cmd.action() == LFUN_UP_SELECT;
897 // move cursor up/down
898 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
899 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
901 if (!atFirstOrLastRow) {
902 needsUpdate |= cur.selHandle(select);
903 cur.upDownInText(up, needsUpdate);
904 needsUpdate |= cur.beforeDispatchCursor().inMathed();
906 pos_type newpos = up ? 0 : cur.lastpos();
907 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
908 needsUpdate |= cur.selHandle(select);
909 // we do not reset the targetx of the cursor
911 needsUpdate |= bv->checkDepm(cur, bv->cursor());
912 cur.updateTextTargetOffset();
914 cur.forceBufferUpdate();
918 // if the cursor cannot be moved up or down do not remove
919 // the selection right now, but wait for the next dispatch.
921 needsUpdate |= cur.selHandle(select);
922 cur.upDownInText(up, needsUpdate);
929 case LFUN_PARAGRAPH_SELECT:
931 needsUpdate |= setCursor(cur, cur.pit(), 0);
932 needsUpdate |= cur.selHandle(true);
933 if (cur.pos() < cur.lastpos())
934 needsUpdate |= setCursor(cur, cur.pit(), cur.lastpos());
937 case LFUN_PARAGRAPH_UP:
938 case LFUN_PARAGRAPH_UP_SELECT:
939 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
940 needsUpdate |= cursorUpParagraph(cur);
943 case LFUN_PARAGRAPH_DOWN:
944 case LFUN_PARAGRAPH_DOWN_SELECT:
945 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
946 needsUpdate |= cursorDownParagraph(cur);
949 case LFUN_LINE_BEGIN:
950 case LFUN_LINE_BEGIN_SELECT:
951 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
952 needsUpdate |= tm->cursorHome(cur);
956 case LFUN_LINE_END_SELECT:
957 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
958 needsUpdate |= tm->cursorEnd(cur);
961 case LFUN_SECTION_SELECT: {
962 Buffer const & buf = *cur.buffer();
963 pit_type const pit = cur.pit();
964 ParagraphList & pars = buf.text().paragraphs();
965 ParagraphList::iterator bgn = pars.begin();
966 // The first paragraph of the area to be selected:
967 ParagraphList::iterator start = pars.iterator_at(pit);
968 // The final paragraph of area to be selected:
969 ParagraphList::iterator finish = start;
970 ParagraphList::iterator end = pars.end();
972 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
973 if (thistoclevel == Layout::NOT_IN_TOC)
977 Cursor const old_cur = cur;
978 needsUpdate |= cur.selHandle(true);
980 // Move out (down) from this section header
984 // Seek the one (on same level) below
985 for (; finish != end; ++finish, ++cur.pit()) {
986 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
987 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
990 cur.pos() = cur.lastpos();
992 cur.setCurrentFont();
994 needsUpdate |= cur != old_cur;
998 case LFUN_WORD_RIGHT:
999 case LFUN_WORD_RIGHT_SELECT:
1000 if (lyxrc.visual_cursor) {
1001 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
1002 bool const cur_moved = cursorVisRightOneWord(cur);
1003 needsUpdate |= cur_moved;
1004 if (!cur_moved && oldTopSlice == cur.top()
1005 && cur.boundary() == oldBoundary) {
1007 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1010 if (cur.reverseDirectionNeeded()) {
1011 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1012 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1014 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1015 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1022 case LFUN_WORD_FORWARD:
1023 case LFUN_WORD_FORWARD_SELECT: {
1024 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
1025 bool const cur_moved = cursorForwardOneWord(cur);
1026 needsUpdate |= cur_moved;
1028 if (!cur_moved && oldTopSlice == cur.top()
1029 && cur.boundary() == oldBoundary) {
1031 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1033 // we will probably be moving out the inset, so we should execute
1034 // the depm-mechanism, but only when the cursor has a place to
1035 // go outside this inset, i.e. in a slice above.
1036 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
1037 && cur.pit() == cur.lastpit()) {
1038 // The cursor hasn't changed yet. To give the
1039 // DEPM the possibility of doing something we must
1040 // provide it with two different cursors.
1042 dummy.pos() = dummy.pit() = 0;
1043 if (cur.bv().checkDepm(dummy, cur))
1044 cur.forceBufferUpdate();
1050 case LFUN_WORD_LEFT:
1051 case LFUN_WORD_LEFT_SELECT:
1052 if (lyxrc.visual_cursor) {
1053 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
1054 bool const cur_moved = cursorVisLeftOneWord(cur);
1055 needsUpdate |= cur_moved;
1056 if (!cur_moved && oldTopSlice == cur.top()
1057 && cur.boundary() == oldBoundary) {
1059 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1062 if (cur.reverseDirectionNeeded()) {
1063 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1064 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1066 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1067 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1074 case LFUN_WORD_BACKWARD:
1075 case LFUN_WORD_BACKWARD_SELECT: {
1076 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1077 bool const cur_moved = cursorBackwardOneWord(cur);
1078 needsUpdate |= cur_moved;
1080 if (!cur_moved && oldTopSlice == cur.top()
1081 && cur.boundary() == oldBoundary) {
1083 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1085 // we will probably be moving out the inset, so we should execute
1086 // the depm-mechanism, but only when the cursor has a place to
1087 // go outside this inset, i.e. in a slice above.
1088 if (cur.depth() > 1 && cur.pos() == 0
1089 && cur.pit() == 0) {
1090 // The cursor hasn't changed yet. To give the
1091 // DEPM the possibility of doing something we must
1092 // provide it with two different cursors.
1094 dummy.pos() = cur.lastpos();
1095 dummy.pit() = cur.lastpit();
1096 if (cur.bv().checkDepm(dummy, cur))
1097 cur.forceBufferUpdate();
1103 case LFUN_WORD_SELECT: {
1104 selectWord(cur, WHOLE_WORD);
1105 finishChange(cur, true);
1109 case LFUN_NEWLINE_INSERT: {
1110 InsetNewlineParams inp;
1111 docstring const & arg = cmd.argument();
1112 if (arg == "linebreak")
1113 inp.kind = InsetNewlineParams::LINEBREAK;
1115 inp.kind = InsetNewlineParams::NEWLINE;
1116 cap::replaceSelection(cur);
1118 cur.insert(new InsetNewline(inp));
1120 moveCursor(cur, false);
1124 case LFUN_TAB_INSERT: {
1125 bool const multi_par_selection = cur.selection() &&
1126 cur.selBegin().pit() != cur.selEnd().pit();
1127 if (multi_par_selection) {
1128 // If there is a multi-paragraph selection, a tab is inserted
1129 // at the beginning of each paragraph.
1130 cur.recordUndoSelection();
1131 pit_type const pit_end = cur.selEnd().pit();
1132 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1133 pars_[pit].insertChar(0, '\t',
1134 bv->buffer().params().track_changes);
1135 // Update the selection pos to make sure the selection does not
1136 // change as the inserted tab will increase the logical pos.
1137 if (cur.realAnchor().pit() == pit)
1138 cur.realAnchor().forwardPos();
1139 if (cur.pit() == pit)
1144 // Maybe we shouldn't allow tabs within a line, because they
1145 // are not (yet) aligned as one might do expect.
1146 FuncRequest ncmd(LFUN_SELF_INSERT, from_ascii("\t"));
1147 dispatch(cur, ncmd);
1152 case LFUN_TAB_DELETE: {
1153 bool const tc = bv->buffer().params().track_changes;
1154 if (cur.selection()) {
1155 // If there is a selection, a tab (if present) is removed from
1156 // the beginning of each paragraph.
1157 cur.recordUndoSelection();
1158 pit_type const pit_end = cur.selEnd().pit();
1159 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1160 Paragraph & par = paragraphs()[pit];
1163 char_type const c = par.getChar(0);
1164 if (c == '\t' || c == ' ') {
1165 // remove either 1 tab or 4 spaces.
1166 int const n = (c == ' ' ? 4 : 1);
1167 for (int i = 0; i < n
1168 && !par.empty() && par.getChar(0) == c; ++i) {
1169 if (cur.pit() == pit)
1171 if (cur.realAnchor().pit() == pit
1172 && cur.realAnchor().pos() > 0 )
1173 cur.realAnchor().backwardPos();
1174 par.eraseChar(0, tc);
1180 // If there is no selection, try to remove a tab or some spaces
1181 // before the position of the cursor.
1182 Paragraph & par = paragraphs()[cur.pit()];
1183 pos_type const pos = cur.pos();
1188 char_type const c = par.getChar(pos - 1);
1192 par.eraseChar(cur.pos(), tc);
1194 for (int n_spaces = 0;
1196 && par.getChar(cur.pos() - 1) == ' '
1200 par.eraseChar(cur.pos(), tc);
1207 case LFUN_CHAR_DELETE_FORWARD:
1208 if (!cur.selection()) {
1209 if (cur.pos() == cur.paragraph().size())
1210 // Par boundary, force full-screen update
1211 singleParUpdate = false;
1212 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1214 cur.selection(true);
1219 needsUpdate |= erase(cur);
1222 cutSelection(cur, false);
1223 cur.setCurrentFont();
1224 singleParUpdate = false;
1226 moveCursor(cur, false);
1229 case LFUN_CHAR_DELETE_BACKWARD:
1230 if (!cur.selection()) {
1231 if (bv->getIntl().getTransManager().backspace()) {
1232 bool par_boundary = cur.pos() == 0;
1233 bool first_par = cur.pit() == 0;
1234 // Par boundary, full-screen update
1236 singleParUpdate = false;
1237 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1239 cur.selection(true);
1244 needsUpdate |= backspace(cur);
1246 if (par_boundary && !first_par && cur.pos() > 0
1247 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1248 needsUpdate |= backspace(cur);
1253 DocIterator const dit = cur.selectionBegin();
1254 cutSelection(cur, false);
1255 if (cur.buffer()->params().track_changes)
1256 // since we're doing backwards deletion,
1257 // and the selection is not really cut,
1258 // move cursor before selection (#11630)
1260 cur.setCurrentFont();
1261 singleParUpdate = false;
1265 case LFUN_PARAGRAPH_BREAK: {
1266 cap::replaceSelection(cur);
1267 pit_type pit = cur.pit();
1268 Paragraph const & par = pars_[pit];
1269 bool lastpar = (pit == pit_type(pars_.size() - 1));
1270 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1271 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1272 if (prev < pit && cur.pos() == par.beginOfBody()
1273 && par.empty() && !par.isEnvSeparator(cur.pos())
1274 && !par.layout().keepempty
1275 && !par.layout().isCommand()
1276 && pars_[prev].layout() != par.layout()
1277 && pars_[prev].layout().isEnvironment()
1278 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1279 if (par.layout().isEnvironment()
1280 && pars_[prev].getDepth() == par.getDepth()) {
1281 docstring const layout = par.layout().name();
1282 DocumentClass const & tc = bv->buffer().params().documentClass();
1283 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1284 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1285 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1286 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1288 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1289 breakParagraph(cur);
1291 Font const f(inherit_font, cur.current_font.language());
1292 pars_[cur.pit() - 1].resetFonts(f);
1294 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1296 breakParagraph(cur, cmd.getArg(0) == "inverse");
1299 // If we have a list and autoinsert item insets,
1301 Layout::LaTeXArgMap args = par.layout().args();
1302 for (auto const & thearg : args) {
1303 Layout::latexarg arg = thearg.second;
1304 if (arg.autoinsert && prefixIs(thearg.first, "item:")) {
1305 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, thearg.first);
1306 lyx::dispatch(cmd2);
1312 case LFUN_INSET_INSERT: {
1315 // We have to avoid triggering InstantPreview loading
1316 // before inserting into the document. See bug #5626.
1317 bool loaded = bv->buffer().isFullyLoaded();
1318 bv->buffer().setFullyLoaded(false);
1319 Inset * inset = createInset(&bv->buffer(), cmd);
1320 bv->buffer().setFullyLoaded(loaded);
1323 // FIXME (Abdel 01/02/2006):
1324 // What follows would be a partial fix for bug 2154:
1325 // http://www.lyx.org/trac/ticket/2154
1326 // This automatically put the label inset _after_ a
1327 // numbered section. It should be possible to extend the mechanism
1328 // to any kind of LateX environement.
1329 // The correct way to fix that bug would be at LateX generation.
1330 // I'll let the code here for reference as it could be used for some
1331 // other feature like "automatic labelling".
1333 Paragraph & par = pars_[cur.pit()];
1334 if (inset->lyxCode() == LABEL_CODE
1335 && !par.layout().counter.empty()) {
1336 // Go to the end of the paragraph
1337 // Warning: Because of Change-Tracking, the last
1338 // position is 'size()' and not 'size()-1':
1339 cur.pos() = par.size();
1340 // Insert a new paragraph
1341 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1345 if (cur.selection())
1346 cutSelection(cur, false);
1348 cur.forceBufferUpdate();
1349 if (inset->editable() && inset->asInsetText())
1350 inset->edit(cur, true);
1354 // trigger InstantPreview now
1355 if (inset->lyxCode() == EXTERNAL_CODE) {
1356 InsetExternal & ins =
1357 static_cast<InsetExternal &>(*inset);
1358 ins.updatePreview();
1365 case LFUN_INSET_DISSOLVE: {
1366 if (dissolveInset(cur)) {
1368 cur.forceBufferUpdate();
1373 case LFUN_INSET_SPLIT: {
1374 if (splitInset(cur)) {
1376 cur.forceBufferUpdate();
1381 case LFUN_GRAPHICS_SET_GROUP: {
1382 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1388 string id = to_utf8(cmd.argument());
1389 string grp = graphics::getGroupParams(bv->buffer(), id);
1390 InsetGraphicsParams tmp, inspar = ins->getParams();
1393 inspar.groupId = to_utf8(cmd.argument());
1395 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1396 tmp.filename = inspar.filename;
1400 ins->setParams(inspar);
1404 case LFUN_SPACE_INSERT:
1405 if (cur.paragraph().layout().free_spacing)
1406 insertChar(cur, ' ');
1408 doInsertInset(cur, this, cmd, false, false);
1411 moveCursor(cur, false);
1414 case LFUN_SPECIALCHAR_INSERT: {
1415 string const name = to_utf8(cmd.argument());
1416 if (name == "hyphenation")
1417 specialChar(cur, InsetSpecialChar::HYPHENATION);
1418 else if (name == "allowbreak")
1419 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1420 else if (name == "ligature-break")
1421 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1422 else if (name == "slash")
1423 specialChar(cur, InsetSpecialChar::SLASH);
1424 else if (name == "nobreakdash")
1425 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1426 else if (name == "dots")
1427 specialChar(cur, InsetSpecialChar::LDOTS);
1428 else if (name == "end-of-sentence")
1429 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1430 else if (name == "menu-separator")
1431 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1432 else if (name == "lyx")
1433 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1434 else if (name == "tex")
1435 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1436 else if (name == "latex")
1437 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1438 else if (name == "latex2e")
1439 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1440 else if (name.empty())
1441 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1443 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1447 case LFUN_IPAMACRO_INSERT: {
1448 string const arg = cmd.getArg(0);
1449 if (arg == "deco") {
1450 // Open the inset, and move the current selection
1452 doInsertInset(cur, this, cmd, true, true);
1454 // Some insets are numbered, others are shown in the outline pane so
1455 // let's update the labels and the toc backend.
1456 cur.forceBufferUpdate();
1459 if (arg == "tone-falling")
1460 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1461 else if (arg == "tone-rising")
1462 ipaChar(cur, InsetIPAChar::TONE_RISING);
1463 else if (arg == "tone-high-rising")
1464 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1465 else if (arg == "tone-low-rising")
1466 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1467 else if (arg == "tone-high-rising-falling")
1468 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1469 else if (arg.empty())
1470 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1472 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1476 case LFUN_WORD_UPCASE:
1477 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1480 case LFUN_WORD_LOWCASE:
1481 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1484 case LFUN_WORD_CAPITALIZE:
1485 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1488 case LFUN_CHARS_TRANSPOSE:
1489 charsTranspose(cur);
1493 cur.message(_("Paste"));
1494 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1495 cap::replaceSelection(cur);
1497 // without argument?
1498 string const arg = to_utf8(cmd.argument());
1500 bool tryGraphics = true;
1501 if (theClipboard().isInternal())
1502 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1503 else if (theClipboard().hasTextContents()) {
1504 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1505 true, Clipboard::AnyTextType))
1506 tryGraphics = false;
1508 if (tryGraphics && theClipboard().hasGraphicsContents())
1509 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1510 } else if (isStrUnsignedInt(arg)) {
1511 // we have a numerical argument
1512 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1513 convert<unsigned int>(arg));
1514 } else if (arg == "html" || arg == "latex") {
1515 Clipboard::TextType type = (arg == "html") ?
1516 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1517 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1519 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1521 type = Clipboard::PdfGraphicsType;
1522 else if (arg == "png")
1523 type = Clipboard::PngGraphicsType;
1524 else if (arg == "jpeg")
1525 type = Clipboard::JpegGraphicsType;
1526 else if (arg == "linkback")
1527 type = Clipboard::LinkBackGraphicsType;
1528 else if (arg == "emf")
1529 type = Clipboard::EmfGraphicsType;
1530 else if (arg == "wmf")
1531 type = Clipboard::WmfGraphicsType;
1533 // we also check in getStatus()
1534 LYXERR0("Unrecognized graphics type: " << arg);
1536 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1539 bv->buffer().errors("Paste");
1540 bv->buffer().updatePreviews(); // bug 11619
1541 cur.clearSelection(); // bug 393
1547 cutSelection(cur, true);
1548 cur.message(_("Cut"));
1553 cur.message(_("Copy"));
1556 case LFUN_SERVER_GET_XY:
1557 cur.message(from_utf8(
1558 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1559 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1562 case LFUN_SERVER_SET_XY: {
1565 istringstream is(to_utf8(cmd.argument()));
1568 lyxerr << "SETXY: Could not parse coordinates in '"
1569 << to_utf8(cmd.argument()) << endl;
1571 tm->setCursorFromCoordinates(cur, x, y);
1575 case LFUN_SERVER_GET_LAYOUT:
1576 cur.message(cur.paragraph().layout().name());
1580 case LFUN_LAYOUT_TOGGLE: {
1581 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1582 docstring req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1583 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(req_layout));
1585 docstring layout = resolveLayout(req_layout, cur);
1586 if (layout.empty()) {
1587 cur.errorMessage(from_utf8(N_("Layout ")) + req_layout +
1588 from_utf8(N_(" not known")));
1592 docstring const old_layout = cur.paragraph().layout().name();
1593 bool change_layout = !isAlreadyLayout(layout, cur);
1595 if (cmd.action() == LFUN_LAYOUT_TOGGLE && !change_layout) {
1596 change_layout = true;
1597 layout = resolveLayout(docstring(), cur);
1600 if (change_layout) {
1601 setLayout(cur, layout);
1602 if (cur.pit() > 0 && !ignoreautonests) {
1603 pit_type prev_pit = cur.pit() - 1;
1604 depth_type const cur_depth = pars_[cur.pit()].getDepth();
1605 // Scan for the previous par on same nesting level
1606 while (prev_pit > 0 && pars_[prev_pit].getDepth() > cur_depth)
1608 set<docstring> const & autonests =
1609 pars_[prev_pit].layout().autonests();
1610 set<docstring> const & autonested =
1611 pars_[cur.pit()].layout().isAutonestedBy();
1612 if (autonests.find(layout) != autonests.end()
1613 || autonested.find(old_layout) != autonested.end())
1614 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1618 DocumentClass const & tclass = bv->buffer().params().documentClass();
1619 bool inautoarg = false;
1620 for (auto const & la_pair : tclass[layout].args()) {
1621 Layout::latexarg const & arg = la_pair.second;
1622 if (arg.autoinsert) {
1623 // If we had already inserted an arg automatically,
1624 // leave this now in order to insert the next one.
1626 cur.leaveInset(cur.inset());
1629 FuncRequest const cmd2(LFUN_ARGUMENT_INSERT, la_pair.first);
1630 lyx::dispatch(cmd2);
1638 case LFUN_ENVIRONMENT_SPLIT: {
1639 bool const outer = cmd.argument() == "outer";
1640 bool const previous = cmd.argument() == "previous";
1641 bool const before = cmd.argument() == "before";
1642 bool const normal = cmd.argument().empty();
1643 Paragraph const & para = cur.paragraph();
1645 if (para.layout().isEnvironment())
1646 layout = para.layout().name();
1647 depth_type split_depth = cur.paragraph().params().depth();
1648 vector<depth_type> nextpars_depth;
1649 if (outer || previous) {
1650 // check if we have an environment in our scope
1651 pit_type pit = cur.pit();
1652 Paragraph cpar = pars_[pit];
1658 if (layout.empty() && previous
1659 && cpar.layout().isEnvironment()
1660 && cpar.params().depth() <= split_depth)
1661 layout = cpar.layout().name();
1662 if (cpar.params().depth() < split_depth
1663 && cpar.layout().isEnvironment()) {
1665 layout = cpar.layout().name();
1666 split_depth = cpar.params().depth();
1668 if (cpar.params().depth() == 0)
1672 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1673 // save nesting of following paragraphs if they are deeper
1675 pit_type offset = 1;
1676 depth_type cur_depth = pars_[cur.pit()].params().depth();
1677 while (cur.pit() + offset <= cur.lastpit()) {
1678 Paragraph cpar = pars_[cur.pit() + offset];
1679 depth_type nextpar_depth = cpar.params().depth();
1680 if (cur_depth <= nextpar_depth) {
1681 nextpars_depth.push_back(nextpar_depth);
1682 cur_depth = nextpar_depth;
1689 cur.top().setPitPos(cur.pit(), 0);
1690 if (before || cur.pos() > 0)
1691 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1692 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1693 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1695 while (cur.paragraph().params().depth() > split_depth)
1696 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1698 DocumentClass const & tc = bv->buffer().params().documentClass();
1699 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1700 + from_ascii("\" ignoreautonests")));
1701 // FIXME: Bibitem mess!
1702 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
1703 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
1704 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1707 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1708 while (cur.paragraph().params().depth() < split_depth)
1709 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1712 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1713 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1714 if ((outer || normal) && !nextpars_depth.empty()) {
1715 // restore nesting of following paragraphs
1716 DocIterator scur = cur;
1717 depth_type max_depth = cur.paragraph().params().depth() + 1;
1718 for (auto nextpar_depth : nextpars_depth) {
1720 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth)) {
1721 depth_type const olddepth = cur.paragraph().params().depth();
1722 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1723 if (olddepth == cur.paragraph().params().depth())
1724 // leave loop if no incrementation happens
1727 max_depth = cur.paragraph().params().depth() + 1;
1729 cur.setCursor(scur);
1735 case LFUN_CLIPBOARD_PASTE:
1736 cap::replaceSelection(cur);
1737 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1738 cmd.argument() == "paragraph");
1739 bv->buffer().errors("Paste");
1742 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1743 cap::replaceSelection(cur);
1744 pasteSimpleText(cur, cmd.argument() == "paragraph");
1747 case LFUN_PRIMARY_SELECTION_PASTE:
1748 cap::replaceSelection(cur);
1749 pasteString(cur, theSelection().get(),
1750 cmd.argument() == "paragraph");
1753 case LFUN_SELECTION_PASTE:
1754 // Copy the selection buffer to the clipboard stack,
1755 // because we want it to appear in the "Edit->Paste
1757 cap::replaceSelection(cur);
1758 cap::copySelectionToStack();
1759 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1760 bv->buffer().errors("Paste");
1763 case LFUN_QUOTE_INSERT: {
1764 cap::replaceSelection(cur);
1767 Paragraph const & par = cur.paragraph();
1768 pos_type pos = cur.pos();
1769 // Ignore deleted text before cursor
1770 while (pos > 0 && par.isDeleted(pos - 1))
1773 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1775 // Guess quote side.
1776 // A space triggers an opening quote. This is passed if the preceding
1777 // char/inset is a space or at paragraph start.
1779 if (pos > 0 && !par.isSpace(pos - 1)) {
1780 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1781 // If an opening double quotation mark precedes, and this
1782 // is a single quote, make it opening as well
1784 static_cast<InsetQuotes &>(*cur.prevInset());
1785 string const type = ins.getType();
1786 if (!suffixIs(type, "ld") || !inner)
1787 c = par.getChar(pos - 1);
1789 else if (!cur.prevInset()
1790 || (cur.prevInset() && cur.prevInset()->isChar()))
1791 // If a char precedes, pass that and let InsetQuote decide
1792 c = par.getChar(pos - 1);
1795 if (par.getInset(pos - 1)
1796 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1797 // skip "invisible" insets
1801 c = par.getChar(pos - 1);
1806 QuoteLevel const quote_level = inner
1807 ? QuoteLevel::Secondary : QuoteLevel::Primary;
1808 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1809 cur.buffer()->updateBuffer();
1814 case LFUN_MOUSE_TRIPLE:
1815 if (cmd.button() == mouse_button::button1) {
1817 setCursor(cur, cur.pit(), 0);
1820 if (cur.pos() < cur.lastpos())
1821 setCursor(cur, cur.pit(), cur.lastpos());
1827 case LFUN_MOUSE_DOUBLE:
1828 if (cmd.button() == mouse_button::button1) {
1829 selectWord(cur, WHOLE_WORD);
1834 // Single-click on work area
1835 case LFUN_MOUSE_PRESS: {
1836 // We are not marking a selection with the keyboard in any case.
1837 Cursor & bvcur = cur.bv().cursor();
1838 bvcur.setMark(false);
1839 switch (cmd.button()) {
1840 case mouse_button::button1:
1841 if (!bvcur.selection())
1843 bvcur.resetAnchor();
1844 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1845 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1846 if (bvcur.wordSelection())
1847 selectWord(bvcur, WHOLE_WORD);
1850 case mouse_button::button2:
1851 if (lyxrc.mouse_middlebutton_paste) {
1852 // Middle mouse pasting.
1853 bv->mouseSetCursor(cur);
1855 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1856 "selection-paste ; primary-selection-paste paragraph"));
1858 cur.noScreenUpdate();
1861 case mouse_button::button3: {
1862 // Don't do anything if we right-click a
1863 // selection, a context menu will popup.
1864 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1865 && cur < bvcur.selectionEnd()) {
1866 cur.noScreenUpdate();
1869 if (!bv->mouseSetCursor(cur, false))
1870 cur.screenUpdateFlags(Update::FitCursor);
1876 } // switch (cmd.button())
1879 case LFUN_MOUSE_MOTION: {
1880 // Mouse motion with right or middle mouse do nothing for now.
1881 if (cmd.button() != mouse_button::button1) {
1882 cur.noScreenUpdate();
1885 // ignore motions deeper nested than the real anchor
1886 Cursor & bvcur = cur.bv().cursor();
1887 if (!bvcur.realAnchor().hasPart(cur)) {
1891 CursorSlice old = bvcur.top();
1893 int const wh = bv->workHeight();
1894 int const y = max(0, min(wh - 1, cmd.y()));
1896 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1897 cur.setTargetX(cmd.x());
1898 // Don't allow selecting a separator inset
1899 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1902 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1903 else if (cmd.y() < 0)
1904 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1905 // This is to allow jumping over large insets
1906 if (cur.top() == old) {
1908 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1909 else if (cmd.y() < 0)
1910 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1912 // We continue with our existing selection or start a new one, so don't
1913 // reset the anchor.
1914 bvcur.setCursor(cur);
1915 bvcur.selection(true);
1916 bvcur.setCurrentFont();
1917 if (cur.top() == old) {
1918 // We didn't move one iota, so no need to update the screen.
1919 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1920 //cur.noScreenUpdate();
1926 case LFUN_MOUSE_RELEASE:
1927 switch (cmd.button()) {
1928 case mouse_button::button1:
1929 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1930 // If there is a new selection, update persistent selection;
1931 // otherwise, single click does not clear persistent selection
1933 if (cur.selection()) {
1934 // Finish selection. If double click,
1935 // cur is moved to the end of word by
1936 // selectWord but bvcur is current
1938 cur.bv().cursor().setSelection();
1939 // We might have removed an empty but drawn selection
1940 // (probably a margin)
1941 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1943 cur.noScreenUpdate();
1944 // FIXME: We could try to handle drag and drop of selection here.
1947 case mouse_button::button2:
1948 // Middle mouse pasting is handled at mouse press time,
1949 // see LFUN_MOUSE_PRESS.
1950 cur.noScreenUpdate();
1953 case mouse_button::button3:
1954 // Cursor was set at LFUN_MOUSE_PRESS time.
1955 // FIXME: If there is a selection we could try to handle a special
1956 // drag & drop context menu.
1957 cur.noScreenUpdate();
1960 case mouse_button::none:
1961 case mouse_button::button4:
1962 case mouse_button::button5:
1964 } // switch (cmd.button())
1968 case LFUN_SELF_INSERT: {
1969 if (cmd.argument().empty())
1972 // Automatically delete the currently selected
1973 // text and replace it with what is being
1974 // typed in now. Depends on lyxrc settings
1975 // "auto_region_delete", which defaults to
1978 if (lyxrc.auto_region_delete && cur.selection()) {
1979 cutSelection(cur, false);
1980 cur.setCurrentFont();
1982 cur.clearSelection();
1984 for (char_type c : cmd.argument())
1985 bv->translateAndInsert(c, this, cur);
1988 moveCursor(cur, false);
1989 cur.markNewWordPosition();
1990 bv->bookmarkEditPosition();
1994 case LFUN_HREF_INSERT: {
1995 docstring content = cmd.argument();
1996 if (content.empty() && cur.selection())
1997 content = cur.selectionAsString(false);
1999 InsetCommandParams p(HYPERLINK_CODE);
2000 if (!content.empty()){
2001 // if it looks like a link, we'll put it as target,
2002 // otherwise as name (bug #8792).
2005 // regex_match(to_utf8(content), matches, link_re)
2006 // because smatch stores pointers to the substrings rather
2007 // than making copies of them. And those pointers become
2008 // invalid after regex_match returns, since it is then
2009 // being given a temporary object. (Thanks to Georg for
2010 // figuring that out.)
2011 regex const link_re("^([a-z]+):.*");
2013 string const c = to_utf8(lowercase(content));
2015 if (c.substr(0,7) == "mailto:") {
2016 p["target"] = content;
2017 p["type"] = from_ascii("mailto:");
2018 } else if (regex_match(c, matches, link_re)) {
2019 p["target"] = content;
2020 string protocol = matches.str(1);
2021 if (protocol == "file")
2022 p["type"] = from_ascii("file:");
2024 p["name"] = content;
2026 string const data = InsetCommand::params2string(p);
2028 // we need to have a target. if we already have one, then
2029 // that gets used at the default for the name, too, which
2030 // is probably what is wanted.
2031 if (p["target"].empty()) {
2032 bv->showDialog("href", data);
2034 FuncRequest fr(LFUN_INSET_INSERT, data);
2040 case LFUN_LABEL_INSERT: {
2041 InsetCommandParams p(LABEL_CODE);
2042 // Try to generate a valid label
2043 p["name"] = (cmd.argument().empty()) ?
2044 cur.getPossibleLabel() :
2046 string const data = InsetCommand::params2string(p);
2048 if (cmd.argument().empty()) {
2049 bv->showDialog("label", data);
2051 FuncRequest fr(LFUN_INSET_INSERT, data);
2057 case LFUN_INFO_INSERT: {
2058 if (cmd.argument().empty()) {
2059 bv->showDialog("info", cur.current_font.language()->lang());
2062 inset = createInset(cur.buffer(), cmd);
2066 insertInset(cur, inset);
2067 cur.forceBufferUpdate();
2072 case LFUN_CAPTION_INSERT:
2073 case LFUN_FOOTNOTE_INSERT:
2074 case LFUN_NOTE_INSERT:
2075 case LFUN_BOX_INSERT:
2076 case LFUN_BRANCH_INSERT:
2077 case LFUN_PHANTOM_INSERT:
2078 case LFUN_ERT_INSERT:
2079 case LFUN_LISTING_INSERT:
2080 case LFUN_MARGINALNOTE_INSERT:
2081 case LFUN_ARGUMENT_INSERT:
2082 case LFUN_INDEX_INSERT:
2083 case LFUN_PREVIEW_INSERT:
2084 case LFUN_SCRIPT_INSERT:
2085 case LFUN_IPA_INSERT:
2086 // Open the inset, and move the current selection
2088 doInsertInset(cur, this, cmd, true, true);
2090 if (act == LFUN_SCRIPT_INSERT) {
2091 /* Script insets change the font style in metrics(), and
2092 * this is used to compute the height of the caret
2093 * (because the font is stored in TextMetrics::font_).
2094 * When we insert, we have to make sure that metrics are
2095 * computed so that the caret height is wrong. Arguably,
2096 * this is hackish.*/
2097 bv->processUpdateFlags(Update::SinglePar);
2099 cur.setCurrentFont();
2100 // Some insets are numbered, others are shown in the outline pane so
2101 // let's update the labels and the toc backend.
2102 cur.forceBufferUpdate();
2105 case LFUN_FLEX_INSERT: {
2106 // Open the inset, and move the current selection
2108 bool const sel = cur.selection();
2109 doInsertInset(cur, this, cmd, true, true);
2110 // Insert auto-insert arguments
2111 bool autoargs = false, inautoarg = false;
2112 Layout::LaTeXArgMap args = cur.inset().getLayout().args();
2113 for (auto const & argt : args) {
2114 Layout::latexarg arg = argt.second;
2115 if (!inautoarg && arg.insertonnewline && cur.pos() > 0) {
2116 FuncRequest cmd2(LFUN_PARAGRAPH_BREAK);
2117 lyx::dispatch(cmd2);
2119 if (arg.autoinsert) {
2120 // The cursor might have been invalidated by the replaceSelection.
2121 cur.buffer()->changed(true);
2122 // If we had already inserted an arg automatically,
2123 // leave this now in order to insert the next one.
2125 cur.leaveInset(cur.inset());
2127 if (arg.insertonnewline && cur.pos() > 0) {
2128 FuncRequest cmd2(LFUN_PARAGRAPH_BREAK);
2129 lyx::dispatch(cmd2);
2132 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, argt.first);
2133 lyx::dispatch(cmd2);
2140 cur.leaveInset(cur.inset());
2143 // Some insets are numbered, others are shown in the outline pane so
2144 // let's update the labels and the toc backend.
2145 cur.forceBufferUpdate();
2149 case LFUN_TABULAR_INSERT: {
2150 // if there were no arguments, just open the dialog
2151 if (cmd.argument().empty()) {
2152 bv->showDialog("tabularcreate");
2154 } else if (cur.buffer()->masterParams().tablestyle != "default"
2155 || bv->buffer().params().documentClass().tablestyle() != "default") {
2156 string tabstyle = cur.buffer()->masterParams().tablestyle;
2157 if (tabstyle == "default")
2158 tabstyle = bv->buffer().params().documentClass().tablestyle();
2159 if (!libFileSearch("tabletemplates", tabstyle + ".lyx").empty()) {
2160 FuncRequest fr(LFUN_TABULAR_STYLE_INSERT,
2161 tabstyle + " " + to_ascii(cmd.argument()));
2165 // Unknown style. Report and fall back to default.
2166 cur.errorMessage(from_utf8(N_("Table Style ")) + from_utf8(tabstyle) +
2167 from_utf8(N_(" not known")));
2169 if (doInsertInset(cur, this, cmd, false, true))
2174 case LFUN_TABULAR_STYLE_INSERT: {
2175 string const style = cmd.getArg(0);
2176 string const rows = cmd.getArg(1);
2177 string const cols = cmd.getArg(2);
2178 if (cols.empty() || !isStrInt(cols)
2179 || rows.empty() || !isStrInt(rows))
2181 int const r = convert<int>(rows);
2182 int const c = convert<int>(cols);
2189 FileName const tabstyle = libFileSearch("tabletemplates",
2190 style + suffix + ".lyx", "lyx");
2191 if (tabstyle.empty())
2193 UndoGroupHelper ugh(cur.buffer());
2195 FuncRequest cmd2(LFUN_FILE_INSERT, tabstyle.absFileName() + " ignorelang");
2196 lyx::dispatch(cmd2);
2200 // move one cell up to middle cell
2202 // add the missing rows
2203 int const addrows = r - 3;
2204 for (int i = 0 ; i < addrows ; ++i) {
2205 FuncRequest fr(LFUN_TABULAR_FEATURE, "append-row");
2209 // add the missing columns
2210 int const addcols = c - 1;
2211 for (int i = 0 ; i < addcols ; ++i) {
2212 FuncRequest fr(LFUN_TABULAR_FEATURE, "append-column");
2221 case LFUN_FLOAT_INSERT:
2222 case LFUN_FLOAT_WIDE_INSERT:
2223 case LFUN_WRAP_INSERT: {
2224 // will some content be moved into the inset?
2225 bool const content = cur.selection();
2226 // does the content consist of multiple paragraphs?
2227 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2229 doInsertInset(cur, this, cmd, true, true);
2232 // If some single-par content is moved into the inset,
2233 // doInsertInset puts the cursor outside the inset.
2234 // To insert the caption we put it back into the inset.
2235 // FIXME cleanup doInsertInset to avoid such dances!
2236 if (content && singlepar)
2239 ParagraphList & pars = cur.text()->paragraphs();
2241 DocumentClass const & tclass = bv->buffer().params().documentClass();
2243 // add a separate paragraph for the caption inset
2244 pars.push_back(Paragraph());
2245 pars.back().setInsetOwner(&cur.text()->inset());
2246 pars.back().setPlainOrDefaultLayout(tclass);
2247 int cap_pit = pars.size() - 1;
2249 // if an empty inset was created, we create an additional empty
2250 // paragraph at the bottom so that the user can choose where to put
2251 // the graphics (or table).
2253 pars.push_back(Paragraph());
2254 pars.back().setInsetOwner(&cur.text()->inset());
2255 pars.back().setPlainOrDefaultLayout(tclass);
2258 // reposition the cursor to the caption
2259 cur.pit() = cap_pit;
2261 // FIXME: This Text/Cursor dispatch handling is a mess!
2262 // We cannot use Cursor::dispatch here it needs access to up to
2264 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2265 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2266 cur.forceBufferUpdate();
2267 cur.screenUpdateFlags(Update::Force);
2268 // FIXME: When leaving the Float (or Wrap) inset we should
2269 // delete any empty paragraph left above or below the
2274 case LFUN_NOMENCL_INSERT: {
2275 InsetCommandParams p(NOMENCL_CODE);
2276 if (cmd.argument().empty()) {
2278 bv->cursor().innerText()->getStringForDialog(bv->cursor());
2279 cur.clearSelection();
2281 p["symbol"] = cmd.argument();
2282 string const data = InsetCommand::params2string(p);
2283 bv->showDialog("nomenclature", data);
2287 case LFUN_INDEX_PRINT: {
2288 InsetCommandParams p(INDEX_PRINT_CODE);
2289 if (cmd.argument().empty())
2290 p["type"] = from_ascii("idx");
2292 p["type"] = cmd.argument();
2293 string const data = InsetCommand::params2string(p);
2294 FuncRequest fr(LFUN_INSET_INSERT, data);
2299 case LFUN_NOMENCL_PRINT:
2300 case LFUN_NEWPAGE_INSERT:
2302 doInsertInset(cur, this, cmd, false, false);
2306 case LFUN_SEPARATOR_INSERT: {
2307 doInsertInset(cur, this, cmd, false, false);
2309 // remove a following space
2310 Paragraph & par = cur.paragraph();
2311 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2312 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2316 case LFUN_DEPTH_DECREMENT:
2317 changeDepth(cur, DEC_DEPTH);
2320 case LFUN_DEPTH_INCREMENT:
2321 changeDepth(cur, INC_DEPTH);
2324 case LFUN_REGEXP_MODE:
2325 regexpDispatch(cur, cmd);
2328 case LFUN_MATH_MODE: {
2329 if (cmd.argument() == "on" || cmd.argument() == "") {
2330 // don't pass "on" as argument
2331 // (it would appear literally in the first cell)
2332 docstring sel = cur.selectionAsString(false);
2333 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2334 // create a macro template if we see "\\newcommand" somewhere, and
2335 // an ordinary formula otherwise
2337 && (sel.find(from_ascii("\\newcommand")) != string::npos
2338 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2339 || sel.find(from_ascii("\\def")) != string::npos)
2340 && macro->fromString(sel)) {
2342 replaceSelection(cur);
2345 // no meaningful macro template was found
2347 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2350 // The argument is meaningful
2351 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2352 // has a different meaning in math mode
2353 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2357 case LFUN_MATH_MACRO:
2358 if (cmd.argument().empty())
2359 cur.errorMessage(from_utf8(N_("Missing argument")));
2362 string s = to_utf8(cmd.argument());
2363 string const s1 = token(s, ' ', 1);
2364 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2365 string const s2 = token(s, ' ', 2);
2366 MacroType type = MacroTypeNewcommand;
2368 type = MacroTypeDef;
2369 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2370 from_utf8(token(s, ' ', 0)), nargs, false, type);
2371 inset->setBuffer(bv->buffer());
2372 insertInset(cur, inset);
2374 // enter macro inset and select the name
2376 cur.top().pos() = cur.top().lastpos();
2378 cur.selection(true);
2379 cur.top().pos() = 0;
2383 case LFUN_MATH_DISPLAY:
2384 case LFUN_MATH_SUBSCRIPT:
2385 case LFUN_MATH_SUPERSCRIPT:
2386 case LFUN_MATH_INSERT:
2387 case LFUN_MATH_AMS_MATRIX:
2388 case LFUN_MATH_MATRIX:
2389 case LFUN_MATH_DELIM:
2390 case LFUN_MATH_BIGDELIM:
2391 mathDispatch(cur, cmd);
2394 case LFUN_FONT_EMPH: {
2395 Font font(ignore_font, ignore_language);
2396 font.fontInfo().setEmph(FONT_TOGGLE);
2397 toggleAndShow(cur, this, font);
2401 case LFUN_FONT_ITAL: {
2402 Font font(ignore_font, ignore_language);
2403 font.fontInfo().setShape(ITALIC_SHAPE);
2404 toggleAndShow(cur, this, font);
2408 case LFUN_FONT_BOLD:
2409 case LFUN_FONT_BOLDSYMBOL: {
2410 Font font(ignore_font, ignore_language);
2411 font.fontInfo().setSeries(BOLD_SERIES);
2412 toggleAndShow(cur, this, font);
2416 case LFUN_FONT_NOUN: {
2417 Font font(ignore_font, ignore_language);
2418 font.fontInfo().setNoun(FONT_TOGGLE);
2419 toggleAndShow(cur, this, font);
2423 case LFUN_FONT_TYPEWRITER: {
2424 Font font(ignore_font, ignore_language);
2425 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2426 toggleAndShow(cur, this, font);
2430 case LFUN_FONT_SANS: {
2431 Font font(ignore_font, ignore_language);
2432 font.fontInfo().setFamily(SANS_FAMILY);
2433 toggleAndShow(cur, this, font);
2437 case LFUN_FONT_ROMAN: {
2438 Font font(ignore_font, ignore_language);
2439 font.fontInfo().setFamily(ROMAN_FAMILY);
2440 toggleAndShow(cur, this, font);
2444 case LFUN_FONT_DEFAULT: {
2445 Font font(inherit_font, ignore_language);
2446 toggleAndShow(cur, this, font);
2450 case LFUN_FONT_STRIKEOUT: {
2451 Font font(ignore_font, ignore_language);
2452 font.fontInfo().setStrikeout(FONT_TOGGLE);
2453 toggleAndShow(cur, this, font);
2457 case LFUN_FONT_CROSSOUT: {
2458 Font font(ignore_font, ignore_language);
2459 font.fontInfo().setXout(FONT_TOGGLE);
2460 toggleAndShow(cur, this, font);
2464 case LFUN_FONT_UNDERUNDERLINE: {
2465 Font font(ignore_font, ignore_language);
2466 font.fontInfo().setUuline(FONT_TOGGLE);
2467 toggleAndShow(cur, this, font);
2471 case LFUN_FONT_UNDERWAVE: {
2472 Font font(ignore_font, ignore_language);
2473 font.fontInfo().setUwave(FONT_TOGGLE);
2474 toggleAndShow(cur, this, font);
2478 case LFUN_FONT_UNDERLINE: {
2479 Font font(ignore_font, ignore_language);
2480 font.fontInfo().setUnderbar(FONT_TOGGLE);
2481 toggleAndShow(cur, this, font);
2485 case LFUN_FONT_SIZE: {
2486 Font font(ignore_font, ignore_language);
2487 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2488 toggleAndShow(cur, this, font);
2492 case LFUN_LANGUAGE: {
2493 string const lang_arg = cmd.getArg(0);
2494 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2495 Language const * lang =
2496 reset ? reset_language
2497 : languages.getLanguage(lang_arg);
2498 // we allow reset_language, which is 0, but only if it
2499 // was requested via empty or "reset" arg.
2500 if (!lang && !reset)
2502 bool const toggle = (cmd.getArg(1) != "set");
2503 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2504 Font font(ignore_font, lang);
2505 toggleAndShow(cur, this, font, toggle);
2506 // We need a buffer update if we change the language
2508 if (cur.insetInSelection(INFO_CODE))
2509 cur.forceBufferUpdate();
2513 case LFUN_TEXTSTYLE_APPLY: {
2514 unsigned int num = 0;
2515 string const arg = to_utf8(cmd.argument());
2518 if (isStrUnsignedInt(arg)) {
2519 num = convert<uint>(arg);
2520 if (num >= freeFonts.size()) {
2521 cur.message(_("Invalid argument (number exceeds stack size)!"));
2525 cur.message(_("Invalid argument (must be a non-negative number)!"));
2529 toggleAndShow(cur, this, freeFonts[num].second, toggleall);
2530 cur.message(bformat(_("Text properties applied: %1$s"), freeFonts[num].first));
2534 // Set the freefont using the contents of \param data dispatched from
2535 // the frontends and apply it at the current cursor location.
2536 case LFUN_TEXTSTYLE_UPDATE: {
2537 Font font(ignore_font, ignore_language);
2539 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2540 docstring const props = font.stateText(&bv->buffer().params(), true);
2541 freeFonts.push(make_pair(props, font));
2543 toggleAndShow(cur, this, font, toggleall);
2544 // We need a buffer update if we change the language
2546 if (cur.insetInSelection(INFO_CODE))
2547 cur.forceBufferUpdate();
2548 cur.message(bformat(_("Text properties applied: %1$s"), props));
2550 LYXERR0("Invalid argument of textstyle-update");
2554 case LFUN_FINISHED_LEFT:
2555 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2556 // We're leaving an inset, going left. If the inset is LTR, we're
2557 // leaving from the front, so we should not move (remain at --- but
2558 // not in --- the inset). If the inset is RTL, move left, without
2559 // entering the inset itself; i.e., move to after the inset.
2560 if (cur.paragraph().getFontSettings(
2561 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2562 cursorVisLeft(cur, true);
2565 case LFUN_FINISHED_RIGHT:
2566 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2567 // We're leaving an inset, going right. If the inset is RTL, we're
2568 // leaving from the front, so we should not move (remain at --- but
2569 // not in --- the inset). If the inset is LTR, move right, without
2570 // entering the inset itself; i.e., move to after the inset.
2571 if (!cur.paragraph().getFontSettings(
2572 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2573 cursorVisRight(cur, true);
2576 case LFUN_FINISHED_BACKWARD:
2577 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2578 cur.setCurrentFont();
2581 case LFUN_FINISHED_FORWARD:
2582 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2584 cur.setCurrentFont();
2587 case LFUN_LAYOUT_PARAGRAPH: {
2589 params2string(cur.paragraph(), data);
2590 data = "show\n" + data;
2591 bv->showDialog("paragraph", data);
2595 case LFUN_PARAGRAPH_UPDATE: {
2597 params2string(cur.paragraph(), data);
2599 // Will the paragraph accept changes from the dialog?
2601 cur.inset().allowParagraphCustomization(cur.idx());
2603 data = "update " + convert<string>(accept) + '\n' + data;
2604 bv->updateDialog("paragraph", data);
2608 case LFUN_ACCENT_UMLAUT:
2609 case LFUN_ACCENT_CIRCUMFLEX:
2610 case LFUN_ACCENT_GRAVE:
2611 case LFUN_ACCENT_ACUTE:
2612 case LFUN_ACCENT_TILDE:
2613 case LFUN_ACCENT_PERISPOMENI:
2614 case LFUN_ACCENT_CEDILLA:
2615 case LFUN_ACCENT_MACRON:
2616 case LFUN_ACCENT_DOT:
2617 case LFUN_ACCENT_UNDERDOT:
2618 case LFUN_ACCENT_UNDERBAR:
2619 case LFUN_ACCENT_CARON:
2620 case LFUN_ACCENT_BREVE:
2621 case LFUN_ACCENT_TIE:
2622 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2623 case LFUN_ACCENT_CIRCLE:
2624 case LFUN_ACCENT_OGONEK:
2625 theApp()->handleKeyFunc(cmd.action());
2626 if (!cmd.argument().empty())
2627 // FIXME: Are all these characters encoded in one byte in utf8?
2628 bv->translateAndInsert(cmd.argument()[0], this, cur);
2629 cur.screenUpdateFlags(Update::FitCursor);
2632 case LFUN_FLOAT_LIST_INSERT: {
2633 DocumentClass const & tclass = bv->buffer().params().documentClass();
2634 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2636 if (cur.selection())
2637 cutSelection(cur, false);
2638 breakParagraph(cur);
2640 if (cur.lastpos() != 0) {
2641 cursorBackward(cur);
2642 breakParagraph(cur);
2645 docstring const laystr = cur.inset().usePlainLayout() ?
2646 tclass.plainLayoutName() :
2647 tclass.defaultLayoutName();
2648 setLayout(cur, laystr);
2649 ParagraphParameters p;
2650 // FIXME If this call were replaced with one to clearParagraphParams(),
2651 // then we could get rid of this method altogether.
2652 setParagraphs(cur, p);
2653 // FIXME This should be simplified when InsetFloatList takes a
2654 // Buffer in its constructor.
2655 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2656 ifl->setBuffer(bv->buffer());
2657 insertInset(cur, ifl);
2660 lyxerr << "Non-existent float type: "
2661 << to_utf8(cmd.argument()) << endl;
2666 case LFUN_CHANGE_ACCEPT: {
2667 acceptOrRejectChanges(cur, ACCEPT);
2671 case LFUN_CHANGE_REJECT: {
2672 acceptOrRejectChanges(cur, REJECT);
2676 case LFUN_THESAURUS_ENTRY: {
2677 Language const * language = cur.getFont().language();
2678 docstring arg = cmd.argument();
2680 arg = cur.selectionAsString(false);
2682 if (arg.size() > 100 || arg.empty()) {
2683 // Get word or selection
2684 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2685 arg = cur.selectionAsString(false);
2686 arg += " lang=" + from_ascii(language->lang());
2689 string lang = cmd.getArg(1);
2690 // This duplicates the code in GuiThesaurus::initialiseParams
2691 if (prefixIs(lang, "lang=")) {
2692 language = languages.getLanguage(lang.substr(5));
2694 language = cur.getFont().language();
2697 string lang = language->code();
2698 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2699 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2700 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2701 _("The path to the thesaurus directory has not been specified.\n"
2702 "The thesaurus is not functional.\n"
2703 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2706 bv->showDialog("thesaurus", to_utf8(arg));
2710 case LFUN_SPELLING_ADD: {
2711 Language const * language = getLanguage(cur, cmd.getArg(1));
2712 docstring word = from_utf8(cmd.getArg(0));
2714 word = cur.selectionAsString(false);
2716 if (word.size() > 100 || word.empty()) {
2717 // Get word or selection
2718 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2719 word = cur.selectionAsString(false);
2722 WordLangTuple wl(word, language);
2723 theSpellChecker()->insert(wl);
2727 case LFUN_SPELLING_IGNORE: {
2728 Language const * language = getLanguage(cur, cmd.getArg(1));
2729 docstring word = from_utf8(cmd.getArg(0));
2731 word = cur.selectionAsString(false);
2733 if (word.size() > 100 || word.empty()) {
2734 // Get word or selection
2735 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2736 word = cur.selectionAsString(false);
2739 WordLangTuple wl(word, language);
2740 theSpellChecker()->accept(wl);
2744 case LFUN_SPELLING_REMOVE: {
2745 Language const * language = getLanguage(cur, cmd.getArg(1));
2746 docstring word = from_utf8(cmd.getArg(0));
2748 word = cur.selectionAsString(false);
2750 if (word.size() > 100 || word.empty()) {
2751 // Get word or selection
2752 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2753 word = cur.selectionAsString(false);
2756 WordLangTuple wl(word, language);
2757 theSpellChecker()->remove(wl);
2761 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2762 // Given data, an encoding of the ParagraphParameters
2763 // generated in the Paragraph dialog, this function sets
2764 // the current paragraph, or currently selected paragraphs,
2766 // NOTE: This function overrides all existing settings.
2767 setParagraphs(cur, cmd.argument());
2768 cur.message(_("Paragraph layout set"));
2772 case LFUN_PARAGRAPH_PARAMS: {
2773 // Given data, an encoding of the ParagraphParameters as we'd
2774 // find them in a LyX file, this function modifies the current paragraph,
2775 // or currently selected paragraphs.
2776 // NOTE: This function only modifies, and does not override, existing
2778 setParagraphs(cur, cmd.argument(), true);
2779 cur.message(_("Paragraph layout set"));
2784 if (cur.selection()) {
2785 cur.selection(false);
2788 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2789 // correct, but I'm not 100% sure -- dov, 071019
2790 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2794 case LFUN_OUTLINE_UP: {
2795 pos_type const opos = cur.pos();
2796 outline(OutlineUp, cur, this);
2797 setCursor(cur, cur.pit(), opos);
2798 cur.forceBufferUpdate();
2803 case LFUN_OUTLINE_DOWN: {
2804 pos_type const opos = cur.pos();
2805 outline(OutlineDown, cur, this);
2806 setCursor(cur, cur.pit(), opos);
2807 cur.forceBufferUpdate();
2812 case LFUN_OUTLINE_IN:
2813 outline(OutlineIn, cur, this);
2814 cur.forceBufferUpdate();
2818 case LFUN_OUTLINE_OUT:
2819 outline(OutlineOut, cur, this);
2820 cur.forceBufferUpdate();
2824 case LFUN_SERVER_GET_STATISTICS:
2826 DocIterator from, to;
2827 if (cur.selection()) {
2828 from = cur.selectionBegin();
2829 to = cur.selectionEnd();
2831 from = doc_iterator_begin(cur.buffer());
2832 to = doc_iterator_end(cur.buffer());
2835 cur.buffer()->updateStatistics(from, to);
2836 string const arg0 = cmd.getArg(0);
2837 if (arg0 == "words") {
2838 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2839 } else if (arg0 == "chars") {
2840 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2841 } else if (arg0 == "chars-space") {
2842 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2844 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2845 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2846 + convert<docstring>(cur.buffer()->charCount(true)));
2852 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2857 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2859 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2860 // Check for misspelled text
2861 // The redraw is useful because of the painting of
2862 // misspelled markers depends on the cursor position.
2863 // Trigger a redraw for cursor moves inside misspelled text.
2864 if (!cur.inTexted()) {
2865 // move from regular text to math
2866 needsUpdate = last_misspelled;
2867 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2868 // move inside regular text
2869 needsUpdate = last_misspelled
2870 || cur.paragraph().isMisspelled(cur.pos(), true);
2874 // FIXME: The cursor flag is reset two lines below
2875 // so we need to check here if some of the LFUN did touch that.
2876 // for now only Text::erase() and Text::backspace() do that.
2877 // The plan is to verify all the LFUNs and then to remove this
2878 // singleParUpdate boolean altogether.
2879 if (cur.result().screenUpdate() & Update::Force) {
2880 singleParUpdate = false;
2884 // FIXME: the following code should go in favor of fine grained
2885 // update flag treatment.
2886 if (singleParUpdate) {
2887 // Inserting characters does not change par height in general. So, try
2888 // to update _only_ this paragraph. BufferView will detect if a full
2889 // metrics update is needed anyway.
2890 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2894 && &oldTopSlice.inset() == &cur.inset()
2895 && oldTopSlice.idx() == cur.idx()
2896 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2897 && !cur.selection())
2898 // FIXME: it would be better if we could just do this
2900 //if (cur.result().update() != Update::FitCursor)
2901 // cur.noScreenUpdate();
2903 // But some LFUNs do not set Update::FitCursor when needed, so we
2904 // do it for all. This is not very harmfull as FitCursor will provoke
2905 // a full redraw only if needed but still, a proper review of all LFUN
2906 // should be done and this needsUpdate boolean can then be removed.
2907 cur.screenUpdateFlags(Update::FitCursor);
2909 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2913 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2914 FuncStatus & status) const
2916 LBUFERR(this == cur.text());
2918 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2920 bool allow_in_passthru = false;
2921 InsetCode code = NO_CODE;
2923 switch (cmd.action()) {
2925 case LFUN_DEPTH_DECREMENT:
2926 enable = changeDepthAllowed(cur, DEC_DEPTH);
2929 case LFUN_DEPTH_INCREMENT:
2930 enable = changeDepthAllowed(cur, INC_DEPTH);
2934 // FIXME We really should not allow this to be put, e.g.,
2935 // in a footnote, or in ERT. But it would make sense in a
2936 // branch, so I'm not sure what to do.
2937 status.setOnOff(cur.paragraph().params().startOfAppendix());
2940 case LFUN_DIALOG_SHOW_NEW_INSET:
2941 if (cmd.argument() == "bibitem")
2942 code = BIBITEM_CODE;
2943 else if (cmd.argument() == "bibtex") {
2945 // not allowed in description items
2946 enable = !inDescriptionItem(cur);
2948 else if (cmd.argument() == "box")
2950 else if (cmd.argument() == "branch")
2952 else if (cmd.argument() == "citation")
2954 else if (cmd.argument() == "ert")
2956 else if (cmd.argument() == "external")
2957 code = EXTERNAL_CODE;
2958 else if (cmd.argument() == "float")
2960 else if (cmd.argument() == "graphics")
2961 code = GRAPHICS_CODE;
2962 else if (cmd.argument() == "href")
2963 code = HYPERLINK_CODE;
2964 else if (cmd.argument() == "include")
2965 code = INCLUDE_CODE;
2966 else if (cmd.argument() == "index")
2968 else if (cmd.argument() == "index_print")
2969 code = INDEX_PRINT_CODE;
2970 else if (cmd.argument() == "listings")
2971 code = LISTINGS_CODE;
2972 else if (cmd.argument() == "mathspace")
2973 code = MATH_HULL_CODE;
2974 else if (cmd.argument() == "nomenclature")
2975 code = NOMENCL_CODE;
2976 else if (cmd.argument() == "nomencl_print")
2977 code = NOMENCL_PRINT_CODE;
2978 else if (cmd.argument() == "label")
2980 else if (cmd.argument() == "line")
2982 else if (cmd.argument() == "note")
2984 else if (cmd.argument() == "phantom")
2985 code = PHANTOM_CODE;
2986 else if (cmd.argument() == "ref")
2988 else if (cmd.argument() == "space")
2990 else if (cmd.argument() == "toc")
2992 else if (cmd.argument() == "vspace")
2994 else if (cmd.argument() == "wrap")
2998 case LFUN_ERT_INSERT:
3001 case LFUN_LISTING_INSERT:
3002 code = LISTINGS_CODE;
3003 // not allowed in description items
3004 enable = !inDescriptionItem(cur);
3006 case LFUN_FOOTNOTE_INSERT:
3009 case LFUN_TABULAR_INSERT:
3010 code = TABULAR_CODE;
3012 case LFUN_TABULAR_STYLE_INSERT:
3013 code = TABULAR_CODE;
3015 case LFUN_MARGINALNOTE_INSERT:
3018 case LFUN_FLOAT_INSERT:
3019 case LFUN_FLOAT_WIDE_INSERT:
3020 // FIXME: If there is a selection, we should check whether there
3021 // are floats in the selection, but this has performance issues, see
3022 // LFUN_CHANGE_ACCEPT/REJECT.
3024 if (inDescriptionItem(cur))
3025 // not allowed in description items
3028 InsetCode const inset_code = cur.inset().lyxCode();
3030 // algorithm floats cannot be put in another float
3031 if (to_utf8(cmd.argument()) == "algorithm") {
3032 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
3036 // for figures and tables: only allow in another
3037 // float or wrap if it is of the same type and
3038 // not a subfloat already
3039 if(cur.inset().lyxCode() == code) {
3040 InsetFloat const & ins =
3041 static_cast<InsetFloat const &>(cur.inset());
3042 enable = ins.params().type == to_utf8(cmd.argument())
3043 && !ins.params().subfloat;
3044 } else if(cur.inset().lyxCode() == WRAP_CODE) {
3045 InsetWrap const & ins =
3046 static_cast<InsetWrap const &>(cur.inset());
3047 enable = ins.params().type == to_utf8(cmd.argument());
3051 case LFUN_WRAP_INSERT:
3053 // not allowed in description items
3054 enable = !inDescriptionItem(cur);
3056 case LFUN_FLOAT_LIST_INSERT: {
3057 code = FLOAT_LIST_CODE;
3058 // not allowed in description items
3059 enable = !inDescriptionItem(cur);
3061 FloatList const & floats = cur.buffer()->params().documentClass().floats();
3062 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
3063 // make sure we know about such floats
3064 if (cit == floats.end() ||
3065 // and that we know how to generate a list of them
3066 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
3067 status.setUnknown(true);
3068 // probably not necessary, but...
3074 case LFUN_CAPTION_INSERT: {
3075 code = CAPTION_CODE;
3076 string arg = cmd.getArg(0);
3077 bool varia = arg != "Unnumbered"
3078 && cur.inset().allowsCaptionVariation(arg);
3079 // not allowed in description items,
3080 // and in specific insets
3081 enable = !inDescriptionItem(cur)
3082 && (varia || arg.empty() || arg == "Standard");
3085 case LFUN_NOTE_INSERT:
3088 case LFUN_FLEX_INSERT: {
3090 string s = cmd.getArg(0);
3092 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
3093 if (il.lyxtype() != InsetLyXType::CHARSTYLE &&
3094 il.lyxtype() != InsetLyXType::CUSTOM &&
3095 il.lyxtype ()!= InsetLyXType::STANDARD)
3099 case LFUN_BOX_INSERT:
3102 case LFUN_BRANCH_INSERT:
3104 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
3105 && cur.buffer()->params().branchlist().empty())
3108 case LFUN_IPA_INSERT:
3111 case LFUN_PHANTOM_INSERT:
3112 code = PHANTOM_CODE;
3114 case LFUN_LABEL_INSERT:
3117 case LFUN_INFO_INSERT:
3119 enable = cmd.argument().empty()
3120 || infoparams.validateArgument(cur.buffer(), cmd.argument(), true);
3122 case LFUN_ARGUMENT_INSERT: {
3124 allow_in_passthru = true;
3125 string const arg = cmd.getArg(0);
3130 Layout const & lay = cur.paragraph().layout();
3131 Layout::LaTeXArgMap args = lay.args();
3132 Layout::LaTeXArgMap::const_iterator const lait =
3134 if (lait != args.end()) {
3136 pit_type pit = cur.pit();
3137 pit_type lastpit = cur.pit();
3138 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
3139 // In a sequence of "merged" environment layouts, we only allow
3140 // non-item arguments once.
3141 lastpit = cur.lastpit();
3142 // get the first paragraph in sequence with this layout
3143 depth_type const current_depth = cur.paragraph().params().depth();
3147 Paragraph cpar = pars_[pit - 1];
3148 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
3154 for (; pit <= lastpit; ++pit) {
3155 if (pars_[pit].layout() != lay)
3157 for (auto const & table : pars_[pit].insetList())
3158 if (InsetArgument const * ins = table.inset->asInsetArgument())
3159 if (ins->name() == arg) {
3160 // we have this already
3169 case LFUN_INDEX_INSERT:
3172 case LFUN_INDEX_PRINT:
3173 code = INDEX_PRINT_CODE;
3174 // not allowed in description items
3175 enable = !inDescriptionItem(cur);
3177 case LFUN_NOMENCL_INSERT:
3178 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3182 code = NOMENCL_CODE;
3184 case LFUN_NOMENCL_PRINT:
3185 code = NOMENCL_PRINT_CODE;
3186 // not allowed in description items
3187 enable = !inDescriptionItem(cur);
3189 case LFUN_HREF_INSERT:
3190 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3194 code = HYPERLINK_CODE;
3196 case LFUN_IPAMACRO_INSERT: {
3197 string const arg = cmd.getArg(0);
3199 code = IPADECO_CODE;
3201 code = IPACHAR_CODE;
3204 case LFUN_QUOTE_INSERT:
3205 // always allow this, since we will inset a raw quote
3206 // if an inset is not allowed.
3207 allow_in_passthru = true;
3209 case LFUN_SPECIALCHAR_INSERT:
3210 code = SPECIALCHAR_CODE;
3212 case LFUN_SPACE_INSERT:
3213 // slight hack: we know this is allowed in math mode
3217 case LFUN_PREVIEW_INSERT:
3218 code = PREVIEW_CODE;
3220 case LFUN_SCRIPT_INSERT:
3224 case LFUN_MATH_INSERT:
3225 case LFUN_MATH_AMS_MATRIX:
3226 case LFUN_MATH_MATRIX:
3227 case LFUN_MATH_DELIM:
3228 case LFUN_MATH_BIGDELIM:
3229 case LFUN_MATH_DISPLAY:
3230 case LFUN_MATH_MODE:
3231 case LFUN_MATH_MACRO:
3232 case LFUN_MATH_SUBSCRIPT:
3233 case LFUN_MATH_SUPERSCRIPT:
3234 code = MATH_HULL_CODE;
3237 case LFUN_REGEXP_MODE:
3238 code = MATH_HULL_CODE;
3239 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3242 case LFUN_INSET_MODIFY:
3243 // We need to disable this, because we may get called for a
3245 // InsetTabular::getStatus() -> InsetText::getStatus()
3246 // and we don't handle LFUN_INSET_MODIFY.
3250 case LFUN_FONT_EMPH:
3251 status.setOnOff(fontinfo.emph() == FONT_ON);
3252 enable = !cur.paragraph().isPassThru();
3255 case LFUN_FONT_ITAL:
3256 status.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3257 enable = !cur.paragraph().isPassThru();
3260 case LFUN_FONT_NOUN:
3261 status.setOnOff(fontinfo.noun() == FONT_ON);
3262 enable = !cur.paragraph().isPassThru();
3265 case LFUN_FONT_BOLD:
3266 case LFUN_FONT_BOLDSYMBOL:
3267 status.setOnOff(fontinfo.series() == BOLD_SERIES);
3268 enable = !cur.paragraph().isPassThru();
3271 case LFUN_FONT_SANS:
3272 status.setOnOff(fontinfo.family() == SANS_FAMILY);
3273 enable = !cur.paragraph().isPassThru();
3276 case LFUN_FONT_ROMAN:
3277 status.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3278 enable = !cur.paragraph().isPassThru();
3281 case LFUN_FONT_TYPEWRITER:
3282 status.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3283 enable = !cur.paragraph().isPassThru();
3288 enable = cur.selection();
3292 if (cmd.argument().empty()) {
3293 if (theClipboard().isInternal())
3294 enable = cap::numberOfSelections() > 0;
3296 enable = !theClipboard().empty();
3300 // we have an argument
3301 string const arg = to_utf8(cmd.argument());
3302 if (isStrUnsignedInt(arg)) {
3303 // it's a number and therefore means the internal stack
3304 unsigned int n = convert<unsigned int>(arg);
3305 enable = cap::numberOfSelections() > n;
3309 // explicit text type?
3310 if (arg == "html") {
3311 // Do not enable for PlainTextType, since some tidying in the
3312 // frontend is needed for HTML, which is too unsafe for plain text.
3313 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3315 } else if (arg == "latex") {
3316 // LaTeX is usually not available on the clipboard with
3317 // the correct MIME type, but in plain text.
3318 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3319 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3323 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3325 type = Clipboard::PdfGraphicsType;
3326 else if (arg == "png")
3327 type = Clipboard::PngGraphicsType;
3328 else if (arg == "jpeg")
3329 type = Clipboard::JpegGraphicsType;
3330 else if (arg == "linkback")
3331 type = Clipboard::LinkBackGraphicsType;
3332 else if (arg == "emf")
3333 type = Clipboard::EmfGraphicsType;
3334 else if (arg == "wmf")
3335 type = Clipboard::WmfGraphicsType;
3338 LYXERR0("Unrecognized graphics type: " << arg);
3339 // we don't want to assert if the user just mistyped the LFUN
3340 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3344 enable = theClipboard().hasGraphicsContents(type);
3348 case LFUN_CLIPBOARD_PASTE:
3349 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3350 enable = !theClipboard().empty();
3353 case LFUN_PRIMARY_SELECTION_PASTE:
3354 enable = cur.selection() || !theSelection().empty();
3357 case LFUN_SELECTION_PASTE:
3358 enable = cap::selection();
3361 case LFUN_PARAGRAPH_MOVE_UP:
3362 enable = cur.pit() > 0 && !cur.selection();
3365 case LFUN_PARAGRAPH_MOVE_DOWN:
3366 enable = cur.pit() < cur.lastpit() && !cur.selection();
3369 case LFUN_CHANGE_ACCEPT:
3370 case LFUN_CHANGE_REJECT:
3371 if (!cur.selection())
3372 enable = cur.paragraph().isChanged(cur.pos());
3374 // will enable if there is a change in the selection
3377 // cheap improvement for efficiency: using cached
3378 // buffer variable, if there is no change in the
3379 // document, no need to check further.
3380 if (!cur.buffer()->areChangesPresent())
3383 for (DocIterator it = cur.selectionBegin(); ; it.forwardPar()) {
3384 pos_type const beg = it.pos();
3386 bool const in_last_par = (it.pit() == cur.selectionEnd().pit() &&
3387 it.idx() == cur.selectionEnd().idx());
3389 end = cur.selectionEnd().pos();
3391 // the +1 is needed for cases, e.g., where there is a
3392 // paragraph break. See #11629.
3393 end = it.lastpos() + 1;
3394 if (beg != end && it.paragraph().isChanged(beg, end)) {
3398 if (beg != end && it.paragraph().hasChangedInsets(beg, end)) {
3408 case LFUN_OUTLINE_UP:
3409 case LFUN_OUTLINE_DOWN:
3410 case LFUN_OUTLINE_IN:
3411 case LFUN_OUTLINE_OUT:
3412 // FIXME: LyX is not ready for outlining within inset.
3413 enable = isMainText()
3414 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3417 case LFUN_NEWLINE_INSERT:
3418 // LaTeX restrictions (labels or empty par)
3419 enable = !cur.paragraph().isPassThru()
3420 && cur.pos() > cur.paragraph().beginOfBody();
3423 case LFUN_SEPARATOR_INSERT:
3424 // Always enabled for now
3428 case LFUN_TAB_INSERT:
3429 case LFUN_TAB_DELETE:
3430 enable = cur.paragraph().isPassThru();
3433 case LFUN_GRAPHICS_SET_GROUP: {
3434 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3438 status.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3442 case LFUN_NEWPAGE_INSERT:
3443 // not allowed in description items
3444 code = NEWPAGE_CODE;
3445 enable = !inDescriptionItem(cur);
3449 enable = !cur.paragraph().isPassThru();
3450 status.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3453 case LFUN_PARAGRAPH_BREAK:
3454 enable = inset().allowMultiPar();
3457 case LFUN_SPELLING_ADD:
3458 case LFUN_SPELLING_IGNORE:
3459 case LFUN_SPELLING_REMOVE:
3460 enable = theSpellChecker() != nullptr;
3461 if (enable && !cmd.getArg(1).empty()) {
3462 // validate explicitly given language
3463 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3464 enable &= lang != nullptr;
3469 case LFUN_LAYOUT_TOGGLE: {
3470 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3471 docstring const req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3472 docstring const layout = resolveLayout(req_layout, cur);
3474 enable = !owner_->forcePlainLayout() && !layout.empty();
3475 status.setOnOff(isAlreadyLayout(layout, cur));
3479 case LFUN_ENVIRONMENT_SPLIT: {
3480 if (cmd.argument() == "outer") {
3481 // check if we have an environment in our nesting hierarchy
3483 depth_type const current_depth = cur.paragraph().params().depth();
3484 pit_type pit = cur.pit();
3485 Paragraph cpar = pars_[pit];
3487 if (pit == 0 || cpar.params().depth() == 0)
3491 if (cpar.params().depth() < current_depth)
3492 res = cpar.layout().isEnvironment();
3497 else if (cmd.argument() == "previous") {
3498 // look if we have an environment in the previous par
3499 pit_type pit = cur.pit();
3500 Paragraph cpar = pars_[pit];
3504 enable = cpar.layout().isEnvironment();
3510 else if (cur.paragraph().layout().isEnvironment()) {
3511 enable = cmd.argument() == "before"
3512 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3519 case LFUN_LAYOUT_PARAGRAPH:
3520 case LFUN_PARAGRAPH_PARAMS:
3521 case LFUN_PARAGRAPH_PARAMS_APPLY:
3522 case LFUN_PARAGRAPH_UPDATE:
3523 enable = owner_->allowParagraphCustomization();
3526 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3527 // Because they insert COMBINING DIACRITICAL Unicode characters,
3528 // that cannot be handled by LaTeX but must be converted according
3529 // to the definition in lib/unicodesymbols?
3530 case LFUN_ACCENT_ACUTE:
3531 case LFUN_ACCENT_BREVE:
3532 case LFUN_ACCENT_CARON:
3533 case LFUN_ACCENT_CEDILLA:
3534 case LFUN_ACCENT_CIRCLE:
3535 case LFUN_ACCENT_CIRCUMFLEX:
3536 case LFUN_ACCENT_DOT:
3537 case LFUN_ACCENT_GRAVE:
3538 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3539 case LFUN_ACCENT_MACRON:
3540 case LFUN_ACCENT_OGONEK:
3541 case LFUN_ACCENT_TIE:
3542 case LFUN_ACCENT_TILDE:
3543 case LFUN_ACCENT_PERISPOMENI:
3544 case LFUN_ACCENT_UMLAUT:
3545 case LFUN_ACCENT_UNDERBAR:
3546 case LFUN_ACCENT_UNDERDOT:
3547 case LFUN_FONT_FRAK:
3548 case LFUN_FONT_SIZE:
3549 case LFUN_FONT_STATE:
3550 case LFUN_FONT_UNDERLINE:
3551 case LFUN_FONT_STRIKEOUT:
3552 case LFUN_FONT_CROSSOUT:
3553 case LFUN_FONT_UNDERUNDERLINE:
3554 case LFUN_FONT_UNDERWAVE:
3555 case LFUN_TEXTSTYLE_UPDATE:
3556 enable = !cur.paragraph().isPassThru();
3559 case LFUN_FONT_DEFAULT: {
3560 Font font(inherit_font, ignore_language);
3561 BufferParams const & bp = cur.buffer()->masterParams();
3562 if (cur.selection()) {
3564 // Check if we have a non-default font attribute
3565 // in the selection range.
3566 DocIterator const from = cur.selectionBegin();
3567 DocIterator const to = cur.selectionEnd();
3568 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
3569 if (!dit.inTexted()) {
3573 Paragraph const & par = dit.paragraph();
3574 pos_type const pos = dit.pos();
3575 Font tmp = par.getFontSettings(bp, pos);
3576 if (tmp.fontInfo() != font.fontInfo()
3577 || tmp.language() != bp.language) {
3585 // Disable if all is default already.
3586 enable = (cur.current_font.fontInfo() != font.fontInfo()
3587 || cur.current_font.language() != bp.language);
3591 case LFUN_TEXTSTYLE_APPLY:
3592 enable = !freeFonts.empty();
3595 case LFUN_WORD_DELETE_FORWARD:
3596 case LFUN_WORD_DELETE_BACKWARD:
3597 case LFUN_LINE_DELETE_FORWARD:
3598 case LFUN_WORD_FORWARD:
3599 case LFUN_WORD_BACKWARD:
3600 case LFUN_WORD_RIGHT:
3601 case LFUN_WORD_LEFT:
3602 case LFUN_CHAR_FORWARD:
3603 case LFUN_CHAR_FORWARD_SELECT:
3604 case LFUN_CHAR_BACKWARD:
3605 case LFUN_CHAR_BACKWARD_SELECT:
3606 case LFUN_CHAR_LEFT:
3607 case LFUN_CHAR_LEFT_SELECT:
3608 case LFUN_CHAR_RIGHT:
3609 case LFUN_CHAR_RIGHT_SELECT:
3611 case LFUN_UP_SELECT:
3613 case LFUN_DOWN_SELECT:
3614 case LFUN_PARAGRAPH_SELECT:
3615 case LFUN_PARAGRAPH_UP_SELECT:
3616 case LFUN_PARAGRAPH_DOWN_SELECT:
3617 case LFUN_LINE_BEGIN_SELECT:
3618 case LFUN_LINE_END_SELECT:
3619 case LFUN_WORD_FORWARD_SELECT:
3620 case LFUN_WORD_BACKWARD_SELECT:
3621 case LFUN_WORD_RIGHT_SELECT:
3622 case LFUN_WORD_LEFT_SELECT:
3623 case LFUN_WORD_SELECT:
3624 case LFUN_SECTION_SELECT:
3625 case LFUN_BUFFER_BEGIN:
3626 case LFUN_BUFFER_END:
3627 case LFUN_BUFFER_BEGIN_SELECT:
3628 case LFUN_BUFFER_END_SELECT:
3629 case LFUN_INSET_BEGIN:
3630 case LFUN_INSET_END:
3631 case LFUN_INSET_BEGIN_SELECT:
3632 case LFUN_INSET_END_SELECT:
3633 case LFUN_PARAGRAPH_UP:
3634 case LFUN_PARAGRAPH_DOWN:
3635 case LFUN_LINE_BEGIN:
3637 case LFUN_CHAR_DELETE_FORWARD:
3638 case LFUN_CHAR_DELETE_BACKWARD:
3639 case LFUN_WORD_UPCASE:
3640 case LFUN_WORD_LOWCASE:
3641 case LFUN_WORD_CAPITALIZE:
3642 case LFUN_CHARS_TRANSPOSE:
3643 case LFUN_SERVER_GET_XY:
3644 case LFUN_SERVER_SET_XY:
3645 case LFUN_SERVER_GET_LAYOUT:
3646 case LFUN_SELF_INSERT:
3647 case LFUN_UNICODE_INSERT:
3648 case LFUN_THESAURUS_ENTRY:
3650 case LFUN_SERVER_GET_STATISTICS:
3651 // these are handled in our dispatch()
3655 case LFUN_INSET_INSERT: {
3656 string const type = cmd.getArg(0);
3657 if (type == "toc") {
3659 // not allowed in description items
3660 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3661 enable = !inDescriptionItem(cur);
3668 case LFUN_SEARCH_IGNORE: {
3669 bool const value = cmd.getArg(1) == "true";
3670 setIgnoreFormat(cmd.getArg(0), value);
3680 || !cur.inset().insetAllowed(code)
3681 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3684 status.setEnabled(enable);
3689 void Text::pasteString(Cursor & cur, docstring const & clip,
3692 if (!clip.empty()) {
3695 insertStringAsParagraphs(cur, clip, cur.current_font);
3697 insertStringAsLines(cur, clip, cur.current_font);
3702 // FIXME: an item inset would make things much easier.
3703 bool Text::inDescriptionItem(Cursor const & cur) const
3705 Paragraph const & par = cur.paragraph();
3706 pos_type const pos = cur.pos();
3707 pos_type const body_pos = par.beginOfBody();
3709 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3710 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3711 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3714 return (pos < body_pos
3716 && (pos == 0 || par.getChar(pos - 1) != ' ')));
3720 std::vector<docstring> Text::getFreeFonts() const
3722 vector<docstring> ffList;
3724 for (auto const & f : freeFonts)
3725 ffList.push_back(f.first);