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 BOOST_ASSERT(old_pos == cur.pos());
135 cur.nextInset()->edit(cur, true);
136 // don't do that also for LFUN_MATH_MODE
137 // unless you want end up with always changing
138 // to mathrm when opening an inlined inset --
139 // I really hate "LyXfunc overloading"...
141 cur.dispatch(FuncRequest(LFUN_MATH_DISPLAY));
142 // Avoid an unnecessary undo step if cmd.argument
144 if (!cmd.argument().empty())
145 cur.dispatch(FuncRequest(LFUN_MATH_INSERT,
148 // create a macro if we see "\\newcommand"
149 // somewhere, and an ordinary formula
151 if (sel.find(from_ascii("\\newcommand")) == string::npos
152 && sel.find(from_ascii("\\newlyxcommand")) == string::npos
153 && sel.find(from_ascii("\\def")) == string::npos)
155 InsetMathHull * formula = new InsetMathHull;
156 istringstream is(to_utf8(sel));
159 formula->read(cur.buffer(), lex);
160 if (formula->getType() == hullNone)
161 // Don't create pseudo formulas if
162 // delimiters are left out
163 formula->mutate(hullSimple);
166 cur.insert(new MathMacroTemplate(sel));
169 cur.message(from_utf8(N_("Math editor mode")));
173 static void specialChar(Cursor & cur, InsetSpecialChar::Kind kind)
176 cap::replaceSelection(cur);
177 cur.insert(new InsetSpecialChar(kind));
182 static bool doInsertInset(Cursor & cur, Text * text,
183 FuncRequest const & cmd, bool edit, bool pastesel)
185 Buffer & buffer = cur.bv().buffer();
186 BufferParams const & bparams = buffer.params();
187 Inset * inset = createInset(buffer, cmd);
191 if (InsetCollapsable * ci = inset->asInsetCollapsable())
192 ci->setLayout(bparams);
195 if (cmd.action == LFUN_INDEX_INSERT) {
196 docstring ds = subst(text->getStringToIndex(cur), '\n', ' ');
197 text->insertInset(cur, inset);
199 inset->edit(cur, true);
200 // Now put this into inset
201 static_cast<InsetCollapsable *>(inset)->text_.insertStringAsParagraphs(cur, ds);
206 if (cur.selection()) {
207 lyx::dispatch(FuncRequest(LFUN_CUT));
210 text->insertInset(cur, inset);
213 inset->edit(cur, true);
215 if (!gotsel || !pastesel)
218 lyx::dispatch(FuncRequest(LFUN_PASTE, "0"));
219 InsetText * insetText = dynamic_cast<InsetText *>(inset);
220 if (insetText && !insetText->allowMultiPar() || cur.lastpit() == 0) {
221 // reset first par to default
222 LayoutPtr const layout = insetText->useEmptyLayout()
223 ? bparams.textClass().emptyLayout()
224 : bparams.textClass().defaultLayout();
225 cur.text()->paragraphs().begin()->layout(layout);
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.textClass().emptyLayoutName()
235 : bparams.textClass().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 TextClass::const_iterator lit = buf.params().textClass().begin();
272 TextClass::const_iterator const lend = buf.params().textClass().end();
274 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
285 && toclevel <= thistoclevel) {
289 ParagraphList::iterator dest = start;
290 // Move out (up) from this header
293 // Search previous same-level header above
296 toclevel = dest->layout()->toclevel;
298 && (toclevel == Layout::NOT_IN_TOC
299 || toclevel > thistoclevel));
300 // Not found; do nothing
301 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
303 pit_type const newpit = distance(bgn, dest);
304 pit_type const len = distance(start, finish);
305 pit_type const deletepit = pit + len;
306 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
307 pars.insert(dest, start, finish);
308 start = boost::next(pars.begin(), deletepit);
310 pars.erase(start, finish);
314 // Go down out of current header:
317 // Find next same-level header:
318 for (; finish != end; ++finish) {
319 toclevel = finish->layout()->toclevel;
320 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
323 ParagraphList::iterator dest = finish;
324 // Go one down from *this* header:
329 // Go further down to find header to insert in front of:
330 for (; dest != end; ++dest) {
331 toclevel = dest->layout()->toclevel;
332 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
335 // One such was found:
336 pit_type newpit = distance(bgn, dest);
337 pit_type const len = distance(start, finish);
338 buf.undo().recordUndo(cur, ATOMIC_UNDO, pit, newpit - 1);
339 pars.insert(dest, start, finish);
340 start = boost::next(bgn, pit);
342 pars.erase(start, finish);
346 buf.undo().recordUndo(cur);
347 for (; lit != lend; ++lit) {
348 if ((*lit)->toclevel == thistoclevel + 1 &&
349 start->layout()->labeltype == (*lit)->labeltype) {
350 start->layout((*lit));
356 buf.undo().recordUndo(cur);
357 for (; lit != lend; ++lit) {
358 if ((*lit)->toclevel == thistoclevel - 1 &&
359 start->layout()->labeltype == (*lit)->labeltype) {
360 start->layout((*lit));
371 void Text::number(Cursor & cur)
373 FontInfo font = ignore_font;
374 font.setNumber(FONT_TOGGLE);
375 toggleAndShow(cur, this, Font(font, ignore_language));
379 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
381 return par.isRTL(buffer.params());
385 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
387 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
389 BufferView * bv = &cur.bv();
390 TextMetrics & tm = bv->textMetrics(this);
391 if (!tm.has(cur.pit()))
392 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
394 // FIXME: We use the update flag to indicates wether a singlePar or a
395 // full screen update is needed. We reset it here but shall we restore it
399 BOOST_ASSERT(cur.text() == this);
400 CursorSlice oldTopSlice = cur.top();
401 bool oldBoundary = cur.boundary();
402 bool sel = cur.selection();
403 // Signals that, even if needsUpdate == false, an update of the
404 // cursor paragraph is required
405 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
406 LyXAction::SingleParUpdate);
407 // Signals that a full-screen update is required
408 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
409 LyXAction::NoUpdate) || singleParUpdate);
411 switch (cmd.action) {
413 case LFUN_PARAGRAPH_MOVE_DOWN: {
414 pit_type const pit = cur.pit();
415 recUndo(cur, pit, pit + 1);
417 swap(pars_[pit], pars_[pit + 1]);
418 updateLabels(cur.buffer());
424 case LFUN_PARAGRAPH_MOVE_UP: {
425 pit_type const pit = cur.pit();
426 recUndo(cur, pit - 1, pit);
428 swap(pars_[pit], pars_[pit - 1]);
429 updateLabels(cur.buffer());
435 case LFUN_APPENDIX: {
436 Paragraph & par = cur.paragraph();
437 bool start = !par.params().startOfAppendix();
439 // FIXME: The code below only makes sense at top level.
440 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
441 // ensure that we have only one start_of_appendix in this document
442 // FIXME: this don't work for multipart document!
443 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
444 if (pars_[tmp].params().startOfAppendix()) {
446 pars_[tmp].params().startOfAppendix(false);
452 par.params().startOfAppendix(start);
454 // we can set the refreshing parameters now
455 updateLabels(cur.buffer());
459 case LFUN_WORD_DELETE_FORWARD:
460 if (cur.selection()) {
461 cutSelection(cur, true, false);
463 deleteWordForward(cur);
464 finishChange(cur, false);
467 case LFUN_WORD_DELETE_BACKWARD:
468 if (cur.selection()) {
469 cutSelection(cur, true, false);
471 deleteWordBackward(cur);
472 finishChange(cur, false);
475 case LFUN_LINE_DELETE:
476 if (cur.selection()) {
477 cutSelection(cur, true, false);
479 tm.deleteLineForward(cur);
480 finishChange(cur, false);
483 case LFUN_BUFFER_BEGIN:
484 case LFUN_BUFFER_BEGIN_SELECT:
485 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
486 if (cur.depth() == 1) {
487 needsUpdate |= cursorTop(cur);
491 cur.updateFlags(Update::FitCursor);
494 case LFUN_BUFFER_END:
495 case LFUN_BUFFER_END_SELECT:
496 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
497 if (cur.depth() == 1) {
498 needsUpdate |= cursorBottom(cur);
502 cur.updateFlags(Update::FitCursor);
505 case LFUN_CHAR_FORWARD:
506 case LFUN_CHAR_FORWARD_SELECT:
507 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
508 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
509 needsUpdate |= cursorForward(cur);
511 if (!needsUpdate && oldTopSlice == cur.top()
512 && cur.boundary() == oldBoundary) {
514 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
518 case LFUN_CHAR_BACKWARD:
519 case LFUN_CHAR_BACKWARD_SELECT:
520 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
521 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
522 needsUpdate |= cursorBackward(cur);
524 if (!needsUpdate && oldTopSlice == cur.top()
525 && cur.boundary() == oldBoundary) {
527 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
532 case LFUN_CHAR_LEFT_SELECT:
533 if (lyxrc.visual_cursor) {
534 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
535 needsUpdate |= cursorVisLeft(cur);
536 if (!needsUpdate && oldTopSlice == cur.top()
537 && cur.boundary() == oldBoundary) {
539 cmd = FuncRequest(LFUN_FINISHED_LEFT);
542 if (reverseDirectionNeeded(cur)) {
543 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
544 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
546 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
547 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
554 case LFUN_CHAR_RIGHT:
555 case LFUN_CHAR_RIGHT_SELECT:
556 if (lyxrc.visual_cursor) {
557 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
558 needsUpdate |= cursorVisRight(cur);
559 if (!needsUpdate && oldTopSlice == cur.top()
560 && cur.boundary() == oldBoundary) {
562 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
565 if (reverseDirectionNeeded(cur)) {
566 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
567 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
569 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
570 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
579 case LFUN_DOWN_SELECT:
582 // stop/start the selection
583 bool select = cmd.action == LFUN_DOWN_SELECT ||
584 cmd.action == LFUN_UP_SELECT;
585 cur.selHandle(select);
587 // move cursor up/down
588 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
589 bool const successful = cur.upDownInText(up, needsUpdate);
591 // redraw if you leave mathed (for the decorations)
592 needsUpdate |= cur.beforeDispatchCursor().inMathed();
599 case LFUN_PARAGRAPH_UP:
600 case LFUN_PARAGRAPH_UP_SELECT:
601 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
602 needsUpdate |= cursorUpParagraph(cur);
605 case LFUN_PARAGRAPH_DOWN:
606 case LFUN_PARAGRAPH_DOWN_SELECT:
607 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
608 needsUpdate |= cursorDownParagraph(cur);
611 case LFUN_SCREEN_UP_SELECT:
612 needsUpdate |= cur.selHandle(true);
613 if (cur.pit() == 0 && cur.textRow().pos() == 0)
616 tm.cursorPrevious(cur);
620 case LFUN_SCREEN_DOWN_SELECT:
621 needsUpdate |= cur.selHandle(true);
622 if (cur.pit() == cur.lastpit()
623 && cur.textRow().endpos() == cur.lastpos())
630 case LFUN_LINE_BEGIN:
631 case LFUN_LINE_BEGIN_SELECT:
632 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
633 needsUpdate |= tm.cursorHome(cur);
637 case LFUN_LINE_END_SELECT:
638 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
639 needsUpdate |= tm.cursorEnd(cur);
642 case LFUN_WORD_RIGHT:
643 case LFUN_WORD_RIGHT_SELECT:
644 //FIXME: for visual cursor mode, really move 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;
655 case LFUN_WORD_FORWARD:
656 case LFUN_WORD_FORWARD_SELECT:
657 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
658 needsUpdate |= cursorForwardOneWord(cur);
662 case LFUN_WORD_LEFT_SELECT:
663 //FIXME: for visual cursor mode, really move left
664 if (reverseDirectionNeeded(cur)) {
665 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
666 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
668 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
669 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
674 case LFUN_WORD_BACKWARD:
675 case LFUN_WORD_BACKWARD_SELECT:
676 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
677 needsUpdate |= cursorBackwardOneWord(cur);
680 case LFUN_WORD_SELECT: {
681 selectWord(cur, WHOLE_WORD);
682 finishChange(cur, true);
686 case LFUN_NEW_LINE: {
687 // Not allowed by LaTeX (labels or empty par)
688 if (cur.pos() > cur.paragraph().beginOfBody()) {
689 // this avoids a double undo
690 // FIXME: should not be needed, ideally
691 if (!cur.selection())
693 cap::replaceSelection(cur);
694 cur.insert(new InsetNewline);
696 moveCursor(cur, false);
701 case LFUN_LINE_BREAK: {
702 // Not allowed by LaTeX (labels or empty par)
703 if (cur.pos() > cur.paragraph().beginOfBody()) {
704 // this avoids a double undo
705 // FIXME: should not be needed, ideally
706 if (!cur.selection())
708 cap::replaceSelection(cur);
709 cur.insert(new InsetLinebreak);
711 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);
865 insertInset(cur, inset);
871 case LFUN_INSET_DISSOLVE:
872 needsUpdate |= dissolveInset(cur);
875 case LFUN_INSET_SETTINGS:
876 cur.inset().showInsetDialog(bv);
879 case LFUN_SPACE_INSERT:
880 if (cur.paragraph().layout()->free_spacing)
881 insertChar(cur, ' ');
883 doInsertInset(cur, this, cmd, false, false);
886 moveCursor(cur, false);
889 case LFUN_SPECIALCHAR_INSERT: {
890 string const name = to_utf8(cmd.argument());
891 if (name == "hyphenation")
892 specialChar(cur, InsetSpecialChar::HYPHENATION);
893 else if (name == "ligature-break")
894 specialChar(cur, InsetSpecialChar::LIGATURE_BREAK);
895 else if (name == "slash")
896 specialChar(cur, InsetSpecialChar::SLASH);
897 else if (name == "nobreakdash")
898 specialChar(cur, InsetSpecialChar::NOBREAKDASH);
899 else if (name == "dots")
900 specialChar(cur, InsetSpecialChar::LDOTS);
901 else if (name == "end-of-sentence")
902 specialChar(cur, InsetSpecialChar::END_OF_SENTENCE);
903 else if (name == "menu-separator")
904 specialChar(cur, InsetSpecialChar::MENU_SEPARATOR);
905 else if (name.empty())
906 lyxerr << "LyX function 'specialchar-insert' needs an argument." << endl;
908 lyxerr << "Wrong argument for LyX function 'specialchar-insert'." << endl;
912 case LFUN_WORD_UPCASE:
913 changeCase(cur, text_uppercase);
916 case LFUN_WORD_LOWCASE:
917 changeCase(cur, text_lowercase);
920 case LFUN_WORD_CAPITALIZE:
921 changeCase(cur, text_capitalization);
924 case LFUN_CHARS_TRANSPOSE:
929 cur.message(_("Paste"));
930 cap::replaceSelection(cur);
933 string const arg = to_utf8(cmd.argument());
935 if (theClipboard().isInternal())
936 pasteFromStack(cur, bv->buffer().errorList("Paste"), 0);
937 else if (theClipboard().hasGraphicsContents())
938 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"));
940 pasteClipboardText(cur, bv->buffer().errorList("Paste"));
941 } else if (isStrUnsignedInt(arg)) {
942 // we have a numerical argument
943 pasteFromStack(cur, bv->buffer().errorList("Paste"),
944 convert<unsigned int>(arg));
946 Clipboard::GraphicsType type;
948 type = Clipboard::PdfGraphicsType;
949 else if (arg == "png")
950 type = Clipboard::PngGraphicsType;
951 else if (arg == "jpeg")
952 type = Clipboard::JpegGraphicsType;
953 else if (arg == "linkback")
954 type = Clipboard::LinkBackGraphicsType;
958 pasteClipboardGraphics(cur, bv->buffer().errorList("Paste"), type);
961 bv->buffer().errors("Paste");
962 cur.clearSelection(); // bug 393
968 cutSelection(cur, true, true);
969 cur.message(_("Cut"));
974 cur.message(_("Copy"));
977 case LFUN_SERVER_GET_XY:
978 cur.message(from_utf8(
979 convert<string>(tm.cursorX(cur.top(), cur.boundary()))
980 + ' ' + convert<string>(tm.cursorY(cur.top(), cur.boundary()))));
983 case LFUN_SERVER_SET_XY: {
986 istringstream is(to_utf8(cmd.argument()));
989 lyxerr << "SETXY: Could not parse coordinates in '"
990 << to_utf8(cmd.argument()) << endl;
992 tm.setCursorFromCoordinates(cur, x, y);
996 case LFUN_SERVER_GET_FONT:
997 if (cur.current_font.fontInfo().shape() == ITALIC_SHAPE)
998 cur.message(from_ascii("E"));
999 else if (cur.current_font.fontInfo().shape() == SMALLCAPS_SHAPE)
1000 cur.message(from_ascii("N"));
1002 cur.message(from_ascii("0"));
1005 case LFUN_SERVER_GET_LAYOUT:
1006 cur.message(cur.paragraph().layout()->name());
1010 docstring layout = cmd.argument();
1011 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1013 Paragraph const & para = cur.paragraph();
1014 docstring const old_layout = para.layout()->name();
1015 TextClass const & tclass = bv->buffer().params().textClass();
1018 layout = tclass.defaultLayoutName();
1020 if (para.forceEmptyLayout())
1021 // in this case only the empty layout is allowed
1022 layout = tclass.emptyLayoutName();
1023 else if (para.useEmptyLayout()) {
1024 // in this case, default layout maps to empty layout
1025 if (layout == tclass.defaultLayoutName())
1026 layout = tclass.emptyLayoutName();
1028 // otherwise, the empty layout maps to the default
1029 if (layout == tclass.emptyLayoutName())
1030 layout = tclass.defaultLayoutName();
1033 bool hasLayout = tclass.hasLayout(layout);
1035 // If the entry is obsolete, use the new one instead.
1037 docstring const & obs = tclass[layout]->obsoleted_by();
1043 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1044 from_utf8(N_(" not known")));
1048 bool change_layout = (old_layout != layout);
1050 if (!change_layout && cur.selection() &&
1051 cur.selBegin().pit() != cur.selEnd().pit())
1053 pit_type spit = cur.selBegin().pit();
1054 pit_type epit = cur.selEnd().pit() + 1;
1055 while (spit != epit) {
1056 if (pars_[spit].layout()->name() != old_layout) {
1057 change_layout = true;
1065 setLayout(cur, layout);
1070 case LFUN_CLIPBOARD_PASTE:
1071 cur.clearSelection();
1072 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1073 cmd.argument() == "paragraph");
1074 bv->buffer().errors("Paste");
1077 case LFUN_PRIMARY_SELECTION_PASTE:
1078 pasteString(cur, theSelection().get(),
1079 cmd.argument() == "paragraph");
1082 case LFUN_UNICODE_INSERT: {
1083 if (cmd.argument().empty())
1085 docstring hexstring = cmd.argument();
1086 if (isHex(hexstring)) {
1087 char_type c = hexToInt(hexstring);
1088 if (c >= 32 && c < 0x10ffff) {
1089 lyxerr << "Inserting c: " << c << endl;
1090 docstring s = docstring(1, c);
1091 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1097 case LFUN_QUOTE_INSERT: {
1098 Paragraph & par = cur.paragraph();
1099 pos_type pos = cur.pos();
1100 BufferParams const & bufparams = bv->buffer().params();
1101 LayoutPtr const & style = par.layout();
1102 if (!style->pass_thru
1103 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1104 // this avoids a double undo
1105 // FIXME: should not be needed, ideally
1106 if (!cur.selection())
1108 cap::replaceSelection(cur);
1113 else if (cur.prevInset() && cur.prevInset()->isSpace())
1116 c = par.getChar(pos - 1);
1117 string arg = to_utf8(cmd.argument());
1118 if (arg == "single")
1119 cur.insert(new InsetQuotes(c,
1120 bufparams.quotes_language,
1121 InsetQuotes::SingleQ));
1123 cur.insert(new InsetQuotes(c,
1124 bufparams.quotes_language,
1125 InsetQuotes::DoubleQ));
1129 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1133 case LFUN_DATE_INSERT: {
1134 string const format = cmd.argument().empty()
1135 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1136 string const time = formatted_time(current_time(), format);
1137 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1141 case LFUN_MOUSE_TRIPLE:
1142 if (cmd.button() == mouse_button::button1) {
1151 case LFUN_MOUSE_DOUBLE:
1152 if (cmd.button() == mouse_button::button1) {
1153 selectWord(cur, WHOLE_WORD_STRICT);
1158 // Single-click on work area
1159 case LFUN_MOUSE_PRESS: {
1160 // Right click on a footnote flag opens float menu
1161 // FIXME: Why should we clear the selection in this case?
1162 if (cmd.button() == mouse_button::button3)
1163 cur.clearSelection();
1165 bool do_selection = cmd.button() == mouse_button::button1
1166 && cmd.argument() == "region-select";
1168 bool update = bv->mouseSetCursor(cur, do_selection);
1170 // Insert primary selection with middle mouse
1171 // if there is a local selection in the current buffer,
1173 if (cmd.button() == mouse_button::button2) {
1174 if (cap::selection()) {
1175 // Copy the selection buffer to the clipboard
1176 // stack, because we want it to appear in the
1177 // "Edit->Paste recent" menu.
1178 cap::copySelectionToStack();
1180 cap::pasteSelection(bv->cursor(),
1181 bv->buffer().errorList("Paste"));
1182 bv->buffer().errors("Paste");
1183 bv->buffer().markDirty();
1184 bv->cursor().finishUndo();
1186 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1190 // we have to update after dEPM triggered
1191 if (!update && cmd.button() == mouse_button::button1) {
1192 needsUpdate = false;
1199 case LFUN_MOUSE_MOTION: {
1200 // Only use motion with button 1
1201 //if (cmd.button() != mouse_button::button1)
1204 // ignore motions deeper nested than the real anchor
1205 Cursor & bvcur = cur.bv().cursor();
1206 if (!bvcur.anchor_.hasPart(cur)) {
1210 CursorSlice old = bvcur.top();
1212 int const wh = bv->workHeight();
1213 int const y = max(0, min(wh - 1, cmd.y));
1215 tm.setCursorFromCoordinates(cur, cmd.x, y);
1216 cur.setTargetX(cmd.x);
1218 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1220 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1221 // This is to allow jumping over large insets
1222 if (cur.top() == old) {
1224 lyx::dispatch(FuncRequest(LFUN_DOWN_SELECT));
1226 lyx::dispatch(FuncRequest(LFUN_UP_SELECT));
1229 if (cur.top() == old)
1232 // FIXME: This is brute force! But without it the selected
1233 // area is not corrected updated while moving the mouse.
1234 cur.updateFlags(Update::Force | Update::FitCursor);
1235 // don't set anchor_
1236 bvcur.setCursor(cur);
1237 bvcur.selection() = true;
1238 //lyxerr << "MOTION: " << bv->cursor() << endl;
1243 case LFUN_MOUSE_RELEASE: {
1244 if (cmd.button() == mouse_button::button2)
1247 if (cmd.button() == mouse_button::button1) {
1248 // if there is new selection, update persistent
1249 // selection, otherwise, single click does not
1250 // clear persistent selection buffer
1251 if (cur.selection()) {
1253 // if double click, cur is moved to the end of word by selectWord
1254 // but bvcur is current mouse position
1255 Cursor & bvcur = cur.bv().cursor();
1256 bvcur.selection() = true;
1258 needsUpdate = false;
1265 case LFUN_SELF_INSERT: {
1266 if (cmd.argument().empty())
1269 // Automatically delete the currently selected
1270 // text and replace it with what is being
1271 // typed in now. Depends on lyxrc settings
1272 // "auto_region_delete", which defaults to
1275 if (lyxrc.auto_region_delete && cur.selection())
1276 cutSelection(cur, false, false);
1278 cur.clearSelection();
1279 Font const old_font = cur.real_current_font;
1281 docstring::const_iterator cit = cmd.argument().begin();
1282 docstring::const_iterator end = cmd.argument().end();
1283 for (; cit != end; ++cit)
1284 bv->translateAndInsert(*cit, this, cur);
1287 moveCursor(cur, false);
1291 case LFUN_HYPERLINK_INSERT: {
1292 InsetCommandParams p(HYPERLINK_CODE);
1294 if (cur.selection()) {
1295 content = cur.selectionAsString(false);
1296 cutSelection(cur, true, false);
1298 p["target"] = (cmd.argument().empty()) ?
1299 content : cmd.argument();
1300 string const data = InsetCommandMailer::params2string("href", p);
1301 if (p["target"].empty()) {
1302 bv->showDialog("href", data);
1304 FuncRequest fr(LFUN_INSET_INSERT, data);
1310 case LFUN_LABEL_INSERT: {
1311 InsetCommandParams p(LABEL_CODE);
1312 // Try to generate a valid label
1313 p["name"] = (cmd.argument().empty()) ?
1314 cur.getPossibleLabel() :
1316 string const data = InsetCommandMailer::params2string("label", p);
1318 if (cmd.argument().empty()) {
1319 bv->showDialog("label", data);
1321 FuncRequest fr(LFUN_INSET_INSERT, data);
1327 case LFUN_INFO_INSERT: {
1328 Inset * inset = createInset(cur.bv().buffer(), cmd);
1331 // if an empty inset is created (cmd.argument() is empty)
1332 // use current selection as parameter.
1333 if (cmd.argument().empty() && cur.selection()) {
1334 // use selected text as info to avoid a separate UI
1335 docstring ds = cur.selectionAsString(false);
1336 cutSelection(cur, true, false);
1337 static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1338 static_cast<InsetInfo *>(inset)->updateInfo(cur.bv().buffer());
1340 insertInset(cur, inset);
1345 case LFUN_LIST_INSERT:
1346 case LFUN_THEOREM_INSERT:
1348 case LFUN_CAPTION_INSERT:
1349 case LFUN_FOOTNOTE_INSERT:
1350 case LFUN_NOTE_INSERT:
1351 case LFUN_FLEX_INSERT:
1352 case LFUN_BOX_INSERT:
1353 case LFUN_BRANCH_INSERT:
1354 case LFUN_BIBITEM_INSERT:
1355 case LFUN_ERT_INSERT:
1356 case LFUN_LISTING_INSERT:
1357 case LFUN_MARGINALNOTE_INSERT:
1358 case LFUN_OPTIONAL_INSERT:
1359 case LFUN_ENVIRONMENT_INSERT:
1360 case LFUN_INDEX_INSERT:
1361 // Open the inset, and move the current selection
1363 doInsertInset(cur, this, cmd, true, true);
1365 // Some insets are numbered, others are shown in the outline pane so
1366 // let's update the labels and the toc backend.
1367 updateLabels(bv->buffer());
1370 case LFUN_TABULAR_INSERT:
1371 // if there were no arguments, just open the dialog
1372 if (doInsertInset(cur, this, cmd, false, true))
1375 bv->showDialog("tabularcreate");
1379 case LFUN_FLOAT_INSERT:
1380 case LFUN_FLOAT_WIDE_INSERT:
1381 case LFUN_WRAP_INSERT: {
1382 bool content = cur.selection(); // will some text be moved into the inset?
1384 doInsertInset(cur, this, cmd, true, true);
1386 ParagraphList & pars = cur.text()->paragraphs();
1388 TextClass const & tclass = bv->buffer().params().textClass();
1390 // add a separate paragraph for the caption inset
1391 pars.push_back(Paragraph());
1392 pars.back().setInsetOwner(pars[0].inInset());
1393 if (pars.back().useEmptyLayout())
1394 pars.back().layout(tclass.emptyLayout());
1396 pars.back().layout(tclass.defaultLayout());
1398 int cap_pit = pars.size() - 1;
1400 // if an empty inset was created, we create an additional empty
1401 // paragraph at the bottom so that the user can choose where to put
1402 // the graphics (or table).
1404 pars.push_back(Paragraph());
1405 pars.back().setInsetOwner(pars[0].inInset());
1406 if (pars.back().useEmptyLayout())
1407 pars.back().layout(tclass.emptyLayout());
1409 pars.back().layout(tclass.defaultLayout());
1412 // reposition the cursor to the caption
1413 cur.pit() = cap_pit;
1415 // FIXME: This Text/Cursor dispatch handling is a mess!
1416 // We cannot use Cursor::dispatch here it needs access to up to
1418 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1419 cur.text()->dispatch(cur, cmd_caption);
1420 cur.updateFlags(Update::Force);
1421 // FIXME: When leaving the Float (or Wrap) inset we should
1422 // delete any empty paragraph left above or below the
1427 case LFUN_NOMENCL_INSERT: {
1428 FuncRequest cmd1 = cmd;
1429 if (cmd.argument().empty())
1430 cmd1 = FuncRequest(cmd,
1431 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1432 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1436 cur.clearSelection();
1437 insertInset(cur, inset);
1438 // Show the dialog for the nomenclature entry, since the
1439 // description entry still needs to be filled in.
1440 if (cmd.action == LFUN_NOMENCL_INSERT)
1441 inset->edit(cur, true);
1446 case LFUN_INDEX_PRINT:
1447 case LFUN_NOMENCL_PRINT:
1448 case LFUN_TOC_INSERT:
1449 case LFUN_HFILL_INSERT:
1450 case LFUN_LINE_INSERT:
1451 case LFUN_NEWPAGE_INSERT:
1452 case LFUN_PAGEBREAK_INSERT:
1453 case LFUN_CLEARPAGE_INSERT:
1454 case LFUN_CLEARDOUBLEPAGE_INSERT:
1456 doInsertInset(cur, this, cmd, false, false);
1460 case LFUN_DEPTH_DECREMENT:
1461 changeDepth(cur, DEC_DEPTH);
1464 case LFUN_DEPTH_INCREMENT:
1465 changeDepth(cur, INC_DEPTH);
1468 case LFUN_MATH_DISPLAY:
1469 mathDispatch(cur, cmd, true);
1472 case LFUN_MATH_IMPORT_SELECTION:
1473 case LFUN_MATH_MODE:
1474 if (cmd.argument() == "on")
1475 // don't pass "on" as argument
1476 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1478 mathDispatch(cur, cmd, false);
1481 case LFUN_MATH_MACRO:
1482 if (cmd.argument().empty())
1483 cur.errorMessage(from_utf8(N_("Missing argument")));
1485 string s = to_utf8(cmd.argument());
1486 string const s1 = token(s, ' ', 1);
1487 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1488 string const s2 = token(s, ' ', 2);
1489 MacroType type = MacroTypeNewcommand;
1491 type = MacroTypeDef;
1492 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1493 //cur.nextInset()->edit(cur, true);
1497 // passthrough hat and underscore outside mathed:
1498 case LFUN_MATH_SUBSCRIPT:
1499 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1501 case LFUN_MATH_SUPERSCRIPT:
1502 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1505 case LFUN_MATH_INSERT:
1506 case LFUN_MATH_MATRIX:
1507 case LFUN_MATH_DELIM:
1508 case LFUN_MATH_BIGDELIM: {
1509 cap::replaceSelection(cur);
1510 cur.insert(new InsetMathHull(hullSimple));
1511 checkAndActivateInset(cur, true);
1512 BOOST_ASSERT(cur.inMathed());
1517 case LFUN_FONT_EMPH: {
1518 Font font(ignore_font, ignore_language);
1519 font.fontInfo().setEmph(FONT_TOGGLE);
1520 toggleAndShow(cur, this, font);
1524 case LFUN_FONT_BOLD: {
1525 Font font(ignore_font, ignore_language);
1526 font.fontInfo().setSeries(BOLD_SERIES);
1527 toggleAndShow(cur, this, font);
1531 case LFUN_FONT_NOUN: {
1532 Font font(ignore_font, ignore_language);
1533 font.fontInfo().setNoun(FONT_TOGGLE);
1534 toggleAndShow(cur, this, font);
1538 case LFUN_FONT_TYPEWRITER: {
1539 Font font(ignore_font, ignore_language);
1540 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1541 toggleAndShow(cur, this, font);
1545 case LFUN_FONT_SANS: {
1546 Font font(ignore_font, ignore_language);
1547 font.fontInfo().setFamily(SANS_FAMILY);
1548 toggleAndShow(cur, this, font);
1552 case LFUN_FONT_ROMAN: {
1553 Font font(ignore_font, ignore_language);
1554 font.fontInfo().setFamily(ROMAN_FAMILY);
1555 toggleAndShow(cur, this, font);
1559 case LFUN_FONT_DEFAULT: {
1560 Font font(inherit_font, ignore_language);
1561 toggleAndShow(cur, this, font);
1565 case LFUN_FONT_UNDERLINE: {
1566 Font font(ignore_font, ignore_language);
1567 font.fontInfo().setUnderbar(FONT_TOGGLE);
1568 toggleAndShow(cur, this, font);
1572 case LFUN_FONT_SIZE: {
1573 Font font(ignore_font, ignore_language);
1574 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1575 toggleAndShow(cur, this, font);
1579 case LFUN_LANGUAGE: {
1580 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1583 Font font(ignore_font, lang);
1584 toggleAndShow(cur, this, font);
1588 case LFUN_FONT_FREE_APPLY:
1589 toggleAndShow(cur, this, freefont, toggleall);
1590 cur.message(_("Character set"));
1593 // Set the freefont using the contents of \param data dispatched from
1594 // the frontends and apply it at the current cursor location.
1595 case LFUN_FONT_FREE_UPDATE: {
1598 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1601 toggleAndShow(cur, this, freefont, toggleall);
1602 cur.message(_("Character set"));
1604 lyxerr << "Argument not ok";
1609 case LFUN_FINISHED_LEFT:
1610 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1611 // We're leaving an inset, going left. If the inset is LTR, we're
1612 // leaving from the front, so we should not move (remain at --- but
1613 // not in --- the inset). If the inset is RTL, move left, without
1614 // entering the inset itself; i.e., move to after the inset.
1615 if (cur.paragraph().getFontSettings(
1616 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1617 cursorVisLeft(cur, true);
1620 case LFUN_FINISHED_RIGHT:
1621 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1622 // We're leaving an inset, going right. If the inset is RTL, we're
1623 // leaving from the front, so we should not move (remain at --- but
1624 // not in --- the inset). If the inset is LTR, move right, without
1625 // entering the inset itself; i.e., move to after the inset.
1626 if (!cur.paragraph().getFontSettings(
1627 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1628 cursorVisRight(cur, true);
1631 case LFUN_FINISHED_BACKWARD:
1632 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1635 case LFUN_FINISHED_FORWARD:
1636 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1638 cur.setCurrentFont();
1641 case LFUN_LAYOUT_PARAGRAPH: {
1643 params2string(cur.paragraph(), data);
1644 data = "show\n" + data;
1645 bv->showDialog("paragraph", data);
1649 case LFUN_PARAGRAPH_UPDATE: {
1651 params2string(cur.paragraph(), data);
1653 // Will the paragraph accept changes from the dialog?
1655 cur.inset().allowParagraphCustomization(cur.idx());
1657 data = "update " + convert<string>(accept) + '\n' + data;
1658 bv->updateDialog("paragraph", data);
1662 case LFUN_ACCENT_UMLAUT:
1663 case LFUN_ACCENT_CIRCUMFLEX:
1664 case LFUN_ACCENT_GRAVE:
1665 case LFUN_ACCENT_ACUTE:
1666 case LFUN_ACCENT_TILDE:
1667 case LFUN_ACCENT_CEDILLA:
1668 case LFUN_ACCENT_MACRON:
1669 case LFUN_ACCENT_DOT:
1670 case LFUN_ACCENT_UNDERDOT:
1671 case LFUN_ACCENT_UNDERBAR:
1672 case LFUN_ACCENT_CARON:
1673 case LFUN_ACCENT_SPECIAL_CARON:
1674 case LFUN_ACCENT_BREVE:
1675 case LFUN_ACCENT_TIE:
1676 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1677 case LFUN_ACCENT_CIRCLE:
1678 case LFUN_ACCENT_OGONEK:
1679 theLyXFunc().handleKeyFunc(cmd.action);
1680 if (!cmd.argument().empty())
1681 // FIXME: Are all these characters encoded in one byte in utf8?
1682 bv->translateAndInsert(cmd.argument()[0], this, cur);
1685 case LFUN_FLOAT_LIST: {
1686 TextClass const & tclass = bv->buffer().params().textClass();
1687 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1689 if (cur.selection())
1690 cutSelection(cur, true, false);
1691 breakParagraph(cur);
1693 if (cur.lastpos() != 0) {
1694 cursorBackward(cur);
1695 breakParagraph(cur);
1698 //FIXME Check if this should be emptyLayout()
1699 setLayout(cur, tclass.defaultLayoutName());
1700 ParagraphParameters p;
1701 setParagraphs(cur, p);
1702 insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1705 lyxerr << "Non-existent float type: "
1706 << to_utf8(cmd.argument()) << endl;
1711 case LFUN_CHANGE_ACCEPT: {
1712 acceptOrRejectChanges(cur, ACCEPT);
1716 case LFUN_CHANGE_REJECT: {
1717 acceptOrRejectChanges(cur, REJECT);
1721 case LFUN_THESAURUS_ENTRY: {
1722 docstring arg = cmd.argument();
1724 arg = cur.selectionAsString(false);
1726 if (arg.size() > 100 || arg.empty()) {
1727 // Get word or selection
1728 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1729 arg = cur.selectionAsString(false);
1732 bv->showDialog("thesaurus", to_utf8(arg));
1736 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1737 // Given data, an encoding of the ParagraphParameters
1738 // generated in the Paragraph dialog, this function sets
1739 // the current paragraph, or currently selected paragraphs,
1741 // NOTE: This function overrides all existing settings.
1742 setParagraphs(cur, cmd.argument());
1743 cur.message(_("Paragraph layout set"));
1747 case LFUN_PARAGRAPH_PARAMS: {
1748 // Given data, an encoding of the ParagraphParameters as we'd
1749 // find them in a LyX file, this function modifies the current paragraph,
1750 // or currently selected paragraphs.
1751 // NOTE: This function only modifies, and does not override, existing
1753 setParagraphs(cur, cmd.argument(), true);
1754 cur.message(_("Paragraph layout set"));
1759 if (cur.selection()) {
1760 cur.selection() = false;
1763 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1764 // correct, but I'm not 100% sure -- dov, 071019
1765 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1769 case LFUN_OUTLINE_UP:
1770 outline(OutlineUp, cur);
1771 setCursor(cur, cur.pit(), 0);
1772 updateLabels(cur.buffer());
1776 case LFUN_OUTLINE_DOWN:
1777 outline(OutlineDown, cur);
1778 setCursor(cur, cur.pit(), 0);
1779 updateLabels(cur.buffer());
1783 case LFUN_OUTLINE_IN:
1784 outline(OutlineIn, cur);
1785 updateLabels(cur.buffer());
1789 case LFUN_OUTLINE_OUT:
1790 outline(OutlineOut, cur);
1791 updateLabels(cur.buffer());
1796 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1801 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1803 // FIXME: The cursor flag is reset two lines below
1804 // so we need to check here if some of the LFUN did touch that.
1805 // for now only Text::erase() and Text::backspace() do that.
1806 // The plan is to verify all the LFUNs and then to remove this
1807 // singleParUpdate boolean altogether.
1808 if (cur.result().update() & Update::Force) {
1809 singleParUpdate = false;
1813 // FIXME: the following code should go in favor of fine grained
1814 // update flag treatment.
1815 if (singleParUpdate) {
1816 // Inserting characters does not change par height in general. So, try
1817 // to update _only_ this paragraph. BufferView will detect if a full
1818 // metrics update is needed anyway.
1819 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1824 && &oldTopSlice.inset() == &cur.inset()
1825 && oldTopSlice.idx() == cur.idx()
1826 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1827 && !cur.selection())
1828 // FIXME: it would be better if we could just do this
1830 //if (cur.result().update() != Update::FitCursor)
1833 // But some LFUNs do not set Update::FitCursor when needed, so we
1834 // do it for all. This is not very harmfull as FitCursor will provoke
1835 // a full redraw only if needed but still, a proper review of all LFUN
1836 // should be done and this needsUpdate boolean can then be removed.
1837 cur.updateFlags(Update::FitCursor);
1839 cur.updateFlags(Update::Force | Update::FitCursor);
1843 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1844 FuncStatus & flag) const
1846 BOOST_ASSERT(cur.text() == this);
1848 Font const & font = cur.real_current_font;
1849 FontInfo const & fontinfo = font.fontInfo();
1851 InsetCode code = NO_CODE;
1853 switch (cmd.action) {
1855 case LFUN_DEPTH_DECREMENT:
1856 enable = changeDepthAllowed(cur, DEC_DEPTH);
1859 case LFUN_DEPTH_INCREMENT:
1860 enable = changeDepthAllowed(cur, INC_DEPTH);
1864 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1867 case LFUN_BIBITEM_INSERT:
1868 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1872 case LFUN_DIALOG_SHOW_NEW_INSET:
1873 if (cmd.argument() == "bibitem")
1874 code = BIBITEM_CODE;
1875 else if (cmd.argument() == "bibtex")
1877 else if (cmd.argument() == "box")
1879 else if (cmd.argument() == "branch")
1881 else if (cmd.argument() == "citation")
1883 else if (cmd.argument() == "ert")
1885 else if (cmd.argument() == "external")
1886 code = EXTERNAL_CODE;
1887 else if (cmd.argument() == "float")
1889 else if (cmd.argument() == "graphics")
1890 code = GRAPHICS_CODE;
1891 else if (cmd.argument() == "href")
1892 code = HYPERLINK_CODE;
1893 else if (cmd.argument() == "include")
1894 code = INCLUDE_CODE;
1895 else if (cmd.argument() == "index")
1897 else if (cmd.argument() == "nomenclature")
1898 code = NOMENCL_CODE;
1899 else if (cmd.argument() == "label")
1901 else if (cmd.argument() == "note")
1903 else if (cmd.argument() == "ref")
1905 else if (cmd.argument() == "toc")
1907 else if (cmd.argument() == "vspace")
1909 else if (cmd.argument() == "wrap")
1911 else if (cmd.argument() == "listings")
1912 code = LISTINGS_CODE;
1915 case LFUN_ERT_INSERT:
1918 case LFUN_LISTING_INSERT:
1919 code = LISTINGS_CODE;
1921 case LFUN_FOOTNOTE_INSERT:
1924 case LFUN_TABULAR_INSERT:
1925 code = TABULAR_CODE;
1927 case LFUN_MARGINALNOTE_INSERT:
1930 case LFUN_FLOAT_INSERT:
1931 case LFUN_FLOAT_WIDE_INSERT:
1934 case LFUN_WRAP_INSERT:
1937 case LFUN_FLOAT_LIST:
1938 code = FLOAT_LIST_CODE;
1941 case LFUN_LIST_INSERT:
1944 case LFUN_THEOREM_INSERT:
1945 code = THEOREM_CODE;
1948 case LFUN_CAPTION_INSERT:
1949 code = CAPTION_CODE;
1951 case LFUN_NOTE_INSERT:
1954 case LFUN_FLEX_INSERT: {
1956 string s = cmd.getArg(0);
1957 InsetLayout il = cur.buffer().params().textClass().insetlayout(from_utf8(s));
1958 if (il.lyxtype() != "charstyle" &&
1959 il.lyxtype() != "custom" &&
1960 il.lyxtype() != "element" &&
1961 il.lyxtype ()!= "standard")
1965 case LFUN_BOX_INSERT:
1968 case LFUN_BRANCH_INSERT:
1970 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1973 case LFUN_LABEL_INSERT:
1976 case LFUN_INFO_INSERT:
1979 case LFUN_OPTIONAL_INSERT:
1981 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1982 < cur.paragraph().layout()->optionalargs;
1984 case LFUN_ENVIRONMENT_INSERT:
1987 case LFUN_INDEX_INSERT:
1990 case LFUN_INDEX_PRINT:
1991 code = INDEX_PRINT_CODE;
1993 case LFUN_NOMENCL_INSERT:
1994 code = NOMENCL_CODE;
1996 case LFUN_NOMENCL_PRINT:
1997 code = NOMENCL_PRINT_CODE;
1999 case LFUN_TOC_INSERT:
2002 case LFUN_HYPERLINK_INSERT:
2003 code = HYPERLINK_CODE;
2005 case LFUN_QUOTE_INSERT:
2006 // always allow this, since we will inset a raw quote
2007 // if an inset is not allowed.
2009 case LFUN_HFILL_INSERT:
2010 case LFUN_SPECIALCHAR_INSERT:
2011 code = SPECIALCHAR_CODE;
2013 case LFUN_SPACE_INSERT:
2014 // slight hack: we know this is allowed in math mode
2019 case LFUN_INSET_MODIFY:
2020 // We need to disable this, because we may get called for a
2022 // InsetTabular::getStatus() -> InsetText::getStatus()
2023 // and we don't handle LFUN_INSET_MODIFY.
2027 case LFUN_FONT_EMPH:
2028 flag.setOnOff(fontinfo.emph() == FONT_ON);
2031 case LFUN_FONT_NOUN:
2032 flag.setOnOff(fontinfo.noun() == FONT_ON);
2035 case LFUN_FONT_BOLD:
2036 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2039 case LFUN_FONT_SANS:
2040 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2043 case LFUN_FONT_ROMAN:
2044 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2047 case LFUN_FONT_TYPEWRITER:
2048 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2053 enable = cur.selection();
2057 if (cmd.argument().empty()) {
2058 if (theClipboard().isInternal())
2059 enable = cap::numberOfSelections() > 0;
2061 enable = !theClipboard().empty();
2065 // we have an argument
2066 string const arg = to_utf8(cmd.argument());
2067 if (isStrUnsignedInt(arg)) {
2068 // it's a number and therefore means the internal stack
2069 unsigned int n = convert<unsigned int>(arg);
2070 enable = cap::numberOfSelections() > n;
2074 // explicit graphics type?
2075 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2076 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2077 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2078 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2088 case LFUN_CLIPBOARD_PASTE:
2089 enable = !theClipboard().empty();
2092 case LFUN_PRIMARY_SELECTION_PASTE:
2093 enable = cur.selection() || !theSelection().empty();
2096 case LFUN_PARAGRAPH_MOVE_UP:
2097 enable = cur.pit() > 0 && !cur.selection();
2100 case LFUN_PARAGRAPH_MOVE_DOWN:
2101 enable = cur.pit() < cur.lastpit() && !cur.selection();
2104 case LFUN_INSET_DISSOLVE:
2105 if (!cmd.argument().empty()) {
2106 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2107 enable = cur.inset().lyxCode() == FLEX_CODE
2108 && il.lyxtype() == to_utf8(cmd.argument());
2110 enable = !isMainText(cur.bv().buffer())
2111 && cur.inset().nargs() == 1;
2115 case LFUN_CHANGE_ACCEPT:
2116 case LFUN_CHANGE_REJECT:
2117 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2118 // In principle, these LFUNs should only be enabled if there
2119 // is a change at the current position/in the current selection.
2120 // However, without proper optimizations, this will inevitably
2121 // result in unacceptable performance - just imagine a user who
2122 // wants to select the complete content of a long document.
2126 case LFUN_OUTLINE_UP:
2127 case LFUN_OUTLINE_DOWN:
2128 case LFUN_OUTLINE_IN:
2129 case LFUN_OUTLINE_OUT:
2130 enable = (cur.paragraph().layout()->toclevel != Layout::NOT_IN_TOC);
2133 case LFUN_WORD_DELETE_FORWARD:
2134 case LFUN_WORD_DELETE_BACKWARD:
2135 case LFUN_LINE_DELETE:
2136 case LFUN_WORD_FORWARD:
2137 case LFUN_WORD_BACKWARD:
2138 case LFUN_WORD_RIGHT:
2139 case LFUN_WORD_LEFT:
2140 case LFUN_CHAR_FORWARD:
2141 case LFUN_CHAR_FORWARD_SELECT:
2142 case LFUN_CHAR_BACKWARD:
2143 case LFUN_CHAR_BACKWARD_SELECT:
2144 case LFUN_CHAR_LEFT:
2145 case LFUN_CHAR_LEFT_SELECT:
2146 case LFUN_CHAR_RIGHT:
2147 case LFUN_CHAR_RIGHT_SELECT:
2149 case LFUN_UP_SELECT:
2151 case LFUN_DOWN_SELECT:
2152 case LFUN_PARAGRAPH_UP_SELECT:
2153 case LFUN_PARAGRAPH_DOWN_SELECT:
2154 case LFUN_SCREEN_UP_SELECT:
2155 case LFUN_SCREEN_DOWN_SELECT:
2156 case LFUN_LINE_BEGIN_SELECT:
2157 case LFUN_LINE_END_SELECT:
2158 case LFUN_WORD_FORWARD_SELECT:
2159 case LFUN_WORD_BACKWARD_SELECT:
2160 case LFUN_WORD_RIGHT_SELECT:
2161 case LFUN_WORD_LEFT_SELECT:
2162 case LFUN_WORD_SELECT:
2163 case LFUN_PARAGRAPH_UP:
2164 case LFUN_PARAGRAPH_DOWN:
2165 case LFUN_LINE_BEGIN:
2166 case LFUN_LINE_BREAK:
2169 case LFUN_CHAR_DELETE_FORWARD:
2170 case LFUN_DELETE_FORWARD_SKIP:
2171 case LFUN_CHAR_DELETE_BACKWARD:
2172 case LFUN_DELETE_BACKWARD_SKIP:
2173 case LFUN_BREAK_PARAGRAPH:
2174 case LFUN_BREAK_PARAGRAPH_SKIP:
2175 case LFUN_PARAGRAPH_SPACING:
2176 case LFUN_INSET_INSERT:
2177 case LFUN_WORD_UPCASE:
2178 case LFUN_WORD_LOWCASE:
2179 case LFUN_WORD_CAPITALIZE:
2180 case LFUN_CHARS_TRANSPOSE:
2181 case LFUN_SERVER_GET_XY:
2182 case LFUN_SERVER_SET_XY:
2183 case LFUN_SERVER_GET_FONT:
2184 case LFUN_SERVER_GET_LAYOUT:
2186 case LFUN_DATE_INSERT:
2187 case LFUN_SELF_INSERT:
2188 case LFUN_LINE_INSERT:
2189 case LFUN_NEWPAGE_INSERT:
2190 case LFUN_PAGEBREAK_INSERT:
2191 case LFUN_CLEARPAGE_INSERT:
2192 case LFUN_CLEARDOUBLEPAGE_INSERT:
2193 case LFUN_MATH_DISPLAY:
2194 case LFUN_MATH_IMPORT_SELECTION:
2195 case LFUN_MATH_MODE:
2196 case LFUN_MATH_MACRO:
2197 case LFUN_MATH_MATRIX:
2198 case LFUN_MATH_DELIM:
2199 case LFUN_MATH_BIGDELIM:
2200 case LFUN_MATH_INSERT:
2201 case LFUN_MATH_SUBSCRIPT:
2202 case LFUN_MATH_SUPERSCRIPT:
2203 case LFUN_FONT_DEFAULT:
2204 case LFUN_FONT_UNDERLINE:
2205 case LFUN_FONT_SIZE:
2207 case LFUN_FONT_FREE_APPLY:
2208 case LFUN_FONT_FREE_UPDATE:
2209 case LFUN_LAYOUT_PARAGRAPH:
2210 case LFUN_PARAGRAPH_UPDATE:
2211 case LFUN_ACCENT_UMLAUT:
2212 case LFUN_ACCENT_CIRCUMFLEX:
2213 case LFUN_ACCENT_GRAVE:
2214 case LFUN_ACCENT_ACUTE:
2215 case LFUN_ACCENT_TILDE:
2216 case LFUN_ACCENT_CEDILLA:
2217 case LFUN_ACCENT_MACRON:
2218 case LFUN_ACCENT_DOT:
2219 case LFUN_ACCENT_UNDERDOT:
2220 case LFUN_ACCENT_UNDERBAR:
2221 case LFUN_ACCENT_CARON:
2222 case LFUN_ACCENT_SPECIAL_CARON:
2223 case LFUN_ACCENT_BREVE:
2224 case LFUN_ACCENT_TIE:
2225 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2226 case LFUN_ACCENT_CIRCLE:
2227 case LFUN_ACCENT_OGONEK:
2228 case LFUN_THESAURUS_ENTRY:
2229 case LFUN_PARAGRAPH_PARAMS_APPLY:
2230 case LFUN_PARAGRAPH_PARAMS:
2232 case LFUN_BUFFER_END:
2233 case LFUN_BUFFER_BEGIN:
2234 case LFUN_BUFFER_BEGIN_SELECT:
2235 case LFUN_BUFFER_END_SELECT:
2236 case LFUN_UNICODE_INSERT:
2237 // these are handled in our dispatch()
2246 && (cur.empty() || !cur.inset().insetAllowed(code)))
2249 flag.enabled(enable);
2254 void Text::pasteString(Cursor & cur, docstring const & clip,
2257 cur.clearSelection();
2258 if (!clip.empty()) {
2261 insertStringAsParagraphs(cur, clip);
2263 insertStringAsLines(cur, clip);