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::copySelectionToTemp;
93 using cap::cutSelection;
94 using cap::cutSelectionToTemp;
95 using cap::pasteFromStack;
96 using cap::pasteFromTemp;
97 using cap::pasteClipboardText;
98 using cap::pasteClipboardGraphics;
99 using cap::replaceSelection;
100 using cap::grabAndEraseSelection;
101 using cap::selClearOrDel;
102 using cap::pasteSimpleText;
103 using frontend::Clipboard;
106 static Font freefont(ignore_font, ignore_language);
107 static bool toggleall = false;
109 static void toggleAndShow(Cursor & cur, Text * text,
110 Font const & font, bool toggleall = true)
112 text->toggleFree(cur, font, toggleall);
114 if (font.language() != ignore_language ||
115 font.fontInfo().number() != FONT_IGNORE) {
116 TextMetrics const & tm = cur.bv().textMetrics(text);
117 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
118 cur.real_current_font))
119 text->setCursor(cur, cur.pit(), cur.pos(),
120 false, !cur.boundary());
125 static void moveCursor(Cursor & cur, bool selecting)
127 if (selecting || cur.mark())
132 static void finishChange(Cursor & cur, bool selecting)
135 moveCursor(cur, selecting);
139 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
142 docstring sel = cur.selectionAsString(false);
144 // It may happen that sel is empty but there is a selection
145 replaceSelection(cur);
147 // Is this a valid formula?
151 #ifdef ENABLE_ASSERTIONS
152 const int old_pos = cur.pos();
154 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
155 #ifdef ENABLE_ASSERTIONS
156 LATTEST(old_pos == cur.pos());
158 cur.nextInset()->edit(cur, true);
159 if (cmd.action() != LFUN_MATH_MODE)
160 // LFUN_MATH_MODE has a different meaning in math mode
163 InsetMathHull * formula = new InsetMathHull(cur.buffer());
164 string const selstr = to_utf8(sel);
165 istringstream is(selstr);
168 if (!formula->readQuiet(lex)) {
169 // No valid formula, let's try with delims
170 is.str("$" + selstr + "$");
172 if (!formula->readQuiet(lex)) {
173 // Still not valid, leave it as is
181 cur.nextInset()->edit(cur, true);
182 LASSERT(cur.inMathed(), return);
186 cur.pos() = cur.lastpos();
187 if (cmd.action() != LFUN_MATH_MODE)
188 // LFUN_MATH_MODE has a different meaning in math mode
190 cur.clearSelection();
191 cur.pos() = cur.lastpos();
195 cur.message(from_utf8(N_("Math editor mode")));
197 cur.message(from_utf8(N_("No valid math formula")));
201 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
203 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
204 if (cur.inRegexped()) {
205 cur.message(_("Already in regular expression mode"));
209 docstring sel = cur.selectionAsString(false);
211 // It may happen that sel is empty but there is a selection
212 replaceSelection(cur);
214 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
215 cur.nextInset()->edit(cur, true);
218 cur.message(_("Regexp editor mode"));
222 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
225 cap::replaceSelection(cur);
226 cur.insert(new InsetSpecialChar(kind));
231 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
234 cap::replaceSelection(cur);
235 cur.insert(new InsetIPAChar(kind));
240 static bool doInsertInset(Cursor & cur, Text * text,
241 FuncRequest const & cmd, bool edit, bool pastesel)
243 Buffer & buffer = cur.bv().buffer();
244 BufferParams const & bparams = buffer.params();
245 Inset * inset = createInset(&buffer, cmd);
249 if (InsetCollapsible * ci = inset->asInsetCollapsible())
250 ci->setButtonLabel();
253 if (cmd.action() == LFUN_ARGUMENT_INSERT) {
254 bool cotextinsert = false;
255 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
256 Layout const & lay = cur.paragraph().layout();
257 Layout::LaTeXArgMap args = lay.args();
258 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
259 if (lait != args.end())
260 cotextinsert = (*lait).second.insertcotext;
262 InsetLayout const & il = cur.inset().getLayout();
264 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
265 if (ilait != args.end())
266 cotextinsert = (*ilait).second.insertcotext;
268 // The argument requests to insert a copy of the co-text to the inset
271 // If we have a selection within a paragraph, use this
272 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
273 ds = cur.selectionAsString(false);
274 // else use the whole paragraph
276 ds = cur.paragraph().asString();
277 text->insertInset(cur, inset);
279 inset->edit(cur, true);
280 // Now put co-text into inset
281 Font const f(inherit_font, cur.current_font.language());
283 cur.text()->insertStringAsLines(cur, ds, f);
284 cur.leaveInset(*inset);
291 if (cur.selection()) {
292 if (cmd.action() == LFUN_INDEX_INSERT)
293 copySelectionToTemp(cur);
295 cutSelectionToTemp(cur, pastesel);
296 cur.clearSelection();
298 } else if (cmd.action() == LFUN_INDEX_INSERT) {
299 gotsel = text->selectWordWhenUnderCursor(cur, WHOLE_WORD);
300 copySelectionToTemp(cur);
301 cur.clearSelection();
303 text->insertInset(cur, inset);
306 inset->edit(cur, true);
308 if (!gotsel || !pastesel)
311 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
312 cur.buffer()->errors("Paste");
313 cur.clearSelection(); // bug 393
315 InsetText * inset_text = inset->asInsetText();
317 inset_text->fixParagraphsFont();
318 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
319 // reset first par to default
320 cur.text()->paragraphs().begin()
321 ->setPlainOrDefaultLayout(bparams.documentClass());
324 // Merge multiple paragraphs -- hack
325 while (cur.lastpit() > 0)
326 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
327 if (cmd.action() == LFUN_FLEX_INSERT)
330 cur.leaveInset(*inset);
331 if (cmd.action() == LFUN_PREVIEW_INSERT
332 || cmd.action() == LFUN_IPA_INSERT)
334 notifyCursorLeavesOrEnters(old, cur);
337 cur.leaveInset(*inset);
338 // reset surrounding par to default
339 DocumentClass const & dc = bparams.documentClass();
340 docstring const layoutname = inset->usePlainLayout()
341 ? dc.plainLayoutName()
342 : dc.defaultLayoutName();
343 text->setLayout(cur, layoutname);
349 string const freefont2string()
351 return freefont.toString(toggleall);
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(Paragraph const & par) const
568 Buffer const & buffer = owner_->buffer();
569 return par.isRTL(buffer.params());
575 Language const * getLanguage(Cursor const & cur, string const & lang) {
576 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
582 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
584 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
586 // Dispatch if the cursor is inside the text. It is not the
587 // case for context menus (bug 5797).
588 if (cur.text() != this) {
593 BufferView * bv = &cur.bv();
594 TextMetrics * tm = &bv->textMetrics(this);
595 if (!tm->contains(cur.pit())) {
596 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
597 tm = &bv->textMetrics(this);
600 // FIXME: We use the update flag to indicates wether a singlePar or a
601 // full screen update is needed. We reset it here but shall we restore it
603 cur.noScreenUpdate();
605 LBUFERR(this == cur.text());
607 // NOTE: This should NOT be a reference. See commit 94a5481a.
608 CursorSlice const oldTopSlice = cur.top();
609 bool const oldBoundary = cur.boundary();
610 bool const oldSelection = cur.selection();
611 // Signals that, even if needsUpdate == false, an update of the
612 // cursor paragraph is required
613 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
614 LyXAction::SingleParUpdate);
615 // Signals that a full-screen update is required
616 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
617 LyXAction::NoUpdate) || singleParUpdate);
618 bool const last_misspelled = lyxrc.spellcheck_continuously
619 && cur.paragraph().isMisspelled(cur.pos(), true);
621 FuncCode const act = cmd.action();
624 case LFUN_PARAGRAPH_MOVE_DOWN: {
625 pit_type const pit = cur.pit();
626 cur.recordUndo(pit, pit + 1);
628 pars_.swap(pit, pit + 1);
630 cur.forceBufferUpdate();
635 case LFUN_PARAGRAPH_MOVE_UP: {
636 pit_type const pit = cur.pit();
637 cur.recordUndo(pit - 1, pit);
639 pars_.swap(pit, pit - 1);
642 cur.forceBufferUpdate();
646 case LFUN_APPENDIX: {
647 Paragraph & par = cur.paragraph();
648 bool start = !par.params().startOfAppendix();
650 // FIXME: The code below only makes sense at top level.
651 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
652 // ensure that we have only one start_of_appendix in this document
653 // FIXME: this don't work for multipart document!
654 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
655 if (pars_[tmp].params().startOfAppendix()) {
656 cur.recordUndo(tmp, tmp);
657 pars_[tmp].params().startOfAppendix(false);
663 par.params().startOfAppendix(start);
665 // we can set the refreshing parameters now
666 cur.forceBufferUpdate();
670 case LFUN_WORD_DELETE_FORWARD:
672 cutSelection(cur, false);
674 deleteWordForward(cur, cmd.getArg(0) == "force");
675 finishChange(cur, false);
678 case LFUN_WORD_DELETE_BACKWARD:
680 cutSelection(cur, false);
682 deleteWordBackward(cur, cmd.getArg(0) == "force");
683 finishChange(cur, false);
686 case LFUN_LINE_DELETE_FORWARD:
688 cutSelection(cur, false);
690 tm->deleteLineForward(cur);
691 finishChange(cur, false);
694 case LFUN_BUFFER_BEGIN:
695 case LFUN_BUFFER_BEGIN_SELECT:
696 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
697 if (cur.depth() == 1)
698 needsUpdate |= cursorTop(cur);
701 cur.screenUpdateFlags(Update::FitCursor);
704 case LFUN_BUFFER_END:
705 case LFUN_BUFFER_END_SELECT:
706 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
707 if (cur.depth() == 1)
708 needsUpdate |= cursorBottom(cur);
711 cur.screenUpdateFlags(Update::FitCursor);
714 case LFUN_INSET_BEGIN:
715 case LFUN_INSET_BEGIN_SELECT:
716 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
717 if (cur.depth() == 1 || !cur.top().at_begin())
718 needsUpdate |= cursorTop(cur);
721 cur.screenUpdateFlags(Update::FitCursor);
725 case LFUN_INSET_END_SELECT:
726 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
727 if (cur.depth() == 1 || !cur.top().at_end())
728 needsUpdate |= cursorBottom(cur);
731 cur.screenUpdateFlags(Update::FitCursor);
734 case LFUN_CHAR_FORWARD:
735 case LFUN_CHAR_FORWARD_SELECT: {
736 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
737 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
738 bool const cur_moved = cursorForward(cur);
739 needsUpdate |= cur_moved;
741 if (!cur_moved && oldTopSlice == cur.top()
742 && cur.boundary() == oldBoundary) {
744 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
746 // we will probably be moving out the inset, so we should execute
747 // the depm-mechanism, but only when the cursor has a place to
748 // go outside this inset, i.e. in a slice above.
749 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
750 && cur.pit() == cur.lastpit()) {
751 // The cursor hasn't changed yet. To give the
752 // DEPM the possibility of doing something we must
753 // provide it with two different cursors.
755 dummy.pos() = dummy.pit() = 0;
756 if (cur.bv().checkDepm(dummy, cur))
757 cur.forceBufferUpdate();
763 case LFUN_CHAR_BACKWARD:
764 case LFUN_CHAR_BACKWARD_SELECT: {
765 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
766 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
767 bool const cur_moved = cursorBackward(cur);
768 needsUpdate |= cur_moved;
770 if (!cur_moved && oldTopSlice == cur.top()
771 && cur.boundary() == oldBoundary) {
773 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
775 // we will probably be moving out the inset, so we should execute
776 // the depm-mechanism, but only when the cursor has a place to
777 // go outside this inset, i.e. in a slice above.
778 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
779 // The cursor hasn't changed yet. To give the
780 // DEPM the possibility of doing something we must
781 // provide it with two different cursors.
783 dummy.pos() = cur.lastpos();
784 dummy.pit() = cur.lastpit();
785 if (cur.bv().checkDepm(dummy, cur))
786 cur.forceBufferUpdate();
793 case LFUN_CHAR_LEFT_SELECT:
794 if (lyxrc.visual_cursor) {
795 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
796 bool const cur_moved = cursorVisLeft(cur);
797 needsUpdate |= cur_moved;
798 if (!cur_moved && oldTopSlice == cur.top()
799 && cur.boundary() == oldBoundary) {
801 cmd = FuncRequest(LFUN_FINISHED_LEFT);
804 if (cur.reverseDirectionNeeded()) {
805 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
806 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
808 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
809 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
816 case LFUN_CHAR_RIGHT:
817 case LFUN_CHAR_RIGHT_SELECT:
818 if (lyxrc.visual_cursor) {
819 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
820 bool const cur_moved = cursorVisRight(cur);
821 needsUpdate |= cur_moved;
822 if (!cur_moved && oldTopSlice == cur.top()
823 && cur.boundary() == oldBoundary) {
825 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
828 if (cur.reverseDirectionNeeded()) {
829 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
830 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
832 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
833 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
842 case LFUN_DOWN_SELECT:
845 // stop/start the selection
846 bool select = cmd.action() == LFUN_DOWN_SELECT ||
847 cmd.action() == LFUN_UP_SELECT;
849 // move cursor up/down
850 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
851 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
853 if (!atFirstOrLastRow) {
854 needsUpdate |= cur.selHandle(select);
855 cur.upDownInText(up, needsUpdate);
856 needsUpdate |= cur.beforeDispatchCursor().inMathed();
858 pos_type newpos = up ? 0 : cur.lastpos();
859 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
860 needsUpdate |= cur.selHandle(select);
861 // we do not reset the targetx of the cursor
863 needsUpdate |= bv->checkDepm(cur, bv->cursor());
864 cur.updateTextTargetOffset();
866 cur.forceBufferUpdate();
870 // if the cursor cannot be moved up or down do not remove
871 // the selection right now, but wait for the next dispatch.
873 needsUpdate |= cur.selHandle(select);
874 cur.upDownInText(up, needsUpdate);
881 case LFUN_PARAGRAPH_UP:
882 case LFUN_PARAGRAPH_UP_SELECT:
883 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
884 needsUpdate |= cursorUpParagraph(cur);
887 case LFUN_PARAGRAPH_DOWN:
888 case LFUN_PARAGRAPH_DOWN_SELECT:
889 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
890 needsUpdate |= cursorDownParagraph(cur);
893 case LFUN_LINE_BEGIN:
894 case LFUN_LINE_BEGIN_SELECT:
895 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
896 needsUpdate |= tm->cursorHome(cur);
900 case LFUN_LINE_END_SELECT:
901 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
902 needsUpdate |= tm->cursorEnd(cur);
905 case LFUN_SECTION_SELECT: {
906 Buffer const & buf = *cur.buffer();
907 pit_type const pit = cur.pit();
908 ParagraphList & pars = buf.text().paragraphs();
909 ParagraphList::iterator bgn = pars.begin();
910 // The first paragraph of the area to be selected:
911 ParagraphList::iterator start = lyx::next(bgn, pit);
912 // The final paragraph of area to be selected:
913 ParagraphList::iterator finish = start;
914 ParagraphList::iterator end = pars.end();
916 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
917 if (thistoclevel == Layout::NOT_IN_TOC)
921 Cursor const old_cur = cur;
922 needsUpdate |= cur.selHandle(true);
924 // Move out (down) from this section header
928 // Seek the one (on same level) below
929 for (; finish != end; ++finish, ++cur.pit()) {
930 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
931 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
934 cur.pos() = cur.lastpos();
936 cur.setCurrentFont();
938 needsUpdate |= cur != old_cur;
942 case LFUN_WORD_RIGHT:
943 case LFUN_WORD_RIGHT_SELECT:
944 if (lyxrc.visual_cursor) {
945 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
946 bool const cur_moved = cursorVisRightOneWord(cur);
947 needsUpdate |= cur_moved;
948 if (!cur_moved && oldTopSlice == cur.top()
949 && cur.boundary() == oldBoundary) {
951 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
954 if (cur.reverseDirectionNeeded()) {
955 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
956 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
958 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
959 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
966 case LFUN_WORD_FORWARD:
967 case LFUN_WORD_FORWARD_SELECT: {
968 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
969 bool const cur_moved = cursorForwardOneWord(cur);
970 needsUpdate |= cur_moved;
972 if (!cur_moved && oldTopSlice == cur.top()
973 && cur.boundary() == oldBoundary) {
975 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
977 // we will probably be moving out the inset, so we should execute
978 // the depm-mechanism, but only when the cursor has a place to
979 // go outside this inset, i.e. in a slice above.
980 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
981 && cur.pit() == cur.lastpit()) {
982 // The cursor hasn't changed yet. To give the
983 // DEPM the possibility of doing something we must
984 // provide it with two different cursors.
986 dummy.pos() = dummy.pit() = 0;
987 if (cur.bv().checkDepm(dummy, cur))
988 cur.forceBufferUpdate();
995 case LFUN_WORD_LEFT_SELECT:
996 if (lyxrc.visual_cursor) {
997 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
998 bool const cur_moved = cursorVisLeftOneWord(cur);
999 needsUpdate |= cur_moved;
1000 if (!cur_moved && oldTopSlice == cur.top()
1001 && cur.boundary() == oldBoundary) {
1003 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1006 if (cur.reverseDirectionNeeded()) {
1007 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1008 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1010 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1011 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1018 case LFUN_WORD_BACKWARD:
1019 case LFUN_WORD_BACKWARD_SELECT: {
1020 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1021 bool const cur_moved = cursorBackwardOneWord(cur);
1022 needsUpdate |= cur_moved;
1024 if (!cur_moved && oldTopSlice == cur.top()
1025 && cur.boundary() == oldBoundary) {
1027 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1029 // we will probably be moving out the inset, so we should execute
1030 // the depm-mechanism, but only when the cursor has a place to
1031 // go outside this inset, i.e. in a slice above.
1032 if (cur.depth() > 1 && cur.pos() == 0
1033 && cur.pit() == 0) {
1034 // The cursor hasn't changed yet. To give the
1035 // DEPM the possibility of doing something we must
1036 // provide it with two different cursors.
1038 dummy.pos() = cur.lastpos();
1039 dummy.pit() = cur.lastpit();
1040 if (cur.bv().checkDepm(dummy, cur))
1041 cur.forceBufferUpdate();
1047 case LFUN_WORD_SELECT: {
1048 selectWord(cur, WHOLE_WORD);
1049 finishChange(cur, true);
1053 case LFUN_NEWLINE_INSERT: {
1054 InsetNewlineParams inp;
1055 docstring arg = cmd.argument();
1056 if (arg == "linebreak")
1057 inp.kind = InsetNewlineParams::LINEBREAK;
1059 inp.kind = InsetNewlineParams::NEWLINE;
1060 cap::replaceSelection(cur);
1062 cur.insert(new InsetNewline(inp));
1064 moveCursor(cur, false);
1068 case LFUN_TAB_INSERT: {
1069 bool const multi_par_selection = cur.selection() &&
1070 cur.selBegin().pit() != cur.selEnd().pit();
1071 if (multi_par_selection) {
1072 // If there is a multi-paragraph selection, a tab is inserted
1073 // at the beginning of each paragraph.
1074 cur.recordUndoSelection();
1075 pit_type const pit_end = cur.selEnd().pit();
1076 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1077 pars_[pit].insertChar(0, '\t',
1078 bv->buffer().params().track_changes);
1079 // Update the selection pos to make sure the selection does not
1080 // change as the inserted tab will increase the logical pos.
1081 if (cur.realAnchor().pit() == pit)
1082 cur.realAnchor().forwardPos();
1083 if (cur.pit() == pit)
1088 // Maybe we shouldn't allow tabs within a line, because they
1089 // are not (yet) aligned as one might do expect.
1090 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
1096 case LFUN_TAB_DELETE: {
1097 bool const tc = bv->buffer().params().track_changes;
1098 if (cur.selection()) {
1099 // If there is a selection, a tab (if present) is removed from
1100 // the beginning of each paragraph.
1101 cur.recordUndoSelection();
1102 pit_type const pit_end = cur.selEnd().pit();
1103 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1104 Paragraph & par = paragraphs()[pit];
1107 char_type const c = par.getChar(0);
1108 if (c == '\t' || c == ' ') {
1109 // remove either 1 tab or 4 spaces.
1110 int const n = (c == ' ' ? 4 : 1);
1111 for (int i = 0; i < n
1112 && !par.empty() && par.getChar(0) == c; ++i) {
1113 if (cur.pit() == pit)
1115 if (cur.realAnchor().pit() == pit
1116 && cur.realAnchor().pos() > 0 )
1117 cur.realAnchor().backwardPos();
1118 par.eraseChar(0, tc);
1124 // If there is no selection, try to remove a tab or some spaces
1125 // before the position of the cursor.
1126 Paragraph & par = paragraphs()[cur.pit()];
1127 pos_type const pos = cur.pos();
1132 char_type const c = par.getChar(pos - 1);
1136 par.eraseChar(cur.pos(), tc);
1138 for (int n_spaces = 0;
1140 && par.getChar(cur.pos() - 1) == ' '
1144 par.eraseChar(cur.pos(), tc);
1151 case LFUN_CHAR_DELETE_FORWARD:
1152 if (!cur.selection()) {
1153 if (cur.pos() == cur.paragraph().size())
1154 // Par boundary, force full-screen update
1155 singleParUpdate = false;
1156 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1158 cur.selection(true);
1163 needsUpdate |= erase(cur);
1166 cutSelection(cur, false);
1167 singleParUpdate = false;
1169 moveCursor(cur, false);
1172 case LFUN_CHAR_DELETE_BACKWARD:
1173 if (!cur.selection()) {
1174 if (bv->getIntl().getTransManager().backspace()) {
1175 bool par_boundary = cur.pos() == 0;
1176 bool first_par = cur.pit() == 0;
1177 // Par boundary, full-screen update
1179 singleParUpdate = false;
1180 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1182 cur.selection(true);
1187 needsUpdate |= backspace(cur);
1189 if (par_boundary && !first_par && cur.pos() > 0
1190 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1191 needsUpdate |= backspace(cur);
1196 cutSelection(cur, false);
1197 singleParUpdate = false;
1201 case LFUN_PARAGRAPH_BREAK: {
1202 cap::replaceSelection(cur);
1203 pit_type pit = cur.pit();
1204 Paragraph const & par = pars_[pit];
1205 bool lastpar = (pit == pit_type(pars_.size() - 1));
1206 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1207 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1208 if (prev < pit && cur.pos() == par.beginOfBody()
1209 && !par.size() && !par.isEnvSeparator(cur.pos())
1210 && !par.layout().isCommand()
1211 && pars_[prev].layout() != par.layout()
1212 && pars_[prev].layout().isEnvironment()
1213 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1214 if (par.layout().isEnvironment()
1215 && pars_[prev].getDepth() == par.getDepth()) {
1216 docstring const layout = par.layout().name();
1217 DocumentClass const & tc = bv->buffer().params().documentClass();
1218 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1219 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1220 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1221 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1223 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1224 breakParagraph(cur);
1226 Font const f(inherit_font, cur.current_font.language());
1227 pars_[cur.pit() - 1].resetFonts(f);
1229 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1231 breakParagraph(cur, cmd.getArg(0) == "inverse");
1234 // If we have a list and autoinsert item insets,
1236 Layout::LaTeXArgMap args = par.layout().args();
1237 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1238 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1239 for (; lait != laend; ++lait) {
1240 Layout::latexarg arg = (*lait).second;
1241 if (arg.autoinsert && prefixIs((*lait).first, "item:")) {
1242 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1249 case LFUN_INSET_INSERT: {
1252 // We have to avoid triggering InstantPreview loading
1253 // before inserting into the document. See bug #5626.
1254 bool loaded = bv->buffer().isFullyLoaded();
1255 bv->buffer().setFullyLoaded(false);
1256 Inset * inset = createInset(&bv->buffer(), cmd);
1257 bv->buffer().setFullyLoaded(loaded);
1260 // FIXME (Abdel 01/02/2006):
1261 // What follows would be a partial fix for bug 2154:
1262 // http://www.lyx.org/trac/ticket/2154
1263 // This automatically put the label inset _after_ a
1264 // numbered section. It should be possible to extend the mechanism
1265 // to any kind of LateX environement.
1266 // The correct way to fix that bug would be at LateX generation.
1267 // I'll let the code here for reference as it could be used for some
1268 // other feature like "automatic labelling".
1270 Paragraph & par = pars_[cur.pit()];
1271 if (inset->lyxCode() == LABEL_CODE
1272 && !par.layout().counter.empty()) {
1273 // Go to the end of the paragraph
1274 // Warning: Because of Change-Tracking, the last
1275 // position is 'size()' and not 'size()-1':
1276 cur.pos() = par.size();
1277 // Insert a new paragraph
1278 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1282 if (cur.selection())
1283 cutSelection(cur, false);
1285 cur.forceBufferUpdate();
1286 if (inset->editable() && inset->asInsetText())
1287 inset->edit(cur, true);
1291 // trigger InstantPreview now
1292 if (inset->lyxCode() == EXTERNAL_CODE) {
1293 InsetExternal & ins =
1294 static_cast<InsetExternal &>(*inset);
1295 ins.updatePreview();
1302 case LFUN_INSET_DISSOLVE: {
1303 if (dissolveInset(cur)) {
1305 cur.forceBufferUpdate();
1310 case LFUN_SET_GRAPHICS_GROUP: {
1311 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1317 string id = to_utf8(cmd.argument());
1318 string grp = graphics::getGroupParams(bv->buffer(), id);
1319 InsetGraphicsParams tmp, inspar = ins->getParams();
1322 inspar.groupId = to_utf8(cmd.argument());
1324 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1325 tmp.filename = inspar.filename;
1329 ins->setParams(inspar);
1333 case LFUN_SPACE_INSERT:
1334 if (cur.paragraph().layout().free_spacing)
1335 insertChar(cur, ' ');
1337 doInsertInset(cur, this, cmd, false, false);
1340 moveCursor(cur, false);
1343 case LFUN_SPECIALCHAR_INSERT: {
1344 string const name = to_utf8(cmd.argument());
1345 if (name == "hyphenation")
1346 specialChar(cur, InsetSpecialChar::HYPHENATION);
1347 else if (name == "allowbreak")
1348 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1349 else if (name == "ligature-break")
1350 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1351 else if (name == "slash")
1352 specialChar(cur, InsetSpecialChar::SLASH);
1353 else if (name == "nobreakdash")
1354 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1355 else if (name == "dots")
1356 specialChar(cur, InsetSpecialChar::LDOTS);
1357 else if (name == "end-of-sentence")
1358 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1359 else if (name == "menu-separator")
1360 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1361 else if (name == "lyx")
1362 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1363 else if (name == "tex")
1364 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1365 else if (name == "latex")
1366 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1367 else if (name == "latex2e")
1368 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1369 else if (name.empty())
1370 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1372 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1376 case LFUN_IPAMACRO_INSERT: {
1377 string const arg = cmd.getArg(0);
1378 if (arg == "deco") {
1379 // Open the inset, and move the current selection
1381 doInsertInset(cur, this, cmd, true, true);
1383 // Some insets are numbered, others are shown in the outline pane so
1384 // let's update the labels and the toc backend.
1385 cur.forceBufferUpdate();
1388 if (arg == "tone-falling")
1389 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1390 else if (arg == "tone-rising")
1391 ipaChar(cur, InsetIPAChar::TONE_RISING);
1392 else if (arg == "tone-high-rising")
1393 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1394 else if (arg == "tone-low-rising")
1395 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1396 else if (arg == "tone-high-rising-falling")
1397 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1398 else if (arg.empty())
1399 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1401 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1405 case LFUN_WORD_UPCASE:
1406 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1409 case LFUN_WORD_LOWCASE:
1410 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1413 case LFUN_WORD_CAPITALIZE:
1414 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1417 case LFUN_CHARS_TRANSPOSE:
1418 charsTranspose(cur);
1422 cur.message(_("Paste"));
1423 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1424 cap::replaceSelection(cur);
1426 // without argument?
1427 string const arg = to_utf8(cmd.argument());
1429 bool tryGraphics = true;
1430 if (theClipboard().isInternal())
1431 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1432 else if (theClipboard().hasTextContents()) {
1433 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1434 true, Clipboard::AnyTextType))
1435 tryGraphics = false;
1437 if (tryGraphics && theClipboard().hasGraphicsContents())
1438 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1439 } else if (isStrUnsignedInt(arg)) {
1440 // we have a numerical argument
1441 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1442 convert<unsigned int>(arg));
1443 } else if (arg == "html" || arg == "latex") {
1444 Clipboard::TextType type = (arg == "html") ?
1445 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1446 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1448 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1450 type = Clipboard::PdfGraphicsType;
1451 else if (arg == "png")
1452 type = Clipboard::PngGraphicsType;
1453 else if (arg == "jpeg")
1454 type = Clipboard::JpegGraphicsType;
1455 else if (arg == "linkback")
1456 type = Clipboard::LinkBackGraphicsType;
1457 else if (arg == "emf")
1458 type = Clipboard::EmfGraphicsType;
1459 else if (arg == "wmf")
1460 type = Clipboard::WmfGraphicsType;
1462 // we also check in getStatus()
1463 LYXERR0("Unrecognized graphics type: " << arg);
1465 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1468 bv->buffer().errors("Paste");
1469 cur.clearSelection(); // bug 393
1475 cutSelection(cur, true);
1476 cur.message(_("Cut"));
1481 cur.message(_("Copy"));
1484 case LFUN_SERVER_GET_XY:
1485 cur.message(from_utf8(
1486 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1487 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1490 case LFUN_SERVER_SET_XY: {
1493 istringstream is(to_utf8(cmd.argument()));
1496 lyxerr << "SETXY: Could not parse coordinates in '"
1497 << to_utf8(cmd.argument()) << endl;
1499 tm->setCursorFromCoordinates(cur, x, y);
1503 case LFUN_SERVER_GET_LAYOUT:
1504 cur.message(cur.paragraph().layout().name());
1508 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1509 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1510 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1512 Paragraph const & para = cur.paragraph();
1513 docstring const old_layout = para.layout().name();
1514 DocumentClass const & tclass = bv->buffer().params().documentClass();
1517 layout = tclass.defaultLayoutName();
1519 if (owner_->forcePlainLayout())
1520 // in this case only the empty layout is allowed
1521 layout = tclass.plainLayoutName();
1522 else if (para.usePlainLayout()) {
1523 // in this case, default layout maps to empty layout
1524 if (layout == tclass.defaultLayoutName())
1525 layout = tclass.plainLayoutName();
1527 // otherwise, the empty layout maps to the default
1528 if (layout == tclass.plainLayoutName())
1529 layout = tclass.defaultLayoutName();
1532 bool hasLayout = tclass.hasLayout(layout);
1534 // If the entry is obsolete, use the new one instead.
1536 docstring const & obs = tclass[layout].obsoleted_by();
1542 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1543 from_utf8(N_(" not known")));
1547 bool change_layout = (old_layout != layout);
1549 if (!change_layout && cur.selection() &&
1550 cur.selBegin().pit() != cur.selEnd().pit())
1552 pit_type spit = cur.selBegin().pit();
1553 pit_type epit = cur.selEnd().pit() + 1;
1554 while (spit != epit) {
1555 if (pars_[spit].layout().name() != old_layout) {
1556 change_layout = true;
1563 if (change_layout) {
1564 setLayout(cur, layout);
1565 if (cur.pit() > 0 && !ignoreautonests) {
1566 set<docstring> const & autonests =
1567 pars_[cur.pit() - 1].layout().autonests();
1568 set<docstring> const & autonested =
1569 pars_[cur.pit()].layout().isAutonestedBy();
1570 if (autonests.find(layout) != autonests.end()
1571 || autonested.find(old_layout) != autonested.end())
1572 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1576 Layout::LaTeXArgMap args = tclass[layout].args();
1577 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1578 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1579 for (; lait != laend; ++lait) {
1580 Layout::latexarg arg = (*lait).second;
1581 if (arg.autoinsert) {
1582 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1590 case LFUN_ENVIRONMENT_SPLIT: {
1591 bool const outer = cmd.argument() == "outer";
1592 bool const previous = cmd.argument() == "previous";
1593 bool const before = cmd.argument() == "before";
1594 bool const normal = cmd.argument().empty();
1595 Paragraph const & para = cur.paragraph();
1597 if (para.layout().isEnvironment())
1598 layout = para.layout().name();
1599 depth_type split_depth = cur.paragraph().params().depth();
1600 depth_type nextpar_depth = 0;
1601 if (outer || previous) {
1602 // check if we have an environment in our scope
1603 pit_type pit = cur.pit();
1604 Paragraph cpar = pars_[pit];
1610 if (layout.empty() && previous
1611 && cpar.layout().isEnvironment()
1612 && cpar.params().depth() <= split_depth)
1613 layout = cpar.layout().name();
1614 if (cpar.params().depth() < split_depth
1615 && cpar.layout().isEnvironment()) {
1617 layout = cpar.layout().name();
1618 split_depth = cpar.params().depth();
1620 if (cpar.params().depth() == 0)
1624 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1625 // save nesting of following paragraph
1626 Paragraph cpar = pars_[cur.pit() + 1];
1627 nextpar_depth = cpar.params().depth();
1630 cur.top().setPitPos(cur.pit(), 0);
1631 if (before || cur.pos() > 0)
1632 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1633 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1634 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1636 while (cur.paragraph().params().depth() > split_depth)
1637 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1639 DocumentClass const & tc = bv->buffer().params().documentClass();
1640 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1641 + from_ascii("\" ignoreautonests")));
1642 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1645 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1646 while (cur.paragraph().params().depth() < split_depth)
1647 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1650 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1651 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1652 if ((outer || normal) && nextpar_depth > 0) {
1653 // restore nesting of following paragraph
1654 DocIterator scur = cur;
1655 depth_type const max_depth = cur.paragraph().params().depth() + 1;
1657 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth)) {
1658 depth_type const olddepth = cur.paragraph().params().depth();
1659 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1660 if (olddepth == cur.paragraph().params().depth())
1661 // leave loop if no incrementation happens
1664 cur.setCursor(scur);
1670 case LFUN_CLIPBOARD_PASTE:
1671 cap::replaceSelection(cur);
1672 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1673 cmd.argument() == "paragraph");
1674 bv->buffer().errors("Paste");
1677 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1678 cap::replaceSelection(cur);
1679 pasteSimpleText(cur, cmd.argument() == "paragraph");
1682 case LFUN_PRIMARY_SELECTION_PASTE:
1683 cap::replaceSelection(cur);
1684 pasteString(cur, theSelection().get(),
1685 cmd.argument() == "paragraph");
1688 case LFUN_SELECTION_PASTE:
1689 // Copy the selection buffer to the clipboard stack,
1690 // because we want it to appear in the "Edit->Paste
1692 cap::replaceSelection(cur);
1693 cap::copySelectionToStack();
1694 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1695 bv->buffer().errors("Paste");
1698 case LFUN_QUOTE_INSERT: {
1699 cap::replaceSelection(cur);
1702 Paragraph const & par = cur.paragraph();
1703 pos_type pos = cur.pos();
1704 // Ignore deleted text before cursor
1705 while (pos > 0 && par.isDeleted(pos - 1))
1708 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1710 // Guess quote side.
1711 // A space triggers an opening quote. This is passed if the preceding
1712 // char/inset is a space or at paragraph start.
1714 if (pos > 0 && !par.isSpace(pos - 1)) {
1715 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1716 // If an opening double quotation mark precedes, and this
1717 // is a single quote, make it opening as well
1719 static_cast<InsetQuotes &>(*cur.prevInset());
1720 string const type = ins.getType();
1721 if (!suffixIs(type, "ld") || !inner)
1722 c = par.getChar(pos - 1);
1724 else if (!cur.prevInset()
1725 || (cur.prevInset() && cur.prevInset()->isChar()))
1726 // If a char precedes, pass that and let InsetQuote decide
1727 c = par.getChar(pos - 1);
1730 if (par.getInset(pos - 1)
1731 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1732 // skip "invisible" insets
1736 c = par.getChar(pos - 1);
1741 InsetQuotesParams::QuoteLevel const quote_level = inner
1742 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1743 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1744 cur.buffer()->updateBuffer();
1749 case LFUN_DATE_INSERT: {
1750 string const format = cmd.argument().empty()
1751 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1752 string const time = formatted_time(current_time(), format);
1753 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1757 case LFUN_MOUSE_TRIPLE:
1758 if (cmd.button() == mouse_button::button1) {
1759 tm->cursorHome(cur);
1767 case LFUN_MOUSE_DOUBLE:
1768 if (cmd.button() == mouse_button::button1) {
1769 selectWord(cur, WHOLE_WORD);
1774 // Single-click on work area
1775 case LFUN_MOUSE_PRESS: {
1776 // We are not marking a selection with the keyboard in any case.
1777 Cursor & bvcur = cur.bv().cursor();
1778 bvcur.setMark(false);
1779 switch (cmd.button()) {
1780 case mouse_button::button1:
1781 if (!bvcur.selection())
1783 bvcur.resetAnchor();
1784 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1785 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1786 if (bvcur.wordSelection())
1787 selectWord(bvcur, WHOLE_WORD);
1790 case mouse_button::button2:
1791 if (lyxrc.mouse_middlebutton_paste) {
1792 // Middle mouse pasting.
1793 bv->mouseSetCursor(cur);
1795 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1796 "selection-paste ; primary-selection-paste paragraph"));
1798 cur.noScreenUpdate();
1801 case mouse_button::button3: {
1802 // Don't do anything if we right-click a
1803 // selection, a context menu will popup.
1804 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1805 && cur < bvcur.selectionEnd()) {
1806 cur.noScreenUpdate();
1809 if (!bv->mouseSetCursor(cur, false))
1810 cur.screenUpdateFlags(Update::FitCursor);
1816 } // switch (cmd.button())
1819 case LFUN_MOUSE_MOTION: {
1820 // Mouse motion with right or middle mouse do nothing for now.
1821 if (cmd.button() != mouse_button::button1) {
1822 cur.noScreenUpdate();
1825 // ignore motions deeper nested than the real anchor
1826 Cursor & bvcur = cur.bv().cursor();
1827 if (!bvcur.realAnchor().hasPart(cur)) {
1831 CursorSlice old = bvcur.top();
1833 int const wh = bv->workHeight();
1834 int const y = max(0, min(wh - 1, cmd.y()));
1836 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1837 cur.setTargetX(cmd.x());
1838 // Don't allow selecting a separator inset
1839 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1842 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1843 else if (cmd.y() < 0)
1844 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1845 // This is to allow jumping over large insets
1846 if (cur.top() == old) {
1848 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1849 else if (cmd.y() < 0)
1850 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1852 // We continue with our existing selection or start a new one, so don't
1853 // reset the anchor.
1854 bvcur.setCursor(cur);
1855 bvcur.selection(true);
1856 bvcur.setCurrentFont();
1857 if (cur.top() == old) {
1858 // We didn't move one iota, so no need to update the screen.
1859 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1860 //cur.noScreenUpdate();
1866 case LFUN_MOUSE_RELEASE:
1867 switch (cmd.button()) {
1868 case mouse_button::button1:
1869 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1870 // If there is a new selection, update persistent selection;
1871 // otherwise, single click does not clear persistent selection
1873 if (cur.selection()) {
1874 // Finish selection. If double click,
1875 // cur is moved to the end of word by
1876 // selectWord but bvcur is current
1878 cur.bv().cursor().setSelection();
1879 // We might have removed an empty but drawn selection
1880 // (probably a margin)
1881 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1883 cur.noScreenUpdate();
1884 // FIXME: We could try to handle drag and drop of selection here.
1887 case mouse_button::button2:
1888 // Middle mouse pasting is handled at mouse press time,
1889 // see LFUN_MOUSE_PRESS.
1890 cur.noScreenUpdate();
1893 case mouse_button::button3:
1894 // Cursor was set at LFUN_MOUSE_PRESS time.
1895 // FIXME: If there is a selection we could try to handle a special
1896 // drag & drop context menu.
1897 cur.noScreenUpdate();
1900 case mouse_button::none:
1901 case mouse_button::button4:
1902 case mouse_button::button5:
1904 } // switch (cmd.button())
1908 case LFUN_SELF_INSERT: {
1909 if (cmd.argument().empty())
1912 // Automatically delete the currently selected
1913 // text and replace it with what is being
1914 // typed in now. Depends on lyxrc settings
1915 // "auto_region_delete", which defaults to
1918 if (lyxrc.auto_region_delete && cur.selection())
1919 cutSelection(cur, false);
1920 cur.clearSelection();
1922 for (char_type c : cmd.argument())
1923 bv->translateAndInsert(c, this, cur);
1926 moveCursor(cur, false);
1927 cur.markNewWordPosition();
1928 bv->bookmarkEditPosition();
1932 case LFUN_HREF_INSERT: {
1933 docstring content = cmd.argument();
1934 if (content.empty() && cur.selection())
1935 content = cur.selectionAsString(false);
1937 InsetCommandParams p(HYPERLINK_CODE);
1938 if (!content.empty()){
1939 // if it looks like a link, we'll put it as target,
1940 // otherwise as name (bug #8792).
1943 // regex_match(to_utf8(content), matches, link_re)
1944 // because smatch stores pointers to the substrings rather
1945 // than making copies of them. And those pointers become
1946 // invalid after regex_match returns, since it is then
1947 // being given a temporary object. (Thanks to Georg for
1948 // figuring that out.)
1949 regex const link_re("^([a-z]+):.*");
1951 string const c = to_utf8(lowercase(content));
1953 if (c.substr(0,7) == "mailto:") {
1954 p["target"] = content;
1955 p["type"] = from_ascii("mailto:");
1956 } else if (regex_match(c, matches, link_re)) {
1957 p["target"] = content;
1958 string protocol = matches.str(1);
1959 if (protocol == "file")
1960 p["type"] = from_ascii("file:");
1962 p["name"] = content;
1964 string const data = InsetCommand::params2string(p);
1966 // we need to have a target. if we already have one, then
1967 // that gets used at the default for the name, too, which
1968 // is probably what is wanted.
1969 if (p["target"].empty()) {
1970 bv->showDialog("href", data);
1972 FuncRequest fr(LFUN_INSET_INSERT, data);
1978 case LFUN_LABEL_INSERT: {
1979 InsetCommandParams p(LABEL_CODE);
1980 // Try to generate a valid label
1981 p["name"] = (cmd.argument().empty()) ?
1982 cur.getPossibleLabel() :
1984 string const data = InsetCommand::params2string(p);
1986 if (cmd.argument().empty()) {
1987 bv->showDialog("label", data);
1989 FuncRequest fr(LFUN_INSET_INSERT, data);
1995 case LFUN_INFO_INSERT: {
1997 if (cmd.argument().empty() && cur.selection()) {
1998 // if command argument is empty use current selection as parameter.
1999 docstring ds = cur.selectionAsString(false);
2000 cutSelection(cur, false);
2001 FuncRequest cmd0(cmd, ds);
2002 inset = createInset(cur.buffer(), cmd0);
2004 inset = createInset(cur.buffer(), cmd);
2009 insertInset(cur, inset);
2010 cur.forceBufferUpdate();
2014 case LFUN_CAPTION_INSERT:
2015 case LFUN_FOOTNOTE_INSERT:
2016 case LFUN_NOTE_INSERT:
2017 case LFUN_BOX_INSERT:
2018 case LFUN_BRANCH_INSERT:
2019 case LFUN_PHANTOM_INSERT:
2020 case LFUN_ERT_INSERT:
2021 case LFUN_LISTING_INSERT:
2022 case LFUN_MARGINALNOTE_INSERT:
2023 case LFUN_ARGUMENT_INSERT:
2024 case LFUN_INDEX_INSERT:
2025 case LFUN_PREVIEW_INSERT:
2026 case LFUN_SCRIPT_INSERT:
2027 case LFUN_IPA_INSERT:
2028 // Open the inset, and move the current selection
2030 doInsertInset(cur, this, cmd, true, true);
2032 // Some insets are numbered, others are shown in the outline pane so
2033 // let's update the labels and the toc backend.
2034 cur.forceBufferUpdate();
2037 case LFUN_FLEX_INSERT: {
2038 // Open the inset, and move the current selection
2040 bool const sel = cur.selection();
2041 doInsertInset(cur, this, cmd, true, true);
2042 // Insert auto-insert arguments
2043 bool autoargs = false;
2044 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
2045 Layout::LaTeXArgMap::const_iterator lait = args.begin();
2046 Layout::LaTeXArgMap::const_iterator const laend = args.end();
2047 for (; lait != laend; ++lait) {
2048 Layout::latexarg arg = (*lait).second;
2049 if (arg.autoinsert) {
2050 // The cursor might have been invalidated by the replaceSelection.
2051 cur.buffer()->changed(true);
2052 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
2059 cur.leaveInset(cur.inset());
2062 // Some insets are numbered, others are shown in the outline pane so
2063 // let's update the labels and the toc backend.
2064 cur.forceBufferUpdate();
2068 case LFUN_TABULAR_INSERT:
2069 // if there were no arguments, just open the dialog
2070 if (doInsertInset(cur, this, cmd, false, true))
2073 bv->showDialog("tabularcreate");
2077 case LFUN_FLOAT_INSERT:
2078 case LFUN_FLOAT_WIDE_INSERT:
2079 case LFUN_WRAP_INSERT: {
2080 // will some content be moved into the inset?
2081 bool const content = cur.selection();
2082 // does the content consist of multiple paragraphs?
2083 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2085 doInsertInset(cur, this, cmd, true, true);
2088 // If some single-par content is moved into the inset,
2089 // doInsertInset puts the cursor outside the inset.
2090 // To insert the caption we put it back into the inset.
2091 // FIXME cleanup doInsertInset to avoid such dances!
2092 if (content && singlepar)
2095 ParagraphList & pars = cur.text()->paragraphs();
2097 DocumentClass const & tclass = bv->buffer().params().documentClass();
2099 // add a separate paragraph for the caption inset
2100 pars.push_back(Paragraph());
2101 pars.back().setInsetOwner(&cur.text()->inset());
2102 pars.back().setPlainOrDefaultLayout(tclass);
2103 int cap_pit = pars.size() - 1;
2105 // if an empty inset was created, we create an additional empty
2106 // paragraph at the bottom so that the user can choose where to put
2107 // the graphics (or table).
2109 pars.push_back(Paragraph());
2110 pars.back().setInsetOwner(&cur.text()->inset());
2111 pars.back().setPlainOrDefaultLayout(tclass);
2114 // reposition the cursor to the caption
2115 cur.pit() = cap_pit;
2117 // FIXME: This Text/Cursor dispatch handling is a mess!
2118 // We cannot use Cursor::dispatch here it needs access to up to
2120 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2121 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2122 cur.forceBufferUpdate();
2123 cur.screenUpdateFlags(Update::Force);
2124 // FIXME: When leaving the Float (or Wrap) inset we should
2125 // delete any empty paragraph left above or below the
2130 case LFUN_NOMENCL_INSERT: {
2131 InsetCommandParams p(NOMENCL_CODE);
2132 if (cmd.argument().empty())
2133 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
2135 p["symbol"] = cmd.argument();
2136 string const data = InsetCommand::params2string(p);
2137 bv->showDialog("nomenclature", data);
2141 case LFUN_INDEX_PRINT: {
2142 InsetCommandParams p(INDEX_PRINT_CODE);
2143 if (cmd.argument().empty())
2144 p["type"] = from_ascii("idx");
2146 p["type"] = cmd.argument();
2147 string const data = InsetCommand::params2string(p);
2148 FuncRequest fr(LFUN_INSET_INSERT, data);
2153 case LFUN_NOMENCL_PRINT:
2154 case LFUN_NEWPAGE_INSERT:
2156 doInsertInset(cur, this, cmd, false, false);
2160 case LFUN_SEPARATOR_INSERT: {
2161 doInsertInset(cur, this, cmd, false, false);
2163 // remove a following space
2164 Paragraph & par = cur.paragraph();
2165 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2166 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2170 case LFUN_DEPTH_DECREMENT:
2171 changeDepth(cur, DEC_DEPTH);
2174 case LFUN_DEPTH_INCREMENT:
2175 changeDepth(cur, INC_DEPTH);
2178 case LFUN_REGEXP_MODE:
2179 regexpDispatch(cur, cmd);
2182 case LFUN_MATH_MODE: {
2183 if (cmd.argument() == "on" || cmd.argument() == "") {
2184 // don't pass "on" as argument
2185 // (it would appear literally in the first cell)
2186 docstring sel = cur.selectionAsString(false);
2187 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2188 // create a macro template if we see "\\newcommand" somewhere, and
2189 // an ordinary formula otherwise
2191 && (sel.find(from_ascii("\\newcommand")) != string::npos
2192 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2193 || sel.find(from_ascii("\\def")) != string::npos)
2194 && macro->fromString(sel)) {
2196 replaceSelection(cur);
2199 // no meaningful macro template was found
2201 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2204 // The argument is meaningful
2205 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2206 // has a different meaning in math mode
2207 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2211 case LFUN_MATH_MACRO:
2212 if (cmd.argument().empty())
2213 cur.errorMessage(from_utf8(N_("Missing argument")));
2216 string s = to_utf8(cmd.argument());
2217 string const s1 = token(s, ' ', 1);
2218 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2219 string const s2 = token(s, ' ', 2);
2220 MacroType type = MacroTypeNewcommand;
2222 type = MacroTypeDef;
2223 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2224 from_utf8(token(s, ' ', 0)), nargs, false, type);
2225 inset->setBuffer(bv->buffer());
2226 insertInset(cur, inset);
2228 // enter macro inset and select the name
2230 cur.top().pos() = cur.top().lastpos();
2232 cur.selection(true);
2233 cur.top().pos() = 0;
2237 case LFUN_MATH_DISPLAY:
2238 case LFUN_MATH_SUBSCRIPT:
2239 case LFUN_MATH_SUPERSCRIPT:
2240 case LFUN_MATH_INSERT:
2241 case LFUN_MATH_AMS_MATRIX:
2242 case LFUN_MATH_MATRIX:
2243 case LFUN_MATH_DELIM:
2244 case LFUN_MATH_BIGDELIM:
2245 mathDispatch(cur, cmd);
2248 case LFUN_FONT_EMPH: {
2249 Font font(ignore_font, ignore_language);
2250 font.fontInfo().setEmph(FONT_TOGGLE);
2251 toggleAndShow(cur, this, font);
2255 case LFUN_FONT_ITAL: {
2256 Font font(ignore_font, ignore_language);
2257 font.fontInfo().setShape(ITALIC_SHAPE);
2258 toggleAndShow(cur, this, font);
2262 case LFUN_FONT_BOLD:
2263 case LFUN_FONT_BOLDSYMBOL: {
2264 Font font(ignore_font, ignore_language);
2265 font.fontInfo().setSeries(BOLD_SERIES);
2266 toggleAndShow(cur, this, font);
2270 case LFUN_FONT_NOUN: {
2271 Font font(ignore_font, ignore_language);
2272 font.fontInfo().setNoun(FONT_TOGGLE);
2273 toggleAndShow(cur, this, font);
2277 case LFUN_FONT_TYPEWRITER: {
2278 Font font(ignore_font, ignore_language);
2279 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2280 toggleAndShow(cur, this, font);
2284 case LFUN_FONT_SANS: {
2285 Font font(ignore_font, ignore_language);
2286 font.fontInfo().setFamily(SANS_FAMILY);
2287 toggleAndShow(cur, this, font);
2291 case LFUN_FONT_ROMAN: {
2292 Font font(ignore_font, ignore_language);
2293 font.fontInfo().setFamily(ROMAN_FAMILY);
2294 toggleAndShow(cur, this, font);
2298 case LFUN_FONT_DEFAULT: {
2299 Font font(inherit_font, ignore_language);
2300 toggleAndShow(cur, this, font);
2304 case LFUN_FONT_STRIKEOUT: {
2305 Font font(ignore_font, ignore_language);
2306 font.fontInfo().setStrikeout(FONT_TOGGLE);
2307 toggleAndShow(cur, this, font);
2311 case LFUN_FONT_CROSSOUT: {
2312 Font font(ignore_font, ignore_language);
2313 font.fontInfo().setXout(FONT_TOGGLE);
2314 toggleAndShow(cur, this, font);
2318 case LFUN_FONT_UNDERUNDERLINE: {
2319 Font font(ignore_font, ignore_language);
2320 font.fontInfo().setUuline(FONT_TOGGLE);
2321 toggleAndShow(cur, this, font);
2325 case LFUN_FONT_UNDERWAVE: {
2326 Font font(ignore_font, ignore_language);
2327 font.fontInfo().setUwave(FONT_TOGGLE);
2328 toggleAndShow(cur, this, font);
2332 case LFUN_FONT_UNDERLINE: {
2333 Font font(ignore_font, ignore_language);
2334 font.fontInfo().setUnderbar(FONT_TOGGLE);
2335 toggleAndShow(cur, this, font);
2339 case LFUN_FONT_SIZE: {
2340 Font font(ignore_font, ignore_language);
2341 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2342 toggleAndShow(cur, this, font);
2346 case LFUN_LANGUAGE: {
2347 string const lang_arg = cmd.getArg(0);
2348 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2349 Language const * lang =
2350 reset ? reset_language
2351 : languages.getLanguage(lang_arg);
2352 // we allow reset_language, which is 0, but only if it
2353 // was requested via empty or "reset" arg.
2354 if (!lang && !reset)
2356 bool const toggle = (cmd.getArg(1) != "set");
2357 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2358 Font font(ignore_font, lang);
2359 toggleAndShow(cur, this, font, toggle);
2363 case LFUN_TEXTSTYLE_APPLY:
2364 toggleAndShow(cur, this, freefont, toggleall);
2365 cur.message(_("Character set"));
2368 // Set the freefont using the contents of \param data dispatched from
2369 // the frontends and apply it at the current cursor location.
2370 case LFUN_TEXTSTYLE_UPDATE: {
2373 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2376 toggleAndShow(cur, this, freefont, toggleall);
2377 cur.message(_("Character set"));
2379 lyxerr << "Argument not ok";
2384 case LFUN_FINISHED_LEFT:
2385 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2386 // We're leaving an inset, going left. If the inset is LTR, we're
2387 // leaving from the front, so we should not move (remain at --- but
2388 // not in --- the inset). If the inset is RTL, move left, without
2389 // entering the inset itself; i.e., move to after the inset.
2390 if (cur.paragraph().getFontSettings(
2391 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2392 cursorVisLeft(cur, true);
2395 case LFUN_FINISHED_RIGHT:
2396 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2397 // We're leaving an inset, going right. If the inset is RTL, we're
2398 // leaving from the front, so we should not move (remain at --- but
2399 // not in --- the inset). If the inset is LTR, move right, without
2400 // entering the inset itself; i.e., move to after the inset.
2401 if (!cur.paragraph().getFontSettings(
2402 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2403 cursorVisRight(cur, true);
2406 case LFUN_FINISHED_BACKWARD:
2407 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2408 cur.setCurrentFont();
2411 case LFUN_FINISHED_FORWARD:
2412 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2414 cur.setCurrentFont();
2417 case LFUN_LAYOUT_PARAGRAPH: {
2419 params2string(cur.paragraph(), data);
2420 data = "show\n" + data;
2421 bv->showDialog("paragraph", data);
2425 case LFUN_PARAGRAPH_UPDATE: {
2427 params2string(cur.paragraph(), data);
2429 // Will the paragraph accept changes from the dialog?
2431 cur.inset().allowParagraphCustomization(cur.idx());
2433 data = "update " + convert<string>(accept) + '\n' + data;
2434 bv->updateDialog("paragraph", data);
2438 case LFUN_ACCENT_UMLAUT:
2439 case LFUN_ACCENT_CIRCUMFLEX:
2440 case LFUN_ACCENT_GRAVE:
2441 case LFUN_ACCENT_ACUTE:
2442 case LFUN_ACCENT_TILDE:
2443 case LFUN_ACCENT_PERISPOMENI:
2444 case LFUN_ACCENT_CEDILLA:
2445 case LFUN_ACCENT_MACRON:
2446 case LFUN_ACCENT_DOT:
2447 case LFUN_ACCENT_UNDERDOT:
2448 case LFUN_ACCENT_UNDERBAR:
2449 case LFUN_ACCENT_CARON:
2450 case LFUN_ACCENT_BREVE:
2451 case LFUN_ACCENT_TIE:
2452 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2453 case LFUN_ACCENT_CIRCLE:
2454 case LFUN_ACCENT_OGONEK:
2455 theApp()->handleKeyFunc(cmd.action());
2456 if (!cmd.argument().empty())
2457 // FIXME: Are all these characters encoded in one byte in utf8?
2458 bv->translateAndInsert(cmd.argument()[0], this, cur);
2459 cur.screenUpdateFlags(Update::FitCursor);
2462 case LFUN_FLOAT_LIST_INSERT: {
2463 DocumentClass const & tclass = bv->buffer().params().documentClass();
2464 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2466 if (cur.selection())
2467 cutSelection(cur, false);
2468 breakParagraph(cur);
2470 if (cur.lastpos() != 0) {
2471 cursorBackward(cur);
2472 breakParagraph(cur);
2475 docstring const laystr = cur.inset().usePlainLayout() ?
2476 tclass.plainLayoutName() :
2477 tclass.defaultLayoutName();
2478 setLayout(cur, laystr);
2479 ParagraphParameters p;
2480 // FIXME If this call were replaced with one to clearParagraphParams(),
2481 // then we could get rid of this method altogether.
2482 setParagraphs(cur, p);
2483 // FIXME This should be simplified when InsetFloatList takes a
2484 // Buffer in its constructor.
2485 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2486 ifl->setBuffer(bv->buffer());
2487 insertInset(cur, ifl);
2490 lyxerr << "Non-existent float type: "
2491 << to_utf8(cmd.argument()) << endl;
2496 case LFUN_CHANGE_ACCEPT: {
2497 acceptOrRejectChanges(cur, ACCEPT);
2501 case LFUN_CHANGE_REJECT: {
2502 acceptOrRejectChanges(cur, REJECT);
2506 case LFUN_THESAURUS_ENTRY: {
2507 Language const * language = cur.getFont().language();
2508 docstring arg = cmd.argument();
2510 arg = cur.selectionAsString(false);
2512 if (arg.size() > 100 || arg.empty()) {
2513 // Get word or selection
2514 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2515 arg = cur.selectionAsString(false);
2516 arg += " lang=" + from_ascii(language->lang());
2519 string lang = cmd.getArg(1);
2520 // This duplicates the code in GuiThesaurus::initialiseParams
2521 if (prefixIs(lang, "lang=")) {
2522 language = languages.getLanguage(lang.substr(5));
2524 language = cur.getFont().language();
2527 string lang = language->code();
2528 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2529 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2530 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2531 _("The path to the thesaurus directory has not been specified.\n"
2532 "The thesaurus is not functional.\n"
2533 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2536 bv->showDialog("thesaurus", to_utf8(arg));
2540 case LFUN_SPELLING_ADD: {
2541 Language const * language = getLanguage(cur, cmd.getArg(1));
2542 docstring word = from_utf8(cmd.getArg(0));
2544 word = cur.selectionAsString(false);
2546 if (word.size() > 100 || word.empty()) {
2547 // Get word or selection
2548 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2549 word = cur.selectionAsString(false);
2552 WordLangTuple wl(word, language);
2553 theSpellChecker()->insert(wl);
2557 case LFUN_SPELLING_IGNORE: {
2558 Language const * language = getLanguage(cur, cmd.getArg(1));
2559 docstring word = from_utf8(cmd.getArg(0));
2561 word = cur.selectionAsString(false);
2563 if (word.size() > 100 || word.empty()) {
2564 // Get word or selection
2565 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2566 word = cur.selectionAsString(false);
2569 WordLangTuple wl(word, language);
2570 theSpellChecker()->accept(wl);
2574 case LFUN_SPELLING_REMOVE: {
2575 Language const * language = getLanguage(cur, cmd.getArg(1));
2576 docstring word = from_utf8(cmd.getArg(0));
2578 word = cur.selectionAsString(false);
2580 if (word.size() > 100 || word.empty()) {
2581 // Get word or selection
2582 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2583 word = cur.selectionAsString(false);
2586 WordLangTuple wl(word, language);
2587 theSpellChecker()->remove(wl);
2591 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2592 // Given data, an encoding of the ParagraphParameters
2593 // generated in the Paragraph dialog, this function sets
2594 // the current paragraph, or currently selected paragraphs,
2596 // NOTE: This function overrides all existing settings.
2597 setParagraphs(cur, cmd.argument());
2598 cur.message(_("Paragraph layout set"));
2602 case LFUN_PARAGRAPH_PARAMS: {
2603 // Given data, an encoding of the ParagraphParameters as we'd
2604 // find them in a LyX file, this function modifies the current paragraph,
2605 // or currently selected paragraphs.
2606 // NOTE: This function only modifies, and does not override, existing
2608 setParagraphs(cur, cmd.argument(), true);
2609 cur.message(_("Paragraph layout set"));
2614 if (cur.selection()) {
2615 cur.selection(false);
2618 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2619 // correct, but I'm not 100% sure -- dov, 071019
2620 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2624 case LFUN_OUTLINE_UP:
2625 outline(OutlineUp, cur, this);
2626 setCursor(cur, cur.pit(), 0);
2627 cur.forceBufferUpdate();
2631 case LFUN_OUTLINE_DOWN:
2632 outline(OutlineDown, cur, this);
2633 setCursor(cur, cur.pit(), 0);
2634 cur.forceBufferUpdate();
2638 case LFUN_OUTLINE_IN:
2639 outline(OutlineIn, cur, this);
2640 cur.forceBufferUpdate();
2644 case LFUN_OUTLINE_OUT:
2645 outline(OutlineOut, cur, this);
2646 cur.forceBufferUpdate();
2650 case LFUN_SERVER_GET_STATISTICS:
2652 DocIterator from, to;
2653 if (cur.selection()) {
2654 from = cur.selectionBegin();
2655 to = cur.selectionEnd();
2657 from = doc_iterator_begin(cur.buffer());
2658 to = doc_iterator_end(cur.buffer());
2661 cur.buffer()->updateStatistics(from, to);
2662 string const arg0 = cmd.getArg(0);
2663 if (arg0 == "words") {
2664 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2665 } else if (arg0 == "chars") {
2666 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2667 } else if (arg0 == "chars-space") {
2668 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2670 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2671 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2672 + convert<docstring>(cur.buffer()->charCount(true)));
2678 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2683 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2685 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2686 // Check for misspelled text
2687 // The redraw is useful because of the painting of
2688 // misspelled markers depends on the cursor position.
2689 // Trigger a redraw for cursor moves inside misspelled text.
2690 if (!cur.inTexted()) {
2691 // move from regular text to math
2692 needsUpdate = last_misspelled;
2693 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2694 // move inside regular text
2695 needsUpdate = last_misspelled
2696 || cur.paragraph().isMisspelled(cur.pos(), true);
2700 // FIXME: The cursor flag is reset two lines below
2701 // so we need to check here if some of the LFUN did touch that.
2702 // for now only Text::erase() and Text::backspace() do that.
2703 // The plan is to verify all the LFUNs and then to remove this
2704 // singleParUpdate boolean altogether.
2705 if (cur.result().screenUpdate() & Update::Force) {
2706 singleParUpdate = false;
2710 // FIXME: the following code should go in favor of fine grained
2711 // update flag treatment.
2712 if (singleParUpdate) {
2713 // Inserting characters does not change par height in general. So, try
2714 // to update _only_ this paragraph. BufferView will detect if a full
2715 // metrics update is needed anyway.
2716 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2720 && &oldTopSlice.inset() == &cur.inset()
2721 && oldTopSlice.idx() == cur.idx()
2722 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2723 && !cur.selection())
2724 // FIXME: it would be better if we could just do this
2726 //if (cur.result().update() != Update::FitCursor)
2727 // cur.noScreenUpdate();
2729 // But some LFUNs do not set Update::FitCursor when needed, so we
2730 // do it for all. This is not very harmfull as FitCursor will provoke
2731 // a full redraw only if needed but still, a proper review of all LFUN
2732 // should be done and this needsUpdate boolean can then be removed.
2733 cur.screenUpdateFlags(Update::FitCursor);
2735 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2739 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2740 FuncStatus & flag) const
2742 LBUFERR(this == cur.text());
2744 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2746 bool allow_in_passthru = false;
2747 InsetCode code = NO_CODE;
2749 switch (cmd.action()) {
2751 case LFUN_DEPTH_DECREMENT:
2752 enable = changeDepthAllowed(cur, DEC_DEPTH);
2755 case LFUN_DEPTH_INCREMENT:
2756 enable = changeDepthAllowed(cur, INC_DEPTH);
2760 // FIXME We really should not allow this to be put, e.g.,
2761 // in a footnote, or in ERT. But it would make sense in a
2762 // branch, so I'm not sure what to do.
2763 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2766 case LFUN_DIALOG_SHOW_NEW_INSET:
2767 if (cmd.argument() == "bibitem")
2768 code = BIBITEM_CODE;
2769 else if (cmd.argument() == "bibtex") {
2771 // not allowed in description items
2772 enable = !inDescriptionItem(cur);
2774 else if (cmd.argument() == "box")
2776 else if (cmd.argument() == "branch")
2778 else if (cmd.argument() == "citation")
2780 else if (cmd.argument() == "ert")
2782 else if (cmd.argument() == "external")
2783 code = EXTERNAL_CODE;
2784 else if (cmd.argument() == "float")
2786 else if (cmd.argument() == "graphics")
2787 code = GRAPHICS_CODE;
2788 else if (cmd.argument() == "href")
2789 code = HYPERLINK_CODE;
2790 else if (cmd.argument() == "include")
2791 code = INCLUDE_CODE;
2792 else if (cmd.argument() == "index")
2794 else if (cmd.argument() == "index_print")
2795 code = INDEX_PRINT_CODE;
2796 else if (cmd.argument() == "listings")
2797 code = LISTINGS_CODE;
2798 else if (cmd.argument() == "mathspace")
2799 code = MATH_HULL_CODE;
2800 else if (cmd.argument() == "nomenclature")
2801 code = NOMENCL_CODE;
2802 else if (cmd.argument() == "nomencl_print")
2803 code = NOMENCL_PRINT_CODE;
2804 else if (cmd.argument() == "label")
2806 else if (cmd.argument() == "line")
2808 else if (cmd.argument() == "note")
2810 else if (cmd.argument() == "phantom")
2811 code = PHANTOM_CODE;
2812 else if (cmd.argument() == "ref")
2814 else if (cmd.argument() == "space")
2816 else if (cmd.argument() == "toc")
2818 else if (cmd.argument() == "vspace")
2820 else if (cmd.argument() == "wrap")
2824 case LFUN_ERT_INSERT:
2827 case LFUN_LISTING_INSERT:
2828 code = LISTINGS_CODE;
2829 // not allowed in description items
2830 enable = !inDescriptionItem(cur);
2832 case LFUN_FOOTNOTE_INSERT:
2835 case LFUN_TABULAR_INSERT:
2836 code = TABULAR_CODE;
2838 case LFUN_MARGINALNOTE_INSERT:
2841 case LFUN_FLOAT_INSERT:
2842 case LFUN_FLOAT_WIDE_INSERT:
2843 // FIXME: If there is a selection, we should check whether there
2844 // are floats in the selection, but this has performance issues, see
2845 // LFUN_CHANGE_ACCEPT/REJECT.
2847 if (inDescriptionItem(cur))
2848 // not allowed in description items
2851 InsetCode const inset_code = cur.inset().lyxCode();
2853 // algorithm floats cannot be put in another float
2854 if (to_utf8(cmd.argument()) == "algorithm") {
2855 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2859 // for figures and tables: only allow in another
2860 // float or wrap if it is of the same type and
2861 // not a subfloat already
2862 if(cur.inset().lyxCode() == code) {
2863 InsetFloat const & ins =
2864 static_cast<InsetFloat const &>(cur.inset());
2865 enable = ins.params().type == to_utf8(cmd.argument())
2866 && !ins.params().subfloat;
2867 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2868 InsetWrap const & ins =
2869 static_cast<InsetWrap const &>(cur.inset());
2870 enable = ins.params().type == to_utf8(cmd.argument());
2874 case LFUN_WRAP_INSERT:
2876 // not allowed in description items
2877 enable = !inDescriptionItem(cur);
2879 case LFUN_FLOAT_LIST_INSERT: {
2880 code = FLOAT_LIST_CODE;
2881 // not allowed in description items
2882 enable = !inDescriptionItem(cur);
2884 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2885 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2886 // make sure we know about such floats
2887 if (cit == floats.end() ||
2888 // and that we know how to generate a list of them
2889 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2890 flag.setUnknown(true);
2891 // probably not necessary, but...
2897 case LFUN_CAPTION_INSERT: {
2898 code = CAPTION_CODE;
2899 string arg = cmd.getArg(0);
2900 bool varia = arg != "Unnumbered"
2901 && cur.inset().allowsCaptionVariation(arg);
2902 // not allowed in description items,
2903 // and in specific insets
2904 enable = !inDescriptionItem(cur)
2905 && (varia || arg.empty() || arg == "Standard");
2908 case LFUN_NOTE_INSERT:
2910 // in commands (sections etc.) and description items,
2911 // only Notes are allowed
2912 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2913 (!cur.paragraph().layout().isCommand()
2914 && !inDescriptionItem(cur)));
2916 case LFUN_FLEX_INSERT: {
2918 string s = cmd.getArg(0);
2920 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2921 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2922 il.lyxtype() != InsetLayout::CUSTOM &&
2923 il.lyxtype() != InsetLayout::ELEMENT &&
2924 il.lyxtype ()!= InsetLayout::STANDARD)
2928 case LFUN_BOX_INSERT:
2931 case LFUN_BRANCH_INSERT:
2933 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2934 && cur.buffer()->params().branchlist().empty())
2937 case LFUN_IPA_INSERT:
2940 case LFUN_PHANTOM_INSERT:
2941 code = PHANTOM_CODE;
2943 case LFUN_LABEL_INSERT:
2946 case LFUN_INFO_INSERT:
2949 case LFUN_ARGUMENT_INSERT: {
2951 allow_in_passthru = true;
2952 string const arg = cmd.getArg(0);
2957 Layout const & lay = cur.paragraph().layout();
2958 Layout::LaTeXArgMap args = lay.args();
2959 Layout::LaTeXArgMap::const_iterator const lait =
2961 if (lait != args.end()) {
2963 pit_type pit = cur.pit();
2964 pit_type lastpit = cur.pit();
2965 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2966 // In a sequence of "merged" environment layouts, we only allow
2967 // non-item arguments once.
2968 lastpit = cur.lastpit();
2969 // get the first paragraph in sequence with this layout
2970 depth_type const current_depth = cur.paragraph().params().depth();
2974 Paragraph cpar = pars_[pit - 1];
2975 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2981 for (; pit <= lastpit; ++pit) {
2982 if (pars_[pit].layout() != lay)
2984 for (auto const & table : pars_[pit].insetList())
2985 if (InsetArgument const * ins = table.inset->asInsetArgument())
2986 if (ins->name() == arg) {
2987 // we have this already
2996 case LFUN_INDEX_INSERT:
2999 case LFUN_INDEX_PRINT:
3000 code = INDEX_PRINT_CODE;
3001 // not allowed in description items
3002 enable = !inDescriptionItem(cur);
3004 case LFUN_NOMENCL_INSERT:
3005 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3009 code = NOMENCL_CODE;
3011 case LFUN_NOMENCL_PRINT:
3012 code = NOMENCL_PRINT_CODE;
3013 // not allowed in description items
3014 enable = !inDescriptionItem(cur);
3016 case LFUN_HREF_INSERT:
3017 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3021 code = HYPERLINK_CODE;
3023 case LFUN_IPAMACRO_INSERT: {
3024 string const arg = cmd.getArg(0);
3026 code = IPADECO_CODE;
3028 code = IPACHAR_CODE;
3031 case LFUN_QUOTE_INSERT:
3032 // always allow this, since we will inset a raw quote
3033 // if an inset is not allowed.
3034 allow_in_passthru = true;
3036 case LFUN_SPECIALCHAR_INSERT:
3037 code = SPECIALCHAR_CODE;
3039 case LFUN_SPACE_INSERT:
3040 // slight hack: we know this is allowed in math mode
3044 case LFUN_PREVIEW_INSERT:
3045 code = PREVIEW_CODE;
3047 case LFUN_SCRIPT_INSERT:
3051 case LFUN_MATH_INSERT:
3052 case LFUN_MATH_AMS_MATRIX:
3053 case LFUN_MATH_MATRIX:
3054 case LFUN_MATH_DELIM:
3055 case LFUN_MATH_BIGDELIM:
3056 case LFUN_MATH_DISPLAY:
3057 case LFUN_MATH_MODE:
3058 case LFUN_MATH_MACRO:
3059 case LFUN_MATH_SUBSCRIPT:
3060 case LFUN_MATH_SUPERSCRIPT:
3061 code = MATH_HULL_CODE;
3064 case LFUN_REGEXP_MODE:
3065 code = MATH_HULL_CODE;
3066 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3069 case LFUN_INSET_MODIFY:
3070 // We need to disable this, because we may get called for a
3072 // InsetTabular::getStatus() -> InsetText::getStatus()
3073 // and we don't handle LFUN_INSET_MODIFY.
3077 case LFUN_FONT_EMPH:
3078 flag.setOnOff(fontinfo.emph() == FONT_ON);
3079 enable = !cur.paragraph().isPassThru();
3082 case LFUN_FONT_ITAL:
3083 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3084 enable = !cur.paragraph().isPassThru();
3087 case LFUN_FONT_NOUN:
3088 flag.setOnOff(fontinfo.noun() == FONT_ON);
3089 enable = !cur.paragraph().isPassThru();
3092 case LFUN_FONT_BOLD:
3093 case LFUN_FONT_BOLDSYMBOL:
3094 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3095 enable = !cur.paragraph().isPassThru();
3098 case LFUN_FONT_SANS:
3099 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3100 enable = !cur.paragraph().isPassThru();
3103 case LFUN_FONT_ROMAN:
3104 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3105 enable = !cur.paragraph().isPassThru();
3108 case LFUN_FONT_TYPEWRITER:
3109 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3110 enable = !cur.paragraph().isPassThru();
3115 enable = cur.selection();
3119 if (cmd.argument().empty()) {
3120 if (theClipboard().isInternal())
3121 enable = cap::numberOfSelections() > 0;
3123 enable = !theClipboard().empty();
3127 // we have an argument
3128 string const arg = to_utf8(cmd.argument());
3129 if (isStrUnsignedInt(arg)) {
3130 // it's a number and therefore means the internal stack
3131 unsigned int n = convert<unsigned int>(arg);
3132 enable = cap::numberOfSelections() > n;
3136 // explicit text type?
3137 if (arg == "html") {
3138 // Do not enable for PlainTextType, since some tidying in the
3139 // frontend is needed for HTML, which is too unsafe for plain text.
3140 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3142 } else if (arg == "latex") {
3143 // LaTeX is usually not available on the clipboard with
3144 // the correct MIME type, but in plain text.
3145 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3146 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3150 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3152 type = Clipboard::PdfGraphicsType;
3153 else if (arg == "png")
3154 type = Clipboard::PngGraphicsType;
3155 else if (arg == "jpeg")
3156 type = Clipboard::JpegGraphicsType;
3157 else if (arg == "linkback")
3158 type = Clipboard::LinkBackGraphicsType;
3159 else if (arg == "emf")
3160 type = Clipboard::EmfGraphicsType;
3161 else if (arg == "wmf")
3162 type = Clipboard::WmfGraphicsType;
3165 LYXERR0("Unrecognized graphics type: " << arg);
3166 // we don't want to assert if the user just mistyped the LFUN
3167 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3171 enable = theClipboard().hasGraphicsContents(type);
3175 case LFUN_CLIPBOARD_PASTE:
3176 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3177 enable = !theClipboard().empty();
3180 case LFUN_PRIMARY_SELECTION_PASTE:
3181 enable = cur.selection() || !theSelection().empty();
3184 case LFUN_SELECTION_PASTE:
3185 enable = cap::selection();
3188 case LFUN_PARAGRAPH_MOVE_UP:
3189 enable = cur.pit() > 0 && !cur.selection();
3192 case LFUN_PARAGRAPH_MOVE_DOWN:
3193 enable = cur.pit() < cur.lastpit() && !cur.selection();
3196 case LFUN_CHANGE_ACCEPT:
3197 case LFUN_CHANGE_REJECT:
3198 // In principle, these LFUNs should only be enabled if there
3199 // is a change at the current position/in the current selection.
3200 // However, without proper optimizations, this will inevitably
3201 // result in unacceptable performance - just imagine a user who
3202 // wants to select the complete content of a long document.
3203 if (!cur.selection())
3204 enable = cur.paragraph().isChanged(cur.pos());
3206 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3211 case LFUN_OUTLINE_UP:
3212 case LFUN_OUTLINE_DOWN:
3213 case LFUN_OUTLINE_IN:
3214 case LFUN_OUTLINE_OUT:
3215 // FIXME: LyX is not ready for outlining within inset.
3216 enable = isMainText()
3217 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3220 case LFUN_NEWLINE_INSERT:
3221 // LaTeX restrictions (labels or empty par)
3222 enable = !cur.paragraph().isPassThru()
3223 && cur.pos() > cur.paragraph().beginOfBody();
3226 case LFUN_SEPARATOR_INSERT:
3227 // Always enabled for now
3231 case LFUN_TAB_INSERT:
3232 case LFUN_TAB_DELETE:
3233 enable = cur.paragraph().isPassThru();
3236 case LFUN_SET_GRAPHICS_GROUP: {
3237 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3241 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3245 case LFUN_NEWPAGE_INSERT:
3246 // not allowed in description items
3247 code = NEWPAGE_CODE;
3248 enable = !inDescriptionItem(cur);
3251 case LFUN_DATE_INSERT: {
3252 string const format = cmd.argument().empty()
3253 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3254 enable = support::os::is_valid_strftime(format);
3259 enable = !cur.paragraph().isPassThru();
3260 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3263 case LFUN_PARAGRAPH_BREAK:
3264 enable = inset().allowMultiPar();
3267 case LFUN_SPELLING_ADD:
3268 case LFUN_SPELLING_IGNORE:
3269 case LFUN_SPELLING_REMOVE:
3270 enable = theSpellChecker() != NULL;
3271 if (enable && !cmd.getArg(1).empty()) {
3272 // validate explicitly given language
3273 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3274 enable &= lang != NULL;
3279 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3280 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3281 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3283 layout = tclass.defaultLayoutName();
3284 enable = !owner_->forcePlainLayout() && tclass.hasLayout(layout);
3286 flag.setOnOff(layout == cur.paragraph().layout().name());
3290 case LFUN_ENVIRONMENT_SPLIT: {
3291 if (cmd.argument() == "outer") {
3292 // check if we have an environment in our nesting hierarchy
3294 depth_type const current_depth = cur.paragraph().params().depth();
3295 pit_type pit = cur.pit();
3296 Paragraph cpar = pars_[pit];
3298 if (pit == 0 || cpar.params().depth() == 0)
3302 if (cpar.params().depth() < current_depth)
3303 res = cpar.layout().isEnvironment();
3308 else if (cmd.argument() == "previous") {
3309 // look if we have an environment in the previous par
3310 pit_type pit = cur.pit();
3311 Paragraph cpar = pars_[pit];
3315 enable = cpar.layout().isEnvironment();
3321 else if (cur.paragraph().layout().isEnvironment()) {
3322 enable = cmd.argument() == "before"
3323 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3330 case LFUN_LAYOUT_PARAGRAPH:
3331 case LFUN_PARAGRAPH_PARAMS:
3332 case LFUN_PARAGRAPH_PARAMS_APPLY:
3333 case LFUN_PARAGRAPH_UPDATE:
3334 enable = owner_->allowParagraphCustomization();
3337 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3338 // Because they insert COMBINING DIACRITICAL Unicode characters,
3339 // that cannot be handled by LaTeX but must be converted according
3340 // to the definition in lib/unicodesymbols?
3341 case LFUN_ACCENT_ACUTE:
3342 case LFUN_ACCENT_BREVE:
3343 case LFUN_ACCENT_CARON:
3344 case LFUN_ACCENT_CEDILLA:
3345 case LFUN_ACCENT_CIRCLE:
3346 case LFUN_ACCENT_CIRCUMFLEX:
3347 case LFUN_ACCENT_DOT:
3348 case LFUN_ACCENT_GRAVE:
3349 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3350 case LFUN_ACCENT_MACRON:
3351 case LFUN_ACCENT_OGONEK:
3352 case LFUN_ACCENT_TIE:
3353 case LFUN_ACCENT_TILDE:
3354 case LFUN_ACCENT_PERISPOMENI:
3355 case LFUN_ACCENT_UMLAUT:
3356 case LFUN_ACCENT_UNDERBAR:
3357 case LFUN_ACCENT_UNDERDOT:
3358 case LFUN_FONT_DEFAULT:
3359 case LFUN_FONT_FRAK:
3360 case LFUN_FONT_SIZE:
3361 case LFUN_FONT_STATE:
3362 case LFUN_FONT_UNDERLINE:
3363 case LFUN_FONT_STRIKEOUT:
3364 case LFUN_FONT_CROSSOUT:
3365 case LFUN_FONT_UNDERUNDERLINE:
3366 case LFUN_FONT_UNDERWAVE:
3367 case LFUN_TEXTSTYLE_APPLY:
3368 case LFUN_TEXTSTYLE_UPDATE:
3369 enable = !cur.paragraph().isPassThru();
3372 case LFUN_WORD_DELETE_FORWARD:
3373 case LFUN_WORD_DELETE_BACKWARD:
3374 case LFUN_LINE_DELETE_FORWARD:
3375 case LFUN_WORD_FORWARD:
3376 case LFUN_WORD_BACKWARD:
3377 case LFUN_WORD_RIGHT:
3378 case LFUN_WORD_LEFT:
3379 case LFUN_CHAR_FORWARD:
3380 case LFUN_CHAR_FORWARD_SELECT:
3381 case LFUN_CHAR_BACKWARD:
3382 case LFUN_CHAR_BACKWARD_SELECT:
3383 case LFUN_CHAR_LEFT:
3384 case LFUN_CHAR_LEFT_SELECT:
3385 case LFUN_CHAR_RIGHT:
3386 case LFUN_CHAR_RIGHT_SELECT:
3388 case LFUN_UP_SELECT:
3390 case LFUN_DOWN_SELECT:
3391 case LFUN_PARAGRAPH_UP_SELECT:
3392 case LFUN_PARAGRAPH_DOWN_SELECT:
3393 case LFUN_LINE_BEGIN_SELECT:
3394 case LFUN_LINE_END_SELECT:
3395 case LFUN_WORD_FORWARD_SELECT:
3396 case LFUN_WORD_BACKWARD_SELECT:
3397 case LFUN_WORD_RIGHT_SELECT:
3398 case LFUN_WORD_LEFT_SELECT:
3399 case LFUN_WORD_SELECT:
3400 case LFUN_SECTION_SELECT:
3401 case LFUN_BUFFER_BEGIN:
3402 case LFUN_BUFFER_END:
3403 case LFUN_BUFFER_BEGIN_SELECT:
3404 case LFUN_BUFFER_END_SELECT:
3405 case LFUN_INSET_BEGIN:
3406 case LFUN_INSET_END:
3407 case LFUN_INSET_BEGIN_SELECT:
3408 case LFUN_INSET_END_SELECT:
3409 case LFUN_PARAGRAPH_UP:
3410 case LFUN_PARAGRAPH_DOWN:
3411 case LFUN_LINE_BEGIN:
3413 case LFUN_CHAR_DELETE_FORWARD:
3414 case LFUN_CHAR_DELETE_BACKWARD:
3415 case LFUN_WORD_UPCASE:
3416 case LFUN_WORD_LOWCASE:
3417 case LFUN_WORD_CAPITALIZE:
3418 case LFUN_CHARS_TRANSPOSE:
3419 case LFUN_SERVER_GET_XY:
3420 case LFUN_SERVER_SET_XY:
3421 case LFUN_SERVER_GET_LAYOUT:
3422 case LFUN_SELF_INSERT:
3423 case LFUN_UNICODE_INSERT:
3424 case LFUN_THESAURUS_ENTRY:
3426 case LFUN_SERVER_GET_STATISTICS:
3427 // these are handled in our dispatch()
3431 case LFUN_INSET_INSERT: {
3432 string const type = cmd.getArg(0);
3433 if (type == "toc") {
3435 // not allowed in description items
3436 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3437 enable = !inDescriptionItem(cur);
3450 || !cur.inset().insetAllowed(code)
3451 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3454 flag.setEnabled(enable);
3459 void Text::pasteString(Cursor & cur, docstring const & clip,
3462 if (!clip.empty()) {
3465 insertStringAsParagraphs(cur, clip, cur.current_font);
3467 insertStringAsLines(cur, clip, cur.current_font);
3472 // FIXME: an item inset would make things much easier.
3473 bool Text::inDescriptionItem(Cursor & cur) const
3475 Paragraph & par = cur.paragraph();
3476 pos_type const pos = cur.pos();
3477 pos_type const body_pos = par.beginOfBody();
3479 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3480 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3481 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3484 return (pos < body_pos
3486 && (pos == 0 || par.getChar(pos - 1) != ' ')));