3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
20 #include "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "SpellChecker.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
47 #include "Thesaurus.h"
48 #include "WordLangTuple.h"
50 #include "frontends/alert.h"
51 #include "frontends/Application.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetArgument.h"
56 #include "insets/InsetCollapsible.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetFloat.h"
60 #include "insets/InsetFloatList.h"
61 #include "insets/InsetGraphics.h"
62 #include "insets/InsetGraphicsParams.h"
63 #include "insets/InsetInfo.h"
64 #include "insets/InsetIPAMacro.h"
65 #include "insets/InsetNewline.h"
66 #include "insets/InsetQuotes.h"
67 #include "insets/InsetSpecialChar.h"
68 #include "insets/InsetText.h"
69 #include "insets/InsetWrap.h"
71 #include "support/convert.h"
72 #include "support/debug.h"
73 #include "support/gettext.h"
74 #include "support/lassert.h"
75 #include "support/lstrings.h"
76 #include "support/lyxalgo.h"
77 #include "support/lyxtime.h"
78 #include "support/os.h"
79 #include "support/regex.h"
81 #include "mathed/InsetMathHull.h"
82 #include "mathed/InsetMathMacroTemplate.h"
88 using namespace lyx::support;
92 using cap::copySelection;
93 using cap::copySelectionToTemp;
94 using cap::cutSelection;
95 using cap::cutSelectionToTemp;
96 using cap::pasteFromStack;
97 using cap::pasteFromTemp;
98 using cap::pasteClipboardText;
99 using cap::pasteClipboardGraphics;
100 using cap::replaceSelection;
101 using cap::grabAndEraseSelection;
102 using cap::selClearOrDel;
103 using cap::pasteSimpleText;
104 using frontend::Clipboard;
107 static Font freefont(ignore_font, ignore_language);
108 static bool toggleall = false;
110 static void toggleAndShow(Cursor & cur, Text * text,
111 Font const & font, bool togall = true)
113 text->toggleFree(cur, font, togall);
115 if (font.language() != ignore_language ||
116 font.fontInfo().number() != FONT_IGNORE) {
117 TextMetrics const & tm = cur.bv().textMetrics(text);
118 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
119 cur.real_current_font))
120 text->setCursor(cur, cur.pit(), cur.pos(),
121 false, !cur.boundary());
126 static void moveCursor(Cursor & cur, bool selecting)
128 if (selecting || cur.mark())
133 static void finishChange(Cursor & cur, bool selecting)
136 moveCursor(cur, selecting);
140 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
143 docstring sel = cur.selectionAsString(false);
145 // It may happen that sel is empty but there is a selection
146 replaceSelection(cur);
148 // Is this a valid formula?
152 #ifdef ENABLE_ASSERTIONS
153 const int old_pos = cur.pos();
155 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
156 #ifdef ENABLE_ASSERTIONS
157 LATTEST(old_pos == cur.pos());
159 cur.nextInset()->edit(cur, true);
160 if (cmd.action() != LFUN_MATH_MODE)
161 // LFUN_MATH_MODE has a different meaning in math mode
164 InsetMathHull * formula = new InsetMathHull(cur.buffer());
165 string const selstr = to_utf8(sel);
166 istringstream is(selstr);
169 if (!formula->readQuiet(lex)) {
170 // No valid formula, let's try with delims
171 is.str("$" + selstr + "$");
173 if (!formula->readQuiet(lex)) {
174 // Still not valid, leave it as is
182 cur.nextInset()->edit(cur, true);
183 LASSERT(cur.inMathed(), return);
187 cur.pos() = cur.lastpos();
188 if (cmd.action() != LFUN_MATH_MODE)
189 // LFUN_MATH_MODE has a different meaning in math mode
191 cur.clearSelection();
192 cur.pos() = cur.lastpos();
196 cur.message(from_utf8(N_("Math editor mode")));
198 cur.message(from_utf8(N_("No valid math formula")));
202 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
204 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
205 if (cur.inRegexped()) {
206 cur.message(_("Already in regular expression mode"));
210 docstring sel = cur.selectionAsString(false);
212 // It may happen that sel is empty but there is a selection
213 replaceSelection(cur);
215 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
216 cur.nextInset()->edit(cur, true);
219 cur.message(_("Regexp editor mode"));
223 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
226 cap::replaceSelection(cur);
227 cur.insert(new InsetSpecialChar(kind));
232 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
235 cap::replaceSelection(cur);
236 cur.insert(new InsetIPAChar(kind));
241 static bool doInsertInset(Cursor & cur, Text * text,
242 FuncRequest const & cmd, bool edit, bool pastesel)
244 Buffer & buffer = cur.bv().buffer();
245 BufferParams const & bparams = buffer.params();
246 Inset * inset = createInset(&buffer, cmd);
250 if (InsetCollapsible * ci = inset->asInsetCollapsible())
251 ci->setButtonLabel();
254 if (cmd.action() == LFUN_ARGUMENT_INSERT) {
255 bool cotextinsert = false;
256 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
257 Layout const & lay = cur.paragraph().layout();
258 Layout::LaTeXArgMap args = lay.args();
259 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
260 if (lait != args.end())
261 cotextinsert = (*lait).second.insertcotext;
263 InsetLayout const & il = cur.inset().getLayout();
265 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
266 if (ilait != args.end())
267 cotextinsert = (*ilait).second.insertcotext;
269 // The argument requests to insert a copy of the co-text to the inset
272 // If we have a selection within a paragraph, use this
273 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
274 ds = cur.selectionAsString(false);
275 // else use the whole paragraph
277 ds = cur.paragraph().asString();
278 text->insertInset(cur, inset);
280 inset->edit(cur, true);
281 // Now put co-text into inset
282 Font const f(inherit_font, cur.current_font.language());
284 cur.text()->insertStringAsLines(cur, ds, f);
285 cur.leaveInset(*inset);
292 if (cur.selection()) {
293 if (cmd.action() == LFUN_INDEX_INSERT)
294 copySelectionToTemp(cur);
296 cutSelectionToTemp(cur, pastesel);
297 cur.clearSelection();
299 } else if (cmd.action() == LFUN_INDEX_INSERT) {
300 gotsel = text->selectWordWhenUnderCursor(cur, WHOLE_WORD);
301 copySelectionToTemp(cur);
302 cur.clearSelection();
304 text->insertInset(cur, inset);
307 inset->edit(cur, true);
309 if (!gotsel || !pastesel)
312 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
313 cur.buffer()->errors("Paste");
314 cur.clearSelection(); // bug 393
316 InsetText * inset_text = inset->asInsetText();
318 inset_text->fixParagraphsFont();
319 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
320 // reset first par to default
321 cur.text()->paragraphs().begin()
322 ->setPlainOrDefaultLayout(bparams.documentClass());
325 // Merge multiple paragraphs -- hack
326 while (cur.lastpit() > 0)
327 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
328 if (cmd.action() == LFUN_FLEX_INSERT)
331 cur.leaveInset(*inset);
332 if (cmd.action() == LFUN_PREVIEW_INSERT
333 || cmd.action() == LFUN_IPA_INSERT)
335 notifyCursorLeavesOrEnters(old, cur);
338 cur.leaveInset(*inset);
339 // reset surrounding par to default
340 DocumentClass const & dc = bparams.documentClass();
341 docstring const layoutname = inset->usePlainLayout()
342 ? dc.plainLayoutName()
343 : dc.defaultLayoutName();
344 text->setLayout(cur, layoutname);
350 /// the type of outline operation
352 OutlineUp, // Move this header with text down
353 OutlineDown, // Move this header with text up
354 OutlineIn, // Make this header deeper
355 OutlineOut // Make this header shallower
359 static void insertSeparator(Cursor & cur, depth_type const depth)
361 Buffer & buf = *cur.buffer();
362 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
363 DocumentClass const & tc = buf.params().documentClass();
364 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
365 + from_ascii("\" ignoreautonests")));
366 // FIXME: Bibitem mess!
367 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
368 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
369 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
370 while (cur.paragraph().params().depth() > depth)
371 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
375 static void outline(OutlineOp mode, Cursor & cur, Text * text)
377 Buffer & buf = *cur.buffer();
378 pit_type & pit = cur.pit();
379 ParagraphList & pars = buf.text().paragraphs();
380 ParagraphList::iterator const bgn = pars.begin();
381 // The first paragraph of the area to be copied:
382 ParagraphList::iterator start = lyx::next(bgn, pit);
383 // The final paragraph of area to be copied:
384 ParagraphList::iterator finish = start;
385 ParagraphList::iterator const end = pars.end();
386 depth_type const current_depth = cur.paragraph().params().depth();
388 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
391 // Move out (down) from this section header
395 // Seek the one (on same level) below
396 for (; finish != end; ++finish) {
397 toclevel = buf.text().getTocLevel(distance(bgn, finish));
398 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
404 if (start == pars.begin())
407 ParagraphList::iterator dest = start;
408 // Move out (up) from this header
411 // Search previous same-level header above
414 toclevel = buf.text().getTocLevel(distance(bgn, dest));
416 && (toclevel == Layout::NOT_IN_TOC
417 || toclevel > thistoclevel));
418 // Not found; do nothing
419 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
421 pit_type newpit = distance(bgn, dest);
422 pit_type const len = distance(start, finish);
423 pit_type const deletepit = pit + len;
424 buf.undo().recordUndo(cur, newpit, deletepit - 1);
425 // If we move an environment upwards, make sure it is
426 // separated from its new neighbour below:
427 // If an environment of the same layout follows, and the moved
428 // paragraph sequence does not end with a separator, insert one.
429 ParagraphList::iterator lastmoved = finish;
431 if (start->layout().isEnvironment()
432 && dest->layout() == start->layout()
433 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
434 cur.pit() = distance(bgn, lastmoved);
435 cur.pos() = cur.lastpos();
436 insertSeparator(cur, current_depth);
439 // Likewise, if we moved an environment upwards, make sure it
440 // is separated from its new neighbour above.
441 // The paragraph before the target of movement
443 ParagraphList::iterator before = dest;
445 // Get the parent paragraph (outer in nested context)
446 pit_type const parent =
447 before->params().depth() > current_depth
448 ? text->depthHook(distance(bgn, before), current_depth)
449 : distance(bgn, before);
450 // If a environment with same layout preceeds the moved one in the new
451 // position, and there is no separator yet, insert one.
452 if (start->layout().isEnvironment()
453 && pars[parent].layout() == start->layout()
454 && !before->isEnvSeparator(before->beginOfBody())) {
455 cur.pit() = distance(bgn, before);
456 cur.pos() = cur.lastpos();
457 insertSeparator(cur, current_depth);
461 newpit = distance(bgn, dest);
462 pars.splice(dest, start, finish);
470 // Go one down from *this* header:
471 ParagraphList::iterator dest = next(finish, 1);
472 // Go further down to find header to insert in front of:
473 for (; dest != end; ++dest) {
474 toclevel = buf.text().getTocLevel(distance(bgn, dest));
475 if (toclevel != Layout::NOT_IN_TOC
476 && toclevel <= thistoclevel)
479 // One such was found, so go on...
480 // If we move an environment downwards, make sure it is
481 // separated from its new neighbour above.
482 pit_type newpit = distance(bgn, dest);
483 buf.undo().recordUndo(cur, pit, newpit - 1);
484 // The paragraph before the target of movement
485 ParagraphList::iterator before = dest;
487 // Get the parent paragraph (outer in nested context)
488 pit_type const parent =
489 before->params().depth() > current_depth
490 ? text->depthHook(distance(bgn, before), current_depth)
491 : distance(bgn, before);
492 // If a environment with same layout preceeds the moved one in the new
493 // position, and there is no separator yet, insert one.
494 if (start->layout().isEnvironment()
495 && pars[parent].layout() == start->layout()
496 && !before->isEnvSeparator(before->beginOfBody())) {
497 cur.pit() = distance(bgn, before);
498 cur.pos() = cur.lastpos();
499 insertSeparator(cur, current_depth);
502 // Likewise, make sure moved environments are separated
503 // from their new neighbour below:
504 // If an environment of the same layout follows, and the moved
505 // paragraph sequence does not end with a separator, insert one.
506 ParagraphList::iterator lastmoved = finish;
509 && start->layout().isEnvironment()
510 && dest->layout() == start->layout()
511 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
512 cur.pit() = distance(bgn, lastmoved);
513 cur.pos() = cur.lastpos();
514 insertSeparator(cur, current_depth);
517 newpit = distance(bgn, dest);
518 pit_type const len = distance(start, finish);
519 pars.splice(dest, start, finish);
520 cur.pit() = newpit - len;
525 pit_type const len = distance(start, finish);
526 buf.undo().recordUndo(cur, pit, pit + len - 1);
527 for (; start != finish; ++start) {
528 toclevel = buf.text().getTocLevel(distance(bgn, start));
529 if (toclevel == Layout::NOT_IN_TOC)
532 DocumentClass const & tc = buf.params().documentClass();
533 DocumentClass::const_iterator lit = tc.begin();
534 DocumentClass::const_iterator len = tc.end();
535 int const newtoclevel =
536 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
537 LabelType const oldlabeltype = start->layout().labeltype;
539 for (; lit != len; ++lit) {
540 if (lit->toclevel == newtoclevel &&
541 lit->labeltype == oldlabeltype) {
542 start->setLayout(*lit);
553 void Text::number(Cursor & cur)
555 FontInfo font = ignore_font;
556 font.setNumber(FONT_TOGGLE);
557 toggleAndShow(cur, this, Font(font, ignore_language));
561 bool Text::isRTL(Paragraph const & par) const
563 Buffer const & buffer = owner_->buffer();
564 return par.isRTL(buffer.params());
570 Language const * getLanguage(Cursor const & cur, string const & lang)
572 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
576 docstring resolveLayout(docstring layout, DocIterator const & dit)
578 Paragraph const & par = dit.paragraph();
579 docstring const old_layout = par.layout().name();
580 DocumentClass const & tclass = dit.buffer()->params().documentClass();
583 layout = tclass.defaultLayoutName();
585 if (dit.inset().forcePlainLayout(dit.idx()))
586 // in this case only the empty layout is allowed
587 layout = tclass.plainLayoutName();
588 else if (par.usePlainLayout()) {
589 // in this case, default layout maps to empty layout
590 if (layout == tclass.defaultLayoutName())
591 layout = tclass.plainLayoutName();
593 // otherwise, the empty layout maps to the default
594 if (layout == tclass.plainLayoutName())
595 layout = tclass.defaultLayoutName();
598 // If the entry is obsolete, use the new one instead.
599 if (tclass.hasLayout(layout)) {
600 docstring const & obs = tclass[layout].obsoleted_by();
604 if (!tclass.hasLayout(layout))
610 bool isAlreadyLayout(docstring const & layout, CursorData const & cur)
612 ParagraphList const & pars = cur.text()->paragraphs();
614 pit_type pit = cur.selBegin().pit();
615 pit_type const epit = cur.selEnd().pit() + 1;
616 for ( ; pit != epit; ++pit)
617 if (pars[pit].layout().name() != layout)
627 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
629 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
631 // Dispatch if the cursor is inside the text. It is not the
632 // case for context menus (bug 5797).
633 if (cur.text() != this) {
638 BufferView * bv = &cur.bv();
639 TextMetrics * tm = &bv->textMetrics(this);
640 if (!tm->contains(cur.pit())) {
641 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
642 tm = &bv->textMetrics(this);
645 // FIXME: We use the update flag to indicates wether a singlePar or a
646 // full screen update is needed. We reset it here but shall we restore it
648 cur.noScreenUpdate();
650 LBUFERR(this == cur.text());
652 // NOTE: This should NOT be a reference. See commit 94a5481a.
653 CursorSlice const oldTopSlice = cur.top();
654 bool const oldBoundary = cur.boundary();
655 bool const oldSelection = cur.selection();
656 // Signals that, even if needsUpdate == false, an update of the
657 // cursor paragraph is required
658 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
659 LyXAction::SingleParUpdate);
660 // Signals that a full-screen update is required
661 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
662 LyXAction::NoUpdate) || singleParUpdate);
663 bool const last_misspelled = lyxrc.spellcheck_continuously
664 && cur.paragraph().isMisspelled(cur.pos(), true);
666 FuncCode const act = cmd.action();
669 case LFUN_PARAGRAPH_MOVE_DOWN: {
670 pit_type const pit = cur.pit();
671 cur.recordUndo(pit, pit + 1);
673 pars_.swap(pit, pit + 1);
675 cur.forceBufferUpdate();
680 case LFUN_PARAGRAPH_MOVE_UP: {
681 pit_type const pit = cur.pit();
682 cur.recordUndo(pit - 1, pit);
684 pars_.swap(pit, pit - 1);
687 cur.forceBufferUpdate();
691 case LFUN_APPENDIX: {
692 Paragraph & par = cur.paragraph();
693 bool start = !par.params().startOfAppendix();
695 // FIXME: The code below only makes sense at top level.
696 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
697 // ensure that we have only one start_of_appendix in this document
698 // FIXME: this don't work for multipart document!
699 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
700 if (pars_[tmp].params().startOfAppendix()) {
701 cur.recordUndo(tmp, tmp);
702 pars_[tmp].params().startOfAppendix(false);
708 par.params().startOfAppendix(start);
710 // we can set the refreshing parameters now
711 cur.forceBufferUpdate();
715 case LFUN_WORD_DELETE_FORWARD:
717 cutSelection(cur, false);
719 deleteWordForward(cur, cmd.getArg(0) == "force");
720 finishChange(cur, false);
723 case LFUN_WORD_DELETE_BACKWARD:
725 cutSelection(cur, false);
727 deleteWordBackward(cur, cmd.getArg(0) == "force");
728 finishChange(cur, false);
731 case LFUN_LINE_DELETE_FORWARD:
733 cutSelection(cur, false);
735 tm->deleteLineForward(cur);
736 finishChange(cur, false);
739 case LFUN_BUFFER_BEGIN:
740 case LFUN_BUFFER_BEGIN_SELECT:
741 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
742 if (cur.depth() == 1)
743 needsUpdate |= cursorTop(cur);
746 cur.screenUpdateFlags(Update::FitCursor);
749 case LFUN_BUFFER_END:
750 case LFUN_BUFFER_END_SELECT:
751 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
752 if (cur.depth() == 1)
753 needsUpdate |= cursorBottom(cur);
756 cur.screenUpdateFlags(Update::FitCursor);
759 case LFUN_INSET_BEGIN:
760 case LFUN_INSET_BEGIN_SELECT:
761 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
762 if (cur.depth() == 1 || !cur.top().at_begin())
763 needsUpdate |= cursorTop(cur);
766 cur.screenUpdateFlags(Update::FitCursor);
770 case LFUN_INSET_END_SELECT:
771 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
772 if (cur.depth() == 1 || !cur.top().at_end())
773 needsUpdate |= cursorBottom(cur);
776 cur.screenUpdateFlags(Update::FitCursor);
779 case LFUN_CHAR_FORWARD:
780 case LFUN_CHAR_FORWARD_SELECT: {
781 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
782 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
783 bool const cur_moved = cursorForward(cur);
784 needsUpdate |= cur_moved;
786 if (!cur_moved && oldTopSlice == cur.top()
787 && cur.boundary() == oldBoundary) {
789 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
791 // we will probably be moving out the inset, so we should execute
792 // the depm-mechanism, but only when the cursor has a place to
793 // go outside this inset, i.e. in a slice above.
794 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
795 && cur.pit() == cur.lastpit()) {
796 // The cursor hasn't changed yet. To give the
797 // DEPM the possibility of doing something we must
798 // provide it with two different cursors.
800 dummy.pos() = dummy.pit() = 0;
801 if (cur.bv().checkDepm(dummy, cur))
802 cur.forceBufferUpdate();
808 case LFUN_CHAR_BACKWARD:
809 case LFUN_CHAR_BACKWARD_SELECT: {
810 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
811 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
812 bool const cur_moved = cursorBackward(cur);
813 needsUpdate |= cur_moved;
815 if (!cur_moved && oldTopSlice == cur.top()
816 && cur.boundary() == oldBoundary) {
818 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
820 // we will probably be moving out the inset, so we should execute
821 // the depm-mechanism, but only when the cursor has a place to
822 // go outside this inset, i.e. in a slice above.
823 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
824 // The cursor hasn't changed yet. To give the
825 // DEPM the possibility of doing something we must
826 // provide it with two different cursors.
828 dummy.pos() = cur.lastpos();
829 dummy.pit() = cur.lastpit();
830 if (cur.bv().checkDepm(dummy, cur))
831 cur.forceBufferUpdate();
838 case LFUN_CHAR_LEFT_SELECT:
839 if (lyxrc.visual_cursor) {
840 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
841 bool const cur_moved = cursorVisLeft(cur);
842 needsUpdate |= cur_moved;
843 if (!cur_moved && oldTopSlice == cur.top()
844 && cur.boundary() == oldBoundary) {
846 cmd = FuncRequest(LFUN_FINISHED_LEFT);
849 if (cur.reverseDirectionNeeded()) {
850 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
851 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
853 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
854 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
861 case LFUN_CHAR_RIGHT:
862 case LFUN_CHAR_RIGHT_SELECT:
863 if (lyxrc.visual_cursor) {
864 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
865 bool const cur_moved = cursorVisRight(cur);
866 needsUpdate |= cur_moved;
867 if (!cur_moved && oldTopSlice == cur.top()
868 && cur.boundary() == oldBoundary) {
870 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
873 if (cur.reverseDirectionNeeded()) {
874 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
875 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
877 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
878 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
887 case LFUN_DOWN_SELECT:
890 // stop/start the selection
891 bool select = cmd.action() == LFUN_DOWN_SELECT ||
892 cmd.action() == LFUN_UP_SELECT;
894 // move cursor up/down
895 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
896 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
898 if (!atFirstOrLastRow) {
899 needsUpdate |= cur.selHandle(select);
900 cur.upDownInText(up, needsUpdate);
901 needsUpdate |= cur.beforeDispatchCursor().inMathed();
903 pos_type newpos = up ? 0 : cur.lastpos();
904 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
905 needsUpdate |= cur.selHandle(select);
906 // we do not reset the targetx of the cursor
908 needsUpdate |= bv->checkDepm(cur, bv->cursor());
909 cur.updateTextTargetOffset();
911 cur.forceBufferUpdate();
915 // if the cursor cannot be moved up or down do not remove
916 // the selection right now, but wait for the next dispatch.
918 needsUpdate |= cur.selHandle(select);
919 cur.upDownInText(up, needsUpdate);
926 case LFUN_PARAGRAPH_UP:
927 case LFUN_PARAGRAPH_UP_SELECT:
928 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
929 needsUpdate |= cursorUpParagraph(cur);
932 case LFUN_PARAGRAPH_DOWN:
933 case LFUN_PARAGRAPH_DOWN_SELECT:
934 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
935 needsUpdate |= cursorDownParagraph(cur);
938 case LFUN_LINE_BEGIN:
939 case LFUN_LINE_BEGIN_SELECT:
940 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
941 needsUpdate |= tm->cursorHome(cur);
945 case LFUN_LINE_END_SELECT:
946 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
947 needsUpdate |= tm->cursorEnd(cur);
950 case LFUN_SECTION_SELECT: {
951 Buffer const & buf = *cur.buffer();
952 pit_type const pit = cur.pit();
953 ParagraphList & pars = buf.text().paragraphs();
954 ParagraphList::iterator bgn = pars.begin();
955 // The first paragraph of the area to be selected:
956 ParagraphList::iterator start = lyx::next(bgn, pit);
957 // The final paragraph of area to be selected:
958 ParagraphList::iterator finish = start;
959 ParagraphList::iterator end = pars.end();
961 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
962 if (thistoclevel == Layout::NOT_IN_TOC)
966 Cursor const old_cur = cur;
967 needsUpdate |= cur.selHandle(true);
969 // Move out (down) from this section header
973 // Seek the one (on same level) below
974 for (; finish != end; ++finish, ++cur.pit()) {
975 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
976 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
979 cur.pos() = cur.lastpos();
981 cur.setCurrentFont();
983 needsUpdate |= cur != old_cur;
987 case LFUN_WORD_RIGHT:
988 case LFUN_WORD_RIGHT_SELECT:
989 if (lyxrc.visual_cursor) {
990 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
991 bool const cur_moved = cursorVisRightOneWord(cur);
992 needsUpdate |= cur_moved;
993 if (!cur_moved && oldTopSlice == cur.top()
994 && cur.boundary() == oldBoundary) {
996 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
999 if (cur.reverseDirectionNeeded()) {
1000 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1001 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1003 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1004 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1011 case LFUN_WORD_FORWARD:
1012 case LFUN_WORD_FORWARD_SELECT: {
1013 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
1014 bool const cur_moved = cursorForwardOneWord(cur);
1015 needsUpdate |= cur_moved;
1017 if (!cur_moved && oldTopSlice == cur.top()
1018 && cur.boundary() == oldBoundary) {
1020 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1022 // we will probably be moving out the inset, so we should execute
1023 // the depm-mechanism, but only when the cursor has a place to
1024 // go outside this inset, i.e. in a slice above.
1025 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
1026 && cur.pit() == cur.lastpit()) {
1027 // The cursor hasn't changed yet. To give the
1028 // DEPM the possibility of doing something we must
1029 // provide it with two different cursors.
1031 dummy.pos() = dummy.pit() = 0;
1032 if (cur.bv().checkDepm(dummy, cur))
1033 cur.forceBufferUpdate();
1039 case LFUN_WORD_LEFT:
1040 case LFUN_WORD_LEFT_SELECT:
1041 if (lyxrc.visual_cursor) {
1042 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
1043 bool const cur_moved = cursorVisLeftOneWord(cur);
1044 needsUpdate |= cur_moved;
1045 if (!cur_moved && oldTopSlice == cur.top()
1046 && cur.boundary() == oldBoundary) {
1048 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1051 if (cur.reverseDirectionNeeded()) {
1052 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1053 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1055 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1056 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1063 case LFUN_WORD_BACKWARD:
1064 case LFUN_WORD_BACKWARD_SELECT: {
1065 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1066 bool const cur_moved = cursorBackwardOneWord(cur);
1067 needsUpdate |= cur_moved;
1069 if (!cur_moved && oldTopSlice == cur.top()
1070 && cur.boundary() == oldBoundary) {
1072 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1074 // we will probably be moving out the inset, so we should execute
1075 // the depm-mechanism, but only when the cursor has a place to
1076 // go outside this inset, i.e. in a slice above.
1077 if (cur.depth() > 1 && cur.pos() == 0
1078 && cur.pit() == 0) {
1079 // The cursor hasn't changed yet. To give the
1080 // DEPM the possibility of doing something we must
1081 // provide it with two different cursors.
1083 dummy.pos() = cur.lastpos();
1084 dummy.pit() = cur.lastpit();
1085 if (cur.bv().checkDepm(dummy, cur))
1086 cur.forceBufferUpdate();
1092 case LFUN_WORD_SELECT: {
1093 selectWord(cur, WHOLE_WORD);
1094 finishChange(cur, true);
1098 case LFUN_NEWLINE_INSERT: {
1099 InsetNewlineParams inp;
1100 docstring arg = cmd.argument();
1101 if (arg == "linebreak")
1102 inp.kind = InsetNewlineParams::LINEBREAK;
1104 inp.kind = InsetNewlineParams::NEWLINE;
1105 cap::replaceSelection(cur);
1107 cur.insert(new InsetNewline(inp));
1109 moveCursor(cur, false);
1113 case LFUN_TAB_INSERT: {
1114 bool const multi_par_selection = cur.selection() &&
1115 cur.selBegin().pit() != cur.selEnd().pit();
1116 if (multi_par_selection) {
1117 // If there is a multi-paragraph selection, a tab is inserted
1118 // at the beginning of each paragraph.
1119 cur.recordUndoSelection();
1120 pit_type const pit_end = cur.selEnd().pit();
1121 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1122 pars_[pit].insertChar(0, '\t',
1123 bv->buffer().params().track_changes);
1124 // Update the selection pos to make sure the selection does not
1125 // change as the inserted tab will increase the logical pos.
1126 if (cur.realAnchor().pit() == pit)
1127 cur.realAnchor().forwardPos();
1128 if (cur.pit() == pit)
1133 // Maybe we shouldn't allow tabs within a line, because they
1134 // are not (yet) aligned as one might do expect.
1135 FuncRequest ncmd(LFUN_SELF_INSERT, from_ascii("\t"));
1136 dispatch(cur, ncmd);
1141 case LFUN_TAB_DELETE: {
1142 bool const tc = bv->buffer().params().track_changes;
1143 if (cur.selection()) {
1144 // If there is a selection, a tab (if present) is removed from
1145 // the beginning of each paragraph.
1146 cur.recordUndoSelection();
1147 pit_type const pit_end = cur.selEnd().pit();
1148 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1149 Paragraph & par = paragraphs()[pit];
1152 char_type const c = par.getChar(0);
1153 if (c == '\t' || c == ' ') {
1154 // remove either 1 tab or 4 spaces.
1155 int const n = (c == ' ' ? 4 : 1);
1156 for (int i = 0; i < n
1157 && !par.empty() && par.getChar(0) == c; ++i) {
1158 if (cur.pit() == pit)
1160 if (cur.realAnchor().pit() == pit
1161 && cur.realAnchor().pos() > 0 )
1162 cur.realAnchor().backwardPos();
1163 par.eraseChar(0, tc);
1169 // If there is no selection, try to remove a tab or some spaces
1170 // before the position of the cursor.
1171 Paragraph & par = paragraphs()[cur.pit()];
1172 pos_type const pos = cur.pos();
1177 char_type const c = par.getChar(pos - 1);
1181 par.eraseChar(cur.pos(), tc);
1183 for (int n_spaces = 0;
1185 && par.getChar(cur.pos() - 1) == ' '
1189 par.eraseChar(cur.pos(), tc);
1196 case LFUN_CHAR_DELETE_FORWARD:
1197 if (!cur.selection()) {
1198 if (cur.pos() == cur.paragraph().size())
1199 // Par boundary, force full-screen update
1200 singleParUpdate = false;
1201 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1203 cur.selection(true);
1208 needsUpdate |= erase(cur);
1211 cutSelection(cur, false);
1212 singleParUpdate = false;
1214 moveCursor(cur, false);
1217 case LFUN_CHAR_DELETE_BACKWARD:
1218 if (!cur.selection()) {
1219 if (bv->getIntl().getTransManager().backspace()) {
1220 bool par_boundary = cur.pos() == 0;
1221 bool first_par = cur.pit() == 0;
1222 // Par boundary, full-screen update
1224 singleParUpdate = false;
1225 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1227 cur.selection(true);
1232 needsUpdate |= backspace(cur);
1234 if (par_boundary && !first_par && cur.pos() > 0
1235 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1236 needsUpdate |= backspace(cur);
1241 cutSelection(cur, false);
1242 singleParUpdate = false;
1246 case LFUN_PARAGRAPH_BREAK: {
1247 cap::replaceSelection(cur);
1248 pit_type pit = cur.pit();
1249 Paragraph const & par = pars_[pit];
1250 bool lastpar = (pit == pit_type(pars_.size() - 1));
1251 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1252 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1253 if (prev < pit && cur.pos() == par.beginOfBody()
1254 && !par.size() && !par.isEnvSeparator(cur.pos())
1255 && !par.layout().isCommand()
1256 && pars_[prev].layout() != par.layout()
1257 && pars_[prev].layout().isEnvironment()
1258 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1259 if (par.layout().isEnvironment()
1260 && pars_[prev].getDepth() == par.getDepth()) {
1261 docstring const layout = par.layout().name();
1262 DocumentClass const & tc = bv->buffer().params().documentClass();
1263 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1264 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1265 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1266 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1268 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1269 breakParagraph(cur);
1271 Font const f(inherit_font, cur.current_font.language());
1272 pars_[cur.pit() - 1].resetFonts(f);
1274 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1276 breakParagraph(cur, cmd.getArg(0) == "inverse");
1279 // If we have a list and autoinsert item insets,
1281 Layout::LaTeXArgMap args = par.layout().args();
1282 for (auto const & thearg : args) {
1283 Layout::latexarg arg = thearg.second;
1284 if (arg.autoinsert && prefixIs(thearg.first, "item:")) {
1285 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, thearg.first);
1286 lyx::dispatch(cmd2);
1292 case LFUN_INSET_INSERT: {
1295 // We have to avoid triggering InstantPreview loading
1296 // before inserting into the document. See bug #5626.
1297 bool loaded = bv->buffer().isFullyLoaded();
1298 bv->buffer().setFullyLoaded(false);
1299 Inset * inset = createInset(&bv->buffer(), cmd);
1300 bv->buffer().setFullyLoaded(loaded);
1303 // FIXME (Abdel 01/02/2006):
1304 // What follows would be a partial fix for bug 2154:
1305 // http://www.lyx.org/trac/ticket/2154
1306 // This automatically put the label inset _after_ a
1307 // numbered section. It should be possible to extend the mechanism
1308 // to any kind of LateX environement.
1309 // The correct way to fix that bug would be at LateX generation.
1310 // I'll let the code here for reference as it could be used for some
1311 // other feature like "automatic labelling".
1313 Paragraph & par = pars_[cur.pit()];
1314 if (inset->lyxCode() == LABEL_CODE
1315 && !par.layout().counter.empty()) {
1316 // Go to the end of the paragraph
1317 // Warning: Because of Change-Tracking, the last
1318 // position is 'size()' and not 'size()-1':
1319 cur.pos() = par.size();
1320 // Insert a new paragraph
1321 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1325 if (cur.selection())
1326 cutSelection(cur, false);
1328 cur.forceBufferUpdate();
1329 if (inset->editable() && inset->asInsetText())
1330 inset->edit(cur, true);
1334 // trigger InstantPreview now
1335 if (inset->lyxCode() == EXTERNAL_CODE) {
1336 InsetExternal & ins =
1337 static_cast<InsetExternal &>(*inset);
1338 ins.updatePreview();
1345 case LFUN_INSET_DISSOLVE: {
1346 if (dissolveInset(cur)) {
1348 cur.forceBufferUpdate();
1353 case LFUN_GRAPHICS_SET_GROUP: {
1354 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1360 string id = to_utf8(cmd.argument());
1361 string grp = graphics::getGroupParams(bv->buffer(), id);
1362 InsetGraphicsParams tmp, inspar = ins->getParams();
1365 inspar.groupId = to_utf8(cmd.argument());
1367 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1368 tmp.filename = inspar.filename;
1372 ins->setParams(inspar);
1376 case LFUN_SPACE_INSERT:
1377 if (cur.paragraph().layout().free_spacing)
1378 insertChar(cur, ' ');
1380 doInsertInset(cur, this, cmd, false, false);
1383 moveCursor(cur, false);
1386 case LFUN_SPECIALCHAR_INSERT: {
1387 string const name = to_utf8(cmd.argument());
1388 if (name == "hyphenation")
1389 specialChar(cur, InsetSpecialChar::HYPHENATION);
1390 else if (name == "allowbreak")
1391 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1392 else if (name == "ligature-break")
1393 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1394 else if (name == "slash")
1395 specialChar(cur, InsetSpecialChar::SLASH);
1396 else if (name == "nobreakdash")
1397 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1398 else if (name == "dots")
1399 specialChar(cur, InsetSpecialChar::LDOTS);
1400 else if (name == "end-of-sentence")
1401 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1402 else if (name == "menu-separator")
1403 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1404 else if (name == "lyx")
1405 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1406 else if (name == "tex")
1407 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1408 else if (name == "latex")
1409 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1410 else if (name == "latex2e")
1411 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1412 else if (name.empty())
1413 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1415 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1419 case LFUN_IPAMACRO_INSERT: {
1420 string const arg = cmd.getArg(0);
1421 if (arg == "deco") {
1422 // Open the inset, and move the current selection
1424 doInsertInset(cur, this, cmd, true, true);
1426 // Some insets are numbered, others are shown in the outline pane so
1427 // let's update the labels and the toc backend.
1428 cur.forceBufferUpdate();
1431 if (arg == "tone-falling")
1432 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1433 else if (arg == "tone-rising")
1434 ipaChar(cur, InsetIPAChar::TONE_RISING);
1435 else if (arg == "tone-high-rising")
1436 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1437 else if (arg == "tone-low-rising")
1438 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1439 else if (arg == "tone-high-rising-falling")
1440 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1441 else if (arg.empty())
1442 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1444 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1448 case LFUN_WORD_UPCASE:
1449 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1452 case LFUN_WORD_LOWCASE:
1453 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1456 case LFUN_WORD_CAPITALIZE:
1457 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1460 case LFUN_CHARS_TRANSPOSE:
1461 charsTranspose(cur);
1465 cur.message(_("Paste"));
1466 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1467 cap::replaceSelection(cur);
1469 // without argument?
1470 string const arg = to_utf8(cmd.argument());
1472 bool tryGraphics = true;
1473 if (theClipboard().isInternal())
1474 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1475 else if (theClipboard().hasTextContents()) {
1476 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1477 true, Clipboard::AnyTextType))
1478 tryGraphics = false;
1480 if (tryGraphics && theClipboard().hasGraphicsContents())
1481 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1482 } else if (isStrUnsignedInt(arg)) {
1483 // we have a numerical argument
1484 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1485 convert<unsigned int>(arg));
1486 } else if (arg == "html" || arg == "latex") {
1487 Clipboard::TextType type = (arg == "html") ?
1488 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1489 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1491 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1493 type = Clipboard::PdfGraphicsType;
1494 else if (arg == "png")
1495 type = Clipboard::PngGraphicsType;
1496 else if (arg == "jpeg")
1497 type = Clipboard::JpegGraphicsType;
1498 else if (arg == "linkback")
1499 type = Clipboard::LinkBackGraphicsType;
1500 else if (arg == "emf")
1501 type = Clipboard::EmfGraphicsType;
1502 else if (arg == "wmf")
1503 type = Clipboard::WmfGraphicsType;
1505 // we also check in getStatus()
1506 LYXERR0("Unrecognized graphics type: " << arg);
1508 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1511 bv->buffer().errors("Paste");
1512 cur.clearSelection(); // bug 393
1518 cutSelection(cur, true);
1519 cur.message(_("Cut"));
1524 cur.message(_("Copy"));
1527 case LFUN_SERVER_GET_XY:
1528 cur.message(from_utf8(
1529 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1530 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1533 case LFUN_SERVER_SET_XY: {
1536 istringstream is(to_utf8(cmd.argument()));
1539 lyxerr << "SETXY: Could not parse coordinates in '"
1540 << to_utf8(cmd.argument()) << endl;
1542 tm->setCursorFromCoordinates(cur, x, y);
1546 case LFUN_SERVER_GET_LAYOUT:
1547 cur.message(cur.paragraph().layout().name());
1551 case LFUN_LAYOUT_TOGGLE: {
1552 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1553 docstring req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1554 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(req_layout));
1556 docstring layout = resolveLayout(req_layout, cur);
1557 if (layout.empty()) {
1558 cur.errorMessage(from_utf8(N_("Layout ")) + req_layout +
1559 from_utf8(N_(" not known")));
1563 docstring const old_layout = cur.paragraph().layout().name();
1564 bool change_layout = !isAlreadyLayout(layout, cur);
1566 if (cmd.action() == LFUN_LAYOUT_TOGGLE && !change_layout) {
1567 change_layout = true;
1568 layout = resolveLayout(docstring(), cur);
1571 if (change_layout) {
1572 setLayout(cur, layout);
1573 if (cur.pit() > 0 && !ignoreautonests) {
1574 set<docstring> const & autonests =
1575 pars_[cur.pit() - 1].layout().autonests();
1576 set<docstring> const & autonested =
1577 pars_[cur.pit()].layout().isAutonestedBy();
1578 if (autonests.find(layout) != autonests.end()
1579 || autonested.find(old_layout) != autonested.end())
1580 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1584 DocumentClass const & tclass = bv->buffer().params().documentClass();
1585 for (auto const & la_pair : tclass[layout].args()) {
1586 Layout::latexarg const & arg = la_pair.second;
1587 if (arg.autoinsert) {
1588 FuncRequest const cmd2(LFUN_ARGUMENT_INSERT, la_pair.first);
1589 lyx::dispatch(cmd2);
1596 case LFUN_ENVIRONMENT_SPLIT: {
1597 bool const outer = cmd.argument() == "outer";
1598 bool const previous = cmd.argument() == "previous";
1599 bool const before = cmd.argument() == "before";
1600 bool const normal = cmd.argument().empty();
1601 Paragraph const & para = cur.paragraph();
1603 if (para.layout().isEnvironment())
1604 layout = para.layout().name();
1605 depth_type split_depth = cur.paragraph().params().depth();
1606 depth_type nextpar_depth = 0;
1607 if (outer || previous) {
1608 // check if we have an environment in our scope
1609 pit_type pit = cur.pit();
1610 Paragraph cpar = pars_[pit];
1616 if (layout.empty() && previous
1617 && cpar.layout().isEnvironment()
1618 && cpar.params().depth() <= split_depth)
1619 layout = cpar.layout().name();
1620 if (cpar.params().depth() < split_depth
1621 && cpar.layout().isEnvironment()) {
1623 layout = cpar.layout().name();
1624 split_depth = cpar.params().depth();
1626 if (cpar.params().depth() == 0)
1630 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1631 // save nesting of following paragraph
1632 Paragraph cpar = pars_[cur.pit() + 1];
1633 nextpar_depth = cpar.params().depth();
1636 cur.top().setPitPos(cur.pit(), 0);
1637 if (before || cur.pos() > 0)
1638 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1639 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1640 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1642 while (cur.paragraph().params().depth() > split_depth)
1643 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1645 DocumentClass const & tc = bv->buffer().params().documentClass();
1646 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1647 + from_ascii("\" ignoreautonests")));
1648 // FIXME: Bibitem mess!
1649 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
1650 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
1651 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1654 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1655 while (cur.paragraph().params().depth() < split_depth)
1656 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1659 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1660 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1661 if ((outer || normal) && nextpar_depth > 0) {
1662 // restore nesting of following paragraph
1663 DocIterator scur = cur;
1664 depth_type const max_depth = cur.paragraph().params().depth() + 1;
1666 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth)) {
1667 depth_type const olddepth = cur.paragraph().params().depth();
1668 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1669 if (olddepth == cur.paragraph().params().depth())
1670 // leave loop if no incrementation happens
1673 cur.setCursor(scur);
1679 case LFUN_CLIPBOARD_PASTE:
1680 cap::replaceSelection(cur);
1681 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1682 cmd.argument() == "paragraph");
1683 bv->buffer().errors("Paste");
1686 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1687 cap::replaceSelection(cur);
1688 pasteSimpleText(cur, cmd.argument() == "paragraph");
1691 case LFUN_PRIMARY_SELECTION_PASTE:
1692 cap::replaceSelection(cur);
1693 pasteString(cur, theSelection().get(),
1694 cmd.argument() == "paragraph");
1697 case LFUN_SELECTION_PASTE:
1698 // Copy the selection buffer to the clipboard stack,
1699 // because we want it to appear in the "Edit->Paste
1701 cap::replaceSelection(cur);
1702 cap::copySelectionToStack();
1703 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1704 bv->buffer().errors("Paste");
1707 case LFUN_QUOTE_INSERT: {
1708 cap::replaceSelection(cur);
1711 Paragraph const & par = cur.paragraph();
1712 pos_type pos = cur.pos();
1713 // Ignore deleted text before cursor
1714 while (pos > 0 && par.isDeleted(pos - 1))
1717 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1719 // Guess quote side.
1720 // A space triggers an opening quote. This is passed if the preceding
1721 // char/inset is a space or at paragraph start.
1723 if (pos > 0 && !par.isSpace(pos - 1)) {
1724 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1725 // If an opening double quotation mark precedes, and this
1726 // is a single quote, make it opening as well
1728 static_cast<InsetQuotes &>(*cur.prevInset());
1729 string const type = ins.getType();
1730 if (!suffixIs(type, "ld") || !inner)
1731 c = par.getChar(pos - 1);
1733 else if (!cur.prevInset()
1734 || (cur.prevInset() && cur.prevInset()->isChar()))
1735 // If a char precedes, pass that and let InsetQuote decide
1736 c = par.getChar(pos - 1);
1739 if (par.getInset(pos - 1)
1740 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1741 // skip "invisible" insets
1745 c = par.getChar(pos - 1);
1750 InsetQuotesParams::QuoteLevel const quote_level = inner
1751 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1752 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1753 cur.buffer()->updateBuffer();
1758 case LFUN_MOUSE_TRIPLE:
1759 if (cmd.button() == mouse_button::button1) {
1760 tm->cursorHome(cur);
1768 case LFUN_MOUSE_DOUBLE:
1769 if (cmd.button() == mouse_button::button1) {
1770 selectWord(cur, WHOLE_WORD);
1775 // Single-click on work area
1776 case LFUN_MOUSE_PRESS: {
1777 // We are not marking a selection with the keyboard in any case.
1778 Cursor & bvcur = cur.bv().cursor();
1779 bvcur.setMark(false);
1780 switch (cmd.button()) {
1781 case mouse_button::button1:
1782 if (!bvcur.selection())
1784 bvcur.resetAnchor();
1785 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1786 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1787 if (bvcur.wordSelection())
1788 selectWord(bvcur, WHOLE_WORD);
1791 case mouse_button::button2:
1792 if (lyxrc.mouse_middlebutton_paste) {
1793 // Middle mouse pasting.
1794 bv->mouseSetCursor(cur);
1796 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1797 "selection-paste ; primary-selection-paste paragraph"));
1799 cur.noScreenUpdate();
1802 case mouse_button::button3: {
1803 // Don't do anything if we right-click a
1804 // selection, a context menu will popup.
1805 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1806 && cur < bvcur.selectionEnd()) {
1807 cur.noScreenUpdate();
1810 if (!bv->mouseSetCursor(cur, false))
1811 cur.screenUpdateFlags(Update::FitCursor);
1817 } // switch (cmd.button())
1820 case LFUN_MOUSE_MOTION: {
1821 // Mouse motion with right or middle mouse do nothing for now.
1822 if (cmd.button() != mouse_button::button1) {
1823 cur.noScreenUpdate();
1826 // ignore motions deeper nested than the real anchor
1827 Cursor & bvcur = cur.bv().cursor();
1828 if (!bvcur.realAnchor().hasPart(cur)) {
1832 CursorSlice old = bvcur.top();
1834 int const wh = bv->workHeight();
1835 int const y = max(0, min(wh - 1, cmd.y()));
1837 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1838 cur.setTargetX(cmd.x());
1839 // Don't allow selecting a separator inset
1840 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1843 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1844 else if (cmd.y() < 0)
1845 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1846 // This is to allow jumping over large insets
1847 if (cur.top() == old) {
1849 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1850 else if (cmd.y() < 0)
1851 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1853 // We continue with our existing selection or start a new one, so don't
1854 // reset the anchor.
1855 bvcur.setCursor(cur);
1856 bvcur.selection(true);
1857 bvcur.setCurrentFont();
1858 if (cur.top() == old) {
1859 // We didn't move one iota, so no need to update the screen.
1860 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1861 //cur.noScreenUpdate();
1867 case LFUN_MOUSE_RELEASE:
1868 switch (cmd.button()) {
1869 case mouse_button::button1:
1870 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1871 // If there is a new selection, update persistent selection;
1872 // otherwise, single click does not clear persistent selection
1874 if (cur.selection()) {
1875 // Finish selection. If double click,
1876 // cur is moved to the end of word by
1877 // selectWord but bvcur is current
1879 cur.bv().cursor().setSelection();
1880 // We might have removed an empty but drawn selection
1881 // (probably a margin)
1882 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1884 cur.noScreenUpdate();
1885 // FIXME: We could try to handle drag and drop of selection here.
1888 case mouse_button::button2:
1889 // Middle mouse pasting is handled at mouse press time,
1890 // see LFUN_MOUSE_PRESS.
1891 cur.noScreenUpdate();
1894 case mouse_button::button3:
1895 // Cursor was set at LFUN_MOUSE_PRESS time.
1896 // FIXME: If there is a selection we could try to handle a special
1897 // drag & drop context menu.
1898 cur.noScreenUpdate();
1901 case mouse_button::none:
1902 case mouse_button::button4:
1903 case mouse_button::button5:
1905 } // switch (cmd.button())
1909 case LFUN_SELF_INSERT: {
1910 if (cmd.argument().empty())
1913 // Automatically delete the currently selected
1914 // text and replace it with what is being
1915 // typed in now. Depends on lyxrc settings
1916 // "auto_region_delete", which defaults to
1919 if (lyxrc.auto_region_delete && cur.selection())
1920 cutSelection(cur, false);
1921 cur.clearSelection();
1923 for (char_type c : cmd.argument())
1924 bv->translateAndInsert(c, this, cur);
1927 moveCursor(cur, false);
1928 cur.markNewWordPosition();
1929 bv->bookmarkEditPosition();
1933 case LFUN_HREF_INSERT: {
1934 docstring content = cmd.argument();
1935 if (content.empty() && cur.selection())
1936 content = cur.selectionAsString(false);
1938 InsetCommandParams p(HYPERLINK_CODE);
1939 if (!content.empty()){
1940 // if it looks like a link, we'll put it as target,
1941 // otherwise as name (bug #8792).
1944 // regex_match(to_utf8(content), matches, link_re)
1945 // because smatch stores pointers to the substrings rather
1946 // than making copies of them. And those pointers become
1947 // invalid after regex_match returns, since it is then
1948 // being given a temporary object. (Thanks to Georg for
1949 // figuring that out.)
1950 regex const link_re("^([a-z]+):.*");
1952 string const c = to_utf8(lowercase(content));
1954 if (c.substr(0,7) == "mailto:") {
1955 p["target"] = content;
1956 p["type"] = from_ascii("mailto:");
1957 } else if (regex_match(c, matches, link_re)) {
1958 p["target"] = content;
1959 string protocol = matches.str(1);
1960 if (protocol == "file")
1961 p["type"] = from_ascii("file:");
1963 p["name"] = content;
1965 string const data = InsetCommand::params2string(p);
1967 // we need to have a target. if we already have one, then
1968 // that gets used at the default for the name, too, which
1969 // is probably what is wanted.
1970 if (p["target"].empty()) {
1971 bv->showDialog("href", data);
1973 FuncRequest fr(LFUN_INSET_INSERT, data);
1979 case LFUN_LABEL_INSERT: {
1980 InsetCommandParams p(LABEL_CODE);
1981 // Try to generate a valid label
1982 p["name"] = (cmd.argument().empty()) ?
1983 cur.getPossibleLabel() :
1985 string const data = InsetCommand::params2string(p);
1987 if (cmd.argument().empty()) {
1988 bv->showDialog("label", data);
1990 FuncRequest fr(LFUN_INSET_INSERT, data);
1996 case LFUN_INFO_INSERT: {
1997 if (cmd.argument().empty()) {
1998 bv->showDialog("info", cur.current_font.language()->lang());
2001 inset = createInset(cur.buffer(), cmd);
2005 insertInset(cur, inset);
2006 cur.forceBufferUpdate();
2011 case LFUN_CAPTION_INSERT:
2012 case LFUN_FOOTNOTE_INSERT:
2013 case LFUN_NOTE_INSERT:
2014 case LFUN_BOX_INSERT:
2015 case LFUN_BRANCH_INSERT:
2016 case LFUN_PHANTOM_INSERT:
2017 case LFUN_ERT_INSERT:
2018 case LFUN_LISTING_INSERT:
2019 case LFUN_MARGINALNOTE_INSERT:
2020 case LFUN_ARGUMENT_INSERT:
2021 case LFUN_INDEX_INSERT:
2022 case LFUN_PREVIEW_INSERT:
2023 case LFUN_SCRIPT_INSERT:
2024 case LFUN_IPA_INSERT:
2025 // Open the inset, and move the current selection
2027 doInsertInset(cur, this, cmd, true, true);
2029 // Some insets are numbered, others are shown in the outline pane so
2030 // let's update the labels and the toc backend.
2031 cur.forceBufferUpdate();
2034 case LFUN_FLEX_INSERT: {
2035 // Open the inset, and move the current selection
2037 bool const sel = cur.selection();
2038 doInsertInset(cur, this, cmd, true, true);
2039 // Insert auto-insert arguments
2040 bool autoargs = false;
2041 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
2042 Layout::LaTeXArgMap::const_iterator lait = args.begin();
2043 Layout::LaTeXArgMap::const_iterator const laend = args.end();
2044 for (; lait != laend; ++lait) {
2045 Layout::latexarg arg = (*lait).second;
2046 if (arg.autoinsert) {
2047 // The cursor might have been invalidated by the replaceSelection.
2048 cur.buffer()->changed(true);
2049 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first);
2050 lyx::dispatch(cmd2);
2056 cur.leaveInset(cur.inset());
2059 // Some insets are numbered, others are shown in the outline pane so
2060 // let's update the labels and the toc backend.
2061 cur.forceBufferUpdate();
2065 case LFUN_TABULAR_INSERT:
2066 // if there were no arguments, just open the dialog
2067 if (doInsertInset(cur, this, cmd, false, true))
2070 bv->showDialog("tabularcreate");
2074 case LFUN_FLOAT_INSERT:
2075 case LFUN_FLOAT_WIDE_INSERT:
2076 case LFUN_WRAP_INSERT: {
2077 // will some content be moved into the inset?
2078 bool const content = cur.selection();
2079 // does the content consist of multiple paragraphs?
2080 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2082 doInsertInset(cur, this, cmd, true, true);
2085 // If some single-par content is moved into the inset,
2086 // doInsertInset puts the cursor outside the inset.
2087 // To insert the caption we put it back into the inset.
2088 // FIXME cleanup doInsertInset to avoid such dances!
2089 if (content && singlepar)
2092 ParagraphList & pars = cur.text()->paragraphs();
2094 DocumentClass const & tclass = bv->buffer().params().documentClass();
2096 // add a separate paragraph for the caption inset
2097 pars.push_back(Paragraph());
2098 pars.back().setInsetOwner(&cur.text()->inset());
2099 pars.back().setPlainOrDefaultLayout(tclass);
2100 int cap_pit = pars.size() - 1;
2102 // if an empty inset was created, we create an additional empty
2103 // paragraph at the bottom so that the user can choose where to put
2104 // the graphics (or table).
2106 pars.push_back(Paragraph());
2107 pars.back().setInsetOwner(&cur.text()->inset());
2108 pars.back().setPlainOrDefaultLayout(tclass);
2111 // reposition the cursor to the caption
2112 cur.pit() = cap_pit;
2114 // FIXME: This Text/Cursor dispatch handling is a mess!
2115 // We cannot use Cursor::dispatch here it needs access to up to
2117 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2118 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2119 cur.forceBufferUpdate();
2120 cur.screenUpdateFlags(Update::Force);
2121 // FIXME: When leaving the Float (or Wrap) inset we should
2122 // delete any empty paragraph left above or below the
2127 case LFUN_NOMENCL_INSERT: {
2128 InsetCommandParams p(NOMENCL_CODE);
2129 if (cmd.argument().empty()) {
2131 bv->cursor().innerText()->getStringForDialog(bv->cursor());
2132 cur.clearSelection();
2134 p["symbol"] = cmd.argument();
2135 string const data = InsetCommand::params2string(p);
2136 bv->showDialog("nomenclature", data);
2140 case LFUN_INDEX_PRINT: {
2141 InsetCommandParams p(INDEX_PRINT_CODE);
2142 if (cmd.argument().empty())
2143 p["type"] = from_ascii("idx");
2145 p["type"] = cmd.argument();
2146 string const data = InsetCommand::params2string(p);
2147 FuncRequest fr(LFUN_INSET_INSERT, data);
2152 case LFUN_NOMENCL_PRINT:
2153 case LFUN_NEWPAGE_INSERT:
2155 doInsertInset(cur, this, cmd, false, false);
2159 case LFUN_SEPARATOR_INSERT: {
2160 doInsertInset(cur, this, cmd, false, false);
2162 // remove a following space
2163 Paragraph & par = cur.paragraph();
2164 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2165 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2169 case LFUN_DEPTH_DECREMENT:
2170 changeDepth(cur, DEC_DEPTH);
2173 case LFUN_DEPTH_INCREMENT:
2174 changeDepth(cur, INC_DEPTH);
2177 case LFUN_REGEXP_MODE:
2178 regexpDispatch(cur, cmd);
2181 case LFUN_MATH_MODE: {
2182 if (cmd.argument() == "on" || cmd.argument() == "") {
2183 // don't pass "on" as argument
2184 // (it would appear literally in the first cell)
2185 docstring sel = cur.selectionAsString(false);
2186 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2187 // create a macro template if we see "\\newcommand" somewhere, and
2188 // an ordinary formula otherwise
2190 && (sel.find(from_ascii("\\newcommand")) != string::npos
2191 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2192 || sel.find(from_ascii("\\def")) != string::npos)
2193 && macro->fromString(sel)) {
2195 replaceSelection(cur);
2198 // no meaningful macro template was found
2200 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2203 // The argument is meaningful
2204 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2205 // has a different meaning in math mode
2206 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2210 case LFUN_MATH_MACRO:
2211 if (cmd.argument().empty())
2212 cur.errorMessage(from_utf8(N_("Missing argument")));
2215 string s = to_utf8(cmd.argument());
2216 string const s1 = token(s, ' ', 1);
2217 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2218 string const s2 = token(s, ' ', 2);
2219 MacroType type = MacroTypeNewcommand;
2221 type = MacroTypeDef;
2222 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2223 from_utf8(token(s, ' ', 0)), nargs, false, type);
2224 inset->setBuffer(bv->buffer());
2225 insertInset(cur, inset);
2227 // enter macro inset and select the name
2229 cur.top().pos() = cur.top().lastpos();
2231 cur.selection(true);
2232 cur.top().pos() = 0;
2236 case LFUN_MATH_DISPLAY:
2237 case LFUN_MATH_SUBSCRIPT:
2238 case LFUN_MATH_SUPERSCRIPT:
2239 case LFUN_MATH_INSERT:
2240 case LFUN_MATH_AMS_MATRIX:
2241 case LFUN_MATH_MATRIX:
2242 case LFUN_MATH_DELIM:
2243 case LFUN_MATH_BIGDELIM:
2244 mathDispatch(cur, cmd);
2247 case LFUN_FONT_EMPH: {
2248 Font font(ignore_font, ignore_language);
2249 font.fontInfo().setEmph(FONT_TOGGLE);
2250 toggleAndShow(cur, this, font);
2254 case LFUN_FONT_ITAL: {
2255 Font font(ignore_font, ignore_language);
2256 font.fontInfo().setShape(ITALIC_SHAPE);
2257 toggleAndShow(cur, this, font);
2261 case LFUN_FONT_BOLD:
2262 case LFUN_FONT_BOLDSYMBOL: {
2263 Font font(ignore_font, ignore_language);
2264 font.fontInfo().setSeries(BOLD_SERIES);
2265 toggleAndShow(cur, this, font);
2269 case LFUN_FONT_NOUN: {
2270 Font font(ignore_font, ignore_language);
2271 font.fontInfo().setNoun(FONT_TOGGLE);
2272 toggleAndShow(cur, this, font);
2276 case LFUN_FONT_TYPEWRITER: {
2277 Font font(ignore_font, ignore_language);
2278 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2279 toggleAndShow(cur, this, font);
2283 case LFUN_FONT_SANS: {
2284 Font font(ignore_font, ignore_language);
2285 font.fontInfo().setFamily(SANS_FAMILY);
2286 toggleAndShow(cur, this, font);
2290 case LFUN_FONT_ROMAN: {
2291 Font font(ignore_font, ignore_language);
2292 font.fontInfo().setFamily(ROMAN_FAMILY);
2293 toggleAndShow(cur, this, font);
2297 case LFUN_FONT_DEFAULT: {
2298 Font font(inherit_font, ignore_language);
2299 toggleAndShow(cur, this, font);
2303 case LFUN_FONT_STRIKEOUT: {
2304 Font font(ignore_font, ignore_language);
2305 font.fontInfo().setStrikeout(FONT_TOGGLE);
2306 toggleAndShow(cur, this, font);
2310 case LFUN_FONT_CROSSOUT: {
2311 Font font(ignore_font, ignore_language);
2312 font.fontInfo().setXout(FONT_TOGGLE);
2313 toggleAndShow(cur, this, font);
2317 case LFUN_FONT_UNDERUNDERLINE: {
2318 Font font(ignore_font, ignore_language);
2319 font.fontInfo().setUuline(FONT_TOGGLE);
2320 toggleAndShow(cur, this, font);
2324 case LFUN_FONT_UNDERWAVE: {
2325 Font font(ignore_font, ignore_language);
2326 font.fontInfo().setUwave(FONT_TOGGLE);
2327 toggleAndShow(cur, this, font);
2331 case LFUN_FONT_UNDERLINE: {
2332 Font font(ignore_font, ignore_language);
2333 font.fontInfo().setUnderbar(FONT_TOGGLE);
2334 toggleAndShow(cur, this, font);
2338 case LFUN_FONT_SIZE: {
2339 Font font(ignore_font, ignore_language);
2340 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2341 toggleAndShow(cur, this, font);
2345 case LFUN_LANGUAGE: {
2346 string const lang_arg = cmd.getArg(0);
2347 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2348 Language const * lang =
2349 reset ? reset_language
2350 : languages.getLanguage(lang_arg);
2351 // we allow reset_language, which is 0, but only if it
2352 // was requested via empty or "reset" arg.
2353 if (!lang && !reset)
2355 bool const toggle = (cmd.getArg(1) != "set");
2356 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2357 Font font(ignore_font, lang);
2358 toggleAndShow(cur, this, font, toggle);
2359 // We need a buffer update if we change the language
2361 if (cur.insetInSelection(INFO_CODE))
2362 cur.forceBufferUpdate();
2366 case LFUN_TEXTSTYLE_APPLY:
2367 toggleAndShow(cur, this, freefont, toggleall);
2368 cur.message(_("Character set"));
2371 // Set the freefont using the contents of \param data dispatched from
2372 // the frontends and apply it at the current cursor location.
2373 case LFUN_TEXTSTYLE_UPDATE: {
2376 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2379 toggleAndShow(cur, this, freefont, toggleall);
2380 // We need a buffer update if we change the language
2382 if (cur.insetInSelection(INFO_CODE))
2383 cur.forceBufferUpdate();
2384 cur.message(_("Character set"));
2386 lyxerr << "Argument not ok";
2391 case LFUN_FINISHED_LEFT:
2392 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2393 // We're leaving an inset, going left. If the inset is LTR, we're
2394 // leaving from the front, so we should not move (remain at --- but
2395 // not in --- the inset). If the inset is RTL, move left, without
2396 // entering the inset itself; i.e., move to after the inset.
2397 if (cur.paragraph().getFontSettings(
2398 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2399 cursorVisLeft(cur, true);
2402 case LFUN_FINISHED_RIGHT:
2403 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2404 // We're leaving an inset, going right. If the inset is RTL, we're
2405 // leaving from the front, so we should not move (remain at --- but
2406 // not in --- the inset). If the inset is LTR, move right, without
2407 // entering the inset itself; i.e., move to after the inset.
2408 if (!cur.paragraph().getFontSettings(
2409 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2410 cursorVisRight(cur, true);
2413 case LFUN_FINISHED_BACKWARD:
2414 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2415 cur.setCurrentFont();
2418 case LFUN_FINISHED_FORWARD:
2419 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2421 cur.setCurrentFont();
2424 case LFUN_LAYOUT_PARAGRAPH: {
2426 params2string(cur.paragraph(), data);
2427 data = "show\n" + data;
2428 bv->showDialog("paragraph", data);
2432 case LFUN_PARAGRAPH_UPDATE: {
2434 params2string(cur.paragraph(), data);
2436 // Will the paragraph accept changes from the dialog?
2438 cur.inset().allowParagraphCustomization(cur.idx());
2440 data = "update " + convert<string>(accept) + '\n' + data;
2441 bv->updateDialog("paragraph", data);
2445 case LFUN_ACCENT_UMLAUT:
2446 case LFUN_ACCENT_CIRCUMFLEX:
2447 case LFUN_ACCENT_GRAVE:
2448 case LFUN_ACCENT_ACUTE:
2449 case LFUN_ACCENT_TILDE:
2450 case LFUN_ACCENT_PERISPOMENI:
2451 case LFUN_ACCENT_CEDILLA:
2452 case LFUN_ACCENT_MACRON:
2453 case LFUN_ACCENT_DOT:
2454 case LFUN_ACCENT_UNDERDOT:
2455 case LFUN_ACCENT_UNDERBAR:
2456 case LFUN_ACCENT_CARON:
2457 case LFUN_ACCENT_BREVE:
2458 case LFUN_ACCENT_TIE:
2459 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2460 case LFUN_ACCENT_CIRCLE:
2461 case LFUN_ACCENT_OGONEK:
2462 theApp()->handleKeyFunc(cmd.action());
2463 if (!cmd.argument().empty())
2464 // FIXME: Are all these characters encoded in one byte in utf8?
2465 bv->translateAndInsert(cmd.argument()[0], this, cur);
2466 cur.screenUpdateFlags(Update::FitCursor);
2469 case LFUN_FLOAT_LIST_INSERT: {
2470 DocumentClass const & tclass = bv->buffer().params().documentClass();
2471 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2473 if (cur.selection())
2474 cutSelection(cur, false);
2475 breakParagraph(cur);
2477 if (cur.lastpos() != 0) {
2478 cursorBackward(cur);
2479 breakParagraph(cur);
2482 docstring const laystr = cur.inset().usePlainLayout() ?
2483 tclass.plainLayoutName() :
2484 tclass.defaultLayoutName();
2485 setLayout(cur, laystr);
2486 ParagraphParameters p;
2487 // FIXME If this call were replaced with one to clearParagraphParams(),
2488 // then we could get rid of this method altogether.
2489 setParagraphs(cur, p);
2490 // FIXME This should be simplified when InsetFloatList takes a
2491 // Buffer in its constructor.
2492 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2493 ifl->setBuffer(bv->buffer());
2494 insertInset(cur, ifl);
2497 lyxerr << "Non-existent float type: "
2498 << to_utf8(cmd.argument()) << endl;
2503 case LFUN_CHANGE_ACCEPT: {
2504 acceptOrRejectChanges(cur, ACCEPT);
2508 case LFUN_CHANGE_REJECT: {
2509 acceptOrRejectChanges(cur, REJECT);
2513 case LFUN_THESAURUS_ENTRY: {
2514 Language const * language = cur.getFont().language();
2515 docstring arg = cmd.argument();
2517 arg = cur.selectionAsString(false);
2519 if (arg.size() > 100 || arg.empty()) {
2520 // Get word or selection
2521 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2522 arg = cur.selectionAsString(false);
2523 arg += " lang=" + from_ascii(language->lang());
2526 string lang = cmd.getArg(1);
2527 // This duplicates the code in GuiThesaurus::initialiseParams
2528 if (prefixIs(lang, "lang=")) {
2529 language = languages.getLanguage(lang.substr(5));
2531 language = cur.getFont().language();
2534 string lang = language->code();
2535 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2536 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2537 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2538 _("The path to the thesaurus directory has not been specified.\n"
2539 "The thesaurus is not functional.\n"
2540 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2543 bv->showDialog("thesaurus", to_utf8(arg));
2547 case LFUN_SPELLING_ADD: {
2548 Language const * language = getLanguage(cur, cmd.getArg(1));
2549 docstring word = from_utf8(cmd.getArg(0));
2551 word = cur.selectionAsString(false);
2553 if (word.size() > 100 || word.empty()) {
2554 // Get word or selection
2555 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2556 word = cur.selectionAsString(false);
2559 WordLangTuple wl(word, language);
2560 theSpellChecker()->insert(wl);
2564 case LFUN_SPELLING_IGNORE: {
2565 Language const * language = getLanguage(cur, cmd.getArg(1));
2566 docstring word = from_utf8(cmd.getArg(0));
2568 word = cur.selectionAsString(false);
2570 if (word.size() > 100 || word.empty()) {
2571 // Get word or selection
2572 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2573 word = cur.selectionAsString(false);
2576 WordLangTuple wl(word, language);
2577 theSpellChecker()->accept(wl);
2581 case LFUN_SPELLING_REMOVE: {
2582 Language const * language = getLanguage(cur, cmd.getArg(1));
2583 docstring word = from_utf8(cmd.getArg(0));
2585 word = cur.selectionAsString(false);
2587 if (word.size() > 100 || word.empty()) {
2588 // Get word or selection
2589 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2590 word = cur.selectionAsString(false);
2593 WordLangTuple wl(word, language);
2594 theSpellChecker()->remove(wl);
2598 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2599 // Given data, an encoding of the ParagraphParameters
2600 // generated in the Paragraph dialog, this function sets
2601 // the current paragraph, or currently selected paragraphs,
2603 // NOTE: This function overrides all existing settings.
2604 setParagraphs(cur, cmd.argument());
2605 cur.message(_("Paragraph layout set"));
2609 case LFUN_PARAGRAPH_PARAMS: {
2610 // Given data, an encoding of the ParagraphParameters as we'd
2611 // find them in a LyX file, this function modifies the current paragraph,
2612 // or currently selected paragraphs.
2613 // NOTE: This function only modifies, and does not override, existing
2615 setParagraphs(cur, cmd.argument(), true);
2616 cur.message(_("Paragraph layout set"));
2621 if (cur.selection()) {
2622 cur.selection(false);
2625 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2626 // correct, but I'm not 100% sure -- dov, 071019
2627 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2631 case LFUN_OUTLINE_UP:
2632 outline(OutlineUp, cur, this);
2633 setCursor(cur, cur.pit(), 0);
2634 cur.forceBufferUpdate();
2638 case LFUN_OUTLINE_DOWN:
2639 outline(OutlineDown, cur, this);
2640 setCursor(cur, cur.pit(), 0);
2641 cur.forceBufferUpdate();
2645 case LFUN_OUTLINE_IN:
2646 outline(OutlineIn, cur, this);
2647 cur.forceBufferUpdate();
2651 case LFUN_OUTLINE_OUT:
2652 outline(OutlineOut, cur, this);
2653 cur.forceBufferUpdate();
2657 case LFUN_SERVER_GET_STATISTICS:
2659 DocIterator from, to;
2660 if (cur.selection()) {
2661 from = cur.selectionBegin();
2662 to = cur.selectionEnd();
2664 from = doc_iterator_begin(cur.buffer());
2665 to = doc_iterator_end(cur.buffer());
2668 cur.buffer()->updateStatistics(from, to);
2669 string const arg0 = cmd.getArg(0);
2670 if (arg0 == "words") {
2671 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2672 } else if (arg0 == "chars") {
2673 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2674 } else if (arg0 == "chars-space") {
2675 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2677 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2678 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2679 + convert<docstring>(cur.buffer()->charCount(true)));
2685 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2690 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2692 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2693 // Check for misspelled text
2694 // The redraw is useful because of the painting of
2695 // misspelled markers depends on the cursor position.
2696 // Trigger a redraw for cursor moves inside misspelled text.
2697 if (!cur.inTexted()) {
2698 // move from regular text to math
2699 needsUpdate = last_misspelled;
2700 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2701 // move inside regular text
2702 needsUpdate = last_misspelled
2703 || cur.paragraph().isMisspelled(cur.pos(), true);
2707 // FIXME: The cursor flag is reset two lines below
2708 // so we need to check here if some of the LFUN did touch that.
2709 // for now only Text::erase() and Text::backspace() do that.
2710 // The plan is to verify all the LFUNs and then to remove this
2711 // singleParUpdate boolean altogether.
2712 if (cur.result().screenUpdate() & Update::Force) {
2713 singleParUpdate = false;
2717 // FIXME: the following code should go in favor of fine grained
2718 // update flag treatment.
2719 if (singleParUpdate) {
2720 // Inserting characters does not change par height in general. So, try
2721 // to update _only_ this paragraph. BufferView will detect if a full
2722 // metrics update is needed anyway.
2723 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2727 && &oldTopSlice.inset() == &cur.inset()
2728 && oldTopSlice.idx() == cur.idx()
2729 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2730 && !cur.selection())
2731 // FIXME: it would be better if we could just do this
2733 //if (cur.result().update() != Update::FitCursor)
2734 // cur.noScreenUpdate();
2736 // But some LFUNs do not set Update::FitCursor when needed, so we
2737 // do it for all. This is not very harmfull as FitCursor will provoke
2738 // a full redraw only if needed but still, a proper review of all LFUN
2739 // should be done and this needsUpdate boolean can then be removed.
2740 cur.screenUpdateFlags(Update::FitCursor);
2742 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2746 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2747 FuncStatus & flag) const
2749 LBUFERR(this == cur.text());
2751 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2753 bool allow_in_passthru = false;
2754 InsetCode code = NO_CODE;
2756 switch (cmd.action()) {
2758 case LFUN_DEPTH_DECREMENT:
2759 enable = changeDepthAllowed(cur, DEC_DEPTH);
2762 case LFUN_DEPTH_INCREMENT:
2763 enable = changeDepthAllowed(cur, INC_DEPTH);
2767 // FIXME We really should not allow this to be put, e.g.,
2768 // in a footnote, or in ERT. But it would make sense in a
2769 // branch, so I'm not sure what to do.
2770 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2773 case LFUN_DIALOG_SHOW_NEW_INSET:
2774 if (cmd.argument() == "bibitem")
2775 code = BIBITEM_CODE;
2776 else if (cmd.argument() == "bibtex") {
2778 // not allowed in description items
2779 enable = !inDescriptionItem(cur);
2781 else if (cmd.argument() == "box")
2783 else if (cmd.argument() == "branch")
2785 else if (cmd.argument() == "citation")
2787 else if (cmd.argument() == "ert")
2789 else if (cmd.argument() == "external")
2790 code = EXTERNAL_CODE;
2791 else if (cmd.argument() == "float")
2793 else if (cmd.argument() == "graphics")
2794 code = GRAPHICS_CODE;
2795 else if (cmd.argument() == "href")
2796 code = HYPERLINK_CODE;
2797 else if (cmd.argument() == "include")
2798 code = INCLUDE_CODE;
2799 else if (cmd.argument() == "index")
2801 else if (cmd.argument() == "index_print")
2802 code = INDEX_PRINT_CODE;
2803 else if (cmd.argument() == "listings")
2804 code = LISTINGS_CODE;
2805 else if (cmd.argument() == "mathspace")
2806 code = MATH_HULL_CODE;
2807 else if (cmd.argument() == "nomenclature")
2808 code = NOMENCL_CODE;
2809 else if (cmd.argument() == "nomencl_print")
2810 code = NOMENCL_PRINT_CODE;
2811 else if (cmd.argument() == "label")
2813 else if (cmd.argument() == "line")
2815 else if (cmd.argument() == "note")
2817 else if (cmd.argument() == "phantom")
2818 code = PHANTOM_CODE;
2819 else if (cmd.argument() == "ref")
2821 else if (cmd.argument() == "space")
2823 else if (cmd.argument() == "toc")
2825 else if (cmd.argument() == "vspace")
2827 else if (cmd.argument() == "wrap")
2831 case LFUN_ERT_INSERT:
2834 case LFUN_LISTING_INSERT:
2835 code = LISTINGS_CODE;
2836 // not allowed in description items
2837 enable = !inDescriptionItem(cur);
2839 case LFUN_FOOTNOTE_INSERT:
2842 case LFUN_TABULAR_INSERT:
2843 code = TABULAR_CODE;
2845 case LFUN_MARGINALNOTE_INSERT:
2848 case LFUN_FLOAT_INSERT:
2849 case LFUN_FLOAT_WIDE_INSERT:
2850 // FIXME: If there is a selection, we should check whether there
2851 // are floats in the selection, but this has performance issues, see
2852 // LFUN_CHANGE_ACCEPT/REJECT.
2854 if (inDescriptionItem(cur))
2855 // not allowed in description items
2858 InsetCode const inset_code = cur.inset().lyxCode();
2860 // algorithm floats cannot be put in another float
2861 if (to_utf8(cmd.argument()) == "algorithm") {
2862 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2866 // for figures and tables: only allow in another
2867 // float or wrap if it is of the same type and
2868 // not a subfloat already
2869 if(cur.inset().lyxCode() == code) {
2870 InsetFloat const & ins =
2871 static_cast<InsetFloat const &>(cur.inset());
2872 enable = ins.params().type == to_utf8(cmd.argument())
2873 && !ins.params().subfloat;
2874 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2875 InsetWrap const & ins =
2876 static_cast<InsetWrap const &>(cur.inset());
2877 enable = ins.params().type == to_utf8(cmd.argument());
2881 case LFUN_WRAP_INSERT:
2883 // not allowed in description items
2884 enable = !inDescriptionItem(cur);
2886 case LFUN_FLOAT_LIST_INSERT: {
2887 code = FLOAT_LIST_CODE;
2888 // not allowed in description items
2889 enable = !inDescriptionItem(cur);
2891 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2892 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2893 // make sure we know about such floats
2894 if (cit == floats.end() ||
2895 // and that we know how to generate a list of them
2896 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2897 flag.setUnknown(true);
2898 // probably not necessary, but...
2904 case LFUN_CAPTION_INSERT: {
2905 code = CAPTION_CODE;
2906 string arg = cmd.getArg(0);
2907 bool varia = arg != "Unnumbered"
2908 && cur.inset().allowsCaptionVariation(arg);
2909 // not allowed in description items,
2910 // and in specific insets
2911 enable = !inDescriptionItem(cur)
2912 && (varia || arg.empty() || arg == "Standard");
2915 case LFUN_NOTE_INSERT:
2918 case LFUN_FLEX_INSERT: {
2920 string s = cmd.getArg(0);
2922 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2923 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2924 il.lyxtype() != InsetLayout::CUSTOM &&
2925 il.lyxtype() != InsetLayout::ELEMENT &&
2926 il.lyxtype ()!= InsetLayout::STANDARD)
2930 case LFUN_BOX_INSERT:
2933 case LFUN_BRANCH_INSERT:
2935 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2936 && cur.buffer()->params().branchlist().empty())
2939 case LFUN_IPA_INSERT:
2942 case LFUN_PHANTOM_INSERT:
2943 code = PHANTOM_CODE;
2945 case LFUN_LABEL_INSERT:
2948 case LFUN_INFO_INSERT:
2950 enable = cmd.argument().empty()
2951 || infoparams.validateArgument(cur.buffer(), cmd.argument(), true);
2953 case LFUN_ARGUMENT_INSERT: {
2955 allow_in_passthru = true;
2956 string const arg = cmd.getArg(0);
2961 Layout const & lay = cur.paragraph().layout();
2962 Layout::LaTeXArgMap args = lay.args();
2963 Layout::LaTeXArgMap::const_iterator const lait =
2965 if (lait != args.end()) {
2967 pit_type pit = cur.pit();
2968 pit_type lastpit = cur.pit();
2969 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2970 // In a sequence of "merged" environment layouts, we only allow
2971 // non-item arguments once.
2972 lastpit = cur.lastpit();
2973 // get the first paragraph in sequence with this layout
2974 depth_type const current_depth = cur.paragraph().params().depth();
2978 Paragraph cpar = pars_[pit - 1];
2979 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2985 for (; pit <= lastpit; ++pit) {
2986 if (pars_[pit].layout() != lay)
2988 for (auto const & table : pars_[pit].insetList())
2989 if (InsetArgument const * ins = table.inset->asInsetArgument())
2990 if (ins->name() == arg) {
2991 // we have this already
3000 case LFUN_INDEX_INSERT:
3003 case LFUN_INDEX_PRINT:
3004 code = INDEX_PRINT_CODE;
3005 // not allowed in description items
3006 enable = !inDescriptionItem(cur);
3008 case LFUN_NOMENCL_INSERT:
3009 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3013 code = NOMENCL_CODE;
3015 case LFUN_NOMENCL_PRINT:
3016 code = NOMENCL_PRINT_CODE;
3017 // not allowed in description items
3018 enable = !inDescriptionItem(cur);
3020 case LFUN_HREF_INSERT:
3021 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3025 code = HYPERLINK_CODE;
3027 case LFUN_IPAMACRO_INSERT: {
3028 string const arg = cmd.getArg(0);
3030 code = IPADECO_CODE;
3032 code = IPACHAR_CODE;
3035 case LFUN_QUOTE_INSERT:
3036 // always allow this, since we will inset a raw quote
3037 // if an inset is not allowed.
3038 allow_in_passthru = true;
3040 case LFUN_SPECIALCHAR_INSERT:
3041 code = SPECIALCHAR_CODE;
3043 case LFUN_SPACE_INSERT:
3044 // slight hack: we know this is allowed in math mode
3048 case LFUN_PREVIEW_INSERT:
3049 code = PREVIEW_CODE;
3051 case LFUN_SCRIPT_INSERT:
3055 case LFUN_MATH_INSERT:
3056 case LFUN_MATH_AMS_MATRIX:
3057 case LFUN_MATH_MATRIX:
3058 case LFUN_MATH_DELIM:
3059 case LFUN_MATH_BIGDELIM:
3060 case LFUN_MATH_DISPLAY:
3061 case LFUN_MATH_MODE:
3062 case LFUN_MATH_MACRO:
3063 case LFUN_MATH_SUBSCRIPT:
3064 case LFUN_MATH_SUPERSCRIPT:
3065 code = MATH_HULL_CODE;
3068 case LFUN_REGEXP_MODE:
3069 code = MATH_HULL_CODE;
3070 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3073 case LFUN_INSET_MODIFY:
3074 // We need to disable this, because we may get called for a
3076 // InsetTabular::getStatus() -> InsetText::getStatus()
3077 // and we don't handle LFUN_INSET_MODIFY.
3081 case LFUN_FONT_EMPH:
3082 flag.setOnOff(fontinfo.emph() == FONT_ON);
3083 enable = !cur.paragraph().isPassThru();
3086 case LFUN_FONT_ITAL:
3087 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3088 enable = !cur.paragraph().isPassThru();
3091 case LFUN_FONT_NOUN:
3092 flag.setOnOff(fontinfo.noun() == FONT_ON);
3093 enable = !cur.paragraph().isPassThru();
3096 case LFUN_FONT_BOLD:
3097 case LFUN_FONT_BOLDSYMBOL:
3098 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3099 enable = !cur.paragraph().isPassThru();
3102 case LFUN_FONT_SANS:
3103 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3104 enable = !cur.paragraph().isPassThru();
3107 case LFUN_FONT_ROMAN:
3108 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3109 enable = !cur.paragraph().isPassThru();
3112 case LFUN_FONT_TYPEWRITER:
3113 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3114 enable = !cur.paragraph().isPassThru();
3119 enable = cur.selection();
3123 if (cmd.argument().empty()) {
3124 if (theClipboard().isInternal())
3125 enable = cap::numberOfSelections() > 0;
3127 enable = !theClipboard().empty();
3131 // we have an argument
3132 string const arg = to_utf8(cmd.argument());
3133 if (isStrUnsignedInt(arg)) {
3134 // it's a number and therefore means the internal stack
3135 unsigned int n = convert<unsigned int>(arg);
3136 enable = cap::numberOfSelections() > n;
3140 // explicit text type?
3141 if (arg == "html") {
3142 // Do not enable for PlainTextType, since some tidying in the
3143 // frontend is needed for HTML, which is too unsafe for plain text.
3144 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3146 } else if (arg == "latex") {
3147 // LaTeX is usually not available on the clipboard with
3148 // the correct MIME type, but in plain text.
3149 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3150 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3154 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3156 type = Clipboard::PdfGraphicsType;
3157 else if (arg == "png")
3158 type = Clipboard::PngGraphicsType;
3159 else if (arg == "jpeg")
3160 type = Clipboard::JpegGraphicsType;
3161 else if (arg == "linkback")
3162 type = Clipboard::LinkBackGraphicsType;
3163 else if (arg == "emf")
3164 type = Clipboard::EmfGraphicsType;
3165 else if (arg == "wmf")
3166 type = Clipboard::WmfGraphicsType;
3169 LYXERR0("Unrecognized graphics type: " << arg);
3170 // we don't want to assert if the user just mistyped the LFUN
3171 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3175 enable = theClipboard().hasGraphicsContents(type);
3179 case LFUN_CLIPBOARD_PASTE:
3180 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3181 enable = !theClipboard().empty();
3184 case LFUN_PRIMARY_SELECTION_PASTE:
3185 enable = cur.selection() || !theSelection().empty();
3188 case LFUN_SELECTION_PASTE:
3189 enable = cap::selection();
3192 case LFUN_PARAGRAPH_MOVE_UP:
3193 enable = cur.pit() > 0 && !cur.selection();
3196 case LFUN_PARAGRAPH_MOVE_DOWN:
3197 enable = cur.pit() < cur.lastpit() && !cur.selection();
3200 case LFUN_CHANGE_ACCEPT:
3201 case LFUN_CHANGE_REJECT:
3202 if (!cur.selection())
3203 enable = cur.paragraph().isChanged(cur.pos());
3205 // will enable if there is a change in the selection
3208 // cheap improvement for efficiency: using cached
3209 // buffer variable, if there is no change in the
3210 // document, no need to check further.
3211 if (!cur.buffer()->areChangesPresent())
3214 for (DocIterator it = cur.selectionBegin(); ; it.forwardPar()) {
3215 pos_type const beg = it.pos();
3217 bool const in_last_par = (it.pit() == cur.selectionEnd().pit());
3219 end = cur.selectionEnd().pos();
3222 if (beg != end && it.paragraph().isChanged(beg, end)) {
3232 case LFUN_OUTLINE_UP:
3233 case LFUN_OUTLINE_DOWN:
3234 case LFUN_OUTLINE_IN:
3235 case LFUN_OUTLINE_OUT:
3236 // FIXME: LyX is not ready for outlining within inset.
3237 enable = isMainText()
3238 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3241 case LFUN_NEWLINE_INSERT:
3242 // LaTeX restrictions (labels or empty par)
3243 enable = !cur.paragraph().isPassThru()
3244 && cur.pos() > cur.paragraph().beginOfBody();
3247 case LFUN_SEPARATOR_INSERT:
3248 // Always enabled for now
3252 case LFUN_TAB_INSERT:
3253 case LFUN_TAB_DELETE:
3254 enable = cur.paragraph().isPassThru();
3257 case LFUN_GRAPHICS_SET_GROUP: {
3258 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3262 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3266 case LFUN_NEWPAGE_INSERT:
3267 // not allowed in description items
3268 code = NEWPAGE_CODE;
3269 enable = !inDescriptionItem(cur);
3273 enable = !cur.paragraph().isPassThru();
3274 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3277 case LFUN_PARAGRAPH_BREAK:
3278 enable = inset().allowMultiPar();
3281 case LFUN_SPELLING_ADD:
3282 case LFUN_SPELLING_IGNORE:
3283 case LFUN_SPELLING_REMOVE:
3284 enable = theSpellChecker() != NULL;
3285 if (enable && !cmd.getArg(1).empty()) {
3286 // validate explicitly given language
3287 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3288 enable &= lang != NULL;
3293 case LFUN_LAYOUT_TOGGLE: {
3294 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3295 docstring const req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3296 docstring const layout = resolveLayout(req_layout, cur);
3298 enable = !owner_->forcePlainLayout() && !layout.empty();
3299 flag.setOnOff(isAlreadyLayout(layout, cur));
3303 case LFUN_ENVIRONMENT_SPLIT: {
3304 if (cmd.argument() == "outer") {
3305 // check if we have an environment in our nesting hierarchy
3307 depth_type const current_depth = cur.paragraph().params().depth();
3308 pit_type pit = cur.pit();
3309 Paragraph cpar = pars_[pit];
3311 if (pit == 0 || cpar.params().depth() == 0)
3315 if (cpar.params().depth() < current_depth)
3316 res = cpar.layout().isEnvironment();
3321 else if (cmd.argument() == "previous") {
3322 // look if we have an environment in the previous par
3323 pit_type pit = cur.pit();
3324 Paragraph cpar = pars_[pit];
3328 enable = cpar.layout().isEnvironment();
3334 else if (cur.paragraph().layout().isEnvironment()) {
3335 enable = cmd.argument() == "before"
3336 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3343 case LFUN_LAYOUT_PARAGRAPH:
3344 case LFUN_PARAGRAPH_PARAMS:
3345 case LFUN_PARAGRAPH_PARAMS_APPLY:
3346 case LFUN_PARAGRAPH_UPDATE:
3347 enable = owner_->allowParagraphCustomization();
3350 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3351 // Because they insert COMBINING DIACRITICAL Unicode characters,
3352 // that cannot be handled by LaTeX but must be converted according
3353 // to the definition in lib/unicodesymbols?
3354 case LFUN_ACCENT_ACUTE:
3355 case LFUN_ACCENT_BREVE:
3356 case LFUN_ACCENT_CARON:
3357 case LFUN_ACCENT_CEDILLA:
3358 case LFUN_ACCENT_CIRCLE:
3359 case LFUN_ACCENT_CIRCUMFLEX:
3360 case LFUN_ACCENT_DOT:
3361 case LFUN_ACCENT_GRAVE:
3362 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3363 case LFUN_ACCENT_MACRON:
3364 case LFUN_ACCENT_OGONEK:
3365 case LFUN_ACCENT_TIE:
3366 case LFUN_ACCENT_TILDE:
3367 case LFUN_ACCENT_PERISPOMENI:
3368 case LFUN_ACCENT_UMLAUT:
3369 case LFUN_ACCENT_UNDERBAR:
3370 case LFUN_ACCENT_UNDERDOT:
3371 case LFUN_FONT_DEFAULT:
3372 case LFUN_FONT_FRAK:
3373 case LFUN_FONT_SIZE:
3374 case LFUN_FONT_STATE:
3375 case LFUN_FONT_UNDERLINE:
3376 case LFUN_FONT_STRIKEOUT:
3377 case LFUN_FONT_CROSSOUT:
3378 case LFUN_FONT_UNDERUNDERLINE:
3379 case LFUN_FONT_UNDERWAVE:
3380 case LFUN_TEXTSTYLE_APPLY:
3381 case LFUN_TEXTSTYLE_UPDATE:
3382 enable = !cur.paragraph().isPassThru();
3385 case LFUN_WORD_DELETE_FORWARD:
3386 case LFUN_WORD_DELETE_BACKWARD:
3387 case LFUN_LINE_DELETE_FORWARD:
3388 case LFUN_WORD_FORWARD:
3389 case LFUN_WORD_BACKWARD:
3390 case LFUN_WORD_RIGHT:
3391 case LFUN_WORD_LEFT:
3392 case LFUN_CHAR_FORWARD:
3393 case LFUN_CHAR_FORWARD_SELECT:
3394 case LFUN_CHAR_BACKWARD:
3395 case LFUN_CHAR_BACKWARD_SELECT:
3396 case LFUN_CHAR_LEFT:
3397 case LFUN_CHAR_LEFT_SELECT:
3398 case LFUN_CHAR_RIGHT:
3399 case LFUN_CHAR_RIGHT_SELECT:
3401 case LFUN_UP_SELECT:
3403 case LFUN_DOWN_SELECT:
3404 case LFUN_PARAGRAPH_UP_SELECT:
3405 case LFUN_PARAGRAPH_DOWN_SELECT:
3406 case LFUN_LINE_BEGIN_SELECT:
3407 case LFUN_LINE_END_SELECT:
3408 case LFUN_WORD_FORWARD_SELECT:
3409 case LFUN_WORD_BACKWARD_SELECT:
3410 case LFUN_WORD_RIGHT_SELECT:
3411 case LFUN_WORD_LEFT_SELECT:
3412 case LFUN_WORD_SELECT:
3413 case LFUN_SECTION_SELECT:
3414 case LFUN_BUFFER_BEGIN:
3415 case LFUN_BUFFER_END:
3416 case LFUN_BUFFER_BEGIN_SELECT:
3417 case LFUN_BUFFER_END_SELECT:
3418 case LFUN_INSET_BEGIN:
3419 case LFUN_INSET_END:
3420 case LFUN_INSET_BEGIN_SELECT:
3421 case LFUN_INSET_END_SELECT:
3422 case LFUN_PARAGRAPH_UP:
3423 case LFUN_PARAGRAPH_DOWN:
3424 case LFUN_LINE_BEGIN:
3426 case LFUN_CHAR_DELETE_FORWARD:
3427 case LFUN_CHAR_DELETE_BACKWARD:
3428 case LFUN_WORD_UPCASE:
3429 case LFUN_WORD_LOWCASE:
3430 case LFUN_WORD_CAPITALIZE:
3431 case LFUN_CHARS_TRANSPOSE:
3432 case LFUN_SERVER_GET_XY:
3433 case LFUN_SERVER_SET_XY:
3434 case LFUN_SERVER_GET_LAYOUT:
3435 case LFUN_SELF_INSERT:
3436 case LFUN_UNICODE_INSERT:
3437 case LFUN_THESAURUS_ENTRY:
3439 case LFUN_SERVER_GET_STATISTICS:
3440 // these are handled in our dispatch()
3444 case LFUN_INSET_INSERT: {
3445 string const type = cmd.getArg(0);
3446 if (type == "toc") {
3448 // not allowed in description items
3449 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3450 enable = !inDescriptionItem(cur);
3463 || !cur.inset().insetAllowed(code)
3464 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3467 flag.setEnabled(enable);
3472 void Text::pasteString(Cursor & cur, docstring const & clip,
3475 if (!clip.empty()) {
3478 insertStringAsParagraphs(cur, clip, cur.current_font);
3480 insertStringAsLines(cur, clip, cur.current_font);
3485 // FIXME: an item inset would make things much easier.
3486 bool Text::inDescriptionItem(Cursor & cur) const
3488 Paragraph & par = cur.paragraph();
3489 pos_type const pos = cur.pos();
3490 pos_type const body_pos = par.beginOfBody();
3492 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3493 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3494 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3497 return (pos < body_pos
3499 && (pos == 0 || par.getChar(pos - 1) != ' ')));