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.
21 #include "BranchList.h"
22 #include "FloatList.h"
23 #include "FuncStatus.h"
25 #include "buffer_funcs.h"
26 #include "BufferParams.h"
27 #include "BufferView.h"
30 #include "CutAndPaste.h"
31 #include "DispatchResult.h"
32 #include "ErrorList.h"
34 #include "FuncRequest.h"
35 #include "InsetList.h"
39 #include "LyXAction.h"
43 #include "Paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "TextClass.h"
47 #include "TextMetrics.h"
50 #include "frontends/Application.h"
51 #include "frontends/Clipboard.h"
52 #include "frontends/LyXView.h"
53 #include "frontends/Selection.h"
54 #include "frontends/WorkArea.h"
56 #include "insets/InsetCollapsable.h"
57 #include "insets/InsetCommand.h"
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetFloatList.h"
60 #include "insets/InsetGraphics.h"
61 #include "insets/InsetGraphicsParams.h"
62 #include "insets/InsetNewline.h"
63 #include "insets/InsetQuotes.h"
64 #include "insets/InsetSpecialChar.h"
65 #include "insets/InsetText.h"
67 #include "support/convert.h"
68 #include "support/debug.h"
69 #include "support/gettext.h"
70 #include "support/lstrings.h"
71 #include "support/lyxtime.h"
72 #include "support/os.h"
74 #include "mathed/InsetMathHull.h"
75 #include "mathed/MathMacroTemplate.h"
77 #include <boost/next_prior.hpp>
83 using namespace lyx::support;
87 using cap::copySelection;
88 using cap::cutSelection;
89 using cap::pasteFromStack;
90 using cap::pasteClipboardText;
91 using cap::pasteClipboardGraphics;
92 using cap::replaceSelection;
93 using cap::grabAndEraseSelection;
94 using cap::selClearOrDel;
97 static Font freefont(ignore_font, ignore_language);
98 static bool toggleall = false;
100 static void toggleAndShow(Cursor & cur, Text * text,
101 Font const & font, bool toggleall = true)
103 text->toggleFree(cur, font, toggleall);
105 if (font.language() != ignore_language ||
106 font.fontInfo().number() != FONT_IGNORE) {
107 TextMetrics const & tm = cur.bv().textMetrics(text);
108 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
109 cur.real_current_font))
110 text->setCursor(cur, cur.pit(), cur.pos(),
111 false, !cur.boundary());
116 static void moveCursor(Cursor & cur, bool selecting)
118 if (selecting || cur.mark())
123 static void finishChange(Cursor & cur, bool selecting)
126 moveCursor(cur, selecting);
130 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
133 docstring sel = cur.selectionAsString(false);
135 // It may happen that sel is empty but there is a selection
136 replaceSelection(cur);
138 // Is this a valid formula?
142 #ifdef ENABLE_ASSERTIONS
143 const int old_pos = cur.pos();
145 cur.insert(new InsetMathHull(hullSimple));
146 #ifdef ENABLE_ASSERTIONS
147 LASSERT(old_pos == cur.pos(), /**/);
149 cur.nextInset()->edit(cur, true);
150 // don't do that also for LFUN_MATH_MODE
151 // unless you want end up with always changing
152 // to mathrm when opening an inlined inset --
153 // I really hate "LyXfunc overloading"...
155 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
156 // Avoid an unnecessary undo step if cmd.argument
158 if (!cmd.argument().empty())
159 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
162 // create a macro if we see "\\newcommand"
163 // somewhere, and an ordinary formula
165 if (sel.find(from_ascii("\\newcommand")) == string::npos
166 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
167 && sel.find(from_ascii("\\def")) == string::npos)
169 InsetMathHull * formula = new InsetMathHull;
170 string const selstr = to_utf8(sel);
171 istringstream is(selstr);
174 if (!formula->readQuiet(lex)) {
175 // No valid formula, let's try with delims
176 is.str("$" + selstr + "$");
178 if (!formula->readQuiet(lex)) {
179 // Still not valid, leave it as is
188 cur.insert(new MathMacroTemplate(sel));
192 cur.message(from_utf8(N_("Math editor mode")));
194 cur.message(from_utf8(N_("No valid math formula")));
198 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
200 BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
201 if (cur.inRegexped()) {
202 cur.message(_("Already in regexp mode"));
206 docstring const save_selection = grabAndEraseSelection(cur);
208 // replaceSelection(cur);
210 cur.insert(new InsetMathHull(hullRegexp));
211 cur.nextInset()->edit(cur, true);
212 cur.niceInsert(save_selection);
214 cur.message(_("Regexp editor mode"));
218 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
221 cap::replaceSelection(cur);
222 cur.insert(new InsetSpecialChar(kind));
227 static bool doInsertInset(Cursor & cur, Text * text,
228 FuncRequest const & cmd, bool edit, bool pastesel)
230 Buffer & buffer = cur.bv().buffer();
231 BufferParams const & bparams = buffer.params();
232 Inset * inset = createInset(buffer, cmd);
237 if (cmd.action == LFUN_INDEX_INSERT) {
238 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
239 text->insertInset(cur, inset);
241 inset->edit(cur, true);
242 // Now put this into inset
243 cur.text()->insertStringAsLines(cur, ds);
244 cur.leaveInset(*inset);
249 if (cur.selection()) {
250 cutSelection(cur, false, pastesel);
251 cur.clearSelection();
254 text->insertInset(cur, inset);
257 inset->edit(cur, true);
259 if (!gotsel || !pastesel)
262 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
263 cur.buffer()->errors("Paste");
264 cur.clearSelection(); // bug 393
266 InsetText * insetText = dynamic_cast<InsetText *>(inset);
267 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
268 // reset first par to default
269 cur.text()->paragraphs().begin()
270 ->setPlainOrDefaultLayout(bparams.documentClass());
273 // Merge multiple paragraphs -- hack
274 while (cur.lastpit() > 0)
275 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
276 cur.leaveInset(*inset);
278 cur.leaveInset(*inset);
279 // reset surrounding par to default
280 DocumentClass const & dc = bparams.documentClass();
281 docstring const layoutname = inset->usePlainLayout()
282 ? dc.plainLayoutName()
283 : dc.defaultLayoutName();
284 text->setLayout(cur, layoutname);
291 string const freefont2string()
293 return freefont.toString(toggleall);
297 static void dragMove(Cursor & cur, int moveid, int movetoid)
299 // Create pointers to buffers
300 Buffer & buf_move = *cur.buffer();
301 DocIterator dit_move = buf_move.getParFromID(moveid);
302 DocIterator dit_dest = buf_move.getParFromID(movetoid);
304 pit_type & pit_move = dit_move.pit();
305 pit_type & pit_dest = dit_dest.pit();
306 ParagraphList & pars = dit_move.text()->paragraphs();
308 // Create references to the paragraphs to be moved
309 ParagraphList::iterator const bgn = pars.begin();
310 ParagraphList::iterator dest_start = boost::next(bgn, pit_dest);
312 // The first paragraph of the area to be copied:
313 ParagraphList::iterator start = boost::next(bgn, pit_move);
314 // The final paragraph of area to be copied:
315 ParagraphList::iterator finish = start;
316 ParagraphList::iterator const end = pars.end();
318 // Move out (down) from this section header
322 // Seek the one (on same level) below
323 int const thistoclevel = start->layout().toclevel;
324 for (; finish != end; ++finish) {
325 int const toclevel = finish->layout().toclevel;
326 if (toclevel != Layout::NOT_IN_TOC
327 && toclevel <= thistoclevel)
331 if (start == pars.begin() || start == dest_start)
335 pars.insert(dest_start, start, finish);
336 pars.erase(start, finish);
338 // FIXME: This only really needs doing for the newly
339 // introduced paragraphs. Something like:
340 // pit_type const numpars = distance(start, finish);
341 // start = boost::next(bgn, pit);
342 // finish = boost::next(start, numpars);
343 // for (; start != finish; ++start)
344 // start->setBuffer(buf);
345 // But while this seems to work, it is kind of fragile.
346 buf_move.inset().setBuffer(buf_move);
350 /// the type of outline operation
352 OutlineUp, // Move this header with text down
353 OutlineDown, // Move this header with text up
354 OutlineIn, // Make this header deeper
355 OutlineOut // Make this header shallower
359 static void outline(OutlineOp mode, Cursor & cur)
361 Buffer & buf = *cur.buffer();
362 pit_type & pit = cur.pit();
363 ParagraphList & pars = buf.text().paragraphs();
364 ParagraphList::iterator const bgn = pars.begin();
365 // The first paragraph of the area to be copied:
366 ParagraphList::iterator start = boost::next(bgn, pit);
367 // The final paragraph of area to be copied:
368 ParagraphList::iterator finish = start;
369 ParagraphList::iterator const end = pars.end();
371 DocumentClass const & tc = buf.params().documentClass();
373 int const thistoclevel = start->layout().toclevel;
376 // Move out (down) from this section header
380 // Seek the one (on same level) below
381 for (; finish != end; ++finish) {
382 toclevel = finish->layout().toclevel;
383 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
387 // Do we need to set insets' buffer_ members, because we copied
388 // some stuff? We'll assume we do and reset it otherwise.
389 bool set_buffers = true;
393 if (start == pars.begin())
396 ParagraphList::iterator dest = start;
397 // Move out (up) from this header
400 // Search previous same-level header above
403 toclevel = dest->layout().toclevel;
405 && (toclevel == Layout::NOT_IN_TOC
406 || toclevel > thistoclevel));
407 // Not found; do nothing
408 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
410 pit_type const newpit = distance(bgn, dest);
411 pit_type const len = distance(start, finish);
412 pit_type const deletepit = pit + len;
413 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
414 pars.insert(dest, start, finish);
415 start = boost::next(pars.begin(), deletepit);
417 pars.erase(start, finish);
424 // Go one down from *this* header:
425 ParagraphList::iterator dest = boost::next(finish, 1);
426 // Go further down to find header to insert in front of:
427 for (; dest != end; ++dest) {
428 toclevel = dest->layout().toclevel;
429 if (toclevel != Layout::NOT_IN_TOC
430 && toclevel <= thistoclevel)
433 // One such was found:
434 pit_type newpit = distance(bgn, dest);
435 pit_type const len = distance(start, finish);
436 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
437 pars.insert(dest, start, finish);
438 start = boost::next(bgn, pit);
440 pars.erase(start, finish);
444 pit_type const len = distance(start, finish);
445 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
446 for (; start != finish; ++start) {
447 toclevel = start->layout().toclevel;
448 if (toclevel == Layout::NOT_IN_TOC)
450 DocumentClass::const_iterator lit = tc.begin();
451 DocumentClass::const_iterator len = tc.end();
452 for (; lit != len; ++lit) {
453 if (lit->toclevel == toclevel + 1 &&
454 start->layout().labeltype == lit->labeltype) {
455 start->setLayout(*lit);
464 pit_type const len = distance(start, finish);
465 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
466 for (; start != finish; ++start) {
467 toclevel = start->layout().toclevel;
468 if (toclevel == Layout::NOT_IN_TOC)
470 DocumentClass::const_iterator lit = tc.begin();
471 DocumentClass::const_iterator len = tc.end();
472 for (; lit != len; ++lit) {
473 if (lit->toclevel == toclevel - 1 &&
474 start->layout().labeltype == lit->labeltype) {
475 start->setLayout(*lit);
485 // FIXME This only really needs doing for the newly introduced
486 // paragraphs. Something like:
487 // pit_type const numpars = distance(start, finish);
488 // start = boost::next(bgn, pit);
489 // finish = boost::next(start, numpars);
490 // for (; start != finish; ++start)
491 // start->setBuffer(buf);
492 // But while this seems to work, it is kind of fragile.
493 buf.inset().setBuffer(buf);
497 void Text::number(Cursor & cur)
499 FontInfo font = ignore_font;
500 font.setNumber(FONT_TOGGLE);
501 toggleAndShow(cur, this, Font(font, ignore_language));
505 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
507 return par.isRTL(buffer.params());
511 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
513 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
515 // Dispatch if the cursor is inside the text. It is not the
516 // case for context menus (bug 5797).
517 if (cur.text() != this) {
522 BufferView * bv = &cur.bv();
523 TextMetrics * tm = &bv->textMetrics(this);
524 if (!tm->contains(cur.pit())) {
525 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
526 tm = &bv->textMetrics(this);
529 // FIXME: We use the update flag to indicates wether a singlePar or a
530 // full screen update is needed. We reset it here but shall we restore it
534 LASSERT(cur.text() == this, /**/);
535 CursorSlice oldTopSlice = cur.top();
536 bool oldBoundary = cur.boundary();
537 bool sel = cur.selection();
538 // Signals that, even if needsUpdate == false, an update of the
539 // cursor paragraph is required
540 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
541 LyXAction::SingleParUpdate);
542 // Signals that a full-screen update is required
543 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
544 LyXAction::NoUpdate) || singleParUpdate);
546 switch (cmd.action) {
548 case LFUN_PARAGRAPH_MOVE_DOWN: {
549 pit_type const pit = cur.pit();
550 recUndo(cur, pit, pit + 1);
552 swap(pars_[pit], pars_[pit + 1]);
553 cur.buffer()->updateLabels();
559 case LFUN_PARAGRAPH_MOVE_UP: {
560 pit_type const pit = cur.pit();
561 recUndo(cur, pit - 1, pit);
563 swap(pars_[pit], pars_[pit - 1]);
564 cur.buffer()->updateLabels();
570 case LFUN_APPENDIX: {
571 Paragraph & par = cur.paragraph();
572 bool start = !par.params().startOfAppendix();
574 // FIXME: The code below only makes sense at top level.
575 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
576 // ensure that we have only one start_of_appendix in this document
577 // FIXME: this don't work for multipart document!
578 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
579 if (pars_[tmp].params().startOfAppendix()) {
581 pars_[tmp].params().startOfAppendix(false);
587 par.params().startOfAppendix(start);
589 // we can set the refreshing parameters now
590 cur.buffer()->updateLabels();
594 case LFUN_WORD_DELETE_FORWARD:
596 cutSelection(cur, true, false);
598 deleteWordForward(cur);
599 finishChange(cur, false);
602 case LFUN_WORD_DELETE_BACKWARD:
604 cutSelection(cur, true, false);
606 deleteWordBackward(cur);
607 finishChange(cur, false);
610 case LFUN_LINE_DELETE:
612 cutSelection(cur, true, false);
614 tm->deleteLineForward(cur);
615 finishChange(cur, false);
618 case LFUN_BUFFER_BEGIN:
619 case LFUN_BUFFER_BEGIN_SELECT:
620 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
621 if (cur.depth() == 1)
622 needsUpdate |= cursorTop(cur);
625 cur.updateFlags(Update::FitCursor);
628 case LFUN_BUFFER_END:
629 case LFUN_BUFFER_END_SELECT:
630 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
631 if (cur.depth() == 1)
632 needsUpdate |= cursorBottom(cur);
635 cur.updateFlags(Update::FitCursor);
638 case LFUN_INSET_BEGIN:
639 case LFUN_INSET_BEGIN_SELECT:
640 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
641 if (cur.depth() == 1 || cur.pos() > 0)
642 needsUpdate |= cursorTop(cur);
645 cur.updateFlags(Update::FitCursor);
649 case LFUN_INSET_END_SELECT:
650 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
651 if (cur.depth() == 1 || cur.pos() < cur.lastpos())
652 needsUpdate |= cursorBottom(cur);
655 cur.updateFlags(Update::FitCursor);
658 case LFUN_CHAR_FORWARD:
659 case LFUN_CHAR_FORWARD_SELECT:
660 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
661 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
662 needsUpdate |= cursorForward(cur);
664 if (!needsUpdate && oldTopSlice == cur.top()
665 && cur.boundary() == oldBoundary) {
667 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
669 // we will probably be moving out the inset, so we should execute
670 // the depm-mechanism, but only when the cursor has a place to
671 // go outside this inset, i.e. in a slice above.
672 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
673 && cur.pit() == cur.lastpit()) {
674 // The cursor hasn't changed yet. To give the
675 // DEPM the possibility of doing something we must
676 // provide it with two different cursors.
678 dummy.pos() = dummy.pit() = 0;
679 cur.bv().checkDepm(dummy, cur);
684 case LFUN_CHAR_BACKWARD:
685 case LFUN_CHAR_BACKWARD_SELECT:
686 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
687 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
688 needsUpdate |= cursorBackward(cur);
690 if (!needsUpdate && oldTopSlice == cur.top()
691 && cur.boundary() == oldBoundary) {
693 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
695 // we will probably be moving out the inset, so we should execute
696 // the depm-mechanism, but only when the cursor has a place to
697 // go outside this inset, i.e. in a slice above.
698 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
699 // The cursor hasn't changed yet. To give the
700 // DEPM the possibility of doing something we must
701 // provide it with two different cursors.
703 dummy.pos() = cur.lastpos();
704 dummy.pit() = cur.lastpit();
705 cur.bv().checkDepm(dummy, cur);
711 case LFUN_CHAR_LEFT_SELECT:
712 if (lyxrc.visual_cursor) {
713 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
714 needsUpdate |= cursorVisLeft(cur);
715 if (!needsUpdate && oldTopSlice == cur.top()
716 && cur.boundary() == oldBoundary) {
718 cmd = FuncRequest(LFUN_FINISHED_LEFT);
721 if (reverseDirectionNeeded(cur)) {
722 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
723 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
725 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
726 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
733 case LFUN_CHAR_RIGHT:
734 case LFUN_CHAR_RIGHT_SELECT:
735 if (lyxrc.visual_cursor) {
736 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
737 needsUpdate |= cursorVisRight(cur);
738 if (!needsUpdate && oldTopSlice == cur.top()
739 && cur.boundary() == oldBoundary) {
741 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
744 if (reverseDirectionNeeded(cur)) {
745 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
746 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
748 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
749 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
758 case LFUN_DOWN_SELECT:
761 // stop/start the selection
762 bool select = cmd.action == LFUN_DOWN_SELECT ||
763 cmd.action == LFUN_UP_SELECT;
765 // move cursor up/down
766 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
767 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
769 if (!atFirstOrLastRow) {
770 needsUpdate |= cur.selHandle(select);
771 cur.selHandle(select);
772 cur.upDownInText(up, needsUpdate);
773 needsUpdate |= cur.beforeDispatchCursor().inMathed();
775 // if the cursor cannot be moved up or down do not remove
776 // the selection right now, but wait for the next dispatch.
778 needsUpdate |= cur.selHandle(select);
779 cur.upDownInText(up, needsUpdate);
786 case LFUN_PARAGRAPH_UP:
787 case LFUN_PARAGRAPH_UP_SELECT:
788 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
789 needsUpdate |= cursorUpParagraph(cur);
792 case LFUN_PARAGRAPH_DOWN:
793 case LFUN_PARAGRAPH_DOWN_SELECT:
794 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
795 needsUpdate |= cursorDownParagraph(cur);
798 case LFUN_LINE_BEGIN:
799 case LFUN_LINE_BEGIN_SELECT:
800 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
801 needsUpdate |= tm->cursorHome(cur);
805 case LFUN_LINE_END_SELECT:
806 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
807 needsUpdate |= tm->cursorEnd(cur);
810 case LFUN_SECTION_SELECT: {
811 Buffer const & buf = *cur.buffer();
812 pit_type const pit = cur.pit();
813 ParagraphList & pars = buf.text().paragraphs();
814 ParagraphList::iterator bgn = pars.begin();
815 // The first paragraph of the area to be selected:
816 ParagraphList::iterator start = boost::next(bgn, pit);
817 // The final paragraph of area to be selected:
818 ParagraphList::iterator finish = start;
819 ParagraphList::iterator end = pars.end();
821 int const thistoclevel = start->layout().toclevel;
822 if (thistoclevel == Layout::NOT_IN_TOC)
826 Cursor const old_cur = cur;
827 needsUpdate |= cur.selHandle(true);
829 // Move out (down) from this section header
833 // Seek the one (on same level) below
834 for (; finish != end; ++finish, ++cur.pit()) {
835 int const toclevel = finish->layout().toclevel;
836 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
839 cur.pos() = cur.lastpos();
841 needsUpdate |= cur != old_cur;
845 case LFUN_WORD_RIGHT:
846 case LFUN_WORD_RIGHT_SELECT:
847 if (lyxrc.visual_cursor) {
848 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
849 needsUpdate |= cursorVisRightOneWord(cur);
850 if (!needsUpdate && oldTopSlice == cur.top()
851 && cur.boundary() == oldBoundary) {
853 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
856 if (reverseDirectionNeeded(cur)) {
857 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
858 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
860 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
861 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
868 case LFUN_WORD_FORWARD:
869 case LFUN_WORD_FORWARD_SELECT:
870 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
871 needsUpdate |= cursorForwardOneWord(cur);
875 case LFUN_WORD_LEFT_SELECT:
876 if (lyxrc.visual_cursor) {
877 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
878 needsUpdate |= cursorVisLeftOneWord(cur);
879 if (!needsUpdate && oldTopSlice == cur.top()
880 && cur.boundary() == oldBoundary) {
882 cmd = FuncRequest(LFUN_FINISHED_LEFT);
885 if (reverseDirectionNeeded(cur)) {
886 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
887 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
889 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
890 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
897 case LFUN_WORD_BACKWARD:
898 case LFUN_WORD_BACKWARD_SELECT:
899 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
900 needsUpdate |= cursorBackwardOneWord(cur);
903 case LFUN_WORD_SELECT: {
904 selectWord(cur, WHOLE_WORD);
905 finishChange(cur, true);
909 case LFUN_NEWLINE_INSERT: {
910 InsetNewlineParams inp;
911 docstring arg = cmd.argument();
912 // this avoids a double undo
913 // FIXME: should not be needed, ideally
914 if (!cur.selection())
916 cap::replaceSelection(cur);
917 if (arg == "linebreak")
918 inp.kind = InsetNewlineParams::LINEBREAK;
920 inp.kind = InsetNewlineParams::NEWLINE;
921 cur.insert(new InsetNewline(inp));
923 moveCursor(cur, false);
927 case LFUN_TAB_INSERT: {
928 bool const multi_par_selection = cur.selection() &&
929 cur.selBegin().pit() != cur.selEnd().pit();
930 if (multi_par_selection) {
931 // If there is a multi-paragraph selection, a tab is inserted
932 // at the beginning of each paragraph.
933 cur.recordUndoSelection();
934 pit_type const pit_end = cur.selEnd().pit();
935 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
936 pars_[pit].insertChar(0, '\t',
937 bv->buffer().params().trackChanges);
938 // Update the selection pos to make sure the selection does not
939 // change as the inserted tab will increase the logical pos.
940 if (cur.anchor_.pit() == pit)
941 cur.anchor_.forwardPos();
942 if (cur.pit() == pit)
947 // Maybe we shouldn't allow tabs within a line, because they
948 // are not (yet) aligned as one might do expect.
949 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
955 case LFUN_TAB_DELETE: {
956 bool const tc = bv->buffer().params().trackChanges;
957 if (cur.selection()) {
958 // If there is a selection, a tab (if present) is removed from
959 // the beginning of each paragraph.
960 cur.recordUndoSelection();
961 pit_type const pit_end = cur.selEnd().pit();
962 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
963 Paragraph & par = paragraphs()[pit];
964 if (par.getChar(0) == '\t') {
965 if (cur.pit() == pit)
967 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
968 cur.anchor_.backwardPos();
970 par.eraseChar(0, tc);
972 // If no tab was present, try to remove up to four spaces.
973 for (int n_spaces = 0;
974 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
975 if (cur.pit() == pit)
977 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
978 cur.anchor_.backwardPos();
980 par.eraseChar(0, tc);
985 // If there is no selection, try to remove a tab or some spaces
986 // before the position of the cursor.
987 Paragraph & par = paragraphs()[cur.pit()];
988 pos_type const pos = cur.pos();
993 char_type const c = par.getChar(pos - 1);
997 par.eraseChar(cur.pos(), tc);
999 for (int n_spaces = 0;
1001 && par.getChar(cur.pos() - 1) == ' '
1005 par.eraseChar(cur.pos(), tc);
1012 case LFUN_CHAR_DELETE_FORWARD:
1013 if (!cur.selection()) {
1014 if (cur.pos() == cur.paragraph().size())
1015 // Par boundary, force full-screen update
1016 singleParUpdate = false;
1017 needsUpdate |= erase(cur);
1019 // It is possible to make it a lot faster still
1020 // just comment out the line below...
1022 cutSelection(cur, true, false);
1023 singleParUpdate = false;
1025 moveCursor(cur, false);
1028 case LFUN_CHAR_DELETE_BACKWARD:
1029 if (!cur.selection()) {
1030 if (bv->getIntl().getTransManager().backspace()) {
1031 // Par boundary, full-screen update
1033 singleParUpdate = false;
1034 needsUpdate |= backspace(cur);
1036 // It is possible to make it a lot faster still
1037 // just comment out the line below...
1040 cutSelection(cur, true, false);
1041 singleParUpdate = false;
1045 case LFUN_BREAK_PARAGRAPH:
1046 cap::replaceSelection(cur);
1047 breakParagraph(cur, cmd.argument() == "inverse");
1052 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
1053 // as its duties can be performed there. Should it be removed??
1054 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
1055 case LFUN_PARAGRAPH_SPACING: {
1056 Paragraph & par = cur.paragraph();
1057 Spacing::Space cur_spacing = par.params().spacing().getSpace();
1058 string cur_value = "1.0";
1059 if (cur_spacing == Spacing::Other)
1060 cur_value = par.params().spacing().getValueAsString();
1062 istringstream is(to_utf8(cmd.argument()));
1065 Spacing::Space new_spacing = cur_spacing;
1066 string new_value = cur_value;
1068 lyxerr << "Missing argument to `paragraph-spacing'"
1070 } else if (tmp == "single") {
1071 new_spacing = Spacing::Single;
1072 } else if (tmp == "onehalf") {
1073 new_spacing = Spacing::Onehalf;
1074 } else if (tmp == "double") {
1075 new_spacing = Spacing::Double;
1076 } else if (tmp == "other") {
1077 new_spacing = Spacing::Other;
1078 string tmpval = "0.0";
1080 lyxerr << "new_value = " << tmpval << endl;
1081 if (tmpval != "0.0")
1083 } else if (tmp == "default") {
1084 new_spacing = Spacing::Default;
1086 lyxerr << to_utf8(_("Unknown spacing argument: "))
1087 << to_utf8(cmd.argument()) << endl;
1089 if (cur_spacing != new_spacing || cur_value != new_value)
1090 par.params().spacing(Spacing(new_spacing, new_value));
1094 case LFUN_INSET_INSERT: {
1097 // We have to avoid triggering InstantPreview loading
1098 // before inserting into the document. See bug #5626.
1099 bool loaded = bv->buffer().isFullyLoaded();
1100 bv->buffer().setFullyLoaded(false);
1101 Inset * inset = createInset(bv->buffer(), cmd);
1102 bv->buffer().setFullyLoaded(loaded);
1105 // FIXME (Abdel 01/02/2006):
1106 // What follows would be a partial fix for bug 2154:
1107 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
1108 // This automatically put the label inset _after_ a
1109 // numbered section. It should be possible to extend the mechanism
1110 // to any kind of LateX environement.
1111 // The correct way to fix that bug would be at LateX generation.
1112 // I'll let the code here for reference as it could be used for some
1113 // other feature like "automatic labelling".
1115 Paragraph & par = pars_[cur.pit()];
1116 if (inset->lyxCode() == LABEL_CODE
1117 && par.layout().labeltype == LABEL_COUNTER) {
1118 // Go to the end of the paragraph
1119 // Warning: Because of Change-Tracking, the last
1120 // position is 'size()' and not 'size()-1':
1121 cur.pos() = par.size();
1122 // Insert a new paragraph
1123 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1127 if (cur.selection())
1128 cutSelection(cur, true, false);
1132 // trigger InstantPreview now
1133 if (inset->lyxCode() == EXTERNAL_CODE) {
1134 InsetExternal & ins =
1135 static_cast<InsetExternal &>(*inset);
1136 ins.updatePreview();
1143 case LFUN_INSET_DISSOLVE: {
1144 // first, try if there's an inset at cursor
1145 // FIXME: this first part should be moved to
1146 // a LFUN_NEXT_INSET_DISSOLVE, or be called via
1147 // some generic "next-inset inset-dissolve"
1148 Inset * inset = cur.nextInset();
1149 if (inset && inset->isActive()) {
1150 Cursor tmpcur = cur;
1151 tmpcur.pushBackward(*inset);
1152 inset->dispatch(tmpcur, cmd);
1153 if (tmpcur.result().dispatched()) {
1158 // if it did not work, try the underlying inset
1159 if (dissolveInset(cur)) {
1163 // if it did not work, do nothing.
1167 case LFUN_SET_GRAPHICS_GROUP: {
1168 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1174 string id = to_utf8(cmd.argument());
1175 string grp = graphics::getGroupParams(bv->buffer(), id);
1176 InsetGraphicsParams tmp, inspar = ins->getParams();
1179 inspar.groupId = to_utf8(cmd.argument());
1181 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1182 tmp.filename = inspar.filename;
1186 ins->setParams(inspar);
1189 case LFUN_SPACE_INSERT:
1190 if (cur.paragraph().layout().free_spacing)
1191 insertChar(cur, ' ');
1193 doInsertInset(cur, this, cmd, false, false);
1196 moveCursor(cur, false);
1199 case LFUN_SPECIALCHAR_INSERT: {
1200 string const name = to_utf8(cmd.argument());
1201 if (name == "hyphenation")
1202 specialChar(cur, InsetSpecialChar::HYPHENATION);
1203 else if (name == "ligature-break")
1204 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1205 else if (name == "slash")
1206 specialChar(cur, InsetSpecialChar::SLASH);
1207 else if (name == "nobreakdash")
1208 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1209 else if (name == "dots")
1210 specialChar(cur, InsetSpecialChar::LDOTS);
1211 else if (name == "end-of-sentence")
1212 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1213 else if (name == "menu-separator")
1214 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1215 else if (name.empty())
1216 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1218 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1222 case LFUN_WORD_UPCASE:
1223 changeCase(cur, text_uppercase);
1226 case LFUN_WORD_LOWCASE:
1227 changeCase(cur, text_lowercase);
1230 case LFUN_WORD_CAPITALIZE:
1231 changeCase(cur, text_capitalization);
1234 case LFUN_CHARS_TRANSPOSE:
1235 charsTranspose(cur);
1239 cur.message(_("Paste"));
1240 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1241 cap::replaceSelection(cur);
1243 // without argument?
1244 string const arg = to_utf8(cmd.argument());
1246 if (theClipboard().isInternal())
1247 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1248 else if (theClipboard().hasGraphicsContents()
1249 && !theClipboard().hasTextContents())
1250 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1252 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1253 } else if (isStrUnsignedInt(arg)) {
1254 // we have a numerical argument
1255 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1256 convert<unsigned int>(arg));
1258 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1260 type = Clipboard::PdfGraphicsType;
1261 else if (arg == "png")
1262 type = Clipboard::PngGraphicsType;
1263 else if (arg == "jpeg")
1264 type = Clipboard::JpegGraphicsType;
1265 else if (arg == "linkback")
1266 type = Clipboard::LinkBackGraphicsType;
1267 else if (arg == "emf")
1268 type = Clipboard::EmfGraphicsType;
1269 else if (arg == "wmf")
1270 type = Clipboard::WmfGraphicsType;
1273 LASSERT(false, /**/);
1275 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1278 bv->buffer().errors("Paste");
1279 cur.clearSelection(); // bug 393
1285 cutSelection(cur, true, true);
1286 cur.message(_("Cut"));
1291 cur.message(_("Copy"));
1294 case LFUN_SERVER_GET_XY:
1295 cur.message(from_utf8(
1296 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1297 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1300 case LFUN_SERVER_SET_XY: {
1303 istringstream is(to_utf8(cmd.argument()));
1306 lyxerr << "SETXY: Could not parse coordinates in '"
1307 << to_utf8(cmd.argument()) << endl;
1309 tm->setCursorFromCoordinates(cur, x, y);
1313 case LFUN_SERVER_GET_LAYOUT:
1314 cur.message(cur.paragraph().layout().name());
1318 docstring layout = cmd.argument();
1319 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1321 Paragraph const & para = cur.paragraph();
1322 docstring const old_layout = para.layout().name();
1323 DocumentClass const & tclass = bv->buffer().params().documentClass();
1326 layout = tclass.defaultLayoutName();
1328 if (para.forcePlainLayout())
1329 // in this case only the empty layout is allowed
1330 layout = tclass.plainLayoutName();
1331 else if (para.usePlainLayout()) {
1332 // in this case, default layout maps to empty layout
1333 if (layout == tclass.defaultLayoutName())
1334 layout = tclass.plainLayoutName();
1336 // otherwise, the empty layout maps to the default
1337 if (layout == tclass.plainLayoutName())
1338 layout = tclass.defaultLayoutName();
1341 bool hasLayout = tclass.hasLayout(layout);
1343 // If the entry is obsolete, use the new one instead.
1345 docstring const & obs = tclass[layout].obsoleted_by();
1351 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1352 from_utf8(N_(" not known")));
1356 bool change_layout = (old_layout != layout);
1358 if (!change_layout && cur.selection() &&
1359 cur.selBegin().pit() != cur.selEnd().pit())
1361 pit_type spit = cur.selBegin().pit();
1362 pit_type epit = cur.selEnd().pit() + 1;
1363 while (spit != epit) {
1364 if (pars_[spit].layout().name() != old_layout) {
1365 change_layout = true;
1373 setLayout(cur, layout);
1378 case LFUN_CLIPBOARD_PASTE:
1379 cur.clearSelection();
1380 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1381 cmd.argument() == "paragraph");
1382 bv->buffer().errors("Paste");
1385 case LFUN_PRIMARY_SELECTION_PASTE:
1386 pasteString(cur, theSelection().get(),
1387 cmd.argument() == "paragraph");
1390 case LFUN_SELECTION_PASTE:
1391 // Copy the selection buffer to the clipboard stack,
1392 // because we want it to appear in the "Edit->Paste
1394 cap::copySelectionToStack();
1395 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1396 bv->buffer().errors("Paste");
1399 case LFUN_UNICODE_INSERT: {
1400 if (cmd.argument().empty())
1402 docstring hexstring = cmd.argument();
1403 if (isHex(hexstring)) {
1404 char_type c = hexToInt(hexstring);
1405 if (c >= 32 && c < 0x10ffff) {
1406 lyxerr << "Inserting c: " << c << endl;
1407 docstring s = docstring(1, c);
1408 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1414 case LFUN_QUOTE_INSERT: {
1415 Paragraph & par = cur.paragraph();
1416 pos_type pos = cur.pos();
1417 BufferParams const & bufparams = bv->buffer().params();
1418 Layout const & style = par.layout();
1419 InsetLayout const & ilayout = cur.inset().getLayout();
1420 if (!style.pass_thru && !ilayout.isPassThru()
1421 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1422 // this avoids a double undo
1423 // FIXME: should not be needed, ideally
1424 if (!cur.selection())
1426 cap::replaceSelection(cur);
1431 else if (cur.prevInset() && cur.prevInset()->isSpace())
1434 c = par.getChar(pos - 1);
1435 string arg = to_utf8(cmd.argument());
1436 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1437 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1441 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1445 case LFUN_DATE_INSERT: {
1446 string const format = cmd.argument().empty()
1447 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1448 string const time = formatted_time(current_time(), format);
1449 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1453 case LFUN_MOUSE_TRIPLE:
1454 if (cmd.button() == mouse_button::button1) {
1455 tm->cursorHome(cur);
1463 case LFUN_MOUSE_DOUBLE:
1464 if (cmd.button() == mouse_button::button1) {
1465 selectWord(cur, WHOLE_WORD_STRICT);
1470 // Single-click on work area
1471 case LFUN_MOUSE_PRESS:
1472 // We are not marking a selection with the keyboard in any case.
1473 cur.bv().cursor().setMark(false);
1474 switch (cmd.button()) {
1475 case mouse_button::button1:
1477 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1478 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1481 case mouse_button::button2:
1482 // Middle mouse pasting.
1483 bv->mouseSetCursor(cur);
1485 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1486 "selection-paste ; primary-selection-paste paragraph"));
1490 case mouse_button::button3: {
1491 Cursor const & bvcur = cur.bv().cursor();
1492 // Don't do anything if we right-click a
1493 // selection, a context menu will popup.
1494 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1495 && cur < bvcur.selectionEnd()) {
1499 if (!bv->mouseSetCursor(cur, false))
1500 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1506 } // switch (cmd.button())
1509 case LFUN_MOUSE_MOTION: {
1510 // Mouse motion with right or middle mouse do nothing for now.
1511 if (cmd.button() != mouse_button::button1) {
1515 // ignore motions deeper nested than the real anchor
1516 Cursor & bvcur = cur.bv().cursor();
1517 if (!bvcur.anchor_.hasPart(cur)) {
1521 CursorSlice old = bvcur.top();
1523 int const wh = bv->workHeight();
1524 int const y = max(0, min(wh - 1, cmd.y));
1526 tm->setCursorFromCoordinates(cur, cmd.x, y);
1527 cur.setTargetX(cmd.x);
1529 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1531 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1532 // This is to allow jumping over large insets
1533 if (cur.top() == old) {
1535 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1537 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1539 // We continue with our existing selection or start a new one, so don't
1540 // reset the anchor.
1541 bvcur.setCursor(cur);
1542 bvcur.setSelection(true);
1543 if (cur.top() == old) {
1544 // We didn't move one iota, so no need to update the screen.
1545 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1552 case LFUN_MOUSE_RELEASE:
1553 switch (cmd.button()) {
1554 case mouse_button::button1:
1555 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1556 // If there is a new selection, update persistent selection;
1557 // otherwise, single click does not clear persistent selection
1559 if (cur.selection()) {
1560 // Finish selection. If double click,
1561 // cur is moved to the end of word by
1562 // selectWord but bvcur is current
1564 cur.bv().cursor().setSelection();
1565 // We might have removed an empty but drawn selection
1566 // (probably a margin)
1567 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1570 // FIXME: We could try to handle drag and drop of selection here.
1573 case mouse_button::button2:
1574 // Middle mouse pasting is handled at mouse press time,
1575 // see LFUN_MOUSE_PRESS.
1579 case mouse_button::button3:
1580 // Cursor was set at LFUN_MOUSE_PRESS time.
1581 // FIXME: If there is a selection we could try to handle a special
1582 // drag & drop context menu.
1586 case mouse_button::none:
1587 case mouse_button::button4:
1588 case mouse_button::button5:
1590 } // switch (cmd.button())
1594 case LFUN_SELF_INSERT: {
1595 if (cmd.argument().empty())
1598 // Automatically delete the currently selected
1599 // text and replace it with what is being
1600 // typed in now. Depends on lyxrc settings
1601 // "auto_region_delete", which defaults to
1604 if (lyxrc.auto_region_delete && cur.selection())
1605 cutSelection(cur, false, false);
1607 cur.clearSelection();
1609 docstring::const_iterator cit = cmd.argument().begin();
1610 docstring::const_iterator const end = cmd.argument().end();
1611 for (; cit != end; ++cit)
1612 bv->translateAndInsert(*cit, this, cur);
1615 moveCursor(cur, false);
1616 bv->bookmarkEditPosition();
1620 case LFUN_HYPERLINK_INSERT: {
1621 InsetCommandParams p(HYPERLINK_CODE);
1623 if (cur.selection()) {
1624 content = cur.selectionAsString(false);
1625 cutSelection(cur, true, false);
1627 p["target"] = (cmd.argument().empty()) ?
1628 content : cmd.argument();
1629 string const data = InsetCommand::params2string("href", p);
1630 if (p["target"].empty()) {
1631 bv->showDialog("href", data);
1633 FuncRequest fr(LFUN_INSET_INSERT, data);
1639 case LFUN_LABEL_INSERT: {
1640 InsetCommandParams p(LABEL_CODE);
1641 // Try to generate a valid label
1642 p["name"] = (cmd.argument().empty()) ?
1643 cur.getPossibleLabel() :
1645 string const data = InsetCommand::params2string("label", p);
1647 if (cmd.argument().empty()) {
1648 bv->showDialog("label", data);
1650 FuncRequest fr(LFUN_INSET_INSERT, data);
1656 case LFUN_INFO_INSERT: {
1658 if (cmd.argument().empty() && cur.selection()) {
1659 // if command argument is empty use current selection as parameter.
1660 docstring ds = cur.selectionAsString(false);
1661 cutSelection(cur, true, false);
1662 FuncRequest cmd0(cmd, ds);
1663 inset = createInset(cur.bv().buffer(), cmd0);
1665 inset = createInset(cur.bv().buffer(), cmd);
1670 insertInset(cur, inset);
1674 case LFUN_CAPTION_INSERT:
1675 case LFUN_FOOTNOTE_INSERT:
1676 case LFUN_NOTE_INSERT:
1677 case LFUN_FLEX_INSERT:
1678 case LFUN_BOX_INSERT:
1679 case LFUN_BRANCH_INSERT:
1680 case LFUN_PHANTOM_INSERT:
1681 case LFUN_ERT_INSERT:
1682 case LFUN_LISTING_INSERT:
1683 case LFUN_MARGINALNOTE_INSERT:
1684 case LFUN_OPTIONAL_INSERT:
1685 case LFUN_INDEX_INSERT:
1686 // Open the inset, and move the current selection
1688 doInsertInset(cur, this, cmd, true, true);
1690 // Some insets are numbered, others are shown in the outline pane so
1691 // let's update the labels and the toc backend.
1692 bv->buffer().updateLabels();
1695 case LFUN_TABULAR_INSERT:
1696 // if there were no arguments, just open the dialog
1697 if (doInsertInset(cur, this, cmd, false, true))
1700 bv->showDialog("tabularcreate");
1704 case LFUN_FLOAT_INSERT:
1705 case LFUN_FLOAT_WIDE_INSERT:
1706 case LFUN_WRAP_INSERT: {
1707 // will some text be moved into the inset?
1708 bool content = cur.selection();
1710 doInsertInset(cur, this, cmd, true, true);
1713 // If some text is moved into the inset, doInsertInset
1714 // puts the cursor outside the inset. To insert the
1715 // caption we put it back into the inset.
1719 ParagraphList & pars = cur.text()->paragraphs();
1721 DocumentClass const & tclass = bv->buffer().params().documentClass();
1723 // add a separate paragraph for the caption inset
1724 pars.push_back(Paragraph());
1725 pars.back().setInsetOwner(&pars[0].inInset());
1726 pars.back().setPlainOrDefaultLayout(tclass);
1727 int cap_pit = pars.size() - 1;
1729 // if an empty inset was created, we create an additional empty
1730 // paragraph at the bottom so that the user can choose where to put
1731 // the graphics (or table).
1733 pars.push_back(Paragraph());
1734 pars.back().setInsetOwner(&pars[0].inInset());
1735 pars.back().setPlainOrDefaultLayout(tclass);
1738 // reposition the cursor to the caption
1739 cur.pit() = cap_pit;
1741 // FIXME: This Text/Cursor dispatch handling is a mess!
1742 // We cannot use Cursor::dispatch here it needs access to up to
1744 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1745 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1746 bv->buffer().updateLabels();
1747 cur.updateFlags(Update::Force);
1748 // FIXME: When leaving the Float (or Wrap) inset we should
1749 // delete any empty paragraph left above or below the
1754 case LFUN_NOMENCL_INSERT: {
1755 InsetCommandParams p(NOMENCL_CODE);
1756 if (cmd.argument().empty())
1757 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1759 p["symbol"] = cmd.argument();
1760 string const data = InsetCommand::params2string("nomenclature", p);
1761 bv->showDialog("nomenclature", data);
1765 case LFUN_INDEX_PRINT: {
1766 InsetCommandParams p(INDEX_PRINT_CODE);
1767 if (cmd.argument().empty())
1768 p["type"] = from_ascii("idx");
1770 p["type"] = cmd.argument();
1771 string const data = InsetCommand::params2string("index_print", p);
1772 FuncRequest fr(LFUN_INSET_INSERT, data);
1777 case LFUN_NOMENCL_PRINT:
1778 case LFUN_TOC_INSERT:
1779 case LFUN_LINE_INSERT:
1780 case LFUN_NEWPAGE_INSERT:
1782 doInsertInset(cur, this, cmd, false, false);
1786 case LFUN_DEPTH_DECREMENT:
1787 changeDepth(cur, DEC_DEPTH);
1790 case LFUN_DEPTH_INCREMENT:
1791 changeDepth(cur, INC_DEPTH);
1794 case LFUN_MATH_DISPLAY:
1795 mathDispatch(cur, cmd, true);
1798 case LFUN_REGEXP_MODE:
1799 regexpDispatch(cur, cmd);
1802 case LFUN_MATH_MODE:
1803 if (cmd.argument() == "on")
1804 // don't pass "on" as argument
1805 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1807 mathDispatch(cur, cmd, false);
1810 case LFUN_MATH_MACRO:
1811 if (cmd.argument().empty())
1812 cur.errorMessage(from_utf8(N_("Missing argument")));
1814 string s = to_utf8(cmd.argument());
1815 string const s1 = token(s, ' ', 1);
1816 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1817 string const s2 = token(s, ' ', 2);
1818 MacroType type = MacroTypeNewcommand;
1820 type = MacroTypeDef;
1821 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1822 inset->setBuffer(bv->buffer());
1823 insertInset(cur, inset);
1825 // enter macro inset and select the name
1827 cur.top().pos() = cur.top().lastpos();
1829 cur.setSelection(true);
1830 cur.top().pos() = 0;
1834 // passthrough hat and underscore outside mathed:
1835 case LFUN_MATH_SUBSCRIPT:
1836 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1838 case LFUN_MATH_SUPERSCRIPT:
1839 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1842 case LFUN_MATH_INSERT:
1843 case LFUN_MATH_AMS_MATRIX:
1844 case LFUN_MATH_MATRIX:
1845 case LFUN_MATH_DELIM:
1846 case LFUN_MATH_BIGDELIM: {
1848 cap::replaceSelection(cur);
1849 cur.insert(new InsetMathHull(hullSimple));
1850 checkAndActivateInset(cur, true);
1851 LASSERT(cur.inMathed(), /**/);
1856 case LFUN_FONT_EMPH: {
1857 Font font(ignore_font, ignore_language);
1858 font.fontInfo().setEmph(FONT_TOGGLE);
1859 toggleAndShow(cur, this, font);
1863 case LFUN_FONT_ITAL: {
1864 Font font(ignore_font, ignore_language);
1865 font.fontInfo().setShape(ITALIC_SHAPE);
1866 toggleAndShow(cur, this, font);
1870 case LFUN_FONT_BOLD:
1871 case LFUN_FONT_BOLDSYMBOL: {
1872 Font font(ignore_font, ignore_language);
1873 font.fontInfo().setSeries(BOLD_SERIES);
1874 toggleAndShow(cur, this, font);
1878 case LFUN_FONT_NOUN: {
1879 Font font(ignore_font, ignore_language);
1880 font.fontInfo().setNoun(FONT_TOGGLE);
1881 toggleAndShow(cur, this, font);
1885 case LFUN_FONT_TYPEWRITER: {
1886 Font font(ignore_font, ignore_language);
1887 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1888 toggleAndShow(cur, this, font);
1892 case LFUN_FONT_SANS: {
1893 Font font(ignore_font, ignore_language);
1894 font.fontInfo().setFamily(SANS_FAMILY);
1895 toggleAndShow(cur, this, font);
1899 case LFUN_FONT_ROMAN: {
1900 Font font(ignore_font, ignore_language);
1901 font.fontInfo().setFamily(ROMAN_FAMILY);
1902 toggleAndShow(cur, this, font);
1906 case LFUN_FONT_DEFAULT: {
1907 Font font(inherit_font, ignore_language);
1908 toggleAndShow(cur, this, font);
1912 case LFUN_FONT_STRIKEOUT: {
1913 Font font(ignore_font, ignore_language);
1914 font.fontInfo().setStrikeout(FONT_TOGGLE);
1915 toggleAndShow(cur, this, font);
1919 case LFUN_FONT_UULINE: {
1920 Font font(ignore_font, ignore_language);
1921 font.fontInfo().setUuline(FONT_TOGGLE);
1922 toggleAndShow(cur, this, font);
1926 case LFUN_FONT_UWAVE: {
1927 Font font(ignore_font, ignore_language);
1928 font.fontInfo().setUwave(FONT_TOGGLE);
1929 toggleAndShow(cur, this, font);
1933 case LFUN_FONT_UNDERLINE: {
1934 Font font(ignore_font, ignore_language);
1935 font.fontInfo().setUnderbar(FONT_TOGGLE);
1936 toggleAndShow(cur, this, font);
1940 case LFUN_FONT_SIZE: {
1941 Font font(ignore_font, ignore_language);
1942 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1943 toggleAndShow(cur, this, font);
1947 case LFUN_LANGUAGE: {
1948 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1951 Font font(ignore_font, lang);
1952 toggleAndShow(cur, this, font);
1956 case LFUN_TEXTSTYLE_APPLY:
1957 toggleAndShow(cur, this, freefont, toggleall);
1958 cur.message(_("Character set"));
1961 // Set the freefont using the contents of \param data dispatched from
1962 // the frontends and apply it at the current cursor location.
1963 case LFUN_TEXTSTYLE_UPDATE: {
1966 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1969 toggleAndShow(cur, this, freefont, toggleall);
1970 cur.message(_("Character set"));
1972 lyxerr << "Argument not ok";
1977 case LFUN_FINISHED_LEFT:
1978 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1979 // We're leaving an inset, going left. If the inset is LTR, we're
1980 // leaving from the front, so we should not move (remain at --- but
1981 // not in --- the inset). If the inset is RTL, move left, without
1982 // entering the inset itself; i.e., move to after the inset.
1983 if (cur.paragraph().getFontSettings(
1984 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1985 cursorVisLeft(cur, true);
1988 case LFUN_FINISHED_RIGHT:
1989 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1990 // We're leaving an inset, going right. If the inset is RTL, we're
1991 // leaving from the front, so we should not move (remain at --- but
1992 // not in --- the inset). If the inset is LTR, move right, without
1993 // entering the inset itself; i.e., move to after the inset.
1994 if (!cur.paragraph().getFontSettings(
1995 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1996 cursorVisRight(cur, true);
1999 case LFUN_FINISHED_BACKWARD:
2000 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
2003 case LFUN_FINISHED_FORWARD:
2004 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
2006 cur.setCurrentFont();
2009 case LFUN_LAYOUT_PARAGRAPH: {
2011 params2string(cur.paragraph(), data);
2012 data = "show\n" + data;
2013 bv->showDialog("paragraph", data);
2017 case LFUN_PARAGRAPH_UPDATE: {
2019 params2string(cur.paragraph(), data);
2021 // Will the paragraph accept changes from the dialog?
2023 cur.inset().allowParagraphCustomization(cur.idx());
2025 data = "update " + convert<string>(accept) + '\n' + data;
2026 bv->updateDialog("paragraph", data);
2030 case LFUN_ACCENT_UMLAUT:
2031 case LFUN_ACCENT_CIRCUMFLEX:
2032 case LFUN_ACCENT_GRAVE:
2033 case LFUN_ACCENT_ACUTE:
2034 case LFUN_ACCENT_TILDE:
2035 case LFUN_ACCENT_CEDILLA:
2036 case LFUN_ACCENT_MACRON:
2037 case LFUN_ACCENT_DOT:
2038 case LFUN_ACCENT_UNDERDOT:
2039 case LFUN_ACCENT_UNDERBAR:
2040 case LFUN_ACCENT_CARON:
2041 case LFUN_ACCENT_BREVE:
2042 case LFUN_ACCENT_TIE:
2043 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2044 case LFUN_ACCENT_CIRCLE:
2045 case LFUN_ACCENT_OGONEK:
2046 theLyXFunc().handleKeyFunc(cmd.action);
2047 if (!cmd.argument().empty())
2048 // FIXME: Are all these characters encoded in one byte in utf8?
2049 bv->translateAndInsert(cmd.argument()[0], this, cur);
2052 case LFUN_FLOAT_LIST_INSERT: {
2053 DocumentClass const & tclass = bv->buffer().params().documentClass();
2054 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
2056 if (cur.selection())
2057 cutSelection(cur, true, false);
2058 breakParagraph(cur);
2060 if (cur.lastpos() != 0) {
2061 cursorBackward(cur);
2062 breakParagraph(cur);
2065 docstring const laystr = cur.inset().usePlainLayout() ?
2066 tclass.plainLayoutName() :
2067 tclass.defaultLayoutName();
2068 setLayout(cur, laystr);
2069 ParagraphParameters p;
2070 // FIXME If this call were replaced with one to clearParagraphParams(),
2071 // then we could get rid of this method altogether.
2072 setParagraphs(cur, p);
2073 // FIXME This should be simplified when InsetFloatList takes a
2074 // Buffer in its constructor.
2075 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
2076 ifl->setBuffer(bv->buffer());
2077 insertInset(cur, ifl);
2080 lyxerr << "Non-existent float type: "
2081 << to_utf8(cmd.argument()) << endl;
2086 case LFUN_CHANGE_ACCEPT: {
2087 acceptOrRejectChanges(cur, ACCEPT);
2091 case LFUN_CHANGE_REJECT: {
2092 acceptOrRejectChanges(cur, REJECT);
2096 case LFUN_THESAURUS_ENTRY: {
2097 docstring arg = cmd.argument();
2099 arg = cur.selectionAsString(false);
2101 if (arg.size() > 100 || arg.empty()) {
2102 // Get word or selection
2103 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2104 arg = cur.selectionAsString(false);
2105 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2108 bv->showDialog("thesaurus", to_utf8(arg));
2112 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2113 // Given data, an encoding of the ParagraphParameters
2114 // generated in the Paragraph dialog, this function sets
2115 // the current paragraph, or currently selected paragraphs,
2117 // NOTE: This function overrides all existing settings.
2118 setParagraphs(cur, cmd.argument());
2119 cur.message(_("Paragraph layout set"));
2123 case LFUN_PARAGRAPH_PARAMS: {
2124 // Given data, an encoding of the ParagraphParameters as we'd
2125 // find them in a LyX file, this function modifies the current paragraph,
2126 // or currently selected paragraphs.
2127 // NOTE: This function only modifies, and does not override, existing
2129 setParagraphs(cur, cmd.argument(), true);
2130 cur.message(_("Paragraph layout set"));
2135 if (cur.selection()) {
2136 cur.setSelection(false);
2139 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2140 // correct, but I'm not 100% sure -- dov, 071019
2141 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2145 case LFUN_OUTLINE_UP:
2146 outline(OutlineUp, cur);
2147 setCursor(cur, cur.pit(), 0);
2148 cur.buffer()->updateLabels();
2152 case LFUN_OUTLINE_DOWN:
2153 outline(OutlineDown, cur);
2154 setCursor(cur, cur.pit(), 0);
2155 cur.buffer()->updateLabels();
2159 case LFUN_OUTLINE_IN:
2160 outline(OutlineIn, cur);
2161 cur.buffer()->updateLabels();
2165 case LFUN_OUTLINE_OUT:
2166 outline(OutlineOut, cur);
2167 cur.buffer()->updateLabels();
2171 case LFUN_OUTLINE_DRAGMOVE: {
2172 int const move_id = convert<int>(cmd.getArg(0));
2173 int const move_to_id = convert<int>(cmd.getArg(1));
2174 dragMove(cur, move_id, move_to_id);
2175 setCursor(cur, cur.pit(), 0);
2176 cur.buffer()->updateLabels();
2182 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2187 if (lyxrc.spellcheck_continuously && cur.inTexted())
2188 // Take this opportunity to spellcheck current word.
2189 cur.paragraph().isMisspelled(cur.pos());
2191 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2193 // FIXME: The cursor flag is reset two lines below
2194 // so we need to check here if some of the LFUN did touch that.
2195 // for now only Text::erase() and Text::backspace() do that.
2196 // The plan is to verify all the LFUNs and then to remove this
2197 // singleParUpdate boolean altogether.
2198 if (cur.result().update() & Update::Force) {
2199 singleParUpdate = false;
2203 // FIXME: the following code should go in favor of fine grained
2204 // update flag treatment.
2205 if (singleParUpdate) {
2206 // Inserting characters does not change par height in general. So, try
2207 // to update _only_ this paragraph. BufferView will detect if a full
2208 // metrics update is needed anyway.
2209 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2214 && &oldTopSlice.inset() == &cur.inset()
2215 && oldTopSlice.idx() == cur.idx()
2216 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2217 && !cur.selection())
2218 // FIXME: it would be better if we could just do this
2220 //if (cur.result().update() != Update::FitCursor)
2223 // But some LFUNs do not set Update::FitCursor when needed, so we
2224 // do it for all. This is not very harmfull as FitCursor will provoke
2225 // a full redraw only if needed but still, a proper review of all LFUN
2226 // should be done and this needsUpdate boolean can then be removed.
2227 cur.updateFlags(Update::FitCursor);
2229 cur.updateFlags(Update::Force | Update::FitCursor);
2233 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2234 FuncStatus & flag) const
2236 LASSERT(cur.text() == this, /**/);
2238 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2240 InsetCode code = NO_CODE;
2242 switch (cmd.action) {
2244 case LFUN_DEPTH_DECREMENT:
2245 enable = changeDepthAllowed(cur, DEC_DEPTH);
2248 case LFUN_DEPTH_INCREMENT:
2249 enable = changeDepthAllowed(cur, INC_DEPTH);
2253 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2256 case LFUN_DIALOG_SHOW_NEW_INSET:
2257 if (cmd.argument() == "bibitem")
2258 code = BIBITEM_CODE;
2259 else if (cmd.argument() == "bibtex") {
2261 // not allowed in description items
2262 enable = !inDescriptionItem(cur);
2264 else if (cmd.argument() == "box")
2266 else if (cmd.argument() == "branch")
2268 else if (cmd.argument() == "citation")
2270 else if (cmd.argument() == "ert")
2272 else if (cmd.argument() == "external")
2273 code = EXTERNAL_CODE;
2274 else if (cmd.argument() == "float")
2276 else if (cmd.argument() == "graphics")
2277 code = GRAPHICS_CODE;
2278 else if (cmd.argument() == "href")
2279 code = HYPERLINK_CODE;
2280 else if (cmd.argument() == "include")
2281 code = INCLUDE_CODE;
2282 else if (cmd.argument() == "index")
2284 else if (cmd.argument() == "index_print")
2285 code = INDEX_PRINT_CODE;
2286 else if (cmd.argument() == "nomenclature")
2287 code = NOMENCL_CODE;
2288 else if (cmd.argument() == "nomencl_print")
2289 code = NOMENCL_PRINT_CODE;
2290 else if (cmd.argument() == "label")
2292 else if (cmd.argument() == "note")
2294 else if (cmd.argument() == "phantom")
2295 code = PHANTOM_CODE;
2296 else if (cmd.argument() == "ref")
2298 else if (cmd.argument() == "space")
2300 else if (cmd.argument() == "toc")
2302 else if (cmd.argument() == "vspace")
2304 else if (cmd.argument() == "wrap")
2306 else if (cmd.argument() == "listings")
2307 code = LISTINGS_CODE;
2310 case LFUN_ERT_INSERT:
2313 case LFUN_LISTING_INSERT:
2314 code = LISTINGS_CODE;
2315 // not allowed in description items
2316 enable = !inDescriptionItem(cur);
2318 case LFUN_FOOTNOTE_INSERT:
2321 case LFUN_TABULAR_INSERT:
2322 code = TABULAR_CODE;
2324 case LFUN_MARGINALNOTE_INSERT:
2327 case LFUN_FLOAT_INSERT:
2328 case LFUN_FLOAT_WIDE_INSERT:
2330 // not allowed in description items
2331 enable = !inDescriptionItem(cur);
2333 case LFUN_WRAP_INSERT:
2335 // not allowed in description items
2336 enable = !inDescriptionItem(cur);
2338 case LFUN_FLOAT_LIST_INSERT:
2339 code = FLOAT_LIST_CODE;
2340 // not allowed in description items
2341 enable = !inDescriptionItem(cur);
2343 case LFUN_CAPTION_INSERT:
2344 code = CAPTION_CODE;
2345 // not allowed in description items
2346 enable = !inDescriptionItem(cur);
2348 case LFUN_NOTE_INSERT:
2350 // in commands (sections etc.) and description items,
2351 // only Notes are allowed
2352 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2353 (!cur.paragraph().layout().isCommand()
2354 && !inDescriptionItem(cur)));
2356 case LFUN_FLEX_INSERT: {
2358 string s = cmd.getArg(0);
2360 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2361 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2362 il.lyxtype() != InsetLayout::CUSTOM &&
2363 il.lyxtype() != InsetLayout::ELEMENT &&
2364 il.lyxtype ()!= InsetLayout::STANDARD)
2368 case LFUN_BOX_INSERT:
2371 case LFUN_BRANCH_INSERT:
2373 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2374 && cur.buffer()->params().branchlist().empty())
2377 case LFUN_PHANTOM_INSERT:
2378 code = PHANTOM_CODE;
2380 case LFUN_LABEL_INSERT:
2383 case LFUN_INFO_INSERT:
2386 case LFUN_OPTIONAL_INSERT:
2388 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2389 < cur.paragraph().layout().optionalargs;
2391 case LFUN_INDEX_INSERT:
2394 case LFUN_INDEX_PRINT:
2395 code = INDEX_PRINT_CODE;
2396 // not allowed in description items
2397 enable = !inDescriptionItem(cur);
2399 case LFUN_NOMENCL_INSERT:
2400 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2404 code = NOMENCL_CODE;
2406 case LFUN_NOMENCL_PRINT:
2407 code = NOMENCL_PRINT_CODE;
2408 // not allowed in description items
2409 enable = !inDescriptionItem(cur);
2411 case LFUN_TOC_INSERT:
2413 // not allowed in description items
2414 enable = !inDescriptionItem(cur);
2416 case LFUN_HYPERLINK_INSERT:
2417 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2421 code = HYPERLINK_CODE;
2423 case LFUN_QUOTE_INSERT:
2424 // always allow this, since we will inset a raw quote
2425 // if an inset is not allowed.
2427 case LFUN_SPECIALCHAR_INSERT:
2428 code = SPECIALCHAR_CODE;
2430 case LFUN_SPACE_INSERT:
2431 // slight hack: we know this is allowed in math mode
2436 case LFUN_INSET_MODIFY:
2437 // We need to disable this, because we may get called for a
2439 // InsetTabular::getStatus() -> InsetText::getStatus()
2440 // and we don't handle LFUN_INSET_MODIFY.
2444 case LFUN_FONT_EMPH:
2445 flag.setOnOff(fontinfo.emph() == FONT_ON);
2448 case LFUN_FONT_ITAL:
2449 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2452 case LFUN_FONT_NOUN:
2453 flag.setOnOff(fontinfo.noun() == FONT_ON);
2456 case LFUN_FONT_BOLD:
2457 case LFUN_FONT_BOLDSYMBOL:
2458 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2461 case LFUN_FONT_SANS:
2462 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2465 case LFUN_FONT_ROMAN:
2466 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2469 case LFUN_FONT_TYPEWRITER:
2470 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2475 enable = cur.selection();
2479 if (cmd.argument().empty()) {
2480 if (theClipboard().isInternal())
2481 enable = cap::numberOfSelections() > 0;
2483 enable = !theClipboard().empty();
2487 // we have an argument
2488 string const arg = to_utf8(cmd.argument());
2489 if (isStrUnsignedInt(arg)) {
2490 // it's a number and therefore means the internal stack
2491 unsigned int n = convert<unsigned int>(arg);
2492 enable = cap::numberOfSelections() > n;
2496 // explicit graphics type?
2497 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2498 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2499 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2500 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2501 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2502 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2503 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2504 enable = theClipboard().hasGraphicsContents(type);
2513 case LFUN_CLIPBOARD_PASTE:
2514 enable = !theClipboard().empty();
2517 case LFUN_PRIMARY_SELECTION_PASTE:
2518 enable = cur.selection() || !theSelection().empty();
2521 case LFUN_SELECTION_PASTE:
2522 enable = cap::selection();
2525 case LFUN_PARAGRAPH_MOVE_UP:
2526 enable = cur.pit() > 0 && !cur.selection();
2529 case LFUN_PARAGRAPH_MOVE_DOWN:
2530 enable = cur.pit() < cur.lastpit() && !cur.selection();
2533 case LFUN_INSET_DISSOLVE:
2534 if (!cmd.argument().empty()) {
2535 InsetLayout const & il = cur.inset().getLayout();
2536 InsetLayout::InsetLyXType const type =
2537 translateLyXType(to_utf8(cmd.argument()));
2538 enable = cur.inset().lyxCode() == FLEX_CODE
2539 && il.lyxtype() == type;
2541 enable = ((!isMainText(cur.bv().buffer())
2542 && cur.inset().nargs() == 1)
2544 && cur.nextInset()->nargs() == 1));
2548 case LFUN_CHANGE_ACCEPT:
2549 case LFUN_CHANGE_REJECT:
2550 // In principle, these LFUNs should only be enabled if there
2551 // is a change at the current position/in the current selection.
2552 // However, without proper optimizations, this will inevitably
2553 // result in unacceptable performance - just imagine a user who
2554 // wants to select the complete content of a long document.
2555 if (!cur.selection()) {
2556 Change const & change = cur.paragraph().lookupChange(cur.pos());
2557 enable = change.changed();
2559 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2564 case LFUN_OUTLINE_UP:
2565 case LFUN_OUTLINE_DOWN:
2566 case LFUN_OUTLINE_IN:
2567 case LFUN_OUTLINE_OUT:
2568 case LFUN_OUTLINE_DRAGMOVE:
2569 // FIXME: LyX is not ready for outlining within inset.
2570 enable = isMainText(cur.bv().buffer())
2571 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2574 case LFUN_NEWLINE_INSERT:
2575 // LaTeX restrictions (labels or empty par)
2576 enable = (cur.pos() > cur.paragraph().beginOfBody());
2579 case LFUN_TAB_INSERT:
2580 case LFUN_TAB_DELETE:
2581 enable = cur.inset().getLayout().isPassThru();
2584 case LFUN_SET_GRAPHICS_GROUP: {
2585 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2589 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2593 case LFUN_NEWPAGE_INSERT:
2594 // not allowed in description items
2595 enable = !inDescriptionItem(cur);
2598 case LFUN_MATH_INSERT:
2599 case LFUN_MATH_AMS_MATRIX:
2600 case LFUN_MATH_MATRIX:
2601 case LFUN_MATH_DELIM:
2602 case LFUN_MATH_BIGDELIM:
2603 // not allowed in ERT, for example.
2604 enable = cur.inset().insetAllowed(MATH_CODE);
2607 case LFUN_DATE_INSERT: {
2608 string const format = cmd.argument().empty()
2609 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2610 enable = support::os::is_valid_strftime(format);
2615 // FIXME: use isPassThru() here?
2616 enable = !cur.inset().forceLTR();
2618 case LFUN_BREAK_PARAGRAPH:
2619 enable = cur.inset().getLayout().isMultiPar();
2621 case LFUN_WORD_DELETE_FORWARD:
2622 case LFUN_WORD_DELETE_BACKWARD:
2623 case LFUN_LINE_DELETE:
2624 case LFUN_WORD_FORWARD:
2625 case LFUN_WORD_BACKWARD:
2626 case LFUN_WORD_RIGHT:
2627 case LFUN_WORD_LEFT:
2628 case LFUN_CHAR_FORWARD:
2629 case LFUN_CHAR_FORWARD_SELECT:
2630 case LFUN_CHAR_BACKWARD:
2631 case LFUN_CHAR_BACKWARD_SELECT:
2632 case LFUN_CHAR_LEFT:
2633 case LFUN_CHAR_LEFT_SELECT:
2634 case LFUN_CHAR_RIGHT:
2635 case LFUN_CHAR_RIGHT_SELECT:
2637 case LFUN_UP_SELECT:
2639 case LFUN_DOWN_SELECT:
2640 case LFUN_PARAGRAPH_UP_SELECT:
2641 case LFUN_PARAGRAPH_DOWN_SELECT:
2642 case LFUN_LINE_BEGIN_SELECT:
2643 case LFUN_LINE_END_SELECT:
2644 case LFUN_WORD_FORWARD_SELECT:
2645 case LFUN_WORD_BACKWARD_SELECT:
2646 case LFUN_WORD_RIGHT_SELECT:
2647 case LFUN_WORD_LEFT_SELECT:
2648 case LFUN_WORD_SELECT:
2649 case LFUN_SECTION_SELECT:
2650 case LFUN_PARAGRAPH_UP:
2651 case LFUN_PARAGRAPH_DOWN:
2652 case LFUN_LINE_BEGIN:
2654 case LFUN_CHAR_DELETE_FORWARD:
2655 case LFUN_CHAR_DELETE_BACKWARD:
2656 case LFUN_PARAGRAPH_SPACING:
2657 case LFUN_INSET_INSERT:
2658 case LFUN_WORD_UPCASE:
2659 case LFUN_WORD_LOWCASE:
2660 case LFUN_WORD_CAPITALIZE:
2661 case LFUN_CHARS_TRANSPOSE:
2662 case LFUN_SERVER_GET_XY:
2663 case LFUN_SERVER_SET_XY:
2664 case LFUN_SERVER_GET_LAYOUT:
2666 case LFUN_SELF_INSERT:
2667 case LFUN_LINE_INSERT:
2668 case LFUN_MATH_DISPLAY:
2669 case LFUN_MATH_MODE:
2670 case LFUN_MATH_MACRO:
2671 case LFUN_MATH_SUBSCRIPT:
2672 case LFUN_MATH_SUPERSCRIPT:
2673 case LFUN_FONT_DEFAULT:
2674 case LFUN_FONT_UNDERLINE:
2675 case LFUN_FONT_STRIKEOUT:
2676 case LFUN_FONT_UULINE:
2677 case LFUN_FONT_UWAVE:
2678 case LFUN_FONT_SIZE:
2679 case LFUN_TEXTSTYLE_APPLY:
2680 case LFUN_TEXTSTYLE_UPDATE:
2681 case LFUN_LAYOUT_PARAGRAPH:
2682 case LFUN_PARAGRAPH_UPDATE:
2683 case LFUN_ACCENT_UMLAUT:
2684 case LFUN_ACCENT_CIRCUMFLEX:
2685 case LFUN_ACCENT_GRAVE:
2686 case LFUN_ACCENT_ACUTE:
2687 case LFUN_ACCENT_TILDE:
2688 case LFUN_ACCENT_CEDILLA:
2689 case LFUN_ACCENT_MACRON:
2690 case LFUN_ACCENT_DOT:
2691 case LFUN_ACCENT_UNDERDOT:
2692 case LFUN_ACCENT_UNDERBAR:
2693 case LFUN_ACCENT_CARON:
2694 case LFUN_ACCENT_BREVE:
2695 case LFUN_ACCENT_TIE:
2696 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2697 case LFUN_ACCENT_CIRCLE:
2698 case LFUN_ACCENT_OGONEK:
2699 case LFUN_THESAURUS_ENTRY:
2700 case LFUN_PARAGRAPH_PARAMS_APPLY:
2701 case LFUN_PARAGRAPH_PARAMS:
2703 case LFUN_BUFFER_BEGIN:
2704 case LFUN_BUFFER_END:
2705 case LFUN_BUFFER_BEGIN_SELECT:
2706 case LFUN_BUFFER_END_SELECT:
2707 case LFUN_INSET_BEGIN:
2708 case LFUN_INSET_END:
2709 case LFUN_INSET_BEGIN_SELECT:
2710 case LFUN_INSET_END_SELECT:
2711 case LFUN_UNICODE_INSERT:
2712 // these are handled in our dispatch()
2721 && (cur.empty() || !cur.inset().insetAllowed(code)))
2724 switch (cmd.action) {
2725 case LFUN_ACCENT_ACUTE:
2726 case LFUN_ACCENT_BREVE:
2727 case LFUN_ACCENT_CARON:
2728 case LFUN_ACCENT_CEDILLA:
2729 case LFUN_ACCENT_CIRCLE:
2730 case LFUN_ACCENT_CIRCUMFLEX:
2731 case LFUN_ACCENT_DOT:
2732 case LFUN_ACCENT_GRAVE:
2733 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2734 case LFUN_ACCENT_MACRON:
2735 case LFUN_ACCENT_OGONEK:
2736 case LFUN_ACCENT_TIE:
2737 case LFUN_ACCENT_TILDE:
2738 case LFUN_ACCENT_UMLAUT:
2739 case LFUN_ACCENT_UNDERBAR:
2740 case LFUN_ACCENT_UNDERDOT:
2742 case LFUN_BOX_INSERT:
2743 case LFUN_BRANCH_INSERT:
2744 case LFUN_CAPTION_INSERT:
2745 case LFUN_DEPTH_DECREMENT:
2746 case LFUN_DEPTH_INCREMENT:
2747 case LFUN_ERT_INSERT:
2748 case LFUN_FILE_INSERT:
2749 case LFUN_FLEX_INSERT:
2750 case LFUN_FLOAT_INSERT:
2751 case LFUN_FLOAT_LIST_INSERT:
2752 case LFUN_FLOAT_WIDE_INSERT:
2753 case LFUN_FONT_BOLD:
2754 case LFUN_FONT_BOLDSYMBOL:
2755 case LFUN_FONT_TYPEWRITER:
2756 case LFUN_FONT_DEFAULT:
2757 case LFUN_FONT_EMPH:
2758 case LFUN_FONT_NOUN:
2759 case LFUN_FONT_ROMAN:
2760 case LFUN_FONT_SANS:
2761 case LFUN_FONT_FRAK:
2762 case LFUN_FONT_ITAL:
2763 case LFUN_FONT_SIZE:
2764 case LFUN_FONT_STATE:
2765 case LFUN_FONT_UNDERLINE:
2766 case LFUN_FONT_STRIKEOUT:
2767 case LFUN_FONT_UULINE:
2768 case LFUN_FONT_UWAVE:
2769 case LFUN_FOOTNOTE_INSERT:
2770 case LFUN_HYPERLINK_INSERT:
2771 case LFUN_INDEX_INSERT:
2772 case LFUN_INDEX_PRINT:
2773 case LFUN_INSET_INSERT:
2774 case LFUN_LABEL_GOTO:
2775 case LFUN_LABEL_INSERT:
2776 case LFUN_LAYOUT_TABULAR:
2777 case LFUN_LINE_INSERT:
2778 case LFUN_MARGINALNOTE_INSERT:
2779 case LFUN_MATH_DISPLAY:
2780 case LFUN_MATH_INSERT:
2781 case LFUN_MATH_AMS_MATRIX:
2782 case LFUN_MATH_MATRIX:
2783 case LFUN_MATH_MODE:
2784 case LFUN_MENU_OPEN:
2785 case LFUN_NEWLINE_INSERT:
2786 case LFUN_NEWPAGE_INSERT:
2788 case LFUN_NOMENCL_INSERT:
2789 case LFUN_NOMENCL_PRINT:
2790 case LFUN_NOTE_INSERT:
2791 case LFUN_NOTE_NEXT:
2792 case LFUN_OPTIONAL_INSERT:
2793 case LFUN_PHANTOM_INSERT:
2794 case LFUN_REFERENCE_NEXT:
2795 case LFUN_SERVER_GOTO_FILE_ROW:
2796 case LFUN_SERVER_NOTIFY:
2797 case LFUN_SERVER_SET_XY:
2798 case LFUN_SPACE_INSERT:
2799 case LFUN_SPECIALCHAR_INSERT:
2800 case LFUN_TABULAR_INSERT:
2801 case LFUN_TEXTSTYLE_APPLY:
2802 case LFUN_TEXTSTYLE_UPDATE:
2803 case LFUN_TOC_INSERT:
2804 case LFUN_WRAP_INSERT:
2805 if (cur.inset().getLayout().isPassThru())
2812 flag.setEnabled(enable);
2817 void Text::pasteString(Cursor & cur, docstring const & clip,
2820 cur.clearSelection();
2821 if (!clip.empty()) {
2824 insertStringAsParagraphs(cur, clip);
2826 insertStringAsLines(cur, clip);
2831 // FIXME: an item inset would make things much easier.
2832 bool Text::inDescriptionItem(Cursor & cur) const
2834 Paragraph & par = cur.paragraph();
2835 pos_type const pos = cur.pos();
2836 pos_type const body_pos = par.beginOfBody();
2838 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2839 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2840 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2843 return (pos < body_pos
2845 && (pos == 0 || par.getChar(pos - 1) != ' ')));