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/limited_stack.h"
76 #include "support/lstrings.h"
77 #include "support/lyxalgo.h"
78 #include "support/lyxtime.h"
79 #include "support/os.h"
80 #include "support/regex.h"
82 #include "mathed/InsetMathHull.h"
83 #include "mathed/InsetMathMacroTemplate.h"
90 using namespace lyx::support;
94 using cap::copySelection;
95 using cap::copySelectionToTemp;
96 using cap::cutSelection;
97 using cap::cutSelectionToTemp;
98 using cap::pasteFromStack;
99 using cap::pasteFromTemp;
100 using cap::pasteClipboardText;
101 using cap::pasteClipboardGraphics;
102 using cap::replaceSelection;
103 using cap::grabAndEraseSelection;
104 using cap::selClearOrDel;
105 using cap::pasteSimpleText;
106 using frontend::Clipboard;
109 typedef limited_stack<pair<docstring, Font>> FontStack;
110 static FontStack freeFonts(15);
111 static bool toggleall = false;
113 static void toggleAndShow(Cursor & cur, Text * text,
114 Font const & font, bool togall = true)
116 text->toggleFree(cur, font, togall);
118 if (font.language() != ignore_language ||
119 font.fontInfo().number() != FONT_IGNORE) {
120 TextMetrics const & tm = cur.bv().textMetrics(text);
121 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
122 cur.real_current_font))
123 text->setCursor(cur, cur.pit(), cur.pos(),
124 false, !cur.boundary());
129 static void moveCursor(Cursor & cur, bool selecting)
131 if (selecting || cur.mark())
136 static void finishChange(Cursor & cur, bool selecting)
139 moveCursor(cur, selecting);
143 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
146 docstring sel = cur.selectionAsString(false);
148 // It may happen that sel is empty but there is a selection
149 replaceSelection(cur);
151 // Is this a valid formula?
155 #ifdef ENABLE_ASSERTIONS
156 const int old_pos = cur.pos();
158 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
159 #ifdef ENABLE_ASSERTIONS
160 LATTEST(old_pos == cur.pos());
162 cur.nextInset()->edit(cur, true);
163 if (cmd.action() != LFUN_MATH_MODE)
164 // LFUN_MATH_MODE has a different meaning in math mode
167 InsetMathHull * formula = new InsetMathHull(cur.buffer());
168 string const selstr = to_utf8(sel);
169 istringstream is(selstr);
172 if (!formula->readQuiet(lex)) {
173 // No valid formula, let's try with delims
174 is.str("$" + selstr + "$");
176 if (!formula->readQuiet(lex)) {
177 // Still not valid, leave it as is
185 cur.nextInset()->edit(cur, true);
186 LASSERT(cur.inMathed(), return);
190 cur.pos() = cur.lastpos();
191 if (cmd.action() != LFUN_MATH_MODE)
192 // LFUN_MATH_MODE has a different meaning in math mode
194 cur.clearSelection();
195 cur.pos() = cur.lastpos();
199 cur.message(from_utf8(N_("Math editor mode")));
201 cur.message(from_utf8(N_("No valid math formula")));
205 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
207 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
208 if (cur.inRegexped()) {
209 cur.message(_("Already in regular expression mode"));
213 docstring sel = cur.selectionAsString(false);
215 // It may happen that sel is empty but there is a selection
216 replaceSelection(cur);
218 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
219 cur.nextInset()->edit(cur, true);
222 cur.message(_("Regexp editor mode"));
226 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
229 cap::replaceSelection(cur);
230 cur.insert(new InsetSpecialChar(kind));
235 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
238 cap::replaceSelection(cur);
239 cur.insert(new InsetIPAChar(kind));
244 static bool doInsertInset(Cursor & cur, Text * text,
245 FuncRequest const & cmd, bool edit, bool pastesel)
247 Buffer & buffer = cur.bv().buffer();
248 BufferParams const & bparams = buffer.params();
249 Inset * inset = createInset(&buffer, cmd);
253 if (InsetCollapsible * ci = inset->asInsetCollapsible())
254 ci->setButtonLabel();
257 if (cmd.action() == LFUN_ARGUMENT_INSERT) {
258 bool cotextinsert = false;
259 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
260 Layout const & lay = cur.paragraph().layout();
261 Layout::LaTeXArgMap args = lay.args();
262 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
263 if (lait != args.end())
264 cotextinsert = (*lait).second.insertcotext;
266 InsetLayout const & il = cur.inset().getLayout();
268 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
269 if (ilait != args.end())
270 cotextinsert = (*ilait).second.insertcotext;
272 // The argument requests to insert a copy of the co-text to the inset
275 // If we have a selection within a paragraph, use this
276 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
277 ds = cur.selectionAsString(false);
278 // else use the whole paragraph
280 ds = cur.paragraph().asString();
281 text->insertInset(cur, inset);
283 inset->edit(cur, true);
284 // Now put co-text into inset
285 Font const f(inherit_font, cur.current_font.language());
287 cur.text()->insertStringAsLines(cur, ds, f);
288 cur.leaveInset(*inset);
295 if (cur.selection()) {
296 if (cmd.action() == LFUN_INDEX_INSERT)
297 copySelectionToTemp(cur);
299 cutSelectionToTemp(cur, pastesel);
300 cur.clearSelection();
302 } else if (cmd.action() == LFUN_INDEX_INSERT) {
303 gotsel = text->selectWordWhenUnderCursor(cur, WHOLE_WORD);
304 copySelectionToTemp(cur);
305 cur.clearSelection();
307 text->insertInset(cur, inset);
310 inset->edit(cur, true);
312 if (!gotsel || !pastesel)
315 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
316 cur.buffer()->errors("Paste");
317 cur.clearSelection(); // bug 393
319 InsetText * inset_text = inset->asInsetText();
321 inset_text->fixParagraphsFont();
322 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
323 // reset first par to default
324 cur.text()->paragraphs().begin()
325 ->setPlainOrDefaultLayout(bparams.documentClass());
328 // Merge multiple paragraphs -- hack
329 while (cur.lastpit() > 0)
330 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
331 if (cmd.action() == LFUN_FLEX_INSERT)
334 cur.leaveInset(*inset);
335 if (cmd.action() == LFUN_PREVIEW_INSERT
336 || cmd.action() == LFUN_IPA_INSERT)
338 notifyCursorLeavesOrEnters(old, cur);
341 cur.leaveInset(*inset);
342 // reset surrounding par to default
343 DocumentClass const & dc = bparams.documentClass();
344 docstring const layoutname = inset->usePlainLayout()
345 ? dc.plainLayoutName()
346 : dc.defaultLayoutName();
347 text->setLayout(cur, layoutname);
353 /// the type of outline operation
355 OutlineUp, // Move this header with text down
356 OutlineDown, // Move this header with text up
357 OutlineIn, // Make this header deeper
358 OutlineOut // Make this header shallower
362 static void insertSeparator(Cursor & cur, depth_type const depth)
364 Buffer & buf = *cur.buffer();
365 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
366 DocumentClass const & tc = buf.params().documentClass();
367 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
368 + from_ascii("\" ignoreautonests")));
369 // FIXME: Bibitem mess!
370 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
371 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
372 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
373 while (cur.paragraph().params().depth() > depth)
374 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
378 static void outline(OutlineOp mode, Cursor & cur, Text * text)
380 Buffer & buf = *cur.buffer();
381 pit_type & pit = cur.pit();
382 ParagraphList & pars = buf.text().paragraphs();
383 ParagraphList::iterator const bgn = pars.begin();
384 // The first paragraph of the area to be copied:
385 ParagraphList::iterator start = lyx::next(bgn, pit);
386 // The final paragraph of area to be copied:
387 ParagraphList::iterator finish = start;
388 ParagraphList::iterator const end = pars.end();
389 depth_type const current_depth = cur.paragraph().params().depth();
391 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
394 // Move out (down) from this section header
398 // Seek the one (on same level) below
399 for (; finish != end; ++finish) {
400 toclevel = buf.text().getTocLevel(distance(bgn, finish));
401 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
407 if (start == pars.begin())
410 ParagraphList::iterator dest = start;
411 // Move out (up) from this header
414 // Search previous same-level header above
417 toclevel = buf.text().getTocLevel(distance(bgn, dest));
419 && (toclevel == Layout::NOT_IN_TOC
420 || toclevel > thistoclevel));
421 // Not found; do nothing
422 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
424 pit_type newpit = distance(bgn, dest);
425 pit_type const len = distance(start, finish);
426 pit_type const deletepit = pit + len;
427 buf.undo().recordUndo(cur, newpit, deletepit - 1);
428 // If we move an environment upwards, make sure it is
429 // separated from its new neighbour below:
430 // If an environment of the same layout follows, and the moved
431 // paragraph sequence does not end with a separator, insert one.
432 ParagraphList::iterator lastmoved = finish;
434 if (start->layout().isEnvironment()
435 && dest->layout() == start->layout()
436 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
437 cur.pit() = distance(bgn, lastmoved);
438 cur.pos() = cur.lastpos();
439 insertSeparator(cur, current_depth);
442 // Likewise, if we moved an environment upwards, make sure it
443 // is separated from its new neighbour above.
444 // The paragraph before the target of movement
446 ParagraphList::iterator before = dest;
448 // Get the parent paragraph (outer in nested context)
449 pit_type const parent =
450 before->params().depth() > current_depth
451 ? text->depthHook(distance(bgn, before), current_depth)
452 : distance(bgn, before);
453 // If a environment with same layout preceeds the moved one in the new
454 // position, and there is no separator yet, insert one.
455 if (start->layout().isEnvironment()
456 && pars[parent].layout() == start->layout()
457 && !before->isEnvSeparator(before->beginOfBody())) {
458 cur.pit() = distance(bgn, before);
459 cur.pos() = cur.lastpos();
460 insertSeparator(cur, current_depth);
464 newpit = distance(bgn, dest);
465 pars.splice(dest, start, finish);
473 // Go one down from *this* header:
474 ParagraphList::iterator dest = next(finish, 1);
475 // Go further down to find header to insert in front of:
476 for (; dest != end; ++dest) {
477 toclevel = buf.text().getTocLevel(distance(bgn, dest));
478 if (toclevel != Layout::NOT_IN_TOC
479 && toclevel <= thistoclevel)
482 // One such was found, so go on...
483 // If we move an environment downwards, make sure it is
484 // separated from its new neighbour above.
485 pit_type newpit = distance(bgn, dest);
486 buf.undo().recordUndo(cur, pit, newpit - 1);
487 // The paragraph before the target of movement
488 ParagraphList::iterator before = dest;
490 // Get the parent paragraph (outer in nested context)
491 pit_type const parent =
492 before->params().depth() > current_depth
493 ? text->depthHook(distance(bgn, before), current_depth)
494 : distance(bgn, before);
495 // If a environment with same layout preceeds the moved one in the new
496 // position, and there is no separator yet, insert one.
497 if (start->layout().isEnvironment()
498 && pars[parent].layout() == start->layout()
499 && !before->isEnvSeparator(before->beginOfBody())) {
500 cur.pit() = distance(bgn, before);
501 cur.pos() = cur.lastpos();
502 insertSeparator(cur, current_depth);
505 // Likewise, make sure moved environments are separated
506 // from their new neighbour below:
507 // If an environment of the same layout follows, and the moved
508 // paragraph sequence does not end with a separator, insert one.
509 ParagraphList::iterator lastmoved = finish;
512 && start->layout().isEnvironment()
513 && dest->layout() == start->layout()
514 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
515 cur.pit() = distance(bgn, lastmoved);
516 cur.pos() = cur.lastpos();
517 insertSeparator(cur, current_depth);
520 newpit = distance(bgn, dest);
521 pit_type const len = distance(start, finish);
522 pars.splice(dest, start, finish);
523 cur.pit() = newpit - len;
528 pit_type const len = distance(start, finish);
529 buf.undo().recordUndo(cur, pit, pit + len - 1);
530 for (; start != finish; ++start) {
531 toclevel = buf.text().getTocLevel(distance(bgn, start));
532 if (toclevel == Layout::NOT_IN_TOC)
535 DocumentClass const & tc = buf.params().documentClass();
536 DocumentClass::const_iterator lit = tc.begin();
537 DocumentClass::const_iterator len = tc.end();
538 int const newtoclevel =
539 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
540 LabelType const oldlabeltype = start->layout().labeltype;
542 for (; lit != len; ++lit) {
543 if (lit->toclevel == newtoclevel &&
544 lit->labeltype == oldlabeltype) {
545 start->setLayout(*lit);
556 void Text::number(Cursor & cur)
558 FontInfo font = ignore_font;
559 font.setNumber(FONT_TOGGLE);
560 toggleAndShow(cur, this, Font(font, ignore_language));
564 bool Text::isRTL(Paragraph const & par) const
566 Buffer const & buffer = owner_->buffer();
567 return par.isRTL(buffer.params());
573 Language const * getLanguage(Cursor const & cur, string const & lang)
575 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
579 docstring resolveLayout(docstring layout, DocIterator const & dit)
581 Paragraph const & par = dit.paragraph();
582 docstring const old_layout = par.layout().name();
583 DocumentClass const & tclass = dit.buffer()->params().documentClass();
586 layout = tclass.defaultLayoutName();
588 if (dit.inset().forcePlainLayout(dit.idx()))
589 // in this case only the empty layout is allowed
590 layout = tclass.plainLayoutName();
591 else if (par.usePlainLayout()) {
592 // in this case, default layout maps to empty layout
593 if (layout == tclass.defaultLayoutName())
594 layout = tclass.plainLayoutName();
596 // otherwise, the empty layout maps to the default
597 if (layout == tclass.plainLayoutName())
598 layout = tclass.defaultLayoutName();
601 // If the entry is obsolete, use the new one instead.
602 if (tclass.hasLayout(layout)) {
603 docstring const & obs = tclass[layout].obsoleted_by();
607 if (!tclass.hasLayout(layout))
613 bool isAlreadyLayout(docstring const & layout, CursorData const & cur)
615 ParagraphList const & pars = cur.text()->paragraphs();
617 pit_type pit = cur.selBegin().pit();
618 pit_type const epit = cur.selEnd().pit() + 1;
619 for ( ; pit != epit; ++pit)
620 if (pars[pit].layout().name() != layout)
630 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
632 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
634 // Dispatch if the cursor is inside the text. It is not the
635 // case for context menus (bug 5797).
636 if (cur.text() != this) {
641 BufferView * bv = &cur.bv();
642 TextMetrics * tm = &bv->textMetrics(this);
643 if (!tm->contains(cur.pit())) {
644 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
645 tm = &bv->textMetrics(this);
648 // FIXME: We use the update flag to indicates wether a singlePar or a
649 // full screen update is needed. We reset it here but shall we restore it
651 cur.noScreenUpdate();
653 LBUFERR(this == cur.text());
655 // NOTE: This should NOT be a reference. See commit 94a5481a.
656 CursorSlice const oldTopSlice = cur.top();
657 bool const oldBoundary = cur.boundary();
658 bool const oldSelection = cur.selection();
659 // Signals that, even if needsUpdate == false, an update of the
660 // cursor paragraph is required
661 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
662 LyXAction::SingleParUpdate);
663 // Signals that a full-screen update is required
664 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
665 LyXAction::NoUpdate) || singleParUpdate);
666 bool const last_misspelled = lyxrc.spellcheck_continuously
667 && cur.paragraph().isMisspelled(cur.pos(), true);
669 FuncCode const act = cmd.action();
672 case LFUN_PARAGRAPH_MOVE_DOWN: {
673 pit_type const pit = cur.pit();
674 cur.recordUndo(pit, pit + 1);
676 pars_.swap(pit, pit + 1);
678 cur.forceBufferUpdate();
683 case LFUN_PARAGRAPH_MOVE_UP: {
684 pit_type const pit = cur.pit();
685 cur.recordUndo(pit - 1, pit);
687 pars_.swap(pit, pit - 1);
690 cur.forceBufferUpdate();
694 case LFUN_APPENDIX: {
695 Paragraph & par = cur.paragraph();
696 bool start = !par.params().startOfAppendix();
698 // FIXME: The code below only makes sense at top level.
699 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
700 // ensure that we have only one start_of_appendix in this document
701 // FIXME: this don't work for multipart document!
702 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
703 if (pars_[tmp].params().startOfAppendix()) {
704 cur.recordUndo(tmp, tmp);
705 pars_[tmp].params().startOfAppendix(false);
711 par.params().startOfAppendix(start);
713 // we can set the refreshing parameters now
714 cur.forceBufferUpdate();
718 case LFUN_WORD_DELETE_FORWARD:
720 cutSelection(cur, false);
722 deleteWordForward(cur, cmd.getArg(0) == "force");
723 finishChange(cur, false);
726 case LFUN_WORD_DELETE_BACKWARD:
728 cutSelection(cur, false);
730 deleteWordBackward(cur, cmd.getArg(0) == "force");
731 finishChange(cur, false);
734 case LFUN_LINE_DELETE_FORWARD:
736 cutSelection(cur, false);
738 tm->deleteLineForward(cur);
739 finishChange(cur, false);
742 case LFUN_BUFFER_BEGIN:
743 case LFUN_BUFFER_BEGIN_SELECT:
744 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
745 if (cur.depth() == 1)
746 needsUpdate |= cursorTop(cur);
749 cur.screenUpdateFlags(Update::FitCursor);
752 case LFUN_BUFFER_END:
753 case LFUN_BUFFER_END_SELECT:
754 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
755 if (cur.depth() == 1)
756 needsUpdate |= cursorBottom(cur);
759 cur.screenUpdateFlags(Update::FitCursor);
762 case LFUN_INSET_BEGIN:
763 case LFUN_INSET_BEGIN_SELECT:
764 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
765 if (cur.depth() == 1 || !cur.top().at_begin())
766 needsUpdate |= cursorTop(cur);
769 cur.screenUpdateFlags(Update::FitCursor);
773 case LFUN_INSET_END_SELECT:
774 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
775 if (cur.depth() == 1 || !cur.top().at_end())
776 needsUpdate |= cursorBottom(cur);
779 cur.screenUpdateFlags(Update::FitCursor);
782 case LFUN_CHAR_FORWARD:
783 case LFUN_CHAR_FORWARD_SELECT: {
784 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
785 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
786 bool const cur_moved = cursorForward(cur);
787 needsUpdate |= cur_moved;
789 if (!cur_moved && oldTopSlice == cur.top()
790 && cur.boundary() == oldBoundary) {
792 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
794 // we will probably be moving out the inset, so we should execute
795 // the depm-mechanism, but only when the cursor has a place to
796 // go outside this inset, i.e. in a slice above.
797 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
798 && cur.pit() == cur.lastpit()) {
799 // The cursor hasn't changed yet. To give the
800 // DEPM the possibility of doing something we must
801 // provide it with two different cursors.
803 dummy.pos() = dummy.pit() = 0;
804 if (cur.bv().checkDepm(dummy, cur))
805 cur.forceBufferUpdate();
811 case LFUN_CHAR_BACKWARD:
812 case LFUN_CHAR_BACKWARD_SELECT: {
813 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
814 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
815 bool const cur_moved = cursorBackward(cur);
816 needsUpdate |= cur_moved;
818 if (!cur_moved && oldTopSlice == cur.top()
819 && cur.boundary() == oldBoundary) {
821 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
823 // we will probably be moving out the inset, so we should execute
824 // the depm-mechanism, but only when the cursor has a place to
825 // go outside this inset, i.e. in a slice above.
826 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
827 // The cursor hasn't changed yet. To give the
828 // DEPM the possibility of doing something we must
829 // provide it with two different cursors.
831 dummy.pos() = cur.lastpos();
832 dummy.pit() = cur.lastpit();
833 if (cur.bv().checkDepm(dummy, cur))
834 cur.forceBufferUpdate();
841 case LFUN_CHAR_LEFT_SELECT:
842 if (lyxrc.visual_cursor) {
843 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
844 bool const cur_moved = cursorVisLeft(cur);
845 needsUpdate |= cur_moved;
846 if (!cur_moved && oldTopSlice == cur.top()
847 && cur.boundary() == oldBoundary) {
849 cmd = FuncRequest(LFUN_FINISHED_LEFT);
852 if (cur.reverseDirectionNeeded()) {
853 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
854 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
856 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
857 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
864 case LFUN_CHAR_RIGHT:
865 case LFUN_CHAR_RIGHT_SELECT:
866 if (lyxrc.visual_cursor) {
867 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
868 bool const cur_moved = cursorVisRight(cur);
869 needsUpdate |= cur_moved;
870 if (!cur_moved && oldTopSlice == cur.top()
871 && cur.boundary() == oldBoundary) {
873 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
876 if (cur.reverseDirectionNeeded()) {
877 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
878 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
880 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
881 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
890 case LFUN_DOWN_SELECT:
893 // stop/start the selection
894 bool select = cmd.action() == LFUN_DOWN_SELECT ||
895 cmd.action() == LFUN_UP_SELECT;
897 // move cursor up/down
898 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
899 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
901 if (!atFirstOrLastRow) {
902 needsUpdate |= cur.selHandle(select);
903 cur.upDownInText(up, needsUpdate);
904 needsUpdate |= cur.beforeDispatchCursor().inMathed();
906 pos_type newpos = up ? 0 : cur.lastpos();
907 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
908 needsUpdate |= cur.selHandle(select);
909 // we do not reset the targetx of the cursor
911 needsUpdate |= bv->checkDepm(cur, bv->cursor());
912 cur.updateTextTargetOffset();
914 cur.forceBufferUpdate();
918 // if the cursor cannot be moved up or down do not remove
919 // the selection right now, but wait for the next dispatch.
921 needsUpdate |= cur.selHandle(select);
922 cur.upDownInText(up, needsUpdate);
929 case LFUN_PARAGRAPH_UP:
930 case LFUN_PARAGRAPH_UP_SELECT:
931 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
932 needsUpdate |= cursorUpParagraph(cur);
935 case LFUN_PARAGRAPH_DOWN:
936 case LFUN_PARAGRAPH_DOWN_SELECT:
937 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
938 needsUpdate |= cursorDownParagraph(cur);
941 case LFUN_LINE_BEGIN:
942 case LFUN_LINE_BEGIN_SELECT:
943 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
944 needsUpdate |= tm->cursorHome(cur);
948 case LFUN_LINE_END_SELECT:
949 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
950 needsUpdate |= tm->cursorEnd(cur);
953 case LFUN_SECTION_SELECT: {
954 Buffer const & buf = *cur.buffer();
955 pit_type const pit = cur.pit();
956 ParagraphList & pars = buf.text().paragraphs();
957 ParagraphList::iterator bgn = pars.begin();
958 // The first paragraph of the area to be selected:
959 ParagraphList::iterator start = lyx::next(bgn, pit);
960 // The final paragraph of area to be selected:
961 ParagraphList::iterator finish = start;
962 ParagraphList::iterator end = pars.end();
964 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
965 if (thistoclevel == Layout::NOT_IN_TOC)
969 Cursor const old_cur = cur;
970 needsUpdate |= cur.selHandle(true);
972 // Move out (down) from this section header
976 // Seek the one (on same level) below
977 for (; finish != end; ++finish, ++cur.pit()) {
978 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
979 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
982 cur.pos() = cur.lastpos();
984 cur.setCurrentFont();
986 needsUpdate |= cur != old_cur;
990 case LFUN_WORD_RIGHT:
991 case LFUN_WORD_RIGHT_SELECT:
992 if (lyxrc.visual_cursor) {
993 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
994 bool const cur_moved = cursorVisRightOneWord(cur);
995 needsUpdate |= cur_moved;
996 if (!cur_moved && oldTopSlice == cur.top()
997 && cur.boundary() == oldBoundary) {
999 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1002 if (cur.reverseDirectionNeeded()) {
1003 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1004 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1006 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1007 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1014 case LFUN_WORD_FORWARD:
1015 case LFUN_WORD_FORWARD_SELECT: {
1016 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
1017 bool const cur_moved = cursorForwardOneWord(cur);
1018 needsUpdate |= cur_moved;
1020 if (!cur_moved && oldTopSlice == cur.top()
1021 && cur.boundary() == oldBoundary) {
1023 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1025 // we will probably be moving out the inset, so we should execute
1026 // the depm-mechanism, but only when the cursor has a place to
1027 // go outside this inset, i.e. in a slice above.
1028 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
1029 && cur.pit() == cur.lastpit()) {
1030 // The cursor hasn't changed yet. To give the
1031 // DEPM the possibility of doing something we must
1032 // provide it with two different cursors.
1034 dummy.pos() = dummy.pit() = 0;
1035 if (cur.bv().checkDepm(dummy, cur))
1036 cur.forceBufferUpdate();
1042 case LFUN_WORD_LEFT:
1043 case LFUN_WORD_LEFT_SELECT:
1044 if (lyxrc.visual_cursor) {
1045 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
1046 bool const cur_moved = cursorVisLeftOneWord(cur);
1047 needsUpdate |= cur_moved;
1048 if (!cur_moved && oldTopSlice == cur.top()
1049 && cur.boundary() == oldBoundary) {
1051 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1054 if (cur.reverseDirectionNeeded()) {
1055 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1056 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1058 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1059 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1066 case LFUN_WORD_BACKWARD:
1067 case LFUN_WORD_BACKWARD_SELECT: {
1068 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1069 bool const cur_moved = cursorBackwardOneWord(cur);
1070 needsUpdate |= cur_moved;
1072 if (!cur_moved && oldTopSlice == cur.top()
1073 && cur.boundary() == oldBoundary) {
1075 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1077 // we will probably be moving out the inset, so we should execute
1078 // the depm-mechanism, but only when the cursor has a place to
1079 // go outside this inset, i.e. in a slice above.
1080 if (cur.depth() > 1 && cur.pos() == 0
1081 && cur.pit() == 0) {
1082 // The cursor hasn't changed yet. To give the
1083 // DEPM the possibility of doing something we must
1084 // provide it with two different cursors.
1086 dummy.pos() = cur.lastpos();
1087 dummy.pit() = cur.lastpit();
1088 if (cur.bv().checkDepm(dummy, cur))
1089 cur.forceBufferUpdate();
1095 case LFUN_WORD_SELECT: {
1096 selectWord(cur, WHOLE_WORD);
1097 finishChange(cur, true);
1101 case LFUN_NEWLINE_INSERT: {
1102 InsetNewlineParams inp;
1103 docstring arg = cmd.argument();
1104 if (arg == "linebreak")
1105 inp.kind = InsetNewlineParams::LINEBREAK;
1107 inp.kind = InsetNewlineParams::NEWLINE;
1108 cap::replaceSelection(cur);
1110 cur.insert(new InsetNewline(inp));
1112 moveCursor(cur, false);
1116 case LFUN_TAB_INSERT: {
1117 bool const multi_par_selection = cur.selection() &&
1118 cur.selBegin().pit() != cur.selEnd().pit();
1119 if (multi_par_selection) {
1120 // If there is a multi-paragraph selection, a tab is inserted
1121 // at the beginning of each paragraph.
1122 cur.recordUndoSelection();
1123 pit_type const pit_end = cur.selEnd().pit();
1124 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1125 pars_[pit].insertChar(0, '\t',
1126 bv->buffer().params().track_changes);
1127 // Update the selection pos to make sure the selection does not
1128 // change as the inserted tab will increase the logical pos.
1129 if (cur.realAnchor().pit() == pit)
1130 cur.realAnchor().forwardPos();
1131 if (cur.pit() == pit)
1136 // Maybe we shouldn't allow tabs within a line, because they
1137 // are not (yet) aligned as one might do expect.
1138 FuncRequest ncmd(LFUN_SELF_INSERT, from_ascii("\t"));
1139 dispatch(cur, ncmd);
1144 case LFUN_TAB_DELETE: {
1145 bool const tc = bv->buffer().params().track_changes;
1146 if (cur.selection()) {
1147 // If there is a selection, a tab (if present) is removed from
1148 // the beginning of each paragraph.
1149 cur.recordUndoSelection();
1150 pit_type const pit_end = cur.selEnd().pit();
1151 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1152 Paragraph & par = paragraphs()[pit];
1155 char_type const c = par.getChar(0);
1156 if (c == '\t' || c == ' ') {
1157 // remove either 1 tab or 4 spaces.
1158 int const n = (c == ' ' ? 4 : 1);
1159 for (int i = 0; i < n
1160 && !par.empty() && par.getChar(0) == c; ++i) {
1161 if (cur.pit() == pit)
1163 if (cur.realAnchor().pit() == pit
1164 && cur.realAnchor().pos() > 0 )
1165 cur.realAnchor().backwardPos();
1166 par.eraseChar(0, tc);
1172 // If there is no selection, try to remove a tab or some spaces
1173 // before the position of the cursor.
1174 Paragraph & par = paragraphs()[cur.pit()];
1175 pos_type const pos = cur.pos();
1180 char_type const c = par.getChar(pos - 1);
1184 par.eraseChar(cur.pos(), tc);
1186 for (int n_spaces = 0;
1188 && par.getChar(cur.pos() - 1) == ' '
1192 par.eraseChar(cur.pos(), tc);
1199 case LFUN_CHAR_DELETE_FORWARD:
1200 if (!cur.selection()) {
1201 if (cur.pos() == cur.paragraph().size())
1202 // Par boundary, force full-screen update
1203 singleParUpdate = false;
1204 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1206 cur.selection(true);
1211 needsUpdate |= erase(cur);
1214 cutSelection(cur, false);
1215 singleParUpdate = false;
1217 moveCursor(cur, false);
1220 case LFUN_CHAR_DELETE_BACKWARD:
1221 if (!cur.selection()) {
1222 if (bv->getIntl().getTransManager().backspace()) {
1223 bool par_boundary = cur.pos() == 0;
1224 bool first_par = cur.pit() == 0;
1225 // Par boundary, full-screen update
1227 singleParUpdate = false;
1228 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1230 cur.selection(true);
1235 needsUpdate |= backspace(cur);
1237 if (par_boundary && !first_par && cur.pos() > 0
1238 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1239 needsUpdate |= backspace(cur);
1244 cutSelection(cur, false);
1245 singleParUpdate = false;
1249 case LFUN_PARAGRAPH_BREAK: {
1250 cap::replaceSelection(cur);
1251 pit_type pit = cur.pit();
1252 Paragraph const & par = pars_[pit];
1253 bool lastpar = (pit == pit_type(pars_.size() - 1));
1254 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1255 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1256 if (prev < pit && cur.pos() == par.beginOfBody()
1257 && !par.size() && !par.isEnvSeparator(cur.pos())
1258 && !par.layout().keepempty
1259 && !par.layout().isCommand()
1260 && pars_[prev].layout() != par.layout()
1261 && pars_[prev].layout().isEnvironment()
1262 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1263 if (par.layout().isEnvironment()
1264 && pars_[prev].getDepth() == par.getDepth()) {
1265 docstring const layout = par.layout().name();
1266 DocumentClass const & tc = bv->buffer().params().documentClass();
1267 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1268 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1269 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1270 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1272 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1273 breakParagraph(cur);
1275 Font const f(inherit_font, cur.current_font.language());
1276 pars_[cur.pit() - 1].resetFonts(f);
1278 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1280 breakParagraph(cur, cmd.getArg(0) == "inverse");
1283 // If we have a list and autoinsert item insets,
1285 Layout::LaTeXArgMap args = par.layout().args();
1286 for (auto const & thearg : args) {
1287 Layout::latexarg arg = thearg.second;
1288 if (arg.autoinsert && prefixIs(thearg.first, "item:")) {
1289 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, thearg.first);
1290 lyx::dispatch(cmd2);
1296 case LFUN_INSET_INSERT: {
1299 // We have to avoid triggering InstantPreview loading
1300 // before inserting into the document. See bug #5626.
1301 bool loaded = bv->buffer().isFullyLoaded();
1302 bv->buffer().setFullyLoaded(false);
1303 Inset * inset = createInset(&bv->buffer(), cmd);
1304 bv->buffer().setFullyLoaded(loaded);
1307 // FIXME (Abdel 01/02/2006):
1308 // What follows would be a partial fix for bug 2154:
1309 // http://www.lyx.org/trac/ticket/2154
1310 // This automatically put the label inset _after_ a
1311 // numbered section. It should be possible to extend the mechanism
1312 // to any kind of LateX environement.
1313 // The correct way to fix that bug would be at LateX generation.
1314 // I'll let the code here for reference as it could be used for some
1315 // other feature like "automatic labelling".
1317 Paragraph & par = pars_[cur.pit()];
1318 if (inset->lyxCode() == LABEL_CODE
1319 && !par.layout().counter.empty()) {
1320 // Go to the end of the paragraph
1321 // Warning: Because of Change-Tracking, the last
1322 // position is 'size()' and not 'size()-1':
1323 cur.pos() = par.size();
1324 // Insert a new paragraph
1325 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1329 if (cur.selection())
1330 cutSelection(cur, false);
1332 cur.forceBufferUpdate();
1333 if (inset->editable() && inset->asInsetText())
1334 inset->edit(cur, true);
1338 // trigger InstantPreview now
1339 if (inset->lyxCode() == EXTERNAL_CODE) {
1340 InsetExternal & ins =
1341 static_cast<InsetExternal &>(*inset);
1342 ins.updatePreview();
1349 case LFUN_INSET_DISSOLVE: {
1350 if (dissolveInset(cur)) {
1352 cur.forceBufferUpdate();
1357 case LFUN_GRAPHICS_SET_GROUP: {
1358 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1364 string id = to_utf8(cmd.argument());
1365 string grp = graphics::getGroupParams(bv->buffer(), id);
1366 InsetGraphicsParams tmp, inspar = ins->getParams();
1369 inspar.groupId = to_utf8(cmd.argument());
1371 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1372 tmp.filename = inspar.filename;
1376 ins->setParams(inspar);
1380 case LFUN_SPACE_INSERT:
1381 if (cur.paragraph().layout().free_spacing)
1382 insertChar(cur, ' ');
1384 doInsertInset(cur, this, cmd, false, false);
1387 moveCursor(cur, false);
1390 case LFUN_SPECIALCHAR_INSERT: {
1391 string const name = to_utf8(cmd.argument());
1392 if (name == "hyphenation")
1393 specialChar(cur, InsetSpecialChar::HYPHENATION);
1394 else if (name == "allowbreak")
1395 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1396 else if (name == "ligature-break")
1397 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1398 else if (name == "slash")
1399 specialChar(cur, InsetSpecialChar::SLASH);
1400 else if (name == "nobreakdash")
1401 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1402 else if (name == "dots")
1403 specialChar(cur, InsetSpecialChar::LDOTS);
1404 else if (name == "end-of-sentence")
1405 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1406 else if (name == "menu-separator")
1407 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1408 else if (name == "lyx")
1409 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1410 else if (name == "tex")
1411 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1412 else if (name == "latex")
1413 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1414 else if (name == "latex2e")
1415 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1416 else if (name.empty())
1417 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1419 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1423 case LFUN_IPAMACRO_INSERT: {
1424 string const arg = cmd.getArg(0);
1425 if (arg == "deco") {
1426 // Open the inset, and move the current selection
1428 doInsertInset(cur, this, cmd, true, true);
1430 // Some insets are numbered, others are shown in the outline pane so
1431 // let's update the labels and the toc backend.
1432 cur.forceBufferUpdate();
1435 if (arg == "tone-falling")
1436 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1437 else if (arg == "tone-rising")
1438 ipaChar(cur, InsetIPAChar::TONE_RISING);
1439 else if (arg == "tone-high-rising")
1440 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1441 else if (arg == "tone-low-rising")
1442 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1443 else if (arg == "tone-high-rising-falling")
1444 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1445 else if (arg.empty())
1446 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1448 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1452 case LFUN_WORD_UPCASE:
1453 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1456 case LFUN_WORD_LOWCASE:
1457 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1460 case LFUN_WORD_CAPITALIZE:
1461 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1464 case LFUN_CHARS_TRANSPOSE:
1465 charsTranspose(cur);
1469 cur.message(_("Paste"));
1470 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1471 cap::replaceSelection(cur);
1473 // without argument?
1474 string const arg = to_utf8(cmd.argument());
1476 bool tryGraphics = true;
1477 if (theClipboard().isInternal())
1478 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1479 else if (theClipboard().hasTextContents()) {
1480 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1481 true, Clipboard::AnyTextType))
1482 tryGraphics = false;
1484 if (tryGraphics && theClipboard().hasGraphicsContents())
1485 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1486 } else if (isStrUnsignedInt(arg)) {
1487 // we have a numerical argument
1488 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1489 convert<unsigned int>(arg));
1490 } else if (arg == "html" || arg == "latex") {
1491 Clipboard::TextType type = (arg == "html") ?
1492 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1493 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1495 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1497 type = Clipboard::PdfGraphicsType;
1498 else if (arg == "png")
1499 type = Clipboard::PngGraphicsType;
1500 else if (arg == "jpeg")
1501 type = Clipboard::JpegGraphicsType;
1502 else if (arg == "linkback")
1503 type = Clipboard::LinkBackGraphicsType;
1504 else if (arg == "emf")
1505 type = Clipboard::EmfGraphicsType;
1506 else if (arg == "wmf")
1507 type = Clipboard::WmfGraphicsType;
1509 // we also check in getStatus()
1510 LYXERR0("Unrecognized graphics type: " << arg);
1512 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1515 bv->buffer().errors("Paste");
1516 cur.clearSelection(); // bug 393
1522 cutSelection(cur, true);
1523 cur.message(_("Cut"));
1528 cur.message(_("Copy"));
1531 case LFUN_SERVER_GET_XY:
1532 cur.message(from_utf8(
1533 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1534 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1537 case LFUN_SERVER_SET_XY: {
1540 istringstream is(to_utf8(cmd.argument()));
1543 lyxerr << "SETXY: Could not parse coordinates in '"
1544 << to_utf8(cmd.argument()) << endl;
1546 tm->setCursorFromCoordinates(cur, x, y);
1550 case LFUN_SERVER_GET_LAYOUT:
1551 cur.message(cur.paragraph().layout().name());
1555 case LFUN_LAYOUT_TOGGLE: {
1556 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1557 docstring req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1558 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(req_layout));
1560 docstring layout = resolveLayout(req_layout, cur);
1561 if (layout.empty()) {
1562 cur.errorMessage(from_utf8(N_("Layout ")) + req_layout +
1563 from_utf8(N_(" not known")));
1567 docstring const old_layout = cur.paragraph().layout().name();
1568 bool change_layout = !isAlreadyLayout(layout, cur);
1570 if (cmd.action() == LFUN_LAYOUT_TOGGLE && !change_layout) {
1571 change_layout = true;
1572 layout = resolveLayout(docstring(), cur);
1575 if (change_layout) {
1576 setLayout(cur, layout);
1577 if (cur.pit() > 0 && !ignoreautonests) {
1578 set<docstring> const & autonests =
1579 pars_[cur.pit() - 1].layout().autonests();
1580 set<docstring> const & autonested =
1581 pars_[cur.pit()].layout().isAutonestedBy();
1582 if (autonests.find(layout) != autonests.end()
1583 || autonested.find(old_layout) != autonested.end())
1584 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1588 DocumentClass const & tclass = bv->buffer().params().documentClass();
1589 for (auto const & la_pair : tclass[layout].args()) {
1590 Layout::latexarg const & arg = la_pair.second;
1591 if (arg.autoinsert) {
1592 FuncRequest const cmd2(LFUN_ARGUMENT_INSERT, la_pair.first);
1593 lyx::dispatch(cmd2);
1600 case LFUN_ENVIRONMENT_SPLIT: {
1601 bool const outer = cmd.argument() == "outer";
1602 bool const previous = cmd.argument() == "previous";
1603 bool const before = cmd.argument() == "before";
1604 bool const normal = cmd.argument().empty();
1605 Paragraph const & para = cur.paragraph();
1607 if (para.layout().isEnvironment())
1608 layout = para.layout().name();
1609 depth_type split_depth = cur.paragraph().params().depth();
1610 depth_type nextpar_depth = 0;
1611 if (outer || previous) {
1612 // check if we have an environment in our scope
1613 pit_type pit = cur.pit();
1614 Paragraph cpar = pars_[pit];
1620 if (layout.empty() && previous
1621 && cpar.layout().isEnvironment()
1622 && cpar.params().depth() <= split_depth)
1623 layout = cpar.layout().name();
1624 if (cpar.params().depth() < split_depth
1625 && cpar.layout().isEnvironment()) {
1627 layout = cpar.layout().name();
1628 split_depth = cpar.params().depth();
1630 if (cpar.params().depth() == 0)
1634 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1635 // save nesting of following paragraph
1636 Paragraph cpar = pars_[cur.pit() + 1];
1637 nextpar_depth = cpar.params().depth();
1640 cur.top().setPitPos(cur.pit(), 0);
1641 if (before || cur.pos() > 0)
1642 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1643 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1644 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1646 while (cur.paragraph().params().depth() > split_depth)
1647 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1649 DocumentClass const & tc = bv->buffer().params().documentClass();
1650 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1651 + from_ascii("\" ignoreautonests")));
1652 // FIXME: Bibitem mess!
1653 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
1654 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
1655 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1658 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1659 while (cur.paragraph().params().depth() < split_depth)
1660 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1663 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1664 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1665 if ((outer || normal) && nextpar_depth > 0) {
1666 // restore nesting of following paragraph
1667 DocIterator scur = cur;
1668 depth_type const max_depth = cur.paragraph().params().depth() + 1;
1670 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth)) {
1671 depth_type const olddepth = cur.paragraph().params().depth();
1672 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1673 if (olddepth == cur.paragraph().params().depth())
1674 // leave loop if no incrementation happens
1677 cur.setCursor(scur);
1683 case LFUN_CLIPBOARD_PASTE:
1684 cap::replaceSelection(cur);
1685 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1686 cmd.argument() == "paragraph");
1687 bv->buffer().errors("Paste");
1690 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1691 cap::replaceSelection(cur);
1692 pasteSimpleText(cur, cmd.argument() == "paragraph");
1695 case LFUN_PRIMARY_SELECTION_PASTE:
1696 cap::replaceSelection(cur);
1697 pasteString(cur, theSelection().get(),
1698 cmd.argument() == "paragraph");
1701 case LFUN_SELECTION_PASTE:
1702 // Copy the selection buffer to the clipboard stack,
1703 // because we want it to appear in the "Edit->Paste
1705 cap::replaceSelection(cur);
1706 cap::copySelectionToStack();
1707 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1708 bv->buffer().errors("Paste");
1711 case LFUN_QUOTE_INSERT: {
1712 cap::replaceSelection(cur);
1715 Paragraph const & par = cur.paragraph();
1716 pos_type pos = cur.pos();
1717 // Ignore deleted text before cursor
1718 while (pos > 0 && par.isDeleted(pos - 1))
1721 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1723 // Guess quote side.
1724 // A space triggers an opening quote. This is passed if the preceding
1725 // char/inset is a space or at paragraph start.
1727 if (pos > 0 && !par.isSpace(pos - 1)) {
1728 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1729 // If an opening double quotation mark precedes, and this
1730 // is a single quote, make it opening as well
1732 static_cast<InsetQuotes &>(*cur.prevInset());
1733 string const type = ins.getType();
1734 if (!suffixIs(type, "ld") || !inner)
1735 c = par.getChar(pos - 1);
1737 else if (!cur.prevInset()
1738 || (cur.prevInset() && cur.prevInset()->isChar()))
1739 // If a char precedes, pass that and let InsetQuote decide
1740 c = par.getChar(pos - 1);
1743 if (par.getInset(pos - 1)
1744 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1745 // skip "invisible" insets
1749 c = par.getChar(pos - 1);
1754 InsetQuotesParams::QuoteLevel const quote_level = inner
1755 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1756 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1757 cur.buffer()->updateBuffer();
1762 case LFUN_MOUSE_TRIPLE:
1763 if (cmd.button() == mouse_button::button1) {
1764 tm->cursorHome(cur);
1772 case LFUN_MOUSE_DOUBLE:
1773 if (cmd.button() == mouse_button::button1) {
1774 selectWord(cur, WHOLE_WORD);
1779 // Single-click on work area
1780 case LFUN_MOUSE_PRESS: {
1781 // We are not marking a selection with the keyboard in any case.
1782 Cursor & bvcur = cur.bv().cursor();
1783 bvcur.setMark(false);
1784 switch (cmd.button()) {
1785 case mouse_button::button1:
1786 if (!bvcur.selection())
1788 bvcur.resetAnchor();
1789 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1790 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1791 if (bvcur.wordSelection())
1792 selectWord(bvcur, WHOLE_WORD);
1795 case mouse_button::button2:
1796 if (lyxrc.mouse_middlebutton_paste) {
1797 // Middle mouse pasting.
1798 bv->mouseSetCursor(cur);
1800 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1801 "selection-paste ; primary-selection-paste paragraph"));
1803 cur.noScreenUpdate();
1806 case mouse_button::button3: {
1807 // Don't do anything if we right-click a
1808 // selection, a context menu will popup.
1809 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1810 && cur < bvcur.selectionEnd()) {
1811 cur.noScreenUpdate();
1814 if (!bv->mouseSetCursor(cur, false))
1815 cur.screenUpdateFlags(Update::FitCursor);
1821 } // switch (cmd.button())
1824 case LFUN_MOUSE_MOTION: {
1825 // Mouse motion with right or middle mouse do nothing for now.
1826 if (cmd.button() != mouse_button::button1) {
1827 cur.noScreenUpdate();
1830 // ignore motions deeper nested than the real anchor
1831 Cursor & bvcur = cur.bv().cursor();
1832 if (!bvcur.realAnchor().hasPart(cur)) {
1836 CursorSlice old = bvcur.top();
1838 int const wh = bv->workHeight();
1839 int const y = max(0, min(wh - 1, cmd.y()));
1841 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1842 cur.setTargetX(cmd.x());
1843 // Don't allow selecting a separator inset
1844 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1847 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1848 else if (cmd.y() < 0)
1849 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1850 // This is to allow jumping over large insets
1851 if (cur.top() == old) {
1853 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1854 else if (cmd.y() < 0)
1855 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1857 // We continue with our existing selection or start a new one, so don't
1858 // reset the anchor.
1859 bvcur.setCursor(cur);
1860 bvcur.selection(true);
1861 bvcur.setCurrentFont();
1862 if (cur.top() == old) {
1863 // We didn't move one iota, so no need to update the screen.
1864 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1865 //cur.noScreenUpdate();
1871 case LFUN_MOUSE_RELEASE:
1872 switch (cmd.button()) {
1873 case mouse_button::button1:
1874 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1875 // If there is a new selection, update persistent selection;
1876 // otherwise, single click does not clear persistent selection
1878 if (cur.selection()) {
1879 // Finish selection. If double click,
1880 // cur is moved to the end of word by
1881 // selectWord but bvcur is current
1883 cur.bv().cursor().setSelection();
1884 // We might have removed an empty but drawn selection
1885 // (probably a margin)
1886 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1888 cur.noScreenUpdate();
1889 // FIXME: We could try to handle drag and drop of selection here.
1892 case mouse_button::button2:
1893 // Middle mouse pasting is handled at mouse press time,
1894 // see LFUN_MOUSE_PRESS.
1895 cur.noScreenUpdate();
1898 case mouse_button::button3:
1899 // Cursor was set at LFUN_MOUSE_PRESS time.
1900 // FIXME: If there is a selection we could try to handle a special
1901 // drag & drop context menu.
1902 cur.noScreenUpdate();
1905 case mouse_button::none:
1906 case mouse_button::button4:
1907 case mouse_button::button5:
1909 } // switch (cmd.button())
1913 case LFUN_SELF_INSERT: {
1914 if (cmd.argument().empty())
1917 // Automatically delete the currently selected
1918 // text and replace it with what is being
1919 // typed in now. Depends on lyxrc settings
1920 // "auto_region_delete", which defaults to
1923 if (lyxrc.auto_region_delete && cur.selection())
1924 cutSelection(cur, false);
1925 cur.clearSelection();
1927 for (char_type c : cmd.argument())
1928 bv->translateAndInsert(c, this, cur);
1931 moveCursor(cur, false);
1932 cur.markNewWordPosition();
1933 bv->bookmarkEditPosition();
1937 case LFUN_HREF_INSERT: {
1938 docstring content = cmd.argument();
1939 if (content.empty() && cur.selection())
1940 content = cur.selectionAsString(false);
1942 InsetCommandParams p(HYPERLINK_CODE);
1943 if (!content.empty()){
1944 // if it looks like a link, we'll put it as target,
1945 // otherwise as name (bug #8792).
1948 // regex_match(to_utf8(content), matches, link_re)
1949 // because smatch stores pointers to the substrings rather
1950 // than making copies of them. And those pointers become
1951 // invalid after regex_match returns, since it is then
1952 // being given a temporary object. (Thanks to Georg for
1953 // figuring that out.)
1954 regex const link_re("^([a-z]+):.*");
1956 string const c = to_utf8(lowercase(content));
1958 if (c.substr(0,7) == "mailto:") {
1959 p["target"] = content;
1960 p["type"] = from_ascii("mailto:");
1961 } else if (regex_match(c, matches, link_re)) {
1962 p["target"] = content;
1963 string protocol = matches.str(1);
1964 if (protocol == "file")
1965 p["type"] = from_ascii("file:");
1967 p["name"] = content;
1969 string const data = InsetCommand::params2string(p);
1971 // we need to have a target. if we already have one, then
1972 // that gets used at the default for the name, too, which
1973 // is probably what is wanted.
1974 if (p["target"].empty()) {
1975 bv->showDialog("href", data);
1977 FuncRequest fr(LFUN_INSET_INSERT, data);
1983 case LFUN_LABEL_INSERT: {
1984 InsetCommandParams p(LABEL_CODE);
1985 // Try to generate a valid label
1986 p["name"] = (cmd.argument().empty()) ?
1987 cur.getPossibleLabel() :
1989 string const data = InsetCommand::params2string(p);
1991 if (cmd.argument().empty()) {
1992 bv->showDialog("label", data);
1994 FuncRequest fr(LFUN_INSET_INSERT, data);
2000 case LFUN_INFO_INSERT: {
2001 if (cmd.argument().empty()) {
2002 bv->showDialog("info", cur.current_font.language()->lang());
2005 inset = createInset(cur.buffer(), cmd);
2009 insertInset(cur, inset);
2010 cur.forceBufferUpdate();
2015 case LFUN_CAPTION_INSERT:
2016 case LFUN_FOOTNOTE_INSERT:
2017 case LFUN_NOTE_INSERT:
2018 case LFUN_BOX_INSERT:
2019 case LFUN_BRANCH_INSERT:
2020 case LFUN_PHANTOM_INSERT:
2021 case LFUN_ERT_INSERT:
2022 case LFUN_LISTING_INSERT:
2023 case LFUN_MARGINALNOTE_INSERT:
2024 case LFUN_ARGUMENT_INSERT:
2025 case LFUN_INDEX_INSERT:
2026 case LFUN_PREVIEW_INSERT:
2027 case LFUN_SCRIPT_INSERT:
2028 case LFUN_IPA_INSERT:
2029 // Open the inset, and move the current selection
2031 doInsertInset(cur, this, cmd, true, true);
2033 if (act == LFUN_SCRIPT_INSERT) {
2034 /* Script insets change the font style in metrics(), and
2035 * this is used to compute the height of the caret
2036 * (because the font is stored in TextMetrics::font_).
2037 * When we insert, we have to make sure that metrics are
2038 * computed so that the caret height is wrong. Arguably,
2039 * this is hackish.*/
2040 bv->processUpdateFlags(Update::SinglePar);
2042 cur.setCurrentFont();
2043 // Some insets are numbered, others are shown in the outline pane so
2044 // let's update the labels and the toc backend.
2045 cur.forceBufferUpdate();
2048 case LFUN_FLEX_INSERT: {
2049 // Open the inset, and move the current selection
2051 bool const sel = cur.selection();
2052 doInsertInset(cur, this, cmd, true, true);
2053 // Insert auto-insert arguments
2054 bool autoargs = false;
2055 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
2056 Layout::LaTeXArgMap::const_iterator lait = args.begin();
2057 Layout::LaTeXArgMap::const_iterator const laend = args.end();
2058 for (; lait != laend; ++lait) {
2059 Layout::latexarg arg = (*lait).second;
2060 if (arg.autoinsert) {
2061 // The cursor might have been invalidated by the replaceSelection.
2062 cur.buffer()->changed(true);
2063 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first);
2064 lyx::dispatch(cmd2);
2070 cur.leaveInset(cur.inset());
2073 // Some insets are numbered, others are shown in the outline pane so
2074 // let's update the labels and the toc backend.
2075 cur.forceBufferUpdate();
2079 case LFUN_TABULAR_INSERT:
2080 // if there were no arguments, just open the dialog
2081 if (doInsertInset(cur, this, cmd, false, true))
2084 bv->showDialog("tabularcreate");
2088 case LFUN_FLOAT_INSERT:
2089 case LFUN_FLOAT_WIDE_INSERT:
2090 case LFUN_WRAP_INSERT: {
2091 // will some content be moved into the inset?
2092 bool const content = cur.selection();
2093 // does the content consist of multiple paragraphs?
2094 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2096 doInsertInset(cur, this, cmd, true, true);
2099 // If some single-par content is moved into the inset,
2100 // doInsertInset puts the cursor outside the inset.
2101 // To insert the caption we put it back into the inset.
2102 // FIXME cleanup doInsertInset to avoid such dances!
2103 if (content && singlepar)
2106 ParagraphList & pars = cur.text()->paragraphs();
2108 DocumentClass const & tclass = bv->buffer().params().documentClass();
2110 // add a separate paragraph for the caption inset
2111 pars.push_back(Paragraph());
2112 pars.back().setInsetOwner(&cur.text()->inset());
2113 pars.back().setPlainOrDefaultLayout(tclass);
2114 int cap_pit = pars.size() - 1;
2116 // if an empty inset was created, we create an additional empty
2117 // paragraph at the bottom so that the user can choose where to put
2118 // the graphics (or table).
2120 pars.push_back(Paragraph());
2121 pars.back().setInsetOwner(&cur.text()->inset());
2122 pars.back().setPlainOrDefaultLayout(tclass);
2125 // reposition the cursor to the caption
2126 cur.pit() = cap_pit;
2128 // FIXME: This Text/Cursor dispatch handling is a mess!
2129 // We cannot use Cursor::dispatch here it needs access to up to
2131 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2132 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2133 cur.forceBufferUpdate();
2134 cur.screenUpdateFlags(Update::Force);
2135 // FIXME: When leaving the Float (or Wrap) inset we should
2136 // delete any empty paragraph left above or below the
2141 case LFUN_NOMENCL_INSERT: {
2142 InsetCommandParams p(NOMENCL_CODE);
2143 if (cmd.argument().empty()) {
2145 bv->cursor().innerText()->getStringForDialog(bv->cursor());
2146 cur.clearSelection();
2148 p["symbol"] = cmd.argument();
2149 string const data = InsetCommand::params2string(p);
2150 bv->showDialog("nomenclature", data);
2154 case LFUN_INDEX_PRINT: {
2155 InsetCommandParams p(INDEX_PRINT_CODE);
2156 if (cmd.argument().empty())
2157 p["type"] = from_ascii("idx");
2159 p["type"] = cmd.argument();
2160 string const data = InsetCommand::params2string(p);
2161 FuncRequest fr(LFUN_INSET_INSERT, data);
2166 case LFUN_NOMENCL_PRINT:
2167 case LFUN_NEWPAGE_INSERT:
2169 doInsertInset(cur, this, cmd, false, false);
2173 case LFUN_SEPARATOR_INSERT: {
2174 doInsertInset(cur, this, cmd, false, false);
2176 // remove a following space
2177 Paragraph & par = cur.paragraph();
2178 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2179 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2183 case LFUN_DEPTH_DECREMENT:
2184 changeDepth(cur, DEC_DEPTH);
2187 case LFUN_DEPTH_INCREMENT:
2188 changeDepth(cur, INC_DEPTH);
2191 case LFUN_REGEXP_MODE:
2192 regexpDispatch(cur, cmd);
2195 case LFUN_MATH_MODE: {
2196 if (cmd.argument() == "on" || cmd.argument() == "") {
2197 // don't pass "on" as argument
2198 // (it would appear literally in the first cell)
2199 docstring sel = cur.selectionAsString(false);
2200 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2201 // create a macro template if we see "\\newcommand" somewhere, and
2202 // an ordinary formula otherwise
2204 && (sel.find(from_ascii("\\newcommand")) != string::npos
2205 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2206 || sel.find(from_ascii("\\def")) != string::npos)
2207 && macro->fromString(sel)) {
2209 replaceSelection(cur);
2212 // no meaningful macro template was found
2214 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2217 // The argument is meaningful
2218 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2219 // has a different meaning in math mode
2220 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2224 case LFUN_MATH_MACRO:
2225 if (cmd.argument().empty())
2226 cur.errorMessage(from_utf8(N_("Missing argument")));
2229 string s = to_utf8(cmd.argument());
2230 string const s1 = token(s, ' ', 1);
2231 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2232 string const s2 = token(s, ' ', 2);
2233 MacroType type = MacroTypeNewcommand;
2235 type = MacroTypeDef;
2236 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2237 from_utf8(token(s, ' ', 0)), nargs, false, type);
2238 inset->setBuffer(bv->buffer());
2239 insertInset(cur, inset);
2241 // enter macro inset and select the name
2243 cur.top().pos() = cur.top().lastpos();
2245 cur.selection(true);
2246 cur.top().pos() = 0;
2250 case LFUN_MATH_DISPLAY:
2251 case LFUN_MATH_SUBSCRIPT:
2252 case LFUN_MATH_SUPERSCRIPT:
2253 case LFUN_MATH_INSERT:
2254 case LFUN_MATH_AMS_MATRIX:
2255 case LFUN_MATH_MATRIX:
2256 case LFUN_MATH_DELIM:
2257 case LFUN_MATH_BIGDELIM:
2258 mathDispatch(cur, cmd);
2261 case LFUN_FONT_EMPH: {
2262 Font font(ignore_font, ignore_language);
2263 font.fontInfo().setEmph(FONT_TOGGLE);
2264 toggleAndShow(cur, this, font);
2268 case LFUN_FONT_ITAL: {
2269 Font font(ignore_font, ignore_language);
2270 font.fontInfo().setShape(ITALIC_SHAPE);
2271 toggleAndShow(cur, this, font);
2275 case LFUN_FONT_BOLD:
2276 case LFUN_FONT_BOLDSYMBOL: {
2277 Font font(ignore_font, ignore_language);
2278 font.fontInfo().setSeries(BOLD_SERIES);
2279 toggleAndShow(cur, this, font);
2283 case LFUN_FONT_NOUN: {
2284 Font font(ignore_font, ignore_language);
2285 font.fontInfo().setNoun(FONT_TOGGLE);
2286 toggleAndShow(cur, this, font);
2290 case LFUN_FONT_TYPEWRITER: {
2291 Font font(ignore_font, ignore_language);
2292 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2293 toggleAndShow(cur, this, font);
2297 case LFUN_FONT_SANS: {
2298 Font font(ignore_font, ignore_language);
2299 font.fontInfo().setFamily(SANS_FAMILY);
2300 toggleAndShow(cur, this, font);
2304 case LFUN_FONT_ROMAN: {
2305 Font font(ignore_font, ignore_language);
2306 font.fontInfo().setFamily(ROMAN_FAMILY);
2307 toggleAndShow(cur, this, font);
2311 case LFUN_FONT_DEFAULT: {
2312 Font font(inherit_font, ignore_language);
2313 toggleAndShow(cur, this, font);
2317 case LFUN_FONT_STRIKEOUT: {
2318 Font font(ignore_font, ignore_language);
2319 font.fontInfo().setStrikeout(FONT_TOGGLE);
2320 toggleAndShow(cur, this, font);
2324 case LFUN_FONT_CROSSOUT: {
2325 Font font(ignore_font, ignore_language);
2326 font.fontInfo().setXout(FONT_TOGGLE);
2327 toggleAndShow(cur, this, font);
2331 case LFUN_FONT_UNDERUNDERLINE: {
2332 Font font(ignore_font, ignore_language);
2333 font.fontInfo().setUuline(FONT_TOGGLE);
2334 toggleAndShow(cur, this, font);
2338 case LFUN_FONT_UNDERWAVE: {
2339 Font font(ignore_font, ignore_language);
2340 font.fontInfo().setUwave(FONT_TOGGLE);
2341 toggleAndShow(cur, this, font);
2345 case LFUN_FONT_UNDERLINE: {
2346 Font font(ignore_font, ignore_language);
2347 font.fontInfo().setUnderbar(FONT_TOGGLE);
2348 toggleAndShow(cur, this, font);
2352 case LFUN_FONT_SIZE: {
2353 Font font(ignore_font, ignore_language);
2354 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2355 toggleAndShow(cur, this, font);
2359 case LFUN_LANGUAGE: {
2360 string const lang_arg = cmd.getArg(0);
2361 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2362 Language const * lang =
2363 reset ? reset_language
2364 : languages.getLanguage(lang_arg);
2365 // we allow reset_language, which is 0, but only if it
2366 // was requested via empty or "reset" arg.
2367 if (!lang && !reset)
2369 bool const toggle = (cmd.getArg(1) != "set");
2370 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2371 Font font(ignore_font, lang);
2372 toggleAndShow(cur, this, font, toggle);
2373 // We need a buffer update if we change the language
2375 if (cur.insetInSelection(INFO_CODE))
2376 cur.forceBufferUpdate();
2380 case LFUN_TEXTSTYLE_APPLY: {
2381 unsigned int num = 0;
2382 string const arg = to_utf8(cmd.argument());
2385 if (isStrUnsignedInt(arg)) {
2386 num = convert<uint>(arg);
2387 if (num >= freeFonts.size()) {
2388 cur.message(_("Invalid argument (number exceeds stack size)!"));
2392 cur.message(_("Invalid argument (must be a non-negative number)!"));
2396 toggleAndShow(cur, this, freeFonts[num].second, toggleall);
2397 cur.message(bformat(_("Text properties applied: %1$s"), freeFonts[num].first));
2401 // Set the freefont using the contents of \param data dispatched from
2402 // the frontends and apply it at the current cursor location.
2403 case LFUN_TEXTSTYLE_UPDATE: {
2404 Font font(ignore_font, ignore_language);
2406 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2407 docstring const props = font.stateText(&bv->buffer().params(), true);
2408 freeFonts.push(make_pair(props, font));
2410 toggleAndShow(cur, this, font, toggleall);
2411 // We need a buffer update if we change the language
2413 if (cur.insetInSelection(INFO_CODE))
2414 cur.forceBufferUpdate();
2415 cur.message(bformat(_("Text properties applied: %1$s"), props));
2417 LYXERR0("Invalid argument of textstyle-update");
2421 case LFUN_FINISHED_LEFT:
2422 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2423 // We're leaving an inset, going left. If the inset is LTR, we're
2424 // leaving from the front, so we should not move (remain at --- but
2425 // not in --- the inset). If the inset is RTL, move left, without
2426 // entering the inset itself; i.e., move to after the inset.
2427 if (cur.paragraph().getFontSettings(
2428 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2429 cursorVisLeft(cur, true);
2432 case LFUN_FINISHED_RIGHT:
2433 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2434 // We're leaving an inset, going right. If the inset is RTL, we're
2435 // leaving from the front, so we should not move (remain at --- but
2436 // not in --- the inset). If the inset is LTR, move right, without
2437 // entering the inset itself; i.e., move to after the inset.
2438 if (!cur.paragraph().getFontSettings(
2439 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2440 cursorVisRight(cur, true);
2443 case LFUN_FINISHED_BACKWARD:
2444 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2445 cur.setCurrentFont();
2448 case LFUN_FINISHED_FORWARD:
2449 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2451 cur.setCurrentFont();
2454 case LFUN_LAYOUT_PARAGRAPH: {
2456 params2string(cur.paragraph(), data);
2457 data = "show\n" + data;
2458 bv->showDialog("paragraph", data);
2462 case LFUN_PARAGRAPH_UPDATE: {
2464 params2string(cur.paragraph(), data);
2466 // Will the paragraph accept changes from the dialog?
2468 cur.inset().allowParagraphCustomization(cur.idx());
2470 data = "update " + convert<string>(accept) + '\n' + data;
2471 bv->updateDialog("paragraph", data);
2475 case LFUN_ACCENT_UMLAUT:
2476 case LFUN_ACCENT_CIRCUMFLEX:
2477 case LFUN_ACCENT_GRAVE:
2478 case LFUN_ACCENT_ACUTE:
2479 case LFUN_ACCENT_TILDE:
2480 case LFUN_ACCENT_PERISPOMENI:
2481 case LFUN_ACCENT_CEDILLA:
2482 case LFUN_ACCENT_MACRON:
2483 case LFUN_ACCENT_DOT:
2484 case LFUN_ACCENT_UNDERDOT:
2485 case LFUN_ACCENT_UNDERBAR:
2486 case LFUN_ACCENT_CARON:
2487 case LFUN_ACCENT_BREVE:
2488 case LFUN_ACCENT_TIE:
2489 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2490 case LFUN_ACCENT_CIRCLE:
2491 case LFUN_ACCENT_OGONEK:
2492 theApp()->handleKeyFunc(cmd.action());
2493 if (!cmd.argument().empty())
2494 // FIXME: Are all these characters encoded in one byte in utf8?
2495 bv->translateAndInsert(cmd.argument()[0], this, cur);
2496 cur.screenUpdateFlags(Update::FitCursor);
2499 case LFUN_FLOAT_LIST_INSERT: {
2500 DocumentClass const & tclass = bv->buffer().params().documentClass();
2501 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2503 if (cur.selection())
2504 cutSelection(cur, false);
2505 breakParagraph(cur);
2507 if (cur.lastpos() != 0) {
2508 cursorBackward(cur);
2509 breakParagraph(cur);
2512 docstring const laystr = cur.inset().usePlainLayout() ?
2513 tclass.plainLayoutName() :
2514 tclass.defaultLayoutName();
2515 setLayout(cur, laystr);
2516 ParagraphParameters p;
2517 // FIXME If this call were replaced with one to clearParagraphParams(),
2518 // then we could get rid of this method altogether.
2519 setParagraphs(cur, p);
2520 // FIXME This should be simplified when InsetFloatList takes a
2521 // Buffer in its constructor.
2522 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2523 ifl->setBuffer(bv->buffer());
2524 insertInset(cur, ifl);
2527 lyxerr << "Non-existent float type: "
2528 << to_utf8(cmd.argument()) << endl;
2533 case LFUN_CHANGE_ACCEPT: {
2534 acceptOrRejectChanges(cur, ACCEPT);
2538 case LFUN_CHANGE_REJECT: {
2539 acceptOrRejectChanges(cur, REJECT);
2543 case LFUN_THESAURUS_ENTRY: {
2544 Language const * language = cur.getFont().language();
2545 docstring arg = cmd.argument();
2547 arg = cur.selectionAsString(false);
2549 if (arg.size() > 100 || arg.empty()) {
2550 // Get word or selection
2551 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2552 arg = cur.selectionAsString(false);
2553 arg += " lang=" + from_ascii(language->lang());
2556 string lang = cmd.getArg(1);
2557 // This duplicates the code in GuiThesaurus::initialiseParams
2558 if (prefixIs(lang, "lang=")) {
2559 language = languages.getLanguage(lang.substr(5));
2561 language = cur.getFont().language();
2564 string lang = language->code();
2565 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2566 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2567 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2568 _("The path to the thesaurus directory has not been specified.\n"
2569 "The thesaurus is not functional.\n"
2570 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2573 bv->showDialog("thesaurus", to_utf8(arg));
2577 case LFUN_SPELLING_ADD: {
2578 Language const * language = getLanguage(cur, cmd.getArg(1));
2579 docstring word = from_utf8(cmd.getArg(0));
2581 word = cur.selectionAsString(false);
2583 if (word.size() > 100 || word.empty()) {
2584 // Get word or selection
2585 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2586 word = cur.selectionAsString(false);
2589 WordLangTuple wl(word, language);
2590 theSpellChecker()->insert(wl);
2594 case LFUN_SPELLING_IGNORE: {
2595 Language const * language = getLanguage(cur, cmd.getArg(1));
2596 docstring word = from_utf8(cmd.getArg(0));
2598 word = cur.selectionAsString(false);
2600 if (word.size() > 100 || word.empty()) {
2601 // Get word or selection
2602 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2603 word = cur.selectionAsString(false);
2606 WordLangTuple wl(word, language);
2607 theSpellChecker()->accept(wl);
2611 case LFUN_SPELLING_REMOVE: {
2612 Language const * language = getLanguage(cur, cmd.getArg(1));
2613 docstring word = from_utf8(cmd.getArg(0));
2615 word = cur.selectionAsString(false);
2617 if (word.size() > 100 || word.empty()) {
2618 // Get word or selection
2619 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2620 word = cur.selectionAsString(false);
2623 WordLangTuple wl(word, language);
2624 theSpellChecker()->remove(wl);
2628 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2629 // Given data, an encoding of the ParagraphParameters
2630 // generated in the Paragraph dialog, this function sets
2631 // the current paragraph, or currently selected paragraphs,
2633 // NOTE: This function overrides all existing settings.
2634 setParagraphs(cur, cmd.argument());
2635 cur.message(_("Paragraph layout set"));
2639 case LFUN_PARAGRAPH_PARAMS: {
2640 // Given data, an encoding of the ParagraphParameters as we'd
2641 // find them in a LyX file, this function modifies the current paragraph,
2642 // or currently selected paragraphs.
2643 // NOTE: This function only modifies, and does not override, existing
2645 setParagraphs(cur, cmd.argument(), true);
2646 cur.message(_("Paragraph layout set"));
2651 if (cur.selection()) {
2652 cur.selection(false);
2655 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2656 // correct, but I'm not 100% sure -- dov, 071019
2657 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2661 case LFUN_OUTLINE_UP:
2662 outline(OutlineUp, cur, this);
2663 setCursor(cur, cur.pit(), 0);
2664 cur.forceBufferUpdate();
2668 case LFUN_OUTLINE_DOWN:
2669 outline(OutlineDown, cur, this);
2670 setCursor(cur, cur.pit(), 0);
2671 cur.forceBufferUpdate();
2675 case LFUN_OUTLINE_IN:
2676 outline(OutlineIn, cur, this);
2677 cur.forceBufferUpdate();
2681 case LFUN_OUTLINE_OUT:
2682 outline(OutlineOut, cur, this);
2683 cur.forceBufferUpdate();
2687 case LFUN_SERVER_GET_STATISTICS:
2689 DocIterator from, to;
2690 if (cur.selection()) {
2691 from = cur.selectionBegin();
2692 to = cur.selectionEnd();
2694 from = doc_iterator_begin(cur.buffer());
2695 to = doc_iterator_end(cur.buffer());
2698 cur.buffer()->updateStatistics(from, to);
2699 string const arg0 = cmd.getArg(0);
2700 if (arg0 == "words") {
2701 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2702 } else if (arg0 == "chars") {
2703 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2704 } else if (arg0 == "chars-space") {
2705 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2707 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2708 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2709 + convert<docstring>(cur.buffer()->charCount(true)));
2715 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2720 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2722 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2723 // Check for misspelled text
2724 // The redraw is useful because of the painting of
2725 // misspelled markers depends on the cursor position.
2726 // Trigger a redraw for cursor moves inside misspelled text.
2727 if (!cur.inTexted()) {
2728 // move from regular text to math
2729 needsUpdate = last_misspelled;
2730 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2731 // move inside regular text
2732 needsUpdate = last_misspelled
2733 || cur.paragraph().isMisspelled(cur.pos(), true);
2737 // FIXME: The cursor flag is reset two lines below
2738 // so we need to check here if some of the LFUN did touch that.
2739 // for now only Text::erase() and Text::backspace() do that.
2740 // The plan is to verify all the LFUNs and then to remove this
2741 // singleParUpdate boolean altogether.
2742 if (cur.result().screenUpdate() & Update::Force) {
2743 singleParUpdate = false;
2747 // FIXME: the following code should go in favor of fine grained
2748 // update flag treatment.
2749 if (singleParUpdate) {
2750 // Inserting characters does not change par height in general. So, try
2751 // to update _only_ this paragraph. BufferView will detect if a full
2752 // metrics update is needed anyway.
2753 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2757 && &oldTopSlice.inset() == &cur.inset()
2758 && oldTopSlice.idx() == cur.idx()
2759 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2760 && !cur.selection())
2761 // FIXME: it would be better if we could just do this
2763 //if (cur.result().update() != Update::FitCursor)
2764 // cur.noScreenUpdate();
2766 // But some LFUNs do not set Update::FitCursor when needed, so we
2767 // do it for all. This is not very harmfull as FitCursor will provoke
2768 // a full redraw only if needed but still, a proper review of all LFUN
2769 // should be done and this needsUpdate boolean can then be removed.
2770 cur.screenUpdateFlags(Update::FitCursor);
2772 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2776 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2777 FuncStatus & flag) const
2779 LBUFERR(this == cur.text());
2781 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2783 bool allow_in_passthru = false;
2784 InsetCode code = NO_CODE;
2786 switch (cmd.action()) {
2788 case LFUN_DEPTH_DECREMENT:
2789 enable = changeDepthAllowed(cur, DEC_DEPTH);
2792 case LFUN_DEPTH_INCREMENT:
2793 enable = changeDepthAllowed(cur, INC_DEPTH);
2797 // FIXME We really should not allow this to be put, e.g.,
2798 // in a footnote, or in ERT. But it would make sense in a
2799 // branch, so I'm not sure what to do.
2800 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2803 case LFUN_DIALOG_SHOW_NEW_INSET:
2804 if (cmd.argument() == "bibitem")
2805 code = BIBITEM_CODE;
2806 else if (cmd.argument() == "bibtex") {
2808 // not allowed in description items
2809 enable = !inDescriptionItem(cur);
2811 else if (cmd.argument() == "box")
2813 else if (cmd.argument() == "branch")
2815 else if (cmd.argument() == "citation")
2817 else if (cmd.argument() == "ert")
2819 else if (cmd.argument() == "external")
2820 code = EXTERNAL_CODE;
2821 else if (cmd.argument() == "float")
2823 else if (cmd.argument() == "graphics")
2824 code = GRAPHICS_CODE;
2825 else if (cmd.argument() == "href")
2826 code = HYPERLINK_CODE;
2827 else if (cmd.argument() == "include")
2828 code = INCLUDE_CODE;
2829 else if (cmd.argument() == "index")
2831 else if (cmd.argument() == "index_print")
2832 code = INDEX_PRINT_CODE;
2833 else if (cmd.argument() == "listings")
2834 code = LISTINGS_CODE;
2835 else if (cmd.argument() == "mathspace")
2836 code = MATH_HULL_CODE;
2837 else if (cmd.argument() == "nomenclature")
2838 code = NOMENCL_CODE;
2839 else if (cmd.argument() == "nomencl_print")
2840 code = NOMENCL_PRINT_CODE;
2841 else if (cmd.argument() == "label")
2843 else if (cmd.argument() == "line")
2845 else if (cmd.argument() == "note")
2847 else if (cmd.argument() == "phantom")
2848 code = PHANTOM_CODE;
2849 else if (cmd.argument() == "ref")
2851 else if (cmd.argument() == "space")
2853 else if (cmd.argument() == "toc")
2855 else if (cmd.argument() == "vspace")
2857 else if (cmd.argument() == "wrap")
2861 case LFUN_ERT_INSERT:
2864 case LFUN_LISTING_INSERT:
2865 code = LISTINGS_CODE;
2866 // not allowed in description items
2867 enable = !inDescriptionItem(cur);
2869 case LFUN_FOOTNOTE_INSERT:
2872 case LFUN_TABULAR_INSERT:
2873 code = TABULAR_CODE;
2875 case LFUN_MARGINALNOTE_INSERT:
2878 case LFUN_FLOAT_INSERT:
2879 case LFUN_FLOAT_WIDE_INSERT:
2880 // FIXME: If there is a selection, we should check whether there
2881 // are floats in the selection, but this has performance issues, see
2882 // LFUN_CHANGE_ACCEPT/REJECT.
2884 if (inDescriptionItem(cur))
2885 // not allowed in description items
2888 InsetCode const inset_code = cur.inset().lyxCode();
2890 // algorithm floats cannot be put in another float
2891 if (to_utf8(cmd.argument()) == "algorithm") {
2892 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2896 // for figures and tables: only allow in another
2897 // float or wrap if it is of the same type and
2898 // not a subfloat already
2899 if(cur.inset().lyxCode() == code) {
2900 InsetFloat const & ins =
2901 static_cast<InsetFloat const &>(cur.inset());
2902 enable = ins.params().type == to_utf8(cmd.argument())
2903 && !ins.params().subfloat;
2904 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2905 InsetWrap const & ins =
2906 static_cast<InsetWrap const &>(cur.inset());
2907 enable = ins.params().type == to_utf8(cmd.argument());
2911 case LFUN_WRAP_INSERT:
2913 // not allowed in description items
2914 enable = !inDescriptionItem(cur);
2916 case LFUN_FLOAT_LIST_INSERT: {
2917 code = FLOAT_LIST_CODE;
2918 // not allowed in description items
2919 enable = !inDescriptionItem(cur);
2921 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2922 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2923 // make sure we know about such floats
2924 if (cit == floats.end() ||
2925 // and that we know how to generate a list of them
2926 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2927 flag.setUnknown(true);
2928 // probably not necessary, but...
2934 case LFUN_CAPTION_INSERT: {
2935 code = CAPTION_CODE;
2936 string arg = cmd.getArg(0);
2937 bool varia = arg != "Unnumbered"
2938 && cur.inset().allowsCaptionVariation(arg);
2939 // not allowed in description items,
2940 // and in specific insets
2941 enable = !inDescriptionItem(cur)
2942 && (varia || arg.empty() || arg == "Standard");
2945 case LFUN_NOTE_INSERT:
2948 case LFUN_FLEX_INSERT: {
2950 string s = cmd.getArg(0);
2952 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2953 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2954 il.lyxtype() != InsetLayout::CUSTOM &&
2955 il.lyxtype() != InsetLayout::ELEMENT &&
2956 il.lyxtype ()!= InsetLayout::STANDARD)
2960 case LFUN_BOX_INSERT:
2963 case LFUN_BRANCH_INSERT:
2965 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2966 && cur.buffer()->params().branchlist().empty())
2969 case LFUN_IPA_INSERT:
2972 case LFUN_PHANTOM_INSERT:
2973 code = PHANTOM_CODE;
2975 case LFUN_LABEL_INSERT:
2978 case LFUN_INFO_INSERT:
2980 enable = cmd.argument().empty()
2981 || infoparams.validateArgument(cur.buffer(), cmd.argument(), true);
2983 case LFUN_ARGUMENT_INSERT: {
2985 allow_in_passthru = true;
2986 string const arg = cmd.getArg(0);
2991 Layout const & lay = cur.paragraph().layout();
2992 Layout::LaTeXArgMap args = lay.args();
2993 Layout::LaTeXArgMap::const_iterator const lait =
2995 if (lait != args.end()) {
2997 pit_type pit = cur.pit();
2998 pit_type lastpit = cur.pit();
2999 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
3000 // In a sequence of "merged" environment layouts, we only allow
3001 // non-item arguments once.
3002 lastpit = cur.lastpit();
3003 // get the first paragraph in sequence with this layout
3004 depth_type const current_depth = cur.paragraph().params().depth();
3008 Paragraph cpar = pars_[pit - 1];
3009 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
3015 for (; pit <= lastpit; ++pit) {
3016 if (pars_[pit].layout() != lay)
3018 for (auto const & table : pars_[pit].insetList())
3019 if (InsetArgument const * ins = table.inset->asInsetArgument())
3020 if (ins->name() == arg) {
3021 // we have this already
3030 case LFUN_INDEX_INSERT:
3033 case LFUN_INDEX_PRINT:
3034 code = INDEX_PRINT_CODE;
3035 // not allowed in description items
3036 enable = !inDescriptionItem(cur);
3038 case LFUN_NOMENCL_INSERT:
3039 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3043 code = NOMENCL_CODE;
3045 case LFUN_NOMENCL_PRINT:
3046 code = NOMENCL_PRINT_CODE;
3047 // not allowed in description items
3048 enable = !inDescriptionItem(cur);
3050 case LFUN_HREF_INSERT:
3051 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3055 code = HYPERLINK_CODE;
3057 case LFUN_IPAMACRO_INSERT: {
3058 string const arg = cmd.getArg(0);
3060 code = IPADECO_CODE;
3062 code = IPACHAR_CODE;
3065 case LFUN_QUOTE_INSERT:
3066 // always allow this, since we will inset a raw quote
3067 // if an inset is not allowed.
3068 allow_in_passthru = true;
3070 case LFUN_SPECIALCHAR_INSERT:
3071 code = SPECIALCHAR_CODE;
3073 case LFUN_SPACE_INSERT:
3074 // slight hack: we know this is allowed in math mode
3078 case LFUN_PREVIEW_INSERT:
3079 code = PREVIEW_CODE;
3081 case LFUN_SCRIPT_INSERT:
3085 case LFUN_MATH_INSERT:
3086 case LFUN_MATH_AMS_MATRIX:
3087 case LFUN_MATH_MATRIX:
3088 case LFUN_MATH_DELIM:
3089 case LFUN_MATH_BIGDELIM:
3090 case LFUN_MATH_DISPLAY:
3091 case LFUN_MATH_MODE:
3092 case LFUN_MATH_MACRO:
3093 case LFUN_MATH_SUBSCRIPT:
3094 case LFUN_MATH_SUPERSCRIPT:
3095 code = MATH_HULL_CODE;
3098 case LFUN_REGEXP_MODE:
3099 code = MATH_HULL_CODE;
3100 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3103 case LFUN_INSET_MODIFY:
3104 // We need to disable this, because we may get called for a
3106 // InsetTabular::getStatus() -> InsetText::getStatus()
3107 // and we don't handle LFUN_INSET_MODIFY.
3111 case LFUN_FONT_EMPH:
3112 flag.setOnOff(fontinfo.emph() == FONT_ON);
3113 enable = !cur.paragraph().isPassThru();
3116 case LFUN_FONT_ITAL:
3117 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3118 enable = !cur.paragraph().isPassThru();
3121 case LFUN_FONT_NOUN:
3122 flag.setOnOff(fontinfo.noun() == FONT_ON);
3123 enable = !cur.paragraph().isPassThru();
3126 case LFUN_FONT_BOLD:
3127 case LFUN_FONT_BOLDSYMBOL:
3128 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3129 enable = !cur.paragraph().isPassThru();
3132 case LFUN_FONT_SANS:
3133 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3134 enable = !cur.paragraph().isPassThru();
3137 case LFUN_FONT_ROMAN:
3138 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3139 enable = !cur.paragraph().isPassThru();
3142 case LFUN_FONT_TYPEWRITER:
3143 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3144 enable = !cur.paragraph().isPassThru();
3149 enable = cur.selection();
3153 if (cmd.argument().empty()) {
3154 if (theClipboard().isInternal())
3155 enable = cap::numberOfSelections() > 0;
3157 enable = !theClipboard().empty();
3161 // we have an argument
3162 string const arg = to_utf8(cmd.argument());
3163 if (isStrUnsignedInt(arg)) {
3164 // it's a number and therefore means the internal stack
3165 unsigned int n = convert<unsigned int>(arg);
3166 enable = cap::numberOfSelections() > n;
3170 // explicit text type?
3171 if (arg == "html") {
3172 // Do not enable for PlainTextType, since some tidying in the
3173 // frontend is needed for HTML, which is too unsafe for plain text.
3174 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3176 } else if (arg == "latex") {
3177 // LaTeX is usually not available on the clipboard with
3178 // the correct MIME type, but in plain text.
3179 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3180 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3184 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3186 type = Clipboard::PdfGraphicsType;
3187 else if (arg == "png")
3188 type = Clipboard::PngGraphicsType;
3189 else if (arg == "jpeg")
3190 type = Clipboard::JpegGraphicsType;
3191 else if (arg == "linkback")
3192 type = Clipboard::LinkBackGraphicsType;
3193 else if (arg == "emf")
3194 type = Clipboard::EmfGraphicsType;
3195 else if (arg == "wmf")
3196 type = Clipboard::WmfGraphicsType;
3199 LYXERR0("Unrecognized graphics type: " << arg);
3200 // we don't want to assert if the user just mistyped the LFUN
3201 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3205 enable = theClipboard().hasGraphicsContents(type);
3209 case LFUN_CLIPBOARD_PASTE:
3210 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3211 enable = !theClipboard().empty();
3214 case LFUN_PRIMARY_SELECTION_PASTE:
3215 enable = cur.selection() || !theSelection().empty();
3218 case LFUN_SELECTION_PASTE:
3219 enable = cap::selection();
3222 case LFUN_PARAGRAPH_MOVE_UP:
3223 enable = cur.pit() > 0 && !cur.selection();
3226 case LFUN_PARAGRAPH_MOVE_DOWN:
3227 enable = cur.pit() < cur.lastpit() && !cur.selection();
3230 case LFUN_CHANGE_ACCEPT:
3231 case LFUN_CHANGE_REJECT:
3232 if (!cur.selection())
3233 enable = cur.paragraph().isChanged(cur.pos());
3235 // will enable if there is a change in the selection
3238 // cheap improvement for efficiency: using cached
3239 // buffer variable, if there is no change in the
3240 // document, no need to check further.
3241 if (!cur.buffer()->areChangesPresent())
3244 for (DocIterator it = cur.selectionBegin(); ; it.forwardPar()) {
3245 pos_type const beg = it.pos();
3247 bool const in_last_par = (it.pit() == cur.selectionEnd().pit() &&
3248 it.idx() == cur.selectionEnd().idx());
3250 end = cur.selectionEnd().pos();
3253 if (beg != end && it.paragraph().isChanged(beg, end)) {
3263 case LFUN_OUTLINE_UP:
3264 case LFUN_OUTLINE_DOWN:
3265 case LFUN_OUTLINE_IN:
3266 case LFUN_OUTLINE_OUT:
3267 // FIXME: LyX is not ready for outlining within inset.
3268 enable = isMainText()
3269 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3272 case LFUN_NEWLINE_INSERT:
3273 // LaTeX restrictions (labels or empty par)
3274 enable = !cur.paragraph().isPassThru()
3275 && cur.pos() > cur.paragraph().beginOfBody();
3278 case LFUN_SEPARATOR_INSERT:
3279 // Always enabled for now
3283 case LFUN_TAB_INSERT:
3284 case LFUN_TAB_DELETE:
3285 enable = cur.paragraph().isPassThru();
3288 case LFUN_GRAPHICS_SET_GROUP: {
3289 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3293 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3297 case LFUN_NEWPAGE_INSERT:
3298 // not allowed in description items
3299 code = NEWPAGE_CODE;
3300 enable = !inDescriptionItem(cur);
3304 enable = !cur.paragraph().isPassThru();
3305 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3308 case LFUN_PARAGRAPH_BREAK:
3309 enable = inset().allowMultiPar();
3312 case LFUN_SPELLING_ADD:
3313 case LFUN_SPELLING_IGNORE:
3314 case LFUN_SPELLING_REMOVE:
3315 enable = theSpellChecker() != NULL;
3316 if (enable && !cmd.getArg(1).empty()) {
3317 // validate explicitly given language
3318 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3319 enable &= lang != NULL;
3324 case LFUN_LAYOUT_TOGGLE: {
3325 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3326 docstring const req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3327 docstring const layout = resolveLayout(req_layout, cur);
3329 enable = !owner_->forcePlainLayout() && !layout.empty();
3330 flag.setOnOff(isAlreadyLayout(layout, cur));
3334 case LFUN_ENVIRONMENT_SPLIT: {
3335 if (cmd.argument() == "outer") {
3336 // check if we have an environment in our nesting hierarchy
3338 depth_type const current_depth = cur.paragraph().params().depth();
3339 pit_type pit = cur.pit();
3340 Paragraph cpar = pars_[pit];
3342 if (pit == 0 || cpar.params().depth() == 0)
3346 if (cpar.params().depth() < current_depth)
3347 res = cpar.layout().isEnvironment();
3352 else if (cmd.argument() == "previous") {
3353 // look if we have an environment in the previous par
3354 pit_type pit = cur.pit();
3355 Paragraph cpar = pars_[pit];
3359 enable = cpar.layout().isEnvironment();
3365 else if (cur.paragraph().layout().isEnvironment()) {
3366 enable = cmd.argument() == "before"
3367 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3374 case LFUN_LAYOUT_PARAGRAPH:
3375 case LFUN_PARAGRAPH_PARAMS:
3376 case LFUN_PARAGRAPH_PARAMS_APPLY:
3377 case LFUN_PARAGRAPH_UPDATE:
3378 enable = owner_->allowParagraphCustomization();
3381 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3382 // Because they insert COMBINING DIACRITICAL Unicode characters,
3383 // that cannot be handled by LaTeX but must be converted according
3384 // to the definition in lib/unicodesymbols?
3385 case LFUN_ACCENT_ACUTE:
3386 case LFUN_ACCENT_BREVE:
3387 case LFUN_ACCENT_CARON:
3388 case LFUN_ACCENT_CEDILLA:
3389 case LFUN_ACCENT_CIRCLE:
3390 case LFUN_ACCENT_CIRCUMFLEX:
3391 case LFUN_ACCENT_DOT:
3392 case LFUN_ACCENT_GRAVE:
3393 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3394 case LFUN_ACCENT_MACRON:
3395 case LFUN_ACCENT_OGONEK:
3396 case LFUN_ACCENT_TIE:
3397 case LFUN_ACCENT_TILDE:
3398 case LFUN_ACCENT_PERISPOMENI:
3399 case LFUN_ACCENT_UMLAUT:
3400 case LFUN_ACCENT_UNDERBAR:
3401 case LFUN_ACCENT_UNDERDOT:
3402 case LFUN_FONT_FRAK:
3403 case LFUN_FONT_SIZE:
3404 case LFUN_FONT_STATE:
3405 case LFUN_FONT_UNDERLINE:
3406 case LFUN_FONT_STRIKEOUT:
3407 case LFUN_FONT_CROSSOUT:
3408 case LFUN_FONT_UNDERUNDERLINE:
3409 case LFUN_FONT_UNDERWAVE:
3410 case LFUN_TEXTSTYLE_UPDATE:
3411 enable = !cur.paragraph().isPassThru();
3414 case LFUN_FONT_DEFAULT: {
3415 Font font(inherit_font, ignore_language);
3416 BufferParams const & bp = cur.buffer()->masterParams();
3417 if (cur.selection()) {
3419 // Check if we have a non-default font attribute
3420 // in the selection range.
3421 DocIterator const from = cur.selectionBegin();
3422 DocIterator const to = cur.selectionEnd();
3423 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
3424 if (!dit.inTexted()) {
3428 Paragraph const & par = dit.paragraph();
3429 pos_type const pos = dit.pos();
3430 Font tmp = par.getFontSettings(bp, pos);
3431 if (tmp.fontInfo() != font.fontInfo()
3432 || tmp.language() != bp.language) {
3440 // Disable if all is default already.
3441 enable = (cur.current_font.fontInfo() != font.fontInfo()
3442 || cur.current_font.language() != bp.language);
3446 case LFUN_TEXTSTYLE_APPLY:
3447 enable = !freeFonts.empty();
3450 case LFUN_WORD_DELETE_FORWARD:
3451 case LFUN_WORD_DELETE_BACKWARD:
3452 case LFUN_LINE_DELETE_FORWARD:
3453 case LFUN_WORD_FORWARD:
3454 case LFUN_WORD_BACKWARD:
3455 case LFUN_WORD_RIGHT:
3456 case LFUN_WORD_LEFT:
3457 case LFUN_CHAR_FORWARD:
3458 case LFUN_CHAR_FORWARD_SELECT:
3459 case LFUN_CHAR_BACKWARD:
3460 case LFUN_CHAR_BACKWARD_SELECT:
3461 case LFUN_CHAR_LEFT:
3462 case LFUN_CHAR_LEFT_SELECT:
3463 case LFUN_CHAR_RIGHT:
3464 case LFUN_CHAR_RIGHT_SELECT:
3466 case LFUN_UP_SELECT:
3468 case LFUN_DOWN_SELECT:
3469 case LFUN_PARAGRAPH_UP_SELECT:
3470 case LFUN_PARAGRAPH_DOWN_SELECT:
3471 case LFUN_LINE_BEGIN_SELECT:
3472 case LFUN_LINE_END_SELECT:
3473 case LFUN_WORD_FORWARD_SELECT:
3474 case LFUN_WORD_BACKWARD_SELECT:
3475 case LFUN_WORD_RIGHT_SELECT:
3476 case LFUN_WORD_LEFT_SELECT:
3477 case LFUN_WORD_SELECT:
3478 case LFUN_SECTION_SELECT:
3479 case LFUN_BUFFER_BEGIN:
3480 case LFUN_BUFFER_END:
3481 case LFUN_BUFFER_BEGIN_SELECT:
3482 case LFUN_BUFFER_END_SELECT:
3483 case LFUN_INSET_BEGIN:
3484 case LFUN_INSET_END:
3485 case LFUN_INSET_BEGIN_SELECT:
3486 case LFUN_INSET_END_SELECT:
3487 case LFUN_PARAGRAPH_UP:
3488 case LFUN_PARAGRAPH_DOWN:
3489 case LFUN_LINE_BEGIN:
3491 case LFUN_CHAR_DELETE_FORWARD:
3492 case LFUN_CHAR_DELETE_BACKWARD:
3493 case LFUN_WORD_UPCASE:
3494 case LFUN_WORD_LOWCASE:
3495 case LFUN_WORD_CAPITALIZE:
3496 case LFUN_CHARS_TRANSPOSE:
3497 case LFUN_SERVER_GET_XY:
3498 case LFUN_SERVER_SET_XY:
3499 case LFUN_SERVER_GET_LAYOUT:
3500 case LFUN_SELF_INSERT:
3501 case LFUN_UNICODE_INSERT:
3502 case LFUN_THESAURUS_ENTRY:
3504 case LFUN_SERVER_GET_STATISTICS:
3505 // these are handled in our dispatch()
3509 case LFUN_INSET_INSERT: {
3510 string const type = cmd.getArg(0);
3511 if (type == "toc") {
3513 // not allowed in description items
3514 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3515 enable = !inDescriptionItem(cur);
3522 case LFUN_SEARCH_IGNORE: {
3523 bool const value = cmd.getArg(1) == "true";
3524 setIgnoreFormat(cmd.getArg(0), value);
3534 || !cur.inset().insetAllowed(code)
3535 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3538 flag.setEnabled(enable);
3543 void Text::pasteString(Cursor & cur, docstring const & clip,
3546 if (!clip.empty()) {
3549 insertStringAsParagraphs(cur, clip, cur.current_font);
3551 insertStringAsLines(cur, clip, cur.current_font);
3556 // FIXME: an item inset would make things much easier.
3557 bool Text::inDescriptionItem(Cursor & cur) const
3559 Paragraph & par = cur.paragraph();
3560 pos_type const pos = cur.pos();
3561 pos_type const body_pos = par.beginOfBody();
3563 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3564 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3565 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3568 return (pos < body_pos
3570 && (pos == 0 || par.getChar(pos - 1) != ' ')));
3574 std::vector<docstring> Text::getFreeFonts() const
3576 vector<docstring> ffList;
3578 FontStack::const_iterator cit = freeFonts.begin();
3579 FontStack::const_iterator end = freeFonts.end();
3580 for (; cit != end; ++cit)
3581 // we do not use cit-> here because gcc 2.9x does not
3583 ffList.push_back((*cit).first);