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"
29 #include "CutAndPaste.h"
30 #include "DispatchResult.h"
31 #include "ErrorList.h"
33 #include "FuncRequest.h"
34 #include "InsetList.h"
38 #include "LyXAction.h"
42 #include "Paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "TextClass.h"
46 #include "TextMetrics.h"
49 #include "frontends/Clipboard.h"
50 #include "frontends/Selection.h"
52 #include "insets/InsetCollapsable.h"
53 #include "insets/InsetCommand.h"
54 #include "insets/InsetFloatList.h"
55 #include "insets/InsetNewline.h"
56 #include "insets/InsetQuotes.h"
57 #include "insets/InsetSpecialChar.h"
58 #include "insets/InsetText.h"
59 #include "insets/InsetInfo.h"
61 #include "support/convert.h"
62 #include "support/debug.h"
63 #include "support/gettext.h"
64 #include "support/lstrings.h"
65 #include "support/lyxtime.h"
67 #include "mathed/InsetMathHull.h"
68 #include "mathed/MathMacroTemplate.h"
70 #include <boost/next_prior.hpp>
76 using namespace lyx::support;
80 using cap::copySelection;
81 using cap::cutSelection;
82 using cap::pasteFromStack;
83 using cap::pasteClipboardText;
84 using cap::pasteClipboardGraphics;
85 using cap::replaceSelection;
88 static Font freefont(ignore_font, ignore_language);
89 static bool toggleall = false;
91 static void toggleAndShow(Cursor & cur, Text * text,
92 Font const & font, bool toggleall = true)
94 text->toggleFree(cur, font, toggleall);
96 if (font.language() != ignore_language ||
97 font.fontInfo().number() != FONT_IGNORE) {
98 TextMetrics const & tm = cur.bv().textMetrics(text);
99 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
100 cur.real_current_font))
101 text->setCursor(cur, cur.pit(), cur.pos(),
102 false, !cur.boundary());
107 static void moveCursor(Cursor & cur, bool selecting)
109 if (selecting || cur.mark())
114 static void finishChange(Cursor & cur, bool selecting)
117 moveCursor(cur, selecting);
121 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
124 docstring sel = cur.selectionAsString(false);
126 // It may happen that sel is empty but there is a selection
127 replaceSelection(cur);
130 #ifdef ENABLE_ASSERTIONS
131 const int old_pos = cur.pos();
133 cur.insert(new InsetMathHull(hullSimple));
134 #ifdef ENABLE_ASSERTIONS
135 LASSERT(old_pos == cur.pos(), /**/);
137 cur.nextInset()->edit(cur, true);
138 // don't do that also for LFUN_MATH_MODE
139 // unless you want end up with always changing
140 // to mathrm when opening an inlined inset --
141 // I really hate "LyXfunc overloading"...
143 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
144 // Avoid an unnecessary undo step if cmd.argument
146 if (!cmd.argument().empty())
147 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
150 // create a macro if we see "\\newcommand"
151 // somewhere, and an ordinary formula
153 if (sel.find(from_ascii("\\newcommand")) == string::npos
154 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
155 && sel.find(from_ascii("\\def")) == string::npos)
157 InsetMathHull * formula = new InsetMathHull;
158 istringstream is(to_utf8(sel));
162 if (formula->getType() == hullNone)
163 // Don't create pseudo formulas if
164 // delimiters are left out
165 formula->mutate(hullSimple);
168 cur.insert(new MathMacroTemplate(sel));
171 cur.message(from_utf8(N_("Math editor mode")));
175 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
178 cap::replaceSelection(cur);
179 cur.insert(new InsetSpecialChar(kind));
184 static bool doInsertInset(Cursor & cur, Text * text,
185 FuncRequest const & cmd, bool edit, bool pastesel)
187 Buffer & buffer = cur.bv().buffer();
188 BufferParams const & bparams = buffer.params();
189 Inset * inset = createInset(buffer, cmd);
193 if (InsetCollapsable * ci = inset->asInsetCollapsable())
194 ci->setLayout(bparams);
197 if (cmd.action == LFUN_INDEX_INSERT) {
198 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
199 text->insertInset(cur, inset);
201 inset->edit(cur, true);
202 // Now put this into inset
203 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
208 if (cur.selection()) {
209 lyx::dispatch(FuncRequest(LFUN_CUT));
212 text->insertInset(cur, inset);
215 inset->edit(cur, true);
217 if (!gotsel || !pastesel)
220 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
221 InsetText * insetText = dynamic_cast<InsetText *>(inset);
222 if (insetText && !insetText->allowMultiPar() || cur.lastpit() == 0) {
223 // reset first par to default
224 cur.text()->paragraphs().begin()
225 ->setEmptyOrDefaultLayout(bparams.documentClass());
228 // Merge multiple paragraphs -- hack
229 while (cur.lastpit() > 0)
230 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
232 // reset surrounding par to default
233 docstring const layoutname = insetText->useEmptyLayout()
234 ? bparams.documentClass().emptyLayoutName()
235 : bparams.documentClass().defaultLayoutName();
236 cur.leaveInset(*inset);
237 text->setLayout(cur, layoutname);
244 string const freefont2string()
246 return freefont.toString(toggleall);
250 /// the type of outline operation
252 OutlineUp, // Move this header with text down
253 OutlineDown, // Move this header with text up
254 OutlineIn, // Make this header deeper
255 OutlineOut // Make this header shallower
259 static void outline(OutlineOp mode, Cursor & cur)
261 Buffer & buf = cur.buffer();
262 pit_type & pit = cur.pit();
263 ParagraphList & pars = buf.text().paragraphs();
264 ParagraphList::iterator bgn = pars.begin();
265 // The first paragraph of the area to be copied:
266 ParagraphList::iterator start = boost::next(bgn, pit);
267 // The final paragraph of area to be copied:
268 ParagraphList::iterator finish = start;
269 ParagraphList::iterator end = pars.end();
271 DocumentClass const & tc = buf.params().documentClass();
273 int const thistoclevel = start->layout().toclevel;
276 // Move out (down) from this section header
279 // Seek the one (on same level) below
280 for (; finish != end; ++finish) {
281 toclevel = finish->layout().toclevel;
282 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel) {
289 if (start == pars.begin())
292 ParagraphList::iterator dest = start;
293 // Move out (up) from this header
296 // Search previous same-level header above
299 toclevel = dest->layout().toclevel;
301 && (toclevel == Layout::NOT_IN_TOC
302 || toclevel > thistoclevel));
303 // Not found; do nothing
304 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
306 pit_type const newpit = distance(bgn, dest);
307 pit_type const len = distance(start, finish);
308 pit_type const deletepit = pit + len;
309 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
310 pars.insert(dest, start, finish);
311 start = boost::next(pars.begin(), deletepit);
313 pars.erase(start, finish);
320 // Go one down from *this* header:
321 ParagraphList::iterator dest = boost::next(finish, 1);
322 // Go further down to find header to insert in front of:
323 for (; dest != end; ++dest) {
324 toclevel = dest->layout().toclevel;
325 if (toclevel != Layout::NOT_IN_TOC
326 && toclevel <= thistoclevel) {
330 // One such was found:
331 pit_type newpit = distance(bgn, dest);
332 pit_type const len = distance(start, finish);
333 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
334 pars.insert(dest, start, finish);
335 start = boost::next(bgn, pit);
337 pars.erase(start, finish);
341 pit_type const len = distance(start, finish);
342 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
343 for (; start != finish; ++start) {
344 toclevel = start->layout().toclevel;
345 if (toclevel == Layout::NOT_IN_TOC)
347 DocumentClass::const_iterator lit = tc.begin();
348 DocumentClass::const_iterator len = tc.end();
349 for (; lit != len; ++lit) {
350 if (lit->toclevel == toclevel + 1 &&
351 start->layout().labeltype == lit->labeltype) {
352 start->setLayout(*lit);
360 pit_type const len = distance(start, finish);
361 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
362 for (; start != finish; ++start) {
363 toclevel = start->layout().toclevel;
364 if (toclevel == Layout::NOT_IN_TOC)
366 DocumentClass::const_iterator lit = tc.begin();
367 DocumentClass::const_iterator len = tc.end();
368 for (; lit != len; ++lit) {
369 if (lit->toclevel == toclevel - 1 &&
370 start->layout().labeltype == lit->labeltype) {
371 start->setLayout(*lit);
382 void Text::number(Cursor & cur)
384 FontInfo font = ignore_font;
385 font.setNumber(FONT_TOGGLE);
386 toggleAndShow(cur, this, Font(font, ignore_language));
390 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
392 return par.isRTL(buffer.params());
396 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
398 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
400 BufferView * bv = &cur.bv();
401 TextMetrics & tm = bv->textMetrics(this);
402 if (!tm.contains(cur.pit()))
403 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
405 // FIXME: We use the update flag to indicates wether a singlePar or a
406 // full screen update is needed. We reset it here but shall we restore it
410 LASSERT(cur.text() == this, /**/);
411 CursorSlice oldTopSlice = cur.top();
412 bool oldBoundary = cur.boundary();
413 bool sel = cur.selection();
414 // Signals that, even if needsUpdate == false, an update of the
415 // cursor paragraph is required
416 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
417 LyXAction::SingleParUpdate);
418 // Signals that a full-screen update is required
419 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
420 LyXAction::NoUpdate) || singleParUpdate);
422 switch (cmd.action) {
424 case LFUN_PARAGRAPH_MOVE_DOWN: {
425 pit_type const pit = cur.pit();
426 recUndo(cur, pit, pit + 1);
428 swap(pars_[pit], pars_[pit + 1]);
429 updateLabels(cur.buffer());
435 case LFUN_PARAGRAPH_MOVE_UP: {
436 pit_type const pit = cur.pit();
437 recUndo(cur, pit - 1, pit);
439 swap(pars_[pit], pars_[pit - 1]);
440 updateLabels(cur.buffer());
446 case LFUN_APPENDIX: {
447 Paragraph & par = cur.paragraph();
448 bool start = !par.params().startOfAppendix();
450 // FIXME: The code below only makes sense at top level.
451 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
452 // ensure that we have only one start_of_appendix in this document
453 // FIXME: this don't work for multipart document!
454 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
455 if (pars_[tmp].params().startOfAppendix()) {
457 pars_[tmp].params().startOfAppendix(false);
463 par.params().startOfAppendix(start);
465 // we can set the refreshing parameters now
466 updateLabels(cur.buffer());
470 case LFUN_WORD_DELETE_FORWARD:
471 if (cur.selection()) {
472 cutSelection(cur, true, false);
474 deleteWordForward(cur);
475 finishChange(cur, false);
478 case LFUN_WORD_DELETE_BACKWARD:
479 if (cur.selection()) {
480 cutSelection(cur, true, false);
482 deleteWordBackward(cur);
483 finishChange(cur, false);
486 case LFUN_LINE_DELETE:
487 if (cur.selection()) {
488 cutSelection(cur, true, false);
490 tm.deleteLineForward(cur);
491 finishChange(cur, false);
494 case LFUN_BUFFER_BEGIN:
495 case LFUN_BUFFER_BEGIN_SELECT:
496 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
497 if (cur.depth() == 1) {
498 needsUpdate |= cursorTop(cur);
502 cur.updateFlags(Update::FitCursor);
505 case LFUN_BUFFER_END:
506 case LFUN_BUFFER_END_SELECT:
507 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
508 if (cur.depth() == 1) {
509 needsUpdate |= cursorBottom(cur);
513 cur.updateFlags(Update::FitCursor);
516 case LFUN_CHAR_FORWARD:
517 case LFUN_CHAR_FORWARD_SELECT:
518 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
519 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
520 needsUpdate |= cursorForward(cur);
522 if (!needsUpdate && oldTopSlice == cur.top()
523 && cur.boundary() == oldBoundary) {
525 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
529 case LFUN_CHAR_BACKWARD:
530 case LFUN_CHAR_BACKWARD_SELECT:
531 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
532 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
533 needsUpdate |= cursorBackward(cur);
535 if (!needsUpdate && oldTopSlice == cur.top()
536 && cur.boundary() == oldBoundary) {
538 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
543 case LFUN_CHAR_LEFT_SELECT:
544 if (lyxrc.visual_cursor) {
545 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
546 needsUpdate |= cursorVisLeft(cur);
547 if (!needsUpdate && oldTopSlice == cur.top()
548 && cur.boundary() == oldBoundary) {
550 cmd = FuncRequest(LFUN_FINISHED_LEFT);
553 if (reverseDirectionNeeded(cur)) {
554 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
555 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
557 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
558 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
565 case LFUN_CHAR_RIGHT:
566 case LFUN_CHAR_RIGHT_SELECT:
567 if (lyxrc.visual_cursor) {
568 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
569 needsUpdate |= cursorVisRight(cur);
570 if (!needsUpdate && oldTopSlice == cur.top()
571 && cur.boundary() == oldBoundary) {
573 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
576 if (reverseDirectionNeeded(cur)) {
577 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
578 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
580 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
581 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
590 case LFUN_DOWN_SELECT:
593 // stop/start the selection
594 bool select = cmd.action == LFUN_DOWN_SELECT ||
595 cmd.action == LFUN_UP_SELECT;
596 cur.selHandle(select);
598 // move cursor up/down
599 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
600 bool const successful = cur.upDownInText(up, needsUpdate);
602 // redraw if you leave mathed (for the decorations)
603 needsUpdate |= cur.beforeDispatchCursor().inMathed();
610 case LFUN_PARAGRAPH_UP:
611 case LFUN_PARAGRAPH_UP_SELECT:
612 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
613 needsUpdate |= cursorUpParagraph(cur);
616 case LFUN_PARAGRAPH_DOWN:
617 case LFUN_PARAGRAPH_DOWN_SELECT:
618 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
619 needsUpdate |= cursorDownParagraph(cur);
622 case LFUN_LINE_BEGIN:
623 case LFUN_LINE_BEGIN_SELECT:
624 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
625 needsUpdate |= tm.cursorHome(cur);
629 case LFUN_LINE_END_SELECT:
630 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
631 needsUpdate |= tm.cursorEnd(cur);
634 case LFUN_WORD_RIGHT:
635 case LFUN_WORD_RIGHT_SELECT:
636 if (lyxrc.visual_cursor) {
637 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
638 needsUpdate |= cursorVisRightOneWord(cur);
639 if (!needsUpdate && oldTopSlice == cur.top()
640 && cur.boundary() == oldBoundary) {
642 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
645 if (reverseDirectionNeeded(cur)) {
646 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
647 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
649 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
650 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
657 case LFUN_WORD_FORWARD:
658 case LFUN_WORD_FORWARD_SELECT:
659 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
660 needsUpdate |= cursorForwardOneWord(cur);
664 case LFUN_WORD_LEFT_SELECT:
665 if (lyxrc.visual_cursor) {
666 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
667 needsUpdate |= cursorVisLeftOneWord(cur);
668 if (!needsUpdate && oldTopSlice == cur.top()
669 && cur.boundary() == oldBoundary) {
671 cmd = FuncRequest(LFUN_FINISHED_LEFT);
674 if (reverseDirectionNeeded(cur)) {
675 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
676 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
678 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
679 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
686 case LFUN_WORD_BACKWARD:
687 case LFUN_WORD_BACKWARD_SELECT:
688 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
689 needsUpdate |= cursorBackwardOneWord(cur);
692 case LFUN_WORD_SELECT: {
693 selectWord(cur, WHOLE_WORD);
694 finishChange(cur, true);
698 case LFUN_NEWLINE_INSERT: {
699 InsetNewlineParams inp;
700 docstring arg = cmd.argument();
701 // this avoids a double undo
702 // FIXME: should not be needed, ideally
703 if (!cur.selection())
705 cap::replaceSelection(cur);
706 if (arg == "linebreak")
707 inp.kind = InsetNewlineParams::LINEBREAK;
709 inp.kind = InsetNewlineParams::NEWLINE;
710 cur.insert(new InsetNewline(inp));
712 moveCursor(cur, false);
716 case LFUN_CHAR_DELETE_FORWARD:
717 if (!cur.selection()) {
718 if (cur.pos() == cur.paragraph().size())
719 // Par boundary, force full-screen update
720 singleParUpdate = false;
721 needsUpdate |= erase(cur);
723 // It is possible to make it a lot faster still
724 // just comment out the line below...
726 cutSelection(cur, true, false);
727 singleParUpdate = false;
729 moveCursor(cur, false);
732 case LFUN_DELETE_FORWARD_SKIP:
733 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
734 if (!cur.selection()) {
735 if (cur.pos() == cur.lastpos()) {
742 cutSelection(cur, true, false);
747 case LFUN_CHAR_DELETE_BACKWARD:
748 if (!cur.selection()) {
749 if (bv->getIntl().getTransManager().backspace()) {
750 // Par boundary, full-screen update
752 singleParUpdate = false;
753 needsUpdate |= backspace(cur);
755 // It is possible to make it a lot faster still
756 // just comment out the line below...
759 cutSelection(cur, true, false);
760 singleParUpdate = false;
764 case LFUN_DELETE_BACKWARD_SKIP:
765 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
766 if (!cur.selection()) {
768 //CursorSlice cur = cursor();
772 cutSelection(cur, true, false);
776 case LFUN_BREAK_PARAGRAPH:
777 cap::replaceSelection(cur);
778 breakParagraph(cur, cmd.argument() == "inverse");
782 case LFUN_BREAK_PARAGRAPH_SKIP: {
783 // When at the beginning of a paragraph, remove
784 // indentation. Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
785 cap::replaceSelection(cur);
787 cur.paragraph().params().labelWidthString(docstring());
789 breakParagraph(cur, false);
795 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
796 // as its duties can be performed there. Should it be removed??
797 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
798 case LFUN_PARAGRAPH_SPACING: {
799 Paragraph & par = cur.paragraph();
800 Spacing::Space cur_spacing = par.params().spacing().getSpace();
801 string cur_value = "1.0";
802 if (cur_spacing == Spacing::Other)
803 cur_value = par.params().spacing().getValueAsString();
805 istringstream is(to_utf8(cmd.argument()));
808 Spacing::Space new_spacing = cur_spacing;
809 string new_value = cur_value;
811 lyxerr << "Missing argument to `paragraph-spacing'"
813 } else if (tmp == "single") {
814 new_spacing = Spacing::Single;
815 } else if (tmp == "onehalf") {
816 new_spacing = Spacing::Onehalf;
817 } else if (tmp == "double") {
818 new_spacing = Spacing::Double;
819 } else if (tmp == "other") {
820 new_spacing = Spacing::Other;
821 string tmpval = "0.0";
823 lyxerr << "new_value = " << tmpval << endl;
826 } else if (tmp == "default") {
827 new_spacing = Spacing::Default;
829 lyxerr << to_utf8(_("Unknown spacing argument: "))
830 << to_utf8(cmd.argument()) << endl;
832 if (cur_spacing != new_spacing || cur_value != new_value)
833 par.params().spacing(Spacing(new_spacing, new_value));
837 case LFUN_INSET_INSERT: {
839 Inset * inset = createInset(bv->buffer(), cmd);
841 // FIXME (Abdel 01/02/2006):
842 // What follows would be a partial fix for bug 2154:
843 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
844 // This automatically put the label inset _after_ a
845 // numbered section. It should be possible to extend the mechanism
846 // to any kind of LateX environement.
847 // The correct way to fix that bug would be at LateX generation.
848 // I'll let the code here for reference as it could be used for some
849 // other feature like "automatic labelling".
851 Paragraph & par = pars_[cur.pit()];
852 if (inset->lyxCode() == LABEL_CODE
853 && par.layout().labeltype == LABEL_COUNTER) {
854 // Go to the end of the paragraph
855 // Warning: Because of Change-Tracking, the last
856 // position is 'size()' and not 'size()-1':
857 cur.pos() = par.size();
858 // Insert a new paragraph
859 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
864 cutSelection(cur, true, false);
871 case LFUN_INSET_DISSOLVE:
872 needsUpdate |= dissolveInset(cur);
875 case LFUN_INSET_SETTINGS: {
876 // if there is an inset at cursor, access this
877 Inset * inset = cur.nextInset();
879 inset->showInsetDialog(bv);
882 // if not work, access the underlying inset.
883 cur.inset().showInsetDialog(bv);
887 case LFUN_SPACE_INSERT:
888 if (cur.paragraph().layout().free_spacing)
889 insertChar(cur, ' ');
891 doInsertInset(cur, this, cmd, false, false);
894 moveCursor(cur, false);
897 case LFUN_SPECIALCHAR_INSERT: {
898 string const name = to_utf8(cmd.argument());
899 if (name == "hyphenation")
900 specialChar(cur, InsetSpecialChar::HYPHENATION);
901 else if (name == "ligature-break")
902 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
903 else if (name == "slash")
904 specialChar(cur, InsetSpecialChar::SLASH);
905 else if (name == "nobreakdash")
906 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
907 else if (name == "dots")
908 specialChar(cur, InsetSpecialChar::LDOTS);
909 else if (name == "end-of-sentence")
910 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
911 else if (name == "menu-separator")
912 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
913 else if (name.empty())
914 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
916 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
920 case LFUN_WORD_UPCASE:
921 changeCase(cur, text_uppercase);
924 case LFUN_WORD_LOWCASE:
925 changeCase(cur, text_lowercase);
928 case LFUN_WORD_CAPITALIZE:
929 changeCase(cur, text_capitalization);
932 case LFUN_CHARS_TRANSPOSE:
937 cur.message(_("Paste"));
938 cap::replaceSelection(cur);
941 string const arg = to_utf8(cmd.argument());
943 if (theClipboard().isInternal())
944 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
945 else if (theClipboard().hasGraphicsContents())
946 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
948 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
949 } else if (isStrUnsignedInt(arg)) {
950 // we have a numerical argument
951 pasteFromStack(cur, bv->buffer().errorList("Paste"),
952 convert<unsigned int>(arg));
954 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
956 type = Clipboard::PdfGraphicsType;
957 else if (arg == "png")
958 type = Clipboard::PngGraphicsType;
959 else if (arg == "jpeg")
960 type = Clipboard::JpegGraphicsType;
961 else if (arg == "linkback")
962 type = Clipboard::LinkBackGraphicsType;
964 LASSERT(false, /**/);
966 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
969 bv->buffer().errors("Paste");
970 cur.clearSelection(); // bug 393
976 cutSelection(cur, true, true);
977 cur.message(_("Cut"));
982 cur.message(_("Copy"));
985 case LFUN_SERVER_GET_XY:
986 cur.message(from_utf8(
987 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
988 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
991 case LFUN_SERVER_SET_XY: {
994 istringstream is(to_utf8(cmd.argument()));
997 lyxerr << "SETXY: Could not parse coordinates in '"
998 << to_utf8(cmd.argument()) << endl;
1000 tm.setCursorFromCoordinates(cur, x, y);
1004 case LFUN_SERVER_GET_LAYOUT:
1005 cur.message(cur.paragraph().layout().name());
1009 docstring layout = cmd.argument();
1010 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1012 Paragraph const & para = cur.paragraph();
1013 docstring const old_layout = para.layout().name();
1014 DocumentClass const & tclass = bv->buffer().params().documentClass();
1017 layout = tclass.defaultLayoutName();
1019 if (para.forceEmptyLayout())
1020 // in this case only the empty layout is allowed
1021 layout = tclass.emptyLayoutName();
1022 else if (para.useEmptyLayout()) {
1023 // in this case, default layout maps to empty layout
1024 if (layout == tclass.defaultLayoutName())
1025 layout = tclass.emptyLayoutName();
1027 // otherwise, the empty layout maps to the default
1028 if (layout == tclass.emptyLayoutName())
1029 layout = tclass.defaultLayoutName();
1032 bool hasLayout = tclass.hasLayout(layout);
1034 // If the entry is obsolete, use the new one instead.
1036 docstring const & obs = tclass[layout].obsoleted_by();
1042 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1043 from_utf8(N_(" not known")));
1047 bool change_layout = (old_layout != layout);
1049 if (!change_layout && cur.selection() &&
1050 cur.selBegin().pit() != cur.selEnd().pit())
1052 pit_type spit = cur.selBegin().pit();
1053 pit_type epit = cur.selEnd().pit() + 1;
1054 while (spit != epit) {
1055 if (pars_[spit].layout().name() != old_layout) {
1056 change_layout = true;
1064 setLayout(cur, layout);
1069 case LFUN_CLIPBOARD_PASTE:
1070 cur.clearSelection();
1071 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1072 cmd.argument() == "paragraph");
1073 bv->buffer().errors("Paste");
1076 case LFUN_PRIMARY_SELECTION_PASTE:
1077 pasteString(cur, theSelection().get(),
1078 cmd.argument() == "paragraph");
1081 case LFUN_UNICODE_INSERT: {
1082 if (cmd.argument().empty())
1084 docstring hexstring = cmd.argument();
1085 if (isHex(hexstring)) {
1086 char_type c = hexToInt(hexstring);
1087 if (c >= 32 && c < 0x10ffff) {
1088 lyxerr << "Inserting c: " << c << endl;
1089 docstring s = docstring(1, c);
1090 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1096 case LFUN_QUOTE_INSERT: {
1097 Paragraph & par = cur.paragraph();
1098 pos_type pos = cur.pos();
1099 BufferParams const & bufparams = bv->buffer().params();
1100 Layout const & style = par.layout();
1101 if (!style.pass_thru
1102 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1103 // this avoids a double undo
1104 // FIXME: should not be needed, ideally
1105 if (!cur.selection())
1107 cap::replaceSelection(cur);
1112 else if (cur.prevInset() && cur.prevInset()->isSpace())
1115 c = par.getChar(pos - 1);
1116 string arg = to_utf8(cmd.argument());
1117 cur.insert(new InsetQuotes(c, bufparams.quotes_language,
1118 (arg == "single") ? InsetQuotes::SingleQuotes
1119 : InsetQuotes::DoubleQuotes));
1123 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1127 case LFUN_DATE_INSERT: {
1128 string const format = cmd.argument().empty()
1129 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1130 string const time = formatted_time(current_time(), format);
1131 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1135 case LFUN_MOUSE_TRIPLE:
1136 if (cmd.button() == mouse_button::button1) {
1145 case LFUN_MOUSE_DOUBLE:
1146 if (cmd.button() == mouse_button::button1) {
1147 selectWord(cur, WHOLE_WORD_STRICT);
1152 // Single-click on work area
1153 case LFUN_MOUSE_PRESS:
1154 // We are not marking a selection with the keyboard in any case.
1155 cur.bv().cursor().mark() = false;
1156 switch (cmd.button()) {
1157 case mouse_button::button1:
1159 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1160 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1163 case mouse_button::button2:
1164 // Middle mouse pasting.
1165 if (!cap::selection()) {
1166 // There is no local selection in the current buffer, so try to
1167 // paste primary selection instead.
1168 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
1170 // Nothing else to do.
1174 // Copy the selection buffer to the clipboard stack, because we want it
1175 // to appear in the "Edit->Paste recent" menu.
1176 cap::copySelectionToStack();
1177 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1178 cur.updateFlags(Update::Force | Update::FitCursor);
1179 bv->buffer().errors("Paste");
1180 bv->buffer().markDirty();
1181 bv->cursor().finishUndo();
1184 case mouse_button::button3:
1185 if (cur.selection()) {
1186 DocIterator const selbeg = cur.selectionBegin();
1187 DocIterator const selend = cur.selectionEnd();
1188 Cursor tmpcur = cur;
1189 tm.setCursorFromCoordinates(tmpcur, cmd.x, cmd.y);
1190 // Don't do anything if we right-click a selection, a selection
1191 // context menu should popup instead.
1192 if (tmpcur < selbeg || tmpcur >= selend) {
1197 if (!bv->mouseSetCursor(cur, false)) {
1198 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1203 } // switch (cmd.button())
1206 case LFUN_MOUSE_MOTION: {
1207 // Mouse motion with right or middle mouse do nothing for now.
1208 if (cmd.button() != mouse_button::button1) {
1212 // ignore motions deeper nested than the real anchor
1213 Cursor & bvcur = cur.bv().cursor();
1214 if (!bvcur.anchor_.hasPart(cur)) {
1218 CursorSlice old = bvcur.top();
1220 int const wh = bv->workHeight();
1221 int const y = max(0, min(wh - 1, cmd.y));
1223 tm.setCursorFromCoordinates(cur, cmd.x, y);
1224 cur.setTargetX(cmd.x);
1226 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1228 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1229 // This is to allow jumping over large insets
1230 if (cur.top() == old) {
1232 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1234 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1236 // We continue with our existing selection or start a new one, so don't
1237 // reset the anchor.
1238 bvcur.setCursor(cur);
1239 bvcur.selection() = true;
1240 if (cur.top() == old) {
1241 // We didn't move one iota, so no need to update the screen.
1242 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1249 case LFUN_MOUSE_RELEASE:
1250 switch (cmd.button()) {
1251 case mouse_button::button1:
1252 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1253 // If there is a new selection, update persistent selection;
1254 // otherwise, single click does not clear persistent selection
1256 if (cur.selection()) {
1257 // Finish selection.
1258 // If double click, cur is moved to the end of word by selectWord
1259 // but bvcur is current mouse position.
1260 cur.bv().cursor().selection() = true;
1262 // FIXME: We could try to handle drag and drop of selection here.
1266 case mouse_button::button2:
1267 // Middle mouse pasting is handled at mouse press time,
1268 // see LFUN_MOUSE_PRESS.
1272 case mouse_button::button3:
1273 // Cursor was set at LFUN_MOUSE_PRESS time.
1274 // FIXME: If there is a selection we could try to handle a special
1275 // drag & drop context menu.
1279 } // switch (cmd.button())
1283 case LFUN_SELF_INSERT: {
1284 if (cmd.argument().empty())
1287 // Automatically delete the currently selected
1288 // text and replace it with what is being
1289 // typed in now. Depends on lyxrc settings
1290 // "auto_region_delete", which defaults to
1293 if (lyxrc.auto_region_delete && cur.selection())
1294 cutSelection(cur, false, false);
1296 cur.clearSelection();
1297 Font const old_font = cur.real_current_font;
1299 docstring::const_iterator cit = cmd.argument().begin();
1300 docstring::const_iterator end = cmd.argument().end();
1301 for (; cit != end; ++cit)
1302 bv->translateAndInsert(*cit, this, cur);
1305 moveCursor(cur, false);
1309 case LFUN_HYPERLINK_INSERT: {
1310 InsetCommandParams p(HYPERLINK_CODE);
1312 if (cur.selection()) {
1313 content = cur.selectionAsString(false);
1314 cutSelection(cur, true, false);
1316 p["target"] = (cmd.argument().empty()) ?
1317 content : cmd.argument();
1318 string const data = InsetCommand::params2string("href", p);
1319 if (p["target"].empty()) {
1320 bv->showDialog("href", data);
1322 FuncRequest fr(LFUN_INSET_INSERT, data);
1328 case LFUN_LABEL_INSERT: {
1329 InsetCommandParams p(LABEL_CODE);
1330 // Try to generate a valid label
1331 p["name"] = (cmd.argument().empty()) ?
1332 cur.getPossibleLabel() :
1334 string const data = InsetCommand::params2string("label", p);
1336 if (cmd.argument().empty()) {
1337 bv->showDialog("label", data);
1339 FuncRequest fr(LFUN_INSET_INSERT, data);
1345 case LFUN_INFO_INSERT: {
1346 Inset * inset = createInset(cur.bv().buffer(), cmd);
1349 // if an empty inset is created (cmd.argument() is empty)
1350 // use current selection as parameter.
1351 if (cmd.argument().empty() && cur.selection()) {
1352 // use selected text as info to avoid a separate UI
1353 docstring ds = cur.selectionAsString(false);
1354 cutSelection(cur, true, false);
1355 static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1356 static_cast<InsetInfo *>(inset)->updateInfo();
1358 insertInset(cur, inset);
1363 case LFUN_LIST_INSERT:
1365 case LFUN_CAPTION_INSERT:
1366 case LFUN_FOOTNOTE_INSERT:
1367 case LFUN_NOTE_INSERT:
1368 case LFUN_FLEX_INSERT:
1369 case LFUN_BOX_INSERT:
1370 case LFUN_BRANCH_INSERT:
1371 case LFUN_BIBITEM_INSERT:
1372 case LFUN_ERT_INSERT:
1373 case LFUN_LISTING_INSERT:
1374 case LFUN_MARGINALNOTE_INSERT:
1375 case LFUN_OPTIONAL_INSERT:
1376 case LFUN_ENVIRONMENT_INSERT:
1377 case LFUN_INDEX_INSERT:
1378 // Open the inset, and move the current selection
1380 doInsertInset(cur, this, cmd, true, true);
1382 // Some insets are numbered, others are shown in the outline pane so
1383 // let's update the labels and the toc backend.
1384 updateLabels(bv->buffer());
1387 case LFUN_TABULAR_INSERT:
1388 // if there were no arguments, just open the dialog
1389 if (doInsertInset(cur, this, cmd, false, true))
1392 bv->showDialog("tabularcreate");
1396 case LFUN_FLOAT_INSERT:
1397 case LFUN_FLOAT_WIDE_INSERT:
1398 case LFUN_WRAP_INSERT: {
1399 bool content = cur.selection(); // will some text be moved into the inset?
1401 doInsertInset(cur, this, cmd, true, true);
1403 ParagraphList & pars = cur.text()->paragraphs();
1405 DocumentClass const & tclass = bv->buffer().params().documentClass();
1407 // add a separate paragraph for the caption inset
1408 pars.push_back(Paragraph());
1409 pars.back().setInsetOwner(pars[0].inInset());
1410 pars.back().setEmptyOrDefaultLayout(tclass);
1411 int cap_pit = pars.size() - 1;
1413 // if an empty inset was created, we create an additional empty
1414 // paragraph at the bottom so that the user can choose where to put
1415 // the graphics (or table).
1417 pars.push_back(Paragraph());
1418 pars.back().setInsetOwner(pars[0].inInset());
1419 pars.back().setEmptyOrDefaultLayout(tclass);
1422 // reposition the cursor to the caption
1423 cur.pit() = cap_pit;
1425 // FIXME: This Text/Cursor dispatch handling is a mess!
1426 // We cannot use Cursor::dispatch here it needs access to up to
1428 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1429 cur.text()->dispatch(cur, cmd_caption);
1430 cur.updateFlags(Update::Force);
1431 // FIXME: When leaving the Float (or Wrap) inset we should
1432 // delete any empty paragraph left above or below the
1437 case LFUN_NOMENCL_INSERT: {
1438 FuncRequest cmd1 = cmd;
1439 if (cmd.argument().empty())
1440 cmd1 = FuncRequest(cmd,
1441 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1442 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1446 cur.clearSelection();
1447 insertInset(cur, inset);
1448 // Show the dialog for the nomenclature entry, since the
1449 // description entry still needs to be filled in.
1450 if (cmd.action == LFUN_NOMENCL_INSERT)
1451 inset->edit(cur, true);
1456 case LFUN_INDEX_PRINT:
1457 case LFUN_NOMENCL_PRINT:
1458 case LFUN_TOC_INSERT:
1459 case LFUN_LINE_INSERT:
1460 case LFUN_NEWPAGE_INSERT:
1462 doInsertInset(cur, this, cmd, false, false);
1466 case LFUN_DEPTH_DECREMENT:
1467 changeDepth(cur, DEC_DEPTH);
1470 case LFUN_DEPTH_INCREMENT:
1471 changeDepth(cur, INC_DEPTH);
1474 case LFUN_MATH_DISPLAY:
1475 mathDispatch(cur, cmd, true);
1478 case LFUN_MATH_IMPORT_SELECTION:
1479 case LFUN_MATH_MODE:
1480 if (cmd.argument() == "on")
1481 // don't pass "on" as argument
1482 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1484 mathDispatch(cur, cmd, false);
1487 case LFUN_MATH_MACRO:
1488 if (cmd.argument().empty())
1489 cur.errorMessage(from_utf8(N_("Missing argument")));
1491 string s = to_utf8(cmd.argument());
1492 string const s1 = token(s, ' ', 1);
1493 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1494 string const s2 = token(s, ' ', 2);
1495 MacroType type = MacroTypeNewcommand;
1497 type = MacroTypeDef;
1498 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1499 //cur.nextInset()->edit(cur, true);
1503 // passthrough hat and underscore outside mathed:
1504 case LFUN_MATH_SUBSCRIPT:
1505 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1507 case LFUN_MATH_SUPERSCRIPT:
1508 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1511 case LFUN_MATH_INSERT:
1512 case LFUN_MATH_MATRIX:
1513 case LFUN_MATH_DELIM:
1514 case LFUN_MATH_BIGDELIM: {
1515 cap::replaceSelection(cur);
1516 cur.insert(new InsetMathHull(hullSimple));
1517 checkAndActivateInset(cur, true);
1518 LASSERT(cur.inMathed(), /**/);
1523 case LFUN_FONT_EMPH: {
1524 Font font(ignore_font, ignore_language);
1525 font.fontInfo().setEmph(FONT_TOGGLE);
1526 toggleAndShow(cur, this, font);
1530 case LFUN_FONT_BOLD: {
1531 Font font(ignore_font, ignore_language);
1532 font.fontInfo().setSeries(BOLD_SERIES);
1533 toggleAndShow(cur, this, font);
1537 case LFUN_FONT_NOUN: {
1538 Font font(ignore_font, ignore_language);
1539 font.fontInfo().setNoun(FONT_TOGGLE);
1540 toggleAndShow(cur, this, font);
1544 case LFUN_FONT_TYPEWRITER: {
1545 Font font(ignore_font, ignore_language);
1546 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1547 toggleAndShow(cur, this, font);
1551 case LFUN_FONT_SANS: {
1552 Font font(ignore_font, ignore_language);
1553 font.fontInfo().setFamily(SANS_FAMILY);
1554 toggleAndShow(cur, this, font);
1558 case LFUN_FONT_ROMAN: {
1559 Font font(ignore_font, ignore_language);
1560 font.fontInfo().setFamily(ROMAN_FAMILY);
1561 toggleAndShow(cur, this, font);
1565 case LFUN_FONT_DEFAULT: {
1566 Font font(inherit_font, ignore_language);
1567 toggleAndShow(cur, this, font);
1571 case LFUN_FONT_UNDERLINE: {
1572 Font font(ignore_font, ignore_language);
1573 font.fontInfo().setUnderbar(FONT_TOGGLE);
1574 toggleAndShow(cur, this, font);
1578 case LFUN_FONT_SIZE: {
1579 Font font(ignore_font, ignore_language);
1580 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1581 toggleAndShow(cur, this, font);
1585 case LFUN_LANGUAGE: {
1586 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1589 Font font(ignore_font, lang);
1590 toggleAndShow(cur, this, font);
1594 case LFUN_FONT_FREE_APPLY:
1595 toggleAndShow(cur, this, freefont, toggleall);
1596 cur.message(_("Character set"));
1599 // Set the freefont using the contents of \param data dispatched from
1600 // the frontends and apply it at the current cursor location.
1601 case LFUN_FONT_FREE_UPDATE: {
1604 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1607 toggleAndShow(cur, this, freefont, toggleall);
1608 cur.message(_("Character set"));
1610 lyxerr << "Argument not ok";
1615 case LFUN_FINISHED_LEFT:
1616 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1617 // We're leaving an inset, going left. If the inset is LTR, we're
1618 // leaving from the front, so we should not move (remain at --- but
1619 // not in --- the inset). If the inset is RTL, move left, without
1620 // entering the inset itself; i.e., move to after the inset.
1621 if (cur.paragraph().getFontSettings(
1622 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1623 cursorVisLeft(cur, true);
1626 case LFUN_FINISHED_RIGHT:
1627 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1628 // We're leaving an inset, going right. If the inset is RTL, we're
1629 // leaving from the front, so we should not move (remain at --- but
1630 // not in --- the inset). If the inset is LTR, move right, without
1631 // entering the inset itself; i.e., move to after the inset.
1632 if (!cur.paragraph().getFontSettings(
1633 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1634 cursorVisRight(cur, true);
1637 case LFUN_FINISHED_BACKWARD:
1638 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1641 case LFUN_FINISHED_FORWARD:
1642 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1644 cur.setCurrentFont();
1647 case LFUN_LAYOUT_PARAGRAPH: {
1649 params2string(cur.paragraph(), data);
1650 data = "show\n" + data;
1651 bv->showDialog("paragraph", data);
1655 case LFUN_PARAGRAPH_UPDATE: {
1657 params2string(cur.paragraph(), data);
1659 // Will the paragraph accept changes from the dialog?
1661 cur.inset().allowParagraphCustomization(cur.idx());
1663 data = "update " + convert<string>(accept) + '\n' + data;
1664 bv->updateDialog("paragraph", data);
1668 case LFUN_ACCENT_UMLAUT:
1669 case LFUN_ACCENT_CIRCUMFLEX:
1670 case LFUN_ACCENT_GRAVE:
1671 case LFUN_ACCENT_ACUTE:
1672 case LFUN_ACCENT_TILDE:
1673 case LFUN_ACCENT_CEDILLA:
1674 case LFUN_ACCENT_MACRON:
1675 case LFUN_ACCENT_DOT:
1676 case LFUN_ACCENT_UNDERDOT:
1677 case LFUN_ACCENT_UNDERBAR:
1678 case LFUN_ACCENT_CARON:
1679 case LFUN_ACCENT_SPECIAL_CARON:
1680 case LFUN_ACCENT_BREVE:
1681 case LFUN_ACCENT_TIE:
1682 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1683 case LFUN_ACCENT_CIRCLE:
1684 case LFUN_ACCENT_OGONEK:
1685 theLyXFunc().handleKeyFunc(cmd.action);
1686 if (!cmd.argument().empty())
1687 // FIXME: Are all these characters encoded in one byte in utf8?
1688 bv->translateAndInsert(cmd.argument()[0], this, cur);
1691 case LFUN_FLOAT_LIST: {
1692 DocumentClass const & tclass = bv->buffer().params().documentClass();
1693 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1695 if (cur.selection())
1696 cutSelection(cur, true, false);
1697 breakParagraph(cur);
1699 if (cur.lastpos() != 0) {
1700 cursorBackward(cur);
1701 breakParagraph(cur);
1704 docstring const laystr = cur.inset().useEmptyLayout() ?
1705 tclass.emptyLayoutName() :
1706 tclass.defaultLayoutName();
1707 setLayout(cur, laystr);
1708 ParagraphParameters p;
1709 setParagraphs(cur, p);
1710 // FIXME This should be simplified when InsetFloatList takes a
1711 // Buffer in its constructor.
1712 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1713 ifl->setBuffer(bv->buffer());
1714 insertInset(cur, ifl);
1717 lyxerr << "Non-existent float type: "
1718 << to_utf8(cmd.argument()) << endl;
1723 case LFUN_CHANGE_ACCEPT: {
1724 acceptOrRejectChanges(cur, ACCEPT);
1728 case LFUN_CHANGE_REJECT: {
1729 acceptOrRejectChanges(cur, REJECT);
1733 case LFUN_THESAURUS_ENTRY: {
1734 docstring arg = cmd.argument();
1736 arg = cur.selectionAsString(false);
1738 if (arg.size() > 100 || arg.empty()) {
1739 // Get word or selection
1740 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1741 arg = cur.selectionAsString(false);
1744 bv->showDialog("thesaurus", to_utf8(arg));
1748 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1749 // Given data, an encoding of the ParagraphParameters
1750 // generated in the Paragraph dialog, this function sets
1751 // the current paragraph, or currently selected paragraphs,
1753 // NOTE: This function overrides all existing settings.
1754 setParagraphs(cur, cmd.argument());
1755 cur.message(_("Paragraph layout set"));
1759 case LFUN_PARAGRAPH_PARAMS: {
1760 // Given data, an encoding of the ParagraphParameters as we'd
1761 // find them in a LyX file, this function modifies the current paragraph,
1762 // or currently selected paragraphs.
1763 // NOTE: This function only modifies, and does not override, existing
1765 setParagraphs(cur, cmd.argument(), true);
1766 cur.message(_("Paragraph layout set"));
1771 if (cur.selection()) {
1772 cur.selection() = false;
1775 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1776 // correct, but I'm not 100% sure -- dov, 071019
1777 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1781 case LFUN_OUTLINE_UP:
1782 outline(OutlineUp, cur);
1783 setCursor(cur, cur.pit(), 0);
1784 updateLabels(cur.buffer());
1788 case LFUN_OUTLINE_DOWN:
1789 outline(OutlineDown, cur);
1790 setCursor(cur, cur.pit(), 0);
1791 updateLabels(cur.buffer());
1795 case LFUN_OUTLINE_IN:
1796 outline(OutlineIn, cur);
1797 updateLabels(cur.buffer());
1801 case LFUN_OUTLINE_OUT:
1802 outline(OutlineOut, cur);
1803 updateLabels(cur.buffer());
1808 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1813 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1815 // FIXME: The cursor flag is reset two lines below
1816 // so we need to check here if some of the LFUN did touch that.
1817 // for now only Text::erase() and Text::backspace() do that.
1818 // The plan is to verify all the LFUNs and then to remove this
1819 // singleParUpdate boolean altogether.
1820 if (cur.result().update() & Update::Force) {
1821 singleParUpdate = false;
1825 // FIXME: the following code should go in favor of fine grained
1826 // update flag treatment.
1827 if (singleParUpdate) {
1828 // Inserting characters does not change par height in general. So, try
1829 // to update _only_ this paragraph. BufferView will detect if a full
1830 // metrics update is needed anyway.
1831 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1836 && &oldTopSlice.inset() == &cur.inset()
1837 && oldTopSlice.idx() == cur.idx()
1838 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1839 && !cur.selection())
1840 // FIXME: it would be better if we could just do this
1842 //if (cur.result().update() != Update::FitCursor)
1845 // But some LFUNs do not set Update::FitCursor when needed, so we
1846 // do it for all. This is not very harmfull as FitCursor will provoke
1847 // a full redraw only if needed but still, a proper review of all LFUN
1848 // should be done and this needsUpdate boolean can then be removed.
1849 cur.updateFlags(Update::FitCursor);
1851 cur.updateFlags(Update::Force | Update::FitCursor);
1855 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1856 FuncStatus & flag) const
1858 LASSERT(cur.text() == this, /**/);
1860 Font const & font = cur.real_current_font;
1861 FontInfo const & fontinfo = font.fontInfo();
1863 InsetCode code = NO_CODE;
1865 switch (cmd.action) {
1867 case LFUN_DEPTH_DECREMENT:
1868 enable = changeDepthAllowed(cur, DEC_DEPTH);
1871 case LFUN_DEPTH_INCREMENT:
1872 enable = changeDepthAllowed(cur, INC_DEPTH);
1876 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1879 case LFUN_BIBITEM_INSERT:
1880 enable = (cur.paragraph().layout().labeltype == LABEL_BIBLIO
1884 case LFUN_DIALOG_SHOW_NEW_INSET:
1885 if (cmd.argument() == "bibitem")
1886 code = BIBITEM_CODE;
1887 else if (cmd.argument() == "bibtex")
1889 else if (cmd.argument() == "box")
1891 else if (cmd.argument() == "branch")
1893 else if (cmd.argument() == "citation")
1895 else if (cmd.argument() == "ert")
1897 else if (cmd.argument() == "external")
1898 code = EXTERNAL_CODE;
1899 else if (cmd.argument() == "float")
1901 else if (cmd.argument() == "graphics")
1902 code = GRAPHICS_CODE;
1903 else if (cmd.argument() == "href")
1904 code = HYPERLINK_CODE;
1905 else if (cmd.argument() == "include")
1906 code = INCLUDE_CODE;
1907 else if (cmd.argument() == "index")
1909 else if (cmd.argument() == "nomenclature")
1910 code = NOMENCL_CODE;
1911 else if (cmd.argument() == "label")
1913 else if (cmd.argument() == "note")
1915 else if (cmd.argument() == "ref")
1917 else if (cmd.argument() == "space")
1919 else if (cmd.argument() == "toc")
1921 else if (cmd.argument() == "vspace")
1923 else if (cmd.argument() == "wrap")
1925 else if (cmd.argument() == "listings")
1926 code = LISTINGS_CODE;
1929 case LFUN_ERT_INSERT:
1932 case LFUN_LISTING_INSERT:
1933 code = LISTINGS_CODE;
1935 case LFUN_FOOTNOTE_INSERT:
1938 case LFUN_TABULAR_INSERT:
1939 code = TABULAR_CODE;
1941 case LFUN_MARGINALNOTE_INSERT:
1944 case LFUN_FLOAT_INSERT:
1945 case LFUN_FLOAT_WIDE_INSERT:
1948 case LFUN_WRAP_INSERT:
1951 case LFUN_FLOAT_LIST:
1952 code = FLOAT_LIST_CODE;
1955 case LFUN_LIST_INSERT:
1959 case LFUN_CAPTION_INSERT:
1960 code = CAPTION_CODE;
1962 case LFUN_NOTE_INSERT:
1964 // in commands (sections etc., only Notes are allowed)
1965 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
1966 !cur.paragraph().layout().isCommand());
1968 case LFUN_FLEX_INSERT: {
1970 string s = cmd.getArg(0);
1972 cur.buffer().params().documentClass().insetLayout(from_utf8(s));
1973 if (il.lyxtype() != "charstyle" &&
1974 il.lyxtype() != "custom" &&
1975 il.lyxtype() != "element" &&
1976 il.lyxtype ()!= "standard")
1980 case LFUN_BOX_INSERT:
1983 case LFUN_BRANCH_INSERT:
1985 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1988 case LFUN_LABEL_INSERT:
1991 case LFUN_INFO_INSERT:
1994 case LFUN_OPTIONAL_INSERT:
1996 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1997 < cur.paragraph().layout().optionalargs;
1999 case LFUN_ENVIRONMENT_INSERT:
2002 case LFUN_INDEX_INSERT:
2005 case LFUN_INDEX_PRINT:
2006 code = INDEX_PRINT_CODE;
2008 case LFUN_NOMENCL_INSERT:
2009 code = NOMENCL_CODE;
2011 case LFUN_NOMENCL_PRINT:
2012 code = NOMENCL_PRINT_CODE;
2014 case LFUN_TOC_INSERT:
2017 case LFUN_HYPERLINK_INSERT:
2018 code = HYPERLINK_CODE;
2020 case LFUN_QUOTE_INSERT:
2021 // always allow this, since we will inset a raw quote
2022 // if an inset is not allowed.
2024 case LFUN_SPECIALCHAR_INSERT:
2025 code = SPECIALCHAR_CODE;
2027 case LFUN_SPACE_INSERT:
2028 // slight hack: we know this is allowed in math mode
2033 case LFUN_INSET_MODIFY:
2034 // We need to disable this, because we may get called for a
2036 // InsetTabular::getStatus() -> InsetText::getStatus()
2037 // and we don't handle LFUN_INSET_MODIFY.
2041 case LFUN_FONT_EMPH:
2042 flag.setOnOff(fontinfo.emph() == FONT_ON);
2045 case LFUN_FONT_NOUN:
2046 flag.setOnOff(fontinfo.noun() == FONT_ON);
2049 case LFUN_FONT_BOLD:
2050 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2053 case LFUN_FONT_SANS:
2054 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2057 case LFUN_FONT_ROMAN:
2058 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2061 case LFUN_FONT_TYPEWRITER:
2062 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2067 enable = cur.selection();
2071 if (cmd.argument().empty()) {
2072 if (theClipboard().isInternal())
2073 enable = cap::numberOfSelections() > 0;
2075 enable = !theClipboard().empty();
2079 // we have an argument
2080 string const arg = to_utf8(cmd.argument());
2081 if (isStrUnsignedInt(arg)) {
2082 // it's a number and therefore means the internal stack
2083 unsigned int n = convert<unsigned int>(arg);
2084 enable = cap::numberOfSelections() > n;
2088 // explicit graphics type?
2089 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2090 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2091 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2092 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2102 case LFUN_CLIPBOARD_PASTE:
2103 enable = !theClipboard().empty();
2106 case LFUN_PRIMARY_SELECTION_PASTE:
2107 enable = cur.selection() || !theSelection().empty();
2110 case LFUN_PARAGRAPH_MOVE_UP:
2111 enable = cur.pit() > 0 && !cur.selection();
2114 case LFUN_PARAGRAPH_MOVE_DOWN:
2115 enable = cur.pit() < cur.lastpit() && !cur.selection();
2118 case LFUN_INSET_DISSOLVE:
2119 if (!cmd.argument().empty()) {
2120 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2121 enable = cur.inset().lyxCode() == FLEX_CODE
2122 && il.lyxtype() == to_utf8(cmd.argument());
2124 enable = !isMainText(cur.bv().buffer())
2125 && cur.inset().nargs() == 1;
2129 case LFUN_CHANGE_ACCEPT:
2130 case LFUN_CHANGE_REJECT:
2131 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2132 // In principle, these LFUNs should only be enabled if there
2133 // is a change at the current position/in the current selection.
2134 // However, without proper optimizations, this will inevitably
2135 // result in unacceptable performance - just imagine a user who
2136 // wants to select the complete content of a long document.
2140 case LFUN_OUTLINE_UP:
2141 case LFUN_OUTLINE_DOWN:
2142 case LFUN_OUTLINE_IN:
2143 case LFUN_OUTLINE_OUT:
2144 enable = (cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC);
2147 case LFUN_NEWLINE_INSERT:
2148 // LaTeX restrictions (labels or empty par)
2149 enable = (cur.pos() > cur.paragraph().beginOfBody());
2152 case LFUN_WORD_DELETE_FORWARD:
2153 case LFUN_WORD_DELETE_BACKWARD:
2154 case LFUN_LINE_DELETE:
2155 case LFUN_WORD_FORWARD:
2156 case LFUN_WORD_BACKWARD:
2157 case LFUN_WORD_RIGHT:
2158 case LFUN_WORD_LEFT:
2159 case LFUN_CHAR_FORWARD:
2160 case LFUN_CHAR_FORWARD_SELECT:
2161 case LFUN_CHAR_BACKWARD:
2162 case LFUN_CHAR_BACKWARD_SELECT:
2163 case LFUN_CHAR_LEFT:
2164 case LFUN_CHAR_LEFT_SELECT:
2165 case LFUN_CHAR_RIGHT:
2166 case LFUN_CHAR_RIGHT_SELECT:
2168 case LFUN_UP_SELECT:
2170 case LFUN_DOWN_SELECT:
2171 case LFUN_PARAGRAPH_UP_SELECT:
2172 case LFUN_PARAGRAPH_DOWN_SELECT:
2173 case LFUN_LINE_BEGIN_SELECT:
2174 case LFUN_LINE_END_SELECT:
2175 case LFUN_WORD_FORWARD_SELECT:
2176 case LFUN_WORD_BACKWARD_SELECT:
2177 case LFUN_WORD_RIGHT_SELECT:
2178 case LFUN_WORD_LEFT_SELECT:
2179 case LFUN_WORD_SELECT:
2180 case LFUN_PARAGRAPH_UP:
2181 case LFUN_PARAGRAPH_DOWN:
2182 case LFUN_LINE_BEGIN:
2184 case LFUN_CHAR_DELETE_FORWARD:
2185 case LFUN_DELETE_FORWARD_SKIP:
2186 case LFUN_CHAR_DELETE_BACKWARD:
2187 case LFUN_DELETE_BACKWARD_SKIP:
2188 case LFUN_BREAK_PARAGRAPH:
2189 case LFUN_BREAK_PARAGRAPH_SKIP:
2190 case LFUN_PARAGRAPH_SPACING:
2191 case LFUN_INSET_INSERT:
2192 case LFUN_WORD_UPCASE:
2193 case LFUN_WORD_LOWCASE:
2194 case LFUN_WORD_CAPITALIZE:
2195 case LFUN_CHARS_TRANSPOSE:
2196 case LFUN_SERVER_GET_XY:
2197 case LFUN_SERVER_SET_XY:
2198 case LFUN_SERVER_GET_LAYOUT:
2200 case LFUN_DATE_INSERT:
2201 case LFUN_SELF_INSERT:
2202 case LFUN_LINE_INSERT:
2203 case LFUN_NEWPAGE_INSERT:
2204 case LFUN_MATH_DISPLAY:
2205 case LFUN_MATH_IMPORT_SELECTION:
2206 case LFUN_MATH_MODE:
2207 case LFUN_MATH_MACRO:
2208 case LFUN_MATH_MATRIX:
2209 case LFUN_MATH_DELIM:
2210 case LFUN_MATH_BIGDELIM:
2211 case LFUN_MATH_INSERT:
2212 case LFUN_MATH_SUBSCRIPT:
2213 case LFUN_MATH_SUPERSCRIPT:
2214 case LFUN_FONT_DEFAULT:
2215 case LFUN_FONT_UNDERLINE:
2216 case LFUN_FONT_SIZE:
2218 case LFUN_FONT_FREE_APPLY:
2219 case LFUN_FONT_FREE_UPDATE:
2220 case LFUN_LAYOUT_PARAGRAPH:
2221 case LFUN_PARAGRAPH_UPDATE:
2222 case LFUN_ACCENT_UMLAUT:
2223 case LFUN_ACCENT_CIRCUMFLEX:
2224 case LFUN_ACCENT_GRAVE:
2225 case LFUN_ACCENT_ACUTE:
2226 case LFUN_ACCENT_TILDE:
2227 case LFUN_ACCENT_CEDILLA:
2228 case LFUN_ACCENT_MACRON:
2229 case LFUN_ACCENT_DOT:
2230 case LFUN_ACCENT_UNDERDOT:
2231 case LFUN_ACCENT_UNDERBAR:
2232 case LFUN_ACCENT_CARON:
2233 case LFUN_ACCENT_SPECIAL_CARON:
2234 case LFUN_ACCENT_BREVE:
2235 case LFUN_ACCENT_TIE:
2236 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2237 case LFUN_ACCENT_CIRCLE:
2238 case LFUN_ACCENT_OGONEK:
2239 case LFUN_THESAURUS_ENTRY:
2240 case LFUN_PARAGRAPH_PARAMS_APPLY:
2241 case LFUN_PARAGRAPH_PARAMS:
2243 case LFUN_BUFFER_END:
2244 case LFUN_BUFFER_BEGIN:
2245 case LFUN_BUFFER_BEGIN_SELECT:
2246 case LFUN_BUFFER_END_SELECT:
2247 case LFUN_UNICODE_INSERT:
2248 // these are handled in our dispatch()
2257 && (cur.empty() || !cur.inset().insetAllowed(code)))
2260 flag.enabled(enable);
2265 void Text::pasteString(Cursor & cur, docstring const & clip,
2268 cur.clearSelection();
2269 if (!clip.empty()) {
2272 insertStringAsParagraphs(cur, clip);
2274 insertStringAsLines(cur, clip);