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.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
35 #include "InsetList.h"
39 #include "LyXAction.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "SpellChecker.h"
46 #include "TextClass.h"
47 #include "TextMetrics.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/InsetCollapsable.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/MathMacroTemplate.h"
87 using namespace lyx::support;
91 using cap::copySelection;
92 using cap::cutSelection;
93 using cap::pasteFromStack;
94 using cap::pasteClipboardText;
95 using cap::pasteClipboardGraphics;
96 using cap::replaceSelection;
97 using cap::grabAndEraseSelection;
98 using cap::selClearOrDel;
99 using cap::pasteSimpleText;
102 static Font freefont(ignore_font, ignore_language);
103 static bool toggleall = false;
105 static void toggleAndShow(Cursor & cur, Text * text,
106 Font const & font, bool toggleall = true)
108 text->toggleFree(cur, font, toggleall);
110 if (font.language() != ignore_language ||
111 font.fontInfo().number() != FONT_IGNORE) {
112 TextMetrics const & tm = cur.bv().textMetrics(text);
113 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
114 cur.real_current_font))
115 text->setCursor(cur, cur.pit(), cur.pos(),
116 false, !cur.boundary());
121 static void moveCursor(Cursor & cur, bool selecting)
123 if (selecting || cur.mark())
128 static void finishChange(Cursor & cur, bool selecting)
131 moveCursor(cur, selecting);
135 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
138 docstring sel = cur.selectionAsString(false);
140 // It may happen that sel is empty but there is a selection
141 replaceSelection(cur);
143 // Is this a valid formula?
147 #ifdef ENABLE_ASSERTIONS
148 const int old_pos = cur.pos();
150 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
151 #ifdef ENABLE_ASSERTIONS
152 LATTEST(old_pos == cur.pos());
154 cur.nextInset()->edit(cur, true);
155 // don't do that also for LFUN_MATH_MODE
156 // unless you want end up with always changing
157 // to mathrm when opening an inlined inset --
158 // I really hate "LyXfunc overloading"...
160 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
161 // Avoid an unnecessary undo step if cmd.argument
163 if (!cmd.argument().empty())
164 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
167 // create a macro if we see "\\newcommand"
168 // somewhere, and an ordinary formula
170 if (sel.find(from_ascii("\\newcommand")) == string::npos
171 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
172 && sel.find(from_ascii("\\def")) == string::npos)
174 InsetMathHull * formula = new InsetMathHull(cur.buffer());
175 string const selstr = to_utf8(sel);
176 istringstream is(selstr);
179 if (!formula->readQuiet(lex)) {
180 // No valid formula, let's try with delims
181 is.str("$" + selstr + "$");
183 if (!formula->readQuiet(lex)) {
184 // Still not valid, leave it as is
193 cur.insert(new MathMacroTemplate(cur.buffer(), sel));
197 cur.message(from_utf8(N_("Math editor mode")));
199 cur.message(from_utf8(N_("No valid math formula")));
203 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
205 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
206 if (cur.inRegexped()) {
207 cur.message(_("Already in regular expression mode"));
211 docstring sel = cur.selectionAsString(false);
213 // It may happen that sel is empty but there is a selection
214 replaceSelection(cur);
216 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
217 cur.nextInset()->edit(cur, true);
220 cur.message(_("Regexp editor mode"));
224 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
227 cap::replaceSelection(cur);
228 cur.insert(new InsetSpecialChar(kind));
233 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
236 cap::replaceSelection(cur);
237 cur.insert(new InsetIPAChar(kind));
242 static bool doInsertInset(Cursor & cur, Text * text,
243 FuncRequest const & cmd, bool edit, bool pastesel)
245 Buffer & buffer = cur.bv().buffer();
246 BufferParams const & bparams = buffer.params();
247 Inset * inset = createInset(&buffer, cmd);
251 if (InsetCollapsable * ci = inset->asInsetCollapsable())
252 ci->setButtonLabel();
255 if (cmd.action() == LFUN_INDEX_INSERT) {
256 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
257 text->insertInset(cur, inset);
259 inset->edit(cur, true);
260 // Now put this into inset
261 Font const f(inherit_font, cur.current_font.language());
263 cur.text()->insertStringAsLines(cur, ds, f);
264 cur.leaveInset(*inset);
268 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
269 bool cotextinsert = false;
270 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
271 Layout const & lay = cur.paragraph().layout();
272 Layout::LaTeXArgMap args = lay.args();
273 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
274 if (lait != args.end())
275 cotextinsert = (*lait).second.insertcotext;
277 InsetLayout const & il = cur.inset().getLayout();
279 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
280 if (ilait != args.end())
281 cotextinsert = (*ilait).second.insertcotext;
283 // The argument requests to insert a copy of the co-text to the inset
286 // If we have a selection within a paragraph, use this
287 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
288 ds = cur.selectionAsString(false);
289 // else use the whole paragraph
291 ds = cur.paragraph().asString();
292 text->insertInset(cur, inset);
294 inset->edit(cur, true);
295 // Now put co-text into inset
296 Font const f(inherit_font, cur.current_font.language());
298 cur.text()->insertStringAsLines(cur, ds, f);
299 cur.leaveInset(*inset);
306 if (cur.selection()) {
307 cutSelection(cur, false, pastesel);
308 cur.clearSelection();
311 text->insertInset(cur, inset);
314 inset->edit(cur, true);
316 if (!gotsel || !pastesel)
319 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
320 cur.buffer()->errors("Paste");
321 cur.clearSelection(); // bug 393
323 InsetText * inset_text = inset->asInsetText();
325 inset_text->fixParagraphsFont();
326 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
327 // reset first par to default
328 cur.text()->paragraphs().begin()
329 ->setPlainOrDefaultLayout(bparams.documentClass());
332 // Merge multiple paragraphs -- hack
333 while (cur.lastpit() > 0)
334 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
335 if (cmd.action() == LFUN_FLEX_INSERT)
338 cur.leaveInset(*inset);
339 if (cmd.action() == LFUN_PREVIEW_INSERT
340 || cmd.action() == LFUN_IPA_INSERT)
342 notifyCursorLeavesOrEnters(old, cur);
345 cur.leaveInset(*inset);
346 // reset surrounding par to default
347 DocumentClass const & dc = bparams.documentClass();
348 docstring const layoutname = inset->usePlainLayout()
349 ? dc.plainLayoutName()
350 : dc.defaultLayoutName();
351 text->setLayout(cur, layoutname);
357 string const freefont2string()
359 return freefont.toString(toggleall);
363 /// the type of outline operation
365 OutlineUp, // Move this header with text down
366 OutlineDown, // Move this header with text up
367 OutlineIn, // Make this header deeper
368 OutlineOut // Make this header shallower
372 static void outline(OutlineOp mode, Cursor & cur)
374 Buffer & buf = *cur.buffer();
375 pit_type & pit = cur.pit();
376 ParagraphList & pars = buf.text().paragraphs();
377 ParagraphList::iterator const bgn = pars.begin();
378 // The first paragraph of the area to be copied:
379 ParagraphList::iterator start = next(bgn, pit);
380 // The final paragraph of area to be copied:
381 ParagraphList::iterator finish = start;
382 ParagraphList::iterator const end = pars.end();
384 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
387 // Move out (down) from this section header
391 // Seek the one (on same level) below
392 for (; finish != end; ++finish) {
393 toclevel = buf.text().getTocLevel(distance(bgn, finish));
394 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
400 if (start == pars.begin())
403 ParagraphList::iterator dest = start;
404 // Move out (up) from this header
407 // Search previous same-level header above
410 toclevel = buf.text().getTocLevel(distance(bgn, dest));
412 && (toclevel == Layout::NOT_IN_TOC
413 || toclevel > thistoclevel));
414 // Not found; do nothing
415 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
417 pit_type const newpit = distance(bgn, dest);
418 pit_type const len = distance(start, finish);
419 pit_type const deletepit = pit + len;
420 buf.undo().recordUndo(cur, newpit, deletepit - 1);
421 pars.splice(dest, start, finish);
429 // Go one down from *this* header:
430 ParagraphList::iterator dest = next(finish, 1);
431 // Go further down to find header to insert in front of:
432 for (; dest != end; ++dest) {
433 toclevel = buf.text().getTocLevel(distance(bgn, dest));
434 if (toclevel != Layout::NOT_IN_TOC
435 && toclevel <= thistoclevel)
438 // One such was found:
439 pit_type newpit = distance(bgn, dest);
440 buf.undo().recordUndo(cur, pit, newpit - 1);
441 pit_type const len = distance(start, finish);
442 pars.splice(dest, start, finish);
443 cur.pit() = newpit - len;
448 pit_type const len = distance(start, finish);
449 buf.undo().recordUndo(cur, pit, pit + len - 1);
450 for (; start != finish; ++start) {
451 toclevel = buf.text().getTocLevel(distance(bgn, start));
452 if (toclevel == Layout::NOT_IN_TOC)
455 DocumentClass const & tc = buf.params().documentClass();
456 DocumentClass::const_iterator lit = tc.begin();
457 DocumentClass::const_iterator len = tc.end();
458 int const newtoclevel =
459 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
460 LabelType const oldlabeltype = start->layout().labeltype;
462 for (; lit != len; ++lit) {
463 if (lit->toclevel == newtoclevel &&
464 lit->labeltype == oldlabeltype) {
465 start->setLayout(*lit);
476 void Text::number(Cursor & cur)
478 FontInfo font = ignore_font;
479 font.setNumber(FONT_TOGGLE);
480 toggleAndShow(cur, this, Font(font, ignore_language));
484 bool Text::isRTL(Paragraph const & par) const
486 Buffer const & buffer = owner_->buffer();
487 return par.isRTL(buffer.params());
491 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
493 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
495 // Dispatch if the cursor is inside the text. It is not the
496 // case for context menus (bug 5797).
497 if (cur.text() != this) {
502 BufferView * bv = &cur.bv();
503 TextMetrics * tm = &bv->textMetrics(this);
504 if (!tm->contains(cur.pit())) {
505 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
506 tm = &bv->textMetrics(this);
509 // FIXME: We use the update flag to indicates wether a singlePar or a
510 // full screen update is needed. We reset it here but shall we restore it
512 cur.noScreenUpdate();
514 LBUFERR(this == cur.text());
516 // NOTE: This should NOT be a reference. See commit 94a5481a.
517 CursorSlice const oldTopSlice = cur.top();
518 bool const oldBoundary = cur.boundary();
519 bool const oldSelection = cur.selection();
520 // Signals that, even if needsUpdate == false, an update of the
521 // cursor paragraph is required
522 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
523 LyXAction::SingleParUpdate);
524 // Signals that a full-screen update is required
525 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
526 LyXAction::NoUpdate) || singleParUpdate);
527 bool const last_misspelled = lyxrc.spellcheck_continuously
528 && cur.paragraph().isMisspelled(cur.pos(), true);
530 FuncCode const act = cmd.action();
533 case LFUN_PARAGRAPH_MOVE_DOWN: {
534 pit_type const pit = cur.pit();
535 cur.recordUndo(pit, pit + 1);
537 pars_.swap(pit, pit + 1);
539 cur.forceBufferUpdate();
544 case LFUN_PARAGRAPH_MOVE_UP: {
545 pit_type const pit = cur.pit();
546 cur.recordUndo(pit - 1, pit);
548 pars_.swap(pit, pit - 1);
551 cur.forceBufferUpdate();
555 case LFUN_APPENDIX: {
556 Paragraph & par = cur.paragraph();
557 bool start = !par.params().startOfAppendix();
559 // FIXME: The code below only makes sense at top level.
560 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
561 // ensure that we have only one start_of_appendix in this document
562 // FIXME: this don't work for multipart document!
563 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
564 if (pars_[tmp].params().startOfAppendix()) {
565 cur.recordUndo(tmp, tmp);
566 pars_[tmp].params().startOfAppendix(false);
572 par.params().startOfAppendix(start);
574 // we can set the refreshing parameters now
575 cur.forceBufferUpdate();
579 case LFUN_WORD_DELETE_FORWARD:
581 cutSelection(cur, true, false);
583 deleteWordForward(cur);
584 finishChange(cur, false);
587 case LFUN_WORD_DELETE_BACKWARD:
589 cutSelection(cur, true, false);
591 deleteWordBackward(cur);
592 finishChange(cur, false);
595 case LFUN_LINE_DELETE_FORWARD:
597 cutSelection(cur, true, false);
599 tm->deleteLineForward(cur);
600 finishChange(cur, false);
603 case LFUN_BUFFER_BEGIN:
604 case LFUN_BUFFER_BEGIN_SELECT:
605 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
606 if (cur.depth() == 1)
607 needsUpdate |= cursorTop(cur);
610 cur.screenUpdateFlags(Update::FitCursor);
613 case LFUN_BUFFER_END:
614 case LFUN_BUFFER_END_SELECT:
615 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
616 if (cur.depth() == 1)
617 needsUpdate |= cursorBottom(cur);
620 cur.screenUpdateFlags(Update::FitCursor);
623 case LFUN_INSET_BEGIN:
624 case LFUN_INSET_BEGIN_SELECT:
625 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
626 if (cur.depth() == 1 || !cur.top().at_begin())
627 needsUpdate |= cursorTop(cur);
630 cur.screenUpdateFlags(Update::FitCursor);
634 case LFUN_INSET_END_SELECT:
635 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
636 if (cur.depth() == 1 || !cur.top().at_end())
637 needsUpdate |= cursorBottom(cur);
640 cur.screenUpdateFlags(Update::FitCursor);
643 case LFUN_CHAR_FORWARD:
644 case LFUN_CHAR_FORWARD_SELECT: {
645 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
646 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
647 bool const cur_moved = cursorForward(cur);
648 needsUpdate |= cur_moved;
650 if (!cur_moved && oldTopSlice == cur.top()
651 && cur.boundary() == oldBoundary) {
653 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
655 // we will probably be moving out the inset, so we should execute
656 // the depm-mechanism, but only when the cursor has a place to
657 // go outside this inset, i.e. in a slice above.
658 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
659 && cur.pit() == cur.lastpit()) {
660 // The cursor hasn't changed yet. To give the
661 // DEPM the possibility of doing something we must
662 // provide it with two different cursors.
664 dummy.pos() = dummy.pit() = 0;
665 if (cur.bv().checkDepm(dummy, cur))
666 cur.forceBufferUpdate();
672 case LFUN_CHAR_BACKWARD:
673 case LFUN_CHAR_BACKWARD_SELECT: {
674 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
675 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
676 bool const cur_moved = cursorBackward(cur);
677 needsUpdate |= cur_moved;
679 if (!cur_moved && oldTopSlice == cur.top()
680 && cur.boundary() == oldBoundary) {
682 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
684 // we will probably be moving out the inset, so we should execute
685 // the depm-mechanism, but only when the cursor has a place to
686 // go outside this inset, i.e. in a slice above.
687 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
688 // The cursor hasn't changed yet. To give the
689 // DEPM the possibility of doing something we must
690 // provide it with two different cursors.
692 dummy.pos() = cur.lastpos();
693 dummy.pit() = cur.lastpit();
694 if (cur.bv().checkDepm(dummy, cur))
695 cur.forceBufferUpdate();
702 case LFUN_CHAR_LEFT_SELECT:
703 if (lyxrc.visual_cursor) {
704 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
705 bool const cur_moved = cursorVisLeft(cur);
706 needsUpdate |= cur_moved;
707 if (!cur_moved && oldTopSlice == cur.top()
708 && cur.boundary() == oldBoundary) {
710 cmd = FuncRequest(LFUN_FINISHED_LEFT);
713 if (reverseDirectionNeeded(cur)) {
714 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
715 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
717 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
718 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
725 case LFUN_CHAR_RIGHT:
726 case LFUN_CHAR_RIGHT_SELECT:
727 if (lyxrc.visual_cursor) {
728 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
729 bool const cur_moved = cursorVisRight(cur);
730 needsUpdate |= cur_moved;
731 if (!cur_moved && oldTopSlice == cur.top()
732 && cur.boundary() == oldBoundary) {
734 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
737 if (reverseDirectionNeeded(cur)) {
738 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
739 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
741 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
742 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
751 case LFUN_DOWN_SELECT:
754 // stop/start the selection
755 bool select = cmd.action() == LFUN_DOWN_SELECT ||
756 cmd.action() == LFUN_UP_SELECT;
758 // move cursor up/down
759 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
760 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
762 if (!atFirstOrLastRow) {
763 needsUpdate |= cur.selHandle(select);
764 cur.upDownInText(up, needsUpdate);
765 needsUpdate |= cur.beforeDispatchCursor().inMathed();
767 // if the cursor cannot be moved up or down do not remove
768 // the selection right now, but wait for the next dispatch.
770 needsUpdate |= cur.selHandle(select);
771 cur.upDownInText(up, needsUpdate);
778 case LFUN_PARAGRAPH_UP:
779 case LFUN_PARAGRAPH_UP_SELECT:
780 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
781 needsUpdate |= cursorUpParagraph(cur);
784 case LFUN_PARAGRAPH_DOWN:
785 case LFUN_PARAGRAPH_DOWN_SELECT:
786 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
787 needsUpdate |= cursorDownParagraph(cur);
790 case LFUN_LINE_BEGIN:
791 case LFUN_LINE_BEGIN_SELECT:
792 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
793 needsUpdate |= tm->cursorHome(cur);
797 case LFUN_LINE_END_SELECT:
798 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
799 needsUpdate |= tm->cursorEnd(cur);
802 case LFUN_SECTION_SELECT: {
803 Buffer const & buf = *cur.buffer();
804 pit_type const pit = cur.pit();
805 ParagraphList & pars = buf.text().paragraphs();
806 ParagraphList::iterator bgn = pars.begin();
807 // The first paragraph of the area to be selected:
808 ParagraphList::iterator start = next(bgn, pit);
809 // The final paragraph of area to be selected:
810 ParagraphList::iterator finish = start;
811 ParagraphList::iterator end = pars.end();
813 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
814 if (thistoclevel == Layout::NOT_IN_TOC)
818 Cursor const old_cur = cur;
819 needsUpdate |= cur.selHandle(true);
821 // Move out (down) from this section header
825 // Seek the one (on same level) below
826 for (; finish != end; ++finish, ++cur.pit()) {
827 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
828 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
831 cur.pos() = cur.lastpos();
833 needsUpdate |= cur != old_cur;
837 case LFUN_WORD_RIGHT:
838 case LFUN_WORD_RIGHT_SELECT:
839 if (lyxrc.visual_cursor) {
840 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
841 bool const cur_moved = cursorVisRightOneWord(cur);
842 needsUpdate |= cur_moved;
843 if (!cur_moved && oldTopSlice == cur.top()
844 && cur.boundary() == oldBoundary) {
846 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
849 if (reverseDirectionNeeded(cur)) {
850 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
851 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
853 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
854 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
861 case LFUN_WORD_FORWARD:
862 case LFUN_WORD_FORWARD_SELECT: {
863 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
864 bool const cur_moved = cursorForwardOneWord(cur);
865 needsUpdate |= cur_moved;
867 if (!cur_moved && oldTopSlice == cur.top()
868 && cur.boundary() == oldBoundary) {
870 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
872 // we will probably be moving out the inset, so we should execute
873 // the depm-mechanism, but only when the cursor has a place to
874 // go outside this inset, i.e. in a slice above.
875 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
876 && cur.pit() == cur.lastpit()) {
877 // The cursor hasn't changed yet. To give the
878 // DEPM the possibility of doing something we must
879 // provide it with two different cursors.
881 dummy.pos() = dummy.pit() = 0;
882 if (cur.bv().checkDepm(dummy, cur))
883 cur.forceBufferUpdate();
890 case LFUN_WORD_LEFT_SELECT:
891 if (lyxrc.visual_cursor) {
892 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
893 bool const cur_moved = cursorVisLeftOneWord(cur);
894 needsUpdate |= cur_moved;
895 if (!cur_moved && oldTopSlice == cur.top()
896 && cur.boundary() == oldBoundary) {
898 cmd = FuncRequest(LFUN_FINISHED_LEFT);
901 if (reverseDirectionNeeded(cur)) {
902 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
903 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
905 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
906 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
913 case LFUN_WORD_BACKWARD:
914 case LFUN_WORD_BACKWARD_SELECT: {
915 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
916 bool const cur_moved = cursorBackwardOneWord(cur);
917 needsUpdate |= cur_moved;
919 if (!cur_moved && oldTopSlice == cur.top()
920 && cur.boundary() == oldBoundary) {
922 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
924 // we will probably be moving out the inset, so we should execute
925 // the depm-mechanism, but only when the cursor has a place to
926 // go outside this inset, i.e. in a slice above.
927 if (cur.depth() > 1 && cur.pos() == 0
929 // The cursor hasn't changed yet. To give the
930 // DEPM the possibility of doing something we must
931 // provide it with two different cursors.
933 dummy.pos() = cur.lastpos();
934 dummy.pit() = cur.lastpit();
935 if (cur.bv().checkDepm(dummy, cur))
936 cur.forceBufferUpdate();
942 case LFUN_WORD_SELECT: {
943 selectWord(cur, WHOLE_WORD);
944 finishChange(cur, true);
948 case LFUN_NEWLINE_INSERT: {
949 InsetNewlineParams inp;
950 docstring arg = cmd.argument();
951 if (arg == "linebreak")
952 inp.kind = InsetNewlineParams::LINEBREAK;
954 inp.kind = InsetNewlineParams::NEWLINE;
955 cap::replaceSelection(cur);
957 cur.insert(new InsetNewline(inp));
959 moveCursor(cur, false);
963 case LFUN_TAB_INSERT: {
964 bool const multi_par_selection = cur.selection() &&
965 cur.selBegin().pit() != cur.selEnd().pit();
966 if (multi_par_selection) {
967 // If there is a multi-paragraph selection, a tab is inserted
968 // at the beginning of each paragraph.
969 cur.recordUndoSelection();
970 pit_type const pit_end = cur.selEnd().pit();
971 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
972 pars_[pit].insertChar(0, '\t',
973 bv->buffer().params().track_changes);
974 // Update the selection pos to make sure the selection does not
975 // change as the inserted tab will increase the logical pos.
976 if (cur.realAnchor().pit() == pit)
977 cur.realAnchor().forwardPos();
978 if (cur.pit() == pit)
983 // Maybe we shouldn't allow tabs within a line, because they
984 // are not (yet) aligned as one might do expect.
985 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
991 case LFUN_TAB_DELETE: {
992 bool const tc = bv->buffer().params().track_changes;
993 if (cur.selection()) {
994 // If there is a selection, a tab (if present) is removed from
995 // the beginning of each paragraph.
996 cur.recordUndoSelection();
997 pit_type const pit_end = cur.selEnd().pit();
998 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
999 Paragraph & par = paragraphs()[pit];
1002 char_type const c = par.getChar(0);
1003 if (c == '\t' || c == ' ') {
1004 // remove either 1 tab or 4 spaces.
1005 int const n = (c == ' ' ? 4 : 1);
1006 for (int i = 0; i < n
1007 && !par.empty() && par.getChar(0) == c; ++i) {
1008 if (cur.pit() == pit)
1010 if (cur.realAnchor().pit() == pit
1011 && cur.realAnchor().pos() > 0 )
1012 cur.realAnchor().backwardPos();
1013 par.eraseChar(0, tc);
1019 // If there is no selection, try to remove a tab or some spaces
1020 // before the position of the cursor.
1021 Paragraph & par = paragraphs()[cur.pit()];
1022 pos_type const pos = cur.pos();
1027 char_type const c = par.getChar(pos - 1);
1031 par.eraseChar(cur.pos(), tc);
1033 for (int n_spaces = 0;
1035 && par.getChar(cur.pos() - 1) == ' '
1039 par.eraseChar(cur.pos(), tc);
1046 case LFUN_CHAR_DELETE_FORWARD:
1047 if (!cur.selection()) {
1048 bool was_separator = cur.paragraph().isEnvSeparator(cur.pos());
1049 if (cur.pos() == cur.paragraph().size())
1050 // Par boundary, force full-screen update
1051 singleParUpdate = false;
1052 needsUpdate |= erase(cur);
1054 if (was_separator && cur.pos() == cur.paragraph().size()
1055 && (!cur.paragraph().layout().isEnvironment()
1056 || cur.paragraph().size() > 0)) {
1057 // Force full-screen update
1058 singleParUpdate = false;
1059 needsUpdate |= erase(cur);
1062 // It is possible to make it a lot faster still
1063 // just comment out the line below...
1065 cutSelection(cur, true, false);
1066 singleParUpdate = false;
1068 moveCursor(cur, false);
1071 case LFUN_CHAR_DELETE_BACKWARD:
1072 if (!cur.selection()) {
1073 if (bv->getIntl().getTransManager().backspace()) {
1074 bool par_boundary = cur.pos() == 0;
1075 bool first_par = cur.pit() == 0;
1076 // Par boundary, full-screen update
1078 singleParUpdate = false;
1079 needsUpdate |= backspace(cur);
1081 if (par_boundary && !first_par && cur.pos() > 0
1082 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1083 needsUpdate |= backspace(cur);
1088 cutSelection(cur, true, false);
1089 singleParUpdate = false;
1093 case LFUN_PARAGRAPH_BREAK: {
1094 cap::replaceSelection(cur);
1095 pit_type pit = cur.pit();
1096 Paragraph const & par = pars_[pit];
1097 pit_type prev = pit;
1099 if (!pars_[pit - 1].layout().isEnvironment())
1100 prev = depthHook(pit, par.getDepth());
1101 else if (pars_[pit - 1].getDepth() >= par.getDepth())
1104 if (prev < pit && cur.pos() == par.beginOfBody()
1105 && !par.isEnvSeparator(cur.pos())
1106 && !par.layout().isCommand()
1107 && pars_[prev].layout() != par.layout()
1108 && pars_[prev].layout().isEnvironment()) {
1109 if (par.layout().isEnvironment()
1110 && pars_[prev].getDepth() == par.getDepth()) {
1111 docstring const layout = par.layout().name();
1112 DocumentClass const & tc = bv->buffer().params().documentClass();
1113 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1114 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "parbreak"));
1115 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1116 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1118 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "parbreak"));
1119 breakParagraph(cur);
1121 Font const f(inherit_font, cur.current_font.language());
1122 pars_[cur.pit() - 1].resetFonts(f);
1124 breakParagraph(cur, cmd.argument() == "inverse");
1127 // If we have a list and autoinsert item insets,
1129 Layout::LaTeXArgMap args = par.layout().args();
1130 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1131 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1132 for (; lait != laend; ++lait) {
1133 Layout::latexarg arg = (*lait).second;
1134 if (arg.autoinsert && prefixIs((*lait).first, "item:")) {
1135 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1142 case LFUN_INSET_INSERT: {
1145 // We have to avoid triggering InstantPreview loading
1146 // before inserting into the document. See bug #5626.
1147 bool loaded = bv->buffer().isFullyLoaded();
1148 bv->buffer().setFullyLoaded(false);
1149 Inset * inset = createInset(&bv->buffer(), cmd);
1150 bv->buffer().setFullyLoaded(loaded);
1153 // FIXME (Abdel 01/02/2006):
1154 // What follows would be a partial fix for bug 2154:
1155 // http://www.lyx.org/trac/ticket/2154
1156 // This automatically put the label inset _after_ a
1157 // numbered section. It should be possible to extend the mechanism
1158 // to any kind of LateX environement.
1159 // The correct way to fix that bug would be at LateX generation.
1160 // I'll let the code here for reference as it could be used for some
1161 // other feature like "automatic labelling".
1163 Paragraph & par = pars_[cur.pit()];
1164 if (inset->lyxCode() == LABEL_CODE
1165 && !par.layout().counter.empty()) {
1166 // Go to the end of the paragraph
1167 // Warning: Because of Change-Tracking, the last
1168 // position is 'size()' and not 'size()-1':
1169 cur.pos() = par.size();
1170 // Insert a new paragraph
1171 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1175 if (cur.selection())
1176 cutSelection(cur, true, false);
1178 if (inset->editable() && inset->asInsetText())
1179 inset->edit(cur, true);
1183 // trigger InstantPreview now
1184 if (inset->lyxCode() == EXTERNAL_CODE) {
1185 InsetExternal & ins =
1186 static_cast<InsetExternal &>(*inset);
1187 ins.updatePreview();
1194 case LFUN_INSET_DISSOLVE: {
1195 if (dissolveInset(cur)) {
1197 cur.forceBufferUpdate();
1202 case LFUN_SET_GRAPHICS_GROUP: {
1203 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1209 string id = to_utf8(cmd.argument());
1210 string grp = graphics::getGroupParams(bv->buffer(), id);
1211 InsetGraphicsParams tmp, inspar = ins->getParams();
1214 inspar.groupId = to_utf8(cmd.argument());
1216 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1217 tmp.filename = inspar.filename;
1221 ins->setParams(inspar);
1224 case LFUN_SPACE_INSERT:
1225 if (cur.paragraph().layout().free_spacing)
1226 insertChar(cur, ' ');
1228 doInsertInset(cur, this, cmd, false, false);
1231 moveCursor(cur, false);
1234 case LFUN_SPECIALCHAR_INSERT: {
1235 string const name = to_utf8(cmd.argument());
1236 if (name == "hyphenation")
1237 specialChar(cur, InsetSpecialChar::HYPHENATION);
1238 else if (name == "ligature-break")
1239 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1240 else if (name == "slash")
1241 specialChar(cur, InsetSpecialChar::SLASH);
1242 else if (name == "nobreakdash")
1243 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1244 else if (name == "dots")
1245 specialChar(cur, InsetSpecialChar::LDOTS);
1246 else if (name == "end-of-sentence")
1247 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1248 else if (name == "menu-separator")
1249 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1250 else if (name == "lyx")
1251 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1252 else if (name == "tex")
1253 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1254 else if (name == "latex")
1255 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1256 else if (name == "latex2e")
1257 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1258 else if (name.empty())
1259 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1261 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1265 case LFUN_IPAMACRO_INSERT: {
1266 string const arg = cmd.getArg(0);
1267 if (arg == "deco") {
1268 // Open the inset, and move the current selection
1270 doInsertInset(cur, this, cmd, true, true);
1272 // Some insets are numbered, others are shown in the outline pane so
1273 // let's update the labels and the toc backend.
1274 cur.forceBufferUpdate();
1277 if (arg == "tone-falling")
1278 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1279 else if (arg == "tone-rising")
1280 ipaChar(cur, InsetIPAChar::TONE_RISING);
1281 else if (arg == "tone-high-rising")
1282 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1283 else if (arg == "tone-low-rising")
1284 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1285 else if (arg == "tone-high-rising-falling")
1286 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1287 else if (arg.empty())
1288 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1290 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1294 case LFUN_WORD_UPCASE:
1295 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1298 case LFUN_WORD_LOWCASE:
1299 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1302 case LFUN_WORD_CAPITALIZE:
1303 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1306 case LFUN_CHARS_TRANSPOSE:
1307 charsTranspose(cur);
1311 cur.message(_("Paste"));
1312 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1313 cap::replaceSelection(cur);
1315 // without argument?
1316 string const arg = to_utf8(cmd.argument());
1318 bool tryGraphics = true;
1319 if (theClipboard().isInternal())
1320 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1321 else if (theClipboard().hasTextContents()) {
1322 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1323 true, Clipboard::AnyTextType))
1324 tryGraphics = false;
1326 if (tryGraphics && theClipboard().hasGraphicsContents())
1327 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1328 } else if (isStrUnsignedInt(arg)) {
1329 // we have a numerical argument
1330 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1331 convert<unsigned int>(arg));
1332 } else if (arg == "html" || arg == "latex") {
1333 Clipboard::TextType type = (arg == "html") ?
1334 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1335 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1337 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1339 type = Clipboard::PdfGraphicsType;
1340 else if (arg == "png")
1341 type = Clipboard::PngGraphicsType;
1342 else if (arg == "jpeg")
1343 type = Clipboard::JpegGraphicsType;
1344 else if (arg == "linkback")
1345 type = Clipboard::LinkBackGraphicsType;
1346 else if (arg == "emf")
1347 type = Clipboard::EmfGraphicsType;
1348 else if (arg == "wmf")
1349 type = Clipboard::WmfGraphicsType;
1351 // we also check in getStatus()
1352 LYXERR0("Unrecognized graphics type: " << arg);
1354 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1357 bv->buffer().errors("Paste");
1358 cur.clearSelection(); // bug 393
1364 cutSelection(cur, true, true);
1365 cur.message(_("Cut"));
1370 cur.message(_("Copy"));
1373 case LFUN_SERVER_GET_XY:
1374 cur.message(from_utf8(
1375 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1376 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1379 case LFUN_SERVER_SET_XY: {
1382 istringstream is(to_utf8(cmd.argument()));
1385 lyxerr << "SETXY: Could not parse coordinates in '"
1386 << to_utf8(cmd.argument()) << endl;
1388 tm->setCursorFromCoordinates(cur, x, y);
1392 case LFUN_SERVER_GET_LAYOUT:
1393 cur.message(cur.paragraph().layout().name());
1397 docstring layout = cmd.argument();
1398 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1400 Paragraph const & para = cur.paragraph();
1401 docstring const old_layout = para.layout().name();
1402 DocumentClass const & tclass = bv->buffer().params().documentClass();
1405 layout = tclass.defaultLayoutName();
1407 if (owner_->forcePlainLayout())
1408 // in this case only the empty layout is allowed
1409 layout = tclass.plainLayoutName();
1410 else if (para.usePlainLayout()) {
1411 // in this case, default layout maps to empty layout
1412 if (layout == tclass.defaultLayoutName())
1413 layout = tclass.plainLayoutName();
1415 // otherwise, the empty layout maps to the default
1416 if (layout == tclass.plainLayoutName())
1417 layout = tclass.defaultLayoutName();
1420 bool hasLayout = tclass.hasLayout(layout);
1422 // If the entry is obsolete, use the new one instead.
1424 docstring const & obs = tclass[layout].obsoleted_by();
1430 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1431 from_utf8(N_(" not known")));
1435 bool change_layout = (old_layout != layout);
1437 if (!change_layout && cur.selection() &&
1438 cur.selBegin().pit() != cur.selEnd().pit())
1440 pit_type spit = cur.selBegin().pit();
1441 pit_type epit = cur.selEnd().pit() + 1;
1442 while (spit != epit) {
1443 if (pars_[spit].layout().name() != old_layout) {
1444 change_layout = true;
1452 setLayout(cur, layout);
1454 Layout::LaTeXArgMap args = tclass[layout].args();
1455 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1456 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1457 for (; lait != laend; ++lait) {
1458 Layout::latexarg arg = (*lait).second;
1459 if (arg.autoinsert) {
1460 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1468 case LFUN_ENVIRONMENT_SPLIT: {
1469 bool const outer = cmd.argument() == "outer";
1470 Paragraph const & para = cur.paragraph();
1471 docstring layout = para.layout().name();
1472 depth_type split_depth = cur.paragraph().params().depth();
1474 // check if we have an environment in our nesting hierarchy
1475 pit_type pit = cur.pit();
1476 Paragraph cpar = pars_[pit];
1478 if (pit == 0 || cpar.params().depth() == 0)
1482 if (cpar.params().depth() < split_depth
1483 && cpar.layout().isEnvironment()) {
1484 layout = cpar.layout().name();
1485 split_depth = cpar.params().depth();
1490 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1492 while (cur.paragraph().params().depth() > split_depth)
1493 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1495 DocumentClass const & tc = bv->buffer().params().documentClass();
1496 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1497 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1498 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1499 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1504 case LFUN_CLIPBOARD_PASTE:
1505 cap::replaceSelection(cur);
1506 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1507 cmd.argument() == "paragraph");
1508 bv->buffer().errors("Paste");
1511 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1512 cap::replaceSelection(cur);
1513 pasteSimpleText(cur, cmd.argument() == "paragraph");
1516 case LFUN_PRIMARY_SELECTION_PASTE:
1517 cap::replaceSelection(cur);
1518 pasteString(cur, theSelection().get(),
1519 cmd.argument() == "paragraph");
1522 case LFUN_SELECTION_PASTE:
1523 // Copy the selection buffer to the clipboard stack,
1524 // because we want it to appear in the "Edit->Paste
1526 cap::replaceSelection(cur);
1527 cap::copySelectionToStack();
1528 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1529 bv->buffer().errors("Paste");
1532 case LFUN_UNICODE_INSERT: {
1533 if (cmd.argument().empty())
1535 docstring hexstring = cmd.argument();
1536 if (isHex(hexstring)) {
1537 char_type c = hexToInt(hexstring);
1538 if (c >= 32 && c < 0x10ffff) {
1539 lyxerr << "Inserting c: " << c << endl;
1540 docstring s = docstring(1, c);
1541 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1547 case LFUN_QUOTE_INSERT: {
1548 cap::replaceSelection(cur);
1551 Paragraph const & par = cur.paragraph();
1552 pos_type pos = cur.pos();
1553 // Ignore deleted text before cursor
1554 while (pos > 0 && par.isDeleted(pos - 1))
1557 BufferParams const & bufparams = bv->buffer().params();
1559 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1560 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1562 string const arg = to_utf8(cmd.argument());
1563 if (allow_inset_quote) {
1565 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1566 c = par.getChar(pos - 1);
1567 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1568 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1569 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1572 // The cursor might have been invalidated by the replaceSelection.
1573 cur.buffer()->changed(true);
1574 string const quote_string = (arg == "single") ? "'" : "\"";
1575 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, quote_string));
1580 case LFUN_DATE_INSERT: {
1581 string const format = cmd.argument().empty()
1582 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1583 string const time = formatted_time(current_time(), format);
1584 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1588 case LFUN_MOUSE_TRIPLE:
1589 if (cmd.button() == mouse_button::button1) {
1590 tm->cursorHome(cur);
1598 case LFUN_MOUSE_DOUBLE:
1599 if (cmd.button() == mouse_button::button1) {
1600 selectWord(cur, WHOLE_WORD);
1605 // Single-click on work area
1606 case LFUN_MOUSE_PRESS: {
1607 // We are not marking a selection with the keyboard in any case.
1608 Cursor & bvcur = cur.bv().cursor();
1609 bvcur.setMark(false);
1610 switch (cmd.button()) {
1611 case mouse_button::button1:
1613 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1614 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1615 if (bvcur.wordSelection())
1616 selectWord(bvcur, WHOLE_WORD);
1619 case mouse_button::button2:
1620 if (lyxrc.mouse_middlebutton_paste) {
1621 // Middle mouse pasting.
1622 bv->mouseSetCursor(cur);
1624 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1625 "selection-paste ; primary-selection-paste paragraph"));
1627 cur.noScreenUpdate();
1630 case mouse_button::button3: {
1631 // Don't do anything if we right-click a
1632 // selection, a context menu will popup.
1633 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1634 && cur < bvcur.selectionEnd()) {
1635 cur.noScreenUpdate();
1638 if (!bv->mouseSetCursor(cur, false))
1639 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1645 } // switch (cmd.button())
1648 case LFUN_MOUSE_MOTION: {
1649 // Mouse motion with right or middle mouse do nothing for now.
1650 if (cmd.button() != mouse_button::button1) {
1651 cur.noScreenUpdate();
1654 // ignore motions deeper nested than the real anchor
1655 Cursor & bvcur = cur.bv().cursor();
1656 if (!bvcur.realAnchor().hasPart(cur)) {
1660 CursorSlice old = bvcur.top();
1662 int const wh = bv->workHeight();
1663 int const y = max(0, min(wh - 1, cmd.y()));
1665 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1666 cur.setTargetX(cmd.x());
1668 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1669 else if (cmd.y() < 0)
1670 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1671 // This is to allow jumping over large insets
1672 if (cur.top() == old) {
1674 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1675 else if (cmd.y() < 0)
1676 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1678 // We continue with our existing selection or start a new one, so don't
1679 // reset the anchor.
1680 bvcur.setCursor(cur);
1681 bvcur.setSelection(true);
1682 if (cur.top() == old) {
1683 // We didn't move one iota, so no need to update the screen.
1684 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1685 //cur.noScreenUpdate();
1691 case LFUN_MOUSE_RELEASE:
1692 switch (cmd.button()) {
1693 case mouse_button::button1:
1694 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1695 // If there is a new selection, update persistent selection;
1696 // otherwise, single click does not clear persistent selection
1698 if (cur.selection()) {
1699 // Finish selection. If double click,
1700 // cur is moved to the end of word by
1701 // selectWord but bvcur is current
1703 cur.bv().cursor().setSelection();
1704 // We might have removed an empty but drawn selection
1705 // (probably a margin)
1706 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1708 cur.noScreenUpdate();
1709 // FIXME: We could try to handle drag and drop of selection here.
1712 case mouse_button::button2:
1713 // Middle mouse pasting is handled at mouse press time,
1714 // see LFUN_MOUSE_PRESS.
1715 cur.noScreenUpdate();
1718 case mouse_button::button3:
1719 // Cursor was set at LFUN_MOUSE_PRESS time.
1720 // FIXME: If there is a selection we could try to handle a special
1721 // drag & drop context menu.
1722 cur.noScreenUpdate();
1725 case mouse_button::none:
1726 case mouse_button::button4:
1727 case mouse_button::button5:
1729 } // switch (cmd.button())
1733 case LFUN_SELF_INSERT: {
1734 if (cmd.argument().empty())
1737 // Automatically delete the currently selected
1738 // text and replace it with what is being
1739 // typed in now. Depends on lyxrc settings
1740 // "auto_region_delete", which defaults to
1743 if (lyxrc.auto_region_delete && cur.selection())
1744 cutSelection(cur, false, false);
1746 cur.clearSelection();
1748 docstring::const_iterator cit = cmd.argument().begin();
1749 docstring::const_iterator const end = cmd.argument().end();
1750 for (; cit != end; ++cit)
1751 bv->translateAndInsert(*cit, this, cur);
1754 moveCursor(cur, false);
1755 cur.markNewWordPosition();
1756 bv->bookmarkEditPosition();
1760 case LFUN_HREF_INSERT: {
1761 // FIXME If we're actually given an argument, shouldn't
1762 // we use it, whether or not we have a selection?
1763 docstring content = cmd.argument();
1764 if (cur.selection()) {
1765 content = cur.selectionAsString(false);
1766 cutSelection(cur, true, false);
1769 InsetCommandParams p(HYPERLINK_CODE);
1770 if (!content.empty()){
1771 // if it looks like a link, we'll put it as target,
1772 // otherwise as name (bug #8792).
1775 // regex_match(to_utf8(content), matches, link_re)
1776 // because smatch stores pointers to the substrings rather
1777 // than making copies of them. And those pointers become
1778 // invalid after regex_match returns, since it is then
1779 // being given a temporary object. (Thanks to Georg for
1780 // figuring that out.)
1781 regex const link_re("^([a-z]+):.*");
1783 string const c = to_utf8(lowercase(content));
1785 if (c.substr(0,7) == "mailto:") {
1786 p["target"] = content;
1787 p["type"] = from_ascii("mailto:");
1788 } else if (regex_match(c, matches, link_re)) {
1789 p["target"] = content;
1790 string protocol = matches.str(1);
1791 if (protocol == "file")
1792 p["type"] = from_ascii("file:");
1794 p["name"] = content;
1796 string const data = InsetCommand::params2string(p);
1798 // we need to have a target. if we already have one, then
1799 // that gets used at the default for the name, too, which
1800 // is probably what is wanted.
1801 if (p["target"].empty()) {
1802 bv->showDialog("href", data);
1804 FuncRequest fr(LFUN_INSET_INSERT, data);
1810 case LFUN_LABEL_INSERT: {
1811 InsetCommandParams p(LABEL_CODE);
1812 // Try to generate a valid label
1813 p["name"] = (cmd.argument().empty()) ?
1814 cur.getPossibleLabel() :
1816 string const data = InsetCommand::params2string(p);
1818 if (cmd.argument().empty()) {
1819 bv->showDialog("label", data);
1821 FuncRequest fr(LFUN_INSET_INSERT, data);
1827 case LFUN_INFO_INSERT: {
1829 if (cmd.argument().empty() && cur.selection()) {
1830 // if command argument is empty use current selection as parameter.
1831 docstring ds = cur.selectionAsString(false);
1832 cutSelection(cur, true, false);
1833 FuncRequest cmd0(cmd, ds);
1834 inset = createInset(cur.buffer(), cmd0);
1836 inset = createInset(cur.buffer(), cmd);
1841 insertInset(cur, inset);
1845 case LFUN_CAPTION_INSERT:
1846 case LFUN_FOOTNOTE_INSERT:
1847 case LFUN_NOTE_INSERT:
1848 case LFUN_BOX_INSERT:
1849 case LFUN_BRANCH_INSERT:
1850 case LFUN_PHANTOM_INSERT:
1851 case LFUN_ERT_INSERT:
1852 case LFUN_LISTING_INSERT:
1853 case LFUN_MARGINALNOTE_INSERT:
1854 case LFUN_ARGUMENT_INSERT:
1855 case LFUN_INDEX_INSERT:
1856 case LFUN_PREVIEW_INSERT:
1857 case LFUN_SCRIPT_INSERT:
1858 case LFUN_IPA_INSERT:
1859 // Open the inset, and move the current selection
1861 doInsertInset(cur, this, cmd, true, true);
1863 // Some insets are numbered, others are shown in the outline pane so
1864 // let's update the labels and the toc backend.
1865 cur.forceBufferUpdate();
1868 case LFUN_FLEX_INSERT: {
1869 // Open the inset, and move the current selection
1871 bool const sel = cur.selection();
1872 doInsertInset(cur, this, cmd, true, true);
1873 // Insert auto-insert arguments
1874 bool autoargs = false;
1875 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1876 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1877 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1878 for (; lait != laend; ++lait) {
1879 Layout::latexarg arg = (*lait).second;
1880 if (arg.autoinsert) {
1881 // The cursor might have been invalidated by the replaceSelection.
1882 cur.buffer()->changed(true);
1883 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1890 cur.leaveInset(cur.inset());
1893 // Some insets are numbered, others are shown in the outline pane so
1894 // let's update the labels and the toc backend.
1895 cur.forceBufferUpdate();
1899 case LFUN_TABULAR_INSERT:
1900 // if there were no arguments, just open the dialog
1901 if (doInsertInset(cur, this, cmd, false, true))
1904 bv->showDialog("tabularcreate");
1908 case LFUN_FLOAT_INSERT:
1909 case LFUN_FLOAT_WIDE_INSERT:
1910 case LFUN_WRAP_INSERT: {
1911 // will some content be moved into the inset?
1912 bool const content = cur.selection();
1913 // does the content consist of multiple paragraphs?
1914 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1916 doInsertInset(cur, this, cmd, true, true);
1919 // If some single-par content is moved into the inset,
1920 // doInsertInset puts the cursor outside the inset.
1921 // To insert the caption we put it back into the inset.
1922 // FIXME cleanup doInsertInset to avoid such dances!
1923 if (content && singlepar)
1926 ParagraphList & pars = cur.text()->paragraphs();
1928 DocumentClass const & tclass = bv->buffer().params().documentClass();
1930 // add a separate paragraph for the caption inset
1931 pars.push_back(Paragraph());
1932 pars.back().setInsetOwner(&cur.text()->inset());
1933 pars.back().setPlainOrDefaultLayout(tclass);
1934 int cap_pit = pars.size() - 1;
1936 // if an empty inset was created, we create an additional empty
1937 // paragraph at the bottom so that the user can choose where to put
1938 // the graphics (or table).
1940 pars.push_back(Paragraph());
1941 pars.back().setInsetOwner(&cur.text()->inset());
1942 pars.back().setPlainOrDefaultLayout(tclass);
1945 // reposition the cursor to the caption
1946 cur.pit() = cap_pit;
1948 // FIXME: This Text/Cursor dispatch handling is a mess!
1949 // We cannot use Cursor::dispatch here it needs access to up to
1951 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1952 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1953 cur.forceBufferUpdate();
1954 cur.screenUpdateFlags(Update::Force);
1955 // FIXME: When leaving the Float (or Wrap) inset we should
1956 // delete any empty paragraph left above or below the
1961 case LFUN_NOMENCL_INSERT: {
1962 InsetCommandParams p(NOMENCL_CODE);
1963 if (cmd.argument().empty())
1964 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1966 p["symbol"] = cmd.argument();
1967 string const data = InsetCommand::params2string(p);
1968 bv->showDialog("nomenclature", data);
1972 case LFUN_INDEX_PRINT: {
1973 InsetCommandParams p(INDEX_PRINT_CODE);
1974 if (cmd.argument().empty())
1975 p["type"] = from_ascii("idx");
1977 p["type"] = cmd.argument();
1978 string const data = InsetCommand::params2string(p);
1979 FuncRequest fr(LFUN_INSET_INSERT, data);
1984 case LFUN_NOMENCL_PRINT:
1985 case LFUN_NEWPAGE_INSERT:
1987 doInsertInset(cur, this, cmd, false, false);
1991 case LFUN_SEPARATOR_INSERT: {
1992 doInsertInset(cur, this, cmd, false, false);
1994 // remove a following space
1995 Paragraph & par = cur.paragraph();
1996 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
1997 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2001 case LFUN_DEPTH_DECREMENT:
2002 changeDepth(cur, DEC_DEPTH);
2005 case LFUN_DEPTH_INCREMENT:
2006 changeDepth(cur, INC_DEPTH);
2009 case LFUN_MATH_DISPLAY:
2010 mathDispatch(cur, cmd, true);
2013 case LFUN_REGEXP_MODE:
2014 regexpDispatch(cur, cmd);
2017 case LFUN_MATH_MODE:
2018 if (cmd.argument() == "on")
2019 // don't pass "on" as argument
2020 // (it would appear literally in the first cell)
2021 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
2023 mathDispatch(cur, cmd, false);
2026 case LFUN_MATH_MACRO:
2027 if (cmd.argument().empty())
2028 cur.errorMessage(from_utf8(N_("Missing argument")));
2031 string s = to_utf8(cmd.argument());
2032 string const s1 = token(s, ' ', 1);
2033 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2034 string const s2 = token(s, ' ', 2);
2035 MacroType type = MacroTypeNewcommand;
2037 type = MacroTypeDef;
2038 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
2039 from_utf8(token(s, ' ', 0)), nargs, false, type);
2040 inset->setBuffer(bv->buffer());
2041 insertInset(cur, inset);
2043 // enter macro inset and select the name
2045 cur.top().pos() = cur.top().lastpos();
2047 cur.setSelection(true);
2048 cur.top().pos() = 0;
2052 // passthrough hat and underscore outside mathed:
2053 case LFUN_MATH_SUBSCRIPT:
2054 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
2056 case LFUN_MATH_SUPERSCRIPT:
2057 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
2060 case LFUN_MATH_INSERT:
2061 case LFUN_MATH_AMS_MATRIX:
2062 case LFUN_MATH_MATRIX:
2063 case LFUN_MATH_DELIM:
2064 case LFUN_MATH_BIGDELIM: {
2066 cap::replaceSelection(cur);
2067 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
2068 checkAndActivateInset(cur, true);
2069 LASSERT(cur.inMathed(), break);
2074 case LFUN_FONT_EMPH: {
2075 Font font(ignore_font, ignore_language);
2076 font.fontInfo().setEmph(FONT_TOGGLE);
2077 toggleAndShow(cur, this, font);
2081 case LFUN_FONT_ITAL: {
2082 Font font(ignore_font, ignore_language);
2083 font.fontInfo().setShape(ITALIC_SHAPE);
2084 toggleAndShow(cur, this, font);
2088 case LFUN_FONT_BOLD:
2089 case LFUN_FONT_BOLDSYMBOL: {
2090 Font font(ignore_font, ignore_language);
2091 font.fontInfo().setSeries(BOLD_SERIES);
2092 toggleAndShow(cur, this, font);
2096 case LFUN_FONT_NOUN: {
2097 Font font(ignore_font, ignore_language);
2098 font.fontInfo().setNoun(FONT_TOGGLE);
2099 toggleAndShow(cur, this, font);
2103 case LFUN_FONT_TYPEWRITER: {
2104 Font font(ignore_font, ignore_language);
2105 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2106 toggleAndShow(cur, this, font);
2110 case LFUN_FONT_SANS: {
2111 Font font(ignore_font, ignore_language);
2112 font.fontInfo().setFamily(SANS_FAMILY);
2113 toggleAndShow(cur, this, font);
2117 case LFUN_FONT_ROMAN: {
2118 Font font(ignore_font, ignore_language);
2119 font.fontInfo().setFamily(ROMAN_FAMILY);
2120 toggleAndShow(cur, this, font);
2124 case LFUN_FONT_DEFAULT: {
2125 Font font(inherit_font, ignore_language);
2126 toggleAndShow(cur, this, font);
2130 case LFUN_FONT_STRIKEOUT: {
2131 Font font(ignore_font, ignore_language);
2132 font.fontInfo().setStrikeout(FONT_TOGGLE);
2133 toggleAndShow(cur, this, font);
2137 case LFUN_FONT_UNDERUNDERLINE: {
2138 Font font(ignore_font, ignore_language);
2139 font.fontInfo().setUuline(FONT_TOGGLE);
2140 toggleAndShow(cur, this, font);
2144 case LFUN_FONT_UNDERWAVE: {
2145 Font font(ignore_font, ignore_language);
2146 font.fontInfo().setUwave(FONT_TOGGLE);
2147 toggleAndShow(cur, this, font);
2151 case LFUN_FONT_UNDERLINE: {
2152 Font font(ignore_font, ignore_language);
2153 font.fontInfo().setUnderbar(FONT_TOGGLE);
2154 toggleAndShow(cur, this, font);
2158 case LFUN_FONT_SIZE: {
2159 Font font(ignore_font, ignore_language);
2160 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2161 toggleAndShow(cur, this, font);
2165 case LFUN_LANGUAGE: {
2166 string const lang_arg = cmd.getArg(0);
2167 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2168 Language const * lang =
2169 reset ? reset_language
2170 : languages.getLanguage(lang_arg);
2171 // we allow reset_language, which is 0, but only if it
2172 // was requested via empty or "reset" arg.
2173 if (!lang && !reset)
2175 bool const toggle = (cmd.getArg(1) != "set");
2176 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2177 Font font(ignore_font, lang);
2178 toggleAndShow(cur, this, font, toggle);
2182 case LFUN_TEXTSTYLE_APPLY:
2183 toggleAndShow(cur, this, freefont, toggleall);
2184 cur.message(_("Character set"));
2187 // Set the freefont using the contents of \param data dispatched from
2188 // the frontends and apply it at the current cursor location.
2189 case LFUN_TEXTSTYLE_UPDATE: {
2192 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2195 toggleAndShow(cur, this, freefont, toggleall);
2196 cur.message(_("Character set"));
2198 lyxerr << "Argument not ok";
2203 case LFUN_FINISHED_LEFT:
2204 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2205 // We're leaving an inset, going left. If the inset is LTR, we're
2206 // leaving from the front, so we should not move (remain at --- but
2207 // not in --- the inset). If the inset is RTL, move left, without
2208 // entering the inset itself; i.e., move to after the inset.
2209 if (cur.paragraph().getFontSettings(
2210 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2211 cursorVisLeft(cur, true);
2214 case LFUN_FINISHED_RIGHT:
2215 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2216 // We're leaving an inset, going right. If the inset is RTL, we're
2217 // leaving from the front, so we should not move (remain at --- but
2218 // not in --- the inset). If the inset is LTR, move right, without
2219 // entering the inset itself; i.e., move to after the inset.
2220 if (!cur.paragraph().getFontSettings(
2221 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2222 cursorVisRight(cur, true);
2225 case LFUN_FINISHED_BACKWARD:
2226 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2229 case LFUN_FINISHED_FORWARD:
2230 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2232 cur.setCurrentFont();
2235 case LFUN_LAYOUT_PARAGRAPH: {
2237 params2string(cur.paragraph(), data);
2238 data = "show\n" + data;
2239 bv->showDialog("paragraph", data);
2243 case LFUN_PARAGRAPH_UPDATE: {
2245 params2string(cur.paragraph(), data);
2247 // Will the paragraph accept changes from the dialog?
2249 cur.inset().allowParagraphCustomization(cur.idx());
2251 data = "update " + convert<string>(accept) + '\n' + data;
2252 bv->updateDialog("paragraph", data);
2256 case LFUN_ACCENT_UMLAUT:
2257 case LFUN_ACCENT_CIRCUMFLEX:
2258 case LFUN_ACCENT_GRAVE:
2259 case LFUN_ACCENT_ACUTE:
2260 case LFUN_ACCENT_TILDE:
2261 case LFUN_ACCENT_CEDILLA:
2262 case LFUN_ACCENT_MACRON:
2263 case LFUN_ACCENT_DOT:
2264 case LFUN_ACCENT_UNDERDOT:
2265 case LFUN_ACCENT_UNDERBAR:
2266 case LFUN_ACCENT_CARON:
2267 case LFUN_ACCENT_BREVE:
2268 case LFUN_ACCENT_TIE:
2269 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2270 case LFUN_ACCENT_CIRCLE:
2271 case LFUN_ACCENT_OGONEK:
2272 theApp()->handleKeyFunc(cmd.action());
2273 if (!cmd.argument().empty())
2274 // FIXME: Are all these characters encoded in one byte in utf8?
2275 bv->translateAndInsert(cmd.argument()[0], this, cur);
2276 cur.screenUpdateFlags(Update::FitCursor);
2279 case LFUN_FLOAT_LIST_INSERT: {
2280 DocumentClass const & tclass = bv->buffer().params().documentClass();
2281 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2283 if (cur.selection())
2284 cutSelection(cur, true, false);
2285 breakParagraph(cur);
2287 if (cur.lastpos() != 0) {
2288 cursorBackward(cur);
2289 breakParagraph(cur);
2292 docstring const laystr = cur.inset().usePlainLayout() ?
2293 tclass.plainLayoutName() :
2294 tclass.defaultLayoutName();
2295 setLayout(cur, laystr);
2296 ParagraphParameters p;
2297 // FIXME If this call were replaced with one to clearParagraphParams(),
2298 // then we could get rid of this method altogether.
2299 setParagraphs(cur, p);
2300 // FIXME This should be simplified when InsetFloatList takes a
2301 // Buffer in its constructor.
2302 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2303 ifl->setBuffer(bv->buffer());
2304 insertInset(cur, ifl);
2307 lyxerr << "Non-existent float type: "
2308 << to_utf8(cmd.argument()) << endl;
2313 case LFUN_CHANGE_ACCEPT: {
2314 acceptOrRejectChanges(cur, ACCEPT);
2318 case LFUN_CHANGE_REJECT: {
2319 acceptOrRejectChanges(cur, REJECT);
2323 case LFUN_THESAURUS_ENTRY: {
2324 docstring arg = cmd.argument();
2326 arg = cur.selectionAsString(false);
2328 if (arg.size() > 100 || arg.empty()) {
2329 // Get word or selection
2330 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2331 arg = cur.selectionAsString(false);
2332 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2335 if (lyxrc.thesaurusdir_path.empty()) {
2336 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2337 _("The path to the thesaurus directory has not been specified.\n"
2338 "The thesaurus is not functional.\n"
2339 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2342 bv->showDialog("thesaurus", to_utf8(arg));
2346 case LFUN_SPELLING_ADD: {
2347 docstring word = from_utf8(cmd.getArg(0));
2350 word = cur.selectionAsString(false);
2352 if (word.size() > 100 || word.empty()) {
2353 // Get word or selection
2354 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2355 word = cur.selectionAsString(false);
2357 lang = const_cast<Language *>(cur.getFont().language());
2358 } else if (cmd.getArg(1).empty()) {
2359 // optional language argument is missing
2360 // use the language at cursor position
2361 lang = const_cast<Language *>(cur.getFont().language());
2363 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2365 WordLangTuple wl(word, lang);
2366 theSpellChecker()->insert(wl);
2370 case LFUN_SPELLING_IGNORE: {
2371 docstring word = from_utf8(cmd.getArg(0));
2374 word = cur.selectionAsString(false);
2376 if (word.size() > 100 || word.empty()) {
2377 // Get word or selection
2378 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2379 word = cur.selectionAsString(false);
2381 lang = const_cast<Language *>(cur.getFont().language());
2382 } else if (cmd.getArg(1).empty()) {
2383 lang = const_cast<Language *>(cur.getFont().language());
2385 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2387 WordLangTuple wl(word, lang);
2388 theSpellChecker()->accept(wl);
2392 case LFUN_SPELLING_REMOVE: {
2393 docstring word = from_utf8(cmd.getArg(0));
2396 word = cur.selectionAsString(false);
2398 if (word.size() > 100 || word.empty()) {
2399 // Get word or selection
2400 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2401 word = cur.selectionAsString(false);
2403 lang = const_cast<Language *>(cur.getFont().language());
2404 } else if (cmd.getArg(1).empty()) {
2405 lang = const_cast<Language *>(cur.getFont().language());
2407 lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
2409 WordLangTuple wl(word, lang);
2410 theSpellChecker()->remove(wl);
2414 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2415 // Given data, an encoding of the ParagraphParameters
2416 // generated in the Paragraph dialog, this function sets
2417 // the current paragraph, or currently selected paragraphs,
2419 // NOTE: This function overrides all existing settings.
2420 setParagraphs(cur, cmd.argument());
2421 cur.message(_("Paragraph layout set"));
2425 case LFUN_PARAGRAPH_PARAMS: {
2426 // Given data, an encoding of the ParagraphParameters as we'd
2427 // find them in a LyX file, this function modifies the current paragraph,
2428 // or currently selected paragraphs.
2429 // NOTE: This function only modifies, and does not override, existing
2431 setParagraphs(cur, cmd.argument(), true);
2432 cur.message(_("Paragraph layout set"));
2437 if (cur.selection()) {
2438 cur.setSelection(false);
2441 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2442 // correct, but I'm not 100% sure -- dov, 071019
2443 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2447 case LFUN_OUTLINE_UP:
2448 outline(OutlineUp, cur);
2449 setCursor(cur, cur.pit(), 0);
2450 cur.forceBufferUpdate();
2454 case LFUN_OUTLINE_DOWN:
2455 outline(OutlineDown, cur);
2456 setCursor(cur, cur.pit(), 0);
2457 cur.forceBufferUpdate();
2461 case LFUN_OUTLINE_IN:
2462 outline(OutlineIn, cur);
2463 cur.forceBufferUpdate();
2467 case LFUN_OUTLINE_OUT:
2468 outline(OutlineOut, cur);
2469 cur.forceBufferUpdate();
2473 case LFUN_SERVER_GET_STATISTICS:
2475 DocIterator from, to;
2476 if (cur.selection()) {
2477 from = cur.selectionBegin();
2478 to = cur.selectionEnd();
2480 from = doc_iterator_begin(cur.buffer());
2481 to = doc_iterator_end(cur.buffer());
2484 cur.buffer()->updateStatistics(from, to);
2485 string const arg0 = cmd.getArg(0);
2486 if (arg0 == "words") {
2487 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2488 } else if (arg0 == "chars") {
2489 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2490 } else if (arg0 == "chars-space") {
2491 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2493 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2494 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2495 + convert<docstring>(cur.buffer()->charCount(true)));
2501 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2506 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2508 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2509 // Check for misspelled text
2510 // The redraw is useful because of the painting of
2511 // misspelled markers depends on the cursor position.
2512 // Trigger a redraw for cursor moves inside misspelled text.
2513 if (!cur.inTexted()) {
2514 // move from regular text to math
2515 needsUpdate = last_misspelled;
2516 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2517 // move inside regular text
2518 needsUpdate = last_misspelled
2519 || cur.paragraph().isMisspelled(cur.pos(), true);
2523 // FIXME: The cursor flag is reset two lines below
2524 // so we need to check here if some of the LFUN did touch that.
2525 // for now only Text::erase() and Text::backspace() do that.
2526 // The plan is to verify all the LFUNs and then to remove this
2527 // singleParUpdate boolean altogether.
2528 if (cur.result().screenUpdate() & Update::Force) {
2529 singleParUpdate = false;
2533 // FIXME: the following code should go in favor of fine grained
2534 // update flag treatment.
2535 if (singleParUpdate) {
2536 // Inserting characters does not change par height in general. So, try
2537 // to update _only_ this paragraph. BufferView will detect if a full
2538 // metrics update is needed anyway.
2539 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2543 && &oldTopSlice.inset() == &cur.inset()
2544 && oldTopSlice.idx() == cur.idx()
2545 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2546 && !cur.selection())
2547 // FIXME: it would be better if we could just do this
2549 //if (cur.result().update() != Update::FitCursor)
2550 // cur.noScreenUpdate();
2552 // But some LFUNs do not set Update::FitCursor when needed, so we
2553 // do it for all. This is not very harmfull as FitCursor will provoke
2554 // a full redraw only if needed but still, a proper review of all LFUN
2555 // should be done and this needsUpdate boolean can then be removed.
2556 cur.screenUpdateFlags(Update::FitCursor);
2558 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2562 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2563 FuncStatus & flag) const
2565 LBUFERR(this == cur.text());
2567 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2569 bool allow_in_passthru = false;
2570 InsetCode code = NO_CODE;
2572 switch (cmd.action()) {
2574 case LFUN_DEPTH_DECREMENT:
2575 enable = changeDepthAllowed(cur, DEC_DEPTH);
2578 case LFUN_DEPTH_INCREMENT:
2579 enable = changeDepthAllowed(cur, INC_DEPTH);
2583 // FIXME We really should not allow this to be put, e.g.,
2584 // in a footnote, or in ERT. But it would make sense in a
2585 // branch, so I'm not sure what to do.
2586 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2589 case LFUN_DIALOG_SHOW_NEW_INSET:
2590 if (cmd.argument() == "bibitem")
2591 code = BIBITEM_CODE;
2592 else if (cmd.argument() == "bibtex") {
2594 // not allowed in description items
2595 enable = !inDescriptionItem(cur);
2597 else if (cmd.argument() == "box")
2599 else if (cmd.argument() == "branch")
2601 else if (cmd.argument() == "citation")
2603 else if (cmd.argument() == "ert")
2605 else if (cmd.argument() == "external")
2606 code = EXTERNAL_CODE;
2607 else if (cmd.argument() == "float")
2609 else if (cmd.argument() == "graphics")
2610 code = GRAPHICS_CODE;
2611 else if (cmd.argument() == "href")
2612 code = HYPERLINK_CODE;
2613 else if (cmd.argument() == "include")
2614 code = INCLUDE_CODE;
2615 else if (cmd.argument() == "index")
2617 else if (cmd.argument() == "index_print")
2618 code = INDEX_PRINT_CODE;
2619 else if (cmd.argument() == "listings")
2620 code = LISTINGS_CODE;
2621 else if (cmd.argument() == "mathspace")
2622 code = MATH_HULL_CODE;
2623 else if (cmd.argument() == "nomenclature")
2624 code = NOMENCL_CODE;
2625 else if (cmd.argument() == "nomencl_print")
2626 code = NOMENCL_PRINT_CODE;
2627 else if (cmd.argument() == "label")
2629 else if (cmd.argument() == "line")
2631 else if (cmd.argument() == "note")
2633 else if (cmd.argument() == "phantom")
2634 code = PHANTOM_CODE;
2635 else if (cmd.argument() == "ref")
2637 else if (cmd.argument() == "space")
2639 else if (cmd.argument() == "toc")
2641 else if (cmd.argument() == "vspace")
2643 else if (cmd.argument() == "wrap")
2647 case LFUN_ERT_INSERT:
2650 case LFUN_LISTING_INSERT:
2651 code = LISTINGS_CODE;
2652 // not allowed in description items
2653 enable = !inDescriptionItem(cur);
2655 case LFUN_FOOTNOTE_INSERT:
2658 case LFUN_TABULAR_INSERT:
2659 code = TABULAR_CODE;
2661 case LFUN_MARGINALNOTE_INSERT:
2664 case LFUN_FLOAT_INSERT:
2665 case LFUN_FLOAT_WIDE_INSERT:
2666 // FIXME: If there is a selection, we should check whether there
2667 // are floats in the selection, but this has performance issues, see
2668 // LFUN_CHANGE_ACCEPT/REJECT.
2670 if (inDescriptionItem(cur))
2671 // not allowed in description items
2674 InsetCode const inset_code = cur.inset().lyxCode();
2676 // algorithm floats cannot be put in another float
2677 if (to_utf8(cmd.argument()) == "algorithm") {
2678 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2682 // for figures and tables: only allow in another
2683 // float or wrap if it is of the same type and
2684 // not a subfloat already
2685 if(cur.inset().lyxCode() == code) {
2686 InsetFloat const & ins =
2687 static_cast<InsetFloat const &>(cur.inset());
2688 enable = ins.params().type == to_utf8(cmd.argument())
2689 && !ins.params().subfloat;
2690 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2691 InsetWrap const & ins =
2692 static_cast<InsetWrap const &>(cur.inset());
2693 enable = ins.params().type == to_utf8(cmd.argument());
2697 case LFUN_WRAP_INSERT:
2699 // not allowed in description items
2700 enable = !inDescriptionItem(cur);
2702 case LFUN_FLOAT_LIST_INSERT: {
2703 code = FLOAT_LIST_CODE;
2704 // not allowed in description items
2705 enable = !inDescriptionItem(cur);
2707 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2708 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2709 // make sure we know about such floats
2710 if (cit == floats.end() ||
2711 // and that we know how to generate a list of them
2712 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2713 flag.setUnknown(true);
2714 // probably not necessary, but...
2720 case LFUN_CAPTION_INSERT: {
2721 code = CAPTION_CODE;
2722 string arg = cmd.getArg(0);
2723 bool varia = arg != "LongTableNoNumber"
2724 && cur.inset().allowsCaptionVariation(arg);
2725 // not allowed in description items,
2726 // and in specific insets
2727 enable = !inDescriptionItem(cur)
2728 && (varia || arg.empty() || arg == "Standard");
2731 case LFUN_NOTE_INSERT:
2733 // in commands (sections etc.) and description items,
2734 // only Notes are allowed
2735 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2736 (!cur.paragraph().layout().isCommand()
2737 && !inDescriptionItem(cur)));
2739 case LFUN_FLEX_INSERT: {
2741 string s = cmd.getArg(0);
2743 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2744 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2745 il.lyxtype() != InsetLayout::CUSTOM &&
2746 il.lyxtype() != InsetLayout::ELEMENT &&
2747 il.lyxtype ()!= InsetLayout::STANDARD)
2751 case LFUN_BOX_INSERT:
2754 case LFUN_BRANCH_INSERT:
2756 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2757 && cur.buffer()->params().branchlist().empty())
2760 case LFUN_IPA_INSERT:
2763 case LFUN_PHANTOM_INSERT:
2764 code = PHANTOM_CODE;
2766 case LFUN_LABEL_INSERT:
2769 case LFUN_INFO_INSERT:
2772 case LFUN_ARGUMENT_INSERT: {
2774 allow_in_passthru = true;
2775 string const arg = cmd.getArg(0);
2780 Layout const & lay = cur.paragraph().layout();
2781 Layout::LaTeXArgMap args = lay.args();
2782 Layout::LaTeXArgMap::const_iterator const lait =
2784 if (lait != args.end()) {
2786 pit_type pit = cur.pit();
2787 pit_type lastpit = cur.pit();
2788 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2789 // In a sequence of "merged" environment layouts, we only allow
2790 // non-item arguments once.
2791 lastpit = cur.lastpit();
2792 // get the first paragraph in sequence with this layout
2793 depth_type const current_depth = cur.paragraph().params().depth();
2797 Paragraph cpar = pars_[pit - 1];
2798 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2804 for (; pit <= lastpit; ++pit) {
2805 if (pars_[pit].layout() != lay)
2807 InsetList::const_iterator it = pars_[pit].insetList().begin();
2808 InsetList::const_iterator end = pars_[pit].insetList().end();
2809 for (; it != end; ++it) {
2810 if (it->inset->lyxCode() == ARG_CODE) {
2811 InsetArgument const * ins =
2812 static_cast<InsetArgument const *>(it->inset);
2813 if (ins->name() == arg) {
2814 // we have this already
2825 case LFUN_INDEX_INSERT:
2828 case LFUN_INDEX_PRINT:
2829 code = INDEX_PRINT_CODE;
2830 // not allowed in description items
2831 enable = !inDescriptionItem(cur);
2833 case LFUN_NOMENCL_INSERT:
2834 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2838 code = NOMENCL_CODE;
2840 case LFUN_NOMENCL_PRINT:
2841 code = NOMENCL_PRINT_CODE;
2842 // not allowed in description items
2843 enable = !inDescriptionItem(cur);
2845 case LFUN_HREF_INSERT:
2846 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2850 code = HYPERLINK_CODE;
2852 case LFUN_IPAMACRO_INSERT: {
2853 string const arg = cmd.getArg(0);
2855 code = IPADECO_CODE;
2857 code = IPACHAR_CODE;
2860 case LFUN_QUOTE_INSERT:
2861 // always allow this, since we will inset a raw quote
2862 // if an inset is not allowed.
2864 case LFUN_SPECIALCHAR_INSERT:
2865 code = SPECIALCHAR_CODE;
2867 case LFUN_SPACE_INSERT:
2868 // slight hack: we know this is allowed in math mode
2872 case LFUN_PREVIEW_INSERT:
2873 code = PREVIEW_CODE;
2875 case LFUN_SCRIPT_INSERT:
2879 case LFUN_MATH_INSERT:
2880 case LFUN_MATH_AMS_MATRIX:
2881 case LFUN_MATH_MATRIX:
2882 case LFUN_MATH_DELIM:
2883 case LFUN_MATH_BIGDELIM:
2884 case LFUN_MATH_DISPLAY:
2885 case LFUN_MATH_MODE:
2886 case LFUN_MATH_MACRO:
2887 case LFUN_MATH_SUBSCRIPT:
2888 case LFUN_MATH_SUPERSCRIPT:
2889 code = MATH_HULL_CODE;
2892 case LFUN_REGEXP_MODE:
2893 code = MATH_HULL_CODE;
2894 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2897 case LFUN_INSET_MODIFY:
2898 // We need to disable this, because we may get called for a
2900 // InsetTabular::getStatus() -> InsetText::getStatus()
2901 // and we don't handle LFUN_INSET_MODIFY.
2905 case LFUN_FONT_EMPH:
2906 flag.setOnOff(fontinfo.emph() == FONT_ON);
2907 enable = !cur.paragraph().isPassThru();
2910 case LFUN_FONT_ITAL:
2911 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2912 enable = !cur.paragraph().isPassThru();
2915 case LFUN_FONT_NOUN:
2916 flag.setOnOff(fontinfo.noun() == FONT_ON);
2917 enable = !cur.paragraph().isPassThru();
2920 case LFUN_FONT_BOLD:
2921 case LFUN_FONT_BOLDSYMBOL:
2922 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2923 enable = !cur.paragraph().isPassThru();
2926 case LFUN_FONT_SANS:
2927 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2928 enable = !cur.paragraph().isPassThru();
2931 case LFUN_FONT_ROMAN:
2932 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2933 enable = !cur.paragraph().isPassThru();
2936 case LFUN_FONT_TYPEWRITER:
2937 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2938 enable = !cur.paragraph().isPassThru();
2943 enable = cur.selection();
2947 if (cmd.argument().empty()) {
2948 if (theClipboard().isInternal())
2949 enable = cap::numberOfSelections() > 0;
2951 enable = !theClipboard().empty();
2955 // we have an argument
2956 string const arg = to_utf8(cmd.argument());
2957 if (isStrUnsignedInt(arg)) {
2958 // it's a number and therefore means the internal stack
2959 unsigned int n = convert<unsigned int>(arg);
2960 enable = cap::numberOfSelections() > n;
2964 // explicit text type?
2965 if (arg == "html") {
2966 // Do not enable for PlainTextType, since some tidying in the
2967 // frontend is needed for HTML, which is too unsafe for plain text.
2968 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
2970 } else if (arg == "latex") {
2971 // LaTeX is usually not available on the clipboard with
2972 // the correct MIME type, but in plain text.
2973 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
2974 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
2978 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2980 type = Clipboard::PdfGraphicsType;
2981 else if (arg == "png")
2982 type = Clipboard::PngGraphicsType;
2983 else if (arg == "jpeg")
2984 type = Clipboard::JpegGraphicsType;
2985 else if (arg == "linkback")
2986 type = Clipboard::LinkBackGraphicsType;
2987 else if (arg == "emf")
2988 type = Clipboard::EmfGraphicsType;
2989 else if (arg == "wmf")
2990 type = Clipboard::WmfGraphicsType;
2993 LYXERR0("Unrecognized graphics type: " << arg);
2994 // we don't want to assert if the user just mistyped the LFUN
2995 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
2999 enable = theClipboard().hasGraphicsContents(type);
3003 case LFUN_CLIPBOARD_PASTE:
3004 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3005 enable = !theClipboard().empty();
3008 case LFUN_PRIMARY_SELECTION_PASTE:
3009 enable = cur.selection() || !theSelection().empty();
3012 case LFUN_SELECTION_PASTE:
3013 enable = cap::selection();
3016 case LFUN_PARAGRAPH_MOVE_UP:
3017 enable = cur.pit() > 0 && !cur.selection();
3020 case LFUN_PARAGRAPH_MOVE_DOWN:
3021 enable = cur.pit() < cur.lastpit() && !cur.selection();
3024 case LFUN_CHANGE_ACCEPT:
3025 case LFUN_CHANGE_REJECT:
3026 // In principle, these LFUNs should only be enabled if there
3027 // is a change at the current position/in the current selection.
3028 // However, without proper optimizations, this will inevitably
3029 // result in unacceptable performance - just imagine a user who
3030 // wants to select the complete content of a long document.
3031 if (!cur.selection())
3032 enable = cur.paragraph().isChanged(cur.pos());
3034 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3039 case LFUN_OUTLINE_UP:
3040 case LFUN_OUTLINE_DOWN:
3041 case LFUN_OUTLINE_IN:
3042 case LFUN_OUTLINE_OUT:
3043 // FIXME: LyX is not ready for outlining within inset.
3044 enable = isMainText()
3045 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3048 case LFUN_NEWLINE_INSERT:
3049 // LaTeX restrictions (labels or empty par)
3050 enable = !cur.paragraph().isPassThru()
3051 && cur.pos() > cur.paragraph().beginOfBody();
3054 case LFUN_SEPARATOR_INSERT:
3055 // Always enabled for now
3059 case LFUN_TAB_INSERT:
3060 case LFUN_TAB_DELETE:
3061 enable = cur.paragraph().isPassThru();
3064 case LFUN_SET_GRAPHICS_GROUP: {
3065 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3069 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3073 case LFUN_NEWPAGE_INSERT:
3074 // not allowed in description items
3075 code = NEWPAGE_CODE;
3076 enable = !inDescriptionItem(cur);
3079 case LFUN_DATE_INSERT: {
3080 string const format = cmd.argument().empty()
3081 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3082 enable = support::os::is_valid_strftime(format);
3087 enable = !cur.paragraph().isPassThru();
3088 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3091 case LFUN_PARAGRAPH_BREAK:
3092 enable = inset().allowMultiPar();
3095 case LFUN_SPELLING_ADD:
3096 case LFUN_SPELLING_IGNORE:
3097 case LFUN_SPELLING_REMOVE:
3098 enable = theSpellChecker() != NULL;
3099 if (enable && !cmd.getArg(1).empty()) {
3100 // validate explicitly given language
3101 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3102 enable &= lang != NULL;
3107 enable = !cur.inset().forcePlainLayout();
3109 docstring layout = cmd.argument();
3110 if (layout.empty()) {
3111 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3112 layout = tclass.defaultLayoutName();
3114 flag.setOnOff(layout == cur.paragraph().layout().name());
3118 case LFUN_ENVIRONMENT_SPLIT: {
3119 if (cmd.argument() == "outer") {
3120 // check if we have an environment in our nesting hierarchy
3122 depth_type const current_depth = cur.paragraph().params().depth();
3123 pit_type pit = cur.pit();
3124 Paragraph cpar = pars_[pit];
3126 if (pit == 0 || cpar.params().depth() == 0)
3130 if (cpar.params().depth() < current_depth)
3131 res = cpar.layout().isEnvironment();
3136 else if (cur.paragraph().layout().isEnvironment()) {
3144 case LFUN_LAYOUT_PARAGRAPH:
3145 case LFUN_PARAGRAPH_PARAMS:
3146 case LFUN_PARAGRAPH_PARAMS_APPLY:
3147 case LFUN_PARAGRAPH_UPDATE:
3148 enable = cur.inset().allowParagraphCustomization();
3151 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3152 case LFUN_ACCENT_ACUTE:
3153 case LFUN_ACCENT_BREVE:
3154 case LFUN_ACCENT_CARON:
3155 case LFUN_ACCENT_CEDILLA:
3156 case LFUN_ACCENT_CIRCLE:
3157 case LFUN_ACCENT_CIRCUMFLEX:
3158 case LFUN_ACCENT_DOT:
3159 case LFUN_ACCENT_GRAVE:
3160 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3161 case LFUN_ACCENT_MACRON:
3162 case LFUN_ACCENT_OGONEK:
3163 case LFUN_ACCENT_TIE:
3164 case LFUN_ACCENT_TILDE:
3165 case LFUN_ACCENT_UMLAUT:
3166 case LFUN_ACCENT_UNDERBAR:
3167 case LFUN_ACCENT_UNDERDOT:
3168 case LFUN_FONT_DEFAULT:
3169 case LFUN_FONT_FRAK:
3170 case LFUN_FONT_SIZE:
3171 case LFUN_FONT_STATE:
3172 case LFUN_FONT_UNDERLINE:
3173 case LFUN_FONT_STRIKEOUT:
3174 case LFUN_FONT_UNDERUNDERLINE:
3175 case LFUN_FONT_UNDERWAVE:
3176 case LFUN_TEXTSTYLE_APPLY:
3177 case LFUN_TEXTSTYLE_UPDATE:
3178 enable = !cur.paragraph().isPassThru();
3181 case LFUN_WORD_DELETE_FORWARD:
3182 case LFUN_WORD_DELETE_BACKWARD:
3183 case LFUN_LINE_DELETE_FORWARD:
3184 case LFUN_WORD_FORWARD:
3185 case LFUN_WORD_BACKWARD:
3186 case LFUN_WORD_RIGHT:
3187 case LFUN_WORD_LEFT:
3188 case LFUN_CHAR_FORWARD:
3189 case LFUN_CHAR_FORWARD_SELECT:
3190 case LFUN_CHAR_BACKWARD:
3191 case LFUN_CHAR_BACKWARD_SELECT:
3192 case LFUN_CHAR_LEFT:
3193 case LFUN_CHAR_LEFT_SELECT:
3194 case LFUN_CHAR_RIGHT:
3195 case LFUN_CHAR_RIGHT_SELECT:
3197 case LFUN_UP_SELECT:
3199 case LFUN_DOWN_SELECT:
3200 case LFUN_PARAGRAPH_UP_SELECT:
3201 case LFUN_PARAGRAPH_DOWN_SELECT:
3202 case LFUN_LINE_BEGIN_SELECT:
3203 case LFUN_LINE_END_SELECT:
3204 case LFUN_WORD_FORWARD_SELECT:
3205 case LFUN_WORD_BACKWARD_SELECT:
3206 case LFUN_WORD_RIGHT_SELECT:
3207 case LFUN_WORD_LEFT_SELECT:
3208 case LFUN_WORD_SELECT:
3209 case LFUN_SECTION_SELECT:
3210 case LFUN_BUFFER_BEGIN:
3211 case LFUN_BUFFER_END:
3212 case LFUN_BUFFER_BEGIN_SELECT:
3213 case LFUN_BUFFER_END_SELECT:
3214 case LFUN_INSET_BEGIN:
3215 case LFUN_INSET_END:
3216 case LFUN_INSET_BEGIN_SELECT:
3217 case LFUN_INSET_END_SELECT:
3218 case LFUN_PARAGRAPH_UP:
3219 case LFUN_PARAGRAPH_DOWN:
3220 case LFUN_LINE_BEGIN:
3222 case LFUN_CHAR_DELETE_FORWARD:
3223 case LFUN_CHAR_DELETE_BACKWARD:
3224 case LFUN_WORD_UPCASE:
3225 case LFUN_WORD_LOWCASE:
3226 case LFUN_WORD_CAPITALIZE:
3227 case LFUN_CHARS_TRANSPOSE:
3228 case LFUN_SERVER_GET_XY:
3229 case LFUN_SERVER_SET_XY:
3230 case LFUN_SERVER_GET_LAYOUT:
3231 case LFUN_SELF_INSERT:
3232 case LFUN_UNICODE_INSERT:
3233 case LFUN_THESAURUS_ENTRY:
3235 case LFUN_SERVER_GET_STATISTICS:
3236 // these are handled in our dispatch()
3240 case LFUN_INSET_INSERT: {
3241 string const type = cmd.getArg(0);
3242 if (type == "toc") {
3244 // not allowed in description items
3245 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3246 enable = !inDescriptionItem(cur);
3259 || !cur.inset().insetAllowed(code)
3260 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3263 flag.setEnabled(enable);
3268 void Text::pasteString(Cursor & cur, docstring const & clip,
3271 if (!clip.empty()) {
3274 insertStringAsParagraphs(cur, clip, cur.current_font);
3276 insertStringAsLines(cur, clip, cur.current_font);
3281 // FIXME: an item inset would make things much easier.
3282 bool Text::inDescriptionItem(Cursor & cur) const
3284 Paragraph & par = cur.paragraph();
3285 pos_type const pos = cur.pos();
3286 pos_type const body_pos = par.beginOfBody();
3288 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3289 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3290 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3293 return (pos < body_pos
3295 && (pos == 0 || par.getChar(pos - 1) != ' ')));