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::cutSelectionToTemp;
94 using cap::pasteFromStack;
95 using cap::pasteFromTemp;
96 using cap::pasteClipboardText;
97 using cap::pasteClipboardGraphics;
98 using cap::replaceSelection;
99 using cap::grabAndEraseSelection;
100 using cap::selClearOrDel;
101 using cap::pasteSimpleText;
102 using frontend::Clipboard;
105 static Font freefont(ignore_font, ignore_language);
106 static bool toggleall = false;
108 static void toggleAndShow(Cursor & cur, Text * text,
109 Font const & font, bool toggleall = true)
111 text->toggleFree(cur, font, toggleall);
113 if (font.language() != ignore_language ||
114 font.fontInfo().number() != FONT_IGNORE) {
115 TextMetrics const & tm = cur.bv().textMetrics(text);
116 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
117 cur.real_current_font))
118 text->setCursor(cur, cur.pit(), cur.pos(),
119 false, !cur.boundary());
124 static void moveCursor(Cursor & cur, bool selecting)
126 if (selecting || cur.mark())
131 static void finishChange(Cursor & cur, bool selecting)
134 moveCursor(cur, selecting);
138 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
141 docstring sel = cur.selectionAsString(false);
143 // It may happen that sel is empty but there is a selection
144 replaceSelection(cur);
146 // Is this a valid formula?
150 #ifdef ENABLE_ASSERTIONS
151 const int old_pos = cur.pos();
153 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
154 #ifdef ENABLE_ASSERTIONS
155 LATTEST(old_pos == cur.pos());
157 cur.nextInset()->edit(cur, true);
158 if (cmd.action() != LFUN_MATH_MODE)
159 // LFUN_MATH_MODE has a different meaning in math mode
162 InsetMathHull * formula = new InsetMathHull(cur.buffer());
163 string const selstr = to_utf8(sel);
164 istringstream is(selstr);
167 if (!formula->readQuiet(lex)) {
168 // No valid formula, let's try with delims
169 is.str("$" + selstr + "$");
171 if (!formula->readQuiet(lex)) {
172 // Still not valid, leave it as is
180 cur.nextInset()->edit(cur, true);
181 LASSERT(cur.inMathed(), return);
185 cur.pos() = cur.lastpos();
186 if (cmd.action() != LFUN_MATH_MODE)
187 // LFUN_MATH_MODE has a different meaning in math mode
189 cur.clearSelection();
190 cur.pos() = cur.lastpos();
194 cur.message(from_utf8(N_("Math editor mode")));
196 cur.message(from_utf8(N_("No valid math formula")));
200 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
202 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
203 if (cur.inRegexped()) {
204 cur.message(_("Already in regular expression mode"));
208 docstring sel = cur.selectionAsString(false);
210 // It may happen that sel is empty but there is a selection
211 replaceSelection(cur);
213 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
214 cur.nextInset()->edit(cur, true);
217 cur.message(_("Regexp editor mode"));
221 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
224 cap::replaceSelection(cur);
225 cur.insert(new InsetSpecialChar(kind));
230 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
233 cap::replaceSelection(cur);
234 cur.insert(new InsetIPAChar(kind));
239 static bool doInsertInset(Cursor & cur, Text * text,
240 FuncRequest const & cmd, bool edit, bool pastesel)
242 Buffer & buffer = cur.bv().buffer();
243 BufferParams const & bparams = buffer.params();
244 Inset * inset = createInset(&buffer, cmd);
248 if (InsetCollapsable * ci = inset->asInsetCollapsable())
249 ci->setButtonLabel();
252 if (cmd.action() == LFUN_INDEX_INSERT) {
253 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
254 text->insertInset(cur, inset);
256 inset->edit(cur, true);
257 // Now put this into inset
258 Font const f(inherit_font, cur.current_font.language());
260 cur.text()->insertStringAsLines(cur, ds, f);
261 cur.leaveInset(*inset);
265 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
266 bool cotextinsert = false;
267 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
268 Layout const & lay = cur.paragraph().layout();
269 Layout::LaTeXArgMap args = lay.args();
270 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
271 if (lait != args.end())
272 cotextinsert = (*lait).second.insertcotext;
274 InsetLayout const & il = cur.inset().getLayout();
276 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
277 if (ilait != args.end())
278 cotextinsert = (*ilait).second.insertcotext;
280 // The argument requests to insert a copy of the co-text to the inset
283 // If we have a selection within a paragraph, use this
284 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
285 ds = cur.selectionAsString(false);
286 // else use the whole paragraph
288 ds = cur.paragraph().asString();
289 text->insertInset(cur, inset);
291 inset->edit(cur, true);
292 // Now put co-text into inset
293 Font const f(inherit_font, cur.current_font.language());
295 cur.text()->insertStringAsLines(cur, ds, f);
296 cur.leaveInset(*inset);
303 if (cur.selection()) {
304 cutSelectionToTemp(cur, false, pastesel);
305 cur.clearSelection();
308 text->insertInset(cur, inset);
311 inset->edit(cur, true);
313 if (!gotsel || !pastesel)
316 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
317 cur.buffer()->errors("Paste");
318 cur.clearSelection(); // bug 393
320 InsetText * inset_text = inset->asInsetText();
322 inset_text->fixParagraphsFont();
323 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
324 // reset first par to default
325 cur.text()->paragraphs().begin()
326 ->setPlainOrDefaultLayout(bparams.documentClass());
329 // Merge multiple paragraphs -- hack
330 while (cur.lastpit() > 0)
331 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
332 if (cmd.action() == LFUN_FLEX_INSERT)
335 cur.leaveInset(*inset);
336 if (cmd.action() == LFUN_PREVIEW_INSERT
337 || cmd.action() == LFUN_IPA_INSERT)
339 notifyCursorLeavesOrEnters(old, cur);
342 cur.leaveInset(*inset);
343 // reset surrounding par to default
344 DocumentClass const & dc = bparams.documentClass();
345 docstring const layoutname = inset->usePlainLayout()
346 ? dc.plainLayoutName()
347 : dc.defaultLayoutName();
348 text->setLayout(cur, layoutname);
354 string const freefont2string()
356 return freefont.toString(toggleall);
360 /// the type of outline operation
362 OutlineUp, // Move this header with text down
363 OutlineDown, // Move this header with text up
364 OutlineIn, // Make this header deeper
365 OutlineOut // Make this header shallower
369 static void outline(OutlineOp mode, Cursor & cur)
371 Buffer & buf = *cur.buffer();
372 pit_type & pit = cur.pit();
373 ParagraphList & pars = buf.text().paragraphs();
374 ParagraphList::iterator const bgn = pars.begin();
375 // The first paragraph of the area to be copied:
376 ParagraphList::iterator start = lyx::next(bgn, pit);
377 // The final paragraph of area to be copied:
378 ParagraphList::iterator finish = start;
379 ParagraphList::iterator const end = pars.end();
381 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
384 // Move out (down) from this section header
388 // Seek the one (on same level) below
389 for (; finish != end; ++finish) {
390 toclevel = buf.text().getTocLevel(distance(bgn, finish));
391 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
397 if (start == pars.begin())
400 ParagraphList::iterator dest = start;
401 // Move out (up) from this header
404 // Search previous same-level header above
407 toclevel = buf.text().getTocLevel(distance(bgn, dest));
409 && (toclevel == Layout::NOT_IN_TOC
410 || toclevel > thistoclevel));
411 // Not found; do nothing
412 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
414 pit_type const newpit = distance(bgn, dest);
415 pit_type const len = distance(start, finish);
416 pit_type const deletepit = pit + len;
417 buf.undo().recordUndo(cur, newpit, deletepit - 1);
418 pars.splice(dest, start, finish);
426 // Go one down from *this* header:
427 ParagraphList::iterator dest = next(finish, 1);
428 // Go further down to find header to insert in front of:
429 for (; dest != end; ++dest) {
430 toclevel = buf.text().getTocLevel(distance(bgn, dest));
431 if (toclevel != Layout::NOT_IN_TOC
432 && toclevel <= thistoclevel)
435 // One such was found:
436 pit_type newpit = distance(bgn, dest);
437 buf.undo().recordUndo(cur, pit, newpit - 1);
438 pit_type const len = distance(start, finish);
439 pars.splice(dest, start, finish);
440 cur.pit() = newpit - len;
445 pit_type const len = distance(start, finish);
446 buf.undo().recordUndo(cur, pit, pit + len - 1);
447 for (; start != finish; ++start) {
448 toclevel = buf.text().getTocLevel(distance(bgn, start));
449 if (toclevel == Layout::NOT_IN_TOC)
452 DocumentClass const & tc = buf.params().documentClass();
453 DocumentClass::const_iterator lit = tc.begin();
454 DocumentClass::const_iterator len = tc.end();
455 int const newtoclevel =
456 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
457 LabelType const oldlabeltype = start->layout().labeltype;
459 for (; lit != len; ++lit) {
460 if (lit->toclevel == newtoclevel &&
461 lit->labeltype == oldlabeltype) {
462 start->setLayout(*lit);
473 void Text::number(Cursor & cur)
475 FontInfo font = ignore_font;
476 font.setNumber(FONT_TOGGLE);
477 toggleAndShow(cur, this, Font(font, ignore_language));
481 bool Text::isRTL(Paragraph const & par) const
483 Buffer const & buffer = owner_->buffer();
484 return par.isRTL(buffer.params());
490 Language const * getLanguage(Cursor const & cur, string const & lang) {
491 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
497 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
499 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
501 // Dispatch if the cursor is inside the text. It is not the
502 // case for context menus (bug 5797).
503 if (cur.text() != this) {
508 BufferView * bv = &cur.bv();
509 TextMetrics * tm = &bv->textMetrics(this);
510 if (!tm->contains(cur.pit())) {
511 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
512 tm = &bv->textMetrics(this);
515 // FIXME: We use the update flag to indicates wether a singlePar or a
516 // full screen update is needed. We reset it here but shall we restore it
518 cur.noScreenUpdate();
520 LBUFERR(this == cur.text());
522 // NOTE: This should NOT be a reference. See commit 94a5481a.
523 CursorSlice const oldTopSlice = cur.top();
524 bool const oldBoundary = cur.boundary();
525 bool const oldSelection = cur.selection();
526 // Signals that, even if needsUpdate == false, an update of the
527 // cursor paragraph is required
528 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
529 LyXAction::SingleParUpdate);
530 // Signals that a full-screen update is required
531 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
532 LyXAction::NoUpdate) || singleParUpdate);
533 bool const last_misspelled = lyxrc.spellcheck_continuously
534 && cur.paragraph().isMisspelled(cur.pos(), true);
536 FuncCode const act = cmd.action();
539 case LFUN_PARAGRAPH_MOVE_DOWN: {
540 pit_type const pit = cur.pit();
541 cur.recordUndo(pit, pit + 1);
543 pars_.swap(pit, pit + 1);
545 cur.forceBufferUpdate();
550 case LFUN_PARAGRAPH_MOVE_UP: {
551 pit_type const pit = cur.pit();
552 cur.recordUndo(pit - 1, pit);
554 pars_.swap(pit, pit - 1);
557 cur.forceBufferUpdate();
561 case LFUN_APPENDIX: {
562 Paragraph & par = cur.paragraph();
563 bool start = !par.params().startOfAppendix();
565 // FIXME: The code below only makes sense at top level.
566 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
567 // ensure that we have only one start_of_appendix in this document
568 // FIXME: this don't work for multipart document!
569 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
570 if (pars_[tmp].params().startOfAppendix()) {
571 cur.recordUndo(tmp, tmp);
572 pars_[tmp].params().startOfAppendix(false);
578 par.params().startOfAppendix(start);
580 // we can set the refreshing parameters now
581 cur.forceBufferUpdate();
585 case LFUN_WORD_DELETE_FORWARD:
587 cutSelection(cur, true, false);
589 deleteWordForward(cur, cmd.getArg(0) == "force");
590 finishChange(cur, false);
593 case LFUN_WORD_DELETE_BACKWARD:
595 cutSelection(cur, true, false);
597 deleteWordBackward(cur, cmd.getArg(0) == "force");
598 finishChange(cur, false);
601 case LFUN_LINE_DELETE_FORWARD:
603 cutSelection(cur, true, false);
605 tm->deleteLineForward(cur);
606 finishChange(cur, false);
609 case LFUN_BUFFER_BEGIN:
610 case LFUN_BUFFER_BEGIN_SELECT:
611 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
612 if (cur.depth() == 1)
613 needsUpdate |= cursorTop(cur);
616 cur.screenUpdateFlags(Update::FitCursor);
619 case LFUN_BUFFER_END:
620 case LFUN_BUFFER_END_SELECT:
621 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
622 if (cur.depth() == 1)
623 needsUpdate |= cursorBottom(cur);
626 cur.screenUpdateFlags(Update::FitCursor);
629 case LFUN_INSET_BEGIN:
630 case LFUN_INSET_BEGIN_SELECT:
631 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
632 if (cur.depth() == 1 || !cur.top().at_begin())
633 needsUpdate |= cursorTop(cur);
636 cur.screenUpdateFlags(Update::FitCursor);
640 case LFUN_INSET_END_SELECT:
641 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
642 if (cur.depth() == 1 || !cur.top().at_end())
643 needsUpdate |= cursorBottom(cur);
646 cur.screenUpdateFlags(Update::FitCursor);
649 case LFUN_CHAR_FORWARD:
650 case LFUN_CHAR_FORWARD_SELECT: {
651 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
652 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
653 bool const cur_moved = cursorForward(cur);
654 needsUpdate |= cur_moved;
656 if (!cur_moved && oldTopSlice == cur.top()
657 && cur.boundary() == oldBoundary) {
659 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
661 // we will probably be moving out the inset, so we should execute
662 // the depm-mechanism, but only when the cursor has a place to
663 // go outside this inset, i.e. in a slice above.
664 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
665 && cur.pit() == cur.lastpit()) {
666 // The cursor hasn't changed yet. To give the
667 // DEPM the possibility of doing something we must
668 // provide it with two different cursors.
670 dummy.pos() = dummy.pit() = 0;
671 if (cur.bv().checkDepm(dummy, cur))
672 cur.forceBufferUpdate();
678 case LFUN_CHAR_BACKWARD:
679 case LFUN_CHAR_BACKWARD_SELECT: {
680 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
681 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
682 bool const cur_moved = cursorBackward(cur);
683 needsUpdate |= cur_moved;
685 if (!cur_moved && oldTopSlice == cur.top()
686 && cur.boundary() == oldBoundary) {
688 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
690 // we will probably be moving out the inset, so we should execute
691 // the depm-mechanism, but only when the cursor has a place to
692 // go outside this inset, i.e. in a slice above.
693 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
694 // The cursor hasn't changed yet. To give the
695 // DEPM the possibility of doing something we must
696 // provide it with two different cursors.
698 dummy.pos() = cur.lastpos();
699 dummy.pit() = cur.lastpit();
700 if (cur.bv().checkDepm(dummy, cur))
701 cur.forceBufferUpdate();
708 case LFUN_CHAR_LEFT_SELECT:
709 if (lyxrc.visual_cursor) {
710 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
711 bool const cur_moved = cursorVisLeft(cur);
712 needsUpdate |= cur_moved;
713 if (!cur_moved && oldTopSlice == cur.top()
714 && cur.boundary() == oldBoundary) {
716 cmd = FuncRequest(LFUN_FINISHED_LEFT);
719 if (cur.reverseDirectionNeeded()) {
720 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
721 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
723 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
724 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
731 case LFUN_CHAR_RIGHT:
732 case LFUN_CHAR_RIGHT_SELECT:
733 if (lyxrc.visual_cursor) {
734 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
735 bool const cur_moved = cursorVisRight(cur);
736 needsUpdate |= cur_moved;
737 if (!cur_moved && oldTopSlice == cur.top()
738 && cur.boundary() == oldBoundary) {
740 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
743 if (cur.reverseDirectionNeeded()) {
744 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
745 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
747 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
748 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
757 case LFUN_DOWN_SELECT:
760 // stop/start the selection
761 bool select = cmd.action() == LFUN_DOWN_SELECT ||
762 cmd.action() == LFUN_UP_SELECT;
764 // move cursor up/down
765 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
766 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
768 if (!atFirstOrLastRow) {
769 needsUpdate |= cur.selHandle(select);
770 cur.upDownInText(up, needsUpdate);
771 needsUpdate |= cur.beforeDispatchCursor().inMathed();
773 // if the cursor cannot be moved up or down do not remove
774 // the selection right now, but wait for the next dispatch.
776 needsUpdate |= cur.selHandle(select);
777 cur.upDownInText(up, needsUpdate);
784 case LFUN_PARAGRAPH_UP:
785 case LFUN_PARAGRAPH_UP_SELECT:
786 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
787 needsUpdate |= cursorUpParagraph(cur);
790 case LFUN_PARAGRAPH_DOWN:
791 case LFUN_PARAGRAPH_DOWN_SELECT:
792 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
793 needsUpdate |= cursorDownParagraph(cur);
796 case LFUN_LINE_BEGIN:
797 case LFUN_LINE_BEGIN_SELECT:
798 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
799 needsUpdate |= tm->cursorHome(cur);
803 case LFUN_LINE_END_SELECT:
804 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
805 needsUpdate |= tm->cursorEnd(cur);
808 case LFUN_SECTION_SELECT: {
809 Buffer const & buf = *cur.buffer();
810 pit_type const pit = cur.pit();
811 ParagraphList & pars = buf.text().paragraphs();
812 ParagraphList::iterator bgn = pars.begin();
813 // The first paragraph of the area to be selected:
814 ParagraphList::iterator start = lyx::next(bgn, pit);
815 // The final paragraph of area to be selected:
816 ParagraphList::iterator finish = start;
817 ParagraphList::iterator end = pars.end();
819 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
820 if (thistoclevel == Layout::NOT_IN_TOC)
824 Cursor const old_cur = cur;
825 needsUpdate |= cur.selHandle(true);
827 // Move out (down) from this section header
831 // Seek the one (on same level) below
832 for (; finish != end; ++finish, ++cur.pit()) {
833 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
834 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
837 cur.pos() = cur.lastpos();
839 needsUpdate |= cur != old_cur;
843 case LFUN_WORD_RIGHT:
844 case LFUN_WORD_RIGHT_SELECT:
845 if (lyxrc.visual_cursor) {
846 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
847 bool const cur_moved = cursorVisRightOneWord(cur);
848 needsUpdate |= cur_moved;
849 if (!cur_moved && oldTopSlice == cur.top()
850 && cur.boundary() == oldBoundary) {
852 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
855 if (cur.reverseDirectionNeeded()) {
856 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
857 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
859 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
860 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
867 case LFUN_WORD_FORWARD:
868 case LFUN_WORD_FORWARD_SELECT: {
869 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
870 bool const cur_moved = cursorForwardOneWord(cur);
871 needsUpdate |= cur_moved;
873 if (!cur_moved && oldTopSlice == cur.top()
874 && cur.boundary() == oldBoundary) {
876 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
878 // we will probably be moving out the inset, so we should execute
879 // the depm-mechanism, but only when the cursor has a place to
880 // go outside this inset, i.e. in a slice above.
881 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
882 && cur.pit() == cur.lastpit()) {
883 // The cursor hasn't changed yet. To give the
884 // DEPM the possibility of doing something we must
885 // provide it with two different cursors.
887 dummy.pos() = dummy.pit() = 0;
888 if (cur.bv().checkDepm(dummy, cur))
889 cur.forceBufferUpdate();
896 case LFUN_WORD_LEFT_SELECT:
897 if (lyxrc.visual_cursor) {
898 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
899 bool const cur_moved = cursorVisLeftOneWord(cur);
900 needsUpdate |= cur_moved;
901 if (!cur_moved && oldTopSlice == cur.top()
902 && cur.boundary() == oldBoundary) {
904 cmd = FuncRequest(LFUN_FINISHED_LEFT);
907 if (cur.reverseDirectionNeeded()) {
908 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
909 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
911 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
912 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
919 case LFUN_WORD_BACKWARD:
920 case LFUN_WORD_BACKWARD_SELECT: {
921 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
922 bool const cur_moved = cursorBackwardOneWord(cur);
923 needsUpdate |= cur_moved;
925 if (!cur_moved && oldTopSlice == cur.top()
926 && cur.boundary() == oldBoundary) {
928 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
930 // we will probably be moving out the inset, so we should execute
931 // the depm-mechanism, but only when the cursor has a place to
932 // go outside this inset, i.e. in a slice above.
933 if (cur.depth() > 1 && cur.pos() == 0
935 // The cursor hasn't changed yet. To give the
936 // DEPM the possibility of doing something we must
937 // provide it with two different cursors.
939 dummy.pos() = cur.lastpos();
940 dummy.pit() = cur.lastpit();
941 if (cur.bv().checkDepm(dummy, cur))
942 cur.forceBufferUpdate();
948 case LFUN_WORD_SELECT: {
949 selectWord(cur, WHOLE_WORD);
950 finishChange(cur, true);
954 case LFUN_NEWLINE_INSERT: {
955 InsetNewlineParams inp;
956 docstring arg = cmd.argument();
957 if (arg == "linebreak")
958 inp.kind = InsetNewlineParams::LINEBREAK;
960 inp.kind = InsetNewlineParams::NEWLINE;
961 cap::replaceSelection(cur);
963 cur.insert(new InsetNewline(inp));
965 moveCursor(cur, false);
969 case LFUN_TAB_INSERT: {
970 bool const multi_par_selection = cur.selection() &&
971 cur.selBegin().pit() != cur.selEnd().pit();
972 if (multi_par_selection) {
973 // If there is a multi-paragraph selection, a tab is inserted
974 // at the beginning of each paragraph.
975 cur.recordUndoSelection();
976 pit_type const pit_end = cur.selEnd().pit();
977 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
978 pars_[pit].insertChar(0, '\t',
979 bv->buffer().params().track_changes);
980 // Update the selection pos to make sure the selection does not
981 // change as the inserted tab will increase the logical pos.
982 if (cur.realAnchor().pit() == pit)
983 cur.realAnchor().forwardPos();
984 if (cur.pit() == pit)
989 // Maybe we shouldn't allow tabs within a line, because they
990 // are not (yet) aligned as one might do expect.
991 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
997 case LFUN_TAB_DELETE: {
998 bool const tc = bv->buffer().params().track_changes;
999 if (cur.selection()) {
1000 // If there is a selection, a tab (if present) is removed from
1001 // the beginning of each paragraph.
1002 cur.recordUndoSelection();
1003 pit_type const pit_end = cur.selEnd().pit();
1004 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1005 Paragraph & par = paragraphs()[pit];
1008 char_type const c = par.getChar(0);
1009 if (c == '\t' || c == ' ') {
1010 // remove either 1 tab or 4 spaces.
1011 int const n = (c == ' ' ? 4 : 1);
1012 for (int i = 0; i < n
1013 && !par.empty() && par.getChar(0) == c; ++i) {
1014 if (cur.pit() == pit)
1016 if (cur.realAnchor().pit() == pit
1017 && cur.realAnchor().pos() > 0 )
1018 cur.realAnchor().backwardPos();
1019 par.eraseChar(0, tc);
1025 // If there is no selection, try to remove a tab or some spaces
1026 // before the position of the cursor.
1027 Paragraph & par = paragraphs()[cur.pit()];
1028 pos_type const pos = cur.pos();
1033 char_type const c = par.getChar(pos - 1);
1037 par.eraseChar(cur.pos(), tc);
1039 for (int n_spaces = 0;
1041 && par.getChar(cur.pos() - 1) == ' '
1045 par.eraseChar(cur.pos(), tc);
1052 case LFUN_CHAR_DELETE_FORWARD:
1053 if (!cur.selection()) {
1054 if (cur.pos() == cur.paragraph().size())
1055 // Par boundary, force full-screen update
1056 singleParUpdate = false;
1057 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1059 cur.selection(true);
1064 needsUpdate |= erase(cur);
1067 cutSelection(cur, true, false);
1068 singleParUpdate = false;
1070 moveCursor(cur, false);
1073 case LFUN_CHAR_DELETE_BACKWARD:
1074 if (!cur.selection()) {
1075 if (bv->getIntl().getTransManager().backspace()) {
1076 bool par_boundary = cur.pos() == 0;
1077 bool first_par = cur.pit() == 0;
1078 // Par boundary, full-screen update
1080 singleParUpdate = false;
1081 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1083 cur.selection(true);
1088 needsUpdate |= backspace(cur);
1090 if (par_boundary && !first_par && cur.pos() > 0
1091 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1092 needsUpdate |= backspace(cur);
1097 cutSelection(cur, true, false);
1098 singleParUpdate = false;
1102 case LFUN_PARAGRAPH_BREAK: {
1103 cap::replaceSelection(cur);
1104 pit_type pit = cur.pit();
1105 Paragraph const & par = pars_[pit];
1106 bool lastpar = (pit == pit_type(pars_.size() - 1));
1107 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1108 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
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 == "allowbreak")
1247 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1248 else if (name == "ligature-break")
1249 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1250 else if (name == "slash")
1251 specialChar(cur, InsetSpecialChar::SLASH);
1252 else if (name == "nobreakdash")
1253 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1254 else if (name == "dots")
1255 specialChar(cur, InsetSpecialChar::LDOTS);
1256 else if (name == "end-of-sentence")
1257 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1258 else if (name == "menu-separator")
1259 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1260 else if (name == "lyx")
1261 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1262 else if (name == "tex")
1263 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1264 else if (name == "latex")
1265 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1266 else if (name == "latex2e")
1267 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1268 else if (name.empty())
1269 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1271 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1275 case LFUN_IPAMACRO_INSERT: {
1276 string const arg = cmd.getArg(0);
1277 if (arg == "deco") {
1278 // Open the inset, and move the current selection
1280 doInsertInset(cur, this, cmd, true, true);
1282 // Some insets are numbered, others are shown in the outline pane so
1283 // let's update the labels and the toc backend.
1284 cur.forceBufferUpdate();
1287 if (arg == "tone-falling")
1288 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1289 else if (arg == "tone-rising")
1290 ipaChar(cur, InsetIPAChar::TONE_RISING);
1291 else if (arg == "tone-high-rising")
1292 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1293 else if (arg == "tone-low-rising")
1294 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1295 else if (arg == "tone-high-rising-falling")
1296 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1297 else if (arg.empty())
1298 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1300 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1304 case LFUN_WORD_UPCASE:
1305 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1308 case LFUN_WORD_LOWCASE:
1309 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1312 case LFUN_WORD_CAPITALIZE:
1313 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1316 case LFUN_CHARS_TRANSPOSE:
1317 charsTranspose(cur);
1321 cur.message(_("Paste"));
1322 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1323 cap::replaceSelection(cur);
1325 // without argument?
1326 string const arg = to_utf8(cmd.argument());
1328 bool tryGraphics = true;
1329 if (theClipboard().isInternal())
1330 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1331 else if (theClipboard().hasTextContents()) {
1332 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1333 true, Clipboard::AnyTextType))
1334 tryGraphics = false;
1336 if (tryGraphics && theClipboard().hasGraphicsContents())
1337 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1338 } else if (isStrUnsignedInt(arg)) {
1339 // we have a numerical argument
1340 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1341 convert<unsigned int>(arg));
1342 } else if (arg == "html" || arg == "latex") {
1343 Clipboard::TextType type = (arg == "html") ?
1344 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1345 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1347 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1349 type = Clipboard::PdfGraphicsType;
1350 else if (arg == "png")
1351 type = Clipboard::PngGraphicsType;
1352 else if (arg == "jpeg")
1353 type = Clipboard::JpegGraphicsType;
1354 else if (arg == "linkback")
1355 type = Clipboard::LinkBackGraphicsType;
1356 else if (arg == "emf")
1357 type = Clipboard::EmfGraphicsType;
1358 else if (arg == "wmf")
1359 type = Clipboard::WmfGraphicsType;
1361 // we also check in getStatus()
1362 LYXERR0("Unrecognized graphics type: " << arg);
1364 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1367 bv->buffer().errors("Paste");
1368 cur.clearSelection(); // bug 393
1374 cutSelection(cur, true, true);
1375 cur.message(_("Cut"));
1380 cur.message(_("Copy"));
1383 case LFUN_SERVER_GET_XY:
1384 cur.message(from_utf8(
1385 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1386 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1389 case LFUN_SERVER_SET_XY: {
1392 istringstream is(to_utf8(cmd.argument()));
1395 lyxerr << "SETXY: Could not parse coordinates in '"
1396 << to_utf8(cmd.argument()) << endl;
1398 tm->setCursorFromCoordinates(cur, x, y);
1402 case LFUN_SERVER_GET_LAYOUT:
1403 cur.message(cur.paragraph().layout().name());
1407 docstring layout = cmd.argument();
1408 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1410 Paragraph const & para = cur.paragraph();
1411 docstring const old_layout = para.layout().name();
1412 DocumentClass const & tclass = bv->buffer().params().documentClass();
1415 layout = tclass.defaultLayoutName();
1417 if (owner_->forcePlainLayout())
1418 // in this case only the empty layout is allowed
1419 layout = tclass.plainLayoutName();
1420 else if (para.usePlainLayout()) {
1421 // in this case, default layout maps to empty layout
1422 if (layout == tclass.defaultLayoutName())
1423 layout = tclass.plainLayoutName();
1425 // otherwise, the empty layout maps to the default
1426 if (layout == tclass.plainLayoutName())
1427 layout = tclass.defaultLayoutName();
1430 bool hasLayout = tclass.hasLayout(layout);
1432 // If the entry is obsolete, use the new one instead.
1434 docstring const & obs = tclass[layout].obsoleted_by();
1440 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1441 from_utf8(N_(" not known")));
1445 bool change_layout = (old_layout != layout);
1447 if (!change_layout && cur.selection() &&
1448 cur.selBegin().pit() != cur.selEnd().pit())
1450 pit_type spit = cur.selBegin().pit();
1451 pit_type epit = cur.selEnd().pit() + 1;
1452 while (spit != epit) {
1453 if (pars_[spit].layout().name() != old_layout) {
1454 change_layout = true;
1462 setLayout(cur, layout);
1464 Layout::LaTeXArgMap args = tclass[layout].args();
1465 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1466 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1467 for (; lait != laend; ++lait) {
1468 Layout::latexarg arg = (*lait).second;
1469 if (arg.autoinsert) {
1470 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1478 case LFUN_ENVIRONMENT_SPLIT: {
1479 bool const outer = cmd.argument() == "outer";
1480 Paragraph const & para = cur.paragraph();
1481 docstring layout = para.layout().name();
1482 depth_type split_depth = cur.paragraph().params().depth();
1484 // check if we have an environment in our nesting hierarchy
1485 pit_type pit = cur.pit();
1486 Paragraph cpar = pars_[pit];
1488 if (pit == 0 || cpar.params().depth() == 0)
1492 if (cpar.params().depth() < split_depth
1493 && cpar.layout().isEnvironment()) {
1494 layout = cpar.layout().name();
1495 split_depth = cpar.params().depth();
1500 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1502 while (cur.paragraph().params().depth() > split_depth)
1503 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1505 DocumentClass const & tc = bv->buffer().params().documentClass();
1506 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1507 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1508 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1509 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1514 case LFUN_CLIPBOARD_PASTE:
1515 cap::replaceSelection(cur);
1516 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1517 cmd.argument() == "paragraph");
1518 bv->buffer().errors("Paste");
1521 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1522 cap::replaceSelection(cur);
1523 pasteSimpleText(cur, cmd.argument() == "paragraph");
1526 case LFUN_PRIMARY_SELECTION_PASTE:
1527 cap::replaceSelection(cur);
1528 pasteString(cur, theSelection().get(),
1529 cmd.argument() == "paragraph");
1532 case LFUN_SELECTION_PASTE:
1533 // Copy the selection buffer to the clipboard stack,
1534 // because we want it to appear in the "Edit->Paste
1536 cap::replaceSelection(cur);
1537 cap::copySelectionToStack();
1538 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1539 bv->buffer().errors("Paste");
1542 case LFUN_UNICODE_INSERT: {
1543 if (cmd.argument().empty())
1545 docstring hexstring = cmd.argument();
1546 if (isHex(hexstring)) {
1547 char_type c = hexToInt(hexstring);
1548 if (c >= 32 && c < 0x10ffff) {
1549 lyxerr << "Inserting c: " << c << endl;
1550 docstring s = docstring(1, c);
1551 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1557 case LFUN_QUOTE_INSERT: {
1558 cap::replaceSelection(cur);
1561 Paragraph const & par = cur.paragraph();
1562 pos_type pos = cur.pos();
1563 // Ignore deleted text before cursor
1564 while (pos > 0 && par.isDeleted(pos - 1))
1567 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1569 // Guess quote side.
1570 // A space triggers an opening quote. This is passed if the preceding
1571 // char/inset is a space or at paragraph start.
1573 if (pos > 0 && !par.isSpace(pos - 1)) {
1574 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1575 // If an opening double quotation mark precedes, and this
1576 // is a single quote, make it opening as well
1578 static_cast<InsetQuotes &>(*cur.prevInset());
1579 string const type = ins.getType();
1580 if (!suffixIs(type, "ld") || !inner)
1581 c = par.getChar(pos - 1);
1583 else if (!cur.prevInset()
1584 || (cur.prevInset() && cur.prevInset()->isChar()))
1585 // If a char precedes, pass that and let InsetQuote decide
1586 c = par.getChar(pos - 1);
1589 if (par.getInset(pos - 1)
1590 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1591 // skip "invisible" insets
1595 c = par.getChar(pos - 1);
1600 InsetQuotesParams::QuoteLevel const quote_level = inner
1601 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1602 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1603 cur.buffer()->updateBuffer();
1608 case LFUN_DATE_INSERT: {
1609 string const format = cmd.argument().empty()
1610 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1611 string const time = formatted_time(current_time(), format);
1612 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1616 case LFUN_MOUSE_TRIPLE:
1617 if (cmd.button() == mouse_button::button1) {
1618 tm->cursorHome(cur);
1626 case LFUN_MOUSE_DOUBLE:
1627 if (cmd.button() == mouse_button::button1) {
1628 selectWord(cur, WHOLE_WORD);
1633 // Single-click on work area
1634 case LFUN_MOUSE_PRESS: {
1635 // We are not marking a selection with the keyboard in any case.
1636 Cursor & bvcur = cur.bv().cursor();
1637 bvcur.setMark(false);
1638 switch (cmd.button()) {
1639 case mouse_button::button1:
1640 if (!bvcur.selection())
1642 bvcur.resetAnchor();
1643 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1644 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1645 if (bvcur.wordSelection())
1646 selectWord(bvcur, WHOLE_WORD);
1649 case mouse_button::button2:
1650 if (lyxrc.mouse_middlebutton_paste) {
1651 // Middle mouse pasting.
1652 bv->mouseSetCursor(cur);
1654 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1655 "selection-paste ; primary-selection-paste paragraph"));
1657 cur.noScreenUpdate();
1660 case mouse_button::button3: {
1661 // Don't do anything if we right-click a
1662 // selection, a context menu will popup.
1663 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1664 && cur < bvcur.selectionEnd()) {
1665 cur.noScreenUpdate();
1668 if (!bv->mouseSetCursor(cur, false))
1669 cur.screenUpdateFlags(Update::FitCursor);
1675 } // switch (cmd.button())
1678 case LFUN_MOUSE_MOTION: {
1679 // Mouse motion with right or middle mouse do nothing for now.
1680 if (cmd.button() != mouse_button::button1) {
1681 cur.noScreenUpdate();
1684 // ignore motions deeper nested than the real anchor
1685 Cursor & bvcur = cur.bv().cursor();
1686 if (!bvcur.realAnchor().hasPart(cur)) {
1690 CursorSlice old = bvcur.top();
1692 int const wh = bv->workHeight();
1693 int const y = max(0, min(wh - 1, cmd.y()));
1695 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1696 cur.setTargetX(cmd.x());
1697 // Don't allow selecting a separator inset
1698 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1701 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1702 else if (cmd.y() < 0)
1703 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1704 // This is to allow jumping over large insets
1705 if (cur.top() == old) {
1707 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1708 else if (cmd.y() < 0)
1709 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1711 // We continue with our existing selection or start a new one, so don't
1712 // reset the anchor.
1713 bvcur.setCursor(cur);
1714 bvcur.selection(true);
1715 if (cur.top() == old) {
1716 // We didn't move one iota, so no need to update the screen.
1717 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1718 //cur.noScreenUpdate();
1724 case LFUN_MOUSE_RELEASE:
1725 switch (cmd.button()) {
1726 case mouse_button::button1:
1727 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1728 // If there is a new selection, update persistent selection;
1729 // otherwise, single click does not clear persistent selection
1731 if (cur.selection()) {
1732 // Finish selection. If double click,
1733 // cur is moved to the end of word by
1734 // selectWord but bvcur is current
1736 cur.bv().cursor().setSelection();
1737 // We might have removed an empty but drawn selection
1738 // (probably a margin)
1739 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1741 cur.noScreenUpdate();
1742 // FIXME: We could try to handle drag and drop of selection here.
1745 case mouse_button::button2:
1746 // Middle mouse pasting is handled at mouse press time,
1747 // see LFUN_MOUSE_PRESS.
1748 cur.noScreenUpdate();
1751 case mouse_button::button3:
1752 // Cursor was set at LFUN_MOUSE_PRESS time.
1753 // FIXME: If there is a selection we could try to handle a special
1754 // drag & drop context menu.
1755 cur.noScreenUpdate();
1758 case mouse_button::none:
1759 case mouse_button::button4:
1760 case mouse_button::button5:
1762 } // switch (cmd.button())
1766 case LFUN_SELF_INSERT: {
1767 if (cmd.argument().empty())
1770 // Automatically delete the currently selected
1771 // text and replace it with what is being
1772 // typed in now. Depends on lyxrc settings
1773 // "auto_region_delete", which defaults to
1776 if (lyxrc.auto_region_delete && cur.selection())
1777 cutSelection(cur, false, false);
1779 cur.clearSelection();
1781 docstring::const_iterator cit = cmd.argument().begin();
1782 docstring::const_iterator const end = cmd.argument().end();
1783 for (; cit != end; ++cit)
1784 bv->translateAndInsert(*cit, this, cur);
1787 moveCursor(cur, false);
1788 cur.markNewWordPosition();
1789 bv->bookmarkEditPosition();
1793 case LFUN_HREF_INSERT: {
1794 docstring content = cmd.argument();
1795 if (content.empty() && cur.selection())
1796 content = cur.selectionAsString(false);
1798 InsetCommandParams p(HYPERLINK_CODE);
1799 if (!content.empty()){
1800 // if it looks like a link, we'll put it as target,
1801 // otherwise as name (bug #8792).
1804 // regex_match(to_utf8(content), matches, link_re)
1805 // because smatch stores pointers to the substrings rather
1806 // than making copies of them. And those pointers become
1807 // invalid after regex_match returns, since it is then
1808 // being given a temporary object. (Thanks to Georg for
1809 // figuring that out.)
1810 regex const link_re("^([a-z]+):.*");
1812 string const c = to_utf8(lowercase(content));
1814 if (c.substr(0,7) == "mailto:") {
1815 p["target"] = content;
1816 p["type"] = from_ascii("mailto:");
1817 } else if (regex_match(c, matches, link_re)) {
1818 p["target"] = content;
1819 string protocol = matches.str(1);
1820 if (protocol == "file")
1821 p["type"] = from_ascii("file:");
1823 p["name"] = content;
1825 string const data = InsetCommand::params2string(p);
1827 // we need to have a target. if we already have one, then
1828 // that gets used at the default for the name, too, which
1829 // is probably what is wanted.
1830 if (p["target"].empty()) {
1831 bv->showDialog("href", data);
1833 FuncRequest fr(LFUN_INSET_INSERT, data);
1839 case LFUN_LABEL_INSERT: {
1840 InsetCommandParams p(LABEL_CODE);
1841 // Try to generate a valid label
1842 p["name"] = (cmd.argument().empty()) ?
1843 cur.getPossibleLabel() :
1845 string const data = InsetCommand::params2string(p);
1847 if (cmd.argument().empty()) {
1848 bv->showDialog("label", data);
1850 FuncRequest fr(LFUN_INSET_INSERT, data);
1856 case LFUN_INFO_INSERT: {
1858 if (cmd.argument().empty() && cur.selection()) {
1859 // if command argument is empty use current selection as parameter.
1860 docstring ds = cur.selectionAsString(false);
1861 cutSelection(cur, true, false);
1862 FuncRequest cmd0(cmd, ds);
1863 inset = createInset(cur.buffer(), cmd0);
1865 inset = createInset(cur.buffer(), cmd);
1870 insertInset(cur, inset);
1874 case LFUN_CAPTION_INSERT:
1875 case LFUN_FOOTNOTE_INSERT:
1876 case LFUN_NOTE_INSERT:
1877 case LFUN_BOX_INSERT:
1878 case LFUN_BRANCH_INSERT:
1879 case LFUN_PHANTOM_INSERT:
1880 case LFUN_ERT_INSERT:
1881 case LFUN_LISTING_INSERT:
1882 case LFUN_MARGINALNOTE_INSERT:
1883 case LFUN_ARGUMENT_INSERT:
1884 case LFUN_INDEX_INSERT:
1885 case LFUN_PREVIEW_INSERT:
1886 case LFUN_SCRIPT_INSERT:
1887 case LFUN_IPA_INSERT:
1888 // Open the inset, and move the current selection
1890 doInsertInset(cur, this, cmd, true, true);
1892 // Some insets are numbered, others are shown in the outline pane so
1893 // let's update the labels and the toc backend.
1894 cur.forceBufferUpdate();
1897 case LFUN_FLEX_INSERT: {
1898 // Open the inset, and move the current selection
1900 bool const sel = cur.selection();
1901 doInsertInset(cur, this, cmd, true, true);
1902 // Insert auto-insert arguments
1903 bool autoargs = false;
1904 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
1905 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1906 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1907 for (; lait != laend; ++lait) {
1908 Layout::latexarg arg = (*lait).second;
1909 if (arg.autoinsert) {
1910 // The cursor might have been invalidated by the replaceSelection.
1911 cur.buffer()->changed(true);
1912 FuncRequest cmd(LFUN_ARGUMENT_INSERT, (*lait).first);
1919 cur.leaveInset(cur.inset());
1922 // Some insets are numbered, others are shown in the outline pane so
1923 // let's update the labels and the toc backend.
1924 cur.forceBufferUpdate();
1928 case LFUN_TABULAR_INSERT:
1929 // if there were no arguments, just open the dialog
1930 if (doInsertInset(cur, this, cmd, false, true))
1933 bv->showDialog("tabularcreate");
1937 case LFUN_FLOAT_INSERT:
1938 case LFUN_FLOAT_WIDE_INSERT:
1939 case LFUN_WRAP_INSERT: {
1940 // will some content be moved into the inset?
1941 bool const content = cur.selection();
1942 // does the content consist of multiple paragraphs?
1943 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
1945 doInsertInset(cur, this, cmd, true, true);
1948 // If some single-par content is moved into the inset,
1949 // doInsertInset puts the cursor outside the inset.
1950 // To insert the caption we put it back into the inset.
1951 // FIXME cleanup doInsertInset to avoid such dances!
1952 if (content && singlepar)
1955 ParagraphList & pars = cur.text()->paragraphs();
1957 DocumentClass const & tclass = bv->buffer().params().documentClass();
1959 // add a separate paragraph for the caption inset
1960 pars.push_back(Paragraph());
1961 pars.back().setInsetOwner(&cur.text()->inset());
1962 pars.back().setPlainOrDefaultLayout(tclass);
1963 int cap_pit = pars.size() - 1;
1965 // if an empty inset was created, we create an additional empty
1966 // paragraph at the bottom so that the user can choose where to put
1967 // the graphics (or table).
1969 pars.push_back(Paragraph());
1970 pars.back().setInsetOwner(&cur.text()->inset());
1971 pars.back().setPlainOrDefaultLayout(tclass);
1974 // reposition the cursor to the caption
1975 cur.pit() = cap_pit;
1977 // FIXME: This Text/Cursor dispatch handling is a mess!
1978 // We cannot use Cursor::dispatch here it needs access to up to
1980 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1981 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1982 cur.forceBufferUpdate();
1983 cur.screenUpdateFlags(Update::Force);
1984 // FIXME: When leaving the Float (or Wrap) inset we should
1985 // delete any empty paragraph left above or below the
1990 case LFUN_NOMENCL_INSERT: {
1991 InsetCommandParams p(NOMENCL_CODE);
1992 if (cmd.argument().empty())
1993 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1995 p["symbol"] = cmd.argument();
1996 string const data = InsetCommand::params2string(p);
1997 bv->showDialog("nomenclature", data);
2001 case LFUN_INDEX_PRINT: {
2002 InsetCommandParams p(INDEX_PRINT_CODE);
2003 if (cmd.argument().empty())
2004 p["type"] = from_ascii("idx");
2006 p["type"] = cmd.argument();
2007 string const data = InsetCommand::params2string(p);
2008 FuncRequest fr(LFUN_INSET_INSERT, data);
2013 case LFUN_NOMENCL_PRINT:
2014 case LFUN_NEWPAGE_INSERT:
2016 doInsertInset(cur, this, cmd, false, false);
2020 case LFUN_SEPARATOR_INSERT: {
2021 doInsertInset(cur, this, cmd, false, false);
2023 // remove a following space
2024 Paragraph & par = cur.paragraph();
2025 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2026 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2030 case LFUN_DEPTH_DECREMENT:
2031 changeDepth(cur, DEC_DEPTH);
2034 case LFUN_DEPTH_INCREMENT:
2035 changeDepth(cur, INC_DEPTH);
2038 case LFUN_REGEXP_MODE:
2039 regexpDispatch(cur, cmd);
2042 case LFUN_MATH_MODE: {
2043 if (cmd.argument() == "on" || cmd.argument() == "") {
2044 // don't pass "on" as argument
2045 // (it would appear literally in the first cell)
2046 docstring sel = cur.selectionAsString(false);
2047 MathMacroTemplate * macro = new MathMacroTemplate(cur.buffer());
2048 // create a macro template if we see "\\newcommand" somewhere, and
2049 // an ordinary formula otherwise
2051 && (sel.find(from_ascii("\\newcommand")) != string::npos
2052 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2053 || sel.find(from_ascii("\\def")) != string::npos)
2054 && macro->fromString(sel)) {
2056 replaceSelection(cur);
2059 // no meaningful macro template was found
2061 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2064 // The argument is meaningful
2065 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2066 // has a different meaning in math mode
2067 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2071 case LFUN_MATH_MACRO:
2072 if (cmd.argument().empty())
2073 cur.errorMessage(from_utf8(N_("Missing argument")));
2076 string s = to_utf8(cmd.argument());
2077 string const s1 = token(s, ' ', 1);
2078 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2079 string const s2 = token(s, ' ', 2);
2080 MacroType type = MacroTypeNewcommand;
2082 type = MacroTypeDef;
2083 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
2084 from_utf8(token(s, ' ', 0)), nargs, false, type);
2085 inset->setBuffer(bv->buffer());
2086 insertInset(cur, inset);
2088 // enter macro inset and select the name
2090 cur.top().pos() = cur.top().lastpos();
2092 cur.selection(true);
2093 cur.top().pos() = 0;
2097 case LFUN_MATH_DISPLAY:
2098 case LFUN_MATH_SUBSCRIPT:
2099 case LFUN_MATH_SUPERSCRIPT:
2100 case LFUN_MATH_INSERT:
2101 case LFUN_MATH_AMS_MATRIX:
2102 case LFUN_MATH_MATRIX:
2103 case LFUN_MATH_DELIM:
2104 case LFUN_MATH_BIGDELIM:
2105 mathDispatch(cur, cmd);
2108 case LFUN_FONT_EMPH: {
2109 Font font(ignore_font, ignore_language);
2110 font.fontInfo().setEmph(FONT_TOGGLE);
2111 toggleAndShow(cur, this, font);
2115 case LFUN_FONT_ITAL: {
2116 Font font(ignore_font, ignore_language);
2117 font.fontInfo().setShape(ITALIC_SHAPE);
2118 toggleAndShow(cur, this, font);
2122 case LFUN_FONT_BOLD:
2123 case LFUN_FONT_BOLDSYMBOL: {
2124 Font font(ignore_font, ignore_language);
2125 font.fontInfo().setSeries(BOLD_SERIES);
2126 toggleAndShow(cur, this, font);
2130 case LFUN_FONT_NOUN: {
2131 Font font(ignore_font, ignore_language);
2132 font.fontInfo().setNoun(FONT_TOGGLE);
2133 toggleAndShow(cur, this, font);
2137 case LFUN_FONT_TYPEWRITER: {
2138 Font font(ignore_font, ignore_language);
2139 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2140 toggleAndShow(cur, this, font);
2144 case LFUN_FONT_SANS: {
2145 Font font(ignore_font, ignore_language);
2146 font.fontInfo().setFamily(SANS_FAMILY);
2147 toggleAndShow(cur, this, font);
2151 case LFUN_FONT_ROMAN: {
2152 Font font(ignore_font, ignore_language);
2153 font.fontInfo().setFamily(ROMAN_FAMILY);
2154 toggleAndShow(cur, this, font);
2158 case LFUN_FONT_DEFAULT: {
2159 Font font(inherit_font, ignore_language);
2160 toggleAndShow(cur, this, font);
2164 case LFUN_FONT_STRIKEOUT: {
2165 Font font(ignore_font, ignore_language);
2166 font.fontInfo().setStrikeout(FONT_TOGGLE);
2167 toggleAndShow(cur, this, font);
2171 case LFUN_FONT_CROSSOUT: {
2172 Font font(ignore_font, ignore_language);
2173 font.fontInfo().setXout(FONT_TOGGLE);
2174 toggleAndShow(cur, this, font);
2178 case LFUN_FONT_UNDERUNDERLINE: {
2179 Font font(ignore_font, ignore_language);
2180 font.fontInfo().setUuline(FONT_TOGGLE);
2181 toggleAndShow(cur, this, font);
2185 case LFUN_FONT_UNDERWAVE: {
2186 Font font(ignore_font, ignore_language);
2187 font.fontInfo().setUwave(FONT_TOGGLE);
2188 toggleAndShow(cur, this, font);
2192 case LFUN_FONT_UNDERLINE: {
2193 Font font(ignore_font, ignore_language);
2194 font.fontInfo().setUnderbar(FONT_TOGGLE);
2195 toggleAndShow(cur, this, font);
2199 case LFUN_FONT_SIZE: {
2200 Font font(ignore_font, ignore_language);
2201 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2202 toggleAndShow(cur, this, font);
2206 case LFUN_LANGUAGE: {
2207 string const lang_arg = cmd.getArg(0);
2208 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2209 Language const * lang =
2210 reset ? reset_language
2211 : languages.getLanguage(lang_arg);
2212 // we allow reset_language, which is 0, but only if it
2213 // was requested via empty or "reset" arg.
2214 if (!lang && !reset)
2216 bool const toggle = (cmd.getArg(1) != "set");
2217 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2218 Font font(ignore_font, lang);
2219 toggleAndShow(cur, this, font, toggle);
2223 case LFUN_TEXTSTYLE_APPLY:
2224 toggleAndShow(cur, this, freefont, toggleall);
2225 cur.message(_("Character set"));
2228 // Set the freefont using the contents of \param data dispatched from
2229 // the frontends and apply it at the current cursor location.
2230 case LFUN_TEXTSTYLE_UPDATE: {
2233 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2236 toggleAndShow(cur, this, freefont, toggleall);
2237 cur.message(_("Character set"));
2239 lyxerr << "Argument not ok";
2244 case LFUN_FINISHED_LEFT:
2245 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2246 // We're leaving an inset, going left. If the inset is LTR, we're
2247 // leaving from the front, so we should not move (remain at --- but
2248 // not in --- the inset). If the inset is RTL, move left, without
2249 // entering the inset itself; i.e., move to after the inset.
2250 if (cur.paragraph().getFontSettings(
2251 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2252 cursorVisLeft(cur, true);
2255 case LFUN_FINISHED_RIGHT:
2256 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2257 // We're leaving an inset, going right. If the inset is RTL, we're
2258 // leaving from the front, so we should not move (remain at --- but
2259 // not in --- the inset). If the inset is LTR, move right, without
2260 // entering the inset itself; i.e., move to after the inset.
2261 if (!cur.paragraph().getFontSettings(
2262 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2263 cursorVisRight(cur, true);
2266 case LFUN_FINISHED_BACKWARD:
2267 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2268 cur.setCurrentFont();
2271 case LFUN_FINISHED_FORWARD:
2272 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2274 cur.setCurrentFont();
2277 case LFUN_LAYOUT_PARAGRAPH: {
2279 params2string(cur.paragraph(), data);
2280 data = "show\n" + data;
2281 bv->showDialog("paragraph", data);
2285 case LFUN_PARAGRAPH_UPDATE: {
2287 params2string(cur.paragraph(), data);
2289 // Will the paragraph accept changes from the dialog?
2291 cur.inset().allowParagraphCustomization(cur.idx());
2293 data = "update " + convert<string>(accept) + '\n' + data;
2294 bv->updateDialog("paragraph", data);
2298 case LFUN_ACCENT_UMLAUT:
2299 case LFUN_ACCENT_CIRCUMFLEX:
2300 case LFUN_ACCENT_GRAVE:
2301 case LFUN_ACCENT_ACUTE:
2302 case LFUN_ACCENT_TILDE:
2303 case LFUN_ACCENT_PERISPOMENI:
2304 case LFUN_ACCENT_CEDILLA:
2305 case LFUN_ACCENT_MACRON:
2306 case LFUN_ACCENT_DOT:
2307 case LFUN_ACCENT_UNDERDOT:
2308 case LFUN_ACCENT_UNDERBAR:
2309 case LFUN_ACCENT_CARON:
2310 case LFUN_ACCENT_BREVE:
2311 case LFUN_ACCENT_TIE:
2312 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2313 case LFUN_ACCENT_CIRCLE:
2314 case LFUN_ACCENT_OGONEK:
2315 theApp()->handleKeyFunc(cmd.action());
2316 if (!cmd.argument().empty())
2317 // FIXME: Are all these characters encoded in one byte in utf8?
2318 bv->translateAndInsert(cmd.argument()[0], this, cur);
2319 cur.screenUpdateFlags(Update::FitCursor);
2322 case LFUN_FLOAT_LIST_INSERT: {
2323 DocumentClass const & tclass = bv->buffer().params().documentClass();
2324 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2326 if (cur.selection())
2327 cutSelection(cur, true, false);
2328 breakParagraph(cur);
2330 if (cur.lastpos() != 0) {
2331 cursorBackward(cur);
2332 breakParagraph(cur);
2335 docstring const laystr = cur.inset().usePlainLayout() ?
2336 tclass.plainLayoutName() :
2337 tclass.defaultLayoutName();
2338 setLayout(cur, laystr);
2339 ParagraphParameters p;
2340 // FIXME If this call were replaced with one to clearParagraphParams(),
2341 // then we could get rid of this method altogether.
2342 setParagraphs(cur, p);
2343 // FIXME This should be simplified when InsetFloatList takes a
2344 // Buffer in its constructor.
2345 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2346 ifl->setBuffer(bv->buffer());
2347 insertInset(cur, ifl);
2350 lyxerr << "Non-existent float type: "
2351 << to_utf8(cmd.argument()) << endl;
2356 case LFUN_CHANGE_ACCEPT: {
2357 acceptOrRejectChanges(cur, ACCEPT);
2361 case LFUN_CHANGE_REJECT: {
2362 acceptOrRejectChanges(cur, REJECT);
2366 case LFUN_THESAURUS_ENTRY: {
2367 Language const * language = cur.getFont().language();
2368 docstring arg = cmd.argument();
2370 arg = cur.selectionAsString(false);
2372 if (arg.size() > 100 || arg.empty()) {
2373 // Get word or selection
2374 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2375 arg = cur.selectionAsString(false);
2376 arg += " lang=" + from_ascii(language->lang());
2379 string lang = cmd.getArg(1);
2380 // This duplicates the code in GuiThesaurus::initialiseParams
2381 if (prefixIs(lang, "lang=")) {
2382 language = languages.getLanguage(lang.substr(5));
2384 language = cur.getFont().language();
2387 string lang = language->code();
2388 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2389 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2390 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2391 _("The path to the thesaurus directory has not been specified.\n"
2392 "The thesaurus is not functional.\n"
2393 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2396 bv->showDialog("thesaurus", to_utf8(arg));
2400 case LFUN_SPELLING_ADD: {
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()->insert(wl);
2417 case LFUN_SPELLING_IGNORE: {
2418 Language const * language = getLanguage(cur, cmd.getArg(1));
2419 docstring word = from_utf8(cmd.getArg(0));
2421 word = cur.selectionAsString(false);
2423 if (word.size() > 100 || word.empty()) {
2424 // Get word or selection
2425 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2426 word = cur.selectionAsString(false);
2429 WordLangTuple wl(word, language);
2430 theSpellChecker()->accept(wl);
2434 case LFUN_SPELLING_REMOVE: {
2435 Language const * language = getLanguage(cur, cmd.getArg(1));
2436 docstring word = from_utf8(cmd.getArg(0));
2438 word = cur.selectionAsString(false);
2440 if (word.size() > 100 || word.empty()) {
2441 // Get word or selection
2442 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2443 word = cur.selectionAsString(false);
2446 WordLangTuple wl(word, language);
2447 theSpellChecker()->remove(wl);
2451 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2452 // Given data, an encoding of the ParagraphParameters
2453 // generated in the Paragraph dialog, this function sets
2454 // the current paragraph, or currently selected paragraphs,
2456 // NOTE: This function overrides all existing settings.
2457 setParagraphs(cur, cmd.argument());
2458 cur.message(_("Paragraph layout set"));
2462 case LFUN_PARAGRAPH_PARAMS: {
2463 // Given data, an encoding of the ParagraphParameters as we'd
2464 // find them in a LyX file, this function modifies the current paragraph,
2465 // or currently selected paragraphs.
2466 // NOTE: This function only modifies, and does not override, existing
2468 setParagraphs(cur, cmd.argument(), true);
2469 cur.message(_("Paragraph layout set"));
2474 if (cur.selection()) {
2475 cur.selection(false);
2478 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2479 // correct, but I'm not 100% sure -- dov, 071019
2480 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2484 case LFUN_OUTLINE_UP:
2485 outline(OutlineUp, cur);
2486 setCursor(cur, cur.pit(), 0);
2487 cur.forceBufferUpdate();
2491 case LFUN_OUTLINE_DOWN:
2492 outline(OutlineDown, cur);
2493 setCursor(cur, cur.pit(), 0);
2494 cur.forceBufferUpdate();
2498 case LFUN_OUTLINE_IN:
2499 outline(OutlineIn, cur);
2500 cur.forceBufferUpdate();
2504 case LFUN_OUTLINE_OUT:
2505 outline(OutlineOut, cur);
2506 cur.forceBufferUpdate();
2510 case LFUN_SERVER_GET_STATISTICS:
2512 DocIterator from, to;
2513 if (cur.selection()) {
2514 from = cur.selectionBegin();
2515 to = cur.selectionEnd();
2517 from = doc_iterator_begin(cur.buffer());
2518 to = doc_iterator_end(cur.buffer());
2521 cur.buffer()->updateStatistics(from, to);
2522 string const arg0 = cmd.getArg(0);
2523 if (arg0 == "words") {
2524 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2525 } else if (arg0 == "chars") {
2526 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2527 } else if (arg0 == "chars-space") {
2528 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2530 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2531 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2532 + convert<docstring>(cur.buffer()->charCount(true)));
2538 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2543 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2545 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2546 // Check for misspelled text
2547 // The redraw is useful because of the painting of
2548 // misspelled markers depends on the cursor position.
2549 // Trigger a redraw for cursor moves inside misspelled text.
2550 if (!cur.inTexted()) {
2551 // move from regular text to math
2552 needsUpdate = last_misspelled;
2553 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2554 // move inside regular text
2555 needsUpdate = last_misspelled
2556 || cur.paragraph().isMisspelled(cur.pos(), true);
2560 // FIXME: The cursor flag is reset two lines below
2561 // so we need to check here if some of the LFUN did touch that.
2562 // for now only Text::erase() and Text::backspace() do that.
2563 // The plan is to verify all the LFUNs and then to remove this
2564 // singleParUpdate boolean altogether.
2565 if (cur.result().screenUpdate() & Update::Force) {
2566 singleParUpdate = false;
2570 // FIXME: the following code should go in favor of fine grained
2571 // update flag treatment.
2572 if (singleParUpdate) {
2573 // Inserting characters does not change par height in general. So, try
2574 // to update _only_ this paragraph. BufferView will detect if a full
2575 // metrics update is needed anyway.
2576 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2580 && &oldTopSlice.inset() == &cur.inset()
2581 && oldTopSlice.idx() == cur.idx()
2582 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2583 && !cur.selection())
2584 // FIXME: it would be better if we could just do this
2586 //if (cur.result().update() != Update::FitCursor)
2587 // cur.noScreenUpdate();
2589 // But some LFUNs do not set Update::FitCursor when needed, so we
2590 // do it for all. This is not very harmfull as FitCursor will provoke
2591 // a full redraw only if needed but still, a proper review of all LFUN
2592 // should be done and this needsUpdate boolean can then be removed.
2593 cur.screenUpdateFlags(Update::FitCursor);
2595 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2599 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2600 FuncStatus & flag) const
2602 LBUFERR(this == cur.text());
2604 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2606 bool allow_in_passthru = false;
2607 InsetCode code = NO_CODE;
2609 switch (cmd.action()) {
2611 case LFUN_DEPTH_DECREMENT:
2612 enable = changeDepthAllowed(cur, DEC_DEPTH);
2615 case LFUN_DEPTH_INCREMENT:
2616 enable = changeDepthAllowed(cur, INC_DEPTH);
2620 // FIXME We really should not allow this to be put, e.g.,
2621 // in a footnote, or in ERT. But it would make sense in a
2622 // branch, so I'm not sure what to do.
2623 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2626 case LFUN_DIALOG_SHOW_NEW_INSET:
2627 if (cmd.argument() == "bibitem")
2628 code = BIBITEM_CODE;
2629 else if (cmd.argument() == "bibtex") {
2631 // not allowed in description items
2632 enable = !inDescriptionItem(cur);
2634 else if (cmd.argument() == "box")
2636 else if (cmd.argument() == "branch")
2638 else if (cmd.argument() == "citation")
2640 else if (cmd.argument() == "ert")
2642 else if (cmd.argument() == "external")
2643 code = EXTERNAL_CODE;
2644 else if (cmd.argument() == "float")
2646 else if (cmd.argument() == "graphics")
2647 code = GRAPHICS_CODE;
2648 else if (cmd.argument() == "href")
2649 code = HYPERLINK_CODE;
2650 else if (cmd.argument() == "include")
2651 code = INCLUDE_CODE;
2652 else if (cmd.argument() == "index")
2654 else if (cmd.argument() == "index_print")
2655 code = INDEX_PRINT_CODE;
2656 else if (cmd.argument() == "listings")
2657 code = LISTINGS_CODE;
2658 else if (cmd.argument() == "mathspace")
2659 code = MATH_HULL_CODE;
2660 else if (cmd.argument() == "nomenclature")
2661 code = NOMENCL_CODE;
2662 else if (cmd.argument() == "nomencl_print")
2663 code = NOMENCL_PRINT_CODE;
2664 else if (cmd.argument() == "label")
2666 else if (cmd.argument() == "line")
2668 else if (cmd.argument() == "note")
2670 else if (cmd.argument() == "phantom")
2671 code = PHANTOM_CODE;
2672 else if (cmd.argument() == "ref")
2674 else if (cmd.argument() == "space")
2676 else if (cmd.argument() == "toc")
2678 else if (cmd.argument() == "vspace")
2680 else if (cmd.argument() == "wrap")
2684 case LFUN_ERT_INSERT:
2687 case LFUN_LISTING_INSERT:
2688 code = LISTINGS_CODE;
2689 // not allowed in description items
2690 enable = !inDescriptionItem(cur);
2692 case LFUN_FOOTNOTE_INSERT:
2695 case LFUN_TABULAR_INSERT:
2696 code = TABULAR_CODE;
2698 case LFUN_MARGINALNOTE_INSERT:
2701 case LFUN_FLOAT_INSERT:
2702 case LFUN_FLOAT_WIDE_INSERT:
2703 // FIXME: If there is a selection, we should check whether there
2704 // are floats in the selection, but this has performance issues, see
2705 // LFUN_CHANGE_ACCEPT/REJECT.
2707 if (inDescriptionItem(cur))
2708 // not allowed in description items
2711 InsetCode const inset_code = cur.inset().lyxCode();
2713 // algorithm floats cannot be put in another float
2714 if (to_utf8(cmd.argument()) == "algorithm") {
2715 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2719 // for figures and tables: only allow in another
2720 // float or wrap if it is of the same type and
2721 // not a subfloat already
2722 if(cur.inset().lyxCode() == code) {
2723 InsetFloat const & ins =
2724 static_cast<InsetFloat const &>(cur.inset());
2725 enable = ins.params().type == to_utf8(cmd.argument())
2726 && !ins.params().subfloat;
2727 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2728 InsetWrap const & ins =
2729 static_cast<InsetWrap const &>(cur.inset());
2730 enable = ins.params().type == to_utf8(cmd.argument());
2734 case LFUN_WRAP_INSERT:
2736 // not allowed in description items
2737 enable = !inDescriptionItem(cur);
2739 case LFUN_FLOAT_LIST_INSERT: {
2740 code = FLOAT_LIST_CODE;
2741 // not allowed in description items
2742 enable = !inDescriptionItem(cur);
2744 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2745 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2746 // make sure we know about such floats
2747 if (cit == floats.end() ||
2748 // and that we know how to generate a list of them
2749 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2750 flag.setUnknown(true);
2751 // probably not necessary, but...
2757 case LFUN_CAPTION_INSERT: {
2758 code = CAPTION_CODE;
2759 string arg = cmd.getArg(0);
2760 bool varia = arg != "Unnumbered"
2761 && cur.inset().allowsCaptionVariation(arg);
2762 // not allowed in description items,
2763 // and in specific insets
2764 enable = !inDescriptionItem(cur)
2765 && (varia || arg.empty() || arg == "Standard");
2768 case LFUN_NOTE_INSERT:
2770 // in commands (sections etc.) and description items,
2771 // only Notes are allowed
2772 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2773 (!cur.paragraph().layout().isCommand()
2774 && !inDescriptionItem(cur)));
2776 case LFUN_FLEX_INSERT: {
2778 string s = cmd.getArg(0);
2780 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2781 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2782 il.lyxtype() != InsetLayout::CUSTOM &&
2783 il.lyxtype() != InsetLayout::ELEMENT &&
2784 il.lyxtype ()!= InsetLayout::STANDARD)
2788 case LFUN_BOX_INSERT:
2791 case LFUN_BRANCH_INSERT:
2793 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2794 && cur.buffer()->params().branchlist().empty())
2797 case LFUN_IPA_INSERT:
2800 case LFUN_PHANTOM_INSERT:
2801 code = PHANTOM_CODE;
2803 case LFUN_LABEL_INSERT:
2806 case LFUN_INFO_INSERT:
2809 case LFUN_ARGUMENT_INSERT: {
2811 allow_in_passthru = true;
2812 string const arg = cmd.getArg(0);
2817 Layout const & lay = cur.paragraph().layout();
2818 Layout::LaTeXArgMap args = lay.args();
2819 Layout::LaTeXArgMap::const_iterator const lait =
2821 if (lait != args.end()) {
2823 pit_type pit = cur.pit();
2824 pit_type lastpit = cur.pit();
2825 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2826 // In a sequence of "merged" environment layouts, we only allow
2827 // non-item arguments once.
2828 lastpit = cur.lastpit();
2829 // get the first paragraph in sequence with this layout
2830 depth_type const current_depth = cur.paragraph().params().depth();
2834 Paragraph cpar = pars_[pit - 1];
2835 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
2841 for (; pit <= lastpit; ++pit) {
2842 if (pars_[pit].layout() != lay)
2844 InsetList::const_iterator it = pars_[pit].insetList().begin();
2845 InsetList::const_iterator end = pars_[pit].insetList().end();
2846 for (; it != end; ++it) {
2847 if (it->inset->lyxCode() == ARG_CODE) {
2848 InsetArgument const * ins =
2849 static_cast<InsetArgument const *>(it->inset);
2850 if (ins->name() == arg) {
2851 // we have this already
2862 case LFUN_INDEX_INSERT:
2865 case LFUN_INDEX_PRINT:
2866 code = INDEX_PRINT_CODE;
2867 // not allowed in description items
2868 enable = !inDescriptionItem(cur);
2870 case LFUN_NOMENCL_INSERT:
2871 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2875 code = NOMENCL_CODE;
2877 case LFUN_NOMENCL_PRINT:
2878 code = NOMENCL_PRINT_CODE;
2879 // not allowed in description items
2880 enable = !inDescriptionItem(cur);
2882 case LFUN_HREF_INSERT:
2883 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2887 code = HYPERLINK_CODE;
2889 case LFUN_IPAMACRO_INSERT: {
2890 string const arg = cmd.getArg(0);
2892 code = IPADECO_CODE;
2894 code = IPACHAR_CODE;
2897 case LFUN_QUOTE_INSERT:
2898 // always allow this, since we will inset a raw quote
2899 // if an inset is not allowed.
2900 allow_in_passthru = true;
2902 case LFUN_SPECIALCHAR_INSERT:
2903 code = SPECIALCHAR_CODE;
2905 case LFUN_SPACE_INSERT:
2906 // slight hack: we know this is allowed in math mode
2910 case LFUN_PREVIEW_INSERT:
2911 code = PREVIEW_CODE;
2913 case LFUN_SCRIPT_INSERT:
2917 case LFUN_MATH_INSERT:
2918 case LFUN_MATH_AMS_MATRIX:
2919 case LFUN_MATH_MATRIX:
2920 case LFUN_MATH_DELIM:
2921 case LFUN_MATH_BIGDELIM:
2922 case LFUN_MATH_DISPLAY:
2923 case LFUN_MATH_MODE:
2924 case LFUN_MATH_MACRO:
2925 case LFUN_MATH_SUBSCRIPT:
2926 case LFUN_MATH_SUPERSCRIPT:
2927 code = MATH_HULL_CODE;
2930 case LFUN_REGEXP_MODE:
2931 code = MATH_HULL_CODE;
2932 enable = cur.buffer()->isInternal() && !cur.inRegexped();
2935 case LFUN_INSET_MODIFY:
2936 // We need to disable this, because we may get called for a
2938 // InsetTabular::getStatus() -> InsetText::getStatus()
2939 // and we don't handle LFUN_INSET_MODIFY.
2943 case LFUN_FONT_EMPH:
2944 flag.setOnOff(fontinfo.emph() == FONT_ON);
2945 enable = !cur.paragraph().isPassThru();
2948 case LFUN_FONT_ITAL:
2949 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2950 enable = !cur.paragraph().isPassThru();
2953 case LFUN_FONT_NOUN:
2954 flag.setOnOff(fontinfo.noun() == FONT_ON);
2955 enable = !cur.paragraph().isPassThru();
2958 case LFUN_FONT_BOLD:
2959 case LFUN_FONT_BOLDSYMBOL:
2960 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2961 enable = !cur.paragraph().isPassThru();
2964 case LFUN_FONT_SANS:
2965 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2966 enable = !cur.paragraph().isPassThru();
2969 case LFUN_FONT_ROMAN:
2970 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2971 enable = !cur.paragraph().isPassThru();
2974 case LFUN_FONT_TYPEWRITER:
2975 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2976 enable = !cur.paragraph().isPassThru();
2981 enable = cur.selection();
2985 if (cmd.argument().empty()) {
2986 if (theClipboard().isInternal())
2987 enable = cap::numberOfSelections() > 0;
2989 enable = !theClipboard().empty();
2993 // we have an argument
2994 string const arg = to_utf8(cmd.argument());
2995 if (isStrUnsignedInt(arg)) {
2996 // it's a number and therefore means the internal stack
2997 unsigned int n = convert<unsigned int>(arg);
2998 enable = cap::numberOfSelections() > n;
3002 // explicit text type?
3003 if (arg == "html") {
3004 // Do not enable for PlainTextType, since some tidying in the
3005 // frontend is needed for HTML, which is too unsafe for plain text.
3006 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3008 } else if (arg == "latex") {
3009 // LaTeX is usually not available on the clipboard with
3010 // the correct MIME type, but in plain text.
3011 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3012 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3016 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3018 type = Clipboard::PdfGraphicsType;
3019 else if (arg == "png")
3020 type = Clipboard::PngGraphicsType;
3021 else if (arg == "jpeg")
3022 type = Clipboard::JpegGraphicsType;
3023 else if (arg == "linkback")
3024 type = Clipboard::LinkBackGraphicsType;
3025 else if (arg == "emf")
3026 type = Clipboard::EmfGraphicsType;
3027 else if (arg == "wmf")
3028 type = Clipboard::WmfGraphicsType;
3031 LYXERR0("Unrecognized graphics type: " << arg);
3032 // we don't want to assert if the user just mistyped the LFUN
3033 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3037 enable = theClipboard().hasGraphicsContents(type);
3041 case LFUN_CLIPBOARD_PASTE:
3042 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3043 enable = !theClipboard().empty();
3046 case LFUN_PRIMARY_SELECTION_PASTE:
3047 enable = cur.selection() || !theSelection().empty();
3050 case LFUN_SELECTION_PASTE:
3051 enable = cap::selection();
3054 case LFUN_PARAGRAPH_MOVE_UP:
3055 enable = cur.pit() > 0 && !cur.selection();
3058 case LFUN_PARAGRAPH_MOVE_DOWN:
3059 enable = cur.pit() < cur.lastpit() && !cur.selection();
3062 case LFUN_CHANGE_ACCEPT:
3063 case LFUN_CHANGE_REJECT:
3064 // In principle, these LFUNs should only be enabled if there
3065 // is a change at the current position/in the current selection.
3066 // However, without proper optimizations, this will inevitably
3067 // result in unacceptable performance - just imagine a user who
3068 // wants to select the complete content of a long document.
3069 if (!cur.selection())
3070 enable = cur.paragraph().isChanged(cur.pos());
3072 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3077 case LFUN_OUTLINE_UP:
3078 case LFUN_OUTLINE_DOWN:
3079 case LFUN_OUTLINE_IN:
3080 case LFUN_OUTLINE_OUT:
3081 // FIXME: LyX is not ready for outlining within inset.
3082 enable = isMainText()
3083 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3086 case LFUN_NEWLINE_INSERT:
3087 // LaTeX restrictions (labels or empty par)
3088 enable = !cur.paragraph().isPassThru()
3089 && cur.pos() > cur.paragraph().beginOfBody();
3092 case LFUN_SEPARATOR_INSERT:
3093 // Always enabled for now
3097 case LFUN_TAB_INSERT:
3098 case LFUN_TAB_DELETE:
3099 enable = cur.paragraph().isPassThru();
3102 case LFUN_SET_GRAPHICS_GROUP: {
3103 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3107 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3111 case LFUN_NEWPAGE_INSERT:
3112 // not allowed in description items
3113 code = NEWPAGE_CODE;
3114 enable = !inDescriptionItem(cur);
3117 case LFUN_DATE_INSERT: {
3118 string const format = cmd.argument().empty()
3119 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3120 enable = support::os::is_valid_strftime(format);
3125 enable = !cur.paragraph().isPassThru();
3126 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3129 case LFUN_PARAGRAPH_BREAK:
3130 enable = inset().allowMultiPar();
3133 case LFUN_SPELLING_ADD:
3134 case LFUN_SPELLING_IGNORE:
3135 case LFUN_SPELLING_REMOVE:
3136 enable = theSpellChecker() != NULL;
3137 if (enable && !cmd.getArg(1).empty()) {
3138 // validate explicitly given language
3139 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3140 enable &= lang != NULL;
3145 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3146 docstring layout = cmd.argument();
3148 layout = tclass.defaultLayoutName();
3149 enable = !owner_->forcePlainLayout() && tclass.hasLayout(layout);
3151 flag.setOnOff(layout == cur.paragraph().layout().name());
3155 case LFUN_ENVIRONMENT_SPLIT: {
3156 if (cmd.argument() == "outer") {
3157 // check if we have an environment in our nesting hierarchy
3159 depth_type const current_depth = cur.paragraph().params().depth();
3160 pit_type pit = cur.pit();
3161 Paragraph cpar = pars_[pit];
3163 if (pit == 0 || cpar.params().depth() == 0)
3167 if (cpar.params().depth() < current_depth)
3168 res = cpar.layout().isEnvironment();
3173 else if (cur.paragraph().layout().isEnvironment()) {
3181 case LFUN_LAYOUT_PARAGRAPH:
3182 case LFUN_PARAGRAPH_PARAMS:
3183 case LFUN_PARAGRAPH_PARAMS_APPLY:
3184 case LFUN_PARAGRAPH_UPDATE:
3185 enable = owner_->allowParagraphCustomization();
3188 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3189 // Because they insert COMBINING DIACRITICAL Unicode characters,
3190 // that cannot be handled by LaTeX but must be converted according
3191 // to the definition in lib/unicodesymbols?
3192 case LFUN_ACCENT_ACUTE:
3193 case LFUN_ACCENT_BREVE:
3194 case LFUN_ACCENT_CARON:
3195 case LFUN_ACCENT_CEDILLA:
3196 case LFUN_ACCENT_CIRCLE:
3197 case LFUN_ACCENT_CIRCUMFLEX:
3198 case LFUN_ACCENT_DOT:
3199 case LFUN_ACCENT_GRAVE:
3200 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3201 case LFUN_ACCENT_MACRON:
3202 case LFUN_ACCENT_OGONEK:
3203 case LFUN_ACCENT_TIE:
3204 case LFUN_ACCENT_TILDE:
3205 case LFUN_ACCENT_PERISPOMENI:
3206 case LFUN_ACCENT_UMLAUT:
3207 case LFUN_ACCENT_UNDERBAR:
3208 case LFUN_ACCENT_UNDERDOT:
3209 case LFUN_FONT_DEFAULT:
3210 case LFUN_FONT_FRAK:
3211 case LFUN_FONT_SIZE:
3212 case LFUN_FONT_STATE:
3213 case LFUN_FONT_UNDERLINE:
3214 case LFUN_FONT_STRIKEOUT:
3215 case LFUN_FONT_CROSSOUT:
3216 case LFUN_FONT_UNDERUNDERLINE:
3217 case LFUN_FONT_UNDERWAVE:
3218 case LFUN_TEXTSTYLE_APPLY:
3219 case LFUN_TEXTSTYLE_UPDATE:
3220 enable = !cur.paragraph().isPassThru();
3223 case LFUN_WORD_DELETE_FORWARD:
3224 case LFUN_WORD_DELETE_BACKWARD:
3225 case LFUN_LINE_DELETE_FORWARD:
3226 case LFUN_WORD_FORWARD:
3227 case LFUN_WORD_BACKWARD:
3228 case LFUN_WORD_RIGHT:
3229 case LFUN_WORD_LEFT:
3230 case LFUN_CHAR_FORWARD:
3231 case LFUN_CHAR_FORWARD_SELECT:
3232 case LFUN_CHAR_BACKWARD:
3233 case LFUN_CHAR_BACKWARD_SELECT:
3234 case LFUN_CHAR_LEFT:
3235 case LFUN_CHAR_LEFT_SELECT:
3236 case LFUN_CHAR_RIGHT:
3237 case LFUN_CHAR_RIGHT_SELECT:
3239 case LFUN_UP_SELECT:
3241 case LFUN_DOWN_SELECT:
3242 case LFUN_PARAGRAPH_UP_SELECT:
3243 case LFUN_PARAGRAPH_DOWN_SELECT:
3244 case LFUN_LINE_BEGIN_SELECT:
3245 case LFUN_LINE_END_SELECT:
3246 case LFUN_WORD_FORWARD_SELECT:
3247 case LFUN_WORD_BACKWARD_SELECT:
3248 case LFUN_WORD_RIGHT_SELECT:
3249 case LFUN_WORD_LEFT_SELECT:
3250 case LFUN_WORD_SELECT:
3251 case LFUN_SECTION_SELECT:
3252 case LFUN_BUFFER_BEGIN:
3253 case LFUN_BUFFER_END:
3254 case LFUN_BUFFER_BEGIN_SELECT:
3255 case LFUN_BUFFER_END_SELECT:
3256 case LFUN_INSET_BEGIN:
3257 case LFUN_INSET_END:
3258 case LFUN_INSET_BEGIN_SELECT:
3259 case LFUN_INSET_END_SELECT:
3260 case LFUN_PARAGRAPH_UP:
3261 case LFUN_PARAGRAPH_DOWN:
3262 case LFUN_LINE_BEGIN:
3264 case LFUN_CHAR_DELETE_FORWARD:
3265 case LFUN_CHAR_DELETE_BACKWARD:
3266 case LFUN_WORD_UPCASE:
3267 case LFUN_WORD_LOWCASE:
3268 case LFUN_WORD_CAPITALIZE:
3269 case LFUN_CHARS_TRANSPOSE:
3270 case LFUN_SERVER_GET_XY:
3271 case LFUN_SERVER_SET_XY:
3272 case LFUN_SERVER_GET_LAYOUT:
3273 case LFUN_SELF_INSERT:
3274 case LFUN_UNICODE_INSERT:
3275 case LFUN_THESAURUS_ENTRY:
3277 case LFUN_SERVER_GET_STATISTICS:
3278 // these are handled in our dispatch()
3282 case LFUN_INSET_INSERT: {
3283 string const type = cmd.getArg(0);
3284 if (type == "toc") {
3286 // not allowed in description items
3287 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3288 enable = !inDescriptionItem(cur);
3301 || !cur.inset().insetAllowed(code)
3302 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3305 flag.setEnabled(enable);
3310 void Text::pasteString(Cursor & cur, docstring const & clip,
3313 if (!clip.empty()) {
3316 insertStringAsParagraphs(cur, clip, cur.current_font);
3318 insertStringAsLines(cur, clip, cur.current_font);
3323 // FIXME: an item inset would make things much easier.
3324 bool Text::inDescriptionItem(Cursor & cur) const
3326 Paragraph & par = cur.paragraph();
3327 pos_type const pos = cur.pos();
3328 pos_type const body_pos = par.beginOfBody();
3330 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3331 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3332 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3335 return (pos < body_pos
3337 && (pos == 0 || par.getChar(pos - 1) != ' ')));