3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
20 #include "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "SpellChecker.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
47 #include "Thesaurus.h"
48 #include "WordLangTuple.h"
50 #include "frontends/alert.h"
51 #include "frontends/Application.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetArgument.h"
56 #include "insets/InsetCollapsible.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetFloat.h"
60 #include "insets/InsetFloatList.h"
61 #include "insets/InsetGraphics.h"
62 #include "insets/InsetGraphicsParams.h"
63 #include "insets/InsetIPAMacro.h"
64 #include "insets/InsetNewline.h"
65 #include "insets/InsetQuotes.h"
66 #include "insets/InsetSpecialChar.h"
67 #include "insets/InsetText.h"
68 #include "insets/InsetWrap.h"
70 #include "support/convert.h"
71 #include "support/debug.h"
72 #include "support/gettext.h"
73 #include "support/lassert.h"
74 #include "support/lstrings.h"
75 #include "support/lyxalgo.h"
76 #include "support/lyxtime.h"
77 #include "support/os.h"
78 #include "support/regex.h"
80 #include "mathed/InsetMathHull.h"
81 #include "mathed/InsetMathMacroTemplate.h"
87 using namespace lyx::support;
91 using cap::copySelection;
92 using cap::cutSelection;
93 using cap::cutSelectionToTemp;
94 using cap::pasteFromStack;
95 using cap::pasteFromTemp;
96 using cap::pasteClipboardText;
97 using cap::pasteClipboardGraphics;
98 using cap::replaceSelection;
99 using cap::grabAndEraseSelection;
100 using cap::selClearOrDel;
101 using cap::pasteSimpleText;
102 using frontend::Clipboard;
105 static Font freefont(ignore_font, ignore_language);
106 static bool toggleall = false;
108 static void toggleAndShow(Cursor & cur, Text * text,
109 Font const & font, bool toggleall = true)
111 text->toggleFree(cur, font, toggleall);
113 if (font.language() != ignore_language ||
114 font.fontInfo().number() != FONT_IGNORE) {
115 TextMetrics const & tm = cur.bv().textMetrics(text);
116 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
117 cur.real_current_font))
118 text->setCursor(cur, cur.pit(), cur.pos(),
119 false, !cur.boundary());
124 static void moveCursor(Cursor & cur, bool selecting)
126 if (selecting || cur.mark())
131 static void finishChange(Cursor & cur, bool selecting)
134 moveCursor(cur, selecting);
138 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
141 docstring sel = cur.selectionAsString(false);
143 // It may happen that sel is empty but there is a selection
144 replaceSelection(cur);
146 // Is this a valid formula?
150 #ifdef ENABLE_ASSERTIONS
151 const int old_pos = cur.pos();
153 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
154 #ifdef ENABLE_ASSERTIONS
155 LATTEST(old_pos == cur.pos());
157 cur.nextInset()->edit(cur, true);
158 if (cmd.action() != LFUN_MATH_MODE)
159 // LFUN_MATH_MODE has a different meaning in math mode
162 InsetMathHull * formula = new InsetMathHull(cur.buffer());
163 string const selstr = to_utf8(sel);
164 istringstream is(selstr);
167 if (!formula->readQuiet(lex)) {
168 // No valid formula, let's try with delims
169 is.str("$" + selstr + "$");
171 if (!formula->readQuiet(lex)) {
172 // Still not valid, leave it as is
180 cur.nextInset()->edit(cur, true);
181 LASSERT(cur.inMathed(), return);
185 cur.pos() = cur.lastpos();
186 if (cmd.action() != LFUN_MATH_MODE)
187 // LFUN_MATH_MODE has a different meaning in math mode
189 cur.clearSelection();
190 cur.pos() = cur.lastpos();
194 cur.message(from_utf8(N_("Math editor mode")));
196 cur.message(from_utf8(N_("No valid math formula")));
200 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
202 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
203 if (cur.inRegexped()) {
204 cur.message(_("Already in regular expression mode"));
208 docstring sel = cur.selectionAsString(false);
210 // It may happen that sel is empty but there is a selection
211 replaceSelection(cur);
213 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
214 cur.nextInset()->edit(cur, true);
217 cur.message(_("Regexp editor mode"));
221 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
224 cap::replaceSelection(cur);
225 cur.insert(new InsetSpecialChar(kind));
230 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
233 cap::replaceSelection(cur);
234 cur.insert(new InsetIPAChar(kind));
239 static bool doInsertInset(Cursor & cur, Text * text,
240 FuncRequest const & cmd, bool edit, bool pastesel)
242 Buffer & buffer = cur.bv().buffer();
243 BufferParams const & bparams = buffer.params();
244 Inset * inset = createInset(&buffer, cmd);
248 if (InsetCollapsible * ci = inset->asInsetCollapsible())
249 ci->setButtonLabel();
252 if (cmd.action() == LFUN_INDEX_INSERT) {
253 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
254 text->insertInset(cur, inset);
256 inset->edit(cur, true);
257 // Now put this into inset
258 Font const f(inherit_font, cur.current_font.language());
260 cur.text()->insertStringAsLines(cur, ds, f);
261 cur.leaveInset(*inset);
265 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
266 bool cotextinsert = false;
267 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
268 Layout const & lay = cur.paragraph().layout();
269 Layout::LaTeXArgMap args = lay.args();
270 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
271 if (lait != args.end())
272 cotextinsert = (*lait).second.insertcotext;
274 InsetLayout const & il = cur.inset().getLayout();
276 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
277 if (ilait != args.end())
278 cotextinsert = (*ilait).second.insertcotext;
280 // The argument requests to insert a copy of the co-text to the inset
283 // If we have a selection within a paragraph, use this
284 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
285 ds = cur.selectionAsString(false);
286 // else use the whole paragraph
288 ds = cur.paragraph().asString();
289 text->insertInset(cur, inset);
291 inset->edit(cur, true);
292 // Now put co-text into inset
293 Font const f(inherit_font, cur.current_font.language());
295 cur.text()->insertStringAsLines(cur, ds, f);
296 cur.leaveInset(*inset);
303 if (cur.selection()) {
304 cutSelectionToTemp(cur, false, pastesel);
305 cur.clearSelection();
308 text->insertInset(cur, inset);
311 inset->edit(cur, true);
313 if (!gotsel || !pastesel)
316 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
317 cur.buffer()->errors("Paste");
318 cur.clearSelection(); // bug 393
320 InsetText * inset_text = inset->asInsetText();
322 inset_text->fixParagraphsFont();
323 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
324 // reset first par to default
325 cur.text()->paragraphs().begin()
326 ->setPlainOrDefaultLayout(bparams.documentClass());
329 // Merge multiple paragraphs -- hack
330 while (cur.lastpit() > 0)
331 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
332 if (cmd.action() == LFUN_FLEX_INSERT)
335 cur.leaveInset(*inset);
336 if (cmd.action() == LFUN_PREVIEW_INSERT
337 || cmd.action() == LFUN_IPA_INSERT)
339 notifyCursorLeavesOrEnters(old, cur);
342 cur.leaveInset(*inset);
343 // reset surrounding par to default
344 DocumentClass const & dc = bparams.documentClass();
345 docstring const layoutname = inset->usePlainLayout()
346 ? dc.plainLayoutName()
347 : dc.defaultLayoutName();
348 text->setLayout(cur, layoutname);
354 string const freefont2string()
356 return freefont.toString(toggleall);
360 /// the type of outline operation
362 OutlineUp, // Move this header with text down
363 OutlineDown, // Move this header with text up
364 OutlineIn, // Make this header deeper
365 OutlineOut // Make this header shallower
369 static void outline(OutlineOp mode, Cursor & cur)
371 Buffer & buf = *cur.buffer();
372 pit_type & pit = cur.pit();
373 ParagraphList & pars = buf.text().paragraphs();
374 ParagraphList::iterator const bgn = pars.begin();
375 // The first paragraph of the area to be copied:
376 ParagraphList::iterator start = lyx::next(bgn, pit);
377 // The final paragraph of area to be copied:
378 ParagraphList::iterator finish = start;
379 ParagraphList::iterator const end = pars.end();
381 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
384 // Move out (down) from this section header
388 // Seek the one (on same level) below
389 for (; finish != end; ++finish) {
390 toclevel = buf.text().getTocLevel(distance(bgn, finish));
391 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
397 if (start == pars.begin())
400 ParagraphList::iterator dest = start;
401 // Move out (up) from this header
404 // Search previous same-level header above
407 toclevel = buf.text().getTocLevel(distance(bgn, dest));
409 && (toclevel == Layout::NOT_IN_TOC
410 || toclevel > thistoclevel));
411 // Not found; do nothing
412 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
414 pit_type const newpit = distance(bgn, dest);
415 pit_type const len = distance(start, finish);
416 pit_type const deletepit = pit + len;
417 buf.undo().recordUndo(cur, newpit, deletepit - 1);
418 pars.splice(dest, start, finish);
426 // Go one down from *this* header:
427 ParagraphList::iterator dest = next(finish, 1);
428 // Go further down to find header to insert in front of:
429 for (; dest != end; ++dest) {
430 toclevel = buf.text().getTocLevel(distance(bgn, dest));
431 if (toclevel != Layout::NOT_IN_TOC
432 && toclevel <= thistoclevel)
435 // One such was found:
436 pit_type newpit = distance(bgn, dest);
437 buf.undo().recordUndo(cur, pit, newpit - 1);
438 pit_type const len = distance(start, finish);
439 pars.splice(dest, start, finish);
440 cur.pit() = newpit - len;
445 pit_type const len = distance(start, finish);
446 buf.undo().recordUndo(cur, pit, pit + len - 1);
447 for (; start != finish; ++start) {
448 toclevel = buf.text().getTocLevel(distance(bgn, start));
449 if (toclevel == Layout::NOT_IN_TOC)
452 DocumentClass const & tc = buf.params().documentClass();
453 DocumentClass::const_iterator lit = tc.begin();
454 DocumentClass::const_iterator len = tc.end();
455 int const newtoclevel =
456 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
457 LabelType const oldlabeltype = start->layout().labeltype;
459 for (; lit != len; ++lit) {
460 if (lit->toclevel == newtoclevel &&
461 lit->labeltype == oldlabeltype) {
462 start->setLayout(*lit);
473 void Text::number(Cursor & cur)
475 FontInfo font = ignore_font;
476 font.setNumber(FONT_TOGGLE);
477 toggleAndShow(cur, this, Font(font, ignore_language));
481 bool Text::isRTL(Paragraph const & par) const
483 Buffer const & buffer = owner_->buffer();
484 return par.isRTL(buffer.params());
490 Language const * getLanguage(Cursor const & cur, string const & lang) {
491 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
497 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
499 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
501 // Dispatch if the cursor is inside the text. It is not the
502 // case for context menus (bug 5797).
503 if (cur.text() != this) {
508 BufferView * bv = &cur.bv();
509 TextMetrics * tm = &bv->textMetrics(this);
510 if (!tm->contains(cur.pit())) {
511 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
512 tm = &bv->textMetrics(this);
515 // FIXME: We use the update flag to indicates wether a singlePar or a
516 // full screen update is needed. We reset it here but shall we restore it
518 cur.noScreenUpdate();
520 LBUFERR(this == cur.text());
522 // NOTE: This should NOT be a reference. See commit 94a5481a.
523 CursorSlice const oldTopSlice = cur.top();
524 bool const oldBoundary = cur.boundary();
525 bool const oldSelection = cur.selection();
526 // Signals that, even if needsUpdate == false, an update of the
527 // cursor paragraph is required
528 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
529 LyXAction::SingleParUpdate);
530 // Signals that a full-screen update is required
531 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
532 LyXAction::NoUpdate) || singleParUpdate);
533 bool const last_misspelled = lyxrc.spellcheck_continuously
534 && cur.paragraph().isMisspelled(cur.pos(), true);
536 FuncCode const act = cmd.action();
539 case LFUN_PARAGRAPH_MOVE_DOWN: {
540 pit_type const pit = cur.pit();
541 cur.recordUndo(pit, pit + 1);
543 pars_.swap(pit, pit + 1);
545 cur.forceBufferUpdate();
550 case LFUN_PARAGRAPH_MOVE_UP: {
551 pit_type const pit = cur.pit();
552 cur.recordUndo(pit - 1, pit);
554 pars_.swap(pit, pit - 1);
557 cur.forceBufferUpdate();
561 case LFUN_APPENDIX: {
562 Paragraph & par = cur.paragraph();
563 bool start = !par.params().startOfAppendix();
565 // FIXME: The code below only makes sense at top level.
566 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
567 // ensure that we have only one start_of_appendix in this document
568 // FIXME: this don't work for multipart document!
569 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
570 if (pars_[tmp].params().startOfAppendix()) {
571 cur.recordUndo(tmp, tmp);
572 pars_[tmp].params().startOfAppendix(false);
578 par.params().startOfAppendix(start);
580 // we can set the refreshing parameters now
581 cur.forceBufferUpdate();
585 case LFUN_WORD_DELETE_FORWARD:
587 cutSelection(cur, true, false);
589 deleteWordForward(cur, cmd.getArg(0) == "force");
590 finishChange(cur, false);
593 case LFUN_WORD_DELETE_BACKWARD:
595 cutSelection(cur, true, false);
597 deleteWordBackward(cur, cmd.getArg(0) == "force");
598 finishChange(cur, false);
601 case LFUN_LINE_DELETE_FORWARD:
603 cutSelection(cur, true, false);
605 tm->deleteLineForward(cur);
606 finishChange(cur, false);
609 case LFUN_BUFFER_BEGIN:
610 case LFUN_BUFFER_BEGIN_SELECT:
611 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
612 if (cur.depth() == 1)
613 needsUpdate |= cursorTop(cur);
616 cur.screenUpdateFlags(Update::FitCursor);
619 case LFUN_BUFFER_END:
620 case LFUN_BUFFER_END_SELECT:
621 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
622 if (cur.depth() == 1)
623 needsUpdate |= cursorBottom(cur);
626 cur.screenUpdateFlags(Update::FitCursor);
629 case LFUN_INSET_BEGIN:
630 case LFUN_INSET_BEGIN_SELECT:
631 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
632 if (cur.depth() == 1 || !cur.top().at_begin())
633 needsUpdate |= cursorTop(cur);
636 cur.screenUpdateFlags(Update::FitCursor);
640 case LFUN_INSET_END_SELECT:
641 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
642 if (cur.depth() == 1 || !cur.top().at_end())
643 needsUpdate |= cursorBottom(cur);
646 cur.screenUpdateFlags(Update::FitCursor);
649 case LFUN_CHAR_FORWARD:
650 case LFUN_CHAR_FORWARD_SELECT: {
651 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
652 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
653 bool const cur_moved = cursorForward(cur);
654 needsUpdate |= cur_moved;
656 if (!cur_moved && oldTopSlice == cur.top()
657 && cur.boundary() == oldBoundary) {
659 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
661 // we will probably be moving out the inset, so we should execute
662 // the depm-mechanism, but only when the cursor has a place to
663 // go outside this inset, i.e. in a slice above.
664 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
665 && cur.pit() == cur.lastpit()) {
666 // The cursor hasn't changed yet. To give the
667 // DEPM the possibility of doing something we must
668 // provide it with two different cursors.
670 dummy.pos() = dummy.pit() = 0;
671 if (cur.bv().checkDepm(dummy, cur))
672 cur.forceBufferUpdate();
678 case LFUN_CHAR_BACKWARD:
679 case LFUN_CHAR_BACKWARD_SELECT: {
680 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
681 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
682 bool const cur_moved = cursorBackward(cur);
683 needsUpdate |= cur_moved;
685 if (!cur_moved && oldTopSlice == cur.top()
686 && cur.boundary() == oldBoundary) {
688 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
690 // we will probably be moving out the inset, so we should execute
691 // the depm-mechanism, but only when the cursor has a place to
692 // go outside this inset, i.e. in a slice above.
693 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
694 // The cursor hasn't changed yet. To give the
695 // DEPM the possibility of doing something we must
696 // provide it with two different cursors.
698 dummy.pos() = cur.lastpos();
699 dummy.pit() = cur.lastpit();
700 if (cur.bv().checkDepm(dummy, cur))
701 cur.forceBufferUpdate();
708 case LFUN_CHAR_LEFT_SELECT:
709 if (lyxrc.visual_cursor) {
710 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
711 bool const cur_moved = cursorVisLeft(cur);
712 needsUpdate |= cur_moved;
713 if (!cur_moved && oldTopSlice == cur.top()
714 && cur.boundary() == oldBoundary) {
716 cmd = FuncRequest(LFUN_FINISHED_LEFT);
719 if (cur.reverseDirectionNeeded()) {
720 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
721 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
723 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
724 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
731 case LFUN_CHAR_RIGHT:
732 case LFUN_CHAR_RIGHT_SELECT:
733 if (lyxrc.visual_cursor) {
734 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
735 bool const cur_moved = cursorVisRight(cur);
736 needsUpdate |= cur_moved;
737 if (!cur_moved && oldTopSlice == cur.top()
738 && cur.boundary() == oldBoundary) {
740 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
743 if (cur.reverseDirectionNeeded()) {
744 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
745 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
747 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
748 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
757 case LFUN_DOWN_SELECT:
760 // stop/start the selection
761 bool select = cmd.action() == LFUN_DOWN_SELECT ||
762 cmd.action() == LFUN_UP_SELECT;
764 // move cursor up/down
765 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
766 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
768 if (!atFirstOrLastRow) {
769 needsUpdate |= cur.selHandle(select);
770 cur.upDownInText(up, needsUpdate);
771 needsUpdate |= cur.beforeDispatchCursor().inMathed();
773 pos_type newpos = up ? 0 : cur.lastpos();
774 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
775 needsUpdate |= cur.selHandle(select);
776 // we do not reset the targetx of the cursor
778 needsUpdate |= bv->checkDepm(cur, bv->cursor());
779 cur.updateTextTargetOffset();
781 cur.forceBufferUpdate();
785 // if the cursor cannot be moved up or down do not remove
786 // the selection right now, but wait for the next dispatch.
788 needsUpdate |= cur.selHandle(select);
789 cur.upDownInText(up, needsUpdate);
796 case LFUN_PARAGRAPH_UP:
797 case LFUN_PARAGRAPH_UP_SELECT:
798 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
799 needsUpdate |= cursorUpParagraph(cur);
802 case LFUN_PARAGRAPH_DOWN:
803 case LFUN_PARAGRAPH_DOWN_SELECT:
804 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
805 needsUpdate |= cursorDownParagraph(cur);
808 case LFUN_LINE_BEGIN:
809 case LFUN_LINE_BEGIN_SELECT:
810 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
811 needsUpdate |= tm->cursorHome(cur);
815 case LFUN_LINE_END_SELECT:
816 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
817 needsUpdate |= tm->cursorEnd(cur);
820 case LFUN_SECTION_SELECT: {
821 Buffer const & buf = *cur.buffer();
822 pit_type const pit = cur.pit();
823 ParagraphList & pars = buf.text().paragraphs();
824 ParagraphList::iterator bgn = pars.begin();
825 // The first paragraph of the area to be selected:
826 ParagraphList::iterator start = lyx::next(bgn, pit);
827 // The final paragraph of area to be selected:
828 ParagraphList::iterator finish = start;
829 ParagraphList::iterator end = pars.end();
831 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
832 if (thistoclevel == Layout::NOT_IN_TOC)
836 Cursor const old_cur = cur;
837 needsUpdate |= cur.selHandle(true);
839 // Move out (down) from this section header
843 // Seek the one (on same level) below
844 for (; finish != end; ++finish, ++cur.pit()) {
845 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
846 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
849 cur.pos() = cur.lastpos();
851 cur.setCurrentFont();
853 needsUpdate |= cur != old_cur;
857 case LFUN_WORD_RIGHT:
858 case LFUN_WORD_RIGHT_SELECT:
859 if (lyxrc.visual_cursor) {
860 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
861 bool const cur_moved = cursorVisRightOneWord(cur);
862 needsUpdate |= cur_moved;
863 if (!cur_moved && oldTopSlice == cur.top()
864 && cur.boundary() == oldBoundary) {
866 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
869 if (cur.reverseDirectionNeeded()) {
870 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
871 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
873 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
874 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
881 case LFUN_WORD_FORWARD:
882 case LFUN_WORD_FORWARD_SELECT: {
883 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
884 bool const cur_moved = cursorForwardOneWord(cur);
885 needsUpdate |= cur_moved;
887 if (!cur_moved && oldTopSlice == cur.top()
888 && cur.boundary() == oldBoundary) {
890 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
892 // we will probably be moving out the inset, so we should execute
893 // the depm-mechanism, but only when the cursor has a place to
894 // go outside this inset, i.e. in a slice above.
895 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
896 && cur.pit() == cur.lastpit()) {
897 // The cursor hasn't changed yet. To give the
898 // DEPM the possibility of doing something we must
899 // provide it with two different cursors.
901 dummy.pos() = dummy.pit() = 0;
902 if (cur.bv().checkDepm(dummy, cur))
903 cur.forceBufferUpdate();
910 case LFUN_WORD_LEFT_SELECT:
911 if (lyxrc.visual_cursor) {
912 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
913 bool const cur_moved = cursorVisLeftOneWord(cur);
914 needsUpdate |= cur_moved;
915 if (!cur_moved && oldTopSlice == cur.top()
916 && cur.boundary() == oldBoundary) {
918 cmd = FuncRequest(LFUN_FINISHED_LEFT);
921 if (cur.reverseDirectionNeeded()) {
922 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
923 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
925 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
926 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
933 case LFUN_WORD_BACKWARD:
934 case LFUN_WORD_BACKWARD_SELECT: {
935 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
936 bool const cur_moved = cursorBackwardOneWord(cur);
937 needsUpdate |= cur_moved;
939 if (!cur_moved && oldTopSlice == cur.top()
940 && cur.boundary() == oldBoundary) {
942 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
944 // we will probably be moving out the inset, so we should execute
945 // the depm-mechanism, but only when the cursor has a place to
946 // go outside this inset, i.e. in a slice above.
947 if (cur.depth() > 1 && cur.pos() == 0
949 // The cursor hasn't changed yet. To give the
950 // DEPM the possibility of doing something we must
951 // provide it with two different cursors.
953 dummy.pos() = cur.lastpos();
954 dummy.pit() = cur.lastpit();
955 if (cur.bv().checkDepm(dummy, cur))
956 cur.forceBufferUpdate();
962 case LFUN_WORD_SELECT: {
963 selectWord(cur, WHOLE_WORD);
964 finishChange(cur, true);
968 case LFUN_NEWLINE_INSERT: {
969 InsetNewlineParams inp;
970 docstring arg = cmd.argument();
971 if (arg == "linebreak")
972 inp.kind = InsetNewlineParams::LINEBREAK;
974 inp.kind = InsetNewlineParams::NEWLINE;
975 cap::replaceSelection(cur);
977 cur.insert(new InsetNewline(inp));
979 moveCursor(cur, false);
983 case LFUN_TAB_INSERT: {
984 bool const multi_par_selection = cur.selection() &&
985 cur.selBegin().pit() != cur.selEnd().pit();
986 if (multi_par_selection) {
987 // If there is a multi-paragraph selection, a tab is inserted
988 // at the beginning of each paragraph.
989 cur.recordUndoSelection();
990 pit_type const pit_end = cur.selEnd().pit();
991 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
992 pars_[pit].insertChar(0, '\t',
993 bv->buffer().params().track_changes);
994 // Update the selection pos to make sure the selection does not
995 // change as the inserted tab will increase the logical pos.
996 if (cur.realAnchor().pit() == pit)
997 cur.realAnchor().forwardPos();
998 if (cur.pit() == pit)
1003 // Maybe we shouldn't allow tabs within a line, because they
1004 // are not (yet) aligned as one might do expect.
1005 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
1011 case LFUN_TAB_DELETE: {
1012 bool const tc = bv->buffer().params().track_changes;
1013 if (cur.selection()) {
1014 // If there is a selection, a tab (if present) is removed from
1015 // the beginning of each paragraph.
1016 cur.recordUndoSelection();
1017 pit_type const pit_end = cur.selEnd().pit();
1018 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1019 Paragraph & par = paragraphs()[pit];
1022 char_type const c = par.getChar(0);
1023 if (c == '\t' || c == ' ') {
1024 // remove either 1 tab or 4 spaces.
1025 int const n = (c == ' ' ? 4 : 1);
1026 for (int i = 0; i < n
1027 && !par.empty() && par.getChar(0) == c; ++i) {
1028 if (cur.pit() == pit)
1030 if (cur.realAnchor().pit() == pit
1031 && cur.realAnchor().pos() > 0 )
1032 cur.realAnchor().backwardPos();
1033 par.eraseChar(0, tc);
1039 // If there is no selection, try to remove a tab or some spaces
1040 // before the position of the cursor.
1041 Paragraph & par = paragraphs()[cur.pit()];
1042 pos_type const pos = cur.pos();
1047 char_type const c = par.getChar(pos - 1);
1051 par.eraseChar(cur.pos(), tc);
1053 for (int n_spaces = 0;
1055 && par.getChar(cur.pos() - 1) == ' '
1059 par.eraseChar(cur.pos(), tc);
1066 case LFUN_CHAR_DELETE_FORWARD:
1067 if (!cur.selection()) {
1068 if (cur.pos() == cur.paragraph().size())
1069 // Par boundary, force full-screen update
1070 singleParUpdate = false;
1071 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1073 cur.selection(true);
1078 needsUpdate |= erase(cur);
1081 cutSelection(cur, true, false);
1082 singleParUpdate = false;
1084 moveCursor(cur, false);
1087 case LFUN_CHAR_DELETE_BACKWARD:
1088 if (!cur.selection()) {
1089 if (bv->getIntl().getTransManager().backspace()) {
1090 bool par_boundary = cur.pos() == 0;
1091 bool first_par = cur.pit() == 0;
1092 // Par boundary, full-screen update
1094 singleParUpdate = false;
1095 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1097 cur.selection(true);
1102 needsUpdate |= backspace(cur);
1104 if (par_boundary && !first_par && cur.pos() > 0
1105 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1106 needsUpdate |= backspace(cur);
1111 cutSelection(cur, true, false);
1112 singleParUpdate = false;
1116 case LFUN_PARAGRAPH_BREAK: {
1117 cap::replaceSelection(cur);
1118 pit_type pit = cur.pit();
1119 Paragraph const & par = pars_[pit];
1120 bool lastpar = (pit == pit_type(pars_.size() - 1));
1121 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1122 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1123 if (prev < pit && cur.pos() == par.beginOfBody()
1124 && !par.size() && !par.isEnvSeparator(cur.pos())
1125 && !par.layout().isCommand()
1126 && pars_[prev].layout() != par.layout()
1127 && pars_[prev].layout().isEnvironment()
1128 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1129 if (par.layout().isEnvironment()
1130 && pars_[prev].getDepth() == par.getDepth()) {
1131 docstring const layout = par.layout().name();
1132 DocumentClass const & tc = bv->buffer().params().documentClass();
1133 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1134 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1135 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1136 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1138 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1139 breakParagraph(cur);
1141 Font const f(inherit_font, cur.current_font.language());
1142 pars_[cur.pit() - 1].resetFonts(f);
1144 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1146 breakParagraph(cur, cmd.getArg(0) == "inverse");
1149 // If we have a list and autoinsert item insets,
1151 Layout::LaTeXArgMap args = par.layout().args();
1152 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1153 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1154 for (; lait != laend; ++lait) {
1155 Layout::latexarg arg = (*lait).second;
1156 if (arg.autoinsert && prefixIs((*lait).first, "item:")) {
1157 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1164 case LFUN_INSET_INSERT: {
1167 // We have to avoid triggering InstantPreview loading
1168 // before inserting into the document. See bug #5626.
1169 bool loaded = bv->buffer().isFullyLoaded();
1170 bv->buffer().setFullyLoaded(false);
1171 Inset * inset = createInset(&bv->buffer(), cmd);
1172 bv->buffer().setFullyLoaded(loaded);
1175 // FIXME (Abdel 01/02/2006):
1176 // What follows would be a partial fix for bug 2154:
1177 // http://www.lyx.org/trac/ticket/2154
1178 // This automatically put the label inset _after_ a
1179 // numbered section. It should be possible to extend the mechanism
1180 // to any kind of LateX environement.
1181 // The correct way to fix that bug would be at LateX generation.
1182 // I'll let the code here for reference as it could be used for some
1183 // other feature like "automatic labelling".
1185 Paragraph & par = pars_[cur.pit()];
1186 if (inset->lyxCode() == LABEL_CODE
1187 && !par.layout().counter.empty()) {
1188 // Go to the end of the paragraph
1189 // Warning: Because of Change-Tracking, the last
1190 // position is 'size()' and not 'size()-1':
1191 cur.pos() = par.size();
1192 // Insert a new paragraph
1193 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1197 if (cur.selection())
1198 cutSelection(cur, true, false);
1200 cur.forceBufferUpdate();
1201 if (inset->editable() && inset->asInsetText())
1202 inset->edit(cur, true);
1206 // trigger InstantPreview now
1207 if (inset->lyxCode() == EXTERNAL_CODE) {
1208 InsetExternal & ins =
1209 static_cast<InsetExternal &>(*inset);
1210 ins.updatePreview();
1217 case LFUN_INSET_DISSOLVE: {
1218 if (dissolveInset(cur)) {
1220 cur.forceBufferUpdate();
1225 case LFUN_SET_GRAPHICS_GROUP: {
1226 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1232 string id = to_utf8(cmd.argument());
1233 string grp = graphics::getGroupParams(bv->buffer(), id);
1234 InsetGraphicsParams tmp, inspar = ins->getParams();
1237 inspar.groupId = to_utf8(cmd.argument());
1239 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1240 tmp.filename = inspar.filename;
1244 ins->setParams(inspar);
1248 case LFUN_SPACE_INSERT:
1249 if (cur.paragraph().layout().free_spacing)
1250 insertChar(cur, ' ');
1252 doInsertInset(cur, this, cmd, false, false);
1255 moveCursor(cur, false);
1258 case LFUN_SPECIALCHAR_INSERT: {
1259 string const name = to_utf8(cmd.argument());
1260 if (name == "hyphenation")
1261 specialChar(cur, InsetSpecialChar::HYPHENATION);
1262 else if (name == "allowbreak")
1263 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1264 else if (name == "ligature-break")
1265 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1266 else if (name == "slash")
1267 specialChar(cur, InsetSpecialChar::SLASH);
1268 else if (name == "nobreakdash")
1269 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1270 else if (name == "dots")
1271 specialChar(cur, InsetSpecialChar::LDOTS);
1272 else if (name == "end-of-sentence")
1273 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1274 else if (name == "menu-separator")
1275 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1276 else if (name == "lyx")
1277 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1278 else if (name == "tex")
1279 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1280 else if (name == "latex")
1281 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1282 else if (name == "latex2e")
1283 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1284 else if (name.empty())
1285 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1287 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1291 case LFUN_IPAMACRO_INSERT: {
1292 string const arg = cmd.getArg(0);
1293 if (arg == "deco") {
1294 // Open the inset, and move the current selection
1296 doInsertInset(cur, this, cmd, true, true);
1298 // Some insets are numbered, others are shown in the outline pane so
1299 // let's update the labels and the toc backend.
1300 cur.forceBufferUpdate();
1303 if (arg == "tone-falling")
1304 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1305 else if (arg == "tone-rising")
1306 ipaChar(cur, InsetIPAChar::TONE_RISING);
1307 else if (arg == "tone-high-rising")
1308 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1309 else if (arg == "tone-low-rising")
1310 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1311 else if (arg == "tone-high-rising-falling")
1312 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1313 else if (arg.empty())
1314 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1316 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1320 case LFUN_WORD_UPCASE:
1321 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1324 case LFUN_WORD_LOWCASE:
1325 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1328 case LFUN_WORD_CAPITALIZE:
1329 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1332 case LFUN_CHARS_TRANSPOSE:
1333 charsTranspose(cur);
1337 cur.message(_("Paste"));
1338 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1339 cap::replaceSelection(cur);
1341 // without argument?
1342 string const arg = to_utf8(cmd.argument());
1344 bool tryGraphics = true;
1345 if (theClipboard().isInternal())
1346 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1347 else if (theClipboard().hasTextContents()) {
1348 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1349 true, Clipboard::AnyTextType))
1350 tryGraphics = false;
1352 if (tryGraphics && theClipboard().hasGraphicsContents())
1353 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1354 } else if (isStrUnsignedInt(arg)) {
1355 // we have a numerical argument
1356 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1357 convert<unsigned int>(arg));
1358 } else if (arg == "html" || arg == "latex") {
1359 Clipboard::TextType type = (arg == "html") ?
1360 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1361 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1363 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1365 type = Clipboard::PdfGraphicsType;
1366 else if (arg == "png")
1367 type = Clipboard::PngGraphicsType;
1368 else if (arg == "jpeg")
1369 type = Clipboard::JpegGraphicsType;
1370 else if (arg == "linkback")
1371 type = Clipboard::LinkBackGraphicsType;
1372 else if (arg == "emf")
1373 type = Clipboard::EmfGraphicsType;
1374 else if (arg == "wmf")
1375 type = Clipboard::WmfGraphicsType;
1377 // we also check in getStatus()
1378 LYXERR0("Unrecognized graphics type: " << arg);
1380 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1383 bv->buffer().errors("Paste");
1384 cur.clearSelection(); // bug 393
1390 cutSelection(cur, true, true);
1391 cur.message(_("Cut"));
1396 cur.message(_("Copy"));
1399 case LFUN_SERVER_GET_XY:
1400 cur.message(from_utf8(
1401 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1402 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1405 case LFUN_SERVER_SET_XY: {
1408 istringstream is(to_utf8(cmd.argument()));
1411 lyxerr << "SETXY: Could not parse coordinates in '"
1412 << to_utf8(cmd.argument()) << endl;
1414 tm->setCursorFromCoordinates(cur, x, y);
1418 case LFUN_SERVER_GET_LAYOUT:
1419 cur.message(cur.paragraph().layout().name());
1423 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1424 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1425 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1427 Paragraph const & para = cur.paragraph();
1428 docstring const old_layout = para.layout().name();
1429 DocumentClass const & tclass = bv->buffer().params().documentClass();
1432 layout = tclass.defaultLayoutName();
1434 if (owner_->forcePlainLayout())
1435 // in this case only the empty layout is allowed
1436 layout = tclass.plainLayoutName();
1437 else if (para.usePlainLayout()) {
1438 // in this case, default layout maps to empty layout
1439 if (layout == tclass.defaultLayoutName())
1440 layout = tclass.plainLayoutName();
1442 // otherwise, the empty layout maps to the default
1443 if (layout == tclass.plainLayoutName())
1444 layout = tclass.defaultLayoutName();
1447 bool hasLayout = tclass.hasLayout(layout);
1449 // If the entry is obsolete, use the new one instead.
1451 docstring const & obs = tclass[layout].obsoleted_by();
1457 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1458 from_utf8(N_(" not known")));
1462 bool change_layout = (old_layout != layout);
1464 if (!change_layout && cur.selection() &&
1465 cur.selBegin().pit() != cur.selEnd().pit())
1467 pit_type spit = cur.selBegin().pit();
1468 pit_type epit = cur.selEnd().pit() + 1;
1469 while (spit != epit) {
1470 if (pars_[spit].layout().name() != old_layout) {
1471 change_layout = true;
1478 if (change_layout) {
1479 setLayout(cur, layout);
1480 if (cur.pit() > 0 && !ignoreautonests) {
1481 set<docstring> const & autonests =
1482 pars_[cur.pit() - 1].layout().autonests();
1483 set<docstring> const & autonested =
1484 pars_[cur.pit()].layout().isAutonestedBy();
1485 if (autonests.find(layout) != autonests.end()
1486 || autonested.find(old_layout) != autonested.end())
1487 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1491 Layout::LaTeXArgMap args = tclass[layout].args();
1492 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1493 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1494 for (; lait != laend; ++lait) {
1495 Layout::latexarg arg = (*lait).second;
1496 if (arg.autoinsert) {
1497 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1505 case LFUN_ENVIRONMENT_SPLIT: {
1506 bool const outer = cmd.argument() == "outer";
1507 bool const previous = cmd.argument() == "previous";
1508 bool const before = cmd.argument() == "before";
1509 bool const normal = cmd.argument().empty();
1510 Paragraph const & para = cur.paragraph();
1512 if (para.layout().isEnvironment())
1513 layout = para.layout().name();
1514 depth_type split_depth = cur.paragraph().params().depth();
1515 depth_type nextpar_depth = 0;
1516 if (outer || previous) {
1517 // check if we have an environment in our scope
1518 pit_type pit = cur.pit();
1519 Paragraph cpar = pars_[pit];
1525 if (layout.empty() && previous
1526 && cpar.layout().isEnvironment()
1527 && cpar.params().depth() <= split_depth)
1528 layout = cpar.layout().name();
1529 if (cpar.params().depth() < split_depth
1530 && cpar.layout().isEnvironment()) {
1532 layout = cpar.layout().name();
1533 split_depth = cpar.params().depth();
1535 if (cpar.params().depth() == 0)
1539 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1540 // save nesting of following paragraph
1541 Paragraph cpar = pars_[cur.pit() + 1];
1542 nextpar_depth = cpar.params().depth();
1545 cur.top().setPitPos(cur.pit(), 0);
1546 if (before || cur.pos() > 0)
1547 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1548 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1549 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1551 while (cur.paragraph().params().depth() > split_depth)
1552 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1554 DocumentClass const & tc = bv->buffer().params().documentClass();
1555 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1556 + from_ascii("\" ignoreautonests")));
1557 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1560 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1561 while (cur.paragraph().params().depth() < split_depth)
1562 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1565 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1566 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1567 if ((outer || normal) && nextpar_depth > 0) {
1568 // restore nesting of following paragraph
1569 DocIterator scur = cur;
1570 depth_type const max_depth = cur.paragraph().getMaxDepthAfter();
1572 while (cur.paragraph().params().depth() <= min(nextpar_depth, max_depth))
1573 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1574 cur.setCursor(scur);
1580 case LFUN_CLIPBOARD_PASTE:
1581 cap::replaceSelection(cur);
1582 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1583 cmd.argument() == "paragraph");
1584 bv->buffer().errors("Paste");
1587 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1588 cap::replaceSelection(cur);
1589 pasteSimpleText(cur, cmd.argument() == "paragraph");
1592 case LFUN_PRIMARY_SELECTION_PASTE:
1593 cap::replaceSelection(cur);
1594 pasteString(cur, theSelection().get(),
1595 cmd.argument() == "paragraph");
1598 case LFUN_SELECTION_PASTE:
1599 // Copy the selection buffer to the clipboard stack,
1600 // because we want it to appear in the "Edit->Paste
1602 cap::replaceSelection(cur);
1603 cap::copySelectionToStack();
1604 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1605 bv->buffer().errors("Paste");
1608 case LFUN_UNICODE_INSERT: {
1609 if (cmd.argument().empty())
1611 docstring hexstring = cmd.argument();
1612 if (isHex(hexstring)) {
1613 char_type c = hexToInt(hexstring);
1614 if (c >= 32 && c < 0x10ffff) {
1615 lyxerr << "Inserting c: " << c << endl;
1616 docstring s = docstring(1, c);
1617 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1623 case LFUN_QUOTE_INSERT: {
1624 cap::replaceSelection(cur);
1627 Paragraph const & par = cur.paragraph();
1628 pos_type pos = cur.pos();
1629 // Ignore deleted text before cursor
1630 while (pos > 0 && par.isDeleted(pos - 1))
1633 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1635 // Guess quote side.
1636 // A space triggers an opening quote. This is passed if the preceding
1637 // char/inset is a space or at paragraph start.
1639 if (pos > 0 && !par.isSpace(pos - 1)) {
1640 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1641 // If an opening double quotation mark precedes, and this
1642 // is a single quote, make it opening as well
1644 static_cast<InsetQuotes &>(*cur.prevInset());
1645 string const type = ins.getType();
1646 if (!suffixIs(type, "ld") || !inner)
1647 c = par.getChar(pos - 1);
1649 else if (!cur.prevInset()
1650 || (cur.prevInset() && cur.prevInset()->isChar()))
1651 // If a char precedes, pass that and let InsetQuote decide
1652 c = par.getChar(pos - 1);
1655 if (par.getInset(pos - 1)
1656 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1657 // skip "invisible" insets
1661 c = par.getChar(pos - 1);
1666 InsetQuotesParams::QuoteLevel const quote_level = inner
1667 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1668 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1669 cur.buffer()->updateBuffer();
1674 case LFUN_DATE_INSERT: {
1675 string const format = cmd.argument().empty()
1676 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1677 string const time = formatted_time(current_time(), format);
1678 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1682 case LFUN_MOUSE_TRIPLE:
1683 if (cmd.button() == mouse_button::button1) {
1684 tm->cursorHome(cur);
1692 case LFUN_MOUSE_DOUBLE:
1693 if (cmd.button() == mouse_button::button1) {
1694 selectWord(cur, WHOLE_WORD);
1699 // Single-click on work area
1700 case LFUN_MOUSE_PRESS: {
1701 // We are not marking a selection with the keyboard in any case.
1702 Cursor & bvcur = cur.bv().cursor();
1703 bvcur.setMark(false);
1704 switch (cmd.button()) {
1705 case mouse_button::button1:
1706 if (!bvcur.selection())
1708 bvcur.resetAnchor();
1709 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1710 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1711 if (bvcur.wordSelection())
1712 selectWord(bvcur, WHOLE_WORD);
1715 case mouse_button::button2:
1716 if (lyxrc.mouse_middlebutton_paste) {
1717 // Middle mouse pasting.
1718 bv->mouseSetCursor(cur);
1720 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1721 "selection-paste ; primary-selection-paste paragraph"));
1723 cur.noScreenUpdate();
1726 case mouse_button::button3: {
1727 // Don't do anything if we right-click a
1728 // selection, a context menu will popup.
1729 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1730 && cur < bvcur.selectionEnd()) {
1731 cur.noScreenUpdate();
1734 if (!bv->mouseSetCursor(cur, false))
1735 cur.screenUpdateFlags(Update::FitCursor);
1741 } // switch (cmd.button())
1744 case LFUN_MOUSE_MOTION: {
1745 // Mouse motion with right or middle mouse do nothing for now.
1746 if (cmd.button() != mouse_button::button1) {
1747 cur.noScreenUpdate();
1750 // ignore motions deeper nested than the real anchor
1751 Cursor & bvcur = cur.bv().cursor();
1752 if (!bvcur.realAnchor().hasPart(cur)) {
1756 CursorSlice old = bvcur.top();
1758 int const wh = bv->workHeight();
1759 int const y = max(0, min(wh - 1, cmd.y()));
1761 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1762 cur.setTargetX(cmd.x());
1763 // Don't allow selecting a separator inset
1764 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1767 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1768 else if (cmd.y() < 0)
1769 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1770 // This is to allow jumping over large insets
1771 if (cur.top() == old) {
1773 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1774 else if (cmd.y() < 0)
1775 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1777 // We continue with our existing selection or start a new one, so don't
1778 // reset the anchor.
1779 bvcur.setCursor(cur);
1780 bvcur.selection(true);
1781 bvcur.setCurrentFont();
1782 if (cur.top() == old) {
1783 // We didn't move one iota, so no need to update the screen.
1784 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1785 //cur.noScreenUpdate();
1791 case LFUN_MOUSE_RELEASE:
1792 switch (cmd.button()) {
1793 case mouse_button::button1:
1794 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1795 // If there is a new selection, update persistent selection;
1796 // otherwise, single click does not clear persistent selection
1798 if (cur.selection()) {
1799 // Finish selection. If double click,
1800 // cur is moved to the end of word by
1801 // selectWord but bvcur is current
1803 cur.bv().cursor().setSelection();
1804 // We might have removed an empty but drawn selection
1805 // (probably a margin)
1806 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1808 cur.noScreenUpdate();
1809 // FIXME: We could try to handle drag and drop of selection here.
1812 case mouse_button::button2:
1813 // Middle mouse pasting is handled at mouse press time,
1814 // see LFUN_MOUSE_PRESS.
1815 cur.noScreenUpdate();
1818 case mouse_button::button3:
1819 // Cursor was set at LFUN_MOUSE_PRESS time.
1820 // FIXME: If there is a selection we could try to handle a special
1821 // drag & drop context menu.
1822 cur.noScreenUpdate();
1825 case mouse_button::none:
1826 case mouse_button::button4:
1827 case mouse_button::button5:
1829 } // switch (cmd.button())
1833 case LFUN_SELF_INSERT: {
1834 if (cmd.argument().empty())
1837 // Automatically delete the currently selected
1838 // text and replace it with what is being
1839 // typed in now. Depends on lyxrc settings
1840 // "auto_region_delete", which defaults to
1843 if (lyxrc.auto_region_delete && cur.selection())
1844 cutSelection(cur, false, false);
1846 cur.clearSelection();
1848 docstring::const_iterator cit = cmd.argument().begin();
1849 docstring::const_iterator const end = cmd.argument().end();
1850 for (; cit != end; ++cit)
1851 bv->translateAndInsert(*cit, this, cur);
1854 moveCursor(cur, false);
1855 cur.markNewWordPosition();
1856 bv->bookmarkEditPosition();
1860 case LFUN_HREF_INSERT: {
1861 docstring content = cmd.argument();
1862 if (content.empty() && cur.selection())
1863 content = cur.selectionAsString(false);
1865 InsetCommandParams p(HYPERLINK_CODE);
1866 if (!content.empty()){
1867 // if it looks like a link, we'll put it as target,
1868 // otherwise as name (bug #8792).
1871 // regex_match(to_utf8(content), matches, link_re)
1872 // because smatch stores pointers to the substrings rather
1873 // than making copies of them. And those pointers become
1874 // invalid after regex_match returns, since it is then
1875 // being given a temporary object. (Thanks to Georg for
1876 // figuring that out.)
1877 regex const link_re("^([a-z]+):.*");
1879 string const c = to_utf8(lowercase(content));
1881 if (c.substr(0,7) == "mailto:") {
1882 p["target"] = content;
1883 p["type"] = from_ascii("mailto:");
1884 } else if (regex_match(c, matches, link_re)) {
1885 p["target"] = content;
1886 string protocol = matches.str(1);
1887 if (protocol == "file")
1888 p["type"] = from_ascii("file:");
1890 p["name"] = content;
1892 string const data = InsetCommand::params2string(p);
1894 // we need to have a target. if we already have one, then
1895 // that gets used at the default for the name, too, which
1896 // is probably what is wanted.
1897 if (p["target"].empty()) {
1898 bv->showDialog("href", data);
1900 FuncRequest fr(LFUN_INSET_INSERT, data);
1906 case LFUN_LABEL_INSERT: {
1907 InsetCommandParams p(LABEL_CODE);
1908 // Try to generate a valid label
1909 p["name"] = (cmd.argument().empty()) ?
1910 cur.getPossibleLabel() :
1912 string const data = InsetCommand::params2string(p);
1914 if (cmd.argument().empty()) {
1915 bv->showDialog("label", data);
1917 FuncRequest fr(LFUN_INSET_INSERT, data);
1923 case LFUN_INFO_INSERT: {
1925 if (cmd.argument().empty() && cur.selection()) {
1926 // if command argument is empty use current selection as parameter.
1927 docstring ds = cur.selectionAsString(false);
1928 cutSelection(cur, true, false);
1929 FuncRequest cmd0(cmd, ds);
1930 inset = createInset(cur.buffer(), cmd0);
1932 inset = createInset(cur.buffer(), cmd);
1937 insertInset(cur, inset);
1938 cur.forceBufferUpdate();
1942 case LFUN_CAPTION_INSERT:
1943 case LFUN_FOOTNOTE_INSERT:
1944 case LFUN_NOTE_INSERT:
1945 case LFUN_BOX_INSERT:
1946 case LFUN_BRANCH_INSERT:
1947 case LFUN_PHANTOM_INSERT:
1948 case LFUN_ERT_INSERT:
1949 case LFUN_LISTING_INSERT:
1950 case LFUN_MARGINALNOTE_INSERT:
1951 case LFUN_ARGUMENT_INSERT:
1952 case LFUN_INDEX_INSERT:
1953 case LFUN_PREVIEW_INSERT:
1954 case LFUN_SCRIPT_INSERT:
1955 case LFUN_IPA_INSERT:
1956 // Open the inset, and move the current selection
1958 doInsertInset(cur, this, cmd, true, true);
1960 // Some insets are numbered, others are shown in the outline pane so
1961 // let's update the labels and the toc backend.
1962 cur.forceBufferUpdate();
1965 case LFUN_FLEX_INSERT: {
1966 // Open the inset, and move the current selection
1968 bool const sel = cur.selection();
1969 doInsertInset(cur, this, cmd, true, true);
1970 // Insert auto-insert arguments
1971 bool autoargs = false;
1972 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1973 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1974 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1975 for (; lait != laend; ++lait) {
1976 Layout::latexarg arg = (*lait).second;
1977 if (arg.autoinsert) {
1978 // The cursor might have been invalidated by the replaceSelection.
1979 cur.buffer()->changed(true);
1980 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1987 cur.leaveInset(cur.inset());
1990 // Some insets are numbered, others are shown in the outline pane so
1991 // let's update the labels and the toc backend.
1992 cur.forceBufferUpdate();
1996 case LFUN_TABULAR_INSERT:
1997 // if there were no arguments, just open the dialog
1998 if (doInsertInset(cur, this, cmd, false, true))
2001 bv->showDialog("tabularcreate");
2005 case LFUN_FLOAT_INSERT:
2006 case LFUN_FLOAT_WIDE_INSERT:
2007 case LFUN_WRAP_INSERT: {
2008 // will some content be moved into the inset?
2009 bool const content = cur.selection();
2010 // does the content consist of multiple paragraphs?
2011 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2013 doInsertInset(cur, this, cmd, true, true);
2016 // If some single-par content is moved into the inset,
2017 // doInsertInset puts the cursor outside the inset.
2018 // To insert the caption we put it back into the inset.
2019 // FIXME cleanup doInsertInset to avoid such dances!
2020 if (content && singlepar)
2023 ParagraphList & pars = cur.text()->paragraphs();
2025 DocumentClass const & tclass = bv->buffer().params().documentClass();
2027 // add a separate paragraph for the caption inset
2028 pars.push_back(Paragraph());
2029 pars.back().setInsetOwner(&cur.text()->inset());
2030 pars.back().setPlainOrDefaultLayout(tclass);
2031 int cap_pit = pars.size() - 1;
2033 // if an empty inset was created, we create an additional empty
2034 // paragraph at the bottom so that the user can choose where to put
2035 // the graphics (or table).
2037 pars.push_back(Paragraph());
2038 pars.back().setInsetOwner(&cur.text()->inset());
2039 pars.back().setPlainOrDefaultLayout(tclass);
2042 // reposition the cursor to the caption
2043 cur.pit() = cap_pit;
2045 // FIXME: This Text/Cursor dispatch handling is a mess!
2046 // We cannot use Cursor::dispatch here it needs access to up to
2048 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2049 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2050 cur.forceBufferUpdate();
2051 cur.screenUpdateFlags(Update::Force);
2052 // FIXME: When leaving the Float (or Wrap) inset we should
2053 // delete any empty paragraph left above or below the
2058 case LFUN_NOMENCL_INSERT: {
2059 InsetCommandParams p(NOMENCL_CODE);
2060 if (cmd.argument().empty())
2061 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
2063 p["symbol"] = cmd.argument();
2064 string const data = InsetCommand::params2string(p);
2065 bv->showDialog("nomenclature", data);
2069 case LFUN_INDEX_PRINT: {
2070 InsetCommandParams p(INDEX_PRINT_CODE);
2071 if (cmd.argument().empty())
2072 p["type"] = from_ascii("idx");
2074 p["type"] = cmd.argument();
2075 string const data = InsetCommand::params2string(p);
2076 FuncRequest fr(LFUN_INSET_INSERT, data);
2081 case LFUN_NOMENCL_PRINT:
2082 case LFUN_NEWPAGE_INSERT:
2084 doInsertInset(cur, this, cmd, false, false);
2088 case LFUN_SEPARATOR_INSERT: {
2089 doInsertInset(cur, this, cmd, false, false);
2091 // remove a following space
2092 Paragraph & par = cur.paragraph();
2093 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2094 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2098 case LFUN_DEPTH_DECREMENT:
2099 changeDepth(cur, DEC_DEPTH);
2102 case LFUN_DEPTH_INCREMENT:
2103 changeDepth(cur, INC_DEPTH);
2106 case LFUN_REGEXP_MODE:
2107 regexpDispatch(cur, cmd);
2110 case LFUN_MATH_MODE: {
2111 if (cmd.argument() == "on" || cmd.argument() == "") {
2112 // don't pass "on" as argument
2113 // (it would appear literally in the first cell)
2114 docstring sel = cur.selectionAsString(false);
2115 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2116 // create a macro template if we see "\\newcommand" somewhere, and
2117 // an ordinary formula otherwise
2119 && (sel.find(from_ascii("\\newcommand")) != string::npos
2120 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2121 || sel.find(from_ascii("\\def")) != string::npos)
2122 && macro->fromString(sel)) {
2124 replaceSelection(cur);
2127 // no meaningful macro template was found
2129 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2132 // The argument is meaningful
2133 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2134 // has a different meaning in math mode
2135 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2139 case LFUN_MATH_MACRO:
2140 if (cmd.argument().empty())
2141 cur.errorMessage(from_utf8(N_("Missing argument")));
2144 string s = to_utf8(cmd.argument());
2145 string const s1 = token(s, ' ', 1);
2146 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2147 string const s2 = token(s, ' ', 2);
2148 MacroType type = MacroTypeNewcommand;
2150 type = MacroTypeDef;
2151 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2152 from_utf8(token(s, ' ', 0)), nargs, false, type);
2153 inset->setBuffer(bv->buffer());
2154 insertInset(cur, inset);
2156 // enter macro inset and select the name
2158 cur.top().pos() = cur.top().lastpos();
2160 cur.selection(true);
2161 cur.top().pos() = 0;
2165 case LFUN_MATH_DISPLAY:
2166 case LFUN_MATH_SUBSCRIPT:
2167 case LFUN_MATH_SUPERSCRIPT:
2168 case LFUN_MATH_INSERT:
2169 case LFUN_MATH_AMS_MATRIX:
2170 case LFUN_MATH_MATRIX:
2171 case LFUN_MATH_DELIM:
2172 case LFUN_MATH_BIGDELIM:
2173 mathDispatch(cur, cmd);
2176 case LFUN_FONT_EMPH: {
2177 Font font(ignore_font, ignore_language);
2178 font.fontInfo().setEmph(FONT_TOGGLE);
2179 toggleAndShow(cur, this, font);
2183 case LFUN_FONT_ITAL: {
2184 Font font(ignore_font, ignore_language);
2185 font.fontInfo().setShape(ITALIC_SHAPE);
2186 toggleAndShow(cur, this, font);
2190 case LFUN_FONT_BOLD:
2191 case LFUN_FONT_BOLDSYMBOL: {
2192 Font font(ignore_font, ignore_language);
2193 font.fontInfo().setSeries(BOLD_SERIES);
2194 toggleAndShow(cur, this, font);
2198 case LFUN_FONT_NOUN: {
2199 Font font(ignore_font, ignore_language);
2200 font.fontInfo().setNoun(FONT_TOGGLE);
2201 toggleAndShow(cur, this, font);
2205 case LFUN_FONT_TYPEWRITER: {
2206 Font font(ignore_font, ignore_language);
2207 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2208 toggleAndShow(cur, this, font);
2212 case LFUN_FONT_SANS: {
2213 Font font(ignore_font, ignore_language);
2214 font.fontInfo().setFamily(SANS_FAMILY);
2215 toggleAndShow(cur, this, font);
2219 case LFUN_FONT_ROMAN: {
2220 Font font(ignore_font, ignore_language);
2221 font.fontInfo().setFamily(ROMAN_FAMILY);
2222 toggleAndShow(cur, this, font);
2226 case LFUN_FONT_DEFAULT: {
2227 Font font(inherit_font, ignore_language);
2228 toggleAndShow(cur, this, font);
2232 case LFUN_FONT_STRIKEOUT: {
2233 Font font(ignore_font, ignore_language);
2234 font.fontInfo().setStrikeout(FONT_TOGGLE);
2235 toggleAndShow(cur, this, font);
2239 case LFUN_FONT_CROSSOUT: {
2240 Font font(ignore_font, ignore_language);
2241 font.fontInfo().setXout(FONT_TOGGLE);
2242 toggleAndShow(cur, this, font);
2246 case LFUN_FONT_UNDERUNDERLINE: {
2247 Font font(ignore_font, ignore_language);
2248 font.fontInfo().setUuline(FONT_TOGGLE);
2249 toggleAndShow(cur, this, font);
2253 case LFUN_FONT_UNDERWAVE: {
2254 Font font(ignore_font, ignore_language);
2255 font.fontInfo().setUwave(FONT_TOGGLE);
2256 toggleAndShow(cur, this, font);
2260 case LFUN_FONT_UNDERLINE: {
2261 Font font(ignore_font, ignore_language);
2262 font.fontInfo().setUnderbar(FONT_TOGGLE);
2263 toggleAndShow(cur, this, font);
2267 case LFUN_FONT_SIZE: {
2268 Font font(ignore_font, ignore_language);
2269 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2270 toggleAndShow(cur, this, font);
2274 case LFUN_LANGUAGE: {
2275 string const lang_arg = cmd.getArg(0);
2276 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2277 Language const * lang =
2278 reset ? reset_language
2279 : languages.getLanguage(lang_arg);
2280 // we allow reset_language, which is 0, but only if it
2281 // was requested via empty or "reset" arg.
2282 if (!lang && !reset)
2284 bool const toggle = (cmd.getArg(1) != "set");
2285 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2286 Font font(ignore_font, lang);
2287 toggleAndShow(cur, this, font, toggle);
2291 case LFUN_TEXTSTYLE_APPLY:
2292 toggleAndShow(cur, this, freefont, toggleall);
2293 cur.message(_("Character set"));
2296 // Set the freefont using the contents of \param data dispatched from
2297 // the frontends and apply it at the current cursor location.
2298 case LFUN_TEXTSTYLE_UPDATE: {
2301 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2304 toggleAndShow(cur, this, freefont, toggleall);
2305 cur.message(_("Character set"));
2307 lyxerr << "Argument not ok";
2312 case LFUN_FINISHED_LEFT:
2313 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2314 // We're leaving an inset, going left. If the inset is LTR, we're
2315 // leaving from the front, so we should not move (remain at --- but
2316 // not in --- the inset). If the inset is RTL, move left, without
2317 // entering the inset itself; i.e., move to after the inset.
2318 if (cur.paragraph().getFontSettings(
2319 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2320 cursorVisLeft(cur, true);
2323 case LFUN_FINISHED_RIGHT:
2324 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2325 // We're leaving an inset, going right. If the inset is RTL, we're
2326 // leaving from the front, so we should not move (remain at --- but
2327 // not in --- the inset). If the inset is LTR, move right, without
2328 // entering the inset itself; i.e., move to after the inset.
2329 if (!cur.paragraph().getFontSettings(
2330 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2331 cursorVisRight(cur, true);
2334 case LFUN_FINISHED_BACKWARD:
2335 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2336 cur.setCurrentFont();
2339 case LFUN_FINISHED_FORWARD:
2340 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2342 cur.setCurrentFont();
2345 case LFUN_LAYOUT_PARAGRAPH: {
2347 params2string(cur.paragraph(), data);
2348 data = "show\n" + data;
2349 bv->showDialog("paragraph", data);
2353 case LFUN_PARAGRAPH_UPDATE: {
2355 params2string(cur.paragraph(), data);
2357 // Will the paragraph accept changes from the dialog?
2359 cur.inset().allowParagraphCustomization(cur.idx());
2361 data = "update " + convert<string>(accept) + '\n' + data;
2362 bv->updateDialog("paragraph", data);
2366 case LFUN_ACCENT_UMLAUT:
2367 case LFUN_ACCENT_CIRCUMFLEX:
2368 case LFUN_ACCENT_GRAVE:
2369 case LFUN_ACCENT_ACUTE:
2370 case LFUN_ACCENT_TILDE:
2371 case LFUN_ACCENT_PERISPOMENI:
2372 case LFUN_ACCENT_CEDILLA:
2373 case LFUN_ACCENT_MACRON:
2374 case LFUN_ACCENT_DOT:
2375 case LFUN_ACCENT_UNDERDOT:
2376 case LFUN_ACCENT_UNDERBAR:
2377 case LFUN_ACCENT_CARON:
2378 case LFUN_ACCENT_BREVE:
2379 case LFUN_ACCENT_TIE:
2380 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2381 case LFUN_ACCENT_CIRCLE:
2382 case LFUN_ACCENT_OGONEK:
2383 theApp()->handleKeyFunc(cmd.action());
2384 if (!cmd.argument().empty())
2385 // FIXME: Are all these characters encoded in one byte in utf8?
2386 bv->translateAndInsert(cmd.argument()[0], this, cur);
2387 cur.screenUpdateFlags(Update::FitCursor);
2390 case LFUN_FLOAT_LIST_INSERT: {
2391 DocumentClass const & tclass = bv->buffer().params().documentClass();
2392 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2394 if (cur.selection())
2395 cutSelection(cur, true, false);
2396 breakParagraph(cur);
2398 if (cur.lastpos() != 0) {
2399 cursorBackward(cur);
2400 breakParagraph(cur);
2403 docstring const laystr = cur.inset().usePlainLayout() ?
2404 tclass.plainLayoutName() :
2405 tclass.defaultLayoutName();
2406 setLayout(cur, laystr);
2407 ParagraphParameters p;
2408 // FIXME If this call were replaced with one to clearParagraphParams(),
2409 // then we could get rid of this method altogether.
2410 setParagraphs(cur, p);
2411 // FIXME This should be simplified when InsetFloatList takes a
2412 // Buffer in its constructor.
2413 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2414 ifl->setBuffer(bv->buffer());
2415 insertInset(cur, ifl);
2418 lyxerr << "Non-existent float type: "
2419 << to_utf8(cmd.argument()) << endl;
2424 case LFUN_CHANGE_ACCEPT: {
2425 acceptOrRejectChanges(cur, ACCEPT);
2429 case LFUN_CHANGE_REJECT: {
2430 acceptOrRejectChanges(cur, REJECT);
2434 case LFUN_THESAURUS_ENTRY: {
2435 Language const * language = cur.getFont().language();
2436 docstring arg = cmd.argument();
2438 arg = cur.selectionAsString(false);
2440 if (arg.size() > 100 || arg.empty()) {
2441 // Get word or selection
2442 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2443 arg = cur.selectionAsString(false);
2444 arg += " lang=" + from_ascii(language->lang());
2447 string lang = cmd.getArg(1);
2448 // This duplicates the code in GuiThesaurus::initialiseParams
2449 if (prefixIs(lang, "lang=")) {
2450 language = languages.getLanguage(lang.substr(5));
2452 language = cur.getFont().language();
2455 string lang = language->code();
2456 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2457 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2458 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2459 _("The path to the thesaurus directory has not been specified.\n"
2460 "The thesaurus is not functional.\n"
2461 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2464 bv->showDialog("thesaurus", to_utf8(arg));
2468 case LFUN_SPELLING_ADD: {
2469 Language const * language = getLanguage(cur, cmd.getArg(1));
2470 docstring word = from_utf8(cmd.getArg(0));
2472 word = cur.selectionAsString(false);
2474 if (word.size() > 100 || word.empty()) {
2475 // Get word or selection
2476 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2477 word = cur.selectionAsString(false);
2480 WordLangTuple wl(word, language);
2481 theSpellChecker()->insert(wl);
2485 case LFUN_SPELLING_IGNORE: {
2486 Language const * language = getLanguage(cur, cmd.getArg(1));
2487 docstring word = from_utf8(cmd.getArg(0));
2489 word = cur.selectionAsString(false);
2491 if (word.size() > 100 || word.empty()) {
2492 // Get word or selection
2493 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2494 word = cur.selectionAsString(false);
2497 WordLangTuple wl(word, language);
2498 theSpellChecker()->accept(wl);
2502 case LFUN_SPELLING_REMOVE: {
2503 Language const * language = getLanguage(cur, cmd.getArg(1));
2504 docstring word = from_utf8(cmd.getArg(0));
2506 word = cur.selectionAsString(false);
2508 if (word.size() > 100 || word.empty()) {
2509 // Get word or selection
2510 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2511 word = cur.selectionAsString(false);
2514 WordLangTuple wl(word, language);
2515 theSpellChecker()->remove(wl);
2519 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2520 // Given data, an encoding of the ParagraphParameters
2521 // generated in the Paragraph dialog, this function sets
2522 // the current paragraph, or currently selected paragraphs,
2524 // NOTE: This function overrides all existing settings.
2525 setParagraphs(cur, cmd.argument());
2526 cur.message(_("Paragraph layout set"));
2530 case LFUN_PARAGRAPH_PARAMS: {
2531 // Given data, an encoding of the ParagraphParameters as we'd
2532 // find them in a LyX file, this function modifies the current paragraph,
2533 // or currently selected paragraphs.
2534 // NOTE: This function only modifies, and does not override, existing
2536 setParagraphs(cur, cmd.argument(), true);
2537 cur.message(_("Paragraph layout set"));
2542 if (cur.selection()) {
2543 cur.selection(false);
2546 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2547 // correct, but I'm not 100% sure -- dov, 071019
2548 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2552 case LFUN_OUTLINE_UP:
2553 outline(OutlineUp, cur);
2554 setCursor(cur, cur.pit(), 0);
2555 cur.forceBufferUpdate();
2559 case LFUN_OUTLINE_DOWN:
2560 outline(OutlineDown, cur);
2561 setCursor(cur, cur.pit(), 0);
2562 cur.forceBufferUpdate();
2566 case LFUN_OUTLINE_IN:
2567 outline(OutlineIn, cur);
2568 cur.forceBufferUpdate();
2572 case LFUN_OUTLINE_OUT:
2573 outline(OutlineOut, cur);
2574 cur.forceBufferUpdate();
2578 case LFUN_SERVER_GET_STATISTICS:
2580 DocIterator from, to;
2581 if (cur.selection()) {
2582 from = cur.selectionBegin();
2583 to = cur.selectionEnd();
2585 from = doc_iterator_begin(cur.buffer());
2586 to = doc_iterator_end(cur.buffer());
2589 cur.buffer()->updateStatistics(from, to);
2590 string const arg0 = cmd.getArg(0);
2591 if (arg0 == "words") {
2592 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2593 } else if (arg0 == "chars") {
2594 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2595 } else if (arg0 == "chars-space") {
2596 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2598 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2599 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2600 + convert<docstring>(cur.buffer()->charCount(true)));
2606 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2611 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2613 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2614 // Check for misspelled text
2615 // The redraw is useful because of the painting of
2616 // misspelled markers depends on the cursor position.
2617 // Trigger a redraw for cursor moves inside misspelled text.
2618 if (!cur.inTexted()) {
2619 // move from regular text to math
2620 needsUpdate = last_misspelled;
2621 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2622 // move inside regular text
2623 needsUpdate = last_misspelled
2624 || cur.paragraph().isMisspelled(cur.pos(), true);
2628 // FIXME: The cursor flag is reset two lines below
2629 // so we need to check here if some of the LFUN did touch that.
2630 // for now only Text::erase() and Text::backspace() do that.
2631 // The plan is to verify all the LFUNs and then to remove this
2632 // singleParUpdate boolean altogether.
2633 if (cur.result().screenUpdate() & Update::Force) {
2634 singleParUpdate = false;
2638 // FIXME: the following code should go in favor of fine grained
2639 // update flag treatment.
2640 if (singleParUpdate) {
2641 // Inserting characters does not change par height in general. So, try
2642 // to update _only_ this paragraph. BufferView will detect if a full
2643 // metrics update is needed anyway.
2644 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2648 && &oldTopSlice.inset() == &cur.inset()
2649 && oldTopSlice.idx() == cur.idx()
2650 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2651 && !cur.selection())
2652 // FIXME: it would be better if we could just do this
2654 //if (cur.result().update() != Update::FitCursor)
2655 // cur.noScreenUpdate();
2657 // But some LFUNs do not set Update::FitCursor when needed, so we
2658 // do it for all. This is not very harmfull as FitCursor will provoke
2659 // a full redraw only if needed but still, a proper review of all LFUN
2660 // should be done and this needsUpdate boolean can then be removed.
2661 cur.screenUpdateFlags(Update::FitCursor);
2663 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2667 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2668 FuncStatus & flag) const
2670 LBUFERR(this == cur.text());
2672 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2674 bool allow_in_passthru = false;
2675 InsetCode code = NO_CODE;
2677 switch (cmd.action()) {
2679 case LFUN_DEPTH_DECREMENT:
2680 enable = changeDepthAllowed(cur, DEC_DEPTH);
2683 case LFUN_DEPTH_INCREMENT:
2684 enable = changeDepthAllowed(cur, INC_DEPTH);
2688 // FIXME We really should not allow this to be put, e.g.,
2689 // in a footnote, or in ERT. But it would make sense in a
2690 // branch, so I'm not sure what to do.
2691 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2694 case LFUN_DIALOG_SHOW_NEW_INSET:
2695 if (cmd.argument() == "bibitem")
2696 code = BIBITEM_CODE;
2697 else if (cmd.argument() == "bibtex") {
2699 // not allowed in description items
2700 enable = !inDescriptionItem(cur);
2702 else if (cmd.argument() == "box")
2704 else if (cmd.argument() == "branch")
2706 else if (cmd.argument() == "citation")
2708 else if (cmd.argument() == "ert")
2710 else if (cmd.argument() == "external")
2711 code = EXTERNAL_CODE;
2712 else if (cmd.argument() == "float")
2714 else if (cmd.argument() == "graphics")
2715 code = GRAPHICS_CODE;
2716 else if (cmd.argument() == "href")
2717 code = HYPERLINK_CODE;
2718 else if (cmd.argument() == "include")
2719 code = INCLUDE_CODE;
2720 else if (cmd.argument() == "index")
2722 else if (cmd.argument() == "index_print")
2723 code = INDEX_PRINT_CODE;
2724 else if (cmd.argument() == "listings")
2725 code = LISTINGS_CODE;
2726 else if (cmd.argument() == "mathspace")
2727 code = MATH_HULL_CODE;
2728 else if (cmd.argument() == "nomenclature")
2729 code = NOMENCL_CODE;
2730 else if (cmd.argument() == "nomencl_print")
2731 code = NOMENCL_PRINT_CODE;
2732 else if (cmd.argument() == "label")
2734 else if (cmd.argument() == "line")
2736 else if (cmd.argument() == "note")
2738 else if (cmd.argument() == "phantom")
2739 code = PHANTOM_CODE;
2740 else if (cmd.argument() == "ref")
2742 else if (cmd.argument() == "space")
2744 else if (cmd.argument() == "toc")
2746 else if (cmd.argument() == "vspace")
2748 else if (cmd.argument() == "wrap")
2752 case LFUN_ERT_INSERT:
2755 case LFUN_LISTING_INSERT:
2756 code = LISTINGS_CODE;
2757 // not allowed in description items
2758 enable = !inDescriptionItem(cur);
2760 case LFUN_FOOTNOTE_INSERT:
2763 case LFUN_TABULAR_INSERT:
2764 code = TABULAR_CODE;
2766 case LFUN_MARGINALNOTE_INSERT:
2769 case LFUN_FLOAT_INSERT:
2770 case LFUN_FLOAT_WIDE_INSERT:
2771 // FIXME: If there is a selection, we should check whether there
2772 // are floats in the selection, but this has performance issues, see
2773 // LFUN_CHANGE_ACCEPT/REJECT.
2775 if (inDescriptionItem(cur))
2776 // not allowed in description items
2779 InsetCode const inset_code = cur.inset().lyxCode();
2781 // algorithm floats cannot be put in another float
2782 if (to_utf8(cmd.argument()) == "algorithm") {
2783 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2787 // for figures and tables: only allow in another
2788 // float or wrap if it is of the same type and
2789 // not a subfloat already
2790 if(cur.inset().lyxCode() == code) {
2791 InsetFloat const & ins =
2792 static_cast<InsetFloat const &>(cur.inset());
2793 enable = ins.params().type == to_utf8(cmd.argument())
2794 && !ins.params().subfloat;
2795 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2796 InsetWrap const & ins =
2797 static_cast<InsetWrap const &>(cur.inset());
2798 enable = ins.params().type == to_utf8(cmd.argument());
2802 case LFUN_WRAP_INSERT:
2804 // not allowed in description items
2805 enable = !inDescriptionItem(cur);
2807 case LFUN_FLOAT_LIST_INSERT: {
2808 code = FLOAT_LIST_CODE;
2809 // not allowed in description items
2810 enable = !inDescriptionItem(cur);
2812 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2813 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2814 // make sure we know about such floats
2815 if (cit == floats.end() ||
2816 // and that we know how to generate a list of them
2817 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2818 flag.setUnknown(true);
2819 // probably not necessary, but...
2825 case LFUN_CAPTION_INSERT: {
2826 code = CAPTION_CODE;
2827 string arg = cmd.getArg(0);
2828 bool varia = arg != "Unnumbered"
2829 && cur.inset().allowsCaptionVariation(arg);
2830 // not allowed in description items,
2831 // and in specific insets
2832 enable = !inDescriptionItem(cur)
2833 && (varia || arg.empty() || arg == "Standard");
2836 case LFUN_NOTE_INSERT:
2838 // in commands (sections etc.) and description items,
2839 // only Notes are allowed
2840 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2841 (!cur.paragraph().layout().isCommand()
2842 && !inDescriptionItem(cur)));
2844 case LFUN_FLEX_INSERT: {
2846 string s = cmd.getArg(0);
2848 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2849 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2850 il.lyxtype() != InsetLayout::CUSTOM &&
2851 il.lyxtype() != InsetLayout::ELEMENT &&
2852 il.lyxtype ()!= InsetLayout::STANDARD)
2856 case LFUN_BOX_INSERT:
2859 case LFUN_BRANCH_INSERT:
2861 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2862 && cur.buffer()->params().branchlist().empty())
2865 case LFUN_IPA_INSERT:
2868 case LFUN_PHANTOM_INSERT:
2869 code = PHANTOM_CODE;
2871 case LFUN_LABEL_INSERT:
2874 case LFUN_INFO_INSERT:
2877 case LFUN_ARGUMENT_INSERT: {
2879 allow_in_passthru = true;
2880 string const arg = cmd.getArg(0);
2885 Layout const & lay = cur.paragraph().layout();
2886 Layout::LaTeXArgMap args = lay.args();
2887 Layout::LaTeXArgMap::const_iterator const lait =
2889 if (lait != args.end()) {
2891 pit_type pit = cur.pit();
2892 pit_type lastpit = cur.pit();
2893 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2894 // In a sequence of "merged" environment layouts, we only allow
2895 // non-item arguments once.
2896 lastpit = cur.lastpit();
2897 // get the first paragraph in sequence with this layout
2898 depth_type const current_depth = cur.paragraph().params().depth();
2902 Paragraph cpar = pars_[pit - 1];
2903 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2909 for (; pit <= lastpit; ++pit) {
2910 if (pars_[pit].layout() != lay)
2912 for (auto const & table : pars_[pit].insetList())
2913 if (InsetArgument const * ins = table.inset->asInsetArgument())
2914 if (ins->name() == arg) {
2915 // we have this already
2924 case LFUN_INDEX_INSERT:
2927 case LFUN_INDEX_PRINT:
2928 code = INDEX_PRINT_CODE;
2929 // not allowed in description items
2930 enable = !inDescriptionItem(cur);
2932 case LFUN_NOMENCL_INSERT:
2933 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2937 code = NOMENCL_CODE;
2939 case LFUN_NOMENCL_PRINT:
2940 code = NOMENCL_PRINT_CODE;
2941 // not allowed in description items
2942 enable = !inDescriptionItem(cur);
2944 case LFUN_HREF_INSERT:
2945 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2949 code = HYPERLINK_CODE;
2951 case LFUN_IPAMACRO_INSERT: {
2952 string const arg = cmd.getArg(0);
2954 code = IPADECO_CODE;
2956 code = IPACHAR_CODE;
2959 case LFUN_QUOTE_INSERT:
2960 // always allow this, since we will inset a raw quote
2961 // if an inset is not allowed.
2962 allow_in_passthru = true;
2964 case LFUN_SPECIALCHAR_INSERT:
2965 code = SPECIALCHAR_CODE;
2967 case LFUN_SPACE_INSERT:
2968 // slight hack: we know this is allowed in math mode
2972 case LFUN_PREVIEW_INSERT:
2973 code = PREVIEW_CODE;
2975 case LFUN_SCRIPT_INSERT:
2979 case LFUN_MATH_INSERT:
2980 case LFUN_MATH_AMS_MATRIX:
2981 case LFUN_MATH_MATRIX:
2982 case LFUN_MATH_DELIM:
2983 case LFUN_MATH_BIGDELIM:
2984 case LFUN_MATH_DISPLAY:
2985 case LFUN_MATH_MODE:
2986 case LFUN_MATH_MACRO:
2987 case LFUN_MATH_SUBSCRIPT:
2988 case LFUN_MATH_SUPERSCRIPT:
2989 code = MATH_HULL_CODE;
2992 case LFUN_REGEXP_MODE:
2993 code = MATH_HULL_CODE;
2994 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2997 case LFUN_INSET_MODIFY:
2998 // We need to disable this, because we may get called for a
3000 // InsetTabular::getStatus() -> InsetText::getStatus()
3001 // and we don't handle LFUN_INSET_MODIFY.
3005 case LFUN_FONT_EMPH:
3006 flag.setOnOff(fontinfo.emph() == FONT_ON);
3007 enable = !cur.paragraph().isPassThru();
3010 case LFUN_FONT_ITAL:
3011 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3012 enable = !cur.paragraph().isPassThru();
3015 case LFUN_FONT_NOUN:
3016 flag.setOnOff(fontinfo.noun() == FONT_ON);
3017 enable = !cur.paragraph().isPassThru();
3020 case LFUN_FONT_BOLD:
3021 case LFUN_FONT_BOLDSYMBOL:
3022 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3023 enable = !cur.paragraph().isPassThru();
3026 case LFUN_FONT_SANS:
3027 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3028 enable = !cur.paragraph().isPassThru();
3031 case LFUN_FONT_ROMAN:
3032 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3033 enable = !cur.paragraph().isPassThru();
3036 case LFUN_FONT_TYPEWRITER:
3037 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3038 enable = !cur.paragraph().isPassThru();
3043 enable = cur.selection();
3047 if (cmd.argument().empty()) {
3048 if (theClipboard().isInternal())
3049 enable = cap::numberOfSelections() > 0;
3051 enable = !theClipboard().empty();
3055 // we have an argument
3056 string const arg = to_utf8(cmd.argument());
3057 if (isStrUnsignedInt(arg)) {
3058 // it's a number and therefore means the internal stack
3059 unsigned int n = convert<unsigned int>(arg);
3060 enable = cap::numberOfSelections() > n;
3064 // explicit text type?
3065 if (arg == "html") {
3066 // Do not enable for PlainTextType, since some tidying in the
3067 // frontend is needed for HTML, which is too unsafe for plain text.
3068 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3070 } else if (arg == "latex") {
3071 // LaTeX is usually not available on the clipboard with
3072 // the correct MIME type, but in plain text.
3073 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3074 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3078 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3080 type = Clipboard::PdfGraphicsType;
3081 else if (arg == "png")
3082 type = Clipboard::PngGraphicsType;
3083 else if (arg == "jpeg")
3084 type = Clipboard::JpegGraphicsType;
3085 else if (arg == "linkback")
3086 type = Clipboard::LinkBackGraphicsType;
3087 else if (arg == "emf")
3088 type = Clipboard::EmfGraphicsType;
3089 else if (arg == "wmf")
3090 type = Clipboard::WmfGraphicsType;
3093 LYXERR0("Unrecognized graphics type: " << arg);
3094 // we don't want to assert if the user just mistyped the LFUN
3095 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3099 enable = theClipboard().hasGraphicsContents(type);
3103 case LFUN_CLIPBOARD_PASTE:
3104 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3105 enable = !theClipboard().empty();
3108 case LFUN_PRIMARY_SELECTION_PASTE:
3109 enable = cur.selection() || !theSelection().empty();
3112 case LFUN_SELECTION_PASTE:
3113 enable = cap::selection();
3116 case LFUN_PARAGRAPH_MOVE_UP:
3117 enable = cur.pit() > 0 && !cur.selection();
3120 case LFUN_PARAGRAPH_MOVE_DOWN:
3121 enable = cur.pit() < cur.lastpit() && !cur.selection();
3124 case LFUN_CHANGE_ACCEPT:
3125 case LFUN_CHANGE_REJECT:
3126 // In principle, these LFUNs should only be enabled if there
3127 // is a change at the current position/in the current selection.
3128 // However, without proper optimizations, this will inevitably
3129 // result in unacceptable performance - just imagine a user who
3130 // wants to select the complete content of a long document.
3131 if (!cur.selection())
3132 enable = cur.paragraph().isChanged(cur.pos());
3134 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3139 case LFUN_OUTLINE_UP:
3140 case LFUN_OUTLINE_DOWN:
3141 case LFUN_OUTLINE_IN:
3142 case LFUN_OUTLINE_OUT:
3143 // FIXME: LyX is not ready for outlining within inset.
3144 enable = isMainText()
3145 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3148 case LFUN_NEWLINE_INSERT:
3149 // LaTeX restrictions (labels or empty par)
3150 enable = !cur.paragraph().isPassThru()
3151 && cur.pos() > cur.paragraph().beginOfBody();
3154 case LFUN_SEPARATOR_INSERT:
3155 // Always enabled for now
3159 case LFUN_TAB_INSERT:
3160 case LFUN_TAB_DELETE:
3161 enable = cur.paragraph().isPassThru();
3164 case LFUN_SET_GRAPHICS_GROUP: {
3165 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3169 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3173 case LFUN_NEWPAGE_INSERT:
3174 // not allowed in description items
3175 code = NEWPAGE_CODE;
3176 enable = !inDescriptionItem(cur);
3179 case LFUN_DATE_INSERT: {
3180 string const format = cmd.argument().empty()
3181 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3182 enable = support::os::is_valid_strftime(format);
3187 enable = !cur.paragraph().isPassThru();
3188 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3191 case LFUN_PARAGRAPH_BREAK:
3192 enable = inset().allowMultiPar();
3195 case LFUN_SPELLING_ADD:
3196 case LFUN_SPELLING_IGNORE:
3197 case LFUN_SPELLING_REMOVE:
3198 enable = theSpellChecker() != NULL;
3199 if (enable && !cmd.getArg(1).empty()) {
3200 // validate explicitly given language
3201 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3202 enable &= lang != NULL;
3207 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3208 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3209 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3211 layout = tclass.defaultLayoutName();
3212 enable = !owner_->forcePlainLayout() && tclass.hasLayout(layout);
3214 flag.setOnOff(layout == cur.paragraph().layout().name());
3218 case LFUN_ENVIRONMENT_SPLIT: {
3219 if (cmd.argument() == "outer") {
3220 // check if we have an environment in our nesting hierarchy
3222 depth_type const current_depth = cur.paragraph().params().depth();
3223 pit_type pit = cur.pit();
3224 Paragraph cpar = pars_[pit];
3226 if (pit == 0 || cpar.params().depth() == 0)
3230 if (cpar.params().depth() < current_depth)
3231 res = cpar.layout().isEnvironment();
3236 else if (cmd.argument() == "previous") {
3237 // look if we have an environment in the previous par
3238 pit_type pit = cur.pit();
3239 Paragraph cpar = pars_[pit];
3243 enable = cpar.layout().isEnvironment();
3249 else if (cur.paragraph().layout().isEnvironment()) {
3250 enable = cmd.argument() == "before"
3251 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3258 case LFUN_LAYOUT_PARAGRAPH:
3259 case LFUN_PARAGRAPH_PARAMS:
3260 case LFUN_PARAGRAPH_PARAMS_APPLY:
3261 case LFUN_PARAGRAPH_UPDATE:
3262 enable = owner_->allowParagraphCustomization();
3265 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3266 // Because they insert COMBINING DIACRITICAL Unicode characters,
3267 // that cannot be handled by LaTeX but must be converted according
3268 // to the definition in lib/unicodesymbols?
3269 case LFUN_ACCENT_ACUTE:
3270 case LFUN_ACCENT_BREVE:
3271 case LFUN_ACCENT_CARON:
3272 case LFUN_ACCENT_CEDILLA:
3273 case LFUN_ACCENT_CIRCLE:
3274 case LFUN_ACCENT_CIRCUMFLEX:
3275 case LFUN_ACCENT_DOT:
3276 case LFUN_ACCENT_GRAVE:
3277 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3278 case LFUN_ACCENT_MACRON:
3279 case LFUN_ACCENT_OGONEK:
3280 case LFUN_ACCENT_TIE:
3281 case LFUN_ACCENT_TILDE:
3282 case LFUN_ACCENT_PERISPOMENI:
3283 case LFUN_ACCENT_UMLAUT:
3284 case LFUN_ACCENT_UNDERBAR:
3285 case LFUN_ACCENT_UNDERDOT:
3286 case LFUN_FONT_DEFAULT:
3287 case LFUN_FONT_FRAK:
3288 case LFUN_FONT_SIZE:
3289 case LFUN_FONT_STATE:
3290 case LFUN_FONT_UNDERLINE:
3291 case LFUN_FONT_STRIKEOUT:
3292 case LFUN_FONT_CROSSOUT:
3293 case LFUN_FONT_UNDERUNDERLINE:
3294 case LFUN_FONT_UNDERWAVE:
3295 case LFUN_TEXTSTYLE_APPLY:
3296 case LFUN_TEXTSTYLE_UPDATE:
3297 enable = !cur.paragraph().isPassThru();
3300 case LFUN_WORD_DELETE_FORWARD:
3301 case LFUN_WORD_DELETE_BACKWARD:
3302 case LFUN_LINE_DELETE_FORWARD:
3303 case LFUN_WORD_FORWARD:
3304 case LFUN_WORD_BACKWARD:
3305 case LFUN_WORD_RIGHT:
3306 case LFUN_WORD_LEFT:
3307 case LFUN_CHAR_FORWARD:
3308 case LFUN_CHAR_FORWARD_SELECT:
3309 case LFUN_CHAR_BACKWARD:
3310 case LFUN_CHAR_BACKWARD_SELECT:
3311 case LFUN_CHAR_LEFT:
3312 case LFUN_CHAR_LEFT_SELECT:
3313 case LFUN_CHAR_RIGHT:
3314 case LFUN_CHAR_RIGHT_SELECT:
3316 case LFUN_UP_SELECT:
3318 case LFUN_DOWN_SELECT:
3319 case LFUN_PARAGRAPH_UP_SELECT:
3320 case LFUN_PARAGRAPH_DOWN_SELECT:
3321 case LFUN_LINE_BEGIN_SELECT:
3322 case LFUN_LINE_END_SELECT:
3323 case LFUN_WORD_FORWARD_SELECT:
3324 case LFUN_WORD_BACKWARD_SELECT:
3325 case LFUN_WORD_RIGHT_SELECT:
3326 case LFUN_WORD_LEFT_SELECT:
3327 case LFUN_WORD_SELECT:
3328 case LFUN_SECTION_SELECT:
3329 case LFUN_BUFFER_BEGIN:
3330 case LFUN_BUFFER_END:
3331 case LFUN_BUFFER_BEGIN_SELECT:
3332 case LFUN_BUFFER_END_SELECT:
3333 case LFUN_INSET_BEGIN:
3334 case LFUN_INSET_END:
3335 case LFUN_INSET_BEGIN_SELECT:
3336 case LFUN_INSET_END_SELECT:
3337 case LFUN_PARAGRAPH_UP:
3338 case LFUN_PARAGRAPH_DOWN:
3339 case LFUN_LINE_BEGIN:
3341 case LFUN_CHAR_DELETE_FORWARD:
3342 case LFUN_CHAR_DELETE_BACKWARD:
3343 case LFUN_WORD_UPCASE:
3344 case LFUN_WORD_LOWCASE:
3345 case LFUN_WORD_CAPITALIZE:
3346 case LFUN_CHARS_TRANSPOSE:
3347 case LFUN_SERVER_GET_XY:
3348 case LFUN_SERVER_SET_XY:
3349 case LFUN_SERVER_GET_LAYOUT:
3350 case LFUN_SELF_INSERT:
3351 case LFUN_UNICODE_INSERT:
3352 case LFUN_THESAURUS_ENTRY:
3354 case LFUN_SERVER_GET_STATISTICS:
3355 // these are handled in our dispatch()
3359 case LFUN_INSET_INSERT: {
3360 string const type = cmd.getArg(0);
3361 if (type == "toc") {
3363 // not allowed in description items
3364 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3365 enable = !inDescriptionItem(cur);
3378 || !cur.inset().insetAllowed(code)
3379 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3382 flag.setEnabled(enable);
3387 void Text::pasteString(Cursor & cur, docstring const & clip,
3390 if (!clip.empty()) {
3393 insertStringAsParagraphs(cur, clip, cur.current_font);
3395 insertStringAsLines(cur, clip, cur.current_font);
3400 // FIXME: an item inset would make things much easier.
3401 bool Text::inDescriptionItem(Cursor & cur) const
3403 Paragraph & par = cur.paragraph();
3404 pos_type const pos = cur.pos();
3405 pos_type const body_pos = par.beginOfBody();
3407 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3408 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3409 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3412 return (pos < body_pos
3414 && (pos == 0 || par.getChar(pos - 1) != ' ')));