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/InsetIPAMacro.h"
64 #include "insets/InsetNewline.h"
65 #include "insets/InsetQuotes.h"
66 #include "insets/InsetSpecialChar.h"
67 #include "insets/InsetText.h"
68 #include "insets/InsetWrap.h"
70 #include "support/convert.h"
71 #include "support/debug.h"
72 #include "support/gettext.h"
73 #include "support/lassert.h"
74 #include "support/lstrings.h"
75 #include "support/lyxalgo.h"
76 #include "support/lyxtime.h"
77 #include "support/os.h"
78 #include "support/regex.h"
80 #include "mathed/InsetMathHull.h"
81 #include "mathed/InsetMathMacroTemplate.h"
87 using namespace lyx::support;
91 using cap::copySelection;
92 using cap::cutSelection;
93 using cap::cutSelectionToTemp;
94 using cap::pasteFromStack;
95 using cap::pasteFromTemp;
96 using cap::pasteClipboardText;
97 using cap::pasteClipboardGraphics;
98 using cap::replaceSelection;
99 using cap::grabAndEraseSelection;
100 using cap::selClearOrDel;
101 using cap::pasteSimpleText;
102 using frontend::Clipboard;
105 static Font freefont(ignore_font, ignore_language);
106 static bool toggleall = false;
108 static void toggleAndShow(Cursor & cur, Text * text,
109 Font const & font, bool toggleall = true)
111 text->toggleFree(cur, font, toggleall);
113 if (font.language() != ignore_language ||
114 font.fontInfo().number() != FONT_IGNORE) {
115 TextMetrics const & tm = cur.bv().textMetrics(text);
116 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
117 cur.real_current_font))
118 text->setCursor(cur, cur.pit(), cur.pos(),
119 false, !cur.boundary());
124 static void moveCursor(Cursor & cur, bool selecting)
126 if (selecting || cur.mark())
131 static void finishChange(Cursor & cur, bool selecting)
134 moveCursor(cur, selecting);
138 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
141 docstring sel = cur.selectionAsString(false);
143 // It may happen that sel is empty but there is a selection
144 replaceSelection(cur);
146 // Is this a valid formula?
150 #ifdef ENABLE_ASSERTIONS
151 const int old_pos = cur.pos();
153 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
154 #ifdef ENABLE_ASSERTIONS
155 LATTEST(old_pos == cur.pos());
157 cur.nextInset()->edit(cur, true);
158 if (cmd.action() != LFUN_MATH_MODE)
159 // LFUN_MATH_MODE has a different meaning in math mode
162 InsetMathHull * formula = new InsetMathHull(cur.buffer());
163 string const selstr = to_utf8(sel);
164 istringstream is(selstr);
167 if (!formula->readQuiet(lex)) {
168 // No valid formula, let's try with delims
169 is.str("$" + selstr + "$");
171 if (!formula->readQuiet(lex)) {
172 // Still not valid, leave it as is
180 cur.nextInset()->edit(cur, true);
181 LASSERT(cur.inMathed(), return);
185 cur.pos() = cur.lastpos();
186 if (cmd.action() != LFUN_MATH_MODE)
187 // LFUN_MATH_MODE has a different meaning in math mode
189 cur.clearSelection();
190 cur.pos() = cur.lastpos();
194 cur.message(from_utf8(N_("Math editor mode")));
196 cur.message(from_utf8(N_("No valid math formula")));
200 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
202 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
203 if (cur.inRegexped()) {
204 cur.message(_("Already in regular expression mode"));
208 docstring sel = cur.selectionAsString(false);
210 // It may happen that sel is empty but there is a selection
211 replaceSelection(cur);
213 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
214 cur.nextInset()->edit(cur, true);
217 cur.message(_("Regexp editor mode"));
221 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
224 cap::replaceSelection(cur);
225 cur.insert(new InsetSpecialChar(kind));
230 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
233 cap::replaceSelection(cur);
234 cur.insert(new InsetIPAChar(kind));
239 static bool doInsertInset(Cursor & cur, Text * text,
240 FuncRequest const & cmd, bool edit, bool pastesel)
242 Buffer & buffer = cur.bv().buffer();
243 BufferParams const & bparams = buffer.params();
244 Inset * inset = createInset(&buffer, cmd);
248 if (InsetCollapsible * ci = inset->asInsetCollapsible())
249 ci->setButtonLabel();
252 if (cmd.action() == LFUN_INDEX_INSERT) {
253 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
254 text->insertInset(cur, inset);
256 inset->edit(cur, true);
257 // Now put this into inset
258 Font const f(inherit_font, cur.current_font.language());
260 cur.text()->insertStringAsLines(cur, ds, f);
261 cur.leaveInset(*inset);
265 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
266 bool cotextinsert = false;
267 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
268 Layout const & lay = cur.paragraph().layout();
269 Layout::LaTeXArgMap args = lay.args();
270 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
271 if (lait != args.end())
272 cotextinsert = (*lait).second.insertcotext;
274 InsetLayout const & il = cur.inset().getLayout();
276 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
277 if (ilait != args.end())
278 cotextinsert = (*ilait).second.insertcotext;
280 // The argument requests to insert a copy of the co-text to the inset
283 // If we have a selection within a paragraph, use this
284 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
285 ds = cur.selectionAsString(false);
286 // else use the whole paragraph
288 ds = cur.paragraph().asString();
289 text->insertInset(cur, inset);
291 inset->edit(cur, true);
292 // Now put co-text into inset
293 Font const f(inherit_font, cur.current_font.language());
295 cur.text()->insertStringAsLines(cur, ds, f);
296 cur.leaveInset(*inset);
303 if (cur.selection()) {
304 cutSelectionToTemp(cur, false, pastesel);
305 cur.clearSelection();
308 text->insertInset(cur, inset);
311 inset->edit(cur, true);
313 if (!gotsel || !pastesel)
316 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
317 cur.buffer()->errors("Paste");
318 cur.clearSelection(); // bug 393
320 InsetText * inset_text = inset->asInsetText();
322 inset_text->fixParagraphsFont();
323 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
324 // reset first par to default
325 cur.text()->paragraphs().begin()
326 ->setPlainOrDefaultLayout(bparams.documentClass());
329 // Merge multiple paragraphs -- hack
330 while (cur.lastpit() > 0)
331 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
332 if (cmd.action() == LFUN_FLEX_INSERT)
335 cur.leaveInset(*inset);
336 if (cmd.action() == LFUN_PREVIEW_INSERT
337 || cmd.action() == LFUN_IPA_INSERT)
339 notifyCursorLeavesOrEnters(old, cur);
342 cur.leaveInset(*inset);
343 // reset surrounding par to default
344 DocumentClass const & dc = bparams.documentClass();
345 docstring const layoutname = inset->usePlainLayout()
346 ? dc.plainLayoutName()
347 : dc.defaultLayoutName();
348 text->setLayout(cur, layoutname);
354 string const freefont2string()
356 return freefont.toString(toggleall);
360 /// the type of outline operation
362 OutlineUp, // Move this header with text down
363 OutlineDown, // Move this header with text up
364 OutlineIn, // Make this header deeper
365 OutlineOut // Make this header shallower
369 static void outline(OutlineOp mode, Cursor & cur)
371 Buffer & buf = *cur.buffer();
372 pit_type & pit = cur.pit();
373 ParagraphList & pars = buf.text().paragraphs();
374 ParagraphList::iterator const bgn = pars.begin();
375 // The first paragraph of the area to be copied:
376 ParagraphList::iterator start = lyx::next(bgn, pit);
377 // The final paragraph of area to be copied:
378 ParagraphList::iterator finish = start;
379 ParagraphList::iterator const end = pars.end();
381 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
384 // Move out (down) from this section header
388 // Seek the one (on same level) below
389 for (; finish != end; ++finish) {
390 toclevel = buf.text().getTocLevel(distance(bgn, finish));
391 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
397 if (start == pars.begin())
400 ParagraphList::iterator dest = start;
401 // Move out (up) from this header
404 // Search previous same-level header above
407 toclevel = buf.text().getTocLevel(distance(bgn, dest));
409 && (toclevel == Layout::NOT_IN_TOC
410 || toclevel > thistoclevel));
411 // Not found; do nothing
412 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
414 // If we move an environment upwards, make sure it is
415 // separated from its new neighbour below.
416 ParagraphList::iterator lastmoved = finish;
418 if (start->layout().isEnvironment()
419 && dest->layout() == start->layout()
420 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
421 cur.pit() = distance(bgn, finish);
423 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
424 DocumentClass const & tc = buf.params().documentClass();
425 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
426 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
430 pit_type const newpit = distance(bgn, dest);
431 pit_type const len = distance(start, finish);
432 pit_type const deletepit = pit + len;
433 buf.undo().recordUndo(cur, newpit, deletepit - 1);
434 pars.splice(dest, start, finish);
442 // Go one down from *this* header:
443 ParagraphList::iterator dest = next(finish, 1);
444 // Go further down to find header to insert in front of:
445 for (; dest != end; ++dest) {
446 toclevel = buf.text().getTocLevel(distance(bgn, dest));
447 if (toclevel != Layout::NOT_IN_TOC
448 && toclevel <= thistoclevel)
451 // One such was found:
452 pit_type newpit = distance(bgn, dest);
453 buf.undo().recordUndo(cur, pit, newpit - 1);
454 pit_type const len = distance(start, finish);
455 pars.splice(dest, start, finish);
456 cur.pit() = newpit - len;
461 pit_type const len = distance(start, finish);
462 buf.undo().recordUndo(cur, pit, pit + len - 1);
463 for (; start != finish; ++start) {
464 toclevel = buf.text().getTocLevel(distance(bgn, start));
465 if (toclevel == Layout::NOT_IN_TOC)
468 DocumentClass const & tc = buf.params().documentClass();
469 DocumentClass::const_iterator lit = tc.begin();
470 DocumentClass::const_iterator len = tc.end();
471 int const newtoclevel =
472 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
473 LabelType const oldlabeltype = start->layout().labeltype;
475 for (; lit != len; ++lit) {
476 if (lit->toclevel == newtoclevel &&
477 lit->labeltype == oldlabeltype) {
478 start->setLayout(*lit);
489 void Text::number(Cursor & cur)
491 FontInfo font = ignore_font;
492 font.setNumber(FONT_TOGGLE);
493 toggleAndShow(cur, this, Font(font, ignore_language));
497 bool Text::isRTL(Paragraph const & par) const
499 Buffer const & buffer = owner_->buffer();
500 return par.isRTL(buffer.params());
506 Language const * getLanguage(Cursor const & cur, string const & lang) {
507 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
513 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
515 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
517 // Dispatch if the cursor is inside the text. It is not the
518 // case for context menus (bug 5797).
519 if (cur.text() != this) {
524 BufferView * bv = &cur.bv();
525 TextMetrics * tm = &bv->textMetrics(this);
526 if (!tm->contains(cur.pit())) {
527 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
528 tm = &bv->textMetrics(this);
531 // FIXME: We use the update flag to indicates wether a singlePar or a
532 // full screen update is needed. We reset it here but shall we restore it
534 cur.noScreenUpdate();
536 LBUFERR(this == cur.text());
538 // NOTE: This should NOT be a reference. See commit 94a5481a.
539 CursorSlice const oldTopSlice = cur.top();
540 bool const oldBoundary = cur.boundary();
541 bool const oldSelection = cur.selection();
542 // Signals that, even if needsUpdate == false, an update of the
543 // cursor paragraph is required
544 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
545 LyXAction::SingleParUpdate);
546 // Signals that a full-screen update is required
547 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
548 LyXAction::NoUpdate) || singleParUpdate);
549 bool const last_misspelled = lyxrc.spellcheck_continuously
550 && cur.paragraph().isMisspelled(cur.pos(), true);
552 FuncCode const act = cmd.action();
555 case LFUN_PARAGRAPH_MOVE_DOWN: {
556 pit_type const pit = cur.pit();
557 cur.recordUndo(pit, pit + 1);
559 pars_.swap(pit, pit + 1);
561 cur.forceBufferUpdate();
566 case LFUN_PARAGRAPH_MOVE_UP: {
567 pit_type const pit = cur.pit();
568 cur.recordUndo(pit - 1, pit);
570 pars_.swap(pit, pit - 1);
573 cur.forceBufferUpdate();
577 case LFUN_APPENDIX: {
578 Paragraph & par = cur.paragraph();
579 bool start = !par.params().startOfAppendix();
581 // FIXME: The code below only makes sense at top level.
582 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
583 // ensure that we have only one start_of_appendix in this document
584 // FIXME: this don't work for multipart document!
585 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
586 if (pars_[tmp].params().startOfAppendix()) {
587 cur.recordUndo(tmp, tmp);
588 pars_[tmp].params().startOfAppendix(false);
594 par.params().startOfAppendix(start);
596 // we can set the refreshing parameters now
597 cur.forceBufferUpdate();
601 case LFUN_WORD_DELETE_FORWARD:
603 cutSelection(cur, true, false);
605 deleteWordForward(cur, cmd.getArg(0) == "force");
606 finishChange(cur, false);
609 case LFUN_WORD_DELETE_BACKWARD:
611 cutSelection(cur, true, false);
613 deleteWordBackward(cur, cmd.getArg(0) == "force");
614 finishChange(cur, false);
617 case LFUN_LINE_DELETE_FORWARD:
619 cutSelection(cur, true, false);
621 tm->deleteLineForward(cur);
622 finishChange(cur, false);
625 case LFUN_BUFFER_BEGIN:
626 case LFUN_BUFFER_BEGIN_SELECT:
627 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
628 if (cur.depth() == 1)
629 needsUpdate |= cursorTop(cur);
632 cur.screenUpdateFlags(Update::FitCursor);
635 case LFUN_BUFFER_END:
636 case LFUN_BUFFER_END_SELECT:
637 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
638 if (cur.depth() == 1)
639 needsUpdate |= cursorBottom(cur);
642 cur.screenUpdateFlags(Update::FitCursor);
645 case LFUN_INSET_BEGIN:
646 case LFUN_INSET_BEGIN_SELECT:
647 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
648 if (cur.depth() == 1 || !cur.top().at_begin())
649 needsUpdate |= cursorTop(cur);
652 cur.screenUpdateFlags(Update::FitCursor);
656 case LFUN_INSET_END_SELECT:
657 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
658 if (cur.depth() == 1 || !cur.top().at_end())
659 needsUpdate |= cursorBottom(cur);
662 cur.screenUpdateFlags(Update::FitCursor);
665 case LFUN_CHAR_FORWARD:
666 case LFUN_CHAR_FORWARD_SELECT: {
667 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
668 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
669 bool const cur_moved = cursorForward(cur);
670 needsUpdate |= cur_moved;
672 if (!cur_moved && oldTopSlice == cur.top()
673 && cur.boundary() == oldBoundary) {
675 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
677 // we will probably be moving out the inset, so we should execute
678 // the depm-mechanism, but only when the cursor has a place to
679 // go outside this inset, i.e. in a slice above.
680 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
681 && cur.pit() == cur.lastpit()) {
682 // The cursor hasn't changed yet. To give the
683 // DEPM the possibility of doing something we must
684 // provide it with two different cursors.
686 dummy.pos() = dummy.pit() = 0;
687 if (cur.bv().checkDepm(dummy, cur))
688 cur.forceBufferUpdate();
694 case LFUN_CHAR_BACKWARD:
695 case LFUN_CHAR_BACKWARD_SELECT: {
696 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
697 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
698 bool const cur_moved = cursorBackward(cur);
699 needsUpdate |= cur_moved;
701 if (!cur_moved && oldTopSlice == cur.top()
702 && cur.boundary() == oldBoundary) {
704 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
706 // we will probably be moving out the inset, so we should execute
707 // the depm-mechanism, but only when the cursor has a place to
708 // go outside this inset, i.e. in a slice above.
709 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
710 // The cursor hasn't changed yet. To give the
711 // DEPM the possibility of doing something we must
712 // provide it with two different cursors.
714 dummy.pos() = cur.lastpos();
715 dummy.pit() = cur.lastpit();
716 if (cur.bv().checkDepm(dummy, cur))
717 cur.forceBufferUpdate();
724 case LFUN_CHAR_LEFT_SELECT:
725 if (lyxrc.visual_cursor) {
726 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
727 bool const cur_moved = cursorVisLeft(cur);
728 needsUpdate |= cur_moved;
729 if (!cur_moved && oldTopSlice == cur.top()
730 && cur.boundary() == oldBoundary) {
732 cmd = FuncRequest(LFUN_FINISHED_LEFT);
735 if (cur.reverseDirectionNeeded()) {
736 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
737 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
739 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
740 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
747 case LFUN_CHAR_RIGHT:
748 case LFUN_CHAR_RIGHT_SELECT:
749 if (lyxrc.visual_cursor) {
750 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
751 bool const cur_moved = cursorVisRight(cur);
752 needsUpdate |= cur_moved;
753 if (!cur_moved && oldTopSlice == cur.top()
754 && cur.boundary() == oldBoundary) {
756 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
759 if (cur.reverseDirectionNeeded()) {
760 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
761 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
763 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
764 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
773 case LFUN_DOWN_SELECT:
776 // stop/start the selection
777 bool select = cmd.action() == LFUN_DOWN_SELECT ||
778 cmd.action() == LFUN_UP_SELECT;
780 // move cursor up/down
781 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
782 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
784 if (!atFirstOrLastRow) {
785 needsUpdate |= cur.selHandle(select);
786 cur.upDownInText(up, needsUpdate);
787 needsUpdate |= cur.beforeDispatchCursor().inMathed();
789 pos_type newpos = up ? 0 : cur.lastpos();
790 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
791 needsUpdate |= cur.selHandle(select);
792 // we do not reset the targetx of the cursor
794 needsUpdate |= bv->checkDepm(cur, bv->cursor());
795 cur.updateTextTargetOffset();
797 cur.forceBufferUpdate();
801 // if the cursor cannot be moved up or down do not remove
802 // the selection right now, but wait for the next dispatch.
804 needsUpdate |= cur.selHandle(select);
805 cur.upDownInText(up, needsUpdate);
812 case LFUN_PARAGRAPH_UP:
813 case LFUN_PARAGRAPH_UP_SELECT:
814 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
815 needsUpdate |= cursorUpParagraph(cur);
818 case LFUN_PARAGRAPH_DOWN:
819 case LFUN_PARAGRAPH_DOWN_SELECT:
820 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
821 needsUpdate |= cursorDownParagraph(cur);
824 case LFUN_LINE_BEGIN:
825 case LFUN_LINE_BEGIN_SELECT:
826 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
827 needsUpdate |= tm->cursorHome(cur);
831 case LFUN_LINE_END_SELECT:
832 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
833 needsUpdate |= tm->cursorEnd(cur);
836 case LFUN_SECTION_SELECT: {
837 Buffer const & buf = *cur.buffer();
838 pit_type const pit = cur.pit();
839 ParagraphList & pars = buf.text().paragraphs();
840 ParagraphList::iterator bgn = pars.begin();
841 // The first paragraph of the area to be selected:
842 ParagraphList::iterator start = lyx::next(bgn, pit);
843 // The final paragraph of area to be selected:
844 ParagraphList::iterator finish = start;
845 ParagraphList::iterator end = pars.end();
847 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
848 if (thistoclevel == Layout::NOT_IN_TOC)
852 Cursor const old_cur = cur;
853 needsUpdate |= cur.selHandle(true);
855 // Move out (down) from this section header
859 // Seek the one (on same level) below
860 for (; finish != end; ++finish, ++cur.pit()) {
861 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
862 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
865 cur.pos() = cur.lastpos();
867 cur.setCurrentFont();
869 needsUpdate |= cur != old_cur;
873 case LFUN_WORD_RIGHT:
874 case LFUN_WORD_RIGHT_SELECT:
875 if (lyxrc.visual_cursor) {
876 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
877 bool const cur_moved = cursorVisRightOneWord(cur);
878 needsUpdate |= cur_moved;
879 if (!cur_moved && oldTopSlice == cur.top()
880 && cur.boundary() == oldBoundary) {
882 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
885 if (cur.reverseDirectionNeeded()) {
886 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
887 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
889 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
890 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
897 case LFUN_WORD_FORWARD:
898 case LFUN_WORD_FORWARD_SELECT: {
899 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
900 bool const cur_moved = cursorForwardOneWord(cur);
901 needsUpdate |= cur_moved;
903 if (!cur_moved && oldTopSlice == cur.top()
904 && cur.boundary() == oldBoundary) {
906 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
908 // we will probably be moving out the inset, so we should execute
909 // the depm-mechanism, but only when the cursor has a place to
910 // go outside this inset, i.e. in a slice above.
911 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
912 && cur.pit() == cur.lastpit()) {
913 // The cursor hasn't changed yet. To give the
914 // DEPM the possibility of doing something we must
915 // provide it with two different cursors.
917 dummy.pos() = dummy.pit() = 0;
918 if (cur.bv().checkDepm(dummy, cur))
919 cur.forceBufferUpdate();
926 case LFUN_WORD_LEFT_SELECT:
927 if (lyxrc.visual_cursor) {
928 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
929 bool const cur_moved = cursorVisLeftOneWord(cur);
930 needsUpdate |= cur_moved;
931 if (!cur_moved && oldTopSlice == cur.top()
932 && cur.boundary() == oldBoundary) {
934 cmd = FuncRequest(LFUN_FINISHED_LEFT);
937 if (cur.reverseDirectionNeeded()) {
938 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
939 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
941 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
942 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
949 case LFUN_WORD_BACKWARD:
950 case LFUN_WORD_BACKWARD_SELECT: {
951 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
952 bool const cur_moved = cursorBackwardOneWord(cur);
953 needsUpdate |= cur_moved;
955 if (!cur_moved && oldTopSlice == cur.top()
956 && cur.boundary() == oldBoundary) {
958 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
960 // we will probably be moving out the inset, so we should execute
961 // the depm-mechanism, but only when the cursor has a place to
962 // go outside this inset, i.e. in a slice above.
963 if (cur.depth() > 1 && cur.pos() == 0
965 // The cursor hasn't changed yet. To give the
966 // DEPM the possibility of doing something we must
967 // provide it with two different cursors.
969 dummy.pos() = cur.lastpos();
970 dummy.pit() = cur.lastpit();
971 if (cur.bv().checkDepm(dummy, cur))
972 cur.forceBufferUpdate();
978 case LFUN_WORD_SELECT: {
979 selectWord(cur, WHOLE_WORD);
980 finishChange(cur, true);
984 case LFUN_NEWLINE_INSERT: {
985 InsetNewlineParams inp;
986 docstring arg = cmd.argument();
987 if (arg == "linebreak")
988 inp.kind = InsetNewlineParams::LINEBREAK;
990 inp.kind = InsetNewlineParams::NEWLINE;
991 cap::replaceSelection(cur);
993 cur.insert(new InsetNewline(inp));
995 moveCursor(cur, false);
999 case LFUN_TAB_INSERT: {
1000 bool const multi_par_selection = cur.selection() &&
1001 cur.selBegin().pit() != cur.selEnd().pit();
1002 if (multi_par_selection) {
1003 // If there is a multi-paragraph selection, a tab is inserted
1004 // at the beginning of each paragraph.
1005 cur.recordUndoSelection();
1006 pit_type const pit_end = cur.selEnd().pit();
1007 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1008 pars_[pit].insertChar(0, '\t',
1009 bv->buffer().params().track_changes);
1010 // Update the selection pos to make sure the selection does not
1011 // change as the inserted tab will increase the logical pos.
1012 if (cur.realAnchor().pit() == pit)
1013 cur.realAnchor().forwardPos();
1014 if (cur.pit() == pit)
1019 // Maybe we shouldn't allow tabs within a line, because they
1020 // are not (yet) aligned as one might do expect.
1021 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
1027 case LFUN_TAB_DELETE: {
1028 bool const tc = bv->buffer().params().track_changes;
1029 if (cur.selection()) {
1030 // If there is a selection, a tab (if present) is removed from
1031 // the beginning of each paragraph.
1032 cur.recordUndoSelection();
1033 pit_type const pit_end = cur.selEnd().pit();
1034 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1035 Paragraph & par = paragraphs()[pit];
1038 char_type const c = par.getChar(0);
1039 if (c == '\t' || c == ' ') {
1040 // remove either 1 tab or 4 spaces.
1041 int const n = (c == ' ' ? 4 : 1);
1042 for (int i = 0; i < n
1043 && !par.empty() && par.getChar(0) == c; ++i) {
1044 if (cur.pit() == pit)
1046 if (cur.realAnchor().pit() == pit
1047 && cur.realAnchor().pos() > 0 )
1048 cur.realAnchor().backwardPos();
1049 par.eraseChar(0, tc);
1055 // If there is no selection, try to remove a tab or some spaces
1056 // before the position of the cursor.
1057 Paragraph & par = paragraphs()[cur.pit()];
1058 pos_type const pos = cur.pos();
1063 char_type const c = par.getChar(pos - 1);
1067 par.eraseChar(cur.pos(), tc);
1069 for (int n_spaces = 0;
1071 && par.getChar(cur.pos() - 1) == ' '
1075 par.eraseChar(cur.pos(), tc);
1082 case LFUN_CHAR_DELETE_FORWARD:
1083 if (!cur.selection()) {
1084 if (cur.pos() == cur.paragraph().size())
1085 // Par boundary, force full-screen update
1086 singleParUpdate = false;
1087 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1089 cur.selection(true);
1094 needsUpdate |= erase(cur);
1097 cutSelection(cur, true, false);
1098 singleParUpdate = false;
1100 moveCursor(cur, false);
1103 case LFUN_CHAR_DELETE_BACKWARD:
1104 if (!cur.selection()) {
1105 if (bv->getIntl().getTransManager().backspace()) {
1106 bool par_boundary = cur.pos() == 0;
1107 bool first_par = cur.pit() == 0;
1108 // Par boundary, full-screen update
1110 singleParUpdate = false;
1111 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1113 cur.selection(true);
1118 needsUpdate |= backspace(cur);
1120 if (par_boundary && !first_par && cur.pos() > 0
1121 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1122 needsUpdate |= backspace(cur);
1127 cutSelection(cur, true, false);
1128 singleParUpdate = false;
1132 case LFUN_PARAGRAPH_BREAK: {
1133 cap::replaceSelection(cur);
1134 pit_type pit = cur.pit();
1135 Paragraph const & par = pars_[pit];
1136 bool lastpar = (pit == pit_type(pars_.size() - 1));
1137 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1138 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1139 if (prev < pit && cur.pos() == par.beginOfBody()
1140 && !par.size() && !par.isEnvSeparator(cur.pos())
1141 && !par.layout().isCommand()
1142 && pars_[prev].layout() != par.layout()
1143 && pars_[prev].layout().isEnvironment()
1144 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1145 if (par.layout().isEnvironment()
1146 && pars_[prev].getDepth() == par.getDepth()) {
1147 docstring const layout = par.layout().name();
1148 DocumentClass const & tc = bv->buffer().params().documentClass();
1149 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1150 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1151 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1152 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1154 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1155 breakParagraph(cur);
1157 Font const f(inherit_font, cur.current_font.language());
1158 pars_[cur.pit() - 1].resetFonts(f);
1160 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1162 breakParagraph(cur, cmd.getArg(0) == "inverse");
1165 // If we have a list and autoinsert item insets,
1167 Layout::LaTeXArgMap args = par.layout().args();
1168 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1169 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1170 for (; lait != laend; ++lait) {
1171 Layout::latexarg arg = (*lait).second;
1172 if (arg.autoinsert && prefixIs((*lait).first, "item:")) {
1173 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1180 case LFUN_INSET_INSERT: {
1183 // We have to avoid triggering InstantPreview loading
1184 // before inserting into the document. See bug #5626.
1185 bool loaded = bv->buffer().isFullyLoaded();
1186 bv->buffer().setFullyLoaded(false);
1187 Inset * inset = createInset(&bv->buffer(), cmd);
1188 bv->buffer().setFullyLoaded(loaded);
1191 // FIXME (Abdel 01/02/2006):
1192 // What follows would be a partial fix for bug 2154:
1193 // http://www.lyx.org/trac/ticket/2154
1194 // This automatically put the label inset _after_ a
1195 // numbered section. It should be possible to extend the mechanism
1196 // to any kind of LateX environement.
1197 // The correct way to fix that bug would be at LateX generation.
1198 // I'll let the code here for reference as it could be used for some
1199 // other feature like "automatic labelling".
1201 Paragraph & par = pars_[cur.pit()];
1202 if (inset->lyxCode() == LABEL_CODE
1203 && !par.layout().counter.empty()) {
1204 // Go to the end of the paragraph
1205 // Warning: Because of Change-Tracking, the last
1206 // position is 'size()' and not 'size()-1':
1207 cur.pos() = par.size();
1208 // Insert a new paragraph
1209 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1213 if (cur.selection())
1214 cutSelection(cur, true, false);
1216 cur.forceBufferUpdate();
1217 if (inset->editable() && inset->asInsetText())
1218 inset->edit(cur, true);
1222 // trigger InstantPreview now
1223 if (inset->lyxCode() == EXTERNAL_CODE) {
1224 InsetExternal & ins =
1225 static_cast<InsetExternal &>(*inset);
1226 ins.updatePreview();
1233 case LFUN_INSET_DISSOLVE: {
1234 if (dissolveInset(cur)) {
1236 cur.forceBufferUpdate();
1241 case LFUN_SET_GRAPHICS_GROUP: {
1242 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1248 string id = to_utf8(cmd.argument());
1249 string grp = graphics::getGroupParams(bv->buffer(), id);
1250 InsetGraphicsParams tmp, inspar = ins->getParams();
1253 inspar.groupId = to_utf8(cmd.argument());
1255 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1256 tmp.filename = inspar.filename;
1260 ins->setParams(inspar);
1264 case LFUN_SPACE_INSERT:
1265 if (cur.paragraph().layout().free_spacing)
1266 insertChar(cur, ' ');
1268 doInsertInset(cur, this, cmd, false, false);
1271 moveCursor(cur, false);
1274 case LFUN_SPECIALCHAR_INSERT: {
1275 string const name = to_utf8(cmd.argument());
1276 if (name == "hyphenation")
1277 specialChar(cur, InsetSpecialChar::HYPHENATION);
1278 else if (name == "allowbreak")
1279 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1280 else if (name == "ligature-break")
1281 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1282 else if (name == "slash")
1283 specialChar(cur, InsetSpecialChar::SLASH);
1284 else if (name == "nobreakdash")
1285 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1286 else if (name == "dots")
1287 specialChar(cur, InsetSpecialChar::LDOTS);
1288 else if (name == "end-of-sentence")
1289 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1290 else if (name == "menu-separator")
1291 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1292 else if (name == "lyx")
1293 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1294 else if (name == "tex")
1295 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1296 else if (name == "latex")
1297 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1298 else if (name == "latex2e")
1299 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1300 else if (name.empty())
1301 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1303 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1307 case LFUN_IPAMACRO_INSERT: {
1308 string const arg = cmd.getArg(0);
1309 if (arg == "deco") {
1310 // Open the inset, and move the current selection
1312 doInsertInset(cur, this, cmd, true, true);
1314 // Some insets are numbered, others are shown in the outline pane so
1315 // let's update the labels and the toc backend.
1316 cur.forceBufferUpdate();
1319 if (arg == "tone-falling")
1320 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1321 else if (arg == "tone-rising")
1322 ipaChar(cur, InsetIPAChar::TONE_RISING);
1323 else if (arg == "tone-high-rising")
1324 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1325 else if (arg == "tone-low-rising")
1326 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1327 else if (arg == "tone-high-rising-falling")
1328 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1329 else if (arg.empty())
1330 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1332 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1336 case LFUN_WORD_UPCASE:
1337 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1340 case LFUN_WORD_LOWCASE:
1341 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1344 case LFUN_WORD_CAPITALIZE:
1345 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1348 case LFUN_CHARS_TRANSPOSE:
1349 charsTranspose(cur);
1353 cur.message(_("Paste"));
1354 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1355 cap::replaceSelection(cur);
1357 // without argument?
1358 string const arg = to_utf8(cmd.argument());
1360 bool tryGraphics = true;
1361 if (theClipboard().isInternal())
1362 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1363 else if (theClipboard().hasTextContents()) {
1364 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1365 true, Clipboard::AnyTextType))
1366 tryGraphics = false;
1368 if (tryGraphics && theClipboard().hasGraphicsContents())
1369 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1370 } else if (isStrUnsignedInt(arg)) {
1371 // we have a numerical argument
1372 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1373 convert<unsigned int>(arg));
1374 } else if (arg == "html" || arg == "latex") {
1375 Clipboard::TextType type = (arg == "html") ?
1376 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1377 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1379 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1381 type = Clipboard::PdfGraphicsType;
1382 else if (arg == "png")
1383 type = Clipboard::PngGraphicsType;
1384 else if (arg == "jpeg")
1385 type = Clipboard::JpegGraphicsType;
1386 else if (arg == "linkback")
1387 type = Clipboard::LinkBackGraphicsType;
1388 else if (arg == "emf")
1389 type = Clipboard::EmfGraphicsType;
1390 else if (arg == "wmf")
1391 type = Clipboard::WmfGraphicsType;
1393 // we also check in getStatus()
1394 LYXERR0("Unrecognized graphics type: " << arg);
1396 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1399 bv->buffer().errors("Paste");
1400 cur.clearSelection(); // bug 393
1406 cutSelection(cur, true, true);
1407 cur.message(_("Cut"));
1412 cur.message(_("Copy"));
1415 case LFUN_SERVER_GET_XY:
1416 cur.message(from_utf8(
1417 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1418 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1421 case LFUN_SERVER_SET_XY: {
1424 istringstream is(to_utf8(cmd.argument()));
1427 lyxerr << "SETXY: Could not parse coordinates in '"
1428 << to_utf8(cmd.argument()) << endl;
1430 tm->setCursorFromCoordinates(cur, x, y);
1434 case LFUN_SERVER_GET_LAYOUT:
1435 cur.message(cur.paragraph().layout().name());
1439 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1440 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1441 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1443 Paragraph const & para = cur.paragraph();
1444 docstring const old_layout = para.layout().name();
1445 DocumentClass const & tclass = bv->buffer().params().documentClass();
1448 layout = tclass.defaultLayoutName();
1450 if (owner_->forcePlainLayout())
1451 // in this case only the empty layout is allowed
1452 layout = tclass.plainLayoutName();
1453 else if (para.usePlainLayout()) {
1454 // in this case, default layout maps to empty layout
1455 if (layout == tclass.defaultLayoutName())
1456 layout = tclass.plainLayoutName();
1458 // otherwise, the empty layout maps to the default
1459 if (layout == tclass.plainLayoutName())
1460 layout = tclass.defaultLayoutName();
1463 bool hasLayout = tclass.hasLayout(layout);
1465 // If the entry is obsolete, use the new one instead.
1467 docstring const & obs = tclass[layout].obsoleted_by();
1473 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1474 from_utf8(N_(" not known")));
1478 bool change_layout = (old_layout != layout);
1480 if (!change_layout && cur.selection() &&
1481 cur.selBegin().pit() != cur.selEnd().pit())
1483 pit_type spit = cur.selBegin().pit();
1484 pit_type epit = cur.selEnd().pit() + 1;
1485 while (spit != epit) {
1486 if (pars_[spit].layout().name() != old_layout) {
1487 change_layout = true;
1494 if (change_layout) {
1495 setLayout(cur, layout);
1496 if (cur.pit() > 0 && !ignoreautonests) {
1497 set<docstring> const & autonests =
1498 pars_[cur.pit() - 1].layout().autonests();
1499 set<docstring> const & autonested =
1500 pars_[cur.pit()].layout().isAutonestedBy();
1501 if (autonests.find(layout) != autonests.end()
1502 || autonested.find(old_layout) != autonested.end())
1503 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1507 Layout::LaTeXArgMap args = tclass[layout].args();
1508 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1509 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1510 for (; lait != laend; ++lait) {
1511 Layout::latexarg arg = (*lait).second;
1512 if (arg.autoinsert) {
1513 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1521 case LFUN_ENVIRONMENT_SPLIT: {
1522 bool const outer = cmd.argument() == "outer";
1523 bool const previous = cmd.argument() == "previous";
1524 bool const before = cmd.argument() == "before";
1525 bool const normal = cmd.argument().empty();
1526 Paragraph const & para = cur.paragraph();
1528 if (para.layout().isEnvironment())
1529 layout = para.layout().name();
1530 depth_type split_depth = cur.paragraph().params().depth();
1531 depth_type nextpar_depth = 0;
1532 if (outer || previous) {
1533 // check if we have an environment in our scope
1534 pit_type pit = cur.pit();
1535 Paragraph cpar = pars_[pit];
1541 if (layout.empty() && previous
1542 && cpar.layout().isEnvironment()
1543 && cpar.params().depth() <= split_depth)
1544 layout = cpar.layout().name();
1545 if (cpar.params().depth() < split_depth
1546 && cpar.layout().isEnvironment()) {
1548 layout = cpar.layout().name();
1549 split_depth = cpar.params().depth();
1551 if (cpar.params().depth() == 0)
1555 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1556 // save nesting of following paragraph
1557 Paragraph cpar = pars_[cur.pit() + 1];
1558 nextpar_depth = cpar.params().depth();
1561 cur.top().setPitPos(cur.pit(), 0);
1562 if (before || cur.pos() > 0)
1563 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1564 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1565 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1567 while (cur.paragraph().params().depth() > split_depth)
1568 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1570 DocumentClass const & tc = bv->buffer().params().documentClass();
1571 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1572 + from_ascii("\" ignoreautonests")));
1573 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1576 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1577 while (cur.paragraph().params().depth() < split_depth)
1578 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1581 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1582 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1583 if ((outer || normal) && nextpar_depth > 0) {
1584 // restore nesting of following paragraph
1585 DocIterator scur = cur;
1586 depth_type const max_depth = cur.paragraph().getMaxDepthAfter();
1588 while (cur.paragraph().params().depth() <= min(nextpar_depth, max_depth))
1589 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1590 cur.setCursor(scur);
1596 case LFUN_CLIPBOARD_PASTE:
1597 cap::replaceSelection(cur);
1598 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1599 cmd.argument() == "paragraph");
1600 bv->buffer().errors("Paste");
1603 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1604 cap::replaceSelection(cur);
1605 pasteSimpleText(cur, cmd.argument() == "paragraph");
1608 case LFUN_PRIMARY_SELECTION_PASTE:
1609 cap::replaceSelection(cur);
1610 pasteString(cur, theSelection().get(),
1611 cmd.argument() == "paragraph");
1614 case LFUN_SELECTION_PASTE:
1615 // Copy the selection buffer to the clipboard stack,
1616 // because we want it to appear in the "Edit->Paste
1618 cap::replaceSelection(cur);
1619 cap::copySelectionToStack();
1620 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1621 bv->buffer().errors("Paste");
1624 case LFUN_UNICODE_INSERT: {
1625 if (cmd.argument().empty())
1627 docstring hexstring = cmd.argument();
1628 if (isHex(hexstring)) {
1629 char_type c = hexToInt(hexstring);
1630 if (c >= 32 && c < 0x10ffff) {
1631 lyxerr << "Inserting c: " << c << endl;
1632 docstring s = docstring(1, c);
1633 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1639 case LFUN_QUOTE_INSERT: {
1640 cap::replaceSelection(cur);
1643 Paragraph const & par = cur.paragraph();
1644 pos_type pos = cur.pos();
1645 // Ignore deleted text before cursor
1646 while (pos > 0 && par.isDeleted(pos - 1))
1649 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1651 // Guess quote side.
1652 // A space triggers an opening quote. This is passed if the preceding
1653 // char/inset is a space or at paragraph start.
1655 if (pos > 0 && !par.isSpace(pos - 1)) {
1656 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1657 // If an opening double quotation mark precedes, and this
1658 // is a single quote, make it opening as well
1660 static_cast<InsetQuotes &>(*cur.prevInset());
1661 string const type = ins.getType();
1662 if (!suffixIs(type, "ld") || !inner)
1663 c = par.getChar(pos - 1);
1665 else if (!cur.prevInset()
1666 || (cur.prevInset() && cur.prevInset()->isChar()))
1667 // If a char precedes, pass that and let InsetQuote decide
1668 c = par.getChar(pos - 1);
1671 if (par.getInset(pos - 1)
1672 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1673 // skip "invisible" insets
1677 c = par.getChar(pos - 1);
1682 InsetQuotesParams::QuoteLevel const quote_level = inner
1683 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1684 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1685 cur.buffer()->updateBuffer();
1690 case LFUN_DATE_INSERT: {
1691 string const format = cmd.argument().empty()
1692 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1693 string const time = formatted_time(current_time(), format);
1694 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1698 case LFUN_MOUSE_TRIPLE:
1699 if (cmd.button() == mouse_button::button1) {
1700 tm->cursorHome(cur);
1708 case LFUN_MOUSE_DOUBLE:
1709 if (cmd.button() == mouse_button::button1) {
1710 selectWord(cur, WHOLE_WORD);
1715 // Single-click on work area
1716 case LFUN_MOUSE_PRESS: {
1717 // We are not marking a selection with the keyboard in any case.
1718 Cursor & bvcur = cur.bv().cursor();
1719 bvcur.setMark(false);
1720 switch (cmd.button()) {
1721 case mouse_button::button1:
1722 if (!bvcur.selection())
1724 bvcur.resetAnchor();
1725 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1726 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1727 if (bvcur.wordSelection())
1728 selectWord(bvcur, WHOLE_WORD);
1731 case mouse_button::button2:
1732 if (lyxrc.mouse_middlebutton_paste) {
1733 // Middle mouse pasting.
1734 bv->mouseSetCursor(cur);
1736 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1737 "selection-paste ; primary-selection-paste paragraph"));
1739 cur.noScreenUpdate();
1742 case mouse_button::button3: {
1743 // Don't do anything if we right-click a
1744 // selection, a context menu will popup.
1745 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1746 && cur < bvcur.selectionEnd()) {
1747 cur.noScreenUpdate();
1750 if (!bv->mouseSetCursor(cur, false))
1751 cur.screenUpdateFlags(Update::FitCursor);
1757 } // switch (cmd.button())
1760 case LFUN_MOUSE_MOTION: {
1761 // Mouse motion with right or middle mouse do nothing for now.
1762 if (cmd.button() != mouse_button::button1) {
1763 cur.noScreenUpdate();
1766 // ignore motions deeper nested than the real anchor
1767 Cursor & bvcur = cur.bv().cursor();
1768 if (!bvcur.realAnchor().hasPart(cur)) {
1772 CursorSlice old = bvcur.top();
1774 int const wh = bv->workHeight();
1775 int const y = max(0, min(wh - 1, cmd.y()));
1777 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1778 cur.setTargetX(cmd.x());
1779 // Don't allow selecting a separator inset
1780 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1783 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1784 else if (cmd.y() < 0)
1785 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1786 // This is to allow jumping over large insets
1787 if (cur.top() == old) {
1789 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1790 else if (cmd.y() < 0)
1791 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1793 // We continue with our existing selection or start a new one, so don't
1794 // reset the anchor.
1795 bvcur.setCursor(cur);
1796 bvcur.selection(true);
1797 bvcur.setCurrentFont();
1798 if (cur.top() == old) {
1799 // We didn't move one iota, so no need to update the screen.
1800 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1801 //cur.noScreenUpdate();
1807 case LFUN_MOUSE_RELEASE:
1808 switch (cmd.button()) {
1809 case mouse_button::button1:
1810 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1811 // If there is a new selection, update persistent selection;
1812 // otherwise, single click does not clear persistent selection
1814 if (cur.selection()) {
1815 // Finish selection. If double click,
1816 // cur is moved to the end of word by
1817 // selectWord but bvcur is current
1819 cur.bv().cursor().setSelection();
1820 // We might have removed an empty but drawn selection
1821 // (probably a margin)
1822 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1824 cur.noScreenUpdate();
1825 // FIXME: We could try to handle drag and drop of selection here.
1828 case mouse_button::button2:
1829 // Middle mouse pasting is handled at mouse press time,
1830 // see LFUN_MOUSE_PRESS.
1831 cur.noScreenUpdate();
1834 case mouse_button::button3:
1835 // Cursor was set at LFUN_MOUSE_PRESS time.
1836 // FIXME: If there is a selection we could try to handle a special
1837 // drag & drop context menu.
1838 cur.noScreenUpdate();
1841 case mouse_button::none:
1842 case mouse_button::button4:
1843 case mouse_button::button5:
1845 } // switch (cmd.button())
1849 case LFUN_SELF_INSERT: {
1850 if (cmd.argument().empty())
1853 // Automatically delete the currently selected
1854 // text and replace it with what is being
1855 // typed in now. Depends on lyxrc settings
1856 // "auto_region_delete", which defaults to
1859 if (lyxrc.auto_region_delete && cur.selection())
1860 cutSelection(cur, false, false);
1862 cur.clearSelection();
1864 docstring::const_iterator cit = cmd.argument().begin();
1865 docstring::const_iterator const end = cmd.argument().end();
1866 for (; cit != end; ++cit)
1867 bv->translateAndInsert(*cit, this, cur);
1870 moveCursor(cur, false);
1871 cur.markNewWordPosition();
1872 bv->bookmarkEditPosition();
1876 case LFUN_HREF_INSERT: {
1877 docstring content = cmd.argument();
1878 if (content.empty() && cur.selection())
1879 content = cur.selectionAsString(false);
1881 InsetCommandParams p(HYPERLINK_CODE);
1882 if (!content.empty()){
1883 // if it looks like a link, we'll put it as target,
1884 // otherwise as name (bug #8792).
1887 // regex_match(to_utf8(content), matches, link_re)
1888 // because smatch stores pointers to the substrings rather
1889 // than making copies of them. And those pointers become
1890 // invalid after regex_match returns, since it is then
1891 // being given a temporary object. (Thanks to Georg for
1892 // figuring that out.)
1893 regex const link_re("^([a-z]+):.*");
1895 string const c = to_utf8(lowercase(content));
1897 if (c.substr(0,7) == "mailto:") {
1898 p["target"] = content;
1899 p["type"] = from_ascii("mailto:");
1900 } else if (regex_match(c, matches, link_re)) {
1901 p["target"] = content;
1902 string protocol = matches.str(1);
1903 if (protocol == "file")
1904 p["type"] = from_ascii("file:");
1906 p["name"] = content;
1908 string const data = InsetCommand::params2string(p);
1910 // we need to have a target. if we already have one, then
1911 // that gets used at the default for the name, too, which
1912 // is probably what is wanted.
1913 if (p["target"].empty()) {
1914 bv->showDialog("href", data);
1916 FuncRequest fr(LFUN_INSET_INSERT, data);
1922 case LFUN_LABEL_INSERT: {
1923 InsetCommandParams p(LABEL_CODE);
1924 // Try to generate a valid label
1925 p["name"] = (cmd.argument().empty()) ?
1926 cur.getPossibleLabel() :
1928 string const data = InsetCommand::params2string(p);
1930 if (cmd.argument().empty()) {
1931 bv->showDialog("label", data);
1933 FuncRequest fr(LFUN_INSET_INSERT, data);
1939 case LFUN_INFO_INSERT: {
1941 if (cmd.argument().empty() && cur.selection()) {
1942 // if command argument is empty use current selection as parameter.
1943 docstring ds = cur.selectionAsString(false);
1944 cutSelection(cur, true, false);
1945 FuncRequest cmd0(cmd, ds);
1946 inset = createInset(cur.buffer(), cmd0);
1948 inset = createInset(cur.buffer(), cmd);
1953 insertInset(cur, inset);
1954 cur.forceBufferUpdate();
1958 case LFUN_CAPTION_INSERT:
1959 case LFUN_FOOTNOTE_INSERT:
1960 case LFUN_NOTE_INSERT:
1961 case LFUN_BOX_INSERT:
1962 case LFUN_BRANCH_INSERT:
1963 case LFUN_PHANTOM_INSERT:
1964 case LFUN_ERT_INSERT:
1965 case LFUN_LISTING_INSERT:
1966 case LFUN_MARGINALNOTE_INSERT:
1967 case LFUN_ARGUMENT_INSERT:
1968 case LFUN_INDEX_INSERT:
1969 case LFUN_PREVIEW_INSERT:
1970 case LFUN_SCRIPT_INSERT:
1971 case LFUN_IPA_INSERT:
1972 // Open the inset, and move the current selection
1974 doInsertInset(cur, this, cmd, true, true);
1976 // Some insets are numbered, others are shown in the outline pane so
1977 // let's update the labels and the toc backend.
1978 cur.forceBufferUpdate();
1981 case LFUN_FLEX_INSERT: {
1982 // Open the inset, and move the current selection
1984 bool const sel = cur.selection();
1985 doInsertInset(cur, this, cmd, true, true);
1986 // Insert auto-insert arguments
1987 bool autoargs = false;
1988 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1989 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1990 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1991 for (; lait != laend; ++lait) {
1992 Layout::latexarg arg = (*lait).second;
1993 if (arg.autoinsert) {
1994 // The cursor might have been invalidated by the replaceSelection.
1995 cur.buffer()->changed(true);
1996 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
2003 cur.leaveInset(cur.inset());
2006 // Some insets are numbered, others are shown in the outline pane so
2007 // let's update the labels and the toc backend.
2008 cur.forceBufferUpdate();
2012 case LFUN_TABULAR_INSERT:
2013 // if there were no arguments, just open the dialog
2014 if (doInsertInset(cur, this, cmd, false, true))
2017 bv->showDialog("tabularcreate");
2021 case LFUN_FLOAT_INSERT:
2022 case LFUN_FLOAT_WIDE_INSERT:
2023 case LFUN_WRAP_INSERT: {
2024 // will some content be moved into the inset?
2025 bool const content = cur.selection();
2026 // does the content consist of multiple paragraphs?
2027 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2029 doInsertInset(cur, this, cmd, true, true);
2032 // If some single-par content is moved into the inset,
2033 // doInsertInset puts the cursor outside the inset.
2034 // To insert the caption we put it back into the inset.
2035 // FIXME cleanup doInsertInset to avoid such dances!
2036 if (content && singlepar)
2039 ParagraphList & pars = cur.text()->paragraphs();
2041 DocumentClass const & tclass = bv->buffer().params().documentClass();
2043 // add a separate paragraph for the caption inset
2044 pars.push_back(Paragraph());
2045 pars.back().setInsetOwner(&cur.text()->inset());
2046 pars.back().setPlainOrDefaultLayout(tclass);
2047 int cap_pit = pars.size() - 1;
2049 // if an empty inset was created, we create an additional empty
2050 // paragraph at the bottom so that the user can choose where to put
2051 // the graphics (or table).
2053 pars.push_back(Paragraph());
2054 pars.back().setInsetOwner(&cur.text()->inset());
2055 pars.back().setPlainOrDefaultLayout(tclass);
2058 // reposition the cursor to the caption
2059 cur.pit() = cap_pit;
2061 // FIXME: This Text/Cursor dispatch handling is a mess!
2062 // We cannot use Cursor::dispatch here it needs access to up to
2064 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2065 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2066 cur.forceBufferUpdate();
2067 cur.screenUpdateFlags(Update::Force);
2068 // FIXME: When leaving the Float (or Wrap) inset we should
2069 // delete any empty paragraph left above or below the
2074 case LFUN_NOMENCL_INSERT: {
2075 InsetCommandParams p(NOMENCL_CODE);
2076 if (cmd.argument().empty())
2077 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
2079 p["symbol"] = cmd.argument();
2080 string const data = InsetCommand::params2string(p);
2081 bv->showDialog("nomenclature", data);
2085 case LFUN_INDEX_PRINT: {
2086 InsetCommandParams p(INDEX_PRINT_CODE);
2087 if (cmd.argument().empty())
2088 p["type"] = from_ascii("idx");
2090 p["type"] = cmd.argument();
2091 string const data = InsetCommand::params2string(p);
2092 FuncRequest fr(LFUN_INSET_INSERT, data);
2097 case LFUN_NOMENCL_PRINT:
2098 case LFUN_NEWPAGE_INSERT:
2100 doInsertInset(cur, this, cmd, false, false);
2104 case LFUN_SEPARATOR_INSERT: {
2105 doInsertInset(cur, this, cmd, false, false);
2107 // remove a following space
2108 Paragraph & par = cur.paragraph();
2109 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2110 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2114 case LFUN_DEPTH_DECREMENT:
2115 changeDepth(cur, DEC_DEPTH);
2118 case LFUN_DEPTH_INCREMENT:
2119 changeDepth(cur, INC_DEPTH);
2122 case LFUN_REGEXP_MODE:
2123 regexpDispatch(cur, cmd);
2126 case LFUN_MATH_MODE: {
2127 if (cmd.argument() == "on" || cmd.argument() == "") {
2128 // don't pass "on" as argument
2129 // (it would appear literally in the first cell)
2130 docstring sel = cur.selectionAsString(false);
2131 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2132 // create a macro template if we see "\\newcommand" somewhere, and
2133 // an ordinary formula otherwise
2135 && (sel.find(from_ascii("\\newcommand")) != string::npos
2136 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2137 || sel.find(from_ascii("\\def")) != string::npos)
2138 && macro->fromString(sel)) {
2140 replaceSelection(cur);
2143 // no meaningful macro template was found
2145 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2148 // The argument is meaningful
2149 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2150 // has a different meaning in math mode
2151 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2155 case LFUN_MATH_MACRO:
2156 if (cmd.argument().empty())
2157 cur.errorMessage(from_utf8(N_("Missing argument")));
2160 string s = to_utf8(cmd.argument());
2161 string const s1 = token(s, ' ', 1);
2162 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2163 string const s2 = token(s, ' ', 2);
2164 MacroType type = MacroTypeNewcommand;
2166 type = MacroTypeDef;
2167 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2168 from_utf8(token(s, ' ', 0)), nargs, false, type);
2169 inset->setBuffer(bv->buffer());
2170 insertInset(cur, inset);
2172 // enter macro inset and select the name
2174 cur.top().pos() = cur.top().lastpos();
2176 cur.selection(true);
2177 cur.top().pos() = 0;
2181 case LFUN_MATH_DISPLAY:
2182 case LFUN_MATH_SUBSCRIPT:
2183 case LFUN_MATH_SUPERSCRIPT:
2184 case LFUN_MATH_INSERT:
2185 case LFUN_MATH_AMS_MATRIX:
2186 case LFUN_MATH_MATRIX:
2187 case LFUN_MATH_DELIM:
2188 case LFUN_MATH_BIGDELIM:
2189 mathDispatch(cur, cmd);
2192 case LFUN_FONT_EMPH: {
2193 Font font(ignore_font, ignore_language);
2194 font.fontInfo().setEmph(FONT_TOGGLE);
2195 toggleAndShow(cur, this, font);
2199 case LFUN_FONT_ITAL: {
2200 Font font(ignore_font, ignore_language);
2201 font.fontInfo().setShape(ITALIC_SHAPE);
2202 toggleAndShow(cur, this, font);
2206 case LFUN_FONT_BOLD:
2207 case LFUN_FONT_BOLDSYMBOL: {
2208 Font font(ignore_font, ignore_language);
2209 font.fontInfo().setSeries(BOLD_SERIES);
2210 toggleAndShow(cur, this, font);
2214 case LFUN_FONT_NOUN: {
2215 Font font(ignore_font, ignore_language);
2216 font.fontInfo().setNoun(FONT_TOGGLE);
2217 toggleAndShow(cur, this, font);
2221 case LFUN_FONT_TYPEWRITER: {
2222 Font font(ignore_font, ignore_language);
2223 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2224 toggleAndShow(cur, this, font);
2228 case LFUN_FONT_SANS: {
2229 Font font(ignore_font, ignore_language);
2230 font.fontInfo().setFamily(SANS_FAMILY);
2231 toggleAndShow(cur, this, font);
2235 case LFUN_FONT_ROMAN: {
2236 Font font(ignore_font, ignore_language);
2237 font.fontInfo().setFamily(ROMAN_FAMILY);
2238 toggleAndShow(cur, this, font);
2242 case LFUN_FONT_DEFAULT: {
2243 Font font(inherit_font, ignore_language);
2244 toggleAndShow(cur, this, font);
2248 case LFUN_FONT_STRIKEOUT: {
2249 Font font(ignore_font, ignore_language);
2250 font.fontInfo().setStrikeout(FONT_TOGGLE);
2251 toggleAndShow(cur, this, font);
2255 case LFUN_FONT_CROSSOUT: {
2256 Font font(ignore_font, ignore_language);
2257 font.fontInfo().setXout(FONT_TOGGLE);
2258 toggleAndShow(cur, this, font);
2262 case LFUN_FONT_UNDERUNDERLINE: {
2263 Font font(ignore_font, ignore_language);
2264 font.fontInfo().setUuline(FONT_TOGGLE);
2265 toggleAndShow(cur, this, font);
2269 case LFUN_FONT_UNDERWAVE: {
2270 Font font(ignore_font, ignore_language);
2271 font.fontInfo().setUwave(FONT_TOGGLE);
2272 toggleAndShow(cur, this, font);
2276 case LFUN_FONT_UNDERLINE: {
2277 Font font(ignore_font, ignore_language);
2278 font.fontInfo().setUnderbar(FONT_TOGGLE);
2279 toggleAndShow(cur, this, font);
2283 case LFUN_FONT_SIZE: {
2284 Font font(ignore_font, ignore_language);
2285 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2286 toggleAndShow(cur, this, font);
2290 case LFUN_LANGUAGE: {
2291 string const lang_arg = cmd.getArg(0);
2292 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2293 Language const * lang =
2294 reset ? reset_language
2295 : languages.getLanguage(lang_arg);
2296 // we allow reset_language, which is 0, but only if it
2297 // was requested via empty or "reset" arg.
2298 if (!lang && !reset)
2300 bool const toggle = (cmd.getArg(1) != "set");
2301 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2302 Font font(ignore_font, lang);
2303 toggleAndShow(cur, this, font, toggle);
2307 case LFUN_TEXTSTYLE_APPLY:
2308 toggleAndShow(cur, this, freefont, toggleall);
2309 cur.message(_("Character set"));
2312 // Set the freefont using the contents of \param data dispatched from
2313 // the frontends and apply it at the current cursor location.
2314 case LFUN_TEXTSTYLE_UPDATE: {
2317 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2320 toggleAndShow(cur, this, freefont, toggleall);
2321 cur.message(_("Character set"));
2323 lyxerr << "Argument not ok";
2328 case LFUN_FINISHED_LEFT:
2329 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2330 // We're leaving an inset, going left. If the inset is LTR, we're
2331 // leaving from the front, so we should not move (remain at --- but
2332 // not in --- the inset). If the inset is RTL, move left, without
2333 // entering the inset itself; i.e., move to after the inset.
2334 if (cur.paragraph().getFontSettings(
2335 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2336 cursorVisLeft(cur, true);
2339 case LFUN_FINISHED_RIGHT:
2340 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2341 // We're leaving an inset, going right. If the inset is RTL, we're
2342 // leaving from the front, so we should not move (remain at --- but
2343 // not in --- the inset). If the inset is LTR, move right, without
2344 // entering the inset itself; i.e., move to after the inset.
2345 if (!cur.paragraph().getFontSettings(
2346 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2347 cursorVisRight(cur, true);
2350 case LFUN_FINISHED_BACKWARD:
2351 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2352 cur.setCurrentFont();
2355 case LFUN_FINISHED_FORWARD:
2356 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2358 cur.setCurrentFont();
2361 case LFUN_LAYOUT_PARAGRAPH: {
2363 params2string(cur.paragraph(), data);
2364 data = "show\n" + data;
2365 bv->showDialog("paragraph", data);
2369 case LFUN_PARAGRAPH_UPDATE: {
2371 params2string(cur.paragraph(), data);
2373 // Will the paragraph accept changes from the dialog?
2375 cur.inset().allowParagraphCustomization(cur.idx());
2377 data = "update " + convert<string>(accept) + '\n' + data;
2378 bv->updateDialog("paragraph", data);
2382 case LFUN_ACCENT_UMLAUT:
2383 case LFUN_ACCENT_CIRCUMFLEX:
2384 case LFUN_ACCENT_GRAVE:
2385 case LFUN_ACCENT_ACUTE:
2386 case LFUN_ACCENT_TILDE:
2387 case LFUN_ACCENT_PERISPOMENI:
2388 case LFUN_ACCENT_CEDILLA:
2389 case LFUN_ACCENT_MACRON:
2390 case LFUN_ACCENT_DOT:
2391 case LFUN_ACCENT_UNDERDOT:
2392 case LFUN_ACCENT_UNDERBAR:
2393 case LFUN_ACCENT_CARON:
2394 case LFUN_ACCENT_BREVE:
2395 case LFUN_ACCENT_TIE:
2396 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2397 case LFUN_ACCENT_CIRCLE:
2398 case LFUN_ACCENT_OGONEK:
2399 theApp()->handleKeyFunc(cmd.action());
2400 if (!cmd.argument().empty())
2401 // FIXME: Are all these characters encoded in one byte in utf8?
2402 bv->translateAndInsert(cmd.argument()[0], this, cur);
2403 cur.screenUpdateFlags(Update::FitCursor);
2406 case LFUN_FLOAT_LIST_INSERT: {
2407 DocumentClass const & tclass = bv->buffer().params().documentClass();
2408 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2410 if (cur.selection())
2411 cutSelection(cur, true, false);
2412 breakParagraph(cur);
2414 if (cur.lastpos() != 0) {
2415 cursorBackward(cur);
2416 breakParagraph(cur);
2419 docstring const laystr = cur.inset().usePlainLayout() ?
2420 tclass.plainLayoutName() :
2421 tclass.defaultLayoutName();
2422 setLayout(cur, laystr);
2423 ParagraphParameters p;
2424 // FIXME If this call were replaced with one to clearParagraphParams(),
2425 // then we could get rid of this method altogether.
2426 setParagraphs(cur, p);
2427 // FIXME This should be simplified when InsetFloatList takes a
2428 // Buffer in its constructor.
2429 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2430 ifl->setBuffer(bv->buffer());
2431 insertInset(cur, ifl);
2434 lyxerr << "Non-existent float type: "
2435 << to_utf8(cmd.argument()) << endl;
2440 case LFUN_CHANGE_ACCEPT: {
2441 acceptOrRejectChanges(cur, ACCEPT);
2445 case LFUN_CHANGE_REJECT: {
2446 acceptOrRejectChanges(cur, REJECT);
2450 case LFUN_THESAURUS_ENTRY: {
2451 Language const * language = cur.getFont().language();
2452 docstring arg = cmd.argument();
2454 arg = cur.selectionAsString(false);
2456 if (arg.size() > 100 || arg.empty()) {
2457 // Get word or selection
2458 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2459 arg = cur.selectionAsString(false);
2460 arg += " lang=" + from_ascii(language->lang());
2463 string lang = cmd.getArg(1);
2464 // This duplicates the code in GuiThesaurus::initialiseParams
2465 if (prefixIs(lang, "lang=")) {
2466 language = languages.getLanguage(lang.substr(5));
2468 language = cur.getFont().language();
2471 string lang = language->code();
2472 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2473 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2474 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2475 _("The path to the thesaurus directory has not been specified.\n"
2476 "The thesaurus is not functional.\n"
2477 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2480 bv->showDialog("thesaurus", to_utf8(arg));
2484 case LFUN_SPELLING_ADD: {
2485 Language const * language = getLanguage(cur, cmd.getArg(1));
2486 docstring word = from_utf8(cmd.getArg(0));
2488 word = cur.selectionAsString(false);
2490 if (word.size() > 100 || word.empty()) {
2491 // Get word or selection
2492 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2493 word = cur.selectionAsString(false);
2496 WordLangTuple wl(word, language);
2497 theSpellChecker()->insert(wl);
2501 case LFUN_SPELLING_IGNORE: {
2502 Language const * language = getLanguage(cur, cmd.getArg(1));
2503 docstring word = from_utf8(cmd.getArg(0));
2505 word = cur.selectionAsString(false);
2507 if (word.size() > 100 || word.empty()) {
2508 // Get word or selection
2509 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2510 word = cur.selectionAsString(false);
2513 WordLangTuple wl(word, language);
2514 theSpellChecker()->accept(wl);
2518 case LFUN_SPELLING_REMOVE: {
2519 Language const * language = getLanguage(cur, cmd.getArg(1));
2520 docstring word = from_utf8(cmd.getArg(0));
2522 word = cur.selectionAsString(false);
2524 if (word.size() > 100 || word.empty()) {
2525 // Get word or selection
2526 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2527 word = cur.selectionAsString(false);
2530 WordLangTuple wl(word, language);
2531 theSpellChecker()->remove(wl);
2535 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2536 // Given data, an encoding of the ParagraphParameters
2537 // generated in the Paragraph dialog, this function sets
2538 // the current paragraph, or currently selected paragraphs,
2540 // NOTE: This function overrides all existing settings.
2541 setParagraphs(cur, cmd.argument());
2542 cur.message(_("Paragraph layout set"));
2546 case LFUN_PARAGRAPH_PARAMS: {
2547 // Given data, an encoding of the ParagraphParameters as we'd
2548 // find them in a LyX file, this function modifies the current paragraph,
2549 // or currently selected paragraphs.
2550 // NOTE: This function only modifies, and does not override, existing
2552 setParagraphs(cur, cmd.argument(), true);
2553 cur.message(_("Paragraph layout set"));
2558 if (cur.selection()) {
2559 cur.selection(false);
2562 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2563 // correct, but I'm not 100% sure -- dov, 071019
2564 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2568 case LFUN_OUTLINE_UP:
2569 outline(OutlineUp, cur);
2570 setCursor(cur, cur.pit(), 0);
2571 cur.forceBufferUpdate();
2575 case LFUN_OUTLINE_DOWN: {
2576 outline(OutlineDown, cur);
2577 setCursor(cur, cur.pit(), 0);
2578 // If we move an environment, make sure it is separated
2579 // from its new neighbour above.
2580 pit_type pit = cur.pit();
2581 if (pit > 0 && pars_[pit].layout().isEnvironment()
2582 && pars_[pit - 1].layout() == pars_[pit].layout()) {
2583 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
2584 DocumentClass const & tc = bv->buffer().params().documentClass();
2585 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
2586 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
2587 setCursor(cur, pit + 1, 0);
2589 cur.forceBufferUpdate();
2594 case LFUN_OUTLINE_IN:
2595 outline(OutlineIn, cur);
2596 cur.forceBufferUpdate();
2600 case LFUN_OUTLINE_OUT:
2601 outline(OutlineOut, cur);
2602 cur.forceBufferUpdate();
2606 case LFUN_SERVER_GET_STATISTICS:
2608 DocIterator from, to;
2609 if (cur.selection()) {
2610 from = cur.selectionBegin();
2611 to = cur.selectionEnd();
2613 from = doc_iterator_begin(cur.buffer());
2614 to = doc_iterator_end(cur.buffer());
2617 cur.buffer()->updateStatistics(from, to);
2618 string const arg0 = cmd.getArg(0);
2619 if (arg0 == "words") {
2620 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2621 } else if (arg0 == "chars") {
2622 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2623 } else if (arg0 == "chars-space") {
2624 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2626 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2627 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2628 + convert<docstring>(cur.buffer()->charCount(true)));
2634 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2639 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2641 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2642 // Check for misspelled text
2643 // The redraw is useful because of the painting of
2644 // misspelled markers depends on the cursor position.
2645 // Trigger a redraw for cursor moves inside misspelled text.
2646 if (!cur.inTexted()) {
2647 // move from regular text to math
2648 needsUpdate = last_misspelled;
2649 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2650 // move inside regular text
2651 needsUpdate = last_misspelled
2652 || cur.paragraph().isMisspelled(cur.pos(), true);
2656 // FIXME: The cursor flag is reset two lines below
2657 // so we need to check here if some of the LFUN did touch that.
2658 // for now only Text::erase() and Text::backspace() do that.
2659 // The plan is to verify all the LFUNs and then to remove this
2660 // singleParUpdate boolean altogether.
2661 if (cur.result().screenUpdate() & Update::Force) {
2662 singleParUpdate = false;
2666 // FIXME: the following code should go in favor of fine grained
2667 // update flag treatment.
2668 if (singleParUpdate) {
2669 // Inserting characters does not change par height in general. So, try
2670 // to update _only_ this paragraph. BufferView will detect if a full
2671 // metrics update is needed anyway.
2672 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2676 && &oldTopSlice.inset() == &cur.inset()
2677 && oldTopSlice.idx() == cur.idx()
2678 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2679 && !cur.selection())
2680 // FIXME: it would be better if we could just do this
2682 //if (cur.result().update() != Update::FitCursor)
2683 // cur.noScreenUpdate();
2685 // But some LFUNs do not set Update::FitCursor when needed, so we
2686 // do it for all. This is not very harmfull as FitCursor will provoke
2687 // a full redraw only if needed but still, a proper review of all LFUN
2688 // should be done and this needsUpdate boolean can then be removed.
2689 cur.screenUpdateFlags(Update::FitCursor);
2691 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2695 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2696 FuncStatus & flag) const
2698 LBUFERR(this == cur.text());
2700 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2702 bool allow_in_passthru = false;
2703 InsetCode code = NO_CODE;
2705 switch (cmd.action()) {
2707 case LFUN_DEPTH_DECREMENT:
2708 enable = changeDepthAllowed(cur, DEC_DEPTH);
2711 case LFUN_DEPTH_INCREMENT:
2712 enable = changeDepthAllowed(cur, INC_DEPTH);
2716 // FIXME We really should not allow this to be put, e.g.,
2717 // in a footnote, or in ERT. But it would make sense in a
2718 // branch, so I'm not sure what to do.
2719 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2722 case LFUN_DIALOG_SHOW_NEW_INSET:
2723 if (cmd.argument() == "bibitem")
2724 code = BIBITEM_CODE;
2725 else if (cmd.argument() == "bibtex") {
2727 // not allowed in description items
2728 enable = !inDescriptionItem(cur);
2730 else if (cmd.argument() == "box")
2732 else if (cmd.argument() == "branch")
2734 else if (cmd.argument() == "citation")
2736 else if (cmd.argument() == "ert")
2738 else if (cmd.argument() == "external")
2739 code = EXTERNAL_CODE;
2740 else if (cmd.argument() == "float")
2742 else if (cmd.argument() == "graphics")
2743 code = GRAPHICS_CODE;
2744 else if (cmd.argument() == "href")
2745 code = HYPERLINK_CODE;
2746 else if (cmd.argument() == "include")
2747 code = INCLUDE_CODE;
2748 else if (cmd.argument() == "index")
2750 else if (cmd.argument() == "index_print")
2751 code = INDEX_PRINT_CODE;
2752 else if (cmd.argument() == "listings")
2753 code = LISTINGS_CODE;
2754 else if (cmd.argument() == "mathspace")
2755 code = MATH_HULL_CODE;
2756 else if (cmd.argument() == "nomenclature")
2757 code = NOMENCL_CODE;
2758 else if (cmd.argument() == "nomencl_print")
2759 code = NOMENCL_PRINT_CODE;
2760 else if (cmd.argument() == "label")
2762 else if (cmd.argument() == "line")
2764 else if (cmd.argument() == "note")
2766 else if (cmd.argument() == "phantom")
2767 code = PHANTOM_CODE;
2768 else if (cmd.argument() == "ref")
2770 else if (cmd.argument() == "space")
2772 else if (cmd.argument() == "toc")
2774 else if (cmd.argument() == "vspace")
2776 else if (cmd.argument() == "wrap")
2780 case LFUN_ERT_INSERT:
2783 case LFUN_LISTING_INSERT:
2784 code = LISTINGS_CODE;
2785 // not allowed in description items
2786 enable = !inDescriptionItem(cur);
2788 case LFUN_FOOTNOTE_INSERT:
2791 case LFUN_TABULAR_INSERT:
2792 code = TABULAR_CODE;
2794 case LFUN_MARGINALNOTE_INSERT:
2797 case LFUN_FLOAT_INSERT:
2798 case LFUN_FLOAT_WIDE_INSERT:
2799 // FIXME: If there is a selection, we should check whether there
2800 // are floats in the selection, but this has performance issues, see
2801 // LFUN_CHANGE_ACCEPT/REJECT.
2803 if (inDescriptionItem(cur))
2804 // not allowed in description items
2807 InsetCode const inset_code = cur.inset().lyxCode();
2809 // algorithm floats cannot be put in another float
2810 if (to_utf8(cmd.argument()) == "algorithm") {
2811 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2815 // for figures and tables: only allow in another
2816 // float or wrap if it is of the same type and
2817 // not a subfloat already
2818 if(cur.inset().lyxCode() == code) {
2819 InsetFloat const & ins =
2820 static_cast<InsetFloat const &>(cur.inset());
2821 enable = ins.params().type == to_utf8(cmd.argument())
2822 && !ins.params().subfloat;
2823 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2824 InsetWrap const & ins =
2825 static_cast<InsetWrap const &>(cur.inset());
2826 enable = ins.params().type == to_utf8(cmd.argument());
2830 case LFUN_WRAP_INSERT:
2832 // not allowed in description items
2833 enable = !inDescriptionItem(cur);
2835 case LFUN_FLOAT_LIST_INSERT: {
2836 code = FLOAT_LIST_CODE;
2837 // not allowed in description items
2838 enable = !inDescriptionItem(cur);
2840 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2841 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2842 // make sure we know about such floats
2843 if (cit == floats.end() ||
2844 // and that we know how to generate a list of them
2845 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2846 flag.setUnknown(true);
2847 // probably not necessary, but...
2853 case LFUN_CAPTION_INSERT: {
2854 code = CAPTION_CODE;
2855 string arg = cmd.getArg(0);
2856 bool varia = arg != "Unnumbered"
2857 && cur.inset().allowsCaptionVariation(arg);
2858 // not allowed in description items,
2859 // and in specific insets
2860 enable = !inDescriptionItem(cur)
2861 && (varia || arg.empty() || arg == "Standard");
2864 case LFUN_NOTE_INSERT:
2866 // in commands (sections etc.) and description items,
2867 // only Notes are allowed
2868 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2869 (!cur.paragraph().layout().isCommand()
2870 && !inDescriptionItem(cur)));
2872 case LFUN_FLEX_INSERT: {
2874 string s = cmd.getArg(0);
2876 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2877 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2878 il.lyxtype() != InsetLayout::CUSTOM &&
2879 il.lyxtype() != InsetLayout::ELEMENT &&
2880 il.lyxtype ()!= InsetLayout::STANDARD)
2884 case LFUN_BOX_INSERT:
2887 case LFUN_BRANCH_INSERT:
2889 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2890 && cur.buffer()->params().branchlist().empty())
2893 case LFUN_IPA_INSERT:
2896 case LFUN_PHANTOM_INSERT:
2897 code = PHANTOM_CODE;
2899 case LFUN_LABEL_INSERT:
2902 case LFUN_INFO_INSERT:
2905 case LFUN_ARGUMENT_INSERT: {
2907 allow_in_passthru = true;
2908 string const arg = cmd.getArg(0);
2913 Layout const & lay = cur.paragraph().layout();
2914 Layout::LaTeXArgMap args = lay.args();
2915 Layout::LaTeXArgMap::const_iterator const lait =
2917 if (lait != args.end()) {
2919 pit_type pit = cur.pit();
2920 pit_type lastpit = cur.pit();
2921 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2922 // In a sequence of "merged" environment layouts, we only allow
2923 // non-item arguments once.
2924 lastpit = cur.lastpit();
2925 // get the first paragraph in sequence with this layout
2926 depth_type const current_depth = cur.paragraph().params().depth();
2930 Paragraph cpar = pars_[pit - 1];
2931 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2937 for (; pit <= lastpit; ++pit) {
2938 if (pars_[pit].layout() != lay)
2940 for (auto const & table : pars_[pit].insetList())
2941 if (InsetArgument const * ins = table.inset->asInsetArgument())
2942 if (ins->name() == arg) {
2943 // we have this already
2952 case LFUN_INDEX_INSERT:
2955 case LFUN_INDEX_PRINT:
2956 code = INDEX_PRINT_CODE;
2957 // not allowed in description items
2958 enable = !inDescriptionItem(cur);
2960 case LFUN_NOMENCL_INSERT:
2961 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2965 code = NOMENCL_CODE;
2967 case LFUN_NOMENCL_PRINT:
2968 code = NOMENCL_PRINT_CODE;
2969 // not allowed in description items
2970 enable = !inDescriptionItem(cur);
2972 case LFUN_HREF_INSERT:
2973 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2977 code = HYPERLINK_CODE;
2979 case LFUN_IPAMACRO_INSERT: {
2980 string const arg = cmd.getArg(0);
2982 code = IPADECO_CODE;
2984 code = IPACHAR_CODE;
2987 case LFUN_QUOTE_INSERT:
2988 // always allow this, since we will inset a raw quote
2989 // if an inset is not allowed.
2990 allow_in_passthru = true;
2992 case LFUN_SPECIALCHAR_INSERT:
2993 code = SPECIALCHAR_CODE;
2995 case LFUN_SPACE_INSERT:
2996 // slight hack: we know this is allowed in math mode
3000 case LFUN_PREVIEW_INSERT:
3001 code = PREVIEW_CODE;
3003 case LFUN_SCRIPT_INSERT:
3007 case LFUN_MATH_INSERT:
3008 case LFUN_MATH_AMS_MATRIX:
3009 case LFUN_MATH_MATRIX:
3010 case LFUN_MATH_DELIM:
3011 case LFUN_MATH_BIGDELIM:
3012 case LFUN_MATH_DISPLAY:
3013 case LFUN_MATH_MODE:
3014 case LFUN_MATH_MACRO:
3015 case LFUN_MATH_SUBSCRIPT:
3016 case LFUN_MATH_SUPERSCRIPT:
3017 code = MATH_HULL_CODE;
3020 case LFUN_REGEXP_MODE:
3021 code = MATH_HULL_CODE;
3022 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3025 case LFUN_INSET_MODIFY:
3026 // We need to disable this, because we may get called for a
3028 // InsetTabular::getStatus() -> InsetText::getStatus()
3029 // and we don't handle LFUN_INSET_MODIFY.
3033 case LFUN_FONT_EMPH:
3034 flag.setOnOff(fontinfo.emph() == FONT_ON);
3035 enable = !cur.paragraph().isPassThru();
3038 case LFUN_FONT_ITAL:
3039 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3040 enable = !cur.paragraph().isPassThru();
3043 case LFUN_FONT_NOUN:
3044 flag.setOnOff(fontinfo.noun() == FONT_ON);
3045 enable = !cur.paragraph().isPassThru();
3048 case LFUN_FONT_BOLD:
3049 case LFUN_FONT_BOLDSYMBOL:
3050 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3051 enable = !cur.paragraph().isPassThru();
3054 case LFUN_FONT_SANS:
3055 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3056 enable = !cur.paragraph().isPassThru();
3059 case LFUN_FONT_ROMAN:
3060 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3061 enable = !cur.paragraph().isPassThru();
3064 case LFUN_FONT_TYPEWRITER:
3065 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3066 enable = !cur.paragraph().isPassThru();
3071 enable = cur.selection();
3075 if (cmd.argument().empty()) {
3076 if (theClipboard().isInternal())
3077 enable = cap::numberOfSelections() > 0;
3079 enable = !theClipboard().empty();
3083 // we have an argument
3084 string const arg = to_utf8(cmd.argument());
3085 if (isStrUnsignedInt(arg)) {
3086 // it's a number and therefore means the internal stack
3087 unsigned int n = convert<unsigned int>(arg);
3088 enable = cap::numberOfSelections() > n;
3092 // explicit text type?
3093 if (arg == "html") {
3094 // Do not enable for PlainTextType, since some tidying in the
3095 // frontend is needed for HTML, which is too unsafe for plain text.
3096 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3098 } else if (arg == "latex") {
3099 // LaTeX is usually not available on the clipboard with
3100 // the correct MIME type, but in plain text.
3101 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3102 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3106 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3108 type = Clipboard::PdfGraphicsType;
3109 else if (arg == "png")
3110 type = Clipboard::PngGraphicsType;
3111 else if (arg == "jpeg")
3112 type = Clipboard::JpegGraphicsType;
3113 else if (arg == "linkback")
3114 type = Clipboard::LinkBackGraphicsType;
3115 else if (arg == "emf")
3116 type = Clipboard::EmfGraphicsType;
3117 else if (arg == "wmf")
3118 type = Clipboard::WmfGraphicsType;
3121 LYXERR0("Unrecognized graphics type: " << arg);
3122 // we don't want to assert if the user just mistyped the LFUN
3123 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3127 enable = theClipboard().hasGraphicsContents(type);
3131 case LFUN_CLIPBOARD_PASTE:
3132 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3133 enable = !theClipboard().empty();
3136 case LFUN_PRIMARY_SELECTION_PASTE:
3137 enable = cur.selection() || !theSelection().empty();
3140 case LFUN_SELECTION_PASTE:
3141 enable = cap::selection();
3144 case LFUN_PARAGRAPH_MOVE_UP:
3145 enable = cur.pit() > 0 && !cur.selection();
3148 case LFUN_PARAGRAPH_MOVE_DOWN:
3149 enable = cur.pit() < cur.lastpit() && !cur.selection();
3152 case LFUN_CHANGE_ACCEPT:
3153 case LFUN_CHANGE_REJECT:
3154 // In principle, these LFUNs should only be enabled if there
3155 // is a change at the current position/in the current selection.
3156 // However, without proper optimizations, this will inevitably
3157 // result in unacceptable performance - just imagine a user who
3158 // wants to select the complete content of a long document.
3159 if (!cur.selection())
3160 enable = cur.paragraph().isChanged(cur.pos());
3162 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3167 case LFUN_OUTLINE_UP:
3168 case LFUN_OUTLINE_DOWN:
3169 case LFUN_OUTLINE_IN:
3170 case LFUN_OUTLINE_OUT:
3171 // FIXME: LyX is not ready for outlining within inset.
3172 enable = isMainText()
3173 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3176 case LFUN_NEWLINE_INSERT:
3177 // LaTeX restrictions (labels or empty par)
3178 enable = !cur.paragraph().isPassThru()
3179 && cur.pos() > cur.paragraph().beginOfBody();
3182 case LFUN_SEPARATOR_INSERT:
3183 // Always enabled for now
3187 case LFUN_TAB_INSERT:
3188 case LFUN_TAB_DELETE:
3189 enable = cur.paragraph().isPassThru();
3192 case LFUN_SET_GRAPHICS_GROUP: {
3193 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3197 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3201 case LFUN_NEWPAGE_INSERT:
3202 // not allowed in description items
3203 code = NEWPAGE_CODE;
3204 enable = !inDescriptionItem(cur);
3207 case LFUN_DATE_INSERT: {
3208 string const format = cmd.argument().empty()
3209 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3210 enable = support::os::is_valid_strftime(format);
3215 enable = !cur.paragraph().isPassThru();
3216 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3219 case LFUN_PARAGRAPH_BREAK:
3220 enable = inset().allowMultiPar();
3223 case LFUN_SPELLING_ADD:
3224 case LFUN_SPELLING_IGNORE:
3225 case LFUN_SPELLING_REMOVE:
3226 enable = theSpellChecker() != NULL;
3227 if (enable && !cmd.getArg(1).empty()) {
3228 // validate explicitly given language
3229 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3230 enable &= lang != NULL;
3235 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3236 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3237 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3239 layout = tclass.defaultLayoutName();
3240 enable = !owner_->forcePlainLayout() && tclass.hasLayout(layout);
3242 flag.setOnOff(layout == cur.paragraph().layout().name());
3246 case LFUN_ENVIRONMENT_SPLIT: {
3247 if (cmd.argument() == "outer") {
3248 // check if we have an environment in our nesting hierarchy
3250 depth_type const current_depth = cur.paragraph().params().depth();
3251 pit_type pit = cur.pit();
3252 Paragraph cpar = pars_[pit];
3254 if (pit == 0 || cpar.params().depth() == 0)
3258 if (cpar.params().depth() < current_depth)
3259 res = cpar.layout().isEnvironment();
3264 else if (cmd.argument() == "previous") {
3265 // look if we have an environment in the previous par
3266 pit_type pit = cur.pit();
3267 Paragraph cpar = pars_[pit];
3271 enable = cpar.layout().isEnvironment();
3277 else if (cur.paragraph().layout().isEnvironment()) {
3278 enable = cmd.argument() == "before"
3279 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3286 case LFUN_LAYOUT_PARAGRAPH:
3287 case LFUN_PARAGRAPH_PARAMS:
3288 case LFUN_PARAGRAPH_PARAMS_APPLY:
3289 case LFUN_PARAGRAPH_UPDATE:
3290 enable = owner_->allowParagraphCustomization();
3293 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3294 // Because they insert COMBINING DIACRITICAL Unicode characters,
3295 // that cannot be handled by LaTeX but must be converted according
3296 // to the definition in lib/unicodesymbols?
3297 case LFUN_ACCENT_ACUTE:
3298 case LFUN_ACCENT_BREVE:
3299 case LFUN_ACCENT_CARON:
3300 case LFUN_ACCENT_CEDILLA:
3301 case LFUN_ACCENT_CIRCLE:
3302 case LFUN_ACCENT_CIRCUMFLEX:
3303 case LFUN_ACCENT_DOT:
3304 case LFUN_ACCENT_GRAVE:
3305 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3306 case LFUN_ACCENT_MACRON:
3307 case LFUN_ACCENT_OGONEK:
3308 case LFUN_ACCENT_TIE:
3309 case LFUN_ACCENT_TILDE:
3310 case LFUN_ACCENT_PERISPOMENI:
3311 case LFUN_ACCENT_UMLAUT:
3312 case LFUN_ACCENT_UNDERBAR:
3313 case LFUN_ACCENT_UNDERDOT:
3314 case LFUN_FONT_DEFAULT:
3315 case LFUN_FONT_FRAK:
3316 case LFUN_FONT_SIZE:
3317 case LFUN_FONT_STATE:
3318 case LFUN_FONT_UNDERLINE:
3319 case LFUN_FONT_STRIKEOUT:
3320 case LFUN_FONT_CROSSOUT:
3321 case LFUN_FONT_UNDERUNDERLINE:
3322 case LFUN_FONT_UNDERWAVE:
3323 case LFUN_TEXTSTYLE_APPLY:
3324 case LFUN_TEXTSTYLE_UPDATE:
3325 enable = !cur.paragraph().isPassThru();
3328 case LFUN_WORD_DELETE_FORWARD:
3329 case LFUN_WORD_DELETE_BACKWARD:
3330 case LFUN_LINE_DELETE_FORWARD:
3331 case LFUN_WORD_FORWARD:
3332 case LFUN_WORD_BACKWARD:
3333 case LFUN_WORD_RIGHT:
3334 case LFUN_WORD_LEFT:
3335 case LFUN_CHAR_FORWARD:
3336 case LFUN_CHAR_FORWARD_SELECT:
3337 case LFUN_CHAR_BACKWARD:
3338 case LFUN_CHAR_BACKWARD_SELECT:
3339 case LFUN_CHAR_LEFT:
3340 case LFUN_CHAR_LEFT_SELECT:
3341 case LFUN_CHAR_RIGHT:
3342 case LFUN_CHAR_RIGHT_SELECT:
3344 case LFUN_UP_SELECT:
3346 case LFUN_DOWN_SELECT:
3347 case LFUN_PARAGRAPH_UP_SELECT:
3348 case LFUN_PARAGRAPH_DOWN_SELECT:
3349 case LFUN_LINE_BEGIN_SELECT:
3350 case LFUN_LINE_END_SELECT:
3351 case LFUN_WORD_FORWARD_SELECT:
3352 case LFUN_WORD_BACKWARD_SELECT:
3353 case LFUN_WORD_RIGHT_SELECT:
3354 case LFUN_WORD_LEFT_SELECT:
3355 case LFUN_WORD_SELECT:
3356 case LFUN_SECTION_SELECT:
3357 case LFUN_BUFFER_BEGIN:
3358 case LFUN_BUFFER_END:
3359 case LFUN_BUFFER_BEGIN_SELECT:
3360 case LFUN_BUFFER_END_SELECT:
3361 case LFUN_INSET_BEGIN:
3362 case LFUN_INSET_END:
3363 case LFUN_INSET_BEGIN_SELECT:
3364 case LFUN_INSET_END_SELECT:
3365 case LFUN_PARAGRAPH_UP:
3366 case LFUN_PARAGRAPH_DOWN:
3367 case LFUN_LINE_BEGIN:
3369 case LFUN_CHAR_DELETE_FORWARD:
3370 case LFUN_CHAR_DELETE_BACKWARD:
3371 case LFUN_WORD_UPCASE:
3372 case LFUN_WORD_LOWCASE:
3373 case LFUN_WORD_CAPITALIZE:
3374 case LFUN_CHARS_TRANSPOSE:
3375 case LFUN_SERVER_GET_XY:
3376 case LFUN_SERVER_SET_XY:
3377 case LFUN_SERVER_GET_LAYOUT:
3378 case LFUN_SELF_INSERT:
3379 case LFUN_UNICODE_INSERT:
3380 case LFUN_THESAURUS_ENTRY:
3382 case LFUN_SERVER_GET_STATISTICS:
3383 // these are handled in our dispatch()
3387 case LFUN_INSET_INSERT: {
3388 string const type = cmd.getArg(0);
3389 if (type == "toc") {
3391 // not allowed in description items
3392 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3393 enable = !inDescriptionItem(cur);
3406 || !cur.inset().insetAllowed(code)
3407 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3410 flag.setEnabled(enable);
3415 void Text::pasteString(Cursor & cur, docstring const & clip,
3418 if (!clip.empty()) {
3421 insertStringAsParagraphs(cur, clip, cur.current_font);
3423 insertStringAsLines(cur, clip, cur.current_font);
3428 // FIXME: an item inset would make things much easier.
3429 bool Text::inDescriptionItem(Cursor & cur) const
3431 Paragraph & par = cur.paragraph();
3432 pos_type const pos = cur.pos();
3433 pos_type const body_pos = par.beginOfBody();
3435 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3436 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3437 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3440 return (pos < body_pos
3442 && (pos == 0 || par.getChar(pos - 1) != ' ')));