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/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;
100 using frontend::Clipboard;
103 static Font freefont(ignore_font, ignore_language);
104 static bool toggleall = false;
106 static void toggleAndShow(Cursor & cur, Text * text,
107 Font const & font, bool toggleall = true)
109 text->toggleFree(cur, font, toggleall);
111 if (font.language() != ignore_language ||
112 font.fontInfo().number() != FONT_IGNORE) {
113 TextMetrics const & tm = cur.bv().textMetrics(text);
114 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
115 cur.real_current_font))
116 text->setCursor(cur, cur.pit(), cur.pos(),
117 false, !cur.boundary());
122 static void moveCursor(Cursor & cur, bool selecting)
124 if (selecting || cur.mark())
129 static void finishChange(Cursor & cur, bool selecting)
132 moveCursor(cur, selecting);
136 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
139 docstring sel = cur.selectionAsString(false);
141 // It may happen that sel is empty but there is a selection
142 replaceSelection(cur);
144 // Is this a valid formula?
148 #ifdef ENABLE_ASSERTIONS
149 const int old_pos = cur.pos();
151 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
152 #ifdef ENABLE_ASSERTIONS
153 LATTEST(old_pos == cur.pos());
155 cur.nextInset()->edit(cur, true);
156 if (cmd.action() != LFUN_MATH_MODE)
157 // LFUN_MATH_MODE has a different meaning in math mode
160 InsetMathHull * formula = new InsetMathHull(cur.buffer());
161 string const selstr = to_utf8(sel);
162 istringstream is(selstr);
165 if (!formula->readQuiet(lex)) {
166 // No valid formula, let's try with delims
167 is.str("$" + selstr + "$");
169 if (!formula->readQuiet(lex)) {
170 // Still not valid, leave it as is
178 cur.nextInset()->edit(cur, true);
179 LASSERT(cur.inMathed(), return);
183 cur.pos() = cur.lastpos();
184 if (cmd.action() != LFUN_MATH_MODE)
185 // LFUN_MATH_MODE has a different meaning in math mode
187 cur.clearSelection();
188 cur.pos() = cur.lastpos();
192 cur.message(from_utf8(N_("Math editor mode")));
194 cur.message(from_utf8(N_("No valid math formula")));
198 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
200 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
201 if (cur.inRegexped()) {
202 cur.message(_("Already in regular expression mode"));
206 docstring sel = cur.selectionAsString(false);
208 // It may happen that sel is empty but there is a selection
209 replaceSelection(cur);
211 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
212 cur.nextInset()->edit(cur, true);
215 cur.message(_("Regexp editor mode"));
219 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
222 cap::replaceSelection(cur);
223 cur.insert(new InsetSpecialChar(kind));
228 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
231 cap::replaceSelection(cur);
232 cur.insert(new InsetIPAChar(kind));
237 static bool doInsertInset(Cursor & cur, Text * text,
238 FuncRequest const & cmd, bool edit, bool pastesel)
240 Buffer & buffer = cur.bv().buffer();
241 BufferParams const & bparams = buffer.params();
242 Inset * inset = createInset(&buffer, cmd);
246 if (InsetCollapsable * ci = inset->asInsetCollapsable())
247 ci->setButtonLabel();
250 if (cmd.action() == LFUN_INDEX_INSERT) {
251 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
252 text->insertInset(cur, inset);
254 inset->edit(cur, true);
255 // Now put this into inset
256 Font const f(inherit_font, cur.current_font.language());
258 cur.text()->insertStringAsLines(cur, ds, f);
259 cur.leaveInset(*inset);
263 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
264 bool cotextinsert = false;
265 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
266 Layout const & lay = cur.paragraph().layout();
267 Layout::LaTeXArgMap args = lay.args();
268 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
269 if (lait != args.end())
270 cotextinsert = (*lait).second.insertcotext;
272 InsetLayout const & il = cur.inset().getLayout();
274 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
275 if (ilait != args.end())
276 cotextinsert = (*ilait).second.insertcotext;
278 // The argument requests to insert a copy of the co-text to the inset
281 // If we have a selection within a paragraph, use this
282 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
283 ds = cur.selectionAsString(false);
284 // else use the whole paragraph
286 ds = cur.paragraph().asString();
287 text->insertInset(cur, inset);
289 inset->edit(cur, true);
290 // Now put co-text into inset
291 Font const f(inherit_font, cur.current_font.language());
293 cur.text()->insertStringAsLines(cur, ds, f);
294 cur.leaveInset(*inset);
301 if (cur.selection()) {
302 cutSelection(cur, false, pastesel);
303 cur.clearSelection();
306 text->insertInset(cur, inset);
309 inset->edit(cur, true);
311 if (!gotsel || !pastesel)
314 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
315 cur.buffer()->errors("Paste");
316 cur.clearSelection(); // bug 393
318 InsetText * inset_text = inset->asInsetText();
320 inset_text->fixParagraphsFont();
321 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
322 // reset first par to default
323 cur.text()->paragraphs().begin()
324 ->setPlainOrDefaultLayout(bparams.documentClass());
327 // Merge multiple paragraphs -- hack
328 while (cur.lastpit() > 0)
329 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
330 if (cmd.action() == LFUN_FLEX_INSERT)
333 cur.leaveInset(*inset);
334 if (cmd.action() == LFUN_PREVIEW_INSERT
335 || cmd.action() == LFUN_IPA_INSERT)
337 notifyCursorLeavesOrEnters(old, cur);
340 cur.leaveInset(*inset);
341 // reset surrounding par to default
342 DocumentClass const & dc = bparams.documentClass();
343 docstring const layoutname = inset->usePlainLayout()
344 ? dc.plainLayoutName()
345 : dc.defaultLayoutName();
346 text->setLayout(cur, layoutname);
352 string const freefont2string()
354 return freefont.toString(toggleall);
358 /// the type of outline operation
360 OutlineUp, // Move this header with text down
361 OutlineDown, // Move this header with text up
362 OutlineIn, // Make this header deeper
363 OutlineOut // Make this header shallower
367 static void outline(OutlineOp mode, Cursor & cur)
369 Buffer & buf = *cur.buffer();
370 pit_type & pit = cur.pit();
371 ParagraphList & pars = buf.text().paragraphs();
372 ParagraphList::iterator const bgn = pars.begin();
373 // The first paragraph of the area to be copied:
374 ParagraphList::iterator start = lyx::next(bgn, pit);
375 // The final paragraph of area to be copied:
376 ParagraphList::iterator finish = start;
377 ParagraphList::iterator const end = pars.end();
379 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
382 // Move out (down) from this section header
386 // Seek the one (on same level) below
387 for (; finish != end; ++finish) {
388 toclevel = buf.text().getTocLevel(distance(bgn, finish));
389 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
395 if (start == pars.begin())
398 ParagraphList::iterator dest = start;
399 // Move out (up) from this header
402 // Search previous same-level header above
405 toclevel = buf.text().getTocLevel(distance(bgn, dest));
407 && (toclevel == Layout::NOT_IN_TOC
408 || toclevel > thistoclevel));
409 // Not found; do nothing
410 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
412 pit_type const newpit = distance(bgn, dest);
413 pit_type const len = distance(start, finish);
414 pit_type const deletepit = pit + len;
415 buf.undo().recordUndo(cur, newpit, deletepit - 1);
416 pars.splice(dest, start, finish);
424 // Go one down from *this* header:
425 ParagraphList::iterator dest = next(finish, 1);
426 // Go further down to find header to insert in front of:
427 for (; dest != end; ++dest) {
428 toclevel = buf.text().getTocLevel(distance(bgn, dest));
429 if (toclevel != Layout::NOT_IN_TOC
430 && toclevel <= thistoclevel)
433 // One such was found:
434 pit_type newpit = distance(bgn, dest);
435 buf.undo().recordUndo(cur, pit, newpit - 1);
436 pit_type const len = distance(start, finish);
437 pars.splice(dest, start, finish);
438 cur.pit() = newpit - len;
443 pit_type const len = distance(start, finish);
444 buf.undo().recordUndo(cur, pit, pit + len - 1);
445 for (; start != finish; ++start) {
446 toclevel = buf.text().getTocLevel(distance(bgn, start));
447 if (toclevel == Layout::NOT_IN_TOC)
450 DocumentClass const & tc = buf.params().documentClass();
451 DocumentClass::const_iterator lit = tc.begin();
452 DocumentClass::const_iterator len = tc.end();
453 int const newtoclevel =
454 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
455 LabelType const oldlabeltype = start->layout().labeltype;
457 for (; lit != len; ++lit) {
458 if (lit->toclevel == newtoclevel &&
459 lit->labeltype == oldlabeltype) {
460 start->setLayout(*lit);
471 void Text::number(Cursor & cur)
473 FontInfo font = ignore_font;
474 font.setNumber(FONT_TOGGLE);
475 toggleAndShow(cur, this, Font(font, ignore_language));
479 bool Text::isRTL(Paragraph const & par) const
481 Buffer const & buffer = owner_->buffer();
482 return par.isRTL(buffer.params());
488 Language const * getLanguage(Cursor const & cur, string const & lang) {
489 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
495 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
497 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
499 // Dispatch if the cursor is inside the text. It is not the
500 // case for context menus (bug 5797).
501 if (cur.text() != this) {
506 BufferView * bv = &cur.bv();
507 TextMetrics * tm = &bv->textMetrics(this);
508 if (!tm->contains(cur.pit())) {
509 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
510 tm = &bv->textMetrics(this);
513 // FIXME: We use the update flag to indicates wether a singlePar or a
514 // full screen update is needed. We reset it here but shall we restore it
516 cur.noScreenUpdate();
518 LBUFERR(this == cur.text());
520 // NOTE: This should NOT be a reference. See commit 94a5481a.
521 CursorSlice const oldTopSlice = cur.top();
522 bool const oldBoundary = cur.boundary();
523 bool const oldSelection = cur.selection();
524 // Signals that, even if needsUpdate == false, an update of the
525 // cursor paragraph is required
526 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
527 LyXAction::SingleParUpdate);
528 // Signals that a full-screen update is required
529 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
530 LyXAction::NoUpdate) || singleParUpdate);
531 bool const last_misspelled = lyxrc.spellcheck_continuously
532 && cur.paragraph().isMisspelled(cur.pos(), true);
534 FuncCode const act = cmd.action();
537 case LFUN_PARAGRAPH_MOVE_DOWN: {
538 pit_type const pit = cur.pit();
539 cur.recordUndo(pit, pit + 1);
541 pars_.swap(pit, pit + 1);
543 cur.forceBufferUpdate();
548 case LFUN_PARAGRAPH_MOVE_UP: {
549 pit_type const pit = cur.pit();
550 cur.recordUndo(pit - 1, pit);
552 pars_.swap(pit, pit - 1);
555 cur.forceBufferUpdate();
559 case LFUN_APPENDIX: {
560 Paragraph & par = cur.paragraph();
561 bool start = !par.params().startOfAppendix();
563 // FIXME: The code below only makes sense at top level.
564 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
565 // ensure that we have only one start_of_appendix in this document
566 // FIXME: this don't work for multipart document!
567 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
568 if (pars_[tmp].params().startOfAppendix()) {
569 cur.recordUndo(tmp, tmp);
570 pars_[tmp].params().startOfAppendix(false);
576 par.params().startOfAppendix(start);
578 // we can set the refreshing parameters now
579 cur.forceBufferUpdate();
583 case LFUN_WORD_DELETE_FORWARD:
585 cutSelection(cur, true, false);
587 deleteWordForward(cur);
588 finishChange(cur, false);
591 case LFUN_WORD_DELETE_BACKWARD:
593 cutSelection(cur, true, false);
595 deleteWordBackward(cur);
596 finishChange(cur, false);
599 case LFUN_LINE_DELETE_FORWARD:
601 cutSelection(cur, true, false);
603 tm->deleteLineForward(cur);
604 finishChange(cur, false);
607 case LFUN_BUFFER_BEGIN:
608 case LFUN_BUFFER_BEGIN_SELECT:
609 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
610 if (cur.depth() == 1)
611 needsUpdate |= cursorTop(cur);
614 cur.screenUpdateFlags(Update::FitCursor);
617 case LFUN_BUFFER_END:
618 case LFUN_BUFFER_END_SELECT:
619 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
620 if (cur.depth() == 1)
621 needsUpdate |= cursorBottom(cur);
624 cur.screenUpdateFlags(Update::FitCursor);
627 case LFUN_INSET_BEGIN:
628 case LFUN_INSET_BEGIN_SELECT:
629 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
630 if (cur.depth() == 1 || !cur.top().at_begin())
631 needsUpdate |= cursorTop(cur);
634 cur.screenUpdateFlags(Update::FitCursor);
638 case LFUN_INSET_END_SELECT:
639 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
640 if (cur.depth() == 1 || !cur.top().at_end())
641 needsUpdate |= cursorBottom(cur);
644 cur.screenUpdateFlags(Update::FitCursor);
647 case LFUN_CHAR_FORWARD:
648 case LFUN_CHAR_FORWARD_SELECT: {
649 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
650 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
651 bool const cur_moved = cursorForward(cur);
652 needsUpdate |= cur_moved;
654 if (!cur_moved && oldTopSlice == cur.top()
655 && cur.boundary() == oldBoundary) {
657 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
659 // we will probably be moving out the inset, so we should execute
660 // the depm-mechanism, but only when the cursor has a place to
661 // go outside this inset, i.e. in a slice above.
662 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
663 && cur.pit() == cur.lastpit()) {
664 // The cursor hasn't changed yet. To give the
665 // DEPM the possibility of doing something we must
666 // provide it with two different cursors.
668 dummy.pos() = dummy.pit() = 0;
669 if (cur.bv().checkDepm(dummy, cur))
670 cur.forceBufferUpdate();
676 case LFUN_CHAR_BACKWARD:
677 case LFUN_CHAR_BACKWARD_SELECT: {
678 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
679 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
680 bool const cur_moved = cursorBackward(cur);
681 needsUpdate |= cur_moved;
683 if (!cur_moved && oldTopSlice == cur.top()
684 && cur.boundary() == oldBoundary) {
686 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
688 // we will probably be moving out the inset, so we should execute
689 // the depm-mechanism, but only when the cursor has a place to
690 // go outside this inset, i.e. in a slice above.
691 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
692 // The cursor hasn't changed yet. To give the
693 // DEPM the possibility of doing something we must
694 // provide it with two different cursors.
696 dummy.pos() = cur.lastpos();
697 dummy.pit() = cur.lastpit();
698 if (cur.bv().checkDepm(dummy, cur))
699 cur.forceBufferUpdate();
706 case LFUN_CHAR_LEFT_SELECT:
707 if (lyxrc.visual_cursor) {
708 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
709 bool const cur_moved = cursorVisLeft(cur);
710 needsUpdate |= cur_moved;
711 if (!cur_moved && oldTopSlice == cur.top()
712 && cur.boundary() == oldBoundary) {
714 cmd = FuncRequest(LFUN_FINISHED_LEFT);
717 if (cur.reverseDirectionNeeded()) {
718 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
719 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
721 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
722 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
729 case LFUN_CHAR_RIGHT:
730 case LFUN_CHAR_RIGHT_SELECT:
731 if (lyxrc.visual_cursor) {
732 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
733 bool const cur_moved = cursorVisRight(cur);
734 needsUpdate |= cur_moved;
735 if (!cur_moved && oldTopSlice == cur.top()
736 && cur.boundary() == oldBoundary) {
738 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
741 if (cur.reverseDirectionNeeded()) {
742 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
743 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
745 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
746 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
755 case LFUN_DOWN_SELECT:
758 // stop/start the selection
759 bool select = cmd.action() == LFUN_DOWN_SELECT ||
760 cmd.action() == LFUN_UP_SELECT;
762 // move cursor up/down
763 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
764 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
766 if (!atFirstOrLastRow) {
767 needsUpdate |= cur.selHandle(select);
768 cur.upDownInText(up, needsUpdate);
769 needsUpdate |= cur.beforeDispatchCursor().inMathed();
771 // if the cursor cannot be moved up or down do not remove
772 // the selection right now, but wait for the next dispatch.
774 needsUpdate |= cur.selHandle(select);
775 cur.upDownInText(up, needsUpdate);
782 case LFUN_PARAGRAPH_UP:
783 case LFUN_PARAGRAPH_UP_SELECT:
784 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
785 needsUpdate |= cursorUpParagraph(cur);
788 case LFUN_PARAGRAPH_DOWN:
789 case LFUN_PARAGRAPH_DOWN_SELECT:
790 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
791 needsUpdate |= cursorDownParagraph(cur);
794 case LFUN_LINE_BEGIN:
795 case LFUN_LINE_BEGIN_SELECT:
796 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
797 needsUpdate |= tm->cursorHome(cur);
801 case LFUN_LINE_END_SELECT:
802 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
803 needsUpdate |= tm->cursorEnd(cur);
806 case LFUN_SECTION_SELECT: {
807 Buffer const & buf = *cur.buffer();
808 pit_type const pit = cur.pit();
809 ParagraphList & pars = buf.text().paragraphs();
810 ParagraphList::iterator bgn = pars.begin();
811 // The first paragraph of the area to be selected:
812 ParagraphList::iterator start = lyx::next(bgn, pit);
813 // The final paragraph of area to be selected:
814 ParagraphList::iterator finish = start;
815 ParagraphList::iterator end = pars.end();
817 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
818 if (thistoclevel == Layout::NOT_IN_TOC)
822 Cursor const old_cur = cur;
823 needsUpdate |= cur.selHandle(true);
825 // Move out (down) from this section header
829 // Seek the one (on same level) below
830 for (; finish != end; ++finish, ++cur.pit()) {
831 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
832 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
835 cur.pos() = cur.lastpos();
837 needsUpdate |= cur != old_cur;
841 case LFUN_WORD_RIGHT:
842 case LFUN_WORD_RIGHT_SELECT:
843 if (lyxrc.visual_cursor) {
844 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
845 bool const cur_moved = cursorVisRightOneWord(cur);
846 needsUpdate |= cur_moved;
847 if (!cur_moved && oldTopSlice == cur.top()
848 && cur.boundary() == oldBoundary) {
850 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
853 if (cur.reverseDirectionNeeded()) {
854 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
855 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
857 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
858 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
865 case LFUN_WORD_FORWARD:
866 case LFUN_WORD_FORWARD_SELECT: {
867 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
868 bool const cur_moved = cursorForwardOneWord(cur);
869 needsUpdate |= cur_moved;
871 if (!cur_moved && oldTopSlice == cur.top()
872 && cur.boundary() == oldBoundary) {
874 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
876 // we will probably be moving out the inset, so we should execute
877 // the depm-mechanism, but only when the cursor has a place to
878 // go outside this inset, i.e. in a slice above.
879 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
880 && cur.pit() == cur.lastpit()) {
881 // The cursor hasn't changed yet. To give the
882 // DEPM the possibility of doing something we must
883 // provide it with two different cursors.
885 dummy.pos() = dummy.pit() = 0;
886 if (cur.bv().checkDepm(dummy, cur))
887 cur.forceBufferUpdate();
894 case LFUN_WORD_LEFT_SELECT:
895 if (lyxrc.visual_cursor) {
896 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
897 bool const cur_moved = cursorVisLeftOneWord(cur);
898 needsUpdate |= cur_moved;
899 if (!cur_moved && oldTopSlice == cur.top()
900 && cur.boundary() == oldBoundary) {
902 cmd = FuncRequest(LFUN_FINISHED_LEFT);
905 if (cur.reverseDirectionNeeded()) {
906 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
907 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
909 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
910 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
917 case LFUN_WORD_BACKWARD:
918 case LFUN_WORD_BACKWARD_SELECT: {
919 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
920 bool const cur_moved = cursorBackwardOneWord(cur);
921 needsUpdate |= cur_moved;
923 if (!cur_moved && oldTopSlice == cur.top()
924 && cur.boundary() == oldBoundary) {
926 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
928 // we will probably be moving out the inset, so we should execute
929 // the depm-mechanism, but only when the cursor has a place to
930 // go outside this inset, i.e. in a slice above.
931 if (cur.depth() > 1 && cur.pos() == 0
933 // The cursor hasn't changed yet. To give the
934 // DEPM the possibility of doing something we must
935 // provide it with two different cursors.
937 dummy.pos() = cur.lastpos();
938 dummy.pit() = cur.lastpit();
939 if (cur.bv().checkDepm(dummy, cur))
940 cur.forceBufferUpdate();
946 case LFUN_WORD_SELECT: {
947 selectWord(cur, WHOLE_WORD);
948 finishChange(cur, true);
952 case LFUN_NEWLINE_INSERT: {
953 InsetNewlineParams inp;
954 docstring arg = cmd.argument();
955 if (arg == "linebreak")
956 inp.kind = InsetNewlineParams::LINEBREAK;
958 inp.kind = InsetNewlineParams::NEWLINE;
959 cap::replaceSelection(cur);
961 cur.insert(new InsetNewline(inp));
963 moveCursor(cur, false);
967 case LFUN_TAB_INSERT: {
968 bool const multi_par_selection = cur.selection() &&
969 cur.selBegin().pit() != cur.selEnd().pit();
970 if (multi_par_selection) {
971 // If there is a multi-paragraph selection, a tab is inserted
972 // at the beginning of each paragraph.
973 cur.recordUndoSelection();
974 pit_type const pit_end = cur.selEnd().pit();
975 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
976 pars_[pit].insertChar(0, '\t',
977 bv->buffer().params().track_changes);
978 // Update the selection pos to make sure the selection does not
979 // change as the inserted tab will increase the logical pos.
980 if (cur.realAnchor().pit() == pit)
981 cur.realAnchor().forwardPos();
982 if (cur.pit() == pit)
987 // Maybe we shouldn't allow tabs within a line, because they
988 // are not (yet) aligned as one might do expect.
989 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
995 case LFUN_TAB_DELETE: {
996 bool const tc = bv->buffer().params().track_changes;
997 if (cur.selection()) {
998 // If there is a selection, a tab (if present) is removed from
999 // the beginning of each paragraph.
1000 cur.recordUndoSelection();
1001 pit_type const pit_end = cur.selEnd().pit();
1002 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1003 Paragraph & par = paragraphs()[pit];
1006 char_type const c = par.getChar(0);
1007 if (c == '\t' || c == ' ') {
1008 // remove either 1 tab or 4 spaces.
1009 int const n = (c == ' ' ? 4 : 1);
1010 for (int i = 0; i < n
1011 && !par.empty() && par.getChar(0) == c; ++i) {
1012 if (cur.pit() == pit)
1014 if (cur.realAnchor().pit() == pit
1015 && cur.realAnchor().pos() > 0 )
1016 cur.realAnchor().backwardPos();
1017 par.eraseChar(0, tc);
1023 // If there is no selection, try to remove a tab or some spaces
1024 // before the position of the cursor.
1025 Paragraph & par = paragraphs()[cur.pit()];
1026 pos_type const pos = cur.pos();
1031 char_type const c = par.getChar(pos - 1);
1035 par.eraseChar(cur.pos(), tc);
1037 for (int n_spaces = 0;
1039 && par.getChar(cur.pos() - 1) == ' '
1043 par.eraseChar(cur.pos(), tc);
1050 case LFUN_CHAR_DELETE_FORWARD:
1051 if (!cur.selection()) {
1052 if (cur.pos() == cur.paragraph().size())
1053 // Par boundary, force full-screen update
1054 singleParUpdate = false;
1055 needsUpdate |= erase(cur);
1058 cutSelection(cur, true, false);
1059 singleParUpdate = false;
1061 moveCursor(cur, false);
1064 case LFUN_CHAR_DELETE_BACKWARD:
1065 if (!cur.selection()) {
1066 if (bv->getIntl().getTransManager().backspace()) {
1067 bool par_boundary = cur.pos() == 0;
1068 bool first_par = cur.pit() == 0;
1069 // Par boundary, full-screen update
1071 singleParUpdate = false;
1072 needsUpdate |= backspace(cur);
1074 if (par_boundary && !first_par && cur.pos() > 0
1075 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1076 needsUpdate |= backspace(cur);
1081 cutSelection(cur, true, false);
1082 singleParUpdate = false;
1086 case LFUN_PARAGRAPH_BREAK: {
1087 cap::replaceSelection(cur);
1088 pit_type pit = cur.pit();
1089 Paragraph const & par = pars_[pit];
1090 bool lastpar = (pit == pit_type(pars_.size() - 1));
1091 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1092 pit_type prev = pit;
1094 if (!pars_[pit - 1].layout().isEnvironment())
1095 prev = depthHook(pit, par.getDepth());
1096 else if (pars_[pit - 1].getDepth() >= par.getDepth())
1099 if (prev < pit && cur.pos() == par.beginOfBody()
1100 && !par.size() && !par.isEnvSeparator(cur.pos())
1101 && !par.layout().isCommand()
1102 && pars_[prev].layout() != par.layout()
1103 && pars_[prev].layout().isEnvironment()
1104 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1105 if (par.layout().isEnvironment()
1106 && pars_[prev].getDepth() == par.getDepth()) {
1107 docstring const layout = par.layout().name();
1108 DocumentClass const & tc = bv->buffer().params().documentClass();
1109 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1110 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1111 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1112 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1114 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1115 breakParagraph(cur);
1117 Font const f(inherit_font, cur.current_font.language());
1118 pars_[cur.pit() - 1].resetFonts(f);
1120 if (par.isEnvSeparator(cur.pos()))
1122 breakParagraph(cur, cmd.argument() == "inverse");
1125 // If we have a list and autoinsert item insets,
1127 Layout::LaTeXArgMap args = par.layout().args();
1128 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1129 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1130 for (; lait != laend; ++lait) {
1131 Layout::latexarg arg = (*lait).second;
1132 if (arg.autoinsert && prefixIs((*lait).first, "item:")) {
1133 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1140 case LFUN_INSET_INSERT: {
1143 // We have to avoid triggering InstantPreview loading
1144 // before inserting into the document. See bug #5626.
1145 bool loaded = bv->buffer().isFullyLoaded();
1146 bv->buffer().setFullyLoaded(false);
1147 Inset * inset = createInset(&bv->buffer(), cmd);
1148 bv->buffer().setFullyLoaded(loaded);
1151 // FIXME (Abdel 01/02/2006):
1152 // What follows would be a partial fix for bug 2154:
1153 // http://www.lyx.org/trac/ticket/2154
1154 // This automatically put the label inset _after_ a
1155 // numbered section. It should be possible to extend the mechanism
1156 // to any kind of LateX environement.
1157 // The correct way to fix that bug would be at LateX generation.
1158 // I'll let the code here for reference as it could be used for some
1159 // other feature like "automatic labelling".
1161 Paragraph & par = pars_[cur.pit()];
1162 if (inset->lyxCode() == LABEL_CODE
1163 && !par.layout().counter.empty()) {
1164 // Go to the end of the paragraph
1165 // Warning: Because of Change-Tracking, the last
1166 // position is 'size()' and not 'size()-1':
1167 cur.pos() = par.size();
1168 // Insert a new paragraph
1169 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1173 if (cur.selection())
1174 cutSelection(cur, true, false);
1176 if (inset->editable() && inset->asInsetText())
1177 inset->edit(cur, true);
1181 // trigger InstantPreview now
1182 if (inset->lyxCode() == EXTERNAL_CODE) {
1183 InsetExternal & ins =
1184 static_cast<InsetExternal &>(*inset);
1185 ins.updatePreview();
1192 case LFUN_INSET_DISSOLVE: {
1193 if (dissolveInset(cur)) {
1195 cur.forceBufferUpdate();
1200 case LFUN_SET_GRAPHICS_GROUP: {
1201 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1207 string id = to_utf8(cmd.argument());
1208 string grp = graphics::getGroupParams(bv->buffer(), id);
1209 InsetGraphicsParams tmp, inspar = ins->getParams();
1212 inspar.groupId = to_utf8(cmd.argument());
1214 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1215 tmp.filename = inspar.filename;
1219 ins->setParams(inspar);
1222 case LFUN_SPACE_INSERT:
1223 if (cur.paragraph().layout().free_spacing)
1224 insertChar(cur, ' ');
1226 doInsertInset(cur, this, cmd, false, false);
1229 moveCursor(cur, false);
1232 case LFUN_SPECIALCHAR_INSERT: {
1233 string const name = to_utf8(cmd.argument());
1234 if (name == "hyphenation")
1235 specialChar(cur, InsetSpecialChar::HYPHENATION);
1236 else if (name == "ligature-break")
1237 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1238 else if (name == "slash")
1239 specialChar(cur, InsetSpecialChar::SLASH);
1240 else if (name == "nobreakdash")
1241 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1242 else if (name == "dots")
1243 specialChar(cur, InsetSpecialChar::LDOTS);
1244 else if (name == "end-of-sentence")
1245 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1246 else if (name == "menu-separator")
1247 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1248 else if (name == "lyx")
1249 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1250 else if (name == "tex")
1251 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1252 else if (name == "latex")
1253 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1254 else if (name == "latex2e")
1255 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1256 else if (name.empty())
1257 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1259 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1263 case LFUN_IPAMACRO_INSERT: {
1264 string const arg = cmd.getArg(0);
1265 if (arg == "deco") {
1266 // Open the inset, and move the current selection
1268 doInsertInset(cur, this, cmd, true, true);
1270 // Some insets are numbered, others are shown in the outline pane so
1271 // let's update the labels and the toc backend.
1272 cur.forceBufferUpdate();
1275 if (arg == "tone-falling")
1276 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1277 else if (arg == "tone-rising")
1278 ipaChar(cur, InsetIPAChar::TONE_RISING);
1279 else if (arg == "tone-high-rising")
1280 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1281 else if (arg == "tone-low-rising")
1282 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1283 else if (arg == "tone-high-rising-falling")
1284 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1285 else if (arg.empty())
1286 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1288 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1292 case LFUN_WORD_UPCASE:
1293 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1296 case LFUN_WORD_LOWCASE:
1297 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1300 case LFUN_WORD_CAPITALIZE:
1301 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1304 case LFUN_CHARS_TRANSPOSE:
1305 charsTranspose(cur);
1309 cur.message(_("Paste"));
1310 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1311 cap::replaceSelection(cur);
1313 // without argument?
1314 string const arg = to_utf8(cmd.argument());
1316 bool tryGraphics = true;
1317 if (theClipboard().isInternal())
1318 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1319 else if (theClipboard().hasTextContents()) {
1320 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1321 true, Clipboard::AnyTextType))
1322 tryGraphics = false;
1324 if (tryGraphics && theClipboard().hasGraphicsContents())
1325 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1326 } else if (isStrUnsignedInt(arg)) {
1327 // we have a numerical argument
1328 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1329 convert<unsigned int>(arg));
1330 } else if (arg == "html" || arg == "latex") {
1331 Clipboard::TextType type = (arg == "html") ?
1332 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1333 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1335 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1337 type = Clipboard::PdfGraphicsType;
1338 else if (arg == "png")
1339 type = Clipboard::PngGraphicsType;
1340 else if (arg == "jpeg")
1341 type = Clipboard::JpegGraphicsType;
1342 else if (arg == "linkback")
1343 type = Clipboard::LinkBackGraphicsType;
1344 else if (arg == "emf")
1345 type = Clipboard::EmfGraphicsType;
1346 else if (arg == "wmf")
1347 type = Clipboard::WmfGraphicsType;
1349 // we also check in getStatus()
1350 LYXERR0("Unrecognized graphics type: " << arg);
1352 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1355 bv->buffer().errors("Paste");
1356 cur.clearSelection(); // bug 393
1362 cutSelection(cur, true, true);
1363 cur.message(_("Cut"));
1368 cur.message(_("Copy"));
1371 case LFUN_SERVER_GET_XY:
1372 cur.message(from_utf8(
1373 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1374 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1377 case LFUN_SERVER_SET_XY: {
1380 istringstream is(to_utf8(cmd.argument()));
1383 lyxerr << "SETXY: Could not parse coordinates in '"
1384 << to_utf8(cmd.argument()) << endl;
1386 tm->setCursorFromCoordinates(cur, x, y);
1390 case LFUN_SERVER_GET_LAYOUT:
1391 cur.message(cur.paragraph().layout().name());
1395 docstring layout = cmd.argument();
1396 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1398 Paragraph const & para = cur.paragraph();
1399 docstring const old_layout = para.layout().name();
1400 DocumentClass const & tclass = bv->buffer().params().documentClass();
1403 layout = tclass.defaultLayoutName();
1405 if (owner_->forcePlainLayout())
1406 // in this case only the empty layout is allowed
1407 layout = tclass.plainLayoutName();
1408 else if (para.usePlainLayout()) {
1409 // in this case, default layout maps to empty layout
1410 if (layout == tclass.defaultLayoutName())
1411 layout = tclass.plainLayoutName();
1413 // otherwise, the empty layout maps to the default
1414 if (layout == tclass.plainLayoutName())
1415 layout = tclass.defaultLayoutName();
1418 bool hasLayout = tclass.hasLayout(layout);
1420 // If the entry is obsolete, use the new one instead.
1422 docstring const & obs = tclass[layout].obsoleted_by();
1428 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1429 from_utf8(N_(" not known")));
1433 bool change_layout = (old_layout != layout);
1435 if (!change_layout && cur.selection() &&
1436 cur.selBegin().pit() != cur.selEnd().pit())
1438 pit_type spit = cur.selBegin().pit();
1439 pit_type epit = cur.selEnd().pit() + 1;
1440 while (spit != epit) {
1441 if (pars_[spit].layout().name() != old_layout) {
1442 change_layout = true;
1450 setLayout(cur, layout);
1452 Layout::LaTeXArgMap args = tclass[layout].args();
1453 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1454 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1455 for (; lait != laend; ++lait) {
1456 Layout::latexarg arg = (*lait).second;
1457 if (arg.autoinsert) {
1458 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1466 case LFUN_ENVIRONMENT_SPLIT: {
1467 bool const outer = cmd.argument() == "outer";
1468 Paragraph const & para = cur.paragraph();
1469 docstring layout = para.layout().name();
1470 depth_type split_depth = cur.paragraph().params().depth();
1472 // check if we have an environment in our nesting hierarchy
1473 pit_type pit = cur.pit();
1474 Paragraph cpar = pars_[pit];
1476 if (pit == 0 || cpar.params().depth() == 0)
1480 if (cpar.params().depth() < split_depth
1481 && cpar.layout().isEnvironment()) {
1482 layout = cpar.layout().name();
1483 split_depth = cpar.params().depth();
1488 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1490 while (cur.paragraph().params().depth() > split_depth)
1491 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1493 DocumentClass const & tc = bv->buffer().params().documentClass();
1494 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1495 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1496 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1497 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1502 case LFUN_CLIPBOARD_PASTE:
1503 cap::replaceSelection(cur);
1504 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1505 cmd.argument() == "paragraph");
1506 bv->buffer().errors("Paste");
1509 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1510 cap::replaceSelection(cur);
1511 pasteSimpleText(cur, cmd.argument() == "paragraph");
1514 case LFUN_PRIMARY_SELECTION_PASTE:
1515 cap::replaceSelection(cur);
1516 pasteString(cur, theSelection().get(),
1517 cmd.argument() == "paragraph");
1520 case LFUN_SELECTION_PASTE:
1521 // Copy the selection buffer to the clipboard stack,
1522 // because we want it to appear in the "Edit->Paste
1524 cap::replaceSelection(cur);
1525 cap::copySelectionToStack();
1526 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1527 bv->buffer().errors("Paste");
1530 case LFUN_UNICODE_INSERT: {
1531 if (cmd.argument().empty())
1533 docstring hexstring = cmd.argument();
1534 if (isHex(hexstring)) {
1535 char_type c = hexToInt(hexstring);
1536 if (c >= 32 && c < 0x10ffff) {
1537 lyxerr << "Inserting c: " << c << endl;
1538 docstring s = docstring(1, c);
1539 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1545 case LFUN_QUOTE_INSERT: {
1546 cap::replaceSelection(cur);
1549 Paragraph const & par = cur.paragraph();
1550 pos_type pos = cur.pos();
1551 // Ignore deleted text before cursor
1552 while (pos > 0 && par.isDeleted(pos - 1))
1555 BufferParams const & bufparams = bv->buffer().params();
1557 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1558 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1560 string const arg = to_utf8(cmd.argument());
1561 if (allow_inset_quote) {
1563 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1564 c = par.getChar(pos - 1);
1565 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1566 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1567 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1570 // The cursor might have been invalidated by the replaceSelection.
1571 cur.buffer()->changed(true);
1572 string const quote_string = (arg == "single") ? "'" : "\"";
1573 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, quote_string));
1578 case LFUN_DATE_INSERT: {
1579 string const format = cmd.argument().empty()
1580 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1581 string const time = formatted_time(current_time(), format);
1582 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1586 case LFUN_MOUSE_TRIPLE:
1587 if (cmd.button() == mouse_button::button1) {
1588 tm->cursorHome(cur);
1596 case LFUN_MOUSE_DOUBLE:
1597 if (cmd.button() == mouse_button::button1) {
1598 selectWord(cur, WHOLE_WORD);
1603 // Single-click on work area
1604 case LFUN_MOUSE_PRESS: {
1605 // We are not marking a selection with the keyboard in any case.
1606 Cursor & bvcur = cur.bv().cursor();
1607 bvcur.setMark(false);
1608 switch (cmd.button()) {
1609 case mouse_button::button1:
1611 bvcur.resetAnchor();
1612 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1613 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1614 if (bvcur.wordSelection())
1615 selectWord(bvcur, WHOLE_WORD);
1618 case mouse_button::button2:
1619 if (lyxrc.mouse_middlebutton_paste) {
1620 // Middle mouse pasting.
1621 bv->mouseSetCursor(cur);
1623 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1624 "selection-paste ; primary-selection-paste paragraph"));
1626 cur.noScreenUpdate();
1629 case mouse_button::button3: {
1630 // Don't do anything if we right-click a
1631 // selection, a context menu will popup.
1632 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1633 && cur < bvcur.selectionEnd()) {
1634 cur.noScreenUpdate();
1637 if (!bv->mouseSetCursor(cur, false))
1638 cur.screenUpdateFlags(Update::FitCursor);
1644 } // switch (cmd.button())
1647 case LFUN_MOUSE_MOTION: {
1648 // Mouse motion with right or middle mouse do nothing for now.
1649 if (cmd.button() != mouse_button::button1) {
1650 cur.noScreenUpdate();
1653 // ignore motions deeper nested than the real anchor
1654 Cursor & bvcur = cur.bv().cursor();
1655 if (!bvcur.realAnchor().hasPart(cur)) {
1659 CursorSlice old = bvcur.top();
1661 int const wh = bv->workHeight();
1662 int const y = max(0, min(wh - 1, cmd.y()));
1664 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1665 cur.setTargetX(cmd.x());
1666 // Don't allow selecting a separator inset
1667 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1670 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1671 else if (cmd.y() < 0)
1672 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1673 // This is to allow jumping over large insets
1674 if (cur.top() == old) {
1676 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1677 else if (cmd.y() < 0)
1678 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1680 // We continue with our existing selection or start a new one, so don't
1681 // reset the anchor.
1682 bvcur.setCursor(cur);
1683 bvcur.selection(true);
1684 if (cur.top() == old) {
1685 // We didn't move one iota, so no need to update the screen.
1686 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1687 //cur.noScreenUpdate();
1693 case LFUN_MOUSE_RELEASE:
1694 switch (cmd.button()) {
1695 case mouse_button::button1:
1696 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1697 // If there is a new selection, update persistent selection;
1698 // otherwise, single click does not clear persistent selection
1700 if (cur.selection()) {
1701 // Finish selection. If double click,
1702 // cur is moved to the end of word by
1703 // selectWord but bvcur is current
1705 cur.bv().cursor().setSelection();
1706 // We might have removed an empty but drawn selection
1707 // (probably a margin)
1708 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1710 cur.noScreenUpdate();
1711 // FIXME: We could try to handle drag and drop of selection here.
1714 case mouse_button::button2:
1715 // Middle mouse pasting is handled at mouse press time,
1716 // see LFUN_MOUSE_PRESS.
1717 cur.noScreenUpdate();
1720 case mouse_button::button3:
1721 // Cursor was set at LFUN_MOUSE_PRESS time.
1722 // FIXME: If there is a selection we could try to handle a special
1723 // drag & drop context menu.
1724 cur.noScreenUpdate();
1727 case mouse_button::none:
1728 case mouse_button::button4:
1729 case mouse_button::button5:
1731 } // switch (cmd.button())
1735 case LFUN_SELF_INSERT: {
1736 if (cmd.argument().empty())
1739 // Automatically delete the currently selected
1740 // text and replace it with what is being
1741 // typed in now. Depends on lyxrc settings
1742 // "auto_region_delete", which defaults to
1745 if (lyxrc.auto_region_delete && cur.selection())
1746 cutSelection(cur, false, false);
1748 cur.clearSelection();
1750 docstring::const_iterator cit = cmd.argument().begin();
1751 docstring::const_iterator const end = cmd.argument().end();
1752 for (; cit != end; ++cit)
1753 bv->translateAndInsert(*cit, this, cur);
1756 moveCursor(cur, false);
1757 cur.markNewWordPosition();
1758 bv->bookmarkEditPosition();
1762 case LFUN_HREF_INSERT: {
1763 // FIXME If we're actually given an argument, shouldn't
1764 // we use it, whether or not we have a selection?
1765 docstring content = cmd.argument();
1766 if (cur.selection()) {
1767 content = cur.selectionAsString(false);
1768 cutSelection(cur, true, false);
1771 InsetCommandParams p(HYPERLINK_CODE);
1772 if (!content.empty()){
1773 // if it looks like a link, we'll put it as target,
1774 // otherwise as name (bug #8792).
1777 // regex_match(to_utf8(content), matches, link_re)
1778 // because smatch stores pointers to the substrings rather
1779 // than making copies of them. And those pointers become
1780 // invalid after regex_match returns, since it is then
1781 // being given a temporary object. (Thanks to Georg for
1782 // figuring that out.)
1783 regex const link_re("^([a-z]+):.*");
1785 string const c = to_utf8(lowercase(content));
1787 if (c.substr(0,7) == "mailto:") {
1788 p["target"] = content;
1789 p["type"] = from_ascii("mailto:");
1790 } else if (regex_match(c, matches, link_re)) {
1791 p["target"] = content;
1792 string protocol = matches.str(1);
1793 if (protocol == "file")
1794 p["type"] = from_ascii("file:");
1796 p["name"] = content;
1798 string const data = InsetCommand::params2string(p);
1800 // we need to have a target. if we already have one, then
1801 // that gets used at the default for the name, too, which
1802 // is probably what is wanted.
1803 if (p["target"].empty()) {
1804 bv->showDialog("href", data);
1806 FuncRequest fr(LFUN_INSET_INSERT, data);
1812 case LFUN_LABEL_INSERT: {
1813 InsetCommandParams p(LABEL_CODE);
1814 // Try to generate a valid label
1815 p["name"] = (cmd.argument().empty()) ?
1816 cur.getPossibleLabel() :
1818 string const data = InsetCommand::params2string(p);
1820 if (cmd.argument().empty()) {
1821 bv->showDialog("label", data);
1823 FuncRequest fr(LFUN_INSET_INSERT, data);
1829 case LFUN_INFO_INSERT: {
1831 if (cmd.argument().empty() && cur.selection()) {
1832 // if command argument is empty use current selection as parameter.
1833 docstring ds = cur.selectionAsString(false);
1834 cutSelection(cur, true, false);
1835 FuncRequest cmd0(cmd, ds);
1836 inset = createInset(cur.buffer(), cmd0);
1838 inset = createInset(cur.buffer(), cmd);
1843 insertInset(cur, inset);
1847 case LFUN_CAPTION_INSERT:
1848 case LFUN_FOOTNOTE_INSERT:
1849 case LFUN_NOTE_INSERT:
1850 case LFUN_BOX_INSERT:
1851 case LFUN_BRANCH_INSERT:
1852 case LFUN_PHANTOM_INSERT:
1853 case LFUN_ERT_INSERT:
1854 case LFUN_LISTING_INSERT:
1855 case LFUN_MARGINALNOTE_INSERT:
1856 case LFUN_ARGUMENT_INSERT:
1857 case LFUN_INDEX_INSERT:
1858 case LFUN_PREVIEW_INSERT:
1859 case LFUN_SCRIPT_INSERT:
1860 case LFUN_IPA_INSERT:
1861 // Open the inset, and move the current selection
1863 doInsertInset(cur, this, cmd, true, true);
1865 // Some insets are numbered, others are shown in the outline pane so
1866 // let's update the labels and the toc backend.
1867 cur.forceBufferUpdate();
1870 case LFUN_FLEX_INSERT: {
1871 // Open the inset, and move the current selection
1873 bool const sel = cur.selection();
1874 doInsertInset(cur, this, cmd, true, true);
1875 // Insert auto-insert arguments
1876 bool autoargs = false;
1877 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1878 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1879 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1880 for (; lait != laend; ++lait) {
1881 Layout::latexarg arg = (*lait).second;
1882 if (arg.autoinsert) {
1883 // The cursor might have been invalidated by the replaceSelection.
1884 cur.buffer()->changed(true);
1885 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1892 cur.leaveInset(cur.inset());
1895 // Some insets are numbered, others are shown in the outline pane so
1896 // let's update the labels and the toc backend.
1897 cur.forceBufferUpdate();
1901 case LFUN_TABULAR_INSERT:
1902 // if there were no arguments, just open the dialog
1903 if (doInsertInset(cur, this, cmd, false, true))
1906 bv->showDialog("tabularcreate");
1910 case LFUN_FLOAT_INSERT:
1911 case LFUN_FLOAT_WIDE_INSERT:
1912 case LFUN_WRAP_INSERT: {
1913 // will some content be moved into the inset?
1914 bool const content = cur.selection();
1915 // does the content consist of multiple paragraphs?
1916 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1918 doInsertInset(cur, this, cmd, true, true);
1921 // If some single-par content is moved into the inset,
1922 // doInsertInset puts the cursor outside the inset.
1923 // To insert the caption we put it back into the inset.
1924 // FIXME cleanup doInsertInset to avoid such dances!
1925 if (content && singlepar)
1928 ParagraphList & pars = cur.text()->paragraphs();
1930 DocumentClass const & tclass = bv->buffer().params().documentClass();
1932 // add a separate paragraph for the caption inset
1933 pars.push_back(Paragraph());
1934 pars.back().setInsetOwner(&cur.text()->inset());
1935 pars.back().setPlainOrDefaultLayout(tclass);
1936 int cap_pit = pars.size() - 1;
1938 // if an empty inset was created, we create an additional empty
1939 // paragraph at the bottom so that the user can choose where to put
1940 // the graphics (or table).
1942 pars.push_back(Paragraph());
1943 pars.back().setInsetOwner(&cur.text()->inset());
1944 pars.back().setPlainOrDefaultLayout(tclass);
1947 // reposition the cursor to the caption
1948 cur.pit() = cap_pit;
1950 // FIXME: This Text/Cursor dispatch handling is a mess!
1951 // We cannot use Cursor::dispatch here it needs access to up to
1953 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1954 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1955 cur.forceBufferUpdate();
1956 cur.screenUpdateFlags(Update::Force);
1957 // FIXME: When leaving the Float (or Wrap) inset we should
1958 // delete any empty paragraph left above or below the
1963 case LFUN_NOMENCL_INSERT: {
1964 InsetCommandParams p(NOMENCL_CODE);
1965 if (cmd.argument().empty())
1966 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1968 p["symbol"] = cmd.argument();
1969 string const data = InsetCommand::params2string(p);
1970 bv->showDialog("nomenclature", data);
1974 case LFUN_INDEX_PRINT: {
1975 InsetCommandParams p(INDEX_PRINT_CODE);
1976 if (cmd.argument().empty())
1977 p["type"] = from_ascii("idx");
1979 p["type"] = cmd.argument();
1980 string const data = InsetCommand::params2string(p);
1981 FuncRequest fr(LFUN_INSET_INSERT, data);
1986 case LFUN_NOMENCL_PRINT:
1987 case LFUN_NEWPAGE_INSERT:
1989 doInsertInset(cur, this, cmd, false, false);
1993 case LFUN_SEPARATOR_INSERT: {
1994 doInsertInset(cur, this, cmd, false, false);
1996 // remove a following space
1997 Paragraph & par = cur.paragraph();
1998 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
1999 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2003 case LFUN_DEPTH_DECREMENT:
2004 changeDepth(cur, DEC_DEPTH);
2007 case LFUN_DEPTH_INCREMENT:
2008 changeDepth(cur, INC_DEPTH);
2011 case LFUN_REGEXP_MODE:
2012 regexpDispatch(cur, cmd);
2015 case LFUN_MATH_MODE: {
2016 if (cmd.argument() == "on" || cmd.argument() == "") {
2017 // don't pass "on" as argument
2018 // (it would appear literally in the first cell)
2019 docstring sel = cur.selectionAsString(false);
2020 MathMacroTemplate * macro = new MathMacroTemplate(cur.buffer());
2021 // create a macro template if we see "\\newcommand" somewhere, and
2022 // an ordinary formula otherwise
2024 && (sel.find(from_ascii("\\newcommand")) != string::npos
2025 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2026 || sel.find(from_ascii("\\def")) != string::npos)
2027 && macro->fromString(sel)) {
2029 replaceSelection(cur);
2032 // no meaningful macro template was found
2034 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2037 // The argument is meaningful
2038 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2039 // has a different meaning in math mode
2040 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2044 case LFUN_MATH_MACRO:
2045 if (cmd.argument().empty())
2046 cur.errorMessage(from_utf8(N_("Missing argument")));
2049 string s = to_utf8(cmd.argument());
2050 string const s1 = token(s, ' ', 1);
2051 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2052 string const s2 = token(s, ' ', 2);
2053 MacroType type = MacroTypeNewcommand;
2055 type = MacroTypeDef;
2056 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
2057 from_utf8(token(s, ' ', 0)), nargs, false, type);
2058 inset->setBuffer(bv->buffer());
2059 insertInset(cur, inset);
2061 // enter macro inset and select the name
2063 cur.top().pos() = cur.top().lastpos();
2065 cur.selection(true);
2066 cur.top().pos() = 0;
2070 case LFUN_MATH_DISPLAY:
2071 case LFUN_MATH_SUBSCRIPT:
2072 case LFUN_MATH_SUPERSCRIPT:
2073 case LFUN_MATH_INSERT:
2074 case LFUN_MATH_AMS_MATRIX:
2075 case LFUN_MATH_MATRIX:
2076 case LFUN_MATH_DELIM:
2077 case LFUN_MATH_BIGDELIM:
2078 mathDispatch(cur, cmd);
2081 case LFUN_FONT_EMPH: {
2082 Font font(ignore_font, ignore_language);
2083 font.fontInfo().setEmph(FONT_TOGGLE);
2084 toggleAndShow(cur, this, font);
2088 case LFUN_FONT_ITAL: {
2089 Font font(ignore_font, ignore_language);
2090 font.fontInfo().setShape(ITALIC_SHAPE);
2091 toggleAndShow(cur, this, font);
2095 case LFUN_FONT_BOLD:
2096 case LFUN_FONT_BOLDSYMBOL: {
2097 Font font(ignore_font, ignore_language);
2098 font.fontInfo().setSeries(BOLD_SERIES);
2099 toggleAndShow(cur, this, font);
2103 case LFUN_FONT_NOUN: {
2104 Font font(ignore_font, ignore_language);
2105 font.fontInfo().setNoun(FONT_TOGGLE);
2106 toggleAndShow(cur, this, font);
2110 case LFUN_FONT_TYPEWRITER: {
2111 Font font(ignore_font, ignore_language);
2112 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2113 toggleAndShow(cur, this, font);
2117 case LFUN_FONT_SANS: {
2118 Font font(ignore_font, ignore_language);
2119 font.fontInfo().setFamily(SANS_FAMILY);
2120 toggleAndShow(cur, this, font);
2124 case LFUN_FONT_ROMAN: {
2125 Font font(ignore_font, ignore_language);
2126 font.fontInfo().setFamily(ROMAN_FAMILY);
2127 toggleAndShow(cur, this, font);
2131 case LFUN_FONT_DEFAULT: {
2132 Font font(inherit_font, ignore_language);
2133 toggleAndShow(cur, this, font);
2137 case LFUN_FONT_STRIKEOUT: {
2138 Font font(ignore_font, ignore_language);
2139 font.fontInfo().setStrikeout(FONT_TOGGLE);
2140 toggleAndShow(cur, this, font);
2144 case LFUN_FONT_UNDERUNDERLINE: {
2145 Font font(ignore_font, ignore_language);
2146 font.fontInfo().setUuline(FONT_TOGGLE);
2147 toggleAndShow(cur, this, font);
2151 case LFUN_FONT_UNDERWAVE: {
2152 Font font(ignore_font, ignore_language);
2153 font.fontInfo().setUwave(FONT_TOGGLE);
2154 toggleAndShow(cur, this, font);
2158 case LFUN_FONT_UNDERLINE: {
2159 Font font(ignore_font, ignore_language);
2160 font.fontInfo().setUnderbar(FONT_TOGGLE);
2161 toggleAndShow(cur, this, font);
2165 case LFUN_FONT_SIZE: {
2166 Font font(ignore_font, ignore_language);
2167 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2168 toggleAndShow(cur, this, font);
2172 case LFUN_LANGUAGE: {
2173 string const lang_arg = cmd.getArg(0);
2174 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2175 Language const * lang =
2176 reset ? reset_language
2177 : languages.getLanguage(lang_arg);
2178 // we allow reset_language, which is 0, but only if it
2179 // was requested via empty or "reset" arg.
2180 if (!lang && !reset)
2182 bool const toggle = (cmd.getArg(1) != "set");
2183 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2184 Font font(ignore_font, lang);
2185 toggleAndShow(cur, this, font, toggle);
2189 case LFUN_TEXTSTYLE_APPLY:
2190 toggleAndShow(cur, this, freefont, toggleall);
2191 cur.message(_("Character set"));
2194 // Set the freefont using the contents of \param data dispatched from
2195 // the frontends and apply it at the current cursor location.
2196 case LFUN_TEXTSTYLE_UPDATE: {
2199 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2202 toggleAndShow(cur, this, freefont, toggleall);
2203 cur.message(_("Character set"));
2205 lyxerr << "Argument not ok";
2210 case LFUN_FINISHED_LEFT:
2211 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2212 // We're leaving an inset, going left. If the inset is LTR, we're
2213 // leaving from the front, so we should not move (remain at --- but
2214 // not in --- the inset). If the inset is RTL, move left, without
2215 // entering the inset itself; i.e., move to after the inset.
2216 if (cur.paragraph().getFontSettings(
2217 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2218 cursorVisLeft(cur, true);
2221 case LFUN_FINISHED_RIGHT:
2222 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2223 // We're leaving an inset, going right. If the inset is RTL, we're
2224 // leaving from the front, so we should not move (remain at --- but
2225 // not in --- the inset). If the inset is LTR, move right, without
2226 // entering the inset itself; i.e., move to after the inset.
2227 if (!cur.paragraph().getFontSettings(
2228 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2229 cursorVisRight(cur, true);
2232 case LFUN_FINISHED_BACKWARD:
2233 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2234 cur.setCurrentFont();
2237 case LFUN_FINISHED_FORWARD:
2238 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2240 cur.setCurrentFont();
2243 case LFUN_LAYOUT_PARAGRAPH: {
2245 params2string(cur.paragraph(), data);
2246 data = "show\n" + data;
2247 bv->showDialog("paragraph", data);
2251 case LFUN_PARAGRAPH_UPDATE: {
2253 params2string(cur.paragraph(), data);
2255 // Will the paragraph accept changes from the dialog?
2257 cur.inset().allowParagraphCustomization(cur.idx());
2259 data = "update " + convert<string>(accept) + '\n' + data;
2260 bv->updateDialog("paragraph", data);
2264 case LFUN_ACCENT_UMLAUT:
2265 case LFUN_ACCENT_CIRCUMFLEX:
2266 case LFUN_ACCENT_GRAVE:
2267 case LFUN_ACCENT_ACUTE:
2268 case LFUN_ACCENT_TILDE:
2269 case LFUN_ACCENT_PERISPOMENI:
2270 case LFUN_ACCENT_CEDILLA:
2271 case LFUN_ACCENT_MACRON:
2272 case LFUN_ACCENT_DOT:
2273 case LFUN_ACCENT_UNDERDOT:
2274 case LFUN_ACCENT_UNDERBAR:
2275 case LFUN_ACCENT_CARON:
2276 case LFUN_ACCENT_BREVE:
2277 case LFUN_ACCENT_TIE:
2278 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2279 case LFUN_ACCENT_CIRCLE:
2280 case LFUN_ACCENT_OGONEK:
2281 theApp()->handleKeyFunc(cmd.action());
2282 if (!cmd.argument().empty())
2283 // FIXME: Are all these characters encoded in one byte in utf8?
2284 bv->translateAndInsert(cmd.argument()[0], this, cur);
2285 cur.screenUpdateFlags(Update::FitCursor);
2288 case LFUN_FLOAT_LIST_INSERT: {
2289 DocumentClass const & tclass = bv->buffer().params().documentClass();
2290 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2292 if (cur.selection())
2293 cutSelection(cur, true, false);
2294 breakParagraph(cur);
2296 if (cur.lastpos() != 0) {
2297 cursorBackward(cur);
2298 breakParagraph(cur);
2301 docstring const laystr = cur.inset().usePlainLayout() ?
2302 tclass.plainLayoutName() :
2303 tclass.defaultLayoutName();
2304 setLayout(cur, laystr);
2305 ParagraphParameters p;
2306 // FIXME If this call were replaced with one to clearParagraphParams(),
2307 // then we could get rid of this method altogether.
2308 setParagraphs(cur, p);
2309 // FIXME This should be simplified when InsetFloatList takes a
2310 // Buffer in its constructor.
2311 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2312 ifl->setBuffer(bv->buffer());
2313 insertInset(cur, ifl);
2316 lyxerr << "Non-existent float type: "
2317 << to_utf8(cmd.argument()) << endl;
2322 case LFUN_CHANGE_ACCEPT: {
2323 acceptOrRejectChanges(cur, ACCEPT);
2327 case LFUN_CHANGE_REJECT: {
2328 acceptOrRejectChanges(cur, REJECT);
2332 case LFUN_THESAURUS_ENTRY: {
2333 Language const * language = cur.getFont().language();
2334 docstring arg = cmd.argument();
2336 arg = cur.selectionAsString(false);
2338 if (arg.size() > 100 || arg.empty()) {
2339 // Get word or selection
2340 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2341 arg = cur.selectionAsString(false);
2342 arg += " lang=" + from_ascii(language->lang());
2345 string lang = cmd.getArg(1);
2346 // This duplicates the code in GuiThesaurus::initialiseParams
2347 if (prefixIs(lang, "lang=")) {
2348 language = languages.getLanguage(lang.substr(5));
2350 language = cur.getFont().language();
2353 string lang = language->code();
2354 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2355 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2356 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2357 _("The path to the thesaurus directory has not been specified.\n"
2358 "The thesaurus is not functional.\n"
2359 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2362 bv->showDialog("thesaurus", to_utf8(arg));
2366 case LFUN_SPELLING_ADD: {
2367 Language const * language = getLanguage(cur, cmd.getArg(1));
2368 docstring word = from_utf8(cmd.getArg(0));
2370 word = cur.selectionAsString(false);
2372 if (word.size() > 100 || word.empty()) {
2373 // Get word or selection
2374 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2375 word = cur.selectionAsString(false);
2378 WordLangTuple wl(word, language);
2379 theSpellChecker()->insert(wl);
2383 case LFUN_SPELLING_IGNORE: {
2384 Language const * language = getLanguage(cur, cmd.getArg(1));
2385 docstring word = from_utf8(cmd.getArg(0));
2387 word = cur.selectionAsString(false);
2389 if (word.size() > 100 || word.empty()) {
2390 // Get word or selection
2391 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2392 word = cur.selectionAsString(false);
2395 WordLangTuple wl(word, language);
2396 theSpellChecker()->accept(wl);
2400 case LFUN_SPELLING_REMOVE: {
2401 Language const * language = getLanguage(cur, cmd.getArg(1));
2402 docstring word = from_utf8(cmd.getArg(0));
2404 word = cur.selectionAsString(false);
2406 if (word.size() > 100 || word.empty()) {
2407 // Get word or selection
2408 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2409 word = cur.selectionAsString(false);
2412 WordLangTuple wl(word, language);
2413 theSpellChecker()->remove(wl);
2417 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2418 // Given data, an encoding of the ParagraphParameters
2419 // generated in the Paragraph dialog, this function sets
2420 // the current paragraph, or currently selected paragraphs,
2422 // NOTE: This function overrides all existing settings.
2423 setParagraphs(cur, cmd.argument());
2424 cur.message(_("Paragraph layout set"));
2428 case LFUN_PARAGRAPH_PARAMS: {
2429 // Given data, an encoding of the ParagraphParameters as we'd
2430 // find them in a LyX file, this function modifies the current paragraph,
2431 // or currently selected paragraphs.
2432 // NOTE: This function only modifies, and does not override, existing
2434 setParagraphs(cur, cmd.argument(), true);
2435 cur.message(_("Paragraph layout set"));
2440 if (cur.selection()) {
2441 cur.selection(false);
2444 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2445 // correct, but I'm not 100% sure -- dov, 071019
2446 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2450 case LFUN_OUTLINE_UP:
2451 outline(OutlineUp, cur);
2452 setCursor(cur, cur.pit(), 0);
2453 cur.forceBufferUpdate();
2457 case LFUN_OUTLINE_DOWN:
2458 outline(OutlineDown, cur);
2459 setCursor(cur, cur.pit(), 0);
2460 cur.forceBufferUpdate();
2464 case LFUN_OUTLINE_IN:
2465 outline(OutlineIn, cur);
2466 cur.forceBufferUpdate();
2470 case LFUN_OUTLINE_OUT:
2471 outline(OutlineOut, cur);
2472 cur.forceBufferUpdate();
2476 case LFUN_SERVER_GET_STATISTICS:
2478 DocIterator from, to;
2479 if (cur.selection()) {
2480 from = cur.selectionBegin();
2481 to = cur.selectionEnd();
2483 from = doc_iterator_begin(cur.buffer());
2484 to = doc_iterator_end(cur.buffer());
2487 cur.buffer()->updateStatistics(from, to);
2488 string const arg0 = cmd.getArg(0);
2489 if (arg0 == "words") {
2490 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2491 } else if (arg0 == "chars") {
2492 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2493 } else if (arg0 == "chars-space") {
2494 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2496 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2497 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2498 + convert<docstring>(cur.buffer()->charCount(true)));
2504 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2509 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2511 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2512 // Check for misspelled text
2513 // The redraw is useful because of the painting of
2514 // misspelled markers depends on the cursor position.
2515 // Trigger a redraw for cursor moves inside misspelled text.
2516 if (!cur.inTexted()) {
2517 // move from regular text to math
2518 needsUpdate = last_misspelled;
2519 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2520 // move inside regular text
2521 needsUpdate = last_misspelled
2522 || cur.paragraph().isMisspelled(cur.pos(), true);
2526 // FIXME: The cursor flag is reset two lines below
2527 // so we need to check here if some of the LFUN did touch that.
2528 // for now only Text::erase() and Text::backspace() do that.
2529 // The plan is to verify all the LFUNs and then to remove this
2530 // singleParUpdate boolean altogether.
2531 if (cur.result().screenUpdate() & Update::Force) {
2532 singleParUpdate = false;
2536 // FIXME: the following code should go in favor of fine grained
2537 // update flag treatment.
2538 if (singleParUpdate) {
2539 // Inserting characters does not change par height in general. So, try
2540 // to update _only_ this paragraph. BufferView will detect if a full
2541 // metrics update is needed anyway.
2542 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2546 && &oldTopSlice.inset() == &cur.inset()
2547 && oldTopSlice.idx() == cur.idx()
2548 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2549 && !cur.selection())
2550 // FIXME: it would be better if we could just do this
2552 //if (cur.result().update() != Update::FitCursor)
2553 // cur.noScreenUpdate();
2555 // But some LFUNs do not set Update::FitCursor when needed, so we
2556 // do it for all. This is not very harmfull as FitCursor will provoke
2557 // a full redraw only if needed but still, a proper review of all LFUN
2558 // should be done and this needsUpdate boolean can then be removed.
2559 cur.screenUpdateFlags(Update::FitCursor);
2561 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2565 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2566 FuncStatus & flag) const
2568 LBUFERR(this == cur.text());
2570 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2572 bool allow_in_passthru = false;
2573 InsetCode code = NO_CODE;
2575 switch (cmd.action()) {
2577 case LFUN_DEPTH_DECREMENT:
2578 enable = changeDepthAllowed(cur, DEC_DEPTH);
2581 case LFUN_DEPTH_INCREMENT:
2582 enable = changeDepthAllowed(cur, INC_DEPTH);
2586 // FIXME We really should not allow this to be put, e.g.,
2587 // in a footnote, or in ERT. But it would make sense in a
2588 // branch, so I'm not sure what to do.
2589 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2592 case LFUN_DIALOG_SHOW_NEW_INSET:
2593 if (cmd.argument() == "bibitem")
2594 code = BIBITEM_CODE;
2595 else if (cmd.argument() == "bibtex") {
2597 // not allowed in description items
2598 enable = !inDescriptionItem(cur);
2600 else if (cmd.argument() == "box")
2602 else if (cmd.argument() == "branch")
2604 else if (cmd.argument() == "citation")
2606 else if (cmd.argument() == "ert")
2608 else if (cmd.argument() == "external")
2609 code = EXTERNAL_CODE;
2610 else if (cmd.argument() == "float")
2612 else if (cmd.argument() == "graphics")
2613 code = GRAPHICS_CODE;
2614 else if (cmd.argument() == "href")
2615 code = HYPERLINK_CODE;
2616 else if (cmd.argument() == "include")
2617 code = INCLUDE_CODE;
2618 else if (cmd.argument() == "index")
2620 else if (cmd.argument() == "index_print")
2621 code = INDEX_PRINT_CODE;
2622 else if (cmd.argument() == "listings")
2623 code = LISTINGS_CODE;
2624 else if (cmd.argument() == "mathspace")
2625 code = MATH_HULL_CODE;
2626 else if (cmd.argument() == "nomenclature")
2627 code = NOMENCL_CODE;
2628 else if (cmd.argument() == "nomencl_print")
2629 code = NOMENCL_PRINT_CODE;
2630 else if (cmd.argument() == "label")
2632 else if (cmd.argument() == "line")
2634 else if (cmd.argument() == "note")
2636 else if (cmd.argument() == "phantom")
2637 code = PHANTOM_CODE;
2638 else if (cmd.argument() == "ref")
2640 else if (cmd.argument() == "space")
2642 else if (cmd.argument() == "toc")
2644 else if (cmd.argument() == "vspace")
2646 else if (cmd.argument() == "wrap")
2650 case LFUN_ERT_INSERT:
2653 case LFUN_LISTING_INSERT:
2654 code = LISTINGS_CODE;
2655 // not allowed in description items
2656 enable = !inDescriptionItem(cur);
2658 case LFUN_FOOTNOTE_INSERT:
2661 case LFUN_TABULAR_INSERT:
2662 code = TABULAR_CODE;
2664 case LFUN_MARGINALNOTE_INSERT:
2667 case LFUN_FLOAT_INSERT:
2668 case LFUN_FLOAT_WIDE_INSERT:
2669 // FIXME: If there is a selection, we should check whether there
2670 // are floats in the selection, but this has performance issues, see
2671 // LFUN_CHANGE_ACCEPT/REJECT.
2673 if (inDescriptionItem(cur))
2674 // not allowed in description items
2677 InsetCode const inset_code = cur.inset().lyxCode();
2679 // algorithm floats cannot be put in another float
2680 if (to_utf8(cmd.argument()) == "algorithm") {
2681 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2685 // for figures and tables: only allow in another
2686 // float or wrap if it is of the same type and
2687 // not a subfloat already
2688 if(cur.inset().lyxCode() == code) {
2689 InsetFloat const & ins =
2690 static_cast<InsetFloat const &>(cur.inset());
2691 enable = ins.params().type == to_utf8(cmd.argument())
2692 && !ins.params().subfloat;
2693 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2694 InsetWrap const & ins =
2695 static_cast<InsetWrap const &>(cur.inset());
2696 enable = ins.params().type == to_utf8(cmd.argument());
2700 case LFUN_WRAP_INSERT:
2702 // not allowed in description items
2703 enable = !inDescriptionItem(cur);
2705 case LFUN_FLOAT_LIST_INSERT: {
2706 code = FLOAT_LIST_CODE;
2707 // not allowed in description items
2708 enable = !inDescriptionItem(cur);
2710 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2711 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2712 // make sure we know about such floats
2713 if (cit == floats.end() ||
2714 // and that we know how to generate a list of them
2715 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2716 flag.setUnknown(true);
2717 // probably not necessary, but...
2723 case LFUN_CAPTION_INSERT: {
2724 code = CAPTION_CODE;
2725 string arg = cmd.getArg(0);
2726 bool varia = arg != "Unnumbered"
2727 && cur.inset().allowsCaptionVariation(arg);
2728 // not allowed in description items,
2729 // and in specific insets
2730 enable = !inDescriptionItem(cur)
2731 && (varia || arg.empty() || arg == "Standard");
2734 case LFUN_NOTE_INSERT:
2736 // in commands (sections etc.) and description items,
2737 // only Notes are allowed
2738 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2739 (!cur.paragraph().layout().isCommand()
2740 && !inDescriptionItem(cur)));
2742 case LFUN_FLEX_INSERT: {
2744 string s = cmd.getArg(0);
2746 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2747 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2748 il.lyxtype() != InsetLayout::CUSTOM &&
2749 il.lyxtype() != InsetLayout::ELEMENT &&
2750 il.lyxtype ()!= InsetLayout::STANDARD)
2754 case LFUN_BOX_INSERT:
2757 case LFUN_BRANCH_INSERT:
2759 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2760 && cur.buffer()->params().branchlist().empty())
2763 case LFUN_IPA_INSERT:
2766 case LFUN_PHANTOM_INSERT:
2767 code = PHANTOM_CODE;
2769 case LFUN_LABEL_INSERT:
2772 case LFUN_INFO_INSERT:
2775 case LFUN_ARGUMENT_INSERT: {
2777 allow_in_passthru = true;
2778 string const arg = cmd.getArg(0);
2783 Layout const & lay = cur.paragraph().layout();
2784 Layout::LaTeXArgMap args = lay.args();
2785 Layout::LaTeXArgMap::const_iterator const lait =
2787 if (lait != args.end()) {
2789 pit_type pit = cur.pit();
2790 pit_type lastpit = cur.pit();
2791 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2792 // In a sequence of "merged" environment layouts, we only allow
2793 // non-item arguments once.
2794 lastpit = cur.lastpit();
2795 // get the first paragraph in sequence with this layout
2796 depth_type const current_depth = cur.paragraph().params().depth();
2800 Paragraph cpar = pars_[pit - 1];
2801 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2807 for (; pit <= lastpit; ++pit) {
2808 if (pars_[pit].layout() != lay)
2810 InsetList::const_iterator it = pars_[pit].insetList().begin();
2811 InsetList::const_iterator end = pars_[pit].insetList().end();
2812 for (; it != end; ++it) {
2813 if (it->inset->lyxCode() == ARG_CODE) {
2814 InsetArgument const * ins =
2815 static_cast<InsetArgument const *>(it->inset);
2816 if (ins->name() == arg) {
2817 // we have this already
2828 case LFUN_INDEX_INSERT:
2831 case LFUN_INDEX_PRINT:
2832 code = INDEX_PRINT_CODE;
2833 // not allowed in description items
2834 enable = !inDescriptionItem(cur);
2836 case LFUN_NOMENCL_INSERT:
2837 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2841 code = NOMENCL_CODE;
2843 case LFUN_NOMENCL_PRINT:
2844 code = NOMENCL_PRINT_CODE;
2845 // not allowed in description items
2846 enable = !inDescriptionItem(cur);
2848 case LFUN_HREF_INSERT:
2849 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2853 code = HYPERLINK_CODE;
2855 case LFUN_IPAMACRO_INSERT: {
2856 string const arg = cmd.getArg(0);
2858 code = IPADECO_CODE;
2860 code = IPACHAR_CODE;
2863 case LFUN_QUOTE_INSERT:
2864 // always allow this, since we will inset a raw quote
2865 // if an inset is not allowed.
2867 case LFUN_SPECIALCHAR_INSERT:
2868 code = SPECIALCHAR_CODE;
2870 case LFUN_SPACE_INSERT:
2871 // slight hack: we know this is allowed in math mode
2875 case LFUN_PREVIEW_INSERT:
2876 code = PREVIEW_CODE;
2878 case LFUN_SCRIPT_INSERT:
2882 case LFUN_MATH_INSERT:
2883 case LFUN_MATH_AMS_MATRIX:
2884 case LFUN_MATH_MATRIX:
2885 case LFUN_MATH_DELIM:
2886 case LFUN_MATH_BIGDELIM:
2887 case LFUN_MATH_DISPLAY:
2888 case LFUN_MATH_MODE:
2889 case LFUN_MATH_MACRO:
2890 case LFUN_MATH_SUBSCRIPT:
2891 case LFUN_MATH_SUPERSCRIPT:
2892 code = MATH_HULL_CODE;
2895 case LFUN_REGEXP_MODE:
2896 code = MATH_HULL_CODE;
2897 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2900 case LFUN_INSET_MODIFY:
2901 // We need to disable this, because we may get called for a
2903 // InsetTabular::getStatus() -> InsetText::getStatus()
2904 // and we don't handle LFUN_INSET_MODIFY.
2908 case LFUN_FONT_EMPH:
2909 flag.setOnOff(fontinfo.emph() == FONT_ON);
2910 enable = !cur.paragraph().isPassThru();
2913 case LFUN_FONT_ITAL:
2914 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2915 enable = !cur.paragraph().isPassThru();
2918 case LFUN_FONT_NOUN:
2919 flag.setOnOff(fontinfo.noun() == FONT_ON);
2920 enable = !cur.paragraph().isPassThru();
2923 case LFUN_FONT_BOLD:
2924 case LFUN_FONT_BOLDSYMBOL:
2925 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2926 enable = !cur.paragraph().isPassThru();
2929 case LFUN_FONT_SANS:
2930 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2931 enable = !cur.paragraph().isPassThru();
2934 case LFUN_FONT_ROMAN:
2935 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2936 enable = !cur.paragraph().isPassThru();
2939 case LFUN_FONT_TYPEWRITER:
2940 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2941 enable = !cur.paragraph().isPassThru();
2946 enable = cur.selection();
2950 if (cmd.argument().empty()) {
2951 if (theClipboard().isInternal())
2952 enable = cap::numberOfSelections() > 0;
2954 enable = !theClipboard().empty();
2958 // we have an argument
2959 string const arg = to_utf8(cmd.argument());
2960 if (isStrUnsignedInt(arg)) {
2961 // it's a number and therefore means the internal stack
2962 unsigned int n = convert<unsigned int>(arg);
2963 enable = cap::numberOfSelections() > n;
2967 // explicit text type?
2968 if (arg == "html") {
2969 // Do not enable for PlainTextType, since some tidying in the
2970 // frontend is needed for HTML, which is too unsafe for plain text.
2971 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
2973 } else if (arg == "latex") {
2974 // LaTeX is usually not available on the clipboard with
2975 // the correct MIME type, but in plain text.
2976 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
2977 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
2981 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2983 type = Clipboard::PdfGraphicsType;
2984 else if (arg == "png")
2985 type = Clipboard::PngGraphicsType;
2986 else if (arg == "jpeg")
2987 type = Clipboard::JpegGraphicsType;
2988 else if (arg == "linkback")
2989 type = Clipboard::LinkBackGraphicsType;
2990 else if (arg == "emf")
2991 type = Clipboard::EmfGraphicsType;
2992 else if (arg == "wmf")
2993 type = Clipboard::WmfGraphicsType;
2996 LYXERR0("Unrecognized graphics type: " << arg);
2997 // we don't want to assert if the user just mistyped the LFUN
2998 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3002 enable = theClipboard().hasGraphicsContents(type);
3006 case LFUN_CLIPBOARD_PASTE:
3007 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3008 enable = !theClipboard().empty();
3011 case LFUN_PRIMARY_SELECTION_PASTE:
3012 enable = cur.selection() || !theSelection().empty();
3015 case LFUN_SELECTION_PASTE:
3016 enable = cap::selection();
3019 case LFUN_PARAGRAPH_MOVE_UP:
3020 enable = cur.pit() > 0 && !cur.selection();
3023 case LFUN_PARAGRAPH_MOVE_DOWN:
3024 enable = cur.pit() < cur.lastpit() && !cur.selection();
3027 case LFUN_CHANGE_ACCEPT:
3028 case LFUN_CHANGE_REJECT:
3029 // In principle, these LFUNs should only be enabled if there
3030 // is a change at the current position/in the current selection.
3031 // However, without proper optimizations, this will inevitably
3032 // result in unacceptable performance - just imagine a user who
3033 // wants to select the complete content of a long document.
3034 if (!cur.selection())
3035 enable = cur.paragraph().isChanged(cur.pos());
3037 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3042 case LFUN_OUTLINE_UP:
3043 case LFUN_OUTLINE_DOWN:
3044 case LFUN_OUTLINE_IN:
3045 case LFUN_OUTLINE_OUT:
3046 // FIXME: LyX is not ready for outlining within inset.
3047 enable = isMainText()
3048 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3051 case LFUN_NEWLINE_INSERT:
3052 // LaTeX restrictions (labels or empty par)
3053 enable = !cur.paragraph().isPassThru()
3054 && cur.pos() > cur.paragraph().beginOfBody();
3057 case LFUN_SEPARATOR_INSERT:
3058 // Always enabled for now
3062 case LFUN_TAB_INSERT:
3063 case LFUN_TAB_DELETE:
3064 enable = cur.paragraph().isPassThru();
3067 case LFUN_SET_GRAPHICS_GROUP: {
3068 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3072 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3076 case LFUN_NEWPAGE_INSERT:
3077 // not allowed in description items
3078 code = NEWPAGE_CODE;
3079 enable = !inDescriptionItem(cur);
3082 case LFUN_DATE_INSERT: {
3083 string const format = cmd.argument().empty()
3084 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3085 enable = support::os::is_valid_strftime(format);
3090 enable = !cur.paragraph().isPassThru();
3091 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3094 case LFUN_PARAGRAPH_BREAK:
3095 enable = inset().allowMultiPar();
3098 case LFUN_SPELLING_ADD:
3099 case LFUN_SPELLING_IGNORE:
3100 case LFUN_SPELLING_REMOVE:
3101 enable = theSpellChecker() != NULL;
3102 if (enable && !cmd.getArg(1).empty()) {
3103 // validate explicitly given language
3104 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3105 enable &= lang != NULL;
3110 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3111 docstring layout = cmd.argument();
3113 layout = tclass.defaultLayoutName();
3114 enable = !owner_->forcePlainLayout() && tclass.hasLayout(layout);
3116 flag.setOnOff(layout == cur.paragraph().layout().name());
3120 case LFUN_ENVIRONMENT_SPLIT: {
3121 if (cmd.argument() == "outer") {
3122 // check if we have an environment in our nesting hierarchy
3124 depth_type const current_depth = cur.paragraph().params().depth();
3125 pit_type pit = cur.pit();
3126 Paragraph cpar = pars_[pit];
3128 if (pit == 0 || cpar.params().depth() == 0)
3132 if (cpar.params().depth() < current_depth)
3133 res = cpar.layout().isEnvironment();
3138 else if (cur.paragraph().layout().isEnvironment()) {
3146 case LFUN_LAYOUT_PARAGRAPH:
3147 case LFUN_PARAGRAPH_PARAMS:
3148 case LFUN_PARAGRAPH_PARAMS_APPLY:
3149 case LFUN_PARAGRAPH_UPDATE:
3150 enable = owner_->allowParagraphCustomization();
3153 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3154 // Because they insert COMBINING DIACRITICAL Unicode characters,
3155 // that cannot be handled by LaTeX but must be converted according
3156 // to the definition in lib/unicodesymbols?
3157 case LFUN_ACCENT_ACUTE:
3158 case LFUN_ACCENT_BREVE:
3159 case LFUN_ACCENT_CARON:
3160 case LFUN_ACCENT_CEDILLA:
3161 case LFUN_ACCENT_CIRCLE:
3162 case LFUN_ACCENT_CIRCUMFLEX:
3163 case LFUN_ACCENT_DOT:
3164 case LFUN_ACCENT_GRAVE:
3165 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3166 case LFUN_ACCENT_MACRON:
3167 case LFUN_ACCENT_OGONEK:
3168 case LFUN_ACCENT_TIE:
3169 case LFUN_ACCENT_TILDE:
3170 case LFUN_ACCENT_PERISPOMENI:
3171 case LFUN_ACCENT_UMLAUT:
3172 case LFUN_ACCENT_UNDERBAR:
3173 case LFUN_ACCENT_UNDERDOT:
3174 case LFUN_FONT_DEFAULT:
3175 case LFUN_FONT_FRAK:
3176 case LFUN_FONT_SIZE:
3177 case LFUN_FONT_STATE:
3178 case LFUN_FONT_UNDERLINE:
3179 case LFUN_FONT_STRIKEOUT:
3180 case LFUN_FONT_UNDERUNDERLINE:
3181 case LFUN_FONT_UNDERWAVE:
3182 case LFUN_TEXTSTYLE_APPLY:
3183 case LFUN_TEXTSTYLE_UPDATE:
3184 enable = !cur.paragraph().isPassThru();
3187 case LFUN_WORD_DELETE_FORWARD:
3188 case LFUN_WORD_DELETE_BACKWARD:
3189 case LFUN_LINE_DELETE_FORWARD:
3190 case LFUN_WORD_FORWARD:
3191 case LFUN_WORD_BACKWARD:
3192 case LFUN_WORD_RIGHT:
3193 case LFUN_WORD_LEFT:
3194 case LFUN_CHAR_FORWARD:
3195 case LFUN_CHAR_FORWARD_SELECT:
3196 case LFUN_CHAR_BACKWARD:
3197 case LFUN_CHAR_BACKWARD_SELECT:
3198 case LFUN_CHAR_LEFT:
3199 case LFUN_CHAR_LEFT_SELECT:
3200 case LFUN_CHAR_RIGHT:
3201 case LFUN_CHAR_RIGHT_SELECT:
3203 case LFUN_UP_SELECT:
3205 case LFUN_DOWN_SELECT:
3206 case LFUN_PARAGRAPH_UP_SELECT:
3207 case LFUN_PARAGRAPH_DOWN_SELECT:
3208 case LFUN_LINE_BEGIN_SELECT:
3209 case LFUN_LINE_END_SELECT:
3210 case LFUN_WORD_FORWARD_SELECT:
3211 case LFUN_WORD_BACKWARD_SELECT:
3212 case LFUN_WORD_RIGHT_SELECT:
3213 case LFUN_WORD_LEFT_SELECT:
3214 case LFUN_WORD_SELECT:
3215 case LFUN_SECTION_SELECT:
3216 case LFUN_BUFFER_BEGIN:
3217 case LFUN_BUFFER_END:
3218 case LFUN_BUFFER_BEGIN_SELECT:
3219 case LFUN_BUFFER_END_SELECT:
3220 case LFUN_INSET_BEGIN:
3221 case LFUN_INSET_END:
3222 case LFUN_INSET_BEGIN_SELECT:
3223 case LFUN_INSET_END_SELECT:
3224 case LFUN_PARAGRAPH_UP:
3225 case LFUN_PARAGRAPH_DOWN:
3226 case LFUN_LINE_BEGIN:
3228 case LFUN_CHAR_DELETE_FORWARD:
3229 case LFUN_CHAR_DELETE_BACKWARD:
3230 case LFUN_WORD_UPCASE:
3231 case LFUN_WORD_LOWCASE:
3232 case LFUN_WORD_CAPITALIZE:
3233 case LFUN_CHARS_TRANSPOSE:
3234 case LFUN_SERVER_GET_XY:
3235 case LFUN_SERVER_SET_XY:
3236 case LFUN_SERVER_GET_LAYOUT:
3237 case LFUN_SELF_INSERT:
3238 case LFUN_UNICODE_INSERT:
3239 case LFUN_THESAURUS_ENTRY:
3241 case LFUN_SERVER_GET_STATISTICS:
3242 // these are handled in our dispatch()
3246 case LFUN_INSET_INSERT: {
3247 string const type = cmd.getArg(0);
3248 if (type == "toc") {
3250 // not allowed in description items
3251 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3252 enable = !inDescriptionItem(cur);
3265 || !cur.inset().insetAllowed(code)
3266 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3269 flag.setEnabled(enable);
3274 void Text::pasteString(Cursor & cur, docstring const & clip,
3277 if (!clip.empty()) {
3280 insertStringAsParagraphs(cur, clip, cur.current_font);
3282 insertStringAsLines(cur, clip, cur.current_font);
3287 // FIXME: an item inset would make things much easier.
3288 bool Text::inDescriptionItem(Cursor & cur) const
3290 Paragraph & par = cur.paragraph();
3291 pos_type const pos = cur.pos();
3292 pos_type const body_pos = par.beginOfBody();
3294 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3295 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3296 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3299 return (pos < body_pos
3301 && (pos == 0 || par.getChar(pos - 1) != ' ')));