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 "ParagraphParameters.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/Application.h"
50 #include "frontends/Clipboard.h"
51 #include "frontends/Selection.h"
53 #include "insets/InsetCollapsable.h"
54 #include "insets/InsetCommand.h"
55 #include "insets/InsetExternal.h"
56 #include "insets/InsetFloat.h"
57 #include "insets/InsetFloatList.h"
58 #include "insets/InsetGraphics.h"
59 #include "insets/InsetGraphicsParams.h"
60 #include "insets/InsetNewline.h"
61 #include "insets/InsetQuotes.h"
62 #include "insets/InsetSpecialChar.h"
63 #include "insets/InsetText.h"
64 #include "insets/InsetWrap.h"
66 #include "support/convert.h"
67 #include "support/debug.h"
68 #include "support/gettext.h"
69 #include "support/lstrings.h"
70 #include "support/lyxtime.h"
71 #include "support/os.h"
73 #include "mathed/InsetMathHull.h"
74 #include "mathed/MathMacroTemplate.h"
76 #include <boost/next_prior.hpp>
82 using namespace lyx::support;
86 using cap::copySelection;
87 using cap::cutSelection;
88 using cap::pasteFromStack;
89 using cap::pasteClipboardText;
90 using cap::pasteClipboardGraphics;
91 using cap::replaceSelection;
92 using cap::grabAndEraseSelection;
93 using cap::selClearOrDel;
96 static Font freefont(ignore_font, ignore_language);
97 static bool toggleall = false;
99 static void toggleAndShow(Cursor & cur, Text * text,
100 Font const & font, bool toggleall = true)
102 text->toggleFree(cur, font, toggleall);
104 if (font.language() != ignore_language ||
105 font.fontInfo().number() != FONT_IGNORE) {
106 TextMetrics const & tm = cur.bv().textMetrics(text);
107 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
108 cur.real_current_font))
109 text->setCursor(cur, cur.pit(), cur.pos(),
110 false, !cur.boundary());
115 static void moveCursor(Cursor & cur, bool selecting)
117 if (selecting || cur.mark())
122 static void finishChange(Cursor & cur, bool selecting)
125 moveCursor(cur, selecting);
129 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
132 docstring sel = cur.selectionAsString(false);
134 // It may happen that sel is empty but there is a selection
135 replaceSelection(cur);
137 // Is this a valid formula?
141 #ifdef ENABLE_ASSERTIONS
142 const int old_pos = cur.pos();
144 cur.insert(new InsetMathHull(hullSimple));
145 #ifdef ENABLE_ASSERTIONS
146 LASSERT(old_pos == cur.pos(), /**/);
148 cur.nextInset()->edit(cur, true);
149 // don't do that also for LFUN_MATH_MODE
150 // unless you want end up with always changing
151 // to mathrm when opening an inlined inset --
152 // I really hate "LyXfunc overloading"...
154 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
155 // Avoid an unnecessary undo step if cmd.argument
157 if (!cmd.argument().empty())
158 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
161 // create a macro if we see "\\newcommand"
162 // somewhere, and an ordinary formula
164 if (sel.find(from_ascii("\\newcommand")) == string::npos
165 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
166 && sel.find(from_ascii("\\def")) == string::npos)
168 InsetMathHull * formula = new InsetMathHull;
169 string const selstr = to_utf8(sel);
170 istringstream is(selstr);
173 if (!formula->readQuiet(lex)) {
174 // No valid formula, let's try with delims
175 is.str("$" + selstr + "$");
177 if (!formula->readQuiet(lex)) {
178 // Still not valid, leave it as is
187 cur.insert(new MathMacroTemplate(sel));
191 cur.message(from_utf8(N_("Math editor mode")));
193 cur.message(from_utf8(N_("No valid math formula")));
197 void regexpDispatch(Cursor & cur, FuncRequest const & cmd)
199 BOOST_ASSERT(cmd.action == LFUN_REGEXP_MODE);
200 if (cur.inRegexped()) {
201 cur.message(_("Already in regexp mode"));
205 docstring sel = cur.selectionAsString(false);
207 // It may happen that sel is empty but there is a selection
208 replaceSelection(cur);
210 cur.insert(new InsetMathHull(hullRegexp));
211 cur.nextInset()->edit(cur, true);
214 cur.message(_("Regexp editor mode"));
218 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
221 cap::replaceSelection(cur);
222 cur.insert(new InsetSpecialChar(kind));
227 static 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);
236 if (InsetCollapsable * ci = inset->asInsetCollapsable())
237 ci->setButtonLabel();
240 if (cmd.action == LFUN_INDEX_INSERT) {
241 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
242 text->insertInset(cur, inset);
244 inset->edit(cur, true);
245 // Now put this into inset
246 cur.text()->insertStringAsLines(cur, ds, cur.current_font);
247 cur.leaveInset(*inset);
252 if (cur.selection()) {
253 cutSelection(cur, false, pastesel);
254 cur.clearSelection();
257 text->insertInset(cur, inset);
260 inset->edit(cur, true);
262 if (!gotsel || !pastesel)
265 pasteFromStack(cur, cur.buffer()->errorList("Paste"), 0);
266 cur.buffer()->errors("Paste");
267 cur.clearSelection(); // bug 393
269 InsetText * insetText = dynamic_cast<InsetText *>(inset);
270 if (insetText && (!insetText->allowMultiPar() || cur.lastpit() == 0)) {
271 // reset first par to default
272 cur.text()->paragraphs().begin()
273 ->setPlainOrDefaultLayout(bparams.documentClass());
276 // Merge multiple paragraphs -- hack
277 while (cur.lastpit() > 0)
278 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
279 cur.leaveInset(*inset);
281 cur.leaveInset(*inset);
282 // reset surrounding par to default
283 DocumentClass const & dc = bparams.documentClass();
284 docstring const layoutname = inset->usePlainLayout()
285 ? dc.plainLayoutName()
286 : dc.defaultLayoutName();
287 text->setLayout(cur, layoutname);
294 string const freefont2string()
296 return freefont.toString(toggleall);
300 static void dragMove(Cursor & cur, int moveid, int movetoid)
302 // Create pointers to buffers
303 Buffer & buf_move = *cur.buffer();
304 DocIterator dit_move = buf_move.getParFromID(moveid);
305 DocIterator dit_dest = buf_move.getParFromID(movetoid);
307 pit_type & pit_move = dit_move.pit();
308 pit_type & pit_dest = dit_dest.pit();
309 ParagraphList & pars = dit_move.text()->paragraphs();
311 // Create references to the paragraphs to be moved
312 ParagraphList::iterator const bgn = pars.begin();
313 ParagraphList::iterator dest_start = boost::next(bgn, pit_dest);
315 // The first paragraph of the area to be copied:
316 ParagraphList::iterator start = boost::next(bgn, pit_move);
317 // The final paragraph of area to be copied:
318 ParagraphList::iterator finish = start;
319 ParagraphList::iterator const end = pars.end();
321 // Move out (down) from this section header
325 // Seek the one (on same level) below
326 int const thistoclevel = start->layout().toclevel;
327 for (; finish != end; ++finish) {
328 int const toclevel = finish->layout().toclevel;
329 if (toclevel != Layout::NOT_IN_TOC
330 && toclevel <= thistoclevel)
334 if (start == pars.begin() || start == dest_start)
338 pars.insert(dest_start, start, finish);
339 pars.erase(start, finish);
341 // FIXME: This only really needs doing for the newly
342 // introduced paragraphs. Something like:
343 // pit_type const numpars = distance(start, finish);
344 // start = boost::next(bgn, pit);
345 // finish = boost::next(start, numpars);
346 // for (; start != finish; ++start)
347 // start->setBuffer(buf);
348 // But while this seems to work, it is kind of fragile.
349 buf_move.inset().setBuffer(buf_move);
353 /// the type of outline operation
355 OutlineUp, // Move this header with text down
356 OutlineDown, // Move this header with text up
357 OutlineIn, // Make this header deeper
358 OutlineOut // Make this header shallower
362 static void outline(OutlineOp mode, Cursor & cur)
364 Buffer & buf = *cur.buffer();
365 pit_type & pit = cur.pit();
366 ParagraphList & pars = buf.text().paragraphs();
367 ParagraphList::iterator const bgn = pars.begin();
368 // The first paragraph of the area to be copied:
369 ParagraphList::iterator start = boost::next(bgn, pit);
370 // The final paragraph of area to be copied:
371 ParagraphList::iterator finish = start;
372 ParagraphList::iterator const end = pars.end();
374 DocumentClass const & tc = buf.params().documentClass();
376 int const thistoclevel = start->layout().toclevel;
379 // Move out (down) from this section header
383 // Seek the one (on same level) below
384 for (; finish != end; ++finish) {
385 toclevel = finish->layout().toclevel;
386 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
392 if (start == pars.begin())
395 ParagraphList::iterator dest = start;
396 // Move out (up) from this header
399 // Search previous same-level header above
402 toclevel = dest->layout().toclevel;
404 && (toclevel == Layout::NOT_IN_TOC
405 || toclevel > thistoclevel));
406 // Not found; do nothing
407 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
409 pit_type const newpit = distance(bgn, dest);
410 pit_type const len = distance(start, finish);
411 pit_type const deletepit = pit + len;
412 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
413 pars.splice(dest, start, finish);
421 // Go one down from *this* header:
422 ParagraphList::iterator dest = boost::next(finish, 1);
423 // Go further down to find header to insert in front of:
424 for (; dest != end; ++dest) {
425 toclevel = dest->layout().toclevel;
426 if (toclevel != Layout::NOT_IN_TOC
427 && toclevel <= thistoclevel)
430 // One such was found:
431 pit_type newpit = distance(bgn, dest);
432 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
433 pit_type const len = distance(start, finish);
434 pars.splice(dest, start, finish);
435 cur.pit() = newpit - len;
439 pit_type const len = distance(start, finish);
440 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
441 for (; start != finish; ++start) {
442 toclevel = start->layout().toclevel;
443 if (toclevel == Layout::NOT_IN_TOC)
445 DocumentClass::const_iterator lit = tc.begin();
446 DocumentClass::const_iterator len = tc.end();
447 for (; lit != len; ++lit) {
448 if (lit->toclevel == toclevel + 1 &&
449 start->layout().labeltype == lit->labeltype) {
450 start->setLayout(*lit);
458 pit_type const len = distance(start, finish);
459 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
460 for (; start != finish; ++start) {
461 toclevel = start->layout().toclevel;
462 if (toclevel == Layout::NOT_IN_TOC)
464 DocumentClass::const_iterator lit = tc.begin();
465 DocumentClass::const_iterator len = tc.end();
466 for (; lit != len; ++lit) {
467 if (lit->toclevel == toclevel - 1 &&
468 start->layout().labeltype == lit->labeltype) {
469 start->setLayout(*lit);
480 void Text::number(Cursor & cur)
482 FontInfo font = ignore_font;
483 font.setNumber(FONT_TOGGLE);
484 toggleAndShow(cur, this, Font(font, ignore_language));
488 bool Text::isRTL(Paragraph const & par) const
490 Buffer const & buffer = owner_->buffer();
491 return par.isRTL(buffer.params());
495 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
497 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
499 // Dispatch if the cursor is inside the text. It is not the
500 // case for context menus (bug 5797).
501 if (cur.text() != this) {
506 BufferView * bv = &cur.bv();
507 TextMetrics * tm = &bv->textMetrics(this);
508 if (!tm->contains(cur.pit())) {
509 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
510 tm = &bv->textMetrics(this);
513 // FIXME: We use the update flag to indicates wether a singlePar or a
514 // full screen update is needed. We reset it here but shall we restore it
518 LASSERT(cur.text() == this, /**/);
519 CursorSlice oldTopSlice = cur.top();
520 bool oldBoundary = cur.boundary();
521 bool sel = cur.selection();
522 // Signals that, even if needsUpdate == false, an update of the
523 // cursor paragraph is required
524 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
525 LyXAction::SingleParUpdate);
526 // Signals that a full-screen update is required
527 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
528 LyXAction::NoUpdate) || singleParUpdate);
530 switch (cmd.action) {
532 case LFUN_PARAGRAPH_MOVE_DOWN: {
533 pit_type const pit = cur.pit();
534 recUndo(cur, pit, pit + 1);
536 pars_.swap(pit, pit + 1);
537 cur.buffer()->updateLabels();
543 case LFUN_PARAGRAPH_MOVE_UP: {
544 pit_type const pit = cur.pit();
545 recUndo(cur, pit - 1, pit);
547 pars_.swap(pit, pit - 1);
548 cur.buffer()->updateLabels();
554 case LFUN_APPENDIX: {
555 Paragraph & par = cur.paragraph();
556 bool start = !par.params().startOfAppendix();
558 // FIXME: The code below only makes sense at top level.
559 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
560 // ensure that we have only one start_of_appendix in this document
561 // FIXME: this don't work for multipart document!
562 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
563 if (pars_[tmp].params().startOfAppendix()) {
565 pars_[tmp].params().startOfAppendix(false);
571 par.params().startOfAppendix(start);
573 // we can set the refreshing parameters now
574 cur.buffer()->updateLabels();
578 case LFUN_WORD_DELETE_FORWARD:
580 cutSelection(cur, true, false);
582 deleteWordForward(cur);
583 finishChange(cur, false);
586 case LFUN_WORD_DELETE_BACKWARD:
588 cutSelection(cur, true, false);
590 deleteWordBackward(cur);
591 finishChange(cur, false);
594 case LFUN_LINE_DELETE:
596 cutSelection(cur, true, false);
598 tm->deleteLineForward(cur);
599 finishChange(cur, false);
602 case LFUN_BUFFER_BEGIN:
603 case LFUN_BUFFER_BEGIN_SELECT:
604 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
605 if (cur.depth() == 1)
606 needsUpdate |= cursorTop(cur);
609 cur.updateFlags(Update::FitCursor);
612 case LFUN_BUFFER_END:
613 case LFUN_BUFFER_END_SELECT:
614 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
615 if (cur.depth() == 1)
616 needsUpdate |= cursorBottom(cur);
619 cur.updateFlags(Update::FitCursor);
622 case LFUN_INSET_BEGIN:
623 case LFUN_INSET_BEGIN_SELECT:
624 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
625 if (cur.depth() == 1 || !cur.top().at_begin())
626 needsUpdate |= cursorTop(cur);
629 cur.updateFlags(Update::FitCursor);
633 case LFUN_INSET_END_SELECT:
634 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
635 if (cur.depth() == 1 || !cur.top().at_end())
636 needsUpdate |= cursorBottom(cur);
639 cur.updateFlags(Update::FitCursor);
642 case LFUN_INSET_SELECT_ALL:
643 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
644 || !cur.selEnd().at_end()) {
645 needsUpdate |= cur.selHandle(false);
646 needsUpdate |= cursorTop(cur);
647 needsUpdate |= cur.selHandle(true);
648 needsUpdate |= cursorBottom(cur);
651 cur.updateFlags(Update::FitCursor);
654 case LFUN_CHAR_FORWARD:
655 case LFUN_CHAR_FORWARD_SELECT:
656 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
657 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
658 needsUpdate |= cursorForward(cur);
660 if (!needsUpdate && oldTopSlice == cur.top()
661 && cur.boundary() == oldBoundary) {
663 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
665 // we will probably be moving out the inset, so we should execute
666 // the depm-mechanism, but only when the cursor has a place to
667 // go outside this inset, i.e. in a slice above.
668 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
669 && cur.pit() == cur.lastpit()) {
670 // The cursor hasn't changed yet. To give the
671 // DEPM the possibility of doing something we must
672 // provide it with two different cursors.
674 dummy.pos() = dummy.pit() = 0;
675 cur.bv().checkDepm(dummy, cur);
680 case LFUN_CHAR_BACKWARD:
681 case LFUN_CHAR_BACKWARD_SELECT:
682 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
683 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
684 needsUpdate |= cursorBackward(cur);
686 if (!needsUpdate && oldTopSlice == cur.top()
687 && cur.boundary() == oldBoundary) {
689 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
691 // we will probably be moving out the inset, so we should execute
692 // the depm-mechanism, but only when the cursor has a place to
693 // go outside this inset, i.e. in a slice above.
694 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
695 // The cursor hasn't changed yet. To give the
696 // DEPM the possibility of doing something we must
697 // provide it with two different cursors.
699 dummy.pos() = cur.lastpos();
700 dummy.pit() = cur.lastpit();
701 cur.bv().checkDepm(dummy, cur);
707 case LFUN_CHAR_LEFT_SELECT:
708 if (lyxrc.visual_cursor) {
709 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
710 needsUpdate |= cursorVisLeft(cur);
711 if (!needsUpdate && oldTopSlice == cur.top()
712 && cur.boundary() == oldBoundary) {
714 cmd = FuncRequest(LFUN_FINISHED_LEFT);
717 if (reverseDirectionNeeded(cur)) {
718 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
719 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
721 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
722 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
729 case LFUN_CHAR_RIGHT:
730 case LFUN_CHAR_RIGHT_SELECT:
731 if (lyxrc.visual_cursor) {
732 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
733 needsUpdate |= cursorVisRight(cur);
734 if (!needsUpdate && oldTopSlice == cur.top()
735 && cur.boundary() == oldBoundary) {
737 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
740 if (reverseDirectionNeeded(cur)) {
741 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
742 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
744 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
745 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
754 case LFUN_DOWN_SELECT:
757 // stop/start the selection
758 bool select = cmd.action == LFUN_DOWN_SELECT ||
759 cmd.action == LFUN_UP_SELECT;
761 // move cursor up/down
762 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
763 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
765 if (!atFirstOrLastRow) {
766 needsUpdate |= cur.selHandle(select);
767 cur.selHandle(select);
768 cur.upDownInText(up, needsUpdate);
769 needsUpdate |= cur.beforeDispatchCursor().inMathed();
771 // if the cursor cannot be moved up or down do not remove
772 // the selection right now, but wait for the next dispatch.
774 needsUpdate |= cur.selHandle(select);
775 cur.upDownInText(up, needsUpdate);
782 case LFUN_PARAGRAPH_UP:
783 case LFUN_PARAGRAPH_UP_SELECT:
784 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
785 needsUpdate |= cursorUpParagraph(cur);
788 case LFUN_PARAGRAPH_DOWN:
789 case LFUN_PARAGRAPH_DOWN_SELECT:
790 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
791 needsUpdate |= cursorDownParagraph(cur);
794 case LFUN_LINE_BEGIN:
795 case LFUN_LINE_BEGIN_SELECT:
796 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
797 needsUpdate |= tm->cursorHome(cur);
801 case LFUN_LINE_END_SELECT:
802 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
803 needsUpdate |= tm->cursorEnd(cur);
806 case LFUN_SECTION_SELECT: {
807 Buffer const & buf = *cur.buffer();
808 pit_type const pit = cur.pit();
809 ParagraphList & pars = buf.text().paragraphs();
810 ParagraphList::iterator bgn = pars.begin();
811 // The first paragraph of the area to be selected:
812 ParagraphList::iterator start = boost::next(bgn, pit);
813 // The final paragraph of area to be selected:
814 ParagraphList::iterator finish = start;
815 ParagraphList::iterator end = pars.end();
817 int const thistoclevel = start->layout().toclevel;
818 if (thistoclevel == Layout::NOT_IN_TOC)
822 Cursor const old_cur = cur;
823 needsUpdate |= cur.selHandle(true);
825 // Move out (down) from this section header
829 // Seek the one (on same level) below
830 for (; finish != end; ++finish, ++cur.pit()) {
831 int const toclevel = finish->layout().toclevel;
832 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
835 cur.pos() = cur.lastpos();
837 needsUpdate |= cur != old_cur;
841 case LFUN_WORD_RIGHT:
842 case LFUN_WORD_RIGHT_SELECT:
843 if (lyxrc.visual_cursor) {
844 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
845 needsUpdate |= cursorVisRightOneWord(cur);
846 if (!needsUpdate && oldTopSlice == cur.top()
847 && cur.boundary() == oldBoundary) {
849 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
852 if (reverseDirectionNeeded(cur)) {
853 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
854 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
856 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
857 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
864 case LFUN_WORD_FORWARD:
865 case LFUN_WORD_FORWARD_SELECT:
866 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
867 needsUpdate |= cursorForwardOneWord(cur);
871 case LFUN_WORD_LEFT_SELECT:
872 if (lyxrc.visual_cursor) {
873 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
874 needsUpdate |= cursorVisLeftOneWord(cur);
875 if (!needsUpdate && oldTopSlice == cur.top()
876 && cur.boundary() == oldBoundary) {
878 cmd = FuncRequest(LFUN_FINISHED_LEFT);
881 if (reverseDirectionNeeded(cur)) {
882 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
883 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
885 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
886 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
893 case LFUN_WORD_BACKWARD:
894 case LFUN_WORD_BACKWARD_SELECT:
895 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
896 needsUpdate |= cursorBackwardOneWord(cur);
899 case LFUN_WORD_SELECT: {
900 selectWord(cur, WHOLE_WORD);
901 finishChange(cur, true);
905 case LFUN_NEWLINE_INSERT: {
906 InsetNewlineParams inp;
907 docstring arg = cmd.argument();
908 // this avoids a double undo
909 // FIXME: should not be needed, ideally
910 if (!cur.selection())
912 cap::replaceSelection(cur);
913 if (arg == "linebreak")
914 inp.kind = InsetNewlineParams::LINEBREAK;
916 inp.kind = InsetNewlineParams::NEWLINE;
917 cur.insert(new InsetNewline(inp));
919 moveCursor(cur, false);
923 case LFUN_TAB_INSERT: {
924 bool const multi_par_selection = cur.selection() &&
925 cur.selBegin().pit() != cur.selEnd().pit();
926 if (multi_par_selection) {
927 // If there is a multi-paragraph selection, a tab is inserted
928 // at the beginning of each paragraph.
929 cur.recordUndoSelection();
930 pit_type const pit_end = cur.selEnd().pit();
931 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
932 pars_[pit].insertChar(0, '\t',
933 bv->buffer().params().trackChanges);
934 // Update the selection pos to make sure the selection does not
935 // change as the inserted tab will increase the logical pos.
936 if (cur.anchor_.pit() == pit)
937 cur.anchor_.forwardPos();
938 if (cur.pit() == pit)
943 // Maybe we shouldn't allow tabs within a line, because they
944 // are not (yet) aligned as one might do expect.
945 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
951 case LFUN_TAB_DELETE: {
952 bool const tc = bv->buffer().params().trackChanges;
953 if (cur.selection()) {
954 // If there is a selection, a tab (if present) is removed from
955 // the beginning of each paragraph.
956 cur.recordUndoSelection();
957 pit_type const pit_end = cur.selEnd().pit();
958 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
959 Paragraph & par = paragraphs()[pit];
960 if (par.getChar(0) == '\t') {
961 if (cur.pit() == pit)
963 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
964 cur.anchor_.backwardPos();
966 par.eraseChar(0, tc);
968 // If no tab was present, try to remove up to four spaces.
969 for (int n_spaces = 0;
970 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
971 if (cur.pit() == pit)
973 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
974 cur.anchor_.backwardPos();
976 par.eraseChar(0, tc);
981 // If there is no selection, try to remove a tab or some spaces
982 // before the position of the cursor.
983 Paragraph & par = paragraphs()[cur.pit()];
984 pos_type const pos = cur.pos();
989 char_type const c = par.getChar(pos - 1);
993 par.eraseChar(cur.pos(), tc);
995 for (int n_spaces = 0;
997 && par.getChar(cur.pos() - 1) == ' '
1001 par.eraseChar(cur.pos(), tc);
1008 case LFUN_CHAR_DELETE_FORWARD:
1009 if (!cur.selection()) {
1010 if (cur.pos() == cur.paragraph().size())
1011 // Par boundary, force full-screen update
1012 singleParUpdate = false;
1013 needsUpdate |= erase(cur);
1015 // It is possible to make it a lot faster still
1016 // just comment out the line below...
1018 cutSelection(cur, true, false);
1019 singleParUpdate = false;
1021 moveCursor(cur, false);
1024 case LFUN_CHAR_DELETE_BACKWARD:
1025 if (!cur.selection()) {
1026 if (bv->getIntl().getTransManager().backspace()) {
1027 // Par boundary, full-screen update
1029 singleParUpdate = false;
1030 needsUpdate |= backspace(cur);
1032 // It is possible to make it a lot faster still
1033 // just comment out the line below...
1036 cutSelection(cur, true, false);
1037 singleParUpdate = false;
1041 case LFUN_BREAK_PARAGRAPH:
1042 cap::replaceSelection(cur);
1043 breakParagraph(cur, cmd.argument() == "inverse");
1047 case LFUN_INSET_INSERT: {
1050 // We have to avoid triggering InstantPreview loading
1051 // before inserting into the document. See bug #5626.
1052 bool loaded = bv->buffer().isFullyLoaded();
1053 bv->buffer().setFullyLoaded(false);
1054 Inset * inset = createInset(bv->buffer(), cmd);
1055 bv->buffer().setFullyLoaded(loaded);
1058 // FIXME (Abdel 01/02/2006):
1059 // What follows would be a partial fix for bug 2154:
1060 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
1061 // This automatically put the label inset _after_ a
1062 // numbered section. It should be possible to extend the mechanism
1063 // to any kind of LateX environement.
1064 // The correct way to fix that bug would be at LateX generation.
1065 // I'll let the code here for reference as it could be used for some
1066 // other feature like "automatic labelling".
1068 Paragraph & par = pars_[cur.pit()];
1069 if (inset->lyxCode() == LABEL_CODE
1070 && par.layout().labeltype == LABEL_COUNTER) {
1071 // Go to the end of the paragraph
1072 // Warning: Because of Change-Tracking, the last
1073 // position is 'size()' and not 'size()-1':
1074 cur.pos() = par.size();
1075 // Insert a new paragraph
1076 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1080 if (cur.selection())
1081 cutSelection(cur, true, false);
1085 // trigger InstantPreview now
1086 if (inset->lyxCode() == EXTERNAL_CODE) {
1087 InsetExternal & ins =
1088 static_cast<InsetExternal &>(*inset);
1089 ins.updatePreview();
1096 case LFUN_INSET_DISSOLVE: {
1097 if (dissolveInset(cur))
1102 case LFUN_SET_GRAPHICS_GROUP: {
1103 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1109 string id = to_utf8(cmd.argument());
1110 string grp = graphics::getGroupParams(bv->buffer(), id);
1111 InsetGraphicsParams tmp, inspar = ins->getParams();
1114 inspar.groupId = to_utf8(cmd.argument());
1116 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1117 tmp.filename = inspar.filename;
1121 ins->setParams(inspar);
1124 case LFUN_SPACE_INSERT:
1125 if (cur.paragraph().layout().free_spacing)
1126 insertChar(cur, ' ');
1128 doInsertInset(cur, this, cmd, false, false);
1131 moveCursor(cur, false);
1134 case LFUN_SPECIALCHAR_INSERT: {
1135 string const name = to_utf8(cmd.argument());
1136 if (name == "hyphenation")
1137 specialChar(cur, InsetSpecialChar::HYPHENATION);
1138 else if (name == "ligature-break")
1139 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1140 else if (name == "slash")
1141 specialChar(cur, InsetSpecialChar::SLASH);
1142 else if (name == "nobreakdash")
1143 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1144 else if (name == "dots")
1145 specialChar(cur, InsetSpecialChar::LDOTS);
1146 else if (name == "end-of-sentence")
1147 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1148 else if (name == "menu-separator")
1149 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1150 else if (name.empty())
1151 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1153 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1157 case LFUN_WORD_UPCASE:
1158 changeCase(cur, text_uppercase);
1161 case LFUN_WORD_LOWCASE:
1162 changeCase(cur, text_lowercase);
1165 case LFUN_WORD_CAPITALIZE:
1166 changeCase(cur, text_capitalization);
1169 case LFUN_CHARS_TRANSPOSE:
1170 charsTranspose(cur);
1174 cur.message(_("Paste"));
1175 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1176 cap::replaceSelection(cur);
1178 // without argument?
1179 string const arg = to_utf8(cmd.argument());
1181 if (theClipboard().isInternal())
1182 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1183 else if (theClipboard().hasGraphicsContents()
1184 && !theClipboard().hasTextContents())
1185 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1187 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1188 } else if (isStrUnsignedInt(arg)) {
1189 // we have a numerical argument
1190 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1191 convert<unsigned int>(arg));
1193 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1195 type = Clipboard::PdfGraphicsType;
1196 else if (arg == "png")
1197 type = Clipboard::PngGraphicsType;
1198 else if (arg == "jpeg")
1199 type = Clipboard::JpegGraphicsType;
1200 else if (arg == "linkback")
1201 type = Clipboard::LinkBackGraphicsType;
1202 else if (arg == "emf")
1203 type = Clipboard::EmfGraphicsType;
1204 else if (arg == "wmf")
1205 type = Clipboard::WmfGraphicsType;
1208 LASSERT(false, /**/);
1210 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1213 bv->buffer().errors("Paste");
1214 cur.clearSelection(); // bug 393
1220 cutSelection(cur, true, true);
1221 cur.message(_("Cut"));
1226 cur.message(_("Copy"));
1229 case LFUN_SERVER_GET_XY:
1230 cur.message(from_utf8(
1231 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1232 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1235 case LFUN_SERVER_SET_XY: {
1238 istringstream is(to_utf8(cmd.argument()));
1241 lyxerr << "SETXY: Could not parse coordinates in '"
1242 << to_utf8(cmd.argument()) << endl;
1244 tm->setCursorFromCoordinates(cur, x, y);
1248 case LFUN_SERVER_GET_LAYOUT:
1249 cur.message(cur.paragraph().layout().name());
1253 docstring layout = cmd.argument();
1254 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1256 Paragraph const & para = cur.paragraph();
1257 docstring const old_layout = para.layout().name();
1258 DocumentClass const & tclass = bv->buffer().params().documentClass();
1261 layout = tclass.defaultLayoutName();
1263 if (owner_->forcePlainLayout())
1264 // in this case only the empty layout is allowed
1265 layout = tclass.plainLayoutName();
1266 else if (para.usePlainLayout()) {
1267 // in this case, default layout maps to empty layout
1268 if (layout == tclass.defaultLayoutName())
1269 layout = tclass.plainLayoutName();
1271 // otherwise, the empty layout maps to the default
1272 if (layout == tclass.plainLayoutName())
1273 layout = tclass.defaultLayoutName();
1276 bool hasLayout = tclass.hasLayout(layout);
1278 // If the entry is obsolete, use the new one instead.
1280 docstring const & obs = tclass[layout].obsoleted_by();
1286 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1287 from_utf8(N_(" not known")));
1291 bool change_layout = (old_layout != layout);
1293 if (!change_layout && cur.selection() &&
1294 cur.selBegin().pit() != cur.selEnd().pit())
1296 pit_type spit = cur.selBegin().pit();
1297 pit_type epit = cur.selEnd().pit() + 1;
1298 while (spit != epit) {
1299 if (pars_[spit].layout().name() != old_layout) {
1300 change_layout = true;
1308 setLayout(cur, layout);
1313 case LFUN_CLIPBOARD_PASTE:
1314 cur.clearSelection();
1315 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1316 cmd.argument() == "paragraph");
1317 bv->buffer().errors("Paste");
1320 case LFUN_PRIMARY_SELECTION_PASTE:
1321 pasteString(cur, theSelection().get(),
1322 cmd.argument() == "paragraph");
1325 case LFUN_SELECTION_PASTE:
1326 // Copy the selection buffer to the clipboard stack,
1327 // because we want it to appear in the "Edit->Paste
1329 cap::copySelectionToStack();
1330 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1331 bv->buffer().errors("Paste");
1334 case LFUN_UNICODE_INSERT: {
1335 if (cmd.argument().empty())
1337 docstring hexstring = cmd.argument();
1338 if (isHex(hexstring)) {
1339 char_type c = hexToInt(hexstring);
1340 if (c >= 32 && c < 0x10ffff) {
1341 lyxerr << "Inserting c: " << c << endl;
1342 docstring s = docstring(1, c);
1343 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1349 case LFUN_QUOTE_INSERT: {
1350 Paragraph & par = cur.paragraph();
1351 pos_type pos = cur.pos();
1352 BufferParams const & bufparams = bv->buffer().params();
1353 Layout const & style = par.layout();
1354 InsetLayout const & ilayout = cur.inset().getLayout();
1355 if (!style.pass_thru && !ilayout.isPassThru()
1356 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1357 // this avoids a double undo
1358 // FIXME: should not be needed, ideally
1359 if (!cur.selection())
1361 cap::replaceSelection(cur);
1366 else if (cur.prevInset() && cur.prevInset()->isSpace())
1369 c = par.getChar(pos - 1);
1370 string arg = to_utf8(cmd.argument());
1371 cur.insert(new InsetQuotes(bv->buffer(), c, (arg == "single")
1372 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1376 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1380 case LFUN_DATE_INSERT: {
1381 string const format = cmd.argument().empty()
1382 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1383 string const time = formatted_time(current_time(), format);
1384 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1388 case LFUN_MOUSE_TRIPLE:
1389 if (cmd.button() == mouse_button::button1) {
1390 tm->cursorHome(cur);
1398 case LFUN_MOUSE_DOUBLE:
1399 if (cmd.button() == mouse_button::button1) {
1400 selectWord(cur, WHOLE_WORD_STRICT);
1405 // Single-click on work area
1406 case LFUN_MOUSE_PRESS:
1407 // We are not marking a selection with the keyboard in any case.
1408 cur.bv().cursor().setMark(false);
1409 switch (cmd.button()) {
1410 case mouse_button::button1:
1412 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1413 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1416 case mouse_button::button2:
1417 // Middle mouse pasting.
1418 bv->mouseSetCursor(cur);
1420 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1421 "selection-paste ; primary-selection-paste paragraph"));
1425 case mouse_button::button3: {
1426 Cursor const & bvcur = cur.bv().cursor();
1427 // Don't do anything if we right-click a
1428 // selection, a context menu will popup.
1429 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1430 && cur < bvcur.selectionEnd()) {
1434 if (!bv->mouseSetCursor(cur, false))
1435 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1441 } // switch (cmd.button())
1444 case LFUN_MOUSE_MOTION: {
1445 // Mouse motion with right or middle mouse do nothing for now.
1446 if (cmd.button() != mouse_button::button1) {
1450 // ignore motions deeper nested than the real anchor
1451 Cursor & bvcur = cur.bv().cursor();
1452 if (!bvcur.anchor_.hasPart(cur)) {
1456 CursorSlice old = bvcur.top();
1458 int const wh = bv->workHeight();
1459 int const y = max(0, min(wh - 1, cmd.y));
1461 tm->setCursorFromCoordinates(cur, cmd.x, y);
1462 cur.setTargetX(cmd.x);
1464 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1466 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1467 // This is to allow jumping over large insets
1468 if (cur.top() == old) {
1470 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1472 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1474 // We continue with our existing selection or start a new one, so don't
1475 // reset the anchor.
1476 bvcur.setCursor(cur);
1477 bvcur.setSelection(true);
1478 if (cur.top() == old) {
1479 // We didn't move one iota, so no need to update the screen.
1480 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1487 case LFUN_MOUSE_RELEASE:
1488 switch (cmd.button()) {
1489 case mouse_button::button1:
1490 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1491 // If there is a new selection, update persistent selection;
1492 // otherwise, single click does not clear persistent selection
1494 if (cur.selection()) {
1495 // Finish selection. If double click,
1496 // cur is moved to the end of word by
1497 // selectWord but bvcur is current
1499 cur.bv().cursor().setSelection();
1500 // We might have removed an empty but drawn selection
1501 // (probably a margin)
1502 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1505 // FIXME: We could try to handle drag and drop of selection here.
1508 case mouse_button::button2:
1509 // Middle mouse pasting is handled at mouse press time,
1510 // see LFUN_MOUSE_PRESS.
1514 case mouse_button::button3:
1515 // Cursor was set at LFUN_MOUSE_PRESS time.
1516 // FIXME: If there is a selection we could try to handle a special
1517 // drag & drop context menu.
1521 case mouse_button::none:
1522 case mouse_button::button4:
1523 case mouse_button::button5:
1525 } // switch (cmd.button())
1529 case LFUN_SELF_INSERT: {
1530 if (cmd.argument().empty())
1533 // Automatically delete the currently selected
1534 // text and replace it with what is being
1535 // typed in now. Depends on lyxrc settings
1536 // "auto_region_delete", which defaults to
1539 if (lyxrc.auto_region_delete && cur.selection())
1540 cutSelection(cur, false, false);
1542 cur.clearSelection();
1544 docstring::const_iterator cit = cmd.argument().begin();
1545 docstring::const_iterator const end = cmd.argument().end();
1546 for (; cit != end; ++cit)
1547 bv->translateAndInsert(*cit, this, cur);
1550 moveCursor(cur, false);
1551 bv->bookmarkEditPosition();
1555 case LFUN_HYPERLINK_INSERT: {
1556 InsetCommandParams p(HYPERLINK_CODE);
1558 if (cur.selection()) {
1559 content = cur.selectionAsString(false);
1560 cutSelection(cur, true, false);
1562 p["target"] = (cmd.argument().empty()) ?
1563 content : cmd.argument();
1564 string const data = InsetCommand::params2string("href", p);
1565 if (p["target"].empty()) {
1566 bv->showDialog("href", data);
1568 FuncRequest fr(LFUN_INSET_INSERT, data);
1574 case LFUN_LABEL_INSERT: {
1575 InsetCommandParams p(LABEL_CODE);
1576 // Try to generate a valid label
1577 p["name"] = (cmd.argument().empty()) ?
1578 cur.getPossibleLabel() :
1580 string const data = InsetCommand::params2string("label", p);
1582 if (cmd.argument().empty()) {
1583 bv->showDialog("label", data);
1585 FuncRequest fr(LFUN_INSET_INSERT, data);
1591 case LFUN_INFO_INSERT: {
1593 if (cmd.argument().empty() && cur.selection()) {
1594 // if command argument is empty use current selection as parameter.
1595 docstring ds = cur.selectionAsString(false);
1596 cutSelection(cur, true, false);
1597 FuncRequest cmd0(cmd, ds);
1598 inset = createInset(cur.bv().buffer(), cmd0);
1600 inset = createInset(cur.bv().buffer(), cmd);
1605 insertInset(cur, inset);
1609 case LFUN_CAPTION_INSERT:
1610 case LFUN_FOOTNOTE_INSERT:
1611 case LFUN_NOTE_INSERT:
1612 case LFUN_FLEX_INSERT:
1613 case LFUN_BOX_INSERT:
1614 case LFUN_BRANCH_INSERT:
1615 case LFUN_PHANTOM_INSERT:
1616 case LFUN_ERT_INSERT:
1617 case LFUN_LISTING_INSERT:
1618 case LFUN_MARGINALNOTE_INSERT:
1619 case LFUN_OPTIONAL_INSERT:
1620 case LFUN_INDEX_INSERT:
1621 // Open the inset, and move the current selection
1623 doInsertInset(cur, this, cmd, true, true);
1625 // Some insets are numbered, others are shown in the outline pane so
1626 // let's update the labels and the toc backend.
1627 bv->buffer().updateLabels();
1630 case LFUN_TABULAR_INSERT:
1631 // if there were no arguments, just open the dialog
1632 if (doInsertInset(cur, this, cmd, false, true))
1635 bv->showDialog("tabularcreate");
1639 case LFUN_FLOAT_INSERT:
1640 case LFUN_FLOAT_WIDE_INSERT:
1641 case LFUN_WRAP_INSERT: {
1642 // will some text be moved into the inset?
1643 bool content = cur.selection();
1645 doInsertInset(cur, this, cmd, true, true);
1648 // If some text is moved into the inset, doInsertInset
1649 // puts the cursor outside the inset. To insert the
1650 // caption we put it back into the inset.
1654 ParagraphList & pars = cur.text()->paragraphs();
1656 DocumentClass const & tclass = bv->buffer().params().documentClass();
1658 // add a separate paragraph for the caption inset
1659 pars.push_back(Paragraph());
1660 pars.back().setInsetOwner(&cur.text()->inset());
1661 pars.back().setPlainOrDefaultLayout(tclass);
1662 int cap_pit = pars.size() - 1;
1664 // if an empty inset was created, we create an additional empty
1665 // paragraph at the bottom so that the user can choose where to put
1666 // the graphics (or table).
1668 pars.push_back(Paragraph());
1669 pars.back().setInsetOwner(&cur.text()->inset());
1670 pars.back().setPlainOrDefaultLayout(tclass);
1673 // reposition the cursor to the caption
1674 cur.pit() = cap_pit;
1676 // FIXME: This Text/Cursor dispatch handling is a mess!
1677 // We cannot use Cursor::dispatch here it needs access to up to
1679 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1680 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1681 bv->buffer().updateLabels();
1682 cur.updateFlags(Update::Force);
1683 // FIXME: When leaving the Float (or Wrap) inset we should
1684 // delete any empty paragraph left above or below the
1689 case LFUN_NOMENCL_INSERT: {
1690 InsetCommandParams p(NOMENCL_CODE);
1691 if (cmd.argument().empty())
1692 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1694 p["symbol"] = cmd.argument();
1695 string const data = InsetCommand::params2string("nomenclature", p);
1696 bv->showDialog("nomenclature", data);
1700 case LFUN_INDEX_PRINT: {
1701 InsetCommandParams p(INDEX_PRINT_CODE);
1702 if (cmd.argument().empty())
1703 p["type"] = from_ascii("idx");
1705 p["type"] = cmd.argument();
1706 string const data = InsetCommand::params2string("index_print", p);
1707 FuncRequest fr(LFUN_INSET_INSERT, data);
1712 case LFUN_NOMENCL_PRINT:
1713 case LFUN_TOC_INSERT:
1714 case LFUN_LINE_INSERT:
1715 case LFUN_NEWPAGE_INSERT:
1717 doInsertInset(cur, this, cmd, false, false);
1721 case LFUN_DEPTH_DECREMENT:
1722 changeDepth(cur, DEC_DEPTH);
1725 case LFUN_DEPTH_INCREMENT:
1726 changeDepth(cur, INC_DEPTH);
1729 case LFUN_MATH_DISPLAY:
1730 mathDispatch(cur, cmd, true);
1733 case LFUN_REGEXP_MODE:
1734 regexpDispatch(cur, cmd);
1737 case LFUN_MATH_MODE:
1738 if (cmd.argument() == "on")
1739 // don't pass "on" as argument
1740 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1742 mathDispatch(cur, cmd, false);
1745 case LFUN_MATH_MACRO:
1746 if (cmd.argument().empty())
1747 cur.errorMessage(from_utf8(N_("Missing argument")));
1749 string s = to_utf8(cmd.argument());
1750 string const s1 = token(s, ' ', 1);
1751 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1752 string const s2 = token(s, ' ', 2);
1753 MacroType type = MacroTypeNewcommand;
1755 type = MacroTypeDef;
1756 MathMacroTemplate * inset = new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type);
1757 inset->setBuffer(bv->buffer());
1758 insertInset(cur, inset);
1760 // enter macro inset and select the name
1762 cur.top().pos() = cur.top().lastpos();
1764 cur.setSelection(true);
1765 cur.top().pos() = 0;
1769 // passthrough hat and underscore outside mathed:
1770 case LFUN_MATH_SUBSCRIPT:
1771 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1773 case LFUN_MATH_SUPERSCRIPT:
1774 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1777 case LFUN_MATH_INSERT:
1778 case LFUN_MATH_AMS_MATRIX:
1779 case LFUN_MATH_MATRIX:
1780 case LFUN_MATH_DELIM:
1781 case LFUN_MATH_BIGDELIM: {
1783 cap::replaceSelection(cur);
1784 cur.insert(new InsetMathHull(hullSimple));
1785 checkAndActivateInset(cur, true);
1786 LASSERT(cur.inMathed(), /**/);
1791 case LFUN_FONT_EMPH: {
1792 Font font(ignore_font, ignore_language);
1793 font.fontInfo().setEmph(FONT_TOGGLE);
1794 toggleAndShow(cur, this, font);
1798 case LFUN_FONT_ITAL: {
1799 Font font(ignore_font, ignore_language);
1800 font.fontInfo().setShape(ITALIC_SHAPE);
1801 toggleAndShow(cur, this, font);
1805 case LFUN_FONT_BOLD:
1806 case LFUN_FONT_BOLDSYMBOL: {
1807 Font font(ignore_font, ignore_language);
1808 font.fontInfo().setSeries(BOLD_SERIES);
1809 toggleAndShow(cur, this, font);
1813 case LFUN_FONT_NOUN: {
1814 Font font(ignore_font, ignore_language);
1815 font.fontInfo().setNoun(FONT_TOGGLE);
1816 toggleAndShow(cur, this, font);
1820 case LFUN_FONT_TYPEWRITER: {
1821 Font font(ignore_font, ignore_language);
1822 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1823 toggleAndShow(cur, this, font);
1827 case LFUN_FONT_SANS: {
1828 Font font(ignore_font, ignore_language);
1829 font.fontInfo().setFamily(SANS_FAMILY);
1830 toggleAndShow(cur, this, font);
1834 case LFUN_FONT_ROMAN: {
1835 Font font(ignore_font, ignore_language);
1836 font.fontInfo().setFamily(ROMAN_FAMILY);
1837 toggleAndShow(cur, this, font);
1841 case LFUN_FONT_DEFAULT: {
1842 Font font(inherit_font, ignore_language);
1843 toggleAndShow(cur, this, font);
1847 case LFUN_FONT_STRIKEOUT: {
1848 Font font(ignore_font, ignore_language);
1849 font.fontInfo().setStrikeout(FONT_TOGGLE);
1850 toggleAndShow(cur, this, font);
1854 case LFUN_FONT_UULINE: {
1855 Font font(ignore_font, ignore_language);
1856 font.fontInfo().setUuline(FONT_TOGGLE);
1857 toggleAndShow(cur, this, font);
1861 case LFUN_FONT_UWAVE: {
1862 Font font(ignore_font, ignore_language);
1863 font.fontInfo().setUwave(FONT_TOGGLE);
1864 toggleAndShow(cur, this, font);
1868 case LFUN_FONT_UNDERLINE: {
1869 Font font(ignore_font, ignore_language);
1870 font.fontInfo().setUnderbar(FONT_TOGGLE);
1871 toggleAndShow(cur, this, font);
1875 case LFUN_FONT_SIZE: {
1876 Font font(ignore_font, ignore_language);
1877 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1878 toggleAndShow(cur, this, font);
1882 case LFUN_LANGUAGE: {
1883 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1886 Font font(ignore_font, lang);
1887 toggleAndShow(cur, this, font);
1891 case LFUN_TEXTSTYLE_APPLY:
1892 toggleAndShow(cur, this, freefont, toggleall);
1893 cur.message(_("Character set"));
1896 // Set the freefont using the contents of \param data dispatched from
1897 // the frontends and apply it at the current cursor location.
1898 case LFUN_TEXTSTYLE_UPDATE: {
1901 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1904 toggleAndShow(cur, this, freefont, toggleall);
1905 cur.message(_("Character set"));
1907 lyxerr << "Argument not ok";
1912 case LFUN_FINISHED_LEFT:
1913 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1914 // We're leaving an inset, going left. If the inset is LTR, we're
1915 // leaving from the front, so we should not move (remain at --- but
1916 // not in --- the inset). If the inset is RTL, move left, without
1917 // entering the inset itself; i.e., move to after the inset.
1918 if (cur.paragraph().getFontSettings(
1919 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1920 cursorVisLeft(cur, true);
1923 case LFUN_FINISHED_RIGHT:
1924 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1925 // We're leaving an inset, going right. If the inset is RTL, we're
1926 // leaving from the front, so we should not move (remain at --- but
1927 // not in --- the inset). If the inset is LTR, move right, without
1928 // entering the inset itself; i.e., move to after the inset.
1929 if (!cur.paragraph().getFontSettings(
1930 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1931 cursorVisRight(cur, true);
1934 case LFUN_FINISHED_BACKWARD:
1935 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1938 case LFUN_FINISHED_FORWARD:
1939 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1941 cur.setCurrentFont();
1944 case LFUN_LAYOUT_PARAGRAPH: {
1946 params2string(cur.paragraph(), data);
1947 data = "show\n" + data;
1948 bv->showDialog("paragraph", data);
1952 case LFUN_PARAGRAPH_UPDATE: {
1954 params2string(cur.paragraph(), data);
1956 // Will the paragraph accept changes from the dialog?
1958 cur.inset().allowParagraphCustomization(cur.idx());
1960 data = "update " + convert<string>(accept) + '\n' + data;
1961 bv->updateDialog("paragraph", data);
1965 case LFUN_ACCENT_UMLAUT:
1966 case LFUN_ACCENT_CIRCUMFLEX:
1967 case LFUN_ACCENT_GRAVE:
1968 case LFUN_ACCENT_ACUTE:
1969 case LFUN_ACCENT_TILDE:
1970 case LFUN_ACCENT_CEDILLA:
1971 case LFUN_ACCENT_MACRON:
1972 case LFUN_ACCENT_DOT:
1973 case LFUN_ACCENT_UNDERDOT:
1974 case LFUN_ACCENT_UNDERBAR:
1975 case LFUN_ACCENT_CARON:
1976 case LFUN_ACCENT_BREVE:
1977 case LFUN_ACCENT_TIE:
1978 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1979 case LFUN_ACCENT_CIRCLE:
1980 case LFUN_ACCENT_OGONEK:
1981 theApp()->handleKeyFunc(cmd.action);
1982 if (!cmd.argument().empty())
1983 // FIXME: Are all these characters encoded in one byte in utf8?
1984 bv->translateAndInsert(cmd.argument()[0], this, cur);
1987 case LFUN_FLOAT_LIST_INSERT: {
1988 DocumentClass const & tclass = bv->buffer().params().documentClass();
1989 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1991 if (cur.selection())
1992 cutSelection(cur, true, false);
1993 breakParagraph(cur);
1995 if (cur.lastpos() != 0) {
1996 cursorBackward(cur);
1997 breakParagraph(cur);
2000 docstring const laystr = cur.inset().usePlainLayout() ?
2001 tclass.plainLayoutName() :
2002 tclass.defaultLayoutName();
2003 setLayout(cur, laystr);
2004 ParagraphParameters p;
2005 // FIXME If this call were replaced with one to clearParagraphParams(),
2006 // then we could get rid of this method altogether.
2007 setParagraphs(cur, p);
2008 // FIXME This should be simplified when InsetFloatList takes a
2009 // Buffer in its constructor.
2010 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
2011 ifl->setBuffer(bv->buffer());
2012 insertInset(cur, ifl);
2015 lyxerr << "Non-existent float type: "
2016 << to_utf8(cmd.argument()) << endl;
2021 case LFUN_CHANGE_ACCEPT: {
2022 acceptOrRejectChanges(cur, ACCEPT);
2026 case LFUN_CHANGE_REJECT: {
2027 acceptOrRejectChanges(cur, REJECT);
2031 case LFUN_THESAURUS_ENTRY: {
2032 docstring arg = cmd.argument();
2034 arg = cur.selectionAsString(false);
2036 if (arg.size() > 100 || arg.empty()) {
2037 // Get word or selection
2038 selectWordWhenUnderCursor(cur, WHOLE_WORD);
2039 arg = cur.selectionAsString(false);
2040 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
2043 bv->showDialog("thesaurus", to_utf8(arg));
2047 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2048 // Given data, an encoding of the ParagraphParameters
2049 // generated in the Paragraph dialog, this function sets
2050 // the current paragraph, or currently selected paragraphs,
2052 // NOTE: This function overrides all existing settings.
2053 setParagraphs(cur, cmd.argument());
2054 cur.message(_("Paragraph layout set"));
2058 case LFUN_PARAGRAPH_PARAMS: {
2059 // Given data, an encoding of the ParagraphParameters as we'd
2060 // find them in a LyX file, this function modifies the current paragraph,
2061 // or currently selected paragraphs.
2062 // NOTE: This function only modifies, and does not override, existing
2064 setParagraphs(cur, cmd.argument(), true);
2065 cur.message(_("Paragraph layout set"));
2070 if (cur.selection()) {
2071 cur.setSelection(false);
2074 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2075 // correct, but I'm not 100% sure -- dov, 071019
2076 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2080 case LFUN_OUTLINE_UP:
2081 outline(OutlineUp, cur);
2082 setCursor(cur, cur.pit(), 0);
2083 cur.buffer()->updateLabels();
2087 case LFUN_OUTLINE_DOWN:
2088 outline(OutlineDown, cur);
2089 setCursor(cur, cur.pit(), 0);
2090 cur.buffer()->updateLabels();
2094 case LFUN_OUTLINE_IN:
2095 outline(OutlineIn, cur);
2096 cur.buffer()->updateLabels();
2100 case LFUN_OUTLINE_OUT:
2101 outline(OutlineOut, cur);
2102 cur.buffer()->updateLabels();
2106 case LFUN_OUTLINE_DRAGMOVE: {
2107 int const move_id = convert<int>(cmd.getArg(0));
2108 int const move_to_id = convert<int>(cmd.getArg(1));
2109 dragMove(cur, move_id, move_to_id);
2110 setCursor(cur, cur.pit(), 0);
2111 cur.buffer()->updateLabels();
2117 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2122 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2124 // FIXME: The cursor flag is reset two lines below
2125 // so we need to check here if some of the LFUN did touch that.
2126 // for now only Text::erase() and Text::backspace() do that.
2127 // The plan is to verify all the LFUNs and then to remove this
2128 // singleParUpdate boolean altogether.
2129 if (cur.result().update() & Update::Force) {
2130 singleParUpdate = false;
2134 // FIXME: the following code should go in favor of fine grained
2135 // update flag treatment.
2136 if (singleParUpdate) {
2137 // Inserting characters does not change par height in general. So, try
2138 // to update _only_ this paragraph. BufferView will detect if a full
2139 // metrics update is needed anyway.
2140 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2145 && &oldTopSlice.inset() == &cur.inset()
2146 && oldTopSlice.idx() == cur.idx()
2147 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2148 && !cur.selection())
2149 // FIXME: it would be better if we could just do this
2151 //if (cur.result().update() != Update::FitCursor)
2154 // But some LFUNs do not set Update::FitCursor when needed, so we
2155 // do it for all. This is not very harmfull as FitCursor will provoke
2156 // a full redraw only if needed but still, a proper review of all LFUN
2157 // should be done and this needsUpdate boolean can then be removed.
2158 cur.updateFlags(Update::FitCursor);
2160 cur.updateFlags(Update::Force | Update::FitCursor);
2164 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2165 FuncStatus & flag) const
2167 LASSERT(cur.text() == this, /**/);
2169 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2171 InsetCode code = NO_CODE;
2173 switch (cmd.action) {
2175 case LFUN_DEPTH_DECREMENT:
2176 enable = changeDepthAllowed(cur, DEC_DEPTH);
2179 case LFUN_DEPTH_INCREMENT:
2180 enable = changeDepthAllowed(cur, INC_DEPTH);
2184 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2187 case LFUN_DIALOG_SHOW_NEW_INSET:
2188 if (cmd.argument() == "bibitem")
2189 code = BIBITEM_CODE;
2190 else if (cmd.argument() == "bibtex") {
2192 // not allowed in description items
2193 enable = !inDescriptionItem(cur);
2195 else if (cmd.argument() == "box")
2197 else if (cmd.argument() == "branch")
2199 else if (cmd.argument() == "citation")
2201 else if (cmd.argument() == "ert")
2203 else if (cmd.argument() == "external")
2204 code = EXTERNAL_CODE;
2205 else if (cmd.argument() == "float")
2207 else if (cmd.argument() == "graphics")
2208 code = GRAPHICS_CODE;
2209 else if (cmd.argument() == "href")
2210 code = HYPERLINK_CODE;
2211 else if (cmd.argument() == "include")
2212 code = INCLUDE_CODE;
2213 else if (cmd.argument() == "index")
2215 else if (cmd.argument() == "index_print")
2216 code = INDEX_PRINT_CODE;
2217 else if (cmd.argument() == "nomenclature")
2218 code = NOMENCL_CODE;
2219 else if (cmd.argument() == "nomencl_print")
2220 code = NOMENCL_PRINT_CODE;
2221 else if (cmd.argument() == "label")
2223 else if (cmd.argument() == "note")
2225 else if (cmd.argument() == "phantom")
2226 code = PHANTOM_CODE;
2227 else if (cmd.argument() == "ref")
2229 else if (cmd.argument() == "space")
2231 else if (cmd.argument() == "toc")
2233 else if (cmd.argument() == "vspace")
2235 else if (cmd.argument() == "wrap")
2237 else if (cmd.argument() == "listings")
2238 code = LISTINGS_CODE;
2241 case LFUN_ERT_INSERT:
2244 case LFUN_LISTING_INSERT:
2245 code = LISTINGS_CODE;
2246 // not allowed in description items
2247 enable = !inDescriptionItem(cur);
2249 case LFUN_FOOTNOTE_INSERT:
2252 case LFUN_TABULAR_INSERT:
2253 code = TABULAR_CODE;
2255 case LFUN_MARGINALNOTE_INSERT:
2258 case LFUN_FLOAT_INSERT:
2259 case LFUN_FLOAT_WIDE_INSERT:
2260 // FIXME: If there is a selection, we should check whether there
2261 // are floats in the selection, but this has performance issues, see
2262 // LFUN_CHANGE_ACCEPT/REJECT.
2264 if (inDescriptionItem(cur))
2265 // not allowed in description items
2268 InsetCode const inset_code = cur.inset().lyxCode();
2270 // algorithm floats cannot be put in another float
2271 if (to_utf8(cmd.argument()) == "algorithm") {
2272 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2276 // for figures and tables: only allow in another
2277 // float or wrap if it is of the same type and
2278 // not a subfloat already
2279 if(cur.inset().lyxCode() == code) {
2280 InsetFloat const & ins =
2281 static_cast<InsetFloat const &>(cur.inset());
2282 enable = ins.params().type == to_utf8(cmd.argument())
2283 && !ins.params().subfloat;
2284 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2285 InsetWrap const & ins =
2286 static_cast<InsetWrap const &>(cur.inset());
2287 enable = ins.params().type == to_utf8(cmd.argument());
2291 case LFUN_WRAP_INSERT:
2293 // not allowed in description items
2294 enable = !inDescriptionItem(cur);
2296 case LFUN_FLOAT_LIST_INSERT:
2297 code = FLOAT_LIST_CODE;
2298 // not allowed in description items
2299 enable = !inDescriptionItem(cur);
2301 case LFUN_CAPTION_INSERT:
2302 code = CAPTION_CODE;
2303 // not allowed in description items
2304 enable = !inDescriptionItem(cur);
2306 case LFUN_NOTE_INSERT:
2308 // in commands (sections etc.) and description items,
2309 // only Notes are allowed
2310 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2311 (!cur.paragraph().layout().isCommand()
2312 && !inDescriptionItem(cur)));
2314 case LFUN_FLEX_INSERT: {
2316 string s = cmd.getArg(0);
2318 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2319 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2320 il.lyxtype() != InsetLayout::CUSTOM &&
2321 il.lyxtype() != InsetLayout::ELEMENT &&
2322 il.lyxtype ()!= InsetLayout::STANDARD)
2326 case LFUN_BOX_INSERT:
2329 case LFUN_BRANCH_INSERT:
2331 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2332 && cur.buffer()->params().branchlist().empty())
2335 case LFUN_PHANTOM_INSERT:
2336 code = PHANTOM_CODE;
2338 case LFUN_LABEL_INSERT:
2341 case LFUN_LINE_INSERT:
2344 case LFUN_INFO_INSERT:
2347 case LFUN_OPTIONAL_INSERT:
2349 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2350 < cur.paragraph().layout().optionalargs;
2352 case LFUN_INDEX_INSERT:
2355 case LFUN_INDEX_PRINT:
2356 code = INDEX_PRINT_CODE;
2357 // not allowed in description items
2358 enable = !inDescriptionItem(cur);
2360 case LFUN_NOMENCL_INSERT:
2361 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2365 code = NOMENCL_CODE;
2367 case LFUN_NOMENCL_PRINT:
2368 code = NOMENCL_PRINT_CODE;
2369 // not allowed in description items
2370 enable = !inDescriptionItem(cur);
2372 case LFUN_TOC_INSERT:
2374 // not allowed in description items
2375 enable = !inDescriptionItem(cur);
2377 case LFUN_HYPERLINK_INSERT:
2378 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2382 code = HYPERLINK_CODE;
2384 case LFUN_QUOTE_INSERT:
2385 // always allow this, since we will inset a raw quote
2386 // if an inset is not allowed.
2388 case LFUN_SPECIALCHAR_INSERT:
2389 code = SPECIALCHAR_CODE;
2391 case LFUN_SPACE_INSERT:
2392 // slight hack: we know this is allowed in math mode
2397 case LFUN_MATH_INSERT:
2398 case LFUN_MATH_AMS_MATRIX:
2399 case LFUN_MATH_MATRIX:
2400 case LFUN_MATH_DELIM:
2401 case LFUN_MATH_BIGDELIM:
2402 case LFUN_MATH_DISPLAY:
2403 case LFUN_MATH_MODE:
2404 case LFUN_MATH_MACRO:
2405 case LFUN_MATH_SUBSCRIPT:
2406 case LFUN_MATH_SUPERSCRIPT:
2407 code = MATH_HULL_CODE;
2410 case LFUN_INSET_MODIFY:
2411 // We need to disable this, because we may get called for a
2413 // InsetTabular::getStatus() -> InsetText::getStatus()
2414 // and we don't handle LFUN_INSET_MODIFY.
2418 case LFUN_FONT_EMPH:
2419 flag.setOnOff(fontinfo.emph() == FONT_ON);
2422 case LFUN_FONT_ITAL:
2423 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2426 case LFUN_FONT_NOUN:
2427 flag.setOnOff(fontinfo.noun() == FONT_ON);
2430 case LFUN_FONT_BOLD:
2431 case LFUN_FONT_BOLDSYMBOL:
2432 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2435 case LFUN_FONT_SANS:
2436 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2439 case LFUN_FONT_ROMAN:
2440 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2443 case LFUN_FONT_TYPEWRITER:
2444 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2449 enable = cur.selection();
2453 if (cmd.argument().empty()) {
2454 if (theClipboard().isInternal())
2455 enable = cap::numberOfSelections() > 0;
2457 enable = !theClipboard().empty();
2461 // we have an argument
2462 string const arg = to_utf8(cmd.argument());
2463 if (isStrUnsignedInt(arg)) {
2464 // it's a number and therefore means the internal stack
2465 unsigned int n = convert<unsigned int>(arg);
2466 enable = cap::numberOfSelections() > n;
2470 // explicit graphics type?
2471 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2472 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2473 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2474 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2475 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2476 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2477 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2478 enable = theClipboard().hasGraphicsContents(type);
2487 case LFUN_CLIPBOARD_PASTE:
2488 enable = !theClipboard().empty();
2491 case LFUN_PRIMARY_SELECTION_PASTE:
2492 enable = cur.selection() || !theSelection().empty();
2495 case LFUN_SELECTION_PASTE:
2496 enable = cap::selection();
2499 case LFUN_PARAGRAPH_MOVE_UP:
2500 enable = cur.pit() > 0 && !cur.selection();
2503 case LFUN_PARAGRAPH_MOVE_DOWN:
2504 enable = cur.pit() < cur.lastpit() && !cur.selection();
2507 case LFUN_CHANGE_ACCEPT:
2508 case LFUN_CHANGE_REJECT:
2509 // In principle, these LFUNs should only be enabled if there
2510 // is a change at the current position/in the current selection.
2511 // However, without proper optimizations, this will inevitably
2512 // result in unacceptable performance - just imagine a user who
2513 // wants to select the complete content of a long document.
2514 if (!cur.selection())
2515 enable = cur.paragraph().isChanged(cur.pos());
2517 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2522 case LFUN_OUTLINE_UP:
2523 case LFUN_OUTLINE_DOWN:
2524 case LFUN_OUTLINE_IN:
2525 case LFUN_OUTLINE_OUT:
2526 case LFUN_OUTLINE_DRAGMOVE:
2527 // FIXME: LyX is not ready for outlining within inset.
2528 enable = isMainText()
2529 && cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC;
2532 case LFUN_NEWLINE_INSERT:
2533 // LaTeX restrictions (labels or empty par)
2534 enable = (cur.pos() > cur.paragraph().beginOfBody());
2537 case LFUN_TAB_INSERT:
2538 case LFUN_TAB_DELETE:
2539 enable = cur.inset().getLayout().isPassThru();
2542 case LFUN_SET_GRAPHICS_GROUP: {
2543 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2547 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2551 case LFUN_NEWPAGE_INSERT:
2552 // not allowed in description items
2553 code = NEWPAGE_CODE;
2554 enable = !inDescriptionItem(cur);
2557 case LFUN_DATE_INSERT: {
2558 string const format = cmd.argument().empty()
2559 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2560 enable = support::os::is_valid_strftime(format);
2565 enable = !cur.inset().getLayout().isPassThru();
2567 case LFUN_BREAK_PARAGRAPH:
2568 enable = cur.inset().getLayout().isMultiPar();
2570 case LFUN_WORD_DELETE_FORWARD:
2571 case LFUN_WORD_DELETE_BACKWARD:
2572 case LFUN_LINE_DELETE:
2573 case LFUN_WORD_FORWARD:
2574 case LFUN_WORD_BACKWARD:
2575 case LFUN_WORD_RIGHT:
2576 case LFUN_WORD_LEFT:
2577 case LFUN_CHAR_FORWARD:
2578 case LFUN_CHAR_FORWARD_SELECT:
2579 case LFUN_CHAR_BACKWARD:
2580 case LFUN_CHAR_BACKWARD_SELECT:
2581 case LFUN_CHAR_LEFT:
2582 case LFUN_CHAR_LEFT_SELECT:
2583 case LFUN_CHAR_RIGHT:
2584 case LFUN_CHAR_RIGHT_SELECT:
2586 case LFUN_UP_SELECT:
2588 case LFUN_DOWN_SELECT:
2589 case LFUN_PARAGRAPH_UP_SELECT:
2590 case LFUN_PARAGRAPH_DOWN_SELECT:
2591 case LFUN_LINE_BEGIN_SELECT:
2592 case LFUN_LINE_END_SELECT:
2593 case LFUN_WORD_FORWARD_SELECT:
2594 case LFUN_WORD_BACKWARD_SELECT:
2595 case LFUN_WORD_RIGHT_SELECT:
2596 case LFUN_WORD_LEFT_SELECT:
2597 case LFUN_WORD_SELECT:
2598 case LFUN_SECTION_SELECT:
2599 case LFUN_PARAGRAPH_UP:
2600 case LFUN_PARAGRAPH_DOWN:
2601 case LFUN_LINE_BEGIN:
2603 case LFUN_CHAR_DELETE_FORWARD:
2604 case LFUN_CHAR_DELETE_BACKWARD:
2605 case LFUN_INSET_INSERT:
2606 case LFUN_WORD_UPCASE:
2607 case LFUN_WORD_LOWCASE:
2608 case LFUN_WORD_CAPITALIZE:
2609 case LFUN_CHARS_TRANSPOSE:
2610 case LFUN_SERVER_GET_XY:
2611 case LFUN_SERVER_SET_XY:
2612 case LFUN_SERVER_GET_LAYOUT:
2614 case LFUN_SELF_INSERT:
2615 case LFUN_FONT_DEFAULT:
2616 case LFUN_FONT_UNDERLINE:
2617 case LFUN_FONT_STRIKEOUT:
2618 case LFUN_FONT_UULINE:
2619 case LFUN_FONT_UWAVE:
2620 case LFUN_FONT_SIZE:
2621 case LFUN_TEXTSTYLE_APPLY:
2622 case LFUN_TEXTSTYLE_UPDATE:
2623 case LFUN_LAYOUT_PARAGRAPH:
2624 case LFUN_PARAGRAPH_UPDATE:
2625 case LFUN_ACCENT_UMLAUT:
2626 case LFUN_ACCENT_CIRCUMFLEX:
2627 case LFUN_ACCENT_GRAVE:
2628 case LFUN_ACCENT_ACUTE:
2629 case LFUN_ACCENT_TILDE:
2630 case LFUN_ACCENT_CEDILLA:
2631 case LFUN_ACCENT_MACRON:
2632 case LFUN_ACCENT_DOT:
2633 case LFUN_ACCENT_UNDERDOT:
2634 case LFUN_ACCENT_UNDERBAR:
2635 case LFUN_ACCENT_CARON:
2636 case LFUN_ACCENT_BREVE:
2637 case LFUN_ACCENT_TIE:
2638 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2639 case LFUN_ACCENT_CIRCLE:
2640 case LFUN_ACCENT_OGONEK:
2641 case LFUN_THESAURUS_ENTRY:
2642 case LFUN_PARAGRAPH_PARAMS_APPLY:
2643 case LFUN_PARAGRAPH_PARAMS:
2645 case LFUN_BUFFER_BEGIN:
2646 case LFUN_BUFFER_END:
2647 case LFUN_BUFFER_BEGIN_SELECT:
2648 case LFUN_BUFFER_END_SELECT:
2649 case LFUN_INSET_BEGIN:
2650 case LFUN_INSET_END:
2651 case LFUN_INSET_BEGIN_SELECT:
2652 case LFUN_INSET_END_SELECT:
2653 case LFUN_INSET_SELECT_ALL:
2654 case LFUN_UNICODE_INSERT:
2655 // these are handled in our dispatch()
2665 || !cur.inset().insetAllowed(code)
2666 || cur.paragraph().layout().pass_thru))
2669 switch (cmd.action) {
2670 case LFUN_ACCENT_ACUTE:
2671 case LFUN_ACCENT_BREVE:
2672 case LFUN_ACCENT_CARON:
2673 case LFUN_ACCENT_CEDILLA:
2674 case LFUN_ACCENT_CIRCLE:
2675 case LFUN_ACCENT_CIRCUMFLEX:
2676 case LFUN_ACCENT_DOT:
2677 case LFUN_ACCENT_GRAVE:
2678 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2679 case LFUN_ACCENT_MACRON:
2680 case LFUN_ACCENT_OGONEK:
2681 case LFUN_ACCENT_TIE:
2682 case LFUN_ACCENT_TILDE:
2683 case LFUN_ACCENT_UMLAUT:
2684 case LFUN_ACCENT_UNDERBAR:
2685 case LFUN_ACCENT_UNDERDOT:
2687 case LFUN_DEPTH_DECREMENT:
2688 case LFUN_DEPTH_INCREMENT:
2689 case LFUN_FILE_INSERT:
2690 case LFUN_FONT_BOLD:
2691 case LFUN_FONT_BOLDSYMBOL:
2692 case LFUN_FONT_TYPEWRITER:
2693 case LFUN_FONT_DEFAULT:
2694 case LFUN_FONT_EMPH:
2695 case LFUN_FONT_NOUN:
2696 case LFUN_FONT_ROMAN:
2697 case LFUN_FONT_SANS:
2698 case LFUN_FONT_FRAK:
2699 case LFUN_FONT_ITAL:
2700 case LFUN_FONT_SIZE:
2701 case LFUN_FONT_STATE:
2702 case LFUN_FONT_UNDERLINE:
2703 case LFUN_FONT_STRIKEOUT:
2704 case LFUN_FONT_UULINE:
2705 case LFUN_FONT_UWAVE:
2706 case LFUN_LABEL_GOTO:
2707 case LFUN_LAYOUT_TABULAR:
2709 case LFUN_NOTE_NEXT:
2710 case LFUN_REFERENCE_NEXT:
2711 case LFUN_SERVER_SET_XY:
2712 case LFUN_TEXTSTYLE_APPLY:
2713 case LFUN_TEXTSTYLE_UPDATE:
2714 if (cur.inset().getLayout().isPassThru())
2721 flag.setEnabled(enable);
2726 void Text::pasteString(Cursor & cur, docstring const & clip,
2729 cur.clearSelection();
2730 if (!clip.empty()) {
2733 insertStringAsParagraphs(cur, clip, cur.current_font);
2735 insertStringAsLines(cur, clip, cur.current_font);
2740 // FIXME: an item inset would make things much easier.
2741 bool Text::inDescriptionItem(Cursor & cur) const
2743 Paragraph & par = cur.paragraph();
2744 pos_type const pos = cur.pos();
2745 pos_type const body_pos = par.beginOfBody();
2747 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2748 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2749 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2752 return (pos < body_pos
2754 && (pos == 0 || par.getChar(pos - 1) != ' ')));