3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Lars Gullik Bjønnes
8 * \author Alfredo Braunstein
9 * \author Angus Leeming
11 * \author André Pönitz
13 * Full author contact details are available in file CREDITS.
20 #include "BranchList.h"
21 #include "FloatList.h"
22 #include "FuncStatus.h"
24 #include "buffer_funcs.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "SpellChecker.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
47 #include "Thesaurus.h"
48 #include "WordLangTuple.h"
50 #include "frontends/alert.h"
51 #include "frontends/Application.h"
52 #include "frontends/Clipboard.h"
53 #include "frontends/Selection.h"
55 #include "insets/InsetArgument.h"
56 #include "insets/InsetCollapsible.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetFloat.h"
60 #include "insets/InsetFloatList.h"
61 #include "insets/InsetGraphics.h"
62 #include "insets/InsetGraphicsParams.h"
63 #include "insets/InsetIPAMacro.h"
64 #include "insets/InsetNewline.h"
65 #include "insets/InsetQuotes.h"
66 #include "insets/InsetSpecialChar.h"
67 #include "insets/InsetText.h"
68 #include "insets/InsetWrap.h"
70 #include "support/convert.h"
71 #include "support/debug.h"
72 #include "support/gettext.h"
73 #include "support/lassert.h"
74 #include "support/lstrings.h"
75 #include "support/lyxalgo.h"
76 #include "support/lyxtime.h"
77 #include "support/os.h"
78 #include "support/regex.h"
80 #include "mathed/InsetMathHull.h"
81 #include "mathed/InsetMathMacroTemplate.h"
87 using namespace lyx::support;
91 using cap::copySelection;
92 using cap::cutSelection;
93 using cap::cutSelectionToTemp;
94 using cap::pasteFromStack;
95 using cap::pasteFromTemp;
96 using cap::pasteClipboardText;
97 using cap::pasteClipboardGraphics;
98 using cap::replaceSelection;
99 using cap::grabAndEraseSelection;
100 using cap::selClearOrDel;
101 using cap::pasteSimpleText;
102 using frontend::Clipboard;
105 static Font freefont(ignore_font, ignore_language);
106 static bool toggleall = false;
108 static void toggleAndShow(Cursor & cur, Text * text,
109 Font const & font, bool togall = true)
111 text->toggleFree(cur, font, togall);
113 if (font.language() != ignore_language ||
114 font.fontInfo().number() != FONT_IGNORE) {
115 TextMetrics const & tm = cur.bv().textMetrics(text);
116 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
117 cur.real_current_font))
118 text->setCursor(cur, cur.pit(), cur.pos(),
119 false, !cur.boundary());
124 static void moveCursor(Cursor & cur, bool selecting)
126 if (selecting || cur.mark())
131 static void finishChange(Cursor & cur, bool selecting)
134 moveCursor(cur, selecting);
138 static void mathDispatch(Cursor & cur, FuncRequest const & cmd)
141 docstring sel = cur.selectionAsString(false);
143 // It may happen that sel is empty but there is a selection
144 replaceSelection(cur);
146 // Is this a valid formula?
150 #ifdef ENABLE_ASSERTIONS
151 const int old_pos = cur.pos();
153 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
154 #ifdef ENABLE_ASSERTIONS
155 LATTEST(old_pos == cur.pos());
157 cur.nextInset()->edit(cur, true);
158 if (cmd.action() != LFUN_MATH_MODE)
159 // LFUN_MATH_MODE has a different meaning in math mode
162 InsetMathHull * formula = new InsetMathHull(cur.buffer());
163 string const selstr = to_utf8(sel);
164 istringstream is(selstr);
167 if (!formula->readQuiet(lex)) {
168 // No valid formula, let's try with delims
169 is.str("$" + selstr + "$");
171 if (!formula->readQuiet(lex)) {
172 // Still not valid, leave it as is
180 cur.nextInset()->edit(cur, true);
181 LASSERT(cur.inMathed(), return);
185 cur.pos() = cur.lastpos();
186 if (cmd.action() != LFUN_MATH_MODE)
187 // LFUN_MATH_MODE has a different meaning in math mode
189 cur.clearSelection();
190 cur.pos() = cur.lastpos();
194 cur.message(from_utf8(N_("Math editor mode")));
196 cur.message(from_utf8(N_("No valid math formula")));
200 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
202 LASSERT(cmd.action() == LFUN_REGEXP_MODE, return);
203 if (cur.inRegexped()) {
204 cur.message(_("Already in regular expression mode"));
208 docstring sel = cur.selectionAsString(false);
210 // It may happen that sel is empty but there is a selection
211 replaceSelection(cur);
213 cur.insert(new InsetMathHull(cur.buffer(), hullRegexp));
214 cur.nextInset()->edit(cur, true);
217 cur.message(_("Regexp editor mode"));
221 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
224 cap::replaceSelection(cur);
225 cur.insert(new InsetSpecialChar(kind));
230 static void ipaChar(Cursor & cur, InsetIPAChar::Kind kind)
233 cap::replaceSelection(cur);
234 cur.insert(new InsetIPAChar(kind));
239 static bool doInsertInset(Cursor & cur, Text * text,
240 FuncRequest const & cmd, bool edit, bool pastesel)
242 Buffer & buffer = cur.bv().buffer();
243 BufferParams const & bparams = buffer.params();
244 Inset * inset = createInset(&buffer, cmd);
248 if (InsetCollapsible * ci = inset->asInsetCollapsible())
249 ci->setButtonLabel();
252 if (cmd.action() == LFUN_INDEX_INSERT) {
253 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
254 text->insertInset(cur, inset);
256 inset->edit(cur, true);
257 // Now put this into inset
258 Font const f(inherit_font, cur.current_font.language());
260 cur.text()->insertStringAsLines(cur, ds, f);
261 cur.leaveInset(*inset);
265 else if (cmd.action() == LFUN_ARGUMENT_INSERT) {
266 bool cotextinsert = false;
267 InsetArgument const * const ia = static_cast<InsetArgument const *>(inset);
268 Layout const & lay = cur.paragraph().layout();
269 Layout::LaTeXArgMap args = lay.args();
270 Layout::LaTeXArgMap::const_iterator const lait = args.find(ia->name());
271 if (lait != args.end())
272 cotextinsert = (*lait).second.insertcotext;
274 InsetLayout const & il = cur.inset().getLayout();
276 Layout::LaTeXArgMap::const_iterator const ilait = args.find(ia->name());
277 if (ilait != args.end())
278 cotextinsert = (*ilait).second.insertcotext;
280 // The argument requests to insert a copy of the co-text to the inset
283 // If we have a selection within a paragraph, use this
284 if (cur.selection() && cur.selBegin().pit() == cur.selEnd().pit())
285 ds = cur.selectionAsString(false);
286 // else use the whole paragraph
288 ds = cur.paragraph().asString();
289 text->insertInset(cur, inset);
291 inset->edit(cur, true);
292 // Now put co-text into inset
293 Font const f(inherit_font, cur.current_font.language());
295 cur.text()->insertStringAsLines(cur, ds, f);
296 cur.leaveInset(*inset);
303 if (cur.selection()) {
304 cutSelectionToTemp(cur, false, pastesel);
305 cur.clearSelection();
308 text->insertInset(cur, inset);
311 inset->edit(cur, true);
313 if (!gotsel || !pastesel)
316 pasteFromTemp(cur, cur.buffer()->errorList("Paste"));
317 cur.buffer()->errors("Paste");
318 cur.clearSelection(); // bug 393
320 InsetText * inset_text = inset->asInsetText();
322 inset_text->fixParagraphsFont();
323 if (!inset_text->allowMultiPar() || cur.lastpit() == 0) {
324 // reset first par to default
325 cur.text()->paragraphs().begin()
326 ->setPlainOrDefaultLayout(bparams.documentClass());
329 // Merge multiple paragraphs -- hack
330 while (cur.lastpit() > 0)
331 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
332 if (cmd.action() == LFUN_FLEX_INSERT)
335 cur.leaveInset(*inset);
336 if (cmd.action() == LFUN_PREVIEW_INSERT
337 || cmd.action() == LFUN_IPA_INSERT)
339 notifyCursorLeavesOrEnters(old, cur);
342 cur.leaveInset(*inset);
343 // reset surrounding par to default
344 DocumentClass const & dc = bparams.documentClass();
345 docstring const layoutname = inset->usePlainLayout()
346 ? dc.plainLayoutName()
347 : dc.defaultLayoutName();
348 text->setLayout(cur, layoutname);
354 string const freefont2string()
356 return freefont.toString(toggleall);
360 /// the type of outline operation
362 OutlineUp, // Move this header with text down
363 OutlineDown, // Move this header with text up
364 OutlineIn, // Make this header deeper
365 OutlineOut // Make this header shallower
369 static void insertSeparator(Cursor & cur, depth_type const depth)
371 Buffer & buf = *cur.buffer();
372 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
373 DocumentClass const & tc = buf.params().documentClass();
374 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
375 + from_ascii("\" ignoreautonests")));
376 // FIXME: Bibitem mess!
377 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
378 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
379 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
380 while (cur.paragraph().params().depth() > depth)
381 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
385 static void outline(OutlineOp mode, Cursor & cur, Text * text)
387 Buffer & buf = *cur.buffer();
388 pit_type & pit = cur.pit();
389 ParagraphList & pars = buf.text().paragraphs();
390 ParagraphList::iterator const bgn = pars.begin();
391 // The first paragraph of the area to be copied:
392 ParagraphList::iterator start = lyx::next(bgn, pit);
393 // The final paragraph of area to be copied:
394 ParagraphList::iterator finish = start;
395 ParagraphList::iterator const end = pars.end();
396 depth_type const current_depth = cur.paragraph().params().depth();
398 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
401 // Move out (down) from this section header
405 // Seek the one (on same level) below
406 for (; finish != end; ++finish) {
407 toclevel = buf.text().getTocLevel(distance(bgn, finish));
408 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
414 if (start == pars.begin())
417 ParagraphList::iterator dest = start;
418 // Move out (up) from this header
421 // Search previous same-level header above
424 toclevel = buf.text().getTocLevel(distance(bgn, dest));
426 && (toclevel == Layout::NOT_IN_TOC
427 || toclevel > thistoclevel));
428 // Not found; do nothing
429 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
431 pit_type newpit = distance(bgn, dest);
432 pit_type const len = distance(start, finish);
433 pit_type const deletepit = pit + len;
434 buf.undo().recordUndo(cur, newpit, deletepit - 1);
435 // If we move an environment upwards, make sure it is
436 // separated from its new neighbour below:
437 // If an environment of the same layout follows, and the moved
438 // paragraph sequence does not end with a separator, insert one.
439 ParagraphList::iterator lastmoved = finish;
441 if (start->layout().isEnvironment()
442 && dest->layout() == start->layout()
443 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
444 cur.pit() = distance(bgn, lastmoved);
445 cur.pos() = cur.lastpos();
446 insertSeparator(cur, current_depth);
449 // Likewise, if we moved an environment upwards, make sure it
450 // is separated from its new neighbour above.
451 // The paragraph before the target of movement
453 ParagraphList::iterator before = dest;
455 // Get the parent paragraph (outer in nested context)
456 pit_type const parent =
457 before->params().depth() > current_depth
458 ? text->depthHook(distance(bgn, before), current_depth)
459 : distance(bgn, before);
460 // If a environment with same layout preceeds the moved one in the new
461 // position, and there is no separator yet, insert one.
462 if (start->layout().isEnvironment()
463 && pars[parent].layout() == start->layout()
464 && !before->isEnvSeparator(before->beginOfBody())) {
465 cur.pit() = distance(bgn, before);
466 cur.pos() = cur.lastpos();
467 insertSeparator(cur, current_depth);
471 newpit = distance(bgn, dest);
472 pars.splice(dest, start, finish);
480 // Go one down from *this* header:
481 ParagraphList::iterator dest = next(finish, 1);
482 // Go further down to find header to insert in front of:
483 for (; dest != end; ++dest) {
484 toclevel = buf.text().getTocLevel(distance(bgn, dest));
485 if (toclevel != Layout::NOT_IN_TOC
486 && toclevel <= thistoclevel)
489 // One such was found, so go on...
490 // If we move an environment downwards, make sure it is
491 // separated from its new neighbour above.
492 pit_type newpit = distance(bgn, dest);
493 buf.undo().recordUndo(cur, pit, newpit - 1);
494 // The paragraph before the target of movement
495 ParagraphList::iterator before = dest;
497 // Get the parent paragraph (outer in nested context)
498 pit_type const parent =
499 before->params().depth() > current_depth
500 ? text->depthHook(distance(bgn, before), current_depth)
501 : distance(bgn, before);
502 // If a environment with same layout preceeds the moved one in the new
503 // position, and there is no separator yet, insert one.
504 if (start->layout().isEnvironment()
505 && pars[parent].layout() == start->layout()
506 && !before->isEnvSeparator(before->beginOfBody())) {
507 cur.pit() = distance(bgn, before);
508 cur.pos() = cur.lastpos();
509 insertSeparator(cur, current_depth);
512 // Likewise, make sure moved environments are separated
513 // from their new neighbour below:
514 // If an environment of the same layout follows, and the moved
515 // paragraph sequence does not end with a separator, insert one.
516 ParagraphList::iterator lastmoved = finish;
519 && start->layout().isEnvironment()
520 && dest->layout() == start->layout()
521 && !lastmoved->isEnvSeparator(lastmoved->beginOfBody())) {
522 cur.pit() = distance(bgn, lastmoved);
523 cur.pos() = cur.lastpos();
524 insertSeparator(cur, current_depth);
527 newpit = distance(bgn, dest);
528 pit_type const len = distance(start, finish);
529 pars.splice(dest, start, finish);
530 cur.pit() = newpit - len;
535 pit_type const len = distance(start, finish);
536 buf.undo().recordUndo(cur, pit, pit + len - 1);
537 for (; start != finish; ++start) {
538 toclevel = buf.text().getTocLevel(distance(bgn, start));
539 if (toclevel == Layout::NOT_IN_TOC)
542 DocumentClass const & tc = buf.params().documentClass();
543 DocumentClass::const_iterator lit = tc.begin();
544 DocumentClass::const_iterator len = tc.end();
545 int const newtoclevel =
546 (mode == OutlineIn ? toclevel + 1 : toclevel - 1);
547 LabelType const oldlabeltype = start->layout().labeltype;
549 for (; lit != len; ++lit) {
550 if (lit->toclevel == newtoclevel &&
551 lit->labeltype == oldlabeltype) {
552 start->setLayout(*lit);
563 void Text::number(Cursor & cur)
565 FontInfo font = ignore_font;
566 font.setNumber(FONT_TOGGLE);
567 toggleAndShow(cur, this, Font(font, ignore_language));
571 bool Text::isRTL(Paragraph const & par) const
573 Buffer const & buffer = owner_->buffer();
574 return par.isRTL(buffer.params());
580 Language const * getLanguage(Cursor const & cur, string const & lang) {
581 return lang.empty() ? cur.getFont().language() : languages.getLanguage(lang);
587 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
589 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
591 // Dispatch if the cursor is inside the text. It is not the
592 // case for context menus (bug 5797).
593 if (cur.text() != this) {
598 BufferView * bv = &cur.bv();
599 TextMetrics * tm = &bv->textMetrics(this);
600 if (!tm->contains(cur.pit())) {
601 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
602 tm = &bv->textMetrics(this);
605 // FIXME: We use the update flag to indicates wether a singlePar or a
606 // full screen update is needed. We reset it here but shall we restore it
608 cur.noScreenUpdate();
610 LBUFERR(this == cur.text());
612 // NOTE: This should NOT be a reference. See commit 94a5481a.
613 CursorSlice const oldTopSlice = cur.top();
614 bool const oldBoundary = cur.boundary();
615 bool const oldSelection = cur.selection();
616 // Signals that, even if needsUpdate == false, an update of the
617 // cursor paragraph is required
618 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action(),
619 LyXAction::SingleParUpdate);
620 // Signals that a full-screen update is required
621 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action(),
622 LyXAction::NoUpdate) || singleParUpdate);
623 bool const last_misspelled = lyxrc.spellcheck_continuously
624 && cur.paragraph().isMisspelled(cur.pos(), true);
626 FuncCode const act = cmd.action();
629 case LFUN_PARAGRAPH_MOVE_DOWN: {
630 pit_type const pit = cur.pit();
631 cur.recordUndo(pit, pit + 1);
633 pars_.swap(pit, pit + 1);
635 cur.forceBufferUpdate();
640 case LFUN_PARAGRAPH_MOVE_UP: {
641 pit_type const pit = cur.pit();
642 cur.recordUndo(pit - 1, pit);
644 pars_.swap(pit, pit - 1);
647 cur.forceBufferUpdate();
651 case LFUN_APPENDIX: {
652 Paragraph & par = cur.paragraph();
653 bool start = !par.params().startOfAppendix();
655 // FIXME: The code below only makes sense at top level.
656 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
657 // ensure that we have only one start_of_appendix in this document
658 // FIXME: this don't work for multipart document!
659 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
660 if (pars_[tmp].params().startOfAppendix()) {
661 cur.recordUndo(tmp, tmp);
662 pars_[tmp].params().startOfAppendix(false);
668 par.params().startOfAppendix(start);
670 // we can set the refreshing parameters now
671 cur.forceBufferUpdate();
675 case LFUN_WORD_DELETE_FORWARD:
677 cutSelection(cur, true, false);
679 deleteWordForward(cur, cmd.getArg(0) == "force");
680 finishChange(cur, false);
683 case LFUN_WORD_DELETE_BACKWARD:
685 cutSelection(cur, true, false);
687 deleteWordBackward(cur, cmd.getArg(0) == "force");
688 finishChange(cur, false);
691 case LFUN_LINE_DELETE_FORWARD:
693 cutSelection(cur, true, false);
695 tm->deleteLineForward(cur);
696 finishChange(cur, false);
699 case LFUN_BUFFER_BEGIN:
700 case LFUN_BUFFER_BEGIN_SELECT:
701 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_BEGIN_SELECT);
702 if (cur.depth() == 1)
703 needsUpdate |= cursorTop(cur);
706 cur.screenUpdateFlags(Update::FitCursor);
709 case LFUN_BUFFER_END:
710 case LFUN_BUFFER_END_SELECT:
711 needsUpdate |= cur.selHandle(act == LFUN_BUFFER_END_SELECT);
712 if (cur.depth() == 1)
713 needsUpdate |= cursorBottom(cur);
716 cur.screenUpdateFlags(Update::FitCursor);
719 case LFUN_INSET_BEGIN:
720 case LFUN_INSET_BEGIN_SELECT:
721 needsUpdate |= cur.selHandle(act == LFUN_INSET_BEGIN_SELECT);
722 if (cur.depth() == 1 || !cur.top().at_begin())
723 needsUpdate |= cursorTop(cur);
726 cur.screenUpdateFlags(Update::FitCursor);
730 case LFUN_INSET_END_SELECT:
731 needsUpdate |= cur.selHandle(act == LFUN_INSET_END_SELECT);
732 if (cur.depth() == 1 || !cur.top().at_end())
733 needsUpdate |= cursorBottom(cur);
736 cur.screenUpdateFlags(Update::FitCursor);
739 case LFUN_CHAR_FORWARD:
740 case LFUN_CHAR_FORWARD_SELECT: {
741 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
742 needsUpdate |= cur.selHandle(act == LFUN_CHAR_FORWARD_SELECT);
743 bool const cur_moved = cursorForward(cur);
744 needsUpdate |= cur_moved;
746 if (!cur_moved && oldTopSlice == cur.top()
747 && cur.boundary() == oldBoundary) {
749 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
751 // we will probably be moving out the inset, so we should execute
752 // the depm-mechanism, but only when the cursor has a place to
753 // go outside this inset, i.e. in a slice above.
754 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
755 && cur.pit() == cur.lastpit()) {
756 // The cursor hasn't changed yet. To give the
757 // DEPM the possibility of doing something we must
758 // provide it with two different cursors.
760 dummy.pos() = dummy.pit() = 0;
761 if (cur.bv().checkDepm(dummy, cur))
762 cur.forceBufferUpdate();
768 case LFUN_CHAR_BACKWARD:
769 case LFUN_CHAR_BACKWARD_SELECT: {
770 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
771 needsUpdate |= cur.selHandle(act == LFUN_CHAR_BACKWARD_SELECT);
772 bool const cur_moved = cursorBackward(cur);
773 needsUpdate |= cur_moved;
775 if (!cur_moved && oldTopSlice == cur.top()
776 && cur.boundary() == oldBoundary) {
778 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
780 // we will probably be moving out the inset, so we should execute
781 // the depm-mechanism, but only when the cursor has a place to
782 // go outside this inset, i.e. in a slice above.
783 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
784 // The cursor hasn't changed yet. To give the
785 // DEPM the possibility of doing something we must
786 // provide it with two different cursors.
788 dummy.pos() = cur.lastpos();
789 dummy.pit() = cur.lastpit();
790 if (cur.bv().checkDepm(dummy, cur))
791 cur.forceBufferUpdate();
798 case LFUN_CHAR_LEFT_SELECT:
799 if (lyxrc.visual_cursor) {
800 needsUpdate |= cur.selHandle(act == LFUN_CHAR_LEFT_SELECT);
801 bool const cur_moved = cursorVisLeft(cur);
802 needsUpdate |= cur_moved;
803 if (!cur_moved && oldTopSlice == cur.top()
804 && cur.boundary() == oldBoundary) {
806 cmd = FuncRequest(LFUN_FINISHED_LEFT);
809 if (cur.reverseDirectionNeeded()) {
810 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
811 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
813 cmd.setAction(cmd.action() == LFUN_CHAR_LEFT_SELECT ?
814 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
821 case LFUN_CHAR_RIGHT:
822 case LFUN_CHAR_RIGHT_SELECT:
823 if (lyxrc.visual_cursor) {
824 needsUpdate |= cur.selHandle(cmd.action() == LFUN_CHAR_RIGHT_SELECT);
825 bool const cur_moved = cursorVisRight(cur);
826 needsUpdate |= cur_moved;
827 if (!cur_moved && oldTopSlice == cur.top()
828 && cur.boundary() == oldBoundary) {
830 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
833 if (cur.reverseDirectionNeeded()) {
834 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
835 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD);
837 cmd.setAction(cmd.action() == LFUN_CHAR_RIGHT_SELECT ?
838 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD);
847 case LFUN_DOWN_SELECT:
850 // stop/start the selection
851 bool select = cmd.action() == LFUN_DOWN_SELECT ||
852 cmd.action() == LFUN_UP_SELECT;
854 // move cursor up/down
855 bool up = cmd.action() == LFUN_UP_SELECT || cmd.action() == LFUN_UP;
856 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
858 if (!atFirstOrLastRow) {
859 needsUpdate |= cur.selHandle(select);
860 cur.upDownInText(up, needsUpdate);
861 needsUpdate |= cur.beforeDispatchCursor().inMathed();
863 pos_type newpos = up ? 0 : cur.lastpos();
864 if (lyxrc.mac_like_cursor_movement && cur.pos() != newpos) {
865 needsUpdate |= cur.selHandle(select);
866 // we do not reset the targetx of the cursor
868 needsUpdate |= bv->checkDepm(cur, bv->cursor());
869 cur.updateTextTargetOffset();
871 cur.forceBufferUpdate();
875 // if the cursor cannot be moved up or down do not remove
876 // the selection right now, but wait for the next dispatch.
878 needsUpdate |= cur.selHandle(select);
879 cur.upDownInText(up, needsUpdate);
886 case LFUN_PARAGRAPH_UP:
887 case LFUN_PARAGRAPH_UP_SELECT:
888 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_UP_SELECT);
889 needsUpdate |= cursorUpParagraph(cur);
892 case LFUN_PARAGRAPH_DOWN:
893 case LFUN_PARAGRAPH_DOWN_SELECT:
894 needsUpdate |= cur.selHandle(cmd.action() == LFUN_PARAGRAPH_DOWN_SELECT);
895 needsUpdate |= cursorDownParagraph(cur);
898 case LFUN_LINE_BEGIN:
899 case LFUN_LINE_BEGIN_SELECT:
900 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_BEGIN_SELECT);
901 needsUpdate |= tm->cursorHome(cur);
905 case LFUN_LINE_END_SELECT:
906 needsUpdate |= cur.selHandle(cmd.action() == LFUN_LINE_END_SELECT);
907 needsUpdate |= tm->cursorEnd(cur);
910 case LFUN_SECTION_SELECT: {
911 Buffer const & buf = *cur.buffer();
912 pit_type const pit = cur.pit();
913 ParagraphList & pars = buf.text().paragraphs();
914 ParagraphList::iterator bgn = pars.begin();
915 // The first paragraph of the area to be selected:
916 ParagraphList::iterator start = lyx::next(bgn, pit);
917 // The final paragraph of area to be selected:
918 ParagraphList::iterator finish = start;
919 ParagraphList::iterator end = pars.end();
921 int const thistoclevel = buf.text().getTocLevel(distance(bgn, start));
922 if (thistoclevel == Layout::NOT_IN_TOC)
926 Cursor const old_cur = cur;
927 needsUpdate |= cur.selHandle(true);
929 // Move out (down) from this section header
933 // Seek the one (on same level) below
934 for (; finish != end; ++finish, ++cur.pit()) {
935 int const toclevel = buf.text().getTocLevel(distance(bgn, finish));
936 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
939 cur.pos() = cur.lastpos();
941 cur.setCurrentFont();
943 needsUpdate |= cur != old_cur;
947 case LFUN_WORD_RIGHT:
948 case LFUN_WORD_RIGHT_SELECT:
949 if (lyxrc.visual_cursor) {
950 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_RIGHT_SELECT);
951 bool const cur_moved = cursorVisRightOneWord(cur);
952 needsUpdate |= cur_moved;
953 if (!cur_moved && oldTopSlice == cur.top()
954 && cur.boundary() == oldBoundary) {
956 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
959 if (cur.reverseDirectionNeeded()) {
960 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
961 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
963 cmd.setAction(cmd.action() == LFUN_WORD_RIGHT_SELECT ?
964 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
971 case LFUN_WORD_FORWARD:
972 case LFUN_WORD_FORWARD_SELECT: {
973 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_FORWARD_SELECT);
974 bool const cur_moved = cursorForwardOneWord(cur);
975 needsUpdate |= cur_moved;
977 if (!cur_moved && oldTopSlice == cur.top()
978 && cur.boundary() == oldBoundary) {
980 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
982 // we will probably be moving out the inset, so we should execute
983 // the depm-mechanism, but only when the cursor has a place to
984 // go outside this inset, i.e. in a slice above.
985 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
986 && cur.pit() == cur.lastpit()) {
987 // The cursor hasn't changed yet. To give the
988 // DEPM the possibility of doing something we must
989 // provide it with two different cursors.
991 dummy.pos() = dummy.pit() = 0;
992 if (cur.bv().checkDepm(dummy, cur))
993 cur.forceBufferUpdate();
1000 case LFUN_WORD_LEFT_SELECT:
1001 if (lyxrc.visual_cursor) {
1002 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_LEFT_SELECT);
1003 bool const cur_moved = cursorVisLeftOneWord(cur);
1004 needsUpdate |= cur_moved;
1005 if (!cur_moved && oldTopSlice == cur.top()
1006 && cur.boundary() == oldBoundary) {
1008 cmd = FuncRequest(LFUN_FINISHED_LEFT);
1011 if (cur.reverseDirectionNeeded()) {
1012 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1013 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD);
1015 cmd.setAction(cmd.action() == LFUN_WORD_LEFT_SELECT ?
1016 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD);
1023 case LFUN_WORD_BACKWARD:
1024 case LFUN_WORD_BACKWARD_SELECT: {
1025 needsUpdate |= cur.selHandle(cmd.action() == LFUN_WORD_BACKWARD_SELECT);
1026 bool const cur_moved = cursorBackwardOneWord(cur);
1027 needsUpdate |= cur_moved;
1029 if (!cur_moved && oldTopSlice == cur.top()
1030 && cur.boundary() == oldBoundary) {
1032 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
1034 // we will probably be moving out the inset, so we should execute
1035 // the depm-mechanism, but only when the cursor has a place to
1036 // go outside this inset, i.e. in a slice above.
1037 if (cur.depth() > 1 && cur.pos() == 0
1038 && cur.pit() == 0) {
1039 // The cursor hasn't changed yet. To give the
1040 // DEPM the possibility of doing something we must
1041 // provide it with two different cursors.
1043 dummy.pos() = cur.lastpos();
1044 dummy.pit() = cur.lastpit();
1045 if (cur.bv().checkDepm(dummy, cur))
1046 cur.forceBufferUpdate();
1052 case LFUN_WORD_SELECT: {
1053 selectWord(cur, WHOLE_WORD);
1054 finishChange(cur, true);
1058 case LFUN_NEWLINE_INSERT: {
1059 InsetNewlineParams inp;
1060 docstring arg = cmd.argument();
1061 if (arg == "linebreak")
1062 inp.kind = InsetNewlineParams::LINEBREAK;
1064 inp.kind = InsetNewlineParams::NEWLINE;
1065 cap::replaceSelection(cur);
1067 cur.insert(new InsetNewline(inp));
1069 moveCursor(cur, false);
1073 case LFUN_TAB_INSERT: {
1074 bool const multi_par_selection = cur.selection() &&
1075 cur.selBegin().pit() != cur.selEnd().pit();
1076 if (multi_par_selection) {
1077 // If there is a multi-paragraph selection, a tab is inserted
1078 // at the beginning of each paragraph.
1079 cur.recordUndoSelection();
1080 pit_type const pit_end = cur.selEnd().pit();
1081 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1082 pars_[pit].insertChar(0, '\t',
1083 bv->buffer().params().track_changes);
1084 // Update the selection pos to make sure the selection does not
1085 // change as the inserted tab will increase the logical pos.
1086 if (cur.realAnchor().pit() == pit)
1087 cur.realAnchor().forwardPos();
1088 if (cur.pit() == pit)
1093 // Maybe we shouldn't allow tabs within a line, because they
1094 // are not (yet) aligned as one might do expect.
1095 FuncRequest ncmd(LFUN_SELF_INSERT, from_ascii("\t"));
1096 dispatch(cur, ncmd);
1101 case LFUN_TAB_DELETE: {
1102 bool const tc = bv->buffer().params().track_changes;
1103 if (cur.selection()) {
1104 // If there is a selection, a tab (if present) is removed from
1105 // the beginning of each paragraph.
1106 cur.recordUndoSelection();
1107 pit_type const pit_end = cur.selEnd().pit();
1108 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
1109 Paragraph & par = paragraphs()[pit];
1112 char_type const c = par.getChar(0);
1113 if (c == '\t' || c == ' ') {
1114 // remove either 1 tab or 4 spaces.
1115 int const n = (c == ' ' ? 4 : 1);
1116 for (int i = 0; i < n
1117 && !par.empty() && par.getChar(0) == c; ++i) {
1118 if (cur.pit() == pit)
1120 if (cur.realAnchor().pit() == pit
1121 && cur.realAnchor().pos() > 0 )
1122 cur.realAnchor().backwardPos();
1123 par.eraseChar(0, tc);
1129 // If there is no selection, try to remove a tab or some spaces
1130 // before the position of the cursor.
1131 Paragraph & par = paragraphs()[cur.pit()];
1132 pos_type const pos = cur.pos();
1137 char_type const c = par.getChar(pos - 1);
1141 par.eraseChar(cur.pos(), tc);
1143 for (int n_spaces = 0;
1145 && par.getChar(cur.pos() - 1) == ' '
1149 par.eraseChar(cur.pos(), tc);
1156 case LFUN_CHAR_DELETE_FORWARD:
1157 if (!cur.selection()) {
1158 if (cur.pos() == cur.paragraph().size())
1159 // Par boundary, force full-screen update
1160 singleParUpdate = false;
1161 else if (cmd.getArg(0) != "force" && cur.confirmDeletion()) {
1163 cur.selection(true);
1168 needsUpdate |= erase(cur);
1171 cutSelection(cur, true, false);
1172 singleParUpdate = false;
1174 moveCursor(cur, false);
1177 case LFUN_CHAR_DELETE_BACKWARD:
1178 if (!cur.selection()) {
1179 if (bv->getIntl().getTransManager().backspace()) {
1180 bool par_boundary = cur.pos() == 0;
1181 bool first_par = cur.pit() == 0;
1182 // Par boundary, full-screen update
1184 singleParUpdate = false;
1185 else if (cmd.getArg(0) != "force" && cur.confirmDeletion(true)) {
1187 cur.selection(true);
1192 needsUpdate |= backspace(cur);
1194 if (par_boundary && !first_par && cur.pos() > 0
1195 && cur.paragraph().isEnvSeparator(cur.pos() - 1)) {
1196 needsUpdate |= backspace(cur);
1201 cutSelection(cur, true, false);
1202 singleParUpdate = false;
1206 case LFUN_PARAGRAPH_BREAK: {
1207 cap::replaceSelection(cur);
1208 pit_type pit = cur.pit();
1209 Paragraph const & par = pars_[pit];
1210 bool lastpar = (pit == pit_type(pars_.size() - 1));
1211 Paragraph const & nextpar = lastpar ? par : pars_[pit + 1];
1212 pit_type prev = pit > 0 ? depthHook(pit, par.getDepth()) : pit;
1213 if (prev < pit && cur.pos() == par.beginOfBody()
1214 && !par.size() && !par.isEnvSeparator(cur.pos())
1215 && !par.layout().isCommand()
1216 && pars_[prev].layout() != par.layout()
1217 && pars_[prev].layout().isEnvironment()
1218 && !nextpar.isEnvSeparator(nextpar.beginOfBody())) {
1219 if (par.layout().isEnvironment()
1220 && pars_[prev].getDepth() == par.getDepth()) {
1221 docstring const layout = par.layout().name();
1222 DocumentClass const & tc = bv->buffer().params().documentClass();
1223 lyx::dispatch(FuncRequest(LFUN_LAYOUT, tc.plainLayout().name()));
1224 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1225 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1226 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1228 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1229 breakParagraph(cur);
1231 Font const f(inherit_font, cur.current_font.language());
1232 pars_[cur.pit() - 1].resetFonts(f);
1234 if (par.isEnvSeparator(cur.pos()) && cmd.getArg(1) != "ignoresep")
1236 breakParagraph(cur, cmd.getArg(0) == "inverse");
1239 // If we have a list and autoinsert item insets,
1241 Layout::LaTeXArgMap args = par.layout().args();
1242 for (auto const & thearg : args) {
1243 Layout::latexarg arg = thearg.second;
1244 if (arg.autoinsert && prefixIs(thearg.first, "item:")) {
1245 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, thearg.first);
1246 lyx::dispatch(cmd2);
1252 case LFUN_INSET_INSERT: {
1255 // We have to avoid triggering InstantPreview loading
1256 // before inserting into the document. See bug #5626.
1257 bool loaded = bv->buffer().isFullyLoaded();
1258 bv->buffer().setFullyLoaded(false);
1259 Inset * inset = createInset(&bv->buffer(), cmd);
1260 bv->buffer().setFullyLoaded(loaded);
1263 // FIXME (Abdel 01/02/2006):
1264 // What follows would be a partial fix for bug 2154:
1265 // http://www.lyx.org/trac/ticket/2154
1266 // This automatically put the label inset _after_ a
1267 // numbered section. It should be possible to extend the mechanism
1268 // to any kind of LateX environement.
1269 // The correct way to fix that bug would be at LateX generation.
1270 // I'll let the code here for reference as it could be used for some
1271 // other feature like "automatic labelling".
1273 Paragraph & par = pars_[cur.pit()];
1274 if (inset->lyxCode() == LABEL_CODE
1275 && !par.layout().counter.empty()) {
1276 // Go to the end of the paragraph
1277 // Warning: Because of Change-Tracking, the last
1278 // position is 'size()' and not 'size()-1':
1279 cur.pos() = par.size();
1280 // Insert a new paragraph
1281 FuncRequest fr(LFUN_PARAGRAPH_BREAK);
1285 if (cur.selection())
1286 cutSelection(cur, true, false);
1288 cur.forceBufferUpdate();
1289 if (inset->editable() && inset->asInsetText())
1290 inset->edit(cur, true);
1294 // trigger InstantPreview now
1295 if (inset->lyxCode() == EXTERNAL_CODE) {
1296 InsetExternal & ins =
1297 static_cast<InsetExternal &>(*inset);
1298 ins.updatePreview();
1305 case LFUN_INSET_DISSOLVE: {
1306 if (dissolveInset(cur)) {
1308 cur.forceBufferUpdate();
1313 case LFUN_GRAPHICS_SET_GROUP: {
1314 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1320 string id = to_utf8(cmd.argument());
1321 string grp = graphics::getGroupParams(bv->buffer(), id);
1322 InsetGraphicsParams tmp, inspar = ins->getParams();
1325 inspar.groupId = to_utf8(cmd.argument());
1327 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1328 tmp.filename = inspar.filename;
1332 ins->setParams(inspar);
1336 case LFUN_SPACE_INSERT:
1337 if (cur.paragraph().layout().free_spacing)
1338 insertChar(cur, ' ');
1340 doInsertInset(cur, this, cmd, false, false);
1343 moveCursor(cur, false);
1346 case LFUN_SPECIALCHAR_INSERT: {
1347 string const name = to_utf8(cmd.argument());
1348 if (name == "hyphenation")
1349 specialChar(cur, InsetSpecialChar::HYPHENATION);
1350 else if (name == "allowbreak")
1351 specialChar(cur, InsetSpecialChar::ALLOWBREAK);
1352 else if (name == "ligature-break")
1353 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1354 else if (name == "slash")
1355 specialChar(cur, InsetSpecialChar::SLASH);
1356 else if (name == "nobreakdash")
1357 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1358 else if (name == "dots")
1359 specialChar(cur, InsetSpecialChar::LDOTS);
1360 else if (name == "end-of-sentence")
1361 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1362 else if (name == "menu-separator")
1363 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1364 else if (name == "lyx")
1365 specialChar(cur, InsetSpecialChar::PHRASE_LYX);
1366 else if (name == "tex")
1367 specialChar(cur, InsetSpecialChar::PHRASE_TEX);
1368 else if (name == "latex")
1369 specialChar(cur, InsetSpecialChar::PHRASE_LATEX);
1370 else if (name == "latex2e")
1371 specialChar(cur, InsetSpecialChar::PHRASE_LATEX2E);
1372 else if (name.empty())
1373 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1375 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1379 case LFUN_IPAMACRO_INSERT: {
1380 string const arg = cmd.getArg(0);
1381 if (arg == "deco") {
1382 // Open the inset, and move the current selection
1384 doInsertInset(cur, this, cmd, true, true);
1386 // Some insets are numbered, others are shown in the outline pane so
1387 // let's update the labels and the toc backend.
1388 cur.forceBufferUpdate();
1391 if (arg == "tone-falling")
1392 ipaChar(cur, InsetIPAChar::TONE_FALLING);
1393 else if (arg == "tone-rising")
1394 ipaChar(cur, InsetIPAChar::TONE_RISING);
1395 else if (arg == "tone-high-rising")
1396 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING);
1397 else if (arg == "tone-low-rising")
1398 ipaChar(cur, InsetIPAChar::TONE_LOW_RISING);
1399 else if (arg == "tone-high-rising-falling")
1400 ipaChar(cur, InsetIPAChar::TONE_HIGH_RISING_FALLING);
1401 else if (arg.empty())
1402 lyxerr << "LyX function 'ipamacro-insert' needs an argument." << endl;
1404 lyxerr << "Wrong argument for LyX function 'ipamacro-insert'." << endl;
1408 case LFUN_WORD_UPCASE:
1409 changeCase(cur, text_uppercase, cmd.getArg(0) == "partial");
1412 case LFUN_WORD_LOWCASE:
1413 changeCase(cur, text_lowercase, cmd.getArg(0) == "partial");
1416 case LFUN_WORD_CAPITALIZE:
1417 changeCase(cur, text_capitalization, cmd.getArg(0) == "partial");
1420 case LFUN_CHARS_TRANSPOSE:
1421 charsTranspose(cur);
1425 cur.message(_("Paste"));
1426 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), break);
1427 cap::replaceSelection(cur);
1429 // without argument?
1430 string const arg = to_utf8(cmd.argument());
1432 bool tryGraphics = true;
1433 if (theClipboard().isInternal())
1434 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1435 else if (theClipboard().hasTextContents()) {
1436 if (pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1437 true, Clipboard::AnyTextType))
1438 tryGraphics = false;
1440 if (tryGraphics && theClipboard().hasGraphicsContents())
1441 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1442 } else if (isStrUnsignedInt(arg)) {
1443 // we have a numerical argument
1444 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1445 convert<unsigned int>(arg));
1446 } else if (arg == "html" || arg == "latex") {
1447 Clipboard::TextType type = (arg == "html") ?
1448 Clipboard::HtmlTextType : Clipboard::LaTeXTextType;
1449 pasteClipboardText(cur, bv->buffer().errorList("Paste"), true, type);
1451 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1453 type = Clipboard::PdfGraphicsType;
1454 else if (arg == "png")
1455 type = Clipboard::PngGraphicsType;
1456 else if (arg == "jpeg")
1457 type = Clipboard::JpegGraphicsType;
1458 else if (arg == "linkback")
1459 type = Clipboard::LinkBackGraphicsType;
1460 else if (arg == "emf")
1461 type = Clipboard::EmfGraphicsType;
1462 else if (arg == "wmf")
1463 type = Clipboard::WmfGraphicsType;
1465 // we also check in getStatus()
1466 LYXERR0("Unrecognized graphics type: " << arg);
1468 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1471 bv->buffer().errors("Paste");
1472 cur.clearSelection(); // bug 393
1478 cutSelection(cur, true, true);
1479 cur.message(_("Cut"));
1484 cur.message(_("Copy"));
1487 case LFUN_SERVER_GET_XY:
1488 cur.message(from_utf8(
1489 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1490 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1493 case LFUN_SERVER_SET_XY: {
1496 istringstream is(to_utf8(cmd.argument()));
1499 lyxerr << "SETXY: Could not parse coordinates in '"
1500 << to_utf8(cmd.argument()) << endl;
1502 tm->setCursorFromCoordinates(cur, x, y);
1506 case LFUN_SERVER_GET_LAYOUT:
1507 cur.message(cur.paragraph().layout().name());
1511 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
1512 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
1513 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1515 Paragraph const & para = cur.paragraph();
1516 docstring const old_layout = para.layout().name();
1517 DocumentClass const & tclass = bv->buffer().params().documentClass();
1520 layout = tclass.defaultLayoutName();
1522 if (owner_->forcePlainLayout())
1523 // in this case only the empty layout is allowed
1524 layout = tclass.plainLayoutName();
1525 else if (para.usePlainLayout()) {
1526 // in this case, default layout maps to empty layout
1527 if (layout == tclass.defaultLayoutName())
1528 layout = tclass.plainLayoutName();
1530 // otherwise, the empty layout maps to the default
1531 if (layout == tclass.plainLayoutName())
1532 layout = tclass.defaultLayoutName();
1535 bool hasLayout = tclass.hasLayout(layout);
1537 // If the entry is obsolete, use the new one instead.
1539 docstring const & obs = tclass[layout].obsoleted_by();
1545 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1546 from_utf8(N_(" not known")));
1550 bool change_layout = (old_layout != layout);
1552 if (!change_layout && cur.selection() &&
1553 cur.selBegin().pit() != cur.selEnd().pit())
1555 pit_type spit = cur.selBegin().pit();
1556 pit_type epit = cur.selEnd().pit() + 1;
1557 while (spit != epit) {
1558 if (pars_[spit].layout().name() != old_layout) {
1559 change_layout = true;
1566 if (change_layout) {
1567 setLayout(cur, layout);
1568 if (cur.pit() > 0 && !ignoreautonests) {
1569 set<docstring> const & autonests =
1570 pars_[cur.pit() - 1].layout().autonests();
1571 set<docstring> const & autonested =
1572 pars_[cur.pit()].layout().isAutonestedBy();
1573 if (autonests.find(layout) != autonests.end()
1574 || autonested.find(old_layout) != autonested.end())
1575 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1579 Layout::LaTeXArgMap args = tclass[layout].args();
1580 Layout::LaTeXArgMap::const_iterator lait = args.begin();
1581 Layout::LaTeXArgMap::const_iterator const laend = args.end();
1582 for (; lait != laend; ++lait) {
1583 Layout::latexarg arg = (*lait).second;
1584 if (arg.autoinsert) {
1585 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first);
1586 lyx::dispatch(cmd2);
1593 case LFUN_ENVIRONMENT_SPLIT: {
1594 bool const outer = cmd.argument() == "outer";
1595 bool const previous = cmd.argument() == "previous";
1596 bool const before = cmd.argument() == "before";
1597 bool const normal = cmd.argument().empty();
1598 Paragraph const & para = cur.paragraph();
1600 if (para.layout().isEnvironment())
1601 layout = para.layout().name();
1602 depth_type split_depth = cur.paragraph().params().depth();
1603 depth_type nextpar_depth = 0;
1604 if (outer || previous) {
1605 // check if we have an environment in our scope
1606 pit_type pit = cur.pit();
1607 Paragraph cpar = pars_[pit];
1613 if (layout.empty() && previous
1614 && cpar.layout().isEnvironment()
1615 && cpar.params().depth() <= split_depth)
1616 layout = cpar.layout().name();
1617 if (cpar.params().depth() < split_depth
1618 && cpar.layout().isEnvironment()) {
1620 layout = cpar.layout().name();
1621 split_depth = cpar.params().depth();
1623 if (cpar.params().depth() == 0)
1627 if ((outer || normal) && cur.pit() < cur.lastpit()) {
1628 // save nesting of following paragraph
1629 Paragraph cpar = pars_[cur.pit() + 1];
1630 nextpar_depth = cpar.params().depth();
1633 cur.top().setPitPos(cur.pit(), 0);
1634 if (before || cur.pos() > 0)
1635 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK));
1636 else if (previous && cur.nextInset() && cur.nextInset()->lyxCode() == SEPARATOR_CODE)
1637 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1639 while (cur.paragraph().params().depth() > split_depth)
1640 lyx::dispatch(FuncRequest(LFUN_DEPTH_DECREMENT));
1642 DocumentClass const & tc = bv->buffer().params().documentClass();
1643 lyx::dispatch(FuncRequest(LFUN_LAYOUT, from_ascii("\"") + tc.plainLayout().name()
1644 + from_ascii("\" ignoreautonests")));
1645 // FIXME: Bibitem mess!
1646 if (cur.prevInset() && cur.prevInset()->lyxCode() == BIBITEM_CODE)
1647 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_BACKWARD));
1648 lyx::dispatch(FuncRequest(LFUN_SEPARATOR_INSERT, "plain"));
1651 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse ignoresep"));
1652 while (cur.paragraph().params().depth() < split_depth)
1653 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1656 lyx::dispatch(FuncRequest(LFUN_PARAGRAPH_BREAK, "inverse"));
1657 lyx::dispatch(FuncRequest(LFUN_LAYOUT, layout));
1658 if ((outer || normal) && nextpar_depth > 0) {
1659 // restore nesting of following paragraph
1660 DocIterator scur = cur;
1661 depth_type const max_depth = cur.paragraph().params().depth() + 1;
1663 while (cur.paragraph().params().depth() < min(nextpar_depth, max_depth))
1664 lyx::dispatch(FuncRequest(LFUN_DEPTH_INCREMENT));
1665 cur.setCursor(scur);
1671 case LFUN_CLIPBOARD_PASTE:
1672 cap::replaceSelection(cur);
1673 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1674 cmd.argument() == "paragraph");
1675 bv->buffer().errors("Paste");
1678 case LFUN_CLIPBOARD_PASTE_SIMPLE:
1679 cap::replaceSelection(cur);
1680 pasteSimpleText(cur, cmd.argument() == "paragraph");
1683 case LFUN_PRIMARY_SELECTION_PASTE:
1684 cap::replaceSelection(cur);
1685 pasteString(cur, theSelection().get(),
1686 cmd.argument() == "paragraph");
1689 case LFUN_SELECTION_PASTE:
1690 // Copy the selection buffer to the clipboard stack,
1691 // because we want it to appear in the "Edit->Paste
1693 cap::replaceSelection(cur);
1694 cap::copySelectionToStack();
1695 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1696 bv->buffer().errors("Paste");
1699 case LFUN_UNICODE_INSERT: {
1700 if (cmd.argument().empty())
1704 docstring const arg = from_utf8(cmd.getArg(i));
1708 LYXERR0("Not a hexstring: " << arg);
1712 char_type c = hexToInt(arg);
1713 if (c >= 32 && c < 0x10ffff) {
1714 LYXERR(Debug::KEY, "Inserting c: " << c);
1715 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, docstring(1, c)));
1722 case LFUN_QUOTE_INSERT: {
1723 cap::replaceSelection(cur);
1726 Paragraph const & par = cur.paragraph();
1727 pos_type pos = cur.pos();
1728 // Ignore deleted text before cursor
1729 while (pos > 0 && par.isDeleted(pos - 1))
1732 bool const inner = (cmd.getArg(0) == "single" || cmd.getArg(0) == "inner");
1734 // Guess quote side.
1735 // A space triggers an opening quote. This is passed if the preceding
1736 // char/inset is a space or at paragraph start.
1738 if (pos > 0 && !par.isSpace(pos - 1)) {
1739 if (cur.prevInset() && cur.prevInset()->lyxCode() == QUOTE_CODE) {
1740 // If an opening double quotation mark precedes, and this
1741 // is a single quote, make it opening as well
1743 static_cast<InsetQuotes &>(*cur.prevInset());
1744 string const type = ins.getType();
1745 if (!suffixIs(type, "ld") || !inner)
1746 c = par.getChar(pos - 1);
1748 else if (!cur.prevInset()
1749 || (cur.prevInset() && cur.prevInset()->isChar()))
1750 // If a char precedes, pass that and let InsetQuote decide
1751 c = par.getChar(pos - 1);
1754 if (par.getInset(pos - 1)
1755 && !par.getInset(pos - 1)->isPartOfTextSequence()) {
1756 // skip "invisible" insets
1760 c = par.getChar(pos - 1);
1765 InsetQuotesParams::QuoteLevel const quote_level = inner
1766 ? InsetQuotesParams::SecondaryQuotes : InsetQuotesParams::PrimaryQuotes;
1767 cur.insert(new InsetQuotes(cur.buffer(), c, quote_level, cmd.getArg(1), cmd.getArg(2)));
1768 cur.buffer()->updateBuffer();
1773 case LFUN_DATE_INSERT: {
1774 string const format = cmd.argument().empty()
1775 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1776 string const time = formatted_time(current_time(), format);
1777 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1781 case LFUN_MOUSE_TRIPLE:
1782 if (cmd.button() == mouse_button::button1) {
1783 tm->cursorHome(cur);
1791 case LFUN_MOUSE_DOUBLE:
1792 if (cmd.button() == mouse_button::button1) {
1793 selectWord(cur, WHOLE_WORD);
1798 // Single-click on work area
1799 case LFUN_MOUSE_PRESS: {
1800 // We are not marking a selection with the keyboard in any case.
1801 Cursor & bvcur = cur.bv().cursor();
1802 bvcur.setMark(false);
1803 switch (cmd.button()) {
1804 case mouse_button::button1:
1805 if (!bvcur.selection())
1807 bvcur.resetAnchor();
1808 if (!bv->mouseSetCursor(cur, cmd.modifier() == ShiftModifier))
1809 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1810 if (bvcur.wordSelection())
1811 selectWord(bvcur, WHOLE_WORD);
1814 case mouse_button::button2:
1815 if (lyxrc.mouse_middlebutton_paste) {
1816 // Middle mouse pasting.
1817 bv->mouseSetCursor(cur);
1819 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1820 "selection-paste ; primary-selection-paste paragraph"));
1822 cur.noScreenUpdate();
1825 case mouse_button::button3: {
1826 // Don't do anything if we right-click a
1827 // selection, a context menu will popup.
1828 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1829 && cur < bvcur.selectionEnd()) {
1830 cur.noScreenUpdate();
1833 if (!bv->mouseSetCursor(cur, false))
1834 cur.screenUpdateFlags(Update::FitCursor);
1840 } // switch (cmd.button())
1843 case LFUN_MOUSE_MOTION: {
1844 // Mouse motion with right or middle mouse do nothing for now.
1845 if (cmd.button() != mouse_button::button1) {
1846 cur.noScreenUpdate();
1849 // ignore motions deeper nested than the real anchor
1850 Cursor & bvcur = cur.bv().cursor();
1851 if (!bvcur.realAnchor().hasPart(cur)) {
1855 CursorSlice old = bvcur.top();
1857 int const wh = bv->workHeight();
1858 int const y = max(0, min(wh - 1, cmd.y()));
1860 tm->setCursorFromCoordinates(cur, cmd.x(), y);
1861 cur.setTargetX(cmd.x());
1862 // Don't allow selecting a separator inset
1863 if (cur.pos() && cur.paragraph().isEnvSeparator(cur.pos() - 1))
1866 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1867 else if (cmd.y() < 0)
1868 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1869 // This is to allow jumping over large insets
1870 if (cur.top() == old) {
1872 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1873 else if (cmd.y() < 0)
1874 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1876 // We continue with our existing selection or start a new one, so don't
1877 // reset the anchor.
1878 bvcur.setCursor(cur);
1879 bvcur.selection(true);
1880 bvcur.setCurrentFont();
1881 if (cur.top() == old) {
1882 // We didn't move one iota, so no need to update the screen.
1883 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1884 //cur.noScreenUpdate();
1890 case LFUN_MOUSE_RELEASE:
1891 switch (cmd.button()) {
1892 case mouse_button::button1:
1893 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1894 // If there is a new selection, update persistent selection;
1895 // otherwise, single click does not clear persistent selection
1897 if (cur.selection()) {
1898 // Finish selection. If double click,
1899 // cur is moved to the end of word by
1900 // selectWord but bvcur is current
1902 cur.bv().cursor().setSelection();
1903 // We might have removed an empty but drawn selection
1904 // (probably a margin)
1905 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
1907 cur.noScreenUpdate();
1908 // FIXME: We could try to handle drag and drop of selection here.
1911 case mouse_button::button2:
1912 // Middle mouse pasting is handled at mouse press time,
1913 // see LFUN_MOUSE_PRESS.
1914 cur.noScreenUpdate();
1917 case mouse_button::button3:
1918 // Cursor was set at LFUN_MOUSE_PRESS time.
1919 // FIXME: If there is a selection we could try to handle a special
1920 // drag & drop context menu.
1921 cur.noScreenUpdate();
1924 case mouse_button::none:
1925 case mouse_button::button4:
1926 case mouse_button::button5:
1928 } // switch (cmd.button())
1932 case LFUN_SELF_INSERT: {
1933 if (cmd.argument().empty())
1936 // Automatically delete the currently selected
1937 // text and replace it with what is being
1938 // typed in now. Depends on lyxrc settings
1939 // "auto_region_delete", which defaults to
1942 if (lyxrc.auto_region_delete && cur.selection())
1943 cutSelection(cur, false, false);
1945 cur.clearSelection();
1947 docstring::const_iterator cit = cmd.argument().begin();
1948 docstring::const_iterator const end = cmd.argument().end();
1949 for (; cit != end; ++cit)
1950 bv->translateAndInsert(*cit, this, cur);
1953 moveCursor(cur, false);
1954 cur.markNewWordPosition();
1955 bv->bookmarkEditPosition();
1959 case LFUN_HREF_INSERT: {
1960 docstring content = cmd.argument();
1961 if (content.empty() && cur.selection())
1962 content = cur.selectionAsString(false);
1964 InsetCommandParams p(HYPERLINK_CODE);
1965 if (!content.empty()){
1966 // if it looks like a link, we'll put it as target,
1967 // otherwise as name (bug #8792).
1970 // regex_match(to_utf8(content), matches, link_re)
1971 // because smatch stores pointers to the substrings rather
1972 // than making copies of them. And those pointers become
1973 // invalid after regex_match returns, since it is then
1974 // being given a temporary object. (Thanks to Georg for
1975 // figuring that out.)
1976 regex const link_re("^([a-z]+):.*");
1978 string const c = to_utf8(lowercase(content));
1980 if (c.substr(0,7) == "mailto:") {
1981 p["target"] = content;
1982 p["type"] = from_ascii("mailto:");
1983 } else if (regex_match(c, matches, link_re)) {
1984 p["target"] = content;
1985 string protocol = matches.str(1);
1986 if (protocol == "file")
1987 p["type"] = from_ascii("file:");
1989 p["name"] = content;
1991 string const data = InsetCommand::params2string(p);
1993 // we need to have a target. if we already have one, then
1994 // that gets used at the default for the name, too, which
1995 // is probably what is wanted.
1996 if (p["target"].empty()) {
1997 bv->showDialog("href", data);
1999 FuncRequest fr(LFUN_INSET_INSERT, data);
2005 case LFUN_LABEL_INSERT: {
2006 InsetCommandParams p(LABEL_CODE);
2007 // Try to generate a valid label
2008 p["name"] = (cmd.argument().empty()) ?
2009 cur.getPossibleLabel() :
2011 string const data = InsetCommand::params2string(p);
2013 if (cmd.argument().empty()) {
2014 bv->showDialog("label", data);
2016 FuncRequest fr(LFUN_INSET_INSERT, data);
2022 case LFUN_INFO_INSERT: {
2024 if (cmd.argument().empty() && cur.selection()) {
2025 // if command argument is empty use current selection as parameter.
2026 docstring ds = cur.selectionAsString(false);
2027 cutSelection(cur, true, false);
2028 FuncRequest cmd0(cmd, ds);
2029 inset = createInset(cur.buffer(), cmd0);
2031 inset = createInset(cur.buffer(), cmd);
2036 insertInset(cur, inset);
2037 cur.forceBufferUpdate();
2041 case LFUN_CAPTION_INSERT:
2042 case LFUN_FOOTNOTE_INSERT:
2043 case LFUN_NOTE_INSERT:
2044 case LFUN_BOX_INSERT:
2045 case LFUN_BRANCH_INSERT:
2046 case LFUN_PHANTOM_INSERT:
2047 case LFUN_ERT_INSERT:
2048 case LFUN_LISTING_INSERT:
2049 case LFUN_MARGINALNOTE_INSERT:
2050 case LFUN_ARGUMENT_INSERT:
2051 case LFUN_INDEX_INSERT:
2052 case LFUN_PREVIEW_INSERT:
2053 case LFUN_SCRIPT_INSERT:
2054 case LFUN_IPA_INSERT:
2055 // Open the inset, and move the current selection
2057 doInsertInset(cur, this, cmd, true, true);
2059 // Some insets are numbered, others are shown in the outline pane so
2060 // let's update the labels and the toc backend.
2061 cur.forceBufferUpdate();
2064 case LFUN_FLEX_INSERT: {
2065 // Open the inset, and move the current selection
2067 bool const sel = cur.selection();
2068 doInsertInset(cur, this, cmd, true, true);
2069 // Insert auto-insert arguments
2070 bool autoargs = false;
2071 Layout::LaTeXArgMap args = cur.inset().getLayout().latexargs();
2072 Layout::LaTeXArgMap::const_iterator lait = args.begin();
2073 Layout::LaTeXArgMap::const_iterator const laend = args.end();
2074 for (; lait != laend; ++lait) {
2075 Layout::latexarg arg = (*lait).second;
2076 if (arg.autoinsert) {
2077 // The cursor might have been invalidated by the replaceSelection.
2078 cur.buffer()->changed(true);
2079 FuncRequest cmd2(LFUN_ARGUMENT_INSERT, (*lait).first);
2080 lyx::dispatch(cmd2);
2086 cur.leaveInset(cur.inset());
2089 // Some insets are numbered, others are shown in the outline pane so
2090 // let's update the labels and the toc backend.
2091 cur.forceBufferUpdate();
2095 case LFUN_TABULAR_INSERT:
2096 // if there were no arguments, just open the dialog
2097 if (doInsertInset(cur, this, cmd, false, true))
2100 bv->showDialog("tabularcreate");
2104 case LFUN_FLOAT_INSERT:
2105 case LFUN_FLOAT_WIDE_INSERT:
2106 case LFUN_WRAP_INSERT: {
2107 // will some content be moved into the inset?
2108 bool const content = cur.selection();
2109 // does the content consist of multiple paragraphs?
2110 bool const singlepar = (cur.selBegin().pit() == cur.selEnd().pit());
2112 doInsertInset(cur, this, cmd, true, true);
2115 // If some single-par content is moved into the inset,
2116 // doInsertInset puts the cursor outside the inset.
2117 // To insert the caption we put it back into the inset.
2118 // FIXME cleanup doInsertInset to avoid such dances!
2119 if (content && singlepar)
2122 ParagraphList & pars = cur.text()->paragraphs();
2124 DocumentClass const & tclass = bv->buffer().params().documentClass();
2126 // add a separate paragraph for the caption inset
2127 pars.push_back(Paragraph());
2128 pars.back().setInsetOwner(&cur.text()->inset());
2129 pars.back().setPlainOrDefaultLayout(tclass);
2130 int cap_pit = pars.size() - 1;
2132 // if an empty inset was created, we create an additional empty
2133 // paragraph at the bottom so that the user can choose where to put
2134 // the graphics (or table).
2136 pars.push_back(Paragraph());
2137 pars.back().setInsetOwner(&cur.text()->inset());
2138 pars.back().setPlainOrDefaultLayout(tclass);
2141 // reposition the cursor to the caption
2142 cur.pit() = cap_pit;
2144 // FIXME: This Text/Cursor dispatch handling is a mess!
2145 // We cannot use Cursor::dispatch here it needs access to up to
2147 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
2148 doInsertInset(cur, cur.text(), cmd_caption, true, false);
2149 cur.forceBufferUpdate();
2150 cur.screenUpdateFlags(Update::Force);
2151 // FIXME: When leaving the Float (or Wrap) inset we should
2152 // delete any empty paragraph left above or below the
2157 case LFUN_NOMENCL_INSERT: {
2158 InsetCommandParams p(NOMENCL_CODE);
2159 if (cmd.argument().empty())
2160 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
2162 p["symbol"] = cmd.argument();
2163 string const data = InsetCommand::params2string(p);
2164 bv->showDialog("nomenclature", data);
2168 case LFUN_INDEX_PRINT: {
2169 InsetCommandParams p(INDEX_PRINT_CODE);
2170 if (cmd.argument().empty())
2171 p["type"] = from_ascii("idx");
2173 p["type"] = cmd.argument();
2174 string const data = InsetCommand::params2string(p);
2175 FuncRequest fr(LFUN_INSET_INSERT, data);
2180 case LFUN_NOMENCL_PRINT:
2181 case LFUN_NEWPAGE_INSERT:
2183 doInsertInset(cur, this, cmd, false, false);
2187 case LFUN_SEPARATOR_INSERT: {
2188 doInsertInset(cur, this, cmd, false, false);
2190 // remove a following space
2191 Paragraph & par = cur.paragraph();
2192 if (cur.pos() != cur.lastpos() && par.isLineSeparator(cur.pos()))
2193 par.eraseChar(cur.pos(), cur.buffer()->params().track_changes);
2197 case LFUN_DEPTH_DECREMENT:
2198 changeDepth(cur, DEC_DEPTH);
2201 case LFUN_DEPTH_INCREMENT:
2202 changeDepth(cur, INC_DEPTH);
2205 case LFUN_REGEXP_MODE:
2206 regexpDispatch(cur, cmd);
2209 case LFUN_MATH_MODE: {
2210 if (cmd.argument() == "on" || cmd.argument() == "") {
2211 // don't pass "on" as argument
2212 // (it would appear literally in the first cell)
2213 docstring sel = cur.selectionAsString(false);
2214 InsetMathMacroTemplate * macro = new InsetMathMacroTemplate(cur.buffer());
2215 // create a macro template if we see "\\newcommand" somewhere, and
2216 // an ordinary formula otherwise
2218 && (sel.find(from_ascii("\\newcommand")) != string::npos
2219 || sel.find(from_ascii("\\newlyxcommand")) != string::npos
2220 || sel.find(from_ascii("\\def")) != string::npos)
2221 && macro->fromString(sel)) {
2223 replaceSelection(cur);
2226 // no meaningful macro template was found
2228 mathDispatch(cur,FuncRequest(LFUN_MATH_MODE));
2231 // The argument is meaningful
2232 // We replace cmd with LFUN_MATH_INSERT because LFUN_MATH_MODE
2233 // has a different meaning in math mode
2234 mathDispatch(cur, FuncRequest(LFUN_MATH_INSERT,cmd.argument()));
2238 case LFUN_MATH_MACRO:
2239 if (cmd.argument().empty())
2240 cur.errorMessage(from_utf8(N_("Missing argument")));
2243 string s = to_utf8(cmd.argument());
2244 string const s1 = token(s, ' ', 1);
2245 int const nargs = s1.empty() ? 0 : convert<int>(s1);
2246 string const s2 = token(s, ' ', 2);
2247 MacroType type = MacroTypeNewcommand;
2249 type = MacroTypeDef;
2250 InsetMathMacroTemplate * inset = new InsetMathMacroTemplate(cur.buffer(),
2251 from_utf8(token(s, ' ', 0)), nargs, false, type);
2252 inset->setBuffer(bv->buffer());
2253 insertInset(cur, inset);
2255 // enter macro inset and select the name
2257 cur.top().pos() = cur.top().lastpos();
2259 cur.selection(true);
2260 cur.top().pos() = 0;
2264 case LFUN_MATH_DISPLAY:
2265 case LFUN_MATH_SUBSCRIPT:
2266 case LFUN_MATH_SUPERSCRIPT:
2267 case LFUN_MATH_INSERT:
2268 case LFUN_MATH_AMS_MATRIX:
2269 case LFUN_MATH_MATRIX:
2270 case LFUN_MATH_DELIM:
2271 case LFUN_MATH_BIGDELIM:
2272 mathDispatch(cur, cmd);
2275 case LFUN_FONT_EMPH: {
2276 Font font(ignore_font, ignore_language);
2277 font.fontInfo().setEmph(FONT_TOGGLE);
2278 toggleAndShow(cur, this, font);
2282 case LFUN_FONT_ITAL: {
2283 Font font(ignore_font, ignore_language);
2284 font.fontInfo().setShape(ITALIC_SHAPE);
2285 toggleAndShow(cur, this, font);
2289 case LFUN_FONT_BOLD:
2290 case LFUN_FONT_BOLDSYMBOL: {
2291 Font font(ignore_font, ignore_language);
2292 font.fontInfo().setSeries(BOLD_SERIES);
2293 toggleAndShow(cur, this, font);
2297 case LFUN_FONT_NOUN: {
2298 Font font(ignore_font, ignore_language);
2299 font.fontInfo().setNoun(FONT_TOGGLE);
2300 toggleAndShow(cur, this, font);
2304 case LFUN_FONT_TYPEWRITER: {
2305 Font font(ignore_font, ignore_language);
2306 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
2307 toggleAndShow(cur, this, font);
2311 case LFUN_FONT_SANS: {
2312 Font font(ignore_font, ignore_language);
2313 font.fontInfo().setFamily(SANS_FAMILY);
2314 toggleAndShow(cur, this, font);
2318 case LFUN_FONT_ROMAN: {
2319 Font font(ignore_font, ignore_language);
2320 font.fontInfo().setFamily(ROMAN_FAMILY);
2321 toggleAndShow(cur, this, font);
2325 case LFUN_FONT_DEFAULT: {
2326 Font font(inherit_font, ignore_language);
2327 toggleAndShow(cur, this, font);
2331 case LFUN_FONT_STRIKEOUT: {
2332 Font font(ignore_font, ignore_language);
2333 font.fontInfo().setStrikeout(FONT_TOGGLE);
2334 toggleAndShow(cur, this, font);
2338 case LFUN_FONT_CROSSOUT: {
2339 Font font(ignore_font, ignore_language);
2340 font.fontInfo().setXout(FONT_TOGGLE);
2341 toggleAndShow(cur, this, font);
2345 case LFUN_FONT_UNDERUNDERLINE: {
2346 Font font(ignore_font, ignore_language);
2347 font.fontInfo().setUuline(FONT_TOGGLE);
2348 toggleAndShow(cur, this, font);
2352 case LFUN_FONT_UNDERWAVE: {
2353 Font font(ignore_font, ignore_language);
2354 font.fontInfo().setUwave(FONT_TOGGLE);
2355 toggleAndShow(cur, this, font);
2359 case LFUN_FONT_UNDERLINE: {
2360 Font font(ignore_font, ignore_language);
2361 font.fontInfo().setUnderbar(FONT_TOGGLE);
2362 toggleAndShow(cur, this, font);
2366 case LFUN_FONT_SIZE: {
2367 Font font(ignore_font, ignore_language);
2368 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
2369 toggleAndShow(cur, this, font);
2373 case LFUN_LANGUAGE: {
2374 string const lang_arg = cmd.getArg(0);
2375 bool const reset = (lang_arg.empty() || lang_arg == "reset");
2376 Language const * lang =
2377 reset ? reset_language
2378 : languages.getLanguage(lang_arg);
2379 // we allow reset_language, which is 0, but only if it
2380 // was requested via empty or "reset" arg.
2381 if (!lang && !reset)
2383 bool const toggle = (cmd.getArg(1) != "set");
2384 selectWordWhenUnderCursor(cur, WHOLE_WORD_STRICT);
2385 Font font(ignore_font, lang);
2386 toggleAndShow(cur, this, font, toggle);
2390 case LFUN_TEXTSTYLE_APPLY:
2391 toggleAndShow(cur, this, freefont, toggleall);
2392 cur.message(_("Character set"));
2395 // Set the freefont using the contents of \param data dispatched from
2396 // the frontends and apply it at the current cursor location.
2397 case LFUN_TEXTSTYLE_UPDATE: {
2400 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
2403 toggleAndShow(cur, this, freefont, toggleall);
2404 cur.message(_("Character set"));
2406 lyxerr << "Argument not ok";
2411 case LFUN_FINISHED_LEFT:
2412 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
2413 // We're leaving an inset, going left. If the inset is LTR, we're
2414 // leaving from the front, so we should not move (remain at --- but
2415 // not in --- the inset). If the inset is RTL, move left, without
2416 // entering the inset itself; i.e., move to after the inset.
2417 if (cur.paragraph().getFontSettings(
2418 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2419 cursorVisLeft(cur, true);
2422 case LFUN_FINISHED_RIGHT:
2423 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
2424 // We're leaving an inset, going right. If the inset is RTL, we're
2425 // leaving from the front, so we should not move (remain at --- but
2426 // not in --- the inset). If the inset is LTR, move right, without
2427 // entering the inset itself; i.e., move to after the inset.
2428 if (!cur.paragraph().getFontSettings(
2429 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
2430 cursorVisRight(cur, true);
2433 case LFUN_FINISHED_BACKWARD:
2434 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2435 cur.setCurrentFont();
2438 case LFUN_FINISHED_FORWARD:
2439 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2441 cur.setCurrentFont();
2444 case LFUN_LAYOUT_PARAGRAPH: {
2446 params2string(cur.paragraph(), data);
2447 data = "show\n" + data;
2448 bv->showDialog("paragraph", data);
2452 case LFUN_PARAGRAPH_UPDATE: {
2454 params2string(cur.paragraph(), data);
2456 // Will the paragraph accept changes from the dialog?
2458 cur.inset().allowParagraphCustomization(cur.idx());
2460 data = "update " + convert<string>(accept) + '\n' + data;
2461 bv->updateDialog("paragraph", data);
2465 case LFUN_ACCENT_UMLAUT:
2466 case LFUN_ACCENT_CIRCUMFLEX:
2467 case LFUN_ACCENT_GRAVE:
2468 case LFUN_ACCENT_ACUTE:
2469 case LFUN_ACCENT_TILDE:
2470 case LFUN_ACCENT_PERISPOMENI:
2471 case LFUN_ACCENT_CEDILLA:
2472 case LFUN_ACCENT_MACRON:
2473 case LFUN_ACCENT_DOT:
2474 case LFUN_ACCENT_UNDERDOT:
2475 case LFUN_ACCENT_UNDERBAR:
2476 case LFUN_ACCENT_CARON:
2477 case LFUN_ACCENT_BREVE:
2478 case LFUN_ACCENT_TIE:
2479 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2480 case LFUN_ACCENT_CIRCLE:
2481 case LFUN_ACCENT_OGONEK:
2482 theApp()->handleKeyFunc(cmd.action());
2483 if (!cmd.argument().empty())
2484 // FIXME: Are all these characters encoded in one byte in utf8?
2485 bv->translateAndInsert(cmd.argument()[0], this, cur);
2486 cur.screenUpdateFlags(Update::FitCursor);
2489 case LFUN_FLOAT_LIST_INSERT: {
2490 DocumentClass const & tclass = bv->buffer().params().documentClass();
2491 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2493 if (cur.selection())
2494 cutSelection(cur, true, false);
2495 breakParagraph(cur);
2497 if (cur.lastpos() != 0) {
2498 cursorBackward(cur);
2499 breakParagraph(cur);
2502 docstring const laystr = cur.inset().usePlainLayout() ?
2503 tclass.plainLayoutName() :
2504 tclass.defaultLayoutName();
2505 setLayout(cur, laystr);
2506 ParagraphParameters p;
2507 // FIXME If this call were replaced with one to clearParagraphParams(),
2508 // then we could get rid of this method altogether.
2509 setParagraphs(cur, p);
2510 // FIXME This should be simplified when InsetFloatList takes a
2511 // Buffer in its constructor.
2512 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
2513 ifl->setBuffer(bv->buffer());
2514 insertInset(cur, ifl);
2517 lyxerr << "Non-existent float type: "
2518 << to_utf8(cmd.argument()) << endl;
2523 case LFUN_CHANGE_ACCEPT: {
2524 acceptOrRejectChanges(cur, ACCEPT);
2528 case LFUN_CHANGE_REJECT: {
2529 acceptOrRejectChanges(cur, REJECT);
2533 case LFUN_THESAURUS_ENTRY: {
2534 Language const * language = cur.getFont().language();
2535 docstring arg = cmd.argument();
2537 arg = cur.selectionAsString(false);
2539 if (arg.size() > 100 || arg.empty()) {
2540 // Get word or selection
2541 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2542 arg = cur.selectionAsString(false);
2543 arg += " lang=" + from_ascii(language->lang());
2546 string lang = cmd.getArg(1);
2547 // This duplicates the code in GuiThesaurus::initialiseParams
2548 if (prefixIs(lang, "lang=")) {
2549 language = languages.getLanguage(lang.substr(5));
2551 language = cur.getFont().language();
2554 string lang = language->code();
2555 if (lyxrc.thesaurusdir_path.empty() && !thesaurus.thesaurusInstalled(from_ascii(lang))) {
2556 LYXERR(Debug::ACTION, "Command " << cmd << ". Thesaurus not found for language " << lang);
2557 frontend::Alert::warning(_("Path to thesaurus directory not set!"),
2558 _("The path to the thesaurus directory has not been specified.\n"
2559 "The thesaurus is not functional.\n"
2560 "Please refer to sec. 6.15.1 of the User's Guide for setup\n"
2563 bv->showDialog("thesaurus", to_utf8(arg));
2567 case LFUN_SPELLING_ADD: {
2568 Language const * language = getLanguage(cur, cmd.getArg(1));
2569 docstring word = from_utf8(cmd.getArg(0));
2571 word = cur.selectionAsString(false);
2573 if (word.size() > 100 || word.empty()) {
2574 // Get word or selection
2575 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2576 word = cur.selectionAsString(false);
2579 WordLangTuple wl(word, language);
2580 theSpellChecker()->insert(wl);
2584 case LFUN_SPELLING_IGNORE: {
2585 Language const * language = getLanguage(cur, cmd.getArg(1));
2586 docstring word = from_utf8(cmd.getArg(0));
2588 word = cur.selectionAsString(false);
2590 if (word.size() > 100 || word.empty()) {
2591 // Get word or selection
2592 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2593 word = cur.selectionAsString(false);
2596 WordLangTuple wl(word, language);
2597 theSpellChecker()->accept(wl);
2601 case LFUN_SPELLING_REMOVE: {
2602 Language const * language = getLanguage(cur, cmd.getArg(1));
2603 docstring word = from_utf8(cmd.getArg(0));
2605 word = cur.selectionAsString(false);
2607 if (word.size() > 100 || word.empty()) {
2608 // Get word or selection
2609 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2610 word = cur.selectionAsString(false);
2613 WordLangTuple wl(word, language);
2614 theSpellChecker()->remove(wl);
2618 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2619 // Given data, an encoding of the ParagraphParameters
2620 // generated in the Paragraph dialog, this function sets
2621 // the current paragraph, or currently selected paragraphs,
2623 // NOTE: This function overrides all existing settings.
2624 setParagraphs(cur, cmd.argument());
2625 cur.message(_("Paragraph layout set"));
2629 case LFUN_PARAGRAPH_PARAMS: {
2630 // Given data, an encoding of the ParagraphParameters as we'd
2631 // find them in a LyX file, this function modifies the current paragraph,
2632 // or currently selected paragraphs.
2633 // NOTE: This function only modifies, and does not override, existing
2635 setParagraphs(cur, cmd.argument(), true);
2636 cur.message(_("Paragraph layout set"));
2641 if (cur.selection()) {
2642 cur.selection(false);
2645 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2646 // correct, but I'm not 100% sure -- dov, 071019
2647 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2651 case LFUN_OUTLINE_UP:
2652 outline(OutlineUp, cur, this);
2653 setCursor(cur, cur.pit(), 0);
2654 cur.forceBufferUpdate();
2658 case LFUN_OUTLINE_DOWN:
2659 outline(OutlineDown, cur, this);
2660 setCursor(cur, cur.pit(), 0);
2661 cur.forceBufferUpdate();
2665 case LFUN_OUTLINE_IN:
2666 outline(OutlineIn, cur, this);
2667 cur.forceBufferUpdate();
2671 case LFUN_OUTLINE_OUT:
2672 outline(OutlineOut, cur, this);
2673 cur.forceBufferUpdate();
2677 case LFUN_SERVER_GET_STATISTICS:
2679 DocIterator from, to;
2680 if (cur.selection()) {
2681 from = cur.selectionBegin();
2682 to = cur.selectionEnd();
2684 from = doc_iterator_begin(cur.buffer());
2685 to = doc_iterator_end(cur.buffer());
2688 cur.buffer()->updateStatistics(from, to);
2689 string const arg0 = cmd.getArg(0);
2690 if (arg0 == "words") {
2691 cur.message(convert<docstring>(cur.buffer()->wordCount()));
2692 } else if (arg0 == "chars") {
2693 cur.message(convert<docstring>(cur.buffer()->charCount(false)));
2694 } else if (arg0 == "chars-space") {
2695 cur.message(convert<docstring>(cur.buffer()->charCount(true)));
2697 cur.message(convert<docstring>(cur.buffer()->wordCount()) + " "
2698 + convert<docstring>(cur.buffer()->charCount(false)) + " "
2699 + convert<docstring>(cur.buffer()->charCount(true)));
2705 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2710 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2712 if (lyxrc.spellcheck_continuously && !needsUpdate) {
2713 // Check for misspelled text
2714 // The redraw is useful because of the painting of
2715 // misspelled markers depends on the cursor position.
2716 // Trigger a redraw for cursor moves inside misspelled text.
2717 if (!cur.inTexted()) {
2718 // move from regular text to math
2719 needsUpdate = last_misspelled;
2720 } else if (oldTopSlice != cur.top() || oldBoundary != cur.boundary()) {
2721 // move inside regular text
2722 needsUpdate = last_misspelled
2723 || cur.paragraph().isMisspelled(cur.pos(), true);
2727 // FIXME: The cursor flag is reset two lines below
2728 // so we need to check here if some of the LFUN did touch that.
2729 // for now only Text::erase() and Text::backspace() do that.
2730 // The plan is to verify all the LFUNs and then to remove this
2731 // singleParUpdate boolean altogether.
2732 if (cur.result().screenUpdate() & Update::Force) {
2733 singleParUpdate = false;
2737 // FIXME: the following code should go in favor of fine grained
2738 // update flag treatment.
2739 if (singleParUpdate) {
2740 // Inserting characters does not change par height in general. So, try
2741 // to update _only_ this paragraph. BufferView will detect if a full
2742 // metrics update is needed anyway.
2743 cur.screenUpdateFlags(Update::SinglePar | Update::FitCursor);
2747 && &oldTopSlice.inset() == &cur.inset()
2748 && oldTopSlice.idx() == cur.idx()
2749 && !oldSelection // oldSelection is a backup of cur.selection() at the beginning of the function.
2750 && !cur.selection())
2751 // FIXME: it would be better if we could just do this
2753 //if (cur.result().update() != Update::FitCursor)
2754 // cur.noScreenUpdate();
2756 // But some LFUNs do not set Update::FitCursor when needed, so we
2757 // do it for all. This is not very harmfull as FitCursor will provoke
2758 // a full redraw only if needed but still, a proper review of all LFUN
2759 // should be done and this needsUpdate boolean can then be removed.
2760 cur.screenUpdateFlags(Update::FitCursor);
2762 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
2766 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2767 FuncStatus & flag) const
2769 LBUFERR(this == cur.text());
2771 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2773 bool allow_in_passthru = false;
2774 InsetCode code = NO_CODE;
2776 switch (cmd.action()) {
2778 case LFUN_DEPTH_DECREMENT:
2779 enable = changeDepthAllowed(cur, DEC_DEPTH);
2782 case LFUN_DEPTH_INCREMENT:
2783 enable = changeDepthAllowed(cur, INC_DEPTH);
2787 // FIXME We really should not allow this to be put, e.g.,
2788 // in a footnote, or in ERT. But it would make sense in a
2789 // branch, so I'm not sure what to do.
2790 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2793 case LFUN_DIALOG_SHOW_NEW_INSET:
2794 if (cmd.argument() == "bibitem")
2795 code = BIBITEM_CODE;
2796 else if (cmd.argument() == "bibtex") {
2798 // not allowed in description items
2799 enable = !inDescriptionItem(cur);
2801 else if (cmd.argument() == "box")
2803 else if (cmd.argument() == "branch")
2805 else if (cmd.argument() == "citation")
2807 else if (cmd.argument() == "ert")
2809 else if (cmd.argument() == "external")
2810 code = EXTERNAL_CODE;
2811 else if (cmd.argument() == "float")
2813 else if (cmd.argument() == "graphics")
2814 code = GRAPHICS_CODE;
2815 else if (cmd.argument() == "href")
2816 code = HYPERLINK_CODE;
2817 else if (cmd.argument() == "include")
2818 code = INCLUDE_CODE;
2819 else if (cmd.argument() == "index")
2821 else if (cmd.argument() == "index_print")
2822 code = INDEX_PRINT_CODE;
2823 else if (cmd.argument() == "listings")
2824 code = LISTINGS_CODE;
2825 else if (cmd.argument() == "mathspace")
2826 code = MATH_HULL_CODE;
2827 else if (cmd.argument() == "nomenclature")
2828 code = NOMENCL_CODE;
2829 else if (cmd.argument() == "nomencl_print")
2830 code = NOMENCL_PRINT_CODE;
2831 else if (cmd.argument() == "label")
2833 else if (cmd.argument() == "line")
2835 else if (cmd.argument() == "note")
2837 else if (cmd.argument() == "phantom")
2838 code = PHANTOM_CODE;
2839 else if (cmd.argument() == "ref")
2841 else if (cmd.argument() == "space")
2843 else if (cmd.argument() == "toc")
2845 else if (cmd.argument() == "vspace")
2847 else if (cmd.argument() == "wrap")
2851 case LFUN_ERT_INSERT:
2854 case LFUN_LISTING_INSERT:
2855 code = LISTINGS_CODE;
2856 // not allowed in description items
2857 enable = !inDescriptionItem(cur);
2859 case LFUN_FOOTNOTE_INSERT:
2862 case LFUN_TABULAR_INSERT:
2863 code = TABULAR_CODE;
2865 case LFUN_MARGINALNOTE_INSERT:
2868 case LFUN_FLOAT_INSERT:
2869 case LFUN_FLOAT_WIDE_INSERT:
2870 // FIXME: If there is a selection, we should check whether there
2871 // are floats in the selection, but this has performance issues, see
2872 // LFUN_CHANGE_ACCEPT/REJECT.
2874 if (inDescriptionItem(cur))
2875 // not allowed in description items
2878 InsetCode const inset_code = cur.inset().lyxCode();
2880 // algorithm floats cannot be put in another float
2881 if (to_utf8(cmd.argument()) == "algorithm") {
2882 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2886 // for figures and tables: only allow in another
2887 // float or wrap if it is of the same type and
2888 // not a subfloat already
2889 if(cur.inset().lyxCode() == code) {
2890 InsetFloat const & ins =
2891 static_cast<InsetFloat const &>(cur.inset());
2892 enable = ins.params().type == to_utf8(cmd.argument())
2893 && !ins.params().subfloat;
2894 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2895 InsetWrap const & ins =
2896 static_cast<InsetWrap const &>(cur.inset());
2897 enable = ins.params().type == to_utf8(cmd.argument());
2901 case LFUN_WRAP_INSERT:
2903 // not allowed in description items
2904 enable = !inDescriptionItem(cur);
2906 case LFUN_FLOAT_LIST_INSERT: {
2907 code = FLOAT_LIST_CODE;
2908 // not allowed in description items
2909 enable = !inDescriptionItem(cur);
2911 FloatList const & floats = cur.buffer()->params().documentClass().floats();
2912 FloatList::const_iterator cit = floats[to_ascii(cmd.argument())];
2913 // make sure we know about such floats
2914 if (cit == floats.end() ||
2915 // and that we know how to generate a list of them
2916 (!cit->second.usesFloatPkg() && cit->second.listCommand().empty())) {
2917 flag.setUnknown(true);
2918 // probably not necessary, but...
2924 case LFUN_CAPTION_INSERT: {
2925 code = CAPTION_CODE;
2926 string arg = cmd.getArg(0);
2927 bool varia = arg != "Unnumbered"
2928 && cur.inset().allowsCaptionVariation(arg);
2929 // not allowed in description items,
2930 // and in specific insets
2931 enable = !inDescriptionItem(cur)
2932 && (varia || arg.empty() || arg == "Standard");
2935 case LFUN_NOTE_INSERT:
2937 // in commands (sections etc.) and description items,
2938 // only Notes are allowed
2939 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2940 (!cur.paragraph().layout().isCommand()
2941 && !inDescriptionItem(cur)));
2943 case LFUN_FLEX_INSERT: {
2945 string s = cmd.getArg(0);
2947 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2948 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2949 il.lyxtype() != InsetLayout::CUSTOM &&
2950 il.lyxtype() != InsetLayout::ELEMENT &&
2951 il.lyxtype ()!= InsetLayout::STANDARD)
2955 case LFUN_BOX_INSERT:
2958 case LFUN_BRANCH_INSERT:
2960 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2961 && cur.buffer()->params().branchlist().empty())
2964 case LFUN_IPA_INSERT:
2967 case LFUN_PHANTOM_INSERT:
2968 code = PHANTOM_CODE;
2970 case LFUN_LABEL_INSERT:
2973 case LFUN_INFO_INSERT:
2976 case LFUN_ARGUMENT_INSERT: {
2978 allow_in_passthru = true;
2979 string const arg = cmd.getArg(0);
2984 Layout const & lay = cur.paragraph().layout();
2985 Layout::LaTeXArgMap args = lay.args();
2986 Layout::LaTeXArgMap::const_iterator const lait =
2988 if (lait != args.end()) {
2990 pit_type pit = cur.pit();
2991 pit_type lastpit = cur.pit();
2992 if (lay.isEnvironment() && !prefixIs(arg, "item:")) {
2993 // In a sequence of "merged" environment layouts, we only allow
2994 // non-item arguments once.
2995 lastpit = cur.lastpit();
2996 // get the first paragraph in sequence with this layout
2997 depth_type const current_depth = cur.paragraph().params().depth();
3001 Paragraph cpar = pars_[pit - 1];
3002 if (cpar.layout() == lay && cpar.params().depth() == current_depth)
3008 for (; pit <= lastpit; ++pit) {
3009 if (pars_[pit].layout() != lay)
3011 for (auto const & table : pars_[pit].insetList())
3012 if (InsetArgument const * ins = table.inset->asInsetArgument())
3013 if (ins->name() == arg) {
3014 // we have this already
3023 case LFUN_INDEX_INSERT:
3026 case LFUN_INDEX_PRINT:
3027 code = INDEX_PRINT_CODE;
3028 // not allowed in description items
3029 enable = !inDescriptionItem(cur);
3031 case LFUN_NOMENCL_INSERT:
3032 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3036 code = NOMENCL_CODE;
3038 case LFUN_NOMENCL_PRINT:
3039 code = NOMENCL_PRINT_CODE;
3040 // not allowed in description items
3041 enable = !inDescriptionItem(cur);
3043 case LFUN_HREF_INSERT:
3044 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
3048 code = HYPERLINK_CODE;
3050 case LFUN_IPAMACRO_INSERT: {
3051 string const arg = cmd.getArg(0);
3053 code = IPADECO_CODE;
3055 code = IPACHAR_CODE;
3058 case LFUN_QUOTE_INSERT:
3059 // always allow this, since we will inset a raw quote
3060 // if an inset is not allowed.
3061 allow_in_passthru = true;
3063 case LFUN_SPECIALCHAR_INSERT:
3064 code = SPECIALCHAR_CODE;
3066 case LFUN_SPACE_INSERT:
3067 // slight hack: we know this is allowed in math mode
3071 case LFUN_PREVIEW_INSERT:
3072 code = PREVIEW_CODE;
3074 case LFUN_SCRIPT_INSERT:
3078 case LFUN_MATH_INSERT:
3079 case LFUN_MATH_AMS_MATRIX:
3080 case LFUN_MATH_MATRIX:
3081 case LFUN_MATH_DELIM:
3082 case LFUN_MATH_BIGDELIM:
3083 case LFUN_MATH_DISPLAY:
3084 case LFUN_MATH_MODE:
3085 case LFUN_MATH_MACRO:
3086 case LFUN_MATH_SUBSCRIPT:
3087 case LFUN_MATH_SUPERSCRIPT:
3088 code = MATH_HULL_CODE;
3091 case LFUN_REGEXP_MODE:
3092 code = MATH_HULL_CODE;
3093 enable = cur.buffer()->isInternal() && !cur.inRegexped();
3096 case LFUN_INSET_MODIFY:
3097 // We need to disable this, because we may get called for a
3099 // InsetTabular::getStatus() -> InsetText::getStatus()
3100 // and we don't handle LFUN_INSET_MODIFY.
3104 case LFUN_FONT_EMPH:
3105 flag.setOnOff(fontinfo.emph() == FONT_ON);
3106 enable = !cur.paragraph().isPassThru();
3109 case LFUN_FONT_ITAL:
3110 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
3111 enable = !cur.paragraph().isPassThru();
3114 case LFUN_FONT_NOUN:
3115 flag.setOnOff(fontinfo.noun() == FONT_ON);
3116 enable = !cur.paragraph().isPassThru();
3119 case LFUN_FONT_BOLD:
3120 case LFUN_FONT_BOLDSYMBOL:
3121 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
3122 enable = !cur.paragraph().isPassThru();
3125 case LFUN_FONT_SANS:
3126 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
3127 enable = !cur.paragraph().isPassThru();
3130 case LFUN_FONT_ROMAN:
3131 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
3132 enable = !cur.paragraph().isPassThru();
3135 case LFUN_FONT_TYPEWRITER:
3136 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
3137 enable = !cur.paragraph().isPassThru();
3142 enable = cur.selection();
3146 if (cmd.argument().empty()) {
3147 if (theClipboard().isInternal())
3148 enable = cap::numberOfSelections() > 0;
3150 enable = !theClipboard().empty();
3154 // we have an argument
3155 string const arg = to_utf8(cmd.argument());
3156 if (isStrUnsignedInt(arg)) {
3157 // it's a number and therefore means the internal stack
3158 unsigned int n = convert<unsigned int>(arg);
3159 enable = cap::numberOfSelections() > n;
3163 // explicit text type?
3164 if (arg == "html") {
3165 // Do not enable for PlainTextType, since some tidying in the
3166 // frontend is needed for HTML, which is too unsafe for plain text.
3167 enable = theClipboard().hasTextContents(Clipboard::HtmlTextType);
3169 } else if (arg == "latex") {
3170 // LaTeX is usually not available on the clipboard with
3171 // the correct MIME type, but in plain text.
3172 enable = theClipboard().hasTextContents(Clipboard::PlainTextType) ||
3173 theClipboard().hasTextContents(Clipboard::LaTeXTextType);
3177 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
3179 type = Clipboard::PdfGraphicsType;
3180 else if (arg == "png")
3181 type = Clipboard::PngGraphicsType;
3182 else if (arg == "jpeg")
3183 type = Clipboard::JpegGraphicsType;
3184 else if (arg == "linkback")
3185 type = Clipboard::LinkBackGraphicsType;
3186 else if (arg == "emf")
3187 type = Clipboard::EmfGraphicsType;
3188 else if (arg == "wmf")
3189 type = Clipboard::WmfGraphicsType;
3192 LYXERR0("Unrecognized graphics type: " << arg);
3193 // we don't want to assert if the user just mistyped the LFUN
3194 LATTEST(cmd.origin() != FuncRequest::INTERNAL);
3198 enable = theClipboard().hasGraphicsContents(type);
3202 case LFUN_CLIPBOARD_PASTE:
3203 case LFUN_CLIPBOARD_PASTE_SIMPLE:
3204 enable = !theClipboard().empty();
3207 case LFUN_PRIMARY_SELECTION_PASTE:
3208 enable = cur.selection() || !theSelection().empty();
3211 case LFUN_SELECTION_PASTE:
3212 enable = cap::selection();
3215 case LFUN_PARAGRAPH_MOVE_UP:
3216 enable = cur.pit() > 0 && !cur.selection();
3219 case LFUN_PARAGRAPH_MOVE_DOWN:
3220 enable = cur.pit() < cur.lastpit() && !cur.selection();
3223 case LFUN_CHANGE_ACCEPT:
3224 case LFUN_CHANGE_REJECT:
3225 // In principle, these LFUNs should only be enabled if there
3226 // is a change at the current position/in the current selection.
3227 // However, without proper optimizations, this will inevitably
3228 // result in unacceptable performance - just imagine a user who
3229 // wants to select the complete content of a long document.
3230 if (!cur.selection())
3231 enable = cur.paragraph().isChanged(cur.pos());
3233 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
3238 case LFUN_OUTLINE_UP:
3239 case LFUN_OUTLINE_DOWN:
3240 case LFUN_OUTLINE_IN:
3241 case LFUN_OUTLINE_OUT:
3242 // FIXME: LyX is not ready for outlining within inset.
3243 enable = isMainText()
3244 && cur.buffer()->text().getTocLevel(cur.pit()) != Layout::NOT_IN_TOC;
3247 case LFUN_NEWLINE_INSERT:
3248 // LaTeX restrictions (labels or empty par)
3249 enable = !cur.paragraph().isPassThru()
3250 && cur.pos() > cur.paragraph().beginOfBody();
3253 case LFUN_SEPARATOR_INSERT:
3254 // Always enabled for now
3258 case LFUN_TAB_INSERT:
3259 case LFUN_TAB_DELETE:
3260 enable = cur.paragraph().isPassThru();
3263 case LFUN_GRAPHICS_SET_GROUP: {
3264 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
3268 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
3272 case LFUN_NEWPAGE_INSERT:
3273 // not allowed in description items
3274 code = NEWPAGE_CODE;
3275 enable = !inDescriptionItem(cur);
3278 case LFUN_DATE_INSERT: {
3279 string const format = cmd.argument().empty()
3280 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
3281 enable = support::os::is_valid_strftime(format);
3286 enable = !cur.paragraph().isPassThru();
3287 flag.setOnOff(cmd.getArg(0) == cur.real_current_font.language()->lang());
3290 case LFUN_PARAGRAPH_BREAK:
3291 enable = inset().allowMultiPar();
3294 case LFUN_SPELLING_ADD:
3295 case LFUN_SPELLING_IGNORE:
3296 case LFUN_SPELLING_REMOVE:
3297 enable = theSpellChecker() != NULL;
3298 if (enable && !cmd.getArg(1).empty()) {
3299 // validate explicitly given language
3300 Language const * const lang = const_cast<Language *>(languages.getLanguage(cmd.getArg(1)));
3301 enable &= lang != NULL;
3306 DocumentClass const & tclass = cur.buffer()->params().documentClass();
3307 bool const ignoreautonests = cmd.getArg(1) == "ignoreautonests";
3308 docstring layout = ignoreautonests ? from_utf8(cmd.getArg(0)) : cmd.argument();
3310 layout = tclass.defaultLayoutName();
3311 enable = !owner_->forcePlainLayout() && tclass.hasLayout(layout);
3313 flag.setOnOff(layout == cur.paragraph().layout().name());
3317 case LFUN_ENVIRONMENT_SPLIT: {
3318 if (cmd.argument() == "outer") {
3319 // check if we have an environment in our nesting hierarchy
3321 depth_type const current_depth = cur.paragraph().params().depth();
3322 pit_type pit = cur.pit();
3323 Paragraph cpar = pars_[pit];
3325 if (pit == 0 || cpar.params().depth() == 0)
3329 if (cpar.params().depth() < current_depth)
3330 res = cpar.layout().isEnvironment();
3335 else if (cmd.argument() == "previous") {
3336 // look if we have an environment in the previous par
3337 pit_type pit = cur.pit();
3338 Paragraph cpar = pars_[pit];
3342 enable = cpar.layout().isEnvironment();
3348 else if (cur.paragraph().layout().isEnvironment()) {
3349 enable = cmd.argument() == "before"
3350 || cur.pos() > 0 || !isFirstInSequence(cur.pit());
3357 case LFUN_LAYOUT_PARAGRAPH:
3358 case LFUN_PARAGRAPH_PARAMS:
3359 case LFUN_PARAGRAPH_PARAMS_APPLY:
3360 case LFUN_PARAGRAPH_UPDATE:
3361 enable = owner_->allowParagraphCustomization();
3364 // FIXME: why are accent lfuns forbidden with pass_thru layouts?
3365 // Because they insert COMBINING DIACRITICAL Unicode characters,
3366 // that cannot be handled by LaTeX but must be converted according
3367 // to the definition in lib/unicodesymbols?
3368 case LFUN_ACCENT_ACUTE:
3369 case LFUN_ACCENT_BREVE:
3370 case LFUN_ACCENT_CARON:
3371 case LFUN_ACCENT_CEDILLA:
3372 case LFUN_ACCENT_CIRCLE:
3373 case LFUN_ACCENT_CIRCUMFLEX:
3374 case LFUN_ACCENT_DOT:
3375 case LFUN_ACCENT_GRAVE:
3376 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
3377 case LFUN_ACCENT_MACRON:
3378 case LFUN_ACCENT_OGONEK:
3379 case LFUN_ACCENT_TIE:
3380 case LFUN_ACCENT_TILDE:
3381 case LFUN_ACCENT_PERISPOMENI:
3382 case LFUN_ACCENT_UMLAUT:
3383 case LFUN_ACCENT_UNDERBAR:
3384 case LFUN_ACCENT_UNDERDOT:
3385 case LFUN_FONT_DEFAULT:
3386 case LFUN_FONT_FRAK:
3387 case LFUN_FONT_SIZE:
3388 case LFUN_FONT_STATE:
3389 case LFUN_FONT_UNDERLINE:
3390 case LFUN_FONT_STRIKEOUT:
3391 case LFUN_FONT_CROSSOUT:
3392 case LFUN_FONT_UNDERUNDERLINE:
3393 case LFUN_FONT_UNDERWAVE:
3394 case LFUN_TEXTSTYLE_APPLY:
3395 case LFUN_TEXTSTYLE_UPDATE:
3396 enable = !cur.paragraph().isPassThru();
3399 case LFUN_WORD_DELETE_FORWARD:
3400 case LFUN_WORD_DELETE_BACKWARD:
3401 case LFUN_LINE_DELETE_FORWARD:
3402 case LFUN_WORD_FORWARD:
3403 case LFUN_WORD_BACKWARD:
3404 case LFUN_WORD_RIGHT:
3405 case LFUN_WORD_LEFT:
3406 case LFUN_CHAR_FORWARD:
3407 case LFUN_CHAR_FORWARD_SELECT:
3408 case LFUN_CHAR_BACKWARD:
3409 case LFUN_CHAR_BACKWARD_SELECT:
3410 case LFUN_CHAR_LEFT:
3411 case LFUN_CHAR_LEFT_SELECT:
3412 case LFUN_CHAR_RIGHT:
3413 case LFUN_CHAR_RIGHT_SELECT:
3415 case LFUN_UP_SELECT:
3417 case LFUN_DOWN_SELECT:
3418 case LFUN_PARAGRAPH_UP_SELECT:
3419 case LFUN_PARAGRAPH_DOWN_SELECT:
3420 case LFUN_LINE_BEGIN_SELECT:
3421 case LFUN_LINE_END_SELECT:
3422 case LFUN_WORD_FORWARD_SELECT:
3423 case LFUN_WORD_BACKWARD_SELECT:
3424 case LFUN_WORD_RIGHT_SELECT:
3425 case LFUN_WORD_LEFT_SELECT:
3426 case LFUN_WORD_SELECT:
3427 case LFUN_SECTION_SELECT:
3428 case LFUN_BUFFER_BEGIN:
3429 case LFUN_BUFFER_END:
3430 case LFUN_BUFFER_BEGIN_SELECT:
3431 case LFUN_BUFFER_END_SELECT:
3432 case LFUN_INSET_BEGIN:
3433 case LFUN_INSET_END:
3434 case LFUN_INSET_BEGIN_SELECT:
3435 case LFUN_INSET_END_SELECT:
3436 case LFUN_PARAGRAPH_UP:
3437 case LFUN_PARAGRAPH_DOWN:
3438 case LFUN_LINE_BEGIN:
3440 case LFUN_CHAR_DELETE_FORWARD:
3441 case LFUN_CHAR_DELETE_BACKWARD:
3442 case LFUN_WORD_UPCASE:
3443 case LFUN_WORD_LOWCASE:
3444 case LFUN_WORD_CAPITALIZE:
3445 case LFUN_CHARS_TRANSPOSE:
3446 case LFUN_SERVER_GET_XY:
3447 case LFUN_SERVER_SET_XY:
3448 case LFUN_SERVER_GET_LAYOUT:
3449 case LFUN_SELF_INSERT:
3450 case LFUN_UNICODE_INSERT:
3451 case LFUN_THESAURUS_ENTRY:
3453 case LFUN_SERVER_GET_STATISTICS:
3454 // these are handled in our dispatch()
3458 case LFUN_INSET_INSERT: {
3459 string const type = cmd.getArg(0);
3460 if (type == "toc") {
3462 // not allowed in description items
3463 //FIXME: couldn't this be merged in Inset::insetAllowed()?
3464 enable = !inDescriptionItem(cur);
3477 || !cur.inset().insetAllowed(code)
3478 || (cur.paragraph().layout().pass_thru && !allow_in_passthru)))
3481 flag.setEnabled(enable);
3486 void Text::pasteString(Cursor & cur, docstring const & clip,
3489 if (!clip.empty()) {
3492 insertStringAsParagraphs(cur, clip, cur.current_font);
3494 insertStringAsLines(cur, clip, cur.current_font);
3499 // FIXME: an item inset would make things much easier.
3500 bool Text::inDescriptionItem(Cursor & cur) const
3502 Paragraph & par = cur.paragraph();
3503 pos_type const pos = cur.pos();
3504 pos_type const body_pos = par.beginOfBody();
3506 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
3507 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
3508 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
3511 return (pos < body_pos
3513 && (pos == 0 || par.getChar(pos - 1) != ' ')));