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/filetools.h"
74 #include "support/gettext.h"
75 #include "support/lassert.h"
76 #include "support/limited_stack.h"
77 #include "support/lstrings.h"
78 #include "support/lyxalgo.h"
79 #include "support/lyxtime.h"
80 #include "support/os.h"
81 #include "support/regex.h"
83 #include "mathed/InsetMathHull.h"
84 #include "mathed/InsetMathMacroTemplate.h"
91 using namespace lyx::support;
95 using cap::copySelection;
96 using cap::copySelectionToTemp;
97 using cap::cutSelection;
98 using cap::cutSelectionToTemp;
99 using cap::pasteFromStack;
100 using cap::pasteFromTemp;
101 using cap::pasteClipboardText;
102 using cap::pasteClipboardGraphics;
103 using cap::replaceSelection;
104 using cap::grabAndEraseSelection;
105 using cap::selClearOrDel;
106 using cap::pasteSimpleText;
107 using frontend::Clipboard;
110 typedef limited_stack<pair<docstring, Font>> FontStack;
111 static FontStack freeFonts(15);
112 static bool toggleall = false;
114 static void toggleAndShow(Cursor & cur, Text * text,
115 Font const & font, bool togall = true)
117 text->toggleFree(cur, font, togall);
119 if (font.language() != ignore_language ||
120 font.fontInfo().number() != FONT_IGNORE) {
121 TextMetrics const & tm = cur.bv().textMetrics(text);
122 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
123 cur.real_current_font))
124 text->setCursor(cur, cur.pit(), cur.pos(),
125 false, !cur.boundary());
130 static void moveCursor(Cursor & cur, bool selecting)
132 if (selecting || cur.mark())
137 static void finishChange(Cursor & cur, bool selecting)
140 moveCursor(cur, selecting);
144 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
147 docstring sel = cur.selectionAsString(false);
149 // It may happen that sel is empty but there is a selection
150 replaceSelection(cur);
152 // Is this a valid formula?
156 #ifdef ENABLE_ASSERTIONS
157 const int old_pos = cur.pos();
159 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
160 #ifdef ENABLE_ASSERTIONS
161 LATTEST(old_pos == cur.pos());
163 cur.nextInset()->edit(cur, true);
164 if (cmd.action() != LFUN_MATH_MODE)
165 // LFUN_MATH_MODE has a different meaning in math mode
168 InsetMathHull * formula = new InsetMathHull(cur.buffer());
169 string const selstr = to_utf8(sel);
170 istringstream is(selstr);
173 if (!formula->readQuiet(lex)) {
174 // No valid formula, let's try with delims
175 is.str("$" + selstr + "$");
177 if (!formula->readQuiet(lex)) {
178 // Still not valid, leave it as is
186 cur.nextInset()->edit(cur, true);
187 LASSERT(cur.inMathed(), return);
191 cur.pos() = cur.lastpos();
192 if (cmd.action() != LFUN_MATH_MODE)
193 // LFUN_MATH_MODE has a different meaning in math mode
195 cur.clearSelection();
196 cur.pos() = cur.lastpos();
200 cur.message(from_utf8(N_("Math editor mode")));
202 cur.message(from_utf8(N_("No valid math formula")));
206 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
208 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
209 if (cur.inRegexped()) {
210 cur.message(_("Already in regular expression mode"));
214 docstring sel = cur.selectionAsString(false);
216 // It may happen that sel is empty but there is a selection
217 replaceSelection(cur);
219 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
220 cur.nextInset()->edit(cur, true);
223 cur.message(_("Regexp editor mode"));
227 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
230 cap::replaceSelection(cur);
231 cur.insert(new InsetSpecialChar(kind));
236 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
239 cap::replaceSelection(cur);
240 cur.insert(new InsetIPAChar(kind));
245 static bool doInsertInset(Cursor & cur, Text * text,
246 FuncRequest const & cmd, bool edit, bool pastesel)
248 Buffer & buffer = cur.bv().buffer();
249 BufferParams const & bparams = buffer.params();
250 Inset * inset = createInset(&buffer, cmd);
254 if (InsetCollapsible * ci = inset->asInsetCollapsible())
255 ci->setButtonLabel();
258 if (cmd.action() == LFUN_ARGUMENT_INSERT) {
259 bool cotextinsert = false;
260 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
261 Layout const & lay = cur.paragraph().layout();
262 Layout::LaTeXArgMap args = lay.args();
263 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
264 if (lait != args.end())
265 cotextinsert = (*lait).second.insertcotext;
267 InsetLayout const & il = cur.inset().getLayout();
269 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
270 if (ilait != args.end())
271 cotextinsert = (*ilait).second.insertcotext;
273 // The argument requests to insert a copy of the co-text to the inset
276 // If we have a selection within a paragraph, use this
277 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
278 ds = cur.selectionAsString(false);
279 // else use the whole paragraph
281 ds = cur.paragraph().asString();
282 text->insertInset(cur, inset);
284 inset->edit(cur, true);
285 // Now put co-text into inset
286 Font const f(inherit_font, cur.current_font.language());
288 cur.text()->insertStringAsLines(cur, ds, f);
289 cur.leaveInset(*inset);
296 if (cur.selection()) {
297 if (cmd.action() == LFUN_INDEX_INSERT)
298 copySelectionToTemp(cur);
300 cutSelectionToTemp(cur, pastesel);
301 /* Move layout information inside the inset if the whole
302 * paragraph and the inset allows setting layout
303 * FIXME: this does not work as expected when change tracking is on
304 * However, we do not really know what to do in this case.
306 if (cur.paragraph().empty() && !inset->forcePlainLayout())
307 cur.paragraph().setPlainOrDefaultLayout(bparams.documentClass());
309 cur.clearSelection();
311 } else if (cmd.action() == LFUN_INDEX_INSERT) {
312 gotsel = text->selectWordWhenUnderCursor(cur, WHOLE_WORD);
313 copySelectionToTemp(cur);
314 cur.clearSelection();
316 text->insertInset(cur, inset);
319 inset->edit(cur, true);
321 if (!gotsel || !pastesel)
324 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
325 cur.buffer()->errors("Paste");
326 cur.clearSelection(); // bug 393
328 InsetText * inset_text = inset->asInsetText();
330 inset_text->fixParagraphsFont();
333 // FIXME: what does this do?
334 if (cmd.action() == LFUN_FLEX_INSERT)
337 cur.leaveInset(*inset);
338 if (cmd.action() == LFUN_PREVIEW_INSERT
339 || cmd.action() == LFUN_IPA_INSERT)
341 notifyCursorLeavesOrEnters(old, cur);
343 cur.leaveInset(*inset);
344 // reset surrounding par to default
345 DocumentClass const & dc = bparams.documentClass();
346 docstring const layoutname = inset->usePlainLayout()
347 ? dc.plainLayoutName()
348 : dc.defaultLayoutName();
349 text->setLayout(cur, layoutname);
355 /// the type of outline operation
357 OutlineUp, // Move this header with text down
358 OutlineDown, // Move this header with text up
359 OutlineIn, // Make this header deeper
360 OutlineOut // Make this header shallower
364 static void insertSeparator(Cursor & cur, depth_type const depth)
366 Buffer & buf = *cur.buffer();
367 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
368 DocumentClass const & tc = buf.params().documentClass();
369 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
370 + from_ascii("\" ignoreautonests")));
371 // FIXME: Bibitem mess!
372 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
373 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
374 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
375 while (cur.paragraph().params().depth() > depth)
376 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
380 static void outline(OutlineOp mode, Cursor & cur, Text * text)
382 Buffer & buf = *cur.buffer();
383 pit_type & pit = cur.pit();
384 ParagraphList & pars = buf.text().paragraphs();
385 ParagraphList::iterator const bgn = pars.begin();
386 // The first paragraph of the area to be copied:
387 ParagraphList::iterator start = lyx::next(bgn, pit);
388 // The final paragraph of area to be copied:
389 ParagraphList::iterator finish = start;
390 ParagraphList::iterator const end = pars.end();
391 depth_type const current_depth = cur.paragraph().params().depth();
393 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
396 // Move out (down) from this section header
400 // Seek the one (on same level) below
401 for (; finish != end; ++finish) {
402 toclevel = buf.text().getTocLevel(distance(bgn, finish));
403 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
409 if (start == pars.begin())
412 ParagraphList::iterator dest = start;
413 // Move out (up) from this header
416 // Search previous same-level header above
419 toclevel = buf.text().getTocLevel(distance(bgn, dest));
421 && (toclevel == Layout::NOT_IN_TOC
422 || toclevel > thistoclevel));
423 // Not found; do nothing
424 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
426 pit_type newpit = distance(bgn, dest);
427 pit_type const len = distance(start, finish);
428 pit_type const deletepit = pit + len;
429 buf.undo().recordUndo(cur, newpit, deletepit - 1);
430 // If we move an environment upwards, make sure it is
431 // separated from its new neighbour below:
432 // If an environment of the same layout follows, and the moved
433 // paragraph sequence does not end with a separator, insert one.
434 ParagraphList::iterator lastmoved = finish;
436 if (start->layout().isEnvironment()
437 && dest->layout() == start->layout()
438 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
439 cur.pit() = distance(bgn, lastmoved);
440 cur.pos() = cur.lastpos();
441 insertSeparator(cur, current_depth);
444 // Likewise, if we moved an environment upwards, make sure it
445 // is separated from its new neighbour above.
446 // The paragraph before the target of movement
448 ParagraphList::iterator before = dest;
450 // Get the parent paragraph (outer in nested context)
451 pit_type const parent =
452 before->params().depth() > current_depth
453 ? text->depthHook(distance(bgn, before), current_depth)
454 : distance(bgn, before);
455 // If a environment with same layout preceeds the moved one in the new
456 // position, and there is no separator yet, insert one.
457 if (start->layout().isEnvironment()
458 && pars[parent].layout() == start->layout()
459 && !before->isEnvSeparator(before->beginOfBody())) {
460 cur.pit() = distance(bgn, before);
461 cur.pos() = cur.lastpos();
462 insertSeparator(cur, current_depth);
466 newpit = distance(bgn, dest);
467 pars.splice(dest, start, finish);
475 // Go one down from *this* header:
476 ParagraphList::iterator dest = next(finish, 1);
477 // Go further down to find header to insert in front of:
478 for (; dest != end; ++dest) {
479 toclevel = buf.text().getTocLevel(distance(bgn, dest));
480 if (toclevel != Layout::NOT_IN_TOC
481 && toclevel <= thistoclevel)
484 // One such was found, so go on...
485 // If we move an environment downwards, make sure it is
486 // separated from its new neighbour above.
487 pit_type newpit = distance(bgn, dest);
488 buf.undo().recordUndo(cur, pit, newpit - 1);
489 // The paragraph before the target of movement
490 ParagraphList::iterator before = dest;
492 // Get the parent paragraph (outer in nested context)
493 pit_type const parent =
494 before->params().depth() > current_depth
495 ? text->depthHook(distance(bgn, before), current_depth)
496 : distance(bgn, before);
497 // If a environment with same layout preceeds the moved one in the new
498 // position, and there is no separator yet, insert one.
499 if (start->layout().isEnvironment()
500 && pars[parent].layout() == start->layout()
501 && !before->isEnvSeparator(before->beginOfBody())) {
502 cur.pit() = distance(bgn, before);
503 cur.pos() = cur.lastpos();
504 insertSeparator(cur, current_depth);
507 // Likewise, make sure moved environments are separated
508 // from their new neighbour below:
509 // If an environment of the same layout follows, and the moved
510 // paragraph sequence does not end with a separator, insert one.
511 ParagraphList::iterator lastmoved = finish;
514 && start->layout().isEnvironment()
515 && dest->layout() == start->layout()
516 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
517 cur.pit() = distance(bgn, lastmoved);
518 cur.pos() = cur.lastpos();
519 insertSeparator(cur, current_depth);
522 newpit = distance(bgn, dest);
523 pit_type const len = distance(start, finish);
524 pars.splice(dest, start, finish);
525 cur.pit() = newpit - len;
530 pit_type const len = distance(start, finish);
531 buf.undo().recordUndo(cur, pit, pit + len - 1);
532 for (; start != finish; ++start) {
533 toclevel = buf.text().getTocLevel(distance(bgn, start));
534 if (toclevel == Layout::NOT_IN_TOC)
537 DocumentClass const & tc = buf.params().documentClass();
538 DocumentClass::const_iterator lit = tc.begin();
539 DocumentClass::const_iterator len = tc.end();
540 int const newtoclevel =
541 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
542 LabelType const oldlabeltype = start->layout().labeltype;
544 for (; lit != len; ++lit) {
545 if (lit->toclevel == newtoclevel &&
546 lit->labeltype == oldlabeltype) {
547 start->setLayout(*lit);
558 void Text::number(Cursor & cur)
560 FontInfo font = ignore_font;
561 font.setNumber(FONT_TOGGLE);
562 toggleAndShow(cur, this, Font(font, ignore_language));
566 bool Text::isRTL(pit_type const pit) const
568 Buffer const & buffer = owner_->buffer();
569 return pars_[pit].isRTL(buffer.params());
575 Language const * getLanguage(Cursor const & cur, string const & lang)
577 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
581 docstring resolveLayout(docstring layout, DocIterator const & dit)
583 Paragraph const & par = dit.paragraph();
584 DocumentClass const & tclass = dit.buffer()->params().documentClass();
587 layout = tclass.defaultLayoutName();
589 if (dit.inset().forcePlainLayout(dit.idx()))
590 // in this case only the empty layout is allowed
591 layout = tclass.plainLayoutName();
592 else if (par.usePlainLayout()) {
593 // in this case, default layout maps to empty layout
594 if (layout == tclass.defaultLayoutName())
595 layout = tclass.plainLayoutName();
597 // otherwise, the empty layout maps to the default
598 if (layout == tclass.plainLayoutName())
599 layout = tclass.defaultLayoutName();
602 // If the entry is obsolete, use the new one instead.
603 if (tclass.hasLayout(layout)) {
604 docstring const & obs = tclass[layout].obsoleted_by();
608 if (!tclass.hasLayout(layout))
614 bool isAlreadyLayout(docstring const & layout, CursorData const & cur)
616 ParagraphList const & pars = cur.text()->paragraphs();
618 pit_type pit = cur.selBegin().pit();
619 pit_type const epit = cur.selEnd().pit() + 1;
620 for ( ; pit != epit; ++pit)
621 if (pars[pit].layout().name() != layout)
631 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
633 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
635 // Dispatch if the cursor is inside the text. It is not the
636 // case for context menus (bug 5797).
637 if (cur.text() != this) {
642 BufferView * bv = &cur.bv();
643 TextMetrics * tm = &bv->textMetrics(this);
644 if (!tm->contains(cur.pit())) {
645 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
646 tm = &bv->textMetrics(this);
649 // FIXME: We use the update flag to indicates wether a singlePar or a
650 // full screen update is needed. We reset it here but shall we restore it
652 cur.noScreenUpdate();
654 LBUFERR(this == cur.text());
656 // NOTE: This should NOT be a reference. See commit 94a5481a.
657 CursorSlice const oldTopSlice = cur.top();
658 bool const oldBoundary = cur.boundary();
659 bool const oldSelection = cur.selection();
660 // Signals that, even if needsUpdate == false, an update of the
661 // cursor paragraph is required
662 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
663 LyXAction::SingleParUpdate);
664 // Signals that a full-screen update is required
665 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
666 LyXAction::NoUpdate) || singleParUpdate);
667 bool const last_misspelled = lyxrc.spellcheck_continuously
668 && cur.paragraph().isMisspelled(cur.pos(), true);
670 FuncCode const act = cmd.action();
673 case LFUN_PARAGRAPH_MOVE_DOWN: {
674 pit_type const pit = cur.pit();
675 cur.recordUndo(pit, pit + 1);
677 pars_.swap(pit, pit + 1);
679 cur.forceBufferUpdate();
684 case LFUN_PARAGRAPH_MOVE_UP: {
685 pit_type const pit = cur.pit();
686 cur.recordUndo(pit - 1, pit);
688 pars_.swap(pit, pit - 1);
691 cur.forceBufferUpdate();
695 case LFUN_APPENDIX: {
696 Paragraph & par = cur.paragraph();
697 bool start = !par.params().startOfAppendix();
699 // FIXME: The code below only makes sense at top level.
700 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
701 // ensure that we have only one start_of_appendix in this document
702 // FIXME: this don't work for multipart document!
703 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
704 if (pars_[tmp].params().startOfAppendix()) {
705 cur.recordUndo(tmp, tmp);
706 pars_[tmp].params().startOfAppendix(false);
712 par.params().startOfAppendix(start);
714 // we can set the refreshing parameters now
715 cur.forceBufferUpdate();
719 case LFUN_WORD_DELETE_FORWARD:
721 cutSelection(cur, false);
723 deleteWordForward(cur, cmd.getArg(0) == "force");
724 finishChange(cur, false);
727 case LFUN_WORD_DELETE_BACKWARD:
729 cutSelection(cur, false);
731 deleteWordBackward(cur, cmd.getArg(0) == "force");
732 finishChange(cur, false);
735 case LFUN_LINE_DELETE_FORWARD:
737 cutSelection(cur, false);
739 tm->deleteLineForward(cur);
740 finishChange(cur, false);
743 case LFUN_BUFFER_BEGIN:
744 case LFUN_BUFFER_BEGIN_SELECT:
745 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
746 if (cur.depth() == 1)
747 needsUpdate |= cursorTop(cur);
750 cur.screenUpdateFlags(Update::FitCursor);
753 case LFUN_BUFFER_END:
754 case LFUN_BUFFER_END_SELECT:
755 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
756 if (cur.depth() == 1)
757 needsUpdate |= cursorBottom(cur);
760 cur.screenUpdateFlags(Update::FitCursor);
763 case LFUN_INSET_BEGIN:
764 case LFUN_INSET_BEGIN_SELECT:
765 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
766 if (cur.depth() == 1 || !cur.top().at_begin())
767 needsUpdate |= cursorTop(cur);
770 cur.screenUpdateFlags(Update::FitCursor);
774 case LFUN_INSET_END_SELECT:
775 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
776 if (cur.depth() == 1 || !cur.top().at_end())
777 needsUpdate |= cursorBottom(cur);
780 cur.screenUpdateFlags(Update::FitCursor);
783 case LFUN_CHAR_FORWARD:
784 case LFUN_CHAR_FORWARD_SELECT: {
785 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
786 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
787 bool const cur_moved = cursorForward(cur);
788 needsUpdate |= cur_moved;
790 if (!cur_moved && oldTopSlice == cur.top()
791 && cur.boundary() == oldBoundary) {
793 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
795 // we will probably be moving out the inset, so we should execute
796 // the depm-mechanism, but only when the cursor has a place to
797 // go outside this inset, i.e. in a slice above.
798 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
799 && cur.pit() == cur.lastpit()) {
800 // The cursor hasn't changed yet. To give the
801 // DEPM the possibility of doing something we must
802 // provide it with two different cursors.
804 dummy.pos() = dummy.pit() = 0;
805 if (cur.bv().checkDepm(dummy, cur))
806 cur.forceBufferUpdate();
812 case LFUN_CHAR_BACKWARD:
813 case LFUN_CHAR_BACKWARD_SELECT: {
814 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
815 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
816 bool const cur_moved = cursorBackward(cur);
817 needsUpdate |= cur_moved;
819 if (!cur_moved && oldTopSlice == cur.top()
820 && cur.boundary() == oldBoundary) {
822 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
824 // we will probably be moving out the inset, so we should execute
825 // the depm-mechanism, but only when the cursor has a place to
826 // go outside this inset, i.e. in a slice above.
827 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
828 // The cursor hasn't changed yet. To give the
829 // DEPM the possibility of doing something we must
830 // provide it with two different cursors.
832 dummy.pos() = cur.lastpos();
833 dummy.pit() = cur.lastpit();
834 if (cur.bv().checkDepm(dummy, cur))
835 cur.forceBufferUpdate();
842 case LFUN_CHAR_LEFT_SELECT:
843 if (lyxrc.visual_cursor) {
844 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
845 bool const cur_moved = cursorVisLeft(cur);
846 needsUpdate |= cur_moved;
847 if (!cur_moved && oldTopSlice == cur.top()
848 && cur.boundary() == oldBoundary) {
850 cmd = FuncRequest(LFUN_FINISHED_LEFT);
853 if (cur.reverseDirectionNeeded()) {
854 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
855 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
857 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
858 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
865 case LFUN_CHAR_RIGHT:
866 case LFUN_CHAR_RIGHT_SELECT:
867 if (lyxrc.visual_cursor) {
868 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
869 bool const cur_moved = cursorVisRight(cur);
870 needsUpdate |= cur_moved;
871 if (!cur_moved && oldTopSlice == cur.top()
872 && cur.boundary() == oldBoundary) {
874 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
877 if (cur.reverseDirectionNeeded()) {
878 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
879 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
881 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
882 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
891 case LFUN_DOWN_SELECT:
894 // stop/start the selection
895 bool select = cmd.action() == LFUN_DOWN_SELECT ||
896 cmd.action() == LFUN_UP_SELECT;
898 // move cursor up/down
899 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
900 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
902 if (!atFirstOrLastRow) {
903 needsUpdate |= cur.selHandle(select);
904 cur.upDownInText(up, needsUpdate);
905 needsUpdate |= cur.beforeDispatchCursor().inMathed();
907 pos_type newpos = up ? 0 : cur.lastpos();
908 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
909 needsUpdate |= cur.selHandle(select);
910 // we do not reset the targetx of the cursor
912 needsUpdate |= bv->checkDepm(cur, bv->cursor());
913 cur.updateTextTargetOffset();
915 cur.forceBufferUpdate();
919 // if the cursor cannot be moved up or down do not remove
920 // the selection right now, but wait for the next dispatch.
922 needsUpdate |= cur.selHandle(select);
923 cur.upDownInText(up, needsUpdate);
930 case LFUN_PARAGRAPH_SELECT:
932 needsUpdate |= setCursor(cur, cur.pit(), 0);
933 needsUpdate |= cur.selHandle(true);
934 if (cur.pos() < cur.lastpos())
935 needsUpdate |= setCursor(cur, cur.pit(), cur.lastpos());
938 case LFUN_PARAGRAPH_UP:
939 case LFUN_PARAGRAPH_UP_SELECT:
940 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
941 needsUpdate |= cursorUpParagraph(cur);
944 case LFUN_PARAGRAPH_DOWN:
945 case LFUN_PARAGRAPH_DOWN_SELECT:
946 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
947 needsUpdate |= cursorDownParagraph(cur);
950 case LFUN_LINE_BEGIN:
951 case LFUN_LINE_BEGIN_SELECT:
952 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
953 needsUpdate |= tm->cursorHome(cur);
957 case LFUN_LINE_END_SELECT:
958 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
959 needsUpdate |= tm->cursorEnd(cur);
962 case LFUN_SECTION_SELECT: {
963 Buffer const & buf = *cur.buffer();
964 pit_type const pit = cur.pit();
965 ParagraphList & pars = buf.text().paragraphs();
966 ParagraphList::iterator bgn = pars.begin();
967 // The first paragraph of the area to be selected:
968 ParagraphList::iterator start = lyx::next(bgn, pit);
969 // The final paragraph of area to be selected:
970 ParagraphList::iterator finish = start;
971 ParagraphList::iterator end = pars.end();
973 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
974 if (thistoclevel == Layout::NOT_IN_TOC)
978 Cursor const old_cur = cur;
979 needsUpdate |= cur.selHandle(true);
981 // Move out (down) from this section header
985 // Seek the one (on same level) below
986 for (; finish != end; ++finish, ++cur.pit()) {
987 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
988 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
991 cur.pos() = cur.lastpos();
993 cur.setCurrentFont();
995 needsUpdate |= cur != old_cur;
999 case LFUN_WORD_RIGHT:
1000 case LFUN_WORD_RIGHT_SELECT:
1001 if (lyxrc.visual_cursor) {
1002 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
1003 bool const cur_moved = cursorVisRightOneWord(cur);
1004 needsUpdate |= cur_moved;
1005 if (!cur_moved && oldTopSlice == cur.top()
1006 && cur.boundary() == oldBoundary) {
1008 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
1011 if (cur.reverseDirectionNeeded()) {
1012 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1013 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1015 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
1016 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1023 case LFUN_WORD_FORWARD:
1024 case LFUN_WORD_FORWARD_SELECT: {
1025 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
1026 bool const cur_moved = cursorForwardOneWord(cur);
1027 needsUpdate |= cur_moved;
1029 if (!cur_moved && oldTopSlice == cur.top()
1030 && cur.boundary() == oldBoundary) {
1032 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1034 // we will probably be moving out the inset, so we should execute
1035 // the depm-mechanism, but only when the cursor has a place to
1036 // go outside this inset, i.e. in a slice above.
1037 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
1038 && cur.pit() == cur.lastpit()) {
1039 // The cursor hasn't changed yet. To give the
1040 // DEPM the possibility of doing something we must
1041 // provide it with two different cursors.
1043 dummy.pos() = dummy.pit() = 0;
1044 if (cur.bv().checkDepm(dummy, cur))
1045 cur.forceBufferUpdate();
1051 case LFUN_WORD_LEFT:
1052 case LFUN_WORD_LEFT_SELECT:
1053 if (lyxrc.visual_cursor) {
1054 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
1055 bool const cur_moved = cursorVisLeftOneWord(cur);
1056 needsUpdate |= cur_moved;
1057 if (!cur_moved && oldTopSlice == cur.top()
1058 && cur.boundary() == oldBoundary) {
1060 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1063 if (cur.reverseDirectionNeeded()) {
1064 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1065 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1067 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1068 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1075 case LFUN_WORD_BACKWARD:
1076 case LFUN_WORD_BACKWARD_SELECT: {
1077 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1078 bool const cur_moved = cursorBackwardOneWord(cur);
1079 needsUpdate |= cur_moved;
1081 if (!cur_moved && oldTopSlice == cur.top()
1082 && cur.boundary() == oldBoundary) {
1084 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1086 // we will probably be moving out the inset, so we should execute
1087 // the depm-mechanism, but only when the cursor has a place to
1088 // go outside this inset, i.e. in a slice above.
1089 if (cur.depth() > 1 && cur.pos() == 0
1090 && cur.pit() == 0) {
1091 // The cursor hasn't changed yet. To give the
1092 // DEPM the possibility of doing something we must
1093 // provide it with two different cursors.
1095 dummy.pos() = cur.lastpos();
1096 dummy.pit() = cur.lastpit();
1097 if (cur.bv().checkDepm(dummy, cur))
1098 cur.forceBufferUpdate();
1104 case LFUN_WORD_SELECT: {
1105 selectWord(cur, WHOLE_WORD);
1106 finishChange(cur, true);
1110 case LFUN_NEWLINE_INSERT: {
1111 InsetNewlineParams inp;
1112 docstring arg = cmd.argument();
1113 if (arg == "linebreak")
1114 inp.kind = InsetNewlineParams::LINEBREAK;
1116 inp.kind = InsetNewlineParams::NEWLINE;
1117 cap::replaceSelection(cur);
1119 cur.insert(new InsetNewline(inp));
1121 moveCursor(cur, false);
1125 case LFUN_TAB_INSERT: {
1126 bool const multi_par_selection = cur.selection() &&
1127 cur.selBegin().pit() != cur.selEnd().pit();
1128 if (multi_par_selection) {
1129 // If there is a multi-paragraph selection, a tab is inserted
1130 // at the beginning of each paragraph.
1131 cur.recordUndoSelection();
1132 pit_type const pit_end = cur.selEnd().pit();
1133 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1134 pars_[pit].insertChar(0, '\t',
1135 bv->buffer().params().track_changes);
1136 // Update the selection pos to make sure the selection does not
1137 // change as the inserted tab will increase the logical pos.
1138 if (cur.realAnchor().pit() == pit)
1139 cur.realAnchor().forwardPos();
1140 if (cur.pit() == pit)
1145 // Maybe we shouldn't allow tabs within a line, because they
1146 // are not (yet) aligned as one might do expect.
1147 FuncRequest ncmd(LFUN_SELF_INSERT, from_ascii("\t"));
1148 dispatch(cur, ncmd);
1153 case LFUN_TAB_DELETE: {
1154 bool const tc = bv->buffer().params().track_changes;
1155 if (cur.selection()) {
1156 // If there is a selection, a tab (if present) is removed from
1157 // the beginning of each paragraph.
1158 cur.recordUndoSelection();
1159 pit_type const pit_end = cur.selEnd().pit();
1160 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1161 Paragraph & par = paragraphs()[pit];
1164 char_type const c = par.getChar(0);
1165 if (c == '\t' || c == ' ') {
1166 // remove either 1 tab or 4 spaces.
1167 int const n = (c == ' ' ? 4 : 1);
1168 for (int i = 0; i < n
1169 && !par.empty() && par.getChar(0) == c; ++i) {
1170 if (cur.pit() == pit)
1172 if (cur.realAnchor().pit() == pit
1173 && cur.realAnchor().pos() > 0 )
1174 cur.realAnchor().backwardPos();
1175 par.eraseChar(0, tc);
1181 // If there is no selection, try to remove a tab or some spaces
1182 // before the position of the cursor.
1183 Paragraph & par = paragraphs()[cur.pit()];
1184 pos_type const pos = cur.pos();
1189 char_type const c = par.getChar(pos - 1);
1193 par.eraseChar(cur.pos(), tc);
1195 for (int n_spaces = 0;
1197 && par.getChar(cur.pos() - 1) == ' '
1201 par.eraseChar(cur.pos(), tc);
1208 case LFUN_CHAR_DELETE_FORWARD:
1209 if (!cur.selection()) {
1210 if (cur.pos() == cur.paragraph().size())
1211 // Par boundary, force full-screen update
1212 singleParUpdate = false;
1213 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1215 cur.selection(true);
1220 needsUpdate |= erase(cur);
1223 cutSelection(cur, false);
1224 singleParUpdate = false;
1226 moveCursor(cur, false);
1229 case LFUN_CHAR_DELETE_BACKWARD:
1230 if (!cur.selection()) {
1231 if (bv->getIntl().getTransManager().backspace()) {
1232 bool par_boundary = cur.pos() == 0;
1233 bool first_par = cur.pit() == 0;
1234 // Par boundary, full-screen update
1236 singleParUpdate = false;
1237 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1239 cur.selection(true);
1244 needsUpdate |= backspace(cur);
1246 if (par_boundary && !first_par && cur.pos() > 0
1247 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1248 needsUpdate |= backspace(cur);
1253 cutSelection(cur, false);
1254 singleParUpdate = false;
1258 case LFUN_PARAGRAPH_BREAK: {
1259 cap::replaceSelection(cur);
1260 pit_type pit = cur.pit();
1261 Paragraph const & par = pars_[pit];
1262 bool lastpar = (pit == pit_type(pars_.size() - 1));
1263 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1264 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1265 if (prev < pit && cur.pos() == par.beginOfBody()
1266 && !par.size() && !par.isEnvSeparator(cur.pos())
1267 && !par.layout().keepempty
1268 && !par.layout().isCommand()
1269 && pars_[prev].layout() != par.layout()
1270 && pars_[prev].layout().isEnvironment()
1271 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1272 if (par.layout().isEnvironment()
1273 && pars_[prev].getDepth() == par.getDepth()) {
1274 docstring const layout = par.layout().name();
1275 DocumentClass const & tc = bv->buffer().params().documentClass();
1276 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1277 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1278 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1279 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1281 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1282 breakParagraph(cur);
1284 Font const f(inherit_font, cur.current_font.language());
1285 pars_[cur.pit() - 1].resetFonts(f);
1287 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1289 breakParagraph(cur, cmd.getArg(0) == "inverse");
1292 // If we have a list and autoinsert item insets,
1294 Layout::LaTeXArgMap args = par.layout().args();
1295 for (auto const & thearg : args) {
1296 Layout::latexarg arg = thearg.second;
1297 if (arg.autoinsert && prefixIs(thearg.first, "item:")) {
1298 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, thearg.first);
1299 lyx::dispatch(cmd2);
1305 case LFUN_INSET_INSERT: {
1308 // We have to avoid triggering InstantPreview loading
1309 // before inserting into the document. See bug #5626.
1310 bool loaded = bv->buffer().isFullyLoaded();
1311 bv->buffer().setFullyLoaded(false);
1312 Inset * inset = createInset(&bv->buffer(), cmd);
1313 bv->buffer().setFullyLoaded(loaded);
1316 // FIXME (Abdel 01/02/2006):
1317 // What follows would be a partial fix for bug 2154:
1318 // http://www.lyx.org/trac/ticket/2154
1319 // This automatically put the label inset _after_ a
1320 // numbered section. It should be possible to extend the mechanism
1321 // to any kind of LateX environement.
1322 // The correct way to fix that bug would be at LateX generation.
1323 // I'll let the code here for reference as it could be used for some
1324 // other feature like "automatic labelling".
1326 Paragraph & par = pars_[cur.pit()];
1327 if (inset->lyxCode() == LABEL_CODE
1328 && !par.layout().counter.empty()) {
1329 // Go to the end of the paragraph
1330 // Warning: Because of Change-Tracking, the last
1331 // position is 'size()' and not 'size()-1':
1332 cur.pos() = par.size();
1333 // Insert a new paragraph
1334 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1338 if (cur.selection())
1339 cutSelection(cur, false);
1341 cur.forceBufferUpdate();
1342 if (inset->editable() && inset->asInsetText())
1343 inset->edit(cur, true);
1347 // trigger InstantPreview now
1348 if (inset->lyxCode() == EXTERNAL_CODE) {
1349 InsetExternal & ins =
1350 static_cast<InsetExternal &>(*inset);
1351 ins.updatePreview();
1358 case LFUN_INSET_DISSOLVE: {
1359 if (dissolveInset(cur)) {
1361 cur.forceBufferUpdate();
1366 case LFUN_GRAPHICS_SET_GROUP: {
1367 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1373 string id = to_utf8(cmd.argument());
1374 string grp = graphics::getGroupParams(bv->buffer(), id);
1375 InsetGraphicsParams tmp, inspar = ins->getParams();
1378 inspar.groupId = to_utf8(cmd.argument());
1380 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1381 tmp.filename = inspar.filename;
1385 ins->setParams(inspar);
1389 case LFUN_SPACE_INSERT:
1390 if (cur.paragraph().layout().free_spacing)
1391 insertChar(cur, ' ');
1393 doInsertInset(cur, this, cmd, false, false);
1396 moveCursor(cur, false);
1399 case LFUN_SPECIALCHAR_INSERT: {
1400 string const name = to_utf8(cmd.argument());
1401 if (name == "hyphenation")
1402 specialChar(cur, InsetSpecialChar::HYPHENATION);
1403 else if (name == "allowbreak")
1404 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1405 else if (name == "ligature-break")
1406 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1407 else if (name == "slash")
1408 specialChar(cur, InsetSpecialChar::SLASH);
1409 else if (name == "nobreakdash")
1410 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1411 else if (name == "dots")
1412 specialChar(cur, InsetSpecialChar::LDOTS);
1413 else if (name == "end-of-sentence")
1414 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1415 else if (name == "menu-separator")
1416 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1417 else if (name == "lyx")
1418 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1419 else if (name == "tex")
1420 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1421 else if (name == "latex")
1422 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1423 else if (name == "latex2e")
1424 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1425 else if (name.empty())
1426 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1428 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1432 case LFUN_IPAMACRO_INSERT: {
1433 string const arg = cmd.getArg(0);
1434 if (arg == "deco") {
1435 // Open the inset, and move the current selection
1437 doInsertInset(cur, this, cmd, true, true);
1439 // Some insets are numbered, others are shown in the outline pane so
1440 // let's update the labels and the toc backend.
1441 cur.forceBufferUpdate();
1444 if (arg == "tone-falling")
1445 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1446 else if (arg == "tone-rising")
1447 ipaChar(cur, InsetIPAChar::TONE_RISING);
1448 else if (arg == "tone-high-rising")
1449 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1450 else if (arg == "tone-low-rising")
1451 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1452 else if (arg == "tone-high-rising-falling")
1453 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1454 else if (arg.empty())
1455 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1457 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1461 case LFUN_WORD_UPCASE:
1462 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1465 case LFUN_WORD_LOWCASE:
1466 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1469 case LFUN_WORD_CAPITALIZE:
1470 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1473 case LFUN_CHARS_TRANSPOSE:
1474 charsTranspose(cur);
1478 cur.message(_("Paste"));
1479 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1480 cap::replaceSelection(cur);
1482 // without argument?
1483 string const arg = to_utf8(cmd.argument());
1485 bool tryGraphics = true;
1486 if (theClipboard().isInternal())
1487 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1488 else if (theClipboard().hasTextContents()) {
1489 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1490 true, Clipboard::AnyTextType))
1491 tryGraphics = false;
1493 if (tryGraphics && theClipboard().hasGraphicsContents())
1494 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1495 } else if (isStrUnsignedInt(arg)) {
1496 // we have a numerical argument
1497 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1498 convert<unsigned int>(arg));
1499 } else if (arg == "html" || arg == "latex") {
1500 Clipboard::TextType type = (arg == "html") ?
1501 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1502 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1504 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1506 type = Clipboard::PdfGraphicsType;
1507 else if (arg == "png")
1508 type = Clipboard::PngGraphicsType;
1509 else if (arg == "jpeg")
1510 type = Clipboard::JpegGraphicsType;
1511 else if (arg == "linkback")
1512 type = Clipboard::LinkBackGraphicsType;
1513 else if (arg == "emf")
1514 type = Clipboard::EmfGraphicsType;
1515 else if (arg == "wmf")
1516 type = Clipboard::WmfGraphicsType;
1518 // we also check in getStatus()
1519 LYXERR0("Unrecognized graphics type: " << arg);
1521 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1524 bv->buffer().errors("Paste");
1525 bv->buffer().updatePreviews(); // bug 11619
1526 cur.clearSelection(); // bug 393
1532 cutSelection(cur, true);
1533 cur.message(_("Cut"));
1538 cur.message(_("Copy"));
1541 case LFUN_SERVER_GET_XY:
1542 cur.message(from_utf8(
1543 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1544 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1547 case LFUN_SERVER_SET_XY: {
1550 istringstream is(to_utf8(cmd.argument()));
1553 lyxerr << "SETXY: Could not parse coordinates in '"
1554 << to_utf8(cmd.argument()) << endl;
1556 tm->setCursorFromCoordinates(cur, x, y);
1560 case LFUN_SERVER_GET_LAYOUT:
1561 cur.message(cur.paragraph().layout().name());
1565 case LFUN_LAYOUT_TOGGLE: {
1566 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1567 docstring req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1568 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(req_layout));
1570 docstring layout = resolveLayout(req_layout, cur);
1571 if (layout.empty()) {
1572 cur.errorMessage(from_utf8(N_("Layout ")) + req_layout +
1573 from_utf8(N_(" not known")));
1577 docstring const old_layout = cur.paragraph().layout().name();
1578 bool change_layout = !isAlreadyLayout(layout, cur);
1580 if (cmd.action() == LFUN_LAYOUT_TOGGLE && !change_layout) {
1581 change_layout = true;
1582 layout = resolveLayout(docstring(), cur);
1585 if (change_layout) {
1586 setLayout(cur, layout);
1587 if (cur.pit() > 0 && !ignoreautonests) {
1588 set<docstring> const & autonests =
1589 pars_[cur.pit() - 1].layout().autonests();
1590 set<docstring> const & autonested =
1591 pars_[cur.pit()].layout().isAutonestedBy();
1592 if (autonests.find(layout) != autonests.end()
1593 || autonested.find(old_layout) != autonested.end())
1594 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1598 DocumentClass const & tclass = bv->buffer().params().documentClass();
1599 bool inautoarg = false;
1600 for (auto const & la_pair : tclass[layout].args()) {
1601 Layout::latexarg const & arg = la_pair.second;
1602 if (arg.autoinsert) {
1603 // If we had already inserted an arg automatically,
1604 // leave this now in order to insert the next one.
1606 cur.leaveInset(cur.inset());
1610 FuncRequest const cmd2(LFUN_ARGUMENT_INSERT, la_pair.first);
1611 lyx::dispatch(cmd2);
1619 case LFUN_ENVIRONMENT_SPLIT: {
1620 bool const outer = cmd.argument() == "outer";
1621 bool const previous = cmd.argument() == "previous";
1622 bool const before = cmd.argument() == "before";
1623 bool const normal = cmd.argument().empty();
1624 Paragraph const & para = cur.paragraph();
1626 if (para.layout().isEnvironment())
1627 layout = para.layout().name();
1628 depth_type split_depth = cur.paragraph().params().depth();
1629 depth_type nextpar_depth = 0;
1630 if (outer || previous) {
1631 // check if we have an environment in our scope
1632 pit_type pit = cur.pit();
1633 Paragraph cpar = pars_[pit];
1639 if (layout.empty() && previous
1640 && cpar.layout().isEnvironment()
1641 && cpar.params().depth() <= split_depth)
1642 layout = cpar.layout().name();
1643 if (cpar.params().depth() < split_depth
1644 && cpar.layout().isEnvironment()) {
1646 layout = cpar.layout().name();
1647 split_depth = cpar.params().depth();
1649 if (cpar.params().depth() == 0)
1653 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1654 // save nesting of following paragraph
1655 Paragraph cpar = pars_[cur.pit() + 1];
1656 nextpar_depth = cpar.params().depth();
1659 cur.top().setPitPos(cur.pit(), 0);
1660 if (before || cur.pos() > 0)
1661 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1662 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1663 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1665 while (cur.paragraph().params().depth() > split_depth)
1666 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1668 DocumentClass const & tc = bv->buffer().params().documentClass();
1669 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1670 + from_ascii("\" ignoreautonests")));
1671 // FIXME: Bibitem mess!
1672 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
1673 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
1674 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1677 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1678 while (cur.paragraph().params().depth() < split_depth)
1679 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1682 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1683 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1684 if ((outer || normal) && nextpar_depth > 0) {
1685 // restore nesting of following paragraph
1686 DocIterator scur = cur;
1687 depth_type const max_depth = cur.paragraph().params().depth() + 1;
1689 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth)) {
1690 depth_type const olddepth = cur.paragraph().params().depth();
1691 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1692 if (olddepth == cur.paragraph().params().depth())
1693 // leave loop if no incrementation happens
1696 cur.setCursor(scur);
1702 case LFUN_CLIPBOARD_PASTE:
1703 cap::replaceSelection(cur);
1704 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1705 cmd.argument() == "paragraph");
1706 bv->buffer().errors("Paste");
1709 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1710 cap::replaceSelection(cur);
1711 pasteSimpleText(cur, cmd.argument() == "paragraph");
1714 case LFUN_PRIMARY_SELECTION_PASTE:
1715 cap::replaceSelection(cur);
1716 pasteString(cur, theSelection().get(),
1717 cmd.argument() == "paragraph");
1720 case LFUN_SELECTION_PASTE:
1721 // Copy the selection buffer to the clipboard stack,
1722 // because we want it to appear in the "Edit->Paste
1724 cap::replaceSelection(cur);
1725 cap::copySelectionToStack();
1726 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1727 bv->buffer().errors("Paste");
1730 case LFUN_QUOTE_INSERT: {
1731 cap::replaceSelection(cur);
1734 Paragraph const & par = cur.paragraph();
1735 pos_type pos = cur.pos();
1736 // Ignore deleted text before cursor
1737 while (pos > 0 && par.isDeleted(pos - 1))
1740 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1742 // Guess quote side.
1743 // A space triggers an opening quote. This is passed if the preceding
1744 // char/inset is a space or at paragraph start.
1746 if (pos > 0 && !par.isSpace(pos - 1)) {
1747 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1748 // If an opening double quotation mark precedes, and this
1749 // is a single quote, make it opening as well
1751 static_cast<InsetQuotes &>(*cur.prevInset());
1752 string const type = ins.getType();
1753 if (!suffixIs(type, "ld") || !inner)
1754 c = par.getChar(pos - 1);
1756 else if (!cur.prevInset()
1757 || (cur.prevInset() && cur.prevInset()->isChar()))
1758 // If a char precedes, pass that and let InsetQuote decide
1759 c = par.getChar(pos - 1);
1762 if (par.getInset(pos - 1)
1763 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1764 // skip "invisible" insets
1768 c = par.getChar(pos - 1);
1773 InsetQuotesParams::QuoteLevel const quote_level = inner
1774 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1775 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1776 cur.buffer()->updateBuffer();
1781 case LFUN_MOUSE_TRIPLE:
1782 if (cmd.button() == mouse_button::button1) {
1784 setCursor(cur, cur.pit(), 0);
1787 if (cur.pos() < cur.lastpos())
1788 setCursor(cur, cur.pit(), cur.lastpos());
1794 case LFUN_MOUSE_DOUBLE:
1795 if (cmd.button() == mouse_button::button1) {
1796 selectWord(cur, WHOLE_WORD);
1801 // Single-click on work area
1802 case LFUN_MOUSE_PRESS: {
1803 // We are not marking a selection with the keyboard in any case.
1804 Cursor & bvcur = cur.bv().cursor();
1805 bvcur.setMark(false);
1806 switch (cmd.button()) {
1807 case mouse_button::button1:
1808 if (!bvcur.selection())
1810 bvcur.resetAnchor();
1811 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1812 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1813 if (bvcur.wordSelection())
1814 selectWord(bvcur, WHOLE_WORD);
1817 case mouse_button::button2:
1818 if (lyxrc.mouse_middlebutton_paste) {
1819 // Middle mouse pasting.
1820 bv->mouseSetCursor(cur);
1822 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1823 "selection-paste ; primary-selection-paste paragraph"));
1825 cur.noScreenUpdate();
1828 case mouse_button::button3: {
1829 // Don't do anything if we right-click a
1830 // selection, a context menu will popup.
1831 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1832 && cur < bvcur.selectionEnd()) {
1833 cur.noScreenUpdate();
1836 if (!bv->mouseSetCursor(cur, false))
1837 cur.screenUpdateFlags(Update::FitCursor);
1843 } // switch (cmd.button())
1846 case LFUN_MOUSE_MOTION: {
1847 // Mouse motion with right or middle mouse do nothing for now.
1848 if (cmd.button() != mouse_button::button1) {
1849 cur.noScreenUpdate();
1852 // ignore motions deeper nested than the real anchor
1853 Cursor & bvcur = cur.bv().cursor();
1854 if (!bvcur.realAnchor().hasPart(cur)) {
1858 CursorSlice old = bvcur.top();
1860 int const wh = bv->workHeight();
1861 int const y = max(0, min(wh - 1, cmd.y()));
1863 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1864 cur.setTargetX(cmd.x());
1865 // Don't allow selecting a separator inset
1866 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1869 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1870 else if (cmd.y() < 0)
1871 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1872 // This is to allow jumping over large insets
1873 if (cur.top() == old) {
1875 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1876 else if (cmd.y() < 0)
1877 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1879 // We continue with our existing selection or start a new one, so don't
1880 // reset the anchor.
1881 bvcur.setCursor(cur);
1882 bvcur.selection(true);
1883 bvcur.setCurrentFont();
1884 if (cur.top() == old) {
1885 // We didn't move one iota, so no need to update the screen.
1886 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1887 //cur.noScreenUpdate();
1893 case LFUN_MOUSE_RELEASE:
1894 switch (cmd.button()) {
1895 case mouse_button::button1:
1896 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1897 // If there is a new selection, update persistent selection;
1898 // otherwise, single click does not clear persistent selection
1900 if (cur.selection()) {
1901 // Finish selection. If double click,
1902 // cur is moved to the end of word by
1903 // selectWord but bvcur is current
1905 cur.bv().cursor().setSelection();
1906 // We might have removed an empty but drawn selection
1907 // (probably a margin)
1908 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1910 cur.noScreenUpdate();
1911 // FIXME: We could try to handle drag and drop of selection here.
1914 case mouse_button::button2:
1915 // Middle mouse pasting is handled at mouse press time,
1916 // see LFUN_MOUSE_PRESS.
1917 cur.noScreenUpdate();
1920 case mouse_button::button3:
1921 // Cursor was set at LFUN_MOUSE_PRESS time.
1922 // FIXME: If there is a selection we could try to handle a special
1923 // drag & drop context menu.
1924 cur.noScreenUpdate();
1927 case mouse_button::none:
1928 case mouse_button::button4:
1929 case mouse_button::button5:
1931 } // switch (cmd.button())
1935 case LFUN_SELF_INSERT: {
1936 if (cmd.argument().empty())
1939 // Automatically delete the currently selected
1940 // text and replace it with what is being
1941 // typed in now. Depends on lyxrc settings
1942 // "auto_region_delete", which defaults to
1945 if (lyxrc.auto_region_delete && cur.selection())
1946 cutSelection(cur, false);
1947 cur.clearSelection();
1949 for (char_type c : cmd.argument())
1950 bv->translateAndInsert(c, this, cur);
1953 moveCursor(cur, false);
1954 cur.markNewWordPosition();
1955 bv->bookmarkEditPosition();
1959 case LFUN_HREF_INSERT: {
1960 docstring content = cmd.argument();
1961 if (content.empty() && cur.selection())
1962 content = cur.selectionAsString(false);
1964 InsetCommandParams p(HYPERLINK_CODE);
1965 if (!content.empty()){
1966 // if it looks like a link, we'll put it as target,
1967 // otherwise as name (bug #8792).
1970 // regex_match(to_utf8(content), matches, link_re)
1971 // because smatch stores pointers to the substrings rather
1972 // than making copies of them. And those pointers become
1973 // invalid after regex_match returns, since it is then
1974 // being given a temporary object. (Thanks to Georg for
1975 // figuring that out.)
1976 regex const link_re("^([a-z]+):.*");
1978 string const c = to_utf8(lowercase(content));
1980 if (c.substr(0,7) == "mailto:") {
1981 p["target"] = content;
1982 p["type"] = from_ascii("mailto:");
1983 } else if (regex_match(c, matches, link_re)) {
1984 p["target"] = content;
1985 string protocol = matches.str(1);
1986 if (protocol == "file")
1987 p["type"] = from_ascii("file:");
1989 p["name"] = content;
1991 string const data = InsetCommand::params2string(p);
1993 // we need to have a target. if we already have one, then
1994 // that gets used at the default for the name, too, which
1995 // is probably what is wanted.
1996 if (p["target"].empty()) {
1997 bv->showDialog("href", data);
1999 FuncRequest fr(LFUN_INSET_INSERT, data);
2005 case LFUN_LABEL_INSERT: {
2006 InsetCommandParams p(LABEL_CODE);
2007 // Try to generate a valid label
2008 p["name"] = (cmd.argument().empty()) ?
2009 cur.getPossibleLabel() :
2011 string const data = InsetCommand::params2string(p);
2013 if (cmd.argument().empty()) {
2014 bv->showDialog("label", data);
2016 FuncRequest fr(LFUN_INSET_INSERT, data);
2022 case LFUN_INFO_INSERT: {
2023 if (cmd.argument().empty()) {
2024 bv->showDialog("info", cur.current_font.language()->lang());
2027 inset = createInset(cur.buffer(), cmd);
2031 insertInset(cur, inset);
2032 cur.forceBufferUpdate();
2037 case LFUN_CAPTION_INSERT:
2038 case LFUN_FOOTNOTE_INSERT:
2039 case LFUN_NOTE_INSERT:
2040 case LFUN_BOX_INSERT:
2041 case LFUN_BRANCH_INSERT:
2042 case LFUN_PHANTOM_INSERT:
2043 case LFUN_ERT_INSERT:
2044 case LFUN_LISTING_INSERT:
2045 case LFUN_MARGINALNOTE_INSERT:
2046 case LFUN_ARGUMENT_INSERT:
2047 case LFUN_INDEX_INSERT:
2048 case LFUN_PREVIEW_INSERT:
2049 case LFUN_SCRIPT_INSERT:
2050 case LFUN_IPA_INSERT:
2051 // Open the inset, and move the current selection
2053 doInsertInset(cur, this, cmd, true, true);
2055 if (act == LFUN_SCRIPT_INSERT) {
2056 /* Script insets change the font style in metrics(), and
2057 * this is used to compute the height of the caret
2058 * (because the font is stored in TextMetrics::font_).
2059 * When we insert, we have to make sure that metrics are
2060 * computed so that the caret height is wrong. Arguably,
2061 * this is hackish.*/
2062 bv->processUpdateFlags(Update::SinglePar);
2064 cur.setCurrentFont();
2065 // Some insets are numbered, others are shown in the outline pane so
2066 // let's update the labels and the toc backend.
2067 cur.forceBufferUpdate();
2070 case LFUN_FLEX_INSERT: {
2071 // Open the inset, and move the current selection
2073 bool const sel = cur.selection();
2074 doInsertInset(cur, this, cmd, true, true);
2075 // Insert auto-insert arguments
2076 bool autoargs, inautoarg = false;
2077 Layout::LaTeXArgMap args = cur.inset().getLayout().args();
2078 Layout::LaTeXArgMap::const_iterator lait = args.begin();
2079 Layout::LaTeXArgMap::const_iterator const laend = args.end();
2080 for (; lait != laend; ++lait) {
2081 Layout::latexarg arg = (*lait).second;
2082 if (!inautoarg && arg.insertonnewline && cur.pos() > 0) {
2083 FuncRequest cmd2(LFUN_PARAGRAPH_BREAK);
2084 lyx::dispatch(cmd2);
2086 if (arg.autoinsert) {
2087 // The cursor might have been invalidated by the replaceSelection.
2088 cur.buffer()->changed(true);
2089 // If we had already inserted an arg automatically,
2090 // leave this now in order to insert the next one.
2092 cur.leaveInset(cur.inset());
2095 if (arg.insertonnewline && cur.pos() > 0) {
2096 FuncRequest cmd2(LFUN_PARAGRAPH_BREAK);
2097 lyx::dispatch(cmd2);
2100 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first);
2101 lyx::dispatch(cmd2);
2108 cur.leaveInset(cur.inset());
2111 // Some insets are numbered, others are shown in the outline pane so
2112 // let's update the labels and the toc backend.
2113 cur.forceBufferUpdate();
2117 case LFUN_TABULAR_INSERT: {
2118 // if there were no arguments, just open the dialog
2119 if (cmd.argument().empty()) {
2120 bv->showDialog("tabularcreate");
2122 } else if (cur.buffer()->masterParams().tablestyle != "default"
2123 || bv->buffer().params().documentClass().tablestyle() != "default") {
2124 string tabstyle = cur.buffer()->masterParams().tablestyle;
2125 if (tabstyle == "default")
2126 tabstyle = bv->buffer().params().documentClass().tablestyle();
2127 if (!libFileSearch("tabletemplates", tabstyle + ".lyx").empty()) {
2128 FuncRequest fr(LFUN_TABULAR_STYLE_INSERT,
2129 tabstyle + " " + to_ascii(cmd.argument()));
2133 // Unknown style. Report and fall back to default.
2134 cur.errorMessage(from_utf8(N_("Table Style ")) + from_utf8(tabstyle) +
2135 from_utf8(N_(" not known")));
2138 if (doInsertInset(cur, this, cmd, false, true))
2143 case LFUN_TABULAR_STYLE_INSERT: {
2144 string const style = cmd.getArg(0);
2145 string const rows = cmd.getArg(1);
2146 string const cols = cmd.getArg(2);
2147 if (cols.empty() || !isStrInt(cols)
2148 || rows.empty() || !isStrInt(rows))
2150 int const r = convert<int>(rows);
2151 int const c = convert<int>(cols);
2158 FileName const tabstyle = libFileSearch("tabletemplates",
2159 style + suffix + ".lyx", "lyx");
2160 if (tabstyle.empty())
2162 UndoGroupHelper ugh(cur.buffer());
2164 FuncRequest cmd2(LFUN_FILE_INSERT, tabstyle.absFileName() + " ignorelang");
2165 lyx::dispatch(cmd2);
2169 // move one cell up to middle cell
2171 // add the missing rows
2172 int const addrows = r - 3;
2173 for (int i = 0 ; i < addrows ; ++i) {
2174 FuncRequest fr(LFUN_TABULAR_FEATURE, "append-row");
2178 // add the missing columns
2179 int const addcols = c - 1;
2180 for (int i = 0 ; i < addcols ; ++i) {
2181 FuncRequest fr(LFUN_TABULAR_FEATURE, "append-column");
2190 case LFUN_FLOAT_INSERT:
2191 case LFUN_FLOAT_WIDE_INSERT:
2192 case LFUN_WRAP_INSERT: {
2193 // will some content be moved into the inset?
2194 bool const content = cur.selection();
2195 // does the content consist of multiple paragraphs?
2196 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2198 doInsertInset(cur, this, cmd, true, true);
2201 // If some single-par content is moved into the inset,
2202 // doInsertInset puts the cursor outside the inset.
2203 // To insert the caption we put it back into the inset.
2204 // FIXME cleanup doInsertInset to avoid such dances!
2205 if (content && singlepar)
2208 ParagraphList & pars = cur.text()->paragraphs();
2210 DocumentClass const & tclass = bv->buffer().params().documentClass();
2212 // add a separate paragraph for the caption inset
2213 pars.push_back(Paragraph());
2214 pars.back().setInsetOwner(&cur.text()->inset());
2215 pars.back().setPlainOrDefaultLayout(tclass);
2216 int cap_pit = pars.size() - 1;
2218 // if an empty inset was created, we create an additional empty
2219 // paragraph at the bottom so that the user can choose where to put
2220 // the graphics (or table).
2222 pars.push_back(Paragraph());
2223 pars.back().setInsetOwner(&cur.text()->inset());
2224 pars.back().setPlainOrDefaultLayout(tclass);
2227 // reposition the cursor to the caption
2228 cur.pit() = cap_pit;
2230 // FIXME: This Text/Cursor dispatch handling is a mess!
2231 // We cannot use Cursor::dispatch here it needs access to up to
2233 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2234 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2235 cur.forceBufferUpdate();
2236 cur.screenUpdateFlags(Update::Force);
2237 // FIXME: When leaving the Float (or Wrap) inset we should
2238 // delete any empty paragraph left above or below the
2243 case LFUN_NOMENCL_INSERT: {
2244 InsetCommandParams p(NOMENCL_CODE);
2245 if (cmd.argument().empty()) {
2247 bv->cursor().innerText()->getStringForDialog(bv->cursor());
2248 cur.clearSelection();
2250 p["symbol"] = cmd.argument();
2251 string const data = InsetCommand::params2string(p);
2252 bv->showDialog("nomenclature", data);
2256 case LFUN_INDEX_PRINT: {
2257 InsetCommandParams p(INDEX_PRINT_CODE);
2258 if (cmd.argument().empty())
2259 p["type"] = from_ascii("idx");
2261 p["type"] = cmd.argument();
2262 string const data = InsetCommand::params2string(p);
2263 FuncRequest fr(LFUN_INSET_INSERT, data);
2268 case LFUN_NOMENCL_PRINT:
2269 case LFUN_NEWPAGE_INSERT:
2271 doInsertInset(cur, this, cmd, false, false);
2275 case LFUN_SEPARATOR_INSERT: {
2276 doInsertInset(cur, this, cmd, false, false);
2278 // remove a following space
2279 Paragraph & par = cur.paragraph();
2280 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2281 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2285 case LFUN_DEPTH_DECREMENT:
2286 changeDepth(cur, DEC_DEPTH);
2289 case LFUN_DEPTH_INCREMENT:
2290 changeDepth(cur, INC_DEPTH);
2293 case LFUN_REGEXP_MODE:
2294 regexpDispatch(cur, cmd);
2297 case LFUN_MATH_MODE: {
2298 if (cmd.argument() == "on" || cmd.argument() == "") {
2299 // don't pass "on" as argument
2300 // (it would appear literally in the first cell)
2301 docstring sel = cur.selectionAsString(false);
2302 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2303 // create a macro template if we see "\\newcommand" somewhere, and
2304 // an ordinary formula otherwise
2306 && (sel.find(from_ascii("\\newcommand")) != string::npos
2307 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2308 || sel.find(from_ascii("\\def")) != string::npos)
2309 && macro->fromString(sel)) {
2311 replaceSelection(cur);
2314 // no meaningful macro template was found
2316 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2319 // The argument is meaningful
2320 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2321 // has a different meaning in math mode
2322 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2326 case LFUN_MATH_MACRO:
2327 if (cmd.argument().empty())
2328 cur.errorMessage(from_utf8(N_("Missing argument")));
2331 string s = to_utf8(cmd.argument());
2332 string const s1 = token(s, ' ', 1);
2333 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2334 string const s2 = token(s, ' ', 2);
2335 MacroType type = MacroTypeNewcommand;
2337 type = MacroTypeDef;
2338 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2339 from_utf8(token(s, ' ', 0)), nargs, false, type);
2340 inset->setBuffer(bv->buffer());
2341 insertInset(cur, inset);
2343 // enter macro inset and select the name
2345 cur.top().pos() = cur.top().lastpos();
2347 cur.selection(true);
2348 cur.top().pos() = 0;
2352 case LFUN_MATH_DISPLAY:
2353 case LFUN_MATH_SUBSCRIPT:
2354 case LFUN_MATH_SUPERSCRIPT:
2355 case LFUN_MATH_INSERT:
2356 case LFUN_MATH_AMS_MATRIX:
2357 case LFUN_MATH_MATRIX:
2358 case LFUN_MATH_DELIM:
2359 case LFUN_MATH_BIGDELIM:
2360 mathDispatch(cur, cmd);
2363 case LFUN_FONT_EMPH: {
2364 Font font(ignore_font, ignore_language);
2365 font.fontInfo().setEmph(FONT_TOGGLE);
2366 toggleAndShow(cur, this, font);
2370 case LFUN_FONT_ITAL: {
2371 Font font(ignore_font, ignore_language);
2372 font.fontInfo().setShape(ITALIC_SHAPE);
2373 toggleAndShow(cur, this, font);
2377 case LFUN_FONT_BOLD:
2378 case LFUN_FONT_BOLDSYMBOL: {
2379 Font font(ignore_font, ignore_language);
2380 font.fontInfo().setSeries(BOLD_SERIES);
2381 toggleAndShow(cur, this, font);
2385 case LFUN_FONT_NOUN: {
2386 Font font(ignore_font, ignore_language);
2387 font.fontInfo().setNoun(FONT_TOGGLE);
2388 toggleAndShow(cur, this, font);
2392 case LFUN_FONT_TYPEWRITER: {
2393 Font font(ignore_font, ignore_language);
2394 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2395 toggleAndShow(cur, this, font);
2399 case LFUN_FONT_SANS: {
2400 Font font(ignore_font, ignore_language);
2401 font.fontInfo().setFamily(SANS_FAMILY);
2402 toggleAndShow(cur, this, font);
2406 case LFUN_FONT_ROMAN: {
2407 Font font(ignore_font, ignore_language);
2408 font.fontInfo().setFamily(ROMAN_FAMILY);
2409 toggleAndShow(cur, this, font);
2413 case LFUN_FONT_DEFAULT: {
2414 Font font(inherit_font, ignore_language);
2415 toggleAndShow(cur, this, font);
2419 case LFUN_FONT_STRIKEOUT: {
2420 Font font(ignore_font, ignore_language);
2421 font.fontInfo().setStrikeout(FONT_TOGGLE);
2422 toggleAndShow(cur, this, font);
2426 case LFUN_FONT_CROSSOUT: {
2427 Font font(ignore_font, ignore_language);
2428 font.fontInfo().setXout(FONT_TOGGLE);
2429 toggleAndShow(cur, this, font);
2433 case LFUN_FONT_UNDERUNDERLINE: {
2434 Font font(ignore_font, ignore_language);
2435 font.fontInfo().setUuline(FONT_TOGGLE);
2436 toggleAndShow(cur, this, font);
2440 case LFUN_FONT_UNDERWAVE: {
2441 Font font(ignore_font, ignore_language);
2442 font.fontInfo().setUwave(FONT_TOGGLE);
2443 toggleAndShow(cur, this, font);
2447 case LFUN_FONT_UNDERLINE: {
2448 Font font(ignore_font, ignore_language);
2449 font.fontInfo().setUnderbar(FONT_TOGGLE);
2450 toggleAndShow(cur, this, font);
2454 case LFUN_FONT_SIZE: {
2455 Font font(ignore_font, ignore_language);
2456 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2457 toggleAndShow(cur, this, font);
2461 case LFUN_LANGUAGE: {
2462 string const lang_arg = cmd.getArg(0);
2463 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2464 Language const * lang =
2465 reset ? reset_language
2466 : languages.getLanguage(lang_arg);
2467 // we allow reset_language, which is 0, but only if it
2468 // was requested via empty or "reset" arg.
2469 if (!lang && !reset)
2471 bool const toggle = (cmd.getArg(1) != "set");
2472 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2473 Font font(ignore_font, lang);
2474 toggleAndShow(cur, this, font, toggle);
2475 // We need a buffer update if we change the language
2477 if (cur.insetInSelection(INFO_CODE))
2478 cur.forceBufferUpdate();
2482 case LFUN_TEXTSTYLE_APPLY: {
2483 unsigned int num = 0;
2484 string const arg = to_utf8(cmd.argument());
2487 if (isStrUnsignedInt(arg)) {
2488 num = convert<uint>(arg);
2489 if (num >= freeFonts.size()) {
2490 cur.message(_("Invalid argument (number exceeds stack size)!"));
2494 cur.message(_("Invalid argument (must be a non-negative number)!"));
2498 toggleAndShow(cur, this, freeFonts[num].second, toggleall);
2499 cur.message(bformat(_("Text properties applied: %1$s"), freeFonts[num].first));
2503 // Set the freefont using the contents of \param data dispatched from
2504 // the frontends and apply it at the current cursor location.
2505 case LFUN_TEXTSTYLE_UPDATE: {
2506 Font font(ignore_font, ignore_language);
2508 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2509 docstring const props = font.stateText(&bv->buffer().params(), true);
2510 freeFonts.push(make_pair(props, font));
2512 toggleAndShow(cur, this, font, toggleall);
2513 // We need a buffer update if we change the language
2515 if (cur.insetInSelection(INFO_CODE))
2516 cur.forceBufferUpdate();
2517 cur.message(bformat(_("Text properties applied: %1$s"), props));
2519 LYXERR0("Invalid argument of textstyle-update");
2523 case LFUN_FINISHED_LEFT:
2524 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2525 // We're leaving an inset, going left. If the inset is LTR, we're
2526 // leaving from the front, so we should not move (remain at --- but
2527 // not in --- the inset). If the inset is RTL, move left, without
2528 // entering the inset itself; i.e., move to after the inset.
2529 if (cur.paragraph().getFontSettings(
2530 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2531 cursorVisLeft(cur, true);
2534 case LFUN_FINISHED_RIGHT:
2535 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2536 // We're leaving an inset, going right. If the inset is RTL, we're
2537 // leaving from the front, so we should not move (remain at --- but
2538 // not in --- the inset). If the inset is LTR, move right, without
2539 // entering the inset itself; i.e., move to after the inset.
2540 if (!cur.paragraph().getFontSettings(
2541 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2542 cursorVisRight(cur, true);
2545 case LFUN_FINISHED_BACKWARD:
2546 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2547 cur.setCurrentFont();
2550 case LFUN_FINISHED_FORWARD:
2551 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2553 cur.setCurrentFont();
2556 case LFUN_LAYOUT_PARAGRAPH: {
2558 params2string(cur.paragraph(), data);
2559 data = "show\n" + data;
2560 bv->showDialog("paragraph", data);
2564 case LFUN_PARAGRAPH_UPDATE: {
2566 params2string(cur.paragraph(), data);
2568 // Will the paragraph accept changes from the dialog?
2570 cur.inset().allowParagraphCustomization(cur.idx());
2572 data = "update " + convert<string>(accept) + '\n' + data;
2573 bv->updateDialog("paragraph", data);
2577 case LFUN_ACCENT_UMLAUT:
2578 case LFUN_ACCENT_CIRCUMFLEX:
2579 case LFUN_ACCENT_GRAVE:
2580 case LFUN_ACCENT_ACUTE:
2581 case LFUN_ACCENT_TILDE:
2582 case LFUN_ACCENT_PERISPOMENI:
2583 case LFUN_ACCENT_CEDILLA:
2584 case LFUN_ACCENT_MACRON:
2585 case LFUN_ACCENT_DOT:
2586 case LFUN_ACCENT_UNDERDOT:
2587 case LFUN_ACCENT_UNDERBAR:
2588 case LFUN_ACCENT_CARON:
2589 case LFUN_ACCENT_BREVE:
2590 case LFUN_ACCENT_TIE:
2591 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2592 case LFUN_ACCENT_CIRCLE:
2593 case LFUN_ACCENT_OGONEK:
2594 theApp()->handleKeyFunc(cmd.action());
2595 if (!cmd.argument().empty())
2596 // FIXME: Are all these characters encoded in one byte in utf8?
2597 bv->translateAndInsert(cmd.argument()[0], this, cur);
2598 cur.screenUpdateFlags(Update::FitCursor);
2601 case LFUN_FLOAT_LIST_INSERT: {
2602 DocumentClass const & tclass = bv->buffer().params().documentClass();
2603 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2605 if (cur.selection())
2606 cutSelection(cur, false);
2607 breakParagraph(cur);
2609 if (cur.lastpos() != 0) {
2610 cursorBackward(cur);
2611 breakParagraph(cur);
2614 docstring const laystr = cur.inset().usePlainLayout() ?
2615 tclass.plainLayoutName() :
2616 tclass.defaultLayoutName();
2617 setLayout(cur, laystr);
2618 ParagraphParameters p;
2619 // FIXME If this call were replaced with one to clearParagraphParams(),
2620 // then we could get rid of this method altogether.
2621 setParagraphs(cur, p);
2622 // FIXME This should be simplified when InsetFloatList takes a
2623 // Buffer in its constructor.
2624 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2625 ifl->setBuffer(bv->buffer());
2626 insertInset(cur, ifl);
2629 lyxerr << "Non-existent float type: "
2630 << to_utf8(cmd.argument()) << endl;
2635 case LFUN_CHANGE_ACCEPT: {
2636 acceptOrRejectChanges(cur, ACCEPT);
2640 case LFUN_CHANGE_REJECT: {
2641 acceptOrRejectChanges(cur, REJECT);
2645 case LFUN_THESAURUS_ENTRY: {
2646 Language const * language = cur.getFont().language();
2647 docstring arg = cmd.argument();
2649 arg = cur.selectionAsString(false);
2651 if (arg.size() > 100 || arg.empty()) {
2652 // Get word or selection
2653 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2654 arg = cur.selectionAsString(false);
2655 arg += " lang=" + from_ascii(language->lang());
2658 string lang = cmd.getArg(1);
2659 // This duplicates the code in GuiThesaurus::initialiseParams
2660 if (prefixIs(lang, "lang=")) {
2661 language = languages.getLanguage(lang.substr(5));
2663 language = cur.getFont().language();
2666 string lang = language->code();
2667 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2668 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2669 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2670 _("The path to the thesaurus directory has not been specified.\n"
2671 "The thesaurus is not functional.\n"
2672 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2675 bv->showDialog("thesaurus", to_utf8(arg));
2679 case LFUN_SPELLING_ADD: {
2680 Language const * language = getLanguage(cur, cmd.getArg(1));
2681 docstring word = from_utf8(cmd.getArg(0));
2683 word = cur.selectionAsString(false);
2685 if (word.size() > 100 || word.empty()) {
2686 // Get word or selection
2687 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2688 word = cur.selectionAsString(false);
2691 WordLangTuple wl(word, language);
2692 theSpellChecker()->insert(wl);
2696 case LFUN_SPELLING_IGNORE: {
2697 Language const * language = getLanguage(cur, cmd.getArg(1));
2698 docstring word = from_utf8(cmd.getArg(0));
2700 word = cur.selectionAsString(false);
2702 if (word.size() > 100 || word.empty()) {
2703 // Get word or selection
2704 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2705 word = cur.selectionAsString(false);
2708 WordLangTuple wl(word, language);
2709 theSpellChecker()->accept(wl);
2713 case LFUN_SPELLING_REMOVE: {
2714 Language const * language = getLanguage(cur, cmd.getArg(1));
2715 docstring word = from_utf8(cmd.getArg(0));
2717 word = cur.selectionAsString(false);
2719 if (word.size() > 100 || word.empty()) {
2720 // Get word or selection
2721 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2722 word = cur.selectionAsString(false);
2725 WordLangTuple wl(word, language);
2726 theSpellChecker()->remove(wl);
2730 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2731 // Given data, an encoding of the ParagraphParameters
2732 // generated in the Paragraph dialog, this function sets
2733 // the current paragraph, or currently selected paragraphs,
2735 // NOTE: This function overrides all existing settings.
2736 setParagraphs(cur, cmd.argument());
2737 cur.message(_("Paragraph layout set"));
2741 case LFUN_PARAGRAPH_PARAMS: {
2742 // Given data, an encoding of the ParagraphParameters as we'd
2743 // find them in a LyX file, this function modifies the current paragraph,
2744 // or currently selected paragraphs.
2745 // NOTE: This function only modifies, and does not override, existing
2747 setParagraphs(cur, cmd.argument(), true);
2748 cur.message(_("Paragraph layout set"));
2753 if (cur.selection()) {
2754 cur.selection(false);
2757 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2758 // correct, but I'm not 100% sure -- dov, 071019
2759 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2763 case LFUN_OUTLINE_UP:
2764 outline(OutlineUp, cur, this);
2765 setCursor(cur, cur.pit(), 0);
2766 cur.forceBufferUpdate();
2770 case LFUN_OUTLINE_DOWN:
2771 outline(OutlineDown, cur, this);
2772 setCursor(cur, cur.pit(), 0);
2773 cur.forceBufferUpdate();
2777 case LFUN_OUTLINE_IN:
2778 outline(OutlineIn, cur, this);
2779 cur.forceBufferUpdate();
2783 case LFUN_OUTLINE_OUT:
2784 outline(OutlineOut, cur, this);
2785 cur.forceBufferUpdate();
2789 case LFUN_SERVER_GET_STATISTICS:
2791 DocIterator from, to;
2792 if (cur.selection()) {
2793 from = cur.selectionBegin();
2794 to = cur.selectionEnd();
2796 from = doc_iterator_begin(cur.buffer());
2797 to = doc_iterator_end(cur.buffer());
2800 cur.buffer()->updateStatistics(from, to);
2801 string const arg0 = cmd.getArg(0);
2802 if (arg0 == "words") {
2803 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2804 } else if (arg0 == "chars") {
2805 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2806 } else if (arg0 == "chars-space") {
2807 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2809 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2810 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2811 + convert<docstring>(cur.buffer()->charCount(true)));
2817 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2822 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2824 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2825 // Check for misspelled text
2826 // The redraw is useful because of the painting of
2827 // misspelled markers depends on the cursor position.
2828 // Trigger a redraw for cursor moves inside misspelled text.
2829 if (!cur.inTexted()) {
2830 // move from regular text to math
2831 needsUpdate = last_misspelled;
2832 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2833 // move inside regular text
2834 needsUpdate = last_misspelled
2835 || cur.paragraph().isMisspelled(cur.pos(), true);
2839 // FIXME: The cursor flag is reset two lines below
2840 // so we need to check here if some of the LFUN did touch that.
2841 // for now only Text::erase() and Text::backspace() do that.
2842 // The plan is to verify all the LFUNs and then to remove this
2843 // singleParUpdate boolean altogether.
2844 if (cur.result().screenUpdate() & Update::Force) {
2845 singleParUpdate = false;
2849 // FIXME: the following code should go in favor of fine grained
2850 // update flag treatment.
2851 if (singleParUpdate) {
2852 // Inserting characters does not change par height in general. So, try
2853 // to update _only_ this paragraph. BufferView will detect if a full
2854 // metrics update is needed anyway.
2855 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2859 && &oldTopSlice.inset() == &cur.inset()
2860 && oldTopSlice.idx() == cur.idx()
2861 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2862 && !cur.selection())
2863 // FIXME: it would be better if we could just do this
2865 //if (cur.result().update() != Update::FitCursor)
2866 // cur.noScreenUpdate();
2868 // But some LFUNs do not set Update::FitCursor when needed, so we
2869 // do it for all. This is not very harmfull as FitCursor will provoke
2870 // a full redraw only if needed but still, a proper review of all LFUN
2871 // should be done and this needsUpdate boolean can then be removed.
2872 cur.screenUpdateFlags(Update::FitCursor);
2874 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2878 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2879 FuncStatus & flag) const
2881 LBUFERR(this == cur.text());
2883 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2885 bool allow_in_passthru = false;
2886 InsetCode code = NO_CODE;
2888 switch (cmd.action()) {
2890 case LFUN_DEPTH_DECREMENT:
2891 enable = changeDepthAllowed(cur, DEC_DEPTH);
2894 case LFUN_DEPTH_INCREMENT:
2895 enable = changeDepthAllowed(cur, INC_DEPTH);
2899 // FIXME We really should not allow this to be put, e.g.,
2900 // in a footnote, or in ERT. But it would make sense in a
2901 // branch, so I'm not sure what to do.
2902 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2905 case LFUN_DIALOG_SHOW_NEW_INSET:
2906 if (cmd.argument() == "bibitem")
2907 code = BIBITEM_CODE;
2908 else if (cmd.argument() == "bibtex") {
2910 // not allowed in description items
2911 enable = !inDescriptionItem(cur);
2913 else if (cmd.argument() == "box")
2915 else if (cmd.argument() == "branch")
2917 else if (cmd.argument() == "citation")
2919 else if (cmd.argument() == "ert")
2921 else if (cmd.argument() == "external")
2922 code = EXTERNAL_CODE;
2923 else if (cmd.argument() == "float")
2925 else if (cmd.argument() == "graphics")
2926 code = GRAPHICS_CODE;
2927 else if (cmd.argument() == "href")
2928 code = HYPERLINK_CODE;
2929 else if (cmd.argument() == "include")
2930 code = INCLUDE_CODE;
2931 else if (cmd.argument() == "index")
2933 else if (cmd.argument() == "index_print")
2934 code = INDEX_PRINT_CODE;
2935 else if (cmd.argument() == "listings")
2936 code = LISTINGS_CODE;
2937 else if (cmd.argument() == "mathspace")
2938 code = MATH_HULL_CODE;
2939 else if (cmd.argument() == "nomenclature")
2940 code = NOMENCL_CODE;
2941 else if (cmd.argument() == "nomencl_print")
2942 code = NOMENCL_PRINT_CODE;
2943 else if (cmd.argument() == "label")
2945 else if (cmd.argument() == "line")
2947 else if (cmd.argument() == "note")
2949 else if (cmd.argument() == "phantom")
2950 code = PHANTOM_CODE;
2951 else if (cmd.argument() == "ref")
2953 else if (cmd.argument() == "space")
2955 else if (cmd.argument() == "toc")
2957 else if (cmd.argument() == "vspace")
2959 else if (cmd.argument() == "wrap")
2963 case LFUN_ERT_INSERT:
2966 case LFUN_LISTING_INSERT:
2967 code = LISTINGS_CODE;
2968 // not allowed in description items
2969 enable = !inDescriptionItem(cur);
2971 case LFUN_FOOTNOTE_INSERT:
2974 case LFUN_TABULAR_INSERT:
2975 code = TABULAR_CODE;
2977 case LFUN_TABULAR_STYLE_INSERT:
2978 code = TABULAR_CODE;
2980 case LFUN_MARGINALNOTE_INSERT:
2983 case LFUN_FLOAT_INSERT:
2984 case LFUN_FLOAT_WIDE_INSERT:
2985 // FIXME: If there is a selection, we should check whether there
2986 // are floats in the selection, but this has performance issues, see
2987 // LFUN_CHANGE_ACCEPT/REJECT.
2989 if (inDescriptionItem(cur))
2990 // not allowed in description items
2993 InsetCode const inset_code = cur.inset().lyxCode();
2995 // algorithm floats cannot be put in another float
2996 if (to_utf8(cmd.argument()) == "algorithm") {
2997 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
3001 // for figures and tables: only allow in another
3002 // float or wrap if it is of the same type and
3003 // not a subfloat already
3004 if(cur.inset().lyxCode() == code) {
3005 InsetFloat const & ins =
3006 static_cast<InsetFloat const &>(cur.inset());
3007 enable = ins.params().type == to_utf8(cmd.argument())
3008 && !ins.params().subfloat;
3009 } else if(cur.inset().lyxCode() == WRAP_CODE) {
3010 InsetWrap const & ins =
3011 static_cast<InsetWrap const &>(cur.inset());
3012 enable = ins.params().type == to_utf8(cmd.argument());
3016 case LFUN_WRAP_INSERT:
3018 // not allowed in description items
3019 enable = !inDescriptionItem(cur);
3021 case LFUN_FLOAT_LIST_INSERT: {
3022 code = FLOAT_LIST_CODE;
3023 // not allowed in description items
3024 enable = !inDescriptionItem(cur);
3026 FloatList const & floats = cur.buffer()->params().documentClass().floats();
3027 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
3028 // make sure we know about such floats
3029 if (cit == floats.end() ||
3030 // and that we know how to generate a list of them
3031 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
3032 flag.setUnknown(true);
3033 // probably not necessary, but...
3039 case LFUN_CAPTION_INSERT: {
3040 code = CAPTION_CODE;
3041 string arg = cmd.getArg(0);
3042 bool varia = arg != "Unnumbered"
3043 && cur.inset().allowsCaptionVariation(arg);
3044 // not allowed in description items,
3045 // and in specific insets
3046 enable = !inDescriptionItem(cur)
3047 && (varia || arg.empty() || arg == "Standard");
3050 case LFUN_NOTE_INSERT:
3053 case LFUN_FLEX_INSERT: {
3055 string s = cmd.getArg(0);
3057 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
3058 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
3059 il.lyxtype() != InsetLayout::CUSTOM &&
3060 il.lyxtype() != InsetLayout::ELEMENT &&
3061 il.lyxtype ()!= InsetLayout::STANDARD)
3065 case LFUN_BOX_INSERT:
3068 case LFUN_BRANCH_INSERT:
3070 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
3071 && cur.buffer()->params().branchlist().empty())
3074 case LFUN_IPA_INSERT:
3077 case LFUN_PHANTOM_INSERT:
3078 code = PHANTOM_CODE;
3080 case LFUN_LABEL_INSERT:
3083 case LFUN_INFO_INSERT:
3085 enable = cmd.argument().empty()
3086 || infoparams.validateArgument(cur.buffer(), cmd.argument(), true);
3088 case LFUN_ARGUMENT_INSERT: {
3090 allow_in_passthru = true;
3091 string const arg = cmd.getArg(0);
3096 Layout const & lay = cur.paragraph().layout();
3097 Layout::LaTeXArgMap args = lay.args();
3098 Layout::LaTeXArgMap::const_iterator const lait =
3100 if (lait != args.end()) {
3102 pit_type pit = cur.pit();
3103 pit_type lastpit = cur.pit();
3104 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
3105 // In a sequence of "merged" environment layouts, we only allow
3106 // non-item arguments once.
3107 lastpit = cur.lastpit();
3108 // get the first paragraph in sequence with this layout
3109 depth_type const current_depth = cur.paragraph().params().depth();
3113 Paragraph cpar = pars_[pit - 1];
3114 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
3120 for (; pit <= lastpit; ++pit) {
3121 if (pars_[pit].layout() != lay)
3123 for (auto const & table : pars_[pit].insetList())
3124 if (InsetArgument const * ins = table.inset->asInsetArgument())
3125 if (ins->name() == arg) {
3126 // we have this already
3135 case LFUN_INDEX_INSERT:
3138 case LFUN_INDEX_PRINT:
3139 code = INDEX_PRINT_CODE;
3140 // not allowed in description items
3141 enable = !inDescriptionItem(cur);
3143 case LFUN_NOMENCL_INSERT:
3144 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3148 code = NOMENCL_CODE;
3150 case LFUN_NOMENCL_PRINT:
3151 code = NOMENCL_PRINT_CODE;
3152 // not allowed in description items
3153 enable = !inDescriptionItem(cur);
3155 case LFUN_HREF_INSERT:
3156 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3160 code = HYPERLINK_CODE;
3162 case LFUN_IPAMACRO_INSERT: {
3163 string const arg = cmd.getArg(0);
3165 code = IPADECO_CODE;
3167 code = IPACHAR_CODE;
3170 case LFUN_QUOTE_INSERT:
3171 // always allow this, since we will inset a raw quote
3172 // if an inset is not allowed.
3173 allow_in_passthru = true;
3175 case LFUN_SPECIALCHAR_INSERT:
3176 code = SPECIALCHAR_CODE;
3178 case LFUN_SPACE_INSERT:
3179 // slight hack: we know this is allowed in math mode
3183 case LFUN_PREVIEW_INSERT:
3184 code = PREVIEW_CODE;
3186 case LFUN_SCRIPT_INSERT:
3190 case LFUN_MATH_INSERT:
3191 case LFUN_MATH_AMS_MATRIX:
3192 case LFUN_MATH_MATRIX:
3193 case LFUN_MATH_DELIM:
3194 case LFUN_MATH_BIGDELIM:
3195 case LFUN_MATH_DISPLAY:
3196 case LFUN_MATH_MODE:
3197 case LFUN_MATH_MACRO:
3198 case LFUN_MATH_SUBSCRIPT:
3199 case LFUN_MATH_SUPERSCRIPT:
3200 code = MATH_HULL_CODE;
3203 case LFUN_REGEXP_MODE:
3204 code = MATH_HULL_CODE;
3205 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3208 case LFUN_INSET_MODIFY:
3209 // We need to disable this, because we may get called for a
3211 // InsetTabular::getStatus() -> InsetText::getStatus()
3212 // and we don't handle LFUN_INSET_MODIFY.
3216 case LFUN_FONT_EMPH:
3217 flag.setOnOff(fontinfo.emph() == FONT_ON);
3218 enable = !cur.paragraph().isPassThru();
3221 case LFUN_FONT_ITAL:
3222 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3223 enable = !cur.paragraph().isPassThru();
3226 case LFUN_FONT_NOUN:
3227 flag.setOnOff(fontinfo.noun() == FONT_ON);
3228 enable = !cur.paragraph().isPassThru();
3231 case LFUN_FONT_BOLD:
3232 case LFUN_FONT_BOLDSYMBOL:
3233 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3234 enable = !cur.paragraph().isPassThru();
3237 case LFUN_FONT_SANS:
3238 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3239 enable = !cur.paragraph().isPassThru();
3242 case LFUN_FONT_ROMAN:
3243 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3244 enable = !cur.paragraph().isPassThru();
3247 case LFUN_FONT_TYPEWRITER:
3248 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3249 enable = !cur.paragraph().isPassThru();
3254 enable = cur.selection();
3258 if (cmd.argument().empty()) {
3259 if (theClipboard().isInternal())
3260 enable = cap::numberOfSelections() > 0;
3262 enable = !theClipboard().empty();
3266 // we have an argument
3267 string const arg = to_utf8(cmd.argument());
3268 if (isStrUnsignedInt(arg)) {
3269 // it's a number and therefore means the internal stack
3270 unsigned int n = convert<unsigned int>(arg);
3271 enable = cap::numberOfSelections() > n;
3275 // explicit text type?
3276 if (arg == "html") {
3277 // Do not enable for PlainTextType, since some tidying in the
3278 // frontend is needed for HTML, which is too unsafe for plain text.
3279 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3281 } else if (arg == "latex") {
3282 // LaTeX is usually not available on the clipboard with
3283 // the correct MIME type, but in plain text.
3284 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3285 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3289 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3291 type = Clipboard::PdfGraphicsType;
3292 else if (arg == "png")
3293 type = Clipboard::PngGraphicsType;
3294 else if (arg == "jpeg")
3295 type = Clipboard::JpegGraphicsType;
3296 else if (arg == "linkback")
3297 type = Clipboard::LinkBackGraphicsType;
3298 else if (arg == "emf")
3299 type = Clipboard::EmfGraphicsType;
3300 else if (arg == "wmf")
3301 type = Clipboard::WmfGraphicsType;
3304 LYXERR0("Unrecognized graphics type: " << arg);
3305 // we don't want to assert if the user just mistyped the LFUN
3306 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3310 enable = theClipboard().hasGraphicsContents(type);
3314 case LFUN_CLIPBOARD_PASTE:
3315 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3316 enable = !theClipboard().empty();
3319 case LFUN_PRIMARY_SELECTION_PASTE:
3320 enable = cur.selection() || !theSelection().empty();
3323 case LFUN_SELECTION_PASTE:
3324 enable = cap::selection();
3327 case LFUN_PARAGRAPH_MOVE_UP:
3328 enable = cur.pit() > 0 && !cur.selection();
3331 case LFUN_PARAGRAPH_MOVE_DOWN:
3332 enable = cur.pit() < cur.lastpit() && !cur.selection();
3335 case LFUN_CHANGE_ACCEPT:
3336 case LFUN_CHANGE_REJECT:
3337 if (!cur.selection())
3338 enable = cur.paragraph().isChanged(cur.pos());
3340 // will enable if there is a change in the selection
3343 // cheap improvement for efficiency: using cached
3344 // buffer variable, if there is no change in the
3345 // document, no need to check further.
3346 if (!cur.buffer()->areChangesPresent())
3349 for (DocIterator it = cur.selectionBegin(); ; it.forwardPar()) {
3350 pos_type const beg = it.pos();
3352 bool const in_last_par = (it.pit() == cur.selectionEnd().pit() &&
3353 it.idx() == cur.selectionEnd().idx());
3355 end = cur.selectionEnd().pos();
3357 // the +1 is needed for cases, e.g., where there is a
3358 // paragraph break. See #11629.
3359 end = it.lastpos() + 1;
3360 if (beg != end && it.paragraph().isChanged(beg, end)) {
3370 case LFUN_OUTLINE_UP:
3371 case LFUN_OUTLINE_DOWN:
3372 case LFUN_OUTLINE_IN:
3373 case LFUN_OUTLINE_OUT:
3374 // FIXME: LyX is not ready for outlining within inset.
3375 enable = isMainText()
3376 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3379 case LFUN_NEWLINE_INSERT:
3380 // LaTeX restrictions (labels or empty par)
3381 enable = !cur.paragraph().isPassThru()
3382 && cur.pos() > cur.paragraph().beginOfBody();
3385 case LFUN_SEPARATOR_INSERT:
3386 // Always enabled for now
3390 case LFUN_TAB_INSERT:
3391 case LFUN_TAB_DELETE:
3392 enable = cur.paragraph().isPassThru();
3395 case LFUN_GRAPHICS_SET_GROUP: {
3396 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3400 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3404 case LFUN_NEWPAGE_INSERT:
3405 // not allowed in description items
3406 code = NEWPAGE_CODE;
3407 enable = !inDescriptionItem(cur);
3411 enable = !cur.paragraph().isPassThru();
3412 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3415 case LFUN_PARAGRAPH_BREAK:
3416 enable = inset().allowMultiPar();
3419 case LFUN_SPELLING_ADD:
3420 case LFUN_SPELLING_IGNORE:
3421 case LFUN_SPELLING_REMOVE:
3422 enable = theSpellChecker() != NULL;
3423 if (enable && !cmd.getArg(1).empty()) {
3424 // validate explicitly given language
3425 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3426 enable &= lang != NULL;
3431 case LFUN_LAYOUT_TOGGLE: {
3432 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3433 docstring const req_layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3434 docstring const layout = resolveLayout(req_layout, cur);
3436 enable = !owner_->forcePlainLayout() && !layout.empty();
3437 flag.setOnOff(isAlreadyLayout(layout, cur));
3441 case LFUN_ENVIRONMENT_SPLIT: {
3442 if (cmd.argument() == "outer") {
3443 // check if we have an environment in our nesting hierarchy
3445 depth_type const current_depth = cur.paragraph().params().depth();
3446 pit_type pit = cur.pit();
3447 Paragraph cpar = pars_[pit];
3449 if (pit == 0 || cpar.params().depth() == 0)
3453 if (cpar.params().depth() < current_depth)
3454 res = cpar.layout().isEnvironment();
3459 else if (cmd.argument() == "previous") {
3460 // look if we have an environment in the previous par
3461 pit_type pit = cur.pit();
3462 Paragraph cpar = pars_[pit];
3466 enable = cpar.layout().isEnvironment();
3472 else if (cur.paragraph().layout().isEnvironment()) {
3473 enable = cmd.argument() == "before"
3474 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3481 case LFUN_LAYOUT_PARAGRAPH:
3482 case LFUN_PARAGRAPH_PARAMS:
3483 case LFUN_PARAGRAPH_PARAMS_APPLY:
3484 case LFUN_PARAGRAPH_UPDATE:
3485 enable = owner_->allowParagraphCustomization();
3488 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3489 // Because they insert COMBINING DIACRITICAL Unicode characters,
3490 // that cannot be handled by LaTeX but must be converted according
3491 // to the definition in lib/unicodesymbols?
3492 case LFUN_ACCENT_ACUTE:
3493 case LFUN_ACCENT_BREVE:
3494 case LFUN_ACCENT_CARON:
3495 case LFUN_ACCENT_CEDILLA:
3496 case LFUN_ACCENT_CIRCLE:
3497 case LFUN_ACCENT_CIRCUMFLEX:
3498 case LFUN_ACCENT_DOT:
3499 case LFUN_ACCENT_GRAVE:
3500 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3501 case LFUN_ACCENT_MACRON:
3502 case LFUN_ACCENT_OGONEK:
3503 case LFUN_ACCENT_TIE:
3504 case LFUN_ACCENT_TILDE:
3505 case LFUN_ACCENT_PERISPOMENI:
3506 case LFUN_ACCENT_UMLAUT:
3507 case LFUN_ACCENT_UNDERBAR:
3508 case LFUN_ACCENT_UNDERDOT:
3509 case LFUN_FONT_FRAK:
3510 case LFUN_FONT_SIZE:
3511 case LFUN_FONT_STATE:
3512 case LFUN_FONT_UNDERLINE:
3513 case LFUN_FONT_STRIKEOUT:
3514 case LFUN_FONT_CROSSOUT:
3515 case LFUN_FONT_UNDERUNDERLINE:
3516 case LFUN_FONT_UNDERWAVE:
3517 case LFUN_TEXTSTYLE_UPDATE:
3518 enable = !cur.paragraph().isPassThru();
3521 case LFUN_FONT_DEFAULT: {
3522 Font font(inherit_font, ignore_language);
3523 BufferParams const & bp = cur.buffer()->masterParams();
3524 if (cur.selection()) {
3526 // Check if we have a non-default font attribute
3527 // in the selection range.
3528 DocIterator const from = cur.selectionBegin();
3529 DocIterator const to = cur.selectionEnd();
3530 for (DocIterator dit = from ; dit != to && !dit.atEnd(); ) {
3531 if (!dit.inTexted()) {
3535 Paragraph const & par = dit.paragraph();
3536 pos_type const pos = dit.pos();
3537 Font tmp = par.getFontSettings(bp, pos);
3538 if (tmp.fontInfo() != font.fontInfo()
3539 || tmp.language() != bp.language) {
3547 // Disable if all is default already.
3548 enable = (cur.current_font.fontInfo() != font.fontInfo()
3549 || cur.current_font.language() != bp.language);
3553 case LFUN_TEXTSTYLE_APPLY:
3554 enable = !freeFonts.empty();
3557 case LFUN_WORD_DELETE_FORWARD:
3558 case LFUN_WORD_DELETE_BACKWARD:
3559 case LFUN_LINE_DELETE_FORWARD:
3560 case LFUN_WORD_FORWARD:
3561 case LFUN_WORD_BACKWARD:
3562 case LFUN_WORD_RIGHT:
3563 case LFUN_WORD_LEFT:
3564 case LFUN_CHAR_FORWARD:
3565 case LFUN_CHAR_FORWARD_SELECT:
3566 case LFUN_CHAR_BACKWARD:
3567 case LFUN_CHAR_BACKWARD_SELECT:
3568 case LFUN_CHAR_LEFT:
3569 case LFUN_CHAR_LEFT_SELECT:
3570 case LFUN_CHAR_RIGHT:
3571 case LFUN_CHAR_RIGHT_SELECT:
3573 case LFUN_UP_SELECT:
3575 case LFUN_DOWN_SELECT:
3576 case LFUN_PARAGRAPH_SELECT:
3577 case LFUN_PARAGRAPH_UP_SELECT:
3578 case LFUN_PARAGRAPH_DOWN_SELECT:
3579 case LFUN_LINE_BEGIN_SELECT:
3580 case LFUN_LINE_END_SELECT:
3581 case LFUN_WORD_FORWARD_SELECT:
3582 case LFUN_WORD_BACKWARD_SELECT:
3583 case LFUN_WORD_RIGHT_SELECT:
3584 case LFUN_WORD_LEFT_SELECT:
3585 case LFUN_WORD_SELECT:
3586 case LFUN_SECTION_SELECT:
3587 case LFUN_BUFFER_BEGIN:
3588 case LFUN_BUFFER_END:
3589 case LFUN_BUFFER_BEGIN_SELECT:
3590 case LFUN_BUFFER_END_SELECT:
3591 case LFUN_INSET_BEGIN:
3592 case LFUN_INSET_END:
3593 case LFUN_INSET_BEGIN_SELECT:
3594 case LFUN_INSET_END_SELECT:
3595 case LFUN_PARAGRAPH_UP:
3596 case LFUN_PARAGRAPH_DOWN:
3597 case LFUN_LINE_BEGIN:
3599 case LFUN_CHAR_DELETE_FORWARD:
3600 case LFUN_CHAR_DELETE_BACKWARD:
3601 case LFUN_WORD_UPCASE:
3602 case LFUN_WORD_LOWCASE:
3603 case LFUN_WORD_CAPITALIZE:
3604 case LFUN_CHARS_TRANSPOSE:
3605 case LFUN_SERVER_GET_XY:
3606 case LFUN_SERVER_SET_XY:
3607 case LFUN_SERVER_GET_LAYOUT:
3608 case LFUN_SELF_INSERT:
3609 case LFUN_UNICODE_INSERT:
3610 case LFUN_THESAURUS_ENTRY:
3612 case LFUN_SERVER_GET_STATISTICS:
3613 // these are handled in our dispatch()
3617 case LFUN_INSET_INSERT: {
3618 string const type = cmd.getArg(0);
3619 if (type == "toc") {
3621 // not allowed in description items
3622 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3623 enable = !inDescriptionItem(cur);
3630 case LFUN_SEARCH_IGNORE: {
3631 bool const value = cmd.getArg(1) == "true";
3632 setIgnoreFormat(cmd.getArg(0), value);
3642 || !cur.inset().insetAllowed(code)
3643 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3646 flag.setEnabled(enable);
3651 void Text::pasteString(Cursor & cur, docstring const & clip,
3654 if (!clip.empty()) {
3657 insertStringAsParagraphs(cur, clip, cur.current_font);
3659 insertStringAsLines(cur, clip, cur.current_font);
3664 // FIXME: an item inset would make things much easier.
3665 bool Text::inDescriptionItem(Cursor & cur) const
3667 Paragraph & par = cur.paragraph();
3668 pos_type const pos = cur.pos();
3669 pos_type const body_pos = par.beginOfBody();
3671 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3672 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3673 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3676 return (pos < body_pos
3678 && (pos == 0 || par.getChar(pos - 1) != ' ')));
3682 std::vector<docstring> Text::getFreeFonts() const
3684 vector<docstring> ffList;
3686 FontStack::const_iterator cit = freeFonts.begin();
3687 FontStack::const_iterator end = freeFonts.end();
3688 for (; cit != end; ++cit)
3689 // we do not use cit-> here because gcc 2.9x does not
3691 ffList.push_back((*cit).first);