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 cur.text()->paragraphs().begin()
223 ->setEmptyOrDefaultLayout(bparams.textClass());
226 // Merge multiple paragraphs -- hack
227 while (cur.lastpit() > 0)
228 mergeParagraph(bparams, cur.text()->paragraphs(), 0);
230 // reset surrounding par to default
231 docstring const layoutname = insetText->useEmptyLayout()
232 ? bparams.textClass().emptyLayoutName()
233 : bparams.textClass().defaultLayoutName();
234 cur.leaveInset(*inset);
235 text->setLayout(cur, layoutname);
242 string const freefont2string()
244 return freefont.toString(toggleall);
248 /// the type of outline operation
250 OutlineUp, // Move this header with text down
251 OutlineDown, // Move this header with text up
252 OutlineIn, // Make this header deeper
253 OutlineOut // Make this header shallower
257 static void outline(OutlineOp mode, Cursor & cur)
259 Buffer & buf = cur.buffer();
260 pit_type & pit = cur.pit();
261 ParagraphList & pars = buf.text().paragraphs();
262 ParagraphList::iterator bgn = pars.begin();
263 // The first paragraph of the area to be copied:
264 ParagraphList::iterator start = boost::next(bgn, pit);
265 // The final paragraph of area to be copied:
266 ParagraphList::iterator finish = start;
267 ParagraphList::iterator end = pars.end();
269 TextClass const & tc = buf.params().textClass();
271 int const thistoclevel = start->layout()->toclevel;
275 // Move out (down) from this section header
278 // Seek the one (on same level) below
279 for (; finish != end; ++finish) {
280 toclevel = finish->layout()->toclevel;
281 if (toclevel != Layout::NOT_IN_TOC
282 && toclevel <= thistoclevel) {
286 ParagraphList::iterator dest = start;
287 // Move out (up) from this header
290 // Search previous same-level header above
293 toclevel = dest->layout()->toclevel;
295 && (toclevel == Layout::NOT_IN_TOC
296 || toclevel > thistoclevel));
297 // Not found; do nothing
298 if (toclevel == Layout::NOT_IN_TOC || toclevel > thistoclevel)
300 pit_type const newpit = distance(bgn, dest);
301 pit_type const len = distance(start, finish);
302 pit_type const deletepit = pit + len;
303 buf.undo().recordUndo(cur, ATOMIC_UNDO, newpit, deletepit - 1);
304 pars.insert(dest, start, finish);
305 start = boost::next(pars.begin(), deletepit);
307 pars.erase(start, finish);
311 // Go down out of current header:
314 // Find next same-level header:
315 for (; finish != end; ++finish) {
316 toclevel = finish->layout()->toclevel;
317 if (toclevel != Layout::NOT_IN_TOC && toclevel <= thistoclevel)
320 ParagraphList::iterator dest = finish;
321 // Go one down from *this* header:
326 // Go further down to find header to insert in front of:
327 for (; dest != end; ++dest) {
328 toclevel = dest->layout()->toclevel;
329 if (toclevel != Layout::NOT_IN_TOC && 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 buf.undo().recordUndo(cur);
344 for (size_t i = 0; i != tc.layoutCount(); ++i) {
345 LayoutPtr const & lt = tc.layout(i);
346 if (lt->toclevel == thistoclevel + 1 &&
347 start->layout()->labeltype == lt->labeltype) {
348 start->setLayout(lt);
354 buf.undo().recordUndo(cur);
355 for (size_t i = 0; i != tc.layoutCount(); ++i) {
356 LayoutPtr const & lt = tc.layout(i);
357 if (lt->toclevel == thistoclevel - 1 &&
358 start->layout()->labeltype == lt->labeltype) {
359 start->setLayout(lt);
370 void Text::number(Cursor & cur)
372 FontInfo font = ignore_font;
373 font.setNumber(FONT_TOGGLE);
374 toggleAndShow(cur, this, Font(font, ignore_language));
378 bool Text::isRTL(Buffer const & buffer, Paragraph const & par) const
380 return par.isRTL(buffer.params());
384 void Text::dispatch(Cursor & cur, FuncRequest & cmd)
386 LYXERR(Debug::ACTION, "Text::dispatch: cmd: " << cmd);
388 BufferView * bv = &cur.bv();
389 TextMetrics & tm = bv->textMetrics(this);
390 if (!tm.has(cur.pit()))
391 lyx::dispatch(FuncRequest(LFUN_SCREEN_RECENTER));
393 // FIXME: We use the update flag to indicates wether a singlePar or a
394 // full screen update is needed. We reset it here but shall we restore it
398 BOOST_ASSERT(cur.text() == this);
399 CursorSlice oldTopSlice = cur.top();
400 bool oldBoundary = cur.boundary();
401 bool sel = cur.selection();
402 // Signals that, even if needsUpdate == false, an update of the
403 // cursor paragraph is required
404 bool singleParUpdate = lyxaction.funcHasFlag(cmd.action,
405 LyXAction::SingleParUpdate);
406 // Signals that a full-screen update is required
407 bool needsUpdate = !(lyxaction.funcHasFlag(cmd.action,
408 LyXAction::NoUpdate) || singleParUpdate);
410 switch (cmd.action) {
412 case LFUN_PARAGRAPH_MOVE_DOWN: {
413 pit_type const pit = cur.pit();
414 recUndo(cur, pit, pit + 1);
416 swap(pars_[pit], pars_[pit + 1]);
417 updateLabels(cur.buffer());
423 case LFUN_PARAGRAPH_MOVE_UP: {
424 pit_type const pit = cur.pit();
425 recUndo(cur, pit - 1, pit);
427 swap(pars_[pit], pars_[pit - 1]);
428 updateLabels(cur.buffer());
434 case LFUN_APPENDIX: {
435 Paragraph & par = cur.paragraph();
436 bool start = !par.params().startOfAppendix();
438 // FIXME: The code below only makes sense at top level.
439 // Should LFUN_APPENDIX be restricted to top-level paragraphs?
440 // ensure that we have only one start_of_appendix in this document
441 // FIXME: this don't work for multipart document!
442 for (pit_type tmp = 0, end = pars_.size(); tmp != end; ++tmp) {
443 if (pars_[tmp].params().startOfAppendix()) {
445 pars_[tmp].params().startOfAppendix(false);
451 par.params().startOfAppendix(start);
453 // we can set the refreshing parameters now
454 updateLabels(cur.buffer());
458 case LFUN_WORD_DELETE_FORWARD:
459 if (cur.selection()) {
460 cutSelection(cur, true, false);
462 deleteWordForward(cur);
463 finishChange(cur, false);
466 case LFUN_WORD_DELETE_BACKWARD:
467 if (cur.selection()) {
468 cutSelection(cur, true, false);
470 deleteWordBackward(cur);
471 finishChange(cur, false);
474 case LFUN_LINE_DELETE:
475 if (cur.selection()) {
476 cutSelection(cur, true, false);
478 tm.deleteLineForward(cur);
479 finishChange(cur, false);
482 case LFUN_BUFFER_BEGIN:
483 case LFUN_BUFFER_BEGIN_SELECT:
484 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_BEGIN_SELECT);
485 if (cur.depth() == 1) {
486 needsUpdate |= cursorTop(cur);
490 cur.updateFlags(Update::FitCursor);
493 case LFUN_BUFFER_END:
494 case LFUN_BUFFER_END_SELECT:
495 needsUpdate |= cur.selHandle(cmd.action == LFUN_BUFFER_END_SELECT);
496 if (cur.depth() == 1) {
497 needsUpdate |= cursorBottom(cur);
501 cur.updateFlags(Update::FitCursor);
504 case LFUN_CHAR_FORWARD:
505 case LFUN_CHAR_FORWARD_SELECT:
506 //LYXERR0(" LFUN_CHAR_FORWARD[SEL]:\n" << cur);
507 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_FORWARD_SELECT);
508 needsUpdate |= cursorForward(cur);
510 if (!needsUpdate && oldTopSlice == cur.top()
511 && cur.boundary() == oldBoundary) {
513 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
517 case LFUN_CHAR_BACKWARD:
518 case LFUN_CHAR_BACKWARD_SELECT:
519 //lyxerr << "handle LFUN_CHAR_BACKWARD[_SELECT]:\n" << cur << endl;
520 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_BACKWARD_SELECT);
521 needsUpdate |= cursorBackward(cur);
523 if (!needsUpdate && oldTopSlice == cur.top()
524 && cur.boundary() == oldBoundary) {
526 cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
531 case LFUN_CHAR_LEFT_SELECT:
532 if (lyxrc.visual_cursor) {
533 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_LEFT_SELECT);
534 needsUpdate |= cursorVisLeft(cur);
535 if (!needsUpdate && oldTopSlice == cur.top()
536 && cur.boundary() == oldBoundary) {
538 cmd = FuncRequest(LFUN_FINISHED_LEFT);
541 if (reverseDirectionNeeded(cur)) {
542 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
543 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
545 cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ?
546 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
553 case LFUN_CHAR_RIGHT:
554 case LFUN_CHAR_RIGHT_SELECT:
555 if (lyxrc.visual_cursor) {
556 needsUpdate |= cur.selHandle(cmd.action == LFUN_CHAR_RIGHT_SELECT);
557 needsUpdate |= cursorVisRight(cur);
558 if (!needsUpdate && oldTopSlice == cur.top()
559 && cur.boundary() == oldBoundary) {
561 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
564 if (reverseDirectionNeeded(cur)) {
565 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
566 LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
568 cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ?
569 LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
578 case LFUN_DOWN_SELECT:
581 // stop/start the selection
582 bool select = cmd.action == LFUN_DOWN_SELECT ||
583 cmd.action == LFUN_UP_SELECT;
584 cur.selHandle(select);
586 // move cursor up/down
587 bool up = cmd.action == LFUN_UP_SELECT || cmd.action == LFUN_UP;
588 bool const successful = cur.upDownInText(up, needsUpdate);
590 // redraw if you leave mathed (for the decorations)
591 needsUpdate |= cur.beforeDispatchCursor().inMathed();
598 case LFUN_PARAGRAPH_UP:
599 case LFUN_PARAGRAPH_UP_SELECT:
600 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_UP_SELECT);
601 needsUpdate |= cursorUpParagraph(cur);
604 case LFUN_PARAGRAPH_DOWN:
605 case LFUN_PARAGRAPH_DOWN_SELECT:
606 needsUpdate |= cur.selHandle(cmd.action == LFUN_PARAGRAPH_DOWN_SELECT);
607 needsUpdate |= cursorDownParagraph(cur);
610 case LFUN_SCREEN_UP_SELECT:
611 needsUpdate |= cur.selHandle(true);
612 if (cur.pit() == 0 && cur.textRow().pos() == 0)
615 tm.cursorPrevious(cur);
619 case LFUN_SCREEN_DOWN_SELECT:
620 needsUpdate |= cur.selHandle(true);
621 if (cur.pit() == cur.lastpit()
622 && cur.textRow().endpos() == cur.lastpos())
629 case LFUN_LINE_BEGIN:
630 case LFUN_LINE_BEGIN_SELECT:
631 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_BEGIN_SELECT);
632 needsUpdate |= tm.cursorHome(cur);
636 case LFUN_LINE_END_SELECT:
637 needsUpdate |= cur.selHandle(cmd.action == LFUN_LINE_END_SELECT);
638 needsUpdate |= tm.cursorEnd(cur);
641 case LFUN_WORD_RIGHT:
642 case LFUN_WORD_RIGHT_SELECT:
643 //FIXME: for visual cursor mode, really move right
644 if (reverseDirectionNeeded(cur)) {
645 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
646 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
648 cmd.action = cmd.action == LFUN_WORD_RIGHT_SELECT ?
649 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
654 case LFUN_WORD_FORWARD:
655 case LFUN_WORD_FORWARD_SELECT:
656 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT);
657 needsUpdate |= cursorForwardOneWord(cur);
661 case LFUN_WORD_LEFT_SELECT:
662 //FIXME: for visual cursor mode, really move left
663 if (reverseDirectionNeeded(cur)) {
664 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
665 LFUN_WORD_FORWARD_SELECT : LFUN_WORD_FORWARD;
667 cmd.action = cmd.action == LFUN_WORD_LEFT_SELECT ?
668 LFUN_WORD_BACKWARD_SELECT : LFUN_WORD_BACKWARD;
673 case LFUN_WORD_BACKWARD:
674 case LFUN_WORD_BACKWARD_SELECT:
675 needsUpdate |= cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT);
676 needsUpdate |= cursorBackwardOneWord(cur);
679 case LFUN_WORD_SELECT: {
680 selectWord(cur, WHOLE_WORD);
681 finishChange(cur, true);
685 case LFUN_NEW_LINE: {
686 // Not allowed by LaTeX (labels or empty par)
687 if (cur.pos() > cur.paragraph().beginOfBody()) {
688 // this avoids a double undo
689 // FIXME: should not be needed, ideally
690 if (!cur.selection())
692 cap::replaceSelection(cur);
693 cur.insert(new InsetNewline);
695 moveCursor(cur, false);
700 case LFUN_LINE_BREAK: {
701 // Not allowed by LaTeX (labels or empty par)
702 if (cur.pos() > cur.paragraph().beginOfBody()) {
703 // this avoids a double undo
704 // FIXME: should not be needed, ideally
705 if (!cur.selection())
707 cap::replaceSelection(cur);
708 cur.insert(new InsetLinebreak);
710 moveCursor(cur, false);
715 case LFUN_CHAR_DELETE_FORWARD:
716 if (!cur.selection()) {
717 if (cur.pos() == cur.paragraph().size())
718 // Par boundary, force full-screen update
719 singleParUpdate = false;
720 needsUpdate |= erase(cur);
722 // It is possible to make it a lot faster still
723 // just comment out the line below...
725 cutSelection(cur, true, false);
726 singleParUpdate = false;
728 moveCursor(cur, false);
731 case LFUN_DELETE_FORWARD_SKIP:
732 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
733 if (!cur.selection()) {
734 if (cur.pos() == cur.lastpos()) {
741 cutSelection(cur, true, false);
746 case LFUN_CHAR_DELETE_BACKWARD:
747 if (!cur.selection()) {
748 if (bv->getIntl().getTransManager().backspace()) {
749 // Par boundary, full-screen update
751 singleParUpdate = false;
752 needsUpdate |= backspace(cur);
754 // It is possible to make it a lot faster still
755 // just comment out the line below...
758 cutSelection(cur, true, false);
759 singleParUpdate = false;
763 case LFUN_DELETE_BACKWARD_SKIP:
764 // Reverse the effect of LFUN_BREAK_PARAGRAPH_SKIP.
765 if (!cur.selection()) {
767 //CursorSlice cur = cursor();
771 cutSelection(cur, true, false);
775 case LFUN_BREAK_PARAGRAPH:
776 cap::replaceSelection(cur);
777 breakParagraph(cur, cmd.argument() == "inverse");
781 case LFUN_BREAK_PARAGRAPH_SKIP: {
782 // When at the beginning of a paragraph, remove
783 // indentation. Otherwise, do the same as LFUN_BREAK_PARAGRAPH.
784 cap::replaceSelection(cur);
786 cur.paragraph().params().labelWidthString(docstring());
788 breakParagraph(cur, false);
794 // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant,
795 // as its duties can be performed there. Should it be removed??
796 // FIXME For now, it can just dispatch LFUN_PARAGRAPH_PARAMS...
797 case LFUN_PARAGRAPH_SPACING: {
798 Paragraph & par = cur.paragraph();
799 Spacing::Space cur_spacing = par.params().spacing().getSpace();
800 string cur_value = "1.0";
801 if (cur_spacing == Spacing::Other)
802 cur_value = par.params().spacing().getValueAsString();
804 istringstream is(to_utf8(cmd.argument()));
807 Spacing::Space new_spacing = cur_spacing;
808 string new_value = cur_value;
810 lyxerr << "Missing argument to `paragraph-spacing'"
812 } else if (tmp == "single") {
813 new_spacing = Spacing::Single;
814 } else if (tmp == "onehalf") {
815 new_spacing = Spacing::Onehalf;
816 } else if (tmp == "double") {
817 new_spacing = Spacing::Double;
818 } else if (tmp == "other") {
819 new_spacing = Spacing::Other;
820 string tmpval = "0.0";
822 lyxerr << "new_value = " << tmpval << endl;
825 } else if (tmp == "default") {
826 new_spacing = Spacing::Default;
828 lyxerr << to_utf8(_("Unknown spacing argument: "))
829 << to_utf8(cmd.argument()) << endl;
831 if (cur_spacing != new_spacing || cur_value != new_value)
832 par.params().spacing(Spacing(new_spacing, new_value));
836 case LFUN_INSET_INSERT: {
838 Inset * inset = createInset(bv->buffer(), cmd);
840 // FIXME (Abdel 01/02/2006):
841 // What follows would be a partial fix for bug 2154:
842 // http://bugzilla.lyx.org/show_bug.cgi?id=2154
843 // This automatically put the label inset _after_ a
844 // numbered section. It should be possible to extend the mechanism
845 // to any kind of LateX environement.
846 // The correct way to fix that bug would be at LateX generation.
847 // I'll let the code here for reference as it could be used for some
848 // other feature like "automatic labelling".
850 Paragraph & par = pars_[cur.pit()];
851 if (inset->lyxCode() == LABEL_CODE
852 && par.layout()->labeltype == LABEL_COUNTER) {
853 // Go to the end of the paragraph
854 // Warning: Because of Change-Tracking, the last
855 // position is 'size()' and not 'size()-1':
856 cur.pos() = par.size();
857 // Insert a new paragraph
858 FuncRequest fr(LFUN_BREAK_PARAGRAPH);
863 cutSelection(cur, true, false);
864 insertInset(cur, inset);
870 case LFUN_INSET_DISSOLVE:
871 needsUpdate |= dissolveInset(cur);
874 case LFUN_INSET_SETTINGS:
875 cur.inset().showInsetDialog(bv);
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;
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;
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_FONT:
996 if (cur.current_font.fontInfo().shape() == ITALIC_SHAPE)
997 cur.message(from_ascii("E"));
998 else if (cur.current_font.fontInfo().shape() == SMALLCAPS_SHAPE)
999 cur.message(from_ascii("N"));
1001 cur.message(from_ascii("0"));
1004 case LFUN_SERVER_GET_LAYOUT:
1005 cur.message(cur.paragraph().layout()->name());
1009 docstring layout = cmd.argument();
1010 LYXERR(Debug::INFO, "LFUN_LAYOUT: (arg) " << to_utf8(layout));
1012 Paragraph const & para = cur.paragraph();
1013 docstring const old_layout = para.layout()->name();
1014 TextClass const & tclass = bv->buffer().params().textClass();
1017 layout = tclass.defaultLayoutName();
1019 if (para.forceEmptyLayout())
1020 // in this case only the empty layout is allowed
1021 layout = tclass.emptyLayoutName();
1022 else if (para.useEmptyLayout()) {
1023 // in this case, default layout maps to empty layout
1024 if (layout == tclass.defaultLayoutName())
1025 layout = tclass.emptyLayoutName();
1027 // otherwise, the empty layout maps to the default
1028 if (layout == tclass.emptyLayoutName())
1029 layout = tclass.defaultLayoutName();
1032 bool hasLayout = tclass.hasLayout(layout);
1034 // If the entry is obsolete, use the new one instead.
1036 docstring const & obs = tclass[layout]->obsoleted_by();
1042 cur.errorMessage(from_utf8(N_("Layout ")) + cmd.argument() +
1043 from_utf8(N_(" not known")));
1047 bool change_layout = (old_layout != layout);
1049 if (!change_layout && cur.selection() &&
1050 cur.selBegin().pit() != cur.selEnd().pit())
1052 pit_type spit = cur.selBegin().pit();
1053 pit_type epit = cur.selEnd().pit() + 1;
1054 while (spit != epit) {
1055 if (pars_[spit].layout()->name() != old_layout) {
1056 change_layout = true;
1064 setLayout(cur, layout);
1069 case LFUN_CLIPBOARD_PASTE:
1070 cur.clearSelection();
1071 pasteClipboardText(cur, bv->buffer().errorList("Paste"),
1072 cmd.argument() == "paragraph");
1073 bv->buffer().errors("Paste");
1076 case LFUN_PRIMARY_SELECTION_PASTE:
1077 pasteString(cur, theSelection().get(),
1078 cmd.argument() == "paragraph");
1081 case LFUN_UNICODE_INSERT: {
1082 if (cmd.argument().empty())
1084 docstring hexstring = cmd.argument();
1085 if (isHex(hexstring)) {
1086 char_type c = hexToInt(hexstring);
1087 if (c >= 32 && c < 0x10ffff) {
1088 lyxerr << "Inserting c: " << c << endl;
1089 docstring s = docstring(1, c);
1090 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, s));
1096 case LFUN_QUOTE_INSERT: {
1097 Paragraph & par = cur.paragraph();
1098 pos_type pos = cur.pos();
1099 BufferParams const & bufparams = bv->buffer().params();
1100 LayoutPtr const & style = par.layout();
1101 if (!style->pass_thru
1102 && par.getFontSettings(bufparams, pos).language()->lang() != "hebrew") {
1103 // this avoids a double undo
1104 // FIXME: should not be needed, ideally
1105 if (!cur.selection())
1107 cap::replaceSelection(cur);
1112 else if (cur.prevInset() && cur.prevInset()->isSpace())
1115 c = par.getChar(pos - 1);
1116 string arg = to_utf8(cmd.argument());
1117 if (arg == "single")
1118 cur.insert(new InsetQuotes(c,
1119 bufparams.quotes_language,
1120 InsetQuotes::SingleQ));
1122 cur.insert(new InsetQuotes(c,
1123 bufparams.quotes_language,
1124 InsetQuotes::DoubleQ));
1128 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, "\""));
1132 case LFUN_DATE_INSERT: {
1133 string const format = cmd.argument().empty()
1134 ? lyxrc.date_insert_format : to_utf8(cmd.argument());
1135 string const time = formatted_time(current_time(), format);
1136 lyx::dispatch(FuncRequest(LFUN_SELF_INSERT, time));
1140 case LFUN_MOUSE_TRIPLE:
1141 if (cmd.button() == mouse_button::button1) {
1150 case LFUN_MOUSE_DOUBLE:
1151 if (cmd.button() == mouse_button::button1) {
1152 selectWord(cur, WHOLE_WORD_STRICT);
1157 // Single-click on work area
1158 case LFUN_MOUSE_PRESS: {
1159 // Right click on a footnote flag opens float menu
1160 // FIXME: Why should we clear the selection in this case?
1161 if (cmd.button() == mouse_button::button3)
1162 cur.clearSelection();
1164 bool do_selection = cmd.button() == mouse_button::button1
1165 && cmd.argument() == "region-select";
1167 bool update = bv->mouseSetCursor(cur, do_selection);
1169 // Insert primary selection with middle mouse
1170 // if there is a local selection in the current buffer,
1172 if (cmd.button() == mouse_button::button2) {
1173 if (cap::selection()) {
1174 // Copy the selection buffer to the clipboard
1175 // stack, because we want it to appear in the
1176 // "Edit->Paste recent" menu.
1177 cap::copySelectionToStack();
1179 cap::pasteSelection(bv->cursor(),
1180 bv->buffer().errorList("Paste"));
1181 bv->buffer().errors("Paste");
1182 bv->buffer().markDirty();
1183 bv->cursor().finishUndo();
1185 lyx::dispatch(FuncRequest(LFUN_PRIMARY_SELECTION_PASTE, "paragraph"));
1189 // we have to update after dEPM triggered
1190 if (!update && cmd.button() == mouse_button::button1) {
1191 needsUpdate = false;
1198 case LFUN_MOUSE_MOTION: {
1199 // Only use motion with button 1
1200 //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));
1228 if (cur.top() == old)
1231 // FIXME: This is brute force! But without it the selected
1232 // area is not corrected updated while moving the mouse.
1233 cur.updateFlags(Update::Force | Update::FitCursor);
1234 // don't set anchor_
1235 bvcur.setCursor(cur);
1236 bvcur.selection() = true;
1237 //lyxerr << "MOTION: " << bv->cursor() << endl;
1242 case LFUN_MOUSE_RELEASE: {
1243 if (cmd.button() == mouse_button::button2)
1246 if (cmd.button() == mouse_button::button1) {
1247 // if there is new selection, update persistent
1248 // selection, otherwise, single click does not
1249 // clear persistent selection buffer
1250 if (cur.selection()) {
1252 // if double click, cur is moved to the end of word by selectWord
1253 // but bvcur is current mouse position
1254 Cursor & bvcur = cur.bv().cursor();
1255 bvcur.selection() = true;
1257 needsUpdate = false;
1264 case LFUN_SELF_INSERT: {
1265 if (cmd.argument().empty())
1268 // Automatically delete the currently selected
1269 // text and replace it with what is being
1270 // typed in now. Depends on lyxrc settings
1271 // "auto_region_delete", which defaults to
1274 if (lyxrc.auto_region_delete && cur.selection())
1275 cutSelection(cur, false, false);
1277 cur.clearSelection();
1278 Font const old_font = cur.real_current_font;
1280 docstring::const_iterator cit = cmd.argument().begin();
1281 docstring::const_iterator end = cmd.argument().end();
1282 for (; cit != end; ++cit)
1283 bv->translateAndInsert(*cit, this, cur);
1286 moveCursor(cur, false);
1290 case LFUN_HYPERLINK_INSERT: {
1291 InsetCommandParams p(HYPERLINK_CODE);
1293 if (cur.selection()) {
1294 content = cur.selectionAsString(false);
1295 cutSelection(cur, true, false);
1297 p["target"] = (cmd.argument().empty()) ?
1298 content : cmd.argument();
1299 string const data = InsetCommandMailer::params2string("href", p);
1300 if (p["target"].empty()) {
1301 bv->showDialog("href", data);
1303 FuncRequest fr(LFUN_INSET_INSERT, data);
1309 case LFUN_LABEL_INSERT: {
1310 InsetCommandParams p(LABEL_CODE);
1311 // Try to generate a valid label
1312 p["name"] = (cmd.argument().empty()) ?
1313 cur.getPossibleLabel() :
1315 string const data = InsetCommandMailer::params2string("label", p);
1317 if (cmd.argument().empty()) {
1318 bv->showDialog("label", data);
1320 FuncRequest fr(LFUN_INSET_INSERT, data);
1326 case LFUN_INFO_INSERT: {
1327 Inset * inset = createInset(cur.bv().buffer(), cmd);
1330 // if an empty inset is created (cmd.argument() is empty)
1331 // use current selection as parameter.
1332 if (cmd.argument().empty() && cur.selection()) {
1333 // use selected text as info to avoid a separate UI
1334 docstring ds = cur.selectionAsString(false);
1335 cutSelection(cur, true, false);
1336 static_cast<InsetInfo *>(inset)->setInfo(to_utf8(ds));
1337 static_cast<InsetInfo *>(inset)->updateInfo(cur.bv().buffer());
1339 insertInset(cur, inset);
1344 case LFUN_LIST_INSERT:
1345 case LFUN_THEOREM_INSERT:
1347 case LFUN_CAPTION_INSERT:
1348 case LFUN_FOOTNOTE_INSERT:
1349 case LFUN_NOTE_INSERT:
1350 case LFUN_FLEX_INSERT:
1351 case LFUN_BOX_INSERT:
1352 case LFUN_BRANCH_INSERT:
1353 case LFUN_BIBITEM_INSERT:
1354 case LFUN_ERT_INSERT:
1355 case LFUN_LISTING_INSERT:
1356 case LFUN_MARGINALNOTE_INSERT:
1357 case LFUN_OPTIONAL_INSERT:
1358 case LFUN_ENVIRONMENT_INSERT:
1359 case LFUN_INDEX_INSERT:
1360 // Open the inset, and move the current selection
1362 doInsertInset(cur, this, cmd, true, true);
1364 // Some insets are numbered, others are shown in the outline pane so
1365 // let's update the labels and the toc backend.
1366 updateLabels(bv->buffer());
1369 case LFUN_TABULAR_INSERT:
1370 // if there were no arguments, just open the dialog
1371 if (doInsertInset(cur, this, cmd, false, true))
1374 bv->showDialog("tabularcreate");
1378 case LFUN_FLOAT_INSERT:
1379 case LFUN_FLOAT_WIDE_INSERT:
1380 case LFUN_WRAP_INSERT: {
1381 bool content = cur.selection(); // will some text be moved into the inset?
1383 doInsertInset(cur, this, cmd, true, true);
1385 ParagraphList & pars = cur.text()->paragraphs();
1387 TextClass const & tclass = bv->buffer().params().textClass();
1389 // add a separate paragraph for the caption inset
1390 pars.push_back(Paragraph());
1391 pars.back().setInsetOwner(pars[0].inInset());
1392 pars.back().setEmptyOrDefaultLayout(tclass);
1393 int cap_pit = pars.size() - 1;
1395 // if an empty inset was created, we create an additional empty
1396 // paragraph at the bottom so that the user can choose where to put
1397 // the graphics (or table).
1399 pars.push_back(Paragraph());
1400 pars.back().setInsetOwner(pars[0].inInset());
1401 pars.back().setEmptyOrDefaultLayout(tclass);
1404 // reposition the cursor to the caption
1405 cur.pit() = cap_pit;
1407 // FIXME: This Text/Cursor dispatch handling is a mess!
1408 // We cannot use Cursor::dispatch here it needs access to up to
1410 FuncRequest cmd_caption(LFUN_CAPTION_INSERT);
1411 cur.text()->dispatch(cur, cmd_caption);
1412 cur.updateFlags(Update::Force);
1413 // FIXME: When leaving the Float (or Wrap) inset we should
1414 // delete any empty paragraph left above or below the
1419 case LFUN_NOMENCL_INSERT: {
1420 FuncRequest cmd1 = cmd;
1421 if (cmd.argument().empty())
1422 cmd1 = FuncRequest(cmd,
1423 bv->cursor().innerText()->getStringToIndex(bv->cursor()));
1424 Inset * inset = createInset(cur.bv().buffer(), cmd1);
1428 cur.clearSelection();
1429 insertInset(cur, inset);
1430 // Show the dialog for the nomenclature entry, since the
1431 // description entry still needs to be filled in.
1432 if (cmd.action == LFUN_NOMENCL_INSERT)
1433 inset->edit(cur, true);
1438 case LFUN_INDEX_PRINT:
1439 case LFUN_NOMENCL_PRINT:
1440 case LFUN_TOC_INSERT:
1441 case LFUN_HFILL_INSERT:
1442 case LFUN_LINE_INSERT:
1443 case LFUN_NEWPAGE_INSERT:
1444 case LFUN_PAGEBREAK_INSERT:
1445 case LFUN_CLEARPAGE_INSERT:
1446 case LFUN_CLEARDOUBLEPAGE_INSERT:
1448 doInsertInset(cur, this, cmd, false, false);
1452 case LFUN_DEPTH_DECREMENT:
1453 changeDepth(cur, DEC_DEPTH);
1456 case LFUN_DEPTH_INCREMENT:
1457 changeDepth(cur, INC_DEPTH);
1460 case LFUN_MATH_DISPLAY:
1461 mathDispatch(cur, cmd, true);
1464 case LFUN_MATH_IMPORT_SELECTION:
1465 case LFUN_MATH_MODE:
1466 if (cmd.argument() == "on")
1467 // don't pass "on" as argument
1468 mathDispatch(cur, FuncRequest(LFUN_MATH_MODE), false);
1470 mathDispatch(cur, cmd, false);
1473 case LFUN_MATH_MACRO:
1474 if (cmd.argument().empty())
1475 cur.errorMessage(from_utf8(N_("Missing argument")));
1477 string s = to_utf8(cmd.argument());
1478 string const s1 = token(s, ' ', 1);
1479 int const nargs = s1.empty() ? 0 : convert<int>(s1);
1480 string const s2 = token(s, ' ', 2);
1481 MacroType type = MacroTypeNewcommand;
1483 type = MacroTypeDef;
1484 cur.insert(new MathMacroTemplate(from_utf8(token(s, ' ', 0)), nargs, false, type));
1485 //cur.nextInset()->edit(cur, true);
1489 // passthrough hat and underscore outside mathed:
1490 case LFUN_MATH_SUBSCRIPT:
1491 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "_"), false);
1493 case LFUN_MATH_SUPERSCRIPT:
1494 mathDispatch(cur, FuncRequest(LFUN_SELF_INSERT, "^"), false);
1497 case LFUN_MATH_INSERT:
1498 case LFUN_MATH_MATRIX:
1499 case LFUN_MATH_DELIM:
1500 case LFUN_MATH_BIGDELIM: {
1501 cap::replaceSelection(cur);
1502 cur.insert(new InsetMathHull(hullSimple));
1503 checkAndActivateInset(cur, true);
1504 BOOST_ASSERT(cur.inMathed());
1509 case LFUN_FONT_EMPH: {
1510 Font font(ignore_font, ignore_language);
1511 font.fontInfo().setEmph(FONT_TOGGLE);
1512 toggleAndShow(cur, this, font);
1516 case LFUN_FONT_BOLD: {
1517 Font font(ignore_font, ignore_language);
1518 font.fontInfo().setSeries(BOLD_SERIES);
1519 toggleAndShow(cur, this, font);
1523 case LFUN_FONT_NOUN: {
1524 Font font(ignore_font, ignore_language);
1525 font.fontInfo().setNoun(FONT_TOGGLE);
1526 toggleAndShow(cur, this, font);
1530 case LFUN_FONT_TYPEWRITER: {
1531 Font font(ignore_font, ignore_language);
1532 font.fontInfo().setFamily(TYPEWRITER_FAMILY); // no good
1533 toggleAndShow(cur, this, font);
1537 case LFUN_FONT_SANS: {
1538 Font font(ignore_font, ignore_language);
1539 font.fontInfo().setFamily(SANS_FAMILY);
1540 toggleAndShow(cur, this, font);
1544 case LFUN_FONT_ROMAN: {
1545 Font font(ignore_font, ignore_language);
1546 font.fontInfo().setFamily(ROMAN_FAMILY);
1547 toggleAndShow(cur, this, font);
1551 case LFUN_FONT_DEFAULT: {
1552 Font font(inherit_font, ignore_language);
1553 toggleAndShow(cur, this, font);
1557 case LFUN_FONT_UNDERLINE: {
1558 Font font(ignore_font, ignore_language);
1559 font.fontInfo().setUnderbar(FONT_TOGGLE);
1560 toggleAndShow(cur, this, font);
1564 case LFUN_FONT_SIZE: {
1565 Font font(ignore_font, ignore_language);
1566 setLyXSize(to_utf8(cmd.argument()), font.fontInfo());
1567 toggleAndShow(cur, this, font);
1571 case LFUN_LANGUAGE: {
1572 Language const * lang = languages.getLanguage(to_utf8(cmd.argument()));
1575 Font font(ignore_font, lang);
1576 toggleAndShow(cur, this, font);
1580 case LFUN_FONT_FREE_APPLY:
1581 toggleAndShow(cur, this, freefont, toggleall);
1582 cur.message(_("Character set"));
1585 // Set the freefont using the contents of \param data dispatched from
1586 // the frontends and apply it at the current cursor location.
1587 case LFUN_FONT_FREE_UPDATE: {
1590 if (font.fromString(to_utf8(cmd.argument()), toggle)) {
1593 toggleAndShow(cur, this, freefont, toggleall);
1594 cur.message(_("Character set"));
1596 lyxerr << "Argument not ok";
1601 case LFUN_FINISHED_LEFT:
1602 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_LEFT:\n" << cur);
1603 // We're leaving an inset, going left. If the inset is LTR, we're
1604 // leaving from the front, so we should not move (remain at --- but
1605 // not in --- the inset). If the inset is RTL, move left, without
1606 // entering the inset itself; i.e., move to after the inset.
1607 if (cur.paragraph().getFontSettings(
1608 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1609 cursorVisLeft(cur, true);
1612 case LFUN_FINISHED_RIGHT:
1613 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_RIGHT:\n" << cur);
1614 // We're leaving an inset, going right. If the inset is RTL, we're
1615 // leaving from the front, so we should not move (remain at --- but
1616 // not in --- the inset). If the inset is LTR, move right, without
1617 // entering the inset itself; i.e., move to after the inset.
1618 if (!cur.paragraph().getFontSettings(
1619 cur.bv().buffer().params(), cur.pos()).isRightToLeft())
1620 cursorVisRight(cur, true);
1623 case LFUN_FINISHED_BACKWARD:
1624 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_BACKWARD:\n" << cur);
1627 case LFUN_FINISHED_FORWARD:
1628 LYXERR(Debug::DEBUG, "handle LFUN_FINISHED_FORWARD:\n" << cur);
1630 cur.setCurrentFont();
1633 case LFUN_LAYOUT_PARAGRAPH: {
1635 params2string(cur.paragraph(), data);
1636 data = "show\n" + data;
1637 bv->showDialog("paragraph", data);
1641 case LFUN_PARAGRAPH_UPDATE: {
1643 params2string(cur.paragraph(), data);
1645 // Will the paragraph accept changes from the dialog?
1647 cur.inset().allowParagraphCustomization(cur.idx());
1649 data = "update " + convert<string>(accept) + '\n' + data;
1650 bv->updateDialog("paragraph", data);
1654 case LFUN_ACCENT_UMLAUT:
1655 case LFUN_ACCENT_CIRCUMFLEX:
1656 case LFUN_ACCENT_GRAVE:
1657 case LFUN_ACCENT_ACUTE:
1658 case LFUN_ACCENT_TILDE:
1659 case LFUN_ACCENT_CEDILLA:
1660 case LFUN_ACCENT_MACRON:
1661 case LFUN_ACCENT_DOT:
1662 case LFUN_ACCENT_UNDERDOT:
1663 case LFUN_ACCENT_UNDERBAR:
1664 case LFUN_ACCENT_CARON:
1665 case LFUN_ACCENT_SPECIAL_CARON:
1666 case LFUN_ACCENT_BREVE:
1667 case LFUN_ACCENT_TIE:
1668 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
1669 case LFUN_ACCENT_CIRCLE:
1670 case LFUN_ACCENT_OGONEK:
1671 theLyXFunc().handleKeyFunc(cmd.action);
1672 if (!cmd.argument().empty())
1673 // FIXME: Are all these characters encoded in one byte in utf8?
1674 bv->translateAndInsert(cmd.argument()[0], this, cur);
1677 case LFUN_FLOAT_LIST: {
1678 TextClass const & tclass = bv->buffer().params().textClass();
1679 if (tclass.floats().typeExist(to_utf8(cmd.argument()))) {
1681 if (cur.selection())
1682 cutSelection(cur, true, false);
1683 breakParagraph(cur);
1685 if (cur.lastpos() != 0) {
1686 cursorBackward(cur);
1687 breakParagraph(cur);
1690 //FIXME Check if this should be emptyLayout()
1691 setLayout(cur, tclass.defaultLayoutName());
1692 ParagraphParameters p;
1693 setParagraphs(cur, p);
1694 insertInset(cur, new InsetFloatList(to_utf8(cmd.argument())));
1697 lyxerr << "Non-existent float type: "
1698 << to_utf8(cmd.argument()) << endl;
1703 case LFUN_CHANGE_ACCEPT: {
1704 acceptOrRejectChanges(cur, ACCEPT);
1708 case LFUN_CHANGE_REJECT: {
1709 acceptOrRejectChanges(cur, REJECT);
1713 case LFUN_THESAURUS_ENTRY: {
1714 docstring arg = cmd.argument();
1716 arg = cur.selectionAsString(false);
1718 if (arg.size() > 100 || arg.empty()) {
1719 // Get word or selection
1720 selectWordWhenUnderCursor(cur, WHOLE_WORD);
1721 arg = cur.selectionAsString(false);
1724 bv->showDialog("thesaurus", to_utf8(arg));
1728 case LFUN_PARAGRAPH_PARAMS_APPLY: {
1729 // Given data, an encoding of the ParagraphParameters
1730 // generated in the Paragraph dialog, this function sets
1731 // the current paragraph, or currently selected paragraphs,
1733 // NOTE: This function overrides all existing settings.
1734 setParagraphs(cur, cmd.argument());
1735 cur.message(_("Paragraph layout set"));
1739 case LFUN_PARAGRAPH_PARAMS: {
1740 // Given data, an encoding of the ParagraphParameters as we'd
1741 // find them in a LyX file, this function modifies the current paragraph,
1742 // or currently selected paragraphs.
1743 // NOTE: This function only modifies, and does not override, existing
1745 setParagraphs(cur, cmd.argument(), true);
1746 cur.message(_("Paragraph layout set"));
1751 if (cur.selection()) {
1752 cur.selection() = false;
1755 // This used to be LFUN_FINISHED_RIGHT, I think FORWARD is more
1756 // correct, but I'm not 100% sure -- dov, 071019
1757 cmd = FuncRequest(LFUN_FINISHED_FORWARD);
1761 case LFUN_OUTLINE_UP:
1762 outline(OutlineUp, cur);
1763 setCursor(cur, cur.pit(), 0);
1764 updateLabels(cur.buffer());
1768 case LFUN_OUTLINE_DOWN:
1769 outline(OutlineDown, cur);
1770 setCursor(cur, cur.pit(), 0);
1771 updateLabels(cur.buffer());
1775 case LFUN_OUTLINE_IN:
1776 outline(OutlineIn, cur);
1777 updateLabels(cur.buffer());
1781 case LFUN_OUTLINE_OUT:
1782 outline(OutlineOut, cur);
1783 updateLabels(cur.buffer());
1788 LYXERR(Debug::ACTION, "Command " << cmd << " not DISPATCHED by Text");
1793 needsUpdate |= (cur.pos() != cur.lastpos()) && cur.selection();
1795 // FIXME: The cursor flag is reset two lines below
1796 // so we need to check here if some of the LFUN did touch that.
1797 // for now only Text::erase() and Text::backspace() do that.
1798 // The plan is to verify all the LFUNs and then to remove this
1799 // singleParUpdate boolean altogether.
1800 if (cur.result().update() & Update::Force) {
1801 singleParUpdate = false;
1805 // FIXME: the following code should go in favor of fine grained
1806 // update flag treatment.
1807 if (singleParUpdate) {
1808 // Inserting characters does not change par height in general. So, try
1809 // to update _only_ this paragraph. BufferView will detect if a full
1810 // metrics update is needed anyway.
1811 cur.updateFlags(Update::SinglePar | Update::FitCursor);
1816 && &oldTopSlice.inset() == &cur.inset()
1817 && oldTopSlice.idx() == cur.idx()
1818 && !sel // sel is a backup of cur.selection() at the biginning of the function.
1819 && !cur.selection())
1820 // FIXME: it would be better if we could just do this
1822 //if (cur.result().update() != Update::FitCursor)
1825 // But some LFUNs do not set Update::FitCursor when needed, so we
1826 // do it for all. This is not very harmfull as FitCursor will provoke
1827 // a full redraw only if needed but still, a proper review of all LFUN
1828 // should be done and this needsUpdate boolean can then be removed.
1829 cur.updateFlags(Update::FitCursor);
1831 cur.updateFlags(Update::Force | Update::FitCursor);
1835 bool Text::getStatus(Cursor & cur, FuncRequest const & cmd,
1836 FuncStatus & flag) const
1838 BOOST_ASSERT(cur.text() == this);
1840 Font const & font = cur.real_current_font;
1841 FontInfo const & fontinfo = font.fontInfo();
1843 InsetCode code = NO_CODE;
1845 switch (cmd.action) {
1847 case LFUN_DEPTH_DECREMENT:
1848 enable = changeDepthAllowed(cur, DEC_DEPTH);
1851 case LFUN_DEPTH_INCREMENT:
1852 enable = changeDepthAllowed(cur, INC_DEPTH);
1856 flag.setOnOff(cur.paragraph().params().startOfAppendix());
1859 case LFUN_BIBITEM_INSERT:
1860 enable = (cur.paragraph().layout()->labeltype == LABEL_BIBLIO
1864 case LFUN_DIALOG_SHOW_NEW_INSET:
1865 if (cmd.argument() == "bibitem")
1866 code = BIBITEM_CODE;
1867 else if (cmd.argument() == "bibtex")
1869 else if (cmd.argument() == "box")
1871 else if (cmd.argument() == "branch")
1873 else if (cmd.argument() == "citation")
1875 else if (cmd.argument() == "ert")
1877 else if (cmd.argument() == "external")
1878 code = EXTERNAL_CODE;
1879 else if (cmd.argument() == "float")
1881 else if (cmd.argument() == "graphics")
1882 code = GRAPHICS_CODE;
1883 else if (cmd.argument() == "href")
1884 code = HYPERLINK_CODE;
1885 else if (cmd.argument() == "include")
1886 code = INCLUDE_CODE;
1887 else if (cmd.argument() == "index")
1889 else if (cmd.argument() == "nomenclature")
1890 code = NOMENCL_CODE;
1891 else if (cmd.argument() == "label")
1893 else if (cmd.argument() == "note")
1895 else if (cmd.argument() == "ref")
1897 else if (cmd.argument() == "toc")
1899 else if (cmd.argument() == "vspace")
1901 else if (cmd.argument() == "wrap")
1903 else if (cmd.argument() == "listings")
1904 code = LISTINGS_CODE;
1907 case LFUN_ERT_INSERT:
1910 case LFUN_LISTING_INSERT:
1911 code = LISTINGS_CODE;
1913 case LFUN_FOOTNOTE_INSERT:
1916 case LFUN_TABULAR_INSERT:
1917 code = TABULAR_CODE;
1919 case LFUN_MARGINALNOTE_INSERT:
1922 case LFUN_FLOAT_INSERT:
1923 case LFUN_FLOAT_WIDE_INSERT:
1926 case LFUN_WRAP_INSERT:
1929 case LFUN_FLOAT_LIST:
1930 code = FLOAT_LIST_CODE;
1933 case LFUN_LIST_INSERT:
1936 case LFUN_THEOREM_INSERT:
1937 code = THEOREM_CODE;
1940 case LFUN_CAPTION_INSERT:
1941 code = CAPTION_CODE;
1943 case LFUN_NOTE_INSERT:
1946 case LFUN_FLEX_INSERT: {
1948 string s = cmd.getArg(0);
1949 InsetLayout il = cur.buffer().params().textClass().insetLayout(from_utf8(s));
1950 if (il.lyxtype() != "charstyle" &&
1951 il.lyxtype() != "custom" &&
1952 il.lyxtype() != "element" &&
1953 il.lyxtype ()!= "standard")
1957 case LFUN_BOX_INSERT:
1960 case LFUN_BRANCH_INSERT:
1962 if (cur.buffer().masterBuffer()->params().branchlist().empty())
1965 case LFUN_LABEL_INSERT:
1968 case LFUN_INFO_INSERT:
1971 case LFUN_OPTIONAL_INSERT:
1973 enable = cur.paragraph().insetList().count(OPTARG_CODE)
1974 < cur.paragraph().layout()->optionalargs;
1976 case LFUN_ENVIRONMENT_INSERT:
1979 case LFUN_INDEX_INSERT:
1982 case LFUN_INDEX_PRINT:
1983 code = INDEX_PRINT_CODE;
1985 case LFUN_NOMENCL_INSERT:
1986 code = NOMENCL_CODE;
1988 case LFUN_NOMENCL_PRINT:
1989 code = NOMENCL_PRINT_CODE;
1991 case LFUN_TOC_INSERT:
1994 case LFUN_HYPERLINK_INSERT:
1995 code = HYPERLINK_CODE;
1997 case LFUN_QUOTE_INSERT:
1998 // always allow this, since we will inset a raw quote
1999 // if an inset is not allowed.
2001 case LFUN_HFILL_INSERT:
2002 case LFUN_SPECIALCHAR_INSERT:
2003 code = SPECIALCHAR_CODE;
2005 case LFUN_SPACE_INSERT:
2006 // slight hack: we know this is allowed in math mode
2011 case LFUN_INSET_MODIFY:
2012 // We need to disable this, because we may get called for a
2014 // InsetTabular::getStatus() -> InsetText::getStatus()
2015 // and we don't handle LFUN_INSET_MODIFY.
2019 case LFUN_FONT_EMPH:
2020 flag.setOnOff(fontinfo.emph() == FONT_ON);
2023 case LFUN_FONT_NOUN:
2024 flag.setOnOff(fontinfo.noun() == FONT_ON);
2027 case LFUN_FONT_BOLD:
2028 flag.setOnOff(fontinfo.series() == BOLD_SERIES);
2031 case LFUN_FONT_SANS:
2032 flag.setOnOff(fontinfo.family() == SANS_FAMILY);
2035 case LFUN_FONT_ROMAN:
2036 flag.setOnOff(fontinfo.family() == ROMAN_FAMILY);
2039 case LFUN_FONT_TYPEWRITER:
2040 flag.setOnOff(fontinfo.family() == TYPEWRITER_FAMILY);
2045 enable = cur.selection();
2049 if (cmd.argument().empty()) {
2050 if (theClipboard().isInternal())
2051 enable = cap::numberOfSelections() > 0;
2053 enable = !theClipboard().empty();
2057 // we have an argument
2058 string const arg = to_utf8(cmd.argument());
2059 if (isStrUnsignedInt(arg)) {
2060 // it's a number and therefore means the internal stack
2061 unsigned int n = convert<unsigned int>(arg);
2062 enable = cap::numberOfSelections() > n;
2066 // explicit graphics type?
2067 if ((arg == "pdf" && theClipboard().hasGraphicsContents(Clipboard::PdfGraphicsType))
2068 || (arg == "png" && theClipboard().hasGraphicsContents(Clipboard::PngGraphicsType))
2069 || (arg == "jpeg" && theClipboard().hasGraphicsContents(Clipboard::JpegGraphicsType))
2070 || (arg == "linkback" && theClipboard().hasGraphicsContents(Clipboard::LinkBackGraphicsType))) {
2080 case LFUN_CLIPBOARD_PASTE:
2081 enable = !theClipboard().empty();
2084 case LFUN_PRIMARY_SELECTION_PASTE:
2085 enable = cur.selection() || !theSelection().empty();
2088 case LFUN_PARAGRAPH_MOVE_UP:
2089 enable = cur.pit() > 0 && !cur.selection();
2092 case LFUN_PARAGRAPH_MOVE_DOWN:
2093 enable = cur.pit() < cur.lastpit() && !cur.selection();
2096 case LFUN_INSET_DISSOLVE:
2097 if (!cmd.argument().empty()) {
2098 InsetLayout il = cur.inset().getLayout(cur.buffer().params());
2099 enable = cur.inset().lyxCode() == FLEX_CODE
2100 && il.lyxtype() == to_utf8(cmd.argument());
2102 enable = !isMainText(cur.bv().buffer())
2103 && cur.inset().nargs() == 1;
2107 case LFUN_CHANGE_ACCEPT:
2108 case LFUN_CHANGE_REJECT:
2109 // TODO: context-sensitive enabling of LFUN_CHANGE_ACCEPT/REJECT
2110 // In principle, these LFUNs should only be enabled if there
2111 // is a change at the current position/in the current selection.
2112 // However, without proper optimizations, this will inevitably
2113 // result in unacceptable performance - just imagine a user who
2114 // wants to select the complete content of a long document.
2118 case LFUN_OUTLINE_UP:
2119 case LFUN_OUTLINE_DOWN:
2120 case LFUN_OUTLINE_IN:
2121 case LFUN_OUTLINE_OUT:
2122 enable = (cur.paragraph().layout()->toclevel != Layout::NOT_IN_TOC);
2125 case LFUN_WORD_DELETE_FORWARD:
2126 case LFUN_WORD_DELETE_BACKWARD:
2127 case LFUN_LINE_DELETE:
2128 case LFUN_WORD_FORWARD:
2129 case LFUN_WORD_BACKWARD:
2130 case LFUN_WORD_RIGHT:
2131 case LFUN_WORD_LEFT:
2132 case LFUN_CHAR_FORWARD:
2133 case LFUN_CHAR_FORWARD_SELECT:
2134 case LFUN_CHAR_BACKWARD:
2135 case LFUN_CHAR_BACKWARD_SELECT:
2136 case LFUN_CHAR_LEFT:
2137 case LFUN_CHAR_LEFT_SELECT:
2138 case LFUN_CHAR_RIGHT:
2139 case LFUN_CHAR_RIGHT_SELECT:
2141 case LFUN_UP_SELECT:
2143 case LFUN_DOWN_SELECT:
2144 case LFUN_PARAGRAPH_UP_SELECT:
2145 case LFUN_PARAGRAPH_DOWN_SELECT:
2146 case LFUN_SCREEN_UP_SELECT:
2147 case LFUN_SCREEN_DOWN_SELECT:
2148 case LFUN_LINE_BEGIN_SELECT:
2149 case LFUN_LINE_END_SELECT:
2150 case LFUN_WORD_FORWARD_SELECT:
2151 case LFUN_WORD_BACKWARD_SELECT:
2152 case LFUN_WORD_RIGHT_SELECT:
2153 case LFUN_WORD_LEFT_SELECT:
2154 case LFUN_WORD_SELECT:
2155 case LFUN_PARAGRAPH_UP:
2156 case LFUN_PARAGRAPH_DOWN:
2157 case LFUN_LINE_BEGIN:
2158 case LFUN_LINE_BREAK:
2161 case LFUN_CHAR_DELETE_FORWARD:
2162 case LFUN_DELETE_FORWARD_SKIP:
2163 case LFUN_CHAR_DELETE_BACKWARD:
2164 case LFUN_DELETE_BACKWARD_SKIP:
2165 case LFUN_BREAK_PARAGRAPH:
2166 case LFUN_BREAK_PARAGRAPH_SKIP:
2167 case LFUN_PARAGRAPH_SPACING:
2168 case LFUN_INSET_INSERT:
2169 case LFUN_WORD_UPCASE:
2170 case LFUN_WORD_LOWCASE:
2171 case LFUN_WORD_CAPITALIZE:
2172 case LFUN_CHARS_TRANSPOSE:
2173 case LFUN_SERVER_GET_XY:
2174 case LFUN_SERVER_SET_XY:
2175 case LFUN_SERVER_GET_FONT:
2176 case LFUN_SERVER_GET_LAYOUT:
2178 case LFUN_DATE_INSERT:
2179 case LFUN_SELF_INSERT:
2180 case LFUN_LINE_INSERT:
2181 case LFUN_NEWPAGE_INSERT:
2182 case LFUN_PAGEBREAK_INSERT:
2183 case LFUN_CLEARPAGE_INSERT:
2184 case LFUN_CLEARDOUBLEPAGE_INSERT:
2185 case LFUN_MATH_DISPLAY:
2186 case LFUN_MATH_IMPORT_SELECTION:
2187 case LFUN_MATH_MODE:
2188 case LFUN_MATH_MACRO:
2189 case LFUN_MATH_MATRIX:
2190 case LFUN_MATH_DELIM:
2191 case LFUN_MATH_BIGDELIM:
2192 case LFUN_MATH_INSERT:
2193 case LFUN_MATH_SUBSCRIPT:
2194 case LFUN_MATH_SUPERSCRIPT:
2195 case LFUN_FONT_DEFAULT:
2196 case LFUN_FONT_UNDERLINE:
2197 case LFUN_FONT_SIZE:
2199 case LFUN_FONT_FREE_APPLY:
2200 case LFUN_FONT_FREE_UPDATE:
2201 case LFUN_LAYOUT_PARAGRAPH:
2202 case LFUN_PARAGRAPH_UPDATE:
2203 case LFUN_ACCENT_UMLAUT:
2204 case LFUN_ACCENT_CIRCUMFLEX:
2205 case LFUN_ACCENT_GRAVE:
2206 case LFUN_ACCENT_ACUTE:
2207 case LFUN_ACCENT_TILDE:
2208 case LFUN_ACCENT_CEDILLA:
2209 case LFUN_ACCENT_MACRON:
2210 case LFUN_ACCENT_DOT:
2211 case LFUN_ACCENT_UNDERDOT:
2212 case LFUN_ACCENT_UNDERBAR:
2213 case LFUN_ACCENT_CARON:
2214 case LFUN_ACCENT_SPECIAL_CARON:
2215 case LFUN_ACCENT_BREVE:
2216 case LFUN_ACCENT_TIE:
2217 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
2218 case LFUN_ACCENT_CIRCLE:
2219 case LFUN_ACCENT_OGONEK:
2220 case LFUN_THESAURUS_ENTRY:
2221 case LFUN_PARAGRAPH_PARAMS_APPLY:
2222 case LFUN_PARAGRAPH_PARAMS:
2224 case LFUN_BUFFER_END:
2225 case LFUN_BUFFER_BEGIN:
2226 case LFUN_BUFFER_BEGIN_SELECT:
2227 case LFUN_BUFFER_END_SELECT:
2228 case LFUN_UNICODE_INSERT:
2229 // these are handled in our dispatch()
2238 && (cur.empty() || !cur.inset().insetAllowed(code)))
2241 flag.enabled(enable);
2246 void Text::pasteString(Cursor & cur, docstring const & clip,
2249 cur.clearSelection();
2250 if (!clip.empty()) {
2253 insertStringAsParagraphs(cur, clip);
2255 insertStringAsLines(cur, clip);