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;
102 static Font freefont(ignore_font, ignore_language);
103 static bool toggleall = false;
105 static void toggleAndShow(Cursor & cur, Text * text,
106 Font const & font, bool toggleall = true)
108 text->toggleFree(cur, font, toggleall);
110 if (font.language() != ignore_language ||
111 font.fontInfo().number() != FONT_IGNORE) {
112 TextMetrics const & tm = cur.bv().textMetrics(text);
113 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
114 cur.real_current_font))
115 text->setCursor(cur, cur.pit(), cur.pos(),
116 false, !cur.boundary());
121 static void moveCursor(Cursor & cur, bool selecting)
123 if (selecting || cur.mark())
128 static void finishChange(Cursor & cur, bool selecting)
131 moveCursor(cur, selecting);
135 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
138 docstring sel = cur.selectionAsString(false);
140 // It may happen that sel is empty but there is a selection
141 replaceSelection(cur);
143 // Is this a valid formula?
147 #ifdef ENABLE_ASSERTIONS
148 const int old_pos = cur.pos();
150 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
151 #ifdef ENABLE_ASSERTIONS
152 LATTEST(old_pos == cur.pos());
154 cur.nextInset()->edit(cur, true);
155 if (cmd.action() != LFUN_MATH_MODE)
156 // LFUN_MATH_MODE has a different meaning in math mode
159 InsetMathHull * formula = new InsetMathHull(cur.buffer());
160 string const selstr = to_utf8(sel);
161 istringstream is(selstr);
164 if (!formula->readQuiet(lex)) {
165 // No valid formula, let's try with delims
166 is.str("$" + selstr + "$");
168 if (!formula->readQuiet(lex)) {
169 // Still not valid, leave it as is
177 cur.nextInset()->edit(cur, true);
178 LASSERT(cur.inMathed(), return);
181 cur.setSelection(true);
182 cur.pos() = cur.lastpos();
183 if (cmd.action() != LFUN_MATH_MODE)
184 // LFUN_MATH_MODE has a different meaning in math mode
186 cur.clearSelection();
187 cur.pos() = cur.lastpos();
191 cur.message(from_utf8(N_("Math editor mode")));
193 cur.message(from_utf8(N_("No valid math formula")));
197 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
199 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
200 if (cur.inRegexped()) {
201 cur.message(_("Already in regular expression mode"));
205 docstring sel = cur.selectionAsString(false);
207 // It may happen that sel is empty but there is a selection
208 replaceSelection(cur);
210 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
211 cur.nextInset()->edit(cur, true);
214 cur.message(_("Regexp editor mode"));
218 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
221 cap::replaceSelection(cur);
222 cur.insert(new InsetSpecialChar(kind));
227 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
230 cap::replaceSelection(cur);
231 cur.insert(new InsetIPAChar(kind));
236 static bool doInsertInset(Cursor & cur, Text * text,
237 FuncRequest const & cmd, bool edit, bool pastesel)
239 Buffer & buffer = cur.bv().buffer();
240 BufferParams const & bparams = buffer.params();
241 Inset * inset = createInset(&buffer, cmd);
245 if (InsetCollapsable * ci = inset->asInsetCollapsable())
246 ci->setButtonLabel();
249 if (cmd.action() == LFUN_INDEX_INSERT) {
250 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
251 text->insertInset(cur, inset);
253 inset->edit(cur, true);
254 // Now put this into inset
255 Font const f(inherit_font, cur.current_font.language());
257 cur.text()->insertStringAsLines(cur, ds, f);
258 cur.leaveInset(*inset);
262 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
263 bool cotextinsert = false;
264 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
265 Layout const & lay = cur.paragraph().layout();
266 Layout::LaTeXArgMap args = lay.args();
267 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
268 if (lait != args.end())
269 cotextinsert = (*lait).second.insertcotext;
271 InsetLayout const & il = cur.inset().getLayout();
273 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
274 if (ilait != args.end())
275 cotextinsert = (*ilait).second.insertcotext;
277 // The argument requests to insert a copy of the co-text to the inset
280 // If we have a selection within a paragraph, use this
281 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
282 ds = cur.selectionAsString(false);
283 // else use the whole paragraph
285 ds = cur.paragraph().asString();
286 text->insertInset(cur, inset);
288 inset->edit(cur, true);
289 // Now put co-text into inset
290 Font const f(inherit_font, cur.current_font.language());
292 cur.text()->insertStringAsLines(cur, ds, f);
293 cur.leaveInset(*inset);
300 if (cur.selection()) {
301 cutSelection(cur, false, pastesel);
302 cur.clearSelection();
305 text->insertInset(cur, inset);
308 inset->edit(cur, true);
310 if (!gotsel || !pastesel)
313 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
314 cur.buffer()->errors("Paste");
315 cur.clearSelection(); // bug 393
317 InsetText * inset_text = inset->asInsetText();
319 inset_text->fixParagraphsFont();
320 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
321 // reset first par to default
322 cur.text()->paragraphs().begin()
323 ->setPlainOrDefaultLayout(bparams.documentClass());
326 // Merge multiple paragraphs -- hack
327 while (cur.lastpit() > 0)
328 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
329 if (cmd.action() == LFUN_FLEX_INSERT)
332 cur.leaveInset(*inset);
333 if (cmd.action() == LFUN_PREVIEW_INSERT
334 || cmd.action() == LFUN_IPA_INSERT)
336 notifyCursorLeavesOrEnters(old, cur);
339 cur.leaveInset(*inset);
340 // reset surrounding par to default
341 DocumentClass const & dc = bparams.documentClass();
342 docstring const layoutname = inset->usePlainLayout()
343 ? dc.plainLayoutName()
344 : dc.defaultLayoutName();
345 text->setLayout(cur, layoutname);
351 string const freefont2string()
353 return freefont.toString(toggleall);
357 /// the type of outline operation
359 OutlineUp, // Move this header with text down
360 OutlineDown, // Move this header with text up
361 OutlineIn, // Make this header deeper
362 OutlineOut // Make this header shallower
366 static void outline(OutlineOp mode, Cursor & cur)
368 Buffer & buf = *cur.buffer();
369 pit_type & pit = cur.pit();
370 ParagraphList & pars = buf.text().paragraphs();
371 ParagraphList::iterator const bgn = pars.begin();
372 // The first paragraph of the area to be copied:
373 ParagraphList::iterator start = lyx::next(bgn, pit);
374 // The final paragraph of area to be copied:
375 ParagraphList::iterator finish = start;
376 ParagraphList::iterator const end = pars.end();
378 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
381 // Move out (down) from this section header
385 // Seek the one (on same level) below
386 for (; finish != end; ++finish) {
387 toclevel = buf.text().getTocLevel(distance(bgn, finish));
388 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
394 if (start == pars.begin())
397 ParagraphList::iterator dest = start;
398 // Move out (up) from this header
401 // Search previous same-level header above
404 toclevel = buf.text().getTocLevel(distance(bgn, dest));
406 && (toclevel == Layout::NOT_IN_TOC
407 || toclevel > thistoclevel));
408 // Not found; do nothing
409 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
411 pit_type const newpit = distance(bgn, dest);
412 pit_type const len = distance(start, finish);
413 pit_type const deletepit = pit + len;
414 buf.undo().recordUndo(cur, newpit, deletepit - 1);
415 pars.splice(dest, start, finish);
423 // Go one down from *this* header:
424 ParagraphList::iterator dest = next(finish, 1);
425 // Go further down to find header to insert in front of:
426 for (; dest != end; ++dest) {
427 toclevel = buf.text().getTocLevel(distance(bgn, dest));
428 if (toclevel != Layout::NOT_IN_TOC
429 && toclevel <= thistoclevel)
432 // One such was found:
433 pit_type newpit = distance(bgn, dest);
434 buf.undo().recordUndo(cur, pit, newpit - 1);
435 pit_type const len = distance(start, finish);
436 pars.splice(dest, start, finish);
437 cur.pit() = newpit - len;
442 pit_type const len = distance(start, finish);
443 buf.undo().recordUndo(cur, pit, pit + len - 1);
444 for (; start != finish; ++start) {
445 toclevel = buf.text().getTocLevel(distance(bgn, start));
446 if (toclevel == Layout::NOT_IN_TOC)
449 DocumentClass const & tc = buf.params().documentClass();
450 DocumentClass::const_iterator lit = tc.begin();
451 DocumentClass::const_iterator len = tc.end();
452 int const newtoclevel =
453 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
454 LabelType const oldlabeltype = start->layout().labeltype;
456 for (; lit != len; ++lit) {
457 if (lit->toclevel == newtoclevel &&
458 lit->labeltype == oldlabeltype) {
459 start->setLayout(*lit);
470 void Text::number(Cursor & cur)
472 FontInfo font = ignore_font;
473 font.setNumber(FONT_TOGGLE);
474 toggleAndShow(cur, this, Font(font, ignore_language));
478 bool Text::isRTL(Paragraph const & par) const
480 Buffer const & buffer = owner_->buffer();
481 return par.isRTL(buffer.params());
487 Language const * getLanguage(Cursor const & cur, string const & lang) {
488 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
494 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
496 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
498 // Dispatch if the cursor is inside the text. It is not the
499 // case for context menus (bug 5797).
500 if (cur.text() != this) {
505 BufferView * bv = &cur.bv();
506 TextMetrics * tm = &bv->textMetrics(this);
507 if (!tm->contains(cur.pit())) {
508 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
509 tm = &bv->textMetrics(this);
512 // FIXME: We use the update flag to indicates wether a singlePar or a
513 // full screen update is needed. We reset it here but shall we restore it
515 cur.noScreenUpdate();
517 LBUFERR(this == cur.text());
519 // NOTE: This should NOT be a reference. See commit 94a5481a.
520 CursorSlice const oldTopSlice = cur.top();
521 bool const oldBoundary = cur.boundary();
522 bool const oldSelection = cur.selection();
523 // Signals that, even if needsUpdate == false, an update of the
524 // cursor paragraph is required
525 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
526 LyXAction::SingleParUpdate);
527 // Signals that a full-screen update is required
528 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
529 LyXAction::NoUpdate) || singleParUpdate);
530 bool const last_misspelled = lyxrc.spellcheck_continuously
531 && cur.paragraph().isMisspelled(cur.pos(), true);
533 FuncCode const act = cmd.action();
536 case LFUN_PARAGRAPH_MOVE_DOWN: {
537 pit_type const pit = cur.pit();
538 cur.recordUndo(pit, pit + 1);
540 pars_.swap(pit, pit + 1);
542 cur.forceBufferUpdate();
547 case LFUN_PARAGRAPH_MOVE_UP: {
548 pit_type const pit = cur.pit();
549 cur.recordUndo(pit - 1, pit);
551 pars_.swap(pit, pit - 1);
554 cur.forceBufferUpdate();
558 case LFUN_APPENDIX: {
559 Paragraph & par = cur.paragraph();
560 bool start = !par.params().startOfAppendix();
562 // FIXME: The code below only makes sense at top level.
563 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
564 // ensure that we have only one start_of_appendix in this document
565 // FIXME: this don't work for multipart document!
566 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
567 if (pars_[tmp].params().startOfAppendix()) {
568 cur.recordUndo(tmp, tmp);
569 pars_[tmp].params().startOfAppendix(false);
575 par.params().startOfAppendix(start);
577 // we can set the refreshing parameters now
578 cur.forceBufferUpdate();
582 case LFUN_WORD_DELETE_FORWARD:
584 cutSelection(cur, true, false);
586 deleteWordForward(cur);
587 finishChange(cur, false);
590 case LFUN_WORD_DELETE_BACKWARD:
592 cutSelection(cur, true, false);
594 deleteWordBackward(cur);
595 finishChange(cur, false);
598 case LFUN_LINE_DELETE_FORWARD:
600 cutSelection(cur, true, false);
602 tm->deleteLineForward(cur);
603 finishChange(cur, false);
606 case LFUN_BUFFER_BEGIN:
607 case LFUN_BUFFER_BEGIN_SELECT:
608 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
609 if (cur.depth() == 1)
610 needsUpdate |= cursorTop(cur);
613 cur.screenUpdateFlags(Update::FitCursor);
616 case LFUN_BUFFER_END:
617 case LFUN_BUFFER_END_SELECT:
618 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
619 if (cur.depth() == 1)
620 needsUpdate |= cursorBottom(cur);
623 cur.screenUpdateFlags(Update::FitCursor);
626 case LFUN_INSET_BEGIN:
627 case LFUN_INSET_BEGIN_SELECT:
628 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
629 if (cur.depth() == 1 || !cur.top().at_begin())
630 needsUpdate |= cursorTop(cur);
633 cur.screenUpdateFlags(Update::FitCursor);
637 case LFUN_INSET_END_SELECT:
638 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
639 if (cur.depth() == 1 || !cur.top().at_end())
640 needsUpdate |= cursorBottom(cur);
643 cur.screenUpdateFlags(Update::FitCursor);
646 case LFUN_CHAR_FORWARD:
647 case LFUN_CHAR_FORWARD_SELECT: {
648 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
649 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
650 bool const cur_moved = cursorForward(cur);
651 needsUpdate |= cur_moved;
653 if (!cur_moved && oldTopSlice == cur.top()
654 && cur.boundary() == oldBoundary) {
656 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
658 // we will probably be moving out the inset, so we should execute
659 // the depm-mechanism, but only when the cursor has a place to
660 // go outside this inset, i.e. in a slice above.
661 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
662 && cur.pit() == cur.lastpit()) {
663 // The cursor hasn't changed yet. To give the
664 // DEPM the possibility of doing something we must
665 // provide it with two different cursors.
667 dummy.pos() = dummy.pit() = 0;
668 if (cur.bv().checkDepm(dummy, cur))
669 cur.forceBufferUpdate();
675 case LFUN_CHAR_BACKWARD:
676 case LFUN_CHAR_BACKWARD_SELECT: {
677 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
678 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
679 bool const cur_moved = cursorBackward(cur);
680 needsUpdate |= cur_moved;
682 if (!cur_moved && oldTopSlice == cur.top()
683 && cur.boundary() == oldBoundary) {
685 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
687 // we will probably be moving out the inset, so we should execute
688 // the depm-mechanism, but only when the cursor has a place to
689 // go outside this inset, i.e. in a slice above.
690 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
691 // The cursor hasn't changed yet. To give the
692 // DEPM the possibility of doing something we must
693 // provide it with two different cursors.
695 dummy.pos() = cur.lastpos();
696 dummy.pit() = cur.lastpit();
697 if (cur.bv().checkDepm(dummy, cur))
698 cur.forceBufferUpdate();
705 case LFUN_CHAR_LEFT_SELECT:
706 if (lyxrc.visual_cursor) {
707 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
708 bool const cur_moved = cursorVisLeft(cur);
709 needsUpdate |= cur_moved;
710 if (!cur_moved && oldTopSlice == cur.top()
711 && cur.boundary() == oldBoundary) {
713 cmd = FuncRequest(LFUN_FINISHED_LEFT);
716 if (cur.reverseDirectionNeeded()) {
717 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
718 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
720 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
721 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
728 case LFUN_CHAR_RIGHT:
729 case LFUN_CHAR_RIGHT_SELECT:
730 if (lyxrc.visual_cursor) {
731 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
732 bool const cur_moved = cursorVisRight(cur);
733 needsUpdate |= cur_moved;
734 if (!cur_moved && oldTopSlice == cur.top()
735 && cur.boundary() == oldBoundary) {
737 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
740 if (cur.reverseDirectionNeeded()) {
741 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
742 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
744 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
745 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
754 case LFUN_DOWN_SELECT:
757 // stop/start the selection
758 bool select = cmd.action() == LFUN_DOWN_SELECT ||
759 cmd.action() == LFUN_UP_SELECT;
761 // move cursor up/down
762 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
763 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
765 if (!atFirstOrLastRow) {
766 needsUpdate |= cur.selHandle(select);
767 cur.upDownInText(up, needsUpdate);
768 needsUpdate |= cur.beforeDispatchCursor().inMathed();
770 // if the cursor cannot be moved up or down do not remove
771 // the selection right now, but wait for the next dispatch.
773 needsUpdate |= cur.selHandle(select);
774 cur.upDownInText(up, needsUpdate);
781 case LFUN_PARAGRAPH_UP:
782 case LFUN_PARAGRAPH_UP_SELECT:
783 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
784 needsUpdate |= cursorUpParagraph(cur);
787 case LFUN_PARAGRAPH_DOWN:
788 case LFUN_PARAGRAPH_DOWN_SELECT:
789 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
790 needsUpdate |= cursorDownParagraph(cur);
793 case LFUN_LINE_BEGIN:
794 case LFUN_LINE_BEGIN_SELECT:
795 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
796 needsUpdate |= tm->cursorHome(cur);
800 case LFUN_LINE_END_SELECT:
801 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
802 needsUpdate |= tm->cursorEnd(cur);
805 case LFUN_SECTION_SELECT: {
806 Buffer const & buf = *cur.buffer();
807 pit_type const pit = cur.pit();
808 ParagraphList & pars = buf.text().paragraphs();
809 ParagraphList::iterator bgn = pars.begin();
810 // The first paragraph of the area to be selected:
811 ParagraphList::iterator start = lyx::next(bgn, pit);
812 // The final paragraph of area to be selected:
813 ParagraphList::iterator finish = start;
814 ParagraphList::iterator end = pars.end();
816 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
817 if (thistoclevel == Layout::NOT_IN_TOC)
821 Cursor const old_cur = cur;
822 needsUpdate |= cur.selHandle(true);
824 // Move out (down) from this section header
828 // Seek the one (on same level) below
829 for (; finish != end; ++finish, ++cur.pit()) {
830 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
831 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
834 cur.pos() = cur.lastpos();
836 needsUpdate |= cur != old_cur;
840 case LFUN_WORD_RIGHT:
841 case LFUN_WORD_RIGHT_SELECT:
842 if (lyxrc.visual_cursor) {
843 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
844 bool const cur_moved = cursorVisRightOneWord(cur);
845 needsUpdate |= cur_moved;
846 if (!cur_moved && oldTopSlice == cur.top()
847 && cur.boundary() == oldBoundary) {
849 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
852 if (cur.reverseDirectionNeeded()) {
853 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
854 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
856 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
857 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
864 case LFUN_WORD_FORWARD:
865 case LFUN_WORD_FORWARD_SELECT: {
866 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
867 bool const cur_moved = cursorForwardOneWord(cur);
868 needsUpdate |= cur_moved;
870 if (!cur_moved && oldTopSlice == cur.top()
871 && cur.boundary() == oldBoundary) {
873 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
875 // we will probably be moving out the inset, so we should execute
876 // the depm-mechanism, but only when the cursor has a place to
877 // go outside this inset, i.e. in a slice above.
878 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
879 && cur.pit() == cur.lastpit()) {
880 // The cursor hasn't changed yet. To give the
881 // DEPM the possibility of doing something we must
882 // provide it with two different cursors.
884 dummy.pos() = dummy.pit() = 0;
885 if (cur.bv().checkDepm(dummy, cur))
886 cur.forceBufferUpdate();
893 case LFUN_WORD_LEFT_SELECT:
894 if (lyxrc.visual_cursor) {
895 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
896 bool const cur_moved = cursorVisLeftOneWord(cur);
897 needsUpdate |= cur_moved;
898 if (!cur_moved && oldTopSlice == cur.top()
899 && cur.boundary() == oldBoundary) {
901 cmd = FuncRequest(LFUN_FINISHED_LEFT);
904 if (cur.reverseDirectionNeeded()) {
905 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
906 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
908 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
909 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
916 case LFUN_WORD_BACKWARD:
917 case LFUN_WORD_BACKWARD_SELECT: {
918 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
919 bool const cur_moved = cursorBackwardOneWord(cur);
920 needsUpdate |= cur_moved;
922 if (!cur_moved && oldTopSlice == cur.top()
923 && cur.boundary() == oldBoundary) {
925 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
927 // we will probably be moving out the inset, so we should execute
928 // the depm-mechanism, but only when the cursor has a place to
929 // go outside this inset, i.e. in a slice above.
930 if (cur.depth() > 1 && cur.pos() == 0
932 // The cursor hasn't changed yet. To give the
933 // DEPM the possibility of doing something we must
934 // provide it with two different cursors.
936 dummy.pos() = cur.lastpos();
937 dummy.pit() = cur.lastpit();
938 if (cur.bv().checkDepm(dummy, cur))
939 cur.forceBufferUpdate();
945 case LFUN_WORD_SELECT: {
946 selectWord(cur, WHOLE_WORD);
947 finishChange(cur, true);
951 case LFUN_NEWLINE_INSERT: {
952 InsetNewlineParams inp;
953 docstring arg = cmd.argument();
954 if (arg == "linebreak")
955 inp.kind = InsetNewlineParams::LINEBREAK;
957 inp.kind = InsetNewlineParams::NEWLINE;
958 cap::replaceSelection(cur);
960 cur.insert(new InsetNewline(inp));
962 moveCursor(cur, false);
966 case LFUN_TAB_INSERT: {
967 bool const multi_par_selection = cur.selection() &&
968 cur.selBegin().pit() != cur.selEnd().pit();
969 if (multi_par_selection) {
970 // If there is a multi-paragraph selection, a tab is inserted
971 // at the beginning of each paragraph.
972 cur.recordUndoSelection();
973 pit_type const pit_end = cur.selEnd().pit();
974 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
975 pars_[pit].insertChar(0, '\t',
976 bv->buffer().params().track_changes);
977 // Update the selection pos to make sure the selection does not
978 // change as the inserted tab will increase the logical pos.
979 if (cur.realAnchor().pit() == pit)
980 cur.realAnchor().forwardPos();
981 if (cur.pit() == pit)
986 // Maybe we shouldn't allow tabs within a line, because they
987 // are not (yet) aligned as one might do expect.
988 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
994 case LFUN_TAB_DELETE: {
995 bool const tc = bv->buffer().params().track_changes;
996 if (cur.selection()) {
997 // If there is a selection, a tab (if present) is removed from
998 // the beginning of each paragraph.
999 cur.recordUndoSelection();
1000 pit_type const pit_end = cur.selEnd().pit();
1001 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1002 Paragraph & par = paragraphs()[pit];
1005 char_type const c = par.getChar(0);
1006 if (c == '\t' || c == ' ') {
1007 // remove either 1 tab or 4 spaces.
1008 int const n = (c == ' ' ? 4 : 1);
1009 for (int i = 0; i < n
1010 && !par.empty() && par.getChar(0) == c; ++i) {
1011 if (cur.pit() == pit)
1013 if (cur.realAnchor().pit() == pit
1014 && cur.realAnchor().pos() > 0 )
1015 cur.realAnchor().backwardPos();
1016 par.eraseChar(0, tc);
1022 // If there is no selection, try to remove a tab or some spaces
1023 // before the position of the cursor.
1024 Paragraph & par = paragraphs()[cur.pit()];
1025 pos_type const pos = cur.pos();
1030 char_type const c = par.getChar(pos - 1);
1034 par.eraseChar(cur.pos(), tc);
1036 for (int n_spaces = 0;
1038 && par.getChar(cur.pos() - 1) == ' '
1042 par.eraseChar(cur.pos(), tc);
1049 case LFUN_CHAR_DELETE_FORWARD:
1050 if (!cur.selection()) {
1051 bool was_separator = cur.paragraph().isEnvSeparator(cur.pos());
1052 if (cur.pos() == cur.paragraph().size())
1053 // Par boundary, force full-screen update
1054 singleParUpdate = false;
1055 needsUpdate |= erase(cur);
1057 if (was_separator && cur.pos() == cur.paragraph().size()
1058 && (!cur.paragraph().layout().isEnvironment()
1059 || cur.paragraph().size() > 0)) {
1060 // Force full-screen update
1061 singleParUpdate = false;
1062 needsUpdate |= erase(cur);
1065 // It is possible to make it a lot faster still
1066 // just comment out the line below...
1068 cutSelection(cur, true, false);
1069 singleParUpdate = false;
1071 moveCursor(cur, false);
1074 case LFUN_CHAR_DELETE_BACKWARD:
1075 if (!cur.selection()) {
1076 if (bv->getIntl().getTransManager().backspace()) {
1077 bool par_boundary = cur.pos() == 0;
1078 bool first_par = cur.pit() == 0;
1079 // Par boundary, full-screen update
1081 singleParUpdate = false;
1082 needsUpdate |= backspace(cur);
1084 if (par_boundary && !first_par && cur.pos() > 0
1085 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1086 needsUpdate |= backspace(cur);
1091 cutSelection(cur, true, false);
1092 singleParUpdate = false;
1096 case LFUN_PARAGRAPH_BREAK: {
1097 cap::replaceSelection(cur);
1098 pit_type pit = cur.pit();
1099 Paragraph const & par = pars_[pit];
1100 bool lastpar = (pit == pit_type(pars_.size() - 1));
1101 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1102 pit_type prev = pit;
1104 if (!pars_[pit - 1].layout().isEnvironment())
1105 prev = depthHook(pit, par.getDepth());
1106 else if (pars_[pit - 1].getDepth() >= par.getDepth())
1109 if (prev < pit && cur.pos() == par.beginOfBody()
1110 && !par.size() && !par.isEnvSeparator(cur.pos())
1111 && !par.layout().isCommand()
1112 && pars_[prev].layout() != par.layout()
1113 && pars_[prev].layout().isEnvironment()
1114 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1115 if (par.layout().isEnvironment()
1116 && pars_[prev].getDepth() == par.getDepth()) {
1117 docstring const layout = par.layout().name();
1118 DocumentClass const & tc = bv->buffer().params().documentClass();
1119 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1120 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1121 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1122 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1124 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1125 breakParagraph(cur);
1127 Font const f(inherit_font, cur.current_font.language());
1128 pars_[cur.pit() - 1].resetFonts(f);
1130 if (par.isEnvSeparator(cur.pos()))
1132 breakParagraph(cur, cmd.argument() == "inverse");
1135 // If we have a list and autoinsert item insets,
1137 Layout::LaTeXArgMap args = par.layout().args();
1138 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1139 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1140 for (; lait != laend; ++lait) {
1141 Layout::latexarg arg = (*lait).second;
1142 if (arg.autoinsert && prefixIs((*lait).first, "item:")) {
1143 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1150 case LFUN_INSET_INSERT: {
1153 // We have to avoid triggering InstantPreview loading
1154 // before inserting into the document. See bug #5626.
1155 bool loaded = bv->buffer().isFullyLoaded();
1156 bv->buffer().setFullyLoaded(false);
1157 Inset * inset = createInset(&bv->buffer(), cmd);
1158 bv->buffer().setFullyLoaded(loaded);
1161 // FIXME (Abdel 01/02/2006):
1162 // What follows would be a partial fix for bug 2154:
1163 // http://www.lyx.org/trac/ticket/2154
1164 // This automatically put the label inset _after_ a
1165 // numbered section. It should be possible to extend the mechanism
1166 // to any kind of LateX environement.
1167 // The correct way to fix that bug would be at LateX generation.
1168 // I'll let the code here for reference as it could be used for some
1169 // other feature like "automatic labelling".
1171 Paragraph & par = pars_[cur.pit()];
1172 if (inset->lyxCode() == LABEL_CODE
1173 && !par.layout().counter.empty()) {
1174 // Go to the end of the paragraph
1175 // Warning: Because of Change-Tracking, the last
1176 // position is 'size()' and not 'size()-1':
1177 cur.pos() = par.size();
1178 // Insert a new paragraph
1179 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1183 if (cur.selection())
1184 cutSelection(cur, true, false);
1186 if (inset->editable() && inset->asInsetText())
1187 inset->edit(cur, true);
1191 // trigger InstantPreview now
1192 if (inset->lyxCode() == EXTERNAL_CODE) {
1193 InsetExternal & ins =
1194 static_cast<InsetExternal &>(*inset);
1195 ins.updatePreview();
1202 case LFUN_INSET_DISSOLVE: {
1203 if (dissolveInset(cur)) {
1205 cur.forceBufferUpdate();
1210 case LFUN_SET_GRAPHICS_GROUP: {
1211 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1217 string id = to_utf8(cmd.argument());
1218 string grp = graphics::getGroupParams(bv->buffer(), id);
1219 InsetGraphicsParams tmp, inspar = ins->getParams();
1222 inspar.groupId = to_utf8(cmd.argument());
1224 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1225 tmp.filename = inspar.filename;
1229 ins->setParams(inspar);
1232 case LFUN_SPACE_INSERT:
1233 if (cur.paragraph().layout().free_spacing)
1234 insertChar(cur, ' ');
1236 doInsertInset(cur, this, cmd, false, false);
1239 moveCursor(cur, false);
1242 case LFUN_SPECIALCHAR_INSERT: {
1243 string const name = to_utf8(cmd.argument());
1244 if (name == "hyphenation")
1245 specialChar(cur, InsetSpecialChar::HYPHENATION);
1246 else if (name == "ligature-break")
1247 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1248 else if (name == "slash")
1249 specialChar(cur, InsetSpecialChar::SLASH);
1250 else if (name == "nobreakdash")
1251 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1252 else if (name == "dots")
1253 specialChar(cur, InsetSpecialChar::LDOTS);
1254 else if (name == "end-of-sentence")
1255 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1256 else if (name == "menu-separator")
1257 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1258 else if (name == "lyx")
1259 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1260 else if (name == "tex")
1261 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1262 else if (name == "latex")
1263 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1264 else if (name == "latex2e")
1265 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1266 else if (name.empty())
1267 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1269 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1273 case LFUN_IPAMACRO_INSERT: {
1274 string const arg = cmd.getArg(0);
1275 if (arg == "deco") {
1276 // Open the inset, and move the current selection
1278 doInsertInset(cur, this, cmd, true, true);
1280 // Some insets are numbered, others are shown in the outline pane so
1281 // let's update the labels and the toc backend.
1282 cur.forceBufferUpdate();
1285 if (arg == "tone-falling")
1286 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1287 else if (arg == "tone-rising")
1288 ipaChar(cur, InsetIPAChar::TONE_RISING);
1289 else if (arg == "tone-high-rising")
1290 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1291 else if (arg == "tone-low-rising")
1292 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1293 else if (arg == "tone-high-rising-falling")
1294 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1295 else if (arg.empty())
1296 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1298 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1302 case LFUN_WORD_UPCASE:
1303 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1306 case LFUN_WORD_LOWCASE:
1307 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1310 case LFUN_WORD_CAPITALIZE:
1311 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1314 case LFUN_CHARS_TRANSPOSE:
1315 charsTranspose(cur);
1319 cur.message(_("Paste"));
1320 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1321 cap::replaceSelection(cur);
1323 // without argument?
1324 string const arg = to_utf8(cmd.argument());
1326 bool tryGraphics = true;
1327 if (theClipboard().isInternal())
1328 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1329 else if (theClipboard().hasTextContents()) {
1330 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1331 true, Clipboard::AnyTextType))
1332 tryGraphics = false;
1334 if (tryGraphics && theClipboard().hasGraphicsContents())
1335 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1336 } else if (isStrUnsignedInt(arg)) {
1337 // we have a numerical argument
1338 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1339 convert<unsigned int>(arg));
1340 } else if (arg == "html" || arg == "latex") {
1341 Clipboard::TextType type = (arg == "html") ?
1342 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1343 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1345 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1347 type = Clipboard::PdfGraphicsType;
1348 else if (arg == "png")
1349 type = Clipboard::PngGraphicsType;
1350 else if (arg == "jpeg")
1351 type = Clipboard::JpegGraphicsType;
1352 else if (arg == "linkback")
1353 type = Clipboard::LinkBackGraphicsType;
1354 else if (arg == "emf")
1355 type = Clipboard::EmfGraphicsType;
1356 else if (arg == "wmf")
1357 type = Clipboard::WmfGraphicsType;
1359 // we also check in getStatus()
1360 LYXERR0("Unrecognized graphics type: " << arg);
1362 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1365 bv->buffer().errors("Paste");
1366 cur.clearSelection(); // bug 393
1372 cutSelection(cur, true, true);
1373 cur.message(_("Cut"));
1378 cur.message(_("Copy"));
1381 case LFUN_SERVER_GET_XY:
1382 cur.message(from_utf8(
1383 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1384 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1387 case LFUN_SERVER_SET_XY: {
1390 istringstream is(to_utf8(cmd.argument()));
1393 lyxerr << "SETXY: Could not parse coordinates in '"
1394 << to_utf8(cmd.argument()) << endl;
1396 tm->setCursorFromCoordinates(cur, x, y);
1400 case LFUN_SERVER_GET_LAYOUT:
1401 cur.message(cur.paragraph().layout().name());
1405 docstring layout = cmd.argument();
1406 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1408 Paragraph const & para = cur.paragraph();
1409 docstring const old_layout = para.layout().name();
1410 DocumentClass const & tclass = bv->buffer().params().documentClass();
1413 layout = tclass.defaultLayoutName();
1415 if (owner_->forcePlainLayout())
1416 // in this case only the empty layout is allowed
1417 layout = tclass.plainLayoutName();
1418 else if (para.usePlainLayout()) {
1419 // in this case, default layout maps to empty layout
1420 if (layout == tclass.defaultLayoutName())
1421 layout = tclass.plainLayoutName();
1423 // otherwise, the empty layout maps to the default
1424 if (layout == tclass.plainLayoutName())
1425 layout = tclass.defaultLayoutName();
1428 bool hasLayout = tclass.hasLayout(layout);
1430 // If the entry is obsolete, use the new one instead.
1432 docstring const & obs = tclass[layout].obsoleted_by();
1438 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1439 from_utf8(N_(" not known")));
1443 bool change_layout = (old_layout != layout);
1445 if (!change_layout && cur.selection() &&
1446 cur.selBegin().pit() != cur.selEnd().pit())
1448 pit_type spit = cur.selBegin().pit();
1449 pit_type epit = cur.selEnd().pit() + 1;
1450 while (spit != epit) {
1451 if (pars_[spit].layout().name() != old_layout) {
1452 change_layout = true;
1460 setLayout(cur, layout);
1462 Layout::LaTeXArgMap args = tclass[layout].args();
1463 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1464 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1465 for (; lait != laend; ++lait) {
1466 Layout::latexarg arg = (*lait).second;
1467 if (arg.autoinsert) {
1468 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1476 case LFUN_ENVIRONMENT_SPLIT: {
1477 bool const outer = cmd.argument() == "outer";
1478 Paragraph const & para = cur.paragraph();
1479 docstring layout = para.layout().name();
1480 depth_type split_depth = cur.paragraph().params().depth();
1482 // check if we have an environment in our nesting hierarchy
1483 pit_type pit = cur.pit();
1484 Paragraph cpar = pars_[pit];
1486 if (pit == 0 || cpar.params().depth() == 0)
1490 if (cpar.params().depth() < split_depth
1491 && cpar.layout().isEnvironment()) {
1492 layout = cpar.layout().name();
1493 split_depth = cpar.params().depth();
1498 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1500 while (cur.paragraph().params().depth() > split_depth)
1501 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1503 DocumentClass const & tc = bv->buffer().params().documentClass();
1504 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1505 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1506 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1507 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1512 case LFUN_CLIPBOARD_PASTE:
1513 cap::replaceSelection(cur);
1514 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1515 cmd.argument() == "paragraph");
1516 bv->buffer().errors("Paste");
1519 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1520 cap::replaceSelection(cur);
1521 pasteSimpleText(cur, cmd.argument() == "paragraph");
1524 case LFUN_PRIMARY_SELECTION_PASTE:
1525 cap::replaceSelection(cur);
1526 pasteString(cur, theSelection().get(),
1527 cmd.argument() == "paragraph");
1530 case LFUN_SELECTION_PASTE:
1531 // Copy the selection buffer to the clipboard stack,
1532 // because we want it to appear in the "Edit->Paste
1534 cap::replaceSelection(cur);
1535 cap::copySelectionToStack();
1536 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1537 bv->buffer().errors("Paste");
1540 case LFUN_UNICODE_INSERT: {
1541 if (cmd.argument().empty())
1543 docstring hexstring = cmd.argument();
1544 if (isHex(hexstring)) {
1545 char_type c = hexToInt(hexstring);
1546 if (c >= 32 && c < 0x10ffff) {
1547 lyxerr << "Inserting c: " << c << endl;
1548 docstring s = docstring(1, c);
1549 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1555 case LFUN_QUOTE_INSERT: {
1556 cap::replaceSelection(cur);
1559 Paragraph const & par = cur.paragraph();
1560 pos_type pos = cur.pos();
1561 // Ignore deleted text before cursor
1562 while (pos > 0 && par.isDeleted(pos - 1))
1565 BufferParams const & bufparams = bv->buffer().params();
1567 par.getFontSettings(bufparams, pos).language()->lang() == "hebrew";
1568 bool const allow_inset_quote = !(par.isPassThru() || hebrew);
1570 string const arg = to_utf8(cmd.argument());
1571 if (allow_inset_quote) {
1573 if (pos > 0 && (!cur.prevInset() || !cur.prevInset()->isSpace()))
1574 c = par.getChar(pos - 1);
1575 InsetQuotes::QuoteTimes const quote_type = (arg == "single")
1576 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes;
1577 cur.insert(new InsetQuotes(cur.buffer(), c, quote_type));
1580 // The cursor might have been invalidated by the replaceSelection.
1581 cur.buffer()->changed(true);
1582 string const quote_string = (arg == "single") ? "'" : "\"";
1583 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, quote_string));
1588 case LFUN_DATE_INSERT: {
1589 string const format = cmd.argument().empty()
1590 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1591 string const time = formatted_time(current_time(), format);
1592 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1596 case LFUN_MOUSE_TRIPLE:
1597 if (cmd.button() == mouse_button::button1) {
1598 tm->cursorHome(cur);
1606 case LFUN_MOUSE_DOUBLE:
1607 if (cmd.button() == mouse_button::button1) {
1608 selectWord(cur, WHOLE_WORD);
1613 // Single-click on work area
1614 case LFUN_MOUSE_PRESS: {
1615 // We are not marking a selection with the keyboard in any case.
1616 Cursor & bvcur = cur.bv().cursor();
1617 bvcur.setMark(false);
1618 switch (cmd.button()) {
1619 case mouse_button::button1:
1621 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1622 cur.screenUpdateFlags(Update::FitCursor);
1623 if (bvcur.wordSelection())
1624 selectWord(bvcur, WHOLE_WORD);
1627 case mouse_button::button2:
1628 if (lyxrc.mouse_middlebutton_paste) {
1629 // Middle mouse pasting.
1630 bv->mouseSetCursor(cur);
1632 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1633 "selection-paste ; primary-selection-paste paragraph"));
1635 cur.noScreenUpdate();
1638 case mouse_button::button3: {
1639 // Don't do anything if we right-click a
1640 // selection, a context menu will popup.
1641 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1642 && cur < bvcur.selectionEnd()) {
1643 cur.noScreenUpdate();
1646 if (!bv->mouseSetCursor(cur, false))
1647 cur.screenUpdateFlags(Update::FitCursor);
1653 } // switch (cmd.button())
1656 case LFUN_MOUSE_MOTION: {
1657 // Mouse motion with right or middle mouse do nothing for now.
1658 if (cmd.button() != mouse_button::button1) {
1659 cur.noScreenUpdate();
1662 // ignore motions deeper nested than the real anchor
1663 Cursor & bvcur = cur.bv().cursor();
1664 if (!bvcur.realAnchor().hasPart(cur)) {
1668 CursorSlice old = bvcur.top();
1670 int const wh = bv->workHeight();
1671 int const y = max(0, min(wh - 1, cmd.y()));
1673 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1674 cur.setTargetX(cmd.x());
1676 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1677 else if (cmd.y() < 0)
1678 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1679 // This is to allow jumping over large insets
1680 if (cur.top() == old) {
1682 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1683 else if (cmd.y() < 0)
1684 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1686 // We continue with our existing selection or start a new one, so don't
1687 // reset the anchor.
1688 bvcur.setCursor(cur);
1689 bvcur.setSelection(true);
1690 if (cur.top() == old) {
1691 // We didn't move one iota, so no need to update the screen.
1692 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1693 //cur.noScreenUpdate();
1699 case LFUN_MOUSE_RELEASE:
1700 switch (cmd.button()) {
1701 case mouse_button::button1:
1702 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1703 // If there is a new selection, update persistent selection;
1704 // otherwise, single click does not clear persistent selection
1706 if (cur.selection()) {
1707 // Finish selection. If double click,
1708 // cur is moved to the end of word by
1709 // selectWord but bvcur is current
1711 cur.bv().cursor().setSelection();
1712 // We might have removed an empty but drawn selection
1713 // (probably a margin)
1714 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1716 cur.noScreenUpdate();
1717 // FIXME: We could try to handle drag and drop of selection here.
1720 case mouse_button::button2:
1721 // Middle mouse pasting is handled at mouse press time,
1722 // see LFUN_MOUSE_PRESS.
1723 cur.noScreenUpdate();
1726 case mouse_button::button3:
1727 // Cursor was set at LFUN_MOUSE_PRESS time.
1728 // FIXME: If there is a selection we could try to handle a special
1729 // drag & drop context menu.
1730 cur.noScreenUpdate();
1733 case mouse_button::none:
1734 case mouse_button::button4:
1735 case mouse_button::button5:
1737 } // switch (cmd.button())
1741 case LFUN_SELF_INSERT: {
1742 if (cmd.argument().empty())
1745 // Automatically delete the currently selected
1746 // text and replace it with what is being
1747 // typed in now. Depends on lyxrc settings
1748 // "auto_region_delete", which defaults to
1751 if (lyxrc.auto_region_delete && cur.selection())
1752 cutSelection(cur, false, false);
1754 cur.clearSelection();
1756 docstring::const_iterator cit = cmd.argument().begin();
1757 docstring::const_iterator const end = cmd.argument().end();
1758 for (; cit != end; ++cit)
1759 bv->translateAndInsert(*cit, this, cur);
1762 moveCursor(cur, false);
1763 cur.markNewWordPosition();
1764 bv->bookmarkEditPosition();
1768 case LFUN_HREF_INSERT: {
1769 // FIXME If we're actually given an argument, shouldn't
1770 // we use it, whether or not we have a selection?
1771 docstring content = cmd.argument();
1772 if (cur.selection()) {
1773 content = cur.selectionAsString(false);
1774 cutSelection(cur, true, false);
1777 InsetCommandParams p(HYPERLINK_CODE);
1778 if (!content.empty()){
1779 // if it looks like a link, we'll put it as target,
1780 // otherwise as name (bug #8792).
1783 // regex_match(to_utf8(content), matches, link_re)
1784 // because smatch stores pointers to the substrings rather
1785 // than making copies of them. And those pointers become
1786 // invalid after regex_match returns, since it is then
1787 // being given a temporary object. (Thanks to Georg for
1788 // figuring that out.)
1789 regex const link_re("^([a-z]+):.*");
1791 string const c = to_utf8(lowercase(content));
1793 if (c.substr(0,7) == "mailto:") {
1794 p["target"] = content;
1795 p["type"] = from_ascii("mailto:");
1796 } else if (regex_match(c, matches, link_re)) {
1797 p["target"] = content;
1798 string protocol = matches.str(1);
1799 if (protocol == "file")
1800 p["type"] = from_ascii("file:");
1802 p["name"] = content;
1804 string const data = InsetCommand::params2string(p);
1806 // we need to have a target. if we already have one, then
1807 // that gets used at the default for the name, too, which
1808 // is probably what is wanted.
1809 if (p["target"].empty()) {
1810 bv->showDialog("href", data);
1812 FuncRequest fr(LFUN_INSET_INSERT, data);
1818 case LFUN_LABEL_INSERT: {
1819 InsetCommandParams p(LABEL_CODE);
1820 // Try to generate a valid label
1821 p["name"] = (cmd.argument().empty()) ?
1822 cur.getPossibleLabel() :
1824 string const data = InsetCommand::params2string(p);
1826 if (cmd.argument().empty()) {
1827 bv->showDialog("label", data);
1829 FuncRequest fr(LFUN_INSET_INSERT, data);
1835 case LFUN_INFO_INSERT: {
1837 if (cmd.argument().empty() && cur.selection()) {
1838 // if command argument is empty use current selection as parameter.
1839 docstring ds = cur.selectionAsString(false);
1840 cutSelection(cur, true, false);
1841 FuncRequest cmd0(cmd, ds);
1842 inset = createInset(cur.buffer(), cmd0);
1844 inset = createInset(cur.buffer(), cmd);
1849 insertInset(cur, inset);
1853 case LFUN_CAPTION_INSERT:
1854 case LFUN_FOOTNOTE_INSERT:
1855 case LFUN_NOTE_INSERT:
1856 case LFUN_BOX_INSERT:
1857 case LFUN_BRANCH_INSERT:
1858 case LFUN_PHANTOM_INSERT:
1859 case LFUN_ERT_INSERT:
1860 case LFUN_LISTING_INSERT:
1861 case LFUN_MARGINALNOTE_INSERT:
1862 case LFUN_ARGUMENT_INSERT:
1863 case LFUN_INDEX_INSERT:
1864 case LFUN_PREVIEW_INSERT:
1865 case LFUN_SCRIPT_INSERT:
1866 case LFUN_IPA_INSERT:
1867 // Open the inset, and move the current selection
1869 doInsertInset(cur, this, cmd, true, true);
1871 // Some insets are numbered, others are shown in the outline pane so
1872 // let's update the labels and the toc backend.
1873 cur.forceBufferUpdate();
1876 case LFUN_FLEX_INSERT: {
1877 // Open the inset, and move the current selection
1879 bool const sel = cur.selection();
1880 doInsertInset(cur, this, cmd, true, true);
1881 // Insert auto-insert arguments
1882 bool autoargs = false;
1883 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1884 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1885 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1886 for (; lait != laend; ++lait) {
1887 Layout::latexarg arg = (*lait).second;
1888 if (arg.autoinsert) {
1889 // The cursor might have been invalidated by the replaceSelection.
1890 cur.buffer()->changed(true);
1891 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1898 cur.leaveInset(cur.inset());
1901 // Some insets are numbered, others are shown in the outline pane so
1902 // let's update the labels and the toc backend.
1903 cur.forceBufferUpdate();
1907 case LFUN_TABULAR_INSERT:
1908 // if there were no arguments, just open the dialog
1909 if (doInsertInset(cur, this, cmd, false, true))
1912 bv->showDialog("tabularcreate");
1916 case LFUN_FLOAT_INSERT:
1917 case LFUN_FLOAT_WIDE_INSERT:
1918 case LFUN_WRAP_INSERT: {
1919 // will some content be moved into the inset?
1920 bool const content = cur.selection();
1921 // does the content consist of multiple paragraphs?
1922 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1924 doInsertInset(cur, this, cmd, true, true);
1927 // If some single-par content is moved into the inset,
1928 // doInsertInset puts the cursor outside the inset.
1929 // To insert the caption we put it back into the inset.
1930 // FIXME cleanup doInsertInset to avoid such dances!
1931 if (content && singlepar)
1934 ParagraphList & pars = cur.text()->paragraphs();
1936 DocumentClass const & tclass = bv->buffer().params().documentClass();
1938 // add a separate paragraph for the caption inset
1939 pars.push_back(Paragraph());
1940 pars.back().setInsetOwner(&cur.text()->inset());
1941 pars.back().setPlainOrDefaultLayout(tclass);
1942 int cap_pit = pars.size() - 1;
1944 // if an empty inset was created, we create an additional empty
1945 // paragraph at the bottom so that the user can choose where to put
1946 // the graphics (or table).
1948 pars.push_back(Paragraph());
1949 pars.back().setInsetOwner(&cur.text()->inset());
1950 pars.back().setPlainOrDefaultLayout(tclass);
1953 // reposition the cursor to the caption
1954 cur.pit() = cap_pit;
1956 // FIXME: This Text/Cursor dispatch handling is a mess!
1957 // We cannot use Cursor::dispatch here it needs access to up to
1959 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1960 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1961 cur.forceBufferUpdate();
1962 cur.screenUpdateFlags(Update::Force);
1963 // FIXME: When leaving the Float (or Wrap) inset we should
1964 // delete any empty paragraph left above or below the
1969 case LFUN_NOMENCL_INSERT: {
1970 InsetCommandParams p(NOMENCL_CODE);
1971 if (cmd.argument().empty())
1972 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1974 p["symbol"] = cmd.argument();
1975 string const data = InsetCommand::params2string(p);
1976 bv->showDialog("nomenclature", data);
1980 case LFUN_INDEX_PRINT: {
1981 InsetCommandParams p(INDEX_PRINT_CODE);
1982 if (cmd.argument().empty())
1983 p["type"] = from_ascii("idx");
1985 p["type"] = cmd.argument();
1986 string const data = InsetCommand::params2string(p);
1987 FuncRequest fr(LFUN_INSET_INSERT, data);
1992 case LFUN_NOMENCL_PRINT:
1993 case LFUN_NEWPAGE_INSERT:
1995 doInsertInset(cur, this, cmd, false, false);
1999 case LFUN_SEPARATOR_INSERT: {
2000 doInsertInset(cur, this, cmd, false, false);
2002 // remove a following space
2003 Paragraph & par = cur.paragraph();
2004 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2005 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2009 case LFUN_DEPTH_DECREMENT:
2010 changeDepth(cur, DEC_DEPTH);
2013 case LFUN_DEPTH_INCREMENT:
2014 changeDepth(cur, INC_DEPTH);
2017 case LFUN_REGEXP_MODE:
2018 regexpDispatch(cur, cmd);
2021 case LFUN_MATH_MODE: {
2022 if (cmd.argument() == "on" || cmd.argument() == "") {
2023 // don't pass "on" as argument
2024 // (it would appear literally in the first cell)
2025 docstring sel = cur.selectionAsString(false);
2026 MathMacroTemplate * macro = new MathMacroTemplate(cur.buffer());
2027 // create a macro template if we see "\\newcommand" somewhere, and
2028 // an ordinary formula otherwise
2030 && (sel.find(from_ascii("\\newcommand")) != string::npos
2031 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2032 || sel.find(from_ascii("\\def")) != string::npos)
2033 && macro->fromString(sel)) {
2035 replaceSelection(cur);
2038 // no meaningful macro template was found
2040 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2043 // The argument is meaningful
2044 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2045 // has a different meaning in math mode
2046 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2050 case LFUN_MATH_MACRO:
2051 if (cmd.argument().empty())
2052 cur.errorMessage(from_utf8(N_("Missing argument")));
2055 string s = to_utf8(cmd.argument());
2056 string const s1 = token(s, ' ', 1);
2057 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2058 string const s2 = token(s, ' ', 2);
2059 MacroType type = MacroTypeNewcommand;
2061 type = MacroTypeDef;
2062 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
2063 from_utf8(token(s, ' ', 0)), nargs, false, type);
2064 inset->setBuffer(bv->buffer());
2065 insertInset(cur, inset);
2067 // enter macro inset and select the name
2069 cur.top().pos() = cur.top().lastpos();
2071 cur.setSelection(true);
2072 cur.top().pos() = 0;
2076 case LFUN_MATH_DISPLAY:
2077 case LFUN_MATH_SUBSCRIPT:
2078 case LFUN_MATH_SUPERSCRIPT:
2079 case LFUN_MATH_INSERT:
2080 case LFUN_MATH_AMS_MATRIX:
2081 case LFUN_MATH_MATRIX:
2082 case LFUN_MATH_DELIM:
2083 case LFUN_MATH_BIGDELIM:
2084 mathDispatch(cur, cmd);
2087 case LFUN_FONT_EMPH: {
2088 Font font(ignore_font, ignore_language);
2089 font.fontInfo().setEmph(FONT_TOGGLE);
2090 toggleAndShow(cur, this, font);
2094 case LFUN_FONT_ITAL: {
2095 Font font(ignore_font, ignore_language);
2096 font.fontInfo().setShape(ITALIC_SHAPE);
2097 toggleAndShow(cur, this, font);
2101 case LFUN_FONT_BOLD:
2102 case LFUN_FONT_BOLDSYMBOL: {
2103 Font font(ignore_font, ignore_language);
2104 font.fontInfo().setSeries(BOLD_SERIES);
2105 toggleAndShow(cur, this, font);
2109 case LFUN_FONT_NOUN: {
2110 Font font(ignore_font, ignore_language);
2111 font.fontInfo().setNoun(FONT_TOGGLE);
2112 toggleAndShow(cur, this, font);
2116 case LFUN_FONT_TYPEWRITER: {
2117 Font font(ignore_font, ignore_language);
2118 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2119 toggleAndShow(cur, this, font);
2123 case LFUN_FONT_SANS: {
2124 Font font(ignore_font, ignore_language);
2125 font.fontInfo().setFamily(SANS_FAMILY);
2126 toggleAndShow(cur, this, font);
2130 case LFUN_FONT_ROMAN: {
2131 Font font(ignore_font, ignore_language);
2132 font.fontInfo().setFamily(ROMAN_FAMILY);
2133 toggleAndShow(cur, this, font);
2137 case LFUN_FONT_DEFAULT: {
2138 Font font(inherit_font, ignore_language);
2139 toggleAndShow(cur, this, font);
2143 case LFUN_FONT_STRIKEOUT: {
2144 Font font(ignore_font, ignore_language);
2145 font.fontInfo().setStrikeout(FONT_TOGGLE);
2146 toggleAndShow(cur, this, font);
2150 case LFUN_FONT_UNDERUNDERLINE: {
2151 Font font(ignore_font, ignore_language);
2152 font.fontInfo().setUuline(FONT_TOGGLE);
2153 toggleAndShow(cur, this, font);
2157 case LFUN_FONT_UNDERWAVE: {
2158 Font font(ignore_font, ignore_language);
2159 font.fontInfo().setUwave(FONT_TOGGLE);
2160 toggleAndShow(cur, this, font);
2164 case LFUN_FONT_UNDERLINE: {
2165 Font font(ignore_font, ignore_language);
2166 font.fontInfo().setUnderbar(FONT_TOGGLE);
2167 toggleAndShow(cur, this, font);
2171 case LFUN_FONT_SIZE: {
2172 Font font(ignore_font, ignore_language);
2173 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2174 toggleAndShow(cur, this, font);
2178 case LFUN_LANGUAGE: {
2179 string const lang_arg = cmd.getArg(0);
2180 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2181 Language const * lang =
2182 reset ? reset_language
2183 : languages.getLanguage(lang_arg);
2184 // we allow reset_language, which is 0, but only if it
2185 // was requested via empty or "reset" arg.
2186 if (!lang && !reset)
2188 bool const toggle = (cmd.getArg(1) != "set");
2189 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2190 Font font(ignore_font, lang);
2191 toggleAndShow(cur, this, font, toggle);
2195 case LFUN_TEXTSTYLE_APPLY:
2196 toggleAndShow(cur, this, freefont, toggleall);
2197 cur.message(_("Character set"));
2200 // Set the freefont using the contents of \param data dispatched from
2201 // the frontends and apply it at the current cursor location.
2202 case LFUN_TEXTSTYLE_UPDATE: {
2205 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2208 toggleAndShow(cur, this, freefont, toggleall);
2209 cur.message(_("Character set"));
2211 lyxerr << "Argument not ok";
2216 case LFUN_FINISHED_LEFT:
2217 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2218 // We're leaving an inset, going left. If the inset is LTR, we're
2219 // leaving from the front, so we should not move (remain at --- but
2220 // not in --- the inset). If the inset is RTL, move left, without
2221 // entering the inset itself; i.e., move to after the inset.
2222 if (cur.paragraph().getFontSettings(
2223 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2224 cursorVisLeft(cur, true);
2227 case LFUN_FINISHED_RIGHT:
2228 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2229 // We're leaving an inset, going right. If the inset is RTL, we're
2230 // leaving from the front, so we should not move (remain at --- but
2231 // not in --- the inset). If the inset is LTR, move right, without
2232 // entering the inset itself; i.e., move to after the inset.
2233 if (!cur.paragraph().getFontSettings(
2234 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2235 cursorVisRight(cur, true);
2238 case LFUN_FINISHED_BACKWARD:
2239 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2240 cur.setCurrentFont();
2243 case LFUN_FINISHED_FORWARD:
2244 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2246 cur.setCurrentFont();
2249 case LFUN_LAYOUT_PARAGRAPH: {
2251 params2string(cur.paragraph(), data);
2252 data = "show\n" + data;
2253 bv->showDialog("paragraph", data);
2257 case LFUN_PARAGRAPH_UPDATE: {
2259 params2string(cur.paragraph(), data);
2261 // Will the paragraph accept changes from the dialog?
2263 cur.inset().allowParagraphCustomization(cur.idx());
2265 data = "update " + convert<string>(accept) + '\n' + data;
2266 bv->updateDialog("paragraph", data);
2270 case LFUN_ACCENT_UMLAUT:
2271 case LFUN_ACCENT_CIRCUMFLEX:
2272 case LFUN_ACCENT_GRAVE:
2273 case LFUN_ACCENT_ACUTE:
2274 case LFUN_ACCENT_TILDE:
2275 case LFUN_ACCENT_PERISPOMENI:
2276 case LFUN_ACCENT_CEDILLA:
2277 case LFUN_ACCENT_MACRON:
2278 case LFUN_ACCENT_DOT:
2279 case LFUN_ACCENT_UNDERDOT:
2280 case LFUN_ACCENT_UNDERBAR:
2281 case LFUN_ACCENT_CARON:
2282 case LFUN_ACCENT_BREVE:
2283 case LFUN_ACCENT_TIE:
2284 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2285 case LFUN_ACCENT_CIRCLE:
2286 case LFUN_ACCENT_OGONEK:
2287 theApp()->handleKeyFunc(cmd.action());
2288 if (!cmd.argument().empty())
2289 // FIXME: Are all these characters encoded in one byte in utf8?
2290 bv->translateAndInsert(cmd.argument()[0], this, cur);
2291 cur.screenUpdateFlags(Update::FitCursor);
2294 case LFUN_FLOAT_LIST_INSERT: {
2295 DocumentClass const & tclass = bv->buffer().params().documentClass();
2296 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2298 if (cur.selection())
2299 cutSelection(cur, true, false);
2300 breakParagraph(cur);
2302 if (cur.lastpos() != 0) {
2303 cursorBackward(cur);
2304 breakParagraph(cur);
2307 docstring const laystr = cur.inset().usePlainLayout() ?
2308 tclass.plainLayoutName() :
2309 tclass.defaultLayoutName();
2310 setLayout(cur, laystr);
2311 ParagraphParameters p;
2312 // FIXME If this call were replaced with one to clearParagraphParams(),
2313 // then we could get rid of this method altogether.
2314 setParagraphs(cur, p);
2315 // FIXME This should be simplified when InsetFloatList takes a
2316 // Buffer in its constructor.
2317 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2318 ifl->setBuffer(bv->buffer());
2319 insertInset(cur, ifl);
2322 lyxerr << "Non-existent float type: "
2323 << to_utf8(cmd.argument()) << endl;
2328 case LFUN_CHANGE_ACCEPT: {
2329 acceptOrRejectChanges(cur, ACCEPT);
2333 case LFUN_CHANGE_REJECT: {
2334 acceptOrRejectChanges(cur, REJECT);
2338 case LFUN_THESAURUS_ENTRY: {
2339 Language const * language = cur.getFont().language();
2340 docstring arg = cmd.argument();
2342 arg = cur.selectionAsString(false);
2344 if (arg.size() > 100 || arg.empty()) {
2345 // Get word or selection
2346 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2347 arg = cur.selectionAsString(false);
2348 arg += " lang=" + from_ascii(language->lang());
2351 string lang = cmd.getArg(1);
2352 // This duplicates the code in GuiThesaurus::initialiseParams
2353 if (prefixIs(lang, "lang=")) {
2354 language = languages.getLanguage(lang.substr(5));
2356 language = cur.getFont().language();
2359 string lang = language->code();
2360 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2361 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2362 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2363 _("The path to the thesaurus directory has not been specified.\n"
2364 "The thesaurus is not functional.\n"
2365 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2368 bv->showDialog("thesaurus", to_utf8(arg));
2372 case LFUN_SPELLING_ADD: {
2373 Language const * language = getLanguage(cur, cmd.getArg(1));
2374 docstring word = from_utf8(cmd.getArg(0));
2376 word = cur.selectionAsString(false);
2378 if (word.size() > 100 || word.empty()) {
2379 // Get word or selection
2380 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2381 word = cur.selectionAsString(false);
2384 WordLangTuple wl(word, language);
2385 theSpellChecker()->insert(wl);
2389 case LFUN_SPELLING_IGNORE: {
2390 Language const * language = getLanguage(cur, cmd.getArg(1));
2391 docstring word = from_utf8(cmd.getArg(0));
2393 word = cur.selectionAsString(false);
2395 if (word.size() > 100 || word.empty()) {
2396 // Get word or selection
2397 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2398 word = cur.selectionAsString(false);
2401 WordLangTuple wl(word, language);
2402 theSpellChecker()->accept(wl);
2406 case LFUN_SPELLING_REMOVE: {
2407 Language const * language = getLanguage(cur, cmd.getArg(1));
2408 docstring word = from_utf8(cmd.getArg(0));
2410 word = cur.selectionAsString(false);
2412 if (word.size() > 100 || word.empty()) {
2413 // Get word or selection
2414 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2415 word = cur.selectionAsString(false);
2418 WordLangTuple wl(word, language);
2419 theSpellChecker()->remove(wl);
2423 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2424 // Given data, an encoding of the ParagraphParameters
2425 // generated in the Paragraph dialog, this function sets
2426 // the current paragraph, or currently selected paragraphs,
2428 // NOTE: This function overrides all existing settings.
2429 setParagraphs(cur, cmd.argument());
2430 cur.message(_("Paragraph layout set"));
2434 case LFUN_PARAGRAPH_PARAMS: {
2435 // Given data, an encoding of the ParagraphParameters as we'd
2436 // find them in a LyX file, this function modifies the current paragraph,
2437 // or currently selected paragraphs.
2438 // NOTE: This function only modifies, and does not override, existing
2440 setParagraphs(cur, cmd.argument(), true);
2441 cur.message(_("Paragraph layout set"));
2446 if (cur.selection()) {
2447 cur.setSelection(false);
2450 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2451 // correct, but I'm not 100% sure -- dov, 071019
2452 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2456 case LFUN_OUTLINE_UP:
2457 outline(OutlineUp, cur);
2458 setCursor(cur, cur.pit(), 0);
2459 cur.forceBufferUpdate();
2463 case LFUN_OUTLINE_DOWN:
2464 outline(OutlineDown, cur);
2465 setCursor(cur, cur.pit(), 0);
2466 cur.forceBufferUpdate();
2470 case LFUN_OUTLINE_IN:
2471 outline(OutlineIn, cur);
2472 cur.forceBufferUpdate();
2476 case LFUN_OUTLINE_OUT:
2477 outline(OutlineOut, cur);
2478 cur.forceBufferUpdate();
2482 case LFUN_SERVER_GET_STATISTICS:
2484 DocIterator from, to;
2485 if (cur.selection()) {
2486 from = cur.selectionBegin();
2487 to = cur.selectionEnd();
2489 from = doc_iterator_begin(cur.buffer());
2490 to = doc_iterator_end(cur.buffer());
2493 cur.buffer()->updateStatistics(from, to);
2494 string const arg0 = cmd.getArg(0);
2495 if (arg0 == "words") {
2496 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2497 } else if (arg0 == "chars") {
2498 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2499 } else if (arg0 == "chars-space") {
2500 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2502 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2503 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2504 + convert<docstring>(cur.buffer()->charCount(true)));
2510 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2515 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2517 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2518 // Check for misspelled text
2519 // The redraw is useful because of the painting of
2520 // misspelled markers depends on the cursor position.
2521 // Trigger a redraw for cursor moves inside misspelled text.
2522 if (!cur.inTexted()) {
2523 // move from regular text to math
2524 needsUpdate = last_misspelled;
2525 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2526 // move inside regular text
2527 needsUpdate = last_misspelled
2528 || cur.paragraph().isMisspelled(cur.pos(), true);
2532 // FIXME: The cursor flag is reset two lines below
2533 // so we need to check here if some of the LFUN did touch that.
2534 // for now only Text::erase() and Text::backspace() do that.
2535 // The plan is to verify all the LFUNs and then to remove this
2536 // singleParUpdate boolean altogether.
2537 if (cur.result().screenUpdate() & Update::Force) {
2538 singleParUpdate = false;
2542 // FIXME: the following code should go in favor of fine grained
2543 // update flag treatment.
2544 if (singleParUpdate) {
2545 // Inserting characters does not change par height in general. So, try
2546 // to update _only_ this paragraph. BufferView will detect if a full
2547 // metrics update is needed anyway.
2548 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2552 && &oldTopSlice.inset() == &cur.inset()
2553 && oldTopSlice.idx() == cur.idx()
2554 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2555 && !cur.selection())
2556 // FIXME: it would be better if we could just do this
2558 //if (cur.result().update() != Update::FitCursor)
2559 // cur.noScreenUpdate();
2561 // But some LFUNs do not set Update::FitCursor when needed, so we
2562 // do it for all. This is not very harmfull as FitCursor will provoke
2563 // a full redraw only if needed but still, a proper review of all LFUN
2564 // should be done and this needsUpdate boolean can then be removed.
2565 cur.screenUpdateFlags(Update::FitCursor);
2567 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2571 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2572 FuncStatus & flag) const
2574 LBUFERR(this == cur.text());
2576 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2578 bool allow_in_passthru = false;
2579 InsetCode code = NO_CODE;
2581 switch (cmd.action()) {
2583 case LFUN_DEPTH_DECREMENT:
2584 enable = changeDepthAllowed(cur, DEC_DEPTH);
2587 case LFUN_DEPTH_INCREMENT:
2588 enable = changeDepthAllowed(cur, INC_DEPTH);
2592 // FIXME We really should not allow this to be put, e.g.,
2593 // in a footnote, or in ERT. But it would make sense in a
2594 // branch, so I'm not sure what to do.
2595 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2598 case LFUN_DIALOG_SHOW_NEW_INSET:
2599 if (cmd.argument() == "bibitem")
2600 code = BIBITEM_CODE;
2601 else if (cmd.argument() == "bibtex") {
2603 // not allowed in description items
2604 enable = !inDescriptionItem(cur);
2606 else if (cmd.argument() == "box")
2608 else if (cmd.argument() == "branch")
2610 else if (cmd.argument() == "citation")
2612 else if (cmd.argument() == "ert")
2614 else if (cmd.argument() == "external")
2615 code = EXTERNAL_CODE;
2616 else if (cmd.argument() == "float")
2618 else if (cmd.argument() == "graphics")
2619 code = GRAPHICS_CODE;
2620 else if (cmd.argument() == "href")
2621 code = HYPERLINK_CODE;
2622 else if (cmd.argument() == "include")
2623 code = INCLUDE_CODE;
2624 else if (cmd.argument() == "index")
2626 else if (cmd.argument() == "index_print")
2627 code = INDEX_PRINT_CODE;
2628 else if (cmd.argument() == "listings")
2629 code = LISTINGS_CODE;
2630 else if (cmd.argument() == "mathspace")
2631 code = MATH_HULL_CODE;
2632 else if (cmd.argument() == "nomenclature")
2633 code = NOMENCL_CODE;
2634 else if (cmd.argument() == "nomencl_print")
2635 code = NOMENCL_PRINT_CODE;
2636 else if (cmd.argument() == "label")
2638 else if (cmd.argument() == "line")
2640 else if (cmd.argument() == "note")
2642 else if (cmd.argument() == "phantom")
2643 code = PHANTOM_CODE;
2644 else if (cmd.argument() == "ref")
2646 else if (cmd.argument() == "space")
2648 else if (cmd.argument() == "toc")
2650 else if (cmd.argument() == "vspace")
2652 else if (cmd.argument() == "wrap")
2656 case LFUN_ERT_INSERT:
2659 case LFUN_LISTING_INSERT:
2660 code = LISTINGS_CODE;
2661 // not allowed in description items
2662 enable = !inDescriptionItem(cur);
2664 case LFUN_FOOTNOTE_INSERT:
2667 case LFUN_TABULAR_INSERT:
2668 code = TABULAR_CODE;
2670 case LFUN_MARGINALNOTE_INSERT:
2673 case LFUN_FLOAT_INSERT:
2674 case LFUN_FLOAT_WIDE_INSERT:
2675 // FIXME: If there is a selection, we should check whether there
2676 // are floats in the selection, but this has performance issues, see
2677 // LFUN_CHANGE_ACCEPT/REJECT.
2679 if (inDescriptionItem(cur))
2680 // not allowed in description items
2683 InsetCode const inset_code = cur.inset().lyxCode();
2685 // algorithm floats cannot be put in another float
2686 if (to_utf8(cmd.argument()) == "algorithm") {
2687 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2691 // for figures and tables: only allow in another
2692 // float or wrap if it is of the same type and
2693 // not a subfloat already
2694 if(cur.inset().lyxCode() == code) {
2695 InsetFloat const & ins =
2696 static_cast<InsetFloat const &>(cur.inset());
2697 enable = ins.params().type == to_utf8(cmd.argument())
2698 && !ins.params().subfloat;
2699 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2700 InsetWrap const & ins =
2701 static_cast<InsetWrap const &>(cur.inset());
2702 enable = ins.params().type == to_utf8(cmd.argument());
2706 case LFUN_WRAP_INSERT:
2708 // not allowed in description items
2709 enable = !inDescriptionItem(cur);
2711 case LFUN_FLOAT_LIST_INSERT: {
2712 code = FLOAT_LIST_CODE;
2713 // not allowed in description items
2714 enable = !inDescriptionItem(cur);
2716 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2717 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2718 // make sure we know about such floats
2719 if (cit == floats.end() ||
2720 // and that we know how to generate a list of them
2721 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2722 flag.setUnknown(true);
2723 // probably not necessary, but...
2729 case LFUN_CAPTION_INSERT: {
2730 code = CAPTION_CODE;
2731 string arg = cmd.getArg(0);
2732 bool varia = arg != "LongTableNoNumber"
2733 && cur.inset().allowsCaptionVariation(arg);
2734 // not allowed in description items,
2735 // and in specific insets
2736 enable = !inDescriptionItem(cur)
2737 && (varia || arg.empty() || arg == "Standard");
2740 case LFUN_NOTE_INSERT:
2742 // in commands (sections etc.) and description items,
2743 // only Notes are allowed
2744 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2745 (!cur.paragraph().layout().isCommand()
2746 && !inDescriptionItem(cur)));
2748 case LFUN_FLEX_INSERT: {
2750 string s = cmd.getArg(0);
2752 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2753 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2754 il.lyxtype() != InsetLayout::CUSTOM &&
2755 il.lyxtype() != InsetLayout::ELEMENT &&
2756 il.lyxtype ()!= InsetLayout::STANDARD)
2760 case LFUN_BOX_INSERT:
2763 case LFUN_BRANCH_INSERT:
2765 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2766 && cur.buffer()->params().branchlist().empty())
2769 case LFUN_IPA_INSERT:
2772 case LFUN_PHANTOM_INSERT:
2773 code = PHANTOM_CODE;
2775 case LFUN_LABEL_INSERT:
2778 case LFUN_INFO_INSERT:
2781 case LFUN_ARGUMENT_INSERT: {
2783 allow_in_passthru = true;
2784 string const arg = cmd.getArg(0);
2789 Layout const & lay = cur.paragraph().layout();
2790 Layout::LaTeXArgMap args = lay.args();
2791 Layout::LaTeXArgMap::const_iterator const lait =
2793 if (lait != args.end()) {
2795 pit_type pit = cur.pit();
2796 pit_type lastpit = cur.pit();
2797 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2798 // In a sequence of "merged" environment layouts, we only allow
2799 // non-item arguments once.
2800 lastpit = cur.lastpit();
2801 // get the first paragraph in sequence with this layout
2802 depth_type const current_depth = cur.paragraph().params().depth();
2806 Paragraph cpar = pars_[pit - 1];
2807 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2813 for (; pit <= lastpit; ++pit) {
2814 if (pars_[pit].layout() != lay)
2816 InsetList::const_iterator it = pars_[pit].insetList().begin();
2817 InsetList::const_iterator end = pars_[pit].insetList().end();
2818 for (; it != end; ++it) {
2819 if (it->inset->lyxCode() == ARG_CODE) {
2820 InsetArgument const * ins =
2821 static_cast<InsetArgument const *>(it->inset);
2822 if (ins->name() == arg) {
2823 // we have this already
2834 case LFUN_INDEX_INSERT:
2837 case LFUN_INDEX_PRINT:
2838 code = INDEX_PRINT_CODE;
2839 // not allowed in description items
2840 enable = !inDescriptionItem(cur);
2842 case LFUN_NOMENCL_INSERT:
2843 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2847 code = NOMENCL_CODE;
2849 case LFUN_NOMENCL_PRINT:
2850 code = NOMENCL_PRINT_CODE;
2851 // not allowed in description items
2852 enable = !inDescriptionItem(cur);
2854 case LFUN_HREF_INSERT:
2855 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2859 code = HYPERLINK_CODE;
2861 case LFUN_IPAMACRO_INSERT: {
2862 string const arg = cmd.getArg(0);
2864 code = IPADECO_CODE;
2866 code = IPACHAR_CODE;
2869 case LFUN_QUOTE_INSERT:
2870 // always allow this, since we will inset a raw quote
2871 // if an inset is not allowed.
2873 case LFUN_SPECIALCHAR_INSERT:
2874 code = SPECIALCHAR_CODE;
2876 case LFUN_SPACE_INSERT:
2877 // slight hack: we know this is allowed in math mode
2881 case LFUN_PREVIEW_INSERT:
2882 code = PREVIEW_CODE;
2884 case LFUN_SCRIPT_INSERT:
2888 case LFUN_MATH_INSERT:
2889 case LFUN_MATH_AMS_MATRIX:
2890 case LFUN_MATH_MATRIX:
2891 case LFUN_MATH_DELIM:
2892 case LFUN_MATH_BIGDELIM:
2893 case LFUN_MATH_DISPLAY:
2894 case LFUN_MATH_MODE:
2895 case LFUN_MATH_MACRO:
2896 case LFUN_MATH_SUBSCRIPT:
2897 case LFUN_MATH_SUPERSCRIPT:
2898 code = MATH_HULL_CODE;
2901 case LFUN_REGEXP_MODE:
2902 code = MATH_HULL_CODE;
2903 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2906 case LFUN_INSET_MODIFY:
2907 // We need to disable this, because we may get called for a
2909 // InsetTabular::getStatus() -> InsetText::getStatus()
2910 // and we don't handle LFUN_INSET_MODIFY.
2914 case LFUN_FONT_EMPH:
2915 flag.setOnOff(fontinfo.emph() == FONT_ON);
2916 enable = !cur.paragraph().isPassThru();
2919 case LFUN_FONT_ITAL:
2920 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2921 enable = !cur.paragraph().isPassThru();
2924 case LFUN_FONT_NOUN:
2925 flag.setOnOff(fontinfo.noun() == FONT_ON);
2926 enable = !cur.paragraph().isPassThru();
2929 case LFUN_FONT_BOLD:
2930 case LFUN_FONT_BOLDSYMBOL:
2931 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2932 enable = !cur.paragraph().isPassThru();
2935 case LFUN_FONT_SANS:
2936 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2937 enable = !cur.paragraph().isPassThru();
2940 case LFUN_FONT_ROMAN:
2941 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2942 enable = !cur.paragraph().isPassThru();
2945 case LFUN_FONT_TYPEWRITER:
2946 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2947 enable = !cur.paragraph().isPassThru();
2952 enable = cur.selection();
2956 if (cmd.argument().empty()) {
2957 if (theClipboard().isInternal())
2958 enable = cap::numberOfSelections() > 0;
2960 enable = !theClipboard().empty();
2964 // we have an argument
2965 string const arg = to_utf8(cmd.argument());
2966 if (isStrUnsignedInt(arg)) {
2967 // it's a number and therefore means the internal stack
2968 unsigned int n = convert<unsigned int>(arg);
2969 enable = cap::numberOfSelections() > n;
2973 // explicit text type?
2974 if (arg == "html") {
2975 // Do not enable for PlainTextType, since some tidying in the
2976 // frontend is needed for HTML, which is too unsafe for plain text.
2977 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
2979 } else if (arg == "latex") {
2980 // LaTeX is usually not available on the clipboard with
2981 // the correct MIME type, but in plain text.
2982 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
2983 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
2987 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2989 type = Clipboard::PdfGraphicsType;
2990 else if (arg == "png")
2991 type = Clipboard::PngGraphicsType;
2992 else if (arg == "jpeg")
2993 type = Clipboard::JpegGraphicsType;
2994 else if (arg == "linkback")
2995 type = Clipboard::LinkBackGraphicsType;
2996 else if (arg == "emf")
2997 type = Clipboard::EmfGraphicsType;
2998 else if (arg == "wmf")
2999 type = Clipboard::WmfGraphicsType;
3002 LYXERR0("Unrecognized graphics type: " << arg);
3003 // we don't want to assert if the user just mistyped the LFUN
3004 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3008 enable = theClipboard().hasGraphicsContents(type);
3012 case LFUN_CLIPBOARD_PASTE:
3013 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3014 enable = !theClipboard().empty();
3017 case LFUN_PRIMARY_SELECTION_PASTE:
3018 enable = cur.selection() || !theSelection().empty();
3021 case LFUN_SELECTION_PASTE:
3022 enable = cap::selection();
3025 case LFUN_PARAGRAPH_MOVE_UP:
3026 enable = cur.pit() > 0 && !cur.selection();
3029 case LFUN_PARAGRAPH_MOVE_DOWN:
3030 enable = cur.pit() < cur.lastpit() && !cur.selection();
3033 case LFUN_CHANGE_ACCEPT:
3034 case LFUN_CHANGE_REJECT:
3035 // In principle, these LFUNs should only be enabled if there
3036 // is a change at the current position/in the current selection.
3037 // However, without proper optimizations, this will inevitably
3038 // result in unacceptable performance - just imagine a user who
3039 // wants to select the complete content of a long document.
3040 if (!cur.selection())
3041 enable = cur.paragraph().isChanged(cur.pos());
3043 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3048 case LFUN_OUTLINE_UP:
3049 case LFUN_OUTLINE_DOWN:
3050 case LFUN_OUTLINE_IN:
3051 case LFUN_OUTLINE_OUT:
3052 // FIXME: LyX is not ready for outlining within inset.
3053 enable = isMainText()
3054 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3057 case LFUN_NEWLINE_INSERT:
3058 // LaTeX restrictions (labels or empty par)
3059 enable = !cur.paragraph().isPassThru()
3060 && cur.pos() > cur.paragraph().beginOfBody();
3063 case LFUN_SEPARATOR_INSERT:
3064 // Always enabled for now
3068 case LFUN_TAB_INSERT:
3069 case LFUN_TAB_DELETE:
3070 enable = cur.paragraph().isPassThru();
3073 case LFUN_SET_GRAPHICS_GROUP: {
3074 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3078 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3082 case LFUN_NEWPAGE_INSERT:
3083 // not allowed in description items
3084 code = NEWPAGE_CODE;
3085 enable = !inDescriptionItem(cur);
3088 case LFUN_DATE_INSERT: {
3089 string const format = cmd.argument().empty()
3090 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3091 enable = support::os::is_valid_strftime(format);
3096 enable = !cur.paragraph().isPassThru();
3097 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3100 case LFUN_PARAGRAPH_BREAK:
3101 enable = inset().allowMultiPar();
3104 case LFUN_SPELLING_ADD:
3105 case LFUN_SPELLING_IGNORE:
3106 case LFUN_SPELLING_REMOVE:
3107 enable = theSpellChecker() != NULL;
3108 if (enable && !cmd.getArg(1).empty()) {
3109 // validate explicitly given language
3110 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3111 enable &= lang != NULL;
3116 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3117 docstring layout = cmd.argument();
3119 layout = tclass.defaultLayoutName();
3120 enable = !cur.inset().forcePlainLayout() && tclass.hasLayout(layout);
3122 flag.setOnOff(layout == cur.paragraph().layout().name());
3126 case LFUN_ENVIRONMENT_SPLIT: {
3127 if (cmd.argument() == "outer") {
3128 // check if we have an environment in our nesting hierarchy
3130 depth_type const current_depth = cur.paragraph().params().depth();
3131 pit_type pit = cur.pit();
3132 Paragraph cpar = pars_[pit];
3134 if (pit == 0 || cpar.params().depth() == 0)
3138 if (cpar.params().depth() < current_depth)
3139 res = cpar.layout().isEnvironment();
3144 else if (cur.paragraph().layout().isEnvironment()) {
3152 case LFUN_LAYOUT_PARAGRAPH:
3153 case LFUN_PARAGRAPH_PARAMS:
3154 case LFUN_PARAGRAPH_PARAMS_APPLY:
3155 case LFUN_PARAGRAPH_UPDATE:
3156 enable = cur.inset().allowParagraphCustomization();
3159 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3160 // Because they insert COMBINING DIACRITICAL Unicode characters,
3161 // that cannot be handled by LaTeX but must be converted according
3162 // to the definition in lib/unicodesymbols?
3163 case LFUN_ACCENT_ACUTE:
3164 case LFUN_ACCENT_BREVE:
3165 case LFUN_ACCENT_CARON:
3166 case LFUN_ACCENT_CEDILLA:
3167 case LFUN_ACCENT_CIRCLE:
3168 case LFUN_ACCENT_CIRCUMFLEX:
3169 case LFUN_ACCENT_DOT:
3170 case LFUN_ACCENT_GRAVE:
3171 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3172 case LFUN_ACCENT_MACRON:
3173 case LFUN_ACCENT_OGONEK:
3174 case LFUN_ACCENT_TIE:
3175 case LFUN_ACCENT_TILDE:
3176 case LFUN_ACCENT_PERISPOMENI:
3177 case LFUN_ACCENT_UMLAUT:
3178 case LFUN_ACCENT_UNDERBAR:
3179 case LFUN_ACCENT_UNDERDOT:
3180 case LFUN_FONT_DEFAULT:
3181 case LFUN_FONT_FRAK:
3182 case LFUN_FONT_SIZE:
3183 case LFUN_FONT_STATE:
3184 case LFUN_FONT_UNDERLINE:
3185 case LFUN_FONT_STRIKEOUT:
3186 case LFUN_FONT_UNDERUNDERLINE:
3187 case LFUN_FONT_UNDERWAVE:
3188 case LFUN_TEXTSTYLE_APPLY:
3189 case LFUN_TEXTSTYLE_UPDATE:
3190 enable = !cur.paragraph().isPassThru();
3193 case LFUN_WORD_DELETE_FORWARD:
3194 case LFUN_WORD_DELETE_BACKWARD:
3195 case LFUN_LINE_DELETE_FORWARD:
3196 case LFUN_WORD_FORWARD:
3197 case LFUN_WORD_BACKWARD:
3198 case LFUN_WORD_RIGHT:
3199 case LFUN_WORD_LEFT:
3200 case LFUN_CHAR_FORWARD:
3201 case LFUN_CHAR_FORWARD_SELECT:
3202 case LFUN_CHAR_BACKWARD:
3203 case LFUN_CHAR_BACKWARD_SELECT:
3204 case LFUN_CHAR_LEFT:
3205 case LFUN_CHAR_LEFT_SELECT:
3206 case LFUN_CHAR_RIGHT:
3207 case LFUN_CHAR_RIGHT_SELECT:
3209 case LFUN_UP_SELECT:
3211 case LFUN_DOWN_SELECT:
3212 case LFUN_PARAGRAPH_UP_SELECT:
3213 case LFUN_PARAGRAPH_DOWN_SELECT:
3214 case LFUN_LINE_BEGIN_SELECT:
3215 case LFUN_LINE_END_SELECT:
3216 case LFUN_WORD_FORWARD_SELECT:
3217 case LFUN_WORD_BACKWARD_SELECT:
3218 case LFUN_WORD_RIGHT_SELECT:
3219 case LFUN_WORD_LEFT_SELECT:
3220 case LFUN_WORD_SELECT:
3221 case LFUN_SECTION_SELECT:
3222 case LFUN_BUFFER_BEGIN:
3223 case LFUN_BUFFER_END:
3224 case LFUN_BUFFER_BEGIN_SELECT:
3225 case LFUN_BUFFER_END_SELECT:
3226 case LFUN_INSET_BEGIN:
3227 case LFUN_INSET_END:
3228 case LFUN_INSET_BEGIN_SELECT:
3229 case LFUN_INSET_END_SELECT:
3230 case LFUN_PARAGRAPH_UP:
3231 case LFUN_PARAGRAPH_DOWN:
3232 case LFUN_LINE_BEGIN:
3234 case LFUN_CHAR_DELETE_FORWARD:
3235 case LFUN_CHAR_DELETE_BACKWARD:
3236 case LFUN_WORD_UPCASE:
3237 case LFUN_WORD_LOWCASE:
3238 case LFUN_WORD_CAPITALIZE:
3239 case LFUN_CHARS_TRANSPOSE:
3240 case LFUN_SERVER_GET_XY:
3241 case LFUN_SERVER_SET_XY:
3242 case LFUN_SERVER_GET_LAYOUT:
3243 case LFUN_SELF_INSERT:
3244 case LFUN_UNICODE_INSERT:
3245 case LFUN_THESAURUS_ENTRY:
3247 case LFUN_SERVER_GET_STATISTICS:
3248 // these are handled in our dispatch()
3252 case LFUN_INSET_INSERT: {
3253 string const type = cmd.getArg(0);
3254 if (type == "toc") {
3256 // not allowed in description items
3257 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3258 enable = !inDescriptionItem(cur);
3271 || !cur.inset().insetAllowed(code)
3272 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3275 flag.setEnabled(enable);
3280 void Text::pasteString(Cursor & cur, docstring const & clip,
3283 if (!clip.empty()) {
3286 insertStringAsParagraphs(cur, clip, cur.current_font);
3288 insertStringAsLines(cur, clip, cur.current_font);
3293 // FIXME: an item inset would make things much easier.
3294 bool Text::inDescriptionItem(Cursor & cur) const
3296 Paragraph & par = cur.paragraph();
3297 pos_type const pos = cur.pos();
3298 pos_type const body_pos = par.beginOfBody();
3300 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3301 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3302 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3305 return (pos < body_pos
3307 && (pos == 0 || par.getChar(pos - 1) != ' ')));