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"
60 #include "insets/InsetGraphics.h"
61 #include "insets/InsetGraphicsParams.h"
63 #include "support/convert.h"
64 #include "support/debug.h"
65 #include "support/gettext.h"
66 #include "support/lstrings.h"
67 #include "support/lyxtime.h"
69 #include "mathed/InsetMathHull.h"
70 #include "mathed/MathMacroTemplate.h"
72 #include <boost/next_prior.hpp>
78 using namespace lyx::support;
82 using cap::copySelection;
83 using cap::cutSelection;
84 using cap::pasteFromStack;
85 using cap::pasteClipboardText;
86 using cap::pasteClipboardGraphics;
87 using cap::replaceSelection;
90 static Font freefont(ignore_font, ignore_language);
91 static bool toggleall = false;
93 static void toggleAndShow(Cursor & cur, Text * text,
94 Font const & font, bool toggleall = true)
96 text->toggleFree(cur, font, toggleall);
98 if (font.language() != ignore_language ||
99 font.fontInfo().number() != FONT_IGNORE) {
100 TextMetrics const & tm = cur.bv().textMetrics(text);
101 if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(),
102 cur.real_current_font))
103 text->setCursor(cur, cur.pit(), cur.pos(),
104 false, !cur.boundary());
109 static void moveCursor(Cursor & cur, bool selecting)
111 if (selecting || cur.mark())
116 static void finishChange(Cursor & cur, bool selecting)
119 moveCursor(cur, selecting);
123 static void mathDispatch(Cursor & cur, FuncRequest const & cmd, bool display)
126 docstring sel = cur.selectionAsString(false);
128 // It may happen that sel is empty but there is a selection
129 replaceSelection(cur);
132 #ifdef ENABLE_ASSERTIONS
133 const int old_pos = cur.pos();
135 cur.insert(new InsetMathHull(hullSimple));
136 #ifdef ENABLE_ASSERTIONS
137 LASSERT(old_pos == cur.pos(), /**/);
139 cur.nextInset()->edit(cur, true);
140 // don't do that also for LFUN_MATH_MODE
141 // unless you want end up with always changing
142 // to mathrm when opening an inlined inset --
143 // I really hate "LyXfunc overloading"...
145 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
146 // Avoid an unnecessary undo step if cmd.argument
148 if (!cmd.argument().empty())
149 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
152 // create a macro if we see "\\newcommand"
153 // somewhere, and an ordinary formula
155 if (sel.find(from_ascii("\\newcommand")) == string::npos
156 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
157 && sel.find(from_ascii("\\def")) == string::npos)
159 InsetMathHull * formula = new InsetMathHull;
160 istringstream is(to_utf8(sel));
164 if (formula->getType() == hullNone)
165 // Don't create pseudo formulas if
166 // delimiters are left out
167 formula->mutate(hullSimple);
170 cur.insert(new MathMacroTemplate(sel));
173 cur.message(from_utf8(N_("Math editor mode")));
177 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
180 cap::replaceSelection(cur);
181 cur.insert(new InsetSpecialChar(kind));
186 static bool doInsertInset(Cursor & cur, Text * text,
187 FuncRequest const & cmd, bool edit, bool pastesel)
189 Buffer & buffer = cur.bv().buffer();
190 BufferParams const & bparams = buffer.params();
191 Inset * inset = createInset(buffer, cmd);
195 if (InsetCollapsable * ci = inset->asInsetCollapsable())
196 ci->setLayout(bparams);
199 if (cmd.action == LFUN_INDEX_INSERT) {
200 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
201 text->insertInset(cur, inset);
203 inset->edit(cur, true);
204 // Now put this into inset
205 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
210 if (cur.selection()) {
211 lyx::dispatch(FuncRequest(LFUN_CUT));
214 text->insertInset(cur, inset);
217 inset->edit(cur, true);
219 if (!gotsel || !pastesel)
222 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
223 InsetText * insetText = dynamic_cast<InsetText *>(inset);
224 if (insetText && !insetText->allowMultiPar() || cur.lastpit() == 0) {
225 // reset first par to default
226 cur.text()->paragraphs().begin()
227 ->setPlainOrDefaultLayout(bparams.documentClass());
230 // Merge multiple paragraphs -- hack
231 while (cur.lastpit() > 0)
232 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
234 // reset surrounding par to default
235 docstring const layoutname = insetText->usePlainLayout()
236 ? bparams.documentClass().emptyLayoutName()
237 : bparams.documentClass().defaultLayoutName();
238 cur.leaveInset(*inset);
239 text->setLayout(cur, layoutname);
246 string const freefont2string()
248 return freefont.toString(toggleall);
252 /// the type of outline operation
254 OutlineUp, // Move this header with text down
255 OutlineDown, // Move this header with text up
256 OutlineIn, // Make this header deeper
257 OutlineOut // Make this header shallower
261 static void outline(OutlineOp mode, Cursor & cur)
263 Buffer & buf = cur.buffer();
264 pit_type & pit = cur.pit();
265 ParagraphList & pars = buf.text().paragraphs();
266 ParagraphList::iterator bgn = pars.begin();
267 // The first paragraph of the area to be copied:
268 ParagraphList::iterator start = boost::next(bgn, pit);
269 // The final paragraph of area to be copied:
270 ParagraphList::iterator finish = start;
271 ParagraphList::iterator end = pars.end();
273 DocumentClass const & tc = buf.params().documentClass();
275 int const thistoclevel = start->layout().toclevel;
278 // Move out (down) from this section header
281 // Seek the one (on same level) below
282 for (; finish != end; ++finish) {
283 toclevel = finish->layout().toclevel;
284 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel) {
291 if (start == pars.begin())
294 ParagraphList::iterator dest = start;
295 // Move out (up) from this header
298 // Search previous same-level header above
301 toclevel = dest->layout().toclevel;
303 && (toclevel == Layout::NOT_IN_TOC
304 || toclevel > thistoclevel));
305 // Not found; do nothing
306 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
308 pit_type const newpit = distance(bgn, dest);
309 pit_type const len = distance(start, finish);
310 pit_type const deletepit = pit + len;
311 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
312 pars.insert(dest, start, finish);
313 start = boost::next(pars.begin(), deletepit);
315 pars.erase(start, finish);
322 // Go one down from *this* header:
323 ParagraphList::iterator dest = boost::next(finish, 1);
324 // Go further down to find header to insert in front of:
325 for (; dest != end; ++dest) {
326 toclevel = dest->layout().toclevel;
327 if (toclevel != Layout::NOT_IN_TOC
328 && toclevel <= thistoclevel) {
332 // One such was found:
333 pit_type newpit = distance(bgn, dest);
334 pit_type const len = distance(start, finish);
335 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
336 pars.insert(dest, start, finish);
337 start = boost::next(bgn, pit);
339 pars.erase(start, finish);
343 pit_type const len = distance(start, finish);
344 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
345 for (; start != finish; ++start) {
346 toclevel = start->layout().toclevel;
347 if (toclevel == Layout::NOT_IN_TOC)
349 DocumentClass::const_iterator lit = tc.begin();
350 DocumentClass::const_iterator len = tc.end();
351 for (; lit != len; ++lit) {
352 if (lit->toclevel == toclevel + 1 &&
353 start->layout().labeltype == lit->labeltype) {
354 start->setLayout(*lit);
362 pit_type const len = distance(start, finish);
363 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, pit + len - 1);
364 for (; start != finish; ++start) {
365 toclevel = start->layout().toclevel;
366 if (toclevel == Layout::NOT_IN_TOC)
368 DocumentClass::const_iterator lit = tc.begin();
369 DocumentClass::const_iterator len = tc.end();
370 for (; lit != len; ++lit) {
371 if (lit->toclevel == toclevel - 1 &&
372 start->layout().labeltype == lit->labeltype) {
373 start->setLayout(*lit);
384 void Text::number(Cursor & cur)
386 FontInfo font = ignore_font;
387 font.setNumber(FONT_TOGGLE);
388 toggleAndShow(cur, this, Font(font, ignore_language));
392 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
394 return par.isRTL(buffer.params());
398 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
400 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
402 BufferView * bv = &cur.bv();
403 TextMetrics & tm = bv->textMetrics(this);
404 if (!tm.contains(cur.pit()))
405 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
407 // FIXME: We use the update flag to indicates wether a singlePar or a
408 // full screen update is needed. We reset it here but shall we restore it
412 LASSERT(cur.text() == this, /**/);
413 CursorSlice oldTopSlice = cur.top();
414 bool oldBoundary = cur.boundary();
415 bool sel = cur.selection();
416 // Signals that, even if needsUpdate == false, an update of the
417 // cursor paragraph is required
418 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
419 LyXAction::SingleParUpdate);
420 // Signals that a full-screen update is required
421 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
422 LyXAction::NoUpdate) || singleParUpdate);
424 switch (cmd.action) {
426 case LFUN_PARAGRAPH_MOVE_DOWN: {
427 pit_type const pit = cur.pit();
428 recUndo(cur, pit, pit + 1);
430 swap(pars_[pit], pars_[pit + 1]);
431 updateLabels(cur.buffer());
437 case LFUN_PARAGRAPH_MOVE_UP: {
438 pit_type const pit = cur.pit();
439 recUndo(cur, pit - 1, pit);
441 swap(pars_[pit], pars_[pit - 1]);
442 updateLabels(cur.buffer());
448 case LFUN_APPENDIX: {
449 Paragraph & par = cur.paragraph();
450 bool start = !par.params().startOfAppendix();
452 // FIXME: The code below only makes sense at top level.
453 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
454 // ensure that we have only one start_of_appendix in this document
455 // FIXME: this don't work for multipart document!
456 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
457 if (pars_[tmp].params().startOfAppendix()) {
459 pars_[tmp].params().startOfAppendix(false);
465 par.params().startOfAppendix(start);
467 // we can set the refreshing parameters now
468 updateLabels(cur.buffer());
472 case LFUN_WORD_DELETE_FORWARD:
473 if (cur.selection()) {
474 cutSelection(cur, true, false);
476 deleteWordForward(cur);
477 finishChange(cur, false);
480 case LFUN_WORD_DELETE_BACKWARD:
481 if (cur.selection()) {
482 cutSelection(cur, true, false);
484 deleteWordBackward(cur);
485 finishChange(cur, false);
488 case LFUN_LINE_DELETE:
489 if (cur.selection()) {
490 cutSelection(cur, true, false);
492 tm.deleteLineForward(cur);
493 finishChange(cur, false);
496 case LFUN_BUFFER_BEGIN:
497 case LFUN_BUFFER_BEGIN_SELECT:
498 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
499 if (cur.depth() == 1) {
500 needsUpdate |= cursorTop(cur);
504 cur.updateFlags(Update::FitCursor);
507 case LFUN_BUFFER_END:
508 case LFUN_BUFFER_END_SELECT:
509 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
510 if (cur.depth() == 1) {
511 needsUpdate |= cursorBottom(cur);
515 cur.updateFlags(Update::FitCursor);
518 case LFUN_CHAR_FORWARD:
519 case LFUN_CHAR_FORWARD_SELECT:
520 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
521 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
522 needsUpdate |= cursorForward(cur);
524 if (!needsUpdate && oldTopSlice == cur.top()
525 && cur.boundary() == oldBoundary) {
527 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
531 case LFUN_CHAR_BACKWARD:
532 case LFUN_CHAR_BACKWARD_SELECT:
533 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
534 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
535 needsUpdate |= cursorBackward(cur);
537 if (!needsUpdate && oldTopSlice == cur.top()
538 && cur.boundary() == oldBoundary) {
540 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
545 case LFUN_CHAR_LEFT_SELECT:
546 if (lyxrc.visual_cursor) {
547 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
548 needsUpdate |= cursorVisLeft(cur);
549 if (!needsUpdate && oldTopSlice == cur.top()
550 && cur.boundary() == oldBoundary) {
552 cmd = FuncRequest(LFUN_FINISHED_LEFT);
555 if (reverseDirectionNeeded(cur)) {
556 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
557 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
559 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
560 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
567 case LFUN_CHAR_RIGHT:
568 case LFUN_CHAR_RIGHT_SELECT:
569 if (lyxrc.visual_cursor) {
570 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
571 needsUpdate |= cursorVisRight(cur);
572 if (!needsUpdate && oldTopSlice == cur.top()
573 && cur.boundary() == oldBoundary) {
575 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
578 if (reverseDirectionNeeded(cur)) {
579 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
580 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
582 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
583 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
592 case LFUN_DOWN_SELECT:
595 // stop/start the selection
596 bool select = cmd.action == LFUN_DOWN_SELECT ||
597 cmd.action == LFUN_UP_SELECT;
598 cur.selHandle(select);
600 // move cursor up/down
601 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
602 bool const successful = cur.upDownInText(up, needsUpdate);
604 // redraw if you leave mathed (for the decorations)
605 needsUpdate |= cur.beforeDispatchCursor().inMathed();
612 case LFUN_PARAGRAPH_UP:
613 case LFUN_PARAGRAPH_UP_SELECT:
614 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
615 needsUpdate |= cursorUpParagraph(cur);
618 case LFUN_PARAGRAPH_DOWN:
619 case LFUN_PARAGRAPH_DOWN_SELECT:
620 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
621 needsUpdate |= cursorDownParagraph(cur);
624 case LFUN_LINE_BEGIN:
625 case LFUN_LINE_BEGIN_SELECT:
626 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
627 needsUpdate |= tm.cursorHome(cur);
631 case LFUN_LINE_END_SELECT:
632 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
633 needsUpdate |= tm.cursorEnd(cur);
636 case LFUN_WORD_RIGHT:
637 case LFUN_WORD_RIGHT_SELECT:
638 if (lyxrc.visual_cursor) {
639 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_RIGHT_SELECT);
640 needsUpdate |= cursorVisRightOneWord(cur);
641 if (!needsUpdate && oldTopSlice == cur.top()
642 && cur.boundary() == oldBoundary) {
644 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
647 if (reverseDirectionNeeded(cur)) {
648 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
649 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
651 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
652 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
659 case LFUN_WORD_FORWARD:
660 case LFUN_WORD_FORWARD_SELECT:
661 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
662 needsUpdate |= cursorForwardOneWord(cur);
666 case LFUN_WORD_LEFT_SELECT:
667 if (lyxrc.visual_cursor) {
668 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_LEFT_SELECT);
669 needsUpdate |= cursorVisLeftOneWord(cur);
670 if (!needsUpdate && oldTopSlice == cur.top()
671 && cur.boundary() == oldBoundary) {
673 cmd = FuncRequest(LFUN_FINISHED_LEFT);
676 if (reverseDirectionNeeded(cur)) {
677 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
678 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
680 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
681 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
688 case LFUN_WORD_BACKWARD:
689 case LFUN_WORD_BACKWARD_SELECT:
690 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
691 needsUpdate |= cursorBackwardOneWord(cur);
694 case LFUN_WORD_SELECT: {
695 selectWord(cur, WHOLE_WORD);
696 finishChange(cur, true);
700 case LFUN_NEWLINE_INSERT: {
701 InsetNewlineParams inp;
702 docstring arg = cmd.argument();
703 // this avoids a double undo
704 // FIXME: should not be needed, ideally
705 if (!cur.selection())
707 cap::replaceSelection(cur);
708 if (arg == "linebreak")
709 inp.kind = InsetNewlineParams::LINEBREAK;
711 inp.kind = InsetNewlineParams::NEWLINE;
712 cur.insert(new InsetNewline(inp));
714 moveCursor(cur, false);
718 case LFUN_CHAR_DELETE_FORWARD:
719 if (!cur.selection()) {
720 if (cur.pos() == cur.paragraph().size())
721 // Par boundary, force full-screen update
722 singleParUpdate = false;
723 needsUpdate |= erase(cur);
725 // It is possible to make it a lot faster still
726 // just comment out the line below...
728 cutSelection(cur, true, false);
729 singleParUpdate = false;
731 moveCursor(cur, false);
734 case LFUN_CHAR_DELETE_BACKWARD:
735 if (!cur.selection()) {
736 if (bv->getIntl().getTransManager().backspace()) {
737 // Par boundary, full-screen update
739 singleParUpdate = false;
740 needsUpdate |= backspace(cur);
742 // It is possible to make it a lot faster still
743 // just comment out the line below...
746 cutSelection(cur, true, false);
747 singleParUpdate = false;
751 case LFUN_BREAK_PARAGRAPH:
752 cap::replaceSelection(cur);
753 breakParagraph(cur, cmd.argument() == "inverse");
758 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
759 // as its duties can be performed there. Should it be removed??
760 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
761 case LFUN_PARAGRAPH_SPACING: {
762 Paragraph & par = cur.paragraph();
763 Spacing::Space cur_spacing = par.params().spacing().getSpace();
764 string cur_value = "1.0";
765 if (cur_spacing == Spacing::Other)
766 cur_value = par.params().spacing().getValueAsString();
768 istringstream is(to_utf8(cmd.argument()));
771 Spacing::Space new_spacing = cur_spacing;
772 string new_value = cur_value;
774 lyxerr << "Missing argument to `paragraph-spacing'"
776 } else if (tmp == "single") {
777 new_spacing = Spacing::Single;
778 } else if (tmp == "onehalf") {
779 new_spacing = Spacing::Onehalf;
780 } else if (tmp == "double") {
781 new_spacing = Spacing::Double;
782 } else if (tmp == "other") {
783 new_spacing = Spacing::Other;
784 string tmpval = "0.0";
786 lyxerr << "new_value = " << tmpval << endl;
789 } else if (tmp == "default") {
790 new_spacing = Spacing::Default;
792 lyxerr << to_utf8(_("Unknown spacing argument: "))
793 << to_utf8(cmd.argument()) << endl;
795 if (cur_spacing != new_spacing || cur_value != new_value)
796 par.params().spacing(Spacing(new_spacing, new_value));
800 case LFUN_INSET_INSERT: {
802 Inset * inset = createInset(bv->buffer(), cmd);
804 // FIXME (Abdel 01/02/2006):
805 // What follows would be a partial fix for bug 2154:
806 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
807 // This automatically put the label inset _after_ a
808 // numbered section. It should be possible to extend the mechanism
809 // to any kind of LateX environement.
810 // The correct way to fix that bug would be at LateX generation.
811 // I'll let the code here for reference as it could be used for some
812 // other feature like "automatic labelling".
814 Paragraph & par = pars_[cur.pit()];
815 if (inset->lyxCode() == LABEL_CODE
816 && par.layout().labeltype == LABEL_COUNTER) {
817 // Go to the end of the paragraph
818 // Warning: Because of Change-Tracking, the last
819 // position is 'size()' and not 'size()-1':
820 cur.pos() = par.size();
821 // Insert a new paragraph
822 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
827 cutSelection(cur, true, false);
834 case LFUN_INSET_DISSOLVE:
835 needsUpdate |= dissolveInset(cur);
838 case LFUN_INSET_SETTINGS: {
839 Inset & inset = cur.inset();
840 if (cmd.getArg(0) == insetName(inset.lyxCode())) {
841 // This inset dialog has been explicitely requested.
842 inset.showInsetDialog(bv);
845 // else, if there is an inset at the cursor, access this
846 Inset * next_inset = cur.nextInset();
848 next_inset->showInsetDialog(bv);
851 // if not then access the underlying inset.
852 inset.showInsetDialog(bv);
856 case LFUN_SET_GRAPHICS_GROUP: {
857 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
861 cur.recordUndoFullDocument();
863 string id = to_utf8(cmd.argument());
864 string grp = graphics::getGroupParams(bv->buffer(), id);
865 InsetGraphicsParams tmp, inspar = ins->getParams();
868 inspar.groupId = to_utf8(cmd.argument());
870 InsetGraphics::string2params(grp, bv->buffer(), tmp);
871 tmp.filename = inspar.filename;
875 ins->setParams(inspar);
878 case LFUN_SPACE_INSERT:
879 if (cur.paragraph().layout().free_spacing)
880 insertChar(cur, ' ');
882 doInsertInset(cur, this, cmd, false, false);
885 moveCursor(cur, false);
888 case LFUN_SPECIALCHAR_INSERT: {
889 string const name = to_utf8(cmd.argument());
890 if (name == "hyphenation")
891 specialChar(cur, InsetSpecialChar::HYPHENATION);
892 else if (name == "ligature-break")
893 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
894 else if (name == "slash")
895 specialChar(cur, InsetSpecialChar::SLASH);
896 else if (name == "nobreakdash")
897 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
898 else if (name == "dots")
899 specialChar(cur, InsetSpecialChar::LDOTS);
900 else if (name == "end-of-sentence")
901 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
902 else if (name == "menu-separator")
903 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
904 else if (name.empty())
905 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
907 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
911 case LFUN_WORD_UPCASE:
912 changeCase(cur, text_uppercase);
915 case LFUN_WORD_LOWCASE:
916 changeCase(cur, text_lowercase);
919 case LFUN_WORD_CAPITALIZE:
920 changeCase(cur, text_capitalization);
923 case LFUN_CHARS_TRANSPOSE:
928 cur.message(_("Paste"));
929 cap::replaceSelection(cur);
932 string const arg = to_utf8(cmd.argument());
934 if (theClipboard().isInternal())
935 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
936 else if (theClipboard().hasGraphicsContents())
937 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
939 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
940 } else if (isStrUnsignedInt(arg)) {
941 // we have a numerical argument
942 pasteFromStack(cur, bv->buffer().errorList("Paste"),
943 convert<unsigned int>(arg));
945 Clipboard::GraphicsType type = Clipboard::AnyGraphicsType;
947 type = Clipboard::PdfGraphicsType;
948 else if (arg == "png")
949 type = Clipboard::PngGraphicsType;
950 else if (arg == "jpeg")
951 type = Clipboard::JpegGraphicsType;
952 else if (arg == "linkback")
953 type = Clipboard::LinkBackGraphicsType;
955 LASSERT(false, /**/);
957 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
960 bv->buffer().errors("Paste");
961 cur.clearSelection(); // bug 393
967 cutSelection(cur, true, true);
968 cur.message(_("Cut"));
973 cur.message(_("Copy"));
976 case LFUN_SERVER_GET_XY:
977 cur.message(from_utf8(
978 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
979 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
982 case LFUN_SERVER_SET_XY: {
985 istringstream is(to_utf8(cmd.argument()));
988 lyxerr << "SETXY: Could not parse coordinates in '"
989 << to_utf8(cmd.argument()) << endl;
991 tm.setCursorFromCoordinates(cur, x, y);
995 case LFUN_SERVER_GET_LAYOUT:
996 cur.message(cur.paragraph().layout().name());
1000 docstring layout = cmd.argument();
1001 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1003 Paragraph const & para = cur.paragraph();
1004 docstring const old_layout = para.layout().name();
1005 DocumentClass const & tclass = bv->buffer().params().documentClass();
1008 layout = tclass.defaultLayoutName();
1010 if (para.forcePlainLayout())
1011 // in this case only the empty layout is allowed
1012 layout = tclass.emptyLayoutName();
1013 else if (para.usePlainLayout()) {
1014 // in this case, default layout maps to empty layout
1015 if (layout == tclass.defaultLayoutName())
1016 layout = tclass.emptyLayoutName();
1018 // otherwise, the empty layout maps to the default
1019 if (layout == tclass.emptyLayoutName())
1020 layout = tclass.defaultLayoutName();
1023 bool hasLayout = tclass.hasLayout(layout);
1025 // If the entry is obsolete, use the new one instead.
1027 docstring const & obs = tclass[layout].obsoleted_by();
1033 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1034 from_utf8(N_(" not known")));
1038 bool change_layout = (old_layout != layout);
1040 if (!change_layout && cur.selection() &&
1041 cur.selBegin().pit() != cur.selEnd().pit())
1043 pit_type spit = cur.selBegin().pit();
1044 pit_type epit = cur.selEnd().pit() + 1;
1045 while (spit != epit) {
1046 if (pars_[spit].layout().name() != old_layout) {
1047 change_layout = true;
1055 setLayout(cur, layout);
1060 case LFUN_CLIPBOARD_PASTE:
1061 cur.clearSelection();
1062 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1063 cmd.argument() == "paragraph");
1064 bv->buffer().errors("Paste");
1067 case LFUN_PRIMARY_SELECTION_PASTE:
1068 pasteString(cur, theSelection().get(),
1069 cmd.argument() == "paragraph");
1072 case LFUN_UNICODE_INSERT: {
1073 if (cmd.argument().empty())
1075 docstring hexstring = cmd.argument();
1076 if (isHex(hexstring)) {
1077 char_type c = hexToInt(hexstring);
1078 if (c >= 32 && c < 0x10ffff) {
1079 lyxerr << "Inserting c: " << c << endl;
1080 docstring s = docstring(1, c);
1081 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1087 case LFUN_QUOTE_INSERT: {
1088 Paragraph & par = cur.paragraph();
1089 pos_type pos = cur.pos();
1090 BufferParams const & bufparams = bv->buffer().params();
1091 Layout const & style = par.layout();
1092 if (!style.pass_thru
1093 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1094 // this avoids a double undo
1095 // FIXME: should not be needed, ideally
1096 if (!cur.selection())
1098 cap::replaceSelection(cur);
1103 else if (cur.prevInset() && cur.prevInset()->isSpace())
1106 c = par.getChar(pos - 1);
1107 string arg = to_utf8(cmd.argument());
1108 cur.insert(new InsetQuotes(c, bufparams.quotes_language,
1109 (arg == "single") ? InsetQuotes::SingleQuotes
1110 : InsetQuotes::DoubleQuotes));
1114 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1118 case LFUN_DATE_INSERT: {
1119 string const format = cmd.argument().empty()
1120 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1121 string const time = formatted_time(current_time(), format);
1122 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1126 case LFUN_MOUSE_TRIPLE:
1127 if (cmd.button() == mouse_button::button1) {
1136 case LFUN_MOUSE_DOUBLE:
1137 if (cmd.button() == mouse_button::button1) {
1138 selectWord(cur, WHOLE_WORD_STRICT);
1143 // Single-click on work area
1144 case LFUN_MOUSE_PRESS:
1145 // We are not marking a selection with the keyboard in any case.
1146 cur.bv().cursor().mark() = false;
1147 switch (cmd.button()) {
1148 case mouse_button::button1:
1150 if (!bv->mouseSetCursor(cur, cmd.argument() == "region-select"))
1151 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1154 case mouse_button::button2:
1155 // Middle mouse pasting.
1156 if (!cap::selection()) {
1157 // There is no local selection in the current buffer, so try to
1158 // paste primary selection instead.
1159 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE,
1161 // Nothing else to do.
1165 // Copy the selection buffer to the clipboard stack, because we want it
1166 // to appear in the "Edit->Paste recent" menu.
1167 cap::copySelectionToStack();
1168 cap::pasteSelection(bv->cursor(), bv->buffer().errorList("Paste"));
1169 cur.updateFlags(Update::Force | Update::FitCursor);
1170 bv->buffer().errors("Paste");
1171 bv->buffer().markDirty();
1172 bv->cursor().finishUndo();
1175 case mouse_button::button3:
1176 if (cur.selection()) {
1177 DocIterator const selbeg = cur.selectionBegin();
1178 DocIterator const selend = cur.selectionEnd();
1179 Cursor tmpcur = cur;
1180 tm.setCursorFromCoordinates(tmpcur, cmd.x, cmd.y);
1181 // Don't do anything if we right-click a selection, a selection
1182 // context menu should popup instead.
1183 if (tmpcur < selbeg || tmpcur >= selend) {
1188 if (!bv->mouseSetCursor(cur, false)) {
1189 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1194 } // switch (cmd.button())
1197 case LFUN_MOUSE_MOTION: {
1198 // Mouse motion with right or middle mouse do nothing for now.
1199 if (cmd.button() != mouse_button::button1) {
1203 // ignore motions deeper nested than the real anchor
1204 Cursor & bvcur = cur.bv().cursor();
1205 if (!bvcur.anchor_.hasPart(cur)) {
1209 CursorSlice old = bvcur.top();
1211 int const wh = bv->workHeight();
1212 int const y = max(0, min(wh - 1, cmd.y));
1214 tm.setCursorFromCoordinates(cur, cmd.x, y);
1215 cur.setTargetX(cmd.x);
1217 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1219 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1220 // This is to allow jumping over large insets
1221 if (cur.top() == old) {
1223 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1225 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1227 // We continue with our existing selection or start a new one, so don't
1228 // reset the anchor.
1229 bvcur.setCursor(cur);
1230 bvcur.selection() = true;
1231 if (cur.top() == old) {
1232 // We didn't move one iota, so no need to update the screen.
1233 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1240 case LFUN_MOUSE_RELEASE:
1241 switch (cmd.button()) {
1242 case mouse_button::button1:
1243 // Cursor was set at LFUN_MOUSE_PRESS or LFUN_MOUSE_MOTION time.
1244 // If there is a new selection, update persistent selection;
1245 // otherwise, single click does not clear persistent selection
1247 if (cur.selection()) {
1248 // Finish selection.
1249 // If double click, cur is moved to the end of word by selectWord
1250 // but bvcur is current mouse position.
1251 cur.bv().cursor().selection() = true;
1253 // FIXME: We could try to handle drag and drop of selection here.
1257 case mouse_button::button2:
1258 // Middle mouse pasting is handled at mouse press time,
1259 // see LFUN_MOUSE_PRESS.
1263 case mouse_button::button3:
1264 // Cursor was set at LFUN_MOUSE_PRESS time.
1265 // FIXME: If there is a selection we could try to handle a special
1266 // drag & drop context menu.
1270 case mouse_button::none:
1271 case mouse_button::button4:
1272 case mouse_button::button5:
1274 } // switch (cmd.button())
1278 case LFUN_SELF_INSERT: {
1279 if (cmd.argument().empty())
1282 // Automatically delete the currently selected
1283 // text and replace it with what is being
1284 // typed in now. Depends on lyxrc settings
1285 // "auto_region_delete", which defaults to
1288 if (lyxrc.auto_region_delete && cur.selection())
1289 cutSelection(cur, false, false);
1291 cur.clearSelection();
1292 Font const old_font = cur.real_current_font;
1294 docstring::const_iterator cit = cmd.argument().begin();
1295 docstring::const_iterator end = cmd.argument().end();
1296 for (; cit != end; ++cit)
1297 bv->translateAndInsert(*cit, this, cur);
1300 moveCursor(cur, false);
1304 case LFUN_HYPERLINK_INSERT: {
1305 InsetCommandParams p(HYPERLINK_CODE);
1307 if (cur.selection()) {
1308 content = cur.selectionAsString(false);
1309 cutSelection(cur, true, false);
1311 p["target"] = (cmd.argument().empty()) ?
1312 content : cmd.argument();
1313 string const data = InsetCommand::params2string("href", p);
1314 if (p["target"].empty()) {
1315 bv->showDialog("href", data);
1317 FuncRequest fr(LFUN_INSET_INSERT, data);
1323 case LFUN_LABEL_INSERT: {
1324 InsetCommandParams p(LABEL_CODE);
1325 // Try to generate a valid label
1326 p["name"] = (cmd.argument().empty()) ?
1327 cur.getPossibleLabel() :
1329 string const data = InsetCommand::params2string("label", p);
1331 if (cmd.argument().empty()) {
1332 bv->showDialog("label", data);
1334 FuncRequest fr(LFUN_INSET_INSERT, data);
1340 case LFUN_INFO_INSERT: {
1342 if (cmd.argument().empty() && cur.selection()) {
1343 // if command argument is empty use current selection as parameter.
1344 docstring ds = cur.selectionAsString(false);
1345 cutSelection(cur, true, false);
1346 FuncRequest cmd0(cmd, ds);
1347 inset = createInset(cur.bv().buffer(), cmd0);
1349 inset = createInset(cur.bv().buffer(), cmd);
1353 insertInset(cur, inset);
1357 case LFUN_CAPTION_INSERT:
1358 case LFUN_FOOTNOTE_INSERT:
1359 case LFUN_NOTE_INSERT:
1360 case LFUN_FLEX_INSERT:
1361 case LFUN_BOX_INSERT:
1362 case LFUN_BRANCH_INSERT:
1363 case LFUN_BIBITEM_INSERT:
1364 case LFUN_ERT_INSERT:
1365 case LFUN_LISTING_INSERT:
1366 case LFUN_MARGINALNOTE_INSERT:
1367 case LFUN_OPTIONAL_INSERT:
1368 case LFUN_INDEX_INSERT:
1369 // Open the inset, and move the current selection
1371 doInsertInset(cur, this, cmd, true, true);
1373 // Some insets are numbered, others are shown in the outline pane so
1374 // let's update the labels and the toc backend.
1375 updateLabels(bv->buffer());
1378 case LFUN_TABULAR_INSERT:
1379 // if there were no arguments, just open the dialog
1380 if (doInsertInset(cur, this, cmd, false, true))
1383 bv->showDialog("tabularcreate");
1387 case LFUN_FLOAT_INSERT:
1388 case LFUN_FLOAT_WIDE_INSERT:
1389 case LFUN_WRAP_INSERT: {
1390 bool content = cur.selection(); // will some text be moved into the inset?
1392 doInsertInset(cur, this, cmd, true, true);
1394 ParagraphList & pars = cur.text()->paragraphs();
1396 DocumentClass const & tclass = bv->buffer().params().documentClass();
1398 // add a separate paragraph for the caption inset
1399 pars.push_back(Paragraph());
1400 pars.back().setInsetOwner(pars[0].inInset());
1401 pars.back().setPlainOrDefaultLayout(tclass);
1402 int cap_pit = pars.size() - 1;
1404 // if an empty inset was created, we create an additional empty
1405 // paragraph at the bottom so that the user can choose where to put
1406 // the graphics (or table).
1408 pars.push_back(Paragraph());
1409 pars.back().setInsetOwner(pars[0].inInset());
1410 pars.back().setPlainOrDefaultLayout(tclass);
1413 // reposition the cursor to the caption
1414 cur.pit() = cap_pit;
1416 // FIXME: This Text/Cursor dispatch handling is a mess!
1417 // We cannot use Cursor::dispatch here it needs access to up to
1419 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1420 cur.text()->dispatch(cur, cmd_caption);
1421 cur.updateFlags(Update::Force);
1422 // FIXME: When leaving the Float (or Wrap) inset we should
1423 // delete any empty paragraph left above or below the
1428 case LFUN_NOMENCL_INSERT: {
1429 FuncRequest cmd1 = cmd;
1430 if (cmd.argument().empty())
1431 cmd1 = FuncRequest(cmd,
1432 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1433 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1437 cur.clearSelection();
1438 insertInset(cur, inset);
1439 // Show the dialog for the nomenclature entry, since the
1440 // description entry still needs to be filled in.
1441 if (cmd.action == LFUN_NOMENCL_INSERT)
1442 inset->edit(cur, true);
1447 case LFUN_INDEX_PRINT:
1448 case LFUN_NOMENCL_PRINT:
1449 case LFUN_TOC_INSERT:
1450 case LFUN_LINE_INSERT:
1451 case LFUN_NEWPAGE_INSERT:
1453 doInsertInset(cur, this, cmd, false, false);
1457 case LFUN_DEPTH_DECREMENT:
1458 changeDepth(cur, DEC_DEPTH);
1461 case LFUN_DEPTH_INCREMENT:
1462 changeDepth(cur, INC_DEPTH);
1465 case LFUN_MATH_DISPLAY:
1466 mathDispatch(cur, cmd, true);
1469 case LFUN_MATH_MODE:
1470 if (cmd.argument() == "on")
1471 // don't pass "on" as argument
1472 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1474 mathDispatch(cur, cmd, false);
1477 case LFUN_MATH_MACRO:
1478 if (cmd.argument().empty())
1479 cur.errorMessage(from_utf8(N_("Missing argument")));
1481 string s = to_utf8(cmd.argument());
1482 string const s1 = token(s, ' ', 1);
1483 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1484 string const s2 = token(s, ' ', 2);
1485 MacroType type = MacroTypeNewcommand;
1487 type = MacroTypeDef;
1488 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1489 //cur.nextInset()->edit(cur, true);
1493 // passthrough hat and underscore outside mathed:
1494 case LFUN_MATH_SUBSCRIPT:
1495 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1497 case LFUN_MATH_SUPERSCRIPT:
1498 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1501 case LFUN_MATH_INSERT:
1502 case LFUN_MATH_MATRIX:
1503 case LFUN_MATH_DELIM:
1504 case LFUN_MATH_BIGDELIM: {
1505 cap::replaceSelection(cur);
1506 cur.insert(new InsetMathHull(hullSimple));
1507 checkAndActivateInset(cur, true);
1508 LASSERT(cur.inMathed(), /**/);
1513 case LFUN_FONT_EMPH: {
1514 Font font(ignore_font, ignore_language);
1515 font.fontInfo().setEmph(FONT_TOGGLE);
1516 toggleAndShow(cur, this, font);
1520 case LFUN_FONT_BOLD: {
1521 Font font(ignore_font, ignore_language);
1522 font.fontInfo().setSeries(BOLD_SERIES);
1523 toggleAndShow(cur, this, font);
1527 case LFUN_FONT_NOUN: {
1528 Font font(ignore_font, ignore_language);
1529 font.fontInfo().setNoun(FONT_TOGGLE);
1530 toggleAndShow(cur, this, font);
1534 case LFUN_FONT_TYPEWRITER: {
1535 Font font(ignore_font, ignore_language);
1536 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1537 toggleAndShow(cur, this, font);
1541 case LFUN_FONT_SANS: {
1542 Font font(ignore_font, ignore_language);
1543 font.fontInfo().setFamily(SANS_FAMILY);
1544 toggleAndShow(cur, this, font);
1548 case LFUN_FONT_ROMAN: {
1549 Font font(ignore_font, ignore_language);
1550 font.fontInfo().setFamily(ROMAN_FAMILY);
1551 toggleAndShow(cur, this, font);
1555 case LFUN_FONT_DEFAULT: {
1556 Font font(inherit_font, ignore_language);
1557 toggleAndShow(cur, this, font);
1561 case LFUN_FONT_UNDERLINE: {
1562 Font font(ignore_font, ignore_language);
1563 font.fontInfo().setUnderbar(FONT_TOGGLE);
1564 toggleAndShow(cur, this, font);
1568 case LFUN_FONT_SIZE: {
1569 Font font(ignore_font, ignore_language);
1570 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1571 toggleAndShow(cur, this, font);
1575 case LFUN_LANGUAGE: {
1576 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1579 Font font(ignore_font, lang);
1580 toggleAndShow(cur, this, font);
1584 case LFUN_TEXTSTYLE_APPLY:
1585 toggleAndShow(cur, this, freefont, toggleall);
1586 cur.message(_("Character set"));
1589 // Set the freefont using the contents of \param data dispatched from
1590 // the frontends and apply it at the current cursor location.
1591 case LFUN_TEXTSTYLE_UPDATE: {
1594 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1597 toggleAndShow(cur, this, freefont, toggleall);
1598 cur.message(_("Character set"));
1600 lyxerr << "Argument not ok";
1605 case LFUN_FINISHED_LEFT:
1606 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1607 // We're leaving an inset, going left. If the inset is LTR, we're
1608 // leaving from the front, so we should not move (remain at --- but
1609 // not in --- the inset). If the inset is RTL, move left, without
1610 // entering the inset itself; i.e., move to after the inset.
1611 if (cur.paragraph().getFontSettings(
1612 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1613 cursorVisLeft(cur, true);
1616 case LFUN_FINISHED_RIGHT:
1617 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1618 // We're leaving an inset, going right. If the inset is RTL, we're
1619 // leaving from the front, so we should not move (remain at --- but
1620 // not in --- the inset). If the inset is LTR, move right, without
1621 // entering the inset itself; i.e., move to after the inset.
1622 if (!cur.paragraph().getFontSettings(
1623 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1624 cursorVisRight(cur, true);
1627 case LFUN_FINISHED_BACKWARD:
1628 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1631 case LFUN_FINISHED_FORWARD:
1632 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1634 cur.setCurrentFont();
1637 case LFUN_LAYOUT_PARAGRAPH: {
1639 params2string(cur.paragraph(), data);
1640 data = "show\n" + data;
1641 bv->showDialog("paragraph", data);
1645 case LFUN_PARAGRAPH_UPDATE: {
1647 params2string(cur.paragraph(), data);
1649 // Will the paragraph accept changes from the dialog?
1651 cur.inset().allowParagraphCustomization(cur.idx());
1653 data = "update " + convert<string>(accept) + '\n' + data;
1654 bv->updateDialog("paragraph", data);
1658 case LFUN_ACCENT_UMLAUT:
1659 case LFUN_ACCENT_CIRCUMFLEX:
1660 case LFUN_ACCENT_GRAVE:
1661 case LFUN_ACCENT_ACUTE:
1662 case LFUN_ACCENT_TILDE:
1663 case LFUN_ACCENT_CEDILLA:
1664 case LFUN_ACCENT_MACRON:
1665 case LFUN_ACCENT_DOT:
1666 case LFUN_ACCENT_UNDERDOT:
1667 case LFUN_ACCENT_UNDERBAR:
1668 case LFUN_ACCENT_CARON:
1669 case LFUN_ACCENT_BREVE:
1670 case LFUN_ACCENT_TIE:
1671 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1672 case LFUN_ACCENT_CIRCLE:
1673 case LFUN_ACCENT_OGONEK:
1674 theLyXFunc().handleKeyFunc(cmd.action);
1675 if (!cmd.argument().empty())
1676 // FIXME: Are all these characters encoded in one byte in utf8?
1677 bv->translateAndInsert(cmd.argument()[0], this, cur);
1680 case LFUN_FLOAT_LIST_INSERT: {
1681 DocumentClass const & tclass = bv->buffer().params().documentClass();
1682 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1684 if (cur.selection())
1685 cutSelection(cur, true, false);
1686 breakParagraph(cur);
1688 if (cur.lastpos() != 0) {
1689 cursorBackward(cur);
1690 breakParagraph(cur);
1693 docstring const laystr = cur.inset().usePlainLayout() ?
1694 tclass.emptyLayoutName() :
1695 tclass.defaultLayoutName();
1696 setLayout(cur, laystr);
1697 ParagraphParameters p;
1698 setParagraphs(cur, p);
1699 // FIXME This should be simplified when InsetFloatList takes a
1700 // Buffer in its constructor.
1701 InsetFloatList * ifl = new InsetFloatList(to_utf8(cmd.argument()));
1702 ifl->setBuffer(bv->buffer());
1703 insertInset(cur, ifl);
1706 lyxerr << "Non-existent float type: "
1707 << to_utf8(cmd.argument()) << endl;
1712 case LFUN_CHANGE_ACCEPT: {
1713 acceptOrRejectChanges(cur, ACCEPT);
1717 case LFUN_CHANGE_REJECT: {
1718 acceptOrRejectChanges(cur, REJECT);
1722 case LFUN_THESAURUS_ENTRY: {
1723 docstring arg = cmd.argument();
1725 arg = cur.selectionAsString(false);
1727 if (arg.size() > 100 || arg.empty()) {
1728 // Get word or selection
1729 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1730 arg = cur.selectionAsString(false);
1733 bv->showDialog("thesaurus", to_utf8(arg));
1737 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1738 // Given data, an encoding of the ParagraphParameters
1739 // generated in the Paragraph dialog, this function sets
1740 // the current paragraph, or currently selected paragraphs,
1742 // NOTE: This function overrides all existing settings.
1743 setParagraphs(cur, cmd.argument());
1744 cur.message(_("Paragraph layout set"));
1748 case LFUN_PARAGRAPH_PARAMS: {
1749 // Given data, an encoding of the ParagraphParameters as we'd
1750 // find them in a LyX file, this function modifies the current paragraph,
1751 // or currently selected paragraphs.
1752 // NOTE: This function only modifies, and does not override, existing
1754 setParagraphs(cur, cmd.argument(), true);
1755 cur.message(_("Paragraph layout set"));
1760 if (cur.selection()) {
1761 cur.selection() = false;
1764 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1765 // correct, but I'm not 100% sure -- dov, 071019
1766 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1770 case LFUN_OUTLINE_UP:
1771 outline(OutlineUp, cur);
1772 setCursor(cur, cur.pit(), 0);
1773 updateLabels(cur.buffer());
1777 case LFUN_OUTLINE_DOWN:
1778 outline(OutlineDown, cur);
1779 setCursor(cur, cur.pit(), 0);
1780 updateLabels(cur.buffer());
1784 case LFUN_OUTLINE_IN:
1785 outline(OutlineIn, cur);
1786 updateLabels(cur.buffer());
1790 case LFUN_OUTLINE_OUT:
1791 outline(OutlineOut, cur);
1792 updateLabels(cur.buffer());
1797 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1802 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1804 // FIXME: The cursor flag is reset two lines below
1805 // so we need to check here if some of the LFUN did touch that.
1806 // for now only Text::erase() and Text::backspace() do that.
1807 // The plan is to verify all the LFUNs and then to remove this
1808 // singleParUpdate boolean altogether.
1809 if (cur.result().update() & Update::Force) {
1810 singleParUpdate = false;
1814 // FIXME: the following code should go in favor of fine grained
1815 // update flag treatment.
1816 if (singleParUpdate) {
1817 // Inserting characters does not change par height in general. So, try
1818 // to update _only_ this paragraph. BufferView will detect if a full
1819 // metrics update is needed anyway.
1820 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1825 && &oldTopSlice.inset() == &cur.inset()
1826 && oldTopSlice.idx() == cur.idx()
1827 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1828 && !cur.selection())
1829 // FIXME: it would be better if we could just do this
1831 //if (cur.result().update() != Update::FitCursor)
1834 // But some LFUNs do not set Update::FitCursor when needed, so we
1835 // do it for all. This is not very harmfull as FitCursor will provoke
1836 // a full redraw only if needed but still, a proper review of all LFUN
1837 // should be done and this needsUpdate boolean can then be removed.
1838 cur.updateFlags(Update::FitCursor);
1840 cur.updateFlags(Update::Force | Update::FitCursor);
1844 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1845 FuncStatus & flag) const
1847 LASSERT(cur.text() == this, /**/);
1849 Font const & font = cur.real_current_font;
1850 FontInfo const & fontinfo = font.fontInfo();
1852 InsetCode code = NO_CODE;
1854 switch (cmd.action) {
1856 case LFUN_DEPTH_DECREMENT:
1857 enable = changeDepthAllowed(cur, DEC_DEPTH);
1860 case LFUN_DEPTH_INCREMENT:
1861 enable = changeDepthAllowed(cur, INC_DEPTH);
1865 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1868 case LFUN_BIBITEM_INSERT:
1869 enable = (cur.paragraph().layout().labeltype == LABEL_BIBLIO
1873 case LFUN_DIALOG_SHOW_NEW_INSET:
1874 if (cmd.argument() == "bibitem")
1875 code = BIBITEM_CODE;
1876 else if (cmd.argument() == "bibtex")
1878 else if (cmd.argument() == "box")
1880 else if (cmd.argument() == "branch")
1882 else if (cmd.argument() == "citation")
1884 else if (cmd.argument() == "ert")
1886 else if (cmd.argument() == "external")
1887 code = EXTERNAL_CODE;
1888 else if (cmd.argument() == "float")
1890 else if (cmd.argument() == "graphics")
1891 code = GRAPHICS_CODE;
1892 else if (cmd.argument() == "href")
1893 code = HYPERLINK_CODE;
1894 else if (cmd.argument() == "include")
1895 code = INCLUDE_CODE;
1896 else if (cmd.argument() == "index")
1898 else if (cmd.argument() == "nomenclature")
1899 code = NOMENCL_CODE;
1900 else if (cmd.argument() == "label")
1902 else if (cmd.argument() == "note")
1904 else if (cmd.argument() == "ref")
1906 else if (cmd.argument() == "space")
1908 else if (cmd.argument() == "toc")
1910 else if (cmd.argument() == "vspace")
1912 else if (cmd.argument() == "wrap")
1914 else if (cmd.argument() == "listings")
1915 code = LISTINGS_CODE;
1918 case LFUN_ERT_INSERT:
1921 case LFUN_LISTING_INSERT:
1922 code = LISTINGS_CODE;
1924 case LFUN_FOOTNOTE_INSERT:
1927 case LFUN_TABULAR_INSERT:
1928 code = TABULAR_CODE;
1930 case LFUN_MARGINALNOTE_INSERT:
1933 case LFUN_FLOAT_INSERT:
1934 case LFUN_FLOAT_WIDE_INSERT:
1937 case LFUN_WRAP_INSERT:
1940 case LFUN_FLOAT_LIST_INSERT:
1941 code = FLOAT_LIST_CODE;
1943 case LFUN_CAPTION_INSERT:
1944 code = CAPTION_CODE;
1946 case LFUN_NOTE_INSERT:
1948 // in commands (sections etc., only Notes are allowed)
1949 enable = (cmd.argument().empty() || cmd.getArg(0) == "Note" ||
1950 !cur.paragraph().layout().isCommand());
1952 case LFUN_FLEX_INSERT: {
1954 string s = cmd.getArg(0);
1956 cur.buffer().params().documentClass().insetLayout(from_utf8(s));
1957 if (il.lyxtype() != "charstyle" &&
1958 il.lyxtype() != "custom" &&
1959 il.lyxtype() != "element" &&
1960 il.lyxtype ()!= "standard")
1964 case LFUN_BOX_INSERT:
1967 case LFUN_BRANCH_INSERT:
1969 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1972 case LFUN_LABEL_INSERT:
1975 case LFUN_INFO_INSERT:
1978 case LFUN_OPTIONAL_INSERT:
1980 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1981 < cur.paragraph().layout().optionalargs;
1983 case LFUN_INDEX_INSERT:
1986 case LFUN_INDEX_PRINT:
1987 code = INDEX_PRINT_CODE;
1989 case LFUN_NOMENCL_INSERT:
1990 code = NOMENCL_CODE;
1992 case LFUN_NOMENCL_PRINT:
1993 code = NOMENCL_PRINT_CODE;
1995 case LFUN_TOC_INSERT:
1998 case LFUN_HYPERLINK_INSERT:
1999 code = HYPERLINK_CODE;
2001 case LFUN_QUOTE_INSERT:
2002 // always allow this, since we will inset a raw quote
2003 // if an inset is not allowed.
2005 case LFUN_SPECIALCHAR_INSERT:
2006 code = SPECIALCHAR_CODE;
2008 case LFUN_SPACE_INSERT:
2009 // slight hack: we know this is allowed in math mode
2014 case LFUN_INSET_MODIFY:
2015 // We need to disable this, because we may get called for a
2017 // InsetTabular::getStatus() -> InsetText::getStatus()
2018 // and we don't handle LFUN_INSET_MODIFY.
2022 case LFUN_FONT_EMPH:
2023 flag.setOnOff(fontinfo.emph() == FONT_ON);
2026 case LFUN_FONT_NOUN:
2027 flag.setOnOff(fontinfo.noun() == FONT_ON);
2030 case LFUN_FONT_BOLD:
2031 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2034 case LFUN_FONT_SANS:
2035 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2038 case LFUN_FONT_ROMAN:
2039 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2042 case LFUN_FONT_TYPEWRITER:
2043 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2048 enable = cur.selection();
2052 if (cmd.argument().empty()) {
2053 if (theClipboard().isInternal())
2054 enable = cap::numberOfSelections() > 0;
2056 enable = !theClipboard().empty();
2060 // we have an argument
2061 string const arg = to_utf8(cmd.argument());
2062 if (isStrUnsignedInt(arg)) {
2063 // it's a number and therefore means the internal stack
2064 unsigned int n = convert<unsigned int>(arg);
2065 enable = cap::numberOfSelections() > n;
2069 // explicit graphics type?
2070 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2071 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2072 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2073 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2083 case LFUN_CLIPBOARD_PASTE:
2084 enable = !theClipboard().empty();
2087 case LFUN_PRIMARY_SELECTION_PASTE:
2088 enable = cur.selection() || !theSelection().empty();
2091 case LFUN_PARAGRAPH_MOVE_UP:
2092 enable = cur.pit() > 0 && !cur.selection();
2095 case LFUN_PARAGRAPH_MOVE_DOWN:
2096 enable = cur.pit() < cur.lastpit() && !cur.selection();
2099 case LFUN_INSET_DISSOLVE:
2100 if (!cmd.argument().empty()) {
2101 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2102 enable = cur.inset().lyxCode() == FLEX_CODE
2103 && il.lyxtype() == to_utf8(cmd.argument());
2105 enable = !isMainText(cur.bv().buffer())
2106 && cur.inset().nargs() == 1;
2110 case LFUN_CHANGE_ACCEPT:
2111 case LFUN_CHANGE_REJECT:
2112 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2113 // In principle, these LFUNs should only be enabled if there
2114 // is a change at the current position/in the current selection.
2115 // However, without proper optimizations, this will inevitably
2116 // result in unacceptable performance - just imagine a user who
2117 // wants to select the complete content of a long document.
2121 case LFUN_OUTLINE_UP:
2122 case LFUN_OUTLINE_DOWN:
2123 case LFUN_OUTLINE_IN:
2124 case LFUN_OUTLINE_OUT:
2125 enable = (cur.paragraph().layout().toclevel != Layout::NOT_IN_TOC);
2128 case LFUN_NEWLINE_INSERT:
2129 // LaTeX restrictions (labels or empty par)
2130 enable = (cur.pos() > cur.paragraph().beginOfBody());
2133 case LFUN_SET_GRAPHICS_GROUP: {
2134 InsetGraphics * ins = graphics::getCurrentGraphicsInset(cur);
2138 flag.setOnOff(to_utf8(cmd.argument()) == ins->getParams().groupId);
2142 case LFUN_WORD_DELETE_FORWARD:
2143 case LFUN_WORD_DELETE_BACKWARD:
2144 case LFUN_LINE_DELETE:
2145 case LFUN_WORD_FORWARD:
2146 case LFUN_WORD_BACKWARD:
2147 case LFUN_WORD_RIGHT:
2148 case LFUN_WORD_LEFT:
2149 case LFUN_CHAR_FORWARD:
2150 case LFUN_CHAR_FORWARD_SELECT:
2151 case LFUN_CHAR_BACKWARD:
2152 case LFUN_CHAR_BACKWARD_SELECT:
2153 case LFUN_CHAR_LEFT:
2154 case LFUN_CHAR_LEFT_SELECT:
2155 case LFUN_CHAR_RIGHT:
2156 case LFUN_CHAR_RIGHT_SELECT:
2158 case LFUN_UP_SELECT:
2160 case LFUN_DOWN_SELECT:
2161 case LFUN_PARAGRAPH_UP_SELECT:
2162 case LFUN_PARAGRAPH_DOWN_SELECT:
2163 case LFUN_LINE_BEGIN_SELECT:
2164 case LFUN_LINE_END_SELECT:
2165 case LFUN_WORD_FORWARD_SELECT:
2166 case LFUN_WORD_BACKWARD_SELECT:
2167 case LFUN_WORD_RIGHT_SELECT:
2168 case LFUN_WORD_LEFT_SELECT:
2169 case LFUN_WORD_SELECT:
2170 case LFUN_PARAGRAPH_UP:
2171 case LFUN_PARAGRAPH_DOWN:
2172 case LFUN_LINE_BEGIN:
2174 case LFUN_CHAR_DELETE_FORWARD:
2175 case LFUN_CHAR_DELETE_BACKWARD:
2176 case LFUN_BREAK_PARAGRAPH:
2177 case LFUN_PARAGRAPH_SPACING:
2178 case LFUN_INSET_INSERT:
2179 case LFUN_WORD_UPCASE:
2180 case LFUN_WORD_LOWCASE:
2181 case LFUN_WORD_CAPITALIZE:
2182 case LFUN_CHARS_TRANSPOSE:
2183 case LFUN_SERVER_GET_XY:
2184 case LFUN_SERVER_SET_XY:
2185 case LFUN_SERVER_GET_LAYOUT:
2187 case LFUN_DATE_INSERT:
2188 case LFUN_SELF_INSERT:
2189 case LFUN_LINE_INSERT:
2190 case LFUN_NEWPAGE_INSERT:
2191 case LFUN_MATH_DISPLAY:
2192 case LFUN_MATH_MODE:
2193 case LFUN_MATH_MACRO:
2194 case LFUN_MATH_MATRIX:
2195 case LFUN_MATH_DELIM:
2196 case LFUN_MATH_BIGDELIM:
2197 case LFUN_MATH_INSERT:
2198 case LFUN_MATH_SUBSCRIPT:
2199 case LFUN_MATH_SUPERSCRIPT:
2200 case LFUN_FONT_DEFAULT:
2201 case LFUN_FONT_UNDERLINE:
2202 case LFUN_FONT_SIZE:
2204 case LFUN_TEXTSTYLE_APPLY:
2205 case LFUN_TEXTSTYLE_UPDATE:
2206 case LFUN_LAYOUT_PARAGRAPH:
2207 case LFUN_PARAGRAPH_UPDATE:
2208 case LFUN_ACCENT_UMLAUT:
2209 case LFUN_ACCENT_CIRCUMFLEX:
2210 case LFUN_ACCENT_GRAVE:
2211 case LFUN_ACCENT_ACUTE:
2212 case LFUN_ACCENT_TILDE:
2213 case LFUN_ACCENT_CEDILLA:
2214 case LFUN_ACCENT_MACRON:
2215 case LFUN_ACCENT_DOT:
2216 case LFUN_ACCENT_UNDERDOT:
2217 case LFUN_ACCENT_UNDERBAR:
2218 case LFUN_ACCENT_CARON:
2219 case LFUN_ACCENT_BREVE:
2220 case LFUN_ACCENT_TIE:
2221 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2222 case LFUN_ACCENT_CIRCLE:
2223 case LFUN_ACCENT_OGONEK:
2224 case LFUN_THESAURUS_ENTRY:
2225 case LFUN_PARAGRAPH_PARAMS_APPLY:
2226 case LFUN_PARAGRAPH_PARAMS:
2228 case LFUN_BUFFER_END:
2229 case LFUN_BUFFER_BEGIN:
2230 case LFUN_BUFFER_BEGIN_SELECT:
2231 case LFUN_BUFFER_END_SELECT:
2232 case LFUN_UNICODE_INSERT:
2233 // these are handled in our dispatch()
2242 && (cur.empty() || !cur.inset().insetAllowed(code)))
2245 flag.setEnabled(enable);
2250 void Text::pasteString(Cursor & cur, docstring const & clip,
2253 cur.clearSelection();
2254 if (!clip.empty()) {
2257 insertStringAsParagraphs(cur, clip);
2259 insertStringAsLines(cur, clip);