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(cur.buffer(), 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(cur.buffer());
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(cur.buffer(), 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(cur.buffer(), hullRegexp));
211 cur.nextInset()->edit(cur, true);
214 cur.message(_("Regexp editor mode"));
218 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
221 cap::replaceSelection(cur);
222 cur.insert(new InsetSpecialChar(kind));
227 static 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);
271 insetText->fixParagraphsFont();
272 if (!insetText->allowMultiPar() || cur.lastpit() == 0) {
273 // reset first par to default
274 cur.text()->paragraphs().begin()
275 ->setPlainOrDefaultLayout(bparams.documentClass());
278 // Merge multiple paragraphs -- hack
279 while (cur.lastpit() > 0)
280 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
281 cur.leaveInset(*inset);
284 cur.leaveInset(*inset);
285 // reset surrounding par to default
286 DocumentClass const & dc = bparams.documentClass();
287 docstring const layoutname = inset->usePlainLayout()
288 ? dc.plainLayoutName()
289 : dc.defaultLayoutName();
290 text->setLayout(cur, layoutname);
296 string const freefont2string()
298 return freefont.toString(toggleall);
302 /// the type of outline operation
304 OutlineUp, // Move this header with text down
305 OutlineDown, // Move this header with text up
306 OutlineIn, // Make this header deeper
307 OutlineOut // Make this header shallower
311 static void outline(OutlineOp mode, Cursor & cur)
313 Buffer & buf = *cur.buffer();
314 pit_type & pit = cur.pit();
315 ParagraphList & pars = buf.text().paragraphs();
316 ParagraphList::iterator const bgn = pars.begin();
317 // The first paragraph of the area to be copied:
318 ParagraphList::iterator start = boost::next(bgn, pit);
319 // The final paragraph of area to be copied:
320 ParagraphList::iterator finish = start;
321 ParagraphList::iterator const end = pars.end();
323 DocumentClass const & tc = buf.params().documentClass();
325 int const thistoclevel = start->layout().toclevel;
328 // Move out (down) from this section header
332 // Seek the one (on same level) below
333 for (; finish != end; ++finish) {
334 toclevel = finish->layout().toclevel;
335 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
341 if (start == pars.begin())
344 ParagraphList::iterator dest = start;
345 // Move out (up) from this header
348 // Search previous same-level header above
351 toclevel = dest->layout().toclevel;
353 && (toclevel == Layout::NOT_IN_TOC
354 || toclevel > thistoclevel));
355 // Not found; do nothing
356 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
358 pit_type const newpit = distance(bgn, dest);
359 pit_type const len = distance(start, finish);
360 pit_type const deletepit = pit + len;
361 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
362 pars.splice(dest, start, finish);
370 // Go one down from *this* header:
371 ParagraphList::iterator dest = boost::next(finish, 1);
372 // Go further down to find header to insert in front of:
373 for (; dest != end; ++dest) {
374 toclevel = dest->layout().toclevel;
375 if (toclevel != Layout::NOT_IN_TOC
376 && toclevel <= thistoclevel)
379 // One such was found:
380 pit_type newpit = distance(bgn, dest);
381 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
382 pit_type const len = distance(start, finish);
383 pars.splice(dest, start, finish);
384 cur.pit() = newpit - len;
388 pit_type const len = distance(start, finish);
389 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
390 for (; start != finish; ++start) {
391 toclevel = start->layout().toclevel;
392 if (toclevel == Layout::NOT_IN_TOC)
394 DocumentClass::const_iterator lit = tc.begin();
395 DocumentClass::const_iterator len = tc.end();
396 for (; lit != len; ++lit) {
397 if (lit->toclevel == toclevel + 1 &&
398 start->layout().labeltype == lit->labeltype) {
399 start->setLayout(*lit);
407 pit_type const len = distance(start, finish);
408 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
409 for (; start != finish; ++start) {
410 toclevel = start->layout().toclevel;
411 if (toclevel == Layout::NOT_IN_TOC)
413 DocumentClass::const_iterator lit = tc.begin();
414 DocumentClass::const_iterator len = tc.end();
415 for (; lit != len; ++lit) {
416 if (lit->toclevel == toclevel - 1 &&
417 start->layout().labeltype == lit->labeltype) {
418 start->setLayout(*lit);
429 void Text::number(Cursor & cur)
431 FontInfo font = ignore_font;
432 font.setNumber(FONT_TOGGLE);
433 toggleAndShow(cur, this, Font(font, ignore_language));
437 bool Text::isRTL(Paragraph const & par) const
439 Buffer const & buffer = owner_->buffer();
440 return par.isRTL(buffer.params());
444 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
446 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
448 // Dispatch if the cursor is inside the text. It is not the
449 // case for context menus (bug 5797).
450 if (cur.text() != this) {
455 BufferView * bv = &cur.bv();
456 TextMetrics * tm = &bv->textMetrics(this);
457 if (!tm->contains(cur.pit())) {
458 lyx::dispatch(FuncRequest(LFUN_SCREEN_SHOW_CURSOR));
459 tm = &bv->textMetrics(this);
462 // FIXME: We use the update flag to indicates wether a singlePar or a
463 // full screen update is needed. We reset it here but shall we restore it
467 LASSERT(cur.text() == this, /**/);
468 CursorSlice oldTopSlice = cur.top();
469 bool oldBoundary = cur.boundary();
470 bool sel = cur.selection();
471 // Signals that, even if needsUpdate == false, an update of the
472 // cursor paragraph is required
473 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
474 LyXAction::SingleParUpdate);
475 // Signals that a full-screen update is required
476 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
477 LyXAction::NoUpdate) || singleParUpdate);
479 switch (cmd.action) {
481 case LFUN_PARAGRAPH_MOVE_DOWN: {
482 pit_type const pit = cur.pit();
483 recUndo(cur, pit, pit + 1);
485 pars_.swap(pit, pit + 1);
486 cur.buffer()->updateLabels();
492 case LFUN_PARAGRAPH_MOVE_UP: {
493 pit_type const pit = cur.pit();
494 recUndo(cur, pit - 1, pit);
496 pars_.swap(pit, pit - 1);
497 cur.buffer()->updateLabels();
503 case LFUN_APPENDIX: {
504 Paragraph & par = cur.paragraph();
505 bool start = !par.params().startOfAppendix();
507 // FIXME: The code below only makes sense at top level.
508 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
509 // ensure that we have only one start_of_appendix in this document
510 // FIXME: this don't work for multipart document!
511 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
512 if (pars_[tmp].params().startOfAppendix()) {
514 pars_[tmp].params().startOfAppendix(false);
520 par.params().startOfAppendix(start);
522 // we can set the refreshing parameters now
523 cur.buffer()->updateLabels();
527 case LFUN_WORD_DELETE_FORWARD:
529 cutSelection(cur, true, false);
531 deleteWordForward(cur);
532 finishChange(cur, false);
535 case LFUN_WORD_DELETE_BACKWARD:
537 cutSelection(cur, true, false);
539 deleteWordBackward(cur);
540 finishChange(cur, false);
543 case LFUN_LINE_DELETE:
545 cutSelection(cur, true, false);
547 tm->deleteLineForward(cur);
548 finishChange(cur, false);
551 case LFUN_BUFFER_BEGIN:
552 case LFUN_BUFFER_BEGIN_SELECT:
553 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
554 if (cur.depth() == 1)
555 needsUpdate |= cursorTop(cur);
558 cur.updateFlags(Update::FitCursor);
561 case LFUN_BUFFER_END:
562 case LFUN_BUFFER_END_SELECT:
563 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
564 if (cur.depth() == 1)
565 needsUpdate |= cursorBottom(cur);
568 cur.updateFlags(Update::FitCursor);
571 case LFUN_INSET_BEGIN:
572 case LFUN_INSET_BEGIN_SELECT:
573 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_BEGIN_SELECT);
574 if (cur.depth() == 1 || !cur.top().at_begin())
575 needsUpdate |= cursorTop(cur);
578 cur.updateFlags(Update::FitCursor);
582 case LFUN_INSET_END_SELECT:
583 needsUpdate |= cur.selHandle(cmd.action == LFUN_INSET_END_SELECT);
584 if (cur.depth() == 1 || !cur.top().at_end())
585 needsUpdate |= cursorBottom(cur);
588 cur.updateFlags(Update::FitCursor);
591 case LFUN_INSET_SELECT_ALL:
592 if (cur.depth() == 1 || !cur.selection() || !cur.selBegin().at_begin()
593 || !cur.selEnd().at_end()) {
594 needsUpdate |= cur.selHandle(false);
595 needsUpdate |= cursorTop(cur);
596 needsUpdate |= cur.selHandle(true);
597 needsUpdate |= cursorBottom(cur);
600 cur.updateFlags(Update::FitCursor);
603 case LFUN_CHAR_FORWARD:
604 case LFUN_CHAR_FORWARD_SELECT:
605 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
606 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
607 needsUpdate |= cursorForward(cur);
609 if (!needsUpdate && oldTopSlice == cur.top()
610 && cur.boundary() == oldBoundary) {
612 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
614 // we will probably be moving out the inset, so we should execute
615 // the depm-mechanism, but only when the cursor has a place to
616 // go outside this inset, i.e. in a slice above.
617 if (cur.depth() > 1 && cur.pos() == cur.lastpos()
618 && cur.pit() == cur.lastpit()) {
619 // The cursor hasn't changed yet. To give the
620 // DEPM the possibility of doing something we must
621 // provide it with two different cursors.
623 dummy.pos() = dummy.pit() = 0;
624 cur.bv().checkDepm(dummy, cur);
629 case LFUN_CHAR_BACKWARD:
630 case LFUN_CHAR_BACKWARD_SELECT:
631 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
632 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
633 needsUpdate |= cursorBackward(cur);
635 if (!needsUpdate && oldTopSlice == cur.top()
636 && cur.boundary() == oldBoundary) {
638 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
640 // we will probably be moving out the inset, so we should execute
641 // the depm-mechanism, but only when the cursor has a place to
642 // go outside this inset, i.e. in a slice above.
643 if (cur.depth() > 1 && cur.pos() == 0 && cur.pit() == 0) {
644 // The cursor hasn't changed yet. To give the
645 // DEPM the possibility of doing something we must
646 // provide it with two different cursors.
648 dummy.pos() = cur.lastpos();
649 dummy.pit() = cur.lastpit();
650 cur.bv().checkDepm(dummy, cur);
656 case LFUN_CHAR_LEFT_SELECT:
657 if (lyxrc.visual_cursor) {
658 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
659 needsUpdate |= cursorVisLeft(cur);
660 if (!needsUpdate && oldTopSlice == cur.top()
661 && cur.boundary() == oldBoundary) {
663 cmd = FuncRequest(LFUN_FINISHED_LEFT);
666 if (reverseDirectionNeeded(cur)) {
667 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
668 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
670 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
671 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
678 case LFUN_CHAR_RIGHT:
679 case LFUN_CHAR_RIGHT_SELECT:
680 if (lyxrc.visual_cursor) {
681 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
682 needsUpdate |= cursorVisRight(cur);
683 if (!needsUpdate && oldTopSlice == cur.top()
684 && cur.boundary() == oldBoundary) {
686 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
689 if (reverseDirectionNeeded(cur)) {
690 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
691 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
693 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
694 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
703 case LFUN_DOWN_SELECT:
706 // stop/start the selection
707 bool select = cmd.action == LFUN_DOWN_SELECT ||
708 cmd.action == LFUN_UP_SELECT;
710 // move cursor up/down
711 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
712 bool const atFirstOrLastRow = cur.atFirstOrLastRow(up);
714 if (!atFirstOrLastRow) {
715 needsUpdate |= cur.selHandle(select);
716 cur.selHandle(select);
717 cur.upDownInText(up, needsUpdate);
718 needsUpdate |= cur.beforeDispatchCursor().inMathed();
720 // if the cursor cannot be moved up or down do not remove
721 // the selection right now, but wait for the next dispatch.
723 needsUpdate |= cur.selHandle(select);
724 cur.upDownInText(up, needsUpdate);
731 case LFUN_PARAGRAPH_UP:
732 case LFUN_PARAGRAPH_UP_SELECT:
733 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
734 needsUpdate |= cursorUpParagraph(cur);
737 case LFUN_PARAGRAPH_DOWN:
738 case LFUN_PARAGRAPH_DOWN_SELECT:
739 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
740 needsUpdate |= cursorDownParagraph(cur);
743 case LFUN_LINE_BEGIN:
744 case LFUN_LINE_BEGIN_SELECT:
745 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
746 needsUpdate |= tm->cursorHome(cur);
750 case LFUN_LINE_END_SELECT:
751 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
752 needsUpdate |= tm->cursorEnd(cur);
755 case LFUN_SECTION_SELECT: {
756 Buffer const & buf = *cur.buffer();
757 pit_type const pit = cur.pit();
758 ParagraphList & pars = buf.text().paragraphs();
759 ParagraphList::iterator bgn = pars.begin();
760 // The first paragraph of the area to be selected:
761 ParagraphList::iterator start = boost::next(bgn, pit);
762 // The final paragraph of area to be selected:
763 ParagraphList::iterator finish = start;
764 ParagraphList::iterator end = pars.end();
766 int const thistoclevel = start->layout().toclevel;
767 if (thistoclevel == Layout::NOT_IN_TOC)
771 Cursor const old_cur = cur;
772 needsUpdate |= cur.selHandle(true);
774 // Move out (down) from this section header
778 // Seek the one (on same level) below
779 for (; finish != end; ++finish, ++cur.pit()) {
780 int const toclevel = finish->layout().toclevel;
781 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
784 cur.pos() = cur.lastpos();
786 needsUpdate |= cur != old_cur;
790 case LFUN_WORD_RIGHT:
791 case LFUN_WORD_RIGHT_SELECT:
792 if (lyxrc.visual_cursor) {
793 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
794 needsUpdate |= cursorVisRightOneWord(cur);
795 if (!needsUpdate && oldTopSlice == cur.top()
796 && cur.boundary() == oldBoundary) {
798 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
801 if (reverseDirectionNeeded(cur)) {
802 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
803 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
805 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
806 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
813 case LFUN_WORD_FORWARD:
814 case LFUN_WORD_FORWARD_SELECT:
815 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
816 needsUpdate |= cursorForwardOneWord(cur);
820 case LFUN_WORD_LEFT_SELECT:
821 if (lyxrc.visual_cursor) {
822 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
823 needsUpdate |= cursorVisLeftOneWord(cur);
824 if (!needsUpdate && oldTopSlice == cur.top()
825 && cur.boundary() == oldBoundary) {
827 cmd = FuncRequest(LFUN_FINISHED_LEFT);
830 if (reverseDirectionNeeded(cur)) {
831 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
832 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
834 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
835 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
842 case LFUN_WORD_BACKWARD:
843 case LFUN_WORD_BACKWARD_SELECT:
844 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
845 needsUpdate |= cursorBackwardOneWord(cur);
848 case LFUN_WORD_SELECT: {
849 selectWord(cur, WHOLE_WORD);
850 finishChange(cur, true);
854 case LFUN_NEWLINE_INSERT: {
855 InsetNewlineParams inp;
856 docstring arg = cmd.argument();
857 // this avoids a double undo
858 // FIXME: should not be needed, ideally
859 if (!cur.selection())
861 cap::replaceSelection(cur);
862 if (arg == "linebreak")
863 inp.kind = InsetNewlineParams::LINEBREAK;
865 inp.kind = InsetNewlineParams::NEWLINE;
866 cur.insert(new InsetNewline(inp));
868 moveCursor(cur, false);
872 case LFUN_TAB_INSERT: {
873 bool const multi_par_selection = cur.selection() &&
874 cur.selBegin().pit() != cur.selEnd().pit();
875 if (multi_par_selection) {
876 // If there is a multi-paragraph selection, a tab is inserted
877 // at the beginning of each paragraph.
878 cur.recordUndoSelection();
879 pit_type const pit_end = cur.selEnd().pit();
880 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
881 pars_[pit].insertChar(0, '\t',
882 bv->buffer().params().trackChanges);
883 // Update the selection pos to make sure the selection does not
884 // change as the inserted tab will increase the logical pos.
885 if (cur.anchor_.pit() == pit)
886 cur.anchor_.forwardPos();
887 if (cur.pit() == pit)
892 // Maybe we shouldn't allow tabs within a line, because they
893 // are not (yet) aligned as one might do expect.
894 FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
900 case LFUN_TAB_DELETE: {
901 bool const tc = bv->buffer().params().trackChanges;
902 if (cur.selection()) {
903 // If there is a selection, a tab (if present) is removed from
904 // the beginning of each paragraph.
905 cur.recordUndoSelection();
906 pit_type const pit_end = cur.selEnd().pit();
907 for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
908 Paragraph & par = paragraphs()[pit];
909 if (par.getChar(0) == '\t') {
910 if (cur.pit() == pit)
912 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
913 cur.anchor_.backwardPos();
915 par.eraseChar(0, tc);
917 // If no tab was present, try to remove up to four spaces.
918 for (int n_spaces = 0;
919 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
920 if (cur.pit() == pit)
922 if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
923 cur.anchor_.backwardPos();
925 par.eraseChar(0, tc);
930 // If there is no selection, try to remove a tab or some spaces
931 // before the position of the cursor.
932 Paragraph & par = paragraphs()[cur.pit()];
933 pos_type const pos = cur.pos();
938 char_type const c = par.getChar(pos - 1);
942 par.eraseChar(cur.pos(), tc);
944 for (int n_spaces = 0;
946 && par.getChar(cur.pos() - 1) == ' '
950 par.eraseChar(cur.pos(), tc);
957 case LFUN_CHAR_DELETE_FORWARD:
958 if (!cur.selection()) {
959 if (cur.pos() == cur.paragraph().size())
960 // Par boundary, force full-screen update
961 singleParUpdate = false;
962 needsUpdate |= erase(cur);
964 // It is possible to make it a lot faster still
965 // just comment out the line below...
967 cutSelection(cur, true, false);
968 singleParUpdate = false;
970 moveCursor(cur, false);
973 case LFUN_CHAR_DELETE_BACKWARD:
974 if (!cur.selection()) {
975 if (bv->getIntl().getTransManager().backspace()) {
976 // Par boundary, full-screen update
978 singleParUpdate = false;
979 needsUpdate |= backspace(cur);
981 // It is possible to make it a lot faster still
982 // just comment out the line below...
985 cutSelection(cur, true, false);
986 singleParUpdate = false;
990 case LFUN_BREAK_PARAGRAPH:
991 cap::replaceSelection(cur);
992 breakParagraph(cur, cmd.argument() == "inverse");
996 case LFUN_INSET_INSERT: {
999 // We have to avoid triggering InstantPreview loading
1000 // before inserting into the document. See bug #5626.
1001 bool loaded = bv->buffer().isFullyLoaded();
1002 bv->buffer().setFullyLoaded(false);
1003 Inset * inset = createInset(&bv->buffer(), cmd);
1004 bv->buffer().setFullyLoaded(loaded);
1007 // FIXME (Abdel 01/02/2006):
1008 // What follows would be a partial fix for bug 2154:
1009 // http://www.lyx.org/trac/ticket/2154
1010 // This automatically put the label inset _after_ a
1011 // numbered section. It should be possible to extend the mechanism
1012 // to any kind of LateX environement.
1013 // The correct way to fix that bug would be at LateX generation.
1014 // I'll let the code here for reference as it could be used for some
1015 // other feature like "automatic labelling".
1017 Paragraph & par = pars_[cur.pit()];
1018 if (inset->lyxCode() == LABEL_CODE
1019 && par.layout().labeltype == LABEL_COUNTER) {
1020 // Go to the end of the paragraph
1021 // Warning: Because of Change-Tracking, the last
1022 // position is 'size()' and not 'size()-1':
1023 cur.pos() = par.size();
1024 // Insert a new paragraph
1025 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
1029 if (cur.selection())
1030 cutSelection(cur, true, false);
1034 // trigger InstantPreview now
1035 if (inset->lyxCode() == EXTERNAL_CODE) {
1036 InsetExternal & ins =
1037 static_cast<InsetExternal &>(*inset);
1038 ins.updatePreview();
1045 case LFUN_INSET_DISSOLVE: {
1046 if (dissolveInset(cur))
1051 case LFUN_SET_GRAPHICS_GROUP: {
1052 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
1058 string id = to_utf8(cmd.argument());
1059 string grp = graphics::getGroupParams(bv->buffer(), id);
1060 InsetGraphicsParams tmp, inspar = ins->getParams();
1063 inspar.groupId = to_utf8(cmd.argument());
1065 InsetGraphics::string2params(grp, bv->buffer(), tmp);
1066 tmp.filename = inspar.filename;
1070 ins->setParams(inspar);
1073 case LFUN_SPACE_INSERT:
1074 if (cur.paragraph().layout().free_spacing)
1075 insertChar(cur, ' ');
1077 doInsertInset(cur, this, cmd, false, false);
1080 moveCursor(cur, false);
1083 case LFUN_SPECIALCHAR_INSERT: {
1084 string const name = to_utf8(cmd.argument());
1085 if (name == "hyphenation")
1086 specialChar(cur, InsetSpecialChar::HYPHENATION);
1087 else if (name == "ligature-break")
1088 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
1089 else if (name == "slash")
1090 specialChar(cur, InsetSpecialChar::SLASH);
1091 else if (name == "nobreakdash")
1092 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
1093 else if (name == "dots")
1094 specialChar(cur, InsetSpecialChar::LDOTS);
1095 else if (name == "end-of-sentence")
1096 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
1097 else if (name == "menu-separator")
1098 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
1099 else if (name.empty())
1100 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
1102 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
1106 case LFUN_WORD_UPCASE:
1107 changeCase(cur, text_uppercase);
1110 case LFUN_WORD_LOWCASE:
1111 changeCase(cur, text_lowercase);
1114 case LFUN_WORD_CAPITALIZE:
1115 changeCase(cur, text_capitalization);
1118 case LFUN_CHARS_TRANSPOSE:
1119 charsTranspose(cur);
1123 cur.message(_("Paste"));
1124 LASSERT(cur.selBegin().idx() == cur.selEnd().idx(), /**/);
1125 cap::replaceSelection(cur);
1127 // without argument?
1128 string const arg = to_utf8(cmd.argument());
1130 if (theClipboard().isInternal())
1131 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
1132 else if (theClipboard().hasGraphicsContents()
1133 && !theClipboard().hasTextContents())
1134 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
1136 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
1137 } else if (isStrUnsignedInt(arg)) {
1138 // we have a numerical argument
1139 pasteFromStack(cur, bv->buffer().errorList("Paste"),
1140 convert<unsigned int>(arg));
1142 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
1144 type = Clipboard::PdfGraphicsType;
1145 else if (arg == "png")
1146 type = Clipboard::PngGraphicsType;
1147 else if (arg == "jpeg")
1148 type = Clipboard::JpegGraphicsType;
1149 else if (arg == "linkback")
1150 type = Clipboard::LinkBackGraphicsType;
1151 else if (arg == "emf")
1152 type = Clipboard::EmfGraphicsType;
1153 else if (arg == "wmf")
1154 type = Clipboard::WmfGraphicsType;
1157 LASSERT(false, /**/);
1159 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
1162 bv->buffer().errors("Paste");
1163 cur.clearSelection(); // bug 393
1169 cutSelection(cur, true, true);
1170 cur.message(_("Cut"));
1175 cur.message(_("Copy"));
1178 case LFUN_SERVER_GET_XY:
1179 cur.message(from_utf8(
1180 convert<string>(tm->cursorX(cur.top(), cur.boundary()))
1181 + ' ' + convert<string>(tm->cursorY(cur.top(), cur.boundary()))));
1184 case LFUN_SERVER_SET_XY: {
1187 istringstream is(to_utf8(cmd.argument()));
1190 lyxerr << "SETXY: Could not parse coordinates in '"
1191 << to_utf8(cmd.argument()) << endl;
1193 tm->setCursorFromCoordinates(cur, x, y);
1197 case LFUN_SERVER_GET_LAYOUT:
1198 cur.message(cur.paragraph().layout().name());
1202 docstring layout = cmd.argument();
1203 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1205 Paragraph const & para = cur.paragraph();
1206 docstring const old_layout = para.layout().name();
1207 DocumentClass const & tclass = bv->buffer().params().documentClass();
1210 layout = tclass.defaultLayoutName();
1212 if (owner_->forcePlainLayout())
1213 // in this case only the empty layout is allowed
1214 layout = tclass.plainLayoutName();
1215 else if (para.usePlainLayout()) {
1216 // in this case, default layout maps to empty layout
1217 if (layout == tclass.defaultLayoutName())
1218 layout = tclass.plainLayoutName();
1220 // otherwise, the empty layout maps to the default
1221 if (layout == tclass.plainLayoutName())
1222 layout = tclass.defaultLayoutName();
1225 bool hasLayout = tclass.hasLayout(layout);
1227 // If the entry is obsolete, use the new one instead.
1229 docstring const & obs = tclass[layout].obsoleted_by();
1235 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1236 from_utf8(N_(" not known")));
1240 bool change_layout = (old_layout != layout);
1242 if (!change_layout && cur.selection() &&
1243 cur.selBegin().pit() != cur.selEnd().pit())
1245 pit_type spit = cur.selBegin().pit();
1246 pit_type epit = cur.selEnd().pit() + 1;
1247 while (spit != epit) {
1248 if (pars_[spit].layout().name() != old_layout) {
1249 change_layout = true;
1257 setLayout(cur, layout);
1262 case LFUN_CLIPBOARD_PASTE:
1263 cur.clearSelection();
1264 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1265 cmd.argument() == "paragraph");
1266 bv->buffer().errors("Paste");
1269 case LFUN_PRIMARY_SELECTION_PASTE:
1270 pasteString(cur, theSelection().get(),
1271 cmd.argument() == "paragraph");
1274 case LFUN_SELECTION_PASTE:
1275 // Copy the selection buffer to the clipboard stack,
1276 // because we want it to appear in the "Edit->Paste
1278 cap::copySelectionToStack();
1279 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1280 bv->buffer().errors("Paste");
1283 case LFUN_UNICODE_INSERT: {
1284 if (cmd.argument().empty())
1286 docstring hexstring = cmd.argument();
1287 if (isHex(hexstring)) {
1288 char_type c = hexToInt(hexstring);
1289 if (c >= 32 && c < 0x10ffff) {
1290 lyxerr << "Inserting c: " << c << endl;
1291 docstring s = docstring(1, c);
1292 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1298 case LFUN_QUOTE_INSERT: {
1299 Paragraph & par = cur.paragraph();
1300 pos_type pos = cur.pos();
1301 BufferParams const & bufparams = bv->buffer().params();
1302 Layout const & style = par.layout();
1303 InsetLayout const & ilayout = cur.inset().getLayout();
1304 if (!style.pass_thru && !ilayout.isPassThru()
1305 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1306 // this avoids a double undo
1307 // FIXME: should not be needed, ideally
1308 if (!cur.selection())
1310 cap::replaceSelection(cur);
1315 else if (cur.prevInset() && cur.prevInset()->isSpace())
1318 c = par.getChar(pos - 1);
1319 string arg = to_utf8(cmd.argument());
1320 cur.insert(new InsetQuotes(cur.buffer(), c, (arg == "single")
1321 ? InsetQuotes::SingleQuotes : InsetQuotes::DoubleQuotes));
1325 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1329 case LFUN_DATE_INSERT: {
1330 string const format = cmd.argument().empty()
1331 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1332 string const time = formatted_time(current_time(), format);
1333 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1337 case LFUN_MOUSE_TRIPLE:
1338 if (cmd.button() == mouse_button::button1) {
1339 tm->cursorHome(cur);
1347 case LFUN_MOUSE_DOUBLE:
1348 if (cmd.button() == mouse_button::button1) {
1349 selectWord(cur, WHOLE_WORD_STRICT);
1354 // Single-click on work area
1355 case LFUN_MOUSE_PRESS: {
1356 // We are not marking a selection with the keyboard in any case.
1357 Cursor & bvcur = cur.bv().cursor();
1358 bvcur.setMark(false);
1359 switch (cmd.button()) {
1360 case mouse_button::button1:
1362 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1363 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1364 if (bvcur.wordSelection())
1365 selectWord(bvcur, WHOLE_WORD);
1368 case mouse_button::button2:
1369 // Middle mouse pasting.
1370 bv->mouseSetCursor(cur);
1372 FuncRequest(LFUN_COMMAND_ALTERNATIVES,
1373 "selection-paste ; primary-selection-paste paragraph"));
1377 case mouse_button::button3: {
1378 // Don't do anything if we right-click a
1379 // selection, a context menu will popup.
1380 if (bvcur.selection() && cur >= bvcur.selectionBegin()
1381 && cur < bvcur.selectionEnd()) {
1385 if (!bv->mouseSetCursor(cur, false))
1386 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1392 } // switch (cmd.button())
1395 case LFUN_MOUSE_MOTION: {
1396 // Mouse motion with right or middle mouse do nothing for now.
1397 if (cmd.button() != mouse_button::button1) {
1401 // ignore motions deeper nested than the real anchor
1402 Cursor & bvcur = cur.bv().cursor();
1403 if (!bvcur.anchor_.hasPart(cur)) {
1407 CursorSlice old = bvcur.top();
1409 int const wh = bv->workHeight();
1410 int const y = max(0, min(wh - 1, cmd.y));
1412 tm->setCursorFromCoordinates(cur, cmd.x, y);
1413 cur.setTargetX(cmd.x);
1415 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1417 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1418 // This is to allow jumping over large insets
1419 if (cur.top() == old) {
1421 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1423 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1425 // We continue with our existing selection or start a new one, so don't
1426 // reset the anchor.
1427 bvcur.setCursor(cur);
1428 bvcur.setSelection(true);
1429 if (cur.top() == old) {
1430 // We didn't move one iota, so no need to update the screen.
1431 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1438 case LFUN_MOUSE_RELEASE:
1439 switch (cmd.button()) {
1440 case mouse_button::button1:
1441 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1442 // If there is a new selection, update persistent selection;
1443 // otherwise, single click does not clear persistent selection
1445 if (cur.selection()) {
1446 // Finish selection. If double click,
1447 // cur is moved to the end of word by
1448 // selectWord but bvcur is current
1450 cur.bv().cursor().setSelection();
1451 // We might have removed an empty but drawn selection
1452 // (probably a margin)
1453 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1456 // FIXME: We could try to handle drag and drop of selection here.
1459 case mouse_button::button2:
1460 // Middle mouse pasting is handled at mouse press time,
1461 // see LFUN_MOUSE_PRESS.
1465 case mouse_button::button3:
1466 // Cursor was set at LFUN_MOUSE_PRESS time.
1467 // FIXME: If there is a selection we could try to handle a special
1468 // drag & drop context menu.
1472 case mouse_button::none:
1473 case mouse_button::button4:
1474 case mouse_button::button5:
1476 } // switch (cmd.button())
1480 case LFUN_SELF_INSERT: {
1481 if (cmd.argument().empty())
1484 // Automatically delete the currently selected
1485 // text and replace it with what is being
1486 // typed in now. Depends on lyxrc settings
1487 // "auto_region_delete", which defaults to
1490 if (lyxrc.auto_region_delete && cur.selection())
1491 cutSelection(cur, false, false);
1493 cur.clearSelection();
1495 docstring::const_iterator cit = cmd.argument().begin();
1496 docstring::const_iterator const end = cmd.argument().end();
1497 for (; cit != end; ++cit)
1498 bv->translateAndInsert(*cit, this, cur);
1501 moveCursor(cur, false);
1502 bv->bookmarkEditPosition();
1506 case LFUN_HYPERLINK_INSERT: {
1507 InsetCommandParams p(HYPERLINK_CODE);
1509 if (cur.selection()) {
1510 content = cur.selectionAsString(false);
1511 cutSelection(cur, true, false);
1513 p["target"] = (cmd.argument().empty()) ?
1514 content : cmd.argument();
1515 string const data = InsetCommand::params2string("href", p);
1516 if (p["target"].empty()) {
1517 bv->showDialog("href", data);
1519 FuncRequest fr(LFUN_INSET_INSERT, data);
1525 case LFUN_LABEL_INSERT: {
1526 InsetCommandParams p(LABEL_CODE);
1527 // Try to generate a valid label
1528 p["name"] = (cmd.argument().empty()) ?
1529 cur.getPossibleLabel() :
1531 string const data = InsetCommand::params2string("label", p);
1533 if (cmd.argument().empty()) {
1534 bv->showDialog("label", data);
1536 FuncRequest fr(LFUN_INSET_INSERT, data);
1542 case LFUN_INFO_INSERT: {
1544 if (cmd.argument().empty() && cur.selection()) {
1545 // if command argument is empty use current selection as parameter.
1546 docstring ds = cur.selectionAsString(false);
1547 cutSelection(cur, true, false);
1548 FuncRequest cmd0(cmd, ds);
1549 inset = createInset(cur.buffer(), cmd0);
1551 inset = createInset(cur.buffer(), cmd);
1556 insertInset(cur, inset);
1560 case LFUN_CAPTION_INSERT:
1561 case LFUN_FOOTNOTE_INSERT:
1562 case LFUN_NOTE_INSERT:
1563 case LFUN_FLEX_INSERT:
1564 case LFUN_BOX_INSERT:
1565 case LFUN_BRANCH_INSERT:
1566 case LFUN_PHANTOM_INSERT:
1567 case LFUN_ERT_INSERT:
1568 case LFUN_LISTING_INSERT:
1569 case LFUN_MARGINALNOTE_INSERT:
1570 case LFUN_OPTIONAL_INSERT:
1571 case LFUN_INDEX_INSERT:
1572 // Open the inset, and move the current selection
1574 doInsertInset(cur, this, cmd, true, true);
1576 // Some insets are numbered, others are shown in the outline pane so
1577 // let's update the labels and the toc backend.
1578 bv->buffer().updateLabels();
1581 case LFUN_TABULAR_INSERT:
1582 // if there were no arguments, just open the dialog
1583 if (doInsertInset(cur, this, cmd, false, true))
1586 bv->showDialog("tabularcreate");
1590 case LFUN_FLOAT_INSERT:
1591 case LFUN_FLOAT_WIDE_INSERT:
1592 case LFUN_WRAP_INSERT: {
1593 // will some text be moved into the inset?
1594 bool content = cur.selection();
1596 doInsertInset(cur, this, cmd, true, true);
1599 // If some text is moved into the inset, doInsertInset
1600 // puts the cursor outside the inset. To insert the
1601 // caption we put it back into the inset.
1605 ParagraphList & pars = cur.text()->paragraphs();
1607 DocumentClass const & tclass = bv->buffer().params().documentClass();
1609 // add a separate paragraph for the caption inset
1610 pars.push_back(Paragraph());
1611 pars.back().setInsetOwner(&cur.text()->inset());
1612 pars.back().setPlainOrDefaultLayout(tclass);
1613 int cap_pit = pars.size() - 1;
1615 // if an empty inset was created, we create an additional empty
1616 // paragraph at the bottom so that the user can choose where to put
1617 // the graphics (or table).
1619 pars.push_back(Paragraph());
1620 pars.back().setInsetOwner(&cur.text()->inset());
1621 pars.back().setPlainOrDefaultLayout(tclass);
1624 // reposition the cursor to the caption
1625 cur.pit() = cap_pit;
1627 // FIXME: This Text/Cursor dispatch handling is a mess!
1628 // We cannot use Cursor::dispatch here it needs access to up to
1630 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1631 doInsertInset(cur, cur.text(), cmd_caption, true, false);
1632 bv->buffer().updateLabels();
1633 cur.updateFlags(Update::Force);
1634 // FIXME: When leaving the Float (or Wrap) inset we should
1635 // delete any empty paragraph left above or below the
1640 case LFUN_NOMENCL_INSERT: {
1641 InsetCommandParams p(NOMENCL_CODE);
1642 if (cmd.argument().empty())
1643 p["symbol"] = bv->cursor().innerText()->getStringToIndex(bv->cursor());
1645 p["symbol"] = cmd.argument();
1646 string const data = InsetCommand::params2string("nomenclature", p);
1647 bv->showDialog("nomenclature", data);
1651 case LFUN_INDEX_PRINT: {
1652 InsetCommandParams p(INDEX_PRINT_CODE);
1653 if (cmd.argument().empty())
1654 p["type"] = from_ascii("idx");
1656 p["type"] = cmd.argument();
1657 string const data = InsetCommand::params2string("index_print", p);
1658 FuncRequest fr(LFUN_INSET_INSERT, data);
1663 case LFUN_NOMENCL_PRINT:
1664 case LFUN_TOC_INSERT:
1665 case LFUN_LINE_INSERT:
1666 case LFUN_NEWPAGE_INSERT:
1668 doInsertInset(cur, this, cmd, false, false);
1672 case LFUN_DEPTH_DECREMENT:
1673 changeDepth(cur, DEC_DEPTH);
1676 case LFUN_DEPTH_INCREMENT:
1677 changeDepth(cur, INC_DEPTH);
1680 case LFUN_MATH_DISPLAY:
1681 mathDispatch(cur, cmd, true);
1684 case LFUN_REGEXP_MODE:
1685 regexpDispatch(cur, cmd);
1688 case LFUN_MATH_MODE:
1689 if (cmd.argument() == "on")
1690 // don't pass "on" as argument
1691 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1693 mathDispatch(cur, cmd, false);
1696 case LFUN_MATH_MACRO:
1697 if (cmd.argument().empty())
1698 cur.errorMessage(from_utf8(N_("Missing argument")));
1700 string s = to_utf8(cmd.argument());
1701 string const s1 = token(s, ' ', 1);
1702 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1703 string const s2 = token(s, ' ', 2);
1704 MacroType type = MacroTypeNewcommand;
1706 type = MacroTypeDef;
1707 MathMacroTemplate * inset = new MathMacroTemplate(cur.buffer(),
1708 from_utf8(token(s, ' ', 0)), nargs, false, type);
1709 inset->setBuffer(bv->buffer());
1710 insertInset(cur, inset);
1712 // enter macro inset and select the name
1714 cur.top().pos() = cur.top().lastpos();
1716 cur.setSelection(true);
1717 cur.top().pos() = 0;
1721 // passthrough hat and underscore outside mathed:
1722 case LFUN_MATH_SUBSCRIPT:
1723 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1725 case LFUN_MATH_SUPERSCRIPT:
1726 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1729 case LFUN_MATH_INSERT:
1730 case LFUN_MATH_AMS_MATRIX:
1731 case LFUN_MATH_MATRIX:
1732 case LFUN_MATH_DELIM:
1733 case LFUN_MATH_BIGDELIM: {
1735 cap::replaceSelection(cur);
1736 cur.insert(new InsetMathHull(cur.buffer(), hullSimple));
1737 checkAndActivateInset(cur, true);
1738 LASSERT(cur.inMathed(), /**/);
1743 case LFUN_FONT_EMPH: {
1744 Font font(ignore_font, ignore_language);
1745 font.fontInfo().setEmph(FONT_TOGGLE);
1746 toggleAndShow(cur, this, font);
1750 case LFUN_FONT_ITAL: {
1751 Font font(ignore_font, ignore_language);
1752 font.fontInfo().setShape(ITALIC_SHAPE);
1753 toggleAndShow(cur, this, font);
1757 case LFUN_FONT_BOLD:
1758 case LFUN_FONT_BOLDSYMBOL: {
1759 Font font(ignore_font, ignore_language);
1760 font.fontInfo().setSeries(BOLD_SERIES);
1761 toggleAndShow(cur, this, font);
1765 case LFUN_FONT_NOUN: {
1766 Font font(ignore_font, ignore_language);
1767 font.fontInfo().setNoun(FONT_TOGGLE);
1768 toggleAndShow(cur, this, font);
1772 case LFUN_FONT_TYPEWRITER: {
1773 Font font(ignore_font, ignore_language);
1774 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1775 toggleAndShow(cur, this, font);
1779 case LFUN_FONT_SANS: {
1780 Font font(ignore_font, ignore_language);
1781 font.fontInfo().setFamily(SANS_FAMILY);
1782 toggleAndShow(cur, this, font);
1786 case LFUN_FONT_ROMAN: {
1787 Font font(ignore_font, ignore_language);
1788 font.fontInfo().setFamily(ROMAN_FAMILY);
1789 toggleAndShow(cur, this, font);
1793 case LFUN_FONT_DEFAULT: {
1794 Font font(inherit_font, ignore_language);
1795 toggleAndShow(cur, this, font);
1799 case LFUN_FONT_STRIKEOUT: {
1800 Font font(ignore_font, ignore_language);
1801 font.fontInfo().setStrikeout(FONT_TOGGLE);
1802 toggleAndShow(cur, this, font);
1806 case LFUN_FONT_UULINE: {
1807 Font font(ignore_font, ignore_language);
1808 font.fontInfo().setUuline(FONT_TOGGLE);
1809 toggleAndShow(cur, this, font);
1813 case LFUN_FONT_UWAVE: {
1814 Font font(ignore_font, ignore_language);
1815 font.fontInfo().setUwave(FONT_TOGGLE);
1816 toggleAndShow(cur, this, font);
1820 case LFUN_FONT_UNDERLINE: {
1821 Font font(ignore_font, ignore_language);
1822 font.fontInfo().setUnderbar(FONT_TOGGLE);
1823 toggleAndShow(cur, this, font);
1827 case LFUN_FONT_SIZE: {
1828 Font font(ignore_font, ignore_language);
1829 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1830 toggleAndShow(cur, this, font);
1834 case LFUN_LANGUAGE: {
1835 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1838 Font font(ignore_font, lang);
1839 toggleAndShow(cur, this, font);
1843 case LFUN_TEXTSTYLE_APPLY:
1844 toggleAndShow(cur, this, freefont, toggleall);
1845 cur.message(_("Character set"));
1848 // Set the freefont using the contents of \param data dispatched from
1849 // the frontends and apply it at the current cursor location.
1850 case LFUN_TEXTSTYLE_UPDATE: {
1853 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1856 toggleAndShow(cur, this, freefont, toggleall);
1857 cur.message(_("Character set"));
1859 lyxerr << "Argument not ok";
1864 case LFUN_FINISHED_LEFT:
1865 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1866 // We're leaving an inset, going left. If the inset is LTR, we're
1867 // leaving from the front, so we should not move (remain at --- but
1868 // not in --- the inset). If the inset is RTL, move left, without
1869 // entering the inset itself; i.e., move to after the inset.
1870 if (cur.paragraph().getFontSettings(
1871 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1872 cursorVisLeft(cur, true);
1875 case LFUN_FINISHED_RIGHT:
1876 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1877 // We're leaving an inset, going right. If the inset is RTL, we're
1878 // leaving from the front, so we should not move (remain at --- but
1879 // not in --- the inset). If the inset is LTR, move right, without
1880 // entering the inset itself; i.e., move to after the inset.
1881 if (!cur.paragraph().getFontSettings(
1882 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1883 cursorVisRight(cur, true);
1886 case LFUN_FINISHED_BACKWARD:
1887 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1890 case LFUN_FINISHED_FORWARD:
1891 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1893 cur.setCurrentFont();
1896 case LFUN_LAYOUT_PARAGRAPH: {
1898 params2string(cur.paragraph(), data);
1899 data = "show\n" + data;
1900 bv->showDialog("paragraph", data);
1904 case LFUN_PARAGRAPH_UPDATE: {
1906 params2string(cur.paragraph(), data);
1908 // Will the paragraph accept changes from the dialog?
1910 cur.inset().allowParagraphCustomization(cur.idx());
1912 data = "update " + convert<string>(accept) + '\n' + data;
1913 bv->updateDialog("paragraph", data);
1917 case LFUN_ACCENT_UMLAUT:
1918 case LFUN_ACCENT_CIRCUMFLEX:
1919 case LFUN_ACCENT_GRAVE:
1920 case LFUN_ACCENT_ACUTE:
1921 case LFUN_ACCENT_TILDE:
1922 case LFUN_ACCENT_CEDILLA:
1923 case LFUN_ACCENT_MACRON:
1924 case LFUN_ACCENT_DOT:
1925 case LFUN_ACCENT_UNDERDOT:
1926 case LFUN_ACCENT_UNDERBAR:
1927 case LFUN_ACCENT_CARON:
1928 case LFUN_ACCENT_BREVE:
1929 case LFUN_ACCENT_TIE:
1930 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1931 case LFUN_ACCENT_CIRCLE:
1932 case LFUN_ACCENT_OGONEK:
1933 theApp()->handleKeyFunc(cmd.action);
1934 if (!cmd.argument().empty())
1935 // FIXME: Are all these characters encoded in one byte in utf8?
1936 bv->translateAndInsert(cmd.argument()[0], this, cur);
1939 case LFUN_FLOAT_LIST_INSERT: {
1940 DocumentClass const & tclass = bv->buffer().params().documentClass();
1941 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1943 if (cur.selection())
1944 cutSelection(cur, true, false);
1945 breakParagraph(cur);
1947 if (cur.lastpos() != 0) {
1948 cursorBackward(cur);
1949 breakParagraph(cur);
1952 docstring const laystr = cur.inset().usePlainLayout() ?
1953 tclass.plainLayoutName() :
1954 tclass.defaultLayoutName();
1955 setLayout(cur, laystr);
1956 ParagraphParameters p;
1957 // FIXME If this call were replaced with one to clearParagraphParams(),
1958 // then we could get rid of this method altogether.
1959 setParagraphs(cur, p);
1960 // FIXME This should be simplified when InsetFloatList takes a
1961 // Buffer in its constructor.
1962 InsetFloatList * ifl = new InsetFloatList(cur.buffer(), to_utf8(cmd.argument()));
1963 ifl->setBuffer(bv->buffer());
1964 insertInset(cur, ifl);
1967 lyxerr << "Non-existent float type: "
1968 << to_utf8(cmd.argument()) << endl;
1973 case LFUN_CHANGE_ACCEPT: {
1974 acceptOrRejectChanges(cur, ACCEPT);
1978 case LFUN_CHANGE_REJECT: {
1979 acceptOrRejectChanges(cur, REJECT);
1983 case LFUN_THESAURUS_ENTRY: {
1984 docstring arg = cmd.argument();
1986 arg = cur.selectionAsString(false);
1988 if (arg.size() > 100 || arg.empty()) {
1989 // Get word or selection
1990 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1991 arg = cur.selectionAsString(false);
1992 arg += " lang=" + from_ascii(cur.getFont().language()->lang());
1995 bv->showDialog("thesaurus", to_utf8(arg));
1999 case LFUN_PARAGRAPH_PARAMS_APPLY: {
2000 // Given data, an encoding of the ParagraphParameters
2001 // generated in the Paragraph dialog, this function sets
2002 // the current paragraph, or currently selected paragraphs,
2004 // NOTE: This function overrides all existing settings.
2005 setParagraphs(cur, cmd.argument());
2006 cur.message(_("Paragraph layout set"));
2010 case LFUN_PARAGRAPH_PARAMS: {
2011 // Given data, an encoding of the ParagraphParameters as we'd
2012 // find them in a LyX file, this function modifies the current paragraph,
2013 // or currently selected paragraphs.
2014 // NOTE: This function only modifies, and does not override, existing
2016 setParagraphs(cur, cmd.argument(), true);
2017 cur.message(_("Paragraph layout set"));
2022 if (cur.selection()) {
2023 cur.setSelection(false);
2026 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
2027 // correct, but I'm not 100% sure -- dov, 071019
2028 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
2032 case LFUN_OUTLINE_UP:
2033 outline(OutlineUp, cur);
2034 setCursor(cur, cur.pit(), 0);
2035 cur.buffer()->updateLabels();
2039 case LFUN_OUTLINE_DOWN:
2040 outline(OutlineDown, cur);
2041 setCursor(cur, cur.pit(), 0);
2042 cur.buffer()->updateLabels();
2046 case LFUN_OUTLINE_IN:
2047 outline(OutlineIn, cur);
2048 cur.buffer()->updateLabels();
2052 case LFUN_OUTLINE_OUT:
2053 outline(OutlineOut, cur);
2054 cur.buffer()->updateLabels();
2059 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
2064 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
2066 // FIXME: The cursor flag is reset two lines below
2067 // so we need to check here if some of the LFUN did touch that.
2068 // for now only Text::erase() and Text::backspace() do that.
2069 // The plan is to verify all the LFUNs and then to remove this
2070 // singleParUpdate boolean altogether.
2071 if (cur.result().update() & Update::Force) {
2072 singleParUpdate = false;
2076 // FIXME: the following code should go in favor of fine grained
2077 // update flag treatment.
2078 if (singleParUpdate) {
2079 // Inserting characters does not change par height in general. So, try
2080 // to update _only_ this paragraph. BufferView will detect if a full
2081 // metrics update is needed anyway.
2082 cur.updateFlags(Update::SinglePar | Update::FitCursor);
2087 && &oldTopSlice.inset() == &cur.inset()
2088 && oldTopSlice.idx() == cur.idx()
2089 && !sel // sel is a backup of cur.selection() at the beginning of the function.
2090 && !cur.selection())
2091 // FIXME: it would be better if we could just do this
2093 //if (cur.result().update() != Update::FitCursor)
2096 // But some LFUNs do not set Update::FitCursor when needed, so we
2097 // do it for all. This is not very harmfull as FitCursor will provoke
2098 // a full redraw only if needed but still, a proper review of all LFUN
2099 // should be done and this needsUpdate boolean can then be removed.
2100 cur.updateFlags(Update::FitCursor);
2102 cur.updateFlags(Update::Force | Update::FitCursor);
2106 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
2107 FuncStatus & flag) const
2109 LASSERT(cur.text() == this, /**/);
2111 FontInfo const & fontinfo = cur.real_current_font.fontInfo();
2113 InsetCode code = NO_CODE;
2115 switch (cmd.action) {
2117 case LFUN_DEPTH_DECREMENT:
2118 enable = changeDepthAllowed(cur, DEC_DEPTH);
2121 case LFUN_DEPTH_INCREMENT:
2122 enable = changeDepthAllowed(cur, INC_DEPTH);
2126 flag.setOnOff(cur.paragraph().params().startOfAppendix());
2129 case LFUN_DIALOG_SHOW_NEW_INSET:
2130 if (cmd.argument() == "bibitem")
2131 code = BIBITEM_CODE;
2132 else if (cmd.argument() == "bibtex") {
2134 // not allowed in description items
2135 enable = !inDescriptionItem(cur);
2137 else if (cmd.argument() == "box")
2139 else if (cmd.argument() == "branch")
2141 else if (cmd.argument() == "citation")
2143 else if (cmd.argument() == "ert")
2145 else if (cmd.argument() == "external")
2146 code = EXTERNAL_CODE;
2147 else if (cmd.argument() == "float")
2149 else if (cmd.argument() == "graphics")
2150 code = GRAPHICS_CODE;
2151 else if (cmd.argument() == "href")
2152 code = HYPERLINK_CODE;
2153 else if (cmd.argument() == "include")
2154 code = INCLUDE_CODE;
2155 else if (cmd.argument() == "index")
2157 else if (cmd.argument() == "index_print")
2158 code = INDEX_PRINT_CODE;
2159 else if (cmd.argument() == "nomenclature")
2160 code = NOMENCL_CODE;
2161 else if (cmd.argument() == "nomencl_print")
2162 code = NOMENCL_PRINT_CODE;
2163 else if (cmd.argument() == "label")
2165 else if (cmd.argument() == "note")
2167 else if (cmd.argument() == "phantom")
2168 code = PHANTOM_CODE;
2169 else if (cmd.argument() == "ref")
2171 else if (cmd.argument() == "space")
2173 else if (cmd.argument() == "toc")
2175 else if (cmd.argument() == "vspace")
2177 else if (cmd.argument() == "wrap")
2179 else if (cmd.argument() == "listings")
2180 code = LISTINGS_CODE;
2183 case LFUN_ERT_INSERT:
2186 case LFUN_LISTING_INSERT:
2187 code = LISTINGS_CODE;
2188 // not allowed in description items
2189 enable = !inDescriptionItem(cur);
2191 case LFUN_FOOTNOTE_INSERT:
2194 case LFUN_TABULAR_INSERT:
2195 code = TABULAR_CODE;
2197 case LFUN_MARGINALNOTE_INSERT:
2200 case LFUN_FLOAT_INSERT:
2201 case LFUN_FLOAT_WIDE_INSERT:
2202 // FIXME: If there is a selection, we should check whether there
2203 // are floats in the selection, but this has performance issues, see
2204 // LFUN_CHANGE_ACCEPT/REJECT.
2206 if (inDescriptionItem(cur))
2207 // not allowed in description items
2210 InsetCode const inset_code = cur.inset().lyxCode();
2212 // algorithm floats cannot be put in another float
2213 if (to_utf8(cmd.argument()) == "algorithm") {
2214 enable = inset_code != WRAP_CODE && inset_code != FLOAT_CODE;
2218 // for figures and tables: only allow in another
2219 // float or wrap if it is of the same type and
2220 // not a subfloat already
2221 if(cur.inset().lyxCode() == code) {
2222 InsetFloat const & ins =
2223 static_cast<InsetFloat const &>(cur.inset());
2224 enable = ins.params().type == to_utf8(cmd.argument())
2225 && !ins.params().subfloat;
2226 } else if(cur.inset().lyxCode() == WRAP_CODE) {
2227 InsetWrap const & ins =
2228 static_cast<InsetWrap const &>(cur.inset());
2229 enable = ins.params().type == to_utf8(cmd.argument());
2233 case LFUN_WRAP_INSERT:
2235 // not allowed in description items
2236 enable = !inDescriptionItem(cur);
2238 case LFUN_FLOAT_LIST_INSERT:
2239 code = FLOAT_LIST_CODE;
2240 // not allowed in description items
2241 enable = !inDescriptionItem(cur);
2243 case LFUN_CAPTION_INSERT:
2244 code = CAPTION_CODE;
2245 // not allowed in description items
2246 enable = !inDescriptionItem(cur);
2248 case LFUN_NOTE_INSERT:
2250 // in commands (sections etc.) and description items,
2251 // only Notes are allowed
2252 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
2253 (!cur.paragraph().layout().isCommand()
2254 && !inDescriptionItem(cur)));
2256 case LFUN_FLEX_INSERT: {
2258 string s = cmd.getArg(0);
2260 cur.buffer()->params().documentClass().insetLayout(from_utf8(s));
2261 if (il.lyxtype() != InsetLayout::CHARSTYLE &&
2262 il.lyxtype() != InsetLayout::CUSTOM &&
2263 il.lyxtype() != InsetLayout::ELEMENT &&
2264 il.lyxtype ()!= InsetLayout::STANDARD)
2268 case LFUN_BOX_INSERT:
2271 case LFUN_BRANCH_INSERT:
2273 if (cur.buffer()->masterBuffer()->params().branchlist().empty()
2274 && cur.buffer()->params().branchlist().empty())
2277 case LFUN_PHANTOM_INSERT:
2278 code = PHANTOM_CODE;
2280 case LFUN_LABEL_INSERT:
2283 case LFUN_LINE_INSERT:
2286 case LFUN_INFO_INSERT:
2289 case LFUN_OPTIONAL_INSERT:
2291 enable = cur.paragraph().insetList().count(OPTARG_CODE)
2292 < cur.paragraph().layout().optionalargs;
2294 case LFUN_INDEX_INSERT:
2297 case LFUN_INDEX_PRINT:
2298 code = INDEX_PRINT_CODE;
2299 // not allowed in description items
2300 enable = !inDescriptionItem(cur);
2302 case LFUN_NOMENCL_INSERT:
2303 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2307 code = NOMENCL_CODE;
2309 case LFUN_NOMENCL_PRINT:
2310 code = NOMENCL_PRINT_CODE;
2311 // not allowed in description items
2312 enable = !inDescriptionItem(cur);
2314 case LFUN_TOC_INSERT:
2316 // not allowed in description items
2317 enable = !inDescriptionItem(cur);
2319 case LFUN_HYPERLINK_INSERT:
2320 if (cur.selIsMultiCell() || cur.selIsMultiLine()) {
2324 code = HYPERLINK_CODE;
2326 case LFUN_QUOTE_INSERT:
2327 // always allow this, since we will inset a raw quote
2328 // if an inset is not allowed.
2330 case LFUN_SPECIALCHAR_INSERT:
2331 code = SPECIALCHAR_CODE;
2333 case LFUN_SPACE_INSERT:
2334 // slight hack: we know this is allowed in math mode
2339 case LFUN_MATH_INSERT:
2340 case LFUN_MATH_AMS_MATRIX:
2341 case LFUN_MATH_MATRIX:
2342 case LFUN_MATH_DELIM:
2343 case LFUN_MATH_BIGDELIM:
2344 case LFUN_MATH_DISPLAY:
2345 case LFUN_MATH_MODE:
2346 case LFUN_MATH_MACRO:
2347 case LFUN_MATH_SUBSCRIPT:
2348 case LFUN_MATH_SUPERSCRIPT:
2349 code = MATH_HULL_CODE;
2352 case LFUN_INSET_MODIFY:
2353 // We need to disable this, because we may get called for a
2355 // InsetTabular::getStatus() -> InsetText::getStatus()
2356 // and we don't handle LFUN_INSET_MODIFY.
2360 case LFUN_FONT_EMPH:
2361 flag.setOnOff(fontinfo.emph() == FONT_ON);
2364 case LFUN_FONT_ITAL:
2365 flag.setOnOff(fontinfo.shape() == ITALIC_SHAPE);
2368 case LFUN_FONT_NOUN:
2369 flag.setOnOff(fontinfo.noun() == FONT_ON);
2372 case LFUN_FONT_BOLD:
2373 case LFUN_FONT_BOLDSYMBOL:
2374 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2377 case LFUN_FONT_SANS:
2378 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2381 case LFUN_FONT_ROMAN:
2382 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2385 case LFUN_FONT_TYPEWRITER:
2386 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2391 enable = cur.selection();
2395 if (cmd.argument().empty()) {
2396 if (theClipboard().isInternal())
2397 enable = cap::numberOfSelections() > 0;
2399 enable = !theClipboard().empty();
2403 // we have an argument
2404 string const arg = to_utf8(cmd.argument());
2405 if (isStrUnsignedInt(arg)) {
2406 // it's a number and therefore means the internal stack
2407 unsigned int n = convert<unsigned int>(arg);
2408 enable = cap::numberOfSelections() > n;
2412 // explicit graphics type?
2413 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
2414 if ((arg == "pdf" && (type = Clipboard::PdfGraphicsType))
2415 || (arg == "png" && (type = Clipboard::PngGraphicsType))
2416 || (arg == "jpeg" && (type = Clipboard::JpegGraphicsType))
2417 || (arg == "linkback" && (type = Clipboard::LinkBackGraphicsType))
2418 || (arg == "emf" && (type = Clipboard::EmfGraphicsType))
2419 || (arg == "wmf" && (type = Clipboard::WmfGraphicsType))) {
2420 enable = theClipboard().hasGraphicsContents(type);
2429 case LFUN_CLIPBOARD_PASTE:
2430 enable = !theClipboard().empty();
2433 case LFUN_PRIMARY_SELECTION_PASTE:
2434 enable = cur.selection() || !theSelection().empty();
2437 case LFUN_SELECTION_PASTE:
2438 enable = cap::selection();
2441 case LFUN_PARAGRAPH_MOVE_UP:
2442 enable = cur.pit() > 0 && !cur.selection();
2445 case LFUN_PARAGRAPH_MOVE_DOWN:
2446 enable = cur.pit() < cur.lastpit() && !cur.selection();
2449 case LFUN_CHANGE_ACCEPT:
2450 case LFUN_CHANGE_REJECT:
2451 // In principle, these LFUNs should only be enabled if there
2452 // is a change at the current position/in the current selection.
2453 // However, without proper optimizations, this will inevitably
2454 // result in unacceptable performance - just imagine a user who
2455 // wants to select the complete content of a long document.
2456 if (!cur.selection())
2457 enable = cur.paragraph().isChanged(cur.pos());
2459 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2464 case LFUN_OUTLINE_UP:
2465 case LFUN_OUTLINE_DOWN:
2466 case LFUN_OUTLINE_IN:
2467 case LFUN_OUTLINE_OUT:
2469 case LFUN_NEWLINE_INSERT:
2470 // LaTeX restrictions (labels or empty par)
2471 enable = (cur.pos() > cur.paragraph().beginOfBody());
2474 case LFUN_TAB_INSERT:
2475 case LFUN_TAB_DELETE:
2476 enable = cur.inset().getLayout().isPassThru();
2479 case LFUN_SET_GRAPHICS_GROUP: {
2480 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2484 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2488 case LFUN_NEWPAGE_INSERT:
2489 // not allowed in description items
2490 code = NEWPAGE_CODE;
2491 enable = !inDescriptionItem(cur);
2494 case LFUN_DATE_INSERT: {
2495 string const format = cmd.argument().empty()
2496 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
2497 enable = support::os::is_valid_strftime(format);
2502 enable = !cur.inset().getLayout().isPassThru();
2505 case LFUN_BREAK_PARAGRAPH:
2506 enable = cur.inset().getLayout().isMultiPar();
2509 case LFUN_WORD_DELETE_FORWARD:
2510 case LFUN_WORD_DELETE_BACKWARD:
2511 case LFUN_LINE_DELETE:
2512 case LFUN_WORD_FORWARD:
2513 case LFUN_WORD_BACKWARD:
2514 case LFUN_WORD_RIGHT:
2515 case LFUN_WORD_LEFT:
2516 case LFUN_CHAR_FORWARD:
2517 case LFUN_CHAR_FORWARD_SELECT:
2518 case LFUN_CHAR_BACKWARD:
2519 case LFUN_CHAR_BACKWARD_SELECT:
2520 case LFUN_CHAR_LEFT:
2521 case LFUN_CHAR_LEFT_SELECT:
2522 case LFUN_CHAR_RIGHT:
2523 case LFUN_CHAR_RIGHT_SELECT:
2525 case LFUN_UP_SELECT:
2527 case LFUN_DOWN_SELECT:
2528 case LFUN_PARAGRAPH_UP_SELECT:
2529 case LFUN_PARAGRAPH_DOWN_SELECT:
2530 case LFUN_LINE_BEGIN_SELECT:
2531 case LFUN_LINE_END_SELECT:
2532 case LFUN_WORD_FORWARD_SELECT:
2533 case LFUN_WORD_BACKWARD_SELECT:
2534 case LFUN_WORD_RIGHT_SELECT:
2535 case LFUN_WORD_LEFT_SELECT:
2536 case LFUN_WORD_SELECT:
2537 case LFUN_SECTION_SELECT:
2538 case LFUN_PARAGRAPH_UP:
2539 case LFUN_PARAGRAPH_DOWN:
2540 case LFUN_LINE_BEGIN:
2542 case LFUN_CHAR_DELETE_FORWARD:
2543 case LFUN_CHAR_DELETE_BACKWARD:
2544 case LFUN_INSET_INSERT:
2545 case LFUN_WORD_UPCASE:
2546 case LFUN_WORD_LOWCASE:
2547 case LFUN_WORD_CAPITALIZE:
2548 case LFUN_CHARS_TRANSPOSE:
2549 case LFUN_SERVER_GET_XY:
2550 case LFUN_SERVER_SET_XY:
2551 case LFUN_SERVER_GET_LAYOUT:
2553 case LFUN_SELF_INSERT:
2554 case LFUN_FONT_DEFAULT:
2555 case LFUN_FONT_UNDERLINE:
2556 case LFUN_FONT_STRIKEOUT:
2557 case LFUN_FONT_UULINE:
2558 case LFUN_FONT_UWAVE:
2559 case LFUN_FONT_SIZE:
2560 case LFUN_TEXTSTYLE_APPLY:
2561 case LFUN_TEXTSTYLE_UPDATE:
2562 case LFUN_LAYOUT_PARAGRAPH:
2563 case LFUN_PARAGRAPH_UPDATE:
2564 case LFUN_ACCENT_UMLAUT:
2565 case LFUN_ACCENT_CIRCUMFLEX:
2566 case LFUN_ACCENT_GRAVE:
2567 case LFUN_ACCENT_ACUTE:
2568 case LFUN_ACCENT_TILDE:
2569 case LFUN_ACCENT_CEDILLA:
2570 case LFUN_ACCENT_MACRON:
2571 case LFUN_ACCENT_DOT:
2572 case LFUN_ACCENT_UNDERDOT:
2573 case LFUN_ACCENT_UNDERBAR:
2574 case LFUN_ACCENT_CARON:
2575 case LFUN_ACCENT_BREVE:
2576 case LFUN_ACCENT_TIE:
2577 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2578 case LFUN_ACCENT_CIRCLE:
2579 case LFUN_ACCENT_OGONEK:
2580 case LFUN_THESAURUS_ENTRY:
2581 case LFUN_PARAGRAPH_PARAMS_APPLY:
2582 case LFUN_PARAGRAPH_PARAMS:
2584 case LFUN_BUFFER_BEGIN:
2585 case LFUN_BUFFER_END:
2586 case LFUN_BUFFER_BEGIN_SELECT:
2587 case LFUN_BUFFER_END_SELECT:
2588 case LFUN_INSET_BEGIN:
2589 case LFUN_INSET_END:
2590 case LFUN_INSET_BEGIN_SELECT:
2591 case LFUN_INSET_END_SELECT:
2592 case LFUN_INSET_SELECT_ALL:
2593 case LFUN_UNICODE_INSERT:
2594 // these are handled in our dispatch()
2604 || !cur.inset().insetAllowed(code)
2605 || cur.paragraph().layout().pass_thru))
2608 switch (cmd.action) {
2609 case LFUN_ACCENT_ACUTE:
2610 case LFUN_ACCENT_BREVE:
2611 case LFUN_ACCENT_CARON:
2612 case LFUN_ACCENT_CEDILLA:
2613 case LFUN_ACCENT_CIRCLE:
2614 case LFUN_ACCENT_CIRCUMFLEX:
2615 case LFUN_ACCENT_DOT:
2616 case LFUN_ACCENT_GRAVE:
2617 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2618 case LFUN_ACCENT_MACRON:
2619 case LFUN_ACCENT_OGONEK:
2620 case LFUN_ACCENT_TIE:
2621 case LFUN_ACCENT_TILDE:
2622 case LFUN_ACCENT_UMLAUT:
2623 case LFUN_ACCENT_UNDERBAR:
2624 case LFUN_ACCENT_UNDERDOT:
2625 case LFUN_FONT_BOLD:
2626 case LFUN_FONT_BOLDSYMBOL:
2627 case LFUN_FONT_TYPEWRITER:
2628 case LFUN_FONT_DEFAULT:
2629 case LFUN_FONT_EMPH:
2630 case LFUN_FONT_NOUN:
2631 case LFUN_FONT_ROMAN:
2632 case LFUN_FONT_SANS:
2633 case LFUN_FONT_FRAK:
2634 case LFUN_FONT_ITAL:
2635 case LFUN_FONT_SIZE:
2636 case LFUN_FONT_STATE:
2637 case LFUN_FONT_UNDERLINE:
2638 case LFUN_FONT_STRIKEOUT:
2639 case LFUN_FONT_UULINE:
2640 case LFUN_FONT_UWAVE:
2641 case LFUN_TEXTSTYLE_APPLY:
2642 case LFUN_TEXTSTYLE_UPDATE:
2643 if (cur.inset().getLayout().isPassThru())
2650 flag.setEnabled(enable);
2655 void Text::pasteString(Cursor & cur, docstring const & clip,
2658 cur.clearSelection();
2659 if (!clip.empty()) {
2662 insertStringAsParagraphs(cur, clip, cur.current_font);
2664 insertStringAsLines(cur, clip, cur.current_font);
2669 // FIXME: an item inset would make things much easier.
2670 bool Text::inDescriptionItem(Cursor & cur) const
2672 Paragraph & par = cur.paragraph();
2673 pos_type const pos = cur.pos();
2674 pos_type const body_pos = par.beginOfBody();
2676 if (par.layout().latextype != LATEX_LIST_ENVIRONMENT
2677 && (par.layout().latextype != LATEX_ITEM_ENVIRONMENT
2678 || par.layout().margintype != MARGIN_FIRST_DYNAMIC))
2681 return (pos < body_pos
2683 && (pos == 0 || par.getChar(pos - 1) != ' ')));